• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,		/* RFC-ietf-6man-ra-pref64-09 */
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 /* Set to 3 to get tracing... */
62 #define ND_DEBUG 1
63 
64 #define ND_PRINTK(val, level, fmt, ...)				\
65 do {								\
66 	if (val <= ND_DEBUG)					\
67 		net_##level##_ratelimited(fmt, ##__VA_ARGS__);	\
68 } while (0)
69 
70 struct ctl_table;
71 struct inet6_dev;
72 struct net_device;
73 struct net_proto_family;
74 struct sk_buff;
75 struct prefix_info;
76 
77 extern struct neigh_table nd_tbl;
78 
79 struct nd_msg {
80         struct icmp6hdr	icmph;
81         struct in6_addr	target;
82 	__u8		opt[0];
83 };
84 
85 struct rs_msg {
86 	struct icmp6hdr	icmph;
87 	__u8		opt[0];
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[0];
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, 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 (*is_useropt)(u8 nd_opt_type):
149  *     This function is called when IPv6 decide RA userspace options. if
150  *     this function returns 1 then the option given by nd_opt_type will
151  *     be handled as userspace option additional to the IPv6 options.
152  *
153  * int (*parse_options)(const struct net_device *dev,
154  *			struct nd_opt_hdr *nd_opt,
155  *			struct ndisc_options *ndopts):
156  *     This function is called while parsing ndisc ops and put each position
157  *     as pointer into ndopts. If this function return unequal 0, then this
158  *     function took care about the ndisc option, if 0 then the IPv6 ndisc
159  *     option parser will take care about that option.
160  *
161  * void (*update)(const struct net_device *dev, struct neighbour *n,
162  *		  u32 flags, u8 icmp6_type,
163  *		  const struct ndisc_options *ndopts):
164  *     This function is called when IPv6 ndisc updates the neighbour cache
165  *     entry. Additional options which can be updated may be previously
166  *     parsed by parse_opts callback and accessible over ndopts parameter.
167  *
168  * int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
169  *			 struct neighbour *neigh, u8 *ha_buf,
170  *			 u8 **ha):
171  *     This function is called when the necessary option space will be
172  *     calculated before allocating a skb. The parameters neigh, ha_buf
173  *     abd ha are available on NDISC_REDIRECT messages only.
174  *
175  * void (*fill_addr_option)(const struct net_device *dev,
176  *			    struct sk_buff *skb, u8 icmp6_type,
177  *			    const u8 *ha):
178  *     This function is called when the skb will finally fill the option
179  *     fields inside skb. NOTE: this callback should fill the option
180  *     fields to the skb which are previously indicated by opt_space
181  *     parameter. That means the decision to add such option should
182  *     not lost between these two callbacks, e.g. protected by interface
183  *     up state.
184  *
185  * void (*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
186  *			       const struct prefix_info *pinfo,
187  *			       struct inet6_dev *in6_dev,
188  *			       struct in6_addr *addr,
189  *			       int addr_type, u32 addr_flags,
190  *			       bool sllao, bool tokenized,
191  *			       __u32 valid_lft, u32 prefered_lft,
192  *			       bool dev_addr_generated):
193  *     This function is called when a RA messages is received with valid
194  *     PIO option fields and an IPv6 address will be added to the interface
195  *     for autoconfiguration. The parameter dev_addr_generated reports about
196  *     if the address was based on dev->dev_addr or not. This can be used
197  *     to add a second address if link-layer operates with two link layer
198  *     addresses. E.g. 802.15.4 6LoWPAN.
199  */
200 struct ndisc_ops {
201 	int	(*is_useropt)(u8 nd_opt_type);
202 	int	(*parse_options)(const struct net_device *dev,
203 				 struct nd_opt_hdr *nd_opt,
204 				 struct ndisc_options *ndopts);
205 	void	(*update)(const struct net_device *dev, struct neighbour *n,
206 			  u32 flags, u8 icmp6_type,
207 			  const struct ndisc_options *ndopts);
208 	int	(*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
209 				  struct neighbour *neigh, u8 *ha_buf,
210 				  u8 **ha);
211 	void	(*fill_addr_option)(const struct net_device *dev,
212 				    struct sk_buff *skb, u8 icmp6_type,
213 				    const u8 *ha);
214 	void	(*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
215 				       const struct prefix_info *pinfo,
216 				       struct inet6_dev *in6_dev,
217 				       struct in6_addr *addr,
218 				       int addr_type, u32 addr_flags,
219 				       bool sllao, bool tokenized,
220 				       __u32 valid_lft, u32 prefered_lft,
221 				       bool dev_addr_generated);
222 };
223 
224 #if IS_ENABLED(CONFIG_IPV6)
ndisc_ops_is_useropt(const struct net_device * dev,u8 nd_opt_type)225 static inline int ndisc_ops_is_useropt(const struct net_device *dev,
226 				       u8 nd_opt_type)
227 {
228 	if (dev->ndisc_ops && dev->ndisc_ops->is_useropt)
229 		return dev->ndisc_ops->is_useropt(nd_opt_type);
230 	else
231 		return 0;
232 }
233 
ndisc_ops_parse_options(const struct net_device * dev,struct nd_opt_hdr * nd_opt,struct ndisc_options * ndopts)234 static inline int ndisc_ops_parse_options(const struct net_device *dev,
235 					  struct nd_opt_hdr *nd_opt,
236 					  struct ndisc_options *ndopts)
237 {
238 	if (dev->ndisc_ops && dev->ndisc_ops->parse_options)
239 		return dev->ndisc_ops->parse_options(dev, nd_opt, ndopts);
240 	else
241 		return 0;
242 }
243 
ndisc_ops_update(const struct net_device * dev,struct neighbour * n,u32 flags,u8 icmp6_type,const struct ndisc_options * ndopts)244 static inline void ndisc_ops_update(const struct net_device *dev,
245 					  struct neighbour *n, u32 flags,
246 					  u8 icmp6_type,
247 					  const struct ndisc_options *ndopts)
248 {
249 	if (dev->ndisc_ops && dev->ndisc_ops->update)
250 		dev->ndisc_ops->update(dev, n, flags, icmp6_type, ndopts);
251 }
252 
ndisc_ops_opt_addr_space(const struct net_device * dev,u8 icmp6_type)253 static inline int ndisc_ops_opt_addr_space(const struct net_device *dev,
254 					   u8 icmp6_type)
255 {
256 	if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space &&
257 	    icmp6_type != NDISC_REDIRECT)
258 		return dev->ndisc_ops->opt_addr_space(dev, icmp6_type, NULL,
259 						      NULL, NULL);
260 	else
261 		return 0;
262 }
263 
ndisc_ops_redirect_opt_addr_space(const struct net_device * dev,struct neighbour * neigh,u8 * ha_buf,u8 ** ha)264 static inline int ndisc_ops_redirect_opt_addr_space(const struct net_device *dev,
265 						    struct neighbour *neigh,
266 						    u8 *ha_buf, u8 **ha)
267 {
268 	if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space)
269 		return dev->ndisc_ops->opt_addr_space(dev, NDISC_REDIRECT,
270 						      neigh, ha_buf, ha);
271 	else
272 		return 0;
273 }
274 
ndisc_ops_fill_addr_option(const struct net_device * dev,struct sk_buff * skb,u8 icmp6_type)275 static inline void ndisc_ops_fill_addr_option(const struct net_device *dev,
276 					      struct sk_buff *skb,
277 					      u8 icmp6_type)
278 {
279 	if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option &&
280 	    icmp6_type != NDISC_REDIRECT)
281 		dev->ndisc_ops->fill_addr_option(dev, skb, icmp6_type, NULL);
282 }
283 
ndisc_ops_fill_redirect_addr_option(const struct net_device * dev,struct sk_buff * skb,const u8 * ha)284 static inline void ndisc_ops_fill_redirect_addr_option(const struct net_device *dev,
285 						       struct sk_buff *skb,
286 						       const u8 *ha)
287 {
288 	if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option)
289 		dev->ndisc_ops->fill_addr_option(dev, skb, NDISC_REDIRECT, ha);
290 }
291 
ndisc_ops_prefix_rcv_add_addr(struct net * net,struct net_device * dev,const struct prefix_info * pinfo,struct inet6_dev * in6_dev,struct in6_addr * addr,int addr_type,u32 addr_flags,bool sllao,bool tokenized,__u32 valid_lft,u32 prefered_lft,bool dev_addr_generated)292 static inline void ndisc_ops_prefix_rcv_add_addr(struct net *net,
293 						 struct net_device *dev,
294 						 const struct prefix_info *pinfo,
295 						 struct inet6_dev *in6_dev,
296 						 struct in6_addr *addr,
297 						 int addr_type, u32 addr_flags,
298 						 bool sllao, bool tokenized,
299 						 __u32 valid_lft,
300 						 u32 prefered_lft,
301 						 bool dev_addr_generated)
302 {
303 	if (dev->ndisc_ops && dev->ndisc_ops->prefix_rcv_add_addr)
304 		dev->ndisc_ops->prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
305 						    addr, addr_type,
306 						    addr_flags, sllao,
307 						    tokenized, valid_lft,
308 						    prefered_lft,
309 						    dev_addr_generated);
310 }
311 #endif
312 
313 /*
314  * Return the padding between the option length and the start of the
315  * link addr.  Currently only IP-over-InfiniBand needs this, although
316  * if RFC 3831 IPv6-over-Fibre Channel is ever implemented it may
317  * also need a pad of 2.
318  */
ndisc_addr_option_pad(unsigned short type)319 static inline int ndisc_addr_option_pad(unsigned short type)
320 {
321 	switch (type) {
322 	case ARPHRD_INFINIBAND: return 2;
323 	default:                return 0;
324 	}
325 }
326 
__ndisc_opt_addr_space(unsigned char addr_len,int pad)327 static inline int __ndisc_opt_addr_space(unsigned char addr_len, int pad)
328 {
329 	return NDISC_OPT_SPACE(addr_len + pad);
330 }
331 
332 #if IS_ENABLED(CONFIG_IPV6)
ndisc_opt_addr_space(struct net_device * dev,u8 icmp6_type)333 static inline int ndisc_opt_addr_space(struct net_device *dev, u8 icmp6_type)
334 {
335 	return __ndisc_opt_addr_space(dev->addr_len,
336 				      ndisc_addr_option_pad(dev->type)) +
337 		ndisc_ops_opt_addr_space(dev, icmp6_type);
338 }
339 
ndisc_redirect_opt_addr_space(struct net_device * dev,struct neighbour * neigh,u8 * ops_data_buf,u8 ** ops_data)340 static inline int ndisc_redirect_opt_addr_space(struct net_device *dev,
341 						struct neighbour *neigh,
342 						u8 *ops_data_buf,
343 						u8 **ops_data)
344 {
345 	return __ndisc_opt_addr_space(dev->addr_len,
346 				      ndisc_addr_option_pad(dev->type)) +
347 		ndisc_ops_redirect_opt_addr_space(dev, neigh, ops_data_buf,
348 						  ops_data);
349 }
350 #endif
351 
__ndisc_opt_addr_data(struct nd_opt_hdr * p,unsigned char addr_len,int prepad)352 static inline u8 *__ndisc_opt_addr_data(struct nd_opt_hdr *p,
353 					unsigned char addr_len, int prepad)
354 {
355 	u8 *lladdr = (u8 *)(p + 1);
356 	int lladdrlen = p->nd_opt_len << 3;
357 	if (lladdrlen != __ndisc_opt_addr_space(addr_len, prepad))
358 		return NULL;
359 	return lladdr + prepad;
360 }
361 
ndisc_opt_addr_data(struct nd_opt_hdr * p,struct net_device * dev)362 static inline u8 *ndisc_opt_addr_data(struct nd_opt_hdr *p,
363 				      struct net_device *dev)
364 {
365 	return __ndisc_opt_addr_data(p, dev->addr_len,
366 				     ndisc_addr_option_pad(dev->type));
367 }
368 
ndisc_hashfn(const void * pkey,const struct net_device * dev,__u32 * hash_rnd)369 static inline u32 ndisc_hashfn(const void *pkey, const struct net_device *dev, __u32 *hash_rnd)
370 {
371 	const u32 *p32 = pkey;
372 
373 	return (((p32[0] ^ hash32_ptr(dev)) * hash_rnd[0]) +
374 		(p32[1] * hash_rnd[1]) +
375 		(p32[2] * hash_rnd[2]) +
376 		(p32[3] * hash_rnd[3]));
377 }
378 
__ipv6_neigh_lookup_noref(struct net_device * dev,const void * pkey)379 static inline struct neighbour *__ipv6_neigh_lookup_noref(struct net_device *dev, const void *pkey)
380 {
381 	return ___neigh_lookup_noref(&nd_tbl, neigh_key_eq128, ndisc_hashfn, pkey, dev);
382 }
383 
__ipv6_neigh_lookup(struct net_device * dev,const void * pkey)384 static inline struct neighbour *__ipv6_neigh_lookup(struct net_device *dev, const void *pkey)
385 {
386 	struct neighbour *n;
387 
388 	rcu_read_lock_bh();
389 	n = __ipv6_neigh_lookup_noref(dev, pkey);
390 	if (n && !refcount_inc_not_zero(&n->refcnt))
391 		n = NULL;
392 	rcu_read_unlock_bh();
393 
394 	return n;
395 }
396 
__ipv6_confirm_neigh(struct net_device * dev,const void * pkey)397 static inline void __ipv6_confirm_neigh(struct net_device *dev,
398 					const void *pkey)
399 {
400 	struct neighbour *n;
401 
402 	rcu_read_lock_bh();
403 	n = __ipv6_neigh_lookup_noref(dev, pkey);
404 	if (n) {
405 		unsigned long now = jiffies;
406 
407 		/* avoid dirtying neighbour */
408 		if (n->confirmed != now)
409 			n->confirmed = now;
410 	}
411 	rcu_read_unlock_bh();
412 }
413 
414 int ndisc_init(void);
415 int ndisc_late_init(void);
416 
417 void ndisc_late_cleanup(void);
418 void ndisc_cleanup(void);
419 
420 int ndisc_rcv(struct sk_buff *skb);
421 
422 void ndisc_send_ns(struct net_device *dev, const struct in6_addr *solicit,
423 		   const struct in6_addr *daddr, const struct in6_addr *saddr,
424 		   u64 nonce);
425 
426 void ndisc_send_rs(struct net_device *dev,
427 		   const struct in6_addr *saddr, const struct in6_addr *daddr);
428 void ndisc_send_na(struct net_device *dev, const struct in6_addr *daddr,
429 		   const struct in6_addr *solicited_addr,
430 		   bool router, bool solicited, bool override, bool inc_opt);
431 
432 void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target);
433 
434 int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev,
435 		 int dir);
436 
437 void ndisc_update(const struct net_device *dev, struct neighbour *neigh,
438 		  const u8 *lladdr, u8 new, u32 flags, u8 icmp6_type,
439 		  struct ndisc_options *ndopts);
440 
441 /*
442  *	IGMP
443  */
444 int igmp6_init(void);
445 int igmp6_late_init(void);
446 
447 void igmp6_cleanup(void);
448 void igmp6_late_cleanup(void);
449 
450 int igmp6_event_query(struct sk_buff *skb);
451 
452 int igmp6_event_report(struct sk_buff *skb);
453 
454 
455 #ifdef CONFIG_SYSCTL
456 int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write,
457 			       void __user *buffer, size_t *lenp, loff_t *ppos);
458 int ndisc_ifinfo_sysctl_strategy(struct ctl_table *ctl,
459 				 void __user *oldval, size_t __user *oldlenp,
460 				 void __user *newval, size_t newlen);
461 #endif
462 
463 void inet6_ifinfo_notify(int event, struct inet6_dev *idev);
464 
465 #endif
466