xref: /linux/net/ipv6/ndisc.c (revision 0e59523b440c90ed1899476f32ad4e949282f43e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Neighbour Discovery for IPv6
4  *	Linux INET6 implementation
5  *
6  *	Authors:
7  *	Pedro Roque		<roque@di.fc.ul.pt>
8  *	Mike Shaver		<shaver@ingenia.com>
9  */
10 
11 /*
12  *	Changes:
13  *
14  *	Alexey I. Froloff		:	RFC6106 (DNSSL) support
15  *	Pierre Ynard			:	export userland ND options
16  *						through netlink (RDNSS support)
17  *	Lars Fenneberg			:	fixed MTU setting on receipt
18  *						of an RA.
19  *	Janos Farkas			:	kmalloc failure checks
20  *	Alexey Kuznetsov		:	state machine reworked
21  *						and moved to net/core.
22  *	Pekka Savola			:	RFC2461 validation
23  *	YOSHIFUJI Hideaki @USAGI	:	Verify ND options properly
24  */
25 
26 #define pr_fmt(fmt) "ICMPv6: " fmt
27 
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/types.h>
31 #include <linux/socket.h>
32 #include <linux/sockios.h>
33 #include <linux/sched.h>
34 #include <linux/net.h>
35 #include <linux/in6.h>
36 #include <linux/route.h>
37 #include <linux/init.h>
38 #include <linux/rcupdate.h>
39 #include <linux/slab.h>
40 #ifdef CONFIG_SYSCTL
41 #include <linux/sysctl.h>
42 #endif
43 
44 #include <linux/if_addr.h>
45 #include <linux/if_ether.h>
46 #include <linux/if_arp.h>
47 #include <linux/ipv6.h>
48 #include <linux/icmpv6.h>
49 #include <linux/jhash.h>
50 
51 #include <net/sock.h>
52 #include <net/snmp.h>
53 
54 #include <net/ipv6.h>
55 #include <net/protocol.h>
56 #include <net/ndisc.h>
57 #include <net/ip6_route.h>
58 #include <net/addrconf.h>
59 #include <net/icmp.h>
60 
61 #include <net/netlink.h>
62 #include <linux/rtnetlink.h>
63 
64 #include <net/flow.h>
65 #include <net/ip6_checksum.h>
66 #include <net/inet_common.h>
67 #include <linux/proc_fs.h>
68 
69 #include <linux/netfilter.h>
70 #include <linux/netfilter_ipv6.h>
71 
72 static u32 ndisc_hash(const void *pkey,
73 		      const struct net_device *dev,
74 		      __u32 *hash_rnd);
75 static bool ndisc_key_eq(const struct neighbour *neigh, const void *pkey);
76 static bool ndisc_allow_add(const struct net_device *dev,
77 			    struct netlink_ext_ack *extack);
78 static int ndisc_constructor(struct neighbour *neigh);
79 static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb);
80 static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb);
81 static int pndisc_constructor(struct pneigh_entry *n);
82 static void pndisc_destructor(struct pneigh_entry *n);
83 static void pndisc_redo(struct sk_buff *skb);
84 static int ndisc_is_multicast(const void *pkey);
85 
86 static const struct neigh_ops ndisc_generic_ops = {
87 	.family =		AF_INET6,
88 	.solicit =		ndisc_solicit,
89 	.error_report =		ndisc_error_report,
90 	.output =		neigh_resolve_output,
91 	.connected_output =	neigh_connected_output,
92 };
93 
94 static const struct neigh_ops ndisc_hh_ops = {
95 	.family =		AF_INET6,
96 	.solicit =		ndisc_solicit,
97 	.error_report =		ndisc_error_report,
98 	.output =		neigh_resolve_output,
99 	.connected_output =	neigh_resolve_output,
100 };
101 
102 
103 static const struct neigh_ops ndisc_direct_ops = {
104 	.family =		AF_INET6,
105 	.output =		neigh_direct_output,
106 	.connected_output =	neigh_direct_output,
107 };
108 
109 struct neigh_table nd_tbl = {
110 	.family =	AF_INET6,
111 	.key_len =	sizeof(struct in6_addr),
112 	.protocol =	cpu_to_be16(ETH_P_IPV6),
113 	.hash =		ndisc_hash,
114 	.key_eq =	ndisc_key_eq,
115 	.constructor =	ndisc_constructor,
116 	.pconstructor =	pndisc_constructor,
117 	.pdestructor =	pndisc_destructor,
118 	.proxy_redo =	pndisc_redo,
119 	.is_multicast =	ndisc_is_multicast,
120 	.allow_add  =   ndisc_allow_add,
121 	.id =		"ndisc_cache",
122 	.parms = {
123 		.tbl			= &nd_tbl,
124 		.reachable_time		= ND_REACHABLE_TIME,
125 		.data = {
126 			[NEIGH_VAR_MCAST_PROBES] = 3,
127 			[NEIGH_VAR_UCAST_PROBES] = 3,
128 			[NEIGH_VAR_RETRANS_TIME] = ND_RETRANS_TIMER,
129 			[NEIGH_VAR_BASE_REACHABLE_TIME] = ND_REACHABLE_TIME,
130 			[NEIGH_VAR_DELAY_PROBE_TIME] = 5 * HZ,
131 			[NEIGH_VAR_INTERVAL_PROBE_TIME_MS] = 5 * HZ,
132 			[NEIGH_VAR_GC_STALETIME] = 60 * HZ,
133 			[NEIGH_VAR_QUEUE_LEN_BYTES] = SK_WMEM_DEFAULT,
134 			[NEIGH_VAR_PROXY_QLEN] = 64,
135 			[NEIGH_VAR_ANYCAST_DELAY] = 1 * HZ,
136 			[NEIGH_VAR_PROXY_DELAY] = (8 * HZ) / 10,
137 		},
138 	},
139 	.gc_interval =	  30 * HZ,
140 	.gc_thresh1 =	 128,
141 	.gc_thresh2 =	 512,
142 	.gc_thresh3 =	1024,
143 };
144 EXPORT_SYMBOL_GPL(nd_tbl);
145 
146 void __ndisc_fill_addr_option(struct sk_buff *skb, int type, const void *data,
147 			      int data_len, int pad)
148 {
149 	int space = __ndisc_opt_addr_space(data_len, pad);
150 	u8 *opt = skb_put(skb, space);
151 
152 	opt[0] = type;
153 	opt[1] = space>>3;
154 
155 	memset(opt + 2, 0, pad);
156 	opt   += pad;
157 	space -= pad;
158 
159 	memcpy(opt+2, data, data_len);
160 	data_len += 2;
161 	opt += data_len;
162 	space -= data_len;
163 	if (space > 0)
164 		memset(opt, 0, space);
165 }
166 EXPORT_SYMBOL_GPL(__ndisc_fill_addr_option);
167 
168 static inline void ndisc_fill_addr_option(struct sk_buff *skb, int type,
169 					  const void *data, u8 icmp6_type)
170 {
171 	__ndisc_fill_addr_option(skb, type, data, skb->dev->addr_len,
172 				 ndisc_addr_option_pad(skb->dev->type));
173 	ndisc_ops_fill_addr_option(skb->dev, skb, icmp6_type);
174 }
175 
176 static inline void ndisc_fill_redirect_addr_option(struct sk_buff *skb,
177 						   void *ha,
178 						   const u8 *ops_data)
179 {
180 	ndisc_fill_addr_option(skb, ND_OPT_TARGET_LL_ADDR, ha, NDISC_REDIRECT);
181 	ndisc_ops_fill_redirect_addr_option(skb->dev, skb, ops_data);
182 }
183 
184 static struct nd_opt_hdr *ndisc_next_option(struct nd_opt_hdr *cur,
185 					    struct nd_opt_hdr *end)
186 {
187 	int type;
188 	if (!cur || !end || cur >= end)
189 		return NULL;
190 	type = cur->nd_opt_type;
191 	do {
192 		cur = ((void *)cur) + (cur->nd_opt_len << 3);
193 	} while (cur < end && cur->nd_opt_type != type);
194 	return cur <= end && cur->nd_opt_type == type ? cur : NULL;
195 }
196 
197 static inline int ndisc_is_useropt(const struct net_device *dev,
198 				   struct nd_opt_hdr *opt)
199 {
200 	return opt->nd_opt_type == ND_OPT_PREFIX_INFO ||
201 		opt->nd_opt_type == ND_OPT_RDNSS ||
202 		opt->nd_opt_type == ND_OPT_DNSSL ||
203 		opt->nd_opt_type == ND_OPT_6CO ||
204 		opt->nd_opt_type == ND_OPT_CAPTIVE_PORTAL ||
205 		opt->nd_opt_type == ND_OPT_PREF64;
206 }
207 
208 static struct nd_opt_hdr *ndisc_next_useropt(const struct net_device *dev,
209 					     struct nd_opt_hdr *cur,
210 					     struct nd_opt_hdr *end)
211 {
212 	if (!cur || !end || cur >= end)
213 		return NULL;
214 	do {
215 		cur = ((void *)cur) + (cur->nd_opt_len << 3);
216 	} while (cur < end && !ndisc_is_useropt(dev, cur));
217 	return cur <= end && ndisc_is_useropt(dev, cur) ? cur : NULL;
218 }
219 
220 struct ndisc_options *ndisc_parse_options(const struct net_device *dev,
221 					  u8 *opt, int opt_len,
222 					  struct ndisc_options *ndopts)
223 {
224 	struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)opt;
225 
226 	if (!nd_opt || opt_len < 0 || !ndopts)
227 		return NULL;
228 	memset(ndopts, 0, sizeof(*ndopts));
229 	while (opt_len) {
230 		bool unknown = false;
231 		int l;
232 		if (opt_len < sizeof(struct nd_opt_hdr))
233 			return NULL;
234 		l = nd_opt->nd_opt_len << 3;
235 		if (opt_len < l || l == 0)
236 			return NULL;
237 		if (ndisc_ops_parse_options(dev, nd_opt, ndopts))
238 			goto next_opt;
239 		switch (nd_opt->nd_opt_type) {
240 		case ND_OPT_SOURCE_LL_ADDR:
241 		case ND_OPT_TARGET_LL_ADDR:
242 		case ND_OPT_MTU:
243 		case ND_OPT_NONCE:
244 		case ND_OPT_REDIRECT_HDR:
245 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
246 				net_dbg_ratelimited("%s: duplicated ND6 option found: type=%d\n",
247 						    __func__, nd_opt->nd_opt_type);
248 			} else {
249 				ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
250 			}
251 			break;
252 		case ND_OPT_PREFIX_INFO:
253 			ndopts->nd_opts_pi_end = nd_opt;
254 			if (!ndopts->nd_opt_array[nd_opt->nd_opt_type])
255 				ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
256 			break;
257 #ifdef CONFIG_IPV6_ROUTE_INFO
258 		case ND_OPT_ROUTE_INFO:
259 			ndopts->nd_opts_ri_end = nd_opt;
260 			if (!ndopts->nd_opts_ri)
261 				ndopts->nd_opts_ri = nd_opt;
262 			break;
263 #endif
264 		default:
265 			unknown = true;
266 		}
267 		if (ndisc_is_useropt(dev, nd_opt)) {
268 			ndopts->nd_useropts_end = nd_opt;
269 			if (!ndopts->nd_useropts)
270 				ndopts->nd_useropts = nd_opt;
271 		} else if (unknown) {
272 			/*
273 			 * Unknown options must be silently ignored,
274 			 * to accommodate future extension to the
275 			 * protocol.
276 			 */
277 			net_dbg_ratelimited("%s: ignored unsupported option; type=%d, len=%d\n",
278 					    __func__, nd_opt->nd_opt_type, nd_opt->nd_opt_len);
279 		}
280 next_opt:
281 		opt_len -= l;
282 		nd_opt = ((void *)nd_opt) + l;
283 	}
284 	return ndopts;
285 }
286 EXPORT_SYMBOL_GPL(ndisc_parse_options);
287 
288 int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev, int dir)
289 {
290 	switch (dev->type) {
291 	case ARPHRD_ETHER:
292 	case ARPHRD_IEEE802:	/* Not sure. Check it later. --ANK */
293 	case ARPHRD_FDDI:
294 		ipv6_eth_mc_map(addr, buf);
295 		return 0;
296 	case ARPHRD_ARCNET:
297 		ipv6_arcnet_mc_map(addr, buf);
298 		return 0;
299 	case ARPHRD_INFINIBAND:
300 		ipv6_ib_mc_map(addr, dev->broadcast, buf);
301 		return 0;
302 	case ARPHRD_IPGRE:
303 		return ipv6_ipgre_mc_map(addr, dev->broadcast, buf);
304 	default:
305 		if (dir) {
306 			memcpy(buf, dev->broadcast, dev->addr_len);
307 			return 0;
308 		}
309 	}
310 	return -EINVAL;
311 }
312 EXPORT_SYMBOL(ndisc_mc_map);
313 
314 static u32 ndisc_hash(const void *pkey,
315 		      const struct net_device *dev,
316 		      __u32 *hash_rnd)
317 {
318 	return ndisc_hashfn(pkey, dev, hash_rnd);
319 }
320 
321 static bool ndisc_key_eq(const struct neighbour *n, const void *pkey)
322 {
323 	return neigh_key_eq128(n, pkey);
324 }
325 
326 static int ndisc_constructor(struct neighbour *neigh)
327 {
328 	struct in6_addr *addr = (struct in6_addr *)&neigh->primary_key;
329 	struct net_device *dev = neigh->dev;
330 	struct inet6_dev *in6_dev;
331 	struct neigh_parms *parms;
332 	bool is_multicast = ipv6_addr_is_multicast(addr);
333 
334 	in6_dev = in6_dev_get(dev);
335 	if (!in6_dev) {
336 		return -EINVAL;
337 	}
338 
339 	parms = in6_dev->nd_parms;
340 	__neigh_parms_put(neigh->parms);
341 	neigh->parms = neigh_parms_clone(parms);
342 
343 	neigh->type = is_multicast ? RTN_MULTICAST : RTN_UNICAST;
344 	if (!dev->header_ops) {
345 		neigh->nud_state = NUD_NOARP;
346 		neigh->ops = &ndisc_direct_ops;
347 		neigh->output = neigh_direct_output;
348 	} else {
349 		if (is_multicast) {
350 			neigh->nud_state = NUD_NOARP;
351 			ndisc_mc_map(addr, neigh->ha, dev, 1);
352 		} else if (dev->flags&(IFF_NOARP|IFF_LOOPBACK)) {
353 			neigh->nud_state = NUD_NOARP;
354 			memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
355 			if (dev->flags&IFF_LOOPBACK)
356 				neigh->type = RTN_LOCAL;
357 		} else if (dev->flags&IFF_POINTOPOINT) {
358 			neigh->nud_state = NUD_NOARP;
359 			memcpy(neigh->ha, dev->broadcast, dev->addr_len);
360 		}
361 		if (dev->header_ops->cache)
362 			neigh->ops = &ndisc_hh_ops;
363 		else
364 			neigh->ops = &ndisc_generic_ops;
365 		if (neigh->nud_state&NUD_VALID)
366 			neigh->output = neigh->ops->connected_output;
367 		else
368 			neigh->output = neigh->ops->output;
369 	}
370 	in6_dev_put(in6_dev);
371 	return 0;
372 }
373 
374 static int pndisc_constructor(struct pneigh_entry *n)
375 {
376 	struct in6_addr *addr = (struct in6_addr *)&n->key;
377 	struct net_device *dev = n->dev;
378 	struct in6_addr maddr;
379 
380 	if (!dev)
381 		return -EINVAL;
382 
383 	addrconf_addr_solict_mult(addr, &maddr);
384 	return ipv6_dev_mc_inc(dev, &maddr);
385 }
386 
387 static void pndisc_destructor(struct pneigh_entry *n)
388 {
389 	struct in6_addr *addr = (struct in6_addr *)&n->key;
390 	struct net_device *dev = n->dev;
391 	struct in6_addr maddr;
392 
393 	if (!dev)
394 		return;
395 
396 	addrconf_addr_solict_mult(addr, &maddr);
397 	ipv6_dev_mc_dec(dev, &maddr);
398 }
399 
400 /* called with rtnl held */
401 static bool ndisc_allow_add(const struct net_device *dev,
402 			    struct netlink_ext_ack *extack)
403 {
404 	struct inet6_dev *idev = __in6_dev_get(dev);
405 
406 	if (!idev || idev->cnf.disable_ipv6) {
407 		NL_SET_ERR_MSG(extack, "IPv6 is disabled on this device");
408 		return false;
409 	}
410 
411 	return true;
412 }
413 
414 static struct sk_buff *ndisc_alloc_skb(struct net_device *dev,
415 				       int len)
416 {
417 	int hlen = LL_RESERVED_SPACE(dev);
418 	int tlen = dev->needed_tailroom;
419 	struct sk_buff *skb;
420 
421 	skb = alloc_skb(hlen + sizeof(struct ipv6hdr) + len + tlen, GFP_ATOMIC);
422 	if (!skb)
423 		return NULL;
424 
425 	skb->protocol = htons(ETH_P_IPV6);
426 	skb->dev = dev;
427 
428 	skb_reserve(skb, hlen + sizeof(struct ipv6hdr));
429 	skb_reset_transport_header(skb);
430 
431 	/* Manually assign socket ownership as we avoid calling
432 	 * sock_alloc_send_pskb() to bypass wmem buffer limits
433 	 */
434 	rcu_read_lock();
435 	skb_set_owner_w(skb, dev_net_rcu(dev)->ipv6.ndisc_sk);
436 	rcu_read_unlock();
437 
438 	return skb;
439 }
440 
441 static void ip6_nd_hdr(struct sk_buff *skb,
442 		       const struct in6_addr *saddr,
443 		       const struct in6_addr *daddr,
444 		       int hop_limit, int len)
445 {
446 	struct ipv6hdr *hdr;
447 	struct inet6_dev *idev;
448 	unsigned tclass;
449 
450 	rcu_read_lock();
451 	idev = __in6_dev_get(skb->dev);
452 	tclass = idev ? READ_ONCE(idev->cnf.ndisc_tclass) : 0;
453 	rcu_read_unlock();
454 
455 	skb_push(skb, sizeof(*hdr));
456 	skb_reset_network_header(skb);
457 	hdr = ipv6_hdr(skb);
458 
459 	ip6_flow_hdr(hdr, tclass, 0);
460 
461 	hdr->payload_len = htons(len);
462 	hdr->nexthdr = IPPROTO_ICMPV6;
463 	hdr->hop_limit = hop_limit;
464 
465 	hdr->saddr = *saddr;
466 	hdr->daddr = *daddr;
467 }
468 
469 void ndisc_send_skb(struct sk_buff *skb, const struct in6_addr *daddr,
470 		    const struct in6_addr *saddr)
471 {
472 	struct icmp6hdr *icmp6h = icmp6_hdr(skb);
473 	struct dst_entry *dst = skb_dst(skb);
474 	struct net_device *dev;
475 	struct inet6_dev *idev;
476 	struct net *net;
477 	struct sock *sk;
478 	int err;
479 	u8 type;
480 
481 	type = icmp6h->icmp6_type;
482 
483 	rcu_read_lock();
484 
485 	net = dev_net_rcu(skb->dev);
486 	sk = net->ipv6.ndisc_sk;
487 	if (!dst) {
488 		struct flowi6 fl6;
489 		int oif = skb->dev->ifindex;
490 
491 		icmpv6_flow_init(sk, &fl6, type, saddr, daddr, oif);
492 		dst = icmp6_dst_alloc(skb->dev, &fl6);
493 		if (IS_ERR(dst)) {
494 			rcu_read_unlock();
495 			kfree_skb(skb);
496 			return;
497 		}
498 
499 		skb_dst_set(skb, dst);
500 	}
501 
502 	icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, skb->len,
503 					      IPPROTO_ICMPV6,
504 					      csum_partial(icmp6h,
505 							   skb->len, 0));
506 
507 	ip6_nd_hdr(skb, saddr, daddr, READ_ONCE(inet6_sk(sk)->hop_limit), skb->len);
508 
509 	dev = dst_dev_rcu(dst);
510 	idev = __in6_dev_get(dev);
511 	IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTREQUESTS);
512 
513 	err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
514 		      net, sk, skb, NULL, dev,
515 		      dst_output);
516 	if (!err) {
517 		ICMP6MSGOUT_INC_STATS(net, idev, type);
518 		ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
519 	}
520 
521 	rcu_read_unlock();
522 }
523 EXPORT_SYMBOL(ndisc_send_skb);
524 
525 void ndisc_send_na(struct net_device *dev, const struct in6_addr *daddr,
526 		   const struct in6_addr *solicited_addr,
527 		   bool router, bool solicited, bool override, bool inc_opt)
528 {
529 	struct sk_buff *skb;
530 	struct in6_addr tmpaddr;
531 	struct inet6_ifaddr *ifp;
532 	const struct in6_addr *src_addr;
533 	struct nd_msg *msg;
534 	int optlen = 0;
535 
536 	/* for anycast or proxy, solicited_addr != src_addr */
537 	ifp = ipv6_get_ifaddr(dev_net(dev), solicited_addr, dev, 1);
538 	if (ifp) {
539 		src_addr = solicited_addr;
540 		if (ifp->flags & IFA_F_OPTIMISTIC)
541 			override = false;
542 		inc_opt |= READ_ONCE(ifp->idev->cnf.force_tllao);
543 		in6_ifa_put(ifp);
544 	} else {
545 		if (ipv6_dev_get_saddr(dev_net(dev), dev, daddr,
546 				       inet6_sk(dev_net(dev)->ipv6.ndisc_sk)->srcprefs,
547 				       &tmpaddr))
548 			return;
549 		src_addr = &tmpaddr;
550 	}
551 
552 	if (!dev->addr_len)
553 		inc_opt = false;
554 	if (inc_opt)
555 		optlen += ndisc_opt_addr_space(dev,
556 					       NDISC_NEIGHBOUR_ADVERTISEMENT);
557 
558 	skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
559 	if (!skb)
560 		return;
561 
562 	msg = skb_put(skb, sizeof(*msg));
563 	*msg = (struct nd_msg) {
564 		.icmph = {
565 			.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT,
566 			.icmp6_router = router,
567 			.icmp6_solicited = solicited,
568 			.icmp6_override = override,
569 		},
570 		.target = *solicited_addr,
571 	};
572 
573 	if (inc_opt)
574 		ndisc_fill_addr_option(skb, ND_OPT_TARGET_LL_ADDR,
575 				       dev->dev_addr,
576 				       NDISC_NEIGHBOUR_ADVERTISEMENT);
577 
578 	ndisc_send_skb(skb, daddr, src_addr);
579 }
580 EXPORT_SYMBOL_GPL(ndisc_send_na);
581 
582 static void ndisc_send_unsol_na(struct net_device *dev)
583 {
584 	struct inet6_dev *idev;
585 	struct inet6_ifaddr *ifa;
586 
587 	idev = in6_dev_get(dev);
588 	if (!idev)
589 		return;
590 
591 	read_lock_bh(&idev->lock);
592 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
593 		/* skip tentative addresses until dad completes */
594 		if (ifa->flags & IFA_F_TENTATIVE &&
595 		    !(ifa->flags & IFA_F_OPTIMISTIC))
596 			continue;
597 
598 		ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifa->addr,
599 			      /*router=*/ !!idev->cnf.forwarding,
600 			      /*solicited=*/ false, /*override=*/ true,
601 			      /*inc_opt=*/ true);
602 	}
603 	read_unlock_bh(&idev->lock);
604 
605 	in6_dev_put(idev);
606 }
607 
608 struct sk_buff *ndisc_ns_create(struct net_device *dev, const struct in6_addr *solicit,
609 				const struct in6_addr *saddr, u64 nonce)
610 {
611 	int inc_opt = dev->addr_len;
612 	struct sk_buff *skb;
613 	struct nd_msg *msg;
614 	int optlen = 0;
615 
616 	if (!saddr)
617 		return NULL;
618 
619 	if (ipv6_addr_any(saddr))
620 		inc_opt = false;
621 	if (inc_opt)
622 		optlen += ndisc_opt_addr_space(dev,
623 					       NDISC_NEIGHBOUR_SOLICITATION);
624 	if (nonce != 0)
625 		optlen += 8;
626 
627 	skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
628 	if (!skb)
629 		return NULL;
630 
631 	msg = skb_put(skb, sizeof(*msg));
632 	*msg = (struct nd_msg) {
633 		.icmph = {
634 			.icmp6_type = NDISC_NEIGHBOUR_SOLICITATION,
635 		},
636 		.target = *solicit,
637 	};
638 
639 	if (inc_opt)
640 		ndisc_fill_addr_option(skb, ND_OPT_SOURCE_LL_ADDR,
641 				       dev->dev_addr,
642 				       NDISC_NEIGHBOUR_SOLICITATION);
643 	if (nonce != 0) {
644 		u8 *opt = skb_put(skb, 8);
645 
646 		opt[0] = ND_OPT_NONCE;
647 		opt[1] = 8 >> 3;
648 		memcpy(opt + 2, &nonce, 6);
649 	}
650 
651 	return skb;
652 }
653 EXPORT_SYMBOL(ndisc_ns_create);
654 
655 void ndisc_send_ns(struct net_device *dev, const struct in6_addr *solicit,
656 		   const struct in6_addr *daddr, const struct in6_addr *saddr,
657 		   u64 nonce)
658 {
659 	struct in6_addr addr_buf;
660 	struct sk_buff *skb;
661 
662 	if (!saddr) {
663 		if (ipv6_get_lladdr(dev, &addr_buf,
664 				    (IFA_F_TENTATIVE | IFA_F_OPTIMISTIC)))
665 			return;
666 		saddr = &addr_buf;
667 	}
668 
669 	skb = ndisc_ns_create(dev, solicit, saddr, nonce);
670 
671 	if (skb)
672 		ndisc_send_skb(skb, daddr, saddr);
673 }
674 
675 void ndisc_send_rs(struct net_device *dev, const struct in6_addr *saddr,
676 		   const struct in6_addr *daddr)
677 {
678 	struct sk_buff *skb;
679 	struct rs_msg *msg;
680 	int send_sllao = dev->addr_len;
681 	int optlen = 0;
682 
683 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
684 	/*
685 	 * According to section 2.2 of RFC 4429, we must not
686 	 * send router solicitations with a sllao from
687 	 * optimistic addresses, but we may send the solicitation
688 	 * if we don't include the sllao.  So here we check
689 	 * if our address is optimistic, and if so, we
690 	 * suppress the inclusion of the sllao.
691 	 */
692 	if (send_sllao) {
693 		struct inet6_ifaddr *ifp = ipv6_get_ifaddr(dev_net(dev), saddr,
694 							   dev, 1);
695 		if (ifp) {
696 			if (ifp->flags & IFA_F_OPTIMISTIC)  {
697 				send_sllao = 0;
698 			}
699 			in6_ifa_put(ifp);
700 		} else {
701 			send_sllao = 0;
702 		}
703 	}
704 #endif
705 	if (send_sllao)
706 		optlen += ndisc_opt_addr_space(dev, NDISC_ROUTER_SOLICITATION);
707 
708 	skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
709 	if (!skb)
710 		return;
711 
712 	msg = skb_put(skb, sizeof(*msg));
713 	*msg = (struct rs_msg) {
714 		.icmph = {
715 			.icmp6_type = NDISC_ROUTER_SOLICITATION,
716 		},
717 	};
718 
719 	if (send_sllao)
720 		ndisc_fill_addr_option(skb, ND_OPT_SOURCE_LL_ADDR,
721 				       dev->dev_addr,
722 				       NDISC_ROUTER_SOLICITATION);
723 
724 	ndisc_send_skb(skb, daddr, saddr);
725 }
726 
727 
728 static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb)
729 {
730 	/*
731 	 *	"The sender MUST return an ICMP
732 	 *	 destination unreachable"
733 	 */
734 	dst_link_failure(skb);
735 	kfree_skb(skb);
736 }
737 
738 /* Called with locked neigh: either read or both */
739 
740 static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb)
741 {
742 	struct in6_addr *saddr = NULL;
743 	struct in6_addr mcaddr;
744 	struct net_device *dev = neigh->dev;
745 	struct in6_addr *target = (struct in6_addr *)&neigh->primary_key;
746 	int probes = atomic_read(&neigh->probes);
747 
748 	if (skb && ipv6_chk_addr_and_flags(dev_net(dev), &ipv6_hdr(skb)->saddr,
749 					   dev, false, 1,
750 					   IFA_F_TENTATIVE|IFA_F_OPTIMISTIC))
751 		saddr = &ipv6_hdr(skb)->saddr;
752 	probes -= NEIGH_VAR(neigh->parms, UCAST_PROBES);
753 	if (probes < 0) {
754 		if (!(READ_ONCE(neigh->nud_state) & NUD_VALID)) {
755 			net_dbg_ratelimited("%s: trying to ucast probe in NUD_INVALID: %pI6\n",
756 					    __func__, target);
757 		}
758 		ndisc_send_ns(dev, target, target, saddr, 0);
759 	} else if ((probes -= NEIGH_VAR(neigh->parms, APP_PROBES)) < 0) {
760 		neigh_app_ns(neigh);
761 	} else {
762 		addrconf_addr_solict_mult(target, &mcaddr);
763 		ndisc_send_ns(dev, target, &mcaddr, saddr, 0);
764 	}
765 }
766 
767 static int pndisc_is_router(const void *pkey,
768 			    struct net_device *dev)
769 {
770 	struct net *net = dev_net(dev);
771 	struct pneigh_entry *n;
772 	int ret = -1;
773 
774 	n = pneigh_lookup(nd_table(net), pkey, dev);
775 	if (n)
776 		ret = !!(READ_ONCE(n->flags) & NTF_ROUTER);
777 
778 	return ret;
779 }
780 
781 void ndisc_update(const struct net_device *dev, struct neighbour *neigh,
782 		  const u8 *lladdr, u8 new, u32 flags, u8 icmp6_type,
783 		  struct ndisc_options *ndopts)
784 {
785 	neigh_update(neigh, lladdr, new, flags, 0);
786 	/* report ndisc ops about neighbour update */
787 	ndisc_ops_update(dev, neigh, flags, icmp6_type, ndopts);
788 }
789 
790 static enum skb_drop_reason ndisc_recv_ns(struct sk_buff *skb)
791 {
792 	u32 ndoptlen = skb_tail_pointer(skb) - (skb_transport_header(skb) +
793 				    offsetof(struct nd_msg, opt));
794 	struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
795 	const struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
796 	const struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
797 	struct net_device *dev = skb->dev;
798 	struct net *net = dev_net(dev);
799 	int dad = ipv6_addr_any(saddr);
800 	struct inet6_dev *idev = NULL;
801 	struct ndisc_options ndopts;
802 	struct inet6_ifaddr *ifp;
803 	struct neigh_table *tbl;
804 	struct neighbour *neigh;
805 	int is_router = -1;
806 	u8 *lladdr = NULL;
807 	SKB_DR(reason);
808 	u64 nonce = 0;
809 	bool inc;
810 
811 	if (skb->len < sizeof(struct nd_msg))
812 		return SKB_DROP_REASON_PKT_TOO_SMALL;
813 
814 	if (ipv6_addr_is_multicast(&msg->target)) {
815 		net_dbg_ratelimited("NS: multicast target address\n");
816 		return reason;
817 	}
818 
819 	/*
820 	 * RFC2461 7.1.1:
821 	 * DAD has to be destined for solicited node multicast address.
822 	 */
823 	if (dad && !ipv6_addr_is_solict_mult(daddr)) {
824 		net_dbg_ratelimited("NS: bad DAD packet (wrong destination)\n");
825 		return reason;
826 	}
827 
828 	if (!ndisc_parse_options(dev, msg->opt, ndoptlen, &ndopts))
829 		return SKB_DROP_REASON_IPV6_NDISC_BAD_OPTIONS;
830 
831 	if (ndopts.nd_opts_src_lladdr) {
832 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr, dev);
833 		if (!lladdr) {
834 			net_dbg_ratelimited("NS: invalid link-layer address length\n");
835 			return reason;
836 		}
837 
838 		/* RFC2461 7.1.1:
839 		 *	If the IP source address is the unspecified address,
840 		 *	there MUST NOT be source link-layer address option
841 		 *	in the message.
842 		 */
843 		if (dad) {
844 			net_dbg_ratelimited("NS: bad DAD packet (link-layer address option)\n");
845 			return reason;
846 		}
847 	}
848 	if (ndopts.nd_opts_nonce && ndopts.nd_opts_nonce->nd_opt_len == 1)
849 		memcpy(&nonce, (u8 *)(ndopts.nd_opts_nonce + 1), 6);
850 
851 	tbl = nd_table(net);
852 
853 	inc = ipv6_addr_is_multicast(daddr);
854 	ifp = ipv6_get_ifaddr(net, &msg->target, dev, 1);
855 	if (ifp) {
856 have_ifp:
857 		if (ifp->flags & (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)) {
858 			if (dad) {
859 				if (nonce != 0 && ifp->dad_nonce == nonce) {
860 					u8 *np = (u8 *)&nonce;
861 					/* Matching nonce if looped back */
862 					net_dbg_ratelimited("%s: IPv6 DAD loopback for address %pI6c nonce %pM ignored\n",
863 							    ifp->idev->dev->name, &ifp->addr, np);
864 					goto out;
865 				}
866 				/*
867 				 * We are colliding with another node
868 				 * who is doing DAD
869 				 * so fail our DAD process
870 				 */
871 				addrconf_dad_failure(skb, ifp);
872 				return reason;
873 			} else {
874 				/*
875 				 * This is not a dad solicitation.
876 				 * If we are an optimistic node,
877 				 * we should respond.
878 				 * Otherwise, we should ignore it.
879 				 */
880 				if (!(ifp->flags & IFA_F_OPTIMISTIC))
881 					goto out;
882 			}
883 		}
884 
885 		idev = ifp->idev;
886 	} else {
887 		/* perhaps an address on the master device */
888 		if (netif_is_l3_slave(dev)) {
889 			struct net_device *mdev;
890 
891 			mdev = netdev_master_upper_dev_get_rcu(dev);
892 			if (mdev) {
893 				ifp = ipv6_get_ifaddr(net, &msg->target, mdev, 1);
894 				if (ifp)
895 					goto have_ifp;
896 			}
897 		}
898 
899 		idev = in6_dev_get(dev);
900 		if (!idev) {
901 			/* XXX: count this drop? */
902 			return reason;
903 		}
904 
905 		if (ipv6_chk_acast_addr(net, dev, &msg->target) ||
906 		    (READ_ONCE(idev->cnf.forwarding) &&
907 		     (READ_ONCE(net->ipv6.devconf_all->proxy_ndp) ||
908 		      READ_ONCE(idev->cnf.proxy_ndp)) &&
909 		     (is_router = pndisc_is_router(&msg->target, dev)) >= 0)) {
910 			if (!(NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED) &&
911 			    skb->pkt_type != PACKET_HOST &&
912 			    inc &&
913 			    NEIGH_VAR(idev->nd_parms, PROXY_DELAY) != 0) {
914 				/*
915 				 * for anycast or proxy,
916 				 * sender should delay its response
917 				 * by a random time between 0 and
918 				 * MAX_ANYCAST_DELAY_TIME seconds.
919 				 * (RFC2461) -- yoshfuji
920 				 */
921 				struct sk_buff *n = skb_clone(skb, GFP_ATOMIC);
922 				if (n)
923 					pneigh_enqueue(tbl, idev->nd_parms, n);
924 				goto out;
925 			}
926 		} else {
927 			SKB_DR_SET(reason, IPV6_NDISC_NS_OTHERHOST);
928 			goto out;
929 		}
930 	}
931 
932 	if (is_router < 0)
933 		is_router = READ_ONCE(idev->cnf.forwarding);
934 
935 	if (dad) {
936 		ndisc_send_na(dev, &in6addr_linklocal_allnodes, &msg->target,
937 			      !!is_router, false, (ifp != NULL), true);
938 		goto out;
939 	}
940 
941 	if (inc)
942 		NEIGH_CACHE_STAT_INC(tbl, rcv_probes_mcast);
943 	else
944 		NEIGH_CACHE_STAT_INC(tbl, rcv_probes_ucast);
945 
946 	/*
947 	 *	update / create cache entry
948 	 *	for the source address
949 	 */
950 	neigh = ipv6_neigh_lookup(dev, saddr);
951 	if (!neigh && (!inc || lladdr || !dev->addr_len)) {
952 		neigh = ipv6_neigh_create(dev, saddr);
953 		if (IS_ERR(neigh))
954 			neigh = NULL;
955 	}
956 	if (neigh)
957 		ndisc_update(dev, neigh, lladdr, NUD_STALE,
958 			     NEIGH_UPDATE_F_WEAK_OVERRIDE|
959 			     NEIGH_UPDATE_F_OVERRIDE,
960 			     NDISC_NEIGHBOUR_SOLICITATION, &ndopts);
961 	if (neigh || !dev->header_ops) {
962 		ndisc_send_na(dev, saddr, &msg->target, !!is_router,
963 			      true, (ifp != NULL && inc), inc);
964 		if (neigh)
965 			neigh_release(neigh);
966 		reason = SKB_CONSUMED;
967 	}
968 
969 out:
970 	if (ifp)
971 		in6_ifa_put(ifp);
972 	else
973 		in6_dev_put(idev);
974 	return reason;
975 }
976 
977 static int accept_untracked_na(struct inet6_dev *idev, struct in6_addr *saddr)
978 {
979 	switch (READ_ONCE(idev->cnf.accept_untracked_na)) {
980 	case 0: /* Don't accept untracked NA (absent or FAILED) */
981 		return 0;
982 	case 1: /* Create new or update FAILED entries from NA */
983 		return 1;
984 	case 2: /* Create new or update FAILED entries only if saddr is in the
985 		 * same subnet as an address configured on the interface that
986 		 * received the NA
987 		 */
988 		return !!ipv6_chk_prefix(saddr, idev->dev);
989 	default:
990 		return 0;
991 	}
992 }
993 
994 static enum skb_drop_reason ndisc_recv_na(struct sk_buff *skb)
995 {
996 	u32 ndoptlen = skb_tail_pointer(skb) - (skb_transport_header(skb) +
997 				    offsetof(struct nd_msg, opt));
998 	struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
999 	const struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
1000 	struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
1001 	struct net_device *dev = skb->dev;
1002 	struct net *net = dev_net(dev);
1003 	struct ndisc_options ndopts;
1004 	struct inet6_ifaddr *ifp;
1005 	struct neighbour *neigh;
1006 	struct inet6_dev *idev;
1007 	u8 *lladdr = NULL;
1008 	SKB_DR(reason);
1009 	u8 new_state;
1010 
1011 	if (skb->len < sizeof(struct nd_msg))
1012 		return SKB_DROP_REASON_PKT_TOO_SMALL;
1013 
1014 	if (ipv6_addr_is_multicast(&msg->target)) {
1015 		net_dbg_ratelimited("NA: target address is multicast\n");
1016 		return reason;
1017 	}
1018 
1019 	if (ipv6_addr_is_multicast(daddr) &&
1020 	    msg->icmph.icmp6_solicited) {
1021 		net_dbg_ratelimited("NA: solicited NA is multicasted\n");
1022 		return reason;
1023 	}
1024 
1025 	/* For some 802.11 wireless deployments (and possibly other networks),
1026 	 * there will be a NA proxy and unsolicitd packets are attacks
1027 	 * and thus should not be accepted.
1028 	 * drop_unsolicited_na takes precedence over accept_untracked_na
1029 	 */
1030 	idev = __in6_dev_get(dev);
1031 	if (!msg->icmph.icmp6_solicited && idev &&
1032 	    READ_ONCE(idev->cnf.drop_unsolicited_na))
1033 		return reason;
1034 
1035 	if (!ndisc_parse_options(dev, msg->opt, ndoptlen, &ndopts))
1036 		return SKB_DROP_REASON_IPV6_NDISC_BAD_OPTIONS;
1037 
1038 	if (ndopts.nd_opts_tgt_lladdr) {
1039 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, dev);
1040 		if (!lladdr) {
1041 			net_dbg_ratelimited("NA: invalid link-layer address length\n");
1042 			return reason;
1043 		}
1044 	}
1045 	ifp = ipv6_get_ifaddr(net, &msg->target, dev, 1);
1046 	if (ifp) {
1047 		if (skb->pkt_type != PACKET_LOOPBACK
1048 		    && (ifp->flags & IFA_F_TENTATIVE)) {
1049 				addrconf_dad_failure(skb, ifp);
1050 				return reason;
1051 		}
1052 		/* What should we make now? The advertisement
1053 		   is invalid, but ndisc specs say nothing
1054 		   about it. It could be misconfiguration, or
1055 		   an smart proxy agent tries to help us :-)
1056 
1057 		   We should not print the error if NA has been
1058 		   received from loopback - it is just our own
1059 		   unsolicited advertisement.
1060 		 */
1061 		if (skb->pkt_type != PACKET_LOOPBACK)
1062 			net_warn_ratelimited("NA: %pM advertised our address %pI6c on %s!\n",
1063 					     eth_hdr(skb)->h_source, &ifp->addr,
1064 					     ifp->idev->dev->name);
1065 		in6_ifa_put(ifp);
1066 		return reason;
1067 	}
1068 
1069 	neigh = ipv6_neigh_lookup(dev, &msg->target);
1070 
1071 	/* RFC 9131 updates original Neighbour Discovery RFC 4861.
1072 	 * NAs with Target LL Address option can now create a STALE neighbor
1073 	 * cache entry on routers if the NA does not have a corresponding entry
1074 	 * in the neighbour cache or has a corresponding FAILED entry.
1075 	 *
1076 	 *       entry accept  fwding  solicited        behaviour
1077 	 * ----------- ------  ------  ---------    ----------------------
1078 	 *  non-FAILED      X       X         0     Set state to STALE
1079 	 *  non-FAILED      X       X         1     Set state to REACHABLE
1080 	 *      FAILED      0       X         X     Do nothing
1081 	 *      FAILED      1       0         X     Do nothing
1082 	 *      FAILED      1       1         X     Set state to STALE
1083 	 *      absent      0       X         X     Do nothing
1084 	 *      absent      1       0         X     Do nothing
1085 	 *      absent      1       1         X     Add a new STALE entry
1086 	 *
1087 	 * Note that we don't do a (daddr == all-routers-mcast) check.
1088 	 */
1089 	new_state = msg->icmph.icmp6_solicited ? NUD_REACHABLE : NUD_STALE;
1090 	if (!neigh || (READ_ONCE(neigh->nud_state) & NUD_FAILED)) {
1091 		if (!lladdr || !idev || !READ_ONCE(idev->cnf.forwarding) ||
1092 		    !accept_untracked_na(idev, saddr)) {
1093 			if (neigh)
1094 				neigh_release(neigh);
1095 			return reason;
1096 		}
1097 		if (!neigh)
1098 			neigh = ipv6_neigh_create(dev, &msg->target);
1099 		new_state = NUD_STALE;
1100 	}
1101 
1102 	if (neigh && !IS_ERR(neigh)) {
1103 		u8 old_flags = neigh->flags;
1104 
1105 		/*
1106 		 * Don't update the neighbor cache entry on a proxy NA from
1107 		 * ourselves because either the proxied node is off link or it
1108 		 * has already sent a NA to us.
1109 		 */
1110 		if (lladdr && !memcmp(lladdr, dev->dev_addr, dev->addr_len) &&
1111 		    READ_ONCE(net->ipv6.devconf_all->forwarding) &&
1112 		    READ_ONCE(net->ipv6.devconf_all->proxy_ndp) &&
1113 		    pneigh_lookup(nd_table(dev_net(dev)), &msg->target, dev)) {
1114 			/* XXX: idev->cnf.proxy_ndp */
1115 			goto out;
1116 		}
1117 
1118 		ndisc_update(dev, neigh, lladdr,
1119 			     new_state,
1120 			     NEIGH_UPDATE_F_WEAK_OVERRIDE|
1121 			     (msg->icmph.icmp6_override ? NEIGH_UPDATE_F_OVERRIDE : 0)|
1122 			     NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1123 			     (msg->icmph.icmp6_router ? NEIGH_UPDATE_F_ISROUTER : 0),
1124 			     NDISC_NEIGHBOUR_ADVERTISEMENT, &ndopts);
1125 
1126 		if ((old_flags & ~neigh->flags) & NTF_ROUTER) {
1127 			/*
1128 			 * Change: router to host
1129 			 */
1130 			rt6_clean_tohost(dev_net(dev),  saddr);
1131 		}
1132 		reason = SKB_CONSUMED;
1133 out:
1134 		neigh_release(neigh);
1135 	}
1136 	return reason;
1137 }
1138 
1139 static enum skb_drop_reason ndisc_recv_rs(struct sk_buff *skb)
1140 {
1141 	struct rs_msg *rs_msg = (struct rs_msg *)skb_transport_header(skb);
1142 	const struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
1143 	unsigned long ndoptlen = skb->len - sizeof(*rs_msg);
1144 	struct net_device *dev = skb->dev;
1145 	struct ndisc_options ndopts;
1146 	struct neighbour *neigh;
1147 	struct inet6_dev *idev;
1148 	u8 *lladdr = NULL;
1149 	SKB_DR(reason);
1150 
1151 	if (skb->len < sizeof(*rs_msg))
1152 		return SKB_DROP_REASON_PKT_TOO_SMALL;
1153 
1154 	idev = __in6_dev_get(dev);
1155 	if (!idev) {
1156 		net_err_ratelimited("RS: can't find in6 device\n");
1157 		return reason;
1158 	}
1159 
1160 	/* Don't accept RS if we're not in router mode */
1161 	if (!READ_ONCE(idev->cnf.forwarding))
1162 		goto out;
1163 
1164 	/*
1165 	 * Don't update NCE if src = ::;
1166 	 * this implies that the source node has no ip address assigned yet.
1167 	 */
1168 	if (ipv6_addr_any(saddr))
1169 		goto out;
1170 
1171 	/* Parse ND options */
1172 	if (!ndisc_parse_options(dev, rs_msg->opt, ndoptlen, &ndopts))
1173 		return SKB_DROP_REASON_IPV6_NDISC_BAD_OPTIONS;
1174 
1175 	if (ndopts.nd_opts_src_lladdr) {
1176 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr, dev);
1177 		if (!lladdr)
1178 			goto out;
1179 	}
1180 
1181 	neigh = ipv6_neigh_lookup(dev, saddr);
1182 	if (!neigh) {
1183 		neigh = ipv6_neigh_create(dev, saddr);
1184 		if (IS_ERR(neigh))
1185 			goto out;
1186 	}
1187 	if (neigh) {
1188 		ndisc_update(dev, neigh, lladdr, NUD_STALE,
1189 			     NEIGH_UPDATE_F_WEAK_OVERRIDE|
1190 			     NEIGH_UPDATE_F_OVERRIDE|
1191 			     NEIGH_UPDATE_F_OVERRIDE_ISROUTER,
1192 			     NDISC_ROUTER_SOLICITATION, &ndopts);
1193 		neigh_release(neigh);
1194 		reason = SKB_CONSUMED;
1195 	}
1196 out:
1197 	return reason;
1198 }
1199 
1200 static void ndisc_ra_useropt(struct sk_buff *ra, struct nd_opt_hdr *opt)
1201 {
1202 	struct icmp6hdr *icmp6h = (struct icmp6hdr *)skb_transport_header(ra);
1203 	struct sk_buff *skb;
1204 	struct nlmsghdr *nlh;
1205 	struct nduseroptmsg *ndmsg;
1206 	struct net *net = dev_net(ra->dev);
1207 	int err;
1208 	int base_size = NLMSG_ALIGN(sizeof(struct nduseroptmsg)
1209 				    + (opt->nd_opt_len << 3));
1210 	size_t msg_size = base_size + nla_total_size(sizeof(struct in6_addr));
1211 
1212 	skb = nlmsg_new(msg_size, GFP_ATOMIC);
1213 	if (!skb) {
1214 		err = -ENOBUFS;
1215 		goto errout;
1216 	}
1217 
1218 	nlh = nlmsg_put(skb, 0, 0, RTM_NEWNDUSEROPT, base_size, 0);
1219 	if (!nlh) {
1220 		goto nla_put_failure;
1221 	}
1222 
1223 	ndmsg = nlmsg_data(nlh);
1224 	ndmsg->nduseropt_family = AF_INET6;
1225 	ndmsg->nduseropt_ifindex = ra->dev->ifindex;
1226 	ndmsg->nduseropt_icmp_type = icmp6h->icmp6_type;
1227 	ndmsg->nduseropt_icmp_code = icmp6h->icmp6_code;
1228 	ndmsg->nduseropt_opts_len = opt->nd_opt_len << 3;
1229 	ndmsg->nduseropt_pad1 = 0;
1230 	ndmsg->nduseropt_pad2 = 0;
1231 	ndmsg->nduseropt_pad3 = 0;
1232 
1233 	memcpy(ndmsg + 1, opt, opt->nd_opt_len << 3);
1234 
1235 	if (nla_put_in6_addr(skb, NDUSEROPT_SRCADDR, &ipv6_hdr(ra)->saddr))
1236 		goto nla_put_failure;
1237 	nlmsg_end(skb, nlh);
1238 
1239 	rtnl_notify(skb, net, 0, RTNLGRP_ND_USEROPT, NULL, GFP_ATOMIC);
1240 	return;
1241 
1242 nla_put_failure:
1243 	nlmsg_free(skb);
1244 	err = -EMSGSIZE;
1245 errout:
1246 	rtnl_set_sk_err(net, RTNLGRP_ND_USEROPT, err);
1247 }
1248 
1249 static enum skb_drop_reason ndisc_router_discovery(struct sk_buff *skb)
1250 {
1251 	struct ra_msg *ra_msg = (struct ra_msg *)skb_transport_header(skb);
1252 	struct net *net = dev_net(skb->dev);
1253 	bool send_ifinfo_notify = false;
1254 	struct neighbour *neigh = NULL;
1255 	struct ndisc_options ndopts;
1256 	struct fib6_info *rt = NULL;
1257 	struct inet6_dev *in6_dev;
1258 	struct fib6_table *table;
1259 	u32 defrtr_usr_metric;
1260 	unsigned int pref = 0;
1261 	__u32 old_if_flags;
1262 	SKB_DR(reason);
1263 	int lifetime;
1264 	int optlen;
1265 
1266 	__u8 *opt = (__u8 *)(ra_msg + 1);
1267 
1268 	optlen = (skb_tail_pointer(skb) - skb_transport_header(skb)) -
1269 		sizeof(struct ra_msg);
1270 
1271 	net_dbg_ratelimited("RA: %s, dev: %s\n", __func__, skb->dev->name);
1272 	if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
1273 		net_dbg_ratelimited("RA: source address is not link-local\n");
1274 		return reason;
1275 	}
1276 	if (optlen < 0)
1277 		return SKB_DROP_REASON_PKT_TOO_SMALL;
1278 
1279 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1280 	if (skb->ndisc_nodetype == NDISC_NODETYPE_HOST) {
1281 		net_dbg_ratelimited("RA: from host or unauthorized router\n");
1282 		return reason;
1283 	}
1284 #endif
1285 
1286 	in6_dev = __in6_dev_get(skb->dev);
1287 	if (!in6_dev) {
1288 		net_err_ratelimited("RA: can't find inet6 device for %s\n", skb->dev->name);
1289 		return reason;
1290 	}
1291 
1292 	if (!ndisc_parse_options(skb->dev, opt, optlen, &ndopts))
1293 		return SKB_DROP_REASON_IPV6_NDISC_BAD_OPTIONS;
1294 
1295 	if (!ipv6_accept_ra(in6_dev)) {
1296 		net_dbg_ratelimited("RA: %s, did not accept ra for dev: %s\n", __func__,
1297 				    skb->dev->name);
1298 		goto skip_linkparms;
1299 	}
1300 
1301 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1302 	/* skip link-specific parameters from interior routers */
1303 	if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT) {
1304 		net_dbg_ratelimited("RA: %s, nodetype is NODEFAULT, dev: %s\n", __func__,
1305 				    skb->dev->name);
1306 		goto skip_linkparms;
1307 	}
1308 #endif
1309 
1310 	if (in6_dev->if_flags & IF_RS_SENT) {
1311 		/*
1312 		 *	flag that an RA was received after an RS was sent
1313 		 *	out on this interface.
1314 		 */
1315 		in6_dev->if_flags |= IF_RA_RCVD;
1316 	}
1317 
1318 	/*
1319 	 * Remember the managed/otherconf flags from most recently
1320 	 * received RA message (RFC 2462) -- yoshfuji
1321 	 */
1322 	old_if_flags = in6_dev->if_flags;
1323 	in6_dev->if_flags = (in6_dev->if_flags & ~(IF_RA_MANAGED |
1324 				IF_RA_OTHERCONF)) |
1325 				(ra_msg->icmph.icmp6_addrconf_managed ?
1326 					IF_RA_MANAGED : 0) |
1327 				(ra_msg->icmph.icmp6_addrconf_other ?
1328 					IF_RA_OTHERCONF : 0);
1329 
1330 	if (old_if_flags != in6_dev->if_flags)
1331 		send_ifinfo_notify = true;
1332 
1333 	if (!READ_ONCE(in6_dev->cnf.accept_ra_defrtr)) {
1334 		net_dbg_ratelimited("RA: %s, defrtr is false for dev: %s\n", __func__,
1335 				    skb->dev->name);
1336 		goto skip_defrtr;
1337 	}
1338 
1339 	lifetime = ntohs(ra_msg->icmph.icmp6_rt_lifetime);
1340 	if (lifetime != 0 &&
1341 	    lifetime < READ_ONCE(in6_dev->cnf.accept_ra_min_lft)) {
1342 		net_dbg_ratelimited("RA: router lifetime (%ds) is too short: %s\n", lifetime,
1343 				    skb->dev->name);
1344 		goto skip_defrtr;
1345 	}
1346 
1347 	/* Do not accept RA with source-addr found on local machine unless
1348 	 * accept_ra_from_local is set to true.
1349 	 */
1350 	if (!READ_ONCE(in6_dev->cnf.accept_ra_from_local) &&
1351 	    ipv6_chk_addr(net, &ipv6_hdr(skb)->saddr, in6_dev->dev, 0)) {
1352 		net_dbg_ratelimited("RA from local address detected on dev: %s: default router ignored\n",
1353 				    skb->dev->name);
1354 		goto skip_defrtr;
1355 	}
1356 
1357 #ifdef CONFIG_IPV6_ROUTER_PREF
1358 	pref = ra_msg->icmph.icmp6_router_pref;
1359 	/* 10b is handled as if it were 00b (medium) */
1360 	if (pref == ICMPV6_ROUTER_PREF_INVALID ||
1361 	    !READ_ONCE(in6_dev->cnf.accept_ra_rtr_pref))
1362 		pref = ICMPV6_ROUTER_PREF_MEDIUM;
1363 #endif
1364 	/* routes added from RAs do not use nexthop objects */
1365 	rt = rt6_get_dflt_router(net, &ipv6_hdr(skb)->saddr, skb->dev);
1366 	if (rt) {
1367 		neigh = __ip6_dst_neigh_lookup(&rt->fib6_nh->fib_nh_gw6,
1368 					       rt->fib6_nh->fib_nh_dev, NULL,
1369 					       &ipv6_hdr(skb)->saddr);
1370 		if (!neigh) {
1371 			net_err_ratelimited("RA: %s got default router without neighbour\n",
1372 					    __func__);
1373 			fib6_info_release(rt);
1374 			return reason;
1375 		}
1376 	}
1377 	/* Set default route metric as specified by user */
1378 	defrtr_usr_metric = in6_dev->cnf.ra_defrtr_metric;
1379 	/* delete the route if lifetime is 0 or if metric needs change */
1380 	if (rt && (lifetime == 0 || rt->fib6_metric != defrtr_usr_metric)) {
1381 		ip6_del_rt_reason(net, rt,
1382 				  lifetime == 0 ? RT_DEL_REASON_RA_WITHDRAWN :
1383 						  RT_DEL_REASON_UNSPEC);
1384 		rt = NULL;
1385 	}
1386 
1387 	net_dbg_ratelimited("RA: rt: %p  lifetime: %d, metric: %d, for dev: %s\n", rt, lifetime,
1388 			    defrtr_usr_metric, skb->dev->name);
1389 	if (!rt && lifetime) {
1390 		net_dbg_ratelimited("RA: adding default router\n");
1391 
1392 		if (neigh)
1393 			neigh_release(neigh);
1394 
1395 		rt = rt6_add_dflt_router(net, &ipv6_hdr(skb)->saddr,
1396 					 skb->dev, pref, defrtr_usr_metric,
1397 					 lifetime);
1398 		if (!rt) {
1399 			net_err_ratelimited("RA: %s failed to add default route\n", __func__);
1400 			return reason;
1401 		}
1402 
1403 		neigh = __ip6_dst_neigh_lookup(&rt->fib6_nh->fib_nh_gw6,
1404 					       rt->fib6_nh->fib_nh_dev, NULL,
1405 					       &ipv6_hdr(skb)->saddr);
1406 		if (!neigh) {
1407 			net_err_ratelimited("RA: %s got default router without neighbour\n",
1408 					    __func__);
1409 			fib6_info_release(rt);
1410 			return reason;
1411 		}
1412 		neigh->flags |= NTF_ROUTER;
1413 	} else if (rt && IPV6_EXTRACT_PREF(rt->fib6_flags) != pref) {
1414 		struct nl_info nlinfo = {
1415 			.nl_net = net,
1416 		};
1417 		rt->fib6_flags = (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
1418 		inet6_rt_notify(RTM_NEWROUTE, rt, &nlinfo, NLM_F_REPLACE);
1419 	}
1420 
1421 	if (rt) {
1422 		table = rt->fib6_table;
1423 		spin_lock_bh(&table->tb6_lock);
1424 
1425 		fib6_set_expires(rt, jiffies + (HZ * lifetime));
1426 		fib6_add_gc_list(rt);
1427 
1428 		spin_unlock_bh(&table->tb6_lock);
1429 	}
1430 	if (READ_ONCE(in6_dev->cnf.accept_ra_min_hop_limit) < 256 &&
1431 	    ra_msg->icmph.icmp6_hop_limit) {
1432 		if (READ_ONCE(in6_dev->cnf.accept_ra_min_hop_limit) <=
1433 		    ra_msg->icmph.icmp6_hop_limit) {
1434 			WRITE_ONCE(in6_dev->cnf.hop_limit,
1435 				   ra_msg->icmph.icmp6_hop_limit);
1436 			fib6_metric_set(rt, RTAX_HOPLIMIT,
1437 					ra_msg->icmph.icmp6_hop_limit);
1438 		} else {
1439 			net_dbg_ratelimited("RA: Got route advertisement with lower hop_limit than minimum\n");
1440 		}
1441 	}
1442 
1443 skip_defrtr:
1444 
1445 	/*
1446 	 *	Update Reachable Time and Retrans Timer
1447 	 */
1448 
1449 	if (in6_dev->nd_parms) {
1450 		unsigned long rtime = ntohl(ra_msg->retrans_timer);
1451 
1452 		if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/HZ) {
1453 			rtime = (rtime*HZ)/1000;
1454 			if (rtime < HZ/100)
1455 				rtime = HZ/100;
1456 			NEIGH_VAR_SET(in6_dev->nd_parms, RETRANS_TIME, rtime);
1457 			in6_dev->tstamp = jiffies;
1458 			send_ifinfo_notify = true;
1459 		}
1460 
1461 		rtime = ntohl(ra_msg->reachable_time);
1462 		if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/(3*HZ)) {
1463 			rtime = (rtime*HZ)/1000;
1464 
1465 			if (rtime < HZ/10)
1466 				rtime = HZ/10;
1467 
1468 			if (rtime != NEIGH_VAR(in6_dev->nd_parms, BASE_REACHABLE_TIME)) {
1469 				NEIGH_VAR_SET(in6_dev->nd_parms,
1470 					      BASE_REACHABLE_TIME, rtime);
1471 				NEIGH_VAR_SET(in6_dev->nd_parms,
1472 					      GC_STALETIME, 3 * rtime);
1473 				neigh_set_reach_time(in6_dev->nd_parms);
1474 				in6_dev->tstamp = jiffies;
1475 				send_ifinfo_notify = true;
1476 			}
1477 		}
1478 	}
1479 
1480 skip_linkparms:
1481 
1482 	/*
1483 	 *	Process options.
1484 	 */
1485 
1486 	if (!neigh) {
1487 		neigh = ipv6_neigh_lookup(skb->dev, &ipv6_hdr(skb)->saddr);
1488 		if (!neigh) {
1489 			neigh = ipv6_neigh_create(skb->dev, &ipv6_hdr(skb)->saddr);
1490 			if (IS_ERR(neigh))
1491 				neigh = NULL;
1492 		}
1493 	}
1494 	if (neigh) {
1495 		u8 *lladdr = NULL;
1496 		if (ndopts.nd_opts_src_lladdr) {
1497 			lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
1498 						     skb->dev);
1499 			if (!lladdr) {
1500 				net_dbg_ratelimited("RA: invalid link-layer address length\n");
1501 				goto out;
1502 			}
1503 		}
1504 		ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
1505 			     NEIGH_UPDATE_F_WEAK_OVERRIDE|
1506 			     NEIGH_UPDATE_F_OVERRIDE|
1507 			     NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1508 			     NEIGH_UPDATE_F_ISROUTER,
1509 			     NDISC_ROUTER_ADVERTISEMENT, &ndopts);
1510 		reason = SKB_CONSUMED;
1511 	}
1512 
1513 	if (!ipv6_accept_ra(in6_dev)) {
1514 		net_dbg_ratelimited("RA: %s, accept_ra is false for dev: %s\n", __func__,
1515 				    skb->dev->name);
1516 		goto out;
1517 	}
1518 
1519 #ifdef CONFIG_IPV6_ROUTE_INFO
1520 	if (!READ_ONCE(in6_dev->cnf.accept_ra_from_local) &&
1521 	    ipv6_chk_addr(dev_net(in6_dev->dev), &ipv6_hdr(skb)->saddr,
1522 			  in6_dev->dev, 0)) {
1523 		net_dbg_ratelimited("RA from local address detected on dev: %s: router info ignored.\n",
1524 				    skb->dev->name);
1525 		goto skip_routeinfo;
1526 	}
1527 
1528 	if (READ_ONCE(in6_dev->cnf.accept_ra_rtr_pref) && ndopts.nd_opts_ri) {
1529 		struct nd_opt_hdr *p;
1530 		for (p = ndopts.nd_opts_ri;
1531 		     p;
1532 		     p = ndisc_next_option(p, ndopts.nd_opts_ri_end)) {
1533 			struct route_info *ri = (struct route_info *)p;
1534 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1535 			if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT &&
1536 			    ri->prefix_len == 0)
1537 				continue;
1538 #endif
1539 			if (ri->prefix_len == 0 &&
1540 			    !READ_ONCE(in6_dev->cnf.accept_ra_defrtr))
1541 				continue;
1542 			if (ri->lifetime != 0 &&
1543 			    ntohl(ri->lifetime) < READ_ONCE(in6_dev->cnf.accept_ra_min_lft))
1544 				continue;
1545 			if (ri->prefix_len < READ_ONCE(in6_dev->cnf.accept_ra_rt_info_min_plen))
1546 				continue;
1547 			if (ri->prefix_len > READ_ONCE(in6_dev->cnf.accept_ra_rt_info_max_plen))
1548 				continue;
1549 			rt6_route_rcv(skb->dev, (u8 *)p, (p->nd_opt_len) << 3,
1550 				      &ipv6_hdr(skb)->saddr);
1551 		}
1552 	}
1553 
1554 skip_routeinfo:
1555 #endif
1556 
1557 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1558 	/* skip link-specific ndopts from interior routers */
1559 	if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT) {
1560 		net_dbg_ratelimited("RA: %s, nodetype is NODEFAULT (interior routes), dev: %s\n",
1561 				    __func__, skb->dev->name);
1562 		goto out;
1563 	}
1564 #endif
1565 
1566 	if (READ_ONCE(in6_dev->cnf.accept_ra_pinfo) && ndopts.nd_opts_pi) {
1567 		struct nd_opt_hdr *p;
1568 		for (p = ndopts.nd_opts_pi;
1569 		     p;
1570 		     p = ndisc_next_option(p, ndopts.nd_opts_pi_end)) {
1571 			addrconf_prefix_rcv(skb->dev, (u8 *)p,
1572 					    (p->nd_opt_len) << 3,
1573 					    ndopts.nd_opts_src_lladdr != NULL);
1574 		}
1575 	}
1576 
1577 	if (ndopts.nd_opts_mtu && READ_ONCE(in6_dev->cnf.accept_ra_mtu)) {
1578 		__be32 n;
1579 		u32 mtu;
1580 
1581 		memcpy(&n, ((u8 *)(ndopts.nd_opts_mtu+1))+2, sizeof(mtu));
1582 		mtu = ntohl(n);
1583 
1584 		if (READ_ONCE(in6_dev->ra_mtu) != mtu) {
1585 			WRITE_ONCE(in6_dev->ra_mtu, mtu);
1586 			send_ifinfo_notify = true;
1587 		}
1588 
1589 		if (mtu < IPV6_MIN_MTU || mtu > skb->dev->mtu) {
1590 			net_dbg_ratelimited("RA: invalid mtu: %d\n", mtu);
1591 		} else if (READ_ONCE(in6_dev->cnf.mtu6) != mtu) {
1592 			WRITE_ONCE(in6_dev->cnf.mtu6, mtu);
1593 			fib6_metric_set(rt, RTAX_MTU, mtu);
1594 			rt6_mtu_change(skb->dev, mtu);
1595 		}
1596 	}
1597 
1598 	if (ndopts.nd_useropts) {
1599 		struct nd_opt_hdr *p;
1600 		for (p = ndopts.nd_useropts;
1601 		     p;
1602 		     p = ndisc_next_useropt(skb->dev, p,
1603 					    ndopts.nd_useropts_end)) {
1604 			ndisc_ra_useropt(skb, p);
1605 		}
1606 	}
1607 
1608 	if (ndopts.nd_opts_tgt_lladdr || ndopts.nd_opts_rh) {
1609 		net_dbg_ratelimited("RA: invalid RA options\n");
1610 	}
1611 out:
1612 	/* Send a notify if RA changed managed/otherconf flags or
1613 	 * timer settings or ra_mtu value
1614 	 */
1615 	if (send_ifinfo_notify)
1616 		inet6_ifinfo_notify(RTM_NEWLINK, in6_dev);
1617 
1618 	fib6_info_release(rt);
1619 	if (neigh)
1620 		neigh_release(neigh);
1621 	return reason;
1622 }
1623 
1624 static enum skb_drop_reason ndisc_redirect_rcv(struct sk_buff *skb)
1625 {
1626 	struct rd_msg *msg = (struct rd_msg *)skb_transport_header(skb);
1627 	u32 ndoptlen = skb_tail_pointer(skb) - (skb_transport_header(skb) +
1628 				    offsetof(struct rd_msg, opt));
1629 	struct ndisc_options ndopts;
1630 	SKB_DR(reason);
1631 	u8 *hdr;
1632 
1633 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1634 	switch (skb->ndisc_nodetype) {
1635 	case NDISC_NODETYPE_HOST:
1636 	case NDISC_NODETYPE_NODEFAULT:
1637 		net_dbg_ratelimited("Redirect: from host or unauthorized router\n");
1638 		return reason;
1639 	}
1640 #endif
1641 
1642 	if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
1643 		net_dbg_ratelimited("Redirect: source address is not link-local\n");
1644 		return reason;
1645 	}
1646 
1647 	if (!ndisc_parse_options(skb->dev, msg->opt, ndoptlen, &ndopts))
1648 		return SKB_DROP_REASON_IPV6_NDISC_BAD_OPTIONS;
1649 
1650 	if (!ndopts.nd_opts_rh) {
1651 		ip6_redirect_no_header(skb, dev_net(skb->dev),
1652 					skb->dev->ifindex);
1653 		return reason;
1654 	}
1655 
1656 	hdr = (u8 *)ndopts.nd_opts_rh;
1657 	hdr += 8;
1658 	if (!pskb_pull(skb, hdr - skb_transport_header(skb)))
1659 		return SKB_DROP_REASON_PKT_TOO_SMALL;
1660 
1661 	return icmpv6_notify(skb, NDISC_REDIRECT, 0, 0);
1662 }
1663 
1664 static void ndisc_fill_redirect_hdr_option(struct sk_buff *skb,
1665 					   struct sk_buff *orig_skb,
1666 					   int rd_len)
1667 {
1668 	u8 *opt = skb_put(skb, rd_len);
1669 
1670 	memset(opt, 0, 8);
1671 	*(opt++) = ND_OPT_REDIRECT_HDR;
1672 	*(opt++) = (rd_len >> 3);
1673 	opt += 6;
1674 
1675 	skb_copy_bits(orig_skb, skb_network_offset(orig_skb), opt,
1676 		      rd_len - 8);
1677 }
1678 
1679 void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target)
1680 {
1681 	struct net_device *dev = skb->dev;
1682 	struct net *net = dev_net_rcu(dev);
1683 	struct sock *sk = net->ipv6.ndisc_sk;
1684 	int optlen = 0;
1685 	struct inet_peer *peer;
1686 	struct sk_buff *buff;
1687 	struct rd_msg *msg;
1688 	struct in6_addr saddr_buf;
1689 	struct rt6_info *rt;
1690 	struct dst_entry *dst;
1691 	struct flowi6 fl6;
1692 	int rd_len;
1693 	u8 ha_buf[MAX_ADDR_LEN], *ha = NULL,
1694 	   ops_data_buf[NDISC_OPS_REDIRECT_DATA_SPACE], *ops_data = NULL;
1695 	bool ret;
1696 
1697 	if (netif_is_l3_master(dev)) {
1698 		dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
1699 		if (!dev)
1700 			return;
1701 	}
1702 
1703 	if (ipv6_get_lladdr(dev, &saddr_buf, IFA_F_TENTATIVE)) {
1704 		net_dbg_ratelimited("Redirect: no link-local address on %s\n", dev->name);
1705 		return;
1706 	}
1707 
1708 	if (!ipv6_addr_equal(&ipv6_hdr(skb)->daddr, target) &&
1709 	    ipv6_addr_type(target) != (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
1710 		net_dbg_ratelimited("Redirect: target address is not link-local unicast\n");
1711 		return;
1712 	}
1713 
1714 	icmpv6_flow_init(sk, &fl6, NDISC_REDIRECT,
1715 			 &saddr_buf, &ipv6_hdr(skb)->saddr, dev->ifindex);
1716 
1717 	dst = ip6_route_output(net, NULL, &fl6);
1718 	if (dst->error) {
1719 		dst_release(dst);
1720 		return;
1721 	}
1722 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
1723 	if (IS_ERR(dst))
1724 		return;
1725 
1726 	rt = dst_rt6_info(dst);
1727 
1728 	if (rt->rt6i_flags & RTF_GATEWAY) {
1729 		net_dbg_ratelimited("Redirect: destination is not a neighbour\n");
1730 		goto release;
1731 	}
1732 
1733 	peer = inet_getpeer_v6(net->ipv6.peers, &ipv6_hdr(skb)->saddr);
1734 	if (!peer)
1735 		goto release;
1736 	ret = inet_peer_xrlim_allow(peer, 1*HZ);
1737 
1738 	if (!ret)
1739 		goto release;
1740 
1741 	if (dev->addr_len) {
1742 		struct neighbour *neigh = dst_neigh_lookup(skb_dst(skb), target);
1743 		if (!neigh) {
1744 			net_dbg_ratelimited("Redirect: no neigh for target address\n");
1745 			goto release;
1746 		}
1747 
1748 		read_lock_bh(&neigh->lock);
1749 		if (neigh->nud_state & NUD_VALID) {
1750 			memcpy(ha_buf, neigh->ha, dev->addr_len);
1751 			read_unlock_bh(&neigh->lock);
1752 			ha = ha_buf;
1753 			optlen += ndisc_redirect_opt_addr_space(dev, neigh,
1754 								ops_data_buf,
1755 								&ops_data);
1756 		} else
1757 			read_unlock_bh(&neigh->lock);
1758 
1759 		neigh_release(neigh);
1760 	}
1761 
1762 	rd_len = min_t(unsigned int,
1763 		       IPV6_MIN_MTU - sizeof(struct ipv6hdr) - sizeof(*msg) - optlen,
1764 		       skb->len + 8);
1765 	rd_len &= ~0x7;
1766 	optlen += rd_len;
1767 
1768 	buff = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
1769 	if (!buff)
1770 		goto release;
1771 
1772 	msg = skb_put(buff, sizeof(*msg));
1773 	*msg = (struct rd_msg) {
1774 		.icmph = {
1775 			.icmp6_type = NDISC_REDIRECT,
1776 		},
1777 		.target = *target,
1778 		.dest = ipv6_hdr(skb)->daddr,
1779 	};
1780 
1781 	/*
1782 	 *	include target_address option
1783 	 */
1784 
1785 	if (ha)
1786 		ndisc_fill_redirect_addr_option(buff, ha, ops_data);
1787 
1788 	/*
1789 	 *	build redirect option and copy skb over to the new packet.
1790 	 */
1791 
1792 	if (rd_len)
1793 		ndisc_fill_redirect_hdr_option(buff, skb, rd_len);
1794 
1795 	skb_dst_set(buff, dst);
1796 	ndisc_send_skb(buff, &ipv6_hdr(skb)->saddr, &saddr_buf);
1797 	return;
1798 
1799 release:
1800 	dst_release(dst);
1801 }
1802 
1803 static void pndisc_redo(struct sk_buff *skb)
1804 {
1805 	enum skb_drop_reason reason = ndisc_recv_ns(skb);
1806 
1807 	kfree_skb_reason(skb, reason);
1808 }
1809 
1810 static int ndisc_is_multicast(const void *pkey)
1811 {
1812 	return ipv6_addr_is_multicast((struct in6_addr *)pkey);
1813 }
1814 
1815 static bool ndisc_suppress_frag_ndisc(struct sk_buff *skb)
1816 {
1817 	struct inet6_dev *idev = __in6_dev_get(skb->dev);
1818 
1819 	if (!idev)
1820 		return true;
1821 	if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED &&
1822 	    READ_ONCE(idev->cnf.suppress_frag_ndisc)) {
1823 		net_warn_ratelimited("Received fragmented ndisc packet. Carefully consider disabling suppress_frag_ndisc.\n");
1824 		return true;
1825 	}
1826 	return false;
1827 }
1828 
1829 enum skb_drop_reason ndisc_rcv(struct sk_buff *skb)
1830 {
1831 	struct nd_msg *msg;
1832 	SKB_DR(reason);
1833 
1834 	if (ndisc_suppress_frag_ndisc(skb))
1835 		return SKB_DROP_REASON_IPV6_NDISC_FRAG;
1836 
1837 	if (skb_linearize(skb))
1838 		return SKB_DROP_REASON_NOMEM;
1839 
1840 	msg = (struct nd_msg *)skb_transport_header(skb);
1841 
1842 	__skb_push(skb, skb->data - skb_transport_header(skb));
1843 
1844 	if (ipv6_hdr(skb)->hop_limit != 255) {
1845 		net_dbg_ratelimited("NDISC: invalid hop-limit: %d\n", ipv6_hdr(skb)->hop_limit);
1846 		return SKB_DROP_REASON_IPV6_NDISC_HOP_LIMIT;
1847 	}
1848 
1849 	if (msg->icmph.icmp6_code != 0) {
1850 		net_dbg_ratelimited("NDISC: invalid ICMPv6 code: %d\n", msg->icmph.icmp6_code);
1851 		return SKB_DROP_REASON_IPV6_NDISC_BAD_CODE;
1852 	}
1853 
1854 	switch (msg->icmph.icmp6_type) {
1855 	case NDISC_NEIGHBOUR_SOLICITATION:
1856 		memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
1857 		reason = ndisc_recv_ns(skb);
1858 		break;
1859 
1860 	case NDISC_NEIGHBOUR_ADVERTISEMENT:
1861 		reason = ndisc_recv_na(skb);
1862 		break;
1863 
1864 	case NDISC_ROUTER_SOLICITATION:
1865 		reason = ndisc_recv_rs(skb);
1866 		break;
1867 
1868 	case NDISC_ROUTER_ADVERTISEMENT:
1869 		reason = ndisc_router_discovery(skb);
1870 		break;
1871 
1872 	case NDISC_REDIRECT:
1873 		reason = ndisc_redirect_rcv(skb);
1874 		break;
1875 	}
1876 
1877 	return reason;
1878 }
1879 
1880 static int ndisc_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
1881 {
1882 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1883 	struct netdev_notifier_change_info *change_info;
1884 	struct net *net = dev_net(dev);
1885 	struct neigh_table *tbl;
1886 	struct inet6_dev *idev;
1887 	bool evict_nocarrier;
1888 
1889 	tbl = nd_table(net);
1890 
1891 	switch (event) {
1892 	case NETDEV_CHANGEADDR:
1893 		neigh_changeaddr(tbl, dev);
1894 		fib6_run_gc(0, net, false);
1895 		fallthrough;
1896 	case NETDEV_UP:
1897 		idev = in6_dev_get(dev);
1898 		if (!idev)
1899 			break;
1900 		if (READ_ONCE(idev->cnf.ndisc_notify) ||
1901 		    READ_ONCE(net->ipv6.devconf_all->ndisc_notify))
1902 			ndisc_send_unsol_na(dev);
1903 		in6_dev_put(idev);
1904 		break;
1905 	case NETDEV_CHANGE:
1906 		idev = in6_dev_get(dev);
1907 		if (!idev)
1908 			evict_nocarrier = true;
1909 		else {
1910 			evict_nocarrier = READ_ONCE(idev->cnf.ndisc_evict_nocarrier) &&
1911 					  READ_ONCE(net->ipv6.devconf_all->ndisc_evict_nocarrier);
1912 			in6_dev_put(idev);
1913 		}
1914 
1915 		change_info = ptr;
1916 		if (change_info->flags_changed & IFF_NOARP)
1917 			neigh_changeaddr(tbl, dev);
1918 		if (evict_nocarrier && !netif_carrier_ok(dev))
1919 			neigh_carrier_down(tbl, dev);
1920 		break;
1921 	case NETDEV_DOWN:
1922 		neigh_ifdown(tbl, dev);
1923 		fib6_run_gc(0, net, false);
1924 		break;
1925 	case NETDEV_NOTIFY_PEERS:
1926 		ndisc_send_unsol_na(dev);
1927 		break;
1928 	default:
1929 		break;
1930 	}
1931 
1932 	return NOTIFY_DONE;
1933 }
1934 
1935 static struct notifier_block ndisc_netdev_notifier = {
1936 	.notifier_call = ndisc_netdev_event,
1937 	.priority = ADDRCONF_NOTIFY_PRIORITY - 5,
1938 };
1939 
1940 #ifdef CONFIG_SYSCTL
1941 static void ndisc_warn_deprecated_sysctl(const struct ctl_table *ctl,
1942 					 const char *func, const char *dev_name)
1943 {
1944 	static char warncomm[TASK_COMM_LEN];
1945 	static int warned;
1946 	if (strcmp(warncomm, current->comm) && warned < 5) {
1947 		strscpy(warncomm, current->comm);
1948 		pr_warn("process `%s' is using deprecated sysctl (%s) net.ipv6.neigh.%s.%s - use net.ipv6.neigh.%s.%s_ms instead\n",
1949 			warncomm, func,
1950 			dev_name, ctl->procname,
1951 			dev_name, ctl->procname);
1952 		warned++;
1953 	}
1954 }
1955 
1956 int ndisc_ifinfo_sysctl_change(const struct ctl_table *ctl, int write, void *buffer,
1957 		size_t *lenp, loff_t *ppos)
1958 {
1959 	struct net_device *dev = ctl->extra1;
1960 	struct inet6_dev *idev;
1961 	int ret;
1962 
1963 	if ((strcmp(ctl->procname, "retrans_time") == 0) ||
1964 	    (strcmp(ctl->procname, "base_reachable_time") == 0))
1965 		ndisc_warn_deprecated_sysctl(ctl, "syscall", dev ? dev->name : "default");
1966 
1967 	if (strcmp(ctl->procname, "retrans_time") == 0)
1968 		ret = neigh_proc_dointvec(ctl, write, buffer, lenp, ppos);
1969 
1970 	else if (strcmp(ctl->procname, "base_reachable_time") == 0)
1971 		ret = neigh_proc_dointvec_jiffies(ctl, write,
1972 						  buffer, lenp, ppos);
1973 
1974 	else if ((strcmp(ctl->procname, "retrans_time_ms") == 0) ||
1975 		 (strcmp(ctl->procname, "base_reachable_time_ms") == 0))
1976 		ret = neigh_proc_dointvec_ms_jiffies(ctl, write,
1977 						     buffer, lenp, ppos);
1978 	else
1979 		ret = -1;
1980 
1981 	if (write && ret == 0 && dev && (idev = in6_dev_get(dev)) != NULL) {
1982 		if (ctl->data == NEIGH_VAR_PTR(idev->nd_parms, BASE_REACHABLE_TIME))
1983 			neigh_set_reach_time(idev->nd_parms);
1984 
1985 		WRITE_ONCE(idev->tstamp, jiffies);
1986 		inet6_ifinfo_notify(RTM_NEWLINK, idev);
1987 		in6_dev_put(idev);
1988 	}
1989 	return ret;
1990 }
1991 
1992 
1993 #endif
1994 
1995 static int __net_init ndisc_net_init(struct net *net)
1996 {
1997 	struct ipv6_pinfo *np;
1998 	struct sock *sk;
1999 	int err;
2000 
2001 	err = neigh_table_register(net, &nd_tbl, NEIGH_ND_TABLE);
2002 	if (err)
2003 		goto err;
2004 
2005 #ifdef CONFIG_SYSCTL
2006 	err = neigh_sysctl_register(NULL, &nd_table(net)->parms,
2007 				    ndisc_ifinfo_sysctl_change);
2008 	if (err)
2009 		goto err_sysctl;
2010 #endif
2011 
2012 	err = inet_ctl_sock_create(&sk, PF_INET6,
2013 				   SOCK_RAW, IPPROTO_ICMPV6, net);
2014 	if (err < 0) {
2015 		net_err_ratelimited("NDISC: Failed to initialize the control socket (err %d)\n",
2016 				    err);
2017 		goto err_sock_create;
2018 	}
2019 
2020 	net->ipv6.ndisc_sk = sk;
2021 
2022 	np = inet6_sk(sk);
2023 	np->hop_limit = 255;
2024 	/* Do not loopback ndisc messages */
2025 	inet6_clear_bit(MC6_LOOP, sk);
2026 
2027 	return 0;
2028 
2029 err_sock_create:
2030 #ifdef CONFIG_SYSCTL
2031 	neigh_sysctl_unregister(&nd_table(net)->parms);
2032 err_sysctl:
2033 #endif
2034 	neigh_table_unregister(net, NEIGH_ND_TABLE);
2035 err:
2036 	return err;
2037 }
2038 
2039 static void __net_exit ndisc_net_exit(struct net *net)
2040 {
2041 	inet_ctl_sock_destroy(net->ipv6.ndisc_sk);
2042 #ifdef CONFIG_SYSCTL
2043 	neigh_sysctl_unregister(&nd_table(net)->parms);
2044 #endif
2045 	neigh_table_unregister(net, NEIGH_ND_TABLE);
2046 }
2047 
2048 static struct pernet_operations ndisc_net_ops = {
2049 	.init = ndisc_net_init,
2050 	.exit = ndisc_net_exit,
2051 };
2052 
2053 int __init ndisc_init(void)
2054 {
2055 	return register_pernet_subsys(&ndisc_net_ops);
2056 }
2057 
2058 int __init ndisc_late_init(void)
2059 {
2060 	return register_netdevice_notifier(&ndisc_netdev_notifier);
2061 }
2062 
2063 void ndisc_late_cleanup(void)
2064 {
2065 	unregister_netdevice_notifier(&ndisc_netdev_notifier);
2066 }
2067 
2068 void ndisc_cleanup(void)
2069 {
2070 	unregister_pernet_subsys(&ndisc_net_ops);
2071 }
2072