Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * IPv6 input | |
1ab1457c | 3 | * Linux INET6 implementation |
1da177e4 LT |
4 | * |
5 | * Authors: | |
6 | * Pedro Roque <roque@di.fc.ul.pt> | |
7 | * Ian P. Morris <I.P.Morris@soton.ac.uk> | |
8 | * | |
1da177e4 LT |
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 | * | |
67ba4152 IM |
18 | * Mitsuru KANDA @USAGI and |
19 | * YOSHIFUJI Hideaki @USAGI: Remove ipv6_parse_exthdrs(). | |
1da177e4 LT |
20 | */ |
21 | ||
22 | #include <linux/errno.h> | |
23 | #include <linux/types.h> | |
24 | #include <linux/socket.h> | |
25 | #include <linux/sockios.h> | |
1da177e4 LT |
26 | #include <linux/net.h> |
27 | #include <linux/netdevice.h> | |
28 | #include <linux/in6.h> | |
29 | #include <linux/icmpv6.h> | |
7bc570c8 | 30 | #include <linux/mroute6.h> |
5a0e3ad6 | 31 | #include <linux/slab.h> |
1da177e4 LT |
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> | |
1f07d03e | 47 | #include <net/inet_ecn.h> |
48fb6b55 | 48 | #include <net/dst_metadata.h> |
1da177e4 | 49 | |
0c4b51f0 | 50 | int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb) |
1da177e4 | 51 | { |
dddb64bc | 52 | void (*edemux)(struct sk_buff *skb); |
53 | ||
74b20582 DA |
54 | /* if ingress device is enslaved to an L3 master device pass the |
55 | * skb to its handler for processing | |
56 | */ | |
57 | skb = l3mdev_ip6_rcv(skb); | |
58 | if (!skb) | |
59 | return NET_RX_SUCCESS; | |
60 | ||
e21145a9 | 61 | if (net->ipv4.sysctl_ip_early_demux && !skb_dst(skb) && skb->sk == NULL) { |
c7109986 ED |
62 | const struct inet6_protocol *ipprot; |
63 | ||
c7109986 | 64 | ipprot = rcu_dereference(inet6_protos[ipv6_hdr(skb)->nexthdr]); |
dddb64bc | 65 | if (ipprot && (edemux = READ_ONCE(ipprot->early_demux))) |
66 | edemux(skb); | |
c7109986 | 67 | } |
48fb6b55 | 68 | if (!skb_valid_dst(skb)) |
1da177e4 LT |
69 | ip6_route_input(skb); |
70 | ||
71 | return dst_input(skb); | |
72 | } | |
73 | ||
f2ccd8fa | 74 | int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) |
1da177e4 | 75 | { |
b71d1d42 | 76 | const struct ipv6hdr *hdr; |
67ba4152 | 77 | u32 pkt_len; |
a11d206d | 78 | struct inet6_dev *idev; |
483a47d2 | 79 | struct net *net = dev_net(skb->dev); |
1da177e4 | 80 | |
a11d206d YH |
81 | if (skb->pkt_type == PACKET_OTHERHOST) { |
82 | kfree_skb(skb); | |
5c91face | 83 | return NET_RX_DROP; |
a11d206d YH |
84 | } |
85 | ||
86 | rcu_read_lock(); | |
1da177e4 | 87 | |
a11d206d YH |
88 | idev = __in6_dev_get(skb->dev); |
89 | ||
c2005eb0 | 90 | __IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_IN, skb->len); |
1da177e4 | 91 | |
778d80be YH |
92 | if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL || |
93 | !idev || unlikely(idev->cnf.disable_ipv6)) { | |
1d015503 | 94 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); |
71f6f6df | 95 | goto drop; |
1da177e4 LT |
96 | } |
97 | ||
6b7fdc3a GC |
98 | memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); |
99 | ||
1da177e4 LT |
100 | /* |
101 | * Store incoming device index. When the packet will | |
102 | * be queued, we cannot refer to skb->dev anymore. | |
103 | * | |
104 | * BTW, when we send a packet for our own local address on a | |
105 | * non-loopback interface (e.g. ethX), it is being delivered | |
de3cb747 | 106 | * via the loopback interface (lo) here; skb->dev = loopback_dev. |
1da177e4 LT |
107 | * It, however, should be considered as if it is being |
108 | * arrived via the sending interface (ethX), because of the | |
109 | * nature of scoping architecture. --yoshfuji | |
110 | */ | |
48fb6b55 | 111 | IP6CB(skb)->iif = skb_valid_dst(skb) ? ip6_dst_idev(skb_dst(skb))->dev->ifindex : dev->ifindex; |
1da177e4 | 112 | |
2889139a | 113 | if (unlikely(!pskb_may_pull(skb, sizeof(*hdr)))) |
1da177e4 LT |
114 | goto err; |
115 | ||
0660e03f | 116 | hdr = ipv6_hdr(skb); |
1da177e4 LT |
117 | |
118 | if (hdr->version != 6) | |
119 | goto err; | |
120 | ||
1d015503 ED |
121 | __IP6_ADD_STATS(net, idev, |
122 | IPSTATS_MIB_NOECTPKTS + | |
1f07d03e | 123 | (ipv6_get_dsfield(hdr) & INET_ECN_MASK), |
1d015503 | 124 | max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs)); |
f630e43a YH |
125 | /* |
126 | * RFC4291 2.5.3 | |
127 | * A packet received on an interface with a destination address | |
128 | * of loopback must be dropped. | |
129 | */ | |
130 | if (!(dev->flags & IFF_LOOPBACK) && | |
131 | ipv6_addr_loopback(&hdr->daddr)) | |
132 | goto err; | |
133 | ||
1c4a154e HFS |
134 | /* RFC4291 Errata ID: 3480 |
135 | * Interface-Local scope spans only a single interface on a | |
136 | * node and is useful only for loopback transmission of | |
137 | * multicast. Packets with interface-local scope received | |
138 | * from another node must be discarded. | |
139 | */ | |
140 | if (!(skb->pkt_type == PACKET_LOOPBACK || | |
141 | dev->flags & IFF_LOOPBACK) && | |
142 | ipv6_addr_is_multicast(&hdr->daddr) && | |
143 | IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 1) | |
144 | goto err; | |
145 | ||
abbc3043 JB |
146 | /* If enabled, drop unicast packets that were encapsulated in link-layer |
147 | * multicast or broadcast to protected against the so-called "hole-196" | |
148 | * attack in 802.11 wireless. | |
149 | */ | |
150 | if (!ipv6_addr_is_multicast(&hdr->daddr) && | |
151 | (skb->pkt_type == PACKET_BROADCAST || | |
152 | skb->pkt_type == PACKET_MULTICAST) && | |
153 | idev->cnf.drop_unicast_in_l2_multicast) | |
154 | goto err; | |
155 | ||
20314092 HFS |
156 | /* RFC4291 2.7 |
157 | * Nodes must not originate a packet to a multicast address whose scope | |
158 | * field contains the reserved value 0; if such a packet is received, it | |
159 | * must be silently dropped. | |
160 | */ | |
161 | if (ipv6_addr_is_multicast(&hdr->daddr) && | |
162 | IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 0) | |
163 | goto err; | |
164 | ||
c457338d BH |
165 | /* |
166 | * RFC4291 2.7 | |
167 | * Multicast addresses must not be used as source addresses in IPv6 | |
168 | * packets or appear in any Routing header. | |
169 | */ | |
170 | if (ipv6_addr_is_multicast(&hdr->saddr)) | |
171 | goto err; | |
172 | ||
b0e380b1 | 173 | skb->transport_header = skb->network_header + sizeof(*hdr); |
951dbc8a PM |
174 | IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr); |
175 | ||
1da177e4 LT |
176 | pkt_len = ntohs(hdr->payload_len); |
177 | ||
178 | /* pkt_len may be zero if Jumbo payload option is present */ | |
179 | if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) { | |
60e5c166 | 180 | if (pkt_len + sizeof(struct ipv6hdr) > skb->len) { |
1d015503 ED |
181 | __IP6_INC_STATS(net, |
182 | idev, IPSTATS_MIB_INTRUNCATEDPKTS); | |
60e5c166 MC |
183 | goto drop; |
184 | } | |
1da177e4 | 185 | if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) { |
1d015503 | 186 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
1da177e4 LT |
187 | goto drop; |
188 | } | |
0660e03f | 189 | hdr = ipv6_hdr(skb); |
1da177e4 LT |
190 | } |
191 | ||
192 | if (hdr->nexthdr == NEXTHDR_HOP) { | |
e5bbef20 | 193 | if (ipv6_parse_hopopts(skb) < 0) { |
1d015503 | 194 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
a11d206d | 195 | rcu_read_unlock(); |
5c91face | 196 | return NET_RX_DROP; |
1da177e4 | 197 | } |
1da177e4 LT |
198 | } |
199 | ||
a11d206d YH |
200 | rcu_read_unlock(); |
201 | ||
71f9dacd HX |
202 | /* Must drop socket now because of tproxy. */ |
203 | skb_orphan(skb); | |
204 | ||
29a26a56 EB |
205 | return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, |
206 | net, NULL, skb, dev, NULL, | |
6e23ae2a | 207 | ip6_rcv_finish); |
1da177e4 | 208 | err: |
1d015503 | 209 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); |
1da177e4 | 210 | drop: |
a11d206d | 211 | rcu_read_unlock(); |
1da177e4 | 212 | kfree_skb(skb); |
5c91face | 213 | return NET_RX_DROP; |
1da177e4 LT |
214 | } |
215 | ||
216 | /* | |
217 | * Deliver the packet to the host | |
218 | */ | |
219 | ||
220 | ||
0c4b51f0 | 221 | static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb) |
1da177e4 | 222 | { |
41135cc8 | 223 | const struct inet6_protocol *ipprot; |
f9242b6b | 224 | struct inet6_dev *idev; |
1da177e4 | 225 | unsigned int nhoff; |
a50feda5 ED |
226 | int nexthdr; |
227 | bool raw; | |
1da44f9c | 228 | bool have_final = false; |
1da177e4 | 229 | |
1da177e4 LT |
230 | /* |
231 | * Parse extension headers | |
232 | */ | |
233 | ||
1da177e4 | 234 | rcu_read_lock(); |
1b0ccfe5 | 235 | resubmit: |
adf30907 | 236 | idev = ip6_dst_idev(skb_dst(skb)); |
ea2ae17d | 237 | if (!pskb_pull(skb, skb_transport_offset(skb))) |
1da177e4 | 238 | goto discard; |
951dbc8a | 239 | nhoff = IP6CB(skb)->nhoff; |
d56f90a7 | 240 | nexthdr = skb_network_header(skb)[nhoff]; |
1da177e4 | 241 | |
4c64242a | 242 | resubmit_final: |
69d6da0b | 243 | raw = raw6_local_deliver(skb, nexthdr); |
e5d08d71 | 244 | ipprot = rcu_dereference(inet6_protos[nexthdr]); |
53b24b8f | 245 | if (ipprot) { |
1da177e4 | 246 | int ret; |
1ab1457c | 247 | |
1da44f9c TH |
248 | if (have_final) { |
249 | if (!(ipprot->flags & INET6_PROTO_FINAL)) { | |
250 | /* Once we've seen a final protocol don't | |
251 | * allow encapsulation on any non-final | |
252 | * ones. This allows foo in UDP encapsulation | |
253 | * to work. | |
254 | */ | |
255 | goto discard; | |
256 | } | |
257 | } else if (ipprot->flags & INET6_PROTO_FINAL) { | |
b71d1d42 | 258 | const struct ipv6hdr *hdr; |
1da177e4 | 259 | |
1da44f9c TH |
260 | /* Only do this once for first final protocol */ |
261 | have_final = true; | |
262 | ||
9fb9cbb1 YK |
263 | /* Free reference early: we don't need it any more, |
264 | and it may hold ip_conntrack module loaded | |
265 | indefinitely. */ | |
266 | nf_reset(skb); | |
267 | ||
d56f90a7 | 268 | skb_postpull_rcsum(skb, skb_network_header(skb), |
cfe1fc77 | 269 | skb_network_header_len(skb)); |
0660e03f | 270 | hdr = ipv6_hdr(skb); |
1da177e4 LT |
271 | if (ipv6_addr_is_multicast(&hdr->daddr) && |
272 | !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, | |
273 | &hdr->saddr) && | |
daad1512 | 274 | !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb))) |
1da177e4 LT |
275 | goto discard; |
276 | } | |
277 | if (!(ipprot->flags & INET6_PROTO_NOPOLICY) && | |
1ab1457c | 278 | !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) |
1da177e4 | 279 | goto discard; |
1ab1457c | 280 | |
e5bbef20 | 281 | ret = ipprot->handler(skb); |
4c64242a TH |
282 | if (ret > 0) { |
283 | if (ipprot->flags & INET6_PROTO_FINAL) { | |
284 | /* Not an extension header, most likely UDP | |
285 | * encapsulation. Use return value as nexthdr | |
286 | * protocol not nhoff (which presumably is | |
287 | * not set by handler). | |
288 | */ | |
289 | nexthdr = ret; | |
290 | goto resubmit_final; | |
291 | } else { | |
292 | goto resubmit; | |
293 | } | |
294 | } else if (ret == 0) { | |
1d015503 | 295 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS); |
4c64242a | 296 | } |
1da177e4 | 297 | } else { |
69d6da0b | 298 | if (!raw) { |
1da177e4 | 299 | if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { |
1d015503 ED |
300 | __IP6_INC_STATS(net, idev, |
301 | IPSTATS_MIB_INUNKNOWNPROTOS); | |
fad87aca | 302 | icmpv6_send(skb, ICMPV6_PARAMPROB, |
3ffe533c | 303 | ICMPV6_UNK_NEXTHDR, nhoff); |
1da177e4 | 304 | } |
d8c6f4b9 NH |
305 | kfree_skb(skb); |
306 | } else { | |
1d015503 | 307 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS); |
d8c6f4b9 NH |
308 | consume_skb(skb); |
309 | } | |
1da177e4 LT |
310 | } |
311 | rcu_read_unlock(); | |
312 | return 0; | |
313 | ||
314 | discard: | |
1d015503 | 315 | __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); |
1da177e4 LT |
316 | rcu_read_unlock(); |
317 | kfree_skb(skb); | |
318 | return 0; | |
319 | } | |
320 | ||
321 | ||
322 | int ip6_input(struct sk_buff *skb) | |
323 | { | |
29a26a56 EB |
324 | return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN, |
325 | dev_net(skb->dev), NULL, skb, skb->dev, NULL, | |
6e23ae2a | 326 | ip6_input_finish); |
1da177e4 | 327 | } |
b4869aa2 | 328 | EXPORT_SYMBOL_GPL(ip6_input); |
1da177e4 LT |
329 | |
330 | int ip6_mc_input(struct sk_buff *skb) | |
331 | { | |
b71d1d42 | 332 | const struct ipv6hdr *hdr; |
a50feda5 | 333 | bool deliver; |
1da177e4 | 334 | |
c2005eb0 | 335 | __IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev), |
adf30907 | 336 | ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INMCAST, |
edf391ff | 337 | skb->len); |
1da177e4 | 338 | |
0660e03f | 339 | hdr = ipv6_hdr(skb); |
4c7966b8 | 340 | deliver = ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL); |
1da177e4 | 341 | |
7bc570c8 | 342 | #ifdef CONFIG_IPV6_MROUTE |
1da177e4 | 343 | /* |
7bc570c8 | 344 | * IPv6 multicast router mode is now supported ;) |
1da177e4 | 345 | */ |
53b7997f | 346 | if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding && |
ddf64354 HFS |
347 | !(ipv6_addr_type(&hdr->daddr) & |
348 | (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) && | |
7bc570c8 YH |
349 | likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) { |
350 | /* | |
351 | * Okay, we try to forward - split and duplicate | |
352 | * packets. | |
353 | */ | |
354 | struct sk_buff *skb2; | |
355 | struct inet6_skb_parm *opt = IP6CB(skb); | |
356 | ||
357 | /* Check for MLD */ | |
dd3332bf | 358 | if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) { |
7bc570c8 | 359 | /* Check if this is a mld message */ |
7bc570c8 | 360 | u8 nexthdr = hdr->nexthdr; |
75f2811c | 361 | __be16 frag_off; |
7bc570c8 YH |
362 | int offset; |
363 | ||
364 | /* Check if the value of Router Alert | |
365 | * is for MLD (0x0000). | |
366 | */ | |
dd3332bf | 367 | if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) { |
a50feda5 | 368 | deliver = false; |
aba6096b | 369 | |
7bc570c8 YH |
370 | if (!ipv6_ext_hdr(nexthdr)) { |
371 | /* BUG */ | |
aba6096b | 372 | goto out; |
7bc570c8 YH |
373 | } |
374 | offset = ipv6_skip_exthdr(skb, sizeof(*hdr), | |
75f2811c | 375 | &nexthdr, &frag_off); |
7bc570c8 | 376 | if (offset < 0) |
aba6096b | 377 | goto out; |
7bc570c8 | 378 | |
4c938d22 A |
379 | if (ipv6_is_mld(skb, nexthdr, offset)) |
380 | deliver = true; | |
7bc570c8 | 381 | |
4c938d22 | 382 | goto out; |
7bc570c8 YH |
383 | } |
384 | /* unknown RA - process it normally */ | |
385 | } | |
1da177e4 | 386 | |
7bc570c8 YH |
387 | if (deliver) |
388 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
389 | else { | |
390 | skb2 = skb; | |
391 | skb = NULL; | |
392 | } | |
1ab1457c | 393 | |
7bc570c8 | 394 | if (skb2) { |
7bc570c8 | 395 | ip6_mr_input(skb2); |
1da177e4 LT |
396 | } |
397 | } | |
7bc570c8 | 398 | out: |
aba6096b YH |
399 | #endif |
400 | if (likely(deliver)) | |
1da177e4 | 401 | ip6_input(skb); |
aba6096b YH |
402 | else { |
403 | /* discard */ | |
404 | kfree_skb(skb); | |
1da177e4 | 405 | } |
1da177e4 LT |
406 | |
407 | return 0; | |
408 | } |