blob: 9cefd9421dc68fad6366f5a1eff38f37e1f2052c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _NDISC_H
2#define _NDISC_H
3
4/*
5 * ICMP codes for neighbour discovery messages
6 */
7
8#define NDISC_ROUTER_SOLICITATION 133
9#define NDISC_ROUTER_ADVERTISEMENT 134
10#define NDISC_NEIGHBOUR_SOLICITATION 135
11#define NDISC_NEIGHBOUR_ADVERTISEMENT 136
12#define NDISC_REDIRECT 137
13
14/*
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -040015 * Router type: cross-layer information from link-layer to
16 * IPv6 layer reported by certain link types (e.g., RFC4214).
17 */
18#define NDISC_NODETYPE_UNSPEC 0 /* unspecified (default) */
19#define NDISC_NODETYPE_HOST 1 /* host or unauthorized router */
20#define NDISC_NODETYPE_NODEFAULT 2 /* non-default router */
21#define NDISC_NODETYPE_DEFAULT 3 /* default router */
22
23/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * ndisc options
25 */
26
27enum {
28 __ND_OPT_PREFIX_INFO_END = 0,
29 ND_OPT_SOURCE_LL_ADDR = 1, /* RFC2461 */
30 ND_OPT_TARGET_LL_ADDR = 2, /* RFC2461 */
31 ND_OPT_PREFIX_INFO = 3, /* RFC2461 */
32 ND_OPT_REDIRECT_HDR = 4, /* RFC2461 */
33 ND_OPT_MTU = 5, /* RFC2461 */
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -080034 __ND_OPT_ARRAY_MAX,
35 ND_OPT_ROUTE_INFO = 24, /* RFC4191 */
Pierre Ynard31910572007-10-10 21:22:05 -070036 ND_OPT_RDNSS = 25, /* RFC5006 */
Alexey I. Froloffe35f30c2012-04-06 05:50:58 +000037 ND_OPT_DNSSL = 31, /* RFC6106 */
Alexander Aringbbe5f5c2016-06-15 21:20:25 +020038 ND_OPT_6CO = 34, /* RFC6775 */
Maciej Żenczykowski712595f2019-07-18 23:30:03 -070039 ND_OPT_CAPTIVE_PORTAL = 37, /* RFC7710 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 __ND_OPT_MAX
41};
42
43#define MAX_RTR_SOLICITATION_DELAY HZ
44
45#define ND_REACHABLE_TIME (30*HZ)
46#define ND_RETRANS_TIMER HZ
47
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020048#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/icmpv6.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020050#include <linux/in6.h>
51#include <linux/types.h>
David S. Miller30f2a5f2012-07-11 23:26:46 -070052#include <linux/if_arp.h>
53#include <linux/netdevice.h>
Pavel Emelyanovb14f2432012-08-08 21:52:28 +000054#include <linux/hash.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/neighbour.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020057
Alexander Aringcc84b3c2016-06-15 21:20:24 +020058/* Set to 3 to get tracing... */
59#define ND_DEBUG 1
60
61#define ND_PRINTK(val, level, fmt, ...) \
62do { \
63 if (val <= ND_DEBUG) \
64 net_##level##_ratelimited(fmt, ##__VA_ARGS__); \
65} while (0)
66
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020067struct ctl_table;
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020068struct inet6_dev;
69struct net_device;
70struct net_proto_family;
71struct sk_buff;
Alexander Aringf997c552016-06-15 21:20:23 +020072struct prefix_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74extern struct neigh_table nd_tbl;
75
76struct nd_msg {
77 struct icmp6hdr icmph;
78 struct in6_addr target;
79 __u8 opt[0];
80};
81
82struct rs_msg {
83 struct icmp6hdr icmph;
84 __u8 opt[0];
85};
86
87struct ra_msg {
88 struct icmp6hdr icmph;
Al Viroe69a4adc2006-11-14 20:56:00 -080089 __be32 reachable_time;
90 __be32 retrans_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
Duan Jiong093d04d2012-12-14 02:59:59 +000093struct rd_msg {
94 struct icmp6hdr icmph;
95 struct in6_addr target;
96 struct in6_addr dest;
97 __u8 opt[0];
98};
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100struct nd_opt_hdr {
101 __u8 nd_opt_type;
102 __u8 nd_opt_len;
Eric Dumazetbc105022010-06-03 03:21:52 -0700103} __packed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
David S. Miller30f2a5f2012-07-11 23:26:46 -0700105/* ND options */
106struct ndisc_options {
107 struct nd_opt_hdr *nd_opt_array[__ND_OPT_ARRAY_MAX];
108#ifdef CONFIG_IPV6_ROUTE_INFO
109 struct nd_opt_hdr *nd_opts_ri;
110 struct nd_opt_hdr *nd_opts_ri_end;
111#endif
112 struct nd_opt_hdr *nd_useropts;
113 struct nd_opt_hdr *nd_useropts_end;
Alexander Aringbbe5f5c2016-06-15 21:20:25 +0200114#if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
115 struct nd_opt_hdr *nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR + 1];
116#endif
David S. Miller30f2a5f2012-07-11 23:26:46 -0700117};
118
Alexander Aringbbe5f5c2016-06-15 21:20:25 +0200119#define nd_opts_src_lladdr nd_opt_array[ND_OPT_SOURCE_LL_ADDR]
120#define nd_opts_tgt_lladdr nd_opt_array[ND_OPT_TARGET_LL_ADDR]
121#define nd_opts_pi nd_opt_array[ND_OPT_PREFIX_INFO]
122#define nd_opts_pi_end nd_opt_array[__ND_OPT_PREFIX_INFO_END]
123#define nd_opts_rh nd_opt_array[ND_OPT_REDIRECT_HDR]
124#define nd_opts_mtu nd_opt_array[ND_OPT_MTU]
125#define nd_802154_opts_src_lladdr nd_802154_opt_array[ND_OPT_SOURCE_LL_ADDR]
126#define nd_802154_opts_tgt_lladdr nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR]
David S. Miller30f2a5f2012-07-11 23:26:46 -0700127
128#define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
129
Alexander Aringf997c552016-06-15 21:20:23 +0200130struct ndisc_options *ndisc_parse_options(const struct net_device *dev,
131 u8 *opt, int opt_len,
Joe Perches3cc818a2013-09-21 10:22:47 -0700132 struct ndisc_options *ndopts);
David S. Miller30f2a5f2012-07-11 23:26:46 -0700133
Alexander Aringcc84b3c2016-06-15 21:20:24 +0200134void __ndisc_fill_addr_option(struct sk_buff *skb, int type, void *data,
135 int data_len, int pad);
136
Alexander Aringf997c552016-06-15 21:20:23 +0200137#define NDISC_OPS_REDIRECT_DATA_SPACE 2
138
139/*
140 * This structure defines the hooks for IPv6 neighbour discovery.
141 * The following hooks can be defined; unless noted otherwise, they are
142 * optional and can be filled with a null pointer.
143 *
144 * int (*is_useropt)(u8 nd_opt_type):
145 * This function is called when IPv6 decide RA userspace options. if
146 * this function returns 1 then the option given by nd_opt_type will
147 * be handled as userspace option additional to the IPv6 options.
148 *
149 * int (*parse_options)(const struct net_device *dev,
150 * struct nd_opt_hdr *nd_opt,
151 * struct ndisc_options *ndopts):
152 * This function is called while parsing ndisc ops and put each position
153 * as pointer into ndopts. If this function return unequal 0, then this
154 * function took care about the ndisc option, if 0 then the IPv6 ndisc
155 * option parser will take care about that option.
156 *
157 * void (*update)(const struct net_device *dev, struct neighbour *n,
158 * u32 flags, u8 icmp6_type,
159 * const struct ndisc_options *ndopts):
160 * This function is called when IPv6 ndisc updates the neighbour cache
161 * entry. Additional options which can be updated may be previously
162 * parsed by parse_opts callback and accessible over ndopts parameter.
163 *
164 * int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
165 * struct neighbour *neigh, u8 *ha_buf,
166 * u8 **ha):
167 * This function is called when the necessary option space will be
168 * calculated before allocating a skb. The parameters neigh, ha_buf
169 * abd ha are available on NDISC_REDIRECT messages only.
170 *
171 * void (*fill_addr_option)(const struct net_device *dev,
172 * struct sk_buff *skb, u8 icmp6_type,
173 * const u8 *ha):
174 * This function is called when the skb will finally fill the option
175 * fields inside skb. NOTE: this callback should fill the option
176 * fields to the skb which are previously indicated by opt_space
177 * parameter. That means the decision to add such option should
178 * not lost between these two callbacks, e.g. protected by interface
179 * up state.
180 *
181 * void (*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
182 * const struct prefix_info *pinfo,
183 * struct inet6_dev *in6_dev,
184 * struct in6_addr *addr,
185 * int addr_type, u32 addr_flags,
186 * bool sllao, bool tokenized,
187 * __u32 valid_lft, u32 prefered_lft,
188 * bool dev_addr_generated):
189 * This function is called when a RA messages is received with valid
190 * PIO option fields and an IPv6 address will be added to the interface
191 * for autoconfiguration. The parameter dev_addr_generated reports about
192 * if the address was based on dev->dev_addr or not. This can be used
193 * to add a second address if link-layer operates with two link layer
194 * addresses. E.g. 802.15.4 6LoWPAN.
195 */
196struct ndisc_ops {
197 int (*is_useropt)(u8 nd_opt_type);
198 int (*parse_options)(const struct net_device *dev,
199 struct nd_opt_hdr *nd_opt,
200 struct ndisc_options *ndopts);
201 void (*update)(const struct net_device *dev, struct neighbour *n,
202 u32 flags, u8 icmp6_type,
203 const struct ndisc_options *ndopts);
204 int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
205 struct neighbour *neigh, u8 *ha_buf,
206 u8 **ha);
207 void (*fill_addr_option)(const struct net_device *dev,
208 struct sk_buff *skb, u8 icmp6_type,
209 const u8 *ha);
210 void (*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
211 const struct prefix_info *pinfo,
212 struct inet6_dev *in6_dev,
213 struct in6_addr *addr,
214 int addr_type, u32 addr_flags,
215 bool sllao, bool tokenized,
216 __u32 valid_lft, u32 prefered_lft,
217 bool dev_addr_generated);
218};
219
220#if IS_ENABLED(CONFIG_IPV6)
221static inline int ndisc_ops_is_useropt(const struct net_device *dev,
222 u8 nd_opt_type)
223{
224 if (dev->ndisc_ops && dev->ndisc_ops->is_useropt)
225 return dev->ndisc_ops->is_useropt(nd_opt_type);
226 else
227 return 0;
228}
229
230static inline int ndisc_ops_parse_options(const struct net_device *dev,
231 struct nd_opt_hdr *nd_opt,
232 struct ndisc_options *ndopts)
233{
234 if (dev->ndisc_ops && dev->ndisc_ops->parse_options)
235 return dev->ndisc_ops->parse_options(dev, nd_opt, ndopts);
236 else
237 return 0;
238}
239
240static inline void ndisc_ops_update(const struct net_device *dev,
241 struct neighbour *n, u32 flags,
242 u8 icmp6_type,
243 const struct ndisc_options *ndopts)
244{
245 if (dev->ndisc_ops && dev->ndisc_ops->update)
246 dev->ndisc_ops->update(dev, n, flags, icmp6_type, ndopts);
247}
248
249static inline int ndisc_ops_opt_addr_space(const struct net_device *dev,
250 u8 icmp6_type)
251{
252 if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space &&
253 icmp6_type != NDISC_REDIRECT)
254 return dev->ndisc_ops->opt_addr_space(dev, icmp6_type, NULL,
255 NULL, NULL);
256 else
257 return 0;
258}
259
260static inline int ndisc_ops_redirect_opt_addr_space(const struct net_device *dev,
261 struct neighbour *neigh,
262 u8 *ha_buf, u8 **ha)
263{
264 if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space)
265 return dev->ndisc_ops->opt_addr_space(dev, NDISC_REDIRECT,
266 neigh, ha_buf, ha);
267 else
268 return 0;
269}
270
271static inline void ndisc_ops_fill_addr_option(const struct net_device *dev,
272 struct sk_buff *skb,
273 u8 icmp6_type)
274{
275 if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option &&
276 icmp6_type != NDISC_REDIRECT)
277 dev->ndisc_ops->fill_addr_option(dev, skb, icmp6_type, NULL);
278}
279
280static inline void ndisc_ops_fill_redirect_addr_option(const struct net_device *dev,
281 struct sk_buff *skb,
282 const u8 *ha)
283{
284 if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option)
285 dev->ndisc_ops->fill_addr_option(dev, skb, NDISC_REDIRECT, ha);
286}
287
288static inline void ndisc_ops_prefix_rcv_add_addr(struct net *net,
289 struct net_device *dev,
290 const struct prefix_info *pinfo,
291 struct inet6_dev *in6_dev,
292 struct in6_addr *addr,
293 int addr_type, u32 addr_flags,
294 bool sllao, bool tokenized,
295 __u32 valid_lft,
296 u32 prefered_lft,
297 bool dev_addr_generated)
298{
299 if (dev->ndisc_ops && dev->ndisc_ops->prefix_rcv_add_addr)
300 dev->ndisc_ops->prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
301 addr, addr_type,
302 addr_flags, sllao,
303 tokenized, valid_lft,
304 prefered_lft,
305 dev_addr_generated);
306}
307#endif
308
David S. Miller30f2a5f2012-07-11 23:26:46 -0700309/*
310 * Return the padding between the option length and the start of the
311 * link addr. Currently only IP-over-InfiniBand needs this, although
312 * if RFC 3831 IPv6-over-Fibre Channel is ever implemented it may
313 * also need a pad of 2.
314 */
Joe Perchesd9d10a32013-07-30 10:31:00 -0700315static inline int ndisc_addr_option_pad(unsigned short type)
David S. Miller30f2a5f2012-07-11 23:26:46 -0700316{
317 switch (type) {
318 case ARPHRD_INFINIBAND: return 2;
319 default: return 0;
320 }
321}
322
Alexander Aring1e82f962016-06-15 21:20:19 +0200323static inline int __ndisc_opt_addr_space(unsigned char addr_len, int pad)
324{
325 return NDISC_OPT_SPACE(addr_len + pad);
326}
327
Alexander Aringf997c552016-06-15 21:20:23 +0200328#if IS_ENABLED(CONFIG_IPV6)
329static inline int ndisc_opt_addr_space(struct net_device *dev, u8 icmp6_type)
YOSHIFUJI Hideaki / 吉藤英明c558e9f2013-01-21 06:47:56 +0000330{
Alexander Aring1e82f962016-06-15 21:20:19 +0200331 return __ndisc_opt_addr_space(dev->addr_len,
Alexander Aringf997c552016-06-15 21:20:23 +0200332 ndisc_addr_option_pad(dev->type)) +
333 ndisc_ops_opt_addr_space(dev, icmp6_type);
YOSHIFUJI Hideaki / 吉藤英明c558e9f2013-01-21 06:47:56 +0000334}
335
Alexander Aringf997c552016-06-15 21:20:23 +0200336static inline int ndisc_redirect_opt_addr_space(struct net_device *dev,
337 struct neighbour *neigh,
338 u8 *ops_data_buf,
339 u8 **ops_data)
340{
341 return __ndisc_opt_addr_space(dev->addr_len,
342 ndisc_addr_option_pad(dev->type)) +
343 ndisc_ops_redirect_opt_addr_space(dev, neigh, ops_data_buf,
344 ops_data);
345}
346#endif
347
Alexander Aring4f36ce82016-06-15 21:20:20 +0200348static inline u8 *__ndisc_opt_addr_data(struct nd_opt_hdr *p,
349 unsigned char addr_len, int prepad)
David S. Miller30f2a5f2012-07-11 23:26:46 -0700350{
351 u8 *lladdr = (u8 *)(p + 1);
352 int lladdrlen = p->nd_opt_len << 3;
Alexander Aring4f36ce82016-06-15 21:20:20 +0200353 if (lladdrlen != __ndisc_opt_addr_space(addr_len, prepad))
David S. Miller30f2a5f2012-07-11 23:26:46 -0700354 return NULL;
355 return lladdr + prepad;
356}
357
Alexander Aring4f36ce82016-06-15 21:20:20 +0200358static inline u8 *ndisc_opt_addr_data(struct nd_opt_hdr *p,
359 struct net_device *dev)
360{
361 return __ndisc_opt_addr_data(p, dev->addr_len,
362 ndisc_addr_option_pad(dev->type));
363}
364
David S. Miller2c2aba62011-12-28 15:06:58 -0500365static inline u32 ndisc_hashfn(const void *pkey, const struct net_device *dev, __u32 *hash_rnd)
366{
367 const u32 *p32 = pkey;
368
Pavel Emelyanovb14f2432012-08-08 21:52:28 +0000369 return (((p32[0] ^ hash32_ptr(dev)) * hash_rnd[0]) +
David S. Miller2c2aba62011-12-28 15:06:58 -0500370 (p32[1] * hash_rnd[1]) +
371 (p32[2] * hash_rnd[2]) +
372 (p32[3] * hash_rnd[3]));
373}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
YOSHIFUJI Hideaki / 吉藤英明ac3175f2013-01-17 12:53:22 +0000375static inline struct neighbour *__ipv6_neigh_lookup_noref(struct net_device *dev, const void *pkey)
David S. Millerf83c7792011-12-28 15:41:23 -0500376{
Eric W. Biederman60395a22015-03-03 17:10:44 -0600377 return ___neigh_lookup_noref(&nd_tbl, neigh_key_eq128, ndisc_hashfn, pkey, dev);
YOSHIFUJI Hideaki / 吉藤英明ac3175f2013-01-17 12:53:22 +0000378}
379
380static inline struct neighbour *__ipv6_neigh_lookup(struct net_device *dev, const void *pkey)
381{
382 struct neighbour *n;
383
384 rcu_read_lock_bh();
385 n = __ipv6_neigh_lookup_noref(dev, pkey);
386 if (n && !atomic_inc_not_zero(&n->refcnt))
387 n = NULL;
David S. Millerf83c7792011-12-28 15:41:23 -0500388 rcu_read_unlock_bh();
389
390 return n;
391}
392
Joe Perches3cc818a2013-09-21 10:22:47 -0700393int ndisc_init(void);
394int ndisc_late_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Joe Perches3cc818a2013-09-21 10:22:47 -0700396void ndisc_late_cleanup(void);
397void ndisc_cleanup(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Joe Perches3cc818a2013-09-21 10:22:47 -0700399int ndisc_rcv(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Jiri Benc38cf5952015-09-22 18:57:13 +0200401void ndisc_send_ns(struct net_device *dev, const struct in6_addr *solicit,
Nicolas Dichtel304d8882015-11-27 18:17:05 +0100402 const struct in6_addr *daddr, const struct in6_addr *saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Joe Perches3cc818a2013-09-21 10:22:47 -0700404void ndisc_send_rs(struct net_device *dev,
405 const struct in6_addr *saddr, const struct in6_addr *daddr);
Jiri Benc38cf5952015-09-22 18:57:13 +0200406void ndisc_send_na(struct net_device *dev, const struct in6_addr *daddr,
Joe Perches3cc818a2013-09-21 10:22:47 -0700407 const struct in6_addr *solicited_addr,
408 bool router, bool solicited, bool override, bool inc_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Joe Perches3cc818a2013-09-21 10:22:47 -0700410void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Joe Perches3cc818a2013-09-21 10:22:47 -0700412int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev,
413 int dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Alexander Aringf997c552016-06-15 21:20:23 +0200415void ndisc_update(const struct net_device *dev, struct neighbour *neigh,
416 const u8 *lladdr, u8 new, u32 flags, u8 icmp6_type,
417 struct ndisc_options *ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419/*
420 * IGMP
421 */
Joe Perches3cc818a2013-09-21 10:22:47 -0700422int igmp6_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Joe Perches3cc818a2013-09-21 10:22:47 -0700424void igmp6_cleanup(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Joe Perches3cc818a2013-09-21 10:22:47 -0700426int igmp6_event_query(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Joe Perches3cc818a2013-09-21 10:22:47 -0700428int igmp6_event_report(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431#ifdef CONFIG_SYSCTL
Joe Perches3cc818a2013-09-21 10:22:47 -0700432int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write,
433 void __user *buffer, size_t *lenp, loff_t *ppos);
Joe Perchesfedaf4f2013-06-13 19:37:54 -0700434int ndisc_ifinfo_sysctl_strategy(struct ctl_table *ctl,
Alexey Dobriyanf221e722008-10-15 22:04:23 -0700435 void __user *oldval, size_t __user *oldlenp,
YOSHIFUJI Hideaki0686caa2008-05-19 16:25:42 -0700436 void __user *newval, size_t newlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437#endif
438
Joe Perches3cc818a2013-09-21 10:22:47 -0700439void inet6_ifinfo_notify(int event, struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441#endif