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> 320e219ae4SPaolo Abeni #include <linux/indirect_call_wrapper.h> 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #include <linux/netfilter.h> 351da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds #include <net/sock.h> 381da177e4SLinus Torvalds #include <net/snmp.h> 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #include <net/ipv6.h> 411da177e4SLinus Torvalds #include <net/protocol.h> 421da177e4SLinus Torvalds #include <net/transp_v6.h> 431da177e4SLinus Torvalds #include <net/rawv6.h> 441da177e4SLinus Torvalds #include <net/ndisc.h> 451da177e4SLinus Torvalds #include <net/ip6_route.h> 461da177e4SLinus Torvalds #include <net/addrconf.h> 471da177e4SLinus Torvalds #include <net/xfrm.h> 481f07d03eSEric Dumazet #include <net/inet_ecn.h> 4948fb6b55SWei-Chun Chao #include <net/dst_metadata.h> 501da177e4SLinus Torvalds 51*97ff7ffbSPaolo Abeni INDIRECT_CALLABLE_DECLARE(void udp_v6_early_demux(struct sk_buff *)); 52*97ff7ffbSPaolo Abeni INDIRECT_CALLABLE_DECLARE(void tcp_v6_early_demux(struct sk_buff *)); 53d8269e2cSEdward Cree static void ip6_rcv_finish_core(struct net *net, struct sock *sk, 54d8269e2cSEdward Cree struct sk_buff *skb) 551da177e4SLinus Torvalds { 56dddb64bcS[email protected] void (*edemux)(struct sk_buff *skb); 57dddb64bcS[email protected] 58e21145a9SNikolay Borisov if (net->ipv4.sysctl_ip_early_demux && !skb_dst(skb) && skb->sk == NULL) { 59c7109986SEric Dumazet const struct inet6_protocol *ipprot; 60c7109986SEric Dumazet 61c7109986SEric Dumazet ipprot = rcu_dereference(inet6_protos[ipv6_hdr(skb)->nexthdr]); 62dddb64bcS[email protected] if (ipprot && (edemux = READ_ONCE(ipprot->early_demux))) 63*97ff7ffbSPaolo Abeni INDIRECT_CALL_2(edemux, tcp_v6_early_demux, 64*97ff7ffbSPaolo Abeni udp_v6_early_demux, skb); 65c7109986SEric Dumazet } 6648fb6b55SWei-Chun Chao if (!skb_valid_dst(skb)) 671da177e4SLinus Torvalds ip6_route_input(skb); 68d8269e2cSEdward Cree } 69d8269e2cSEdward Cree 70d8269e2cSEdward Cree int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb) 71d8269e2cSEdward Cree { 72d8269e2cSEdward Cree /* if ingress device is enslaved to an L3 master device pass the 73d8269e2cSEdward Cree * skb to its handler for processing 74d8269e2cSEdward Cree */ 75d8269e2cSEdward Cree skb = l3mdev_ip6_rcv(skb); 76d8269e2cSEdward Cree if (!skb) 77d8269e2cSEdward Cree return NET_RX_SUCCESS; 78d8269e2cSEdward Cree ip6_rcv_finish_core(net, sk, skb); 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds return dst_input(skb); 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds 83d8269e2cSEdward Cree static void ip6_sublist_rcv_finish(struct list_head *head) 84d8269e2cSEdward Cree { 85d8269e2cSEdward Cree struct sk_buff *skb, *next; 86d8269e2cSEdward Cree 87d8269e2cSEdward Cree list_for_each_entry_safe(skb, next, head, list) 88d8269e2cSEdward Cree dst_input(skb); 89d8269e2cSEdward Cree } 90d8269e2cSEdward Cree 91d8269e2cSEdward Cree static void ip6_list_rcv_finish(struct net *net, struct sock *sk, 92d8269e2cSEdward Cree struct list_head *head) 93d8269e2cSEdward Cree { 94d8269e2cSEdward Cree struct dst_entry *curr_dst = NULL; 95d8269e2cSEdward Cree struct sk_buff *skb, *next; 96d8269e2cSEdward Cree struct list_head sublist; 97d8269e2cSEdward Cree 98d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist); 99d8269e2cSEdward Cree list_for_each_entry_safe(skb, next, head, list) { 100d8269e2cSEdward Cree struct dst_entry *dst; 101d8269e2cSEdward Cree 10222f6bbb7SEdward Cree skb_list_del_init(skb); 103d8269e2cSEdward Cree /* if ingress device is enslaved to an L3 master device pass the 104d8269e2cSEdward Cree * skb to its handler for processing 105d8269e2cSEdward Cree */ 106d8269e2cSEdward Cree skb = l3mdev_ip6_rcv(skb); 107d8269e2cSEdward Cree if (!skb) 108d8269e2cSEdward Cree continue; 109d8269e2cSEdward Cree ip6_rcv_finish_core(net, sk, skb); 110d8269e2cSEdward Cree dst = skb_dst(skb); 111d8269e2cSEdward Cree if (curr_dst != dst) { 112d8269e2cSEdward Cree /* dispatch old sublist */ 113d8269e2cSEdward Cree if (!list_empty(&sublist)) 114d8269e2cSEdward Cree ip6_sublist_rcv_finish(&sublist); 115d8269e2cSEdward Cree /* start new sublist */ 116d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist); 117d8269e2cSEdward Cree curr_dst = dst; 118d8269e2cSEdward Cree } 119d8269e2cSEdward Cree list_add_tail(&skb->list, &sublist); 120d8269e2cSEdward Cree } 121d8269e2cSEdward Cree /* dispatch final sublist */ 122d8269e2cSEdward Cree ip6_sublist_rcv_finish(&sublist); 123d8269e2cSEdward Cree } 124d8269e2cSEdward Cree 125d8269e2cSEdward Cree static struct sk_buff *ip6_rcv_core(struct sk_buff *skb, struct net_device *dev, 126d8269e2cSEdward Cree struct net *net) 1271da177e4SLinus Torvalds { 128b71d1d42SEric Dumazet const struct ipv6hdr *hdr; 1291da177e4SLinus Torvalds u32 pkt_len; 130a11d206dSYOSHIFUJI Hideaki struct inet6_dev *idev; 1311da177e4SLinus Torvalds 132a11d206dSYOSHIFUJI Hideaki if (skb->pkt_type == PACKET_OTHERHOST) { 133a11d206dSYOSHIFUJI Hideaki kfree_skb(skb); 134d8269e2cSEdward Cree return NULL; 135a11d206dSYOSHIFUJI Hideaki } 1361da177e4SLinus Torvalds 137a11d206dSYOSHIFUJI Hideaki rcu_read_lock(); 138a11d206dSYOSHIFUJI Hideaki 139a11d206dSYOSHIFUJI Hideaki idev = __in6_dev_get(skb->dev); 140a11d206dSYOSHIFUJI Hideaki 141c2005eb0SEric Dumazet __IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_IN, skb->len); 1421da177e4SLinus Torvalds 143778d80beSYOSHIFUJI Hideaki if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL || 144778d80beSYOSHIFUJI Hideaki !idev || unlikely(idev->cnf.disable_ipv6)) { 1451d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 14671f6f6dfSJesper Nilsson goto drop; 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 1496b7fdc3aSGuillaume Chazarain memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); 1506b7fdc3aSGuillaume Chazarain 1511da177e4SLinus Torvalds /* 1521da177e4SLinus Torvalds * Store incoming device index. When the packet will 1531da177e4SLinus Torvalds * be queued, we cannot refer to skb->dev anymore. 1541da177e4SLinus Torvalds * 1551da177e4SLinus Torvalds * BTW, when we send a packet for our own local address on a 1561da177e4SLinus Torvalds * non-loopback interface (e.g. ethX), it is being delivered 157de3cb747SDaniel Lezcano * via the loopback interface (lo) here; skb->dev = loopback_dev. 1581da177e4SLinus Torvalds * It, however, should be considered as if it is being 1591da177e4SLinus Torvalds * arrived via the sending interface (ethX), because of the 1601da177e4SLinus Torvalds * nature of scoping architecture. --yoshfuji 1611da177e4SLinus Torvalds */ 16248fb6b55SWei-Chun Chao IP6CB(skb)->iif = skb_valid_dst(skb) ? ip6_dst_idev(skb_dst(skb))->dev->ifindex : dev->ifindex; 1631da177e4SLinus Torvalds 1642889139aSHerbert Xu if (unlikely(!pskb_may_pull(skb, sizeof(*hdr)))) 1651da177e4SLinus Torvalds goto err; 1661da177e4SLinus Torvalds 1670660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds if (hdr->version != 6) 1701da177e4SLinus Torvalds goto err; 1711da177e4SLinus Torvalds 1721d015503SEric Dumazet __IP6_ADD_STATS(net, idev, 1731f07d03eSEric Dumazet IPSTATS_MIB_NOECTPKTS + 1741f07d03eSEric Dumazet (ipv6_get_dsfield(hdr) & INET_ECN_MASK), 1751f07d03eSEric Dumazet max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs)); 176f630e43aSYOSHIFUJI Hideaki /* 177f630e43aSYOSHIFUJI Hideaki * RFC4291 2.5.3 1780aa8c13eSFlorian Westphal * The loopback address must not be used as the source address in IPv6 1790aa8c13eSFlorian Westphal * packets that are sent outside of a single node. [..] 180f630e43aSYOSHIFUJI Hideaki * A packet received on an interface with a destination address 181f630e43aSYOSHIFUJI Hideaki * of loopback must be dropped. 182f630e43aSYOSHIFUJI Hideaki */ 1830aa8c13eSFlorian Westphal if ((ipv6_addr_loopback(&hdr->saddr) || 1840aa8c13eSFlorian Westphal ipv6_addr_loopback(&hdr->daddr)) && 1853ede0bbcSRobert Shearman !(dev->flags & IFF_LOOPBACK) && 1863ede0bbcSRobert Shearman !netif_is_l3_master(dev)) 187f630e43aSYOSHIFUJI Hideaki goto err; 188f630e43aSYOSHIFUJI Hideaki 1891c4a154eSHannes Frederic Sowa /* RFC4291 Errata ID: 3480 1901c4a154eSHannes Frederic Sowa * Interface-Local scope spans only a single interface on a 1911c4a154eSHannes Frederic Sowa * node and is useful only for loopback transmission of 1921c4a154eSHannes Frederic Sowa * multicast. Packets with interface-local scope received 1931c4a154eSHannes Frederic Sowa * from another node must be discarded. 1941c4a154eSHannes Frederic Sowa */ 1951c4a154eSHannes Frederic Sowa if (!(skb->pkt_type == PACKET_LOOPBACK || 1961c4a154eSHannes Frederic Sowa dev->flags & IFF_LOOPBACK) && 1971c4a154eSHannes Frederic Sowa ipv6_addr_is_multicast(&hdr->daddr) && 1981c4a154eSHannes Frederic Sowa IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 1) 1991c4a154eSHannes Frederic Sowa goto err; 2001c4a154eSHannes Frederic Sowa 201abbc3043SJohannes Berg /* If enabled, drop unicast packets that were encapsulated in link-layer 202abbc3043SJohannes Berg * multicast or broadcast to protected against the so-called "hole-196" 203abbc3043SJohannes Berg * attack in 802.11 wireless. 204abbc3043SJohannes Berg */ 205abbc3043SJohannes Berg if (!ipv6_addr_is_multicast(&hdr->daddr) && 206abbc3043SJohannes Berg (skb->pkt_type == PACKET_BROADCAST || 207abbc3043SJohannes Berg skb->pkt_type == PACKET_MULTICAST) && 208abbc3043SJohannes Berg idev->cnf.drop_unicast_in_l2_multicast) 209abbc3043SJohannes Berg goto err; 210abbc3043SJohannes Berg 21120314092SHannes Frederic Sowa /* RFC4291 2.7 21220314092SHannes Frederic Sowa * Nodes must not originate a packet to a multicast address whose scope 21320314092SHannes Frederic Sowa * field contains the reserved value 0; if such a packet is received, it 21420314092SHannes Frederic Sowa * must be silently dropped. 21520314092SHannes Frederic Sowa */ 21620314092SHannes Frederic Sowa if (ipv6_addr_is_multicast(&hdr->daddr) && 21720314092SHannes Frederic Sowa IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 0) 21820314092SHannes Frederic Sowa goto err; 21920314092SHannes Frederic Sowa 220c457338dSBrian Haley /* 221c457338dSBrian Haley * RFC4291 2.7 222c457338dSBrian Haley * Multicast addresses must not be used as source addresses in IPv6 223c457338dSBrian Haley * packets or appear in any Routing header. 224c457338dSBrian Haley */ 225c457338dSBrian Haley if (ipv6_addr_is_multicast(&hdr->saddr)) 226c457338dSBrian Haley goto err; 227c457338dSBrian Haley 228b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(*hdr); 229951dbc8aSPatrick McHardy IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr); 230951dbc8aSPatrick McHardy 2311da177e4SLinus Torvalds pkt_len = ntohs(hdr->payload_len); 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds /* pkt_len may be zero if Jumbo payload option is present */ 2341da177e4SLinus Torvalds if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) { 23560e5c166SMitsuru Chinen if (pkt_len + sizeof(struct ipv6hdr) > skb->len) { 2361d015503SEric Dumazet __IP6_INC_STATS(net, 237483a47d2SDenis V. Lunev idev, IPSTATS_MIB_INTRUNCATEDPKTS); 23860e5c166SMitsuru Chinen goto drop; 23960e5c166SMitsuru Chinen } 2401da177e4SLinus Torvalds if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) { 2411d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 2421da177e4SLinus Torvalds goto drop; 2431da177e4SLinus Torvalds } 2440660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds if (hdr->nexthdr == NEXTHDR_HOP) { 248e5bbef20SHerbert Xu if (ipv6_parse_hopopts(skb) < 0) { 2491d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 250a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 251d8269e2cSEdward Cree return NULL; 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 255a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 256a11d206dSYOSHIFUJI Hideaki 25771f9dacdSHerbert Xu /* Must drop socket now because of tproxy. */ 25871f9dacdSHerbert Xu skb_orphan(skb); 25971f9dacdSHerbert Xu 260d8269e2cSEdward Cree return skb; 2611da177e4SLinus Torvalds err: 2621d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 2631da177e4SLinus Torvalds drop: 264a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 2651da177e4SLinus Torvalds kfree_skb(skb); 266d8269e2cSEdward Cree return NULL; 267d8269e2cSEdward Cree } 268d8269e2cSEdward Cree 269d8269e2cSEdward Cree int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) 270d8269e2cSEdward Cree { 271d8269e2cSEdward Cree struct net *net = dev_net(skb->dev); 272d8269e2cSEdward Cree 273d8269e2cSEdward Cree skb = ip6_rcv_core(skb, dev, net); 274d8269e2cSEdward Cree if (skb == NULL) 2755c91faceSMark Smith return NET_RX_DROP; 276d8269e2cSEdward Cree return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, 277d8269e2cSEdward Cree net, NULL, skb, dev, NULL, 278d8269e2cSEdward Cree ip6_rcv_finish); 279d8269e2cSEdward Cree } 280d8269e2cSEdward Cree 281d8269e2cSEdward Cree static void ip6_sublist_rcv(struct list_head *head, struct net_device *dev, 282d8269e2cSEdward Cree struct net *net) 283d8269e2cSEdward Cree { 284d8269e2cSEdward Cree NF_HOOK_LIST(NFPROTO_IPV6, NF_INET_PRE_ROUTING, net, NULL, 285d8269e2cSEdward Cree head, dev, NULL, ip6_rcv_finish); 286d8269e2cSEdward Cree ip6_list_rcv_finish(net, NULL, head); 287d8269e2cSEdward Cree } 288d8269e2cSEdward Cree 289d8269e2cSEdward Cree /* Receive a list of IPv6 packets */ 290d8269e2cSEdward Cree void ipv6_list_rcv(struct list_head *head, struct packet_type *pt, 291d8269e2cSEdward Cree struct net_device *orig_dev) 292d8269e2cSEdward Cree { 293d8269e2cSEdward Cree struct net_device *curr_dev = NULL; 294d8269e2cSEdward Cree struct net *curr_net = NULL; 295d8269e2cSEdward Cree struct sk_buff *skb, *next; 296d8269e2cSEdward Cree struct list_head sublist; 297d8269e2cSEdward Cree 298d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist); 299d8269e2cSEdward Cree list_for_each_entry_safe(skb, next, head, list) { 300d8269e2cSEdward Cree struct net_device *dev = skb->dev; 301d8269e2cSEdward Cree struct net *net = dev_net(dev); 302d8269e2cSEdward Cree 30322f6bbb7SEdward Cree skb_list_del_init(skb); 304d8269e2cSEdward Cree skb = ip6_rcv_core(skb, dev, net); 305d8269e2cSEdward Cree if (skb == NULL) 306d8269e2cSEdward Cree continue; 307d8269e2cSEdward Cree 308d8269e2cSEdward Cree if (curr_dev != dev || curr_net != net) { 309d8269e2cSEdward Cree /* dispatch old sublist */ 310d8269e2cSEdward Cree if (!list_empty(&sublist)) 311d8269e2cSEdward Cree ip6_sublist_rcv(&sublist, curr_dev, curr_net); 312d8269e2cSEdward Cree /* start new sublist */ 313d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist); 314d8269e2cSEdward Cree curr_dev = dev; 315d8269e2cSEdward Cree curr_net = net; 316d8269e2cSEdward Cree } 317d8269e2cSEdward Cree list_add_tail(&skb->list, &sublist); 318d8269e2cSEdward Cree } 319d8269e2cSEdward Cree /* dispatch final sublist */ 320d8269e2cSEdward Cree ip6_sublist_rcv(&sublist, curr_dev, curr_net); 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 3230e219ae4SPaolo Abeni INDIRECT_CALLABLE_DECLARE(int udpv6_rcv(struct sk_buff *)); 3240e219ae4SPaolo Abeni INDIRECT_CALLABLE_DECLARE(int tcp_v6_rcv(struct sk_buff *)); 3250e219ae4SPaolo Abeni 3261da177e4SLinus Torvalds /* 3271da177e4SLinus Torvalds * Deliver the packet to the host 3281da177e4SLinus Torvalds */ 32980bde363SPaolo Abeni void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr, 33080bde363SPaolo Abeni bool have_final) 3311da177e4SLinus Torvalds { 33241135cc8SAlexey Dobriyan const struct inet6_protocol *ipprot; 333f9242b6bSDavid S. Miller struct inet6_dev *idev; 3341da177e4SLinus Torvalds unsigned int nhoff; 335a50feda5SEric Dumazet bool raw; 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds /* 3381da177e4SLinus Torvalds * Parse extension headers 3391da177e4SLinus Torvalds */ 3401da177e4SLinus Torvalds 3411b0ccfe5SDavid S. Miller resubmit: 342adf30907SEric Dumazet idev = ip6_dst_idev(skb_dst(skb)); 34380bde363SPaolo Abeni nhoff = IP6CB(skb)->nhoff; 34480bde363SPaolo Abeni if (!have_final) { 345ea2ae17dSArnaldo Carvalho de Melo if (!pskb_pull(skb, skb_transport_offset(skb))) 3461da177e4SLinus Torvalds goto discard; 347d56f90a7SArnaldo Carvalho de Melo nexthdr = skb_network_header(skb)[nhoff]; 34880bde363SPaolo Abeni } 3491da177e4SLinus Torvalds 3504c64242aSTom Herbert resubmit_final: 35169d6da0bSPavel Emelyanov raw = raw6_local_deliver(skb, nexthdr); 352e5d08d71SIan Morris ipprot = rcu_dereference(inet6_protos[nexthdr]); 35353b24b8fSIan Morris if (ipprot) { 3541da177e4SLinus Torvalds int ret; 3551da177e4SLinus Torvalds 3561da44f9cSTom Herbert if (have_final) { 3571da44f9cSTom Herbert if (!(ipprot->flags & INET6_PROTO_FINAL)) { 3581da44f9cSTom Herbert /* Once we've seen a final protocol don't 3591da44f9cSTom Herbert * allow encapsulation on any non-final 3601da44f9cSTom Herbert * ones. This allows foo in UDP encapsulation 3611da44f9cSTom Herbert * to work. 3621da44f9cSTom Herbert */ 3631da44f9cSTom Herbert goto discard; 3641da44f9cSTom Herbert } 3651da44f9cSTom Herbert } else if (ipprot->flags & INET6_PROTO_FINAL) { 366b71d1d42SEric Dumazet const struct ipv6hdr *hdr; 3675226b6a9SMike Manning int sdif = inet6_sdif(skb); 3685226b6a9SMike Manning struct net_device *dev; 3691da177e4SLinus Torvalds 3701da44f9cSTom Herbert /* Only do this once for first final protocol */ 3711da44f9cSTom Herbert have_final = true; 3721da44f9cSTom Herbert 3739fb9cbb1SYasuyuki Kozakai /* Free reference early: we don't need it any more, 3749fb9cbb1SYasuyuki Kozakai and it may hold ip_conntrack module loaded 3759fb9cbb1SYasuyuki Kozakai indefinitely. */ 3769fb9cbb1SYasuyuki Kozakai nf_reset(skb); 3779fb9cbb1SYasuyuki Kozakai 378d56f90a7SArnaldo Carvalho de Melo skb_postpull_rcsum(skb, skb_network_header(skb), 379cfe1fc77SArnaldo Carvalho de Melo skb_network_header_len(skb)); 3800660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 3815226b6a9SMike Manning 3825226b6a9SMike Manning /* skb->dev passed may be master dev for vrfs. */ 3835226b6a9SMike Manning if (sdif) { 3845226b6a9SMike Manning dev = dev_get_by_index_rcu(net, sdif); 3855226b6a9SMike Manning if (!dev) 3865226b6a9SMike Manning goto discard; 3875226b6a9SMike Manning } else { 3885226b6a9SMike Manning dev = skb->dev; 3895226b6a9SMike Manning } 3905226b6a9SMike Manning 3911da177e4SLinus Torvalds if (ipv6_addr_is_multicast(&hdr->daddr) && 3925226b6a9SMike Manning !ipv6_chk_mcast_addr(dev, &hdr->daddr, 3931da177e4SLinus Torvalds &hdr->saddr) && 394daad1512SYOSHIFUJI Hideaki / 吉藤英明 !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb))) 3951da177e4SLinus Torvalds goto discard; 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds if (!(ipprot->flags & INET6_PROTO_NOPOLICY) && 3981da177e4SLinus Torvalds !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) 3991da177e4SLinus Torvalds goto discard; 4001da177e4SLinus Torvalds 4010e219ae4SPaolo Abeni ret = INDIRECT_CALL_2(ipprot->handler, tcp_v6_rcv, udpv6_rcv, 4020e219ae4SPaolo Abeni skb); 4034c64242aSTom Herbert if (ret > 0) { 4044c64242aSTom Herbert if (ipprot->flags & INET6_PROTO_FINAL) { 4054c64242aSTom Herbert /* Not an extension header, most likely UDP 4064c64242aSTom Herbert * encapsulation. Use return value as nexthdr 4074c64242aSTom Herbert * protocol not nhoff (which presumably is 4084c64242aSTom Herbert * not set by handler). 4094c64242aSTom Herbert */ 4104c64242aSTom Herbert nexthdr = ret; 4114c64242aSTom Herbert goto resubmit_final; 4124c64242aSTom Herbert } else { 4131da177e4SLinus Torvalds goto resubmit; 4144c64242aSTom Herbert } 4154c64242aSTom Herbert } else if (ret == 0) { 4161d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS); 4174c64242aSTom Herbert } 4181da177e4SLinus Torvalds } else { 41969d6da0bSPavel Emelyanov if (!raw) { 4201da177e4SLinus Torvalds if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { 4211d015503SEric Dumazet __IP6_INC_STATS(net, idev, 422483a47d2SDenis V. Lunev IPSTATS_MIB_INUNKNOWNPROTOS); 423fad87acaSPatrick McHardy icmpv6_send(skb, ICMPV6_PARAMPROB, 4243ffe533cSAlexey Dobriyan ICMPV6_UNK_NEXTHDR, nhoff); 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds kfree_skb(skb); 427d8c6f4b9SNeil Horman } else { 4281d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS); 429d8c6f4b9SNeil Horman consume_skb(skb); 430d8c6f4b9SNeil Horman } 4311da177e4SLinus Torvalds } 43280bde363SPaolo Abeni return; 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds discard: 4351d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 4361da177e4SLinus Torvalds kfree_skb(skb); 43780bde363SPaolo Abeni } 43880bde363SPaolo Abeni 43980bde363SPaolo Abeni static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb) 44080bde363SPaolo Abeni { 44180bde363SPaolo Abeni rcu_read_lock(); 44280bde363SPaolo Abeni ip6_protocol_deliver_rcu(net, skb, 0, false); 44380bde363SPaolo Abeni rcu_read_unlock(); 44480bde363SPaolo Abeni 4451da177e4SLinus Torvalds return 0; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds int ip6_input(struct sk_buff *skb) 4501da177e4SLinus Torvalds { 45129a26a56SEric W. Biederman return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN, 45229a26a56SEric W. Biederman dev_net(skb->dev), NULL, skb, skb->dev, NULL, 4536e23ae2aSPatrick McHardy ip6_input_finish); 4541da177e4SLinus Torvalds } 455b4869aa2SDavid Ahern EXPORT_SYMBOL_GPL(ip6_input); 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds int ip6_mc_input(struct sk_buff *skb) 4581da177e4SLinus Torvalds { 4595226b6a9SMike Manning int sdif = inet6_sdif(skb); 460b71d1d42SEric Dumazet const struct ipv6hdr *hdr; 4615226b6a9SMike Manning struct net_device *dev; 462a50feda5SEric Dumazet bool deliver; 4631da177e4SLinus Torvalds 464c2005eb0SEric Dumazet __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev), 465bdb7cc64SStephen Suryaputra __in6_dev_get_safely(skb->dev), IPSTATS_MIB_INMCAST, 466edf391ffSNeil Horman skb->len); 4671da177e4SLinus Torvalds 4685226b6a9SMike Manning /* skb->dev passed may be master dev for vrfs. */ 4695226b6a9SMike Manning if (sdif) { 4705226b6a9SMike Manning rcu_read_lock(); 4715226b6a9SMike Manning dev = dev_get_by_index_rcu(dev_net(skb->dev), sdif); 4725226b6a9SMike Manning if (!dev) { 4735226b6a9SMike Manning rcu_read_unlock(); 4745226b6a9SMike Manning kfree_skb(skb); 4755226b6a9SMike Manning return -ENODEV; 4765226b6a9SMike Manning } 4775226b6a9SMike Manning } else { 4785226b6a9SMike Manning dev = skb->dev; 4795226b6a9SMike Manning } 4805226b6a9SMike Manning 4810660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 4825226b6a9SMike Manning deliver = ipv6_chk_mcast_addr(dev, &hdr->daddr, NULL); 4835226b6a9SMike Manning if (sdif) 4845226b6a9SMike Manning rcu_read_unlock(); 4851da177e4SLinus Torvalds 4867bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 4871da177e4SLinus Torvalds /* 4887bc570c8SYOSHIFUJI Hideaki * IPv6 multicast router mode is now supported ;) 4891da177e4SLinus Torvalds */ 49053b7997fSYOSHIFUJI Hideaki if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding && 491ddf64354SHannes Frederic Sowa !(ipv6_addr_type(&hdr->daddr) & 492ddf64354SHannes Frederic Sowa (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) && 4937bc570c8SYOSHIFUJI Hideaki likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) { 4947bc570c8SYOSHIFUJI Hideaki /* 4957bc570c8SYOSHIFUJI Hideaki * Okay, we try to forward - split and duplicate 4967bc570c8SYOSHIFUJI Hideaki * packets. 4977bc570c8SYOSHIFUJI Hideaki */ 4981da177e4SLinus Torvalds struct sk_buff *skb2; 4997bc570c8SYOSHIFUJI Hideaki struct inet6_skb_parm *opt = IP6CB(skb); 5001da177e4SLinus Torvalds 5017bc570c8SYOSHIFUJI Hideaki /* Check for MLD */ 502dd3332bfSYOSHIFUJI Hideaki / 吉藤英明 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) { 5037bc570c8SYOSHIFUJI Hideaki /* Check if this is a mld message */ 5047bc570c8SYOSHIFUJI Hideaki u8 nexthdr = hdr->nexthdr; 50575f2811cSJesse Gross __be16 frag_off; 5067bc570c8SYOSHIFUJI Hideaki int offset; 5071da177e4SLinus Torvalds 5087bc570c8SYOSHIFUJI Hideaki /* Check if the value of Router Alert 5097bc570c8SYOSHIFUJI Hideaki * is for MLD (0x0000). 5107bc570c8SYOSHIFUJI Hideaki */ 511dd3332bfSYOSHIFUJI Hideaki / 吉藤英明 if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) { 512a50feda5SEric Dumazet deliver = false; 513aba6096bSYOSHIFUJI Hideaki 5147bc570c8SYOSHIFUJI Hideaki if (!ipv6_ext_hdr(nexthdr)) { 5157bc570c8SYOSHIFUJI Hideaki /* BUG */ 516aba6096bSYOSHIFUJI Hideaki goto out; 5171da177e4SLinus Torvalds } 5187bc570c8SYOSHIFUJI Hideaki offset = ipv6_skip_exthdr(skb, sizeof(*hdr), 51975f2811cSJesse Gross &nexthdr, &frag_off); 5207bc570c8SYOSHIFUJI Hideaki if (offset < 0) 521aba6096bSYOSHIFUJI Hideaki goto out; 5227bc570c8SYOSHIFUJI Hideaki 5234c938d22SAngga if (ipv6_is_mld(skb, nexthdr, offset)) 524a50feda5SEric Dumazet deliver = true; 5254c938d22SAngga 5264c938d22SAngga goto out; 5277bc570c8SYOSHIFUJI Hideaki } 5287bc570c8SYOSHIFUJI Hideaki /* unknown RA - process it normally */ 5297bc570c8SYOSHIFUJI Hideaki } 5307bc570c8SYOSHIFUJI Hideaki 5317bc570c8SYOSHIFUJI Hideaki if (deliver) 5327bc570c8SYOSHIFUJI Hideaki skb2 = skb_clone(skb, GFP_ATOMIC); 5337bc570c8SYOSHIFUJI Hideaki else { 5347bc570c8SYOSHIFUJI Hideaki skb2 = skb; 5357bc570c8SYOSHIFUJI Hideaki skb = NULL; 5367bc570c8SYOSHIFUJI Hideaki } 5377bc570c8SYOSHIFUJI Hideaki 5387bc570c8SYOSHIFUJI Hideaki if (skb2) { 5397bc570c8SYOSHIFUJI Hideaki ip6_mr_input(skb2); 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds } 5427bc570c8SYOSHIFUJI Hideaki out: 543aba6096bSYOSHIFUJI Hideaki #endif 544aba6096bSYOSHIFUJI Hideaki if (likely(deliver)) 5451da177e4SLinus Torvalds ip6_input(skb); 546aba6096bSYOSHIFUJI Hideaki else { 5471da177e4SLinus Torvalds /* discard */ 5481da177e4SLinus Torvalds kfree_skb(skb); 549aba6096bSYOSHIFUJI Hideaki } 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds return 0; 5521da177e4SLinus Torvalds } 553