5 * ICMP codes for neighbour discovery messages
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
15 * Router type: cross-layer information from link-layer to
16 * IPv6 layer reported by certain link types (e.g., RFC4214).
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 */
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_NONCE = 14, /* RFC7527 */
36 ND_OPT_ROUTE_INFO = 24, /* RFC4191 */
37 ND_OPT_RDNSS = 25, /* RFC5006 */
38 ND_OPT_DNSSL = 31, /* RFC6106 */
39 ND_OPT_6CO = 34, /* RFC6775 */
43 #define MAX_RTR_SOLICITATION_DELAY HZ
45 #define ND_REACHABLE_TIME (30*HZ)
46 #define ND_RETRANS_TIMER HZ
48 #include <linux/compiler.h>
49 #include <linux/icmpv6.h>
50 #include <linux/in6.h>
51 #include <linux/types.h>
52 #include <linux/if_arp.h>
53 #include <linux/netdevice.h>
54 #include <linux/hash.h>
56 #include <net/neighbour.h>
58 /* Set to 3 to get tracing... */
61 #define ND_PRINTK(val, level, fmt, ...) \
63 if (val <= ND_DEBUG) \
64 net_##level##_ratelimited(fmt, ##__VA_ARGS__); \
70 struct net_proto_family;
74 extern struct neigh_table nd_tbl;
77 struct icmp6hdr icmph;
78 struct in6_addr target;
83 struct icmp6hdr icmph;
88 struct icmp6hdr icmph;
89 __be32 reachable_time;
94 struct icmp6hdr icmph;
95 struct in6_addr target;
106 struct ndisc_options {
107 struct nd_opt_hdr *nd_opt_array[__ND_OPT_ARRAY_MAX];
108 #ifdef CONFIG_IPV6_ROUTE_INFO
109 struct nd_opt_hdr *nd_opts_ri;
110 struct nd_opt_hdr *nd_opts_ri_end;
112 struct nd_opt_hdr *nd_useropts;
113 struct nd_opt_hdr *nd_useropts_end;
114 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
115 struct nd_opt_hdr *nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR + 1];
119 #define nd_opts_src_lladdr nd_opt_array[ND_OPT_SOURCE_LL_ADDR]
120 #define nd_opts_tgt_lladdr nd_opt_array[ND_OPT_TARGET_LL_ADDR]
121 #define nd_opts_pi nd_opt_array[ND_OPT_PREFIX_INFO]
122 #define nd_opts_pi_end nd_opt_array[__ND_OPT_PREFIX_INFO_END]
123 #define nd_opts_rh nd_opt_array[ND_OPT_REDIRECT_HDR]
124 #define nd_opts_mtu nd_opt_array[ND_OPT_MTU]
125 #define nd_opts_nonce nd_opt_array[ND_OPT_NONCE]
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]
129 #define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
131 struct ndisc_options *ndisc_parse_options(const struct net_device *dev,
132 u8 *opt, int opt_len,
133 struct ndisc_options *ndopts);
135 void __ndisc_fill_addr_option(struct sk_buff *skb, int type, void *data,
136 int data_len, int pad);
138 #define NDISC_OPS_REDIRECT_DATA_SPACE 2
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.
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.
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.
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.
165 * int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
166 * struct neighbour *neigh, u8 *ha_buf,
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.
172 * void (*fill_addr_option)(const struct net_device *dev,
173 * struct sk_buff *skb, u8 icmp6_type,
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
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.
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,
208 void (*fill_addr_option)(const struct net_device *dev,
209 struct sk_buff *skb, u8 icmp6_type,
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);
221 #if IS_ENABLED(CONFIG_IPV6)
222 static inline int ndisc_ops_is_useropt(const struct net_device *dev,
225 if (dev->ndisc_ops && dev->ndisc_ops->is_useropt)
226 return dev->ndisc_ops->is_useropt(nd_opt_type);
231 static inline int ndisc_ops_parse_options(const struct net_device *dev,
232 struct nd_opt_hdr *nd_opt,
233 struct ndisc_options *ndopts)
235 if (dev->ndisc_ops && dev->ndisc_ops->parse_options)
236 return dev->ndisc_ops->parse_options(dev, nd_opt, ndopts);
241 static inline void ndisc_ops_update(const struct net_device *dev,
242 struct neighbour *n, u32 flags,
244 const struct ndisc_options *ndopts)
246 if (dev->ndisc_ops && dev->ndisc_ops->update)
247 dev->ndisc_ops->update(dev, n, flags, icmp6_type, ndopts);
250 static inline int ndisc_ops_opt_addr_space(const struct net_device *dev,
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,
261 static inline int ndisc_ops_redirect_opt_addr_space(const struct net_device *dev,
262 struct neighbour *neigh,
265 if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space)
266 return dev->ndisc_ops->opt_addr_space(dev, NDISC_REDIRECT,
272 static inline void ndisc_ops_fill_addr_option(const struct net_device *dev,
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);
281 static inline void ndisc_ops_fill_redirect_addr_option(const struct net_device *dev,
285 if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option)
286 dev->ndisc_ops->fill_addr_option(dev, skb, NDISC_REDIRECT, ha);
289 static 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,
298 bool dev_addr_generated)
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,
304 tokenized, valid_lft,
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.
316 static inline int ndisc_addr_option_pad(unsigned short type)
319 case ARPHRD_INFINIBAND: return 2;
324 static inline int __ndisc_opt_addr_space(unsigned char addr_len, int pad)
326 return NDISC_OPT_SPACE(addr_len + pad);
329 #if IS_ENABLED(CONFIG_IPV6)
330 static inline int ndisc_opt_addr_space(struct net_device *dev, u8 icmp6_type)
332 return __ndisc_opt_addr_space(dev->addr_len,
333 ndisc_addr_option_pad(dev->type)) +
334 ndisc_ops_opt_addr_space(dev, icmp6_type);
337 static inline int ndisc_redirect_opt_addr_space(struct net_device *dev,
338 struct neighbour *neigh,
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,
349 static inline u8 *__ndisc_opt_addr_data(struct nd_opt_hdr *p,
350 unsigned char addr_len, int prepad)
352 u8 *lladdr = (u8 *)(p + 1);
353 int lladdrlen = p->nd_opt_len << 3;
354 if (lladdrlen != __ndisc_opt_addr_space(addr_len, prepad))
356 return lladdr + prepad;
359 static inline u8 *ndisc_opt_addr_data(struct nd_opt_hdr *p,
360 struct net_device *dev)
362 return __ndisc_opt_addr_data(p, dev->addr_len,
363 ndisc_addr_option_pad(dev->type));
366 static inline u32 ndisc_hashfn(const void *pkey, const struct net_device *dev, __u32 *hash_rnd)
368 const u32 *p32 = pkey;
370 return (((p32[0] ^ hash32_ptr(dev)) * hash_rnd[0]) +
371 (p32[1] * hash_rnd[1]) +
372 (p32[2] * hash_rnd[2]) +
373 (p32[3] * hash_rnd[3]));
376 static inline struct neighbour *__ipv6_neigh_lookup_noref(struct net_device *dev, const void *pkey)
378 return ___neigh_lookup_noref(&nd_tbl, neigh_key_eq128, ndisc_hashfn, pkey, dev);
381 static inline struct neighbour *__ipv6_neigh_lookup(struct net_device *dev, const void *pkey)
386 n = __ipv6_neigh_lookup_noref(dev, pkey);
387 if (n && !atomic_inc_not_zero(&n->refcnt))
389 rcu_read_unlock_bh();
394 static inline void __ipv6_confirm_neigh(struct net_device *dev,
400 n = __ipv6_neigh_lookup_noref(dev, pkey);
402 unsigned long now = jiffies;
404 /* avoid dirtying neighbour */
405 if (n->confirmed != now)
408 rcu_read_unlock_bh();
411 int ndisc_init(void);
412 int ndisc_late_init(void);
414 void ndisc_late_cleanup(void);
415 void ndisc_cleanup(void);
417 int ndisc_rcv(struct sk_buff *skb);
419 void ndisc_send_ns(struct net_device *dev, const struct in6_addr *solicit,
420 const struct in6_addr *daddr, const struct in6_addr *saddr,
423 void ndisc_send_rs(struct net_device *dev,
424 const struct in6_addr *saddr, const struct in6_addr *daddr);
425 void ndisc_send_na(struct net_device *dev, const struct in6_addr *daddr,
426 const struct in6_addr *solicited_addr,
427 bool router, bool solicited, bool override, bool inc_opt);
429 void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target);
431 int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev,
434 void ndisc_update(const struct net_device *dev, struct neighbour *neigh,
435 const u8 *lladdr, u8 new, u32 flags, u8 icmp6_type,
436 struct ndisc_options *ndopts);
441 int igmp6_init(void);
443 void igmp6_cleanup(void);
445 int igmp6_event_query(struct sk_buff *skb);
447 int igmp6_event_report(struct sk_buff *skb);
451 int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write,
452 void __user *buffer, size_t *lenp, loff_t *ppos);
453 int ndisc_ifinfo_sysctl_strategy(struct ctl_table *ctl,
454 void __user *oldval, size_t __user *oldlenp,
455 void __user *newval, size_t newlen);
458 void inet6_ifinfo_notify(int event, struct inet6_dev *idev);