• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ip6_rcv_finish(struct net * net,struct sock * sk,struct sk_buff * skb)50 int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
51 {
52 	void (*edemux)(struct sk_buff *skb);
53 
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 
61 	if (net->ipv4.sysctl_ip_early_demux && !skb_dst(skb) && skb->sk == NULL) {
62 		const struct inet6_protocol *ipprot;
63 
64 		ipprot = rcu_dereference(inet6_protos[ipv6_hdr(skb)->nexthdr]);
65 		if (ipprot && (edemux = READ_ONCE(ipprot->early_demux)))
66 			edemux(skb);
67 	}
68 	if (!skb_valid_dst(skb))
69 		ip6_route_input(skb);
70 
71 	return dst_input(skb);
72 }
73 
ipv6_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt,struct net_device * orig_dev)74 int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
75 {
76 	const struct ipv6hdr *hdr;
77 	u32 pkt_len;
78 	struct inet6_dev *idev;
79 	struct net *net = dev_net(skb->dev);
80 
81 	if (skb->pkt_type == PACKET_OTHERHOST) {
82 		kfree_skb(skb);
83 		return NET_RX_DROP;
84 	}
85 
86 	rcu_read_lock();
87 
88 	idev = __in6_dev_get(skb->dev);
89 
90 	__IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_IN, skb->len);
91 
92 	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL ||
93 	    !idev || unlikely(idev->cnf.disable_ipv6)) {
94 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
95 		goto drop;
96 	}
97 
98 	memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
99 
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
106 	 * via the loopback interface (lo) here; skb->dev = loopback_dev.
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 	 */
111 	IP6CB(skb)->iif = skb_valid_dst(skb) ? ip6_dst_idev(skb_dst(skb))->dev->ifindex : dev->ifindex;
112 
113 	if (unlikely(!pskb_may_pull(skb, sizeof(*hdr))))
114 		goto err;
115 
116 	hdr = ipv6_hdr(skb);
117 
118 	if (hdr->version != 6)
119 		goto err;
120 
121 	__IP6_ADD_STATS(net, idev,
122 			IPSTATS_MIB_NOECTPKTS +
123 				(ipv6_get_dsfield(hdr) & INET_ECN_MASK),
124 			max_t(unsigned short, 1, skb_shinfo(skb)->gso_segs));
125 	/*
126 	 * RFC4291 2.5.3
127 	 * The loopback address must not be used as the source address in IPv6
128 	 * packets that are sent outside of a single node. [..]
129 	 * A packet received on an interface with a destination address
130 	 * of loopback must be dropped.
131 	 */
132 	if ((ipv6_addr_loopback(&hdr->saddr) ||
133 	     ipv6_addr_loopback(&hdr->daddr)) &&
134 	     !(dev->flags & IFF_LOOPBACK))
135 		goto err;
136 
137 	/* RFC4291 Errata ID: 3480
138 	 * Interface-Local scope spans only a single interface on a
139 	 * node and is useful only for loopback transmission of
140 	 * multicast.  Packets with interface-local scope received
141 	 * from another node must be discarded.
142 	 */
143 	if (!(skb->pkt_type == PACKET_LOOPBACK ||
144 	      dev->flags & IFF_LOOPBACK) &&
145 	    ipv6_addr_is_multicast(&hdr->daddr) &&
146 	    IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 1)
147 		goto err;
148 
149 	/* If enabled, drop unicast packets that were encapsulated in link-layer
150 	 * multicast or broadcast to protected against the so-called "hole-196"
151 	 * attack in 802.11 wireless.
152 	 */
153 	if (!ipv6_addr_is_multicast(&hdr->daddr) &&
154 	    (skb->pkt_type == PACKET_BROADCAST ||
155 	     skb->pkt_type == PACKET_MULTICAST) &&
156 	    idev->cnf.drop_unicast_in_l2_multicast)
157 		goto err;
158 
159 	/* RFC4291 2.7
160 	 * Nodes must not originate a packet to a multicast address whose scope
161 	 * field contains the reserved value 0; if such a packet is received, it
162 	 * must be silently dropped.
163 	 */
164 	if (ipv6_addr_is_multicast(&hdr->daddr) &&
165 	    IPV6_ADDR_MC_SCOPE(&hdr->daddr) == 0)
166 		goto err;
167 
168 	/*
169 	 * RFC4291 2.7
170 	 * Multicast addresses must not be used as source addresses in IPv6
171 	 * packets or appear in any Routing header.
172 	 */
173 	if (ipv6_addr_is_multicast(&hdr->saddr))
174 		goto err;
175 
176 	/* While RFC4291 is not explicit about v4mapped addresses
177 	 * in IPv6 headers, it seems clear linux dual-stack
178 	 * model can not deal properly with these.
179 	 * Security models could be fooled by ::ffff:127.0.0.1 for example.
180 	 *
181 	 * https://tools.ietf.org/html/draft-itojun-v6ops-v4mapped-harmful-02
182 	 */
183 	if (ipv6_addr_v4mapped(&hdr->saddr))
184 		goto err;
185 
186 	skb->transport_header = skb->network_header + sizeof(*hdr);
187 	IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
188 
189 	pkt_len = ntohs(hdr->payload_len);
190 
191 	/* pkt_len may be zero if Jumbo payload option is present */
192 	if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
193 		if (pkt_len + sizeof(struct ipv6hdr) > skb->len) {
194 			__IP6_INC_STATS(net,
195 					idev, IPSTATS_MIB_INTRUNCATEDPKTS);
196 			goto drop;
197 		}
198 		if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) {
199 			__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
200 			goto drop;
201 		}
202 		hdr = ipv6_hdr(skb);
203 	}
204 
205 	if (hdr->nexthdr == NEXTHDR_HOP) {
206 		if (ipv6_parse_hopopts(skb) < 0) {
207 			__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
208 			rcu_read_unlock();
209 			return NET_RX_DROP;
210 		}
211 	}
212 
213 	rcu_read_unlock();
214 
215 	/* Must drop socket now because of tproxy. */
216 	skb_orphan(skb);
217 
218 	return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
219 		       net, NULL, skb, dev, NULL,
220 		       ip6_rcv_finish);
221 err:
222 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
223 drop:
224 	rcu_read_unlock();
225 	kfree_skb(skb);
226 	return NET_RX_DROP;
227 }
228 
229 /*
230  *	Deliver the packet to the host
231  */
232 
233 
ip6_input_finish(struct net * net,struct sock * sk,struct sk_buff * skb)234 static int ip6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
235 {
236 	const struct inet6_protocol *ipprot;
237 	struct inet6_dev *idev;
238 	unsigned int nhoff;
239 	int nexthdr;
240 	bool raw;
241 	bool have_final = false;
242 
243 	/*
244 	 *	Parse extension headers
245 	 */
246 
247 	rcu_read_lock();
248 resubmit:
249 	idev = ip6_dst_idev(skb_dst(skb));
250 	if (!pskb_pull(skb, skb_transport_offset(skb)))
251 		goto discard;
252 	nhoff = IP6CB(skb)->nhoff;
253 	nexthdr = skb_network_header(skb)[nhoff];
254 
255 resubmit_final:
256 	raw = raw6_local_deliver(skb, nexthdr);
257 	ipprot = rcu_dereference(inet6_protos[nexthdr]);
258 	if (ipprot) {
259 		int ret;
260 
261 		if (have_final) {
262 			if (!(ipprot->flags & INET6_PROTO_FINAL)) {
263 				/* Once we've seen a final protocol don't
264 				 * allow encapsulation on any non-final
265 				 * ones. This allows foo in UDP encapsulation
266 				 * to work.
267 				 */
268 				goto discard;
269 			}
270 		} else if (ipprot->flags & INET6_PROTO_FINAL) {
271 			const struct ipv6hdr *hdr;
272 
273 			/* Only do this once for first final protocol */
274 			have_final = true;
275 
276 			/* Free reference early: we don't need it any more,
277 			   and it may hold ip_conntrack module loaded
278 			   indefinitely. */
279 			nf_reset(skb);
280 
281 			skb_postpull_rcsum(skb, skb_network_header(skb),
282 					   skb_network_header_len(skb));
283 			hdr = ipv6_hdr(skb);
284 			if (ipv6_addr_is_multicast(&hdr->daddr) &&
285 			    !ipv6_chk_mcast_addr(skb->dev, &hdr->daddr,
286 			    &hdr->saddr) &&
287 			    !ipv6_is_mld(skb, nexthdr, skb_network_header_len(skb)))
288 				goto discard;
289 		}
290 		if (!(ipprot->flags & INET6_PROTO_NOPOLICY) &&
291 		    !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
292 			goto discard;
293 
294 		ret = ipprot->handler(skb);
295 		if (ret > 0) {
296 			if (ipprot->flags & INET6_PROTO_FINAL) {
297 				/* Not an extension header, most likely UDP
298 				 * encapsulation. Use return value as nexthdr
299 				 * protocol not nhoff (which presumably is
300 				 * not set by handler).
301 				 */
302 				nexthdr = ret;
303 				goto resubmit_final;
304 			} else {
305 				goto resubmit;
306 			}
307 		} else if (ret == 0) {
308 			__IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
309 		}
310 	} else {
311 		if (!raw) {
312 			if (xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
313 				__IP6_INC_STATS(net, idev,
314 						IPSTATS_MIB_INUNKNOWNPROTOS);
315 				icmpv6_send(skb, ICMPV6_PARAMPROB,
316 					    ICMPV6_UNK_NEXTHDR, nhoff);
317 			}
318 			kfree_skb(skb);
319 		} else {
320 			__IP6_INC_STATS(net, idev, IPSTATS_MIB_INDELIVERS);
321 			consume_skb(skb);
322 		}
323 	}
324 	rcu_read_unlock();
325 	return 0;
326 
327 discard:
328 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
329 	rcu_read_unlock();
330 	kfree_skb(skb);
331 	return 0;
332 }
333 
334 
ip6_input(struct sk_buff * skb)335 int ip6_input(struct sk_buff *skb)
336 {
337 	return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN,
338 		       dev_net(skb->dev), NULL, skb, skb->dev, NULL,
339 		       ip6_input_finish);
340 }
341 EXPORT_SYMBOL_GPL(ip6_input);
342 
ip6_mc_input(struct sk_buff * skb)343 int ip6_mc_input(struct sk_buff *skb)
344 {
345 	const struct ipv6hdr *hdr;
346 	bool deliver;
347 
348 	__IP6_UPD_PO_STATS(dev_net(skb_dst(skb)->dev),
349 			 ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INMCAST,
350 			 skb->len);
351 
352 	hdr = ipv6_hdr(skb);
353 	deliver = ipv6_chk_mcast_addr(skb->dev, &hdr->daddr, NULL);
354 
355 #ifdef CONFIG_IPV6_MROUTE
356 	/*
357 	 *      IPv6 multicast router mode is now supported ;)
358 	 */
359 	if (dev_net(skb->dev)->ipv6.devconf_all->mc_forwarding &&
360 	    !(ipv6_addr_type(&hdr->daddr) &
361 	      (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)) &&
362 	    likely(!(IP6CB(skb)->flags & IP6SKB_FORWARDED))) {
363 		/*
364 		 * Okay, we try to forward - split and duplicate
365 		 * packets.
366 		 */
367 		struct sk_buff *skb2;
368 		struct inet6_skb_parm *opt = IP6CB(skb);
369 
370 		/* Check for MLD */
371 		if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
372 			/* Check if this is a mld message */
373 			u8 nexthdr = hdr->nexthdr;
374 			__be16 frag_off;
375 			int offset;
376 
377 			/* Check if the value of Router Alert
378 			 * is for MLD (0x0000).
379 			 */
380 			if (opt->ra == htons(IPV6_OPT_ROUTERALERT_MLD)) {
381 				deliver = false;
382 
383 				if (!ipv6_ext_hdr(nexthdr)) {
384 					/* BUG */
385 					goto out;
386 				}
387 				offset = ipv6_skip_exthdr(skb, sizeof(*hdr),
388 							  &nexthdr, &frag_off);
389 				if (offset < 0)
390 					goto out;
391 
392 				if (ipv6_is_mld(skb, nexthdr, offset))
393 					deliver = true;
394 
395 				goto out;
396 			}
397 			/* unknown RA - process it normally */
398 		}
399 
400 		if (deliver)
401 			skb2 = skb_clone(skb, GFP_ATOMIC);
402 		else {
403 			skb2 = skb;
404 			skb = NULL;
405 		}
406 
407 		if (skb2) {
408 			ip6_mr_input(skb2);
409 		}
410 	}
411 out:
412 #endif
413 	if (likely(deliver))
414 		ip6_input(skb);
415 	else {
416 		/* discard */
417 		kfree_skb(skb);
418 	}
419 
420 	return 0;
421 }
422