xref: /linux/include/net/ndisc.h (revision 0e59523b440c90ed1899476f32ad4e949282f43e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NDISC_H
3 #define _NDISC_H
4 
5 /*
6  *	ICMP codes for neighbour discovery messages
7  */
8 
9 #define NDISC_ROUTER_SOLICITATION	133
10 #define NDISC_ROUTER_ADVERTISEMENT	134
11 #define NDISC_NEIGHBOUR_SOLICITATION	135
12 #define NDISC_NEIGHBOUR_ADVERTISEMENT	136
13 #define NDISC_REDIRECT			137
14 
15 /*
16  * Router type: cross-layer information from link-layer to
17  * IPv6 layer reported by certain link types (e.g., RFC4214).
18  */
19 #define NDISC_NODETYPE_UNSPEC		0	/* unspecified (default) */
20 #define NDISC_NODETYPE_HOST		1	/* host or unauthorized router */
21 #define NDISC_NODETYPE_NODEFAULT	2	/* non-default router */
22 #define NDISC_NODETYPE_DEFAULT		3	/* default router */
23 
24 /*
25  *	ndisc options
26  */
27 
28 enum {
29 	__ND_OPT_PREFIX_INFO_END = 0,
30 	ND_OPT_SOURCE_LL_ADDR = 1,	/* RFC2461 */
31 	ND_OPT_TARGET_LL_ADDR = 2,	/* RFC2461 */
32 	ND_OPT_PREFIX_INFO = 3,		/* RFC2461 */
33 	ND_OPT_REDIRECT_HDR = 4,	/* RFC2461 */
34 	ND_OPT_MTU = 5,			/* RFC2461 */
35 	ND_OPT_NONCE = 14,              /* RFC7527 */
36 	__ND_OPT_ARRAY_MAX,
37 	ND_OPT_ROUTE_INFO = 24,		/* RFC4191 */
38 	ND_OPT_RDNSS = 25,		/* RFC5006 */
39 	ND_OPT_DNSSL = 31,		/* RFC6106 */
40 	ND_OPT_6CO = 34,		/* RFC6775 */
41 	ND_OPT_CAPTIVE_PORTAL = 37,	/* RFC7710 */
42 	ND_OPT_PREF64 = 38,		/* RFC8781 */
43 	__ND_OPT_MAX
44 };
45 
46 #define MAX_RTR_SOLICITATION_DELAY	HZ
47 
48 #define ND_REACHABLE_TIME		(30*HZ)
49 #define ND_RETRANS_TIMER		HZ
50 
51 #include <linux/compiler.h>
52 #include <linux/icmpv6.h>
53 #include <linux/in6.h>
54 #include <linux/types.h>
55 #include <linux/if_arp.h>
56 #include <linux/netdevice.h>
57 #include <linux/hash.h>
58 
59 #include <net/neighbour.h>
60 
61 struct ctl_table;
62 struct inet6_dev;
63 struct net_device;
64 struct net_proto_family;
65 struct sk_buff;
66 struct prefix_info;
67 
68 extern struct neigh_table nd_tbl;
69 
70 static inline struct neigh_table *nd_table(struct net *net)
71 {
72 #if IS_ENABLED(CONFIG_IPV6)
73 	if (disable_ipv6_mod)
74 		return &nd_tbl;
75 #endif
76 	return net->neigh_tables[NEIGH_ND_TABLE];
77 }
78 
79 struct nd_msg {
80         struct icmp6hdr	icmph;
81         struct in6_addr	target;
82 	__u8		opt[];
83 };
84 
85 struct rs_msg {
86 	struct icmp6hdr	icmph;
87 	__u8		opt[];
88 };
89 
90 struct ra_msg {
91         struct icmp6hdr		icmph;
92 	__be32			reachable_time;
93 	__be32			retrans_timer;
94 };
95 
96 struct rd_msg {
97 	struct icmp6hdr icmph;
98 	struct in6_addr	target;
99 	struct in6_addr	dest;
100 	__u8		opt[];
101 };
102 
103 struct nd_opt_hdr {
104 	__u8		nd_opt_type;
105 	__u8		nd_opt_len;
106 } __packed;
107 
108 /* ND options */
109 struct ndisc_options {
110 	struct nd_opt_hdr *nd_opt_array[__ND_OPT_ARRAY_MAX];
111 #ifdef CONFIG_IPV6_ROUTE_INFO
112 	struct nd_opt_hdr *nd_opts_ri;
113 	struct nd_opt_hdr *nd_opts_ri_end;
114 #endif
115 	struct nd_opt_hdr *nd_useropts;
116 	struct nd_opt_hdr *nd_useropts_end;
117 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
118 	struct nd_opt_hdr *nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR + 1];
119 #endif
120 };
121 
122 #define nd_opts_src_lladdr		nd_opt_array[ND_OPT_SOURCE_LL_ADDR]
123 #define nd_opts_tgt_lladdr		nd_opt_array[ND_OPT_TARGET_LL_ADDR]
124 #define nd_opts_pi			nd_opt_array[ND_OPT_PREFIX_INFO]
125 #define nd_opts_pi_end			nd_opt_array[__ND_OPT_PREFIX_INFO_END]
126 #define nd_opts_rh			nd_opt_array[ND_OPT_REDIRECT_HDR]
127 #define nd_opts_mtu			nd_opt_array[ND_OPT_MTU]
128 #define nd_opts_nonce			nd_opt_array[ND_OPT_NONCE]
129 #define nd_802154_opts_src_lladdr	nd_802154_opt_array[ND_OPT_SOURCE_LL_ADDR]
130 #define nd_802154_opts_tgt_lladdr	nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR]
131 
132 #define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
133 
134 struct ndisc_options *ndisc_parse_options(const struct net_device *dev,
135 					  u8 *opt, int opt_len,
136 					  struct ndisc_options *ndopts);
137 
138 void __ndisc_fill_addr_option(struct sk_buff *skb, int type, const void *data,
139 			      int data_len, int pad);
140 
141 #define NDISC_OPS_REDIRECT_DATA_SPACE	2
142 
143 /*
144  * This structure defines the hooks for IPv6 neighbour discovery.
145  * The following hooks can be defined; unless noted otherwise, they are
146  * optional and can be filled with a null pointer.
147  *
148  * int (*parse_options)(const struct net_device *dev,
149  *			struct nd_opt_hdr *nd_opt,
150  *			struct ndisc_options *ndopts):
151  *     This function is called while parsing ndisc ops and put each position
152  *     as pointer into ndopts. If this function return unequal 0, then this
153  *     function took care about the ndisc option, if 0 then the IPv6 ndisc
154  *     option parser will take care about that option.
155  *
156  * void (*update)(const struct net_device *dev, struct neighbour *n,
157  *		  u32 flags, u8 icmp6_type,
158  *		  const struct ndisc_options *ndopts):
159  *     This function is called when IPv6 ndisc updates the neighbour cache
160  *     entry. Additional options which can be updated may be previously
161  *     parsed by parse_opts callback and accessible over ndopts parameter.
162  *
163  * int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
164  *			 struct neighbour *neigh, u8 *ha_buf,
165  *			 u8 **ha):
166  *     This function is called when the necessary option space will be
167  *     calculated before allocating a skb. The parameters neigh, ha_buf
168  *     abd ha are available on NDISC_REDIRECT messages only.
169  *
170  * void (*fill_addr_option)(const struct net_device *dev,
171  *			    struct sk_buff *skb, u8 icmp6_type,
172  *			    const u8 *ha):
173  *     This function is called when the skb will finally fill the option
174  *     fields inside skb. NOTE: this callback should fill the option
175  *     fields to the skb which are previously indicated by opt_space
176  *     parameter. That means the decision to add such option should
177  *     not lost between these two callbacks, e.g. protected by interface
178  *     up state.
179  *
180  * void (*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
181  *			       const struct prefix_info *pinfo,
182  *			       struct inet6_dev *in6_dev,
183  *			       struct in6_addr *addr,
184  *			       int addr_type, u32 addr_flags,
185  *			       bool sllao, bool tokenized,
186  *			       __u32 valid_lft, u32 prefered_lft,
187  *			       bool dev_addr_generated):
188  *     This function is called when a RA messages is received with valid
189  *     PIO option fields and an IPv6 address will be added to the interface
190  *     for autoconfiguration. The parameter dev_addr_generated reports about
191  *     if the address was based on dev->dev_addr or not. This can be used
192  *     to add a second address if link-layer operates with two link layer
193  *     addresses. E.g. 802.15.4 6LoWPAN.
194  */
195 struct ndisc_ops {
196 	int	(*parse_options)(const struct net_device *dev,
197 				 struct nd_opt_hdr *nd_opt,
198 				 struct ndisc_options *ndopts);
199 	void	(*update)(const struct net_device *dev, struct neighbour *n,
200 			  u32 flags, u8 icmp6_type,
201 			  const struct ndisc_options *ndopts);
202 	int	(*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
203 				  struct neighbour *neigh, u8 *ha_buf,
204 				  u8 **ha);
205 	void	(*fill_addr_option)(const struct net_device *dev,
206 				    struct sk_buff *skb, u8 icmp6_type,
207 				    const u8 *ha);
208 	void	(*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
209 				       const struct prefix_info *pinfo,
210 				       struct inet6_dev *in6_dev,
211 				       struct in6_addr *addr,
212 				       int addr_type, u32 addr_flags,
213 				       bool sllao, bool tokenized,
214 				       __u32 valid_lft, u32 prefered_lft,
215 				       bool dev_addr_generated);
216 };
217 
218 #if IS_ENABLED(CONFIG_IPV6)
219 static inline int ndisc_ops_parse_options(const struct net_device *dev,
220 					  struct nd_opt_hdr *nd_opt,
221 					  struct ndisc_options *ndopts)
222 {
223 	if (dev->ndisc_ops && dev->ndisc_ops->parse_options)
224 		return dev->ndisc_ops->parse_options(dev, nd_opt, ndopts);
225 	else
226 		return 0;
227 }
228 
229 static inline void ndisc_ops_update(const struct net_device *dev,
230 					  struct neighbour *n, u32 flags,
231 					  u8 icmp6_type,
232 					  const struct ndisc_options *ndopts)
233 {
234 	if (dev->ndisc_ops && dev->ndisc_ops->update)
235 		dev->ndisc_ops->update(dev, n, flags, icmp6_type, ndopts);
236 }
237 
238 static inline int ndisc_ops_opt_addr_space(const struct net_device *dev,
239 					   u8 icmp6_type)
240 {
241 	if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space &&
242 	    icmp6_type != NDISC_REDIRECT)
243 		return dev->ndisc_ops->opt_addr_space(dev, icmp6_type, NULL,
244 						      NULL, NULL);
245 	else
246 		return 0;
247 }
248 
249 static inline int ndisc_ops_redirect_opt_addr_space(const struct net_device *dev,
250 						    struct neighbour *neigh,
251 						    u8 *ha_buf, u8 **ha)
252 {
253 	if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space)
254 		return dev->ndisc_ops->opt_addr_space(dev, NDISC_REDIRECT,
255 						      neigh, ha_buf, ha);
256 	else
257 		return 0;
258 }
259 
260 static inline void ndisc_ops_fill_addr_option(const struct net_device *dev,
261 					      struct sk_buff *skb,
262 					      u8 icmp6_type)
263 {
264 	if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option &&
265 	    icmp6_type != NDISC_REDIRECT)
266 		dev->ndisc_ops->fill_addr_option(dev, skb, icmp6_type, NULL);
267 }
268 
269 static inline void ndisc_ops_fill_redirect_addr_option(const struct net_device *dev,
270 						       struct sk_buff *skb,
271 						       const u8 *ha)
272 {
273 	if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option)
274 		dev->ndisc_ops->fill_addr_option(dev, skb, NDISC_REDIRECT, ha);
275 }
276 
277 static inline void ndisc_ops_prefix_rcv_add_addr(struct net *net,
278 						 struct net_device *dev,
279 						 const struct prefix_info *pinfo,
280 						 struct inet6_dev *in6_dev,
281 						 struct in6_addr *addr,
282 						 int addr_type, u32 addr_flags,
283 						 bool sllao, bool tokenized,
284 						 __u32 valid_lft,
285 						 u32 prefered_lft,
286 						 bool dev_addr_generated)
287 {
288 	if (dev->ndisc_ops && dev->ndisc_ops->prefix_rcv_add_addr)
289 		dev->ndisc_ops->prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
290 						    addr, addr_type,
291 						    addr_flags, sllao,
292 						    tokenized, valid_lft,
293 						    prefered_lft,
294 						    dev_addr_generated);
295 }
296 #endif
297 
298 /*
299  * Return the padding between the option length and the start of the
300  * link addr.  Currently only IP-over-InfiniBand needs this, although
301  * if RFC 3831 IPv6-over-Fibre Channel is ever implemented it may
302  * also need a pad of 2.
303  */
304 static inline int ndisc_addr_option_pad(unsigned short type)
305 {
306 	switch (type) {
307 	case ARPHRD_INFINIBAND: return 2;
308 	default:                return 0;
309 	}
310 }
311 
312 static inline int __ndisc_opt_addr_space(unsigned char addr_len, int pad)
313 {
314 	return NDISC_OPT_SPACE(addr_len + pad);
315 }
316 
317 #if IS_ENABLED(CONFIG_IPV6)
318 static inline int ndisc_opt_addr_space(struct net_device *dev, u8 icmp6_type)
319 {
320 	return __ndisc_opt_addr_space(dev->addr_len,
321 				      ndisc_addr_option_pad(dev->type)) +
322 		ndisc_ops_opt_addr_space(dev, icmp6_type);
323 }
324 
325 static inline int ndisc_redirect_opt_addr_space(struct net_device *dev,
326 						struct neighbour *neigh,
327 						u8 *ops_data_buf,
328 						u8 **ops_data)
329 {
330 	return __ndisc_opt_addr_space(dev->addr_len,
331 				      ndisc_addr_option_pad(dev->type)) +
332 		ndisc_ops_redirect_opt_addr_space(dev, neigh, ops_data_buf,
333 						  ops_data);
334 }
335 #endif
336 
337 static inline u8 *__ndisc_opt_addr_data(struct nd_opt_hdr *p,
338 					unsigned char addr_len, int prepad)
339 {
340 	u8 *lladdr = (u8 *)(p + 1);
341 	int lladdrlen = p->nd_opt_len << 3;
342 	if (lladdrlen != __ndisc_opt_addr_space(addr_len, prepad))
343 		return NULL;
344 	return lladdr + prepad;
345 }
346 
347 static inline u8 *ndisc_opt_addr_data(struct nd_opt_hdr *p,
348 				      struct net_device *dev)
349 {
350 	return __ndisc_opt_addr_data(p, dev->addr_len,
351 				     ndisc_addr_option_pad(dev->type));
352 }
353 
354 static inline u32 ndisc_hashfn(const void *pkey, const struct net_device *dev, __u32 *hash_rnd)
355 {
356 	const u32 *p32 = pkey;
357 
358 	return (((p32[0] ^ hash32_ptr(dev)) * hash_rnd[0]) +
359 		(p32[1] * hash_rnd[1]) +
360 		(p32[2] * hash_rnd[2]) +
361 		(p32[3] * hash_rnd[3]));
362 }
363 
364 static inline struct neighbour *__ipv6_neigh_lookup_noref(struct net_device *dev, const void *pkey)
365 {
366 	struct neigh_table *tbl = nd_table(dev_net(dev));
367 
368 	return ___neigh_lookup_noref(tbl, neigh_key_eq128, ndisc_hashfn, pkey, dev);
369 }
370 
371 static inline struct neighbour *__ipv6_neigh_lookup(struct net_device *dev, const void *pkey)
372 {
373 	struct neighbour *n;
374 
375 	rcu_read_lock();
376 	n = __ipv6_neigh_lookup_noref(dev, pkey);
377 	if (n && !refcount_inc_not_zero(&n->refcnt))
378 		n = NULL;
379 	rcu_read_unlock();
380 
381 	return n;
382 }
383 
384 static inline struct neighbour *ipv6_neigh_lookup(struct net_device *dev,
385 						  const void *pkey)
386 {
387 	struct neigh_table *tbl = nd_table(dev_net(dev));
388 
389 	return neigh_lookup(tbl, pkey, dev);
390 }
391 
392 static inline struct neighbour *ipv6_neigh_create(struct net_device *dev,
393 						  const void *pkey)
394 {
395 	struct neigh_table *tbl = nd_table(dev_net(dev));
396 
397 	return neigh_create(tbl, pkey, dev);
398 }
399 
400 static inline struct neighbour *ipv6_neigh_create_noref(struct net_device *dev,
401 							const void *pkey)
402 {
403 	struct neigh_table *tbl = nd_table(dev_net(dev));
404 
405 	return __neigh_create(tbl, pkey, dev, false);
406 }
407 
408 static inline void __ipv6_confirm_neigh(struct net_device *dev,
409 					const void *pkey)
410 {
411 	struct neighbour *n;
412 
413 	rcu_read_lock();
414 	n = __ipv6_neigh_lookup_noref(dev, pkey);
415 	neigh_confirm(n);
416 	rcu_read_unlock();
417 }
418 
419 static inline struct neighbour *ip_neigh_gw6(struct net_device *dev,
420 					     const void *addr)
421 {
422 #if IS_ENABLED(CONFIG_IPV6)
423 	struct neighbour *neigh;
424 
425 	neigh = __ipv6_neigh_lookup_noref(dev, addr);
426 	if (unlikely(!neigh))
427 		neigh = ipv6_neigh_create_noref(dev, addr);
428 
429 	return neigh;
430 #else
431 	return ERR_PTR(-EAFNOSUPPORT);
432 #endif
433 }
434 
435 int ndisc_init(void);
436 int ndisc_late_init(void);
437 
438 void ndisc_late_cleanup(void);
439 void ndisc_cleanup(void);
440 
441 enum skb_drop_reason ndisc_rcv(struct sk_buff *skb);
442 
443 struct sk_buff *ndisc_ns_create(struct net_device *dev, const struct in6_addr *solicit,
444 				const struct in6_addr *saddr, u64 nonce);
445 void ndisc_send_ns(struct net_device *dev, const struct in6_addr *solicit,
446 		   const struct in6_addr *daddr, const struct in6_addr *saddr,
447 		   u64 nonce);
448 
449 void ndisc_send_skb(struct sk_buff *skb, const struct in6_addr *daddr,
450 		    const struct in6_addr *saddr);
451 
452 void ndisc_send_rs(struct net_device *dev,
453 		   const struct in6_addr *saddr, const struct in6_addr *daddr);
454 
455 void ndisc_send_na(struct net_device *dev, const struct in6_addr *daddr,
456 		   const struct in6_addr *solicited_addr,
457 		   bool router, bool solicited, bool override, bool inc_opt);
458 
459 void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target);
460 
461 int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev,
462 		 int dir);
463 
464 void ndisc_update(const struct net_device *dev, struct neighbour *neigh,
465 		  const u8 *lladdr, u8 new, u32 flags, u8 icmp6_type,
466 		  struct ndisc_options *ndopts);
467 
468 int ndisc_check_ns_na(struct sk_buff *skb);
469 
470 /*
471  *	IGMP
472  */
473 int igmp6_init(void);
474 int igmp6_late_init(void);
475 
476 void igmp6_cleanup(void);
477 void igmp6_late_cleanup(void);
478 
479 void igmp6_event_query(struct sk_buff *skb);
480 
481 void igmp6_event_report(struct sk_buff *skb);
482 
483 
484 #ifdef CONFIG_SYSCTL
485 int ndisc_ifinfo_sysctl_change(const struct ctl_table *ctl, int write,
486 			       void *buffer, size_t *lenp, loff_t *ppos);
487 #endif
488 
489 void inet6_ifinfo_notify(int event, struct inet6_dev *idev);
490 
491 #endif
492