]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - include/net/ndisc.h
6lowpan: introduce 6lowpan-nd
[mirror_ubuntu-zesty-kernel.git] / include / net / ndisc.h
CommitLineData
1da177e4
LT
1#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
fadf6bf0
TF
14/*
15 * 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
1da177e4
LT
23/*
24 * 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 */
70ceb4f5
YH
34 __ND_OPT_ARRAY_MAX,
35 ND_OPT_ROUTE_INFO = 24, /* RFC4191 */
31910575 36 ND_OPT_RDNSS = 25, /* RFC5006 */
e35f30c1 37 ND_OPT_DNSSL = 31, /* RFC6106 */
bbe5f5ce 38 ND_OPT_6CO = 34, /* RFC6775 */
1da177e4
LT
39 __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
14c85021 47#include <linux/compiler.h>
1da177e4 48#include <linux/icmpv6.h>
14c85021
ACM
49#include <linux/in6.h>
50#include <linux/types.h>
30f2a5f3
DM
51#include <linux/if_arp.h>
52#include <linux/netdevice.h>
b14f243a 53#include <linux/hash.h>
14c85021 54
1da177e4 55#include <net/neighbour.h>
14c85021 56
cc84b3c6
AA
57/* 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
14c85021 66struct ctl_table;
14c85021
ACM
67struct inet6_dev;
68struct net_device;
69struct net_proto_family;
70struct sk_buff;
f997c55c 71struct prefix_info;
1da177e4
LT
72
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;
e69a4adc
AV
88 __be32 reachable_time;
89 __be32 retrans_timer;
1da177e4
LT
90};
91
093d04d4
DJ
92struct rd_msg {
93 struct icmp6hdr icmph;
94 struct in6_addr target;
95 struct in6_addr dest;
96 __u8 opt[0];
97};
98
1da177e4
LT
99struct nd_opt_hdr {
100 __u8 nd_opt_type;
101 __u8 nd_opt_len;
bc10502d 102} __packed;
1da177e4 103
30f2a5f3
DM
104/* 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;
bbe5f5ce
AA
113#if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
114 struct nd_opt_hdr *nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR + 1];
115#endif
30f2a5f3
DM
116};
117
bbe5f5ce
AA
118#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]
30f2a5f3
DM
126
127#define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
128
f997c55c
AA
129struct ndisc_options *ndisc_parse_options(const struct net_device *dev,
130 u8 *opt, int opt_len,
3cc818a2 131 struct ndisc_options *ndopts);
30f2a5f3 132
cc84b3c6
AA
133void __ndisc_fill_addr_option(struct sk_buff *skb, int type, void *data,
134 int data_len, int pad);
135
f997c55c
AA
136#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
30f2a5f3
DM
308/*
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 */
d9d10a30 314static inline int ndisc_addr_option_pad(unsigned short type)
30f2a5f3
DM
315{
316 switch (type) {
317 case ARPHRD_INFINIBAND: return 2;
318 default: return 0;
319 }
320}
321
1e82f961
AA
322static inline int __ndisc_opt_addr_space(unsigned char addr_len, int pad)
323{
324 return NDISC_OPT_SPACE(addr_len + pad);
325}
326
f997c55c
AA
327#if IS_ENABLED(CONFIG_IPV6)
328static inline int ndisc_opt_addr_space(struct net_device *dev, u8 icmp6_type)
329{
330 return __ndisc_opt_addr_space(dev->addr_len,
331 ndisc_addr_option_pad(dev->type)) +
332 ndisc_ops_opt_addr_space(dev, icmp6_type);
333}
334
335static inline int ndisc_redirect_opt_addr_space(struct net_device *dev,
336 struct neighbour *neigh,
337 u8 *ops_data_buf,
338 u8 **ops_data)
c558e9fc 339{
1e82f961 340 return __ndisc_opt_addr_space(dev->addr_len,
f997c55c
AA
341 ndisc_addr_option_pad(dev->type)) +
342 ndisc_ops_redirect_opt_addr_space(dev, neigh, ops_data_buf,
343 ops_data);
c558e9fc 344}
f997c55c 345#endif
c558e9fc 346
4f36ce84
AA
347static inline u8 *__ndisc_opt_addr_data(struct nd_opt_hdr *p,
348 unsigned char addr_len, int prepad)
30f2a5f3
DM
349{
350 u8 *lladdr = (u8 *)(p + 1);
351 int lladdrlen = p->nd_opt_len << 3;
4f36ce84 352 if (lladdrlen != __ndisc_opt_addr_space(addr_len, prepad))
30f2a5f3
DM
353 return NULL;
354 return lladdr + prepad;
355}
356
4f36ce84
AA
357static 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
2c2aba6c
DM
364static inline u32 ndisc_hashfn(const void *pkey, const struct net_device *dev, __u32 *hash_rnd)
365{
366 const u32 *p32 = pkey;
367
b14f243a 368 return (((p32[0] ^ hash32_ptr(dev)) * hash_rnd[0]) +
2c2aba6c
DM
369 (p32[1] * hash_rnd[1]) +
370 (p32[2] * hash_rnd[2]) +
371 (p32[3] * hash_rnd[3]));
372}
1da177e4 373
ac3175fe 374static inline struct neighbour *__ipv6_neigh_lookup_noref(struct net_device *dev, const void *pkey)
f83c7790 375{
60395a20 376 return ___neigh_lookup_noref(&nd_tbl, neigh_key_eq128, ndisc_hashfn, pkey, dev);
ac3175fe
YH
377}
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;
f83c7790
DM
387 rcu_read_unlock_bh();
388
389 return n;
390}
391
3cc818a2
JP
392int ndisc_init(void);
393int ndisc_late_init(void);
1da177e4 394
3cc818a2
JP
395void ndisc_late_cleanup(void);
396void ndisc_cleanup(void);
1da177e4 397
3cc818a2 398int ndisc_rcv(struct sk_buff *skb);
1da177e4 399
38cf595b 400void ndisc_send_ns(struct net_device *dev, const struct in6_addr *solicit,
304d888b 401 const struct in6_addr *daddr, const struct in6_addr *saddr);
1da177e4 402
3cc818a2
JP
403void ndisc_send_rs(struct net_device *dev,
404 const struct in6_addr *saddr, const struct in6_addr *daddr);
38cf595b 405void ndisc_send_na(struct net_device *dev, const struct in6_addr *daddr,
3cc818a2
JP
406 const struct in6_addr *solicited_addr,
407 bool router, bool solicited, bool override, bool inc_opt);
1da177e4 408
3cc818a2 409void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target);
1da177e4 410
3cc818a2
JP
411int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev,
412 int dir);
1da177e4 413
f997c55c
AA
414void 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);
1da177e4
LT
417
418/*
419 * IGMP
420 */
3cc818a2 421int igmp6_init(void);
1da177e4 422
3cc818a2 423void igmp6_cleanup(void);
1da177e4 424
3cc818a2 425int igmp6_event_query(struct sk_buff *skb);
1da177e4 426
3cc818a2 427int igmp6_event_report(struct sk_buff *skb);
1da177e4 428
1da177e4
LT
429
430#ifdef CONFIG_SYSCTL
3cc818a2
JP
431int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write,
432 void __user *buffer, size_t *lenp, loff_t *ppos);
fedaf4ff 433int ndisc_ifinfo_sysctl_strategy(struct ctl_table *ctl,
f221e726 434 void __user *oldval, size_t __user *oldlenp,
0686caa3 435 void __user *newval, size_t newlen);
1da177e4
LT
436#endif
437
3cc818a2 438void inet6_ifinfo_notify(int event, struct inet6_dev *idev);
1da177e4 439
1da177e4 440#endif