12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * IPv6 input
41da177e4SLinus Torvalds * Linux INET6 implementation
51da177e4SLinus Torvalds *
61da177e4SLinus Torvalds * Authors:
71da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt>
81da177e4SLinus Torvalds * Ian P. Morris <I.P.Morris@soton.ac.uk>
91da177e4SLinus Torvalds *
101da177e4SLinus Torvalds * Based in linux/net/ipv4/ip_input.c
111da177e4SLinus Torvalds */
121da177e4SLinus Torvalds /* Changes
131da177e4SLinus Torvalds *
141da177e4SLinus Torvalds * Mitsuru KANDA @USAGI and
151da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI: Remove ipv6_parse_exthdrs().
161da177e4SLinus Torvalds */
171da177e4SLinus Torvalds
181da177e4SLinus Torvalds #include <linux/errno.h>
191da177e4SLinus Torvalds #include <linux/types.h>
201da177e4SLinus Torvalds #include <linux/socket.h>
211da177e4SLinus Torvalds #include <linux/sockios.h>
221da177e4SLinus Torvalds #include <linux/net.h>
231da177e4SLinus Torvalds #include <linux/netdevice.h>
241da177e4SLinus Torvalds #include <linux/in6.h>
251da177e4SLinus Torvalds #include <linux/icmpv6.h>
267bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
275a0e3ad6STejun Heo #include <linux/slab.h>
280e219ae4SPaolo Abeni #include <linux/indirect_call_wrapper.h>
291da177e4SLinus Torvalds
301da177e4SLinus Torvalds #include <linux/netfilter.h>
311da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h>
321da177e4SLinus Torvalds
331da177e4SLinus Torvalds #include <net/sock.h>
341da177e4SLinus Torvalds #include <net/snmp.h>
35f9a4719cSLeon Romanovsky #include <net/udp.h>
361da177e4SLinus Torvalds
371da177e4SLinus Torvalds #include <net/ipv6.h>
381da177e4SLinus Torvalds #include <net/protocol.h>
391da177e4SLinus Torvalds #include <net/transp_v6.h>
401da177e4SLinus Torvalds #include <net/rawv6.h>
411da177e4SLinus Torvalds #include <net/ndisc.h>
421da177e4SLinus Torvalds #include <net/ip6_route.h>
431da177e4SLinus Torvalds #include <net/addrconf.h>
441da177e4SLinus Torvalds #include <net/xfrm.h>
451f07d03eSEric Dumazet #include <net/inet_ecn.h>
4648fb6b55SWei-Chun Chao #include <net/dst_metadata.h>
471da177e4SLinus Torvalds
ip6_rcv_finish_core(struct net * net,struct sock * sk,struct sk_buff * skb)48d8269e2cSEdward Cree static void ip6_rcv_finish_core(struct net *net, struct sock *sk,
49d8269e2cSEdward Cree struct sk_buff *skb)
501da177e4SLinus Torvalds {
5111052589SKuniyuki Iwashima if (READ_ONCE(net->ipv4.sysctl_ip_early_demux) &&
5211052589SKuniyuki Iwashima !skb_dst(skb) && !skb->sk) {
5311052589SKuniyuki Iwashima switch (ipv6_hdr(skb)->nexthdr) {
5411052589SKuniyuki Iwashima case IPPROTO_TCP:
5511052589SKuniyuki Iwashima if (READ_ONCE(net->ipv4.sysctl_tcp_early_demux))
5611052589SKuniyuki Iwashima tcp_v6_early_demux(skb);
5711052589SKuniyuki Iwashima break;
5811052589SKuniyuki Iwashima case IPPROTO_UDP:
5911052589SKuniyuki Iwashima if (READ_ONCE(net->ipv4.sysctl_udp_early_demux))
6011052589SKuniyuki Iwashima udp_v6_early_demux(skb);
6111052589SKuniyuki Iwashima break;
62c7109986SEric Dumazet }
6311052589SKuniyuki Iwashima }
6411052589SKuniyuki Iwashima
6548fb6b55SWei-Chun Chao if (!skb_valid_dst(skb))
661da177e4SLinus Torvalds ip6_route_input(skb);
67d8269e2cSEdward Cree }
68d8269e2cSEdward Cree
ip6_rcv_finish(struct net * net,struct sock * sk,struct sk_buff * skb)69d8269e2cSEdward Cree int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
70d8269e2cSEdward Cree {
71d8269e2cSEdward Cree /* if ingress device is enslaved to an L3 master device pass the
72d8269e2cSEdward Cree * skb to its handler for processing
73d8269e2cSEdward Cree */
74d8269e2cSEdward Cree skb = l3mdev_ip6_rcv(skb);
75d8269e2cSEdward Cree if (!skb)
76d8269e2cSEdward Cree return NET_RX_SUCCESS;
77d8269e2cSEdward Cree ip6_rcv_finish_core(net, sk, skb);
781da177e4SLinus Torvalds
791da177e4SLinus Torvalds return dst_input(skb);
801da177e4SLinus Torvalds }
811da177e4SLinus Torvalds
ip6_sublist_rcv_finish(struct list_head * head)82d8269e2cSEdward Cree static void ip6_sublist_rcv_finish(struct list_head *head)
83d8269e2cSEdward Cree {
84d8269e2cSEdward Cree struct sk_buff *skb, *next;
85d8269e2cSEdward Cree
86c7a42eb4SXin Long list_for_each_entry_safe(skb, next, head, list) {
87c7a42eb4SXin Long skb_list_del_init(skb);
88d8269e2cSEdward Cree dst_input(skb);
89d8269e2cSEdward Cree }
90c7a42eb4SXin Long }
91d8269e2cSEdward Cree
ip6_can_use_hint(const struct sk_buff * skb,const struct sk_buff * hint)92197dbf24SPaolo Abeni static bool ip6_can_use_hint(const struct sk_buff *skb,
93197dbf24SPaolo Abeni const struct sk_buff *hint)
94197dbf24SPaolo Abeni {
95197dbf24SPaolo Abeni return hint && !skb_dst(skb) &&
96197dbf24SPaolo Abeni ipv6_addr_equal(&ipv6_hdr(hint)->daddr, &ipv6_hdr(skb)->daddr);
97197dbf24SPaolo Abeni }
98197dbf24SPaolo Abeni
ip6_extract_route_hint(const struct net * net,struct sk_buff * skb)99197dbf24SPaolo Abeni static struct sk_buff *ip6_extract_route_hint(const struct net *net,
100197dbf24SPaolo Abeni struct sk_buff *skb)
101197dbf24SPaolo Abeni {
1028423be89SSriram Yagnaraman if (fib6_routes_require_src(net) || fib6_has_custom_rules(net) ||
1038423be89SSriram Yagnaraman IP6CB(skb)->flags & IP6SKB_MULTIPATH)
104197dbf24SPaolo Abeni return NULL;
105197dbf24SPaolo Abeni
106197dbf24SPaolo Abeni return skb;
107197dbf24SPaolo Abeni }
108197dbf24SPaolo Abeni
ip6_list_rcv_finish(struct net * net,struct sock * sk,struct list_head * head)109d8269e2cSEdward Cree static void ip6_list_rcv_finish(struct net *net, struct sock *sk,
110d8269e2cSEdward Cree struct list_head *head)
111d8269e2cSEdward Cree {
112197dbf24SPaolo Abeni struct sk_buff *skb, *next, *hint = NULL;
113d8269e2cSEdward Cree struct dst_entry *curr_dst = NULL;
114d8269e2cSEdward Cree struct list_head sublist;
115d8269e2cSEdward Cree
116d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist);
117d8269e2cSEdward Cree list_for_each_entry_safe(skb, next, head, list) {
118d8269e2cSEdward Cree struct dst_entry *dst;
119d8269e2cSEdward Cree
12022f6bbb7SEdward Cree skb_list_del_init(skb);
121d8269e2cSEdward Cree /* if ingress device is enslaved to an L3 master device pass the
122d8269e2cSEdward Cree * skb to its handler for processing
123d8269e2cSEdward Cree */
124d8269e2cSEdward Cree skb = l3mdev_ip6_rcv(skb);
125d8269e2cSEdward Cree if (!skb)
126d8269e2cSEdward Cree continue;
127197dbf24SPaolo Abeni
128197dbf24SPaolo Abeni if (ip6_can_use_hint(skb, hint))
129197dbf24SPaolo Abeni skb_dst_copy(skb, hint);
130197dbf24SPaolo Abeni else
131d8269e2cSEdward Cree ip6_rcv_finish_core(net, sk, skb);
132d8269e2cSEdward Cree dst = skb_dst(skb);
133d8269e2cSEdward Cree if (curr_dst != dst) {
134197dbf24SPaolo Abeni hint = ip6_extract_route_hint(net, skb);
135197dbf24SPaolo Abeni
136d8269e2cSEdward Cree /* dispatch old sublist */
137d8269e2cSEdward Cree if (!list_empty(&sublist))
138d8269e2cSEdward Cree ip6_sublist_rcv_finish(&sublist);
139d8269e2cSEdward Cree /* start new sublist */
140d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist);
141d8269e2cSEdward Cree curr_dst = dst;
142d8269e2cSEdward Cree }
143d8269e2cSEdward Cree list_add_tail(&skb->list, &sublist);
144d8269e2cSEdward Cree }
145d8269e2cSEdward Cree /* dispatch final sublist */
146d8269e2cSEdward Cree ip6_sublist_rcv_finish(&sublist);
147d8269e2cSEdward Cree }
148d8269e2cSEdward Cree
ip6_rcv_core(struct sk_buff * skb,struct net_device * dev,struct net * net)149d8269e2cSEdward Cree static struct sk_buff *ip6_rcv_core(struct sk_buff *skb, struct net_device *dev,
150d8269e2cSEdward Cree struct net *net)
1511da177e4SLinus Torvalds {
1524daf841aSMenglong Dong enum skb_drop_reason reason;
153b71d1d42SEric Dumazet const struct ipv6hdr *hdr;
1541da177e4SLinus Torvalds u32 pkt_len;
155a11d206dSYOSHIFUJI Hideaki struct inet6_dev *idev;
1561da177e4SLinus Torvalds
157a11d206dSYOSHIFUJI Hideaki if (skb->pkt_type == PACKET_OTHERHOST) {
158794c24e9SJeffrey Ji dev_core_stats_rx_otherhost_dropped_inc(skb->dev);
1594daf841aSMenglong Dong kfree_skb_reason(skb, SKB_DROP_REASON_OTHERHOST);
160d8269e2cSEdward Cree return NULL;
161a11d206dSYOSHIFUJI Hideaki }
1621da177e4SLinus Torvalds
163a11d206dSYOSHIFUJI Hideaki rcu_read_lock();
164a11d206dSYOSHIFUJI Hideaki
165a11d206dSYOSHIFUJI Hideaki idev = __in6_dev_get(skb->dev);
166a11d206dSYOSHIFUJI Hideaki
167c2005eb0SEric Dumazet __IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_IN, skb->len);
1681da177e4SLinus Torvalds
1694daf841aSMenglong Dong SKB_DR_SET(reason, NOT_SPECIFIED);
170778d80beSYOSHIFUJI Hideaki if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL ||
171*4b271168SEric Dumazet !idev || unlikely(READ_ONCE(idev->cnf.disable_ipv6))) {
1721d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
173*4b271168SEric Dumazet if (idev && unlikely(READ_ONCE(idev->cnf.disable_ipv6)))
1744daf841aSMenglong Dong SKB_DR_SET(reason, IPV6DISABLED);
17571f6f6dfSJesper Nilsson goto drop;
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds
1786b7fdc3aSGuillaume Chazarain memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
1796b7fdc3aSGuillaume Chazarain
1801da177e4SLinus Torvalds /*
1811da177e4SLinus Torvalds * Store incoming device index. When the packet will
1821da177e4SLinus Torvalds * be queued, we cannot refer to skb->dev anymore.
1831da177e4SLinus Torvalds *
1841da177e4SLinus Torvalds * BTW, when we send a packet for our own local address on a
1851da177e4SLinus Torvalds * non-loopback interface (e.g. ethX), it is being delivered
186de3cb747SDaniel Lezcano * via the loopback interface (lo) here; skb->dev = loopback_dev.
1871da177e4SLinus Torvalds * It, however, should be considered as if it is being
1881da177e4SLinus Torvalds * arrived via the sending interface (ethX), because of the
1891da177e4SLinus Torvalds * nature of scoping architecture. --yoshfuji
1901da177e4SLinus Torvalds */
19148fb6b55SWei-Chun Chao IP6CB(skb)->iif = skb_valid_dst(skb) ? ip6_dst_idev(skb_dst(skb))->dev->ifindex : dev->ifindex;
1921da177e4SLinus Torvalds
1932889139aSHerbert Xu if (unlikely(!pskb_may_pull(skb, sizeof(*hdr))))
1941da177e4SLinus Torvalds goto err;
1951da177e4SLinus Torvalds
1960660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb);
1971da177e4SLinus Torvalds
1984daf841aSMenglong Dong if (hdr->version != 6) {
1994daf841aSMenglong Dong SKB_DR_SET(reason, UNHANDLED_PROTO);
2001da177e4SLinus Torvalds goto err;
2014daf841aSMenglong Dong }
2021da177e4SLinus Torvalds
2031d015503SEric Dumazet __IP6_ADD_STATS(net, idev,
2041f07d03eSEric Dumazet IPSTATS_MIB_NOECTPKTS +
2051f07d03eSEric Dumazet (ipv6_get_dsfield(hdr) & INET_ECN_MASK),
2061f07d03eSEric Dumazet max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs));
207f630e43aSYOSHIFUJI Hideaki /*
208f630e43aSYOSHIFUJI Hideaki * RFC4291 2.5.3
2090aa8c13eSFlorian Westphal * The loopback address must not be used as the source address in IPv6
2100aa8c13eSFlorian Westphal * packets that are sent outside of a single node. [..]
211f630e43aSYOSHIFUJI Hideaki * A packet received on an interface with a destination address
212f630e43aSYOSHIFUJI Hideaki * of loopback must be dropped.
213f630e43aSYOSHIFUJI Hideaki */
2140aa8c13eSFlorian Westphal if ((ipv6_addr_loopback(&hdr->saddr) ||
2150aa8c13eSFlorian Westphal ipv6_addr_loopback(&hdr->daddr)) &&
2163ede0bbcSRobert Shearman !(dev->flags & IFF_LOOPBACK) &&
2173ede0bbcSRobert Shearman !netif_is_l3_master(dev))
218f630e43aSYOSHIFUJI Hideaki goto err;
219f630e43aSYOSHIFUJI Hideaki
2201c4a154eSHannes Frederic Sowa /* RFC4291 Errata ID: 3480
2211c4a154eSHannes Frederic Sowa * Interface-Local scope spans only a single interface on a
2221c4a154eSHannes Frederic Sowa * node and is useful only for loopback transmission of
2231c4a154eSHannes Frederic Sowa * multicast. Packets with interface-local scope received
2241c4a154eSHannes Frederic Sowa * from another node must be discarded.
2251c4a154eSHannes Frederic Sowa */
2261c4a154eSHannes Frederic Sowa if (!(skb->pkt_type == PACKET_LOOPBACK ||
2271c4a154eSHannes Frederic Sowa dev->flags & IFF_LOOPBACK) &&
2281c4a154eSHannes Frederic Sowa ipv6_addr_is_multicast(&hdr->daddr) &&
2291c4a154eSHannes Frederic Sowa IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 1)
2301c4a154eSHannes Frederic Sowa goto err;
2311c4a154eSHannes Frederic Sowa
232abbc3043SJohannes Berg /* If enabled, drop unicast packets that were encapsulated in link-layer
233abbc3043SJohannes Berg * multicast or broadcast to protected against the so-called "hole-196"
234abbc3043SJohannes Berg * attack in 802.11 wireless.
235abbc3043SJohannes Berg */
236abbc3043SJohannes Berg if (!ipv6_addr_is_multicast(&hdr->daddr) &&
237abbc3043SJohannes Berg (skb->pkt_type == PACKET_BROADCAST ||
238abbc3043SJohannes Berg skb->pkt_type == PACKET_MULTICAST) &&
2394daf841aSMenglong Dong idev->cnf.drop_unicast_in_l2_multicast) {
2404daf841aSMenglong Dong SKB_DR_SET(reason, UNICAST_IN_L2_MULTICAST);
241abbc3043SJohannes Berg goto err;
2424daf841aSMenglong Dong }
243abbc3043SJohannes Berg
24420314092SHannes Frederic Sowa /* RFC4291 2.7
24520314092SHannes Frederic Sowa * Nodes must not originate a packet to a multicast address whose scope
24620314092SHannes Frederic Sowa * field contains the reserved value 0; if such a packet is received, it
24720314092SHannes Frederic Sowa * must be silently dropped.
24820314092SHannes Frederic Sowa */
24920314092SHannes Frederic Sowa if (ipv6_addr_is_multicast(&hdr->daddr) &&
25020314092SHannes Frederic Sowa IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 0)
25120314092SHannes Frederic Sowa goto err;
25220314092SHannes Frederic Sowa
253c457338dSBrian Haley /*
254c457338dSBrian Haley * RFC4291 2.7
255c457338dSBrian Haley * Multicast addresses must not be used as source addresses in IPv6
256c457338dSBrian Haley * packets or appear in any Routing header.
257c457338dSBrian Haley */
258c457338dSBrian Haley if (ipv6_addr_is_multicast(&hdr->saddr))
259c457338dSBrian Haley goto err;
260c457338dSBrian Haley
261b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(*hdr);
262951dbc8aSPatrick McHardy IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
263951dbc8aSPatrick McHardy
2641da177e4SLinus Torvalds pkt_len = ntohs(hdr->payload_len);
2651da177e4SLinus Torvalds
2661da177e4SLinus Torvalds /* pkt_len may be zero if Jumbo payload option is present */
2671da177e4SLinus Torvalds if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
26860e5c166SMitsuru Chinen if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
2691d015503SEric Dumazet __IP6_INC_STATS(net,
270483a47d2SDenis V. Lunev idev, IPSTATS_MIB_INTRUNCATEDPKTS);
2714daf841aSMenglong Dong SKB_DR_SET(reason, PKT_TOO_SMALL);
27260e5c166SMitsuru Chinen goto drop;
27360e5c166SMitsuru Chinen }
2744daf841aSMenglong Dong if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
2754daf841aSMenglong Dong goto err;
2760660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb);
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds
2791da177e4SLinus Torvalds if (hdr->nexthdr == NEXTHDR_HOP) {
280e5bbef20SHerbert Xu if (ipv6_parse_hopopts(skb) < 0) {
2811d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
282a11d206dSYOSHIFUJI Hideaki rcu_read_unlock();
283d8269e2cSEdward Cree return NULL;
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds
287a11d206dSYOSHIFUJI Hideaki rcu_read_unlock();
288a11d206dSYOSHIFUJI Hideaki
28971f9dacdSHerbert Xu /* Must drop socket now because of tproxy. */
290cf7fbe66SJoe Stringer if (!skb_sk_is_prefetched(skb))
29171f9dacdSHerbert Xu skb_orphan(skb);
29271f9dacdSHerbert Xu
293d8269e2cSEdward Cree return skb;
2941da177e4SLinus Torvalds err:
2951d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
2964daf841aSMenglong Dong SKB_DR_OR(reason, IP_INHDR);
2971da177e4SLinus Torvalds drop:
298a11d206dSYOSHIFUJI Hideaki rcu_read_unlock();
2994daf841aSMenglong Dong kfree_skb_reason(skb, reason);
300d8269e2cSEdward Cree return NULL;
301d8269e2cSEdward Cree }
302d8269e2cSEdward Cree
ipv6_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt,struct net_device * orig_dev)303d8269e2cSEdward Cree int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
304d8269e2cSEdward Cree {
305d8269e2cSEdward Cree struct net *net = dev_net(skb->dev);
306d8269e2cSEdward Cree
307d8269e2cSEdward Cree skb = ip6_rcv_core(skb, dev, net);
308d8269e2cSEdward Cree if (skb == NULL)
3095c91faceSMark Smith return NET_RX_DROP;
310d8269e2cSEdward Cree return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
311d8269e2cSEdward Cree net, NULL, skb, dev, NULL,
312d8269e2cSEdward Cree ip6_rcv_finish);
313d8269e2cSEdward Cree }
314d8269e2cSEdward Cree
ip6_sublist_rcv(struct list_head * head,struct net_device * dev,struct net * net)315d8269e2cSEdward Cree static void ip6_sublist_rcv(struct list_head *head, struct net_device *dev,
316d8269e2cSEdward Cree struct net *net)
317d8269e2cSEdward Cree {
318d8269e2cSEdward Cree NF_HOOK_LIST(NFPROTO_IPV6, NF_INET_PRE_ROUTING, net, NULL,
319d8269e2cSEdward Cree head, dev, NULL, ip6_rcv_finish);
320d8269e2cSEdward Cree ip6_list_rcv_finish(net, NULL, head);
321d8269e2cSEdward Cree }
322d8269e2cSEdward Cree
323d8269e2cSEdward Cree /* Receive a list of IPv6 packets */
ipv6_list_rcv(struct list_head * head,struct packet_type * pt,struct net_device * orig_dev)324d8269e2cSEdward Cree void ipv6_list_rcv(struct list_head *head, struct packet_type *pt,
325d8269e2cSEdward Cree struct net_device *orig_dev)
326d8269e2cSEdward Cree {
327d8269e2cSEdward Cree struct net_device *curr_dev = NULL;
328d8269e2cSEdward Cree struct net *curr_net = NULL;
329d8269e2cSEdward Cree struct sk_buff *skb, *next;
330d8269e2cSEdward Cree struct list_head sublist;
331d8269e2cSEdward Cree
332d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist);
333d8269e2cSEdward Cree list_for_each_entry_safe(skb, next, head, list) {
334d8269e2cSEdward Cree struct net_device *dev = skb->dev;
335d8269e2cSEdward Cree struct net *net = dev_net(dev);
336d8269e2cSEdward Cree
33722f6bbb7SEdward Cree skb_list_del_init(skb);
338d8269e2cSEdward Cree skb = ip6_rcv_core(skb, dev, net);
339d8269e2cSEdward Cree if (skb == NULL)
340d8269e2cSEdward Cree continue;
341d8269e2cSEdward Cree
342d8269e2cSEdward Cree if (curr_dev != dev || curr_net != net) {
343d8269e2cSEdward Cree /* dispatch old sublist */
344d8269e2cSEdward Cree if (!list_empty(&sublist))
345d8269e2cSEdward Cree ip6_sublist_rcv(&sublist, curr_dev, curr_net);
346d8269e2cSEdward Cree /* start new sublist */
347d8269e2cSEdward Cree INIT_LIST_HEAD(&sublist);
348d8269e2cSEdward Cree curr_dev = dev;
349d8269e2cSEdward Cree curr_net = net;
350d8269e2cSEdward Cree }
351d8269e2cSEdward Cree list_add_tail(&skb->list, &sublist);
352d8269e2cSEdward Cree }
353d8269e2cSEdward Cree /* dispatch final sublist */
35451210ad5SFlorian Westphal if (!list_empty(&sublist))
355d8269e2cSEdward Cree ip6_sublist_rcv(&sublist, curr_dev, curr_net);
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds
3580e219ae4SPaolo Abeni INDIRECT_CALLABLE_DECLARE(int tcp_v6_rcv(struct sk_buff *));
3590e219ae4SPaolo Abeni
3601da177e4SLinus Torvalds /*
3611da177e4SLinus Torvalds * Deliver the packet to the host
3621da177e4SLinus Torvalds */
ip6_protocol_deliver_rcu(struct net * net,struct sk_buff * skb,int nexthdr,bool have_final)36380bde363SPaolo Abeni void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr,
36480bde363SPaolo Abeni bool have_final)
3651da177e4SLinus Torvalds {
36641135cc8SAlexey Dobriyan const struct inet6_protocol *ipprot;
367f9242b6bSDavid S. Miller struct inet6_dev *idev;
3681da177e4SLinus Torvalds unsigned int nhoff;
369eeab7e7fSMenglong Dong SKB_DR(reason);
370a50feda5SEric Dumazet bool raw;
3711da177e4SLinus Torvalds
3721da177e4SLinus Torvalds /*
3731da177e4SLinus Torvalds * Parse extension headers
3741da177e4SLinus Torvalds */
3751da177e4SLinus Torvalds
3761b0ccfe5SDavid S. Miller resubmit:
377adf30907SEric Dumazet idev = ip6_dst_idev(skb_dst(skb));
37880bde363SPaolo Abeni nhoff = IP6CB(skb)->nhoff;
37980bde363SPaolo Abeni if (!have_final) {
380ea2ae17dSArnaldo Carvalho de Melo if (!pskb_pull(skb, skb_transport_offset(skb)))
3811da177e4SLinus Torvalds goto discard;
382d56f90a7SArnaldo Carvalho de Melo nexthdr = skb_network_header(skb)[nhoff];
38380bde363SPaolo Abeni }
3841da177e4SLinus Torvalds
3854c64242aSTom Herbert resubmit_final:
38669d6da0bSPavel Emelyanov raw = raw6_local_deliver(skb, nexthdr);
387e5d08d71SIan Morris ipprot = rcu_dereference(inet6_protos[nexthdr]);
38853b24b8fSIan Morris if (ipprot) {
3891da177e4SLinus Torvalds int ret;
3901da177e4SLinus Torvalds
3911da44f9cSTom Herbert if (have_final) {
3921da44f9cSTom Herbert if (!(ipprot->flags & INET6_PROTO_FINAL)) {
3931da44f9cSTom Herbert /* Once we've seen a final protocol don't
3941da44f9cSTom Herbert * allow encapsulation on any non-final
3951da44f9cSTom Herbert * ones. This allows foo in UDP encapsulation
3961da44f9cSTom Herbert * to work.
3971da44f9cSTom Herbert */
3981da44f9cSTom Herbert goto discard;
3991da44f9cSTom Herbert }
4001da44f9cSTom Herbert } else if (ipprot->flags & INET6_PROTO_FINAL) {
401b71d1d42SEric Dumazet const struct ipv6hdr *hdr;
4025226b6a9SMike Manning int sdif = inet6_sdif(skb);
4035226b6a9SMike Manning struct net_device *dev;
4041da177e4SLinus Torvalds
4051da44f9cSTom Herbert /* Only do this once for first final protocol */
4061da44f9cSTom Herbert have_final = true;
4071da44f9cSTom Herbert
4089fb9cbb1SYasuyuki Kozakai
409d56f90a7SArnaldo Carvalho de Melo skb_postpull_rcsum(skb, skb_network_header(skb),
410cfe1fc77SArnaldo Carvalho de Melo skb_network_header_len(skb));
4110660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb);
4125226b6a9SMike Manning
4135226b6a9SMike Manning /* skb->dev passed may be master dev for vrfs. */
4145226b6a9SMike Manning if (sdif) {
4155226b6a9SMike Manning dev = dev_get_by_index_rcu(net, sdif);
4165226b6a9SMike Manning if (!dev)
4175226b6a9SMike Manning goto discard;
4185226b6a9SMike Manning } else {
4195226b6a9SMike Manning dev = skb->dev;
4205226b6a9SMike Manning }
4215226b6a9SMike Manning
4221da177e4SLinus Torvalds if (ipv6_addr_is_multicast(&hdr->daddr) &&
4235226b6a9SMike Manning !ipv6_chk_mcast_addr(dev, &hdr->daddr,
4241da177e4SLinus Torvalds &hdr->saddr) &&
425eeab7e7fSMenglong Dong !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb))) {
426eeab7e7fSMenglong Dong SKB_DR_SET(reason, IP_INADDRERRORS);
4271da177e4SLinus Torvalds goto discard;
4281da177e4SLinus Torvalds }
429eeab7e7fSMenglong Dong }
430b0e214d2SMadhu Koriginja if (!(ipprot->flags & INET6_PROTO_NOPOLICY)) {
431b0e214d2SMadhu Koriginja if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
432eeab7e7fSMenglong Dong SKB_DR_SET(reason, XFRM_POLICY);
4331da177e4SLinus Torvalds goto discard;
434eeab7e7fSMenglong Dong }
435b0e214d2SMadhu Koriginja nf_reset_ct(skb);
436b0e214d2SMadhu Koriginja }
4371da177e4SLinus Torvalds
4380e219ae4SPaolo Abeni ret = INDIRECT_CALL_2(ipprot->handler, tcp_v6_rcv, udpv6_rcv,
4390e219ae4SPaolo Abeni skb);
4404c64242aSTom Herbert if (ret > 0) {
4414c64242aSTom Herbert if (ipprot->flags & INET6_PROTO_FINAL) {
4424c64242aSTom Herbert /* Not an extension header, most likely UDP
4434c64242aSTom Herbert * encapsulation. Use return value as nexthdr
4444c64242aSTom Herbert * protocol not nhoff (which presumably is
4454c64242aSTom Herbert * not set by handler).
4464c64242aSTom Herbert */
4474c64242aSTom Herbert nexthdr = ret;
4484c64242aSTom Herbert goto resubmit_final;
4494c64242aSTom Herbert } else {
4501da177e4SLinus Torvalds goto resubmit;
4514c64242aSTom Herbert }
4524c64242aSTom Herbert } else if (ret == 0) {
4531d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
4544c64242aSTom Herbert }
4551da177e4SLinus Torvalds } else {
45669d6da0bSPavel Emelyanov if (!raw) {
4571da177e4SLinus Torvalds if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
4581d015503SEric Dumazet __IP6_INC_STATS(net, idev,
459483a47d2SDenis V. Lunev IPSTATS_MIB_INUNKNOWNPROTOS);
460fad87acaSPatrick McHardy icmpv6_send(skb, ICMPV6_PARAMPROB,
4613ffe533cSAlexey Dobriyan ICMPV6_UNK_NEXTHDR, nhoff);
462eeab7e7fSMenglong Dong SKB_DR_SET(reason, IP_NOPROTO);
463eeab7e7fSMenglong Dong } else {
464eeab7e7fSMenglong Dong SKB_DR_SET(reason, XFRM_POLICY);
4651da177e4SLinus Torvalds }
466eeab7e7fSMenglong Dong kfree_skb_reason(skb, reason);
467d8c6f4b9SNeil Horman } else {
4681d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
469d8c6f4b9SNeil Horman consume_skb(skb);
470d8c6f4b9SNeil Horman }
4711da177e4SLinus Torvalds }
47280bde363SPaolo Abeni return;
4731da177e4SLinus Torvalds
4741da177e4SLinus Torvalds discard:
4751d015503SEric Dumazet __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
476eeab7e7fSMenglong Dong kfree_skb_reason(skb, reason);
47780bde363SPaolo Abeni }
47880bde363SPaolo Abeni
ip6_input_finish(struct net * net,struct sock * sk,struct sk_buff * skb)47980bde363SPaolo Abeni static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
48080bde363SPaolo Abeni {
481cd14e9b7SMartin KaFai Lau skb_clear_delivery_time(skb);
48280bde363SPaolo Abeni rcu_read_lock();
48380bde363SPaolo Abeni ip6_protocol_deliver_rcu(net, skb, 0, false);
48480bde363SPaolo Abeni rcu_read_unlock();
48580bde363SPaolo Abeni
4861da177e4SLinus Torvalds return 0;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds
4891da177e4SLinus Torvalds
ip6_input(struct sk_buff * skb)4901da177e4SLinus Torvalds int ip6_input(struct sk_buff *skb)
4911da177e4SLinus Torvalds {
49229a26a56SEric W. Biederman return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
49329a26a56SEric W. Biederman dev_net(skb->dev), NULL, skb, skb->dev, NULL,
4946e23ae2aSPatrick McHardy ip6_input_finish);
4951da177e4SLinus Torvalds }
496b4869aa2SDavid Ahern EXPORT_SYMBOL_GPL(ip6_input);
4971da177e4SLinus Torvalds
ip6_mc_input(struct sk_buff * skb)4981da177e4SLinus Torvalds int ip6_mc_input(struct sk_buff *skb)
4991da177e4SLinus Torvalds {
5005226b6a9SMike Manning int sdif = inet6_sdif(skb);
501b71d1d42SEric Dumazet const struct ipv6hdr *hdr;
5025226b6a9SMike Manning struct net_device *dev;
503a50feda5SEric Dumazet bool deliver;
5041da177e4SLinus Torvalds
505c2005eb0SEric Dumazet __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev),
506bdb7cc64SStephen Suryaputra __in6_dev_get_safely(skb->dev), IPSTATS_MIB_INMCAST,
507edf391ffSNeil Horman skb->len);
5081da177e4SLinus Torvalds
5095226b6a9SMike Manning /* skb->dev passed may be master dev for vrfs. */
5105226b6a9SMike Manning if (sdif) {
5115226b6a9SMike Manning rcu_read_lock();
5125226b6a9SMike Manning dev = dev_get_by_index_rcu(dev_net(skb->dev), sdif);
5135226b6a9SMike Manning if (!dev) {
5145226b6a9SMike Manning rcu_read_unlock();
5155226b6a9SMike Manning kfree_skb(skb);
5165226b6a9SMike Manning return -ENODEV;
5175226b6a9SMike Manning }
5185226b6a9SMike Manning } else {
5195226b6a9SMike Manning dev = skb->dev;
5205226b6a9SMike Manning }
5215226b6a9SMike Manning
5220660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb);
5235226b6a9SMike Manning deliver = ipv6_chk_mcast_addr(dev, &hdr->daddr, NULL);
5245226b6a9SMike Manning if (sdif)
5255226b6a9SMike Manning rcu_read_unlock();
5261da177e4SLinus Torvalds
5277bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
5281da177e4SLinus Torvalds /*
5297bc570c8SYOSHIFUJI Hideaki * IPv6 multicast router mode is now supported ;)
5301da177e4SLinus Torvalds */
531145c7a79SEric Dumazet if (atomic_read(&dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding) &&
532ddf64354SHannes Frederic Sowa !(ipv6_addr_type(&hdr->daddr) &
533ddf64354SHannes Frederic Sowa (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) &&
5347bc570c8SYOSHIFUJI Hideaki likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) {
5357bc570c8SYOSHIFUJI Hideaki /*
5367bc570c8SYOSHIFUJI Hideaki * Okay, we try to forward - split and duplicate
5377bc570c8SYOSHIFUJI Hideaki * packets.
5387bc570c8SYOSHIFUJI Hideaki */
5391da177e4SLinus Torvalds struct sk_buff *skb2;
5407bc570c8SYOSHIFUJI Hideaki struct inet6_skb_parm *opt = IP6CB(skb);
5411da177e4SLinus Torvalds
5427bc570c8SYOSHIFUJI Hideaki /* Check for MLD */
543dd3332bfSYOSHIFUJI Hideaki / 吉藤英明 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
5447bc570c8SYOSHIFUJI Hideaki /* Check if this is a mld message */
5457bc570c8SYOSHIFUJI Hideaki u8 nexthdr = hdr->nexthdr;
54675f2811cSJesse Gross __be16 frag_off;
5477bc570c8SYOSHIFUJI Hideaki int offset;
5481da177e4SLinus Torvalds
5497bc570c8SYOSHIFUJI Hideaki /* Check if the value of Router Alert
5507bc570c8SYOSHIFUJI Hideaki * is for MLD (0x0000).
5517bc570c8SYOSHIFUJI Hideaki */
552dd3332bfSYOSHIFUJI Hideaki / 吉藤英明 if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) {
553a50feda5SEric Dumazet deliver = false;
554aba6096bSYOSHIFUJI Hideaki
5557bc570c8SYOSHIFUJI Hideaki if (!ipv6_ext_hdr(nexthdr)) {
5567bc570c8SYOSHIFUJI Hideaki /* BUG */
557aba6096bSYOSHIFUJI Hideaki goto out;
5581da177e4SLinus Torvalds }
5597bc570c8SYOSHIFUJI Hideaki offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
56075f2811cSJesse Gross &nexthdr, &frag_off);
5617bc570c8SYOSHIFUJI Hideaki if (offset < 0)
562aba6096bSYOSHIFUJI Hideaki goto out;
5637bc570c8SYOSHIFUJI Hideaki
5644c938d22SAngga if (ipv6_is_mld(skb, nexthdr, offset))
565a50feda5SEric Dumazet deliver = true;
5664c938d22SAngga
5674c938d22SAngga goto out;
5687bc570c8SYOSHIFUJI Hideaki }
5697bc570c8SYOSHIFUJI Hideaki /* unknown RA - process it normally */
5707bc570c8SYOSHIFUJI Hideaki }
5717bc570c8SYOSHIFUJI Hideaki
5727bc570c8SYOSHIFUJI Hideaki if (deliver)
5737bc570c8SYOSHIFUJI Hideaki skb2 = skb_clone(skb, GFP_ATOMIC);
5747bc570c8SYOSHIFUJI Hideaki else {
5757bc570c8SYOSHIFUJI Hideaki skb2 = skb;
5767bc570c8SYOSHIFUJI Hideaki skb = NULL;
5777bc570c8SYOSHIFUJI Hideaki }
5787bc570c8SYOSHIFUJI Hideaki
5797bc570c8SYOSHIFUJI Hideaki if (skb2) {
5807bc570c8SYOSHIFUJI Hideaki ip6_mr_input(skb2);
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds }
5837bc570c8SYOSHIFUJI Hideaki out:
584aba6096bSYOSHIFUJI Hideaki #endif
585aba6096bSYOSHIFUJI Hideaki if (likely(deliver))
5861da177e4SLinus Torvalds ip6_input(skb);
587aba6096bSYOSHIFUJI Hideaki else {
5881da177e4SLinus Torvalds /* discard */
5891da177e4SLinus Torvalds kfree_skb(skb);
590aba6096bSYOSHIFUJI Hideaki }
5911da177e4SLinus Torvalds
5921da177e4SLinus Torvalds return 0;
5931da177e4SLinus Torvalds }
594