blob: c7ed2b6d5a1dd192cc776231ede06cef8f78065d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 input
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Ian P. Morris <I.P.Morris@soton.ac.uk>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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 Morris67ba4152014-08-24 21:53:10 +010018 * Mitsuru KANDA @USAGI and
19 * YOSHIFUJI Hideaki @USAGI: Remove ipv6_parse_exthdrs().
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
22#include <linux/errno.h>
23#include <linux/types.h>
24#include <linux/socket.h>
25#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/net.h>
27#include <linux/netdevice.h>
28#include <linux/in6.h>
29#include <linux/icmpv6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090030#include <linux/mroute6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
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 Dumazet1f07d032013-08-06 03:32:11 -070047#include <net/inet_ecn.h>
Wei-Chun Chao48fb6b52015-07-22 18:13:12 -070048#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Edward Creed8269e22018-07-05 15:49:42 +010050static void ip6_rcv_finish_core(struct net *net, struct sock *sk,
51 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
subashab@codeaurora.orgdddb64b2017-03-23 13:34:16 -060053 void (*edemux)(struct sk_buff *skb);
54
Nikolay Borisove21145a2016-02-15 12:11:30 +020055 if (net->ipv4.sysctl_ip_early_demux && !skb_dst(skb) && skb->sk == NULL) {
Eric Dumazetc7109982012-07-26 12:18:11 +000056 const struct inet6_protocol *ipprot;
57
Eric Dumazetc7109982012-07-26 12:18:11 +000058 ipprot = rcu_dereference(inet6_protos[ipv6_hdr(skb)->nexthdr]);
subashab@codeaurora.orgdddb64b2017-03-23 13:34:16 -060059 if (ipprot && (edemux = READ_ONCE(ipprot->early_demux)))
60 edemux(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 Melob0e380b2007-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
319/*
320 * Deliver the packet to the host
321 */
Paolo Abeni80bde362018-11-07 12:38:32 +0100322void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr,
323 bool have_final)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000325 const struct inet6_protocol *ipprot;
David S. Millerf9242b62012-06-19 18:56:21 -0700326 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 unsigned int nhoff;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000328 bool raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /*
331 * Parse extension headers
332 */
333
David S. Miller1b0ccfe2015-06-10 15:29:31 -0700334resubmit:
Eric Dumazetadf30902009-06-02 05:19:30 +0000335 idev = ip6_dst_idev(skb_dst(skb));
Patrick McHardy951dbc82006-01-06 23:02:34 -0800336 nhoff = IP6CB(skb)->nhoff;
Paolo Abeni80bde362018-11-07 12:38:32 +0100337 if (!have_final) {
338 if (!pskb_pull(skb, skb_transport_offset(skb)))
339 goto discard;
340 nexthdr = skb_network_header(skb)[nhoff];
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Tom Herbert4c642422016-05-18 09:06:11 -0700343resubmit_final:
Pavel Emelyanov69d6da02007-11-19 22:35:57 -0800344 raw = raw6_local_deliver(skb, nexthdr);
Ian Morrise5d08d72014-11-23 21:28:43 +0000345 ipprot = rcu_dereference(inet6_protos[nexthdr]);
Ian Morris53b24b82015-03-29 14:00:05 +0100346 if (ipprot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int ret;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900348
Tom Herbert1da44f92016-05-18 09:06:12 -0700349 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 Dumazetb71d1d42011-04-22 04:53:02 +0000359 const struct ipv6hdr *hdr;
Mike Manning5226b6a2018-11-07 15:36:09 +0000360 int sdif = inet6_sdif(skb);
361 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Tom Herbert1da44f92016-05-18 09:06:12 -0700363 /* Only do this once for first final protocol */
364 have_final = true;
365
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800366 /* 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 Melod56f90a2007-04-10 20:50:43 -0700371 skb_postpull_rcsum(skb, skb_network_header(skb),
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300372 skb_network_header_len(skb));
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700373 hdr = ipv6_hdr(skb);
Mike Manning5226b6a2018-11-07 15:36:09 +0000374
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 Torvalds1da177e2005-04-16 15:20:36 -0700384 if (ipv6_addr_is_multicast(&hdr->daddr) &&
Mike Manning5226b6a2018-11-07 15:36:09 +0000385 !ipv6_chk_mcast_addr(dev, &hdr->daddr,
386 &hdr->saddr) &&
YOSHIFUJI Hideaki / 吉藤英明daad1512013-01-13 05:02:18 +0000387 !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 goto discard;
389 }
390 if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900391 !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 goto discard;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900393
Herbert Xue5bbef22007-10-15 12:50:28 -0700394 ret = ipprot->handler(skb);
Tom Herbert4c642422016-05-18 09:06:11 -0700395 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 Dumazet1d015502016-04-27 16:44:40 -0700408 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
Tom Herbert4c642422016-05-18 09:06:11 -0700409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 } else {
Pavel Emelyanov69d6da02007-11-19 22:35:57 -0800411 if (!raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700413 __IP6_INC_STATS(net, idev,
414 IPSTATS_MIB_INUNKNOWNPROTOS);
Patrick McHardyfad87ac2005-08-16 21:03:41 -0700415 icmpv6_send(skb, ICMPV6_PARAMPROB,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000416 ICMPV6_UNK_NEXTHDR, nhoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
Neil Hormand8c6f4b2013-03-01 07:44:08 +0000418 kfree_skb(skb);
419 } else {
Eric Dumazet1d015502016-04-27 16:44:40 -0700420 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
Neil Hormand8c6f4b2013-03-01 07:44:08 +0000421 consume_skb(skb);
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Paolo Abeni80bde362018-11-07 12:38:32 +0100424 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426discard:
Eric Dumazet1d015502016-04-27 16:44:40 -0700427 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 kfree_skb(skb);
Paolo Abeni80bde362018-11-07 12:38:32 +0100429}
430
431static 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 Torvalds1da177e2005-04-16 15:20:36 -0700437 return 0;
438}
439
440
441int ip6_input(struct sk_buff *skb)
442{
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500443 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
444 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800445 ip6_input_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
David Ahernb4869aa2016-06-06 20:50:40 -0700447EXPORT_SYMBOL_GPL(ip6_input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449int ip6_mc_input(struct sk_buff *skb)
450{
Mike Manning5226b6a2018-11-07 15:36:09 +0000451 int sdif = inet6_sdif(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000452 const struct ipv6hdr *hdr;
Mike Manning5226b6a2018-11-07 15:36:09 +0000453 struct net_device *dev;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000454 bool deliver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Eric Dumazetc2005eb2016-04-27 16:44:41 -0700456 __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev),
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -0400457 __in6_dev_get_safely(skb->dev), IPSTATS_MIB_INMCAST,
Neil Hormanedf391f2009-04-27 02:45:02 -0700458 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Mike Manning5226b6a2018-11-07 15:36:09 +0000460 /* 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 Melo0660e032007-04-25 17:54:47 -0700473 hdr = ipv6_hdr(skb);
Mike Manning5226b6a2018-11-07 15:36:09 +0000474 deliver = ipv6_chk_mcast_addr(dev, &hdr->daddr, NULL);
475 if (sdif)
476 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900478#ifdef CONFIG_IPV6_MROUTE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /*
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900480 * IPv6 multicast router mode is now supported ;)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 */
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700482 if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding &&
Hannes Frederic Sowaddf64352013-03-08 02:07:23 +0000483 !(ipv6_addr_type(&hdr->daddr) &
484 (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900485 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 Torvalds1da177e2005-04-16 15:20:36 -0700492
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900493 /* Check for MLD */
YOSHIFUJI Hideaki / 吉藤英明dd3332b2013-01-13 05:02:45 +0000494 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900495 /* Check if this is a mld message */
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900496 u8 nexthdr = hdr->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800497 __be16 frag_off;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900498 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900500 /* Check if the value of Router Alert
501 * is for MLD (0x0000).
502 */
YOSHIFUJI Hideaki / 吉藤英明dd3332b2013-01-13 05:02:45 +0000503 if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) {
Eric Dumazeta50feda2012-05-18 18:57:34 +0000504 deliver = false;
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900505
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900506 if (!ipv6_ext_hdr(nexthdr)) {
507 /* BUG */
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900508 goto out;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900509 }
510 offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
Jesse Gross75f28112011-11-30 17:05:51 -0800511 &nexthdr, &frag_off);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900512 if (offset < 0)
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900513 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Angga4c938d22015-07-03 14:40:52 +1200515 if (ipv6_is_mld(skb, nexthdr, offset))
516 deliver = true;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900517
Angga4c938d22015-07-03 14:40:52 +1200518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900520 /* 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 Hideaki7bc570c2008-04-03 09:22:53 +0900531 ip6_mr_input(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900534out:
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900535#endif
536 if (likely(deliver))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 ip6_input(skb);
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900538 else {
539 /* discard */
540 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 return 0;
544}