blob: be1fe2283254449b7e7183ea6b5d4d209cdcf55a [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 __ND_OPT_MAX
40};
41
42#define MAX_RTR_SOLICITATION_DELAY HZ
43
44#define ND_REACHABLE_TIME (30*HZ)
45#define ND_RETRANS_TIMER HZ
46
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020047#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/icmpv6.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020049#include <linux/in6.h>
50#include <linux/types.h>
David S. Miller30f2a5f2012-07-11 23:26:46 -070051#include <linux/if_arp.h>
52#include <linux/netdevice.h>
Pavel Emelyanovb14f2432012-08-08 21:52:28 +000053#include <linux/hash.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/neighbour.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020056
Alexander Aringcc84b3c2016-06-15 21:20:24 +020057/* Set to 3 to get tracing... */
58#define ND_DEBUG 1
59
60#define ND_PRINTK(val, level, fmt, ...) \
61do { \
62 if (val <= ND_DEBUG) \
63 net_##level##_ratelimited(fmt, ##__VA_ARGS__); \
64} while (0)
65
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020066struct ctl_table;
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020067struct inet6_dev;
68struct net_device;
69struct net_proto_family;
70struct sk_buff;
Alexander Aringf997c552016-06-15 21:20:23 +020071struct prefix_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73extern struct neigh_table nd_tbl;
74
75struct nd_msg {
76 struct icmp6hdr icmph;
77 struct in6_addr target;
78 __u8 opt[0];
79};
80
81struct rs_msg {
82 struct icmp6hdr icmph;
83 __u8 opt[0];
84};
85
86struct ra_msg {
87 struct icmp6hdr icmph;
Al Viroe69a4adc2006-11-14 20:56:00 -080088 __be32 reachable_time;
89 __be32 retrans_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
Duan Jiong093d04d2012-12-14 02:59:59 +000092struct rd_msg {
93 struct icmp6hdr icmph;
94 struct in6_addr target;
95 struct in6_addr dest;
96 __u8 opt[0];
97};
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099struct nd_opt_hdr {
100 __u8 nd_opt_type;
101 __u8 nd_opt_len;
Eric Dumazetbc105022010-06-03 03:21:52 -0700102} __packed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
David S. Miller30f2a5f2012-07-11 23:26:46 -0700104/* ND options */
105struct ndisc_options {
106 struct nd_opt_hdr *nd_opt_array[__ND_OPT_ARRAY_MAX];
107#ifdef CONFIG_IPV6_ROUTE_INFO
108 struct nd_opt_hdr *nd_opts_ri;
109 struct nd_opt_hdr *nd_opts_ri_end;
110#endif
111 struct nd_opt_hdr *nd_useropts;
112 struct nd_opt_hdr *nd_useropts_end;
Alexander Aringbbe5f5c2016-06-15 21:20:25 +0200113#if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
114 struct nd_opt_hdr *nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR + 1];
115#endif
David S. Miller30f2a5f2012-07-11 23:26:46 -0700116};
117
Alexander Aringbbe5f5c2016-06-15 21:20:25 +0200118#define nd_opts_src_lladdr nd_opt_array[ND_OPT_SOURCE_LL_ADDR]
119#define nd_opts_tgt_lladdr nd_opt_array[ND_OPT_TARGET_LL_ADDR]
120#define nd_opts_pi nd_opt_array[ND_OPT_PREFIX_INFO]
121#define nd_opts_pi_end nd_opt_array[__ND_OPT_PREFIX_INFO_END]
122#define nd_opts_rh nd_opt_array[ND_OPT_REDIRECT_HDR]
123#define nd_opts_mtu nd_opt_array[ND_OPT_MTU]
124#define nd_802154_opts_src_lladdr nd_802154_opt_array[ND_OPT_SOURCE_LL_ADDR]
125#define nd_802154_opts_tgt_lladdr nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR]
David S. Miller30f2a5f2012-07-11 23:26:46 -0700126
127#define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
128
Alexander Aringf997c552016-06-15 21:20:23 +0200129struct ndisc_options *ndisc_parse_options(const struct net_device *dev,
130 u8 *opt, int opt_len,
Joe Perches3cc818a2013-09-21 10:22:47 -0700131 struct ndisc_options *ndopts);
David S. Miller30f2a5f2012-07-11 23:26:46 -0700132
Alexander Aringcc84b3c2016-06-15 21:20:24 +0200133void __ndisc_fill_addr_option(struct sk_buff *skb, int type, void *data,
134 int data_len, int pad);
135
Alexander Aringf997c552016-06-15 21:20:23 +0200136#define NDISC_OPS_REDIRECT_DATA_SPACE 2
137
138/*
139 * This structure defines the hooks for IPv6 neighbour discovery.
140 * The following hooks can be defined; unless noted otherwise, they are
141 * optional and can be filled with a null pointer.
142 *
143 * int (*is_useropt)(u8 nd_opt_type):
144 * This function is called when IPv6 decide RA userspace options. if
145 * this function returns 1 then the option given by nd_opt_type will
146 * be handled as userspace option additional to the IPv6 options.
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 */
195struct ndisc_ops {
196 int (*is_useropt)(u8 nd_opt_type);
197 int (*parse_options)(const struct net_device *dev,
198 struct nd_opt_hdr *nd_opt,
199 struct ndisc_options *ndopts);
200 void (*update)(const struct net_device *dev, struct neighbour *n,
201 u32 flags, u8 icmp6_type,
202 const struct ndisc_options *ndopts);
203 int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
204 struct neighbour *neigh, u8 *ha_buf,
205 u8 **ha);
206 void (*fill_addr_option)(const struct net_device *dev,
207 struct sk_buff *skb, u8 icmp6_type,
208 const u8 *ha);
209 void (*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
210 const struct prefix_info *pinfo,
211 struct inet6_dev *in6_dev,
212 struct in6_addr *addr,
213 int addr_type, u32 addr_flags,
214 bool sllao, bool tokenized,
215 __u32 valid_lft, u32 prefered_lft,
216 bool dev_addr_generated);
217};
218
219#if IS_ENABLED(CONFIG_IPV6)
220static inline int ndisc_ops_is_useropt(const struct net_device *dev,
221 u8 nd_opt_type)
222{
223 if (dev->ndisc_ops && dev->ndisc_ops->is_useropt)
224 return dev->ndisc_ops->is_useropt(nd_opt_type);
225 else
226 return 0;
227}
228
229static inline int ndisc_ops_parse_options(const struct net_device *dev,
230 struct nd_opt_hdr *nd_opt,
231 struct ndisc_options *ndopts)
232{
233 if (dev->ndisc_ops && dev->ndisc_ops->parse_options)
234 return dev->ndisc_ops->parse_options(dev, nd_opt, ndopts);
235 else
236 return 0;
237}
238
239static inline void ndisc_ops_update(const struct net_device *dev,
240 struct neighbour *n, u32 flags,
241 u8 icmp6_type,
242 const struct ndisc_options *ndopts)
243{
244 if (dev->ndisc_ops && dev->ndisc_ops->update)
245 dev->ndisc_ops->update(dev, n, flags, icmp6_type, ndopts);
246}
247
248static inline int ndisc_ops_opt_addr_space(const struct net_device *dev,
249 u8 icmp6_type)
250{
251 if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space &&
252 icmp6_type != NDISC_REDIRECT)
253 return dev->ndisc_ops->opt_addr_space(dev, icmp6_type, NULL,
254 NULL, NULL);
255 else
256 return 0;
257}
258
259static inline int ndisc_ops_redirect_opt_addr_space(const struct net_device *dev,
260 struct neighbour *neigh,
261 u8 *ha_buf, u8 **ha)
262{
263 if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space)
264 return dev->ndisc_ops->opt_addr_space(dev, NDISC_REDIRECT,
265 neigh, ha_buf, ha);
266 else
267 return 0;
268}
269
270static inline void ndisc_ops_fill_addr_option(const struct net_device *dev,
271 struct sk_buff *skb,
272 u8 icmp6_type)
273{
274 if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option &&
275 icmp6_type != NDISC_REDIRECT)
276 dev->ndisc_ops->fill_addr_option(dev, skb, icmp6_type, NULL);
277}
278
279static inline void ndisc_ops_fill_redirect_addr_option(const struct net_device *dev,
280 struct sk_buff *skb,
281 const u8 *ha)
282{
283 if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option)
284 dev->ndisc_ops->fill_addr_option(dev, skb, NDISC_REDIRECT, ha);
285}
286
287static inline void ndisc_ops_prefix_rcv_add_addr(struct net *net,
288 struct net_device *dev,
289 const struct prefix_info *pinfo,
290 struct inet6_dev *in6_dev,
291 struct in6_addr *addr,
292 int addr_type, u32 addr_flags,
293 bool sllao, bool tokenized,
294 __u32 valid_lft,
295 u32 prefered_lft,
296 bool dev_addr_generated)
297{
298 if (dev->ndisc_ops && dev->ndisc_ops->prefix_rcv_add_addr)
299 dev->ndisc_ops->prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
300 addr, addr_type,
301 addr_flags, sllao,
302 tokenized, valid_lft,
303 prefered_lft,
304 dev_addr_generated);
305}
306#endif
307
David S. Miller30f2a5f2012-07-11 23:26:46 -0700308/*
309 * Return the padding between the option length and the start of the
310 * link addr. Currently only IP-over-InfiniBand needs this, although
311 * if RFC 3831 IPv6-over-Fibre Channel is ever implemented it may
312 * also need a pad of 2.
313 */
Joe Perchesd9d10a32013-07-30 10:31:00 -0700314static inline int ndisc_addr_option_pad(unsigned short type)
David S. Miller30f2a5f2012-07-11 23:26:46 -0700315{
316 switch (type) {
317 case ARPHRD_INFINIBAND: return 2;
318 default: return 0;
319 }
320}
321
Alexander Aring1e82f962016-06-15 21:20:19 +0200322static inline int __ndisc_opt_addr_space(unsigned char addr_len, int pad)
323{
324 return NDISC_OPT_SPACE(addr_len + pad);
325}
326
Alexander Aringf997c552016-06-15 21:20:23 +0200327#if IS_ENABLED(CONFIG_IPV6)
328static inline int ndisc_opt_addr_space(struct net_device *dev, u8 icmp6_type)
YOSHIFUJI Hideaki / 吉藤英明c558e9f2013-01-21 06:47:56 +0000329{
Alexander Aring1e82f962016-06-15 21:20:19 +0200330 return __ndisc_opt_addr_space(dev->addr_len,
Alexander Aringf997c552016-06-15 21:20:23 +0200331 ndisc_addr_option_pad(dev->type)) +
332 ndisc_ops_opt_addr_space(dev, icmp6_type);
YOSHIFUJI Hideaki / 吉藤英明c558e9f2013-01-21 06:47:56 +0000333}
334
Alexander Aringf997c552016-06-15 21:20:23 +0200335static inline int ndisc_redirect_opt_addr_space(struct net_device *dev,
336 struct neighbour *neigh,
337 u8 *ops_data_buf,
338 u8 **ops_data)
339{
340 return __ndisc_opt_addr_space(dev->addr_len,
341 ndisc_addr_option_pad(dev->type)) +
342 ndisc_ops_redirect_opt_addr_space(dev, neigh, ops_data_buf,
343 ops_data);
344}
345#endif
346
Alexander Aring4f36ce82016-06-15 21:20:20 +0200347static inline u8 *__ndisc_opt_addr_data(struct nd_opt_hdr *p,
348 unsigned char addr_len, int prepad)
David S. Miller30f2a5f2012-07-11 23:26:46 -0700349{
350 u8 *lladdr = (u8 *)(p + 1);
351 int lladdrlen = p->nd_opt_len << 3;
Alexander Aring4f36ce82016-06-15 21:20:20 +0200352 if (lladdrlen != __ndisc_opt_addr_space(addr_len, prepad))
David S. Miller30f2a5f2012-07-11 23:26:46 -0700353 return NULL;
354 return lladdr + prepad;
355}
356
Alexander Aring4f36ce82016-06-15 21:20:20 +0200357static inline u8 *ndisc_opt_addr_data(struct nd_opt_hdr *p,
358 struct net_device *dev)
359{
360 return __ndisc_opt_addr_data(p, dev->addr_len,
361 ndisc_addr_option_pad(dev->type));
362}
363
David S. Miller2c2aba62011-12-28 15:06:58 -0500364static inline u32 ndisc_hashfn(const void *pkey, const struct net_device *dev, __u32 *hash_rnd)
365{
366 const u32 *p32 = pkey;
367
Pavel Emelyanovb14f2432012-08-08 21:52:28 +0000368 return (((p32[0] ^ hash32_ptr(dev)) * hash_rnd[0]) +
David S. Miller2c2aba62011-12-28 15:06:58 -0500369 (p32[1] * hash_rnd[1]) +
370 (p32[2] * hash_rnd[2]) +
371 (p32[3] * hash_rnd[3]));
372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
YOSHIFUJI Hideaki / 吉藤英明ac3175f2013-01-17 12:53:22 +0000374static inline struct neighbour *__ipv6_neigh_lookup_noref(struct net_device *dev, const void *pkey)
David S. Millerf83c7792011-12-28 15:41:23 -0500375{
Eric W. Biederman60395a22015-03-03 17:10:44 -0600376 return ___neigh_lookup_noref(&nd_tbl, neigh_key_eq128, ndisc_hashfn, pkey, dev);
YOSHIFUJI Hideaki / 吉藤英明ac3175f2013-01-17 12:53:22 +0000377}
378
379static inline struct neighbour *__ipv6_neigh_lookup(struct net_device *dev, const void *pkey)
380{
381 struct neighbour *n;
382
383 rcu_read_lock_bh();
384 n = __ipv6_neigh_lookup_noref(dev, pkey);
385 if (n && !atomic_inc_not_zero(&n->refcnt))
386 n = NULL;
David S. Millerf83c7792011-12-28 15:41:23 -0500387 rcu_read_unlock_bh();
388
389 return n;
390}
391
Joe Perches3cc818a2013-09-21 10:22:47 -0700392int ndisc_init(void);
393int ndisc_late_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Joe Perches3cc818a2013-09-21 10:22:47 -0700395void ndisc_late_cleanup(void);
396void ndisc_cleanup(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Joe Perches3cc818a2013-09-21 10:22:47 -0700398int ndisc_rcv(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Jiri Benc38cf5952015-09-22 18:57:13 +0200400void ndisc_send_ns(struct net_device *dev, const struct in6_addr *solicit,
Nicolas Dichtel304d8882015-11-27 18:17:05 +0100401 const struct in6_addr *daddr, const struct in6_addr *saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Joe Perches3cc818a2013-09-21 10:22:47 -0700403void ndisc_send_rs(struct net_device *dev,
404 const struct in6_addr *saddr, const struct in6_addr *daddr);
Jiri Benc38cf5952015-09-22 18:57:13 +0200405void ndisc_send_na(struct net_device *dev, const struct in6_addr *daddr,
Joe Perches3cc818a2013-09-21 10:22:47 -0700406 const struct in6_addr *solicited_addr,
407 bool router, bool solicited, bool override, bool inc_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Joe Perches3cc818a2013-09-21 10:22:47 -0700409void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Joe Perches3cc818a2013-09-21 10:22:47 -0700411int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev,
412 int dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Alexander Aringf997c552016-06-15 21:20:23 +0200414void ndisc_update(const struct net_device *dev, struct neighbour *neigh,
415 const u8 *lladdr, u8 new, u32 flags, u8 icmp6_type,
416 struct ndisc_options *ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418/*
419 * IGMP
420 */
Joe Perches3cc818a2013-09-21 10:22:47 -0700421int igmp6_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Joe Perches3cc818a2013-09-21 10:22:47 -0700423void igmp6_cleanup(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Joe Perches3cc818a2013-09-21 10:22:47 -0700425int igmp6_event_query(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Joe Perches3cc818a2013-09-21 10:22:47 -0700427int igmp6_event_report(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430#ifdef CONFIG_SYSCTL
Joe Perches3cc818a2013-09-21 10:22:47 -0700431int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write,
432 void __user *buffer, size_t *lenp, loff_t *ppos);
Joe Perchesfedaf4f2013-06-13 19:37:54 -0700433int ndisc_ifinfo_sysctl_strategy(struct ctl_table *ctl,
Alexey Dobriyanf221e722008-10-15 22:04:23 -0700434 void __user *oldval, size_t __user *oldlenp,
YOSHIFUJI Hideaki0686caa2008-05-19 16:25:42 -0700435 void __user *newval, size_t newlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436#endif
437
Joe Perches3cc818a2013-09-21 10:22:47 -0700438void inet6_ifinfo_notify(int event, struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440#endif