1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <net/ipv6_stubs.h>
8 * ICMP codes for neighbour discovery messages
11 #define NDISC_ROUTER_SOLICITATION 133
12 #define NDISC_ROUTER_ADVERTISEMENT 134
13 #define NDISC_NEIGHBOUR_SOLICITATION 135
14 #define NDISC_NEIGHBOUR_ADVERTISEMENT 136
15 #define NDISC_REDIRECT 137
18 * Router type: cross-layer information from link-layer to
19 * IPv6 layer reported by certain link types (e.g., RFC4214).
21 #define NDISC_NODETYPE_UNSPEC 0 /* unspecified (default) */
22 #define NDISC_NODETYPE_HOST 1 /* host or unauthorized router */
23 #define NDISC_NODETYPE_NODEFAULT 2 /* non-default router */
24 #define NDISC_NODETYPE_DEFAULT 3 /* default router */
31 __ND_OPT_PREFIX_INFO_END
= 0,
32 ND_OPT_SOURCE_LL_ADDR
= 1, /* RFC2461 */
33 ND_OPT_TARGET_LL_ADDR
= 2, /* RFC2461 */
34 ND_OPT_PREFIX_INFO
= 3, /* RFC2461 */
35 ND_OPT_REDIRECT_HDR
= 4, /* RFC2461 */
36 ND_OPT_MTU
= 5, /* RFC2461 */
37 ND_OPT_NONCE
= 14, /* RFC7527 */
39 ND_OPT_ROUTE_INFO
= 24, /* RFC4191 */
40 ND_OPT_RDNSS
= 25, /* RFC5006 */
41 ND_OPT_DNSSL
= 31, /* RFC6106 */
42 ND_OPT_6CO
= 34, /* RFC6775 */
46 #define MAX_RTR_SOLICITATION_DELAY HZ
48 #define ND_REACHABLE_TIME (30*HZ)
49 #define ND_RETRANS_TIMER HZ
51 #include <linux/compiler.h>
52 #include <linux/icmpv6.h>
53 #include <linux/in6.h>
54 #include <linux/types.h>
55 #include <linux/if_arp.h>
56 #include <linux/netdevice.h>
57 #include <linux/hash.h>
59 #include <net/neighbour.h>
61 /* Set to 3 to get tracing... */
64 #define ND_PRINTK(val, level, fmt, ...) \
66 if (val <= ND_DEBUG) \
67 net_##level##_ratelimited(fmt, ##__VA_ARGS__); \
73 struct net_proto_family
;
77 extern struct neigh_table nd_tbl
;
80 struct icmp6hdr icmph
;
81 struct in6_addr target
;
86 struct icmp6hdr icmph
;
91 struct icmp6hdr icmph
;
92 __be32 reachable_time
;
97 struct icmp6hdr icmph
;
98 struct in6_addr target
;
109 struct ndisc_options
{
110 struct nd_opt_hdr
*nd_opt_array
[__ND_OPT_ARRAY_MAX
];
111 #ifdef CONFIG_IPV6_ROUTE_INFO
112 struct nd_opt_hdr
*nd_opts_ri
;
113 struct nd_opt_hdr
*nd_opts_ri_end
;
115 struct nd_opt_hdr
*nd_useropts
;
116 struct nd_opt_hdr
*nd_useropts_end
;
117 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
118 struct nd_opt_hdr
*nd_802154_opt_array
[ND_OPT_TARGET_LL_ADDR
+ 1];
122 #define nd_opts_src_lladdr nd_opt_array[ND_OPT_SOURCE_LL_ADDR]
123 #define nd_opts_tgt_lladdr nd_opt_array[ND_OPT_TARGET_LL_ADDR]
124 #define nd_opts_pi nd_opt_array[ND_OPT_PREFIX_INFO]
125 #define nd_opts_pi_end nd_opt_array[__ND_OPT_PREFIX_INFO_END]
126 #define nd_opts_rh nd_opt_array[ND_OPT_REDIRECT_HDR]
127 #define nd_opts_mtu nd_opt_array[ND_OPT_MTU]
128 #define nd_opts_nonce nd_opt_array[ND_OPT_NONCE]
129 #define nd_802154_opts_src_lladdr nd_802154_opt_array[ND_OPT_SOURCE_LL_ADDR]
130 #define nd_802154_opts_tgt_lladdr nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR]
132 #define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
134 struct ndisc_options
*ndisc_parse_options(const struct net_device
*dev
,
135 u8
*opt
, int opt_len
,
136 struct ndisc_options
*ndopts
);
138 void __ndisc_fill_addr_option(struct sk_buff
*skb
, int type
, void *data
,
139 int data_len
, int pad
);
141 #define NDISC_OPS_REDIRECT_DATA_SPACE 2
144 * This structure defines the hooks for IPv6 neighbour discovery.
145 * The following hooks can be defined; unless noted otherwise, they are
146 * optional and can be filled with a null pointer.
148 * int (*is_useropt)(u8 nd_opt_type):
149 * This function is called when IPv6 decide RA userspace options. if
150 * this function returns 1 then the option given by nd_opt_type will
151 * be handled as userspace option additional to the IPv6 options.
153 * int (*parse_options)(const struct net_device *dev,
154 * struct nd_opt_hdr *nd_opt,
155 * struct ndisc_options *ndopts):
156 * This function is called while parsing ndisc ops and put each position
157 * as pointer into ndopts. If this function return unequal 0, then this
158 * function took care about the ndisc option, if 0 then the IPv6 ndisc
159 * option parser will take care about that option.
161 * void (*update)(const struct net_device *dev, struct neighbour *n,
162 * u32 flags, u8 icmp6_type,
163 * const struct ndisc_options *ndopts):
164 * This function is called when IPv6 ndisc updates the neighbour cache
165 * entry. Additional options which can be updated may be previously
166 * parsed by parse_opts callback and accessible over ndopts parameter.
168 * int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type,
169 * struct neighbour *neigh, u8 *ha_buf,
171 * This function is called when the necessary option space will be
172 * calculated before allocating a skb. The parameters neigh, ha_buf
173 * abd ha are available on NDISC_REDIRECT messages only.
175 * void (*fill_addr_option)(const struct net_device *dev,
176 * struct sk_buff *skb, u8 icmp6_type,
178 * This function is called when the skb will finally fill the option
179 * fields inside skb. NOTE: this callback should fill the option
180 * fields to the skb which are previously indicated by opt_space
181 * parameter. That means the decision to add such option should
182 * not lost between these two callbacks, e.g. protected by interface
185 * void (*prefix_rcv_add_addr)(struct net *net, struct net_device *dev,
186 * const struct prefix_info *pinfo,
187 * struct inet6_dev *in6_dev,
188 * struct in6_addr *addr,
189 * int addr_type, u32 addr_flags,
190 * bool sllao, bool tokenized,
191 * __u32 valid_lft, u32 prefered_lft,
192 * bool dev_addr_generated):
193 * This function is called when a RA messages is received with valid
194 * PIO option fields and an IPv6 address will be added to the interface
195 * for autoconfiguration. The parameter dev_addr_generated reports about
196 * if the address was based on dev->dev_addr or not. This can be used
197 * to add a second address if link-layer operates with two link layer
198 * addresses. E.g. 802.15.4 6LoWPAN.
201 int (*is_useropt
)(u8 nd_opt_type
);
202 int (*parse_options
)(const struct net_device
*dev
,
203 struct nd_opt_hdr
*nd_opt
,
204 struct ndisc_options
*ndopts
);
205 void (*update
)(const struct net_device
*dev
, struct neighbour
*n
,
206 u32 flags
, u8 icmp6_type
,
207 const struct ndisc_options
*ndopts
);
208 int (*opt_addr_space
)(const struct net_device
*dev
, u8 icmp6_type
,
209 struct neighbour
*neigh
, u8
*ha_buf
,
211 void (*fill_addr_option
)(const struct net_device
*dev
,
212 struct sk_buff
*skb
, u8 icmp6_type
,
214 void (*prefix_rcv_add_addr
)(struct net
*net
, struct net_device
*dev
,
215 const struct prefix_info
*pinfo
,
216 struct inet6_dev
*in6_dev
,
217 struct in6_addr
*addr
,
218 int addr_type
, u32 addr_flags
,
219 bool sllao
, bool tokenized
,
220 __u32 valid_lft
, u32 prefered_lft
,
221 bool dev_addr_generated
);
224 #if IS_ENABLED(CONFIG_IPV6)
225 static inline int ndisc_ops_is_useropt(const struct net_device
*dev
,
228 if (dev
->ndisc_ops
&& dev
->ndisc_ops
->is_useropt
)
229 return dev
->ndisc_ops
->is_useropt(nd_opt_type
);
234 static inline int ndisc_ops_parse_options(const struct net_device
*dev
,
235 struct nd_opt_hdr
*nd_opt
,
236 struct ndisc_options
*ndopts
)
238 if (dev
->ndisc_ops
&& dev
->ndisc_ops
->parse_options
)
239 return dev
->ndisc_ops
->parse_options(dev
, nd_opt
, ndopts
);
244 static inline void ndisc_ops_update(const struct net_device
*dev
,
245 struct neighbour
*n
, u32 flags
,
247 const struct ndisc_options
*ndopts
)
249 if (dev
->ndisc_ops
&& dev
->ndisc_ops
->update
)
250 dev
->ndisc_ops
->update(dev
, n
, flags
, icmp6_type
, ndopts
);
253 static inline int ndisc_ops_opt_addr_space(const struct net_device
*dev
,
256 if (dev
->ndisc_ops
&& dev
->ndisc_ops
->opt_addr_space
&&
257 icmp6_type
!= NDISC_REDIRECT
)
258 return dev
->ndisc_ops
->opt_addr_space(dev
, icmp6_type
, NULL
,
264 static inline int ndisc_ops_redirect_opt_addr_space(const struct net_device
*dev
,
265 struct neighbour
*neigh
,
268 if (dev
->ndisc_ops
&& dev
->ndisc_ops
->opt_addr_space
)
269 return dev
->ndisc_ops
->opt_addr_space(dev
, NDISC_REDIRECT
,
275 static inline void ndisc_ops_fill_addr_option(const struct net_device
*dev
,
279 if (dev
->ndisc_ops
&& dev
->ndisc_ops
->fill_addr_option
&&
280 icmp6_type
!= NDISC_REDIRECT
)
281 dev
->ndisc_ops
->fill_addr_option(dev
, skb
, icmp6_type
, NULL
);
284 static inline void ndisc_ops_fill_redirect_addr_option(const struct net_device
*dev
,
288 if (dev
->ndisc_ops
&& dev
->ndisc_ops
->fill_addr_option
)
289 dev
->ndisc_ops
->fill_addr_option(dev
, skb
, NDISC_REDIRECT
, ha
);
292 static inline void ndisc_ops_prefix_rcv_add_addr(struct net
*net
,
293 struct net_device
*dev
,
294 const struct prefix_info
*pinfo
,
295 struct inet6_dev
*in6_dev
,
296 struct in6_addr
*addr
,
297 int addr_type
, u32 addr_flags
,
298 bool sllao
, bool tokenized
,
301 bool dev_addr_generated
)
303 if (dev
->ndisc_ops
&& dev
->ndisc_ops
->prefix_rcv_add_addr
)
304 dev
->ndisc_ops
->prefix_rcv_add_addr(net
, dev
, pinfo
, in6_dev
,
307 tokenized
, valid_lft
,
314 * Return the padding between the option length and the start of the
315 * link addr. Currently only IP-over-InfiniBand needs this, although
316 * if RFC 3831 IPv6-over-Fibre Channel is ever implemented it may
317 * also need a pad of 2.
319 static inline int ndisc_addr_option_pad(unsigned short type
)
322 case ARPHRD_INFINIBAND
: return 2;
327 static inline int __ndisc_opt_addr_space(unsigned char addr_len
, int pad
)
329 return NDISC_OPT_SPACE(addr_len
+ pad
);
332 #if IS_ENABLED(CONFIG_IPV6)
333 static inline int ndisc_opt_addr_space(struct net_device
*dev
, u8 icmp6_type
)
335 return __ndisc_opt_addr_space(dev
->addr_len
,
336 ndisc_addr_option_pad(dev
->type
)) +
337 ndisc_ops_opt_addr_space(dev
, icmp6_type
);
340 static inline int ndisc_redirect_opt_addr_space(struct net_device
*dev
,
341 struct neighbour
*neigh
,
345 return __ndisc_opt_addr_space(dev
->addr_len
,
346 ndisc_addr_option_pad(dev
->type
)) +
347 ndisc_ops_redirect_opt_addr_space(dev
, neigh
, ops_data_buf
,
352 static inline u8
*__ndisc_opt_addr_data(struct nd_opt_hdr
*p
,
353 unsigned char addr_len
, int prepad
)
355 u8
*lladdr
= (u8
*)(p
+ 1);
356 int lladdrlen
= p
->nd_opt_len
<< 3;
357 if (lladdrlen
!= __ndisc_opt_addr_space(addr_len
, prepad
))
359 return lladdr
+ prepad
;
362 static inline u8
*ndisc_opt_addr_data(struct nd_opt_hdr
*p
,
363 struct net_device
*dev
)
365 return __ndisc_opt_addr_data(p
, dev
->addr_len
,
366 ndisc_addr_option_pad(dev
->type
));
369 static inline u32
ndisc_hashfn(const void *pkey
, const struct net_device
*dev
, __u32
*hash_rnd
)
371 const u32
*p32
= pkey
;
373 return (((p32
[0] ^ hash32_ptr(dev
)) * hash_rnd
[0]) +
374 (p32
[1] * hash_rnd
[1]) +
375 (p32
[2] * hash_rnd
[2]) +
376 (p32
[3] * hash_rnd
[3]));
379 static inline struct neighbour
*__ipv6_neigh_lookup_noref(struct net_device
*dev
, const void *pkey
)
381 return ___neigh_lookup_noref(&nd_tbl
, neigh_key_eq128
, ndisc_hashfn
, pkey
, dev
);
385 struct neighbour
*__ipv6_neigh_lookup_noref_stub(struct net_device
*dev
,
388 return ___neigh_lookup_noref(ipv6_stub
->nd_tbl
, neigh_key_eq128
,
389 ndisc_hashfn
, pkey
, dev
);
392 static inline struct neighbour
*__ipv6_neigh_lookup(struct net_device
*dev
, const void *pkey
)
397 n
= __ipv6_neigh_lookup_noref(dev
, pkey
);
398 if (n
&& !refcount_inc_not_zero(&n
->refcnt
))
400 rcu_read_unlock_bh();
405 static inline void __ipv6_confirm_neigh(struct net_device
*dev
,
411 n
= __ipv6_neigh_lookup_noref(dev
, pkey
);
413 unsigned long now
= jiffies
;
415 /* avoid dirtying neighbour */
416 if (n
->confirmed
!= now
)
419 rcu_read_unlock_bh();
422 static inline void __ipv6_confirm_neigh_stub(struct net_device
*dev
,
428 n
= __ipv6_neigh_lookup_noref_stub(dev
, pkey
);
430 unsigned long now
= jiffies
;
432 /* avoid dirtying neighbour */
433 if (n
->confirmed
!= now
)
436 rcu_read_unlock_bh();
439 /* uses ipv6_stub and is meant for use outside of IPv6 core */
440 static inline struct neighbour
*ip_neigh_gw6(struct net_device
*dev
,
443 struct neighbour
*neigh
;
445 neigh
= __ipv6_neigh_lookup_noref_stub(dev
, addr
);
446 if (unlikely(!neigh
))
447 neigh
= __neigh_create(ipv6_stub
->nd_tbl
, addr
, dev
, false);
452 int ndisc_init(void);
453 int ndisc_late_init(void);
455 void ndisc_late_cleanup(void);
456 void ndisc_cleanup(void);
458 int ndisc_rcv(struct sk_buff
*skb
);
460 void ndisc_send_ns(struct net_device
*dev
, const struct in6_addr
*solicit
,
461 const struct in6_addr
*daddr
, const struct in6_addr
*saddr
,
464 void ndisc_send_rs(struct net_device
*dev
,
465 const struct in6_addr
*saddr
, const struct in6_addr
*daddr
);
466 void ndisc_send_na(struct net_device
*dev
, const struct in6_addr
*daddr
,
467 const struct in6_addr
*solicited_addr
,
468 bool router
, bool solicited
, bool override
, bool inc_opt
);
470 void ndisc_send_redirect(struct sk_buff
*skb
, const struct in6_addr
*target
);
472 int ndisc_mc_map(const struct in6_addr
*addr
, char *buf
, struct net_device
*dev
,
475 void ndisc_update(const struct net_device
*dev
, struct neighbour
*neigh
,
476 const u8
*lladdr
, u8
new, u32 flags
, u8 icmp6_type
,
477 struct ndisc_options
*ndopts
);
482 int igmp6_init(void);
483 int igmp6_late_init(void);
485 void igmp6_cleanup(void);
486 void igmp6_late_cleanup(void);
488 int igmp6_event_query(struct sk_buff
*skb
);
490 int igmp6_event_report(struct sk_buff
*skb
);
494 int ndisc_ifinfo_sysctl_change(struct ctl_table
*ctl
, int write
,
495 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
);
496 int ndisc_ifinfo_sysctl_strategy(struct ctl_table
*ctl
,
497 void __user
*oldval
, size_t __user
*oldlenp
,
498 void __user
*newval
, size_t newlen
);
501 void inet6_ifinfo_notify(int event
, struct inet6_dev
*idev
);