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