ipv6: introduce neighbour discovery ops
[deliverable/linux.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 */
1da177e4
LT
38 __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
14c85021 46#include <linux/compiler.h>
1da177e4 47#include <linux/icmpv6.h>
14c85021
ACM
48#include <linux/in6.h>
49#include <linux/types.h>
30f2a5f3
DM
50#include <linux/if_arp.h>
51#include <linux/netdevice.h>
b14f243a 52#include <linux/hash.h>
14c85021 53
1da177e4 54#include <net/neighbour.h>
14c85021
ACM
55
56struct ctl_table;
14c85021
ACM
57struct inet6_dev;
58struct net_device;
59struct net_proto_family;
60struct sk_buff;
f997c55c 61struct prefix_info;
1da177e4
LT
62
63extern struct neigh_table nd_tbl;
64
65struct nd_msg {
66 struct icmp6hdr icmph;
67 struct in6_addr target;
68 __u8 opt[0];
69};
70
71struct rs_msg {
72 struct icmp6hdr icmph;
73 __u8 opt[0];
74};
75
76struct ra_msg {
77 struct icmp6hdr icmph;
e69a4adc
AV
78 __be32 reachable_time;
79 __be32 retrans_timer;
1da177e4
LT
80};
81
093d04d4
DJ
82struct rd_msg {
83 struct icmp6hdr icmph;
84 struct in6_addr target;
85 struct in6_addr dest;
86 __u8 opt[0];
87};
88
1da177e4
LT
89struct nd_opt_hdr {
90 __u8 nd_opt_type;
91 __u8 nd_opt_len;
bc10502d 92} __packed;
1da177e4 93
30f2a5f3
DM
94/* ND options */
95struct ndisc_options {
96 struct nd_opt_hdr *nd_opt_array[__ND_OPT_ARRAY_MAX];
97#ifdef CONFIG_IPV6_ROUTE_INFO
98 struct nd_opt_hdr *nd_opts_ri;
99 struct nd_opt_hdr *nd_opts_ri_end;
100#endif
101 struct nd_opt_hdr *nd_useropts;
102 struct nd_opt_hdr *nd_useropts_end;
103};
104
105#define nd_opts_src_lladdr nd_opt_array[ND_OPT_SOURCE_LL_ADDR]
106#define nd_opts_tgt_lladdr nd_opt_array[ND_OPT_TARGET_LL_ADDR]
107#define nd_opts_pi nd_opt_array[ND_OPT_PREFIX_INFO]
108#define nd_opts_pi_end nd_opt_array[__ND_OPT_PREFIX_INFO_END]
109#define nd_opts_rh nd_opt_array[ND_OPT_REDIRECT_HDR]
110#define nd_opts_mtu nd_opt_array[ND_OPT_MTU]
111
112#define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
113
f997c55c
AA
114struct ndisc_options *ndisc_parse_options(const struct net_device *dev,
115 u8 *opt, int opt_len,
3cc818a2 116 struct ndisc_options *ndopts);
30f2a5f3 117
f997c55c
AA
118#define NDISC_OPS_REDIRECT_DATA_SPACE 2
119
120/*
121 * This structure defines the hooks for IPv6 neighbour discovery.
122 * The following hooks can be defined; unless noted otherwise, they are
123 * optional and can be filled with a null pointer.
124 *
125 * int (*is_useropt)(u8 nd_opt_type):
126 * This function is called when IPv6 decide RA userspace options. if
127 * this function returns 1 then the option given by nd_opt_type will
128 * be handled as userspace option additional to the IPv6 options.
129 *
130 * int (*parse_options)(const struct net_device *dev,
131 * struct nd_opt_hdr *nd_opt,
132 * struct ndisc_options *ndopts):
133 * This function is called while parsing ndisc ops and put each position
134 * as pointer into ndopts. If this function return unequal 0, then this
135 * function took care about the ndisc option, if 0 then the IPv6 ndisc
136 * option parser will take care about that option.
137 *
138 * void (*update)(const struct net_device *dev, struct neighbour *n,
139 * u32 flags, u8 icmp6_type,
140 * const struct ndisc_options *ndopts):
141 * This function is called when IPv6 ndisc updates the neighbour cache
142 * entry. Additional options which can be updated may be previously
143 * parsed by parse_opts callback and accessible over ndopts parameter.
144 *
145 * int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
146 * struct neighbour *neigh, u8 *ha_buf,
147 * u8 **ha):
148 * This function is called when the necessary option space will be
149 * calculated before allocating a skb. The parameters neigh, ha_buf
150 * abd ha are available on NDISC_REDIRECT messages only.
151 *
152 * void (*fill_addr_option)(const struct net_device *dev,
153 * struct sk_buff *skb, u8 icmp6_type,
154 * const u8 *ha):
155 * This function is called when the skb will finally fill the option
156 * fields inside skb. NOTE: this callback should fill the option
157 * fields to the skb which are previously indicated by opt_space
158 * parameter. That means the decision to add such option should
159 * not lost between these two callbacks, e.g. protected by interface
160 * up state.
161 *
162 * void (*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
163 * const struct prefix_info *pinfo,
164 * struct inet6_dev *in6_dev,
165 * struct in6_addr *addr,
166 * int addr_type, u32 addr_flags,
167 * bool sllao, bool tokenized,
168 * __u32 valid_lft, u32 prefered_lft,
169 * bool dev_addr_generated):
170 * This function is called when a RA messages is received with valid
171 * PIO option fields and an IPv6 address will be added to the interface
172 * for autoconfiguration. The parameter dev_addr_generated reports about
173 * if the address was based on dev->dev_addr or not. This can be used
174 * to add a second address if link-layer operates with two link layer
175 * addresses. E.g. 802.15.4 6LoWPAN.
176 */
177struct ndisc_ops {
178 int (*is_useropt)(u8 nd_opt_type);
179 int (*parse_options)(const struct net_device *dev,
180 struct nd_opt_hdr *nd_opt,
181 struct ndisc_options *ndopts);
182 void (*update)(const struct net_device *dev, struct neighbour *n,
183 u32 flags, u8 icmp6_type,
184 const struct ndisc_options *ndopts);
185 int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
186 struct neighbour *neigh, u8 *ha_buf,
187 u8 **ha);
188 void (*fill_addr_option)(const struct net_device *dev,
189 struct sk_buff *skb, u8 icmp6_type,
190 const u8 *ha);
191 void (*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
192 const struct prefix_info *pinfo,
193 struct inet6_dev *in6_dev,
194 struct in6_addr *addr,
195 int addr_type, u32 addr_flags,
196 bool sllao, bool tokenized,
197 __u32 valid_lft, u32 prefered_lft,
198 bool dev_addr_generated);
199};
200
201#if IS_ENABLED(CONFIG_IPV6)
202static inline int ndisc_ops_is_useropt(const struct net_device *dev,
203 u8 nd_opt_type)
204{
205 if (dev->ndisc_ops && dev->ndisc_ops->is_useropt)
206 return dev->ndisc_ops->is_useropt(nd_opt_type);
207 else
208 return 0;
209}
210
211static inline int ndisc_ops_parse_options(const struct net_device *dev,
212 struct nd_opt_hdr *nd_opt,
213 struct ndisc_options *ndopts)
214{
215 if (dev->ndisc_ops && dev->ndisc_ops->parse_options)
216 return dev->ndisc_ops->parse_options(dev, nd_opt, ndopts);
217 else
218 return 0;
219}
220
221static inline void ndisc_ops_update(const struct net_device *dev,
222 struct neighbour *n, u32 flags,
223 u8 icmp6_type,
224 const struct ndisc_options *ndopts)
225{
226 if (dev->ndisc_ops && dev->ndisc_ops->update)
227 dev->ndisc_ops->update(dev, n, flags, icmp6_type, ndopts);
228}
229
230static inline int ndisc_ops_opt_addr_space(const struct net_device *dev,
231 u8 icmp6_type)
232{
233 if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space &&
234 icmp6_type != NDISC_REDIRECT)
235 return dev->ndisc_ops->opt_addr_space(dev, icmp6_type, NULL,
236 NULL, NULL);
237 else
238 return 0;
239}
240
241static inline int ndisc_ops_redirect_opt_addr_space(const struct net_device *dev,
242 struct neighbour *neigh,
243 u8 *ha_buf, u8 **ha)
244{
245 if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space)
246 return dev->ndisc_ops->opt_addr_space(dev, NDISC_REDIRECT,
247 neigh, ha_buf, ha);
248 else
249 return 0;
250}
251
252static inline void ndisc_ops_fill_addr_option(const struct net_device *dev,
253 struct sk_buff *skb,
254 u8 icmp6_type)
255{
256 if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option &&
257 icmp6_type != NDISC_REDIRECT)
258 dev->ndisc_ops->fill_addr_option(dev, skb, icmp6_type, NULL);
259}
260
261static inline void ndisc_ops_fill_redirect_addr_option(const struct net_device *dev,
262 struct sk_buff *skb,
263 const u8 *ha)
264{
265 if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option)
266 dev->ndisc_ops->fill_addr_option(dev, skb, NDISC_REDIRECT, ha);
267}
268
269static inline void ndisc_ops_prefix_rcv_add_addr(struct net *net,
270 struct net_device *dev,
271 const struct prefix_info *pinfo,
272 struct inet6_dev *in6_dev,
273 struct in6_addr *addr,
274 int addr_type, u32 addr_flags,
275 bool sllao, bool tokenized,
276 __u32 valid_lft,
277 u32 prefered_lft,
278 bool dev_addr_generated)
279{
280 if (dev->ndisc_ops && dev->ndisc_ops->prefix_rcv_add_addr)
281 dev->ndisc_ops->prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
282 addr, addr_type,
283 addr_flags, sllao,
284 tokenized, valid_lft,
285 prefered_lft,
286 dev_addr_generated);
287}
288#endif
289
30f2a5f3
DM
290/*
291 * Return the padding between the option length and the start of the
292 * link addr. Currently only IP-over-InfiniBand needs this, although
293 * if RFC 3831 IPv6-over-Fibre Channel is ever implemented it may
294 * also need a pad of 2.
295 */
d9d10a30 296static inline int ndisc_addr_option_pad(unsigned short type)
30f2a5f3
DM
297{
298 switch (type) {
299 case ARPHRD_INFINIBAND: return 2;
300 default: return 0;
301 }
302}
303
1e82f961
AA
304static inline int __ndisc_opt_addr_space(unsigned char addr_len, int pad)
305{
306 return NDISC_OPT_SPACE(addr_len + pad);
307}
308
f997c55c
AA
309#if IS_ENABLED(CONFIG_IPV6)
310static inline int ndisc_opt_addr_space(struct net_device *dev, u8 icmp6_type)
311{
312 return __ndisc_opt_addr_space(dev->addr_len,
313 ndisc_addr_option_pad(dev->type)) +
314 ndisc_ops_opt_addr_space(dev, icmp6_type);
315}
316
317static inline int ndisc_redirect_opt_addr_space(struct net_device *dev,
318 struct neighbour *neigh,
319 u8 *ops_data_buf,
320 u8 **ops_data)
c558e9fc 321{
1e82f961 322 return __ndisc_opt_addr_space(dev->addr_len,
f997c55c
AA
323 ndisc_addr_option_pad(dev->type)) +
324 ndisc_ops_redirect_opt_addr_space(dev, neigh, ops_data_buf,
325 ops_data);
c558e9fc 326}
f997c55c 327#endif
c558e9fc 328
4f36ce84
AA
329static inline u8 *__ndisc_opt_addr_data(struct nd_opt_hdr *p,
330 unsigned char addr_len, int prepad)
30f2a5f3
DM
331{
332 u8 *lladdr = (u8 *)(p + 1);
333 int lladdrlen = p->nd_opt_len << 3;
4f36ce84 334 if (lladdrlen != __ndisc_opt_addr_space(addr_len, prepad))
30f2a5f3
DM
335 return NULL;
336 return lladdr + prepad;
337}
338
4f36ce84
AA
339static inline u8 *ndisc_opt_addr_data(struct nd_opt_hdr *p,
340 struct net_device *dev)
341{
342 return __ndisc_opt_addr_data(p, dev->addr_len,
343 ndisc_addr_option_pad(dev->type));
344}
345
2c2aba6c
DM
346static inline u32 ndisc_hashfn(const void *pkey, const struct net_device *dev, __u32 *hash_rnd)
347{
348 const u32 *p32 = pkey;
349
b14f243a 350 return (((p32[0] ^ hash32_ptr(dev)) * hash_rnd[0]) +
2c2aba6c
DM
351 (p32[1] * hash_rnd[1]) +
352 (p32[2] * hash_rnd[2]) +
353 (p32[3] * hash_rnd[3]));
354}
1da177e4 355
ac3175fe 356static inline struct neighbour *__ipv6_neigh_lookup_noref(struct net_device *dev, const void *pkey)
f83c7790 357{
60395a20 358 return ___neigh_lookup_noref(&nd_tbl, neigh_key_eq128, ndisc_hashfn, pkey, dev);
ac3175fe
YH
359}
360
361static inline struct neighbour *__ipv6_neigh_lookup(struct net_device *dev, const void *pkey)
362{
363 struct neighbour *n;
364
365 rcu_read_lock_bh();
366 n = __ipv6_neigh_lookup_noref(dev, pkey);
367 if (n && !atomic_inc_not_zero(&n->refcnt))
368 n = NULL;
f83c7790
DM
369 rcu_read_unlock_bh();
370
371 return n;
372}
373
3cc818a2
JP
374int ndisc_init(void);
375int ndisc_late_init(void);
1da177e4 376
3cc818a2
JP
377void ndisc_late_cleanup(void);
378void ndisc_cleanup(void);
1da177e4 379
3cc818a2 380int ndisc_rcv(struct sk_buff *skb);
1da177e4 381
38cf595b 382void ndisc_send_ns(struct net_device *dev, const struct in6_addr *solicit,
304d888b 383 const struct in6_addr *daddr, const struct in6_addr *saddr);
1da177e4 384
3cc818a2
JP
385void ndisc_send_rs(struct net_device *dev,
386 const struct in6_addr *saddr, const struct in6_addr *daddr);
38cf595b 387void ndisc_send_na(struct net_device *dev, const struct in6_addr *daddr,
3cc818a2
JP
388 const struct in6_addr *solicited_addr,
389 bool router, bool solicited, bool override, bool inc_opt);
1da177e4 390
3cc818a2 391void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target);
1da177e4 392
3cc818a2
JP
393int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev,
394 int dir);
1da177e4 395
f997c55c
AA
396void ndisc_update(const struct net_device *dev, struct neighbour *neigh,
397 const u8 *lladdr, u8 new, u32 flags, u8 icmp6_type,
398 struct ndisc_options *ndopts);
1da177e4
LT
399
400/*
401 * IGMP
402 */
3cc818a2 403int igmp6_init(void);
1da177e4 404
3cc818a2 405void igmp6_cleanup(void);
1da177e4 406
3cc818a2 407int igmp6_event_query(struct sk_buff *skb);
1da177e4 408
3cc818a2 409int igmp6_event_report(struct sk_buff *skb);
1da177e4 410
1da177e4
LT
411
412#ifdef CONFIG_SYSCTL
3cc818a2
JP
413int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write,
414 void __user *buffer, size_t *lenp, loff_t *ppos);
fedaf4ff 415int ndisc_ifinfo_sysctl_strategy(struct ctl_table *ctl,
f221e726 416 void __user *oldval, size_t __user *oldlenp,
0686caa3 417 void __user *newval, size_t newlen);
1da177e4
LT
418#endif
419
3cc818a2 420void inet6_ifinfo_notify(int event, struct inet6_dev *idev);
1da177e4 421
1da177e4 422#endif
This page took 0.885172 seconds and 5 git commands to generate.