Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IPv6 input |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3 | * Linux INET6 implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Authors: |
| 6 | * Pedro Roque <roque@di.fc.ul.pt> |
| 7 | * Ian P. Morris <I.P.Morris@soton.ac.uk> |
| 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Based in linux/net/ipv4/ip_input.c |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License |
| 13 | * as published by the Free Software Foundation; either version |
| 14 | * 2 of the License, or (at your option) any later version. |
| 15 | */ |
| 16 | /* Changes |
| 17 | * |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 18 | * Mitsuru KANDA @USAGI and |
| 19 | * YOSHIFUJI Hideaki @USAGI: Remove ipv6_parse_exthdrs(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/socket.h> |
| 25 | #include <linux/sockios.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/net.h> |
| 27 | #include <linux/netdevice.h> |
| 28 | #include <linux/in6.h> |
| 29 | #include <linux/icmpv6.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 30 | #include <linux/mroute6.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | #include <linux/netfilter.h> |
| 34 | #include <linux/netfilter_ipv6.h> |
| 35 | |
| 36 | #include <net/sock.h> |
| 37 | #include <net/snmp.h> |
| 38 | |
| 39 | #include <net/ipv6.h> |
| 40 | #include <net/protocol.h> |
| 41 | #include <net/transp_v6.h> |
| 42 | #include <net/rawv6.h> |
| 43 | #include <net/ndisc.h> |
| 44 | #include <net/ip6_route.h> |
| 45 | #include <net/addrconf.h> |
| 46 | #include <net/xfrm.h> |
Eric Dumazet | 1f07d03 | 2013-08-06 03:32:11 -0700 | [diff] [blame] | 47 | #include <net/inet_ecn.h> |
Wei-Chun Chao | 48fb6b5 | 2015-07-22 18:13:12 -0700 | [diff] [blame] | 48 | #include <net/dst_metadata.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Edward Cree | d8269e2 | 2018-07-05 15:49:42 +0100 | [diff] [blame] | 50 | static void ip6_rcv_finish_core(struct net *net, struct sock *sk, |
| 51 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
subashab@codeaurora.org | dddb64b | 2017-03-23 13:34:16 -0600 | [diff] [blame] | 53 | void (*edemux)(struct sk_buff *skb); |
| 54 | |
Nikolay Borisov | e21145a | 2016-02-15 12:11:30 +0200 | [diff] [blame] | 55 | if (net->ipv4.sysctl_ip_early_demux && !skb_dst(skb) && skb->sk == NULL) { |
Eric Dumazet | c710998 | 2012-07-26 12:18:11 +0000 | [diff] [blame] | 56 | const struct inet6_protocol *ipprot; |
| 57 | |
Eric Dumazet | c710998 | 2012-07-26 12:18:11 +0000 | [diff] [blame] | 58 | ipprot = rcu_dereference(inet6_protos[ipv6_hdr(skb)->nexthdr]); |
subashab@codeaurora.org | dddb64b | 2017-03-23 13:34:16 -0600 | [diff] [blame] | 59 | if (ipprot && (edemux = READ_ONCE(ipprot->early_demux))) |
| 60 | edemux(skb); |
Eric Dumazet | c710998 | 2012-07-26 12:18:11 +0000 | [diff] [blame] | 61 | } |
Wei-Chun Chao | 48fb6b5 | 2015-07-22 18:13:12 -0700 | [diff] [blame] | 62 | if (!skb_valid_dst(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | ip6_route_input(skb); |
Edward Cree | d8269e2 | 2018-07-05 15:49:42 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb) |
| 67 | { |
| 68 | /* if ingress device is enslaved to an L3 master device pass the |
| 69 | * skb to its handler for processing |
| 70 | */ |
| 71 | skb = l3mdev_ip6_rcv(skb); |
| 72 | if (!skb) |
| 73 | return NET_RX_SUCCESS; |
| 74 | ip6_rcv_finish_core(net, sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | return dst_input(skb); |
| 77 | } |
| 78 | |
Edward Cree | d8269e2 | 2018-07-05 15:49:42 +0100 | [diff] [blame] | 79 | static void ip6_sublist_rcv_finish(struct list_head *head) |
| 80 | { |
| 81 | struct sk_buff *skb, *next; |
| 82 | |
| 83 | list_for_each_entry_safe(skb, next, head, list) |
| 84 | dst_input(skb); |
| 85 | } |
| 86 | |
| 87 | static void ip6_list_rcv_finish(struct net *net, struct sock *sk, |
| 88 | struct list_head *head) |
| 89 | { |
| 90 | struct dst_entry *curr_dst = NULL; |
| 91 | struct sk_buff *skb, *next; |
| 92 | struct list_head sublist; |
| 93 | |
| 94 | INIT_LIST_HEAD(&sublist); |
| 95 | list_for_each_entry_safe(skb, next, head, list) { |
| 96 | struct dst_entry *dst; |
| 97 | |
Edward Cree | 22f6bbb | 2018-12-04 17:37:57 +0000 | [diff] [blame] | 98 | skb_list_del_init(skb); |
Edward Cree | d8269e2 | 2018-07-05 15:49:42 +0100 | [diff] [blame] | 99 | /* if ingress device is enslaved to an L3 master device pass the |
| 100 | * skb to its handler for processing |
| 101 | */ |
| 102 | skb = l3mdev_ip6_rcv(skb); |
| 103 | if (!skb) |
| 104 | continue; |
| 105 | ip6_rcv_finish_core(net, sk, skb); |
| 106 | dst = skb_dst(skb); |
| 107 | if (curr_dst != dst) { |
| 108 | /* dispatch old sublist */ |
| 109 | if (!list_empty(&sublist)) |
| 110 | ip6_sublist_rcv_finish(&sublist); |
| 111 | /* start new sublist */ |
| 112 | INIT_LIST_HEAD(&sublist); |
| 113 | curr_dst = dst; |
| 114 | } |
| 115 | list_add_tail(&skb->list, &sublist); |
| 116 | } |
| 117 | /* dispatch final sublist */ |
| 118 | ip6_sublist_rcv_finish(&sublist); |
| 119 | } |
| 120 | |
| 121 | static struct sk_buff *ip6_rcv_core(struct sk_buff *skb, struct net_device *dev, |
| 122 | struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 124 | const struct ipv6hdr *hdr; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 125 | u32 pkt_len; |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 126 | struct inet6_dev *idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 128 | if (skb->pkt_type == PACKET_OTHERHOST) { |
| 129 | kfree_skb(skb); |
Edward Cree | d8269e2 | 2018-07-05 15:49:42 +0100 | [diff] [blame] | 130 | return NULL; |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 131 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 133 | rcu_read_lock(); |
| 134 | |
| 135 | idev = __in6_dev_get(skb->dev); |
| 136 | |
Eric Dumazet | c2005eb | 2016-04-27 16:44:41 -0700 | [diff] [blame] | 137 | __IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_IN, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
YOSHIFUJI Hideaki | 778d80b | 2008-06-28 14:17:11 +0900 | [diff] [blame] | 139 | if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL || |
| 140 | !idev || unlikely(idev->cnf.disable_ipv6)) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 141 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); |
Jesper Nilsson | 71f6f6d | 2009-03-27 00:17:45 -0700 | [diff] [blame] | 142 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Guillaume Chazarain | 6b7fdc3 | 2006-07-24 23:44:44 -0700 | [diff] [blame] | 145 | memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | /* |
| 148 | * Store incoming device index. When the packet will |
| 149 | * be queued, we cannot refer to skb->dev anymore. |
| 150 | * |
| 151 | * BTW, when we send a packet for our own local address on a |
| 152 | * non-loopback interface (e.g. ethX), it is being delivered |
Daniel Lezcano | de3cb74 | 2007-09-25 19:16:28 -0700 | [diff] [blame] | 153 | * via the loopback interface (lo) here; skb->dev = loopback_dev. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | * It, however, should be considered as if it is being |
| 155 | * arrived via the sending interface (ethX), because of the |
| 156 | * nature of scoping architecture. --yoshfuji |
| 157 | */ |
Wei-Chun Chao | 48fb6b5 | 2015-07-22 18:13:12 -0700 | [diff] [blame] | 158 | IP6CB(skb)->iif = skb_valid_dst(skb) ? ip6_dst_idev(skb_dst(skb))->dev->ifindex : dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Herbert Xu | 2889139 | 2006-06-30 13:35:46 -0700 | [diff] [blame] | 160 | if (unlikely(!pskb_may_pull(skb, sizeof(*hdr)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | goto err; |
| 162 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 163 | hdr = ipv6_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | if (hdr->version != 6) |
| 166 | goto err; |
| 167 | |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 168 | __IP6_ADD_STATS(net, idev, |
| 169 | IPSTATS_MIB_NOECTPKTS + |
Eric Dumazet | 1f07d03 | 2013-08-06 03:32:11 -0700 | [diff] [blame] | 170 | (ipv6_get_dsfield(hdr) & INET_ECN_MASK), |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 171 | max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs)); |
YOSHIFUJI Hideaki | f630e43 | 2008-06-19 16:33:57 -0700 | [diff] [blame] | 172 | /* |
| 173 | * RFC4291 2.5.3 |
Florian Westphal | 0aa8c13 | 2017-04-14 20:22:43 +0200 | [diff] [blame] | 174 | * The loopback address must not be used as the source address in IPv6 |
| 175 | * packets that are sent outside of a single node. [..] |
YOSHIFUJI Hideaki | f630e43 | 2008-06-19 16:33:57 -0700 | [diff] [blame] | 176 | * A packet received on an interface with a destination address |
| 177 | * of loopback must be dropped. |
| 178 | */ |
Florian Westphal | 0aa8c13 | 2017-04-14 20:22:43 +0200 | [diff] [blame] | 179 | if ((ipv6_addr_loopback(&hdr->saddr) || |
| 180 | ipv6_addr_loopback(&hdr->daddr)) && |
Robert Shearman | 3ede0bb | 2018-09-19 13:56:53 +0100 | [diff] [blame] | 181 | !(dev->flags & IFF_LOOPBACK) && |
| 182 | !netif_is_l3_master(dev)) |
YOSHIFUJI Hideaki | f630e43 | 2008-06-19 16:33:57 -0700 | [diff] [blame] | 183 | goto err; |
| 184 | |
Hannes Frederic Sowa | 1c4a154 | 2013-03-26 08:13:34 +0000 | [diff] [blame] | 185 | /* RFC4291 Errata ID: 3480 |
| 186 | * Interface-Local scope spans only a single interface on a |
| 187 | * node and is useful only for loopback transmission of |
| 188 | * multicast. Packets with interface-local scope received |
| 189 | * from another node must be discarded. |
| 190 | */ |
| 191 | if (!(skb->pkt_type == PACKET_LOOPBACK || |
| 192 | dev->flags & IFF_LOOPBACK) && |
| 193 | ipv6_addr_is_multicast(&hdr->daddr) && |
| 194 | IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 1) |
| 195 | goto err; |
| 196 | |
Johannes Berg | abbc304 | 2016-02-04 13:31:19 +0100 | [diff] [blame] | 197 | /* If enabled, drop unicast packets that were encapsulated in link-layer |
| 198 | * multicast or broadcast to protected against the so-called "hole-196" |
| 199 | * attack in 802.11 wireless. |
| 200 | */ |
| 201 | if (!ipv6_addr_is_multicast(&hdr->daddr) && |
| 202 | (skb->pkt_type == PACKET_BROADCAST || |
| 203 | skb->pkt_type == PACKET_MULTICAST) && |
| 204 | idev->cnf.drop_unicast_in_l2_multicast) |
| 205 | goto err; |
| 206 | |
Hannes Frederic Sowa | 2031409 | 2013-02-10 05:35:22 +0000 | [diff] [blame] | 207 | /* RFC4291 2.7 |
| 208 | * Nodes must not originate a packet to a multicast address whose scope |
| 209 | * field contains the reserved value 0; if such a packet is received, it |
| 210 | * must be silently dropped. |
| 211 | */ |
| 212 | if (ipv6_addr_is_multicast(&hdr->daddr) && |
| 213 | IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 0) |
| 214 | goto err; |
| 215 | |
Brian Haley | c457338 | 2011-11-08 04:41:42 +0000 | [diff] [blame] | 216 | /* |
| 217 | * RFC4291 2.7 |
| 218 | * Multicast addresses must not be used as source addresses in IPv6 |
| 219 | * packets or appear in any Routing header. |
| 220 | */ |
| 221 | if (ipv6_addr_is_multicast(&hdr->saddr)) |
| 222 | goto err; |
| 223 | |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 224 | skb->transport_header = skb->network_header + sizeof(*hdr); |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 225 | IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr); |
| 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | pkt_len = ntohs(hdr->payload_len); |
| 228 | |
| 229 | /* pkt_len may be zero if Jumbo payload option is present */ |
| 230 | if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) { |
Mitsuru Chinen | 60e5c16 | 2007-04-04 23:54:59 -0700 | [diff] [blame] | 231 | if (pkt_len + sizeof(struct ipv6hdr) > skb->len) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 232 | __IP6_INC_STATS(net, |
| 233 | idev, IPSTATS_MIB_INTRUNCATEDPKTS); |
Mitsuru Chinen | 60e5c16 | 2007-04-04 23:54:59 -0700 | [diff] [blame] | 234 | goto drop; |
| 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 237 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | goto drop; |
| 239 | } |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 240 | hdr = ipv6_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | if (hdr->nexthdr == NEXTHDR_HOP) { |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 244 | if (ipv6_parse_hopopts(skb) < 0) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 245 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 246 | rcu_read_unlock(); |
Edward Cree | d8269e2 | 2018-07-05 15:49:42 +0100 | [diff] [blame] | 247 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 251 | rcu_read_unlock(); |
| 252 | |
Herbert Xu | 71f9dac | 2009-06-26 19:22:37 -0700 | [diff] [blame] | 253 | /* Must drop socket now because of tproxy. */ |
| 254 | skb_orphan(skb); |
| 255 | |
Edward Cree | d8269e2 | 2018-07-05 15:49:42 +0100 | [diff] [blame] | 256 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | err: |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 258 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | drop: |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 260 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | kfree_skb(skb); |
Edward Cree | d8269e2 | 2018-07-05 15:49:42 +0100 | [diff] [blame] | 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) |
| 266 | { |
| 267 | struct net *net = dev_net(skb->dev); |
| 268 | |
| 269 | skb = ip6_rcv_core(skb, dev, net); |
| 270 | if (skb == NULL) |
| 271 | return NET_RX_DROP; |
| 272 | return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, |
| 273 | net, NULL, skb, dev, NULL, |
| 274 | ip6_rcv_finish); |
| 275 | } |
| 276 | |
| 277 | static void ip6_sublist_rcv(struct list_head *head, struct net_device *dev, |
| 278 | struct net *net) |
| 279 | { |
| 280 | NF_HOOK_LIST(NFPROTO_IPV6, NF_INET_PRE_ROUTING, net, NULL, |
| 281 | head, dev, NULL, ip6_rcv_finish); |
| 282 | ip6_list_rcv_finish(net, NULL, head); |
| 283 | } |
| 284 | |
| 285 | /* Receive a list of IPv6 packets */ |
| 286 | void ipv6_list_rcv(struct list_head *head, struct packet_type *pt, |
| 287 | struct net_device *orig_dev) |
| 288 | { |
| 289 | struct net_device *curr_dev = NULL; |
| 290 | struct net *curr_net = NULL; |
| 291 | struct sk_buff *skb, *next; |
| 292 | struct list_head sublist; |
| 293 | |
| 294 | INIT_LIST_HEAD(&sublist); |
| 295 | list_for_each_entry_safe(skb, next, head, list) { |
| 296 | struct net_device *dev = skb->dev; |
| 297 | struct net *net = dev_net(dev); |
| 298 | |
Edward Cree | 22f6bbb | 2018-12-04 17:37:57 +0000 | [diff] [blame] | 299 | skb_list_del_init(skb); |
Edward Cree | d8269e2 | 2018-07-05 15:49:42 +0100 | [diff] [blame] | 300 | skb = ip6_rcv_core(skb, dev, net); |
| 301 | if (skb == NULL) |
| 302 | continue; |
| 303 | |
| 304 | if (curr_dev != dev || curr_net != net) { |
| 305 | /* dispatch old sublist */ |
| 306 | if (!list_empty(&sublist)) |
| 307 | ip6_sublist_rcv(&sublist, curr_dev, curr_net); |
| 308 | /* start new sublist */ |
| 309 | INIT_LIST_HEAD(&sublist); |
| 310 | curr_dev = dev; |
| 311 | curr_net = net; |
| 312 | } |
| 313 | list_add_tail(&skb->list, &sublist); |
| 314 | } |
| 315 | /* dispatch final sublist */ |
| 316 | ip6_sublist_rcv(&sublist, curr_dev, curr_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Deliver the packet to the host |
| 321 | */ |
Paolo Abeni | 80bde36 | 2018-11-07 12:38:32 +0100 | [diff] [blame] | 322 | void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr, |
| 323 | bool have_final) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
Alexey Dobriyan | 41135cc | 2009-09-14 12:22:28 +0000 | [diff] [blame] | 325 | const struct inet6_protocol *ipprot; |
David S. Miller | f9242b6 | 2012-06-19 18:56:21 -0700 | [diff] [blame] | 326 | struct inet6_dev *idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | unsigned int nhoff; |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 328 | bool raw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | /* |
| 331 | * Parse extension headers |
| 332 | */ |
| 333 | |
David S. Miller | 1b0ccfe | 2015-06-10 15:29:31 -0700 | [diff] [blame] | 334 | resubmit: |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 335 | idev = ip6_dst_idev(skb_dst(skb)); |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 336 | nhoff = IP6CB(skb)->nhoff; |
Paolo Abeni | 80bde36 | 2018-11-07 12:38:32 +0100 | [diff] [blame] | 337 | if (!have_final) { |
| 338 | if (!pskb_pull(skb, skb_transport_offset(skb))) |
| 339 | goto discard; |
| 340 | nexthdr = skb_network_header(skb)[nhoff]; |
| 341 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Tom Herbert | 4c64242 | 2016-05-18 09:06:11 -0700 | [diff] [blame] | 343 | resubmit_final: |
Pavel Emelyanov | 69d6da0 | 2007-11-19 22:35:57 -0800 | [diff] [blame] | 344 | raw = raw6_local_deliver(skb, nexthdr); |
Ian Morris | e5d08d7 | 2014-11-23 21:28:43 +0000 | [diff] [blame] | 345 | ipprot = rcu_dereference(inet6_protos[nexthdr]); |
Ian Morris | 53b24b8 | 2015-03-29 14:00:05 +0100 | [diff] [blame] | 346 | if (ipprot) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | int ret; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 348 | |
Tom Herbert | 1da44f9 | 2016-05-18 09:06:12 -0700 | [diff] [blame] | 349 | if (have_final) { |
| 350 | if (!(ipprot->flags & INET6_PROTO_FINAL)) { |
| 351 | /* Once we've seen a final protocol don't |
| 352 | * allow encapsulation on any non-final |
| 353 | * ones. This allows foo in UDP encapsulation |
| 354 | * to work. |
| 355 | */ |
| 356 | goto discard; |
| 357 | } |
| 358 | } else if (ipprot->flags & INET6_PROTO_FINAL) { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 359 | const struct ipv6hdr *hdr; |
Mike Manning | 5226b6a | 2018-11-07 15:36:09 +0000 | [diff] [blame] | 360 | int sdif = inet6_sdif(skb); |
| 361 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Tom Herbert | 1da44f9 | 2016-05-18 09:06:12 -0700 | [diff] [blame] | 363 | /* Only do this once for first final protocol */ |
| 364 | have_final = true; |
| 365 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 366 | /* Free reference early: we don't need it any more, |
| 367 | and it may hold ip_conntrack module loaded |
| 368 | indefinitely. */ |
| 369 | nf_reset(skb); |
| 370 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 371 | skb_postpull_rcsum(skb, skb_network_header(skb), |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 372 | skb_network_header_len(skb)); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 373 | hdr = ipv6_hdr(skb); |
Mike Manning | 5226b6a | 2018-11-07 15:36:09 +0000 | [diff] [blame] | 374 | |
| 375 | /* skb->dev passed may be master dev for vrfs. */ |
| 376 | if (sdif) { |
| 377 | dev = dev_get_by_index_rcu(net, sdif); |
| 378 | if (!dev) |
| 379 | goto discard; |
| 380 | } else { |
| 381 | dev = skb->dev; |
| 382 | } |
| 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | if (ipv6_addr_is_multicast(&hdr->daddr) && |
Mike Manning | 5226b6a | 2018-11-07 15:36:09 +0000 | [diff] [blame] | 385 | !ipv6_chk_mcast_addr(dev, &hdr->daddr, |
| 386 | &hdr->saddr) && |
YOSHIFUJI Hideaki / 吉藤英明 | daad151 | 2013-01-13 05:02:18 +0000 | [diff] [blame] | 387 | !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | goto discard; |
| 389 | } |
| 390 | if (!(ipprot->flags & INET6_PROTO_NOPOLICY) && |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 391 | !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | goto discard; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 393 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 394 | ret = ipprot->handler(skb); |
Tom Herbert | 4c64242 | 2016-05-18 09:06:11 -0700 | [diff] [blame] | 395 | if (ret > 0) { |
| 396 | if (ipprot->flags & INET6_PROTO_FINAL) { |
| 397 | /* Not an extension header, most likely UDP |
| 398 | * encapsulation. Use return value as nexthdr |
| 399 | * protocol not nhoff (which presumably is |
| 400 | * not set by handler). |
| 401 | */ |
| 402 | nexthdr = ret; |
| 403 | goto resubmit_final; |
| 404 | } else { |
| 405 | goto resubmit; |
| 406 | } |
| 407 | } else if (ret == 0) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 408 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS); |
Tom Herbert | 4c64242 | 2016-05-18 09:06:11 -0700 | [diff] [blame] | 409 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } else { |
Pavel Emelyanov | 69d6da0 | 2007-11-19 22:35:57 -0800 | [diff] [blame] | 411 | if (!raw) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 413 | __IP6_INC_STATS(net, idev, |
| 414 | IPSTATS_MIB_INUNKNOWNPROTOS); |
Patrick McHardy | fad87ac | 2005-08-16 21:03:41 -0700 | [diff] [blame] | 415 | icmpv6_send(skb, ICMPV6_PARAMPROB, |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 416 | ICMPV6_UNK_NEXTHDR, nhoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
Neil Horman | d8c6f4b | 2013-03-01 07:44:08 +0000 | [diff] [blame] | 418 | kfree_skb(skb); |
| 419 | } else { |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 420 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS); |
Neil Horman | d8c6f4b | 2013-03-01 07:44:08 +0000 | [diff] [blame] | 421 | consume_skb(skb); |
| 422 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | } |
Paolo Abeni | 80bde36 | 2018-11-07 12:38:32 +0100 | [diff] [blame] | 424 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
| 426 | discard: |
Eric Dumazet | 1d01550 | 2016-04-27 16:44:40 -0700 | [diff] [blame] | 427 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | kfree_skb(skb); |
Paolo Abeni | 80bde36 | 2018-11-07 12:38:32 +0100 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb) |
| 432 | { |
| 433 | rcu_read_lock(); |
| 434 | ip6_protocol_deliver_rcu(net, skb, 0, false); |
| 435 | rcu_read_unlock(); |
| 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | |
| 441 | int ip6_input(struct sk_buff *skb) |
| 442 | { |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 443 | return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN, |
| 444 | dev_net(skb->dev), NULL, skb, skb->dev, NULL, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 445 | ip6_input_finish); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | } |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 447 | EXPORT_SYMBOL_GPL(ip6_input); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
| 449 | int ip6_mc_input(struct sk_buff *skb) |
| 450 | { |
Mike Manning | 5226b6a | 2018-11-07 15:36:09 +0000 | [diff] [blame] | 451 | int sdif = inet6_sdif(skb); |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 452 | const struct ipv6hdr *hdr; |
Mike Manning | 5226b6a | 2018-11-07 15:36:09 +0000 | [diff] [blame] | 453 | struct net_device *dev; |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 454 | bool deliver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
Eric Dumazet | c2005eb | 2016-04-27 16:44:41 -0700 | [diff] [blame] | 456 | __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev), |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 457 | __in6_dev_get_safely(skb->dev), IPSTATS_MIB_INMCAST, |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 458 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
Mike Manning | 5226b6a | 2018-11-07 15:36:09 +0000 | [diff] [blame] | 460 | /* skb->dev passed may be master dev for vrfs. */ |
| 461 | if (sdif) { |
| 462 | rcu_read_lock(); |
| 463 | dev = dev_get_by_index_rcu(dev_net(skb->dev), sdif); |
| 464 | if (!dev) { |
| 465 | rcu_read_unlock(); |
| 466 | kfree_skb(skb); |
| 467 | return -ENODEV; |
| 468 | } |
| 469 | } else { |
| 470 | dev = skb->dev; |
| 471 | } |
| 472 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 473 | hdr = ipv6_hdr(skb); |
Mike Manning | 5226b6a | 2018-11-07 15:36:09 +0000 | [diff] [blame] | 474 | deliver = ipv6_chk_mcast_addr(dev, &hdr->daddr, NULL); |
| 475 | if (sdif) |
| 476 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 478 | #ifdef CONFIG_IPV6_MROUTE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | /* |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 480 | * IPv6 multicast router mode is now supported ;) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | */ |
YOSHIFUJI Hideaki | 53b7997 | 2008-07-19 22:35:03 -0700 | [diff] [blame] | 482 | if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding && |
Hannes Frederic Sowa | ddf6435 | 2013-03-08 02:07:23 +0000 | [diff] [blame] | 483 | !(ipv6_addr_type(&hdr->daddr) & |
| 484 | (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) && |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 485 | likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) { |
| 486 | /* |
| 487 | * Okay, we try to forward - split and duplicate |
| 488 | * packets. |
| 489 | */ |
| 490 | struct sk_buff *skb2; |
| 491 | struct inet6_skb_parm *opt = IP6CB(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 493 | /* Check for MLD */ |
YOSHIFUJI Hideaki / 吉藤英明 | dd3332b | 2013-01-13 05:02:45 +0000 | [diff] [blame] | 494 | if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 495 | /* Check if this is a mld message */ |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 496 | u8 nexthdr = hdr->nexthdr; |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 497 | __be16 frag_off; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 498 | int offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 500 | /* Check if the value of Router Alert |
| 501 | * is for MLD (0x0000). |
| 502 | */ |
YOSHIFUJI Hideaki / 吉藤英明 | dd3332b | 2013-01-13 05:02:45 +0000 | [diff] [blame] | 503 | if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) { |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 504 | deliver = false; |
YOSHIFUJI Hideaki | aba6096 | 2008-04-10 15:41:26 +0900 | [diff] [blame] | 505 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 506 | if (!ipv6_ext_hdr(nexthdr)) { |
| 507 | /* BUG */ |
YOSHIFUJI Hideaki | aba6096 | 2008-04-10 15:41:26 +0900 | [diff] [blame] | 508 | goto out; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 509 | } |
| 510 | offset = ipv6_skip_exthdr(skb, sizeof(*hdr), |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 511 | &nexthdr, &frag_off); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 512 | if (offset < 0) |
YOSHIFUJI Hideaki | aba6096 | 2008-04-10 15:41:26 +0900 | [diff] [blame] | 513 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
Angga | 4c938d2 | 2015-07-03 14:40:52 +1200 | [diff] [blame] | 515 | if (ipv6_is_mld(skb, nexthdr, offset)) |
| 516 | deliver = true; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 517 | |
Angga | 4c938d2 | 2015-07-03 14:40:52 +1200 | [diff] [blame] | 518 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 520 | /* unknown RA - process it normally */ |
| 521 | } |
| 522 | |
| 523 | if (deliver) |
| 524 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 525 | else { |
| 526 | skb2 = skb; |
| 527 | skb = NULL; |
| 528 | } |
| 529 | |
| 530 | if (skb2) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 531 | ip6_mr_input(skb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } |
| 533 | } |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 534 | out: |
YOSHIFUJI Hideaki | aba6096 | 2008-04-10 15:41:26 +0900 | [diff] [blame] | 535 | #endif |
| 536 | if (likely(deliver)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | ip6_input(skb); |
YOSHIFUJI Hideaki | aba6096 | 2008-04-10 15:41:26 +0900 | [diff] [blame] | 538 | else { |
| 539 | /* discard */ |
| 540 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
| 543 | return 0; |
| 544 | } |