1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * IPv6 Address [auto]configuration 4 * Linux INET6 implementation 5 * 6 * Authors: 7 * Pedro Roque <roque@di.fc.ul.pt> 8 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 9 */ 10 11 /* 12 * Changes: 13 * 14 * Janos Farkas : delete timer on ifdown 15 * <chexum@bankinf.banki.hu> 16 * Andi Kleen : kill double kfree on module 17 * unload. 18 * Maciej W. Rozycki : FDDI support 19 * sekiya@USAGI : Don't send too many RS 20 * packets. 21 * yoshfuji@USAGI : Fixed interval between DAD 22 * packets. 23 * YOSHIFUJI Hideaki @USAGI : improved accuracy of 24 * address validation timer. 25 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041) 26 * support. 27 * Yuji SEKIYA @USAGI : Don't assign a same IPv6 28 * address on a same interface. 29 * YOSHIFUJI Hideaki @USAGI : ARCnet support 30 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to 31 * seq_file. 32 * YOSHIFUJI Hideaki @USAGI : improved source address 33 * selection; consider scope, 34 * status etc. 35 */ 36 37 #define pr_fmt(fmt) "IPv6: " fmt 38 39 #include <linux/errno.h> 40 #include <linux/types.h> 41 #include <linux/kernel.h> 42 #include <linux/sched/signal.h> 43 #include <linux/socket.h> 44 #include <linux/sockios.h> 45 #include <linux/net.h> 46 #include <linux/inet.h> 47 #include <linux/in6.h> 48 #include <linux/netdevice.h> 49 #include <linux/if_addr.h> 50 #include <linux/if_arp.h> 51 #include <linux/if_arcnet.h> 52 #include <linux/if_infiniband.h> 53 #include <linux/route.h> 54 #include <linux/inetdevice.h> 55 #include <linux/init.h> 56 #include <linux/slab.h> 57 #ifdef CONFIG_SYSCTL 58 #include <linux/sysctl.h> 59 #endif 60 #include <linux/capability.h> 61 #include <linux/delay.h> 62 #include <linux/notifier.h> 63 #include <linux/string.h> 64 #include <linux/hash.h> 65 66 #include <net/net_namespace.h> 67 #include <net/sock.h> 68 #include <net/snmp.h> 69 70 #include <net/6lowpan.h> 71 #include <net/firewire.h> 72 #include <net/ipv6.h> 73 #include <net/protocol.h> 74 #include <net/ndisc.h> 75 #include <net/ip6_route.h> 76 #include <net/addrconf.h> 77 #include <net/tcp.h> 78 #include <net/ip.h> 79 #include <net/netlink.h> 80 #include <net/pkt_sched.h> 81 #include <net/l3mdev.h> 82 #include <linux/if_tunnel.h> 83 #include <linux/rtnetlink.h> 84 #include <linux/netconf.h> 85 #include <linux/random.h> 86 #include <linux/uaccess.h> 87 #include <asm/unaligned.h> 88 89 #include <linux/proc_fs.h> 90 #include <linux/seq_file.h> 91 #include <linux/export.h> 92 #include <linux/ioam6.h> 93 94 #define INFINITY_LIFE_TIME 0xFFFFFFFF 95 96 #define IPV6_MAX_STRLEN \ 97 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") 98 99 static inline u32 cstamp_delta(unsigned long cstamp) 100 { 101 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; 102 } 103 104 static inline s32 rfc3315_s14_backoff_init(s32 irt) 105 { 106 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */ 107 u64 tmp = get_random_u32_inclusive(900000, 1100000) * (u64)irt; 108 do_div(tmp, 1000000); 109 return (s32)tmp; 110 } 111 112 static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt) 113 { 114 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */ 115 u64 tmp = get_random_u32_inclusive(1900000, 2100000) * (u64)rt; 116 do_div(tmp, 1000000); 117 if ((s32)tmp > mrt) { 118 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */ 119 tmp = get_random_u32_inclusive(900000, 1100000) * (u64)mrt; 120 do_div(tmp, 1000000); 121 } 122 return (s32)tmp; 123 } 124 125 #ifdef CONFIG_SYSCTL 126 static int addrconf_sysctl_register(struct inet6_dev *idev); 127 static void addrconf_sysctl_unregister(struct inet6_dev *idev); 128 #else 129 static inline int addrconf_sysctl_register(struct inet6_dev *idev) 130 { 131 return 0; 132 } 133 134 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) 135 { 136 } 137 #endif 138 139 static void ipv6_gen_rnd_iid(struct in6_addr *addr); 140 141 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 142 static int ipv6_count_addresses(const struct inet6_dev *idev); 143 static int ipv6_generate_stable_address(struct in6_addr *addr, 144 u8 dad_count, 145 const struct inet6_dev *idev); 146 147 #define IN6_ADDR_HSIZE_SHIFT 8 148 #define IN6_ADDR_HSIZE (1 << IN6_ADDR_HSIZE_SHIFT) 149 150 static void addrconf_verify(struct net *net); 151 static void addrconf_verify_rtnl(struct net *net); 152 153 static struct workqueue_struct *addrconf_wq; 154 155 static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 156 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 157 158 static void addrconf_type_change(struct net_device *dev, 159 unsigned long event); 160 static int addrconf_ifdown(struct net_device *dev, bool unregister); 161 162 static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 163 int plen, 164 const struct net_device *dev, 165 u32 flags, u32 noflags, 166 bool no_gw); 167 168 static void addrconf_dad_start(struct inet6_ifaddr *ifp); 169 static void addrconf_dad_work(struct work_struct *w); 170 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id, 171 bool send_na); 172 static void addrconf_dad_run(struct inet6_dev *idev, bool restart); 173 static void addrconf_rs_timer(struct timer_list *t); 174 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 175 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 176 177 static void inet6_prefix_notify(int event, struct inet6_dev *idev, 178 struct prefix_info *pinfo); 179 180 static struct ipv6_devconf ipv6_devconf __read_mostly = { 181 .forwarding = 0, 182 .hop_limit = IPV6_DEFAULT_HOPLIMIT, 183 .mtu6 = IPV6_MIN_MTU, 184 .accept_ra = 1, 185 .accept_redirects = 1, 186 .autoconf = 1, 187 .force_mld_version = 0, 188 .mldv1_unsolicited_report_interval = 10 * HZ, 189 .mldv2_unsolicited_report_interval = HZ, 190 .dad_transmits = 1, 191 .rtr_solicits = MAX_RTR_SOLICITATIONS, 192 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 193 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL, 194 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 195 .use_tempaddr = 0, 196 .temp_valid_lft = TEMP_VALID_LIFETIME, 197 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 198 .regen_max_retry = REGEN_MAX_RETRY, 199 .max_desync_factor = MAX_DESYNC_FACTOR, 200 .max_addresses = IPV6_MAX_ADDRESSES, 201 .accept_ra_defrtr = 1, 202 .ra_defrtr_metric = IP6_RT_PRIO_USER, 203 .accept_ra_from_local = 0, 204 .accept_ra_min_hop_limit= 1, 205 .accept_ra_min_lft = 0, 206 .accept_ra_pinfo = 1, 207 #ifdef CONFIG_IPV6_ROUTER_PREF 208 .accept_ra_rtr_pref = 1, 209 .rtr_probe_interval = 60 * HZ, 210 #ifdef CONFIG_IPV6_ROUTE_INFO 211 .accept_ra_rt_info_min_plen = 0, 212 .accept_ra_rt_info_max_plen = 0, 213 #endif 214 #endif 215 .proxy_ndp = 0, 216 .accept_source_route = 0, /* we do not accept RH0 by default. */ 217 .disable_ipv6 = 0, 218 .accept_dad = 0, 219 .suppress_frag_ndisc = 1, 220 .accept_ra_mtu = 1, 221 .stable_secret = { 222 .initialized = false, 223 }, 224 .use_oif_addrs_only = 0, 225 .ignore_routes_with_linkdown = 0, 226 .keep_addr_on_down = 0, 227 .seg6_enabled = 0, 228 #ifdef CONFIG_IPV6_SEG6_HMAC 229 .seg6_require_hmac = 0, 230 #endif 231 .enhanced_dad = 1, 232 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64, 233 .disable_policy = 0, 234 .rpl_seg_enabled = 0, 235 .ioam6_enabled = 0, 236 .ioam6_id = IOAM6_DEFAULT_IF_ID, 237 .ioam6_id_wide = IOAM6_DEFAULT_IF_ID_WIDE, 238 .ndisc_evict_nocarrier = 1, 239 }; 240 241 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 242 .forwarding = 0, 243 .hop_limit = IPV6_DEFAULT_HOPLIMIT, 244 .mtu6 = IPV6_MIN_MTU, 245 .accept_ra = 1, 246 .accept_redirects = 1, 247 .autoconf = 1, 248 .force_mld_version = 0, 249 .mldv1_unsolicited_report_interval = 10 * HZ, 250 .mldv2_unsolicited_report_interval = HZ, 251 .dad_transmits = 1, 252 .rtr_solicits = MAX_RTR_SOLICITATIONS, 253 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 254 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL, 255 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 256 .use_tempaddr = 0, 257 .temp_valid_lft = TEMP_VALID_LIFETIME, 258 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 259 .regen_max_retry = REGEN_MAX_RETRY, 260 .max_desync_factor = MAX_DESYNC_FACTOR, 261 .max_addresses = IPV6_MAX_ADDRESSES, 262 .accept_ra_defrtr = 1, 263 .ra_defrtr_metric = IP6_RT_PRIO_USER, 264 .accept_ra_from_local = 0, 265 .accept_ra_min_hop_limit= 1, 266 .accept_ra_min_lft = 0, 267 .accept_ra_pinfo = 1, 268 #ifdef CONFIG_IPV6_ROUTER_PREF 269 .accept_ra_rtr_pref = 1, 270 .rtr_probe_interval = 60 * HZ, 271 #ifdef CONFIG_IPV6_ROUTE_INFO 272 .accept_ra_rt_info_min_plen = 0, 273 .accept_ra_rt_info_max_plen = 0, 274 #endif 275 #endif 276 .proxy_ndp = 0, 277 .accept_source_route = 0, /* we do not accept RH0 by default. */ 278 .disable_ipv6 = 0, 279 .accept_dad = 1, 280 .suppress_frag_ndisc = 1, 281 .accept_ra_mtu = 1, 282 .stable_secret = { 283 .initialized = false, 284 }, 285 .use_oif_addrs_only = 0, 286 .ignore_routes_with_linkdown = 0, 287 .keep_addr_on_down = 0, 288 .seg6_enabled = 0, 289 #ifdef CONFIG_IPV6_SEG6_HMAC 290 .seg6_require_hmac = 0, 291 #endif 292 .enhanced_dad = 1, 293 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64, 294 .disable_policy = 0, 295 .rpl_seg_enabled = 0, 296 .ioam6_enabled = 0, 297 .ioam6_id = IOAM6_DEFAULT_IF_ID, 298 .ioam6_id_wide = IOAM6_DEFAULT_IF_ID_WIDE, 299 .ndisc_evict_nocarrier = 1, 300 }; 301 302 /* Check if link is ready: is it up and is a valid qdisc available */ 303 static inline bool addrconf_link_ready(const struct net_device *dev) 304 { 305 return netif_oper_up(dev) && !qdisc_tx_is_noop(dev); 306 } 307 308 static void addrconf_del_rs_timer(struct inet6_dev *idev) 309 { 310 if (del_timer(&idev->rs_timer)) 311 __in6_dev_put(idev); 312 } 313 314 static void addrconf_del_dad_work(struct inet6_ifaddr *ifp) 315 { 316 if (cancel_delayed_work(&ifp->dad_work)) 317 __in6_ifa_put(ifp); 318 } 319 320 static void addrconf_mod_rs_timer(struct inet6_dev *idev, 321 unsigned long when) 322 { 323 if (!mod_timer(&idev->rs_timer, jiffies + when)) 324 in6_dev_hold(idev); 325 } 326 327 static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp, 328 unsigned long delay) 329 { 330 in6_ifa_hold(ifp); 331 if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay)) 332 in6_ifa_put(ifp); 333 } 334 335 static int snmp6_alloc_dev(struct inet6_dev *idev) 336 { 337 int i; 338 339 idev->stats.ipv6 = alloc_percpu_gfp(struct ipstats_mib, GFP_KERNEL_ACCOUNT); 340 if (!idev->stats.ipv6) 341 goto err_ip; 342 343 for_each_possible_cpu(i) { 344 struct ipstats_mib *addrconf_stats; 345 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i); 346 u64_stats_init(&addrconf_stats->syncp); 347 } 348 349 350 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device), 351 GFP_KERNEL); 352 if (!idev->stats.icmpv6dev) 353 goto err_icmp; 354 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device), 355 GFP_KERNEL_ACCOUNT); 356 if (!idev->stats.icmpv6msgdev) 357 goto err_icmpmsg; 358 359 return 0; 360 361 err_icmpmsg: 362 kfree(idev->stats.icmpv6dev); 363 err_icmp: 364 free_percpu(idev->stats.ipv6); 365 err_ip: 366 return -ENOMEM; 367 } 368 369 static struct inet6_dev *ipv6_add_dev(struct net_device *dev) 370 { 371 struct inet6_dev *ndev; 372 int err = -ENOMEM; 373 374 ASSERT_RTNL(); 375 376 if (dev->mtu < IPV6_MIN_MTU && dev != blackhole_netdev) 377 return ERR_PTR(-EINVAL); 378 379 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL_ACCOUNT); 380 if (!ndev) 381 return ERR_PTR(err); 382 383 rwlock_init(&ndev->lock); 384 ndev->dev = dev; 385 INIT_LIST_HEAD(&ndev->addr_list); 386 timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0); 387 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 388 389 if (ndev->cnf.stable_secret.initialized) 390 ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 391 392 ndev->cnf.mtu6 = dev->mtu; 393 ndev->ra_mtu = 0; 394 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 395 if (!ndev->nd_parms) { 396 kfree(ndev); 397 return ERR_PTR(err); 398 } 399 if (ndev->cnf.forwarding) 400 dev_disable_lro(dev); 401 /* We refer to the device */ 402 netdev_hold(dev, &ndev->dev_tracker, GFP_KERNEL); 403 404 if (snmp6_alloc_dev(ndev) < 0) { 405 netdev_dbg(dev, "%s: cannot allocate memory for statistics\n", 406 __func__); 407 neigh_parms_release(&nd_tbl, ndev->nd_parms); 408 netdev_put(dev, &ndev->dev_tracker); 409 kfree(ndev); 410 return ERR_PTR(err); 411 } 412 413 if (dev != blackhole_netdev) { 414 if (snmp6_register_dev(ndev) < 0) { 415 netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n", 416 __func__, dev->name); 417 goto err_release; 418 } 419 } 420 /* One reference from device. */ 421 refcount_set(&ndev->refcnt, 1); 422 423 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 424 ndev->cnf.accept_dad = -1; 425 426 #if IS_ENABLED(CONFIG_IPV6_SIT) 427 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 428 pr_info("%s: Disabled Multicast RS\n", dev->name); 429 ndev->cnf.rtr_solicits = 0; 430 } 431 #endif 432 433 INIT_LIST_HEAD(&ndev->tempaddr_list); 434 ndev->desync_factor = U32_MAX; 435 if ((dev->flags&IFF_LOOPBACK) || 436 dev->type == ARPHRD_TUNNEL || 437 dev->type == ARPHRD_TUNNEL6 || 438 dev->type == ARPHRD_SIT || 439 dev->type == ARPHRD_NONE) { 440 ndev->cnf.use_tempaddr = -1; 441 } 442 443 ndev->token = in6addr_any; 444 445 if (netif_running(dev) && addrconf_link_ready(dev)) 446 ndev->if_flags |= IF_READY; 447 448 ipv6_mc_init_dev(ndev); 449 ndev->tstamp = jiffies; 450 if (dev != blackhole_netdev) { 451 err = addrconf_sysctl_register(ndev); 452 if (err) { 453 ipv6_mc_destroy_dev(ndev); 454 snmp6_unregister_dev(ndev); 455 goto err_release; 456 } 457 } 458 /* protected by rtnl_lock */ 459 rcu_assign_pointer(dev->ip6_ptr, ndev); 460 461 if (dev != blackhole_netdev) { 462 /* Join interface-local all-node multicast group */ 463 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes); 464 465 /* Join all-node multicast group */ 466 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 467 468 /* Join all-router multicast group if forwarding is set */ 469 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST)) 470 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 471 } 472 return ndev; 473 474 err_release: 475 neigh_parms_release(&nd_tbl, ndev->nd_parms); 476 ndev->dead = 1; 477 in6_dev_finish_destroy(ndev); 478 return ERR_PTR(err); 479 } 480 481 static struct inet6_dev *ipv6_find_idev(struct net_device *dev) 482 { 483 struct inet6_dev *idev; 484 485 ASSERT_RTNL(); 486 487 idev = __in6_dev_get(dev); 488 if (!idev) { 489 idev = ipv6_add_dev(dev); 490 if (IS_ERR(idev)) 491 return idev; 492 } 493 494 if (dev->flags&IFF_UP) 495 ipv6_mc_up(idev); 496 return idev; 497 } 498 499 static int inet6_netconf_msgsize_devconf(int type) 500 { 501 int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) 502 + nla_total_size(4); /* NETCONFA_IFINDEX */ 503 bool all = false; 504 505 if (type == NETCONFA_ALL) 506 all = true; 507 508 if (all || type == NETCONFA_FORWARDING) 509 size += nla_total_size(4); 510 #ifdef CONFIG_IPV6_MROUTE 511 if (all || type == NETCONFA_MC_FORWARDING) 512 size += nla_total_size(4); 513 #endif 514 if (all || type == NETCONFA_PROXY_NEIGH) 515 size += nla_total_size(4); 516 517 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) 518 size += nla_total_size(4); 519 520 return size; 521 } 522 523 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex, 524 struct ipv6_devconf *devconf, u32 portid, 525 u32 seq, int event, unsigned int flags, 526 int type) 527 { 528 struct nlmsghdr *nlh; 529 struct netconfmsg *ncm; 530 bool all = false; 531 532 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), 533 flags); 534 if (!nlh) 535 return -EMSGSIZE; 536 537 if (type == NETCONFA_ALL) 538 all = true; 539 540 ncm = nlmsg_data(nlh); 541 ncm->ncm_family = AF_INET6; 542 543 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) 544 goto nla_put_failure; 545 546 if (!devconf) 547 goto out; 548 549 if ((all || type == NETCONFA_FORWARDING) && 550 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0) 551 goto nla_put_failure; 552 #ifdef CONFIG_IPV6_MROUTE 553 if ((all || type == NETCONFA_MC_FORWARDING) && 554 nla_put_s32(skb, NETCONFA_MC_FORWARDING, 555 atomic_read(&devconf->mc_forwarding)) < 0) 556 goto nla_put_failure; 557 #endif 558 if ((all || type == NETCONFA_PROXY_NEIGH) && 559 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0) 560 goto nla_put_failure; 561 562 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) && 563 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 564 devconf->ignore_routes_with_linkdown) < 0) 565 goto nla_put_failure; 566 567 out: 568 nlmsg_end(skb, nlh); 569 return 0; 570 571 nla_put_failure: 572 nlmsg_cancel(skb, nlh); 573 return -EMSGSIZE; 574 } 575 576 void inet6_netconf_notify_devconf(struct net *net, int event, int type, 577 int ifindex, struct ipv6_devconf *devconf) 578 { 579 struct sk_buff *skb; 580 int err = -ENOBUFS; 581 582 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL); 583 if (!skb) 584 goto errout; 585 586 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, 587 event, 0, type); 588 if (err < 0) { 589 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 590 WARN_ON(err == -EMSGSIZE); 591 kfree_skb(skb); 592 goto errout; 593 } 594 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL); 595 return; 596 errout: 597 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err); 598 } 599 600 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = { 601 [NETCONFA_IFINDEX] = { .len = sizeof(int) }, 602 [NETCONFA_FORWARDING] = { .len = sizeof(int) }, 603 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) }, 604 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) }, 605 }; 606 607 static int inet6_netconf_valid_get_req(struct sk_buff *skb, 608 const struct nlmsghdr *nlh, 609 struct nlattr **tb, 610 struct netlink_ext_ack *extack) 611 { 612 int i, err; 613 614 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) { 615 NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf get request"); 616 return -EINVAL; 617 } 618 619 if (!netlink_strict_get_check(skb)) 620 return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg), 621 tb, NETCONFA_MAX, 622 devconf_ipv6_policy, extack); 623 624 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg), 625 tb, NETCONFA_MAX, 626 devconf_ipv6_policy, extack); 627 if (err) 628 return err; 629 630 for (i = 0; i <= NETCONFA_MAX; i++) { 631 if (!tb[i]) 632 continue; 633 634 switch (i) { 635 case NETCONFA_IFINDEX: 636 break; 637 default: 638 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request"); 639 return -EINVAL; 640 } 641 } 642 643 return 0; 644 } 645 646 static int inet6_netconf_get_devconf(struct sk_buff *in_skb, 647 struct nlmsghdr *nlh, 648 struct netlink_ext_ack *extack) 649 { 650 struct net *net = sock_net(in_skb->sk); 651 struct nlattr *tb[NETCONFA_MAX+1]; 652 struct inet6_dev *in6_dev = NULL; 653 struct net_device *dev = NULL; 654 struct sk_buff *skb; 655 struct ipv6_devconf *devconf; 656 int ifindex; 657 int err; 658 659 err = inet6_netconf_valid_get_req(in_skb, nlh, tb, extack); 660 if (err < 0) 661 return err; 662 663 if (!tb[NETCONFA_IFINDEX]) 664 return -EINVAL; 665 666 err = -EINVAL; 667 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); 668 switch (ifindex) { 669 case NETCONFA_IFINDEX_ALL: 670 devconf = net->ipv6.devconf_all; 671 break; 672 case NETCONFA_IFINDEX_DEFAULT: 673 devconf = net->ipv6.devconf_dflt; 674 break; 675 default: 676 dev = dev_get_by_index(net, ifindex); 677 if (!dev) 678 return -EINVAL; 679 in6_dev = in6_dev_get(dev); 680 if (!in6_dev) 681 goto errout; 682 devconf = &in6_dev->cnf; 683 break; 684 } 685 686 err = -ENOBUFS; 687 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL); 688 if (!skb) 689 goto errout; 690 691 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 692 NETLINK_CB(in_skb).portid, 693 nlh->nlmsg_seq, RTM_NEWNETCONF, 0, 694 NETCONFA_ALL); 695 if (err < 0) { 696 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 697 WARN_ON(err == -EMSGSIZE); 698 kfree_skb(skb); 699 goto errout; 700 } 701 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 702 errout: 703 if (in6_dev) 704 in6_dev_put(in6_dev); 705 dev_put(dev); 706 return err; 707 } 708 709 static int inet6_netconf_dump_devconf(struct sk_buff *skb, 710 struct netlink_callback *cb) 711 { 712 const struct nlmsghdr *nlh = cb->nlh; 713 struct net *net = sock_net(skb->sk); 714 int h, s_h; 715 int idx, s_idx; 716 struct net_device *dev; 717 struct inet6_dev *idev; 718 struct hlist_head *head; 719 720 if (cb->strict_check) { 721 struct netlink_ext_ack *extack = cb->extack; 722 struct netconfmsg *ncm; 723 724 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) { 725 NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request"); 726 return -EINVAL; 727 } 728 729 if (nlmsg_attrlen(nlh, sizeof(*ncm))) { 730 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request"); 731 return -EINVAL; 732 } 733 } 734 735 s_h = cb->args[0]; 736 s_idx = idx = cb->args[1]; 737 738 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 739 idx = 0; 740 head = &net->dev_index_head[h]; 741 rcu_read_lock(); 742 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ 743 net->dev_base_seq; 744 hlist_for_each_entry_rcu(dev, head, index_hlist) { 745 if (idx < s_idx) 746 goto cont; 747 idev = __in6_dev_get(dev); 748 if (!idev) 749 goto cont; 750 751 if (inet6_netconf_fill_devconf(skb, dev->ifindex, 752 &idev->cnf, 753 NETLINK_CB(cb->skb).portid, 754 nlh->nlmsg_seq, 755 RTM_NEWNETCONF, 756 NLM_F_MULTI, 757 NETCONFA_ALL) < 0) { 758 rcu_read_unlock(); 759 goto done; 760 } 761 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 762 cont: 763 idx++; 764 } 765 rcu_read_unlock(); 766 } 767 if (h == NETDEV_HASHENTRIES) { 768 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, 769 net->ipv6.devconf_all, 770 NETLINK_CB(cb->skb).portid, 771 nlh->nlmsg_seq, 772 RTM_NEWNETCONF, NLM_F_MULTI, 773 NETCONFA_ALL) < 0) 774 goto done; 775 else 776 h++; 777 } 778 if (h == NETDEV_HASHENTRIES + 1) { 779 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, 780 net->ipv6.devconf_dflt, 781 NETLINK_CB(cb->skb).portid, 782 nlh->nlmsg_seq, 783 RTM_NEWNETCONF, NLM_F_MULTI, 784 NETCONFA_ALL) < 0) 785 goto done; 786 else 787 h++; 788 } 789 done: 790 cb->args[0] = h; 791 cb->args[1] = idx; 792 793 return skb->len; 794 } 795 796 #ifdef CONFIG_SYSCTL 797 static void dev_forward_change(struct inet6_dev *idev) 798 { 799 struct net_device *dev; 800 struct inet6_ifaddr *ifa; 801 LIST_HEAD(tmp_addr_list); 802 803 if (!idev) 804 return; 805 dev = idev->dev; 806 if (idev->cnf.forwarding) 807 dev_disable_lro(dev); 808 if (dev->flags & IFF_MULTICAST) { 809 if (idev->cnf.forwarding) { 810 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 811 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters); 812 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters); 813 } else { 814 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 815 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters); 816 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters); 817 } 818 } 819 820 read_lock_bh(&idev->lock); 821 list_for_each_entry(ifa, &idev->addr_list, if_list) { 822 if (ifa->flags&IFA_F_TENTATIVE) 823 continue; 824 list_add_tail(&ifa->if_list_aux, &tmp_addr_list); 825 } 826 read_unlock_bh(&idev->lock); 827 828 while (!list_empty(&tmp_addr_list)) { 829 ifa = list_first_entry(&tmp_addr_list, 830 struct inet6_ifaddr, if_list_aux); 831 list_del(&ifa->if_list_aux); 832 if (idev->cnf.forwarding) 833 addrconf_join_anycast(ifa); 834 else 835 addrconf_leave_anycast(ifa); 836 } 837 838 inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF, 839 NETCONFA_FORWARDING, 840 dev->ifindex, &idev->cnf); 841 } 842 843 844 static void addrconf_forward_change(struct net *net, __s32 newf) 845 { 846 struct net_device *dev; 847 struct inet6_dev *idev; 848 849 for_each_netdev(net, dev) { 850 idev = __in6_dev_get(dev); 851 if (idev) { 852 int changed = (!idev->cnf.forwarding) ^ (!newf); 853 idev->cnf.forwarding = newf; 854 if (changed) 855 dev_forward_change(idev); 856 } 857 } 858 } 859 860 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) 861 { 862 struct net *net; 863 int old; 864 865 if (!rtnl_trylock()) 866 return restart_syscall(); 867 868 net = (struct net *)table->extra2; 869 old = *p; 870 *p = newf; 871 872 if (p == &net->ipv6.devconf_dflt->forwarding) { 873 if ((!newf) ^ (!old)) 874 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 875 NETCONFA_FORWARDING, 876 NETCONFA_IFINDEX_DEFAULT, 877 net->ipv6.devconf_dflt); 878 rtnl_unlock(); 879 return 0; 880 } 881 882 if (p == &net->ipv6.devconf_all->forwarding) { 883 int old_dflt = net->ipv6.devconf_dflt->forwarding; 884 885 net->ipv6.devconf_dflt->forwarding = newf; 886 if ((!newf) ^ (!old_dflt)) 887 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 888 NETCONFA_FORWARDING, 889 NETCONFA_IFINDEX_DEFAULT, 890 net->ipv6.devconf_dflt); 891 892 addrconf_forward_change(net, newf); 893 if ((!newf) ^ (!old)) 894 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 895 NETCONFA_FORWARDING, 896 NETCONFA_IFINDEX_ALL, 897 net->ipv6.devconf_all); 898 } else if ((!newf) ^ (!old)) 899 dev_forward_change((struct inet6_dev *)table->extra1); 900 rtnl_unlock(); 901 902 if (newf) 903 rt6_purge_dflt_routers(net); 904 return 1; 905 } 906 907 static void addrconf_linkdown_change(struct net *net, __s32 newf) 908 { 909 struct net_device *dev; 910 struct inet6_dev *idev; 911 912 for_each_netdev(net, dev) { 913 idev = __in6_dev_get(dev); 914 if (idev) { 915 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf); 916 917 idev->cnf.ignore_routes_with_linkdown = newf; 918 if (changed) 919 inet6_netconf_notify_devconf(dev_net(dev), 920 RTM_NEWNETCONF, 921 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 922 dev->ifindex, 923 &idev->cnf); 924 } 925 } 926 } 927 928 static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf) 929 { 930 struct net *net; 931 int old; 932 933 if (!rtnl_trylock()) 934 return restart_syscall(); 935 936 net = (struct net *)table->extra2; 937 old = *p; 938 *p = newf; 939 940 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) { 941 if ((!newf) ^ (!old)) 942 inet6_netconf_notify_devconf(net, 943 RTM_NEWNETCONF, 944 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 945 NETCONFA_IFINDEX_DEFAULT, 946 net->ipv6.devconf_dflt); 947 rtnl_unlock(); 948 return 0; 949 } 950 951 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) { 952 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf; 953 addrconf_linkdown_change(net, newf); 954 if ((!newf) ^ (!old)) 955 inet6_netconf_notify_devconf(net, 956 RTM_NEWNETCONF, 957 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 958 NETCONFA_IFINDEX_ALL, 959 net->ipv6.devconf_all); 960 } 961 rtnl_unlock(); 962 963 return 1; 964 } 965 966 #endif 967 968 /* Nobody refers to this ifaddr, destroy it */ 969 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 970 { 971 WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 972 973 #ifdef NET_REFCNT_DEBUG 974 pr_debug("%s\n", __func__); 975 #endif 976 977 in6_dev_put(ifp->idev); 978 979 if (cancel_delayed_work(&ifp->dad_work)) 980 pr_notice("delayed DAD work was pending while freeing ifa=%p\n", 981 ifp); 982 983 if (ifp->state != INET6_IFADDR_STATE_DEAD) { 984 pr_warn("Freeing alive inet6 address %p\n", ifp); 985 return; 986 } 987 988 kfree_rcu(ifp, rcu); 989 } 990 991 static void 992 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 993 { 994 struct list_head *p; 995 int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 996 997 /* 998 * Each device address list is sorted in order of scope - 999 * global before linklocal. 1000 */ 1001 list_for_each(p, &idev->addr_list) { 1002 struct inet6_ifaddr *ifa 1003 = list_entry(p, struct inet6_ifaddr, if_list); 1004 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 1005 break; 1006 } 1007 1008 list_add_tail_rcu(&ifp->if_list, p); 1009 } 1010 1011 static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr) 1012 { 1013 u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net); 1014 1015 return hash_32(val, IN6_ADDR_HSIZE_SHIFT); 1016 } 1017 1018 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 1019 struct net_device *dev, unsigned int hash) 1020 { 1021 struct inet6_ifaddr *ifp; 1022 1023 hlist_for_each_entry(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) { 1024 if (ipv6_addr_equal(&ifp->addr, addr)) { 1025 if (!dev || ifp->idev->dev == dev) 1026 return true; 1027 } 1028 } 1029 return false; 1030 } 1031 1032 static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa) 1033 { 1034 struct net *net = dev_net(dev); 1035 unsigned int hash = inet6_addr_hash(net, &ifa->addr); 1036 int err = 0; 1037 1038 spin_lock_bh(&net->ipv6.addrconf_hash_lock); 1039 1040 /* Ignore adding duplicate addresses on an interface */ 1041 if (ipv6_chk_same_addr(net, &ifa->addr, dev, hash)) { 1042 netdev_dbg(dev, "ipv6_add_addr: already assigned\n"); 1043 err = -EEXIST; 1044 } else { 1045 hlist_add_head_rcu(&ifa->addr_lst, &net->ipv6.inet6_addr_lst[hash]); 1046 } 1047 1048 spin_unlock_bh(&net->ipv6.addrconf_hash_lock); 1049 1050 return err; 1051 } 1052 1053 /* On success it returns ifp with increased reference count */ 1054 1055 static struct inet6_ifaddr * 1056 ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg, 1057 bool can_block, struct netlink_ext_ack *extack) 1058 { 1059 gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC; 1060 int addr_type = ipv6_addr_type(cfg->pfx); 1061 struct net *net = dev_net(idev->dev); 1062 struct inet6_ifaddr *ifa = NULL; 1063 struct fib6_info *f6i = NULL; 1064 int err = 0; 1065 1066 if (addr_type == IPV6_ADDR_ANY) { 1067 NL_SET_ERR_MSG_MOD(extack, "Invalid address"); 1068 return ERR_PTR(-EADDRNOTAVAIL); 1069 } else if (addr_type & IPV6_ADDR_MULTICAST && 1070 !(cfg->ifa_flags & IFA_F_MCAUTOJOIN)) { 1071 NL_SET_ERR_MSG_MOD(extack, "Cannot assign multicast address without \"IFA_F_MCAUTOJOIN\" flag"); 1072 return ERR_PTR(-EADDRNOTAVAIL); 1073 } else if (!(idev->dev->flags & IFF_LOOPBACK) && 1074 !netif_is_l3_master(idev->dev) && 1075 addr_type & IPV6_ADDR_LOOPBACK) { 1076 NL_SET_ERR_MSG_MOD(extack, "Cannot assign loopback address on this device"); 1077 return ERR_PTR(-EADDRNOTAVAIL); 1078 } 1079 1080 if (idev->dead) { 1081 NL_SET_ERR_MSG_MOD(extack, "device is going away"); 1082 err = -ENODEV; 1083 goto out; 1084 } 1085 1086 if (idev->cnf.disable_ipv6) { 1087 NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device"); 1088 err = -EACCES; 1089 goto out; 1090 } 1091 1092 /* validator notifier needs to be blocking; 1093 * do not call in atomic context 1094 */ 1095 if (can_block) { 1096 struct in6_validator_info i6vi = { 1097 .i6vi_addr = *cfg->pfx, 1098 .i6vi_dev = idev, 1099 .extack = extack, 1100 }; 1101 1102 err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi); 1103 err = notifier_to_errno(err); 1104 if (err < 0) 1105 goto out; 1106 } 1107 1108 ifa = kzalloc(sizeof(*ifa), gfp_flags | __GFP_ACCOUNT); 1109 if (!ifa) { 1110 err = -ENOBUFS; 1111 goto out; 1112 } 1113 1114 f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags, extack); 1115 if (IS_ERR(f6i)) { 1116 err = PTR_ERR(f6i); 1117 f6i = NULL; 1118 goto out; 1119 } 1120 1121 neigh_parms_data_state_setall(idev->nd_parms); 1122 1123 ifa->addr = *cfg->pfx; 1124 if (cfg->peer_pfx) 1125 ifa->peer_addr = *cfg->peer_pfx; 1126 1127 spin_lock_init(&ifa->lock); 1128 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work); 1129 INIT_HLIST_NODE(&ifa->addr_lst); 1130 ifa->scope = cfg->scope; 1131 ifa->prefix_len = cfg->plen; 1132 ifa->rt_priority = cfg->rt_priority; 1133 ifa->flags = cfg->ifa_flags; 1134 ifa->ifa_proto = cfg->ifa_proto; 1135 /* No need to add the TENTATIVE flag for addresses with NODAD */ 1136 if (!(cfg->ifa_flags & IFA_F_NODAD)) 1137 ifa->flags |= IFA_F_TENTATIVE; 1138 ifa->valid_lft = cfg->valid_lft; 1139 ifa->prefered_lft = cfg->preferred_lft; 1140 ifa->cstamp = ifa->tstamp = jiffies; 1141 ifa->tokenized = false; 1142 1143 ifa->rt = f6i; 1144 1145 ifa->idev = idev; 1146 in6_dev_hold(idev); 1147 1148 /* For caller */ 1149 refcount_set(&ifa->refcnt, 1); 1150 1151 rcu_read_lock(); 1152 1153 err = ipv6_add_addr_hash(idev->dev, ifa); 1154 if (err < 0) { 1155 rcu_read_unlock(); 1156 goto out; 1157 } 1158 1159 write_lock_bh(&idev->lock); 1160 1161 /* Add to inet6_dev unicast addr list. */ 1162 ipv6_link_dev_addr(idev, ifa); 1163 1164 if (ifa->flags&IFA_F_TEMPORARY) { 1165 list_add(&ifa->tmp_list, &idev->tempaddr_list); 1166 in6_ifa_hold(ifa); 1167 } 1168 1169 in6_ifa_hold(ifa); 1170 write_unlock_bh(&idev->lock); 1171 1172 rcu_read_unlock(); 1173 1174 inet6addr_notifier_call_chain(NETDEV_UP, ifa); 1175 out: 1176 if (unlikely(err < 0)) { 1177 fib6_info_release(f6i); 1178 1179 if (ifa) { 1180 if (ifa->idev) 1181 in6_dev_put(ifa->idev); 1182 kfree(ifa); 1183 } 1184 ifa = ERR_PTR(err); 1185 } 1186 1187 return ifa; 1188 } 1189 1190 enum cleanup_prefix_rt_t { 1191 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */ 1192 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */ 1193 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */ 1194 }; 1195 1196 /* 1197 * Check, whether the prefix for ifp would still need a prefix route 1198 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_* 1199 * constants. 1200 * 1201 * 1) we don't purge prefix if address was not permanent. 1202 * prefix is managed by its own lifetime. 1203 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE. 1204 * 3) if there are no addresses, delete prefix. 1205 * 4) if there are still other permanent address(es), 1206 * corresponding prefix is still permanent. 1207 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE, 1208 * don't purge the prefix, assume user space is managing it. 1209 * 6) otherwise, update prefix lifetime to the 1210 * longest valid lifetime among the corresponding 1211 * addresses on the device. 1212 * Note: subsequent RA will update lifetime. 1213 **/ 1214 static enum cleanup_prefix_rt_t 1215 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires) 1216 { 1217 struct inet6_ifaddr *ifa; 1218 struct inet6_dev *idev = ifp->idev; 1219 unsigned long lifetime; 1220 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL; 1221 1222 *expires = jiffies; 1223 1224 list_for_each_entry(ifa, &idev->addr_list, if_list) { 1225 if (ifa == ifp) 1226 continue; 1227 if (ifa->prefix_len != ifp->prefix_len || 1228 !ipv6_prefix_equal(&ifa->addr, &ifp->addr, 1229 ifp->prefix_len)) 1230 continue; 1231 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE)) 1232 return CLEANUP_PREFIX_RT_NOP; 1233 1234 action = CLEANUP_PREFIX_RT_EXPIRE; 1235 1236 spin_lock(&ifa->lock); 1237 1238 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 1239 /* 1240 * Note: Because this address is 1241 * not permanent, lifetime < 1242 * LONG_MAX / HZ here. 1243 */ 1244 if (time_before(*expires, ifa->tstamp + lifetime * HZ)) 1245 *expires = ifa->tstamp + lifetime * HZ; 1246 spin_unlock(&ifa->lock); 1247 } 1248 1249 return action; 1250 } 1251 1252 static void 1253 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, 1254 bool del_rt, bool del_peer) 1255 { 1256 struct fib6_info *f6i; 1257 1258 f6i = addrconf_get_prefix_route(del_peer ? &ifp->peer_addr : &ifp->addr, 1259 ifp->prefix_len, 1260 ifp->idev->dev, 0, RTF_DEFAULT, true); 1261 if (f6i) { 1262 if (del_rt) 1263 ip6_del_rt(dev_net(ifp->idev->dev), f6i, false); 1264 else { 1265 if (!(f6i->fib6_flags & RTF_EXPIRES)) 1266 fib6_set_expires(f6i, expires); 1267 fib6_info_release(f6i); 1268 } 1269 } 1270 } 1271 1272 1273 /* This function wants to get referenced ifp and releases it before return */ 1274 1275 static void ipv6_del_addr(struct inet6_ifaddr *ifp) 1276 { 1277 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP; 1278 struct net *net = dev_net(ifp->idev->dev); 1279 unsigned long expires; 1280 int state; 1281 1282 ASSERT_RTNL(); 1283 1284 spin_lock_bh(&ifp->lock); 1285 state = ifp->state; 1286 ifp->state = INET6_IFADDR_STATE_DEAD; 1287 spin_unlock_bh(&ifp->lock); 1288 1289 if (state == INET6_IFADDR_STATE_DEAD) 1290 goto out; 1291 1292 spin_lock_bh(&net->ipv6.addrconf_hash_lock); 1293 hlist_del_init_rcu(&ifp->addr_lst); 1294 spin_unlock_bh(&net->ipv6.addrconf_hash_lock); 1295 1296 write_lock_bh(&ifp->idev->lock); 1297 1298 if (ifp->flags&IFA_F_TEMPORARY) { 1299 list_del(&ifp->tmp_list); 1300 if (ifp->ifpub) { 1301 in6_ifa_put(ifp->ifpub); 1302 ifp->ifpub = NULL; 1303 } 1304 __in6_ifa_put(ifp); 1305 } 1306 1307 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE)) 1308 action = check_cleanup_prefix_route(ifp, &expires); 1309 1310 list_del_rcu(&ifp->if_list); 1311 __in6_ifa_put(ifp); 1312 1313 write_unlock_bh(&ifp->idev->lock); 1314 1315 addrconf_del_dad_work(ifp); 1316 1317 ipv6_ifa_notify(RTM_DELADDR, ifp); 1318 1319 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); 1320 1321 if (action != CLEANUP_PREFIX_RT_NOP) { 1322 cleanup_prefix_route(ifp, expires, 1323 action == CLEANUP_PREFIX_RT_DEL, false); 1324 } 1325 1326 /* clean up prefsrc entries */ 1327 rt6_remove_prefsrc(ifp); 1328 out: 1329 in6_ifa_put(ifp); 1330 } 1331 1332 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, bool block) 1333 { 1334 struct inet6_dev *idev = ifp->idev; 1335 unsigned long tmp_tstamp, age; 1336 unsigned long regen_advance; 1337 unsigned long now = jiffies; 1338 s32 cnf_temp_preferred_lft; 1339 struct inet6_ifaddr *ift; 1340 struct ifa6_config cfg; 1341 long max_desync_factor; 1342 struct in6_addr addr; 1343 int ret = 0; 1344 1345 write_lock_bh(&idev->lock); 1346 1347 retry: 1348 in6_dev_hold(idev); 1349 if (idev->cnf.use_tempaddr <= 0) { 1350 write_unlock_bh(&idev->lock); 1351 pr_info("%s: use_tempaddr is disabled\n", __func__); 1352 in6_dev_put(idev); 1353 ret = -1; 1354 goto out; 1355 } 1356 spin_lock_bh(&ifp->lock); 1357 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 1358 idev->cnf.use_tempaddr = -1; /*XXX*/ 1359 spin_unlock_bh(&ifp->lock); 1360 write_unlock_bh(&idev->lock); 1361 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", 1362 __func__); 1363 in6_dev_put(idev); 1364 ret = -1; 1365 goto out; 1366 } 1367 in6_ifa_hold(ifp); 1368 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 1369 ipv6_gen_rnd_iid(&addr); 1370 1371 age = (now - ifp->tstamp) / HZ; 1372 1373 regen_advance = idev->cnf.regen_max_retry * 1374 idev->cnf.dad_transmits * 1375 max(NEIGH_VAR(idev->nd_parms, RETRANS_TIME), HZ/100) / HZ; 1376 1377 /* recalculate max_desync_factor each time and update 1378 * idev->desync_factor if it's larger 1379 */ 1380 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft); 1381 max_desync_factor = min_t(long, 1382 idev->cnf.max_desync_factor, 1383 cnf_temp_preferred_lft - regen_advance); 1384 1385 if (unlikely(idev->desync_factor > max_desync_factor)) { 1386 if (max_desync_factor > 0) { 1387 get_random_bytes(&idev->desync_factor, 1388 sizeof(idev->desync_factor)); 1389 idev->desync_factor %= max_desync_factor; 1390 } else { 1391 idev->desync_factor = 0; 1392 } 1393 } 1394 1395 memset(&cfg, 0, sizeof(cfg)); 1396 cfg.valid_lft = min_t(__u32, ifp->valid_lft, 1397 idev->cnf.temp_valid_lft + age); 1398 cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor; 1399 cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft); 1400 1401 cfg.plen = ifp->prefix_len; 1402 tmp_tstamp = ifp->tstamp; 1403 spin_unlock_bh(&ifp->lock); 1404 1405 write_unlock_bh(&idev->lock); 1406 1407 /* A temporary address is created only if this calculated Preferred 1408 * Lifetime is greater than REGEN_ADVANCE time units. In particular, 1409 * an implementation must not create a temporary address with a zero 1410 * Preferred Lifetime. 1411 * Use age calculation as in addrconf_verify to avoid unnecessary 1412 * temporary addresses being generated. 1413 */ 1414 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 1415 if (cfg.preferred_lft <= regen_advance + age) { 1416 in6_ifa_put(ifp); 1417 in6_dev_put(idev); 1418 ret = -1; 1419 goto out; 1420 } 1421 1422 cfg.ifa_flags = IFA_F_TEMPORARY; 1423 /* set in addrconf_prefix_rcv() */ 1424 if (ifp->flags & IFA_F_OPTIMISTIC) 1425 cfg.ifa_flags |= IFA_F_OPTIMISTIC; 1426 1427 cfg.pfx = &addr; 1428 cfg.scope = ipv6_addr_scope(cfg.pfx); 1429 1430 ift = ipv6_add_addr(idev, &cfg, block, NULL); 1431 if (IS_ERR(ift)) { 1432 in6_ifa_put(ifp); 1433 in6_dev_put(idev); 1434 pr_info("%s: retry temporary address regeneration\n", __func__); 1435 write_lock_bh(&idev->lock); 1436 goto retry; 1437 } 1438 1439 spin_lock_bh(&ift->lock); 1440 ift->ifpub = ifp; 1441 ift->cstamp = now; 1442 ift->tstamp = tmp_tstamp; 1443 spin_unlock_bh(&ift->lock); 1444 1445 addrconf_dad_start(ift); 1446 in6_ifa_put(ift); 1447 in6_dev_put(idev); 1448 out: 1449 return ret; 1450 } 1451 1452 /* 1453 * Choose an appropriate source address (RFC3484) 1454 */ 1455 enum { 1456 IPV6_SADDR_RULE_INIT = 0, 1457 IPV6_SADDR_RULE_LOCAL, 1458 IPV6_SADDR_RULE_SCOPE, 1459 IPV6_SADDR_RULE_PREFERRED, 1460 #ifdef CONFIG_IPV6_MIP6 1461 IPV6_SADDR_RULE_HOA, 1462 #endif 1463 IPV6_SADDR_RULE_OIF, 1464 IPV6_SADDR_RULE_LABEL, 1465 IPV6_SADDR_RULE_PRIVACY, 1466 IPV6_SADDR_RULE_ORCHID, 1467 IPV6_SADDR_RULE_PREFIX, 1468 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 1469 IPV6_SADDR_RULE_NOT_OPTIMISTIC, 1470 #endif 1471 IPV6_SADDR_RULE_MAX 1472 }; 1473 1474 struct ipv6_saddr_score { 1475 int rule; 1476 int addr_type; 1477 struct inet6_ifaddr *ifa; 1478 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 1479 int scopedist; 1480 int matchlen; 1481 }; 1482 1483 struct ipv6_saddr_dst { 1484 const struct in6_addr *addr; 1485 int ifindex; 1486 int scope; 1487 int label; 1488 unsigned int prefs; 1489 }; 1490 1491 static inline int ipv6_saddr_preferred(int type) 1492 { 1493 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 1494 return 1; 1495 return 0; 1496 } 1497 1498 static bool ipv6_use_optimistic_addr(struct net *net, 1499 struct inet6_dev *idev) 1500 { 1501 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 1502 if (!idev) 1503 return false; 1504 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad) 1505 return false; 1506 if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic) 1507 return false; 1508 1509 return true; 1510 #else 1511 return false; 1512 #endif 1513 } 1514 1515 static bool ipv6_allow_optimistic_dad(struct net *net, 1516 struct inet6_dev *idev) 1517 { 1518 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 1519 if (!idev) 1520 return false; 1521 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad) 1522 return false; 1523 1524 return true; 1525 #else 1526 return false; 1527 #endif 1528 } 1529 1530 static int ipv6_get_saddr_eval(struct net *net, 1531 struct ipv6_saddr_score *score, 1532 struct ipv6_saddr_dst *dst, 1533 int i) 1534 { 1535 int ret; 1536 1537 if (i <= score->rule) { 1538 switch (i) { 1539 case IPV6_SADDR_RULE_SCOPE: 1540 ret = score->scopedist; 1541 break; 1542 case IPV6_SADDR_RULE_PREFIX: 1543 ret = score->matchlen; 1544 break; 1545 default: 1546 ret = !!test_bit(i, score->scorebits); 1547 } 1548 goto out; 1549 } 1550 1551 switch (i) { 1552 case IPV6_SADDR_RULE_INIT: 1553 /* Rule 0: remember if hiscore is not ready yet */ 1554 ret = !!score->ifa; 1555 break; 1556 case IPV6_SADDR_RULE_LOCAL: 1557 /* Rule 1: Prefer same address */ 1558 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1559 break; 1560 case IPV6_SADDR_RULE_SCOPE: 1561 /* Rule 2: Prefer appropriate scope 1562 * 1563 * ret 1564 * ^ 1565 * -1 | d 15 1566 * ---+--+-+---> scope 1567 * | 1568 * | d is scope of the destination. 1569 * B-d | \ 1570 * | \ <- smaller scope is better if 1571 * B-15 | \ if scope is enough for destination. 1572 * | ret = B - scope (-1 <= scope >= d <= 15). 1573 * d-C-1 | / 1574 * |/ <- greater is better 1575 * -C / if scope is not enough for destination. 1576 * /| ret = scope - C (-1 <= d < scope <= 15). 1577 * 1578 * d - C - 1 < B -15 (for all -1 <= d <= 15). 1579 * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1580 * Assume B = 0 and we get C > 29. 1581 */ 1582 ret = __ipv6_addr_src_scope(score->addr_type); 1583 if (ret >= dst->scope) 1584 ret = -ret; 1585 else 1586 ret -= 128; /* 30 is enough */ 1587 score->scopedist = ret; 1588 break; 1589 case IPV6_SADDR_RULE_PREFERRED: 1590 { 1591 /* Rule 3: Avoid deprecated and optimistic addresses */ 1592 u8 avoid = IFA_F_DEPRECATED; 1593 1594 if (!ipv6_use_optimistic_addr(net, score->ifa->idev)) 1595 avoid |= IFA_F_OPTIMISTIC; 1596 ret = ipv6_saddr_preferred(score->addr_type) || 1597 !(score->ifa->flags & avoid); 1598 break; 1599 } 1600 #ifdef CONFIG_IPV6_MIP6 1601 case IPV6_SADDR_RULE_HOA: 1602 { 1603 /* Rule 4: Prefer home address */ 1604 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 1605 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1606 break; 1607 } 1608 #endif 1609 case IPV6_SADDR_RULE_OIF: 1610 /* Rule 5: Prefer outgoing interface */ 1611 ret = (!dst->ifindex || 1612 dst->ifindex == score->ifa->idev->dev->ifindex); 1613 break; 1614 case IPV6_SADDR_RULE_LABEL: 1615 /* Rule 6: Prefer matching label */ 1616 ret = ipv6_addr_label(net, 1617 &score->ifa->addr, score->addr_type, 1618 score->ifa->idev->dev->ifindex) == dst->label; 1619 break; 1620 case IPV6_SADDR_RULE_PRIVACY: 1621 { 1622 /* Rule 7: Prefer public address 1623 * Note: prefer temporary address if use_tempaddr >= 2 1624 */ 1625 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 1626 !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 1627 score->ifa->idev->cnf.use_tempaddr >= 2; 1628 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1629 break; 1630 } 1631 case IPV6_SADDR_RULE_ORCHID: 1632 /* Rule 8-: Prefer ORCHID vs ORCHID or 1633 * non-ORCHID vs non-ORCHID 1634 */ 1635 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1636 ipv6_addr_orchid(dst->addr)); 1637 break; 1638 case IPV6_SADDR_RULE_PREFIX: 1639 /* Rule 8: Use longest matching prefix */ 1640 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); 1641 if (ret > score->ifa->prefix_len) 1642 ret = score->ifa->prefix_len; 1643 score->matchlen = ret; 1644 break; 1645 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 1646 case IPV6_SADDR_RULE_NOT_OPTIMISTIC: 1647 /* Optimistic addresses still have lower precedence than other 1648 * preferred addresses. 1649 */ 1650 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC); 1651 break; 1652 #endif 1653 default: 1654 ret = 0; 1655 } 1656 1657 if (ret) 1658 __set_bit(i, score->scorebits); 1659 score->rule = i; 1660 out: 1661 return ret; 1662 } 1663 1664 static int __ipv6_dev_get_saddr(struct net *net, 1665 struct ipv6_saddr_dst *dst, 1666 struct inet6_dev *idev, 1667 struct ipv6_saddr_score *scores, 1668 int hiscore_idx) 1669 { 1670 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx]; 1671 1672 list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) { 1673 int i; 1674 1675 /* 1676 * - Tentative Address (RFC2462 section 5.4) 1677 * - A tentative address is not considered 1678 * "assigned to an interface" in the traditional 1679 * sense, unless it is also flagged as optimistic. 1680 * - Candidate Source Address (section 4) 1681 * - In any case, anycast addresses, multicast 1682 * addresses, and the unspecified address MUST 1683 * NOT be included in a candidate set. 1684 */ 1685 if ((score->ifa->flags & IFA_F_TENTATIVE) && 1686 (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 1687 continue; 1688 1689 score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1690 1691 if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1692 score->addr_type & IPV6_ADDR_MULTICAST)) { 1693 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s", 1694 idev->dev->name); 1695 continue; 1696 } 1697 1698 score->rule = -1; 1699 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1700 1701 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1702 int minihiscore, miniscore; 1703 1704 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i); 1705 miniscore = ipv6_get_saddr_eval(net, score, dst, i); 1706 1707 if (minihiscore > miniscore) { 1708 if (i == IPV6_SADDR_RULE_SCOPE && 1709 score->scopedist > 0) { 1710 /* 1711 * special case: 1712 * each remaining entry 1713 * has too small (not enough) 1714 * scope, because ifa entries 1715 * are sorted by their scope 1716 * values. 1717 */ 1718 goto out; 1719 } 1720 break; 1721 } else if (minihiscore < miniscore) { 1722 swap(hiscore, score); 1723 hiscore_idx = 1 - hiscore_idx; 1724 1725 /* restore our iterator */ 1726 score->ifa = hiscore->ifa; 1727 1728 break; 1729 } 1730 } 1731 } 1732 out: 1733 return hiscore_idx; 1734 } 1735 1736 static int ipv6_get_saddr_master(struct net *net, 1737 const struct net_device *dst_dev, 1738 const struct net_device *master, 1739 struct ipv6_saddr_dst *dst, 1740 struct ipv6_saddr_score *scores, 1741 int hiscore_idx) 1742 { 1743 struct inet6_dev *idev; 1744 1745 idev = __in6_dev_get(dst_dev); 1746 if (idev) 1747 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1748 scores, hiscore_idx); 1749 1750 idev = __in6_dev_get(master); 1751 if (idev) 1752 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, 1753 scores, hiscore_idx); 1754 1755 return hiscore_idx; 1756 } 1757 1758 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, 1759 const struct in6_addr *daddr, unsigned int prefs, 1760 struct in6_addr *saddr) 1761 { 1762 struct ipv6_saddr_score scores[2], *hiscore; 1763 struct ipv6_saddr_dst dst; 1764 struct inet6_dev *idev; 1765 struct net_device *dev; 1766 int dst_type; 1767 bool use_oif_addr = false; 1768 int hiscore_idx = 0; 1769 int ret = 0; 1770 1771 dst_type = __ipv6_addr_type(daddr); 1772 dst.addr = daddr; 1773 dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 1774 dst.scope = __ipv6_addr_src_scope(dst_type); 1775 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 1776 dst.prefs = prefs; 1777 1778 scores[hiscore_idx].rule = -1; 1779 scores[hiscore_idx].ifa = NULL; 1780 1781 rcu_read_lock(); 1782 1783 /* Candidate Source Address (section 4) 1784 * - multicast and link-local destination address, 1785 * the set of candidate source address MUST only 1786 * include addresses assigned to interfaces 1787 * belonging to the same link as the outgoing 1788 * interface. 1789 * (- For site-local destination addresses, the 1790 * set of candidate source addresses MUST only 1791 * include addresses assigned to interfaces 1792 * belonging to the same site as the outgoing 1793 * interface.) 1794 * - "It is RECOMMENDED that the candidate source addresses 1795 * be the set of unicast addresses assigned to the 1796 * interface that will be used to send to the destination 1797 * (the 'outgoing' interface)." (RFC 6724) 1798 */ 1799 if (dst_dev) { 1800 idev = __in6_dev_get(dst_dev); 1801 if ((dst_type & IPV6_ADDR_MULTICAST) || 1802 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL || 1803 (idev && idev->cnf.use_oif_addrs_only)) { 1804 use_oif_addr = true; 1805 } 1806 } 1807 1808 if (use_oif_addr) { 1809 if (idev) 1810 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 1811 } else { 1812 const struct net_device *master; 1813 int master_idx = 0; 1814 1815 /* if dst_dev exists and is enslaved to an L3 device, then 1816 * prefer addresses from dst_dev and then the master over 1817 * any other enslaved devices in the L3 domain. 1818 */ 1819 master = l3mdev_master_dev_rcu(dst_dev); 1820 if (master) { 1821 master_idx = master->ifindex; 1822 1823 hiscore_idx = ipv6_get_saddr_master(net, dst_dev, 1824 master, &dst, 1825 scores, hiscore_idx); 1826 1827 if (scores[hiscore_idx].ifa) 1828 goto out; 1829 } 1830 1831 for_each_netdev_rcu(net, dev) { 1832 /* only consider addresses on devices in the 1833 * same L3 domain 1834 */ 1835 if (l3mdev_master_ifindex_rcu(dev) != master_idx) 1836 continue; 1837 idev = __in6_dev_get(dev); 1838 if (!idev) 1839 continue; 1840 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); 1841 } 1842 } 1843 1844 out: 1845 hiscore = &scores[hiscore_idx]; 1846 if (!hiscore->ifa) 1847 ret = -EADDRNOTAVAIL; 1848 else 1849 *saddr = hiscore->ifa->addr; 1850 1851 rcu_read_unlock(); 1852 return ret; 1853 } 1854 EXPORT_SYMBOL(ipv6_dev_get_saddr); 1855 1856 static int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, 1857 u32 banned_flags) 1858 { 1859 struct inet6_ifaddr *ifp; 1860 int err = -EADDRNOTAVAIL; 1861 1862 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 1863 if (ifp->scope > IFA_LINK) 1864 break; 1865 if (ifp->scope == IFA_LINK && 1866 !(ifp->flags & banned_flags)) { 1867 *addr = ifp->addr; 1868 err = 0; 1869 break; 1870 } 1871 } 1872 return err; 1873 } 1874 1875 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 1876 u32 banned_flags) 1877 { 1878 struct inet6_dev *idev; 1879 int err = -EADDRNOTAVAIL; 1880 1881 rcu_read_lock(); 1882 idev = __in6_dev_get(dev); 1883 if (idev) { 1884 read_lock_bh(&idev->lock); 1885 err = __ipv6_get_lladdr(idev, addr, banned_flags); 1886 read_unlock_bh(&idev->lock); 1887 } 1888 rcu_read_unlock(); 1889 return err; 1890 } 1891 1892 static int ipv6_count_addresses(const struct inet6_dev *idev) 1893 { 1894 const struct inet6_ifaddr *ifp; 1895 int cnt = 0; 1896 1897 rcu_read_lock(); 1898 list_for_each_entry_rcu(ifp, &idev->addr_list, if_list) 1899 cnt++; 1900 rcu_read_unlock(); 1901 return cnt; 1902 } 1903 1904 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, 1905 const struct net_device *dev, int strict) 1906 { 1907 return ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1908 strict, IFA_F_TENTATIVE); 1909 } 1910 EXPORT_SYMBOL(ipv6_chk_addr); 1911 1912 /* device argument is used to find the L3 domain of interest. If 1913 * skip_dev_check is set, then the ifp device is not checked against 1914 * the passed in dev argument. So the 2 cases for addresses checks are: 1915 * 1. does the address exist in the L3 domain that dev is part of 1916 * (skip_dev_check = true), or 1917 * 1918 * 2. does the address exist on the specific device 1919 * (skip_dev_check = false) 1920 */ 1921 static struct net_device * 1922 __ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, 1923 const struct net_device *dev, bool skip_dev_check, 1924 int strict, u32 banned_flags) 1925 { 1926 unsigned int hash = inet6_addr_hash(net, addr); 1927 struct net_device *l3mdev, *ndev; 1928 struct inet6_ifaddr *ifp; 1929 u32 ifp_flags; 1930 1931 rcu_read_lock(); 1932 1933 l3mdev = l3mdev_master_dev_rcu(dev); 1934 if (skip_dev_check) 1935 dev = NULL; 1936 1937 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) { 1938 ndev = ifp->idev->dev; 1939 1940 if (l3mdev_master_dev_rcu(ndev) != l3mdev) 1941 continue; 1942 1943 /* Decouple optimistic from tentative for evaluation here. 1944 * Ban optimistic addresses explicitly, when required. 1945 */ 1946 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC) 1947 ? (ifp->flags&~IFA_F_TENTATIVE) 1948 : ifp->flags; 1949 if (ipv6_addr_equal(&ifp->addr, addr) && 1950 !(ifp_flags&banned_flags) && 1951 (!dev || ndev == dev || 1952 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 1953 rcu_read_unlock(); 1954 return ndev; 1955 } 1956 } 1957 1958 rcu_read_unlock(); 1959 return NULL; 1960 } 1961 1962 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, 1963 const struct net_device *dev, bool skip_dev_check, 1964 int strict, u32 banned_flags) 1965 { 1966 return __ipv6_chk_addr_and_flags(net, addr, dev, skip_dev_check, 1967 strict, banned_flags) ? 1 : 0; 1968 } 1969 EXPORT_SYMBOL(ipv6_chk_addr_and_flags); 1970 1971 1972 /* Compares an address/prefix_len with addresses on device @dev. 1973 * If one is found it returns true. 1974 */ 1975 bool ipv6_chk_custom_prefix(const struct in6_addr *addr, 1976 const unsigned int prefix_len, struct net_device *dev) 1977 { 1978 const struct inet6_ifaddr *ifa; 1979 const struct inet6_dev *idev; 1980 bool ret = false; 1981 1982 rcu_read_lock(); 1983 idev = __in6_dev_get(dev); 1984 if (idev) { 1985 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) { 1986 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len); 1987 if (ret) 1988 break; 1989 } 1990 } 1991 rcu_read_unlock(); 1992 1993 return ret; 1994 } 1995 EXPORT_SYMBOL(ipv6_chk_custom_prefix); 1996 1997 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) 1998 { 1999 const struct inet6_ifaddr *ifa; 2000 const struct inet6_dev *idev; 2001 int onlink; 2002 2003 onlink = 0; 2004 rcu_read_lock(); 2005 idev = __in6_dev_get(dev); 2006 if (idev) { 2007 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) { 2008 onlink = ipv6_prefix_equal(addr, &ifa->addr, 2009 ifa->prefix_len); 2010 if (onlink) 2011 break; 2012 } 2013 } 2014 rcu_read_unlock(); 2015 return onlink; 2016 } 2017 EXPORT_SYMBOL(ipv6_chk_prefix); 2018 2019 /** 2020 * ipv6_dev_find - find the first device with a given source address. 2021 * @net: the net namespace 2022 * @addr: the source address 2023 * @dev: used to find the L3 domain of interest 2024 * 2025 * The caller should be protected by RCU, or RTNL. 2026 */ 2027 struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr, 2028 struct net_device *dev) 2029 { 2030 return __ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1, 2031 IFA_F_TENTATIVE); 2032 } 2033 EXPORT_SYMBOL(ipv6_dev_find); 2034 2035 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 2036 struct net_device *dev, int strict) 2037 { 2038 unsigned int hash = inet6_addr_hash(net, addr); 2039 struct inet6_ifaddr *ifp, *result = NULL; 2040 2041 rcu_read_lock(); 2042 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) { 2043 if (ipv6_addr_equal(&ifp->addr, addr)) { 2044 if (!dev || ifp->idev->dev == dev || 2045 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 2046 result = ifp; 2047 in6_ifa_hold(ifp); 2048 break; 2049 } 2050 } 2051 } 2052 rcu_read_unlock(); 2053 2054 return result; 2055 } 2056 2057 /* Gets referenced address, destroys ifaddr */ 2058 2059 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 2060 { 2061 if (dad_failed) 2062 ifp->flags |= IFA_F_DADFAILED; 2063 2064 if (ifp->flags&IFA_F_TEMPORARY) { 2065 struct inet6_ifaddr *ifpub; 2066 spin_lock_bh(&ifp->lock); 2067 ifpub = ifp->ifpub; 2068 if (ifpub) { 2069 in6_ifa_hold(ifpub); 2070 spin_unlock_bh(&ifp->lock); 2071 ipv6_create_tempaddr(ifpub, true); 2072 in6_ifa_put(ifpub); 2073 } else { 2074 spin_unlock_bh(&ifp->lock); 2075 } 2076 ipv6_del_addr(ifp); 2077 } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) { 2078 spin_lock_bh(&ifp->lock); 2079 addrconf_del_dad_work(ifp); 2080 ifp->flags |= IFA_F_TENTATIVE; 2081 if (dad_failed) 2082 ifp->flags &= ~IFA_F_OPTIMISTIC; 2083 spin_unlock_bh(&ifp->lock); 2084 if (dad_failed) 2085 ipv6_ifa_notify(0, ifp); 2086 in6_ifa_put(ifp); 2087 } else { 2088 ipv6_del_addr(ifp); 2089 } 2090 } 2091 2092 static int addrconf_dad_end(struct inet6_ifaddr *ifp) 2093 { 2094 int err = -ENOENT; 2095 2096 spin_lock_bh(&ifp->lock); 2097 if (ifp->state == INET6_IFADDR_STATE_DAD) { 2098 ifp->state = INET6_IFADDR_STATE_POSTDAD; 2099 err = 0; 2100 } 2101 spin_unlock_bh(&ifp->lock); 2102 2103 return err; 2104 } 2105 2106 void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp) 2107 { 2108 struct inet6_dev *idev = ifp->idev; 2109 struct net *net = dev_net(idev->dev); 2110 2111 if (addrconf_dad_end(ifp)) { 2112 in6_ifa_put(ifp); 2113 return; 2114 } 2115 2116 net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n", 2117 ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source); 2118 2119 spin_lock_bh(&ifp->lock); 2120 2121 if (ifp->flags & IFA_F_STABLE_PRIVACY) { 2122 struct in6_addr new_addr; 2123 struct inet6_ifaddr *ifp2; 2124 int retries = ifp->stable_privacy_retry + 1; 2125 struct ifa6_config cfg = { 2126 .pfx = &new_addr, 2127 .plen = ifp->prefix_len, 2128 .ifa_flags = ifp->flags, 2129 .valid_lft = ifp->valid_lft, 2130 .preferred_lft = ifp->prefered_lft, 2131 .scope = ifp->scope, 2132 }; 2133 2134 if (retries > net->ipv6.sysctl.idgen_retries) { 2135 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n", 2136 ifp->idev->dev->name); 2137 goto errdad; 2138 } 2139 2140 new_addr = ifp->addr; 2141 if (ipv6_generate_stable_address(&new_addr, retries, 2142 idev)) 2143 goto errdad; 2144 2145 spin_unlock_bh(&ifp->lock); 2146 2147 if (idev->cnf.max_addresses && 2148 ipv6_count_addresses(idev) >= 2149 idev->cnf.max_addresses) 2150 goto lock_errdad; 2151 2152 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n", 2153 ifp->idev->dev->name); 2154 2155 ifp2 = ipv6_add_addr(idev, &cfg, false, NULL); 2156 if (IS_ERR(ifp2)) 2157 goto lock_errdad; 2158 2159 spin_lock_bh(&ifp2->lock); 2160 ifp2->stable_privacy_retry = retries; 2161 ifp2->state = INET6_IFADDR_STATE_PREDAD; 2162 spin_unlock_bh(&ifp2->lock); 2163 2164 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay); 2165 in6_ifa_put(ifp2); 2166 lock_errdad: 2167 spin_lock_bh(&ifp->lock); 2168 } 2169 2170 errdad: 2171 /* transition from _POSTDAD to _ERRDAD */ 2172 ifp->state = INET6_IFADDR_STATE_ERRDAD; 2173 spin_unlock_bh(&ifp->lock); 2174 2175 addrconf_mod_dad_work(ifp, 0); 2176 in6_ifa_put(ifp); 2177 } 2178 2179 /* Join to solicited addr multicast group. 2180 * caller must hold RTNL */ 2181 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) 2182 { 2183 struct in6_addr maddr; 2184 2185 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 2186 return; 2187 2188 addrconf_addr_solict_mult(addr, &maddr); 2189 ipv6_dev_mc_inc(dev, &maddr); 2190 } 2191 2192 /* caller must hold RTNL */ 2193 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) 2194 { 2195 struct in6_addr maddr; 2196 2197 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 2198 return; 2199 2200 addrconf_addr_solict_mult(addr, &maddr); 2201 __ipv6_dev_mc_dec(idev, &maddr); 2202 } 2203 2204 /* caller must hold RTNL */ 2205 static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 2206 { 2207 struct in6_addr addr; 2208 2209 if (ifp->prefix_len >= 127) /* RFC 6164 */ 2210 return; 2211 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 2212 if (ipv6_addr_any(&addr)) 2213 return; 2214 __ipv6_dev_ac_inc(ifp->idev, &addr); 2215 } 2216 2217 /* caller must hold RTNL */ 2218 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 2219 { 2220 struct in6_addr addr; 2221 2222 if (ifp->prefix_len >= 127) /* RFC 6164 */ 2223 return; 2224 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 2225 if (ipv6_addr_any(&addr)) 2226 return; 2227 __ipv6_dev_ac_dec(ifp->idev, &addr); 2228 } 2229 2230 static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev) 2231 { 2232 switch (dev->addr_len) { 2233 case ETH_ALEN: 2234 memcpy(eui, dev->dev_addr, 3); 2235 eui[3] = 0xFF; 2236 eui[4] = 0xFE; 2237 memcpy(eui + 5, dev->dev_addr + 3, 3); 2238 break; 2239 case EUI64_ADDR_LEN: 2240 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN); 2241 eui[0] ^= 2; 2242 break; 2243 default: 2244 return -1; 2245 } 2246 2247 return 0; 2248 } 2249 2250 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) 2251 { 2252 const union fwnet_hwaddr *ha; 2253 2254 if (dev->addr_len != FWNET_ALEN) 2255 return -1; 2256 2257 ha = (const union fwnet_hwaddr *)dev->dev_addr; 2258 2259 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); 2260 eui[0] ^= 2; 2261 return 0; 2262 } 2263 2264 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 2265 { 2266 /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 2267 if (dev->addr_len != ARCNET_ALEN) 2268 return -1; 2269 memset(eui, 0, 7); 2270 eui[7] = *(u8 *)dev->dev_addr; 2271 return 0; 2272 } 2273 2274 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 2275 { 2276 if (dev->addr_len != INFINIBAND_ALEN) 2277 return -1; 2278 memcpy(eui, dev->dev_addr + 12, 8); 2279 eui[0] |= 2; 2280 return 0; 2281 } 2282 2283 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 2284 { 2285 if (addr == 0) 2286 return -1; 2287 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 2288 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 2289 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 2290 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 2291 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 2292 ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 2293 eui[1] = 0; 2294 eui[2] = 0x5E; 2295 eui[3] = 0xFE; 2296 memcpy(eui + 4, &addr, 4); 2297 return 0; 2298 } 2299 2300 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 2301 { 2302 if (dev->priv_flags & IFF_ISATAP) 2303 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2304 return -1; 2305 } 2306 2307 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) 2308 { 2309 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 2310 } 2311 2312 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev) 2313 { 2314 memcpy(eui, dev->perm_addr, 3); 2315 memcpy(eui + 5, dev->perm_addr + 3, 3); 2316 eui[3] = 0xFF; 2317 eui[4] = 0xFE; 2318 eui[0] ^= 2; 2319 return 0; 2320 } 2321 2322 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 2323 { 2324 switch (dev->type) { 2325 case ARPHRD_ETHER: 2326 case ARPHRD_FDDI: 2327 return addrconf_ifid_eui48(eui, dev); 2328 case ARPHRD_ARCNET: 2329 return addrconf_ifid_arcnet(eui, dev); 2330 case ARPHRD_INFINIBAND: 2331 return addrconf_ifid_infiniband(eui, dev); 2332 case ARPHRD_SIT: 2333 return addrconf_ifid_sit(eui, dev); 2334 case ARPHRD_IPGRE: 2335 case ARPHRD_TUNNEL: 2336 return addrconf_ifid_gre(eui, dev); 2337 case ARPHRD_6LOWPAN: 2338 return addrconf_ifid_6lowpan(eui, dev); 2339 case ARPHRD_IEEE1394: 2340 return addrconf_ifid_ieee1394(eui, dev); 2341 case ARPHRD_TUNNEL6: 2342 case ARPHRD_IP6GRE: 2343 case ARPHRD_RAWIP: 2344 return addrconf_ifid_ip6tnl(eui, dev); 2345 } 2346 return -1; 2347 } 2348 2349 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 2350 { 2351 int err = -1; 2352 struct inet6_ifaddr *ifp; 2353 2354 read_lock_bh(&idev->lock); 2355 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 2356 if (ifp->scope > IFA_LINK) 2357 break; 2358 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 2359 memcpy(eui, ifp->addr.s6_addr+8, 8); 2360 err = 0; 2361 break; 2362 } 2363 } 2364 read_unlock_bh(&idev->lock); 2365 return err; 2366 } 2367 2368 /* Generation of a randomized Interface Identifier 2369 * draft-ietf-6man-rfc4941bis, Section 3.3.1 2370 */ 2371 2372 static void ipv6_gen_rnd_iid(struct in6_addr *addr) 2373 { 2374 regen: 2375 get_random_bytes(&addr->s6_addr[8], 8); 2376 2377 /* <draft-ietf-6man-rfc4941bis-08.txt>, Section 3.3.1: 2378 * check if generated address is not inappropriate: 2379 * 2380 * - Reserved IPv6 Interface Identifiers 2381 * - XXX: already assigned to an address on the device 2382 */ 2383 2384 /* Subnet-router anycast: 0000:0000:0000:0000 */ 2385 if (!(addr->s6_addr32[2] | addr->s6_addr32[3])) 2386 goto regen; 2387 2388 /* IANA Ethernet block: 0200:5EFF:FE00:0000-0200:5EFF:FE00:5212 2389 * Proxy Mobile IPv6: 0200:5EFF:FE00:5213 2390 * IANA Ethernet block: 0200:5EFF:FE00:5214-0200:5EFF:FEFF:FFFF 2391 */ 2392 if (ntohl(addr->s6_addr32[2]) == 0x02005eff && 2393 (ntohl(addr->s6_addr32[3]) & 0Xff000000) == 0xfe000000) 2394 goto regen; 2395 2396 /* Reserved subnet anycast addresses */ 2397 if (ntohl(addr->s6_addr32[2]) == 0xfdffffff && 2398 ntohl(addr->s6_addr32[3]) >= 0Xffffff80) 2399 goto regen; 2400 } 2401 2402 /* 2403 * Add prefix route. 2404 */ 2405 2406 static void 2407 addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric, 2408 struct net_device *dev, unsigned long expires, 2409 u32 flags, gfp_t gfp_flags) 2410 { 2411 struct fib6_config cfg = { 2412 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX, 2413 .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF, 2414 .fc_ifindex = dev->ifindex, 2415 .fc_expires = expires, 2416 .fc_dst_len = plen, 2417 .fc_flags = RTF_UP | flags, 2418 .fc_nlinfo.nl_net = dev_net(dev), 2419 .fc_protocol = RTPROT_KERNEL, 2420 .fc_type = RTN_UNICAST, 2421 }; 2422 2423 cfg.fc_dst = *pfx; 2424 2425 /* Prevent useless cloning on PtP SIT. 2426 This thing is done here expecting that the whole 2427 class of non-broadcast devices need not cloning. 2428 */ 2429 #if IS_ENABLED(CONFIG_IPV6_SIT) 2430 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 2431 cfg.fc_flags |= RTF_NONEXTHOP; 2432 #endif 2433 2434 ip6_route_add(&cfg, gfp_flags, NULL); 2435 } 2436 2437 2438 static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 2439 int plen, 2440 const struct net_device *dev, 2441 u32 flags, u32 noflags, 2442 bool no_gw) 2443 { 2444 struct fib6_node *fn; 2445 struct fib6_info *rt = NULL; 2446 struct fib6_table *table; 2447 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX; 2448 2449 table = fib6_get_table(dev_net(dev), tb_id); 2450 if (!table) 2451 return NULL; 2452 2453 rcu_read_lock(); 2454 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true); 2455 if (!fn) 2456 goto out; 2457 2458 for_each_fib6_node_rt_rcu(fn) { 2459 /* prefix routes only use builtin fib6_nh */ 2460 if (rt->nh) 2461 continue; 2462 2463 if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex) 2464 continue; 2465 if (no_gw && rt->fib6_nh->fib_nh_gw_family) 2466 continue; 2467 if ((rt->fib6_flags & flags) != flags) 2468 continue; 2469 if ((rt->fib6_flags & noflags) != 0) 2470 continue; 2471 if (!fib6_info_hold_safe(rt)) 2472 continue; 2473 break; 2474 } 2475 out: 2476 rcu_read_unlock(); 2477 return rt; 2478 } 2479 2480 2481 /* Create "default" multicast route to the interface */ 2482 2483 static void addrconf_add_mroute(struct net_device *dev) 2484 { 2485 struct fib6_config cfg = { 2486 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL, 2487 .fc_metric = IP6_RT_PRIO_ADDRCONF, 2488 .fc_ifindex = dev->ifindex, 2489 .fc_dst_len = 8, 2490 .fc_flags = RTF_UP, 2491 .fc_type = RTN_MULTICAST, 2492 .fc_nlinfo.nl_net = dev_net(dev), 2493 .fc_protocol = RTPROT_KERNEL, 2494 }; 2495 2496 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 2497 2498 ip6_route_add(&cfg, GFP_KERNEL, NULL); 2499 } 2500 2501 static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 2502 { 2503 struct inet6_dev *idev; 2504 2505 ASSERT_RTNL(); 2506 2507 idev = ipv6_find_idev(dev); 2508 if (IS_ERR(idev)) 2509 return idev; 2510 2511 if (idev->cnf.disable_ipv6) 2512 return ERR_PTR(-EACCES); 2513 2514 /* Add default multicast route */ 2515 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev)) 2516 addrconf_add_mroute(dev); 2517 2518 return idev; 2519 } 2520 2521 static void manage_tempaddrs(struct inet6_dev *idev, 2522 struct inet6_ifaddr *ifp, 2523 __u32 valid_lft, __u32 prefered_lft, 2524 bool create, unsigned long now) 2525 { 2526 u32 flags; 2527 struct inet6_ifaddr *ift; 2528 2529 read_lock_bh(&idev->lock); 2530 /* update all temporary addresses in the list */ 2531 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) { 2532 int age, max_valid, max_prefered; 2533 2534 if (ifp != ift->ifpub) 2535 continue; 2536 2537 /* RFC 4941 section 3.3: 2538 * If a received option will extend the lifetime of a public 2539 * address, the lifetimes of temporary addresses should 2540 * be extended, subject to the overall constraint that no 2541 * temporary addresses should ever remain "valid" or "preferred" 2542 * for a time longer than (TEMP_VALID_LIFETIME) or 2543 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively. 2544 */ 2545 age = (now - ift->cstamp) / HZ; 2546 max_valid = idev->cnf.temp_valid_lft - age; 2547 if (max_valid < 0) 2548 max_valid = 0; 2549 2550 max_prefered = idev->cnf.temp_prefered_lft - 2551 idev->desync_factor - age; 2552 if (max_prefered < 0) 2553 max_prefered = 0; 2554 2555 if (valid_lft > max_valid) 2556 valid_lft = max_valid; 2557 2558 if (prefered_lft > max_prefered) 2559 prefered_lft = max_prefered; 2560 2561 spin_lock(&ift->lock); 2562 flags = ift->flags; 2563 ift->valid_lft = valid_lft; 2564 ift->prefered_lft = prefered_lft; 2565 ift->tstamp = now; 2566 if (prefered_lft > 0) 2567 ift->flags &= ~IFA_F_DEPRECATED; 2568 2569 spin_unlock(&ift->lock); 2570 if (!(flags&IFA_F_TENTATIVE)) 2571 ipv6_ifa_notify(0, ift); 2572 } 2573 2574 /* Also create a temporary address if it's enabled but no temporary 2575 * address currently exists. 2576 * However, we get called with valid_lft == 0, prefered_lft == 0, create == false 2577 * as part of cleanup (ie. deleting the mngtmpaddr). 2578 * We don't want that to result in creating a new temporary ip address. 2579 */ 2580 if (list_empty(&idev->tempaddr_list) && (valid_lft || prefered_lft)) 2581 create = true; 2582 2583 if (create && idev->cnf.use_tempaddr > 0) { 2584 /* When a new public address is created as described 2585 * in [ADDRCONF], also create a new temporary address. 2586 */ 2587 read_unlock_bh(&idev->lock); 2588 ipv6_create_tempaddr(ifp, false); 2589 } else { 2590 read_unlock_bh(&idev->lock); 2591 } 2592 } 2593 2594 static bool is_addr_mode_generate_stable(struct inet6_dev *idev) 2595 { 2596 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY || 2597 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM; 2598 } 2599 2600 int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev, 2601 const struct prefix_info *pinfo, 2602 struct inet6_dev *in6_dev, 2603 const struct in6_addr *addr, int addr_type, 2604 u32 addr_flags, bool sllao, bool tokenized, 2605 __u32 valid_lft, u32 prefered_lft) 2606 { 2607 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1); 2608 int create = 0, update_lft = 0; 2609 2610 if (!ifp && valid_lft) { 2611 int max_addresses = in6_dev->cnf.max_addresses; 2612 struct ifa6_config cfg = { 2613 .pfx = addr, 2614 .plen = pinfo->prefix_len, 2615 .ifa_flags = addr_flags, 2616 .valid_lft = valid_lft, 2617 .preferred_lft = prefered_lft, 2618 .scope = addr_type & IPV6_ADDR_SCOPE_MASK, 2619 .ifa_proto = IFAPROT_KERNEL_RA 2620 }; 2621 2622 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 2623 if ((net->ipv6.devconf_all->optimistic_dad || 2624 in6_dev->cnf.optimistic_dad) && 2625 !net->ipv6.devconf_all->forwarding && sllao) 2626 cfg.ifa_flags |= IFA_F_OPTIMISTIC; 2627 #endif 2628 2629 /* Do not allow to create too much of autoconfigured 2630 * addresses; this would be too easy way to crash kernel. 2631 */ 2632 if (!max_addresses || 2633 ipv6_count_addresses(in6_dev) < max_addresses) 2634 ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL); 2635 2636 if (IS_ERR_OR_NULL(ifp)) 2637 return -1; 2638 2639 create = 1; 2640 spin_lock_bh(&ifp->lock); 2641 ifp->flags |= IFA_F_MANAGETEMPADDR; 2642 ifp->cstamp = jiffies; 2643 ifp->tokenized = tokenized; 2644 spin_unlock_bh(&ifp->lock); 2645 addrconf_dad_start(ifp); 2646 } 2647 2648 if (ifp) { 2649 u32 flags; 2650 unsigned long now; 2651 u32 stored_lft; 2652 2653 /* update lifetime (RFC2462 5.5.3 e) */ 2654 spin_lock_bh(&ifp->lock); 2655 now = jiffies; 2656 if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 2657 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 2658 else 2659 stored_lft = 0; 2660 if (!create && stored_lft) { 2661 const u32 minimum_lft = min_t(u32, 2662 stored_lft, MIN_VALID_LIFETIME); 2663 valid_lft = max(valid_lft, minimum_lft); 2664 2665 /* RFC4862 Section 5.5.3e: 2666 * "Note that the preferred lifetime of the 2667 * corresponding address is always reset to 2668 * the Preferred Lifetime in the received 2669 * Prefix Information option, regardless of 2670 * whether the valid lifetime is also reset or 2671 * ignored." 2672 * 2673 * So we should always update prefered_lft here. 2674 */ 2675 update_lft = 1; 2676 } 2677 2678 if (update_lft) { 2679 ifp->valid_lft = valid_lft; 2680 ifp->prefered_lft = prefered_lft; 2681 ifp->tstamp = now; 2682 flags = ifp->flags; 2683 ifp->flags &= ~IFA_F_DEPRECATED; 2684 spin_unlock_bh(&ifp->lock); 2685 2686 if (!(flags&IFA_F_TENTATIVE)) 2687 ipv6_ifa_notify(0, ifp); 2688 } else 2689 spin_unlock_bh(&ifp->lock); 2690 2691 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, 2692 create, now); 2693 2694 in6_ifa_put(ifp); 2695 addrconf_verify(net); 2696 } 2697 2698 return 0; 2699 } 2700 EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr); 2701 2702 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) 2703 { 2704 struct prefix_info *pinfo; 2705 __u32 valid_lft; 2706 __u32 prefered_lft; 2707 int addr_type, err; 2708 u32 addr_flags = 0; 2709 struct inet6_dev *in6_dev; 2710 struct net *net = dev_net(dev); 2711 2712 pinfo = (struct prefix_info *) opt; 2713 2714 if (len < sizeof(struct prefix_info)) { 2715 netdev_dbg(dev, "addrconf: prefix option too short\n"); 2716 return; 2717 } 2718 2719 /* 2720 * Validation checks ([ADDRCONF], page 19) 2721 */ 2722 2723 addr_type = ipv6_addr_type(&pinfo->prefix); 2724 2725 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 2726 return; 2727 2728 valid_lft = ntohl(pinfo->valid); 2729 prefered_lft = ntohl(pinfo->prefered); 2730 2731 if (prefered_lft > valid_lft) { 2732 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); 2733 return; 2734 } 2735 2736 in6_dev = in6_dev_get(dev); 2737 2738 if (!in6_dev) { 2739 net_dbg_ratelimited("addrconf: device %s not configured\n", 2740 dev->name); 2741 return; 2742 } 2743 2744 if (valid_lft != 0 && valid_lft < in6_dev->cnf.accept_ra_min_lft) 2745 goto put; 2746 2747 /* 2748 * Two things going on here: 2749 * 1) Add routes for on-link prefixes 2750 * 2) Configure prefixes with the auto flag set 2751 */ 2752 2753 if (pinfo->onlink) { 2754 struct fib6_info *rt; 2755 unsigned long rt_expires; 2756 2757 /* Avoid arithmetic overflow. Really, we could 2758 * save rt_expires in seconds, likely valid_lft, 2759 * but it would require division in fib gc, that it 2760 * not good. 2761 */ 2762 if (HZ > USER_HZ) 2763 rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 2764 else 2765 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 2766 2767 if (addrconf_finite_timeout(rt_expires)) 2768 rt_expires *= HZ; 2769 2770 rt = addrconf_get_prefix_route(&pinfo->prefix, 2771 pinfo->prefix_len, 2772 dev, 2773 RTF_ADDRCONF | RTF_PREFIX_RT, 2774 RTF_DEFAULT, true); 2775 2776 if (rt) { 2777 /* Autoconf prefix route */ 2778 if (valid_lft == 0) { 2779 ip6_del_rt(net, rt, false); 2780 rt = NULL; 2781 } else if (addrconf_finite_timeout(rt_expires)) { 2782 /* not infinity */ 2783 fib6_set_expires(rt, jiffies + rt_expires); 2784 } else { 2785 fib6_clean_expires(rt); 2786 } 2787 } else if (valid_lft) { 2788 clock_t expires = 0; 2789 int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 2790 if (addrconf_finite_timeout(rt_expires)) { 2791 /* not infinity */ 2792 flags |= RTF_EXPIRES; 2793 expires = jiffies_to_clock_t(rt_expires); 2794 } 2795 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 2796 0, dev, expires, flags, 2797 GFP_ATOMIC); 2798 } 2799 fib6_info_release(rt); 2800 } 2801 2802 /* Try to figure out our local address for this prefix */ 2803 2804 if (pinfo->autoconf && in6_dev->cnf.autoconf) { 2805 struct in6_addr addr; 2806 bool tokenized = false, dev_addr_generated = false; 2807 2808 if (pinfo->prefix_len == 64) { 2809 memcpy(&addr, &pinfo->prefix, 8); 2810 2811 if (!ipv6_addr_any(&in6_dev->token)) { 2812 read_lock_bh(&in6_dev->lock); 2813 memcpy(addr.s6_addr + 8, 2814 in6_dev->token.s6_addr + 8, 8); 2815 read_unlock_bh(&in6_dev->lock); 2816 tokenized = true; 2817 } else if (is_addr_mode_generate_stable(in6_dev) && 2818 !ipv6_generate_stable_address(&addr, 0, 2819 in6_dev)) { 2820 addr_flags |= IFA_F_STABLE_PRIVACY; 2821 goto ok; 2822 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 2823 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 2824 goto put; 2825 } else { 2826 dev_addr_generated = true; 2827 } 2828 goto ok; 2829 } 2830 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", 2831 pinfo->prefix_len); 2832 goto put; 2833 2834 ok: 2835 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, 2836 &addr, addr_type, 2837 addr_flags, sllao, 2838 tokenized, valid_lft, 2839 prefered_lft); 2840 if (err) 2841 goto put; 2842 2843 /* Ignore error case here because previous prefix add addr was 2844 * successful which will be notified. 2845 */ 2846 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr, 2847 addr_type, addr_flags, sllao, 2848 tokenized, valid_lft, 2849 prefered_lft, 2850 dev_addr_generated); 2851 } 2852 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 2853 put: 2854 in6_dev_put(in6_dev); 2855 } 2856 2857 static int addrconf_set_sit_dstaddr(struct net *net, struct net_device *dev, 2858 struct in6_ifreq *ireq) 2859 { 2860 struct ip_tunnel_parm p = { }; 2861 int err; 2862 2863 if (!(ipv6_addr_type(&ireq->ifr6_addr) & IPV6_ADDR_COMPATv4)) 2864 return -EADDRNOTAVAIL; 2865 2866 p.iph.daddr = ireq->ifr6_addr.s6_addr32[3]; 2867 p.iph.version = 4; 2868 p.iph.ihl = 5; 2869 p.iph.protocol = IPPROTO_IPV6; 2870 p.iph.ttl = 64; 2871 2872 if (!dev->netdev_ops->ndo_tunnel_ctl) 2873 return -EOPNOTSUPP; 2874 err = dev->netdev_ops->ndo_tunnel_ctl(dev, &p, SIOCADDTUNNEL); 2875 if (err) 2876 return err; 2877 2878 dev = __dev_get_by_name(net, p.name); 2879 if (!dev) 2880 return -ENOBUFS; 2881 return dev_open(dev, NULL); 2882 } 2883 2884 /* 2885 * Set destination address. 2886 * Special case for SIT interfaces where we create a new "virtual" 2887 * device. 2888 */ 2889 int addrconf_set_dstaddr(struct net *net, void __user *arg) 2890 { 2891 struct net_device *dev; 2892 struct in6_ifreq ireq; 2893 int err = -ENODEV; 2894 2895 if (!IS_ENABLED(CONFIG_IPV6_SIT)) 2896 return -ENODEV; 2897 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 2898 return -EFAULT; 2899 2900 rtnl_lock(); 2901 dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 2902 if (dev && dev->type == ARPHRD_SIT) 2903 err = addrconf_set_sit_dstaddr(net, dev, &ireq); 2904 rtnl_unlock(); 2905 return err; 2906 } 2907 2908 static int ipv6_mc_config(struct sock *sk, bool join, 2909 const struct in6_addr *addr, int ifindex) 2910 { 2911 int ret; 2912 2913 ASSERT_RTNL(); 2914 2915 lock_sock(sk); 2916 if (join) 2917 ret = ipv6_sock_mc_join(sk, ifindex, addr); 2918 else 2919 ret = ipv6_sock_mc_drop(sk, ifindex, addr); 2920 release_sock(sk); 2921 2922 return ret; 2923 } 2924 2925 /* 2926 * Manual configuration of address on an interface 2927 */ 2928 static int inet6_addr_add(struct net *net, int ifindex, 2929 struct ifa6_config *cfg, 2930 struct netlink_ext_ack *extack) 2931 { 2932 struct inet6_ifaddr *ifp; 2933 struct inet6_dev *idev; 2934 struct net_device *dev; 2935 unsigned long timeout; 2936 clock_t expires; 2937 u32 flags; 2938 2939 ASSERT_RTNL(); 2940 2941 if (cfg->plen > 128) { 2942 NL_SET_ERR_MSG_MOD(extack, "Invalid prefix length"); 2943 return -EINVAL; 2944 } 2945 2946 /* check the lifetime */ 2947 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) { 2948 NL_SET_ERR_MSG_MOD(extack, "address lifetime invalid"); 2949 return -EINVAL; 2950 } 2951 2952 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64) { 2953 NL_SET_ERR_MSG_MOD(extack, "address with \"mngtmpaddr\" flag must have a prefix length of 64"); 2954 return -EINVAL; 2955 } 2956 2957 dev = __dev_get_by_index(net, ifindex); 2958 if (!dev) 2959 return -ENODEV; 2960 2961 idev = addrconf_add_dev(dev); 2962 if (IS_ERR(idev)) { 2963 NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device"); 2964 return PTR_ERR(idev); 2965 } 2966 2967 if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { 2968 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, 2969 true, cfg->pfx, ifindex); 2970 2971 if (ret < 0) { 2972 NL_SET_ERR_MSG_MOD(extack, "Multicast auto join failed"); 2973 return ret; 2974 } 2975 } 2976 2977 cfg->scope = ipv6_addr_scope(cfg->pfx); 2978 2979 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); 2980 if (addrconf_finite_timeout(timeout)) { 2981 expires = jiffies_to_clock_t(timeout * HZ); 2982 cfg->valid_lft = timeout; 2983 flags = RTF_EXPIRES; 2984 } else { 2985 expires = 0; 2986 flags = 0; 2987 cfg->ifa_flags |= IFA_F_PERMANENT; 2988 } 2989 2990 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); 2991 if (addrconf_finite_timeout(timeout)) { 2992 if (timeout == 0) 2993 cfg->ifa_flags |= IFA_F_DEPRECATED; 2994 cfg->preferred_lft = timeout; 2995 } 2996 2997 ifp = ipv6_add_addr(idev, cfg, true, extack); 2998 if (!IS_ERR(ifp)) { 2999 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { 3000 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 3001 ifp->rt_priority, dev, expires, 3002 flags, GFP_KERNEL); 3003 } 3004 3005 /* Send a netlink notification if DAD is enabled and 3006 * optimistic flag is not set 3007 */ 3008 if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD))) 3009 ipv6_ifa_notify(0, ifp); 3010 /* 3011 * Note that section 3.1 of RFC 4429 indicates 3012 * that the Optimistic flag should not be set for 3013 * manually configured addresses 3014 */ 3015 addrconf_dad_start(ifp); 3016 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR) 3017 manage_tempaddrs(idev, ifp, cfg->valid_lft, 3018 cfg->preferred_lft, true, jiffies); 3019 in6_ifa_put(ifp); 3020 addrconf_verify_rtnl(net); 3021 return 0; 3022 } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { 3023 ipv6_mc_config(net->ipv6.mc_autojoin_sk, false, 3024 cfg->pfx, ifindex); 3025 } 3026 3027 return PTR_ERR(ifp); 3028 } 3029 3030 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, 3031 const struct in6_addr *pfx, unsigned int plen, 3032 struct netlink_ext_ack *extack) 3033 { 3034 struct inet6_ifaddr *ifp; 3035 struct inet6_dev *idev; 3036 struct net_device *dev; 3037 3038 if (plen > 128) { 3039 NL_SET_ERR_MSG_MOD(extack, "Invalid prefix length"); 3040 return -EINVAL; 3041 } 3042 3043 dev = __dev_get_by_index(net, ifindex); 3044 if (!dev) { 3045 NL_SET_ERR_MSG_MOD(extack, "Unable to find the interface"); 3046 return -ENODEV; 3047 } 3048 3049 idev = __in6_dev_get(dev); 3050 if (!idev) { 3051 NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device"); 3052 return -ENXIO; 3053 } 3054 3055 read_lock_bh(&idev->lock); 3056 list_for_each_entry(ifp, &idev->addr_list, if_list) { 3057 if (ifp->prefix_len == plen && 3058 ipv6_addr_equal(pfx, &ifp->addr)) { 3059 in6_ifa_hold(ifp); 3060 read_unlock_bh(&idev->lock); 3061 3062 if (!(ifp->flags & IFA_F_TEMPORARY) && 3063 (ifa_flags & IFA_F_MANAGETEMPADDR)) 3064 manage_tempaddrs(idev, ifp, 0, 0, false, 3065 jiffies); 3066 ipv6_del_addr(ifp); 3067 addrconf_verify_rtnl(net); 3068 if (ipv6_addr_is_multicast(pfx)) { 3069 ipv6_mc_config(net->ipv6.mc_autojoin_sk, 3070 false, pfx, dev->ifindex); 3071 } 3072 return 0; 3073 } 3074 } 3075 read_unlock_bh(&idev->lock); 3076 3077 NL_SET_ERR_MSG_MOD(extack, "address not found"); 3078 return -EADDRNOTAVAIL; 3079 } 3080 3081 3082 int addrconf_add_ifaddr(struct net *net, void __user *arg) 3083 { 3084 struct ifa6_config cfg = { 3085 .ifa_flags = IFA_F_PERMANENT, 3086 .preferred_lft = INFINITY_LIFE_TIME, 3087 .valid_lft = INFINITY_LIFE_TIME, 3088 }; 3089 struct in6_ifreq ireq; 3090 int err; 3091 3092 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 3093 return -EPERM; 3094 3095 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 3096 return -EFAULT; 3097 3098 cfg.pfx = &ireq.ifr6_addr; 3099 cfg.plen = ireq.ifr6_prefixlen; 3100 3101 rtnl_lock(); 3102 err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL); 3103 rtnl_unlock(); 3104 return err; 3105 } 3106 3107 int addrconf_del_ifaddr(struct net *net, void __user *arg) 3108 { 3109 struct in6_ifreq ireq; 3110 int err; 3111 3112 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 3113 return -EPERM; 3114 3115 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 3116 return -EFAULT; 3117 3118 rtnl_lock(); 3119 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, 3120 ireq.ifr6_prefixlen, NULL); 3121 rtnl_unlock(); 3122 return err; 3123 } 3124 3125 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 3126 int plen, int scope, u8 proto) 3127 { 3128 struct inet6_ifaddr *ifp; 3129 struct ifa6_config cfg = { 3130 .pfx = addr, 3131 .plen = plen, 3132 .ifa_flags = IFA_F_PERMANENT, 3133 .valid_lft = INFINITY_LIFE_TIME, 3134 .preferred_lft = INFINITY_LIFE_TIME, 3135 .scope = scope, 3136 .ifa_proto = proto 3137 }; 3138 3139 ifp = ipv6_add_addr(idev, &cfg, true, NULL); 3140 if (!IS_ERR(ifp)) { 3141 spin_lock_bh(&ifp->lock); 3142 ifp->flags &= ~IFA_F_TENTATIVE; 3143 spin_unlock_bh(&ifp->lock); 3144 rt_genid_bump_ipv6(dev_net(idev->dev)); 3145 ipv6_ifa_notify(RTM_NEWADDR, ifp); 3146 in6_ifa_put(ifp); 3147 } 3148 } 3149 3150 #if IS_ENABLED(CONFIG_IPV6_SIT) || IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE) 3151 static void add_v4_addrs(struct inet6_dev *idev) 3152 { 3153 struct in6_addr addr; 3154 struct net_device *dev; 3155 struct net *net = dev_net(idev->dev); 3156 int scope, plen, offset = 0; 3157 u32 pflags = 0; 3158 3159 ASSERT_RTNL(); 3160 3161 memset(&addr, 0, sizeof(struct in6_addr)); 3162 /* in case of IP6GRE the dev_addr is an IPv6 and therefore we use only the last 4 bytes */ 3163 if (idev->dev->addr_len == sizeof(struct in6_addr)) 3164 offset = sizeof(struct in6_addr) - 4; 3165 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr + offset, 4); 3166 3167 if (!(idev->dev->flags & IFF_POINTOPOINT) && idev->dev->type == ARPHRD_SIT) { 3168 scope = IPV6_ADDR_COMPATv4; 3169 plen = 96; 3170 pflags |= RTF_NONEXTHOP; 3171 } else { 3172 if (idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_NONE) 3173 return; 3174 3175 addr.s6_addr32[0] = htonl(0xfe800000); 3176 scope = IFA_LINK; 3177 plen = 64; 3178 } 3179 3180 if (addr.s6_addr32[3]) { 3181 add_addr(idev, &addr, plen, scope, IFAPROT_UNSPEC); 3182 addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags, 3183 GFP_KERNEL); 3184 return; 3185 } 3186 3187 for_each_netdev(net, dev) { 3188 struct in_device *in_dev = __in_dev_get_rtnl(dev); 3189 if (in_dev && (dev->flags & IFF_UP)) { 3190 struct in_ifaddr *ifa; 3191 int flag = scope; 3192 3193 in_dev_for_each_ifa_rtnl(ifa, in_dev) { 3194 addr.s6_addr32[3] = ifa->ifa_local; 3195 3196 if (ifa->ifa_scope == RT_SCOPE_LINK) 3197 continue; 3198 if (ifa->ifa_scope >= RT_SCOPE_HOST) { 3199 if (idev->dev->flags&IFF_POINTOPOINT) 3200 continue; 3201 flag |= IFA_HOST; 3202 } 3203 3204 add_addr(idev, &addr, plen, flag, 3205 IFAPROT_UNSPEC); 3206 addrconf_prefix_route(&addr, plen, 0, idev->dev, 3207 0, pflags, GFP_KERNEL); 3208 } 3209 } 3210 } 3211 } 3212 #endif 3213 3214 static void init_loopback(struct net_device *dev) 3215 { 3216 struct inet6_dev *idev; 3217 3218 /* ::1 */ 3219 3220 ASSERT_RTNL(); 3221 3222 idev = ipv6_find_idev(dev); 3223 if (IS_ERR(idev)) { 3224 pr_debug("%s: add_dev failed\n", __func__); 3225 return; 3226 } 3227 3228 add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFAPROT_KERNEL_LO); 3229 } 3230 3231 void addrconf_add_linklocal(struct inet6_dev *idev, 3232 const struct in6_addr *addr, u32 flags) 3233 { 3234 struct ifa6_config cfg = { 3235 .pfx = addr, 3236 .plen = 64, 3237 .ifa_flags = flags | IFA_F_PERMANENT, 3238 .valid_lft = INFINITY_LIFE_TIME, 3239 .preferred_lft = INFINITY_LIFE_TIME, 3240 .scope = IFA_LINK, 3241 .ifa_proto = IFAPROT_KERNEL_LL 3242 }; 3243 struct inet6_ifaddr *ifp; 3244 3245 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 3246 if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad || 3247 idev->cnf.optimistic_dad) && 3248 !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 3249 cfg.ifa_flags |= IFA_F_OPTIMISTIC; 3250 #endif 3251 3252 ifp = ipv6_add_addr(idev, &cfg, true, NULL); 3253 if (!IS_ERR(ifp)) { 3254 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev, 3255 0, 0, GFP_ATOMIC); 3256 addrconf_dad_start(ifp); 3257 in6_ifa_put(ifp); 3258 } 3259 } 3260 EXPORT_SYMBOL_GPL(addrconf_add_linklocal); 3261 3262 static bool ipv6_reserved_interfaceid(struct in6_addr address) 3263 { 3264 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0) 3265 return true; 3266 3267 if (address.s6_addr32[2] == htonl(0x02005eff) && 3268 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000))) 3269 return true; 3270 3271 if (address.s6_addr32[2] == htonl(0xfdffffff) && 3272 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80))) 3273 return true; 3274 3275 return false; 3276 } 3277 3278 static int ipv6_generate_stable_address(struct in6_addr *address, 3279 u8 dad_count, 3280 const struct inet6_dev *idev) 3281 { 3282 static DEFINE_SPINLOCK(lock); 3283 static __u32 digest[SHA1_DIGEST_WORDS]; 3284 static __u32 workspace[SHA1_WORKSPACE_WORDS]; 3285 3286 static union { 3287 char __data[SHA1_BLOCK_SIZE]; 3288 struct { 3289 struct in6_addr secret; 3290 __be32 prefix[2]; 3291 unsigned char hwaddr[MAX_ADDR_LEN]; 3292 u8 dad_count; 3293 } __packed; 3294 } data; 3295 3296 struct in6_addr secret; 3297 struct in6_addr temp; 3298 struct net *net = dev_net(idev->dev); 3299 3300 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data)); 3301 3302 if (idev->cnf.stable_secret.initialized) 3303 secret = idev->cnf.stable_secret.secret; 3304 else if (net->ipv6.devconf_dflt->stable_secret.initialized) 3305 secret = net->ipv6.devconf_dflt->stable_secret.secret; 3306 else 3307 return -1; 3308 3309 retry: 3310 spin_lock_bh(&lock); 3311 3312 sha1_init(digest); 3313 memset(&data, 0, sizeof(data)); 3314 memset(workspace, 0, sizeof(workspace)); 3315 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len); 3316 data.prefix[0] = address->s6_addr32[0]; 3317 data.prefix[1] = address->s6_addr32[1]; 3318 data.secret = secret; 3319 data.dad_count = dad_count; 3320 3321 sha1_transform(digest, data.__data, workspace); 3322 3323 temp = *address; 3324 temp.s6_addr32[2] = (__force __be32)digest[0]; 3325 temp.s6_addr32[3] = (__force __be32)digest[1]; 3326 3327 spin_unlock_bh(&lock); 3328 3329 if (ipv6_reserved_interfaceid(temp)) { 3330 dad_count++; 3331 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries) 3332 return -1; 3333 goto retry; 3334 } 3335 3336 *address = temp; 3337 return 0; 3338 } 3339 3340 static void ipv6_gen_mode_random_init(struct inet6_dev *idev) 3341 { 3342 struct ipv6_stable_secret *s = &idev->cnf.stable_secret; 3343 3344 if (s->initialized) 3345 return; 3346 s = &idev->cnf.stable_secret; 3347 get_random_bytes(&s->secret, sizeof(s->secret)); 3348 s->initialized = true; 3349 } 3350 3351 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) 3352 { 3353 struct in6_addr addr; 3354 3355 /* no link local addresses on L3 master devices */ 3356 if (netif_is_l3_master(idev->dev)) 3357 return; 3358 3359 /* no link local addresses on devices flagged as slaves */ 3360 if (idev->dev->priv_flags & IFF_NO_ADDRCONF) 3361 return; 3362 3363 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 3364 3365 switch (idev->cnf.addr_gen_mode) { 3366 case IN6_ADDR_GEN_MODE_RANDOM: 3367 ipv6_gen_mode_random_init(idev); 3368 fallthrough; 3369 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY: 3370 if (!ipv6_generate_stable_address(&addr, 0, idev)) 3371 addrconf_add_linklocal(idev, &addr, 3372 IFA_F_STABLE_PRIVACY); 3373 else if (prefix_route) 3374 addrconf_prefix_route(&addr, 64, 0, idev->dev, 3375 0, 0, GFP_KERNEL); 3376 break; 3377 case IN6_ADDR_GEN_MODE_EUI64: 3378 /* addrconf_add_linklocal also adds a prefix_route and we 3379 * only need to care about prefix routes if ipv6_generate_eui64 3380 * couldn't generate one. 3381 */ 3382 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) 3383 addrconf_add_linklocal(idev, &addr, 0); 3384 else if (prefix_route) 3385 addrconf_prefix_route(&addr, 64, 0, idev->dev, 3386 0, 0, GFP_KERNEL); 3387 break; 3388 case IN6_ADDR_GEN_MODE_NONE: 3389 default: 3390 /* will not add any link local address */ 3391 break; 3392 } 3393 } 3394 3395 static void addrconf_dev_config(struct net_device *dev) 3396 { 3397 struct inet6_dev *idev; 3398 3399 ASSERT_RTNL(); 3400 3401 if ((dev->type != ARPHRD_ETHER) && 3402 (dev->type != ARPHRD_FDDI) && 3403 (dev->type != ARPHRD_ARCNET) && 3404 (dev->type != ARPHRD_INFINIBAND) && 3405 (dev->type != ARPHRD_IEEE1394) && 3406 (dev->type != ARPHRD_TUNNEL6) && 3407 (dev->type != ARPHRD_6LOWPAN) && 3408 (dev->type != ARPHRD_TUNNEL) && 3409 (dev->type != ARPHRD_NONE) && 3410 (dev->type != ARPHRD_RAWIP)) { 3411 /* Alas, we support only Ethernet autoconfiguration. */ 3412 idev = __in6_dev_get(dev); 3413 if (!IS_ERR_OR_NULL(idev) && dev->flags & IFF_UP && 3414 dev->flags & IFF_MULTICAST) 3415 ipv6_mc_up(idev); 3416 return; 3417 } 3418 3419 idev = addrconf_add_dev(dev); 3420 if (IS_ERR(idev)) 3421 return; 3422 3423 /* this device type has no EUI support */ 3424 if (dev->type == ARPHRD_NONE && 3425 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) 3426 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM; 3427 3428 addrconf_addr_gen(idev, false); 3429 } 3430 3431 #if IS_ENABLED(CONFIG_IPV6_SIT) 3432 static void addrconf_sit_config(struct net_device *dev) 3433 { 3434 struct inet6_dev *idev; 3435 3436 ASSERT_RTNL(); 3437 3438 /* 3439 * Configure the tunnel with one of our IPv4 3440 * addresses... we should configure all of 3441 * our v4 addrs in the tunnel 3442 */ 3443 3444 idev = ipv6_find_idev(dev); 3445 if (IS_ERR(idev)) { 3446 pr_debug("%s: add_dev failed\n", __func__); 3447 return; 3448 } 3449 3450 if (dev->priv_flags & IFF_ISATAP) { 3451 addrconf_addr_gen(idev, false); 3452 return; 3453 } 3454 3455 add_v4_addrs(idev); 3456 3457 if (dev->flags&IFF_POINTOPOINT) 3458 addrconf_add_mroute(dev); 3459 } 3460 #endif 3461 3462 #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE) 3463 static void addrconf_gre_config(struct net_device *dev) 3464 { 3465 struct inet6_dev *idev; 3466 3467 ASSERT_RTNL(); 3468 3469 idev = ipv6_find_idev(dev); 3470 if (IS_ERR(idev)) { 3471 pr_debug("%s: add_dev failed\n", __func__); 3472 return; 3473 } 3474 3475 if (dev->type == ARPHRD_ETHER) { 3476 addrconf_addr_gen(idev, true); 3477 return; 3478 } 3479 3480 add_v4_addrs(idev); 3481 3482 if (dev->flags & IFF_POINTOPOINT) 3483 addrconf_add_mroute(dev); 3484 } 3485 #endif 3486 3487 static void addrconf_init_auto_addrs(struct net_device *dev) 3488 { 3489 switch (dev->type) { 3490 #if IS_ENABLED(CONFIG_IPV6_SIT) 3491 case ARPHRD_SIT: 3492 addrconf_sit_config(dev); 3493 break; 3494 #endif 3495 #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE) 3496 case ARPHRD_IP6GRE: 3497 case ARPHRD_IPGRE: 3498 addrconf_gre_config(dev); 3499 break; 3500 #endif 3501 case ARPHRD_LOOPBACK: 3502 init_loopback(dev); 3503 break; 3504 3505 default: 3506 addrconf_dev_config(dev); 3507 break; 3508 } 3509 } 3510 3511 static int fixup_permanent_addr(struct net *net, 3512 struct inet6_dev *idev, 3513 struct inet6_ifaddr *ifp) 3514 { 3515 /* !fib6_node means the host route was removed from the 3516 * FIB, for example, if 'lo' device is taken down. In that 3517 * case regenerate the host route. 3518 */ 3519 if (!ifp->rt || !ifp->rt->fib6_node) { 3520 struct fib6_info *f6i, *prev; 3521 3522 f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false, 3523 GFP_ATOMIC, NULL); 3524 if (IS_ERR(f6i)) 3525 return PTR_ERR(f6i); 3526 3527 /* ifp->rt can be accessed outside of rtnl */ 3528 spin_lock(&ifp->lock); 3529 prev = ifp->rt; 3530 ifp->rt = f6i; 3531 spin_unlock(&ifp->lock); 3532 3533 fib6_info_release(prev); 3534 } 3535 3536 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) { 3537 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 3538 ifp->rt_priority, idev->dev, 0, 0, 3539 GFP_ATOMIC); 3540 } 3541 3542 if (ifp->state == INET6_IFADDR_STATE_PREDAD) 3543 addrconf_dad_start(ifp); 3544 3545 return 0; 3546 } 3547 3548 static void addrconf_permanent_addr(struct net *net, struct net_device *dev) 3549 { 3550 struct inet6_ifaddr *ifp, *tmp; 3551 struct inet6_dev *idev; 3552 3553 idev = __in6_dev_get(dev); 3554 if (!idev) 3555 return; 3556 3557 write_lock_bh(&idev->lock); 3558 3559 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) { 3560 if ((ifp->flags & IFA_F_PERMANENT) && 3561 fixup_permanent_addr(net, idev, ifp) < 0) { 3562 write_unlock_bh(&idev->lock); 3563 in6_ifa_hold(ifp); 3564 ipv6_del_addr(ifp); 3565 write_lock_bh(&idev->lock); 3566 3567 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n", 3568 idev->dev->name, &ifp->addr); 3569 } 3570 } 3571 3572 write_unlock_bh(&idev->lock); 3573 } 3574 3575 static int addrconf_notify(struct notifier_block *this, unsigned long event, 3576 void *ptr) 3577 { 3578 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3579 struct netdev_notifier_change_info *change_info; 3580 struct netdev_notifier_changeupper_info *info; 3581 struct inet6_dev *idev = __in6_dev_get(dev); 3582 struct net *net = dev_net(dev); 3583 int run_pending = 0; 3584 int err; 3585 3586 switch (event) { 3587 case NETDEV_REGISTER: 3588 if (!idev && dev->mtu >= IPV6_MIN_MTU) { 3589 idev = ipv6_add_dev(dev); 3590 if (IS_ERR(idev)) 3591 return notifier_from_errno(PTR_ERR(idev)); 3592 } 3593 break; 3594 3595 case NETDEV_CHANGEMTU: 3596 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */ 3597 if (dev->mtu < IPV6_MIN_MTU) { 3598 addrconf_ifdown(dev, dev != net->loopback_dev); 3599 break; 3600 } 3601 3602 if (idev) { 3603 rt6_mtu_change(dev, dev->mtu); 3604 idev->cnf.mtu6 = dev->mtu; 3605 break; 3606 } 3607 3608 /* allocate new idev */ 3609 idev = ipv6_add_dev(dev); 3610 if (IS_ERR(idev)) 3611 break; 3612 3613 /* device is still not ready */ 3614 if (!(idev->if_flags & IF_READY)) 3615 break; 3616 3617 run_pending = 1; 3618 fallthrough; 3619 case NETDEV_UP: 3620 case NETDEV_CHANGE: 3621 if (idev && idev->cnf.disable_ipv6) 3622 break; 3623 3624 if (dev->priv_flags & IFF_NO_ADDRCONF) { 3625 if (event == NETDEV_UP && !IS_ERR_OR_NULL(idev) && 3626 dev->flags & IFF_UP && dev->flags & IFF_MULTICAST) 3627 ipv6_mc_up(idev); 3628 break; 3629 } 3630 3631 if (event == NETDEV_UP) { 3632 /* restore routes for permanent addresses */ 3633 addrconf_permanent_addr(net, dev); 3634 3635 if (!addrconf_link_ready(dev)) { 3636 /* device is not ready yet. */ 3637 pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 3638 dev->name); 3639 break; 3640 } 3641 3642 if (!idev && dev->mtu >= IPV6_MIN_MTU) 3643 idev = ipv6_add_dev(dev); 3644 3645 if (!IS_ERR_OR_NULL(idev)) { 3646 idev->if_flags |= IF_READY; 3647 run_pending = 1; 3648 } 3649 } else if (event == NETDEV_CHANGE) { 3650 if (!addrconf_link_ready(dev)) { 3651 /* device is still not ready. */ 3652 rt6_sync_down_dev(dev, event); 3653 break; 3654 } 3655 3656 if (!IS_ERR_OR_NULL(idev)) { 3657 if (idev->if_flags & IF_READY) { 3658 /* device is already configured - 3659 * but resend MLD reports, we might 3660 * have roamed and need to update 3661 * multicast snooping switches 3662 */ 3663 ipv6_mc_up(idev); 3664 change_info = ptr; 3665 if (change_info->flags_changed & IFF_NOARP) 3666 addrconf_dad_run(idev, true); 3667 rt6_sync_up(dev, RTNH_F_LINKDOWN); 3668 break; 3669 } 3670 idev->if_flags |= IF_READY; 3671 } 3672 3673 pr_debug("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 3674 dev->name); 3675 3676 run_pending = 1; 3677 } 3678 3679 addrconf_init_auto_addrs(dev); 3680 3681 if (!IS_ERR_OR_NULL(idev)) { 3682 if (run_pending) 3683 addrconf_dad_run(idev, false); 3684 3685 /* Device has an address by now */ 3686 rt6_sync_up(dev, RTNH_F_DEAD); 3687 3688 /* 3689 * If the MTU changed during the interface down, 3690 * when the interface up, the changed MTU must be 3691 * reflected in the idev as well as routers. 3692 */ 3693 if (idev->cnf.mtu6 != dev->mtu && 3694 dev->mtu >= IPV6_MIN_MTU) { 3695 rt6_mtu_change(dev, dev->mtu); 3696 idev->cnf.mtu6 = dev->mtu; 3697 } 3698 idev->tstamp = jiffies; 3699 inet6_ifinfo_notify(RTM_NEWLINK, idev); 3700 3701 /* 3702 * If the changed mtu during down is lower than 3703 * IPV6_MIN_MTU stop IPv6 on this interface. 3704 */ 3705 if (dev->mtu < IPV6_MIN_MTU) 3706 addrconf_ifdown(dev, dev != net->loopback_dev); 3707 } 3708 break; 3709 3710 case NETDEV_DOWN: 3711 case NETDEV_UNREGISTER: 3712 /* 3713 * Remove all addresses from this interface. 3714 */ 3715 addrconf_ifdown(dev, event != NETDEV_DOWN); 3716 break; 3717 3718 case NETDEV_CHANGENAME: 3719 if (idev) { 3720 snmp6_unregister_dev(idev); 3721 addrconf_sysctl_unregister(idev); 3722 err = addrconf_sysctl_register(idev); 3723 if (err) 3724 return notifier_from_errno(err); 3725 err = snmp6_register_dev(idev); 3726 if (err) { 3727 addrconf_sysctl_unregister(idev); 3728 return notifier_from_errno(err); 3729 } 3730 } 3731 break; 3732 3733 case NETDEV_PRE_TYPE_CHANGE: 3734 case NETDEV_POST_TYPE_CHANGE: 3735 if (idev) 3736 addrconf_type_change(dev, event); 3737 break; 3738 3739 case NETDEV_CHANGEUPPER: 3740 info = ptr; 3741 3742 /* flush all routes if dev is linked to or unlinked from 3743 * an L3 master device (e.g., VRF) 3744 */ 3745 if (info->upper_dev && netif_is_l3_master(info->upper_dev)) 3746 addrconf_ifdown(dev, false); 3747 } 3748 3749 return NOTIFY_OK; 3750 } 3751 3752 /* 3753 * addrconf module should be notified of a device going up 3754 */ 3755 static struct notifier_block ipv6_dev_notf = { 3756 .notifier_call = addrconf_notify, 3757 .priority = ADDRCONF_NOTIFY_PRIORITY, 3758 }; 3759 3760 static void addrconf_type_change(struct net_device *dev, unsigned long event) 3761 { 3762 struct inet6_dev *idev; 3763 ASSERT_RTNL(); 3764 3765 idev = __in6_dev_get(dev); 3766 3767 if (event == NETDEV_POST_TYPE_CHANGE) 3768 ipv6_mc_remap(idev); 3769 else if (event == NETDEV_PRE_TYPE_CHANGE) 3770 ipv6_mc_unmap(idev); 3771 } 3772 3773 static bool addr_is_local(const struct in6_addr *addr) 3774 { 3775 return ipv6_addr_type(addr) & 3776 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 3777 } 3778 3779 static int addrconf_ifdown(struct net_device *dev, bool unregister) 3780 { 3781 unsigned long event = unregister ? NETDEV_UNREGISTER : NETDEV_DOWN; 3782 struct net *net = dev_net(dev); 3783 struct inet6_dev *idev; 3784 struct inet6_ifaddr *ifa; 3785 LIST_HEAD(tmp_addr_list); 3786 bool keep_addr = false; 3787 bool was_ready; 3788 int state, i; 3789 3790 ASSERT_RTNL(); 3791 3792 rt6_disable_ip(dev, event); 3793 3794 idev = __in6_dev_get(dev); 3795 if (!idev) 3796 return -ENODEV; 3797 3798 /* 3799 * Step 1: remove reference to ipv6 device from parent device. 3800 * Do not dev_put! 3801 */ 3802 if (unregister) { 3803 idev->dead = 1; 3804 3805 /* protected by rtnl_lock */ 3806 RCU_INIT_POINTER(dev->ip6_ptr, NULL); 3807 3808 /* Step 1.5: remove snmp6 entry */ 3809 snmp6_unregister_dev(idev); 3810 3811 } 3812 3813 /* combine the user config with event to determine if permanent 3814 * addresses are to be removed from address hash table 3815 */ 3816 if (!unregister && !idev->cnf.disable_ipv6) { 3817 /* aggregate the system setting and interface setting */ 3818 int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down; 3819 3820 if (!_keep_addr) 3821 _keep_addr = idev->cnf.keep_addr_on_down; 3822 3823 keep_addr = (_keep_addr > 0); 3824 } 3825 3826 /* Step 2: clear hash table */ 3827 for (i = 0; i < IN6_ADDR_HSIZE; i++) { 3828 struct hlist_head *h = &net->ipv6.inet6_addr_lst[i]; 3829 3830 spin_lock_bh(&net->ipv6.addrconf_hash_lock); 3831 restart: 3832 hlist_for_each_entry_rcu(ifa, h, addr_lst) { 3833 if (ifa->idev == idev) { 3834 addrconf_del_dad_work(ifa); 3835 /* combined flag + permanent flag decide if 3836 * address is retained on a down event 3837 */ 3838 if (!keep_addr || 3839 !(ifa->flags & IFA_F_PERMANENT) || 3840 addr_is_local(&ifa->addr)) { 3841 hlist_del_init_rcu(&ifa->addr_lst); 3842 goto restart; 3843 } 3844 } 3845 } 3846 spin_unlock_bh(&net->ipv6.addrconf_hash_lock); 3847 } 3848 3849 write_lock_bh(&idev->lock); 3850 3851 addrconf_del_rs_timer(idev); 3852 3853 /* Step 2: clear flags for stateless addrconf, repeated down 3854 * detection 3855 */ 3856 was_ready = idev->if_flags & IF_READY; 3857 if (!unregister) 3858 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 3859 3860 /* Step 3: clear tempaddr list */ 3861 while (!list_empty(&idev->tempaddr_list)) { 3862 ifa = list_first_entry(&idev->tempaddr_list, 3863 struct inet6_ifaddr, tmp_list); 3864 list_del(&ifa->tmp_list); 3865 write_unlock_bh(&idev->lock); 3866 spin_lock_bh(&ifa->lock); 3867 3868 if (ifa->ifpub) { 3869 in6_ifa_put(ifa->ifpub); 3870 ifa->ifpub = NULL; 3871 } 3872 spin_unlock_bh(&ifa->lock); 3873 in6_ifa_put(ifa); 3874 write_lock_bh(&idev->lock); 3875 } 3876 3877 list_for_each_entry(ifa, &idev->addr_list, if_list) 3878 list_add_tail(&ifa->if_list_aux, &tmp_addr_list); 3879 write_unlock_bh(&idev->lock); 3880 3881 while (!list_empty(&tmp_addr_list)) { 3882 struct fib6_info *rt = NULL; 3883 bool keep; 3884 3885 ifa = list_first_entry(&tmp_addr_list, 3886 struct inet6_ifaddr, if_list_aux); 3887 list_del(&ifa->if_list_aux); 3888 3889 addrconf_del_dad_work(ifa); 3890 3891 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) && 3892 !addr_is_local(&ifa->addr); 3893 3894 spin_lock_bh(&ifa->lock); 3895 3896 if (keep) { 3897 /* set state to skip the notifier below */ 3898 state = INET6_IFADDR_STATE_DEAD; 3899 ifa->state = INET6_IFADDR_STATE_PREDAD; 3900 if (!(ifa->flags & IFA_F_NODAD)) 3901 ifa->flags |= IFA_F_TENTATIVE; 3902 3903 rt = ifa->rt; 3904 ifa->rt = NULL; 3905 } else { 3906 state = ifa->state; 3907 ifa->state = INET6_IFADDR_STATE_DEAD; 3908 } 3909 3910 spin_unlock_bh(&ifa->lock); 3911 3912 if (rt) 3913 ip6_del_rt(net, rt, false); 3914 3915 if (state != INET6_IFADDR_STATE_DEAD) { 3916 __ipv6_ifa_notify(RTM_DELADDR, ifa); 3917 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); 3918 } else { 3919 if (idev->cnf.forwarding) 3920 addrconf_leave_anycast(ifa); 3921 addrconf_leave_solict(ifa->idev, &ifa->addr); 3922 } 3923 3924 if (!keep) { 3925 write_lock_bh(&idev->lock); 3926 list_del_rcu(&ifa->if_list); 3927 write_unlock_bh(&idev->lock); 3928 in6_ifa_put(ifa); 3929 } 3930 } 3931 3932 /* Step 5: Discard anycast and multicast list */ 3933 if (unregister) { 3934 ipv6_ac_destroy_dev(idev); 3935 ipv6_mc_destroy_dev(idev); 3936 } else if (was_ready) { 3937 ipv6_mc_down(idev); 3938 } 3939 3940 idev->tstamp = jiffies; 3941 idev->ra_mtu = 0; 3942 3943 /* Last: Shot the device (if unregistered) */ 3944 if (unregister) { 3945 addrconf_sysctl_unregister(idev); 3946 neigh_parms_release(&nd_tbl, idev->nd_parms); 3947 neigh_ifdown(&nd_tbl, dev); 3948 in6_dev_put(idev); 3949 } 3950 return 0; 3951 } 3952 3953 static void addrconf_rs_timer(struct timer_list *t) 3954 { 3955 struct inet6_dev *idev = from_timer(idev, t, rs_timer); 3956 struct net_device *dev = idev->dev; 3957 struct in6_addr lladdr; 3958 3959 write_lock(&idev->lock); 3960 if (idev->dead || !(idev->if_flags & IF_READY)) 3961 goto out; 3962 3963 if (!ipv6_accept_ra(idev)) 3964 goto out; 3965 3966 /* Announcement received after solicitation was sent */ 3967 if (idev->if_flags & IF_RA_RCVD) 3968 goto out; 3969 3970 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) { 3971 write_unlock(&idev->lock); 3972 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3973 ndisc_send_rs(dev, &lladdr, 3974 &in6addr_linklocal_allrouters); 3975 else 3976 goto put; 3977 3978 write_lock(&idev->lock); 3979 idev->rs_interval = rfc3315_s14_backoff_update( 3980 idev->rs_interval, idev->cnf.rtr_solicit_max_interval); 3981 /* The wait after the last probe can be shorter */ 3982 addrconf_mod_rs_timer(idev, (idev->rs_probes == 3983 idev->cnf.rtr_solicits) ? 3984 idev->cnf.rtr_solicit_delay : 3985 idev->rs_interval); 3986 } else { 3987 /* 3988 * Note: we do not support deprecated "all on-link" 3989 * assumption any longer. 3990 */ 3991 pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 3992 } 3993 3994 out: 3995 write_unlock(&idev->lock); 3996 put: 3997 in6_dev_put(idev); 3998 } 3999 4000 /* 4001 * Duplicate Address Detection 4002 */ 4003 static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 4004 { 4005 unsigned long rand_num; 4006 struct inet6_dev *idev = ifp->idev; 4007 u64 nonce; 4008 4009 if (ifp->flags & IFA_F_OPTIMISTIC) 4010 rand_num = 0; 4011 else 4012 rand_num = get_random_u32_below(idev->cnf.rtr_solicit_delay ? : 1); 4013 4014 nonce = 0; 4015 if (idev->cnf.enhanced_dad || 4016 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) { 4017 do 4018 get_random_bytes(&nonce, 6); 4019 while (nonce == 0); 4020 } 4021 ifp->dad_nonce = nonce; 4022 ifp->dad_probes = idev->cnf.dad_transmits; 4023 addrconf_mod_dad_work(ifp, rand_num); 4024 } 4025 4026 static void addrconf_dad_begin(struct inet6_ifaddr *ifp) 4027 { 4028 struct inet6_dev *idev = ifp->idev; 4029 struct net_device *dev = idev->dev; 4030 bool bump_id, notify = false; 4031 struct net *net; 4032 4033 addrconf_join_solict(dev, &ifp->addr); 4034 4035 read_lock_bh(&idev->lock); 4036 spin_lock(&ifp->lock); 4037 if (ifp->state == INET6_IFADDR_STATE_DEAD) 4038 goto out; 4039 4040 net = dev_net(dev); 4041 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 4042 (net->ipv6.devconf_all->accept_dad < 1 && 4043 idev->cnf.accept_dad < 1) || 4044 !(ifp->flags&IFA_F_TENTATIVE) || 4045 ifp->flags & IFA_F_NODAD) { 4046 bool send_na = false; 4047 4048 if (ifp->flags & IFA_F_TENTATIVE && 4049 !(ifp->flags & IFA_F_OPTIMISTIC)) 4050 send_na = true; 4051 bump_id = ifp->flags & IFA_F_TENTATIVE; 4052 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 4053 spin_unlock(&ifp->lock); 4054 read_unlock_bh(&idev->lock); 4055 4056 addrconf_dad_completed(ifp, bump_id, send_na); 4057 return; 4058 } 4059 4060 if (!(idev->if_flags & IF_READY)) { 4061 spin_unlock(&ifp->lock); 4062 read_unlock_bh(&idev->lock); 4063 /* 4064 * If the device is not ready: 4065 * - keep it tentative if it is a permanent address. 4066 * - otherwise, kill it. 4067 */ 4068 in6_ifa_hold(ifp); 4069 addrconf_dad_stop(ifp, 0); 4070 return; 4071 } 4072 4073 /* 4074 * Optimistic nodes can start receiving 4075 * Frames right away 4076 */ 4077 if (ifp->flags & IFA_F_OPTIMISTIC) { 4078 ip6_ins_rt(net, ifp->rt); 4079 if (ipv6_use_optimistic_addr(net, idev)) { 4080 /* Because optimistic nodes can use this address, 4081 * notify listeners. If DAD fails, RTM_DELADDR is sent. 4082 */ 4083 notify = true; 4084 } 4085 } 4086 4087 addrconf_dad_kick(ifp); 4088 out: 4089 spin_unlock(&ifp->lock); 4090 read_unlock_bh(&idev->lock); 4091 if (notify) 4092 ipv6_ifa_notify(RTM_NEWADDR, ifp); 4093 } 4094 4095 static void addrconf_dad_start(struct inet6_ifaddr *ifp) 4096 { 4097 bool begin_dad = false; 4098 4099 spin_lock_bh(&ifp->lock); 4100 if (ifp->state != INET6_IFADDR_STATE_DEAD) { 4101 ifp->state = INET6_IFADDR_STATE_PREDAD; 4102 begin_dad = true; 4103 } 4104 spin_unlock_bh(&ifp->lock); 4105 4106 if (begin_dad) 4107 addrconf_mod_dad_work(ifp, 0); 4108 } 4109 4110 static void addrconf_dad_work(struct work_struct *w) 4111 { 4112 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), 4113 struct inet6_ifaddr, 4114 dad_work); 4115 struct inet6_dev *idev = ifp->idev; 4116 bool bump_id, disable_ipv6 = false; 4117 struct in6_addr mcaddr; 4118 4119 enum { 4120 DAD_PROCESS, 4121 DAD_BEGIN, 4122 DAD_ABORT, 4123 } action = DAD_PROCESS; 4124 4125 rtnl_lock(); 4126 4127 spin_lock_bh(&ifp->lock); 4128 if (ifp->state == INET6_IFADDR_STATE_PREDAD) { 4129 action = DAD_BEGIN; 4130 ifp->state = INET6_IFADDR_STATE_DAD; 4131 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { 4132 action = DAD_ABORT; 4133 ifp->state = INET6_IFADDR_STATE_POSTDAD; 4134 4135 if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 || 4136 idev->cnf.accept_dad > 1) && 4137 !idev->cnf.disable_ipv6 && 4138 !(ifp->flags & IFA_F_STABLE_PRIVACY)) { 4139 struct in6_addr addr; 4140 4141 addr.s6_addr32[0] = htonl(0xfe800000); 4142 addr.s6_addr32[1] = 0; 4143 4144 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 4145 ipv6_addr_equal(&ifp->addr, &addr)) { 4146 /* DAD failed for link-local based on MAC */ 4147 idev->cnf.disable_ipv6 = 1; 4148 4149 pr_info("%s: IPv6 being disabled!\n", 4150 ifp->idev->dev->name); 4151 disable_ipv6 = true; 4152 } 4153 } 4154 } 4155 spin_unlock_bh(&ifp->lock); 4156 4157 if (action == DAD_BEGIN) { 4158 addrconf_dad_begin(ifp); 4159 goto out; 4160 } else if (action == DAD_ABORT) { 4161 in6_ifa_hold(ifp); 4162 addrconf_dad_stop(ifp, 1); 4163 if (disable_ipv6) 4164 addrconf_ifdown(idev->dev, false); 4165 goto out; 4166 } 4167 4168 if (!ifp->dad_probes && addrconf_dad_end(ifp)) 4169 goto out; 4170 4171 write_lock_bh(&idev->lock); 4172 if (idev->dead || !(idev->if_flags & IF_READY)) { 4173 write_unlock_bh(&idev->lock); 4174 goto out; 4175 } 4176 4177 spin_lock(&ifp->lock); 4178 if (ifp->state == INET6_IFADDR_STATE_DEAD) { 4179 spin_unlock(&ifp->lock); 4180 write_unlock_bh(&idev->lock); 4181 goto out; 4182 } 4183 4184 if (ifp->dad_probes == 0) { 4185 bool send_na = false; 4186 4187 /* 4188 * DAD was successful 4189 */ 4190 4191 if (ifp->flags & IFA_F_TENTATIVE && 4192 !(ifp->flags & IFA_F_OPTIMISTIC)) 4193 send_na = true; 4194 bump_id = ifp->flags & IFA_F_TENTATIVE; 4195 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 4196 spin_unlock(&ifp->lock); 4197 write_unlock_bh(&idev->lock); 4198 4199 addrconf_dad_completed(ifp, bump_id, send_na); 4200 4201 goto out; 4202 } 4203 4204 ifp->dad_probes--; 4205 addrconf_mod_dad_work(ifp, 4206 max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME), 4207 HZ/100)); 4208 spin_unlock(&ifp->lock); 4209 write_unlock_bh(&idev->lock); 4210 4211 /* send a neighbour solicitation for our addr */ 4212 addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 4213 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any, 4214 ifp->dad_nonce); 4215 out: 4216 in6_ifa_put(ifp); 4217 rtnl_unlock(); 4218 } 4219 4220 /* ifp->idev must be at least read locked */ 4221 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) 4222 { 4223 struct inet6_ifaddr *ifpiter; 4224 struct inet6_dev *idev = ifp->idev; 4225 4226 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { 4227 if (ifpiter->scope > IFA_LINK) 4228 break; 4229 if (ifp != ifpiter && ifpiter->scope == IFA_LINK && 4230 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| 4231 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == 4232 IFA_F_PERMANENT) 4233 return false; 4234 } 4235 return true; 4236 } 4237 4238 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id, 4239 bool send_na) 4240 { 4241 struct net_device *dev = ifp->idev->dev; 4242 struct in6_addr lladdr; 4243 bool send_rs, send_mld; 4244 4245 addrconf_del_dad_work(ifp); 4246 4247 /* 4248 * Configure the address for reception. Now it is valid. 4249 */ 4250 4251 ipv6_ifa_notify(RTM_NEWADDR, ifp); 4252 4253 /* If added prefix is link local and we are prepared to process 4254 router advertisements, start sending router solicitations. 4255 */ 4256 4257 read_lock_bh(&ifp->idev->lock); 4258 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); 4259 send_rs = send_mld && 4260 ipv6_accept_ra(ifp->idev) && 4261 ifp->idev->cnf.rtr_solicits != 0 && 4262 (dev->flags & IFF_LOOPBACK) == 0 && 4263 (dev->type != ARPHRD_TUNNEL) && 4264 !netif_is_team_port(dev); 4265 read_unlock_bh(&ifp->idev->lock); 4266 4267 /* While dad is in progress mld report's source address is in6_addrany. 4268 * Resend with proper ll now. 4269 */ 4270 if (send_mld) 4271 ipv6_mc_dad_complete(ifp->idev); 4272 4273 /* send unsolicited NA if enabled */ 4274 if (send_na && 4275 (ifp->idev->cnf.ndisc_notify || 4276 dev_net(dev)->ipv6.devconf_all->ndisc_notify)) { 4277 ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr, 4278 /*router=*/ !!ifp->idev->cnf.forwarding, 4279 /*solicited=*/ false, /*override=*/ true, 4280 /*inc_opt=*/ true); 4281 } 4282 4283 if (send_rs) { 4284 /* 4285 * If a host as already performed a random delay 4286 * [...] as part of DAD [...] there is no need 4287 * to delay again before sending the first RS 4288 */ 4289 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 4290 return; 4291 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); 4292 4293 write_lock_bh(&ifp->idev->lock); 4294 spin_lock(&ifp->lock); 4295 ifp->idev->rs_interval = rfc3315_s14_backoff_init( 4296 ifp->idev->cnf.rtr_solicit_interval); 4297 ifp->idev->rs_probes = 1; 4298 ifp->idev->if_flags |= IF_RS_SENT; 4299 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval); 4300 spin_unlock(&ifp->lock); 4301 write_unlock_bh(&ifp->idev->lock); 4302 } 4303 4304 if (bump_id) 4305 rt_genid_bump_ipv6(dev_net(dev)); 4306 4307 /* Make sure that a new temporary address will be created 4308 * before this temporary address becomes deprecated. 4309 */ 4310 if (ifp->flags & IFA_F_TEMPORARY) 4311 addrconf_verify_rtnl(dev_net(dev)); 4312 } 4313 4314 static void addrconf_dad_run(struct inet6_dev *idev, bool restart) 4315 { 4316 struct inet6_ifaddr *ifp; 4317 4318 read_lock_bh(&idev->lock); 4319 list_for_each_entry(ifp, &idev->addr_list, if_list) { 4320 spin_lock(&ifp->lock); 4321 if ((ifp->flags & IFA_F_TENTATIVE && 4322 ifp->state == INET6_IFADDR_STATE_DAD) || restart) { 4323 if (restart) 4324 ifp->state = INET6_IFADDR_STATE_PREDAD; 4325 addrconf_dad_kick(ifp); 4326 } 4327 spin_unlock(&ifp->lock); 4328 } 4329 read_unlock_bh(&idev->lock); 4330 } 4331 4332 #ifdef CONFIG_PROC_FS 4333 struct if6_iter_state { 4334 struct seq_net_private p; 4335 int bucket; 4336 int offset; 4337 }; 4338 4339 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 4340 { 4341 struct if6_iter_state *state = seq->private; 4342 struct net *net = seq_file_net(seq); 4343 struct inet6_ifaddr *ifa = NULL; 4344 int p = 0; 4345 4346 /* initial bucket if pos is 0 */ 4347 if (pos == 0) { 4348 state->bucket = 0; 4349 state->offset = 0; 4350 } 4351 4352 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 4353 hlist_for_each_entry_rcu(ifa, &net->ipv6.inet6_addr_lst[state->bucket], 4354 addr_lst) { 4355 /* sync with offset */ 4356 if (p < state->offset) { 4357 p++; 4358 continue; 4359 } 4360 return ifa; 4361 } 4362 4363 /* prepare for next bucket */ 4364 state->offset = 0; 4365 p = 0; 4366 } 4367 return NULL; 4368 } 4369 4370 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 4371 struct inet6_ifaddr *ifa) 4372 { 4373 struct if6_iter_state *state = seq->private; 4374 struct net *net = seq_file_net(seq); 4375 4376 hlist_for_each_entry_continue_rcu(ifa, addr_lst) { 4377 state->offset++; 4378 return ifa; 4379 } 4380 4381 state->offset = 0; 4382 while (++state->bucket < IN6_ADDR_HSIZE) { 4383 hlist_for_each_entry_rcu(ifa, 4384 &net->ipv6.inet6_addr_lst[state->bucket], addr_lst) { 4385 return ifa; 4386 } 4387 } 4388 4389 return NULL; 4390 } 4391 4392 static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 4393 __acquires(rcu) 4394 { 4395 rcu_read_lock(); 4396 return if6_get_first(seq, *pos); 4397 } 4398 4399 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 4400 { 4401 struct inet6_ifaddr *ifa; 4402 4403 ifa = if6_get_next(seq, v); 4404 ++*pos; 4405 return ifa; 4406 } 4407 4408 static void if6_seq_stop(struct seq_file *seq, void *v) 4409 __releases(rcu) 4410 { 4411 rcu_read_unlock(); 4412 } 4413 4414 static int if6_seq_show(struct seq_file *seq, void *v) 4415 { 4416 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 4417 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 4418 &ifp->addr, 4419 ifp->idev->dev->ifindex, 4420 ifp->prefix_len, 4421 ifp->scope, 4422 (u8) ifp->flags, 4423 ifp->idev->dev->name); 4424 return 0; 4425 } 4426 4427 static const struct seq_operations if6_seq_ops = { 4428 .start = if6_seq_start, 4429 .next = if6_seq_next, 4430 .show = if6_seq_show, 4431 .stop = if6_seq_stop, 4432 }; 4433 4434 static int __net_init if6_proc_net_init(struct net *net) 4435 { 4436 if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops, 4437 sizeof(struct if6_iter_state))) 4438 return -ENOMEM; 4439 return 0; 4440 } 4441 4442 static void __net_exit if6_proc_net_exit(struct net *net) 4443 { 4444 remove_proc_entry("if_inet6", net->proc_net); 4445 } 4446 4447 static struct pernet_operations if6_proc_net_ops = { 4448 .init = if6_proc_net_init, 4449 .exit = if6_proc_net_exit, 4450 }; 4451 4452 int __init if6_proc_init(void) 4453 { 4454 return register_pernet_subsys(&if6_proc_net_ops); 4455 } 4456 4457 void if6_proc_exit(void) 4458 { 4459 unregister_pernet_subsys(&if6_proc_net_ops); 4460 } 4461 #endif /* CONFIG_PROC_FS */ 4462 4463 #if IS_ENABLED(CONFIG_IPV6_MIP6) 4464 /* Check if address is a home address configured on any interface. */ 4465 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 4466 { 4467 unsigned int hash = inet6_addr_hash(net, addr); 4468 struct inet6_ifaddr *ifp = NULL; 4469 int ret = 0; 4470 4471 rcu_read_lock(); 4472 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) { 4473 if (ipv6_addr_equal(&ifp->addr, addr) && 4474 (ifp->flags & IFA_F_HOMEADDRESS)) { 4475 ret = 1; 4476 break; 4477 } 4478 } 4479 rcu_read_unlock(); 4480 return ret; 4481 } 4482 #endif 4483 4484 /* RFC6554 has some algorithm to avoid loops in segment routing by 4485 * checking if the segments contains any of a local interface address. 4486 * 4487 * Quote: 4488 * 4489 * To detect loops in the SRH, a router MUST determine if the SRH 4490 * includes multiple addresses assigned to any interface on that router. 4491 * If such addresses appear more than once and are separated by at least 4492 * one address not assigned to that router. 4493 */ 4494 int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs, 4495 unsigned char nsegs) 4496 { 4497 const struct in6_addr *addr; 4498 int i, ret = 0, found = 0; 4499 struct inet6_ifaddr *ifp; 4500 bool separated = false; 4501 unsigned int hash; 4502 bool hash_found; 4503 4504 rcu_read_lock(); 4505 for (i = 0; i < nsegs; i++) { 4506 addr = &segs[i]; 4507 hash = inet6_addr_hash(net, addr); 4508 4509 hash_found = false; 4510 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) { 4511 4512 if (ipv6_addr_equal(&ifp->addr, addr)) { 4513 hash_found = true; 4514 break; 4515 } 4516 } 4517 4518 if (hash_found) { 4519 if (found > 1 && separated) { 4520 ret = 1; 4521 break; 4522 } 4523 4524 separated = false; 4525 found++; 4526 } else { 4527 separated = true; 4528 } 4529 } 4530 rcu_read_unlock(); 4531 4532 return ret; 4533 } 4534 4535 /* 4536 * Periodic address status verification 4537 */ 4538 4539 static void addrconf_verify_rtnl(struct net *net) 4540 { 4541 unsigned long now, next, next_sec, next_sched; 4542 struct inet6_ifaddr *ifp; 4543 int i; 4544 4545 ASSERT_RTNL(); 4546 4547 rcu_read_lock_bh(); 4548 now = jiffies; 4549 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 4550 4551 cancel_delayed_work(&net->ipv6.addr_chk_work); 4552 4553 for (i = 0; i < IN6_ADDR_HSIZE; i++) { 4554 restart: 4555 hlist_for_each_entry_rcu_bh(ifp, &net->ipv6.inet6_addr_lst[i], addr_lst) { 4556 unsigned long age; 4557 4558 /* When setting preferred_lft to a value not zero or 4559 * infinity, while valid_lft is infinity 4560 * IFA_F_PERMANENT has a non-infinity life time. 4561 */ 4562 if ((ifp->flags & IFA_F_PERMANENT) && 4563 (ifp->prefered_lft == INFINITY_LIFE_TIME)) 4564 continue; 4565 4566 spin_lock(&ifp->lock); 4567 /* We try to batch several events at once. */ 4568 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 4569 4570 if ((ifp->flags&IFA_F_TEMPORARY) && 4571 !(ifp->flags&IFA_F_TENTATIVE) && 4572 ifp->prefered_lft != INFINITY_LIFE_TIME && 4573 !ifp->regen_count && ifp->ifpub) { 4574 /* This is a non-regenerated temporary addr. */ 4575 4576 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 4577 ifp->idev->cnf.dad_transmits * 4578 max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME), HZ/100) / HZ; 4579 4580 if (age + regen_advance >= ifp->prefered_lft) { 4581 struct inet6_ifaddr *ifpub = ifp->ifpub; 4582 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 4583 next = ifp->tstamp + ifp->prefered_lft * HZ; 4584 4585 ifp->regen_count++; 4586 in6_ifa_hold(ifp); 4587 in6_ifa_hold(ifpub); 4588 spin_unlock(&ifp->lock); 4589 4590 spin_lock(&ifpub->lock); 4591 ifpub->regen_count = 0; 4592 spin_unlock(&ifpub->lock); 4593 rcu_read_unlock_bh(); 4594 ipv6_create_tempaddr(ifpub, true); 4595 in6_ifa_put(ifpub); 4596 in6_ifa_put(ifp); 4597 rcu_read_lock_bh(); 4598 goto restart; 4599 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 4600 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 4601 } 4602 4603 if (ifp->valid_lft != INFINITY_LIFE_TIME && 4604 age >= ifp->valid_lft) { 4605 spin_unlock(&ifp->lock); 4606 in6_ifa_hold(ifp); 4607 rcu_read_unlock_bh(); 4608 ipv6_del_addr(ifp); 4609 rcu_read_lock_bh(); 4610 goto restart; 4611 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 4612 spin_unlock(&ifp->lock); 4613 continue; 4614 } else if (age >= ifp->prefered_lft) { 4615 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 4616 int deprecate = 0; 4617 4618 if (!(ifp->flags&IFA_F_DEPRECATED)) { 4619 deprecate = 1; 4620 ifp->flags |= IFA_F_DEPRECATED; 4621 } 4622 4623 if ((ifp->valid_lft != INFINITY_LIFE_TIME) && 4624 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) 4625 next = ifp->tstamp + ifp->valid_lft * HZ; 4626 4627 spin_unlock(&ifp->lock); 4628 4629 if (deprecate) { 4630 in6_ifa_hold(ifp); 4631 4632 ipv6_ifa_notify(0, ifp); 4633 in6_ifa_put(ifp); 4634 goto restart; 4635 } 4636 } else { 4637 /* ifp->prefered_lft <= ifp->valid_lft */ 4638 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 4639 next = ifp->tstamp + ifp->prefered_lft * HZ; 4640 spin_unlock(&ifp->lock); 4641 } 4642 } 4643 } 4644 4645 next_sec = round_jiffies_up(next); 4646 next_sched = next; 4647 4648 /* If rounded timeout is accurate enough, accept it. */ 4649 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 4650 next_sched = next_sec; 4651 4652 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 4653 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 4654 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 4655 4656 pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 4657 now, next, next_sec, next_sched); 4658 mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, next_sched - now); 4659 rcu_read_unlock_bh(); 4660 } 4661 4662 static void addrconf_verify_work(struct work_struct *w) 4663 { 4664 struct net *net = container_of(to_delayed_work(w), struct net, 4665 ipv6.addr_chk_work); 4666 4667 rtnl_lock(); 4668 addrconf_verify_rtnl(net); 4669 rtnl_unlock(); 4670 } 4671 4672 static void addrconf_verify(struct net *net) 4673 { 4674 mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, 0); 4675 } 4676 4677 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 4678 struct in6_addr **peer_pfx) 4679 { 4680 struct in6_addr *pfx = NULL; 4681 4682 *peer_pfx = NULL; 4683 4684 if (addr) 4685 pfx = nla_data(addr); 4686 4687 if (local) { 4688 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 4689 *peer_pfx = pfx; 4690 pfx = nla_data(local); 4691 } 4692 4693 return pfx; 4694 } 4695 4696 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 4697 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 4698 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 4699 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 4700 [IFA_FLAGS] = { .len = sizeof(u32) }, 4701 [IFA_RT_PRIORITY] = { .len = sizeof(u32) }, 4702 [IFA_TARGET_NETNSID] = { .type = NLA_S32 }, 4703 [IFA_PROTO] = { .type = NLA_U8 }, 4704 }; 4705 4706 static int 4707 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, 4708 struct netlink_ext_ack *extack) 4709 { 4710 struct net *net = sock_net(skb->sk); 4711 struct ifaddrmsg *ifm; 4712 struct nlattr *tb[IFA_MAX+1]; 4713 struct in6_addr *pfx, *peer_pfx; 4714 u32 ifa_flags; 4715 int err; 4716 4717 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, 4718 ifa_ipv6_policy, extack); 4719 if (err < 0) 4720 return err; 4721 4722 ifm = nlmsg_data(nlh); 4723 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 4724 if (!pfx) 4725 return -EINVAL; 4726 4727 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 4728 4729 /* We ignore other flags so far. */ 4730 ifa_flags &= IFA_F_MANAGETEMPADDR; 4731 4732 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, 4733 ifm->ifa_prefixlen, extack); 4734 } 4735 4736 static int modify_prefix_route(struct inet6_ifaddr *ifp, 4737 unsigned long expires, u32 flags, 4738 bool modify_peer) 4739 { 4740 struct fib6_info *f6i; 4741 u32 prio; 4742 4743 f6i = addrconf_get_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr, 4744 ifp->prefix_len, 4745 ifp->idev->dev, 0, RTF_DEFAULT, true); 4746 if (!f6i) 4747 return -ENOENT; 4748 4749 prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF; 4750 if (f6i->fib6_metric != prio) { 4751 /* delete old one */ 4752 ip6_del_rt(dev_net(ifp->idev->dev), f6i, false); 4753 4754 /* add new one */ 4755 addrconf_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr, 4756 ifp->prefix_len, 4757 ifp->rt_priority, ifp->idev->dev, 4758 expires, flags, GFP_KERNEL); 4759 } else { 4760 if (!expires) 4761 fib6_clean_expires(f6i); 4762 else 4763 fib6_set_expires(f6i, expires); 4764 4765 fib6_info_release(f6i); 4766 } 4767 4768 return 0; 4769 } 4770 4771 static int inet6_addr_modify(struct net *net, struct inet6_ifaddr *ifp, 4772 struct ifa6_config *cfg) 4773 { 4774 u32 flags; 4775 clock_t expires; 4776 unsigned long timeout; 4777 bool was_managetempaddr; 4778 bool had_prefixroute; 4779 bool new_peer = false; 4780 4781 ASSERT_RTNL(); 4782 4783 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) 4784 return -EINVAL; 4785 4786 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && 4787 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) 4788 return -EINVAL; 4789 4790 if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED) 4791 cfg->ifa_flags &= ~IFA_F_OPTIMISTIC; 4792 4793 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); 4794 if (addrconf_finite_timeout(timeout)) { 4795 expires = jiffies_to_clock_t(timeout * HZ); 4796 cfg->valid_lft = timeout; 4797 flags = RTF_EXPIRES; 4798 } else { 4799 expires = 0; 4800 flags = 0; 4801 cfg->ifa_flags |= IFA_F_PERMANENT; 4802 } 4803 4804 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); 4805 if (addrconf_finite_timeout(timeout)) { 4806 if (timeout == 0) 4807 cfg->ifa_flags |= IFA_F_DEPRECATED; 4808 cfg->preferred_lft = timeout; 4809 } 4810 4811 if (cfg->peer_pfx && 4812 memcmp(&ifp->peer_addr, cfg->peer_pfx, sizeof(struct in6_addr))) { 4813 if (!ipv6_addr_any(&ifp->peer_addr)) 4814 cleanup_prefix_route(ifp, expires, true, true); 4815 new_peer = true; 4816 } 4817 4818 spin_lock_bh(&ifp->lock); 4819 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; 4820 had_prefixroute = ifp->flags & IFA_F_PERMANENT && 4821 !(ifp->flags & IFA_F_NOPREFIXROUTE); 4822 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | 4823 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4824 IFA_F_NOPREFIXROUTE); 4825 ifp->flags |= cfg->ifa_flags; 4826 ifp->tstamp = jiffies; 4827 ifp->valid_lft = cfg->valid_lft; 4828 ifp->prefered_lft = cfg->preferred_lft; 4829 ifp->ifa_proto = cfg->ifa_proto; 4830 4831 if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority) 4832 ifp->rt_priority = cfg->rt_priority; 4833 4834 if (new_peer) 4835 ifp->peer_addr = *cfg->peer_pfx; 4836 4837 spin_unlock_bh(&ifp->lock); 4838 if (!(ifp->flags&IFA_F_TENTATIVE)) 4839 ipv6_ifa_notify(0, ifp); 4840 4841 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { 4842 int rc = -ENOENT; 4843 4844 if (had_prefixroute) 4845 rc = modify_prefix_route(ifp, expires, flags, false); 4846 4847 /* prefix route could have been deleted; if so restore it */ 4848 if (rc == -ENOENT) { 4849 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 4850 ifp->rt_priority, ifp->idev->dev, 4851 expires, flags, GFP_KERNEL); 4852 } 4853 4854 if (had_prefixroute && !ipv6_addr_any(&ifp->peer_addr)) 4855 rc = modify_prefix_route(ifp, expires, flags, true); 4856 4857 if (rc == -ENOENT && !ipv6_addr_any(&ifp->peer_addr)) { 4858 addrconf_prefix_route(&ifp->peer_addr, ifp->prefix_len, 4859 ifp->rt_priority, ifp->idev->dev, 4860 expires, flags, GFP_KERNEL); 4861 } 4862 } else if (had_prefixroute) { 4863 enum cleanup_prefix_rt_t action; 4864 unsigned long rt_expires; 4865 4866 write_lock_bh(&ifp->idev->lock); 4867 action = check_cleanup_prefix_route(ifp, &rt_expires); 4868 write_unlock_bh(&ifp->idev->lock); 4869 4870 if (action != CLEANUP_PREFIX_RT_NOP) { 4871 cleanup_prefix_route(ifp, rt_expires, 4872 action == CLEANUP_PREFIX_RT_DEL, false); 4873 } 4874 } 4875 4876 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { 4877 if (was_managetempaddr && 4878 !(ifp->flags & IFA_F_MANAGETEMPADDR)) { 4879 cfg->valid_lft = 0; 4880 cfg->preferred_lft = 0; 4881 } 4882 manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft, 4883 cfg->preferred_lft, !was_managetempaddr, 4884 jiffies); 4885 } 4886 4887 addrconf_verify_rtnl(net); 4888 4889 return 0; 4890 } 4891 4892 static int 4893 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, 4894 struct netlink_ext_ack *extack) 4895 { 4896 struct net *net = sock_net(skb->sk); 4897 struct ifaddrmsg *ifm; 4898 struct nlattr *tb[IFA_MAX+1]; 4899 struct in6_addr *peer_pfx; 4900 struct inet6_ifaddr *ifa; 4901 struct net_device *dev; 4902 struct inet6_dev *idev; 4903 struct ifa6_config cfg; 4904 int err; 4905 4906 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, 4907 ifa_ipv6_policy, extack); 4908 if (err < 0) 4909 return err; 4910 4911 memset(&cfg, 0, sizeof(cfg)); 4912 4913 ifm = nlmsg_data(nlh); 4914 cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 4915 if (!cfg.pfx) 4916 return -EINVAL; 4917 4918 cfg.peer_pfx = peer_pfx; 4919 cfg.plen = ifm->ifa_prefixlen; 4920 if (tb[IFA_RT_PRIORITY]) 4921 cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]); 4922 4923 if (tb[IFA_PROTO]) 4924 cfg.ifa_proto = nla_get_u8(tb[IFA_PROTO]); 4925 4926 cfg.valid_lft = INFINITY_LIFE_TIME; 4927 cfg.preferred_lft = INFINITY_LIFE_TIME; 4928 4929 if (tb[IFA_CACHEINFO]) { 4930 struct ifa_cacheinfo *ci; 4931 4932 ci = nla_data(tb[IFA_CACHEINFO]); 4933 cfg.valid_lft = ci->ifa_valid; 4934 cfg.preferred_lft = ci->ifa_prefered; 4935 } 4936 4937 dev = __dev_get_by_index(net, ifm->ifa_index); 4938 if (!dev) { 4939 NL_SET_ERR_MSG_MOD(extack, "Unable to find the interface"); 4940 return -ENODEV; 4941 } 4942 4943 if (tb[IFA_FLAGS]) 4944 cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]); 4945 else 4946 cfg.ifa_flags = ifm->ifa_flags; 4947 4948 /* We ignore other flags so far. */ 4949 cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | 4950 IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE | 4951 IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC; 4952 4953 idev = ipv6_find_idev(dev); 4954 if (IS_ERR(idev)) 4955 return PTR_ERR(idev); 4956 4957 if (!ipv6_allow_optimistic_dad(net, idev)) 4958 cfg.ifa_flags &= ~IFA_F_OPTIMISTIC; 4959 4960 if (cfg.ifa_flags & IFA_F_NODAD && 4961 cfg.ifa_flags & IFA_F_OPTIMISTIC) { 4962 NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive"); 4963 return -EINVAL; 4964 } 4965 4966 ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1); 4967 if (!ifa) { 4968 /* 4969 * It would be best to check for !NLM_F_CREATE here but 4970 * userspace already relies on not having to provide this. 4971 */ 4972 return inet6_addr_add(net, ifm->ifa_index, &cfg, extack); 4973 } 4974 4975 if (nlh->nlmsg_flags & NLM_F_EXCL || 4976 !(nlh->nlmsg_flags & NLM_F_REPLACE)) { 4977 NL_SET_ERR_MSG_MOD(extack, "address already assigned"); 4978 err = -EEXIST; 4979 } else { 4980 err = inet6_addr_modify(net, ifa, &cfg); 4981 } 4982 4983 in6_ifa_put(ifa); 4984 4985 return err; 4986 } 4987 4988 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, 4989 u8 scope, int ifindex) 4990 { 4991 struct ifaddrmsg *ifm; 4992 4993 ifm = nlmsg_data(nlh); 4994 ifm->ifa_family = AF_INET6; 4995 ifm->ifa_prefixlen = prefixlen; 4996 ifm->ifa_flags = flags; 4997 ifm->ifa_scope = scope; 4998 ifm->ifa_index = ifindex; 4999 } 5000 5001 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 5002 unsigned long tstamp, u32 preferred, u32 valid) 5003 { 5004 struct ifa_cacheinfo ci; 5005 5006 ci.cstamp = cstamp_delta(cstamp); 5007 ci.tstamp = cstamp_delta(tstamp); 5008 ci.ifa_prefered = preferred; 5009 ci.ifa_valid = valid; 5010 5011 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 5012 } 5013 5014 static inline int rt_scope(int ifa_scope) 5015 { 5016 if (ifa_scope & IFA_HOST) 5017 return RT_SCOPE_HOST; 5018 else if (ifa_scope & IFA_LINK) 5019 return RT_SCOPE_LINK; 5020 else if (ifa_scope & IFA_SITE) 5021 return RT_SCOPE_SITE; 5022 else 5023 return RT_SCOPE_UNIVERSE; 5024 } 5025 5026 static inline int inet6_ifaddr_msgsize(void) 5027 { 5028 return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 5029 + nla_total_size(16) /* IFA_LOCAL */ 5030 + nla_total_size(16) /* IFA_ADDRESS */ 5031 + nla_total_size(sizeof(struct ifa_cacheinfo)) 5032 + nla_total_size(4) /* IFA_FLAGS */ 5033 + nla_total_size(1) /* IFA_PROTO */ 5034 + nla_total_size(4) /* IFA_RT_PRIORITY */; 5035 } 5036 5037 enum addr_type_t { 5038 UNICAST_ADDR, 5039 MULTICAST_ADDR, 5040 ANYCAST_ADDR, 5041 }; 5042 5043 struct inet6_fill_args { 5044 u32 portid; 5045 u32 seq; 5046 int event; 5047 unsigned int flags; 5048 int netnsid; 5049 int ifindex; 5050 enum addr_type_t type; 5051 }; 5052 5053 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 5054 struct inet6_fill_args *args) 5055 { 5056 struct nlmsghdr *nlh; 5057 u32 preferred, valid; 5058 5059 nlh = nlmsg_put(skb, args->portid, args->seq, args->event, 5060 sizeof(struct ifaddrmsg), args->flags); 5061 if (!nlh) 5062 return -EMSGSIZE; 5063 5064 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 5065 ifa->idev->dev->ifindex); 5066 5067 if (args->netnsid >= 0 && 5068 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) 5069 goto error; 5070 5071 spin_lock_bh(&ifa->lock); 5072 if (!((ifa->flags&IFA_F_PERMANENT) && 5073 (ifa->prefered_lft == INFINITY_LIFE_TIME))) { 5074 preferred = ifa->prefered_lft; 5075 valid = ifa->valid_lft; 5076 if (preferred != INFINITY_LIFE_TIME) { 5077 long tval = (jiffies - ifa->tstamp)/HZ; 5078 if (preferred > tval) 5079 preferred -= tval; 5080 else 5081 preferred = 0; 5082 if (valid != INFINITY_LIFE_TIME) { 5083 if (valid > tval) 5084 valid -= tval; 5085 else 5086 valid = 0; 5087 } 5088 } 5089 } else { 5090 preferred = INFINITY_LIFE_TIME; 5091 valid = INFINITY_LIFE_TIME; 5092 } 5093 spin_unlock_bh(&ifa->lock); 5094 5095 if (!ipv6_addr_any(&ifa->peer_addr)) { 5096 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || 5097 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) 5098 goto error; 5099 } else 5100 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) 5101 goto error; 5102 5103 if (ifa->rt_priority && 5104 nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority)) 5105 goto error; 5106 5107 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 5108 goto error; 5109 5110 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) 5111 goto error; 5112 5113 if (ifa->ifa_proto && 5114 nla_put_u8(skb, IFA_PROTO, ifa->ifa_proto)) 5115 goto error; 5116 5117 nlmsg_end(skb, nlh); 5118 return 0; 5119 5120 error: 5121 nlmsg_cancel(skb, nlh); 5122 return -EMSGSIZE; 5123 } 5124 5125 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 5126 struct inet6_fill_args *args) 5127 { 5128 struct nlmsghdr *nlh; 5129 u8 scope = RT_SCOPE_UNIVERSE; 5130 int ifindex = ifmca->idev->dev->ifindex; 5131 5132 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 5133 scope = RT_SCOPE_SITE; 5134 5135 nlh = nlmsg_put(skb, args->portid, args->seq, args->event, 5136 sizeof(struct ifaddrmsg), args->flags); 5137 if (!nlh) 5138 return -EMSGSIZE; 5139 5140 if (args->netnsid >= 0 && 5141 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) { 5142 nlmsg_cancel(skb, nlh); 5143 return -EMSGSIZE; 5144 } 5145 5146 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 5147 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || 5148 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 5149 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 5150 nlmsg_cancel(skb, nlh); 5151 return -EMSGSIZE; 5152 } 5153 5154 nlmsg_end(skb, nlh); 5155 return 0; 5156 } 5157 5158 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 5159 struct inet6_fill_args *args) 5160 { 5161 struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt); 5162 int ifindex = dev ? dev->ifindex : 1; 5163 struct nlmsghdr *nlh; 5164 u8 scope = RT_SCOPE_UNIVERSE; 5165 5166 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 5167 scope = RT_SCOPE_SITE; 5168 5169 nlh = nlmsg_put(skb, args->portid, args->seq, args->event, 5170 sizeof(struct ifaddrmsg), args->flags); 5171 if (!nlh) 5172 return -EMSGSIZE; 5173 5174 if (args->netnsid >= 0 && 5175 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) { 5176 nlmsg_cancel(skb, nlh); 5177 return -EMSGSIZE; 5178 } 5179 5180 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 5181 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || 5182 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 5183 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 5184 nlmsg_cancel(skb, nlh); 5185 return -EMSGSIZE; 5186 } 5187 5188 nlmsg_end(skb, nlh); 5189 return 0; 5190 } 5191 5192 /* called with rcu_read_lock() */ 5193 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 5194 struct netlink_callback *cb, int s_ip_idx, 5195 struct inet6_fill_args *fillargs) 5196 { 5197 struct ifmcaddr6 *ifmca; 5198 struct ifacaddr6 *ifaca; 5199 int ip_idx = 0; 5200 int err = 1; 5201 5202 read_lock_bh(&idev->lock); 5203 switch (fillargs->type) { 5204 case UNICAST_ADDR: { 5205 struct inet6_ifaddr *ifa; 5206 fillargs->event = RTM_NEWADDR; 5207 5208 /* unicast address incl. temp addr */ 5209 list_for_each_entry(ifa, &idev->addr_list, if_list) { 5210 if (ip_idx < s_ip_idx) 5211 goto next; 5212 err = inet6_fill_ifaddr(skb, ifa, fillargs); 5213 if (err < 0) 5214 break; 5215 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 5216 next: 5217 ip_idx++; 5218 } 5219 break; 5220 } 5221 case MULTICAST_ADDR: 5222 read_unlock_bh(&idev->lock); 5223 fillargs->event = RTM_GETMULTICAST; 5224 5225 /* multicast address */ 5226 for (ifmca = rtnl_dereference(idev->mc_list); 5227 ifmca; 5228 ifmca = rtnl_dereference(ifmca->next), ip_idx++) { 5229 if (ip_idx < s_ip_idx) 5230 continue; 5231 err = inet6_fill_ifmcaddr(skb, ifmca, fillargs); 5232 if (err < 0) 5233 break; 5234 } 5235 read_lock_bh(&idev->lock); 5236 break; 5237 case ANYCAST_ADDR: 5238 fillargs->event = RTM_GETANYCAST; 5239 /* anycast address */ 5240 for (ifaca = idev->ac_list; ifaca; 5241 ifaca = ifaca->aca_next, ip_idx++) { 5242 if (ip_idx < s_ip_idx) 5243 continue; 5244 err = inet6_fill_ifacaddr(skb, ifaca, fillargs); 5245 if (err < 0) 5246 break; 5247 } 5248 break; 5249 default: 5250 break; 5251 } 5252 read_unlock_bh(&idev->lock); 5253 cb->args[2] = ip_idx; 5254 return err; 5255 } 5256 5257 static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh, 5258 struct inet6_fill_args *fillargs, 5259 struct net **tgt_net, struct sock *sk, 5260 struct netlink_callback *cb) 5261 { 5262 struct netlink_ext_ack *extack = cb->extack; 5263 struct nlattr *tb[IFA_MAX+1]; 5264 struct ifaddrmsg *ifm; 5265 int err, i; 5266 5267 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 5268 NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request"); 5269 return -EINVAL; 5270 } 5271 5272 ifm = nlmsg_data(nlh); 5273 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) { 5274 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request"); 5275 return -EINVAL; 5276 } 5277 5278 fillargs->ifindex = ifm->ifa_index; 5279 if (fillargs->ifindex) { 5280 cb->answer_flags |= NLM_F_DUMP_FILTERED; 5281 fillargs->flags |= NLM_F_DUMP_FILTERED; 5282 } 5283 5284 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX, 5285 ifa_ipv6_policy, extack); 5286 if (err < 0) 5287 return err; 5288 5289 for (i = 0; i <= IFA_MAX; ++i) { 5290 if (!tb[i]) 5291 continue; 5292 5293 if (i == IFA_TARGET_NETNSID) { 5294 struct net *net; 5295 5296 fillargs->netnsid = nla_get_s32(tb[i]); 5297 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid); 5298 if (IS_ERR(net)) { 5299 fillargs->netnsid = -1; 5300 NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id"); 5301 return PTR_ERR(net); 5302 } 5303 *tgt_net = net; 5304 } else { 5305 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request"); 5306 return -EINVAL; 5307 } 5308 } 5309 5310 return 0; 5311 } 5312 5313 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 5314 enum addr_type_t type) 5315 { 5316 const struct nlmsghdr *nlh = cb->nlh; 5317 struct inet6_fill_args fillargs = { 5318 .portid = NETLINK_CB(cb->skb).portid, 5319 .seq = cb->nlh->nlmsg_seq, 5320 .flags = NLM_F_MULTI, 5321 .netnsid = -1, 5322 .type = type, 5323 }; 5324 struct net *tgt_net = sock_net(skb->sk); 5325 int idx, s_idx, s_ip_idx; 5326 int h, s_h; 5327 struct net_device *dev; 5328 struct inet6_dev *idev; 5329 struct hlist_head *head; 5330 int err = 0; 5331 5332 s_h = cb->args[0]; 5333 s_idx = idx = cb->args[1]; 5334 s_ip_idx = cb->args[2]; 5335 5336 if (cb->strict_check) { 5337 err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net, 5338 skb->sk, cb); 5339 if (err < 0) 5340 goto put_tgt_net; 5341 5342 err = 0; 5343 if (fillargs.ifindex) { 5344 dev = __dev_get_by_index(tgt_net, fillargs.ifindex); 5345 if (!dev) { 5346 err = -ENODEV; 5347 goto put_tgt_net; 5348 } 5349 idev = __in6_dev_get(dev); 5350 if (idev) { 5351 err = in6_dump_addrs(idev, skb, cb, s_ip_idx, 5352 &fillargs); 5353 if (err > 0) 5354 err = 0; 5355 } 5356 goto put_tgt_net; 5357 } 5358 } 5359 5360 rcu_read_lock(); 5361 cb->seq = atomic_read(&tgt_net->ipv6.dev_addr_genid) ^ tgt_net->dev_base_seq; 5362 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 5363 idx = 0; 5364 head = &tgt_net->dev_index_head[h]; 5365 hlist_for_each_entry_rcu(dev, head, index_hlist) { 5366 if (idx < s_idx) 5367 goto cont; 5368 if (h > s_h || idx > s_idx) 5369 s_ip_idx = 0; 5370 idev = __in6_dev_get(dev); 5371 if (!idev) 5372 goto cont; 5373 5374 if (in6_dump_addrs(idev, skb, cb, s_ip_idx, 5375 &fillargs) < 0) 5376 goto done; 5377 cont: 5378 idx++; 5379 } 5380 } 5381 done: 5382 rcu_read_unlock(); 5383 cb->args[0] = h; 5384 cb->args[1] = idx; 5385 put_tgt_net: 5386 if (fillargs.netnsid >= 0) 5387 put_net(tgt_net); 5388 5389 return skb->len ? : err; 5390 } 5391 5392 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 5393 { 5394 enum addr_type_t type = UNICAST_ADDR; 5395 5396 return inet6_dump_addr(skb, cb, type); 5397 } 5398 5399 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 5400 { 5401 enum addr_type_t type = MULTICAST_ADDR; 5402 5403 return inet6_dump_addr(skb, cb, type); 5404 } 5405 5406 5407 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 5408 { 5409 enum addr_type_t type = ANYCAST_ADDR; 5410 5411 return inet6_dump_addr(skb, cb, type); 5412 } 5413 5414 static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb, 5415 const struct nlmsghdr *nlh, 5416 struct nlattr **tb, 5417 struct netlink_ext_ack *extack) 5418 { 5419 struct ifaddrmsg *ifm; 5420 int i, err; 5421 5422 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 5423 NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request"); 5424 return -EINVAL; 5425 } 5426 5427 if (!netlink_strict_get_check(skb)) 5428 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, 5429 ifa_ipv6_policy, extack); 5430 5431 ifm = nlmsg_data(nlh); 5432 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) { 5433 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request"); 5434 return -EINVAL; 5435 } 5436 5437 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX, 5438 ifa_ipv6_policy, extack); 5439 if (err) 5440 return err; 5441 5442 for (i = 0; i <= IFA_MAX; i++) { 5443 if (!tb[i]) 5444 continue; 5445 5446 switch (i) { 5447 case IFA_TARGET_NETNSID: 5448 case IFA_ADDRESS: 5449 case IFA_LOCAL: 5450 break; 5451 default: 5452 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request"); 5453 return -EINVAL; 5454 } 5455 } 5456 5457 return 0; 5458 } 5459 5460 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh, 5461 struct netlink_ext_ack *extack) 5462 { 5463 struct net *tgt_net = sock_net(in_skb->sk); 5464 struct inet6_fill_args fillargs = { 5465 .portid = NETLINK_CB(in_skb).portid, 5466 .seq = nlh->nlmsg_seq, 5467 .event = RTM_NEWADDR, 5468 .flags = 0, 5469 .netnsid = -1, 5470 }; 5471 struct ifaddrmsg *ifm; 5472 struct nlattr *tb[IFA_MAX+1]; 5473 struct in6_addr *addr = NULL, *peer; 5474 struct net_device *dev = NULL; 5475 struct inet6_ifaddr *ifa; 5476 struct sk_buff *skb; 5477 int err; 5478 5479 err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack); 5480 if (err < 0) 5481 return err; 5482 5483 if (tb[IFA_TARGET_NETNSID]) { 5484 fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]); 5485 5486 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk, 5487 fillargs.netnsid); 5488 if (IS_ERR(tgt_net)) 5489 return PTR_ERR(tgt_net); 5490 } 5491 5492 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 5493 if (!addr) 5494 return -EINVAL; 5495 5496 ifm = nlmsg_data(nlh); 5497 if (ifm->ifa_index) 5498 dev = dev_get_by_index(tgt_net, ifm->ifa_index); 5499 5500 ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1); 5501 if (!ifa) { 5502 err = -EADDRNOTAVAIL; 5503 goto errout; 5504 } 5505 5506 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 5507 if (!skb) { 5508 err = -ENOBUFS; 5509 goto errout_ifa; 5510 } 5511 5512 err = inet6_fill_ifaddr(skb, ifa, &fillargs); 5513 if (err < 0) { 5514 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 5515 WARN_ON(err == -EMSGSIZE); 5516 kfree_skb(skb); 5517 goto errout_ifa; 5518 } 5519 err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid); 5520 errout_ifa: 5521 in6_ifa_put(ifa); 5522 errout: 5523 dev_put(dev); 5524 if (fillargs.netnsid >= 0) 5525 put_net(tgt_net); 5526 5527 return err; 5528 } 5529 5530 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 5531 { 5532 struct sk_buff *skb; 5533 struct net *net = dev_net(ifa->idev->dev); 5534 struct inet6_fill_args fillargs = { 5535 .portid = 0, 5536 .seq = 0, 5537 .event = event, 5538 .flags = 0, 5539 .netnsid = -1, 5540 }; 5541 int err = -ENOBUFS; 5542 5543 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 5544 if (!skb) 5545 goto errout; 5546 5547 err = inet6_fill_ifaddr(skb, ifa, &fillargs); 5548 if (err < 0) { 5549 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 5550 WARN_ON(err == -EMSGSIZE); 5551 kfree_skb(skb); 5552 goto errout; 5553 } 5554 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 5555 return; 5556 errout: 5557 if (err < 0) 5558 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 5559 } 5560 5561 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 5562 __s32 *array, int bytes) 5563 { 5564 BUG_ON(bytes < (DEVCONF_MAX * 4)); 5565 5566 memset(array, 0, bytes); 5567 array[DEVCONF_FORWARDING] = cnf->forwarding; 5568 array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 5569 array[DEVCONF_MTU6] = cnf->mtu6; 5570 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 5571 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 5572 array[DEVCONF_AUTOCONF] = cnf->autoconf; 5573 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 5574 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 5575 array[DEVCONF_RTR_SOLICIT_INTERVAL] = 5576 jiffies_to_msecs(cnf->rtr_solicit_interval); 5577 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] = 5578 jiffies_to_msecs(cnf->rtr_solicit_max_interval); 5579 array[DEVCONF_RTR_SOLICIT_DELAY] = 5580 jiffies_to_msecs(cnf->rtr_solicit_delay); 5581 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 5582 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = 5583 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 5584 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 5585 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 5586 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 5587 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 5588 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 5589 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 5590 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 5591 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 5592 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 5593 array[DEVCONF_RA_DEFRTR_METRIC] = cnf->ra_defrtr_metric; 5594 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit; 5595 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 5596 #ifdef CONFIG_IPV6_ROUTER_PREF 5597 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 5598 array[DEVCONF_RTR_PROBE_INTERVAL] = 5599 jiffies_to_msecs(cnf->rtr_probe_interval); 5600 #ifdef CONFIG_IPV6_ROUTE_INFO 5601 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen; 5602 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 5603 #endif 5604 #endif 5605 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 5606 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 5607 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 5608 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 5609 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; 5610 #endif 5611 #ifdef CONFIG_IPV6_MROUTE 5612 array[DEVCONF_MC_FORWARDING] = atomic_read(&cnf->mc_forwarding); 5613 #endif 5614 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 5615 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 5616 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 5617 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 5618 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; 5619 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; 5620 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; 5621 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown; 5622 /* we omit DEVCONF_STABLE_SECRET for now */ 5623 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only; 5624 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast; 5625 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na; 5626 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down; 5627 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled; 5628 #ifdef CONFIG_IPV6_SEG6_HMAC 5629 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac; 5630 #endif 5631 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad; 5632 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode; 5633 array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy; 5634 array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass; 5635 array[DEVCONF_RPL_SEG_ENABLED] = cnf->rpl_seg_enabled; 5636 array[DEVCONF_IOAM6_ENABLED] = cnf->ioam6_enabled; 5637 array[DEVCONF_IOAM6_ID] = cnf->ioam6_id; 5638 array[DEVCONF_IOAM6_ID_WIDE] = cnf->ioam6_id_wide; 5639 array[DEVCONF_NDISC_EVICT_NOCARRIER] = cnf->ndisc_evict_nocarrier; 5640 array[DEVCONF_ACCEPT_UNTRACKED_NA] = cnf->accept_untracked_na; 5641 array[DEVCONF_ACCEPT_RA_MIN_LFT] = cnf->accept_ra_min_lft; 5642 } 5643 5644 static inline size_t inet6_ifla6_size(void) 5645 { 5646 return nla_total_size(4) /* IFLA_INET6_FLAGS */ 5647 + nla_total_size(sizeof(struct ifla_cacheinfo)) 5648 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 5649 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 5650 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 5651 + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */ 5652 + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */ 5653 + nla_total_size(4) /* IFLA_INET6_RA_MTU */ 5654 + 0; 5655 } 5656 5657 static inline size_t inet6_if_nlmsg_size(void) 5658 { 5659 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 5660 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 5661 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 5662 + nla_total_size(4) /* IFLA_MTU */ 5663 + nla_total_size(4) /* IFLA_LINK */ 5664 + nla_total_size(1) /* IFLA_OPERSTATE */ 5665 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 5666 } 5667 5668 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 5669 int bytes) 5670 { 5671 int i; 5672 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX; 5673 BUG_ON(pad < 0); 5674 5675 /* Use put_unaligned() because stats may not be aligned for u64. */ 5676 put_unaligned(ICMP6_MIB_MAX, &stats[0]); 5677 for (i = 1; i < ICMP6_MIB_MAX; i++) 5678 put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 5679 5680 memset(&stats[ICMP6_MIB_MAX], 0, pad); 5681 } 5682 5683 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, 5684 int bytes, size_t syncpoff) 5685 { 5686 int i, c; 5687 u64 buff[IPSTATS_MIB_MAX]; 5688 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; 5689 5690 BUG_ON(pad < 0); 5691 5692 memset(buff, 0, sizeof(buff)); 5693 buff[0] = IPSTATS_MIB_MAX; 5694 5695 for_each_possible_cpu(c) { 5696 for (i = 1; i < IPSTATS_MIB_MAX; i++) 5697 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); 5698 } 5699 5700 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); 5701 memset(&stats[IPSTATS_MIB_MAX], 0, pad); 5702 } 5703 5704 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 5705 int bytes) 5706 { 5707 switch (attrtype) { 5708 case IFLA_INET6_STATS: 5709 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, 5710 offsetof(struct ipstats_mib, syncp)); 5711 break; 5712 case IFLA_INET6_ICMP6STATS: 5713 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes); 5714 break; 5715 } 5716 } 5717 5718 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev, 5719 u32 ext_filter_mask) 5720 { 5721 struct nlattr *nla; 5722 struct ifla_cacheinfo ci; 5723 5724 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 5725 goto nla_put_failure; 5726 ci.max_reasm_len = IPV6_MAXPLEN; 5727 ci.tstamp = cstamp_delta(idev->tstamp); 5728 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 5729 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); 5730 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 5731 goto nla_put_failure; 5732 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 5733 if (!nla) 5734 goto nla_put_failure; 5735 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 5736 5737 /* XXX - MC not implemented */ 5738 5739 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS) 5740 return 0; 5741 5742 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 5743 if (!nla) 5744 goto nla_put_failure; 5745 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 5746 5747 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 5748 if (!nla) 5749 goto nla_put_failure; 5750 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 5751 5752 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 5753 if (!nla) 5754 goto nla_put_failure; 5755 read_lock_bh(&idev->lock); 5756 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 5757 read_unlock_bh(&idev->lock); 5758 5759 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode)) 5760 goto nla_put_failure; 5761 5762 if (idev->ra_mtu && 5763 nla_put_u32(skb, IFLA_INET6_RA_MTU, idev->ra_mtu)) 5764 goto nla_put_failure; 5765 5766 return 0; 5767 5768 nla_put_failure: 5769 return -EMSGSIZE; 5770 } 5771 5772 static size_t inet6_get_link_af_size(const struct net_device *dev, 5773 u32 ext_filter_mask) 5774 { 5775 if (!__in6_dev_get(dev)) 5776 return 0; 5777 5778 return inet6_ifla6_size(); 5779 } 5780 5781 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev, 5782 u32 ext_filter_mask) 5783 { 5784 struct inet6_dev *idev = __in6_dev_get(dev); 5785 5786 if (!idev) 5787 return -ENODATA; 5788 5789 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0) 5790 return -EMSGSIZE; 5791 5792 return 0; 5793 } 5794 5795 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token, 5796 struct netlink_ext_ack *extack) 5797 { 5798 struct inet6_ifaddr *ifp; 5799 struct net_device *dev = idev->dev; 5800 bool clear_token, update_rs = false; 5801 struct in6_addr ll_addr; 5802 5803 ASSERT_RTNL(); 5804 5805 if (!token) 5806 return -EINVAL; 5807 5808 if (dev->flags & IFF_LOOPBACK) { 5809 NL_SET_ERR_MSG_MOD(extack, "Device is loopback"); 5810 return -EINVAL; 5811 } 5812 5813 if (dev->flags & IFF_NOARP) { 5814 NL_SET_ERR_MSG_MOD(extack, 5815 "Device does not do neighbour discovery"); 5816 return -EINVAL; 5817 } 5818 5819 if (!ipv6_accept_ra(idev)) { 5820 NL_SET_ERR_MSG_MOD(extack, 5821 "Router advertisement is disabled on device"); 5822 return -EINVAL; 5823 } 5824 5825 if (idev->cnf.rtr_solicits == 0) { 5826 NL_SET_ERR_MSG(extack, 5827 "Router solicitation is disabled on device"); 5828 return -EINVAL; 5829 } 5830 5831 write_lock_bh(&idev->lock); 5832 5833 BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 5834 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 5835 5836 write_unlock_bh(&idev->lock); 5837 5838 clear_token = ipv6_addr_any(token); 5839 if (clear_token) 5840 goto update_lft; 5841 5842 if (!idev->dead && (idev->if_flags & IF_READY) && 5843 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 5844 IFA_F_OPTIMISTIC)) { 5845 /* If we're not ready, then normal ifup will take care 5846 * of this. Otherwise, we need to request our rs here. 5847 */ 5848 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 5849 update_rs = true; 5850 } 5851 5852 update_lft: 5853 write_lock_bh(&idev->lock); 5854 5855 if (update_rs) { 5856 idev->if_flags |= IF_RS_SENT; 5857 idev->rs_interval = rfc3315_s14_backoff_init( 5858 idev->cnf.rtr_solicit_interval); 5859 idev->rs_probes = 1; 5860 addrconf_mod_rs_timer(idev, idev->rs_interval); 5861 } 5862 5863 /* Well, that's kinda nasty ... */ 5864 list_for_each_entry(ifp, &idev->addr_list, if_list) { 5865 spin_lock(&ifp->lock); 5866 if (ifp->tokenized) { 5867 ifp->valid_lft = 0; 5868 ifp->prefered_lft = 0; 5869 } 5870 spin_unlock(&ifp->lock); 5871 } 5872 5873 write_unlock_bh(&idev->lock); 5874 inet6_ifinfo_notify(RTM_NEWLINK, idev); 5875 addrconf_verify_rtnl(dev_net(dev)); 5876 return 0; 5877 } 5878 5879 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { 5880 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, 5881 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, 5882 [IFLA_INET6_RA_MTU] = { .type = NLA_REJECT, 5883 .reject_message = 5884 "IFLA_INET6_RA_MTU can not be set" }, 5885 }; 5886 5887 static int check_addr_gen_mode(int mode) 5888 { 5889 if (mode != IN6_ADDR_GEN_MODE_EUI64 && 5890 mode != IN6_ADDR_GEN_MODE_NONE && 5891 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5892 mode != IN6_ADDR_GEN_MODE_RANDOM) 5893 return -EINVAL; 5894 return 1; 5895 } 5896 5897 static int check_stable_privacy(struct inet6_dev *idev, struct net *net, 5898 int mode) 5899 { 5900 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 5901 !idev->cnf.stable_secret.initialized && 5902 !net->ipv6.devconf_dflt->stable_secret.initialized) 5903 return -EINVAL; 5904 return 1; 5905 } 5906 5907 static int inet6_validate_link_af(const struct net_device *dev, 5908 const struct nlattr *nla, 5909 struct netlink_ext_ack *extack) 5910 { 5911 struct nlattr *tb[IFLA_INET6_MAX + 1]; 5912 struct inet6_dev *idev = NULL; 5913 int err; 5914 5915 if (dev) { 5916 idev = __in6_dev_get(dev); 5917 if (!idev) 5918 return -EAFNOSUPPORT; 5919 } 5920 5921 err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, 5922 inet6_af_policy, extack); 5923 if (err) 5924 return err; 5925 5926 if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE]) 5927 return -EINVAL; 5928 5929 if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 5930 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 5931 5932 if (check_addr_gen_mode(mode) < 0) 5933 return -EINVAL; 5934 if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0) 5935 return -EINVAL; 5936 } 5937 5938 return 0; 5939 } 5940 5941 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla, 5942 struct netlink_ext_ack *extack) 5943 { 5944 struct inet6_dev *idev = __in6_dev_get(dev); 5945 struct nlattr *tb[IFLA_INET6_MAX + 1]; 5946 int err; 5947 5948 if (!idev) 5949 return -EAFNOSUPPORT; 5950 5951 if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0) 5952 return -EINVAL; 5953 5954 if (tb[IFLA_INET6_TOKEN]) { 5955 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]), 5956 extack); 5957 if (err) 5958 return err; 5959 } 5960 5961 if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 5962 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 5963 5964 idev->cnf.addr_gen_mode = mode; 5965 } 5966 5967 return 0; 5968 } 5969 5970 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 5971 u32 portid, u32 seq, int event, unsigned int flags) 5972 { 5973 struct net_device *dev = idev->dev; 5974 struct ifinfomsg *hdr; 5975 struct nlmsghdr *nlh; 5976 void *protoinfo; 5977 5978 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 5979 if (!nlh) 5980 return -EMSGSIZE; 5981 5982 hdr = nlmsg_data(nlh); 5983 hdr->ifi_family = AF_INET6; 5984 hdr->__ifi_pad = 0; 5985 hdr->ifi_type = dev->type; 5986 hdr->ifi_index = dev->ifindex; 5987 hdr->ifi_flags = dev_get_flags(dev); 5988 hdr->ifi_change = 0; 5989 5990 if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 5991 (dev->addr_len && 5992 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 5993 nla_put_u32(skb, IFLA_MTU, dev->mtu) || 5994 (dev->ifindex != dev_get_iflink(dev) && 5995 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || 5996 nla_put_u8(skb, IFLA_OPERSTATE, 5997 netif_running(dev) ? dev->operstate : IF_OPER_DOWN)) 5998 goto nla_put_failure; 5999 protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO); 6000 if (!protoinfo) 6001 goto nla_put_failure; 6002 6003 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0) 6004 goto nla_put_failure; 6005 6006 nla_nest_end(skb, protoinfo); 6007 nlmsg_end(skb, nlh); 6008 return 0; 6009 6010 nla_put_failure: 6011 nlmsg_cancel(skb, nlh); 6012 return -EMSGSIZE; 6013 } 6014 6015 static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh, 6016 struct netlink_ext_ack *extack) 6017 { 6018 struct ifinfomsg *ifm; 6019 6020 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 6021 NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request"); 6022 return -EINVAL; 6023 } 6024 6025 if (nlmsg_attrlen(nlh, sizeof(*ifm))) { 6026 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header"); 6027 return -EINVAL; 6028 } 6029 6030 ifm = nlmsg_data(nlh); 6031 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 6032 ifm->ifi_change || ifm->ifi_index) { 6033 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request"); 6034 return -EINVAL; 6035 } 6036 6037 return 0; 6038 } 6039 6040 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 6041 { 6042 struct net *net = sock_net(skb->sk); 6043 int h, s_h; 6044 int idx = 0, s_idx; 6045 struct net_device *dev; 6046 struct inet6_dev *idev; 6047 struct hlist_head *head; 6048 6049 /* only requests using strict checking can pass data to 6050 * influence the dump 6051 */ 6052 if (cb->strict_check) { 6053 int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack); 6054 6055 if (err < 0) 6056 return err; 6057 } 6058 6059 s_h = cb->args[0]; 6060 s_idx = cb->args[1]; 6061 6062 rcu_read_lock(); 6063 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 6064 idx = 0; 6065 head = &net->dev_index_head[h]; 6066 hlist_for_each_entry_rcu(dev, head, index_hlist) { 6067 if (idx < s_idx) 6068 goto cont; 6069 idev = __in6_dev_get(dev); 6070 if (!idev) 6071 goto cont; 6072 if (inet6_fill_ifinfo(skb, idev, 6073 NETLINK_CB(cb->skb).portid, 6074 cb->nlh->nlmsg_seq, 6075 RTM_NEWLINK, NLM_F_MULTI) < 0) 6076 goto out; 6077 cont: 6078 idx++; 6079 } 6080 } 6081 out: 6082 rcu_read_unlock(); 6083 cb->args[1] = idx; 6084 cb->args[0] = h; 6085 6086 return skb->len; 6087 } 6088 6089 void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 6090 { 6091 struct sk_buff *skb; 6092 struct net *net = dev_net(idev->dev); 6093 int err = -ENOBUFS; 6094 6095 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 6096 if (!skb) 6097 goto errout; 6098 6099 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 6100 if (err < 0) { 6101 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 6102 WARN_ON(err == -EMSGSIZE); 6103 kfree_skb(skb); 6104 goto errout; 6105 } 6106 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 6107 return; 6108 errout: 6109 if (err < 0) 6110 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 6111 } 6112 6113 static inline size_t inet6_prefix_nlmsg_size(void) 6114 { 6115 return NLMSG_ALIGN(sizeof(struct prefixmsg)) 6116 + nla_total_size(sizeof(struct in6_addr)) 6117 + nla_total_size(sizeof(struct prefix_cacheinfo)); 6118 } 6119 6120 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 6121 struct prefix_info *pinfo, u32 portid, u32 seq, 6122 int event, unsigned int flags) 6123 { 6124 struct prefixmsg *pmsg; 6125 struct nlmsghdr *nlh; 6126 struct prefix_cacheinfo ci; 6127 6128 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 6129 if (!nlh) 6130 return -EMSGSIZE; 6131 6132 pmsg = nlmsg_data(nlh); 6133 pmsg->prefix_family = AF_INET6; 6134 pmsg->prefix_pad1 = 0; 6135 pmsg->prefix_pad2 = 0; 6136 pmsg->prefix_ifindex = idev->dev->ifindex; 6137 pmsg->prefix_len = pinfo->prefix_len; 6138 pmsg->prefix_type = pinfo->type; 6139 pmsg->prefix_pad3 = 0; 6140 pmsg->prefix_flags = pinfo->flags; 6141 6142 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 6143 goto nla_put_failure; 6144 ci.preferred_time = ntohl(pinfo->prefered); 6145 ci.valid_time = ntohl(pinfo->valid); 6146 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 6147 goto nla_put_failure; 6148 nlmsg_end(skb, nlh); 6149 return 0; 6150 6151 nla_put_failure: 6152 nlmsg_cancel(skb, nlh); 6153 return -EMSGSIZE; 6154 } 6155 6156 static void inet6_prefix_notify(int event, struct inet6_dev *idev, 6157 struct prefix_info *pinfo) 6158 { 6159 struct sk_buff *skb; 6160 struct net *net = dev_net(idev->dev); 6161 int err = -ENOBUFS; 6162 6163 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 6164 if (!skb) 6165 goto errout; 6166 6167 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 6168 if (err < 0) { 6169 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 6170 WARN_ON(err == -EMSGSIZE); 6171 kfree_skb(skb); 6172 goto errout; 6173 } 6174 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 6175 return; 6176 errout: 6177 if (err < 0) 6178 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 6179 } 6180 6181 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 6182 { 6183 struct net *net = dev_net(ifp->idev->dev); 6184 6185 if (event) 6186 ASSERT_RTNL(); 6187 6188 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 6189 6190 switch (event) { 6191 case RTM_NEWADDR: 6192 /* 6193 * If the address was optimistic we inserted the route at the 6194 * start of our DAD process, so we don't need to do it again. 6195 * If the device was taken down in the middle of the DAD 6196 * cycle there is a race where we could get here without a 6197 * host route, so nothing to insert. That will be fixed when 6198 * the device is brought up. 6199 */ 6200 if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) { 6201 ip6_ins_rt(net, ifp->rt); 6202 } else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) { 6203 pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n", 6204 &ifp->addr, ifp->idev->dev->name); 6205 } 6206 6207 if (ifp->idev->cnf.forwarding) 6208 addrconf_join_anycast(ifp); 6209 if (!ipv6_addr_any(&ifp->peer_addr)) 6210 addrconf_prefix_route(&ifp->peer_addr, 128, 6211 ifp->rt_priority, ifp->idev->dev, 6212 0, 0, GFP_ATOMIC); 6213 break; 6214 case RTM_DELADDR: 6215 if (ifp->idev->cnf.forwarding) 6216 addrconf_leave_anycast(ifp); 6217 addrconf_leave_solict(ifp->idev, &ifp->addr); 6218 if (!ipv6_addr_any(&ifp->peer_addr)) { 6219 struct fib6_info *rt; 6220 6221 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, 6222 ifp->idev->dev, 0, 0, 6223 false); 6224 if (rt) 6225 ip6_del_rt(net, rt, false); 6226 } 6227 if (ifp->rt) { 6228 ip6_del_rt(net, ifp->rt, false); 6229 ifp->rt = NULL; 6230 } 6231 rt_genid_bump_ipv6(net); 6232 break; 6233 } 6234 atomic_inc(&net->ipv6.dev_addr_genid); 6235 } 6236 6237 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 6238 { 6239 if (likely(ifp->idev->dead == 0)) 6240 __ipv6_ifa_notify(event, ifp); 6241 } 6242 6243 #ifdef CONFIG_SYSCTL 6244 6245 static int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 6246 void *buffer, size_t *lenp, loff_t *ppos) 6247 { 6248 int *valp = ctl->data; 6249 int val = *valp; 6250 loff_t pos = *ppos; 6251 struct ctl_table lctl; 6252 int ret; 6253 6254 /* 6255 * ctl->data points to idev->cnf.forwarding, we should 6256 * not modify it until we get the rtnl lock. 6257 */ 6258 lctl = *ctl; 6259 lctl.data = &val; 6260 6261 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 6262 6263 if (write) 6264 ret = addrconf_fixup_forwarding(ctl, valp, val); 6265 if (ret) 6266 *ppos = pos; 6267 return ret; 6268 } 6269 6270 static int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, 6271 void *buffer, size_t *lenp, loff_t *ppos) 6272 { 6273 struct inet6_dev *idev = ctl->extra1; 6274 int min_mtu = IPV6_MIN_MTU; 6275 struct ctl_table lctl; 6276 6277 lctl = *ctl; 6278 lctl.extra1 = &min_mtu; 6279 lctl.extra2 = idev ? &idev->dev->mtu : NULL; 6280 6281 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 6282 } 6283 6284 static void dev_disable_change(struct inet6_dev *idev) 6285 { 6286 struct netdev_notifier_info info; 6287 6288 if (!idev || !idev->dev) 6289 return; 6290 6291 netdev_notifier_info_init(&info, idev->dev); 6292 if (idev->cnf.disable_ipv6) 6293 addrconf_notify(NULL, NETDEV_DOWN, &info); 6294 else 6295 addrconf_notify(NULL, NETDEV_UP, &info); 6296 } 6297 6298 static void addrconf_disable_change(struct net *net, __s32 newf) 6299 { 6300 struct net_device *dev; 6301 struct inet6_dev *idev; 6302 6303 for_each_netdev(net, dev) { 6304 idev = __in6_dev_get(dev); 6305 if (idev) { 6306 int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 6307 idev->cnf.disable_ipv6 = newf; 6308 if (changed) 6309 dev_disable_change(idev); 6310 } 6311 } 6312 } 6313 6314 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 6315 { 6316 struct net *net; 6317 int old; 6318 6319 if (!rtnl_trylock()) 6320 return restart_syscall(); 6321 6322 net = (struct net *)table->extra2; 6323 old = *p; 6324 *p = newf; 6325 6326 if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 6327 rtnl_unlock(); 6328 return 0; 6329 } 6330 6331 if (p == &net->ipv6.devconf_all->disable_ipv6) { 6332 net->ipv6.devconf_dflt->disable_ipv6 = newf; 6333 addrconf_disable_change(net, newf); 6334 } else if ((!newf) ^ (!old)) 6335 dev_disable_change((struct inet6_dev *)table->extra1); 6336 6337 rtnl_unlock(); 6338 return 0; 6339 } 6340 6341 static int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 6342 void *buffer, size_t *lenp, loff_t *ppos) 6343 { 6344 int *valp = ctl->data; 6345 int val = *valp; 6346 loff_t pos = *ppos; 6347 struct ctl_table lctl; 6348 int ret; 6349 6350 /* 6351 * ctl->data points to idev->cnf.disable_ipv6, we should 6352 * not modify it until we get the rtnl lock. 6353 */ 6354 lctl = *ctl; 6355 lctl.data = &val; 6356 6357 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 6358 6359 if (write) 6360 ret = addrconf_disable_ipv6(ctl, valp, val); 6361 if (ret) 6362 *ppos = pos; 6363 return ret; 6364 } 6365 6366 static int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, 6367 void *buffer, size_t *lenp, loff_t *ppos) 6368 { 6369 int *valp = ctl->data; 6370 int ret; 6371 int old, new; 6372 6373 old = *valp; 6374 ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 6375 new = *valp; 6376 6377 if (write && old != new) { 6378 struct net *net = ctl->extra2; 6379 6380 if (!rtnl_trylock()) 6381 return restart_syscall(); 6382 6383 if (valp == &net->ipv6.devconf_dflt->proxy_ndp) 6384 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 6385 NETCONFA_PROXY_NEIGH, 6386 NETCONFA_IFINDEX_DEFAULT, 6387 net->ipv6.devconf_dflt); 6388 else if (valp == &net->ipv6.devconf_all->proxy_ndp) 6389 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 6390 NETCONFA_PROXY_NEIGH, 6391 NETCONFA_IFINDEX_ALL, 6392 net->ipv6.devconf_all); 6393 else { 6394 struct inet6_dev *idev = ctl->extra1; 6395 6396 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 6397 NETCONFA_PROXY_NEIGH, 6398 idev->dev->ifindex, 6399 &idev->cnf); 6400 } 6401 rtnl_unlock(); 6402 } 6403 6404 return ret; 6405 } 6406 6407 static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write, 6408 void *buffer, size_t *lenp, 6409 loff_t *ppos) 6410 { 6411 int ret = 0; 6412 u32 new_val; 6413 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1; 6414 struct net *net = (struct net *)ctl->extra2; 6415 struct ctl_table tmp = { 6416 .data = &new_val, 6417 .maxlen = sizeof(new_val), 6418 .mode = ctl->mode, 6419 }; 6420 6421 if (!rtnl_trylock()) 6422 return restart_syscall(); 6423 6424 new_val = *((u32 *)ctl->data); 6425 6426 ret = proc_douintvec(&tmp, write, buffer, lenp, ppos); 6427 if (ret != 0) 6428 goto out; 6429 6430 if (write) { 6431 if (check_addr_gen_mode(new_val) < 0) { 6432 ret = -EINVAL; 6433 goto out; 6434 } 6435 6436 if (idev) { 6437 if (check_stable_privacy(idev, net, new_val) < 0) { 6438 ret = -EINVAL; 6439 goto out; 6440 } 6441 6442 if (idev->cnf.addr_gen_mode != new_val) { 6443 idev->cnf.addr_gen_mode = new_val; 6444 addrconf_init_auto_addrs(idev->dev); 6445 } 6446 } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) { 6447 struct net_device *dev; 6448 6449 net->ipv6.devconf_dflt->addr_gen_mode = new_val; 6450 for_each_netdev(net, dev) { 6451 idev = __in6_dev_get(dev); 6452 if (idev && 6453 idev->cnf.addr_gen_mode != new_val) { 6454 idev->cnf.addr_gen_mode = new_val; 6455 addrconf_init_auto_addrs(idev->dev); 6456 } 6457 } 6458 } 6459 6460 *((u32 *)ctl->data) = new_val; 6461 } 6462 6463 out: 6464 rtnl_unlock(); 6465 6466 return ret; 6467 } 6468 6469 static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, 6470 void *buffer, size_t *lenp, 6471 loff_t *ppos) 6472 { 6473 int err; 6474 struct in6_addr addr; 6475 char str[IPV6_MAX_STRLEN]; 6476 struct ctl_table lctl = *ctl; 6477 struct net *net = ctl->extra2; 6478 struct ipv6_stable_secret *secret = ctl->data; 6479 6480 if (&net->ipv6.devconf_all->stable_secret == ctl->data) 6481 return -EIO; 6482 6483 lctl.maxlen = IPV6_MAX_STRLEN; 6484 lctl.data = str; 6485 6486 if (!rtnl_trylock()) 6487 return restart_syscall(); 6488 6489 if (!write && !secret->initialized) { 6490 err = -EIO; 6491 goto out; 6492 } 6493 6494 err = snprintf(str, sizeof(str), "%pI6", &secret->secret); 6495 if (err >= sizeof(str)) { 6496 err = -EIO; 6497 goto out; 6498 } 6499 6500 err = proc_dostring(&lctl, write, buffer, lenp, ppos); 6501 if (err || !write) 6502 goto out; 6503 6504 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { 6505 err = -EIO; 6506 goto out; 6507 } 6508 6509 secret->initialized = true; 6510 secret->secret = addr; 6511 6512 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { 6513 struct net_device *dev; 6514 6515 for_each_netdev(net, dev) { 6516 struct inet6_dev *idev = __in6_dev_get(dev); 6517 6518 if (idev) { 6519 idev->cnf.addr_gen_mode = 6520 IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 6521 } 6522 } 6523 } else { 6524 struct inet6_dev *idev = ctl->extra1; 6525 6526 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 6527 } 6528 6529 out: 6530 rtnl_unlock(); 6531 6532 return err; 6533 } 6534 6535 static 6536 int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl, 6537 int write, void *buffer, 6538 size_t *lenp, 6539 loff_t *ppos) 6540 { 6541 int *valp = ctl->data; 6542 int val = *valp; 6543 loff_t pos = *ppos; 6544 struct ctl_table lctl; 6545 int ret; 6546 6547 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown 6548 * we should not modify it until we get the rtnl lock. 6549 */ 6550 lctl = *ctl; 6551 lctl.data = &val; 6552 6553 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 6554 6555 if (write) 6556 ret = addrconf_fixup_linkdown(ctl, valp, val); 6557 if (ret) 6558 *ppos = pos; 6559 return ret; 6560 } 6561 6562 static 6563 void addrconf_set_nopolicy(struct rt6_info *rt, int action) 6564 { 6565 if (rt) { 6566 if (action) 6567 rt->dst.flags |= DST_NOPOLICY; 6568 else 6569 rt->dst.flags &= ~DST_NOPOLICY; 6570 } 6571 } 6572 6573 static 6574 void addrconf_disable_policy_idev(struct inet6_dev *idev, int val) 6575 { 6576 struct inet6_ifaddr *ifa; 6577 6578 read_lock_bh(&idev->lock); 6579 list_for_each_entry(ifa, &idev->addr_list, if_list) { 6580 spin_lock(&ifa->lock); 6581 if (ifa->rt) { 6582 /* host routes only use builtin fib6_nh */ 6583 struct fib6_nh *nh = ifa->rt->fib6_nh; 6584 int cpu; 6585 6586 rcu_read_lock(); 6587 ifa->rt->dst_nopolicy = val ? true : false; 6588 if (nh->rt6i_pcpu) { 6589 for_each_possible_cpu(cpu) { 6590 struct rt6_info **rtp; 6591 6592 rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu); 6593 addrconf_set_nopolicy(*rtp, val); 6594 } 6595 } 6596 rcu_read_unlock(); 6597 } 6598 spin_unlock(&ifa->lock); 6599 } 6600 read_unlock_bh(&idev->lock); 6601 } 6602 6603 static 6604 int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val) 6605 { 6606 struct inet6_dev *idev; 6607 struct net *net; 6608 6609 if (!rtnl_trylock()) 6610 return restart_syscall(); 6611 6612 *valp = val; 6613 6614 net = (struct net *)ctl->extra2; 6615 if (valp == &net->ipv6.devconf_dflt->disable_policy) { 6616 rtnl_unlock(); 6617 return 0; 6618 } 6619 6620 if (valp == &net->ipv6.devconf_all->disable_policy) { 6621 struct net_device *dev; 6622 6623 for_each_netdev(net, dev) { 6624 idev = __in6_dev_get(dev); 6625 if (idev) 6626 addrconf_disable_policy_idev(idev, val); 6627 } 6628 } else { 6629 idev = (struct inet6_dev *)ctl->extra1; 6630 addrconf_disable_policy_idev(idev, val); 6631 } 6632 6633 rtnl_unlock(); 6634 return 0; 6635 } 6636 6637 static int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write, 6638 void *buffer, size_t *lenp, loff_t *ppos) 6639 { 6640 int *valp = ctl->data; 6641 int val = *valp; 6642 loff_t pos = *ppos; 6643 struct ctl_table lctl; 6644 int ret; 6645 6646 lctl = *ctl; 6647 lctl.data = &val; 6648 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 6649 6650 if (write && (*valp != val)) 6651 ret = addrconf_disable_policy(ctl, valp, val); 6652 6653 if (ret) 6654 *ppos = pos; 6655 6656 return ret; 6657 } 6658 6659 static int minus_one = -1; 6660 static const int two_five_five = 255; 6661 static u32 ioam6_if_id_max = U16_MAX; 6662 6663 static const struct ctl_table addrconf_sysctl[] = { 6664 { 6665 .procname = "forwarding", 6666 .data = &ipv6_devconf.forwarding, 6667 .maxlen = sizeof(int), 6668 .mode = 0644, 6669 .proc_handler = addrconf_sysctl_forward, 6670 }, 6671 { 6672 .procname = "hop_limit", 6673 .data = &ipv6_devconf.hop_limit, 6674 .maxlen = sizeof(int), 6675 .mode = 0644, 6676 .proc_handler = proc_dointvec_minmax, 6677 .extra1 = (void *)SYSCTL_ONE, 6678 .extra2 = (void *)&two_five_five, 6679 }, 6680 { 6681 .procname = "mtu", 6682 .data = &ipv6_devconf.mtu6, 6683 .maxlen = sizeof(int), 6684 .mode = 0644, 6685 .proc_handler = addrconf_sysctl_mtu, 6686 }, 6687 { 6688 .procname = "accept_ra", 6689 .data = &ipv6_devconf.accept_ra, 6690 .maxlen = sizeof(int), 6691 .mode = 0644, 6692 .proc_handler = proc_dointvec, 6693 }, 6694 { 6695 .procname = "accept_redirects", 6696 .data = &ipv6_devconf.accept_redirects, 6697 .maxlen = sizeof(int), 6698 .mode = 0644, 6699 .proc_handler = proc_dointvec, 6700 }, 6701 { 6702 .procname = "autoconf", 6703 .data = &ipv6_devconf.autoconf, 6704 .maxlen = sizeof(int), 6705 .mode = 0644, 6706 .proc_handler = proc_dointvec, 6707 }, 6708 { 6709 .procname = "dad_transmits", 6710 .data = &ipv6_devconf.dad_transmits, 6711 .maxlen = sizeof(int), 6712 .mode = 0644, 6713 .proc_handler = proc_dointvec, 6714 }, 6715 { 6716 .procname = "router_solicitations", 6717 .data = &ipv6_devconf.rtr_solicits, 6718 .maxlen = sizeof(int), 6719 .mode = 0644, 6720 .proc_handler = proc_dointvec_minmax, 6721 .extra1 = &minus_one, 6722 }, 6723 { 6724 .procname = "router_solicitation_interval", 6725 .data = &ipv6_devconf.rtr_solicit_interval, 6726 .maxlen = sizeof(int), 6727 .mode = 0644, 6728 .proc_handler = proc_dointvec_jiffies, 6729 }, 6730 { 6731 .procname = "router_solicitation_max_interval", 6732 .data = &ipv6_devconf.rtr_solicit_max_interval, 6733 .maxlen = sizeof(int), 6734 .mode = 0644, 6735 .proc_handler = proc_dointvec_jiffies, 6736 }, 6737 { 6738 .procname = "router_solicitation_delay", 6739 .data = &ipv6_devconf.rtr_solicit_delay, 6740 .maxlen = sizeof(int), 6741 .mode = 0644, 6742 .proc_handler = proc_dointvec_jiffies, 6743 }, 6744 { 6745 .procname = "force_mld_version", 6746 .data = &ipv6_devconf.force_mld_version, 6747 .maxlen = sizeof(int), 6748 .mode = 0644, 6749 .proc_handler = proc_dointvec, 6750 }, 6751 { 6752 .procname = "mldv1_unsolicited_report_interval", 6753 .data = 6754 &ipv6_devconf.mldv1_unsolicited_report_interval, 6755 .maxlen = sizeof(int), 6756 .mode = 0644, 6757 .proc_handler = proc_dointvec_ms_jiffies, 6758 }, 6759 { 6760 .procname = "mldv2_unsolicited_report_interval", 6761 .data = 6762 &ipv6_devconf.mldv2_unsolicited_report_interval, 6763 .maxlen = sizeof(int), 6764 .mode = 0644, 6765 .proc_handler = proc_dointvec_ms_jiffies, 6766 }, 6767 { 6768 .procname = "use_tempaddr", 6769 .data = &ipv6_devconf.use_tempaddr, 6770 .maxlen = sizeof(int), 6771 .mode = 0644, 6772 .proc_handler = proc_dointvec, 6773 }, 6774 { 6775 .procname = "temp_valid_lft", 6776 .data = &ipv6_devconf.temp_valid_lft, 6777 .maxlen = sizeof(int), 6778 .mode = 0644, 6779 .proc_handler = proc_dointvec, 6780 }, 6781 { 6782 .procname = "temp_prefered_lft", 6783 .data = &ipv6_devconf.temp_prefered_lft, 6784 .maxlen = sizeof(int), 6785 .mode = 0644, 6786 .proc_handler = proc_dointvec, 6787 }, 6788 { 6789 .procname = "regen_max_retry", 6790 .data = &ipv6_devconf.regen_max_retry, 6791 .maxlen = sizeof(int), 6792 .mode = 0644, 6793 .proc_handler = proc_dointvec, 6794 }, 6795 { 6796 .procname = "max_desync_factor", 6797 .data = &ipv6_devconf.max_desync_factor, 6798 .maxlen = sizeof(int), 6799 .mode = 0644, 6800 .proc_handler = proc_dointvec, 6801 }, 6802 { 6803 .procname = "max_addresses", 6804 .data = &ipv6_devconf.max_addresses, 6805 .maxlen = sizeof(int), 6806 .mode = 0644, 6807 .proc_handler = proc_dointvec, 6808 }, 6809 { 6810 .procname = "accept_ra_defrtr", 6811 .data = &ipv6_devconf.accept_ra_defrtr, 6812 .maxlen = sizeof(int), 6813 .mode = 0644, 6814 .proc_handler = proc_dointvec, 6815 }, 6816 { 6817 .procname = "ra_defrtr_metric", 6818 .data = &ipv6_devconf.ra_defrtr_metric, 6819 .maxlen = sizeof(u32), 6820 .mode = 0644, 6821 .proc_handler = proc_douintvec_minmax, 6822 .extra1 = (void *)SYSCTL_ONE, 6823 }, 6824 { 6825 .procname = "accept_ra_min_hop_limit", 6826 .data = &ipv6_devconf.accept_ra_min_hop_limit, 6827 .maxlen = sizeof(int), 6828 .mode = 0644, 6829 .proc_handler = proc_dointvec, 6830 }, 6831 { 6832 .procname = "accept_ra_min_lft", 6833 .data = &ipv6_devconf.accept_ra_min_lft, 6834 .maxlen = sizeof(int), 6835 .mode = 0644, 6836 .proc_handler = proc_dointvec, 6837 }, 6838 { 6839 .procname = "accept_ra_pinfo", 6840 .data = &ipv6_devconf.accept_ra_pinfo, 6841 .maxlen = sizeof(int), 6842 .mode = 0644, 6843 .proc_handler = proc_dointvec, 6844 }, 6845 #ifdef CONFIG_IPV6_ROUTER_PREF 6846 { 6847 .procname = "accept_ra_rtr_pref", 6848 .data = &ipv6_devconf.accept_ra_rtr_pref, 6849 .maxlen = sizeof(int), 6850 .mode = 0644, 6851 .proc_handler = proc_dointvec, 6852 }, 6853 { 6854 .procname = "router_probe_interval", 6855 .data = &ipv6_devconf.rtr_probe_interval, 6856 .maxlen = sizeof(int), 6857 .mode = 0644, 6858 .proc_handler = proc_dointvec_jiffies, 6859 }, 6860 #ifdef CONFIG_IPV6_ROUTE_INFO 6861 { 6862 .procname = "accept_ra_rt_info_min_plen", 6863 .data = &ipv6_devconf.accept_ra_rt_info_min_plen, 6864 .maxlen = sizeof(int), 6865 .mode = 0644, 6866 .proc_handler = proc_dointvec, 6867 }, 6868 { 6869 .procname = "accept_ra_rt_info_max_plen", 6870 .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 6871 .maxlen = sizeof(int), 6872 .mode = 0644, 6873 .proc_handler = proc_dointvec, 6874 }, 6875 #endif 6876 #endif 6877 { 6878 .procname = "proxy_ndp", 6879 .data = &ipv6_devconf.proxy_ndp, 6880 .maxlen = sizeof(int), 6881 .mode = 0644, 6882 .proc_handler = addrconf_sysctl_proxy_ndp, 6883 }, 6884 { 6885 .procname = "accept_source_route", 6886 .data = &ipv6_devconf.accept_source_route, 6887 .maxlen = sizeof(int), 6888 .mode = 0644, 6889 .proc_handler = proc_dointvec, 6890 }, 6891 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 6892 { 6893 .procname = "optimistic_dad", 6894 .data = &ipv6_devconf.optimistic_dad, 6895 .maxlen = sizeof(int), 6896 .mode = 0644, 6897 .proc_handler = proc_dointvec, 6898 }, 6899 { 6900 .procname = "use_optimistic", 6901 .data = &ipv6_devconf.use_optimistic, 6902 .maxlen = sizeof(int), 6903 .mode = 0644, 6904 .proc_handler = proc_dointvec, 6905 }, 6906 #endif 6907 #ifdef CONFIG_IPV6_MROUTE 6908 { 6909 .procname = "mc_forwarding", 6910 .data = &ipv6_devconf.mc_forwarding, 6911 .maxlen = sizeof(int), 6912 .mode = 0444, 6913 .proc_handler = proc_dointvec, 6914 }, 6915 #endif 6916 { 6917 .procname = "disable_ipv6", 6918 .data = &ipv6_devconf.disable_ipv6, 6919 .maxlen = sizeof(int), 6920 .mode = 0644, 6921 .proc_handler = addrconf_sysctl_disable, 6922 }, 6923 { 6924 .procname = "accept_dad", 6925 .data = &ipv6_devconf.accept_dad, 6926 .maxlen = sizeof(int), 6927 .mode = 0644, 6928 .proc_handler = proc_dointvec, 6929 }, 6930 { 6931 .procname = "force_tllao", 6932 .data = &ipv6_devconf.force_tllao, 6933 .maxlen = sizeof(int), 6934 .mode = 0644, 6935 .proc_handler = proc_dointvec 6936 }, 6937 { 6938 .procname = "ndisc_notify", 6939 .data = &ipv6_devconf.ndisc_notify, 6940 .maxlen = sizeof(int), 6941 .mode = 0644, 6942 .proc_handler = proc_dointvec 6943 }, 6944 { 6945 .procname = "suppress_frag_ndisc", 6946 .data = &ipv6_devconf.suppress_frag_ndisc, 6947 .maxlen = sizeof(int), 6948 .mode = 0644, 6949 .proc_handler = proc_dointvec 6950 }, 6951 { 6952 .procname = "accept_ra_from_local", 6953 .data = &ipv6_devconf.accept_ra_from_local, 6954 .maxlen = sizeof(int), 6955 .mode = 0644, 6956 .proc_handler = proc_dointvec, 6957 }, 6958 { 6959 .procname = "accept_ra_mtu", 6960 .data = &ipv6_devconf.accept_ra_mtu, 6961 .maxlen = sizeof(int), 6962 .mode = 0644, 6963 .proc_handler = proc_dointvec, 6964 }, 6965 { 6966 .procname = "stable_secret", 6967 .data = &ipv6_devconf.stable_secret, 6968 .maxlen = IPV6_MAX_STRLEN, 6969 .mode = 0600, 6970 .proc_handler = addrconf_sysctl_stable_secret, 6971 }, 6972 { 6973 .procname = "use_oif_addrs_only", 6974 .data = &ipv6_devconf.use_oif_addrs_only, 6975 .maxlen = sizeof(int), 6976 .mode = 0644, 6977 .proc_handler = proc_dointvec, 6978 }, 6979 { 6980 .procname = "ignore_routes_with_linkdown", 6981 .data = &ipv6_devconf.ignore_routes_with_linkdown, 6982 .maxlen = sizeof(int), 6983 .mode = 0644, 6984 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, 6985 }, 6986 { 6987 .procname = "drop_unicast_in_l2_multicast", 6988 .data = &ipv6_devconf.drop_unicast_in_l2_multicast, 6989 .maxlen = sizeof(int), 6990 .mode = 0644, 6991 .proc_handler = proc_dointvec, 6992 }, 6993 { 6994 .procname = "drop_unsolicited_na", 6995 .data = &ipv6_devconf.drop_unsolicited_na, 6996 .maxlen = sizeof(int), 6997 .mode = 0644, 6998 .proc_handler = proc_dointvec, 6999 }, 7000 { 7001 .procname = "keep_addr_on_down", 7002 .data = &ipv6_devconf.keep_addr_on_down, 7003 .maxlen = sizeof(int), 7004 .mode = 0644, 7005 .proc_handler = proc_dointvec, 7006 7007 }, 7008 { 7009 .procname = "seg6_enabled", 7010 .data = &ipv6_devconf.seg6_enabled, 7011 .maxlen = sizeof(int), 7012 .mode = 0644, 7013 .proc_handler = proc_dointvec, 7014 }, 7015 #ifdef CONFIG_IPV6_SEG6_HMAC 7016 { 7017 .procname = "seg6_require_hmac", 7018 .data = &ipv6_devconf.seg6_require_hmac, 7019 .maxlen = sizeof(int), 7020 .mode = 0644, 7021 .proc_handler = proc_dointvec, 7022 }, 7023 #endif 7024 { 7025 .procname = "enhanced_dad", 7026 .data = &ipv6_devconf.enhanced_dad, 7027 .maxlen = sizeof(int), 7028 .mode = 0644, 7029 .proc_handler = proc_dointvec, 7030 }, 7031 { 7032 .procname = "addr_gen_mode", 7033 .data = &ipv6_devconf.addr_gen_mode, 7034 .maxlen = sizeof(int), 7035 .mode = 0644, 7036 .proc_handler = addrconf_sysctl_addr_gen_mode, 7037 }, 7038 { 7039 .procname = "disable_policy", 7040 .data = &ipv6_devconf.disable_policy, 7041 .maxlen = sizeof(int), 7042 .mode = 0644, 7043 .proc_handler = addrconf_sysctl_disable_policy, 7044 }, 7045 { 7046 .procname = "ndisc_tclass", 7047 .data = &ipv6_devconf.ndisc_tclass, 7048 .maxlen = sizeof(int), 7049 .mode = 0644, 7050 .proc_handler = proc_dointvec_minmax, 7051 .extra1 = (void *)SYSCTL_ZERO, 7052 .extra2 = (void *)&two_five_five, 7053 }, 7054 { 7055 .procname = "rpl_seg_enabled", 7056 .data = &ipv6_devconf.rpl_seg_enabled, 7057 .maxlen = sizeof(int), 7058 .mode = 0644, 7059 .proc_handler = proc_dointvec, 7060 }, 7061 { 7062 .procname = "ioam6_enabled", 7063 .data = &ipv6_devconf.ioam6_enabled, 7064 .maxlen = sizeof(u8), 7065 .mode = 0644, 7066 .proc_handler = proc_dou8vec_minmax, 7067 .extra1 = (void *)SYSCTL_ZERO, 7068 .extra2 = (void *)SYSCTL_ONE, 7069 }, 7070 { 7071 .procname = "ioam6_id", 7072 .data = &ipv6_devconf.ioam6_id, 7073 .maxlen = sizeof(u32), 7074 .mode = 0644, 7075 .proc_handler = proc_douintvec_minmax, 7076 .extra1 = (void *)SYSCTL_ZERO, 7077 .extra2 = (void *)&ioam6_if_id_max, 7078 }, 7079 { 7080 .procname = "ioam6_id_wide", 7081 .data = &ipv6_devconf.ioam6_id_wide, 7082 .maxlen = sizeof(u32), 7083 .mode = 0644, 7084 .proc_handler = proc_douintvec, 7085 }, 7086 { 7087 .procname = "ndisc_evict_nocarrier", 7088 .data = &ipv6_devconf.ndisc_evict_nocarrier, 7089 .maxlen = sizeof(u8), 7090 .mode = 0644, 7091 .proc_handler = proc_dou8vec_minmax, 7092 .extra1 = (void *)SYSCTL_ZERO, 7093 .extra2 = (void *)SYSCTL_ONE, 7094 }, 7095 { 7096 .procname = "accept_untracked_na", 7097 .data = &ipv6_devconf.accept_untracked_na, 7098 .maxlen = sizeof(int), 7099 .mode = 0644, 7100 .proc_handler = proc_dointvec_minmax, 7101 .extra1 = SYSCTL_ZERO, 7102 .extra2 = SYSCTL_TWO, 7103 }, 7104 { 7105 /* sentinel */ 7106 } 7107 }; 7108 7109 static int __addrconf_sysctl_register(struct net *net, char *dev_name, 7110 struct inet6_dev *idev, struct ipv6_devconf *p) 7111 { 7112 int i, ifindex; 7113 struct ctl_table *table; 7114 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 7115 7116 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL_ACCOUNT); 7117 if (!table) 7118 goto out; 7119 7120 for (i = 0; table[i].data; i++) { 7121 table[i].data += (char *)p - (char *)&ipv6_devconf; 7122 /* If one of these is already set, then it is not safe to 7123 * overwrite either of them: this makes proc_dointvec_minmax 7124 * usable. 7125 */ 7126 if (!table[i].extra1 && !table[i].extra2) { 7127 table[i].extra1 = idev; /* embedded; no ref */ 7128 table[i].extra2 = net; 7129 } 7130 } 7131 7132 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 7133 7134 p->sysctl_header = register_net_sysctl_sz(net, path, table, 7135 ARRAY_SIZE(addrconf_sysctl)); 7136 if (!p->sysctl_header) 7137 goto free; 7138 7139 if (!strcmp(dev_name, "all")) 7140 ifindex = NETCONFA_IFINDEX_ALL; 7141 else if (!strcmp(dev_name, "default")) 7142 ifindex = NETCONFA_IFINDEX_DEFAULT; 7143 else 7144 ifindex = idev->dev->ifindex; 7145 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, 7146 ifindex, p); 7147 return 0; 7148 7149 free: 7150 kfree(table); 7151 out: 7152 return -ENOBUFS; 7153 } 7154 7155 static void __addrconf_sysctl_unregister(struct net *net, 7156 struct ipv6_devconf *p, int ifindex) 7157 { 7158 struct ctl_table *table; 7159 7160 if (!p->sysctl_header) 7161 return; 7162 7163 table = p->sysctl_header->ctl_table_arg; 7164 unregister_net_sysctl_table(p->sysctl_header); 7165 p->sysctl_header = NULL; 7166 kfree(table); 7167 7168 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL); 7169 } 7170 7171 static int addrconf_sysctl_register(struct inet6_dev *idev) 7172 { 7173 int err; 7174 7175 if (!sysctl_dev_name_is_allowed(idev->dev->name)) 7176 return -EINVAL; 7177 7178 err = neigh_sysctl_register(idev->dev, idev->nd_parms, 7179 &ndisc_ifinfo_sysctl_change); 7180 if (err) 7181 return err; 7182 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 7183 idev, &idev->cnf); 7184 if (err) 7185 neigh_sysctl_unregister(idev->nd_parms); 7186 7187 return err; 7188 } 7189 7190 static void addrconf_sysctl_unregister(struct inet6_dev *idev) 7191 { 7192 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf, 7193 idev->dev->ifindex); 7194 neigh_sysctl_unregister(idev->nd_parms); 7195 } 7196 7197 7198 #endif 7199 7200 static int __net_init addrconf_init_net(struct net *net) 7201 { 7202 int err = -ENOMEM; 7203 struct ipv6_devconf *all, *dflt; 7204 7205 spin_lock_init(&net->ipv6.addrconf_hash_lock); 7206 INIT_DEFERRABLE_WORK(&net->ipv6.addr_chk_work, addrconf_verify_work); 7207 net->ipv6.inet6_addr_lst = kcalloc(IN6_ADDR_HSIZE, 7208 sizeof(struct hlist_head), 7209 GFP_KERNEL); 7210 if (!net->ipv6.inet6_addr_lst) 7211 goto err_alloc_addr; 7212 7213 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 7214 if (!all) 7215 goto err_alloc_all; 7216 7217 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 7218 if (!dflt) 7219 goto err_alloc_dflt; 7220 7221 if (!net_eq(net, &init_net)) { 7222 switch (net_inherit_devconf()) { 7223 case 1: /* copy from init_net */ 7224 memcpy(all, init_net.ipv6.devconf_all, 7225 sizeof(ipv6_devconf)); 7226 memcpy(dflt, init_net.ipv6.devconf_dflt, 7227 sizeof(ipv6_devconf_dflt)); 7228 break; 7229 case 3: /* copy from the current netns */ 7230 memcpy(all, current->nsproxy->net_ns->ipv6.devconf_all, 7231 sizeof(ipv6_devconf)); 7232 memcpy(dflt, 7233 current->nsproxy->net_ns->ipv6.devconf_dflt, 7234 sizeof(ipv6_devconf_dflt)); 7235 break; 7236 case 0: 7237 case 2: 7238 /* use compiled values */ 7239 break; 7240 } 7241 } 7242 7243 /* these will be inherited by all namespaces */ 7244 dflt->autoconf = ipv6_defaults.autoconf; 7245 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 7246 7247 dflt->stable_secret.initialized = false; 7248 all->stable_secret.initialized = false; 7249 7250 net->ipv6.devconf_all = all; 7251 net->ipv6.devconf_dflt = dflt; 7252 7253 #ifdef CONFIG_SYSCTL 7254 err = __addrconf_sysctl_register(net, "all", NULL, all); 7255 if (err < 0) 7256 goto err_reg_all; 7257 7258 err = __addrconf_sysctl_register(net, "default", NULL, dflt); 7259 if (err < 0) 7260 goto err_reg_dflt; 7261 #endif 7262 return 0; 7263 7264 #ifdef CONFIG_SYSCTL 7265 err_reg_dflt: 7266 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL); 7267 err_reg_all: 7268 kfree(dflt); 7269 net->ipv6.devconf_dflt = NULL; 7270 #endif 7271 err_alloc_dflt: 7272 kfree(all); 7273 net->ipv6.devconf_all = NULL; 7274 err_alloc_all: 7275 kfree(net->ipv6.inet6_addr_lst); 7276 err_alloc_addr: 7277 return err; 7278 } 7279 7280 static void __net_exit addrconf_exit_net(struct net *net) 7281 { 7282 int i; 7283 7284 #ifdef CONFIG_SYSCTL 7285 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt, 7286 NETCONFA_IFINDEX_DEFAULT); 7287 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all, 7288 NETCONFA_IFINDEX_ALL); 7289 #endif 7290 kfree(net->ipv6.devconf_dflt); 7291 net->ipv6.devconf_dflt = NULL; 7292 kfree(net->ipv6.devconf_all); 7293 net->ipv6.devconf_all = NULL; 7294 7295 cancel_delayed_work_sync(&net->ipv6.addr_chk_work); 7296 /* 7297 * Check hash table, then free it. 7298 */ 7299 for (i = 0; i < IN6_ADDR_HSIZE; i++) 7300 WARN_ON_ONCE(!hlist_empty(&net->ipv6.inet6_addr_lst[i])); 7301 7302 kfree(net->ipv6.inet6_addr_lst); 7303 net->ipv6.inet6_addr_lst = NULL; 7304 } 7305 7306 static struct pernet_operations addrconf_ops = { 7307 .init = addrconf_init_net, 7308 .exit = addrconf_exit_net, 7309 }; 7310 7311 static struct rtnl_af_ops inet6_ops __read_mostly = { 7312 .family = AF_INET6, 7313 .fill_link_af = inet6_fill_link_af, 7314 .get_link_af_size = inet6_get_link_af_size, 7315 .validate_link_af = inet6_validate_link_af, 7316 .set_link_af = inet6_set_link_af, 7317 }; 7318 7319 /* 7320 * Init / cleanup code 7321 */ 7322 7323 int __init addrconf_init(void) 7324 { 7325 struct inet6_dev *idev; 7326 int err; 7327 7328 err = ipv6_addr_label_init(); 7329 if (err < 0) { 7330 pr_crit("%s: cannot initialize default policy table: %d\n", 7331 __func__, err); 7332 goto out; 7333 } 7334 7335 err = register_pernet_subsys(&addrconf_ops); 7336 if (err < 0) 7337 goto out_addrlabel; 7338 7339 addrconf_wq = create_workqueue("ipv6_addrconf"); 7340 if (!addrconf_wq) { 7341 err = -ENOMEM; 7342 goto out_nowq; 7343 } 7344 7345 rtnl_lock(); 7346 idev = ipv6_add_dev(blackhole_netdev); 7347 rtnl_unlock(); 7348 if (IS_ERR(idev)) { 7349 err = PTR_ERR(idev); 7350 goto errlo; 7351 } 7352 7353 ip6_route_init_special_entries(); 7354 7355 register_netdevice_notifier(&ipv6_dev_notf); 7356 7357 addrconf_verify(&init_net); 7358 7359 rtnl_af_register(&inet6_ops); 7360 7361 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK, 7362 NULL, inet6_dump_ifinfo, 0); 7363 if (err < 0) 7364 goto errout; 7365 7366 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR, 7367 inet6_rtm_newaddr, NULL, 0); 7368 if (err < 0) 7369 goto errout; 7370 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR, 7371 inet6_rtm_deladdr, NULL, 0); 7372 if (err < 0) 7373 goto errout; 7374 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR, 7375 inet6_rtm_getaddr, inet6_dump_ifaddr, 7376 RTNL_FLAG_DOIT_UNLOCKED); 7377 if (err < 0) 7378 goto errout; 7379 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST, 7380 NULL, inet6_dump_ifmcaddr, 0); 7381 if (err < 0) 7382 goto errout; 7383 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST, 7384 NULL, inet6_dump_ifacaddr, 0); 7385 if (err < 0) 7386 goto errout; 7387 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF, 7388 inet6_netconf_get_devconf, 7389 inet6_netconf_dump_devconf, 7390 RTNL_FLAG_DOIT_UNLOCKED); 7391 if (err < 0) 7392 goto errout; 7393 err = ipv6_addr_label_rtnl_register(); 7394 if (err < 0) 7395 goto errout; 7396 7397 return 0; 7398 errout: 7399 rtnl_unregister_all(PF_INET6); 7400 rtnl_af_unregister(&inet6_ops); 7401 unregister_netdevice_notifier(&ipv6_dev_notf); 7402 errlo: 7403 destroy_workqueue(addrconf_wq); 7404 out_nowq: 7405 unregister_pernet_subsys(&addrconf_ops); 7406 out_addrlabel: 7407 ipv6_addr_label_cleanup(); 7408 out: 7409 return err; 7410 } 7411 7412 void addrconf_cleanup(void) 7413 { 7414 struct net_device *dev; 7415 7416 unregister_netdevice_notifier(&ipv6_dev_notf); 7417 unregister_pernet_subsys(&addrconf_ops); 7418 ipv6_addr_label_cleanup(); 7419 7420 rtnl_af_unregister(&inet6_ops); 7421 7422 rtnl_lock(); 7423 7424 /* clean dev list */ 7425 for_each_netdev(&init_net, dev) { 7426 if (__in6_dev_get(dev) == NULL) 7427 continue; 7428 addrconf_ifdown(dev, true); 7429 } 7430 addrconf_ifdown(init_net.loopback_dev, true); 7431 7432 rtnl_unlock(); 7433 7434 destroy_workqueue(addrconf_wq); 7435 } 7436