1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _NDISC_H 3 #define _NDISC_H 4 5 #include <net/ipv6_stubs.h> 6 7 /* 8 * ICMP codes for neighbour discovery messages 9 */ 10 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 16 17 /* 18 * Router type: cross-layer information from link-layer to 19 * IPv6 layer reported by certain link types (e.g., RFC4214). 20 */ 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 */ 25 26 /* 27 * ndisc options 28 */ 29 30 enum { 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 */ 38 __ND_OPT_ARRAY_MAX, 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 */ 43 __ND_OPT_MAX 44 }; 45 46 #define MAX_RTR_SOLICITATION_DELAY HZ 47 48 #define ND_REACHABLE_TIME (30*HZ) 49 #define ND_RETRANS_TIMER HZ 50 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> 58 59 #include <net/neighbour.h> 60 61 /* Set to 3 to get tracing... */ 62 #define ND_DEBUG 1 63 64 #define ND_PRINTK(val, level, fmt, ...) \ 65 do { \ 66 if (val <= ND_DEBUG) \ 67 net_##level##_ratelimited(fmt, ##__VA_ARGS__); \ 68 } while (0) 69 70 struct ctl_table; 71 struct inet6_dev; 72 struct net_device; 73 struct net_proto_family; 74 struct sk_buff; 75 struct prefix_info; 76 77 extern struct neigh_table nd_tbl; 78 79 struct nd_msg { 80 struct icmp6hdr icmph; 81 struct in6_addr target; 82 __u8 opt[0]; 83 }; 84 85 struct rs_msg { 86 struct icmp6hdr icmph; 87 __u8 opt[0]; 88 }; 89 90 struct ra_msg { 91 struct icmp6hdr icmph; 92 __be32 reachable_time; 93 __be32 retrans_timer; 94 }; 95 96 struct rd_msg { 97 struct icmp6hdr icmph; 98 struct in6_addr target; 99 struct in6_addr dest; 100 __u8 opt[0]; 101 }; 102 103 struct nd_opt_hdr { 104 __u8 nd_opt_type; 105 __u8 nd_opt_len; 106 } __packed; 107 108 /* ND options */ 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; 114 #endif 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]; 119 #endif 120 }; 121 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] 131 132 #define NDISC_OPT_SPACE(len) (((len)+2+7)&~7) 133 134 struct ndisc_options *ndisc_parse_options(const struct net_device *dev, 135 u8 *opt, int opt_len, 136 struct ndisc_options *ndopts); 137 138 void __ndisc_fill_addr_option(struct sk_buff *skb, int type, void *data, 139 int data_len, int pad); 140 141 #define NDISC_OPS_REDIRECT_DATA_SPACE 2 142 143 /* 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. 147 * 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. 152 * 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. 160 * 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. 167 * 168 * int (*opt_addr_space)(const struct net_device *dev, u8 icmp6_type, 169 * struct neighbour *neigh, u8 *ha_buf, 170 * u8 **ha): 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. 174 * 175 * void (*fill_addr_option)(const struct net_device *dev, 176 * struct sk_buff *skb, u8 icmp6_type, 177 * const u8 *ha): 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 183 * up state. 184 * 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. 199 */ 200 struct ndisc_ops { 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, 210 u8 **ha); 211 void (*fill_addr_option)(const struct net_device *dev, 212 struct sk_buff *skb, u8 icmp6_type, 213 const u8 *ha); 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); 222 }; 223 224 #if IS_ENABLED(CONFIG_IPV6) 225 static inline int ndisc_ops_is_useropt(const struct net_device *dev, 226 u8 nd_opt_type) 227 { 228 if (dev->ndisc_ops && dev->ndisc_ops->is_useropt) 229 return dev->ndisc_ops->is_useropt(nd_opt_type); 230 else 231 return 0; 232 } 233 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) 237 { 238 if (dev->ndisc_ops && dev->ndisc_ops->parse_options) 239 return dev->ndisc_ops->parse_options(dev, nd_opt, ndopts); 240 else 241 return 0; 242 } 243 244 static inline void ndisc_ops_update(const struct net_device *dev, 245 struct neighbour *n, u32 flags, 246 u8 icmp6_type, 247 const struct ndisc_options *ndopts) 248 { 249 if (dev->ndisc_ops && dev->ndisc_ops->update) 250 dev->ndisc_ops->update(dev, n, flags, icmp6_type, ndopts); 251 } 252 253 static inline int ndisc_ops_opt_addr_space(const struct net_device *dev, 254 u8 icmp6_type) 255 { 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, 259 NULL, NULL); 260 else 261 return 0; 262 } 263 264 static inline int ndisc_ops_redirect_opt_addr_space(const struct net_device *dev, 265 struct neighbour *neigh, 266 u8 *ha_buf, u8 **ha) 267 { 268 if (dev->ndisc_ops && dev->ndisc_ops->opt_addr_space) 269 return dev->ndisc_ops->opt_addr_space(dev, NDISC_REDIRECT, 270 neigh, ha_buf, ha); 271 else 272 return 0; 273 } 274 275 static inline void ndisc_ops_fill_addr_option(const struct net_device *dev, 276 struct sk_buff *skb, 277 u8 icmp6_type) 278 { 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); 282 } 283 284 static inline void ndisc_ops_fill_redirect_addr_option(const struct net_device *dev, 285 struct sk_buff *skb, 286 const u8 *ha) 287 { 288 if (dev->ndisc_ops && dev->ndisc_ops->fill_addr_option) 289 dev->ndisc_ops->fill_addr_option(dev, skb, NDISC_REDIRECT, ha); 290 } 291 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, 299 __u32 valid_lft, 300 u32 prefered_lft, 301 bool dev_addr_generated) 302 { 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, 305 addr, addr_type, 306 addr_flags, sllao, 307 tokenized, valid_lft, 308 prefered_lft, 309 dev_addr_generated); 310 } 311 #endif 312 313 /* 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. 318 */ 319 static inline int ndisc_addr_option_pad(unsigned short type) 320 { 321 switch (type) { 322 case ARPHRD_INFINIBAND: return 2; 323 default: return 0; 324 } 325 } 326 327 static inline int __ndisc_opt_addr_space(unsigned char addr_len, int pad) 328 { 329 return NDISC_OPT_SPACE(addr_len + pad); 330 } 331 332 #if IS_ENABLED(CONFIG_IPV6) 333 static inline int ndisc_opt_addr_space(struct net_device *dev, u8 icmp6_type) 334 { 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); 338 } 339 340 static inline int ndisc_redirect_opt_addr_space(struct net_device *dev, 341 struct neighbour *neigh, 342 u8 *ops_data_buf, 343 u8 **ops_data) 344 { 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, 348 ops_data); 349 } 350 #endif 351 352 static inline u8 *__ndisc_opt_addr_data(struct nd_opt_hdr *p, 353 unsigned char addr_len, int prepad) 354 { 355 u8 *lladdr = (u8 *)(p + 1); 356 int lladdrlen = p->nd_opt_len << 3; 357 if (lladdrlen != __ndisc_opt_addr_space(addr_len, prepad)) 358 return NULL; 359 return lladdr + prepad; 360 } 361 362 static inline u8 *ndisc_opt_addr_data(struct nd_opt_hdr *p, 363 struct net_device *dev) 364 { 365 return __ndisc_opt_addr_data(p, dev->addr_len, 366 ndisc_addr_option_pad(dev->type)); 367 } 368 369 static inline u32 ndisc_hashfn(const void *pkey, const struct net_device *dev, __u32 *hash_rnd) 370 { 371 const u32 *p32 = pkey; 372 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])); 377 } 378 379 static inline struct neighbour *__ipv6_neigh_lookup_noref(struct net_device *dev, const void *pkey) 380 { 381 return ___neigh_lookup_noref(&nd_tbl, neigh_key_eq128, ndisc_hashfn, pkey, dev); 382 } 383 384 static inline 385 struct neighbour *__ipv6_neigh_lookup_noref_stub(struct net_device *dev, 386 const void *pkey) 387 { 388 return ___neigh_lookup_noref(ipv6_stub->nd_tbl, neigh_key_eq128, 389 ndisc_hashfn, pkey, dev); 390 } 391 392 static inline struct neighbour *__ipv6_neigh_lookup(struct net_device *dev, const void *pkey) 393 { 394 struct neighbour *n; 395 396 rcu_read_lock_bh(); 397 n = __ipv6_neigh_lookup_noref(dev, pkey); 398 if (n && !refcount_inc_not_zero(&n->refcnt)) 399 n = NULL; 400 rcu_read_unlock_bh(); 401 402 return n; 403 } 404 405 static inline void __ipv6_confirm_neigh(struct net_device *dev, 406 const void *pkey) 407 { 408 struct neighbour *n; 409 410 rcu_read_lock_bh(); 411 n = __ipv6_neigh_lookup_noref(dev, pkey); 412 if (n) { 413 unsigned long now = jiffies; 414 415 /* avoid dirtying neighbour */ 416 if (n->confirmed != now) 417 n->confirmed = now; 418 } 419 rcu_read_unlock_bh(); 420 } 421 422 static inline void __ipv6_confirm_neigh_stub(struct net_device *dev, 423 const void *pkey) 424 { 425 struct neighbour *n; 426 427 rcu_read_lock_bh(); 428 n = __ipv6_neigh_lookup_noref_stub(dev, pkey); 429 if (n) { 430 unsigned long now = jiffies; 431 432 /* avoid dirtying neighbour */ 433 if (n->confirmed != now) 434 n->confirmed = now; 435 } 436 rcu_read_unlock_bh(); 437 } 438 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, 441 const void *addr) 442 { 443 struct neighbour *neigh; 444 445 neigh = __ipv6_neigh_lookup_noref_stub(dev, addr); 446 if (unlikely(!neigh)) 447 neigh = __neigh_create(ipv6_stub->nd_tbl, addr, dev, false); 448 449 return neigh; 450 } 451 452 int ndisc_init(void); 453 int ndisc_late_init(void); 454 455 void ndisc_late_cleanup(void); 456 void ndisc_cleanup(void); 457 458 int ndisc_rcv(struct sk_buff *skb); 459 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, 462 u64 nonce); 463 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); 469 470 void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target); 471 472 int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev, 473 int dir); 474 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); 478 479 /* 480 * IGMP 481 */ 482 int igmp6_init(void); 483 int igmp6_late_init(void); 484 485 void igmp6_cleanup(void); 486 void igmp6_late_cleanup(void); 487 488 int igmp6_event_query(struct sk_buff *skb); 489 490 int igmp6_event_report(struct sk_buff *skb); 491 492 493 #ifdef CONFIG_SYSCTL 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); 499 #endif 500 501 void inet6_ifinfo_notify(int event, struct inet6_dev *idev); 502 503 #endif 504