blob: 96c475bbc57bc0d00854ffb772d176dac56745d2 [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 Żenczykowski28236052019-07-18 23:30:03 -070039 ND_OPT_CAPTIVE_PORTAL = 37, /* RFC7710 */
Maciej Żenczykowski436619d2020-03-23 18:10:19 -070040 ND_OPT_PREF64 = 38, /* RFC-ietf-6man-ra-pref64-09 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 __ND_OPT_MAX
42};
43
44#define MAX_RTR_SOLICITATION_DELAY HZ
45
46#define ND_REACHABLE_TIME (30*HZ)
47#define ND_RETRANS_TIMER HZ
48
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020049#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/icmpv6.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020051#include <linux/in6.h>
52#include <linux/types.h>
David S. Miller30f2a5f2012-07-11 23:26:46 -070053#include <linux/if_arp.h>
54#include <linux/netdevice.h>
Pavel Emelyanovb14f2432012-08-08 21:52:28 +000055#include <linux/hash.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <net/neighbour.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020058
Alexander Aringcc84b3c2016-06-15 21:20:24 +020059/* Set to 3 to get tracing... */
60#define ND_DEBUG 1
61
62#define ND_PRINTK(val, level, fmt, ...) \
63do { \
64 if (val <= ND_DEBUG) \
65 net_##level##_ratelimited(fmt, ##__VA_ARGS__); \
66} while (0)
67
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020068struct ctl_table;
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020069struct inet6_dev;
70struct net_device;
71struct net_proto_family;
72struct sk_buff;
Alexander Aringf997c552016-06-15 21:20:23 +020073struct prefix_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75extern struct neigh_table nd_tbl;
76
77struct nd_msg {
78 struct icmp6hdr icmph;
79 struct in6_addr target;
80 __u8 opt[0];
81};
82
83struct rs_msg {
84 struct icmp6hdr icmph;
85 __u8 opt[0];
86};
87
88struct ra_msg {
89 struct icmp6hdr icmph;
Al Viroe69a4adc2006-11-14 20:56:00 -080090 __be32 reachable_time;
91 __be32 retrans_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
Duan Jiong093d04d2012-12-14 02:59:59 +000094struct rd_msg {
95 struct icmp6hdr icmph;
96 struct in6_addr target;
97 struct in6_addr dest;
98 __u8 opt[0];
99};
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101struct nd_opt_hdr {
102 __u8 nd_opt_type;
103 __u8 nd_opt_len;
Eric Dumazetbc105022010-06-03 03:21:52 -0700104} __packed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
David S. Miller30f2a5f2012-07-11 23:26:46 -0700106/* ND options */
107struct ndisc_options {
108 struct nd_opt_hdr *nd_opt_array[__ND_OPT_ARRAY_MAX];
109#ifdef CONFIG_IPV6_ROUTE_INFO
110 struct nd_opt_hdr *nd_opts_ri;
111 struct nd_opt_hdr *nd_opts_ri_end;
112#endif
113 struct nd_opt_hdr *nd_useropts;
114 struct nd_opt_hdr *nd_useropts_end;
Alexander Aringbbe5f5c2016-06-15 21:20:25 +0200115#if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
116 struct nd_opt_hdr *nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR + 1];
117#endif
David S. Miller30f2a5f2012-07-11 23:26:46 -0700118};
119
Alexander Aringbbe5f5c2016-06-15 21:20:25 +0200120#define nd_opts_src_lladdr nd_opt_array[ND_OPT_SOURCE_LL_ADDR]
121#define nd_opts_tgt_lladdr nd_opt_array[ND_OPT_TARGET_LL_ADDR]
122#define nd_opts_pi nd_opt_array[ND_OPT_PREFIX_INFO]
123#define nd_opts_pi_end nd_opt_array[__ND_OPT_PREFIX_INFO_END]
124#define nd_opts_rh nd_opt_array[ND_OPT_REDIRECT_HDR]
125#define nd_opts_mtu nd_opt_array[ND_OPT_MTU]
126#define nd_802154_opts_src_lladdr nd_802154_opt_array[ND_OPT_SOURCE_LL_ADDR]
127#define nd_802154_opts_tgt_lladdr nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR]
David S. Miller30f2a5f2012-07-11 23:26:46 -0700128
129#define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
130
Alexander Aringf997c552016-06-15 21:20:23 +0200131struct ndisc_options *ndisc_parse_options(const struct net_device *dev,
132 u8 *opt, int opt_len,
Joe Perches3cc818a2013-09-21 10:22:47 -0700133 struct ndisc_options *ndopts);
David S. Miller30f2a5f2012-07-11 23:26:46 -0700134
Alexander Aringcc84b3c2016-06-15 21:20:24 +0200135void __ndisc_fill_addr_option(struct sk_buff *skb, int type, void *data,
136 int data_len, int pad);
137
Alexander Aringf997c552016-06-15 21:20:23 +0200138#define NDISC_OPS_REDIRECT_DATA_SPACE 2
139
140/*
141 * This structure defines the hooks for IPv6 neighbour discovery.
142 * The following hooks can be defined; unless noted otherwise, they are
143 * optional and can be filled with a null pointer.
144 *
145 * int (*is_useropt)(u8 nd_opt_type):
146 * This function is called when IPv6 decide RA userspace options. if
147 * this function returns 1 then the option given by nd_opt_type will
148 * be handled as userspace option additional to the IPv6 options.
149 *
150 * int (*parse_options)(const struct net_device *dev,
151 * struct nd_opt_hdr *nd_opt,
152 * struct ndisc_options *ndopts):
153 * This function is called while parsing ndisc ops and put each position
154 * as pointer into ndopts. If this function return unequal 0, then this
155 * function took care about the ndisc option, if 0 then the IPv6 ndisc
156 * option parser will take care about that option.
157 *
158 * void (*update)(const struct net_device *dev, struct neighbour *n,
159 * u32 flags, u8 icmp6_type,
160 * const struct ndisc_options *ndopts):
161 * This function is called when IPv6 ndisc updates the neighbour cache
162 * entry. Additional options which can be updated may be previously
163 * parsed by parse_opts callback and accessible over ndopts parameter.
164 *
165 * int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
166 * struct neighbour *neigh, u8 *ha_buf,
167 * u8 **ha):
168 * This function is called when the necessary option space will be
169 * calculated before allocating a skb. The parameters neigh, ha_buf
170 * abd ha are available on NDISC_REDIRECT messages only.
171 *
172 * void (*fill_addr_option)(const struct net_device *dev,
173 * struct sk_buff *skb, u8 icmp6_type,
174 * const u8 *ha):
175 * This function is called when the skb will finally fill the option
176 * fields inside skb. NOTE: this callback should fill the option
177 * fields to the skb which are previously indicated by opt_space
178 * parameter. That means the decision to add such option should
179 * not lost between these two callbacks, e.g. protected by interface
180 * up state.
181 *
182 * void (*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
183 * const struct prefix_info *pinfo,
184 * struct inet6_dev *in6_dev,
185 * struct in6_addr *addr,
186 * int addr_type, u32 addr_flags,
187 * bool sllao, bool tokenized,
188 * __u32 valid_lft, u32 prefered_lft,
189 * bool dev_addr_generated):
190 * This function is called when a RA messages is received with valid
191 * PIO option fields and an IPv6 address will be added to the interface
192 * for autoconfiguration. The parameter dev_addr_generated reports about
193 * if the address was based on dev->dev_addr or not. This can be used
194 * to add a second address if link-layer operates with two link layer
195 * addresses. E.g. 802.15.4 6LoWPAN.
196 */
197struct ndisc_ops {
198 int (*is_useropt)(u8 nd_opt_type);
199 int (*parse_options)(const struct net_device *dev,
200 struct nd_opt_hdr *nd_opt,
201 struct ndisc_options *ndopts);
202 void (*update)(const struct net_device *dev, struct neighbour *n,
203 u32 flags, u8 icmp6_type,
204 const struct ndisc_options *ndopts);
205 int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
206 struct neighbour *neigh, u8 *ha_buf,
207 u8 **ha);
208 void (*fill_addr_option)(const struct net_device *dev,
209 struct sk_buff *skb, u8 icmp6_type,
210 const u8 *ha);
211 void (*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
212 const struct prefix_info *pinfo,
213 struct inet6_dev *in6_dev,
214 struct in6_addr *addr,
215 int addr_type, u32 addr_flags,
216 bool sllao, bool tokenized,
217 __u32 valid_lft, u32 prefered_lft,
218 bool dev_addr_generated);
219};
220
221#if IS_ENABLED(CONFIG_IPV6)
222static inline int ndisc_ops_is_useropt(const struct net_device *dev,
223 u8 nd_opt_type)
224{
225 if (dev->ndisc_ops && dev->ndisc_ops->is_useropt)
226 return dev->ndisc_ops->is_useropt(nd_opt_type);
227 else
228 return 0;
229}
230
231static inline int ndisc_ops_parse_options(const struct net_device *dev,
232 struct nd_opt_hdr *nd_opt,
233 struct ndisc_options *ndopts)
234{
235 if (dev->ndisc_ops && dev->ndisc_ops->parse_options)
236 return dev->ndisc_ops->parse_options(dev, nd_opt, ndopts);
237 else
238 return 0;
239}
240
241static inline void ndisc_ops_update(const struct net_device *dev,
242 struct neighbour *n, u32 flags,
243 u8 icmp6_type,
244 const struct ndisc_options *ndopts)
245{
246 if (dev->ndisc_ops && dev->ndisc_ops->update)
247 dev->ndisc_ops->update(dev, n, flags, icmp6_type, ndopts);
248}
249
250static inline int ndisc_ops_opt_addr_space(const struct net_device *dev,
251 u8 icmp6_type)
252{
253 if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space &&
254 icmp6_type != NDISC_REDIRECT)
255 return dev->ndisc_ops->opt_addr_space(dev, icmp6_type, NULL,
256 NULL, NULL);
257 else
258 return 0;
259}
260
261static inline int ndisc_ops_redirect_opt_addr_space(const struct net_device *dev,
262 struct neighbour *neigh,
263 u8 *ha_buf, u8 **ha)
264{
265 if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space)
266 return dev->ndisc_ops->opt_addr_space(dev, NDISC_REDIRECT,
267 neigh, ha_buf, ha);
268 else
269 return 0;
270}
271
272static inline void ndisc_ops_fill_addr_option(const struct net_device *dev,
273 struct sk_buff *skb,
274 u8 icmp6_type)
275{
276 if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option &&
277 icmp6_type != NDISC_REDIRECT)
278 dev->ndisc_ops->fill_addr_option(dev, skb, icmp6_type, NULL);
279}
280
281static inline void ndisc_ops_fill_redirect_addr_option(const struct net_device *dev,
282 struct sk_buff *skb,
283 const u8 *ha)
284{
285 if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option)
286 dev->ndisc_ops->fill_addr_option(dev, skb, NDISC_REDIRECT, ha);
287}
288
289static inline void ndisc_ops_prefix_rcv_add_addr(struct net *net,
290 struct net_device *dev,
291 const struct prefix_info *pinfo,
292 struct inet6_dev *in6_dev,
293 struct in6_addr *addr,
294 int addr_type, u32 addr_flags,
295 bool sllao, bool tokenized,
296 __u32 valid_lft,
297 u32 prefered_lft,
298 bool dev_addr_generated)
299{
300 if (dev->ndisc_ops && dev->ndisc_ops->prefix_rcv_add_addr)
301 dev->ndisc_ops->prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
302 addr, addr_type,
303 addr_flags, sllao,
304 tokenized, valid_lft,
305 prefered_lft,
306 dev_addr_generated);
307}
308#endif
309
David S. Miller30f2a5f2012-07-11 23:26:46 -0700310/*
311 * Return the padding between the option length and the start of the
312 * link addr. Currently only IP-over-InfiniBand needs this, although
313 * if RFC 3831 IPv6-over-Fibre Channel is ever implemented it may
314 * also need a pad of 2.
315 */
Joe Perchesd9d10a32013-07-30 10:31:00 -0700316static inline int ndisc_addr_option_pad(unsigned short type)
David S. Miller30f2a5f2012-07-11 23:26:46 -0700317{
318 switch (type) {
319 case ARPHRD_INFINIBAND: return 2;
320 default: return 0;
321 }
322}
323
Alexander Aring1e82f962016-06-15 21:20:19 +0200324static inline int __ndisc_opt_addr_space(unsigned char addr_len, int pad)
325{
326 return NDISC_OPT_SPACE(addr_len + pad);
327}
328
Alexander Aringf997c552016-06-15 21:20:23 +0200329#if IS_ENABLED(CONFIG_IPV6)
330static inline int ndisc_opt_addr_space(struct net_device *dev, u8 icmp6_type)
YOSHIFUJI Hideaki / 吉藤英明c558e9f2013-01-21 06:47:56 +0000331{
Alexander Aring1e82f962016-06-15 21:20:19 +0200332 return __ndisc_opt_addr_space(dev->addr_len,
Alexander Aringf997c552016-06-15 21:20:23 +0200333 ndisc_addr_option_pad(dev->type)) +
334 ndisc_ops_opt_addr_space(dev, icmp6_type);
YOSHIFUJI Hideaki / 吉藤英明c558e9f2013-01-21 06:47:56 +0000335}
336
Alexander Aringf997c552016-06-15 21:20:23 +0200337static inline int ndisc_redirect_opt_addr_space(struct net_device *dev,
338 struct neighbour *neigh,
339 u8 *ops_data_buf,
340 u8 **ops_data)
341{
342 return __ndisc_opt_addr_space(dev->addr_len,
343 ndisc_addr_option_pad(dev->type)) +
344 ndisc_ops_redirect_opt_addr_space(dev, neigh, ops_data_buf,
345 ops_data);
346}
347#endif
348
Alexander Aring4f36ce82016-06-15 21:20:20 +0200349static inline u8 *__ndisc_opt_addr_data(struct nd_opt_hdr *p,
350 unsigned char addr_len, int prepad)
David S. Miller30f2a5f2012-07-11 23:26:46 -0700351{
352 u8 *lladdr = (u8 *)(p + 1);
353 int lladdrlen = p->nd_opt_len << 3;
Alexander Aring4f36ce82016-06-15 21:20:20 +0200354 if (lladdrlen != __ndisc_opt_addr_space(addr_len, prepad))
David S. Miller30f2a5f2012-07-11 23:26:46 -0700355 return NULL;
356 return lladdr + prepad;
357}
358
Alexander Aring4f36ce82016-06-15 21:20:20 +0200359static inline u8 *ndisc_opt_addr_data(struct nd_opt_hdr *p,
360 struct net_device *dev)
361{
362 return __ndisc_opt_addr_data(p, dev->addr_len,
363 ndisc_addr_option_pad(dev->type));
364}
365
David S. Miller2c2aba62011-12-28 15:06:58 -0500366static inline u32 ndisc_hashfn(const void *pkey, const struct net_device *dev, __u32 *hash_rnd)
367{
368 const u32 *p32 = pkey;
369
Pavel Emelyanovb14f2432012-08-08 21:52:28 +0000370 return (((p32[0] ^ hash32_ptr(dev)) * hash_rnd[0]) +
David S. Miller2c2aba62011-12-28 15:06:58 -0500371 (p32[1] * hash_rnd[1]) +
372 (p32[2] * hash_rnd[2]) +
373 (p32[3] * hash_rnd[3]));
374}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
YOSHIFUJI Hideaki / 吉藤英明ac3175f2013-01-17 12:53:22 +0000376static inline struct neighbour *__ipv6_neigh_lookup_noref(struct net_device *dev, const void *pkey)
David S. Millerf83c7792011-12-28 15:41:23 -0500377{
Eric W. Biederman60395a22015-03-03 17:10:44 -0600378 return ___neigh_lookup_noref(&nd_tbl, neigh_key_eq128, ndisc_hashfn, pkey, dev);
YOSHIFUJI Hideaki / 吉藤英明ac3175f2013-01-17 12:53:22 +0000379}
380
381static inline struct neighbour *__ipv6_neigh_lookup(struct net_device *dev, const void *pkey)
382{
383 struct neighbour *n;
384
385 rcu_read_lock_bh();
386 n = __ipv6_neigh_lookup_noref(dev, pkey);
387 if (n && !atomic_inc_not_zero(&n->refcnt))
388 n = NULL;
David S. Millerf83c7792011-12-28 15:41:23 -0500389 rcu_read_unlock_bh();
390
391 return n;
392}
393
Joe Perches3cc818a2013-09-21 10:22:47 -0700394int ndisc_init(void);
395int ndisc_late_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Joe Perches3cc818a2013-09-21 10:22:47 -0700397void ndisc_late_cleanup(void);
398void ndisc_cleanup(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Joe Perches3cc818a2013-09-21 10:22:47 -0700400int ndisc_rcv(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Jiri Benc38cf5952015-09-22 18:57:13 +0200402void ndisc_send_ns(struct net_device *dev, const struct in6_addr *solicit,
Nicolas Dichtel304d8882015-11-27 18:17:05 +0100403 const struct in6_addr *daddr, const struct in6_addr *saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Joe Perches3cc818a2013-09-21 10:22:47 -0700405void ndisc_send_rs(struct net_device *dev,
406 const struct in6_addr *saddr, const struct in6_addr *daddr);
Jiri Benc38cf5952015-09-22 18:57:13 +0200407void ndisc_send_na(struct net_device *dev, const struct in6_addr *daddr,
Joe Perches3cc818a2013-09-21 10:22:47 -0700408 const struct in6_addr *solicited_addr,
409 bool router, bool solicited, bool override, bool inc_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Joe Perches3cc818a2013-09-21 10:22:47 -0700411void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Joe Perches3cc818a2013-09-21 10:22:47 -0700413int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev,
414 int dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Alexander Aringf997c552016-06-15 21:20:23 +0200416void ndisc_update(const struct net_device *dev, struct neighbour *neigh,
417 const u8 *lladdr, u8 new, u32 flags, u8 icmp6_type,
418 struct ndisc_options *ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420/*
421 * IGMP
422 */
Joe Perches3cc818a2013-09-21 10:22:47 -0700423int igmp6_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Joe Perches3cc818a2013-09-21 10:22:47 -0700425void igmp6_cleanup(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Joe Perches3cc818a2013-09-21 10:22:47 -0700427int igmp6_event_query(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Joe Perches3cc818a2013-09-21 10:22:47 -0700429int igmp6_event_report(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432#ifdef CONFIG_SYSCTL
Joe Perches3cc818a2013-09-21 10:22:47 -0700433int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write,
434 void __user *buffer, size_t *lenp, loff_t *ppos);
Joe Perchesfedaf4f2013-06-13 19:37:54 -0700435int ndisc_ifinfo_sysctl_strategy(struct ctl_table *ctl,
Alexey Dobriyanf221e722008-10-15 22:04:23 -0700436 void __user *oldval, size_t __user *oldlenp,
YOSHIFUJI Hideaki0686caa2008-05-19 16:25:42 -0700437 void __user *newval, size_t newlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438#endif
439
Joe Perches3cc818a2013-09-21 10:22:47 -0700440void inet6_ifinfo_notify(int event, struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442#endif