blob: fa014d5f17321eb6ad1ef15583ed12798f35f118 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * IPv6 input
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Authors:
7 * Pedro Roque <roque@di.fc.ul.pt>
8 * Ian P. Morris <I.P.Morris@soton.ac.uk>
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Based in linux/net/ipv4/ip_input.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12/* Changes
13 *
Ian Morris67ba4152014-08-24 21:53:10 +010014 * Mitsuru KANDA @USAGI and
15 * YOSHIFUJI Hideaki @USAGI: Remove ipv6_parse_exthdrs().
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
18#include <linux/errno.h>
19#include <linux/types.h>
20#include <linux/socket.h>
21#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/net.h>
23#include <linux/netdevice.h>
24#include <linux/in6.h>
25#include <linux/icmpv6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090026#include <linux/mroute6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Paolo Abeni0e219ae2019-05-03 17:01:37 +020028#include <linux/indirect_call_wrapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <linux/netfilter.h>
31#include <linux/netfilter_ipv6.h>
32
33#include <net/sock.h>
34#include <net/snmp.h>
35
36#include <net/ipv6.h>
37#include <net/protocol.h>
38#include <net/transp_v6.h>
39#include <net/rawv6.h>
40#include <net/ndisc.h>
41#include <net/ip6_route.h>
42#include <net/addrconf.h>
43#include <net/xfrm.h>
Eric Dumazet1f07d032013-08-06 03:32:11 -070044#include <net/inet_ecn.h>
Wei-Chun Chao48fb6b52015-07-22 18:13:12 -070045#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Paolo Abeni97ff7ff2019-05-03 17:01:38 +020047INDIRECT_CALLABLE_DECLARE(void udp_v6_early_demux(struct sk_buff *));
48INDIRECT_CALLABLE_DECLARE(void tcp_v6_early_demux(struct sk_buff *));
Edward Creed8269e22018-07-05 15:49:42 +010049static void ip6_rcv_finish_core(struct net *net, struct sock *sk,
50 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
subashab@codeaurora.orgdddb64b2017-03-23 13:34:16 -060052 void (*edemux)(struct sk_buff *skb);
53
Nikolay Borisove21145a2016-02-15 12:11:30 +020054 if (net->ipv4.sysctl_ip_early_demux && !skb_dst(skb) && skb->sk == NULL) {
Eric Dumazetc7109982012-07-26 12:18:11 +000055 const struct inet6_protocol *ipprot;
56
Eric Dumazetc7109982012-07-26 12:18:11 +000057 ipprot = rcu_dereference(inet6_protos[ipv6_hdr(skb)->nexthdr]);
subashab@codeaurora.orgdddb64b2017-03-23 13:34:16 -060058 if (ipprot && (edemux = READ_ONCE(ipprot->early_demux)))
Paolo Abeni97ff7ff2019-05-03 17:01:38 +020059 INDIRECT_CALL_2(edemux, tcp_v6_early_demux,
60 udp_v6_early_demux, skb);
Eric Dumazetc7109982012-07-26 12:18:11 +000061 }
Wei-Chun Chao48fb6b52015-07-22 18:13:12 -070062 if (!skb_valid_dst(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 ip6_route_input(skb);
Edward Creed8269e22018-07-05 15:49:42 +010064}
65
66int 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 Torvalds1da177e2005-04-16 15:20:36 -070075
76 return dst_input(skb);
77}
78
Edward Creed8269e22018-07-05 15:49:42 +010079static 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
87static 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 Cree22f6bbb2018-12-04 17:37:57 +000098 skb_list_del_init(skb);
Edward Creed8269e22018-07-05 15:49:42 +010099 /* 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
121static struct sk_buff *ip6_rcv_core(struct sk_buff *skb, struct net_device *dev,
122 struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000124 const struct ipv6hdr *hdr;
Ian Morris67ba4152014-08-24 21:53:10 +0100125 u32 pkt_len;
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900126 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900128 if (skb->pkt_type == PACKET_OTHERHOST) {
129 kfree_skb(skb);
Edward Creed8269e22018-07-05 15:49:42 +0100130 return NULL;
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900133 rcu_read_lock();
134
135 idev = __in6_dev_get(skb->dev);
136
Eric Dumazetc2005eb2016-04-27 16:44:41 -0700137 __IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_IN, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900139 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL ||
140 !idev || unlikely(idev->cnf.disable_ipv6)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700141 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
Jesper Nilsson71f6f6d2009-03-27 00:17:45 -0700142 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
Guillaume Chazarain6b7fdc32006-07-24 23:44:44 -0700145 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /*
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 Lezcanode3cb742007-09-25 19:16:28 -0700153 * via the loopback interface (lo) here; skb->dev = loopback_dev.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * 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 Chao48fb6b52015-07-22 18:13:12 -0700158 IP6CB(skb)->iif = skb_valid_dst(skb) ? ip6_dst_idev(skb_dst(skb))->dev->ifindex : dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Herbert Xu28891392006-06-30 13:35:46 -0700160 if (unlikely(!pskb_may_pull(skb, sizeof(*hdr))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 goto err;
162
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700163 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 if (hdr->version != 6)
166 goto err;
167
Eric Dumazet1d015502016-04-27 16:44:40 -0700168 __IP6_ADD_STATS(net, idev,
169 IPSTATS_MIB_NOECTPKTS +
Eric Dumazet1f07d032013-08-06 03:32:11 -0700170 (ipv6_get_dsfield(hdr) & INET_ECN_MASK),
Eric Dumazet1d015502016-04-27 16:44:40 -0700171 max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs));
YOSHIFUJI Hideakif630e432008-06-19 16:33:57 -0700172 /*
173 * RFC4291 2.5.3
Florian Westphal0aa8c132017-04-14 20:22:43 +0200174 * The loopback address must not be used as the source address in IPv6
175 * packets that are sent outside of a single node. [..]
YOSHIFUJI Hideakif630e432008-06-19 16:33:57 -0700176 * A packet received on an interface with a destination address
177 * of loopback must be dropped.
178 */
Florian Westphal0aa8c132017-04-14 20:22:43 +0200179 if ((ipv6_addr_loopback(&hdr->saddr) ||
180 ipv6_addr_loopback(&hdr->daddr)) &&
Robert Shearman3ede0bb2018-09-19 13:56:53 +0100181 !(dev->flags & IFF_LOOPBACK) &&
182 !netif_is_l3_master(dev))
YOSHIFUJI Hideakif630e432008-06-19 16:33:57 -0700183 goto err;
184
Hannes Frederic Sowa1c4a1542013-03-26 08:13:34 +0000185 /* 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 Bergabbc3042016-02-04 13:31:19 +0100197 /* 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 Sowa20314092013-02-10 05:35:22 +0000207 /* 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 Haleyc4573382011-11-08 04:41:42 +0000216 /*
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 Melob0e380b12007-04-10 21:21:55 -0700224 skb->transport_header = skb->network_header + sizeof(*hdr);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800225 IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 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 Chinen60e5c162007-04-04 23:54:59 -0700231 if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700232 __IP6_INC_STATS(net,
233 idev, IPSTATS_MIB_INTRUNCATEDPKTS);
Mitsuru Chinen60e5c162007-04-04 23:54:59 -0700234 goto drop;
235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700237 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto drop;
239 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700240 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
243 if (hdr->nexthdr == NEXTHDR_HOP) {
Herbert Xue5bbef22007-10-15 12:50:28 -0700244 if (ipv6_parse_hopopts(skb) < 0) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700245 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900246 rcu_read_unlock();
Edward Creed8269e22018-07-05 15:49:42 +0100247 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900251 rcu_read_unlock();
252
Herbert Xu71f9dac2009-06-26 19:22:37 -0700253 /* Must drop socket now because of tproxy. */
254 skb_orphan(skb);
255
Edward Creed8269e22018-07-05 15:49:42 +0100256 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257err:
Eric Dumazet1d015502016-04-27 16:44:40 -0700258 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259drop:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900260 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 kfree_skb(skb);
Edward Creed8269e22018-07-05 15:49:42 +0100262 return NULL;
263}
264
265int 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
277static 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 */
286void 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 Cree22f6bbb2018-12-04 17:37:57 +0000299 skb_list_del_init(skb);
Edward Creed8269e22018-07-05 15:49:42 +0100300 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 Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Paolo Abeni0e219ae2019-05-03 17:01:37 +0200319INDIRECT_CALLABLE_DECLARE(int udpv6_rcv(struct sk_buff *));
320INDIRECT_CALLABLE_DECLARE(int tcp_v6_rcv(struct sk_buff *));
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/*
323 * Deliver the packet to the host
324 */
Paolo Abeni80bde362018-11-07 12:38:32 +0100325void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr,
326 bool have_final)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000328 const struct inet6_protocol *ipprot;
David S. Millerf9242b62012-06-19 18:56:21 -0700329 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 unsigned int nhoff;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000331 bool raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /*
334 * Parse extension headers
335 */
336
David S. Miller1b0ccfe2015-06-10 15:29:31 -0700337resubmit:
Eric Dumazetadf30902009-06-02 05:19:30 +0000338 idev = ip6_dst_idev(skb_dst(skb));
Patrick McHardy951dbc82006-01-06 23:02:34 -0800339 nhoff = IP6CB(skb)->nhoff;
Paolo Abeni80bde362018-11-07 12:38:32 +0100340 if (!have_final) {
341 if (!pskb_pull(skb, skb_transport_offset(skb)))
342 goto discard;
343 nexthdr = skb_network_header(skb)[nhoff];
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Tom Herbert4c642422016-05-18 09:06:11 -0700346resubmit_final:
Pavel Emelyanov69d6da02007-11-19 22:35:57 -0800347 raw = raw6_local_deliver(skb, nexthdr);
Ian Morrise5d08d72014-11-23 21:28:43 +0000348 ipprot = rcu_dereference(inet6_protos[nexthdr]);
Ian Morris53b24b82015-03-29 14:00:05 +0100349 if (ipprot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int ret;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900351
Tom Herbert1da44f92016-05-18 09:06:12 -0700352 if (have_final) {
353 if (!(ipprot->flags & INET6_PROTO_FINAL)) {
354 /* Once we've seen a final protocol don't
355 * allow encapsulation on any non-final
356 * ones. This allows foo in UDP encapsulation
357 * to work.
358 */
359 goto discard;
360 }
361 } else if (ipprot->flags & INET6_PROTO_FINAL) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000362 const struct ipv6hdr *hdr;
Mike Manning5226b6a2018-11-07 15:36:09 +0000363 int sdif = inet6_sdif(skb);
364 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Tom Herbert1da44f92016-05-18 09:06:12 -0700366 /* Only do this once for first final protocol */
367 have_final = true;
368
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800369 /* Free reference early: we don't need it any more,
370 and it may hold ip_conntrack module loaded
371 indefinitely. */
372 nf_reset(skb);
373
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700374 skb_postpull_rcsum(skb, skb_network_header(skb),
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300375 skb_network_header_len(skb));
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700376 hdr = ipv6_hdr(skb);
Mike Manning5226b6a2018-11-07 15:36:09 +0000377
378 /* skb->dev passed may be master dev for vrfs. */
379 if (sdif) {
380 dev = dev_get_by_index_rcu(net, sdif);
381 if (!dev)
382 goto discard;
383 } else {
384 dev = skb->dev;
385 }
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (ipv6_addr_is_multicast(&hdr->daddr) &&
Mike Manning5226b6a2018-11-07 15:36:09 +0000388 !ipv6_chk_mcast_addr(dev, &hdr->daddr,
389 &hdr->saddr) &&
YOSHIFUJI Hideaki / 吉藤英明daad1512013-01-13 05:02:18 +0000390 !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 goto discard;
392 }
393 if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900394 !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 goto discard;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900396
Paolo Abeni0e219ae2019-05-03 17:01:37 +0200397 ret = INDIRECT_CALL_2(ipprot->handler, tcp_v6_rcv, udpv6_rcv,
398 skb);
Tom Herbert4c642422016-05-18 09:06:11 -0700399 if (ret > 0) {
400 if (ipprot->flags & INET6_PROTO_FINAL) {
401 /* Not an extension header, most likely UDP
402 * encapsulation. Use return value as nexthdr
403 * protocol not nhoff (which presumably is
404 * not set by handler).
405 */
406 nexthdr = ret;
407 goto resubmit_final;
408 } else {
409 goto resubmit;
410 }
411 } else if (ret == 0) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700412 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
Tom Herbert4c642422016-05-18 09:06:11 -0700413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 } else {
Pavel Emelyanov69d6da02007-11-19 22:35:57 -0800415 if (!raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700417 __IP6_INC_STATS(net, idev,
418 IPSTATS_MIB_INUNKNOWNPROTOS);
Patrick McHardyfad87ac2005-08-16 21:03:41 -0700419 icmpv6_send(skb, ICMPV6_PARAMPROB,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000420 ICMPV6_UNK_NEXTHDR, nhoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
Neil Hormand8c6f4b2013-03-01 07:44:08 +0000422 kfree_skb(skb);
423 } else {
Eric Dumazet1d015502016-04-27 16:44:40 -0700424 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
Neil Hormand8c6f4b2013-03-01 07:44:08 +0000425 consume_skb(skb);
426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Paolo Abeni80bde362018-11-07 12:38:32 +0100428 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430discard:
Eric Dumazet1d015502016-04-27 16:44:40 -0700431 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 kfree_skb(skb);
Paolo Abeni80bde362018-11-07 12:38:32 +0100433}
434
435static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
436{
437 rcu_read_lock();
438 ip6_protocol_deliver_rcu(net, skb, 0, false);
439 rcu_read_unlock();
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 0;
442}
443
444
445int ip6_input(struct sk_buff *skb)
446{
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500447 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
448 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800449 ip6_input_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
David Ahernb4869aa2016-06-06 20:50:40 -0700451EXPORT_SYMBOL_GPL(ip6_input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453int ip6_mc_input(struct sk_buff *skb)
454{
Mike Manning5226b6a2018-11-07 15:36:09 +0000455 int sdif = inet6_sdif(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000456 const struct ipv6hdr *hdr;
Mike Manning5226b6a2018-11-07 15:36:09 +0000457 struct net_device *dev;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000458 bool deliver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Eric Dumazetc2005eb2016-04-27 16:44:41 -0700460 __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev),
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -0400461 __in6_dev_get_safely(skb->dev), IPSTATS_MIB_INMCAST,
Neil Hormanedf391f2009-04-27 02:45:02 -0700462 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Mike Manning5226b6a2018-11-07 15:36:09 +0000464 /* skb->dev passed may be master dev for vrfs. */
465 if (sdif) {
466 rcu_read_lock();
467 dev = dev_get_by_index_rcu(dev_net(skb->dev), sdif);
468 if (!dev) {
469 rcu_read_unlock();
470 kfree_skb(skb);
471 return -ENODEV;
472 }
473 } else {
474 dev = skb->dev;
475 }
476
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700477 hdr = ipv6_hdr(skb);
Mike Manning5226b6a2018-11-07 15:36:09 +0000478 deliver = ipv6_chk_mcast_addr(dev, &hdr->daddr, NULL);
479 if (sdif)
480 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900482#ifdef CONFIG_IPV6_MROUTE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /*
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900484 * IPv6 multicast router mode is now supported ;)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 */
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700486 if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding &&
Hannes Frederic Sowaddf64352013-03-08 02:07:23 +0000487 !(ipv6_addr_type(&hdr->daddr) &
488 (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900489 likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) {
490 /*
491 * Okay, we try to forward - split and duplicate
492 * packets.
493 */
494 struct sk_buff *skb2;
495 struct inet6_skb_parm *opt = IP6CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900497 /* Check for MLD */
YOSHIFUJI Hideaki / 吉藤英明dd3332b2013-01-13 05:02:45 +0000498 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900499 /* Check if this is a mld message */
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900500 u8 nexthdr = hdr->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800501 __be16 frag_off;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900502 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900504 /* Check if the value of Router Alert
505 * is for MLD (0x0000).
506 */
YOSHIFUJI Hideaki / 吉藤英明dd3332b2013-01-13 05:02:45 +0000507 if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) {
Eric Dumazeta50feda2012-05-18 18:57:34 +0000508 deliver = false;
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900509
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900510 if (!ipv6_ext_hdr(nexthdr)) {
511 /* BUG */
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900512 goto out;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900513 }
514 offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
Jesse Gross75f28112011-11-30 17:05:51 -0800515 &nexthdr, &frag_off);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900516 if (offset < 0)
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900517 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Angga4c938d22015-07-03 14:40:52 +1200519 if (ipv6_is_mld(skb, nexthdr, offset))
520 deliver = true;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900521
Angga4c938d22015-07-03 14:40:52 +1200522 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900524 /* unknown RA - process it normally */
525 }
526
527 if (deliver)
528 skb2 = skb_clone(skb, GFP_ATOMIC);
529 else {
530 skb2 = skb;
531 skb = NULL;
532 }
533
534 if (skb2) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900535 ip6_mr_input(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900538out:
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900539#endif
540 if (likely(deliver))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 ip6_input(skb);
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900542 else {
543 /* discard */
544 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 return 0;
548}