Merge tag 'upstream-4.9-rc2' of git://git.infradead.org/linux-ubifs
[cascardo/linux.git] / include / net / ndisc.h
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
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
23 /*
24  *      ndisc options
25  */
26
27 enum {
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 */
34         __ND_OPT_ARRAY_MAX,
35         ND_OPT_ROUTE_INFO = 24,         /* RFC4191 */
36         ND_OPT_RDNSS = 25,              /* RFC5006 */
37         ND_OPT_DNSSL = 31,              /* RFC6106 */
38         ND_OPT_6CO = 34,                /* RFC6775 */
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
47 #include <linux/compiler.h>
48 #include <linux/icmpv6.h>
49 #include <linux/in6.h>
50 #include <linux/types.h>
51 #include <linux/if_arp.h>
52 #include <linux/netdevice.h>
53 #include <linux/hash.h>
54
55 #include <net/neighbour.h>
56
57 /* Set to 3 to get tracing... */
58 #define ND_DEBUG 1
59
60 #define ND_PRINTK(val, level, fmt, ...)                         \
61 do {                                                            \
62         if (val <= ND_DEBUG)                                    \
63                 net_##level##_ratelimited(fmt, ##__VA_ARGS__);  \
64 } while (0)
65
66 struct ctl_table;
67 struct inet6_dev;
68 struct net_device;
69 struct net_proto_family;
70 struct sk_buff;
71 struct prefix_info;
72
73 extern struct neigh_table nd_tbl;
74
75 struct nd_msg {
76         struct icmp6hdr icmph;
77         struct in6_addr target;
78         __u8            opt[0];
79 };
80
81 struct rs_msg {
82         struct icmp6hdr icmph;
83         __u8            opt[0];
84 };
85
86 struct ra_msg {
87         struct icmp6hdr         icmph;
88         __be32                  reachable_time;
89         __be32                  retrans_timer;
90 };
91
92 struct rd_msg {
93         struct icmp6hdr icmph;
94         struct in6_addr target;
95         struct in6_addr dest;
96         __u8            opt[0];
97 };
98
99 struct nd_opt_hdr {
100         __u8            nd_opt_type;
101         __u8            nd_opt_len;
102 } __packed;
103
104 /* ND options */
105 struct 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;
113 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
114         struct nd_opt_hdr *nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR + 1];
115 #endif
116 };
117
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]
126
127 #define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
128
129 struct ndisc_options *ndisc_parse_options(const struct net_device *dev,
130                                           u8 *opt, int opt_len,
131                                           struct ndisc_options *ndopts);
132
133 void __ndisc_fill_addr_option(struct sk_buff *skb, int type, void *data,
134                               int data_len, int pad);
135
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  */
195 struct 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)
220 static 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
229 static 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
239 static 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
248 static 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
259 static 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
270 static 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
279 static 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
287 static 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
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  */
314 static inline int ndisc_addr_option_pad(unsigned short type)
315 {
316         switch (type) {
317         case ARPHRD_INFINIBAND: return 2;
318         default:                return 0;
319         }
320 }
321
322 static inline int __ndisc_opt_addr_space(unsigned char addr_len, int pad)
323 {
324         return NDISC_OPT_SPACE(addr_len + pad);
325 }
326
327 #if IS_ENABLED(CONFIG_IPV6)
328 static 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
335 static 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
347 static inline u8 *__ndisc_opt_addr_data(struct nd_opt_hdr *p,
348                                         unsigned char addr_len, int prepad)
349 {
350         u8 *lladdr = (u8 *)(p + 1);
351         int lladdrlen = p->nd_opt_len << 3;
352         if (lladdrlen != __ndisc_opt_addr_space(addr_len, prepad))
353                 return NULL;
354         return lladdr + prepad;
355 }
356
357 static 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
364 static inline u32 ndisc_hashfn(const void *pkey, const struct net_device *dev, __u32 *hash_rnd)
365 {
366         const u32 *p32 = pkey;
367
368         return (((p32[0] ^ hash32_ptr(dev)) * hash_rnd[0]) +
369                 (p32[1] * hash_rnd[1]) +
370                 (p32[2] * hash_rnd[2]) +
371                 (p32[3] * hash_rnd[3]));
372 }
373
374 static inline struct neighbour *__ipv6_neigh_lookup_noref(struct net_device *dev, const void *pkey)
375 {
376         return ___neigh_lookup_noref(&nd_tbl, neigh_key_eq128, ndisc_hashfn, pkey, dev);
377 }
378
379 static 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;
387         rcu_read_unlock_bh();
388
389         return n;
390 }
391
392 int ndisc_init(void);
393 int ndisc_late_init(void);
394
395 void ndisc_late_cleanup(void);
396 void ndisc_cleanup(void);
397
398 int ndisc_rcv(struct sk_buff *skb);
399
400 void ndisc_send_ns(struct net_device *dev, const struct in6_addr *solicit,
401                    const struct in6_addr *daddr, const struct in6_addr *saddr);
402
403 void ndisc_send_rs(struct net_device *dev,
404                    const struct in6_addr *saddr, const struct in6_addr *daddr);
405 void ndisc_send_na(struct net_device *dev, const struct in6_addr *daddr,
406                    const struct in6_addr *solicited_addr,
407                    bool router, bool solicited, bool override, bool inc_opt);
408
409 void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target);
410
411 int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev,
412                  int dir);
413
414 void 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);
417
418 /*
419  *      IGMP
420  */
421 int igmp6_init(void);
422
423 void igmp6_cleanup(void);
424
425 int igmp6_event_query(struct sk_buff *skb);
426
427 int igmp6_event_report(struct sk_buff *skb);
428
429
430 #ifdef CONFIG_SYSCTL
431 int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write,
432                                void __user *buffer, size_t *lenp, loff_t *ppos);
433 int ndisc_ifinfo_sysctl_strategy(struct ctl_table *ctl,
434                                  void __user *oldval, size_t __user *oldlenp,
435                                  void __user *newval, size_t newlen);
436 #endif
437
438 void inet6_ifinfo_notify(int event, struct inet6_dev *idev);
439
440 #endif