blob: ef7f707d9ae3aa2b5b579260a073801f774919b0 [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
Xin Longc7a42eb2019-08-23 19:33:03 +080083 list_for_each_entry_safe(skb, next, head, list) {
84 skb_list_del_init(skb);
Edward Creed8269e22018-07-05 15:49:42 +010085 dst_input(skb);
Xin Longc7a42eb2019-08-23 19:33:03 +080086 }
Edward Creed8269e22018-07-05 15:49:42 +010087}
88
89static void ip6_list_rcv_finish(struct net *net, struct sock *sk,
90 struct list_head *head)
91{
92 struct dst_entry *curr_dst = NULL;
93 struct sk_buff *skb, *next;
94 struct list_head sublist;
95
96 INIT_LIST_HEAD(&sublist);
97 list_for_each_entry_safe(skb, next, head, list) {
98 struct dst_entry *dst;
99
Edward Cree22f6bbb2018-12-04 17:37:57 +0000100 skb_list_del_init(skb);
Edward Creed8269e22018-07-05 15:49:42 +0100101 /* if ingress device is enslaved to an L3 master device pass the
102 * skb to its handler for processing
103 */
104 skb = l3mdev_ip6_rcv(skb);
105 if (!skb)
106 continue;
107 ip6_rcv_finish_core(net, sk, skb);
108 dst = skb_dst(skb);
109 if (curr_dst != dst) {
110 /* dispatch old sublist */
111 if (!list_empty(&sublist))
112 ip6_sublist_rcv_finish(&sublist);
113 /* start new sublist */
114 INIT_LIST_HEAD(&sublist);
115 curr_dst = dst;
116 }
117 list_add_tail(&skb->list, &sublist);
118 }
119 /* dispatch final sublist */
120 ip6_sublist_rcv_finish(&sublist);
121}
122
123static struct sk_buff *ip6_rcv_core(struct sk_buff *skb, struct net_device *dev,
124 struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000126 const struct ipv6hdr *hdr;
Ian Morris67ba4152014-08-24 21:53:10 +0100127 u32 pkt_len;
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900128 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900130 if (skb->pkt_type == PACKET_OTHERHOST) {
131 kfree_skb(skb);
Edward Creed8269e22018-07-05 15:49:42 +0100132 return NULL;
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900135 rcu_read_lock();
136
137 idev = __in6_dev_get(skb->dev);
138
Eric Dumazetc2005eb2016-04-27 16:44:41 -0700139 __IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_IN, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900141 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL ||
142 !idev || unlikely(idev->cnf.disable_ipv6)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700143 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
Jesper Nilsson71f6f6d2009-03-27 00:17:45 -0700144 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
Guillaume Chazarain6b7fdc32006-07-24 23:44:44 -0700147 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /*
150 * Store incoming device index. When the packet will
151 * be queued, we cannot refer to skb->dev anymore.
152 *
153 * BTW, when we send a packet for our own local address on a
154 * non-loopback interface (e.g. ethX), it is being delivered
Daniel Lezcanode3cb742007-09-25 19:16:28 -0700155 * via the loopback interface (lo) here; skb->dev = loopback_dev.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * It, however, should be considered as if it is being
157 * arrived via the sending interface (ethX), because of the
158 * nature of scoping architecture. --yoshfuji
159 */
Wei-Chun Chao48fb6b52015-07-22 18:13:12 -0700160 IP6CB(skb)->iif = skb_valid_dst(skb) ? ip6_dst_idev(skb_dst(skb))->dev->ifindex : dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Herbert Xu28891392006-06-30 13:35:46 -0700162 if (unlikely(!pskb_may_pull(skb, sizeof(*hdr))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 goto err;
164
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700165 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 if (hdr->version != 6)
168 goto err;
169
Eric Dumazet1d015502016-04-27 16:44:40 -0700170 __IP6_ADD_STATS(net, idev,
171 IPSTATS_MIB_NOECTPKTS +
Eric Dumazet1f07d032013-08-06 03:32:11 -0700172 (ipv6_get_dsfield(hdr) & INET_ECN_MASK),
Eric Dumazet1d015502016-04-27 16:44:40 -0700173 max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs));
YOSHIFUJI Hideakif630e432008-06-19 16:33:57 -0700174 /*
175 * RFC4291 2.5.3
Florian Westphal0aa8c132017-04-14 20:22:43 +0200176 * The loopback address must not be used as the source address in IPv6
177 * packets that are sent outside of a single node. [..]
YOSHIFUJI Hideakif630e432008-06-19 16:33:57 -0700178 * A packet received on an interface with a destination address
179 * of loopback must be dropped.
180 */
Florian Westphal0aa8c132017-04-14 20:22:43 +0200181 if ((ipv6_addr_loopback(&hdr->saddr) ||
182 ipv6_addr_loopback(&hdr->daddr)) &&
Robert Shearman3ede0bb2018-09-19 13:56:53 +0100183 !(dev->flags & IFF_LOOPBACK) &&
184 !netif_is_l3_master(dev))
YOSHIFUJI Hideakif630e432008-06-19 16:33:57 -0700185 goto err;
186
Hannes Frederic Sowa1c4a1542013-03-26 08:13:34 +0000187 /* RFC4291 Errata ID: 3480
188 * Interface-Local scope spans only a single interface on a
189 * node and is useful only for loopback transmission of
190 * multicast. Packets with interface-local scope received
191 * from another node must be discarded.
192 */
193 if (!(skb->pkt_type == PACKET_LOOPBACK ||
194 dev->flags & IFF_LOOPBACK) &&
195 ipv6_addr_is_multicast(&hdr->daddr) &&
196 IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 1)
197 goto err;
198
Johannes Bergabbc3042016-02-04 13:31:19 +0100199 /* If enabled, drop unicast packets that were encapsulated in link-layer
200 * multicast or broadcast to protected against the so-called "hole-196"
201 * attack in 802.11 wireless.
202 */
203 if (!ipv6_addr_is_multicast(&hdr->daddr) &&
204 (skb->pkt_type == PACKET_BROADCAST ||
205 skb->pkt_type == PACKET_MULTICAST) &&
206 idev->cnf.drop_unicast_in_l2_multicast)
207 goto err;
208
Hannes Frederic Sowa20314092013-02-10 05:35:22 +0000209 /* RFC4291 2.7
210 * Nodes must not originate a packet to a multicast address whose scope
211 * field contains the reserved value 0; if such a packet is received, it
212 * must be silently dropped.
213 */
214 if (ipv6_addr_is_multicast(&hdr->daddr) &&
215 IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 0)
216 goto err;
217
Brian Haleyc4573382011-11-08 04:41:42 +0000218 /*
219 * RFC4291 2.7
220 * Multicast addresses must not be used as source addresses in IPv6
221 * packets or appear in any Routing header.
222 */
223 if (ipv6_addr_is_multicast(&hdr->saddr))
224 goto err;
225
Eric Dumazet6af17992019-10-02 09:38:55 -0700226 /* While RFC4291 is not explicit about v4mapped addresses
227 * in IPv6 headers, it seems clear linux dual-stack
228 * model can not deal properly with these.
229 * Security models could be fooled by ::ffff:127.0.0.1 for example.
230 *
231 * https://tools.ietf.org/html/draft-itojun-v6ops-v4mapped-harmful-02
232 */
233 if (ipv6_addr_v4mapped(&hdr->saddr))
234 goto err;
235
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700236 skb->transport_header = skb->network_header + sizeof(*hdr);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800237 IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 pkt_len = ntohs(hdr->payload_len);
240
241 /* pkt_len may be zero if Jumbo payload option is present */
242 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
Mitsuru Chinen60e5c162007-04-04 23:54:59 -0700243 if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700244 __IP6_INC_STATS(net,
245 idev, IPSTATS_MIB_INTRUNCATEDPKTS);
Mitsuru Chinen60e5c162007-04-04 23:54:59 -0700246 goto drop;
247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700249 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 goto drop;
251 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700252 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
255 if (hdr->nexthdr == NEXTHDR_HOP) {
Herbert Xue5bbef22007-10-15 12:50:28 -0700256 if (ipv6_parse_hopopts(skb) < 0) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700257 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900258 rcu_read_unlock();
Edward Creed8269e22018-07-05 15:49:42 +0100259 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900263 rcu_read_unlock();
264
Herbert Xu71f9dac2009-06-26 19:22:37 -0700265 /* Must drop socket now because of tproxy. */
266 skb_orphan(skb);
267
Edward Creed8269e22018-07-05 15:49:42 +0100268 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269err:
Eric Dumazet1d015502016-04-27 16:44:40 -0700270 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271drop:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900272 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 kfree_skb(skb);
Edward Creed8269e22018-07-05 15:49:42 +0100274 return NULL;
275}
276
277int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
278{
279 struct net *net = dev_net(skb->dev);
280
281 skb = ip6_rcv_core(skb, dev, net);
282 if (skb == NULL)
283 return NET_RX_DROP;
284 return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
285 net, NULL, skb, dev, NULL,
286 ip6_rcv_finish);
287}
288
289static void ip6_sublist_rcv(struct list_head *head, struct net_device *dev,
290 struct net *net)
291{
292 NF_HOOK_LIST(NFPROTO_IPV6, NF_INET_PRE_ROUTING, net, NULL,
293 head, dev, NULL, ip6_rcv_finish);
294 ip6_list_rcv_finish(net, NULL, head);
295}
296
297/* Receive a list of IPv6 packets */
298void ipv6_list_rcv(struct list_head *head, struct packet_type *pt,
299 struct net_device *orig_dev)
300{
301 struct net_device *curr_dev = NULL;
302 struct net *curr_net = NULL;
303 struct sk_buff *skb, *next;
304 struct list_head sublist;
305
306 INIT_LIST_HEAD(&sublist);
307 list_for_each_entry_safe(skb, next, head, list) {
308 struct net_device *dev = skb->dev;
309 struct net *net = dev_net(dev);
310
Edward Cree22f6bbb2018-12-04 17:37:57 +0000311 skb_list_del_init(skb);
Edward Creed8269e22018-07-05 15:49:42 +0100312 skb = ip6_rcv_core(skb, dev, net);
313 if (skb == NULL)
314 continue;
315
316 if (curr_dev != dev || curr_net != net) {
317 /* dispatch old sublist */
318 if (!list_empty(&sublist))
319 ip6_sublist_rcv(&sublist, curr_dev, curr_net);
320 /* start new sublist */
321 INIT_LIST_HEAD(&sublist);
322 curr_dev = dev;
323 curr_net = net;
324 }
325 list_add_tail(&skb->list, &sublist);
326 }
327 /* dispatch final sublist */
Florian Westphal51210ad2019-10-29 01:44:04 +0100328 if (!list_empty(&sublist))
329 ip6_sublist_rcv(&sublist, curr_dev, curr_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Paolo Abeni0e219ae2019-05-03 17:01:37 +0200332INDIRECT_CALLABLE_DECLARE(int udpv6_rcv(struct sk_buff *));
333INDIRECT_CALLABLE_DECLARE(int tcp_v6_rcv(struct sk_buff *));
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/*
336 * Deliver the packet to the host
337 */
Paolo Abeni80bde362018-11-07 12:38:32 +0100338void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr,
339 bool have_final)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000341 const struct inet6_protocol *ipprot;
David S. Millerf9242b62012-06-19 18:56:21 -0700342 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 unsigned int nhoff;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000344 bool raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /*
347 * Parse extension headers
348 */
349
David S. Miller1b0ccfe2015-06-10 15:29:31 -0700350resubmit:
Eric Dumazetadf30902009-06-02 05:19:30 +0000351 idev = ip6_dst_idev(skb_dst(skb));
Patrick McHardy951dbc82006-01-06 23:02:34 -0800352 nhoff = IP6CB(skb)->nhoff;
Paolo Abeni80bde362018-11-07 12:38:32 +0100353 if (!have_final) {
354 if (!pskb_pull(skb, skb_transport_offset(skb)))
355 goto discard;
356 nexthdr = skb_network_header(skb)[nhoff];
357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Tom Herbert4c642422016-05-18 09:06:11 -0700359resubmit_final:
Pavel Emelyanov69d6da02007-11-19 22:35:57 -0800360 raw = raw6_local_deliver(skb, nexthdr);
Ian Morrise5d08d72014-11-23 21:28:43 +0000361 ipprot = rcu_dereference(inet6_protos[nexthdr]);
Ian Morris53b24b82015-03-29 14:00:05 +0100362 if (ipprot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 int ret;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900364
Tom Herbert1da44f92016-05-18 09:06:12 -0700365 if (have_final) {
366 if (!(ipprot->flags & INET6_PROTO_FINAL)) {
367 /* Once we've seen a final protocol don't
368 * allow encapsulation on any non-final
369 * ones. This allows foo in UDP encapsulation
370 * to work.
371 */
372 goto discard;
373 }
374 } else if (ipprot->flags & INET6_PROTO_FINAL) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000375 const struct ipv6hdr *hdr;
Mike Manning5226b6a2018-11-07 15:36:09 +0000376 int sdif = inet6_sdif(skb);
377 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Tom Herbert1da44f92016-05-18 09:06:12 -0700379 /* Only do this once for first final protocol */
380 have_final = true;
381
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800382 /* Free reference early: we don't need it any more,
383 and it may hold ip_conntrack module loaded
384 indefinitely. */
Florian Westphal895b5c92019-09-29 20:54:03 +0200385 nf_reset_ct(skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800386
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700387 skb_postpull_rcsum(skb, skb_network_header(skb),
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300388 skb_network_header_len(skb));
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700389 hdr = ipv6_hdr(skb);
Mike Manning5226b6a2018-11-07 15:36:09 +0000390
391 /* skb->dev passed may be master dev for vrfs. */
392 if (sdif) {
393 dev = dev_get_by_index_rcu(net, sdif);
394 if (!dev)
395 goto discard;
396 } else {
397 dev = skb->dev;
398 }
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (ipv6_addr_is_multicast(&hdr->daddr) &&
Mike Manning5226b6a2018-11-07 15:36:09 +0000401 !ipv6_chk_mcast_addr(dev, &hdr->daddr,
402 &hdr->saddr) &&
YOSHIFUJI Hideaki / 吉藤英明daad1512013-01-13 05:02:18 +0000403 !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 goto discard;
405 }
406 if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900407 !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 goto discard;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900409
Paolo Abeni0e219ae2019-05-03 17:01:37 +0200410 ret = INDIRECT_CALL_2(ipprot->handler, tcp_v6_rcv, udpv6_rcv,
411 skb);
Tom Herbert4c642422016-05-18 09:06:11 -0700412 if (ret > 0) {
413 if (ipprot->flags & INET6_PROTO_FINAL) {
414 /* Not an extension header, most likely UDP
415 * encapsulation. Use return value as nexthdr
416 * protocol not nhoff (which presumably is
417 * not set by handler).
418 */
419 nexthdr = ret;
420 goto resubmit_final;
421 } else {
422 goto resubmit;
423 }
424 } else if (ret == 0) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700425 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
Tom Herbert4c642422016-05-18 09:06:11 -0700426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 } else {
Pavel Emelyanov69d6da02007-11-19 22:35:57 -0800428 if (!raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700430 __IP6_INC_STATS(net, idev,
431 IPSTATS_MIB_INUNKNOWNPROTOS);
Patrick McHardyfad87ac2005-08-16 21:03:41 -0700432 icmpv6_send(skb, ICMPV6_PARAMPROB,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000433 ICMPV6_UNK_NEXTHDR, nhoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Neil Hormand8c6f4b2013-03-01 07:44:08 +0000435 kfree_skb(skb);
436 } else {
Eric Dumazet1d015502016-04-27 16:44:40 -0700437 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
Neil Hormand8c6f4b2013-03-01 07:44:08 +0000438 consume_skb(skb);
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
Paolo Abeni80bde362018-11-07 12:38:32 +0100441 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443discard:
Eric Dumazet1d015502016-04-27 16:44:40 -0700444 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 kfree_skb(skb);
Paolo Abeni80bde362018-11-07 12:38:32 +0100446}
447
448static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
449{
450 rcu_read_lock();
451 ip6_protocol_deliver_rcu(net, skb, 0, false);
452 rcu_read_unlock();
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return 0;
455}
456
457
458int ip6_input(struct sk_buff *skb)
459{
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500460 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
461 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800462 ip6_input_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
David Ahernb4869aa2016-06-06 20:50:40 -0700464EXPORT_SYMBOL_GPL(ip6_input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466int ip6_mc_input(struct sk_buff *skb)
467{
Mike Manning5226b6a2018-11-07 15:36:09 +0000468 int sdif = inet6_sdif(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000469 const struct ipv6hdr *hdr;
Mike Manning5226b6a2018-11-07 15:36:09 +0000470 struct net_device *dev;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000471 bool deliver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Eric Dumazetc2005eb2016-04-27 16:44:41 -0700473 __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev),
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -0400474 __in6_dev_get_safely(skb->dev), IPSTATS_MIB_INMCAST,
Neil Hormanedf391f2009-04-27 02:45:02 -0700475 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Mike Manning5226b6a2018-11-07 15:36:09 +0000477 /* skb->dev passed may be master dev for vrfs. */
478 if (sdif) {
479 rcu_read_lock();
480 dev = dev_get_by_index_rcu(dev_net(skb->dev), sdif);
481 if (!dev) {
482 rcu_read_unlock();
483 kfree_skb(skb);
484 return -ENODEV;
485 }
486 } else {
487 dev = skb->dev;
488 }
489
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700490 hdr = ipv6_hdr(skb);
Mike Manning5226b6a2018-11-07 15:36:09 +0000491 deliver = ipv6_chk_mcast_addr(dev, &hdr->daddr, NULL);
492 if (sdif)
493 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900495#ifdef CONFIG_IPV6_MROUTE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /*
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900497 * IPv6 multicast router mode is now supported ;)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 */
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700499 if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding &&
Hannes Frederic Sowaddf64352013-03-08 02:07:23 +0000500 !(ipv6_addr_type(&hdr->daddr) &
501 (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900502 likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) {
503 /*
504 * Okay, we try to forward - split and duplicate
505 * packets.
506 */
507 struct sk_buff *skb2;
508 struct inet6_skb_parm *opt = IP6CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900510 /* Check for MLD */
YOSHIFUJI Hideaki / 吉藤英明dd3332b2013-01-13 05:02:45 +0000511 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900512 /* Check if this is a mld message */
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900513 u8 nexthdr = hdr->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800514 __be16 frag_off;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900515 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900517 /* Check if the value of Router Alert
518 * is for MLD (0x0000).
519 */
YOSHIFUJI Hideaki / 吉藤英明dd3332b2013-01-13 05:02:45 +0000520 if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) {
Eric Dumazeta50feda2012-05-18 18:57:34 +0000521 deliver = false;
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900522
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900523 if (!ipv6_ext_hdr(nexthdr)) {
524 /* BUG */
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900525 goto out;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900526 }
527 offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
Jesse Gross75f28112011-11-30 17:05:51 -0800528 &nexthdr, &frag_off);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900529 if (offset < 0)
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900530 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Angga4c938d22015-07-03 14:40:52 +1200532 if (ipv6_is_mld(skb, nexthdr, offset))
533 deliver = true;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900534
Angga4c938d22015-07-03 14:40:52 +1200535 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900537 /* unknown RA - process it normally */
538 }
539
540 if (deliver)
541 skb2 = skb_clone(skb, GFP_ATOMIC);
542 else {
543 skb2 = skb;
544 skb = NULL;
545 }
546
547 if (skb2) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900548 ip6_mr_input(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900551out:
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900552#endif
553 if (likely(deliver))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 ip6_input(skb);
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900555 else {
556 /* discard */
557 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 return 0;
561}