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