blob: d432d0011c160f41aec09640e95179dd7b364cfc [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
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700226 skb->transport_header = skb->network_header + sizeof(*hdr);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800227 IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 pkt_len = ntohs(hdr->payload_len);
230
231 /* pkt_len may be zero if Jumbo payload option is present */
232 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
Mitsuru Chinen60e5c162007-04-04 23:54:59 -0700233 if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700234 __IP6_INC_STATS(net,
235 idev, IPSTATS_MIB_INTRUNCATEDPKTS);
Mitsuru Chinen60e5c162007-04-04 23:54:59 -0700236 goto drop;
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700239 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto drop;
241 }
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700242 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
245 if (hdr->nexthdr == NEXTHDR_HOP) {
Herbert Xue5bbef22007-10-15 12:50:28 -0700246 if (ipv6_parse_hopopts(skb) < 0) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700247 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900248 rcu_read_unlock();
Edward Creed8269e22018-07-05 15:49:42 +0100249 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900253 rcu_read_unlock();
254
Herbert Xu71f9dac2009-06-26 19:22:37 -0700255 /* Must drop socket now because of tproxy. */
256 skb_orphan(skb);
257
Edward Creed8269e22018-07-05 15:49:42 +0100258 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259err:
Eric Dumazet1d015502016-04-27 16:44:40 -0700260 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261drop:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900262 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 kfree_skb(skb);
Edward Creed8269e22018-07-05 15:49:42 +0100264 return NULL;
265}
266
267int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
268{
269 struct net *net = dev_net(skb->dev);
270
271 skb = ip6_rcv_core(skb, dev, net);
272 if (skb == NULL)
273 return NET_RX_DROP;
274 return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
275 net, NULL, skb, dev, NULL,
276 ip6_rcv_finish);
277}
278
279static void ip6_sublist_rcv(struct list_head *head, struct net_device *dev,
280 struct net *net)
281{
282 NF_HOOK_LIST(NFPROTO_IPV6, NF_INET_PRE_ROUTING, net, NULL,
283 head, dev, NULL, ip6_rcv_finish);
284 ip6_list_rcv_finish(net, NULL, head);
285}
286
287/* Receive a list of IPv6 packets */
288void ipv6_list_rcv(struct list_head *head, struct packet_type *pt,
289 struct net_device *orig_dev)
290{
291 struct net_device *curr_dev = NULL;
292 struct net *curr_net = NULL;
293 struct sk_buff *skb, *next;
294 struct list_head sublist;
295
296 INIT_LIST_HEAD(&sublist);
297 list_for_each_entry_safe(skb, next, head, list) {
298 struct net_device *dev = skb->dev;
299 struct net *net = dev_net(dev);
300
Edward Cree22f6bbb2018-12-04 17:37:57 +0000301 skb_list_del_init(skb);
Edward Creed8269e22018-07-05 15:49:42 +0100302 skb = ip6_rcv_core(skb, dev, net);
303 if (skb == NULL)
304 continue;
305
306 if (curr_dev != dev || curr_net != net) {
307 /* dispatch old sublist */
308 if (!list_empty(&sublist))
309 ip6_sublist_rcv(&sublist, curr_dev, curr_net);
310 /* start new sublist */
311 INIT_LIST_HEAD(&sublist);
312 curr_dev = dev;
313 curr_net = net;
314 }
315 list_add_tail(&skb->list, &sublist);
316 }
317 /* dispatch final sublist */
318 ip6_sublist_rcv(&sublist, curr_dev, curr_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Paolo Abeni0e219ae2019-05-03 17:01:37 +0200321INDIRECT_CALLABLE_DECLARE(int udpv6_rcv(struct sk_buff *));
322INDIRECT_CALLABLE_DECLARE(int tcp_v6_rcv(struct sk_buff *));
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*
325 * Deliver the packet to the host
326 */
Paolo Abeni80bde362018-11-07 12:38:32 +0100327void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr,
328 bool have_final)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000330 const struct inet6_protocol *ipprot;
David S. Millerf9242b62012-06-19 18:56:21 -0700331 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 unsigned int nhoff;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000333 bool raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /*
336 * Parse extension headers
337 */
338
David S. Miller1b0ccfe2015-06-10 15:29:31 -0700339resubmit:
Eric Dumazetadf30902009-06-02 05:19:30 +0000340 idev = ip6_dst_idev(skb_dst(skb));
Patrick McHardy951dbc82006-01-06 23:02:34 -0800341 nhoff = IP6CB(skb)->nhoff;
Paolo Abeni80bde362018-11-07 12:38:32 +0100342 if (!have_final) {
343 if (!pskb_pull(skb, skb_transport_offset(skb)))
344 goto discard;
345 nexthdr = skb_network_header(skb)[nhoff];
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Tom Herbert4c642422016-05-18 09:06:11 -0700348resubmit_final:
Pavel Emelyanov69d6da02007-11-19 22:35:57 -0800349 raw = raw6_local_deliver(skb, nexthdr);
Ian Morrise5d08d72014-11-23 21:28:43 +0000350 ipprot = rcu_dereference(inet6_protos[nexthdr]);
Ian Morris53b24b82015-03-29 14:00:05 +0100351 if (ipprot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 int ret;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900353
Tom Herbert1da44f92016-05-18 09:06:12 -0700354 if (have_final) {
355 if (!(ipprot->flags & INET6_PROTO_FINAL)) {
356 /* Once we've seen a final protocol don't
357 * allow encapsulation on any non-final
358 * ones. This allows foo in UDP encapsulation
359 * to work.
360 */
361 goto discard;
362 }
363 } else if (ipprot->flags & INET6_PROTO_FINAL) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000364 const struct ipv6hdr *hdr;
Mike Manning5226b6a2018-11-07 15:36:09 +0000365 int sdif = inet6_sdif(skb);
366 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Tom Herbert1da44f92016-05-18 09:06:12 -0700368 /* Only do this once for first final protocol */
369 have_final = true;
370
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800371 /* Free reference early: we don't need it any more,
372 and it may hold ip_conntrack module loaded
373 indefinitely. */
374 nf_reset(skb);
375
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700376 skb_postpull_rcsum(skb, skb_network_header(skb),
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300377 skb_network_header_len(skb));
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700378 hdr = ipv6_hdr(skb);
Mike Manning5226b6a2018-11-07 15:36:09 +0000379
380 /* skb->dev passed may be master dev for vrfs. */
381 if (sdif) {
382 dev = dev_get_by_index_rcu(net, sdif);
383 if (!dev)
384 goto discard;
385 } else {
386 dev = skb->dev;
387 }
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (ipv6_addr_is_multicast(&hdr->daddr) &&
Mike Manning5226b6a2018-11-07 15:36:09 +0000390 !ipv6_chk_mcast_addr(dev, &hdr->daddr,
391 &hdr->saddr) &&
YOSHIFUJI Hideaki / 吉藤英明daad1512013-01-13 05:02:18 +0000392 !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 goto discard;
394 }
395 if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900396 !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 goto discard;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900398
Paolo Abeni0e219ae2019-05-03 17:01:37 +0200399 ret = INDIRECT_CALL_2(ipprot->handler, tcp_v6_rcv, udpv6_rcv,
400 skb);
Tom Herbert4c642422016-05-18 09:06:11 -0700401 if (ret > 0) {
402 if (ipprot->flags & INET6_PROTO_FINAL) {
403 /* Not an extension header, most likely UDP
404 * encapsulation. Use return value as nexthdr
405 * protocol not nhoff (which presumably is
406 * not set by handler).
407 */
408 nexthdr = ret;
409 goto resubmit_final;
410 } else {
411 goto resubmit;
412 }
413 } else if (ret == 0) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700414 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
Tom Herbert4c642422016-05-18 09:06:11 -0700415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 } else {
Pavel Emelyanov69d6da02007-11-19 22:35:57 -0800417 if (!raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700419 __IP6_INC_STATS(net, idev,
420 IPSTATS_MIB_INUNKNOWNPROTOS);
Patrick McHardyfad87ac2005-08-16 21:03:41 -0700421 icmpv6_send(skb, ICMPV6_PARAMPROB,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000422 ICMPV6_UNK_NEXTHDR, nhoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Neil Hormand8c6f4b2013-03-01 07:44:08 +0000424 kfree_skb(skb);
425 } else {
Eric Dumazet1d015502016-04-27 16:44:40 -0700426 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
Neil Hormand8c6f4b2013-03-01 07:44:08 +0000427 consume_skb(skb);
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Paolo Abeni80bde362018-11-07 12:38:32 +0100430 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432discard:
Eric Dumazet1d015502016-04-27 16:44:40 -0700433 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 kfree_skb(skb);
Paolo Abeni80bde362018-11-07 12:38:32 +0100435}
436
437static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
438{
439 rcu_read_lock();
440 ip6_protocol_deliver_rcu(net, skb, 0, false);
441 rcu_read_unlock();
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return 0;
444}
445
446
447int ip6_input(struct sk_buff *skb)
448{
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500449 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
450 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800451 ip6_input_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
David Ahernb4869aa2016-06-06 20:50:40 -0700453EXPORT_SYMBOL_GPL(ip6_input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455int ip6_mc_input(struct sk_buff *skb)
456{
Mike Manning5226b6a2018-11-07 15:36:09 +0000457 int sdif = inet6_sdif(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000458 const struct ipv6hdr *hdr;
Mike Manning5226b6a2018-11-07 15:36:09 +0000459 struct net_device *dev;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000460 bool deliver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Eric Dumazetc2005eb2016-04-27 16:44:41 -0700462 __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev),
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -0400463 __in6_dev_get_safely(skb->dev), IPSTATS_MIB_INMCAST,
Neil Hormanedf391f2009-04-27 02:45:02 -0700464 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Mike Manning5226b6a2018-11-07 15:36:09 +0000466 /* skb->dev passed may be master dev for vrfs. */
467 if (sdif) {
468 rcu_read_lock();
469 dev = dev_get_by_index_rcu(dev_net(skb->dev), sdif);
470 if (!dev) {
471 rcu_read_unlock();
472 kfree_skb(skb);
473 return -ENODEV;
474 }
475 } else {
476 dev = skb->dev;
477 }
478
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700479 hdr = ipv6_hdr(skb);
Mike Manning5226b6a2018-11-07 15:36:09 +0000480 deliver = ipv6_chk_mcast_addr(dev, &hdr->daddr, NULL);
481 if (sdif)
482 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900484#ifdef CONFIG_IPV6_MROUTE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /*
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900486 * IPv6 multicast router mode is now supported ;)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 */
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700488 if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding &&
Hannes Frederic Sowaddf64352013-03-08 02:07:23 +0000489 !(ipv6_addr_type(&hdr->daddr) &
490 (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900491 likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) {
492 /*
493 * Okay, we try to forward - split and duplicate
494 * packets.
495 */
496 struct sk_buff *skb2;
497 struct inet6_skb_parm *opt = IP6CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900499 /* Check for MLD */
YOSHIFUJI Hideaki / 吉藤英明dd3332b2013-01-13 05:02:45 +0000500 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900501 /* Check if this is a mld message */
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900502 u8 nexthdr = hdr->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800503 __be16 frag_off;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900504 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900506 /* Check if the value of Router Alert
507 * is for MLD (0x0000).
508 */
YOSHIFUJI Hideaki / 吉藤英明dd3332b2013-01-13 05:02:45 +0000509 if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) {
Eric Dumazeta50feda2012-05-18 18:57:34 +0000510 deliver = false;
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900511
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900512 if (!ipv6_ext_hdr(nexthdr)) {
513 /* BUG */
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900514 goto out;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900515 }
516 offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
Jesse Gross75f28112011-11-30 17:05:51 -0800517 &nexthdr, &frag_off);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900518 if (offset < 0)
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900519 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Angga4c938d22015-07-03 14:40:52 +1200521 if (ipv6_is_mld(skb, nexthdr, offset))
522 deliver = true;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900523
Angga4c938d22015-07-03 14:40:52 +1200524 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900526 /* unknown RA - process it normally */
527 }
528
529 if (deliver)
530 skb2 = skb_clone(skb, GFP_ATOMIC);
531 else {
532 skb2 = skb;
533 skb = NULL;
534 }
535
536 if (skb2) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900537 ip6_mr_input(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900540out:
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900541#endif
542 if (likely(deliver))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 ip6_input(skb);
YOSHIFUJI Hideakiaba60962008-04-10 15:41:26 +0900544 else {
545 /* discard */
546 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 return 0;
550}