12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * IPv6 input 41da177e4SLinus Torvalds * Linux INET6 implementation 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Authors: 71da177e4SLinus Torvalds * Pedro Roque <[email protected]> 81da177e4SLinus Torvalds * Ian P. Morris <[email protected]> 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * Based in linux/net/ipv4/ip_input.c 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds /* Changes 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Mitsuru KANDA @USAGI and 151da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI: Remove ipv6_parse_exthdrs(). 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds #include <linux/errno.h> 191da177e4SLinus Torvalds #include <linux/types.h> 201da177e4SLinus Torvalds #include <linux/socket.h> 211da177e4SLinus Torvalds #include <linux/sockios.h> 221da177e4SLinus Torvalds #include <linux/net.h> 231da177e4SLinus Torvalds #include <linux/netdevice.h> 241da177e4SLinus Torvalds #include <linux/in6.h> 251da177e4SLinus Torvalds #include <linux/icmpv6.h> 267bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 275a0e3ad6STejun Heo #include <linux/slab.h> 280e219ae4SPaolo Abeni #include <linux/indirect_call_wrapper.h> 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds #include <linux/netfilter.h> 311da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds #include <net/sock.h> 341da177e4SLinus Torvalds #include <net/snmp.h> 35f9a4719cSLeon Romanovsky #include <net/udp.h> 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds #include <net/ipv6.h> 381da177e4SLinus Torvalds #include <net/protocol.h> 391da177e4SLinus Torvalds #include <net/transp_v6.h> 401da177e4SLinus Torvalds #include <net/rawv6.h> 411da177e4SLinus Torvalds #include <net/ndisc.h> 421da177e4SLinus Torvalds #include <net/ip6_route.h> 431da177e4SLinus Torvalds #include <net/addrconf.h> 441da177e4SLinus Torvalds #include <net/xfrm.h> 451f07d03eSEric Dumazet #include <net/inet_ecn.h> 4648fb6b55SWei-Chun Chao #include <net/dst_metadata.h> 471da177e4SLinus Torvalds 4897ff7ffbSPaolo Abeni INDIRECT_CALLABLE_DECLARE(void tcp_v6_early_demux(struct sk_buff *)); 49d8269e2cSEdward Cree static void ip6_rcv_finish_core(struct net *net, struct sock *sk, 50d8269e2cSEdward Cree struct sk_buff *skb) 511da177e4SLinus Torvalds { 52dddb64bcS[email protected] void (*edemux)(struct sk_buff *skb); 53dddb64bcS[email protected] 54e21145a9SNikolay Borisov if (net->ipv4.sysctl_ip_early_demux && !skb_dst(skb) && skb->sk == NULL) { 55c7109986SEric Dumazet const struct inet6_protocol *ipprot; 56c7109986SEric Dumazet 57c7109986SEric Dumazet ipprot = rcu_dereference(inet6_protos[ipv6_hdr(skb)->nexthdr]); 58dddb64bcS[email protected] if (ipprot && (edemux = READ_ONCE(ipprot->early_demux))) 5997ff7ffbSPaolo Abeni INDIRECT_CALL_2(edemux, tcp_v6_early_demux, 6097ff7ffbSPaolo Abeni udp_v6_early_demux, skb); 61c7109986SEric Dumazet } 6248fb6b55SWei-Chun Chao if (!skb_valid_dst(skb)) 631da177e4SLinus Torvalds ip6_route_input(skb); 64d8269e2cSEdward Cree } 65d8269e2cSEdward Cree 66d8269e2cSEdward Cree int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb) 67d8269e2cSEdward Cree { 68d8269e2cSEdward Cree /* if ingress device is enslaved to an L3 master device pass the 69d8269e2cSEdward Cree * skb to its handler for processing 70d8269e2cSEdward Cree */ 71d8269e2cSEdward Cree skb = l3mdev_ip6_rcv(skb); 72d8269e2cSEdward Cree if (!skb) 73d8269e2cSEdward Cree return NET_RX_SUCCESS; 74d8269e2cSEdward Cree ip6_rcv_finish_core(net, sk, skb); 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds return dst_input(skb); 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds 79d8269e2cSEdward Cree static void ip6_sublist_rcv_finish(struct list_head *head) 80d8269e2cSEdward Cree { 81d8269e2cSEdward Cree struct sk_buff *skb, *next; 82d8269e2cSEdward Cree 83c7a42eb4SXin Long list_for_each_entry_safe(skb, next, head, list) { 84c7a42eb4SXin Long skb_list_del_init(skb); 85d8269e2cSEdward Cree dst_input(skb); 86d8269e2cSEdward Cree } 87c7a42eb4SXin Long } 88d8269e2cSEdward Cree 89197dbf24SPaolo Abeni static bool ip6_can_use_hint(const struct sk_buff *skb, 90197dbf24SPaolo Abeni const struct sk_buff *hint) 91197dbf24SPaolo Abeni { 92197dbf24SPaolo Abeni return hint && !skb_dst(skb) && 93197dbf24SPaolo Abeni ipv6_addr_equal(&ipv6_hdr(hint)->daddr, &ipv6_hdr(skb)->daddr); 94197dbf24SPaolo Abeni } 95197dbf24SPaolo Abeni 96197dbf24SPaolo Abeni static struct sk_buff *ip6_extract_route_hint(const struct net *net, 97197dbf24SPaolo Abeni struct sk_buff *skb) 98197dbf24SPaolo Abeni { 99197dbf24SPaolo Abeni if (fib6_routes_require_src(net) || fib6_has_custom_rules(net)) 100197dbf24SPaolo Abeni return NULL; 101197dbf24SPaolo Abeni 102197dbf24SPaolo Abeni return skb; 103197dbf24SPaolo Abeni } 104197dbf24SPaolo Abeni 105d8269e2cSEdward Cree static void ip6_list_rcv_finish(struct net *net, struct sock *sk, 106d8269e2cSEdward Cree struct list_head *head) 107d8269e2cSEdward Cree { 108197dbf24SPaolo Abeni struct sk_buff *skb, *next, *hint = NULL; 109d8269e2cSEdward Cree struct dst_entry *curr_dst = NULL; 110d8269e2cSEdward Cree struct list_head sublist; 111d8269e2cSEdward Cree 112d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist); 113d8269e2cSEdward Cree list_for_each_entry_safe(skb, next, head, list) { 114d8269e2cSEdward Cree struct dst_entry *dst; 115d8269e2cSEdward Cree 11622f6bbb7SEdward Cree skb_list_del_init(skb); 117d8269e2cSEdward Cree /* if ingress device is enslaved to an L3 master device pass the 118d8269e2cSEdward Cree * skb to its handler for processing 119d8269e2cSEdward Cree */ 120d8269e2cSEdward Cree skb = l3mdev_ip6_rcv(skb); 121d8269e2cSEdward Cree if (!skb) 122d8269e2cSEdward Cree continue; 123197dbf24SPaolo Abeni 124197dbf24SPaolo Abeni if (ip6_can_use_hint(skb, hint)) 125197dbf24SPaolo Abeni skb_dst_copy(skb, hint); 126197dbf24SPaolo Abeni else 127d8269e2cSEdward Cree ip6_rcv_finish_core(net, sk, skb); 128d8269e2cSEdward Cree dst = skb_dst(skb); 129d8269e2cSEdward Cree if (curr_dst != dst) { 130197dbf24SPaolo Abeni hint = ip6_extract_route_hint(net, skb); 131197dbf24SPaolo Abeni 132d8269e2cSEdward Cree /* dispatch old sublist */ 133d8269e2cSEdward Cree if (!list_empty(&sublist)) 134d8269e2cSEdward Cree ip6_sublist_rcv_finish(&sublist); 135d8269e2cSEdward Cree /* start new sublist */ 136d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist); 137d8269e2cSEdward Cree curr_dst = dst; 138d8269e2cSEdward Cree } 139d8269e2cSEdward Cree list_add_tail(&skb->list, &sublist); 140d8269e2cSEdward Cree } 141d8269e2cSEdward Cree /* dispatch final sublist */ 142d8269e2cSEdward Cree ip6_sublist_rcv_finish(&sublist); 143d8269e2cSEdward Cree } 144d8269e2cSEdward Cree 145d8269e2cSEdward Cree static struct sk_buff *ip6_rcv_core(struct sk_buff *skb, struct net_device *dev, 146d8269e2cSEdward Cree struct net *net) 1471da177e4SLinus Torvalds { 148b71d1d42SEric Dumazet const struct ipv6hdr *hdr; 1491da177e4SLinus Torvalds u32 pkt_len; 150a11d206dSYOSHIFUJI Hideaki struct inet6_dev *idev; 1511da177e4SLinus Torvalds 152a11d206dSYOSHIFUJI Hideaki if (skb->pkt_type == PACKET_OTHERHOST) { 153a11d206dSYOSHIFUJI Hideaki kfree_skb(skb); 154d8269e2cSEdward Cree return NULL; 155a11d206dSYOSHIFUJI Hideaki } 1561da177e4SLinus Torvalds 157a11d206dSYOSHIFUJI Hideaki rcu_read_lock(); 158a11d206dSYOSHIFUJI Hideaki 159a11d206dSYOSHIFUJI Hideaki idev = __in6_dev_get(skb->dev); 160a11d206dSYOSHIFUJI Hideaki 161c2005eb0SEric Dumazet __IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_IN, skb->len); 1621da177e4SLinus Torvalds 163778d80beSYOSHIFUJI Hideaki if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL || 164778d80beSYOSHIFUJI Hideaki !idev || unlikely(idev->cnf.disable_ipv6)) { 1651d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 16671f6f6dfSJesper Nilsson goto drop; 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds 1696b7fdc3aSGuillaume Chazarain memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); 1706b7fdc3aSGuillaume Chazarain 1711da177e4SLinus Torvalds /* 1721da177e4SLinus Torvalds * Store incoming device index. When the packet will 1731da177e4SLinus Torvalds * be queued, we cannot refer to skb->dev anymore. 1741da177e4SLinus Torvalds * 1751da177e4SLinus Torvalds * BTW, when we send a packet for our own local address on a 1761da177e4SLinus Torvalds * non-loopback interface (e.g. ethX), it is being delivered 177de3cb747SDaniel Lezcano * via the loopback interface (lo) here; skb->dev = loopback_dev. 1781da177e4SLinus Torvalds * It, however, should be considered as if it is being 1791da177e4SLinus Torvalds * arrived via the sending interface (ethX), because of the 1801da177e4SLinus Torvalds * nature of scoping architecture. --yoshfuji 1811da177e4SLinus Torvalds */ 18248fb6b55SWei-Chun Chao IP6CB(skb)->iif = skb_valid_dst(skb) ? ip6_dst_idev(skb_dst(skb))->dev->ifindex : dev->ifindex; 1831da177e4SLinus Torvalds 1842889139aSHerbert Xu if (unlikely(!pskb_may_pull(skb, sizeof(*hdr)))) 1851da177e4SLinus Torvalds goto err; 1861da177e4SLinus Torvalds 1870660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds if (hdr->version != 6) 1901da177e4SLinus Torvalds goto err; 1911da177e4SLinus Torvalds 1921d015503SEric Dumazet __IP6_ADD_STATS(net, idev, 1931f07d03eSEric Dumazet IPSTATS_MIB_NOECTPKTS + 1941f07d03eSEric Dumazet (ipv6_get_dsfield(hdr) & INET_ECN_MASK), 1951f07d03eSEric Dumazet max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs)); 196f630e43aSYOSHIFUJI Hideaki /* 197f630e43aSYOSHIFUJI Hideaki * RFC4291 2.5.3 1980aa8c13eSFlorian Westphal * The loopback address must not be used as the source address in IPv6 1990aa8c13eSFlorian Westphal * packets that are sent outside of a single node. [..] 200f630e43aSYOSHIFUJI Hideaki * A packet received on an interface with a destination address 201f630e43aSYOSHIFUJI Hideaki * of loopback must be dropped. 202f630e43aSYOSHIFUJI Hideaki */ 2030aa8c13eSFlorian Westphal if ((ipv6_addr_loopback(&hdr->saddr) || 2040aa8c13eSFlorian Westphal ipv6_addr_loopback(&hdr->daddr)) && 2053ede0bbcSRobert Shearman !(dev->flags & IFF_LOOPBACK) && 2063ede0bbcSRobert Shearman !netif_is_l3_master(dev)) 207f630e43aSYOSHIFUJI Hideaki goto err; 208f630e43aSYOSHIFUJI Hideaki 2091c4a154eSHannes Frederic Sowa /* RFC4291 Errata ID: 3480 2101c4a154eSHannes Frederic Sowa * Interface-Local scope spans only a single interface on a 2111c4a154eSHannes Frederic Sowa * node and is useful only for loopback transmission of 2121c4a154eSHannes Frederic Sowa * multicast. Packets with interface-local scope received 2131c4a154eSHannes Frederic Sowa * from another node must be discarded. 2141c4a154eSHannes Frederic Sowa */ 2151c4a154eSHannes Frederic Sowa if (!(skb->pkt_type == PACKET_LOOPBACK || 2161c4a154eSHannes Frederic Sowa dev->flags & IFF_LOOPBACK) && 2171c4a154eSHannes Frederic Sowa ipv6_addr_is_multicast(&hdr->daddr) && 2181c4a154eSHannes Frederic Sowa IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 1) 2191c4a154eSHannes Frederic Sowa goto err; 2201c4a154eSHannes Frederic Sowa 221abbc3043SJohannes Berg /* If enabled, drop unicast packets that were encapsulated in link-layer 222abbc3043SJohannes Berg * multicast or broadcast to protected against the so-called "hole-196" 223abbc3043SJohannes Berg * attack in 802.11 wireless. 224abbc3043SJohannes Berg */ 225abbc3043SJohannes Berg if (!ipv6_addr_is_multicast(&hdr->daddr) && 226abbc3043SJohannes Berg (skb->pkt_type == PACKET_BROADCAST || 227abbc3043SJohannes Berg skb->pkt_type == PACKET_MULTICAST) && 228abbc3043SJohannes Berg idev->cnf.drop_unicast_in_l2_multicast) 229abbc3043SJohannes Berg goto err; 230abbc3043SJohannes Berg 23120314092SHannes Frederic Sowa /* RFC4291 2.7 23220314092SHannes Frederic Sowa * Nodes must not originate a packet to a multicast address whose scope 23320314092SHannes Frederic Sowa * field contains the reserved value 0; if such a packet is received, it 23420314092SHannes Frederic Sowa * must be silently dropped. 23520314092SHannes Frederic Sowa */ 23620314092SHannes Frederic Sowa if (ipv6_addr_is_multicast(&hdr->daddr) && 23720314092SHannes Frederic Sowa IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 0) 23820314092SHannes Frederic Sowa goto err; 23920314092SHannes Frederic Sowa 240c457338dSBrian Haley /* 241c457338dSBrian Haley * RFC4291 2.7 242c457338dSBrian Haley * Multicast addresses must not be used as source addresses in IPv6 243c457338dSBrian Haley * packets or appear in any Routing header. 244c457338dSBrian Haley */ 245c457338dSBrian Haley if (ipv6_addr_is_multicast(&hdr->saddr)) 246c457338dSBrian Haley goto err; 247c457338dSBrian Haley 248b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(*hdr); 249951dbc8aSPatrick McHardy IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr); 250951dbc8aSPatrick McHardy 2511da177e4SLinus Torvalds pkt_len = ntohs(hdr->payload_len); 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds /* pkt_len may be zero if Jumbo payload option is present */ 2541da177e4SLinus Torvalds if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) { 25560e5c166SMitsuru Chinen if (pkt_len + sizeof(struct ipv6hdr) > skb->len) { 2561d015503SEric Dumazet __IP6_INC_STATS(net, 257483a47d2SDenis V. Lunev idev, IPSTATS_MIB_INTRUNCATEDPKTS); 25860e5c166SMitsuru Chinen goto drop; 25960e5c166SMitsuru Chinen } 2601da177e4SLinus Torvalds if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) { 2611d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 2621da177e4SLinus Torvalds goto drop; 2631da177e4SLinus Torvalds } 2640660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds if (hdr->nexthdr == NEXTHDR_HOP) { 268e5bbef20SHerbert Xu if (ipv6_parse_hopopts(skb) < 0) { 2691d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 270a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 271d8269e2cSEdward Cree return NULL; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 275a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 276a11d206dSYOSHIFUJI Hideaki 27771f9dacdSHerbert Xu /* Must drop socket now because of tproxy. */ 278cf7fbe66SJoe Stringer if (!skb_sk_is_prefetched(skb)) 27971f9dacdSHerbert Xu skb_orphan(skb); 28071f9dacdSHerbert Xu 281d8269e2cSEdward Cree return skb; 2821da177e4SLinus Torvalds err: 2831d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 2841da177e4SLinus Torvalds drop: 285a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 2861da177e4SLinus Torvalds kfree_skb(skb); 287d8269e2cSEdward Cree return NULL; 288d8269e2cSEdward Cree } 289d8269e2cSEdward Cree 290d8269e2cSEdward Cree int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) 291d8269e2cSEdward Cree { 292d8269e2cSEdward Cree struct net *net = dev_net(skb->dev); 293d8269e2cSEdward Cree 294d8269e2cSEdward Cree skb = ip6_rcv_core(skb, dev, net); 295d8269e2cSEdward Cree if (skb == NULL) 2965c91faceSMark Smith return NET_RX_DROP; 297d8269e2cSEdward Cree return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, 298d8269e2cSEdward Cree net, NULL, skb, dev, NULL, 299d8269e2cSEdward Cree ip6_rcv_finish); 300d8269e2cSEdward Cree } 301d8269e2cSEdward Cree 302d8269e2cSEdward Cree static void ip6_sublist_rcv(struct list_head *head, struct net_device *dev, 303d8269e2cSEdward Cree struct net *net) 304d8269e2cSEdward Cree { 305d8269e2cSEdward Cree NF_HOOK_LIST(NFPROTO_IPV6, NF_INET_PRE_ROUTING, net, NULL, 306d8269e2cSEdward Cree head, dev, NULL, ip6_rcv_finish); 307d8269e2cSEdward Cree ip6_list_rcv_finish(net, NULL, head); 308d8269e2cSEdward Cree } 309d8269e2cSEdward Cree 310d8269e2cSEdward Cree /* Receive a list of IPv6 packets */ 311d8269e2cSEdward Cree void ipv6_list_rcv(struct list_head *head, struct packet_type *pt, 312d8269e2cSEdward Cree struct net_device *orig_dev) 313d8269e2cSEdward Cree { 314d8269e2cSEdward Cree struct net_device *curr_dev = NULL; 315d8269e2cSEdward Cree struct net *curr_net = NULL; 316d8269e2cSEdward Cree struct sk_buff *skb, *next; 317d8269e2cSEdward Cree struct list_head sublist; 318d8269e2cSEdward Cree 319d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist); 320d8269e2cSEdward Cree list_for_each_entry_safe(skb, next, head, list) { 321d8269e2cSEdward Cree struct net_device *dev = skb->dev; 322d8269e2cSEdward Cree struct net *net = dev_net(dev); 323d8269e2cSEdward Cree 32422f6bbb7SEdward Cree skb_list_del_init(skb); 325d8269e2cSEdward Cree skb = ip6_rcv_core(skb, dev, net); 326d8269e2cSEdward Cree if (skb == NULL) 327d8269e2cSEdward Cree continue; 328d8269e2cSEdward Cree 329d8269e2cSEdward Cree if (curr_dev != dev || curr_net != net) { 330d8269e2cSEdward Cree /* dispatch old sublist */ 331d8269e2cSEdward Cree if (!list_empty(&sublist)) 332d8269e2cSEdward Cree ip6_sublist_rcv(&sublist, curr_dev, curr_net); 333d8269e2cSEdward Cree /* start new sublist */ 334d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist); 335d8269e2cSEdward Cree curr_dev = dev; 336d8269e2cSEdward Cree curr_net = net; 337d8269e2cSEdward Cree } 338d8269e2cSEdward Cree list_add_tail(&skb->list, &sublist); 339d8269e2cSEdward Cree } 340d8269e2cSEdward Cree /* dispatch final sublist */ 34151210ad5SFlorian Westphal if (!list_empty(&sublist)) 342d8269e2cSEdward Cree ip6_sublist_rcv(&sublist, curr_dev, curr_net); 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3450e219ae4SPaolo Abeni INDIRECT_CALLABLE_DECLARE(int tcp_v6_rcv(struct sk_buff *)); 3460e219ae4SPaolo Abeni 3471da177e4SLinus Torvalds /* 3481da177e4SLinus Torvalds * Deliver the packet to the host 3491da177e4SLinus Torvalds */ 35080bde363SPaolo Abeni void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr, 35180bde363SPaolo Abeni bool have_final) 3521da177e4SLinus Torvalds { 35341135cc8SAlexey Dobriyan const struct inet6_protocol *ipprot; 354f9242b6bSDavid S. Miller struct inet6_dev *idev; 3551da177e4SLinus Torvalds unsigned int nhoff; 356a50feda5SEric Dumazet bool raw; 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds /* 3591da177e4SLinus Torvalds * Parse extension headers 3601da177e4SLinus Torvalds */ 3611da177e4SLinus Torvalds 3621b0ccfe5SDavid S. Miller resubmit: 363adf30907SEric Dumazet idev = ip6_dst_idev(skb_dst(skb)); 36480bde363SPaolo Abeni nhoff = IP6CB(skb)->nhoff; 36580bde363SPaolo Abeni if (!have_final) { 366ea2ae17dSArnaldo Carvalho de Melo if (!pskb_pull(skb, skb_transport_offset(skb))) 3671da177e4SLinus Torvalds goto discard; 368d56f90a7SArnaldo Carvalho de Melo nexthdr = skb_network_header(skb)[nhoff]; 36980bde363SPaolo Abeni } 3701da177e4SLinus Torvalds 3714c64242aSTom Herbert resubmit_final: 37269d6da0bSPavel Emelyanov raw = raw6_local_deliver(skb, nexthdr); 373e5d08d71SIan Morris ipprot = rcu_dereference(inet6_protos[nexthdr]); 37453b24b8fSIan Morris if (ipprot) { 3751da177e4SLinus Torvalds int ret; 3761da177e4SLinus Torvalds 3771da44f9cSTom Herbert if (have_final) { 3781da44f9cSTom Herbert if (!(ipprot->flags & INET6_PROTO_FINAL)) { 3791da44f9cSTom Herbert /* Once we've seen a final protocol don't 3801da44f9cSTom Herbert * allow encapsulation on any non-final 3811da44f9cSTom Herbert * ones. This allows foo in UDP encapsulation 3821da44f9cSTom Herbert * to work. 3831da44f9cSTom Herbert */ 3841da44f9cSTom Herbert goto discard; 3851da44f9cSTom Herbert } 3861da44f9cSTom Herbert } else if (ipprot->flags & INET6_PROTO_FINAL) { 387b71d1d42SEric Dumazet const struct ipv6hdr *hdr; 3885226b6a9SMike Manning int sdif = inet6_sdif(skb); 3895226b6a9SMike Manning struct net_device *dev; 3901da177e4SLinus Torvalds 3911da44f9cSTom Herbert /* Only do this once for first final protocol */ 3921da44f9cSTom Herbert have_final = true; 3931da44f9cSTom Herbert 3949fb9cbb1SYasuyuki Kozakai /* Free reference early: we don't need it any more, 3959fb9cbb1SYasuyuki Kozakai and it may hold ip_conntrack module loaded 3969fb9cbb1SYasuyuki Kozakai indefinitely. */ 397895b5c9fSFlorian Westphal nf_reset_ct(skb); 3989fb9cbb1SYasuyuki Kozakai 399d56f90a7SArnaldo Carvalho de Melo skb_postpull_rcsum(skb, skb_network_header(skb), 400cfe1fc77SArnaldo Carvalho de Melo skb_network_header_len(skb)); 4010660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 4025226b6a9SMike Manning 4035226b6a9SMike Manning /* skb->dev passed may be master dev for vrfs. */ 4045226b6a9SMike Manning if (sdif) { 4055226b6a9SMike Manning dev = dev_get_by_index_rcu(net, sdif); 4065226b6a9SMike Manning if (!dev) 4075226b6a9SMike Manning goto discard; 4085226b6a9SMike Manning } else { 4095226b6a9SMike Manning dev = skb->dev; 4105226b6a9SMike Manning } 4115226b6a9SMike Manning 4121da177e4SLinus Torvalds if (ipv6_addr_is_multicast(&hdr->daddr) && 4135226b6a9SMike Manning !ipv6_chk_mcast_addr(dev, &hdr->daddr, 4141da177e4SLinus Torvalds &hdr->saddr) && 415daad1512SYOSHIFUJI Hideaki / 吉藤英明 !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb))) 4161da177e4SLinus Torvalds goto discard; 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds if (!(ipprot->flags & INET6_PROTO_NOPOLICY) && 4191da177e4SLinus Torvalds !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) 4201da177e4SLinus Torvalds goto discard; 4211da177e4SLinus Torvalds 4220e219ae4SPaolo Abeni ret = INDIRECT_CALL_2(ipprot->handler, tcp_v6_rcv, udpv6_rcv, 4230e219ae4SPaolo Abeni skb); 4244c64242aSTom Herbert if (ret > 0) { 4254c64242aSTom Herbert if (ipprot->flags & INET6_PROTO_FINAL) { 4264c64242aSTom Herbert /* Not an extension header, most likely UDP 4274c64242aSTom Herbert * encapsulation. Use return value as nexthdr 4284c64242aSTom Herbert * protocol not nhoff (which presumably is 4294c64242aSTom Herbert * not set by handler). 4304c64242aSTom Herbert */ 4314c64242aSTom Herbert nexthdr = ret; 4324c64242aSTom Herbert goto resubmit_final; 4334c64242aSTom Herbert } else { 4341da177e4SLinus Torvalds goto resubmit; 4354c64242aSTom Herbert } 4364c64242aSTom Herbert } else if (ret == 0) { 4371d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS); 4384c64242aSTom Herbert } 4391da177e4SLinus Torvalds } else { 44069d6da0bSPavel Emelyanov if (!raw) { 4411da177e4SLinus Torvalds if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { 4421d015503SEric Dumazet __IP6_INC_STATS(net, idev, 443483a47d2SDenis V. Lunev IPSTATS_MIB_INUNKNOWNPROTOS); 444fad87acaSPatrick McHardy icmpv6_send(skb, ICMPV6_PARAMPROB, 4453ffe533cSAlexey Dobriyan ICMPV6_UNK_NEXTHDR, nhoff); 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds kfree_skb(skb); 448d8c6f4b9SNeil Horman } else { 4491d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS); 450d8c6f4b9SNeil Horman consume_skb(skb); 451d8c6f4b9SNeil Horman } 4521da177e4SLinus Torvalds } 45380bde363SPaolo Abeni return; 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds discard: 4561d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 4571da177e4SLinus Torvalds kfree_skb(skb); 45880bde363SPaolo Abeni } 45980bde363SPaolo Abeni 46080bde363SPaolo Abeni static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb) 46180bde363SPaolo Abeni { 46280bde363SPaolo Abeni rcu_read_lock(); 46380bde363SPaolo Abeni ip6_protocol_deliver_rcu(net, skb, 0, false); 46480bde363SPaolo Abeni rcu_read_unlock(); 46580bde363SPaolo Abeni 4661da177e4SLinus Torvalds return 0; 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds int ip6_input(struct sk_buff *skb) 4711da177e4SLinus Torvalds { 47229a26a56SEric W. Biederman return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN, 47329a26a56SEric W. Biederman dev_net(skb->dev), NULL, skb, skb->dev, NULL, 4746e23ae2aSPatrick McHardy ip6_input_finish); 4751da177e4SLinus Torvalds } 476b4869aa2SDavid Ahern EXPORT_SYMBOL_GPL(ip6_input); 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds int ip6_mc_input(struct sk_buff *skb) 4791da177e4SLinus Torvalds { 4805226b6a9SMike Manning int sdif = inet6_sdif(skb); 481b71d1d42SEric Dumazet const struct ipv6hdr *hdr; 4825226b6a9SMike Manning struct net_device *dev; 483a50feda5SEric Dumazet bool deliver; 4841da177e4SLinus Torvalds 485c2005eb0SEric Dumazet __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev), 486bdb7cc64SStephen Suryaputra __in6_dev_get_safely(skb->dev), IPSTATS_MIB_INMCAST, 487edf391ffSNeil Horman skb->len); 4881da177e4SLinus Torvalds 4895226b6a9SMike Manning /* skb->dev passed may be master dev for vrfs. */ 4905226b6a9SMike Manning if (sdif) { 4915226b6a9SMike Manning rcu_read_lock(); 4925226b6a9SMike Manning dev = dev_get_by_index_rcu(dev_net(skb->dev), sdif); 4935226b6a9SMike Manning if (!dev) { 4945226b6a9SMike Manning rcu_read_unlock(); 4955226b6a9SMike Manning kfree_skb(skb); 4965226b6a9SMike Manning return -ENODEV; 4975226b6a9SMike Manning } 4985226b6a9SMike Manning } else { 4995226b6a9SMike Manning dev = skb->dev; 5005226b6a9SMike Manning } 5015226b6a9SMike Manning 5020660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 5035226b6a9SMike Manning deliver = ipv6_chk_mcast_addr(dev, &hdr->daddr, NULL); 5045226b6a9SMike Manning if (sdif) 5055226b6a9SMike Manning rcu_read_unlock(); 5061da177e4SLinus Torvalds 5077bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 5081da177e4SLinus Torvalds /* 5097bc570c8SYOSHIFUJI Hideaki * IPv6 multicast router mode is now supported ;) 5101da177e4SLinus Torvalds */ 511*145c7a79SEric Dumazet if (atomic_read(&dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding) && 512ddf64354SHannes Frederic Sowa !(ipv6_addr_type(&hdr->daddr) & 513ddf64354SHannes Frederic Sowa (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) && 5147bc570c8SYOSHIFUJI Hideaki likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) { 5157bc570c8SYOSHIFUJI Hideaki /* 5167bc570c8SYOSHIFUJI Hideaki * Okay, we try to forward - split and duplicate 5177bc570c8SYOSHIFUJI Hideaki * packets. 5187bc570c8SYOSHIFUJI Hideaki */ 5191da177e4SLinus Torvalds struct sk_buff *skb2; 5207bc570c8SYOSHIFUJI Hideaki struct inet6_skb_parm *opt = IP6CB(skb); 5211da177e4SLinus Torvalds 5227bc570c8SYOSHIFUJI Hideaki /* Check for MLD */ 523dd3332bfSYOSHIFUJI Hideaki / 吉藤英明 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) { 5247bc570c8SYOSHIFUJI Hideaki /* Check if this is a mld message */ 5257bc570c8SYOSHIFUJI Hideaki u8 nexthdr = hdr->nexthdr; 52675f2811cSJesse Gross __be16 frag_off; 5277bc570c8SYOSHIFUJI Hideaki int offset; 5281da177e4SLinus Torvalds 5297bc570c8SYOSHIFUJI Hideaki /* Check if the value of Router Alert 5307bc570c8SYOSHIFUJI Hideaki * is for MLD (0x0000). 5317bc570c8SYOSHIFUJI Hideaki */ 532dd3332bfSYOSHIFUJI Hideaki / 吉藤英明 if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) { 533a50feda5SEric Dumazet deliver = false; 534aba6096bSYOSHIFUJI Hideaki 5357bc570c8SYOSHIFUJI Hideaki if (!ipv6_ext_hdr(nexthdr)) { 5367bc570c8SYOSHIFUJI Hideaki /* BUG */ 537aba6096bSYOSHIFUJI Hideaki goto out; 5381da177e4SLinus Torvalds } 5397bc570c8SYOSHIFUJI Hideaki offset = ipv6_skip_exthdr(skb, sizeof(*hdr), 54075f2811cSJesse Gross &nexthdr, &frag_off); 5417bc570c8SYOSHIFUJI Hideaki if (offset < 0) 542aba6096bSYOSHIFUJI Hideaki goto out; 5437bc570c8SYOSHIFUJI Hideaki 5444c938d22SAngga if (ipv6_is_mld(skb, nexthdr, offset)) 545a50feda5SEric Dumazet deliver = true; 5464c938d22SAngga 5474c938d22SAngga goto out; 5487bc570c8SYOSHIFUJI Hideaki } 5497bc570c8SYOSHIFUJI Hideaki /* unknown RA - process it normally */ 5507bc570c8SYOSHIFUJI Hideaki } 5517bc570c8SYOSHIFUJI Hideaki 5527bc570c8SYOSHIFUJI Hideaki if (deliver) 5537bc570c8SYOSHIFUJI Hideaki skb2 = skb_clone(skb, GFP_ATOMIC); 5547bc570c8SYOSHIFUJI Hideaki else { 5557bc570c8SYOSHIFUJI Hideaki skb2 = skb; 5567bc570c8SYOSHIFUJI Hideaki skb = NULL; 5577bc570c8SYOSHIFUJI Hideaki } 5587bc570c8SYOSHIFUJI Hideaki 5597bc570c8SYOSHIFUJI Hideaki if (skb2) { 5607bc570c8SYOSHIFUJI Hideaki ip6_mr_input(skb2); 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds } 5637bc570c8SYOSHIFUJI Hideaki out: 564aba6096bSYOSHIFUJI Hideaki #endif 565aba6096bSYOSHIFUJI Hideaki if (likely(deliver)) 5661da177e4SLinus Torvalds ip6_input(skb); 567aba6096bSYOSHIFUJI Hideaki else { 5681da177e4SLinus Torvalds /* discard */ 5691da177e4SLinus Torvalds kfree_skb(skb); 570aba6096bSYOSHIFUJI Hideaki } 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds return 0; 5731da177e4SLinus Torvalds } 574