3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Ian P. Morris <I.P.Morris@soton.ac.uk>
9 * Based in linux/net/ipv4/ip_input.c
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.
18 * Mitsuru KANDA @USAGI and
19 * YOSHIFUJI Hideaki @USAGI: Remove ipv6_parse_exthdrs().
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>
33 #include <linux/netfilter.h>
34 #include <linux/netfilter_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>
47 #include <net/inet_ecn.h>
48 #include <net/dst_metadata.h>
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,
55 if (READ_ONCE(net->ipv4.sysctl_ip_early_demux) &&
56 !skb_dst(skb) && !skb->sk) {
57 switch (ipv6_hdr(skb)->nexthdr) {
59 if (READ_ONCE(net->ipv4.sysctl_tcp_early_demux))
60 tcp_v6_early_demux(skb);
63 if (READ_ONCE(net->ipv4.sysctl_udp_early_demux))
64 udp_v6_early_demux(skb);
69 if (!skb_valid_dst(skb))
73 int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
75 /* if ingress device is enslaved to an L3 master device pass the
76 * skb to its handler for processing
78 skb = l3mdev_ip6_rcv(skb);
80 return NET_RX_SUCCESS;
81 ip6_rcv_finish_core(net, sk, skb);
83 return dst_input(skb);
86 static void ip6_sublist_rcv_finish(struct list_head *head)
88 struct sk_buff *skb, *next;
90 list_for_each_entry_safe(skb, next, head, list) {
91 skb_list_del_init(skb);
96 static void ip6_list_rcv_finish(struct net *net, struct sock *sk,
97 struct list_head *head)
99 struct dst_entry *curr_dst = NULL;
100 struct sk_buff *skb, *next;
101 struct list_head sublist;
103 INIT_LIST_HEAD(&sublist);
104 list_for_each_entry_safe(skb, next, head, list) {
105 struct dst_entry *dst;
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
111 skb = l3mdev_ip6_rcv(skb);
114 ip6_rcv_finish_core(net, sk, 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);
124 list_add_tail(&skb->list, &sublist);
126 /* dispatch final sublist */
127 ip6_sublist_rcv_finish(&sublist);
130 static struct sk_buff *ip6_rcv_core(struct sk_buff *skb, struct net_device *dev,
133 const struct ipv6hdr *hdr;
135 struct inet6_dev *idev;
137 if (skb->pkt_type == PACKET_OTHERHOST) {
144 idev = __in6_dev_get(skb->dev);
146 __IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_IN, skb->len);
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);
154 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
157 * Store incoming device index. When the packet will
158 * be queued, we cannot refer to skb->dev anymore.
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
167 IP6CB(skb)->iif = skb_valid_dst(skb) ? ip6_dst_idev(skb_dst(skb))->dev->ifindex : dev->ifindex;
169 if (unlikely(!pskb_may_pull(skb, sizeof(*hdr))))
174 if (hdr->version != 6)
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));
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.
188 if ((ipv6_addr_loopback(&hdr->saddr) ||
189 ipv6_addr_loopback(&hdr->daddr)) &&
190 !(dev->flags & IFF_LOOPBACK))
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.
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)
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.
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)
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.
220 if (ipv6_addr_is_multicast(&hdr->daddr) &&
221 IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 0)
226 * Multicast addresses must not be used as source addresses in IPv6
227 * packets or appear in any Routing header.
229 if (ipv6_addr_is_multicast(&hdr->saddr))
232 skb->transport_header = skb->network_header + sizeof(*hdr);
233 IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
235 pkt_len = ntohs(hdr->payload_len);
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) {
241 idev, IPSTATS_MIB_INTRUNCATEDPKTS);
244 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
245 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
251 if (hdr->nexthdr == NEXTHDR_HOP) {
252 if (ipv6_parse_hopopts(skb) < 0) {
253 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
261 /* Must drop socket now because of tproxy. */
266 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
273 int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
275 struct net *net = dev_net(skb->dev);
277 skb = ip6_rcv_core(skb, dev, net);
280 return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
281 net, NULL, skb, dev, NULL,
285 static void ip6_sublist_rcv(struct list_head *head, struct net_device *dev,
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);
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)
297 struct net_device *curr_dev = NULL;
298 struct net *curr_net = NULL;
299 struct sk_buff *skb, *next;
300 struct list_head sublist;
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);
307 skb_list_del_init(skb);
308 skb = ip6_rcv_core(skb, dev, net);
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);
321 list_add_tail(&skb->list, &sublist);
323 /* dispatch final sublist */
324 ip6_sublist_rcv(&sublist, curr_dev, curr_net);
328 * Deliver the packet to the host
332 static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
334 const struct inet6_protocol *ipprot;
335 struct inet6_dev *idev;
339 bool have_final = false;
342 * Parse extension headers
347 idev = ip6_dst_idev(skb_dst(skb));
348 if (!pskb_pull(skb, skb_transport_offset(skb)))
350 nhoff = IP6CB(skb)->nhoff;
351 nexthdr = skb_network_header(skb)[nhoff];
354 raw = raw6_local_deliver(skb, nexthdr);
355 ipprot = rcu_dereference(inet6_protos[nexthdr]);
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
368 } else if (ipprot->flags & INET6_PROTO_FINAL) {
369 const struct ipv6hdr *hdr;
371 /* Only do this once for first final protocol */
374 /* Free reference early: we don't need it any more,
375 and it may hold ip_conntrack module loaded
379 skb_postpull_rcsum(skb, skb_network_header(skb),
380 skb_network_header_len(skb));
382 if (ipv6_addr_is_multicast(&hdr->daddr) &&
383 !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr,
385 !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb)))
388 if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
389 !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
392 ret = ipprot->handler(skb);
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).
405 } else if (ret == 0) {
406 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
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);
418 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
426 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
433 int ip6_input(struct sk_buff *skb)
435 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
436 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
439 EXPORT_SYMBOL_GPL(ip6_input);
441 int ip6_mc_input(struct sk_buff *skb)
443 const struct ipv6hdr *hdr;
446 __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev),
447 __in6_dev_get_safely(skb->dev), IPSTATS_MIB_INMCAST,
451 deliver = ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL);
453 #ifdef CONFIG_IPV6_MROUTE
455 * IPv6 multicast router mode is now supported ;)
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))) {
462 * Okay, we try to forward - split and duplicate
465 struct sk_buff *skb2;
466 struct inet6_skb_parm *opt = IP6CB(skb);
469 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
470 /* Check if this is a mld message */
471 u8 nexthdr = hdr->nexthdr;
475 /* Check if the value of Router Alert
476 * is for MLD (0x0000).
478 if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) {
481 if (!ipv6_ext_hdr(nexthdr)) {
485 offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
486 &nexthdr, &frag_off);
490 if (ipv6_is_mld(skb, nexthdr, offset))
495 /* unknown RA - process it normally */
499 skb2 = skb_clone(skb, GFP_ATOMIC);