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