1*2874c5fdSThomas 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> 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds #include <net/ipv6.h> 371da177e4SLinus Torvalds #include <net/protocol.h> 381da177e4SLinus Torvalds #include <net/transp_v6.h> 391da177e4SLinus Torvalds #include <net/rawv6.h> 401da177e4SLinus Torvalds #include <net/ndisc.h> 411da177e4SLinus Torvalds #include <net/ip6_route.h> 421da177e4SLinus Torvalds #include <net/addrconf.h> 431da177e4SLinus Torvalds #include <net/xfrm.h> 441f07d03eSEric Dumazet #include <net/inet_ecn.h> 4548fb6b55SWei-Chun Chao #include <net/dst_metadata.h> 461da177e4SLinus Torvalds 4797ff7ffbSPaolo Abeni INDIRECT_CALLABLE_DECLARE(void udp_v6_early_demux(struct sk_buff *)); 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 83d8269e2cSEdward Cree list_for_each_entry_safe(skb, next, head, list) 84d8269e2cSEdward Cree dst_input(skb); 85d8269e2cSEdward Cree } 86d8269e2cSEdward Cree 87d8269e2cSEdward Cree static void ip6_list_rcv_finish(struct net *net, struct sock *sk, 88d8269e2cSEdward Cree struct list_head *head) 89d8269e2cSEdward Cree { 90d8269e2cSEdward Cree struct dst_entry *curr_dst = NULL; 91d8269e2cSEdward Cree struct sk_buff *skb, *next; 92d8269e2cSEdward Cree struct list_head sublist; 93d8269e2cSEdward Cree 94d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist); 95d8269e2cSEdward Cree list_for_each_entry_safe(skb, next, head, list) { 96d8269e2cSEdward Cree struct dst_entry *dst; 97d8269e2cSEdward Cree 9822f6bbb7SEdward Cree skb_list_del_init(skb); 99d8269e2cSEdward Cree /* if ingress device is enslaved to an L3 master device pass the 100d8269e2cSEdward Cree * skb to its handler for processing 101d8269e2cSEdward Cree */ 102d8269e2cSEdward Cree skb = l3mdev_ip6_rcv(skb); 103d8269e2cSEdward Cree if (!skb) 104d8269e2cSEdward Cree continue; 105d8269e2cSEdward Cree ip6_rcv_finish_core(net, sk, skb); 106d8269e2cSEdward Cree dst = skb_dst(skb); 107d8269e2cSEdward Cree if (curr_dst != dst) { 108d8269e2cSEdward Cree /* dispatch old sublist */ 109d8269e2cSEdward Cree if (!list_empty(&sublist)) 110d8269e2cSEdward Cree ip6_sublist_rcv_finish(&sublist); 111d8269e2cSEdward Cree /* start new sublist */ 112d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist); 113d8269e2cSEdward Cree curr_dst = dst; 114d8269e2cSEdward Cree } 115d8269e2cSEdward Cree list_add_tail(&skb->list, &sublist); 116d8269e2cSEdward Cree } 117d8269e2cSEdward Cree /* dispatch final sublist */ 118d8269e2cSEdward Cree ip6_sublist_rcv_finish(&sublist); 119d8269e2cSEdward Cree } 120d8269e2cSEdward Cree 121d8269e2cSEdward Cree static struct sk_buff *ip6_rcv_core(struct sk_buff *skb, struct net_device *dev, 122d8269e2cSEdward 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); 130d8269e2cSEdward 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)) && 1813ede0bbcSRobert Shearman !(dev->flags & IFF_LOOPBACK) && 1823ede0bbcSRobert Shearman !netif_is_l3_master(dev)) 183f630e43aSYOSHIFUJI Hideaki goto err; 184f630e43aSYOSHIFUJI Hideaki 1851c4a154eSHannes Frederic Sowa /* RFC4291 Errata ID: 3480 1861c4a154eSHannes Frederic Sowa * Interface-Local scope spans only a single interface on a 1871c4a154eSHannes Frederic Sowa * node and is useful only for loopback transmission of 1881c4a154eSHannes Frederic Sowa * multicast. Packets with interface-local scope received 1891c4a154eSHannes Frederic Sowa * from another node must be discarded. 1901c4a154eSHannes Frederic Sowa */ 1911c4a154eSHannes Frederic Sowa if (!(skb->pkt_type == PACKET_LOOPBACK || 1921c4a154eSHannes Frederic Sowa dev->flags & IFF_LOOPBACK) && 1931c4a154eSHannes Frederic Sowa ipv6_addr_is_multicast(&hdr->daddr) && 1941c4a154eSHannes Frederic Sowa IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 1) 1951c4a154eSHannes Frederic Sowa goto err; 1961c4a154eSHannes Frederic Sowa 197abbc3043SJohannes Berg /* If enabled, drop unicast packets that were encapsulated in link-layer 198abbc3043SJohannes Berg * multicast or broadcast to protected against the so-called "hole-196" 199abbc3043SJohannes Berg * attack in 802.11 wireless. 200abbc3043SJohannes Berg */ 201abbc3043SJohannes Berg if (!ipv6_addr_is_multicast(&hdr->daddr) && 202abbc3043SJohannes Berg (skb->pkt_type == PACKET_BROADCAST || 203abbc3043SJohannes Berg skb->pkt_type == PACKET_MULTICAST) && 204abbc3043SJohannes Berg idev->cnf.drop_unicast_in_l2_multicast) 205abbc3043SJohannes Berg goto err; 206abbc3043SJohannes Berg 20720314092SHannes Frederic Sowa /* RFC4291 2.7 20820314092SHannes Frederic Sowa * Nodes must not originate a packet to a multicast address whose scope 20920314092SHannes Frederic Sowa * field contains the reserved value 0; if such a packet is received, it 21020314092SHannes Frederic Sowa * must be silently dropped. 21120314092SHannes Frederic Sowa */ 21220314092SHannes Frederic Sowa if (ipv6_addr_is_multicast(&hdr->daddr) && 21320314092SHannes Frederic Sowa IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 0) 21420314092SHannes Frederic Sowa goto err; 21520314092SHannes Frederic Sowa 216c457338dSBrian Haley /* 217c457338dSBrian Haley * RFC4291 2.7 218c457338dSBrian Haley * Multicast addresses must not be used as source addresses in IPv6 219c457338dSBrian Haley * packets or appear in any Routing header. 220c457338dSBrian Haley */ 221c457338dSBrian Haley if (ipv6_addr_is_multicast(&hdr->saddr)) 222c457338dSBrian Haley goto err; 223c457338dSBrian Haley 224b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(*hdr); 225951dbc8aSPatrick McHardy IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr); 226951dbc8aSPatrick McHardy 2271da177e4SLinus Torvalds pkt_len = ntohs(hdr->payload_len); 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds /* pkt_len may be zero if Jumbo payload option is present */ 2301da177e4SLinus Torvalds if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) { 23160e5c166SMitsuru Chinen if (pkt_len + sizeof(struct ipv6hdr) > skb->len) { 2321d015503SEric Dumazet __IP6_INC_STATS(net, 233483a47d2SDenis V. Lunev idev, IPSTATS_MIB_INTRUNCATEDPKTS); 23460e5c166SMitsuru Chinen goto drop; 23560e5c166SMitsuru Chinen } 2361da177e4SLinus Torvalds if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) { 2371d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 2381da177e4SLinus Torvalds goto drop; 2391da177e4SLinus Torvalds } 2400660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds if (hdr->nexthdr == NEXTHDR_HOP) { 244e5bbef20SHerbert Xu if (ipv6_parse_hopopts(skb) < 0) { 2451d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 246a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 247d8269e2cSEdward Cree return NULL; 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds 251a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 252a11d206dSYOSHIFUJI Hideaki 25371f9dacdSHerbert Xu /* Must drop socket now because of tproxy. */ 25471f9dacdSHerbert Xu skb_orphan(skb); 25571f9dacdSHerbert Xu 256d8269e2cSEdward Cree return skb; 2571da177e4SLinus Torvalds err: 2581d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 2591da177e4SLinus Torvalds drop: 260a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 2611da177e4SLinus Torvalds kfree_skb(skb); 262d8269e2cSEdward Cree return NULL; 263d8269e2cSEdward Cree } 264d8269e2cSEdward Cree 265d8269e2cSEdward Cree int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) 266d8269e2cSEdward Cree { 267d8269e2cSEdward Cree struct net *net = dev_net(skb->dev); 268d8269e2cSEdward Cree 269d8269e2cSEdward Cree skb = ip6_rcv_core(skb, dev, net); 270d8269e2cSEdward Cree if (skb == NULL) 2715c91faceSMark Smith return NET_RX_DROP; 272d8269e2cSEdward Cree return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, 273d8269e2cSEdward Cree net, NULL, skb, dev, NULL, 274d8269e2cSEdward Cree ip6_rcv_finish); 275d8269e2cSEdward Cree } 276d8269e2cSEdward Cree 277d8269e2cSEdward Cree static void ip6_sublist_rcv(struct list_head *head, struct net_device *dev, 278d8269e2cSEdward Cree struct net *net) 279d8269e2cSEdward Cree { 280d8269e2cSEdward Cree NF_HOOK_LIST(NFPROTO_IPV6, NF_INET_PRE_ROUTING, net, NULL, 281d8269e2cSEdward Cree head, dev, NULL, ip6_rcv_finish); 282d8269e2cSEdward Cree ip6_list_rcv_finish(net, NULL, head); 283d8269e2cSEdward Cree } 284d8269e2cSEdward Cree 285d8269e2cSEdward Cree /* Receive a list of IPv6 packets */ 286d8269e2cSEdward Cree void ipv6_list_rcv(struct list_head *head, struct packet_type *pt, 287d8269e2cSEdward Cree struct net_device *orig_dev) 288d8269e2cSEdward Cree { 289d8269e2cSEdward Cree struct net_device *curr_dev = NULL; 290d8269e2cSEdward Cree struct net *curr_net = NULL; 291d8269e2cSEdward Cree struct sk_buff *skb, *next; 292d8269e2cSEdward Cree struct list_head sublist; 293d8269e2cSEdward Cree 294d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist); 295d8269e2cSEdward Cree list_for_each_entry_safe(skb, next, head, list) { 296d8269e2cSEdward Cree struct net_device *dev = skb->dev; 297d8269e2cSEdward Cree struct net *net = dev_net(dev); 298d8269e2cSEdward Cree 29922f6bbb7SEdward Cree skb_list_del_init(skb); 300d8269e2cSEdward Cree skb = ip6_rcv_core(skb, dev, net); 301d8269e2cSEdward Cree if (skb == NULL) 302d8269e2cSEdward Cree continue; 303d8269e2cSEdward Cree 304d8269e2cSEdward Cree if (curr_dev != dev || curr_net != net) { 305d8269e2cSEdward Cree /* dispatch old sublist */ 306d8269e2cSEdward Cree if (!list_empty(&sublist)) 307d8269e2cSEdward Cree ip6_sublist_rcv(&sublist, curr_dev, curr_net); 308d8269e2cSEdward Cree /* start new sublist */ 309d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist); 310d8269e2cSEdward Cree curr_dev = dev; 311d8269e2cSEdward Cree curr_net = net; 312d8269e2cSEdward Cree } 313d8269e2cSEdward Cree list_add_tail(&skb->list, &sublist); 314d8269e2cSEdward Cree } 315d8269e2cSEdward Cree /* dispatch final sublist */ 316d8269e2cSEdward Cree ip6_sublist_rcv(&sublist, curr_dev, curr_net); 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds 3190e219ae4SPaolo Abeni INDIRECT_CALLABLE_DECLARE(int udpv6_rcv(struct sk_buff *)); 3200e219ae4SPaolo Abeni INDIRECT_CALLABLE_DECLARE(int tcp_v6_rcv(struct sk_buff *)); 3210e219ae4SPaolo Abeni 3221da177e4SLinus Torvalds /* 3231da177e4SLinus Torvalds * Deliver the packet to the host 3241da177e4SLinus Torvalds */ 32580bde363SPaolo Abeni void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr, 32680bde363SPaolo Abeni bool have_final) 3271da177e4SLinus Torvalds { 32841135cc8SAlexey Dobriyan const struct inet6_protocol *ipprot; 329f9242b6bSDavid S. Miller struct inet6_dev *idev; 3301da177e4SLinus Torvalds unsigned int nhoff; 331a50feda5SEric Dumazet bool raw; 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds /* 3341da177e4SLinus Torvalds * Parse extension headers 3351da177e4SLinus Torvalds */ 3361da177e4SLinus Torvalds 3371b0ccfe5SDavid S. Miller resubmit: 338adf30907SEric Dumazet idev = ip6_dst_idev(skb_dst(skb)); 33980bde363SPaolo Abeni nhoff = IP6CB(skb)->nhoff; 34080bde363SPaolo Abeni if (!have_final) { 341ea2ae17dSArnaldo Carvalho de Melo if (!pskb_pull(skb, skb_transport_offset(skb))) 3421da177e4SLinus Torvalds goto discard; 343d56f90a7SArnaldo Carvalho de Melo nexthdr = skb_network_header(skb)[nhoff]; 34480bde363SPaolo Abeni } 3451da177e4SLinus Torvalds 3464c64242aSTom Herbert resubmit_final: 34769d6da0bSPavel Emelyanov raw = raw6_local_deliver(skb, nexthdr); 348e5d08d71SIan Morris ipprot = rcu_dereference(inet6_protos[nexthdr]); 34953b24b8fSIan Morris if (ipprot) { 3501da177e4SLinus Torvalds int ret; 3511da177e4SLinus Torvalds 3521da44f9cSTom Herbert if (have_final) { 3531da44f9cSTom Herbert if (!(ipprot->flags & INET6_PROTO_FINAL)) { 3541da44f9cSTom Herbert /* Once we've seen a final protocol don't 3551da44f9cSTom Herbert * allow encapsulation on any non-final 3561da44f9cSTom Herbert * ones. This allows foo in UDP encapsulation 3571da44f9cSTom Herbert * to work. 3581da44f9cSTom Herbert */ 3591da44f9cSTom Herbert goto discard; 3601da44f9cSTom Herbert } 3611da44f9cSTom Herbert } else if (ipprot->flags & INET6_PROTO_FINAL) { 362b71d1d42SEric Dumazet const struct ipv6hdr *hdr; 3635226b6a9SMike Manning int sdif = inet6_sdif(skb); 3645226b6a9SMike Manning struct net_device *dev; 3651da177e4SLinus Torvalds 3661da44f9cSTom Herbert /* Only do this once for first final protocol */ 3671da44f9cSTom Herbert have_final = true; 3681da44f9cSTom Herbert 3699fb9cbb1SYasuyuki Kozakai /* Free reference early: we don't need it any more, 3709fb9cbb1SYasuyuki Kozakai and it may hold ip_conntrack module loaded 3719fb9cbb1SYasuyuki Kozakai indefinitely. */ 3729fb9cbb1SYasuyuki Kozakai nf_reset(skb); 3739fb9cbb1SYasuyuki Kozakai 374d56f90a7SArnaldo Carvalho de Melo skb_postpull_rcsum(skb, skb_network_header(skb), 375cfe1fc77SArnaldo Carvalho de Melo skb_network_header_len(skb)); 3760660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 3775226b6a9SMike Manning 3785226b6a9SMike Manning /* skb->dev passed may be master dev for vrfs. */ 3795226b6a9SMike Manning if (sdif) { 3805226b6a9SMike Manning dev = dev_get_by_index_rcu(net, sdif); 3815226b6a9SMike Manning if (!dev) 3825226b6a9SMike Manning goto discard; 3835226b6a9SMike Manning } else { 3845226b6a9SMike Manning dev = skb->dev; 3855226b6a9SMike Manning } 3865226b6a9SMike Manning 3871da177e4SLinus Torvalds if (ipv6_addr_is_multicast(&hdr->daddr) && 3885226b6a9SMike Manning !ipv6_chk_mcast_addr(dev, &hdr->daddr, 3891da177e4SLinus Torvalds &hdr->saddr) && 390daad1512SYOSHIFUJI Hideaki / 吉藤英明 !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb))) 3911da177e4SLinus Torvalds goto discard; 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds if (!(ipprot->flags & INET6_PROTO_NOPOLICY) && 3941da177e4SLinus Torvalds !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) 3951da177e4SLinus Torvalds goto discard; 3961da177e4SLinus Torvalds 3970e219ae4SPaolo Abeni ret = INDIRECT_CALL_2(ipprot->handler, tcp_v6_rcv, udpv6_rcv, 3980e219ae4SPaolo Abeni skb); 3994c64242aSTom Herbert if (ret > 0) { 4004c64242aSTom Herbert if (ipprot->flags & INET6_PROTO_FINAL) { 4014c64242aSTom Herbert /* Not an extension header, most likely UDP 4024c64242aSTom Herbert * encapsulation. Use return value as nexthdr 4034c64242aSTom Herbert * protocol not nhoff (which presumably is 4044c64242aSTom Herbert * not set by handler). 4054c64242aSTom Herbert */ 4064c64242aSTom Herbert nexthdr = ret; 4074c64242aSTom Herbert goto resubmit_final; 4084c64242aSTom Herbert } else { 4091da177e4SLinus Torvalds goto resubmit; 4104c64242aSTom Herbert } 4114c64242aSTom Herbert } else if (ret == 0) { 4121d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS); 4134c64242aSTom Herbert } 4141da177e4SLinus Torvalds } else { 41569d6da0bSPavel Emelyanov if (!raw) { 4161da177e4SLinus Torvalds if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { 4171d015503SEric Dumazet __IP6_INC_STATS(net, idev, 418483a47d2SDenis V. Lunev IPSTATS_MIB_INUNKNOWNPROTOS); 419fad87acaSPatrick McHardy icmpv6_send(skb, ICMPV6_PARAMPROB, 4203ffe533cSAlexey Dobriyan ICMPV6_UNK_NEXTHDR, nhoff); 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds kfree_skb(skb); 423d8c6f4b9SNeil Horman } else { 4241d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS); 425d8c6f4b9SNeil Horman consume_skb(skb); 426d8c6f4b9SNeil Horman } 4271da177e4SLinus Torvalds } 42880bde363SPaolo Abeni return; 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds discard: 4311d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 4321da177e4SLinus Torvalds kfree_skb(skb); 43380bde363SPaolo Abeni } 43480bde363SPaolo Abeni 43580bde363SPaolo Abeni static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb) 43680bde363SPaolo Abeni { 43780bde363SPaolo Abeni rcu_read_lock(); 43880bde363SPaolo Abeni ip6_protocol_deliver_rcu(net, skb, 0, false); 43980bde363SPaolo Abeni rcu_read_unlock(); 44080bde363SPaolo Abeni 4411da177e4SLinus Torvalds return 0; 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds int ip6_input(struct sk_buff *skb) 4461da177e4SLinus Torvalds { 44729a26a56SEric W. Biederman return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN, 44829a26a56SEric W. Biederman dev_net(skb->dev), NULL, skb, skb->dev, NULL, 4496e23ae2aSPatrick McHardy ip6_input_finish); 4501da177e4SLinus Torvalds } 451b4869aa2SDavid Ahern EXPORT_SYMBOL_GPL(ip6_input); 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds int ip6_mc_input(struct sk_buff *skb) 4541da177e4SLinus Torvalds { 4555226b6a9SMike Manning int sdif = inet6_sdif(skb); 456b71d1d42SEric Dumazet const struct ipv6hdr *hdr; 4575226b6a9SMike Manning struct net_device *dev; 458a50feda5SEric Dumazet bool deliver; 4591da177e4SLinus Torvalds 460c2005eb0SEric Dumazet __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev), 461bdb7cc64SStephen Suryaputra __in6_dev_get_safely(skb->dev), IPSTATS_MIB_INMCAST, 462edf391ffSNeil Horman skb->len); 4631da177e4SLinus Torvalds 4645226b6a9SMike Manning /* skb->dev passed may be master dev for vrfs. */ 4655226b6a9SMike Manning if (sdif) { 4665226b6a9SMike Manning rcu_read_lock(); 4675226b6a9SMike Manning dev = dev_get_by_index_rcu(dev_net(skb->dev), sdif); 4685226b6a9SMike Manning if (!dev) { 4695226b6a9SMike Manning rcu_read_unlock(); 4705226b6a9SMike Manning kfree_skb(skb); 4715226b6a9SMike Manning return -ENODEV; 4725226b6a9SMike Manning } 4735226b6a9SMike Manning } else { 4745226b6a9SMike Manning dev = skb->dev; 4755226b6a9SMike Manning } 4765226b6a9SMike Manning 4770660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 4785226b6a9SMike Manning deliver = ipv6_chk_mcast_addr(dev, &hdr->daddr, NULL); 4795226b6a9SMike Manning if (sdif) 4805226b6a9SMike Manning rcu_read_unlock(); 4811da177e4SLinus Torvalds 4827bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 4831da177e4SLinus Torvalds /* 4847bc570c8SYOSHIFUJI Hideaki * IPv6 multicast router mode is now supported ;) 4851da177e4SLinus Torvalds */ 48653b7997fSYOSHIFUJI Hideaki if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding && 487ddf64354SHannes Frederic Sowa !(ipv6_addr_type(&hdr->daddr) & 488ddf64354SHannes Frederic Sowa (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) && 4897bc570c8SYOSHIFUJI Hideaki likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) { 4907bc570c8SYOSHIFUJI Hideaki /* 4917bc570c8SYOSHIFUJI Hideaki * Okay, we try to forward - split and duplicate 4927bc570c8SYOSHIFUJI Hideaki * packets. 4937bc570c8SYOSHIFUJI Hideaki */ 4941da177e4SLinus Torvalds struct sk_buff *skb2; 4957bc570c8SYOSHIFUJI Hideaki struct inet6_skb_parm *opt = IP6CB(skb); 4961da177e4SLinus Torvalds 4977bc570c8SYOSHIFUJI Hideaki /* Check for MLD */ 498dd3332bfSYOSHIFUJI Hideaki / 吉藤英明 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) { 4997bc570c8SYOSHIFUJI Hideaki /* Check if this is a mld message */ 5007bc570c8SYOSHIFUJI Hideaki u8 nexthdr = hdr->nexthdr; 50175f2811cSJesse Gross __be16 frag_off; 5027bc570c8SYOSHIFUJI Hideaki int offset; 5031da177e4SLinus Torvalds 5047bc570c8SYOSHIFUJI Hideaki /* Check if the value of Router Alert 5057bc570c8SYOSHIFUJI Hideaki * is for MLD (0x0000). 5067bc570c8SYOSHIFUJI Hideaki */ 507dd3332bfSYOSHIFUJI Hideaki / 吉藤英明 if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) { 508a50feda5SEric Dumazet deliver = false; 509aba6096bSYOSHIFUJI Hideaki 5107bc570c8SYOSHIFUJI Hideaki if (!ipv6_ext_hdr(nexthdr)) { 5117bc570c8SYOSHIFUJI Hideaki /* BUG */ 512aba6096bSYOSHIFUJI Hideaki goto out; 5131da177e4SLinus Torvalds } 5147bc570c8SYOSHIFUJI Hideaki offset = ipv6_skip_exthdr(skb, sizeof(*hdr), 51575f2811cSJesse Gross &nexthdr, &frag_off); 5167bc570c8SYOSHIFUJI Hideaki if (offset < 0) 517aba6096bSYOSHIFUJI Hideaki goto out; 5187bc570c8SYOSHIFUJI Hideaki 5194c938d22SAngga if (ipv6_is_mld(skb, nexthdr, offset)) 520a50feda5SEric Dumazet deliver = true; 5214c938d22SAngga 5224c938d22SAngga goto out; 5237bc570c8SYOSHIFUJI Hideaki } 5247bc570c8SYOSHIFUJI Hideaki /* unknown RA - process it normally */ 5257bc570c8SYOSHIFUJI Hideaki } 5267bc570c8SYOSHIFUJI Hideaki 5277bc570c8SYOSHIFUJI Hideaki if (deliver) 5287bc570c8SYOSHIFUJI Hideaki skb2 = skb_clone(skb, GFP_ATOMIC); 5297bc570c8SYOSHIFUJI Hideaki else { 5307bc570c8SYOSHIFUJI Hideaki skb2 = skb; 5317bc570c8SYOSHIFUJI Hideaki skb = NULL; 5327bc570c8SYOSHIFUJI Hideaki } 5337bc570c8SYOSHIFUJI Hideaki 5347bc570c8SYOSHIFUJI Hideaki if (skb2) { 5357bc570c8SYOSHIFUJI Hideaki ip6_mr_input(skb2); 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds } 5387bc570c8SYOSHIFUJI Hideaki out: 539aba6096bSYOSHIFUJI Hideaki #endif 540aba6096bSYOSHIFUJI Hideaki if (likely(deliver)) 5411da177e4SLinus Torvalds ip6_input(skb); 542aba6096bSYOSHIFUJI Hideaki else { 5431da177e4SLinus Torvalds /* discard */ 5441da177e4SLinus Torvalds kfree_skb(skb); 545aba6096bSYOSHIFUJI Hideaki } 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds return 0; 5481da177e4SLinus Torvalds } 549