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