blob: 3f0f41ddbeb043103b348c522c171862cea9ba52 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 __ND_OPT_MAX
39};
40
41#define MAX_RTR_SOLICITATION_DELAY HZ
42
43#define ND_REACHABLE_TIME (30*HZ)
44#define ND_RETRANS_TIMER HZ
45
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020046#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/icmpv6.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020048#include <linux/in6.h>
49#include <linux/types.h>
David S. Miller30f2a5f2012-07-11 23:26:46 -070050#include <linux/if_arp.h>
51#include <linux/netdevice.h>
Pavel Emelyanovb14f2432012-08-08 21:52:28 +000052#include <linux/hash.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <net/neighbour.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020055
Alexander Aringcc84b3c2016-06-15 21:20:24 +020056/* Set to 3 to get tracing... */
57#define ND_DEBUG 1
58
59#define ND_PRINTK(val, level, fmt, ...) \
60do { \
61 if (val <= ND_DEBUG) \
62 net_##level##_ratelimited(fmt, ##__VA_ARGS__); \
63} while (0)
64
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020065struct ctl_table;
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020066struct inet6_dev;
67struct net_device;
68struct net_proto_family;
69struct sk_buff;
Alexander Aringf997c552016-06-15 21:20:23 +020070struct prefix_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72extern struct neigh_table nd_tbl;
73
74struct nd_msg {
75 struct icmp6hdr icmph;
76 struct in6_addr target;
77 __u8 opt[0];
78};
79
80struct rs_msg {
81 struct icmp6hdr icmph;
82 __u8 opt[0];
83};
84
85struct ra_msg {
86 struct icmp6hdr icmph;
Al Viroe69a4adc2006-11-14 20:56:00 -080087 __be32 reachable_time;
88 __be32 retrans_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
Duan Jiong093d04d2012-12-14 02:59:59 +000091struct rd_msg {
92 struct icmp6hdr icmph;
93 struct in6_addr target;
94 struct in6_addr dest;
95 __u8 opt[0];
96};
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098struct nd_opt_hdr {
99 __u8 nd_opt_type;
100 __u8 nd_opt_len;
Eric Dumazetbc105022010-06-03 03:21:52 -0700101} __packed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
David S. Miller30f2a5f2012-07-11 23:26:46 -0700103/* ND options */
104struct ndisc_options {
105 struct nd_opt_hdr *nd_opt_array[__ND_OPT_ARRAY_MAX];
106#ifdef CONFIG_IPV6_ROUTE_INFO
107 struct nd_opt_hdr *nd_opts_ri;
108 struct nd_opt_hdr *nd_opts_ri_end;
109#endif
110 struct nd_opt_hdr *nd_useropts;
111 struct nd_opt_hdr *nd_useropts_end;
112};
113
114#define nd_opts_src_lladdr nd_opt_array[ND_OPT_SOURCE_LL_ADDR]
115#define nd_opts_tgt_lladdr nd_opt_array[ND_OPT_TARGET_LL_ADDR]
116#define nd_opts_pi nd_opt_array[ND_OPT_PREFIX_INFO]
117#define nd_opts_pi_end nd_opt_array[__ND_OPT_PREFIX_INFO_END]
118#define nd_opts_rh nd_opt_array[ND_OPT_REDIRECT_HDR]
119#define nd_opts_mtu nd_opt_array[ND_OPT_MTU]
120
121#define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
122
Alexander Aringf997c552016-06-15 21:20:23 +0200123struct ndisc_options *ndisc_parse_options(const struct net_device *dev,
124 u8 *opt, int opt_len,
Joe Perches3cc818a2013-09-21 10:22:47 -0700125 struct ndisc_options *ndopts);
David S. Miller30f2a5f2012-07-11 23:26:46 -0700126
Alexander Aringcc84b3c2016-06-15 21:20:24 +0200127void __ndisc_fill_addr_option(struct sk_buff *skb, int type, void *data,
128 int data_len, int pad);
129
Alexander Aringf997c552016-06-15 21:20:23 +0200130#define NDISC_OPS_REDIRECT_DATA_SPACE 2
131
132/*
133 * This structure defines the hooks for IPv6 neighbour discovery.
134 * The following hooks can be defined; unless noted otherwise, they are
135 * optional and can be filled with a null pointer.
136 *
137 * int (*is_useropt)(u8 nd_opt_type):
138 * This function is called when IPv6 decide RA userspace options. if
139 * this function returns 1 then the option given by nd_opt_type will
140 * be handled as userspace option additional to the IPv6 options.
141 *
142 * int (*parse_options)(const struct net_device *dev,
143 * struct nd_opt_hdr *nd_opt,
144 * struct ndisc_options *ndopts):
145 * This function is called while parsing ndisc ops and put each position
146 * as pointer into ndopts. If this function return unequal 0, then this
147 * function took care about the ndisc option, if 0 then the IPv6 ndisc
148 * option parser will take care about that option.
149 *
150 * void (*update)(const struct net_device *dev, struct neighbour *n,
151 * u32 flags, u8 icmp6_type,
152 * const struct ndisc_options *ndopts):
153 * This function is called when IPv6 ndisc updates the neighbour cache
154 * entry. Additional options which can be updated may be previously
155 * parsed by parse_opts callback and accessible over ndopts parameter.
156 *
157 * int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
158 * struct neighbour *neigh, u8 *ha_buf,
159 * u8 **ha):
160 * This function is called when the necessary option space will be
161 * calculated before allocating a skb. The parameters neigh, ha_buf
162 * abd ha are available on NDISC_REDIRECT messages only.
163 *
164 * void (*fill_addr_option)(const struct net_device *dev,
165 * struct sk_buff *skb, u8 icmp6_type,
166 * const u8 *ha):
167 * This function is called when the skb will finally fill the option
168 * fields inside skb. NOTE: this callback should fill the option
169 * fields to the skb which are previously indicated by opt_space
170 * parameter. That means the decision to add such option should
171 * not lost between these two callbacks, e.g. protected by interface
172 * up state.
173 *
174 * void (*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
175 * const struct prefix_info *pinfo,
176 * struct inet6_dev *in6_dev,
177 * struct in6_addr *addr,
178 * int addr_type, u32 addr_flags,
179 * bool sllao, bool tokenized,
180 * __u32 valid_lft, u32 prefered_lft,
181 * bool dev_addr_generated):
182 * This function is called when a RA messages is received with valid
183 * PIO option fields and an IPv6 address will be added to the interface
184 * for autoconfiguration. The parameter dev_addr_generated reports about
185 * if the address was based on dev->dev_addr or not. This can be used
186 * to add a second address if link-layer operates with two link layer
187 * addresses. E.g. 802.15.4 6LoWPAN.
188 */
189struct ndisc_ops {
190 int (*is_useropt)(u8 nd_opt_type);
191 int (*parse_options)(const struct net_device *dev,
192 struct nd_opt_hdr *nd_opt,
193 struct ndisc_options *ndopts);
194 void (*update)(const struct net_device *dev, struct neighbour *n,
195 u32 flags, u8 icmp6_type,
196 const struct ndisc_options *ndopts);
197 int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
198 struct neighbour *neigh, u8 *ha_buf,
199 u8 **ha);
200 void (*fill_addr_option)(const struct net_device *dev,
201 struct sk_buff *skb, u8 icmp6_type,
202 const u8 *ha);
203 void (*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
204 const struct prefix_info *pinfo,
205 struct inet6_dev *in6_dev,
206 struct in6_addr *addr,
207 int addr_type, u32 addr_flags,
208 bool sllao, bool tokenized,
209 __u32 valid_lft, u32 prefered_lft,
210 bool dev_addr_generated);
211};
212
213#if IS_ENABLED(CONFIG_IPV6)
214static inline int ndisc_ops_is_useropt(const struct net_device *dev,
215 u8 nd_opt_type)
216{
217 if (dev->ndisc_ops && dev->ndisc_ops->is_useropt)
218 return dev->ndisc_ops->is_useropt(nd_opt_type);
219 else
220 return 0;
221}
222
223static inline int ndisc_ops_parse_options(const struct net_device *dev,
224 struct nd_opt_hdr *nd_opt,
225 struct ndisc_options *ndopts)
226{
227 if (dev->ndisc_ops && dev->ndisc_ops->parse_options)
228 return dev->ndisc_ops->parse_options(dev, nd_opt, ndopts);
229 else
230 return 0;
231}
232
233static inline void ndisc_ops_update(const struct net_device *dev,
234 struct neighbour *n, u32 flags,
235 u8 icmp6_type,
236 const struct ndisc_options *ndopts)
237{
238 if (dev->ndisc_ops && dev->ndisc_ops->update)
239 dev->ndisc_ops->update(dev, n, flags, icmp6_type, ndopts);
240}
241
242static inline int ndisc_ops_opt_addr_space(const struct net_device *dev,
243 u8 icmp6_type)
244{
245 if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space &&
246 icmp6_type != NDISC_REDIRECT)
247 return dev->ndisc_ops->opt_addr_space(dev, icmp6_type, NULL,
248 NULL, NULL);
249 else
250 return 0;
251}
252
253static inline int ndisc_ops_redirect_opt_addr_space(const struct net_device *dev,
254 struct neighbour *neigh,
255 u8 *ha_buf, u8 **ha)
256{
257 if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space)
258 return dev->ndisc_ops->opt_addr_space(dev, NDISC_REDIRECT,
259 neigh, ha_buf, ha);
260 else
261 return 0;
262}
263
264static inline void ndisc_ops_fill_addr_option(const struct net_device *dev,
265 struct sk_buff *skb,
266 u8 icmp6_type)
267{
268 if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option &&
269 icmp6_type != NDISC_REDIRECT)
270 dev->ndisc_ops->fill_addr_option(dev, skb, icmp6_type, NULL);
271}
272
273static inline void ndisc_ops_fill_redirect_addr_option(const struct net_device *dev,
274 struct sk_buff *skb,
275 const u8 *ha)
276{
277 if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option)
278 dev->ndisc_ops->fill_addr_option(dev, skb, NDISC_REDIRECT, ha);
279}
280
281static inline void ndisc_ops_prefix_rcv_add_addr(struct net *net,
282 struct net_device *dev,
283 const struct prefix_info *pinfo,
284 struct inet6_dev *in6_dev,
285 struct in6_addr *addr,
286 int addr_type, u32 addr_flags,
287 bool sllao, bool tokenized,
288 __u32 valid_lft,
289 u32 prefered_lft,
290 bool dev_addr_generated)
291{
292 if (dev->ndisc_ops && dev->ndisc_ops->prefix_rcv_add_addr)
293 dev->ndisc_ops->prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
294 addr, addr_type,
295 addr_flags, sllao,
296 tokenized, valid_lft,
297 prefered_lft,
298 dev_addr_generated);
299}
300#endif
301
David S. Miller30f2a5f2012-07-11 23:26:46 -0700302/*
303 * Return the padding between the option length and the start of the
304 * link addr. Currently only IP-over-InfiniBand needs this, although
305 * if RFC 3831 IPv6-over-Fibre Channel is ever implemented it may
306 * also need a pad of 2.
307 */
Joe Perchesd9d10a32013-07-30 10:31:00 -0700308static inline int ndisc_addr_option_pad(unsigned short type)
David S. Miller30f2a5f2012-07-11 23:26:46 -0700309{
310 switch (type) {
311 case ARPHRD_INFINIBAND: return 2;
312 default: return 0;
313 }
314}
315
Alexander Aring1e82f962016-06-15 21:20:19 +0200316static inline int __ndisc_opt_addr_space(unsigned char addr_len, int pad)
317{
318 return NDISC_OPT_SPACE(addr_len + pad);
319}
320
Alexander Aringf997c552016-06-15 21:20:23 +0200321#if IS_ENABLED(CONFIG_IPV6)
322static inline int ndisc_opt_addr_space(struct net_device *dev, u8 icmp6_type)
YOSHIFUJI Hideaki / 吉藤英明c558e9f2013-01-21 06:47:56 +0000323{
Alexander Aring1e82f962016-06-15 21:20:19 +0200324 return __ndisc_opt_addr_space(dev->addr_len,
Alexander Aringf997c552016-06-15 21:20:23 +0200325 ndisc_addr_option_pad(dev->type)) +
326 ndisc_ops_opt_addr_space(dev, icmp6_type);
YOSHIFUJI Hideaki / 吉藤英明c558e9f2013-01-21 06:47:56 +0000327}
328
Alexander Aringf997c552016-06-15 21:20:23 +0200329static inline int ndisc_redirect_opt_addr_space(struct net_device *dev,
330 struct neighbour *neigh,
331 u8 *ops_data_buf,
332 u8 **ops_data)
333{
334 return __ndisc_opt_addr_space(dev->addr_len,
335 ndisc_addr_option_pad(dev->type)) +
336 ndisc_ops_redirect_opt_addr_space(dev, neigh, ops_data_buf,
337 ops_data);
338}
339#endif
340
Alexander Aring4f36ce82016-06-15 21:20:20 +0200341static inline u8 *__ndisc_opt_addr_data(struct nd_opt_hdr *p,
342 unsigned char addr_len, int prepad)
David S. Miller30f2a5f2012-07-11 23:26:46 -0700343{
344 u8 *lladdr = (u8 *)(p + 1);
345 int lladdrlen = p->nd_opt_len << 3;
Alexander Aring4f36ce82016-06-15 21:20:20 +0200346 if (lladdrlen != __ndisc_opt_addr_space(addr_len, prepad))
David S. Miller30f2a5f2012-07-11 23:26:46 -0700347 return NULL;
348 return lladdr + prepad;
349}
350
Alexander Aring4f36ce82016-06-15 21:20:20 +0200351static inline u8 *ndisc_opt_addr_data(struct nd_opt_hdr *p,
352 struct net_device *dev)
353{
354 return __ndisc_opt_addr_data(p, dev->addr_len,
355 ndisc_addr_option_pad(dev->type));
356}
357
David S. Miller2c2aba62011-12-28 15:06:58 -0500358static inline u32 ndisc_hashfn(const void *pkey, const struct net_device *dev, __u32 *hash_rnd)
359{
360 const u32 *p32 = pkey;
361
Pavel Emelyanovb14f2432012-08-08 21:52:28 +0000362 return (((p32[0] ^ hash32_ptr(dev)) * hash_rnd[0]) +
David S. Miller2c2aba62011-12-28 15:06:58 -0500363 (p32[1] * hash_rnd[1]) +
364 (p32[2] * hash_rnd[2]) +
365 (p32[3] * hash_rnd[3]));
366}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
YOSHIFUJI Hideaki / 吉藤英明ac3175f2013-01-17 12:53:22 +0000368static inline struct neighbour *__ipv6_neigh_lookup_noref(struct net_device *dev, const void *pkey)
David S. Millerf83c7792011-12-28 15:41:23 -0500369{
Eric W. Biederman60395a22015-03-03 17:10:44 -0600370 return ___neigh_lookup_noref(&nd_tbl, neigh_key_eq128, ndisc_hashfn, pkey, dev);
YOSHIFUJI Hideaki / 吉藤英明ac3175f2013-01-17 12:53:22 +0000371}
372
373static inline struct neighbour *__ipv6_neigh_lookup(struct net_device *dev, const void *pkey)
374{
375 struct neighbour *n;
376
377 rcu_read_lock_bh();
378 n = __ipv6_neigh_lookup_noref(dev, pkey);
379 if (n && !atomic_inc_not_zero(&n->refcnt))
380 n = NULL;
David S. Millerf83c7792011-12-28 15:41:23 -0500381 rcu_read_unlock_bh();
382
383 return n;
384}
385
Joe Perches3cc818a2013-09-21 10:22:47 -0700386int ndisc_init(void);
387int ndisc_late_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Joe Perches3cc818a2013-09-21 10:22:47 -0700389void ndisc_late_cleanup(void);
390void ndisc_cleanup(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Joe Perches3cc818a2013-09-21 10:22:47 -0700392int ndisc_rcv(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Jiri Benc38cf5952015-09-22 18:57:13 +0200394void ndisc_send_ns(struct net_device *dev, const struct in6_addr *solicit,
Nicolas Dichtel304d8882015-11-27 18:17:05 +0100395 const struct in6_addr *daddr, const struct in6_addr *saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Joe Perches3cc818a2013-09-21 10:22:47 -0700397void ndisc_send_rs(struct net_device *dev,
398 const struct in6_addr *saddr, const struct in6_addr *daddr);
Jiri Benc38cf5952015-09-22 18:57:13 +0200399void ndisc_send_na(struct net_device *dev, const struct in6_addr *daddr,
Joe Perches3cc818a2013-09-21 10:22:47 -0700400 const struct in6_addr *solicited_addr,
401 bool router, bool solicited, bool override, bool inc_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Joe Perches3cc818a2013-09-21 10:22:47 -0700403void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Joe Perches3cc818a2013-09-21 10:22:47 -0700405int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev,
406 int dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Alexander Aringf997c552016-06-15 21:20:23 +0200408void ndisc_update(const struct net_device *dev, struct neighbour *neigh,
409 const u8 *lladdr, u8 new, u32 flags, u8 icmp6_type,
410 struct ndisc_options *ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412/*
413 * IGMP
414 */
Joe Perches3cc818a2013-09-21 10:22:47 -0700415int igmp6_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Joe Perches3cc818a2013-09-21 10:22:47 -0700417void igmp6_cleanup(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Joe Perches3cc818a2013-09-21 10:22:47 -0700419int igmp6_event_query(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Joe Perches3cc818a2013-09-21 10:22:47 -0700421int igmp6_event_report(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424#ifdef CONFIG_SYSCTL
Joe Perches3cc818a2013-09-21 10:22:47 -0700425int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write,
426 void __user *buffer, size_t *lenp, loff_t *ppos);
Joe Perchesfedaf4f2013-06-13 19:37:54 -0700427int ndisc_ifinfo_sysctl_strategy(struct ctl_table *ctl,
Alexey Dobriyanf221e722008-10-15 22:04:23 -0700428 void __user *oldval, size_t __user *oldlenp,
YOSHIFUJI Hideaki0686caa2008-05-19 16:25:42 -0700429 void __user *newval, size_t newlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430#endif
431
Joe Perches3cc818a2013-09-21 10:22:47 -0700432void inet6_ifinfo_notify(int event, struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434#endif