1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _ADDRCONF_H 3 #define _ADDRCONF_H 4 5 #define MAX_RTR_SOLICITATIONS -1 /* unlimited */ 6 #define RTR_SOLICITATION_INTERVAL (4*HZ) 7 #define RTR_SOLICITATION_MAX_INTERVAL (3600*HZ) /* 1 hour */ 8 9 #define MIN_VALID_LIFETIME (2*3600) /* 2 hours */ 10 11 /* TEMP_VALID_LIFETIME default value as specified in RFC 8981 3.8 */ 12 #define TEMP_VALID_LIFETIME (2*86400) /* 2 days */ 13 #define TEMP_PREFERRED_LIFETIME (86400) /* 24 hours */ 14 #define REGEN_MIN_ADVANCE (2) /* 2 seconds */ 15 #define REGEN_MAX_RETRY (3) 16 #define MAX_DESYNC_FACTOR (600) 17 18 #define ADDR_CHECK_FREQUENCY (120*HZ) 19 20 #define IPV6_MAX_ADDRESSES 16 21 22 #define ADDRCONF_TIMER_FUZZ_MINUS (HZ > 50 ? HZ / 50 : 1) 23 #define ADDRCONF_TIMER_FUZZ (HZ / 4) 24 #define ADDRCONF_TIMER_FUZZ_MAX (HZ) 25 26 #define ADDRCONF_NOTIFY_PRIORITY 0 27 28 #include <linux/in.h> 29 #include <linux/in6.h> 30 31 struct prefix_info { 32 __u8 type; 33 __u8 length; 34 __u8 prefix_len; 35 36 union __packed { 37 __u8 flags; 38 struct __packed { 39 #if defined(__BIG_ENDIAN_BITFIELD) 40 __u8 onlink : 1, 41 autoconf : 1, 42 routeraddr : 1, 43 preferpd : 1, 44 reserved : 4; 45 #elif defined(__LITTLE_ENDIAN_BITFIELD) 46 __u8 reserved : 4, 47 preferpd : 1, 48 routeraddr : 1, 49 autoconf : 1, 50 onlink : 1; 51 #else 52 #error "Please fix <asm/byteorder.h>" 53 #endif 54 }; 55 }; 56 __be32 valid; 57 __be32 prefered; 58 __be32 reserved2; 59 60 struct in6_addr prefix; 61 }; 62 63 /* rfc4861 4.6.2: IPv6 PIO is 32 bytes in size */ 64 static_assert(sizeof(struct prefix_info) == 32); 65 66 #include <linux/ipv6.h> 67 #include <linux/netdevice.h> 68 #include <net/if_inet6.h> 69 #include <net/ipv6.h> 70 71 struct in6_validator_info { 72 struct in6_addr i6vi_addr; 73 struct inet6_dev *i6vi_dev; 74 struct netlink_ext_ack *extack; 75 }; 76 77 struct ifa6_config { 78 const struct in6_addr *pfx; 79 unsigned int plen; 80 81 u8 ifa_proto; 82 83 const struct in6_addr *peer_pfx; 84 struct inet6_ifaddr *ifpub; 85 unsigned long cstamp; 86 unsigned long tstamp; 87 88 u32 rt_priority; 89 u32 ifa_flags; 90 u32 preferred_lft; 91 u32 valid_lft; 92 u16 scope; 93 }; 94 95 enum addr_type_t { 96 UNICAST_ADDR, 97 MULTICAST_ADDR, 98 ANYCAST_ADDR, 99 }; 100 101 struct inet6_fill_args { 102 u32 portid; 103 u32 seq; 104 int event; 105 unsigned int flags; 106 int netnsid; 107 int ifindex; 108 enum addr_type_t type; 109 bool force_rt_scope_universe; 110 }; 111 112 int addrconf_init(void); 113 void addrconf_cleanup(void); 114 115 int addrconf_add_ifaddr(struct net *net, void __user *arg); 116 int addrconf_del_ifaddr(struct net *net, void __user *arg); 117 int addrconf_set_dstaddr(struct net *net, void __user *arg); 118 119 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, 120 const struct net_device *dev, int strict); 121 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, 122 const struct net_device *dev, bool skip_dev_check, 123 int strict, u32 banned_flags); 124 125 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) 126 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr); 127 #endif 128 129 int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs, 130 unsigned char nsegs); 131 132 bool ipv6_chk_custom_prefix(const struct in6_addr *addr, 133 const unsigned int prefix_len, 134 struct net_device *dev); 135 136 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev); 137 138 struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr, 139 struct net_device *dev); 140 141 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, 142 const struct in6_addr *addr, 143 struct net_device *dev, int strict); 144 145 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dev, 146 const struct in6_addr *daddr, unsigned int srcprefs, 147 struct in6_addr *saddr); 148 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 149 u32 banned_flags); 150 bool inet_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2, 151 bool match_wildcard); 152 bool inet_rcv_saddr_any(const struct sock *sk); 153 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr); 154 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr); 155 156 void addrconf_add_linklocal(struct inet6_dev *idev, 157 const struct in6_addr *addr, u32 flags); 158 159 int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev, 160 const struct prefix_info *pinfo, 161 struct inet6_dev *in6_dev, 162 const struct in6_addr *addr, int addr_type, 163 u32 addr_flags, bool sllao, bool tokenized, 164 __u32 valid_lft, u32 prefered_lft); 165 166 static inline void addrconf_addr_eui48_base(u8 *eui, const char *const addr) 167 { 168 memcpy(eui, addr, 3); 169 eui[3] = 0xFF; 170 eui[4] = 0xFE; 171 memcpy(eui + 5, addr + 3, 3); 172 } 173 174 static inline void addrconf_addr_eui48(u8 *eui, const char *const addr) 175 { 176 addrconf_addr_eui48_base(eui, addr); 177 eui[0] ^= 2; 178 } 179 180 static inline int addrconf_ifid_eui48(u8 *eui, struct net_device *dev) 181 { 182 if (dev->addr_len != ETH_ALEN) 183 return -1; 184 185 /* 186 * The zSeries OSA network cards can be shared among various 187 * OS instances, but the OSA cards have only one MAC address. 188 * This leads to duplicate address conflicts in conjunction 189 * with IPv6 if more than one instance uses the same card. 190 * 191 * The driver for these cards can deliver a unique 16-bit 192 * identifier for each instance sharing the same card. It is 193 * placed instead of 0xFFFE in the interface identifier. The 194 * "u" bit of the interface identifier is not inverted in this 195 * case. Hence the resulting interface identifier has local 196 * scope according to RFC2373. 197 */ 198 199 addrconf_addr_eui48_base(eui, dev->dev_addr); 200 201 if (dev->dev_id) { 202 eui[3] = (dev->dev_id >> 8) & 0xFF; 203 eui[4] = dev->dev_id & 0xFF; 204 } else { 205 eui[0] ^= 2; 206 } 207 208 return 0; 209 } 210 211 #define INFINITY_LIFE_TIME 0xFFFFFFFF 212 213 static inline unsigned long addrconf_timeout_fixup(u32 timeout, 214 unsigned int unit) 215 { 216 if (timeout == INFINITY_LIFE_TIME) 217 return ~0UL; 218 219 /* 220 * Avoid arithmetic overflow. 221 * Assuming unit is constant and non-zero, this "if" statement 222 * will go away on 64bit archs. 223 */ 224 if (0xfffffffe > LONG_MAX / unit && timeout > LONG_MAX / unit) 225 return LONG_MAX / unit; 226 227 return timeout; 228 } 229 230 static inline int addrconf_finite_timeout(unsigned long timeout) 231 { 232 return ~timeout; 233 } 234 235 /* 236 * IPv6 Address Label subsystem (addrlabel.c) 237 */ 238 int ipv6_addr_label_init(void); 239 void ipv6_addr_label_cleanup(void); 240 int ipv6_addr_label_rtnl_register(void); 241 u32 ipv6_addr_label(struct net *net, const struct in6_addr *addr, 242 int type, int ifindex); 243 244 /* 245 * multicast prototypes (mcast.c) 246 */ 247 static inline bool ipv6_mc_may_pull(struct sk_buff *skb, 248 unsigned int len) 249 { 250 if (skb_transport_offset(skb) + ipv6_transport_len(skb) < len) 251 return false; 252 253 return pskb_may_pull(skb, len); 254 } 255 256 int ipv6_sock_mc_join(struct sock *sk, int ifindex, 257 const struct in6_addr *addr); 258 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, 259 const struct in6_addr *addr); 260 void __ipv6_sock_mc_close(struct sock *sk); 261 void ipv6_sock_mc_close(struct sock *sk); 262 bool inet6_mc_check(const struct sock *sk, const struct in6_addr *mc_addr, 263 const struct in6_addr *src_addr); 264 265 int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr); 266 int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr); 267 int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr); 268 void ipv6_mc_up(struct inet6_dev *idev); 269 void ipv6_mc_down(struct inet6_dev *idev); 270 void ipv6_mc_unmap(struct inet6_dev *idev); 271 void ipv6_mc_remap(struct inet6_dev *idev); 272 void ipv6_mc_init_dev(struct inet6_dev *idev); 273 void ipv6_mc_destroy_dev(struct inet6_dev *idev); 274 int ipv6_mc_check_mld(struct sk_buff *skb); 275 void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp); 276 277 bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group, 278 const struct in6_addr *src_addr); 279 280 void ipv6_mc_dad_complete(struct inet6_dev *idev); 281 282 /* 283 * identify MLD packets for MLD filter exceptions 284 */ 285 static inline bool ipv6_is_mld(struct sk_buff *skb, int nexthdr, int offset) 286 { 287 struct icmp6hdr *hdr; 288 289 if (nexthdr != IPPROTO_ICMPV6 || 290 !pskb_network_may_pull(skb, offset + sizeof(struct icmp6hdr))) 291 return false; 292 293 hdr = (struct icmp6hdr *)(skb_network_header(skb) + offset); 294 295 switch (hdr->icmp6_type) { 296 case ICMPV6_MGM_QUERY: 297 case ICMPV6_MGM_REPORT: 298 case ICMPV6_MGM_REDUCTION: 299 case ICMPV6_MLD2_REPORT: 300 return true; 301 default: 302 break; 303 } 304 return false; 305 } 306 307 void addrconf_prefix_rcv(struct net_device *dev, 308 u8 *opt, int len, bool sllao); 309 310 /* 311 * anycast prototypes (anycast.c) 312 */ 313 int ipv6_sock_ac_join(struct sock *sk, int ifindex, 314 const struct in6_addr *addr); 315 int ipv6_sock_ac_drop(struct sock *sk, int ifindex, 316 const struct in6_addr *addr); 317 void __ipv6_sock_ac_close(struct sock *sk); 318 void ipv6_sock_ac_close(struct sock *sk); 319 320 int __ipv6_dev_ac_inc(struct inet6_dev *idev, const struct in6_addr *addr); 321 int __ipv6_dev_ac_dec(struct inet6_dev *idev, const struct in6_addr *addr); 322 void ipv6_ac_destroy_dev(struct inet6_dev *idev); 323 bool ipv6_chk_acast_addr(struct net *net, struct net_device *dev, 324 const struct in6_addr *addr); 325 bool ipv6_chk_acast_addr_src(struct net *net, struct net_device *dev, 326 const struct in6_addr *addr); 327 int ipv6_anycast_init(void); 328 void ipv6_anycast_cleanup(void); 329 330 /* Device notifier */ 331 int register_inet6addr_notifier(struct notifier_block *nb); 332 int unregister_inet6addr_notifier(struct notifier_block *nb); 333 int inet6addr_notifier_call_chain(unsigned long val, void *v); 334 335 int register_inet6addr_validator_notifier(struct notifier_block *nb); 336 int unregister_inet6addr_validator_notifier(struct notifier_block *nb); 337 int inet6addr_validator_notifier_call_chain(unsigned long val, void *v); 338 339 void inet6_netconf_notify_devconf(struct net *net, int event, int type, 340 int ifindex, struct ipv6_devconf *devconf); 341 342 /** 343 * __in6_dev_get - get inet6_dev pointer from netdevice 344 * @dev: network device 345 * 346 * Caller must hold rcu_read_lock or RTNL, because this function 347 * does not take a reference on the inet6_dev. 348 */ 349 static inline struct inet6_dev *__in6_dev_get(const struct net_device *dev) 350 { 351 return rcu_dereference_rtnl(dev->ip6_ptr); 352 } 353 354 static inline struct inet6_dev *in6_dev_rcu(const struct net_device *dev) 355 { 356 return rcu_dereference(dev->ip6_ptr); 357 } 358 359 static inline struct inet6_dev *__in6_dev_get_rtnl_net(const struct net_device *dev) 360 { 361 return rtnl_net_dereference(dev_net(dev), dev->ip6_ptr); 362 } 363 364 /** 365 * __in6_dev_stats_get - get inet6_dev pointer for stats 366 * @dev: network device 367 * @skb: skb for original incoming interface if needed 368 * 369 * Caller must hold rcu_read_lock or RTNL, because this function 370 * does not take a reference on the inet6_dev. 371 */ 372 static inline struct inet6_dev *__in6_dev_stats_get(const struct net_device *dev, 373 const struct sk_buff *skb) 374 { 375 if (netif_is_l3_master(dev)) { 376 dev = dev_get_by_index_rcu(dev_net(dev), inet6_iif(skb)); 377 if (!dev) 378 return NULL; 379 } 380 return __in6_dev_get(dev); 381 } 382 383 /** 384 * __in6_dev_get_safely - get inet6_dev pointer from netdevice 385 * @dev: network device 386 * 387 * This is a safer version of __in6_dev_get 388 */ 389 static inline struct inet6_dev *__in6_dev_get_safely(const struct net_device *dev) 390 { 391 if (likely(dev)) 392 return rcu_dereference_rtnl(dev->ip6_ptr); 393 else 394 return NULL; 395 } 396 397 /** 398 * in6_dev_get - get inet6_dev pointer from netdevice 399 * @dev: network device 400 * 401 * This version can be used in any context, and takes a reference 402 * on the inet6_dev. Callers must use in6_dev_put() later to 403 * release this reference. 404 */ 405 static inline struct inet6_dev *in6_dev_get(const struct net_device *dev) 406 { 407 struct inet6_dev *idev; 408 409 rcu_read_lock(); 410 idev = rcu_dereference(dev->ip6_ptr); 411 if (idev && !refcount_inc_not_zero(&idev->refcnt)) 412 idev = NULL; 413 rcu_read_unlock(); 414 return idev; 415 } 416 417 static inline struct neigh_parms *__in6_dev_nd_parms_get_rcu(const struct net_device *dev) 418 { 419 struct inet6_dev *idev = __in6_dev_get(dev); 420 421 return idev ? idev->nd_parms : NULL; 422 } 423 424 void in6_dev_finish_destroy(struct inet6_dev *idev); 425 426 static inline void in6_dev_put(struct inet6_dev *idev) 427 { 428 if (refcount_dec_and_test(&idev->refcnt)) 429 in6_dev_finish_destroy(idev); 430 } 431 432 static inline void in6_dev_put_clear(struct inet6_dev **pidev) 433 { 434 struct inet6_dev *idev = *pidev; 435 436 if (idev) { 437 in6_dev_put(idev); 438 *pidev = NULL; 439 } 440 } 441 442 static inline void __in6_dev_put(struct inet6_dev *idev) 443 { 444 refcount_dec(&idev->refcnt); 445 } 446 447 static inline void in6_dev_hold(struct inet6_dev *idev) 448 { 449 refcount_inc(&idev->refcnt); 450 } 451 452 static inline bool in6_dev_hold_safe(struct inet6_dev *idev) 453 { 454 return refcount_inc_not_zero(&idev->refcnt); 455 } 456 457 /* called with rcu_read_lock held */ 458 static inline bool ip6_ignore_linkdown(const struct net_device *dev) 459 { 460 const struct inet6_dev *idev = __in6_dev_get(dev); 461 462 if (unlikely(!idev)) 463 return true; 464 465 return !!READ_ONCE(idev->cnf.ignore_routes_with_linkdown); 466 } 467 468 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp); 469 470 static inline void in6_ifa_put(struct inet6_ifaddr *ifp) 471 { 472 if (refcount_dec_and_test(&ifp->refcnt)) 473 inet6_ifa_finish_destroy(ifp); 474 } 475 476 static inline void __in6_ifa_put(struct inet6_ifaddr *ifp) 477 { 478 refcount_dec(&ifp->refcnt); 479 } 480 481 static inline void in6_ifa_hold(struct inet6_ifaddr *ifp) 482 { 483 refcount_inc(&ifp->refcnt); 484 } 485 486 static inline bool in6_ifa_hold_safe(struct inet6_ifaddr *ifp) 487 { 488 return refcount_inc_not_zero(&ifp->refcnt); 489 } 490 491 /* 492 * compute link-local solicited-node multicast address 493 */ 494 495 static inline void addrconf_addr_solict_mult(const struct in6_addr *addr, 496 struct in6_addr *solicited) 497 { 498 ipv6_addr_set(solicited, 499 htonl(0xFF020000), 0, 500 htonl(0x1), 501 htonl(0xFF000000) | addr->s6_addr32[3]); 502 } 503 504 static inline bool ipv6_addr_is_ll_all_nodes(const struct in6_addr *addr) 505 { 506 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 507 __be64 *p = (__force __be64 *)addr; 508 return ((p[0] ^ cpu_to_be64(0xff02000000000000UL)) | (p[1] ^ cpu_to_be64(1))) == 0UL; 509 #else 510 return ((addr->s6_addr32[0] ^ htonl(0xff020000)) | 511 addr->s6_addr32[1] | addr->s6_addr32[2] | 512 (addr->s6_addr32[3] ^ htonl(0x00000001))) == 0; 513 #endif 514 } 515 516 static inline bool ipv6_addr_is_ll_all_routers(const struct in6_addr *addr) 517 { 518 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 519 __be64 *p = (__force __be64 *)addr; 520 return ((p[0] ^ cpu_to_be64(0xff02000000000000UL)) | (p[1] ^ cpu_to_be64(2))) == 0UL; 521 #else 522 return ((addr->s6_addr32[0] ^ htonl(0xff020000)) | 523 addr->s6_addr32[1] | addr->s6_addr32[2] | 524 (addr->s6_addr32[3] ^ htonl(0x00000002))) == 0; 525 #endif 526 } 527 528 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr) 529 { 530 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE); 531 } 532 533 static inline bool ipv6_addr_is_solict_mult(const struct in6_addr *addr) 534 { 535 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 536 __be64 *p = (__force __be64 *)addr; 537 return ((p[0] ^ cpu_to_be64(0xff02000000000000UL)) | 538 ((p[1] ^ cpu_to_be64(0x00000001ff000000UL)) & 539 cpu_to_be64(0xffffffffff000000UL))) == 0UL; 540 #else 541 return ((addr->s6_addr32[0] ^ htonl(0xff020000)) | 542 addr->s6_addr32[1] | 543 (addr->s6_addr32[2] ^ htonl(0x00000001)) | 544 (addr->s6_addr[12] ^ 0xff)) == 0; 545 #endif 546 } 547 548 static inline bool ipv6_addr_is_all_snoopers(const struct in6_addr *addr) 549 { 550 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 551 __be64 *p = (__force __be64 *)addr; 552 553 return ((p[0] ^ cpu_to_be64(0xff02000000000000UL)) | 554 (p[1] ^ cpu_to_be64(0x6a))) == 0UL; 555 #else 556 return ((addr->s6_addr32[0] ^ htonl(0xff020000)) | 557 addr->s6_addr32[1] | addr->s6_addr32[2] | 558 (addr->s6_addr32[3] ^ htonl(0x0000006a))) == 0; 559 #endif 560 } 561 562 #ifdef CONFIG_PROC_FS 563 int if6_proc_init(void); 564 void if6_proc_exit(void); 565 #endif 566 567 int inet6_fill_ifmcaddr(struct sk_buff *skb, 568 const struct ifmcaddr6 *ifmca, 569 struct inet6_fill_args *args); 570 571 int inet6_fill_ifacaddr(struct sk_buff *skb, 572 const struct ifacaddr6 *ifaca, 573 struct inet6_fill_args *args); 574 #endif 575