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