11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * IPv6 input 31da177e4SLinus Torvalds * Linux INET6 implementation 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <[email protected]> 71da177e4SLinus Torvalds * Ian P. Morris <[email protected]> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Based in linux/net/ipv4/ip_input.c 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 121da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 131da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 141da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds /* Changes 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * Mitsuru KANDA @USAGI and 191da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI: Remove ipv6_parse_exthdrs(). 201da177e4SLinus Torvalds */ 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds #include <linux/errno.h> 231da177e4SLinus Torvalds #include <linux/types.h> 241da177e4SLinus Torvalds #include <linux/socket.h> 251da177e4SLinus Torvalds #include <linux/sockios.h> 261da177e4SLinus Torvalds #include <linux/net.h> 271da177e4SLinus Torvalds #include <linux/netdevice.h> 281da177e4SLinus Torvalds #include <linux/in6.h> 291da177e4SLinus Torvalds #include <linux/icmpv6.h> 307bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 315a0e3ad6STejun Heo #include <linux/slab.h> 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds #include <linux/netfilter.h> 341da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds #include <net/sock.h> 371da177e4SLinus Torvalds #include <net/snmp.h> 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #include <net/ipv6.h> 401da177e4SLinus Torvalds #include <net/protocol.h> 411da177e4SLinus Torvalds #include <net/transp_v6.h> 421da177e4SLinus Torvalds #include <net/rawv6.h> 431da177e4SLinus Torvalds #include <net/ndisc.h> 441da177e4SLinus Torvalds #include <net/ip6_route.h> 451da177e4SLinus Torvalds #include <net/addrconf.h> 461da177e4SLinus Torvalds #include <net/xfrm.h> 471f07d03eSEric Dumazet #include <net/inet_ecn.h> 4848fb6b55SWei-Chun Chao #include <net/dst_metadata.h> 491da177e4SLinus Torvalds 50*d8269e2cSEdward Cree static void ip6_rcv_finish_core(struct net *net, struct sock *sk, 51*d8269e2cSEdward Cree struct sk_buff *skb) 521da177e4SLinus Torvalds { 53dddb64bcS[email protected] void (*edemux)(struct sk_buff *skb); 54dddb64bcS[email protected] 55e21145a9SNikolay Borisov if (net->ipv4.sysctl_ip_early_demux && !skb_dst(skb) && skb->sk == NULL) { 56c7109986SEric Dumazet const struct inet6_protocol *ipprot; 57c7109986SEric Dumazet 58c7109986SEric Dumazet ipprot = rcu_dereference(inet6_protos[ipv6_hdr(skb)->nexthdr]); 59dddb64bcS[email protected] if (ipprot && (edemux = READ_ONCE(ipprot->early_demux))) 60dddb64bcS[email protected] edemux(skb); 61c7109986SEric Dumazet } 6248fb6b55SWei-Chun Chao if (!skb_valid_dst(skb)) 631da177e4SLinus Torvalds ip6_route_input(skb); 64*d8269e2cSEdward Cree } 65*d8269e2cSEdward Cree 66*d8269e2cSEdward Cree int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb) 67*d8269e2cSEdward Cree { 68*d8269e2cSEdward Cree /* if ingress device is enslaved to an L3 master device pass the 69*d8269e2cSEdward Cree * skb to its handler for processing 70*d8269e2cSEdward Cree */ 71*d8269e2cSEdward Cree skb = l3mdev_ip6_rcv(skb); 72*d8269e2cSEdward Cree if (!skb) 73*d8269e2cSEdward Cree return NET_RX_SUCCESS; 74*d8269e2cSEdward Cree ip6_rcv_finish_core(net, sk, skb); 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds return dst_input(skb); 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds 79*d8269e2cSEdward Cree static void ip6_sublist_rcv_finish(struct list_head *head) 80*d8269e2cSEdward Cree { 81*d8269e2cSEdward Cree struct sk_buff *skb, *next; 82*d8269e2cSEdward Cree 83*d8269e2cSEdward Cree list_for_each_entry_safe(skb, next, head, list) 84*d8269e2cSEdward Cree dst_input(skb); 85*d8269e2cSEdward Cree } 86*d8269e2cSEdward Cree 87*d8269e2cSEdward Cree static void ip6_list_rcv_finish(struct net *net, struct sock *sk, 88*d8269e2cSEdward Cree struct list_head *head) 89*d8269e2cSEdward Cree { 90*d8269e2cSEdward Cree struct dst_entry *curr_dst = NULL; 91*d8269e2cSEdward Cree struct sk_buff *skb, *next; 92*d8269e2cSEdward Cree struct list_head sublist; 93*d8269e2cSEdward Cree 94*d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist); 95*d8269e2cSEdward Cree list_for_each_entry_safe(skb, next, head, list) { 96*d8269e2cSEdward Cree struct dst_entry *dst; 97*d8269e2cSEdward Cree 98*d8269e2cSEdward Cree list_del(&skb->list); 99*d8269e2cSEdward Cree /* if ingress device is enslaved to an L3 master device pass the 100*d8269e2cSEdward Cree * skb to its handler for processing 101*d8269e2cSEdward Cree */ 102*d8269e2cSEdward Cree skb = l3mdev_ip6_rcv(skb); 103*d8269e2cSEdward Cree if (!skb) 104*d8269e2cSEdward Cree continue; 105*d8269e2cSEdward Cree ip6_rcv_finish_core(net, sk, skb); 106*d8269e2cSEdward Cree dst = skb_dst(skb); 107*d8269e2cSEdward Cree if (curr_dst != dst) { 108*d8269e2cSEdward Cree /* dispatch old sublist */ 109*d8269e2cSEdward Cree if (!list_empty(&sublist)) 110*d8269e2cSEdward Cree ip6_sublist_rcv_finish(&sublist); 111*d8269e2cSEdward Cree /* start new sublist */ 112*d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist); 113*d8269e2cSEdward Cree curr_dst = dst; 114*d8269e2cSEdward Cree } 115*d8269e2cSEdward Cree list_add_tail(&skb->list, &sublist); 116*d8269e2cSEdward Cree } 117*d8269e2cSEdward Cree /* dispatch final sublist */ 118*d8269e2cSEdward Cree ip6_sublist_rcv_finish(&sublist); 119*d8269e2cSEdward Cree } 120*d8269e2cSEdward Cree 121*d8269e2cSEdward Cree static struct sk_buff *ip6_rcv_core(struct sk_buff *skb, struct net_device *dev, 122*d8269e2cSEdward Cree struct net *net) 1231da177e4SLinus Torvalds { 124b71d1d42SEric Dumazet const struct ipv6hdr *hdr; 1251da177e4SLinus Torvalds u32 pkt_len; 126a11d206dSYOSHIFUJI Hideaki struct inet6_dev *idev; 1271da177e4SLinus Torvalds 128a11d206dSYOSHIFUJI Hideaki if (skb->pkt_type == PACKET_OTHERHOST) { 129a11d206dSYOSHIFUJI Hideaki kfree_skb(skb); 130*d8269e2cSEdward Cree return NULL; 131a11d206dSYOSHIFUJI Hideaki } 1321da177e4SLinus Torvalds 133a11d206dSYOSHIFUJI Hideaki rcu_read_lock(); 134a11d206dSYOSHIFUJI Hideaki 135a11d206dSYOSHIFUJI Hideaki idev = __in6_dev_get(skb->dev); 136a11d206dSYOSHIFUJI Hideaki 137c2005eb0SEric Dumazet __IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_IN, skb->len); 1381da177e4SLinus Torvalds 139778d80beSYOSHIFUJI Hideaki if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL || 140778d80beSYOSHIFUJI Hideaki !idev || unlikely(idev->cnf.disable_ipv6)) { 1411d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 14271f6f6dfSJesper Nilsson goto drop; 1431da177e4SLinus Torvalds } 1441da177e4SLinus Torvalds 1456b7fdc3aSGuillaume Chazarain memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); 1466b7fdc3aSGuillaume Chazarain 1471da177e4SLinus Torvalds /* 1481da177e4SLinus Torvalds * Store incoming device index. When the packet will 1491da177e4SLinus Torvalds * be queued, we cannot refer to skb->dev anymore. 1501da177e4SLinus Torvalds * 1511da177e4SLinus Torvalds * BTW, when we send a packet for our own local address on a 1521da177e4SLinus Torvalds * non-loopback interface (e.g. ethX), it is being delivered 153de3cb747SDaniel Lezcano * via the loopback interface (lo) here; skb->dev = loopback_dev. 1541da177e4SLinus Torvalds * It, however, should be considered as if it is being 1551da177e4SLinus Torvalds * arrived via the sending interface (ethX), because of the 1561da177e4SLinus Torvalds * nature of scoping architecture. --yoshfuji 1571da177e4SLinus Torvalds */ 15848fb6b55SWei-Chun Chao IP6CB(skb)->iif = skb_valid_dst(skb) ? ip6_dst_idev(skb_dst(skb))->dev->ifindex : dev->ifindex; 1591da177e4SLinus Torvalds 1602889139aSHerbert Xu if (unlikely(!pskb_may_pull(skb, sizeof(*hdr)))) 1611da177e4SLinus Torvalds goto err; 1621da177e4SLinus Torvalds 1630660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds if (hdr->version != 6) 1661da177e4SLinus Torvalds goto err; 1671da177e4SLinus Torvalds 1681d015503SEric Dumazet __IP6_ADD_STATS(net, idev, 1691f07d03eSEric Dumazet IPSTATS_MIB_NOECTPKTS + 1701f07d03eSEric Dumazet (ipv6_get_dsfield(hdr) & INET_ECN_MASK), 1711f07d03eSEric Dumazet max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs)); 172f630e43aSYOSHIFUJI Hideaki /* 173f630e43aSYOSHIFUJI Hideaki * RFC4291 2.5.3 1740aa8c13eSFlorian Westphal * The loopback address must not be used as the source address in IPv6 1750aa8c13eSFlorian Westphal * packets that are sent outside of a single node. [..] 176f630e43aSYOSHIFUJI Hideaki * A packet received on an interface with a destination address 177f630e43aSYOSHIFUJI Hideaki * of loopback must be dropped. 178f630e43aSYOSHIFUJI Hideaki */ 1790aa8c13eSFlorian Westphal if ((ipv6_addr_loopback(&hdr->saddr) || 1800aa8c13eSFlorian Westphal ipv6_addr_loopback(&hdr->daddr)) && 1810aa8c13eSFlorian Westphal !(dev->flags & IFF_LOOPBACK)) 182f630e43aSYOSHIFUJI Hideaki goto err; 183f630e43aSYOSHIFUJI Hideaki 1841c4a154eSHannes Frederic Sowa /* RFC4291 Errata ID: 3480 1851c4a154eSHannes Frederic Sowa * Interface-Local scope spans only a single interface on a 1861c4a154eSHannes Frederic Sowa * node and is useful only for loopback transmission of 1871c4a154eSHannes Frederic Sowa * multicast. Packets with interface-local scope received 1881c4a154eSHannes Frederic Sowa * from another node must be discarded. 1891c4a154eSHannes Frederic Sowa */ 1901c4a154eSHannes Frederic Sowa if (!(skb->pkt_type == PACKET_LOOPBACK || 1911c4a154eSHannes Frederic Sowa dev->flags & IFF_LOOPBACK) && 1921c4a154eSHannes Frederic Sowa ipv6_addr_is_multicast(&hdr->daddr) && 1931c4a154eSHannes Frederic Sowa IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 1) 1941c4a154eSHannes Frederic Sowa goto err; 1951c4a154eSHannes Frederic Sowa 196abbc3043SJohannes Berg /* If enabled, drop unicast packets that were encapsulated in link-layer 197abbc3043SJohannes Berg * multicast or broadcast to protected against the so-called "hole-196" 198abbc3043SJohannes Berg * attack in 802.11 wireless. 199abbc3043SJohannes Berg */ 200abbc3043SJohannes Berg if (!ipv6_addr_is_multicast(&hdr->daddr) && 201abbc3043SJohannes Berg (skb->pkt_type == PACKET_BROADCAST || 202abbc3043SJohannes Berg skb->pkt_type == PACKET_MULTICAST) && 203abbc3043SJohannes Berg idev->cnf.drop_unicast_in_l2_multicast) 204abbc3043SJohannes Berg goto err; 205abbc3043SJohannes Berg 20620314092SHannes Frederic Sowa /* RFC4291 2.7 20720314092SHannes Frederic Sowa * Nodes must not originate a packet to a multicast address whose scope 20820314092SHannes Frederic Sowa * field contains the reserved value 0; if such a packet is received, it 20920314092SHannes Frederic Sowa * must be silently dropped. 21020314092SHannes Frederic Sowa */ 21120314092SHannes Frederic Sowa if (ipv6_addr_is_multicast(&hdr->daddr) && 21220314092SHannes Frederic Sowa IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 0) 21320314092SHannes Frederic Sowa goto err; 21420314092SHannes Frederic Sowa 215c457338dSBrian Haley /* 216c457338dSBrian Haley * RFC4291 2.7 217c457338dSBrian Haley * Multicast addresses must not be used as source addresses in IPv6 218c457338dSBrian Haley * packets or appear in any Routing header. 219c457338dSBrian Haley */ 220c457338dSBrian Haley if (ipv6_addr_is_multicast(&hdr->saddr)) 221c457338dSBrian Haley goto err; 222c457338dSBrian Haley 223b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(*hdr); 224951dbc8aSPatrick McHardy IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr); 225951dbc8aSPatrick McHardy 2261da177e4SLinus Torvalds pkt_len = ntohs(hdr->payload_len); 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /* pkt_len may be zero if Jumbo payload option is present */ 2291da177e4SLinus Torvalds if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) { 23060e5c166SMitsuru Chinen if (pkt_len + sizeof(struct ipv6hdr) > skb->len) { 2311d015503SEric Dumazet __IP6_INC_STATS(net, 232483a47d2SDenis V. Lunev idev, IPSTATS_MIB_INTRUNCATEDPKTS); 23360e5c166SMitsuru Chinen goto drop; 23460e5c166SMitsuru Chinen } 2351da177e4SLinus Torvalds if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) { 2361d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 2371da177e4SLinus Torvalds goto drop; 2381da177e4SLinus Torvalds } 2390660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds if (hdr->nexthdr == NEXTHDR_HOP) { 243e5bbef20SHerbert Xu if (ipv6_parse_hopopts(skb) < 0) { 2441d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 245a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 246*d8269e2cSEdward Cree return NULL; 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 250a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 251a11d206dSYOSHIFUJI Hideaki 25271f9dacdSHerbert Xu /* Must drop socket now because of tproxy. */ 25371f9dacdSHerbert Xu skb_orphan(skb); 25471f9dacdSHerbert Xu 255*d8269e2cSEdward Cree return skb; 2561da177e4SLinus Torvalds err: 2571d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 2581da177e4SLinus Torvalds drop: 259a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 2601da177e4SLinus Torvalds kfree_skb(skb); 261*d8269e2cSEdward Cree return NULL; 262*d8269e2cSEdward Cree } 263*d8269e2cSEdward Cree 264*d8269e2cSEdward Cree int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) 265*d8269e2cSEdward Cree { 266*d8269e2cSEdward Cree struct net *net = dev_net(skb->dev); 267*d8269e2cSEdward Cree 268*d8269e2cSEdward Cree skb = ip6_rcv_core(skb, dev, net); 269*d8269e2cSEdward Cree if (skb == NULL) 2705c91faceSMark Smith return NET_RX_DROP; 271*d8269e2cSEdward Cree return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, 272*d8269e2cSEdward Cree net, NULL, skb, dev, NULL, 273*d8269e2cSEdward Cree ip6_rcv_finish); 274*d8269e2cSEdward Cree } 275*d8269e2cSEdward Cree 276*d8269e2cSEdward Cree static void ip6_sublist_rcv(struct list_head *head, struct net_device *dev, 277*d8269e2cSEdward Cree struct net *net) 278*d8269e2cSEdward Cree { 279*d8269e2cSEdward Cree NF_HOOK_LIST(NFPROTO_IPV6, NF_INET_PRE_ROUTING, net, NULL, 280*d8269e2cSEdward Cree head, dev, NULL, ip6_rcv_finish); 281*d8269e2cSEdward Cree ip6_list_rcv_finish(net, NULL, head); 282*d8269e2cSEdward Cree } 283*d8269e2cSEdward Cree 284*d8269e2cSEdward Cree /* Receive a list of IPv6 packets */ 285*d8269e2cSEdward Cree void ipv6_list_rcv(struct list_head *head, struct packet_type *pt, 286*d8269e2cSEdward Cree struct net_device *orig_dev) 287*d8269e2cSEdward Cree { 288*d8269e2cSEdward Cree struct net_device *curr_dev = NULL; 289*d8269e2cSEdward Cree struct net *curr_net = NULL; 290*d8269e2cSEdward Cree struct sk_buff *skb, *next; 291*d8269e2cSEdward Cree struct list_head sublist; 292*d8269e2cSEdward Cree 293*d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist); 294*d8269e2cSEdward Cree list_for_each_entry_safe(skb, next, head, list) { 295*d8269e2cSEdward Cree struct net_device *dev = skb->dev; 296*d8269e2cSEdward Cree struct net *net = dev_net(dev); 297*d8269e2cSEdward Cree 298*d8269e2cSEdward Cree list_del(&skb->list); 299*d8269e2cSEdward Cree skb = ip6_rcv_core(skb, dev, net); 300*d8269e2cSEdward Cree if (skb == NULL) 301*d8269e2cSEdward Cree continue; 302*d8269e2cSEdward Cree 303*d8269e2cSEdward Cree if (curr_dev != dev || curr_net != net) { 304*d8269e2cSEdward Cree /* dispatch old sublist */ 305*d8269e2cSEdward Cree if (!list_empty(&sublist)) 306*d8269e2cSEdward Cree ip6_sublist_rcv(&sublist, curr_dev, curr_net); 307*d8269e2cSEdward Cree /* start new sublist */ 308*d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist); 309*d8269e2cSEdward Cree curr_dev = dev; 310*d8269e2cSEdward Cree curr_net = net; 311*d8269e2cSEdward Cree } 312*d8269e2cSEdward Cree list_add_tail(&skb->list, &sublist); 313*d8269e2cSEdward Cree } 314*d8269e2cSEdward Cree /* dispatch final sublist */ 315*d8269e2cSEdward Cree ip6_sublist_rcv(&sublist, curr_dev, curr_net); 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds /* 3191da177e4SLinus Torvalds * Deliver the packet to the host 3201da177e4SLinus Torvalds */ 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds 3230c4b51f0SEric W. Biederman static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb) 3241da177e4SLinus Torvalds { 32541135cc8SAlexey Dobriyan const struct inet6_protocol *ipprot; 326f9242b6bSDavid S. Miller struct inet6_dev *idev; 3271da177e4SLinus Torvalds unsigned int nhoff; 328a50feda5SEric Dumazet int nexthdr; 329a50feda5SEric Dumazet bool raw; 3301da44f9cSTom Herbert bool have_final = false; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds /* 3331da177e4SLinus Torvalds * Parse extension headers 3341da177e4SLinus Torvalds */ 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds rcu_read_lock(); 3371b0ccfe5SDavid S. Miller resubmit: 338adf30907SEric Dumazet idev = ip6_dst_idev(skb_dst(skb)); 339ea2ae17dSArnaldo Carvalho de Melo if (!pskb_pull(skb, skb_transport_offset(skb))) 3401da177e4SLinus Torvalds goto discard; 341951dbc8aSPatrick McHardy nhoff = IP6CB(skb)->nhoff; 342d56f90a7SArnaldo Carvalho de Melo nexthdr = skb_network_header(skb)[nhoff]; 3431da177e4SLinus Torvalds 3444c64242aSTom Herbert resubmit_final: 34569d6da0bSPavel Emelyanov raw = raw6_local_deliver(skb, nexthdr); 346e5d08d71SIan Morris ipprot = rcu_dereference(inet6_protos[nexthdr]); 34753b24b8fSIan Morris if (ipprot) { 3481da177e4SLinus Torvalds int ret; 3491da177e4SLinus Torvalds 3501da44f9cSTom Herbert if (have_final) { 3511da44f9cSTom Herbert if (!(ipprot->flags & INET6_PROTO_FINAL)) { 3521da44f9cSTom Herbert /* Once we've seen a final protocol don't 3531da44f9cSTom Herbert * allow encapsulation on any non-final 3541da44f9cSTom Herbert * ones. This allows foo in UDP encapsulation 3551da44f9cSTom Herbert * to work. 3561da44f9cSTom Herbert */ 3571da44f9cSTom Herbert goto discard; 3581da44f9cSTom Herbert } 3591da44f9cSTom Herbert } else if (ipprot->flags & INET6_PROTO_FINAL) { 360b71d1d42SEric Dumazet const struct ipv6hdr *hdr; 3611da177e4SLinus Torvalds 3621da44f9cSTom Herbert /* Only do this once for first final protocol */ 3631da44f9cSTom Herbert have_final = true; 3641da44f9cSTom Herbert 3659fb9cbb1SYasuyuki Kozakai /* Free reference early: we don't need it any more, 3669fb9cbb1SYasuyuki Kozakai and it may hold ip_conntrack module loaded 3679fb9cbb1SYasuyuki Kozakai indefinitely. */ 3689fb9cbb1SYasuyuki Kozakai nf_reset(skb); 3699fb9cbb1SYasuyuki Kozakai 370d56f90a7SArnaldo Carvalho de Melo skb_postpull_rcsum(skb, skb_network_header(skb), 371cfe1fc77SArnaldo Carvalho de Melo skb_network_header_len(skb)); 3720660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 3731da177e4SLinus Torvalds if (ipv6_addr_is_multicast(&hdr->daddr) && 3741da177e4SLinus Torvalds !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, 3751da177e4SLinus Torvalds &hdr->saddr) && 376daad1512SYOSHIFUJI Hideaki / 吉藤英明 !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb))) 3771da177e4SLinus Torvalds goto discard; 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds if (!(ipprot->flags & INET6_PROTO_NOPOLICY) && 3801da177e4SLinus Torvalds !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) 3811da177e4SLinus Torvalds goto discard; 3821da177e4SLinus Torvalds 383e5bbef20SHerbert Xu ret = ipprot->handler(skb); 3844c64242aSTom Herbert if (ret > 0) { 3854c64242aSTom Herbert if (ipprot->flags & INET6_PROTO_FINAL) { 3864c64242aSTom Herbert /* Not an extension header, most likely UDP 3874c64242aSTom Herbert * encapsulation. Use return value as nexthdr 3884c64242aSTom Herbert * protocol not nhoff (which presumably is 3894c64242aSTom Herbert * not set by handler). 3904c64242aSTom Herbert */ 3914c64242aSTom Herbert nexthdr = ret; 3924c64242aSTom Herbert goto resubmit_final; 3934c64242aSTom Herbert } else { 3941da177e4SLinus Torvalds goto resubmit; 3954c64242aSTom Herbert } 3964c64242aSTom Herbert } else if (ret == 0) { 3971d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS); 3984c64242aSTom Herbert } 3991da177e4SLinus Torvalds } else { 40069d6da0bSPavel Emelyanov if (!raw) { 4011da177e4SLinus Torvalds if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { 4021d015503SEric Dumazet __IP6_INC_STATS(net, idev, 403483a47d2SDenis V. Lunev IPSTATS_MIB_INUNKNOWNPROTOS); 404fad87acaSPatrick McHardy icmpv6_send(skb, ICMPV6_PARAMPROB, 4053ffe533cSAlexey Dobriyan ICMPV6_UNK_NEXTHDR, nhoff); 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds kfree_skb(skb); 408d8c6f4b9SNeil Horman } else { 4091d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS); 410d8c6f4b9SNeil Horman consume_skb(skb); 411d8c6f4b9SNeil Horman } 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds rcu_read_unlock(); 4141da177e4SLinus Torvalds return 0; 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds discard: 4171d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 4181da177e4SLinus Torvalds rcu_read_unlock(); 4191da177e4SLinus Torvalds kfree_skb(skb); 4201da177e4SLinus Torvalds return 0; 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds int ip6_input(struct sk_buff *skb) 4251da177e4SLinus Torvalds { 42629a26a56SEric W. Biederman return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN, 42729a26a56SEric W. Biederman dev_net(skb->dev), NULL, skb, skb->dev, NULL, 4286e23ae2aSPatrick McHardy ip6_input_finish); 4291da177e4SLinus Torvalds } 430b4869aa2SDavid Ahern EXPORT_SYMBOL_GPL(ip6_input); 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds int ip6_mc_input(struct sk_buff *skb) 4331da177e4SLinus Torvalds { 434b71d1d42SEric Dumazet const struct ipv6hdr *hdr; 435a50feda5SEric Dumazet bool deliver; 4361da177e4SLinus Torvalds 437c2005eb0SEric Dumazet __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev), 438bdb7cc64SStephen Suryaputra __in6_dev_get_safely(skb->dev), IPSTATS_MIB_INMCAST, 439edf391ffSNeil Horman skb->len); 4401da177e4SLinus Torvalds 4410660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 4424c7966b8SYOSHIFUJI Hideaki deliver = ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL); 4431da177e4SLinus Torvalds 4447bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 4451da177e4SLinus Torvalds /* 4467bc570c8SYOSHIFUJI Hideaki * IPv6 multicast router mode is now supported ;) 4471da177e4SLinus Torvalds */ 44853b7997fSYOSHIFUJI Hideaki if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding && 449ddf64354SHannes Frederic Sowa !(ipv6_addr_type(&hdr->daddr) & 450ddf64354SHannes Frederic Sowa (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) && 4517bc570c8SYOSHIFUJI Hideaki likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) { 4527bc570c8SYOSHIFUJI Hideaki /* 4537bc570c8SYOSHIFUJI Hideaki * Okay, we try to forward - split and duplicate 4547bc570c8SYOSHIFUJI Hideaki * packets. 4557bc570c8SYOSHIFUJI Hideaki */ 4561da177e4SLinus Torvalds struct sk_buff *skb2; 4577bc570c8SYOSHIFUJI Hideaki struct inet6_skb_parm *opt = IP6CB(skb); 4581da177e4SLinus Torvalds 4597bc570c8SYOSHIFUJI Hideaki /* Check for MLD */ 460dd3332bfSYOSHIFUJI Hideaki / 吉藤英明 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) { 4617bc570c8SYOSHIFUJI Hideaki /* Check if this is a mld message */ 4627bc570c8SYOSHIFUJI Hideaki u8 nexthdr = hdr->nexthdr; 46375f2811cSJesse Gross __be16 frag_off; 4647bc570c8SYOSHIFUJI Hideaki int offset; 4651da177e4SLinus Torvalds 4667bc570c8SYOSHIFUJI Hideaki /* Check if the value of Router Alert 4677bc570c8SYOSHIFUJI Hideaki * is for MLD (0x0000). 4687bc570c8SYOSHIFUJI Hideaki */ 469dd3332bfSYOSHIFUJI Hideaki / 吉藤英明 if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) { 470a50feda5SEric Dumazet deliver = false; 471aba6096bSYOSHIFUJI Hideaki 4727bc570c8SYOSHIFUJI Hideaki if (!ipv6_ext_hdr(nexthdr)) { 4737bc570c8SYOSHIFUJI Hideaki /* BUG */ 474aba6096bSYOSHIFUJI Hideaki goto out; 4751da177e4SLinus Torvalds } 4767bc570c8SYOSHIFUJI Hideaki offset = ipv6_skip_exthdr(skb, sizeof(*hdr), 47775f2811cSJesse Gross &nexthdr, &frag_off); 4787bc570c8SYOSHIFUJI Hideaki if (offset < 0) 479aba6096bSYOSHIFUJI Hideaki goto out; 4807bc570c8SYOSHIFUJI Hideaki 4814c938d22SAngga if (ipv6_is_mld(skb, nexthdr, offset)) 482a50feda5SEric Dumazet deliver = true; 4834c938d22SAngga 4844c938d22SAngga goto out; 4857bc570c8SYOSHIFUJI Hideaki } 4867bc570c8SYOSHIFUJI Hideaki /* unknown RA - process it normally */ 4877bc570c8SYOSHIFUJI Hideaki } 4887bc570c8SYOSHIFUJI Hideaki 4897bc570c8SYOSHIFUJI Hideaki if (deliver) 4907bc570c8SYOSHIFUJI Hideaki skb2 = skb_clone(skb, GFP_ATOMIC); 4917bc570c8SYOSHIFUJI Hideaki else { 4927bc570c8SYOSHIFUJI Hideaki skb2 = skb; 4937bc570c8SYOSHIFUJI Hideaki skb = NULL; 4947bc570c8SYOSHIFUJI Hideaki } 4957bc570c8SYOSHIFUJI Hideaki 4967bc570c8SYOSHIFUJI Hideaki if (skb2) { 4977bc570c8SYOSHIFUJI Hideaki ip6_mr_input(skb2); 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds } 5007bc570c8SYOSHIFUJI Hideaki out: 501aba6096bSYOSHIFUJI Hideaki #endif 502aba6096bSYOSHIFUJI Hideaki if (likely(deliver)) 5031da177e4SLinus Torvalds ip6_input(skb); 504aba6096bSYOSHIFUJI Hideaki else { 5051da177e4SLinus Torvalds /* discard */ 5061da177e4SLinus Torvalds kfree_skb(skb); 507aba6096bSYOSHIFUJI Hideaki } 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds return 0; 5101da177e4SLinus Torvalds } 511