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 put_cacheinfo(skb, ifmca->mca_cstamp, READ_ONCE(ifmca->mca_tstamp), 5268 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 5269 nlmsg_cancel(skb, nlh); 5270 return -EMSGSIZE; 5271 } 5272 5273 nlmsg_end(skb, nlh); 5274 return 0; 5275 } 5276 5277 int inet6_fill_ifacaddr(struct sk_buff *skb, 5278 const struct ifacaddr6 *ifaca, 5279 struct inet6_fill_args *args) 5280 { 5281 struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt); 5282 int ifindex = dev ? dev->ifindex : 1; 5283 u8 scope = RT_SCOPE_UNIVERSE; 5284 struct nlmsghdr *nlh; 5285 5286 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 5287 scope = RT_SCOPE_SITE; 5288 5289 nlh = nlmsg_put(skb, args->portid, args->seq, args->event, 5290 sizeof(struct ifaddrmsg), args->flags); 5291 if (!nlh) 5292 return -EMSGSIZE; 5293 5294 if (args->netnsid >= 0 && 5295 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) { 5296 nlmsg_cancel(skb, nlh); 5297 return -EMSGSIZE; 5298 } 5299 5300 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 5301 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || 5302 put_cacheinfo(skb, ifaca->aca_cstamp, READ_ONCE(ifaca->aca_tstamp), 5303 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 5304 nlmsg_cancel(skb, nlh); 5305 return -EMSGSIZE; 5306 } 5307 5308 nlmsg_end(skb, nlh); 5309 return 0; 5310 } 5311 5312 /* called with rcu_read_lock() */ 5313 static int in6_dump_addrs(const struct inet6_dev *idev, struct sk_buff *skb, 5314 struct netlink_callback *cb, int *s_ip_idx, 5315 struct inet6_fill_args *fillargs) 5316 { 5317 const struct ifmcaddr6 *ifmca; 5318 const struct ifacaddr6 *ifaca; 5319 int ip_idx = 0; 5320 int err = 0; 5321 5322 switch (fillargs->type) { 5323 case UNICAST_ADDR: { 5324 const struct inet6_ifaddr *ifa; 5325 fillargs->event = RTM_NEWADDR; 5326 5327 /* unicast address incl. temp addr */ 5328 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) { 5329 if (ip_idx < *s_ip_idx) 5330 goto next; 5331 err = inet6_fill_ifaddr(skb, ifa, fillargs); 5332 if (err < 0) 5333 break; 5334 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 5335 next: 5336 ip_idx++; 5337 } 5338 break; 5339 } 5340 case MULTICAST_ADDR: 5341 fillargs->event = RTM_GETMULTICAST; 5342 5343 /* multicast address */ 5344 for (ifmca = rcu_dereference(idev->mc_list); 5345 ifmca; 5346 ifmca = rcu_dereference(ifmca->next), ip_idx++) { 5347 if (ip_idx < *s_ip_idx) 5348 continue; 5349 err = inet6_fill_ifmcaddr(skb, ifmca, fillargs); 5350 if (err < 0) 5351 break; 5352 } 5353 break; 5354 case ANYCAST_ADDR: 5355 fillargs->event = RTM_GETANYCAST; 5356 /* anycast address */ 5357 for (ifaca = rcu_dereference(idev->ac_list); ifaca; 5358 ifaca = rcu_dereference(ifaca->aca_next), ip_idx++) { 5359 if (ip_idx < *s_ip_idx) 5360 continue; 5361 err = inet6_fill_ifacaddr(skb, ifaca, fillargs); 5362 if (err < 0) 5363 break; 5364 } 5365 break; 5366 default: 5367 break; 5368 } 5369 *s_ip_idx = err ? ip_idx : 0; 5370 return err; 5371 } 5372 5373 static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh, 5374 struct inet6_fill_args *fillargs, 5375 struct net **tgt_net, struct sock *sk, 5376 struct netlink_callback *cb) 5377 { 5378 struct netlink_ext_ack *extack = cb->extack; 5379 struct nlattr *tb[IFA_MAX+1]; 5380 struct ifaddrmsg *ifm; 5381 int err, i; 5382 5383 ifm = nlmsg_payload(nlh, sizeof(*ifm)); 5384 if (!ifm) { 5385 NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request"); 5386 return -EINVAL; 5387 } 5388 5389 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) { 5390 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request"); 5391 return -EINVAL; 5392 } 5393 5394 fillargs->ifindex = ifm->ifa_index; 5395 if (fillargs->ifindex) { 5396 cb->answer_flags |= NLM_F_DUMP_FILTERED; 5397 fillargs->flags |= NLM_F_DUMP_FILTERED; 5398 } 5399 5400 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX, 5401 ifa_ipv6_policy, extack); 5402 if (err < 0) 5403 return err; 5404 5405 for (i = 0; i <= IFA_MAX; ++i) { 5406 if (!tb[i]) 5407 continue; 5408 5409 if (i == IFA_TARGET_NETNSID) { 5410 struct net *net; 5411 5412 fillargs->netnsid = nla_get_s32(tb[i]); 5413 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid); 5414 if (IS_ERR(net)) { 5415 fillargs->netnsid = -1; 5416 NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id"); 5417 return PTR_ERR(net); 5418 } 5419 *tgt_net = net; 5420 } else { 5421 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request"); 5422 return -EINVAL; 5423 } 5424 } 5425 5426 return 0; 5427 } 5428 5429 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 5430 enum addr_type_t type) 5431 { 5432 struct net *tgt_net = sock_net(skb->sk); 5433 const struct nlmsghdr *nlh = cb->nlh; 5434 struct inet6_fill_args fillargs = { 5435 .portid = NETLINK_CB(cb->skb).portid, 5436 .seq = cb->nlh->nlmsg_seq, 5437 .flags = NLM_F_MULTI, 5438 .netnsid = -1, 5439 .type = type, 5440 .force_rt_scope_universe = false, 5441 }; 5442 struct { 5443 unsigned long ifindex; 5444 int ip_idx; 5445 } *ctx = (void *)cb->ctx; 5446 struct net_device *dev; 5447 struct inet6_dev *idev; 5448 int err = 0; 5449 5450 rcu_read_lock(); 5451 if (cb->strict_check) { 5452 err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net, 5453 skb->sk, cb); 5454 if (err < 0) 5455 goto done; 5456 5457 err = 0; 5458 if (fillargs.ifindex) { 5459 dev = dev_get_by_index_rcu(tgt_net, fillargs.ifindex); 5460 if (!dev) { 5461 err = -ENODEV; 5462 goto done; 5463 } 5464 idev = __in6_dev_get(dev); 5465 if (idev) 5466 err = in6_dump_addrs(idev, skb, cb, 5467 &ctx->ip_idx, 5468 &fillargs); 5469 goto done; 5470 } 5471 } 5472 5473 cb->seq = inet6_base_seq(tgt_net); 5474 for_each_netdev_dump(tgt_net, dev, ctx->ifindex) { 5475 idev = __in6_dev_get(dev); 5476 if (!idev) 5477 continue; 5478 err = in6_dump_addrs(idev, skb, cb, &ctx->ip_idx, 5479 &fillargs); 5480 if (err < 0) 5481 goto done; 5482 } 5483 done: 5484 rcu_read_unlock(); 5485 if (fillargs.netnsid >= 0) 5486 put_net(tgt_net); 5487 5488 return err; 5489 } 5490 5491 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 5492 { 5493 enum addr_type_t type = UNICAST_ADDR; 5494 5495 return inet6_dump_addr(skb, cb, type); 5496 } 5497 5498 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 5499 { 5500 enum addr_type_t type = MULTICAST_ADDR; 5501 5502 return inet6_dump_addr(skb, cb, type); 5503 } 5504 5505 5506 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 5507 { 5508 enum addr_type_t type = ANYCAST_ADDR; 5509 5510 return inet6_dump_addr(skb, cb, type); 5511 } 5512 5513 static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb, 5514 const struct nlmsghdr *nlh, 5515 struct nlattr **tb, 5516 struct netlink_ext_ack *extack) 5517 { 5518 struct ifaddrmsg *ifm; 5519 int i, err; 5520 5521 ifm = nlmsg_payload(nlh, sizeof(*ifm)); 5522 if (!ifm) { 5523 NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request"); 5524 return -EINVAL; 5525 } 5526 5527 if (!netlink_strict_get_check(skb)) 5528 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, 5529 ifa_ipv6_policy, extack); 5530 5531 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) { 5532 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request"); 5533 return -EINVAL; 5534 } 5535 5536 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX, 5537 ifa_ipv6_policy, extack); 5538 if (err) 5539 return err; 5540 5541 for (i = 0; i <= IFA_MAX; i++) { 5542 if (!tb[i]) 5543 continue; 5544 5545 switch (i) { 5546 case IFA_TARGET_NETNSID: 5547 case IFA_ADDRESS: 5548 case IFA_LOCAL: 5549 break; 5550 default: 5551 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request"); 5552 return -EINVAL; 5553 } 5554 } 5555 5556 return 0; 5557 } 5558 5559 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh, 5560 struct netlink_ext_ack *extack) 5561 { 5562 struct net *tgt_net = sock_net(in_skb->sk); 5563 struct inet6_fill_args fillargs = { 5564 .portid = NETLINK_CB(in_skb).portid, 5565 .seq = nlh->nlmsg_seq, 5566 .event = RTM_NEWADDR, 5567 .flags = 0, 5568 .netnsid = -1, 5569 .force_rt_scope_universe = false, 5570 }; 5571 struct ifaddrmsg *ifm; 5572 struct nlattr *tb[IFA_MAX+1]; 5573 struct in6_addr *addr = NULL, *peer; 5574 struct net_device *dev = NULL; 5575 struct inet6_ifaddr *ifa; 5576 struct sk_buff *skb; 5577 int err; 5578 5579 err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack); 5580 if (err < 0) 5581 return err; 5582 5583 if (tb[IFA_TARGET_NETNSID]) { 5584 fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]); 5585 5586 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk, 5587 fillargs.netnsid); 5588 if (IS_ERR(tgt_net)) 5589 return PTR_ERR(tgt_net); 5590 } 5591 5592 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 5593 if (!addr) { 5594 err = -EINVAL; 5595 goto errout; 5596 } 5597 ifm = nlmsg_data(nlh); 5598 if (ifm->ifa_index) 5599 dev = dev_get_by_index(tgt_net, ifm->ifa_index); 5600 5601 ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1); 5602 if (!ifa) { 5603 err = -EADDRNOTAVAIL; 5604 goto errout; 5605 } 5606 5607 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 5608 if (!skb) { 5609 err = -ENOBUFS; 5610 goto errout_ifa; 5611 } 5612 5613 err = inet6_fill_ifaddr(skb, ifa, &fillargs); 5614 if (err < 0) { 5615 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 5616 WARN_ON(err == -EMSGSIZE); 5617 kfree_skb(skb); 5618 goto errout_ifa; 5619 } 5620 err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid); 5621 errout_ifa: 5622 in6_ifa_put(ifa); 5623 errout: 5624 dev_put(dev); 5625 if (fillargs.netnsid >= 0) 5626 put_net(tgt_net); 5627 5628 return err; 5629 } 5630 5631 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 5632 { 5633 struct sk_buff *skb; 5634 struct net *net = dev_net(ifa->idev->dev); 5635 struct inet6_fill_args fillargs = { 5636 .portid = 0, 5637 .seq = 0, 5638 .event = event, 5639 .flags = 0, 5640 .netnsid = -1, 5641 .force_rt_scope_universe = false, 5642 }; 5643 int err = -ENOBUFS; 5644 5645 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 5646 if (!skb) 5647 goto errout; 5648 5649 err = inet6_fill_ifaddr(skb, ifa, &fillargs); 5650 if (err < 0) { 5651 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 5652 WARN_ON(err == -EMSGSIZE); 5653 kfree_skb(skb); 5654 goto errout; 5655 } 5656 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 5657 return; 5658 errout: 5659 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 5660 } 5661 5662 static void ipv6_store_devconf(const struct ipv6_devconf *cnf, 5663 __s32 *array, int bytes) 5664 { 5665 BUG_ON(bytes < (DEVCONF_MAX * 4)); 5666 5667 memset(array, 0, bytes); 5668 array[DEVCONF_FORWARDING] = READ_ONCE(cnf->forwarding); 5669 array[DEVCONF_HOPLIMIT] = READ_ONCE(cnf->hop_limit); 5670 array[DEVCONF_MTU6] = READ_ONCE(cnf->mtu6); 5671 array[DEVCONF_ACCEPT_RA] = READ_ONCE(cnf->accept_ra); 5672 array[DEVCONF_ACCEPT_REDIRECTS] = READ_ONCE(cnf->accept_redirects); 5673 array[DEVCONF_AUTOCONF] = READ_ONCE(cnf->autoconf); 5674 array[DEVCONF_DAD_TRANSMITS] = READ_ONCE(cnf->dad_transmits); 5675 array[DEVCONF_RTR_SOLICITS] = READ_ONCE(cnf->rtr_solicits); 5676 array[DEVCONF_RTR_SOLICIT_INTERVAL] = 5677 jiffies_to_msecs(READ_ONCE(cnf->rtr_solicit_interval)); 5678 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] = 5679 jiffies_to_msecs(READ_ONCE(cnf->rtr_solicit_max_interval)); 5680 array[DEVCONF_RTR_SOLICIT_DELAY] = 5681 jiffies_to_msecs(READ_ONCE(cnf->rtr_solicit_delay)); 5682 array[DEVCONF_FORCE_MLD_VERSION] = READ_ONCE(cnf->force_mld_version); 5683 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = 5684 jiffies_to_msecs(READ_ONCE(cnf->mldv1_unsolicited_report_interval)); 5685 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 5686 jiffies_to_msecs(READ_ONCE(cnf->mldv2_unsolicited_report_interval)); 5687 array[DEVCONF_USE_TEMPADDR] = READ_ONCE(cnf->use_tempaddr); 5688 array[DEVCONF_TEMP_VALID_LFT] = READ_ONCE(cnf->temp_valid_lft); 5689 array[DEVCONF_TEMP_PREFERED_LFT] = READ_ONCE(cnf->temp_prefered_lft); 5690 array[DEVCONF_REGEN_MAX_RETRY] = READ_ONCE(cnf->regen_max_retry); 5691 array[DEVCONF_MAX_DESYNC_FACTOR] = READ_ONCE(cnf->max_desync_factor); 5692 array[DEVCONF_MAX_ADDRESSES] = READ_ONCE(cnf->max_addresses); 5693 array[DEVCONF_ACCEPT_RA_DEFRTR] = READ_ONCE(cnf->accept_ra_defrtr); 5694 array[DEVCONF_RA_DEFRTR_METRIC] = READ_ONCE(cnf->ra_defrtr_metric); 5695 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = 5696 READ_ONCE(cnf->accept_ra_min_hop_limit); 5697 array[DEVCONF_ACCEPT_RA_PINFO] = READ_ONCE(cnf->accept_ra_pinfo); 5698 #ifdef CONFIG_IPV6_ROUTER_PREF 5699 array[DEVCONF_ACCEPT_RA_RTR_PREF] = READ_ONCE(cnf->accept_ra_rtr_pref); 5700 array[DEVCONF_RTR_PROBE_INTERVAL] = 5701 jiffies_to_msecs(READ_ONCE(cnf->rtr_probe_interval)); 5702 #ifdef CONFIG_IPV6_ROUTE_INFO 5703 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = 5704 READ_ONCE(cnf->accept_ra_rt_info_min_plen); 5705 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = 5706 READ_ONCE(cnf->accept_ra_rt_info_max_plen); 5707 #endif 5708 #endif 5709 array[DEVCONF_PROXY_NDP] = READ_ONCE(cnf->proxy_ndp); 5710 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = 5711 READ_ONCE(cnf->accept_source_route); 5712 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 5713 array[DEVCONF_OPTIMISTIC_DAD] = READ_ONCE(cnf->optimistic_dad); 5714 array[DEVCONF_USE_OPTIMISTIC] = READ_ONCE(cnf->use_optimistic); 5715 #endif 5716 #ifdef CONFIG_IPV6_MROUTE 5717 array[DEVCONF_MC_FORWARDING] = atomic_read(&cnf->mc_forwarding); 5718 #endif 5719 array[DEVCONF_DISABLE_IPV6] = READ_ONCE(cnf->disable_ipv6); 5720 array[DEVCONF_ACCEPT_DAD] = READ_ONCE(cnf->accept_dad); 5721 array[DEVCONF_FORCE_TLLAO] = READ_ONCE(cnf->force_tllao); 5722 array[DEVCONF_NDISC_NOTIFY] = READ_ONCE(cnf->ndisc_notify); 5723 array[DEVCONF_SUPPRESS_FRAG_NDISC] = 5724 READ_ONCE(cnf->suppress_frag_ndisc); 5725 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = 5726 READ_ONCE(cnf->accept_ra_from_local); 5727 array[DEVCONF_ACCEPT_RA_MTU] = READ_ONCE(cnf->accept_ra_mtu); 5728 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = 5729 READ_ONCE(cnf->ignore_routes_with_linkdown); 5730 /* we omit DEVCONF_STABLE_SECRET for now */ 5731 array[DEVCONF_USE_OIF_ADDRS_ONLY] = READ_ONCE(cnf->use_oif_addrs_only); 5732 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = 5733 READ_ONCE(cnf->drop_unicast_in_l2_multicast); 5734 array[DEVCONF_DROP_UNSOLICITED_NA] = READ_ONCE(cnf->drop_unsolicited_na); 5735 array[DEVCONF_KEEP_ADDR_ON_DOWN] = READ_ONCE(cnf->keep_addr_on_down); 5736 array[DEVCONF_SEG6_ENABLED] = READ_ONCE(cnf->seg6_enabled); 5737 #ifdef CONFIG_IPV6_SEG6_HMAC 5738 array[DEVCONF_SEG6_REQUIRE_HMAC] = READ_ONCE(cnf->seg6_require_hmac); 5739 #endif 5740 array[DEVCONF_ENHANCED_DAD] = READ_ONCE(cnf->enhanced_dad); 5741 array[DEVCONF_ADDR_GEN_MODE] = READ_ONCE(cnf->addr_gen_mode); 5742 array[DEVCONF_DISABLE_POLICY] = READ_ONCE(cnf->disable_policy); 5743 array[DEVCONF_NDISC_TCLASS] = READ_ONCE(cnf->ndisc_tclass); 5744 array[DEVCONF_RPL_SEG_ENABLED] = READ_ONCE(cnf->rpl_seg_enabled); 5745 array[DEVCONF_IOAM6_ENABLED] = READ_ONCE(cnf->ioam6_enabled); 5746 array[DEVCONF_IOAM6_ID] = READ_ONCE(cnf->ioam6_id); 5747 array[DEVCONF_IOAM6_ID_WIDE] = READ_ONCE(cnf->ioam6_id_wide); 5748 array[DEVCONF_NDISC_EVICT_NOCARRIER] = 5749 READ_ONCE(cnf->ndisc_evict_nocarrier); 5750 array[DEVCONF_ACCEPT_UNTRACKED_NA] = 5751 READ_ONCE(cnf->accept_untracked_na); 5752 array[DEVCONF_ACCEPT_RA_MIN_LFT] = READ_ONCE(cnf->accept_ra_min_lft); 5753 array[DEVCONF_FORCE_FORWARDING] = READ_ONCE(cnf->force_forwarding); 5754 } 5755 5756 static inline size_t inet6_ifla6_size(void) 5757 { 5758 return nla_total_size(4) /* IFLA_INET6_FLAGS */ 5759 + nla_total_size(sizeof(struct ifla_cacheinfo)) 5760 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 5761 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 5762 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 5763 + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */ 5764 + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */ 5765 + nla_total_size(4) /* IFLA_INET6_RA_MTU */ 5766 + 0; 5767 } 5768 5769 static inline size_t inet6_if_nlmsg_size(void) 5770 { 5771 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 5772 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 5773 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 5774 + nla_total_size(4) /* IFLA_MTU */ 5775 + nla_total_size(4) /* IFLA_LINK */ 5776 + nla_total_size(1) /* IFLA_OPERSTATE */ 5777 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 5778 } 5779 5780 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 5781 int bytes) 5782 { 5783 int i; 5784 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX; 5785 BUG_ON(pad < 0); 5786 5787 /* Use put_unaligned() because stats may not be aligned for u64. */ 5788 put_unaligned(ICMP6_MIB_MAX, &stats[0]); 5789 for (i = 1; i < ICMP6_MIB_MAX; i++) 5790 put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 5791 5792 memset(&stats[ICMP6_MIB_MAX], 0, pad); 5793 } 5794 5795 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, 5796 int bytes, size_t syncpoff) 5797 { 5798 int i, c; 5799 u64 buff[IPSTATS_MIB_MAX]; 5800 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; 5801 5802 BUG_ON(pad < 0); 5803 5804 memset(buff, 0, sizeof(buff)); 5805 buff[0] = IPSTATS_MIB_MAX; 5806 5807 for_each_possible_cpu(c) { 5808 for (i = 1; i < IPSTATS_MIB_MAX; i++) 5809 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); 5810 } 5811 5812 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); 5813 memset(&stats[IPSTATS_MIB_MAX], 0, pad); 5814 } 5815 5816 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 5817 int bytes) 5818 { 5819 switch (attrtype) { 5820 case IFLA_INET6_STATS: 5821 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, 5822 offsetof(struct ipstats_mib, syncp)); 5823 break; 5824 case IFLA_INET6_ICMP6STATS: 5825 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes); 5826 break; 5827 } 5828 } 5829 5830 static int inet6_fill_ifla6_stats_attrs(struct sk_buff *skb, 5831 struct inet6_dev *idev) 5832 { 5833 struct nlattr *nla; 5834 5835 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 5836 if (!nla) 5837 goto nla_put_failure; 5838 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 5839 5840 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 5841 if (!nla) 5842 goto nla_put_failure; 5843 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 5844 5845 return 0; 5846 5847 nla_put_failure: 5848 return -EMSGSIZE; 5849 } 5850 5851 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev, 5852 u32 ext_filter_mask) 5853 { 5854 struct ifla_cacheinfo ci; 5855 struct nlattr *nla; 5856 u32 ra_mtu; 5857 5858 if (nla_put_u32(skb, IFLA_INET6_FLAGS, READ_ONCE(idev->if_flags))) 5859 goto nla_put_failure; 5860 ci.max_reasm_len = IPV6_MAXPLEN; 5861 ci.tstamp = cstamp_delta(READ_ONCE(idev->tstamp)); 5862 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 5863 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); 5864 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 5865 goto nla_put_failure; 5866 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 5867 if (!nla) 5868 goto nla_put_failure; 5869 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 5870 5871 /* XXX - MC not implemented */ 5872 5873 if (!(ext_filter_mask & RTEXT_FILTER_SKIP_STATS)) { 5874 if (inet6_fill_ifla6_stats_attrs(skb, idev) < 0) 5875 goto nla_put_failure; 5876 } 5877 5878 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 5879 if (!nla) 5880 goto nla_put_failure; 5881 read_lock_bh(&idev->lock); 5882 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 5883 read_unlock_bh(&idev->lock); 5884 5885 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, 5886 READ_ONCE(idev->cnf.addr_gen_mode))) 5887 goto nla_put_failure; 5888 5889 ra_mtu = READ_ONCE(idev->ra_mtu); 5890 if (ra_mtu && nla_put_u32(skb, IFLA_INET6_RA_MTU, ra_mtu)) 5891 goto nla_put_failure; 5892 5893 return 0; 5894 5895 nla_put_failure: 5896 return -EMSGSIZE; 5897 } 5898 5899 static size_t inet6_get_link_af_size(const struct net_device *dev, 5900 u32 ext_filter_mask) 5901 { 5902 if (!__in6_dev_get(dev)) 5903 return 0; 5904 5905 return inet6_ifla6_size(); 5906 } 5907 5908 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev, 5909 u32 ext_filter_mask) 5910 { 5911 struct inet6_dev *idev = __in6_dev_get(dev); 5912 5913 if (!idev) 5914 return -ENODATA; 5915 5916 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0) 5917 return -EMSGSIZE; 5918 5919 return 0; 5920 } 5921 5922 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token, 5923 struct netlink_ext_ack *extack) 5924 { 5925 struct inet6_ifaddr *ifp; 5926 struct net_device *dev = idev->dev; 5927 bool clear_token, update_rs = false; 5928 struct in6_addr ll_addr; 5929 5930 ASSERT_RTNL(); 5931 5932 if (!token) 5933 return -EINVAL; 5934 5935 if (dev->flags & IFF_LOOPBACK) { 5936 NL_SET_ERR_MSG_MOD(extack, "Device is loopback"); 5937 return -EINVAL; 5938 } 5939 5940 if (dev->flags & IFF_NOARP) { 5941 NL_SET_ERR_MSG_MOD(extack, 5942 "Device does not do neighbour discovery"); 5943 return -EINVAL; 5944 } 5945 5946 if (!ipv6_accept_ra(idev)) { 5947 NL_SET_ERR_MSG_MOD(extack, 5948 "Router advertisement is disabled on device"); 5949 return -EINVAL; 5950 } 5951 5952 if (READ_ONCE(idev->cnf.rtr_solicits) == 0) { 5953 NL_SET_ERR_MSG(extack, 5954 "Router solicitation is disabled on device"); 5955 return -EINVAL; 5956 } 5957 5958 write_lock_bh(&idev->lock); 5959 5960 BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 5961 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 5962 5963 write_unlock_bh(&idev->lock); 5964 5965 clear_token = ipv6_addr_any(token); 5966 if (clear_token) 5967 goto update_lft; 5968 5969 if (!idev->dead && (idev->if_flags & IF_READY) && 5970 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 5971 IFA_F_OPTIMISTIC)) { 5972 /* If we're not ready, then normal ifup will take care 5973 * of this. Otherwise, we need to request our rs here. 5974 */ 5975 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 5976 update_rs = true; 5977 } 5978 5979 update_lft: 5980 write_lock_bh(&idev->lock); 5981 5982 if (update_rs) { 5983 idev->if_flags |= IF_RS_SENT; 5984 idev->rs_interval = rfc3315_s14_backoff_init( 5985 READ_ONCE(idev->cnf.rtr_solicit_interval)); 5986 idev->rs_probes = 1; 5987 addrconf_mod_rs_timer(idev, idev->rs_interval); 5988 } 5989 5990 /* Well, that's kinda nasty ... */ 5991 list_for_each_entry(ifp, &idev->addr_list, if_list) { 5992 spin_lock(&ifp->lock); 5993 if (ifp->tokenized) { 5994 ifp->valid_lft = 0; 5995 ifp->prefered_lft = 0; 5996 } 5997 spin_unlock(&ifp->lock); 5998 } 5999 6000 write_unlock_bh(&idev->lock); 6001 inet6_ifinfo_notify(RTM_NEWLINK, idev); 6002 addrconf_verify_rtnl(dev_net(dev)); 6003 return 0; 6004 } 6005 6006 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { 6007 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, 6008 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, 6009 [IFLA_INET6_RA_MTU] = { .type = NLA_REJECT, 6010 .reject_message = 6011 "IFLA_INET6_RA_MTU can not be set" }, 6012 }; 6013 6014 static int check_addr_gen_mode(int mode) 6015 { 6016 if (mode != IN6_ADDR_GEN_MODE_EUI64 && 6017 mode != IN6_ADDR_GEN_MODE_NONE && 6018 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 6019 mode != IN6_ADDR_GEN_MODE_RANDOM) 6020 return -EINVAL; 6021 return 1; 6022 } 6023 6024 static int check_stable_privacy(struct inet6_dev *idev, struct net *net, 6025 int mode) 6026 { 6027 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 6028 !idev->cnf.stable_secret.initialized && 6029 !net->ipv6.devconf_dflt->stable_secret.initialized) 6030 return -EINVAL; 6031 return 1; 6032 } 6033 6034 static int inet6_validate_link_af(const struct net_device *dev, 6035 const struct nlattr *nla, 6036 struct netlink_ext_ack *extack) 6037 { 6038 struct nlattr *tb[IFLA_INET6_MAX + 1]; 6039 struct inet6_dev *idev = NULL; 6040 int err; 6041 6042 if (dev) { 6043 idev = __in6_dev_get(dev); 6044 if (!idev) 6045 return -EAFNOSUPPORT; 6046 } 6047 6048 err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, 6049 inet6_af_policy, extack); 6050 if (err) 6051 return err; 6052 6053 if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE]) 6054 return -EINVAL; 6055 6056 if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 6057 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 6058 6059 if (check_addr_gen_mode(mode) < 0) 6060 return -EINVAL; 6061 if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0) 6062 return -EINVAL; 6063 } 6064 6065 return 0; 6066 } 6067 6068 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla, 6069 struct netlink_ext_ack *extack) 6070 { 6071 struct inet6_dev *idev = __in6_dev_get(dev); 6072 struct nlattr *tb[IFLA_INET6_MAX + 1]; 6073 int err; 6074 6075 if (!idev) 6076 return -EAFNOSUPPORT; 6077 6078 if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0) 6079 return -EINVAL; 6080 6081 if (tb[IFLA_INET6_TOKEN]) { 6082 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]), 6083 extack); 6084 if (err) 6085 return err; 6086 } 6087 6088 if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 6089 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 6090 6091 WRITE_ONCE(idev->cnf.addr_gen_mode, mode); 6092 } 6093 6094 return 0; 6095 } 6096 6097 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 6098 u32 portid, u32 seq, int event, unsigned int flags) 6099 { 6100 struct net_device *dev = idev->dev; 6101 struct ifinfomsg *hdr; 6102 struct nlmsghdr *nlh; 6103 int ifindex, iflink; 6104 void *protoinfo; 6105 6106 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 6107 if (!nlh) 6108 return -EMSGSIZE; 6109 6110 hdr = nlmsg_data(nlh); 6111 hdr->ifi_family = AF_INET6; 6112 hdr->__ifi_pad = 0; 6113 hdr->ifi_type = dev->type; 6114 ifindex = READ_ONCE(dev->ifindex); 6115 hdr->ifi_index = ifindex; 6116 hdr->ifi_flags = netif_get_flags(dev); 6117 hdr->ifi_change = 0; 6118 6119 iflink = dev_get_iflink(dev); 6120 if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 6121 (dev->addr_len && 6122 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 6123 nla_put_u32(skb, IFLA_MTU, READ_ONCE(dev->mtu)) || 6124 (ifindex != iflink && 6125 nla_put_u32(skb, IFLA_LINK, iflink)) || 6126 nla_put_u8(skb, IFLA_OPERSTATE, 6127 netif_running(dev) ? READ_ONCE(dev->operstate) : IF_OPER_DOWN)) 6128 goto nla_put_failure; 6129 protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO); 6130 if (!protoinfo) 6131 goto nla_put_failure; 6132 6133 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0) 6134 goto nla_put_failure; 6135 6136 nla_nest_end(skb, protoinfo); 6137 nlmsg_end(skb, nlh); 6138 return 0; 6139 6140 nla_put_failure: 6141 nlmsg_cancel(skb, nlh); 6142 return -EMSGSIZE; 6143 } 6144 6145 static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh, 6146 struct netlink_ext_ack *extack) 6147 { 6148 struct ifinfomsg *ifm; 6149 6150 ifm = nlmsg_payload(nlh, sizeof(*ifm)); 6151 if (!ifm) { 6152 NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request"); 6153 return -EINVAL; 6154 } 6155 6156 if (nlmsg_attrlen(nlh, sizeof(*ifm))) { 6157 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header"); 6158 return -EINVAL; 6159 } 6160 6161 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 6162 ifm->ifi_change || ifm->ifi_index) { 6163 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request"); 6164 return -EINVAL; 6165 } 6166 6167 return 0; 6168 } 6169 6170 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 6171 { 6172 struct net *net = sock_net(skb->sk); 6173 struct { 6174 unsigned long ifindex; 6175 } *ctx = (void *)cb->ctx; 6176 struct net_device *dev; 6177 struct inet6_dev *idev; 6178 int err; 6179 6180 /* only requests using strict checking can pass data to 6181 * influence the dump 6182 */ 6183 if (cb->strict_check) { 6184 err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack); 6185 6186 if (err < 0) 6187 return err; 6188 } 6189 6190 err = 0; 6191 rcu_read_lock(); 6192 for_each_netdev_dump(net, dev, ctx->ifindex) { 6193 idev = __in6_dev_get(dev); 6194 if (!idev) 6195 continue; 6196 err = inet6_fill_ifinfo(skb, idev, 6197 NETLINK_CB(cb->skb).portid, 6198 cb->nlh->nlmsg_seq, 6199 RTM_NEWLINK, NLM_F_MULTI); 6200 if (err < 0) 6201 break; 6202 } 6203 rcu_read_unlock(); 6204 6205 return err; 6206 } 6207 6208 void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 6209 { 6210 struct sk_buff *skb; 6211 struct net *net = dev_net(idev->dev); 6212 int err = -ENOBUFS; 6213 6214 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 6215 if (!skb) 6216 goto errout; 6217 6218 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 6219 if (err < 0) { 6220 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 6221 WARN_ON(err == -EMSGSIZE); 6222 kfree_skb(skb); 6223 goto errout; 6224 } 6225 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 6226 return; 6227 errout: 6228 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 6229 } 6230 6231 static inline size_t inet6_prefix_nlmsg_size(void) 6232 { 6233 return NLMSG_ALIGN(sizeof(struct prefixmsg)) 6234 + nla_total_size(sizeof(struct in6_addr)) 6235 + nla_total_size(sizeof(struct prefix_cacheinfo)); 6236 } 6237 6238 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 6239 struct prefix_info *pinfo, u32 portid, u32 seq, 6240 int event, unsigned int flags) 6241 { 6242 struct prefixmsg *pmsg; 6243 struct nlmsghdr *nlh; 6244 struct prefix_cacheinfo ci; 6245 6246 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 6247 if (!nlh) 6248 return -EMSGSIZE; 6249 6250 pmsg = nlmsg_data(nlh); 6251 pmsg->prefix_family = AF_INET6; 6252 pmsg->prefix_pad1 = 0; 6253 pmsg->prefix_pad2 = 0; 6254 pmsg->prefix_ifindex = idev->dev->ifindex; 6255 pmsg->prefix_len = pinfo->prefix_len; 6256 pmsg->prefix_type = pinfo->type; 6257 pmsg->prefix_pad3 = 0; 6258 pmsg->prefix_flags = pinfo->flags; 6259 6260 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 6261 goto nla_put_failure; 6262 ci.preferred_time = ntohl(pinfo->prefered); 6263 ci.valid_time = ntohl(pinfo->valid); 6264 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 6265 goto nla_put_failure; 6266 nlmsg_end(skb, nlh); 6267 return 0; 6268 6269 nla_put_failure: 6270 nlmsg_cancel(skb, nlh); 6271 return -EMSGSIZE; 6272 } 6273 6274 static void inet6_prefix_notify(int event, struct inet6_dev *idev, 6275 struct prefix_info *pinfo) 6276 { 6277 struct sk_buff *skb; 6278 struct net *net = dev_net(idev->dev); 6279 int err = -ENOBUFS; 6280 6281 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 6282 if (!skb) 6283 goto errout; 6284 6285 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 6286 if (err < 0) { 6287 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 6288 WARN_ON(err == -EMSGSIZE); 6289 kfree_skb(skb); 6290 goto errout; 6291 } 6292 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 6293 return; 6294 errout: 6295 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 6296 } 6297 6298 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 6299 { 6300 struct net *net = dev_net(ifp->idev->dev); 6301 6302 if (event) 6303 ASSERT_RTNL(); 6304 6305 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 6306 6307 switch (event) { 6308 case RTM_NEWADDR: 6309 /* 6310 * If the address was optimistic we inserted the route at the 6311 * start of our DAD process, so we don't need to do it again. 6312 * If the device was taken down in the middle of the DAD 6313 * cycle there is a race where we could get here without a 6314 * host route, so nothing to insert. That will be fixed when 6315 * the device is brought up. 6316 */ 6317 if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) { 6318 ip6_ins_rt(net, ifp->rt); 6319 } else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) { 6320 pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n", 6321 &ifp->addr, ifp->idev->dev->name); 6322 } 6323 6324 if (ifp->idev->cnf.forwarding) 6325 addrconf_join_anycast(ifp); 6326 if (!ipv6_addr_any(&ifp->peer_addr)) 6327 addrconf_prefix_route(&ifp->peer_addr, 128, 6328 ifp->rt_priority, ifp->idev->dev, 6329 0, 0, GFP_ATOMIC); 6330 break; 6331 case RTM_DELADDR: 6332 if (ifp->idev->cnf.forwarding) 6333 addrconf_leave_anycast(ifp); 6334 addrconf_leave_solict(ifp->idev, &ifp->addr); 6335 if (!ipv6_addr_any(&ifp->peer_addr)) { 6336 struct fib6_info *rt; 6337 6338 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, 6339 ifp->idev->dev, 0, 0, 6340 false); 6341 if (rt) 6342 ip6_del_rt(net, rt, false); 6343 } 6344 if (ifp->rt) { 6345 ip6_del_rt(net, ifp->rt, false); 6346 ifp->rt = NULL; 6347 } 6348 rt_genid_bump_ipv6(net); 6349 break; 6350 } 6351 atomic_inc(&net->ipv6.dev_addr_genid); 6352 } 6353 6354 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 6355 { 6356 if (likely(ifp->idev->dead == 0)) 6357 __ipv6_ifa_notify(event, ifp); 6358 } 6359 6360 #ifdef CONFIG_SYSCTL 6361 6362 static int addrconf_sysctl_forward(const struct ctl_table *ctl, int write, 6363 void *buffer, size_t *lenp, loff_t *ppos) 6364 { 6365 int *valp = ctl->data; 6366 int val = *valp; 6367 loff_t pos = *ppos; 6368 struct ctl_table lctl; 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 if (ret) 6385 *ppos = pos; 6386 return ret; 6387 } 6388 6389 static int addrconf_sysctl_mtu(const struct ctl_table *ctl, int write, 6390 void *buffer, size_t *lenp, loff_t *ppos) 6391 { 6392 struct inet6_dev *idev = ctl->extra1; 6393 int min_mtu = IPV6_MIN_MTU; 6394 struct ctl_table lctl; 6395 6396 lctl = *ctl; 6397 lctl.extra1 = &min_mtu; 6398 lctl.extra2 = idev ? &idev->dev->mtu : NULL; 6399 6400 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 6401 } 6402 6403 static void dev_disable_change(struct inet6_dev *idev) 6404 { 6405 struct netdev_notifier_info info; 6406 6407 if (!idev || !idev->dev) 6408 return; 6409 6410 netdev_notifier_info_init(&info, idev->dev); 6411 if (idev->cnf.disable_ipv6) 6412 addrconf_notify(NULL, NETDEV_DOWN, &info); 6413 else 6414 addrconf_notify(NULL, NETDEV_UP, &info); 6415 } 6416 6417 static void addrconf_disable_change(struct net *net, __s32 newf) 6418 { 6419 struct net_device *dev; 6420 struct inet6_dev *idev; 6421 6422 for_each_netdev(net, dev) { 6423 idev = __in6_dev_get_rtnl_net(dev); 6424 if (idev) { 6425 int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 6426 6427 WRITE_ONCE(idev->cnf.disable_ipv6, newf); 6428 if (changed) 6429 dev_disable_change(idev); 6430 } 6431 } 6432 } 6433 6434 static int addrconf_disable_ipv6(const struct ctl_table *table, int *p, int newf) 6435 { 6436 struct net *net = (struct net *)table->extra2; 6437 int old; 6438 6439 if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 6440 WRITE_ONCE(*p, newf); 6441 return 0; 6442 } 6443 6444 if (!rtnl_net_trylock(net)) 6445 return restart_syscall(); 6446 6447 old = *p; 6448 WRITE_ONCE(*p, newf); 6449 6450 if (p == &net->ipv6.devconf_all->disable_ipv6) { 6451 WRITE_ONCE(net->ipv6.devconf_dflt->disable_ipv6, newf); 6452 addrconf_disable_change(net, newf); 6453 } else if ((!newf) ^ (!old)) { 6454 dev_disable_change((struct inet6_dev *)table->extra1); 6455 } 6456 6457 rtnl_net_unlock(net); 6458 return 0; 6459 } 6460 6461 static int addrconf_sysctl_disable(const struct ctl_table *ctl, int write, 6462 void *buffer, size_t *lenp, loff_t *ppos) 6463 { 6464 int *valp = ctl->data; 6465 int val = *valp; 6466 loff_t pos = *ppos; 6467 struct ctl_table lctl; 6468 int ret; 6469 6470 /* 6471 * ctl->data points to idev->cnf.disable_ipv6, we should 6472 * not modify it until we get the rtnl lock. 6473 */ 6474 lctl = *ctl; 6475 lctl.data = &val; 6476 6477 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 6478 if (ret) 6479 return ret; 6480 6481 if (write) 6482 ret = addrconf_disable_ipv6(ctl, valp, val); 6483 if (ret) 6484 *ppos = pos; 6485 return ret; 6486 } 6487 6488 static int addrconf_sysctl_proxy_ndp(const struct ctl_table *ctl, int write, 6489 void *buffer, size_t *lenp, loff_t *ppos) 6490 { 6491 struct net *net = ctl->extra2; 6492 int *valp = ctl->data; 6493 int old, new; 6494 int ret; 6495 6496 if (write && !rtnl_net_trylock(net)) 6497 return restart_syscall(); 6498 6499 old = *valp; 6500 ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 6501 new = *valp; 6502 6503 if (write && old != new) { 6504 if (valp == &net->ipv6.devconf_dflt->proxy_ndp) { 6505 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 6506 NETCONFA_PROXY_NEIGH, 6507 NETCONFA_IFINDEX_DEFAULT, 6508 net->ipv6.devconf_dflt); 6509 } else if (valp == &net->ipv6.devconf_all->proxy_ndp) { 6510 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 6511 NETCONFA_PROXY_NEIGH, 6512 NETCONFA_IFINDEX_ALL, 6513 net->ipv6.devconf_all); 6514 } else { 6515 struct inet6_dev *idev = ctl->extra1; 6516 6517 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 6518 NETCONFA_PROXY_NEIGH, 6519 idev->dev->ifindex, 6520 &idev->cnf); 6521 } 6522 } 6523 if (write) 6524 rtnl_net_unlock(net); 6525 6526 return ret; 6527 } 6528 6529 static int addrconf_sysctl_addr_gen_mode(const struct ctl_table *ctl, int write, 6530 void *buffer, size_t *lenp, 6531 loff_t *ppos) 6532 { 6533 int ret = 0; 6534 u32 new_val; 6535 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1; 6536 struct net *net = (struct net *)ctl->extra2; 6537 struct ctl_table tmp = { 6538 .data = &new_val, 6539 .maxlen = sizeof(new_val), 6540 .mode = ctl->mode, 6541 }; 6542 6543 if (!rtnl_net_trylock(net)) 6544 return restart_syscall(); 6545 6546 new_val = *((u32 *)ctl->data); 6547 6548 ret = proc_douintvec(&tmp, write, buffer, lenp, ppos); 6549 if (ret != 0) 6550 goto out; 6551 6552 if (write) { 6553 if (check_addr_gen_mode(new_val) < 0) { 6554 ret = -EINVAL; 6555 goto out; 6556 } 6557 6558 if (idev) { 6559 if (check_stable_privacy(idev, net, new_val) < 0) { 6560 ret = -EINVAL; 6561 goto out; 6562 } 6563 6564 if (idev->cnf.addr_gen_mode != new_val) { 6565 WRITE_ONCE(idev->cnf.addr_gen_mode, new_val); 6566 netdev_lock_ops(idev->dev); 6567 addrconf_init_auto_addrs(idev->dev); 6568 netdev_unlock_ops(idev->dev); 6569 } 6570 } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) { 6571 struct net_device *dev; 6572 6573 WRITE_ONCE(net->ipv6.devconf_dflt->addr_gen_mode, new_val); 6574 for_each_netdev(net, dev) { 6575 idev = __in6_dev_get_rtnl_net(dev); 6576 if (idev && 6577 idev->cnf.addr_gen_mode != new_val) { 6578 WRITE_ONCE(idev->cnf.addr_gen_mode, 6579 new_val); 6580 netdev_lock_ops(idev->dev); 6581 addrconf_init_auto_addrs(idev->dev); 6582 netdev_unlock_ops(idev->dev); 6583 } 6584 } 6585 } 6586 6587 WRITE_ONCE(*((u32 *)ctl->data), new_val); 6588 } 6589 6590 out: 6591 rtnl_net_unlock(net); 6592 6593 return ret; 6594 } 6595 6596 static int addrconf_sysctl_stable_secret(const struct ctl_table *ctl, int write, 6597 void *buffer, size_t *lenp, 6598 loff_t *ppos) 6599 { 6600 int err; 6601 struct in6_addr addr; 6602 char str[IPV6_MAX_STRLEN]; 6603 struct ctl_table lctl = *ctl; 6604 struct net *net = ctl->extra2; 6605 struct ipv6_stable_secret *secret = ctl->data; 6606 6607 if (&net->ipv6.devconf_all->stable_secret == ctl->data) 6608 return -EIO; 6609 6610 lctl.maxlen = IPV6_MAX_STRLEN; 6611 lctl.data = str; 6612 6613 if (!rtnl_net_trylock(net)) 6614 return restart_syscall(); 6615 6616 if (!write && !secret->initialized) { 6617 err = -EIO; 6618 goto out; 6619 } 6620 6621 err = snprintf(str, sizeof(str), "%pI6", &secret->secret); 6622 if (err >= sizeof(str)) { 6623 err = -EIO; 6624 goto out; 6625 } 6626 6627 err = proc_dostring(&lctl, write, buffer, lenp, ppos); 6628 if (err || !write) 6629 goto out; 6630 6631 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { 6632 err = -EIO; 6633 goto out; 6634 } 6635 6636 secret->initialized = true; 6637 secret->secret = addr; 6638 6639 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { 6640 struct net_device *dev; 6641 6642 for_each_netdev(net, dev) { 6643 struct inet6_dev *idev = __in6_dev_get_rtnl_net(dev); 6644 6645 if (idev) { 6646 WRITE_ONCE(idev->cnf.addr_gen_mode, 6647 IN6_ADDR_GEN_MODE_STABLE_PRIVACY); 6648 } 6649 } 6650 } else { 6651 struct inet6_dev *idev = ctl->extra1; 6652 6653 WRITE_ONCE(idev->cnf.addr_gen_mode, 6654 IN6_ADDR_GEN_MODE_STABLE_PRIVACY); 6655 } 6656 6657 out: 6658 rtnl_net_unlock(net); 6659 6660 return err; 6661 } 6662 6663 static 6664 int addrconf_sysctl_ignore_routes_with_linkdown(const struct ctl_table *ctl, 6665 int write, void *buffer, 6666 size_t *lenp, 6667 loff_t *ppos) 6668 { 6669 int *valp = ctl->data; 6670 int val = *valp; 6671 loff_t pos = *ppos; 6672 struct ctl_table lctl; 6673 int ret; 6674 6675 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown 6676 * we should not modify it until we get the rtnl lock. 6677 */ 6678 lctl = *ctl; 6679 lctl.data = &val; 6680 6681 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 6682 if (ret) 6683 return ret; 6684 6685 if (write) 6686 ret = addrconf_fixup_linkdown(ctl, valp, val); 6687 if (ret) 6688 *ppos = pos; 6689 return ret; 6690 } 6691 6692 static 6693 void addrconf_set_nopolicy(struct rt6_info *rt, int action) 6694 { 6695 if (rt) { 6696 if (action) 6697 rt->dst.flags |= DST_NOPOLICY; 6698 else 6699 rt->dst.flags &= ~DST_NOPOLICY; 6700 } 6701 } 6702 6703 static 6704 void addrconf_disable_policy_idev(struct inet6_dev *idev, int val) 6705 { 6706 struct inet6_ifaddr *ifa; 6707 6708 read_lock_bh(&idev->lock); 6709 list_for_each_entry(ifa, &idev->addr_list, if_list) { 6710 spin_lock(&ifa->lock); 6711 if (ifa->rt) { 6712 /* host routes only use builtin fib6_nh */ 6713 struct fib6_nh *nh = ifa->rt->fib6_nh; 6714 int cpu; 6715 6716 rcu_read_lock(); 6717 ifa->rt->dst_nopolicy = val ? true : false; 6718 if (nh->rt6i_pcpu) { 6719 for_each_possible_cpu(cpu) { 6720 struct rt6_info **rtp; 6721 6722 rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu); 6723 addrconf_set_nopolicy(*rtp, val); 6724 } 6725 } 6726 rcu_read_unlock(); 6727 } 6728 spin_unlock(&ifa->lock); 6729 } 6730 read_unlock_bh(&idev->lock); 6731 } 6732 6733 static 6734 int addrconf_disable_policy(const struct ctl_table *ctl, int *valp, int val) 6735 { 6736 struct net *net = (struct net *)ctl->extra2; 6737 struct inet6_dev *idev; 6738 6739 if (valp == &net->ipv6.devconf_dflt->disable_policy) { 6740 WRITE_ONCE(*valp, val); 6741 return 0; 6742 } 6743 6744 if (!rtnl_net_trylock(net)) 6745 return restart_syscall(); 6746 6747 WRITE_ONCE(*valp, val); 6748 6749 if (valp == &net->ipv6.devconf_all->disable_policy) { 6750 struct net_device *dev; 6751 6752 for_each_netdev(net, dev) { 6753 idev = __in6_dev_get_rtnl_net(dev); 6754 if (idev) 6755 addrconf_disable_policy_idev(idev, val); 6756 } 6757 } else { 6758 idev = (struct inet6_dev *)ctl->extra1; 6759 addrconf_disable_policy_idev(idev, val); 6760 } 6761 6762 rtnl_net_unlock(net); 6763 return 0; 6764 } 6765 6766 static int addrconf_sysctl_disable_policy(const struct ctl_table *ctl, int write, 6767 void *buffer, size_t *lenp, loff_t *ppos) 6768 { 6769 int *valp = ctl->data; 6770 int val = *valp; 6771 loff_t pos = *ppos; 6772 struct ctl_table lctl; 6773 int ret; 6774 6775 lctl = *ctl; 6776 lctl.data = &val; 6777 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 6778 if (ret) 6779 return ret; 6780 6781 if (write && (*valp != val)) 6782 ret = addrconf_disable_policy(ctl, valp, val); 6783 6784 if (ret) 6785 *ppos = pos; 6786 6787 return ret; 6788 } 6789 6790 static void addrconf_force_forward_change(struct net *net, __s32 newf) 6791 { 6792 struct net_device *dev; 6793 struct inet6_dev *idev; 6794 6795 for_each_netdev(net, dev) { 6796 idev = __in6_dev_get_rtnl_net(dev); 6797 if (idev) { 6798 int changed = (!idev->cnf.force_forwarding) ^ (!newf); 6799 6800 WRITE_ONCE(idev->cnf.force_forwarding, newf); 6801 if (changed) 6802 inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF, 6803 NETCONFA_FORCE_FORWARDING, 6804 dev->ifindex, &idev->cnf); 6805 } 6806 } 6807 } 6808 6809 static int addrconf_sysctl_force_forwarding(const struct ctl_table *ctl, int write, 6810 void *buffer, size_t *lenp, loff_t *ppos) 6811 { 6812 struct inet6_dev *idev = ctl->extra1; 6813 struct ctl_table tmp_ctl = *ctl; 6814 struct net *net = ctl->extra2; 6815 int *valp = ctl->data; 6816 int new_val = *valp; 6817 int old_val = *valp; 6818 loff_t pos = *ppos; 6819 int ret; 6820 6821 tmp_ctl.extra1 = SYSCTL_ZERO; 6822 tmp_ctl.extra2 = SYSCTL_ONE; 6823 tmp_ctl.data = &new_val; 6824 6825 ret = proc_douintvec_minmax(&tmp_ctl, write, buffer, lenp, ppos); 6826 6827 if (write && old_val != new_val) { 6828 if (!rtnl_net_trylock(net)) 6829 return restart_syscall(); 6830 6831 WRITE_ONCE(*valp, new_val); 6832 6833 if (valp == &net->ipv6.devconf_dflt->force_forwarding) { 6834 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 6835 NETCONFA_FORCE_FORWARDING, 6836 NETCONFA_IFINDEX_DEFAULT, 6837 net->ipv6.devconf_dflt); 6838 } else if (valp == &net->ipv6.devconf_all->force_forwarding) { 6839 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 6840 NETCONFA_FORCE_FORWARDING, 6841 NETCONFA_IFINDEX_ALL, 6842 net->ipv6.devconf_all); 6843 6844 addrconf_force_forward_change(net, new_val); 6845 } else { 6846 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, 6847 NETCONFA_FORCE_FORWARDING, 6848 idev->dev->ifindex, 6849 &idev->cnf); 6850 } 6851 rtnl_net_unlock(net); 6852 } 6853 6854 if (ret) 6855 *ppos = pos; 6856 return ret; 6857 } 6858 6859 static int minus_one = -1; 6860 static const int two_five_five = 255; 6861 static u32 ioam6_if_id_max = U16_MAX; 6862 6863 static const struct ctl_table addrconf_sysctl[] = { 6864 { 6865 .procname = "forwarding", 6866 .data = &ipv6_devconf.forwarding, 6867 .maxlen = sizeof(int), 6868 .mode = 0644, 6869 .proc_handler = addrconf_sysctl_forward, 6870 }, 6871 { 6872 .procname = "hop_limit", 6873 .data = &ipv6_devconf.hop_limit, 6874 .maxlen = sizeof(int), 6875 .mode = 0644, 6876 .proc_handler = proc_dointvec_minmax, 6877 .extra1 = (void *)SYSCTL_ONE, 6878 .extra2 = (void *)&two_five_five, 6879 }, 6880 { 6881 .procname = "mtu", 6882 .data = &ipv6_devconf.mtu6, 6883 .maxlen = sizeof(int), 6884 .mode = 0644, 6885 .proc_handler = addrconf_sysctl_mtu, 6886 }, 6887 { 6888 .procname = "accept_ra", 6889 .data = &ipv6_devconf.accept_ra, 6890 .maxlen = sizeof(int), 6891 .mode = 0644, 6892 .proc_handler = proc_dointvec, 6893 }, 6894 { 6895 .procname = "accept_redirects", 6896 .data = &ipv6_devconf.accept_redirects, 6897 .maxlen = sizeof(int), 6898 .mode = 0644, 6899 .proc_handler = proc_dointvec, 6900 }, 6901 { 6902 .procname = "autoconf", 6903 .data = &ipv6_devconf.autoconf, 6904 .maxlen = sizeof(int), 6905 .mode = 0644, 6906 .proc_handler = proc_dointvec, 6907 }, 6908 { 6909 .procname = "dad_transmits", 6910 .data = &ipv6_devconf.dad_transmits, 6911 .maxlen = sizeof(int), 6912 .mode = 0644, 6913 .proc_handler = proc_dointvec, 6914 }, 6915 { 6916 .procname = "router_solicitations", 6917 .data = &ipv6_devconf.rtr_solicits, 6918 .maxlen = sizeof(int), 6919 .mode = 0644, 6920 .proc_handler = proc_dointvec_minmax, 6921 .extra1 = &minus_one, 6922 }, 6923 { 6924 .procname = "router_solicitation_interval", 6925 .data = &ipv6_devconf.rtr_solicit_interval, 6926 .maxlen = sizeof(int), 6927 .mode = 0644, 6928 .proc_handler = proc_dointvec_jiffies, 6929 }, 6930 { 6931 .procname = "router_solicitation_max_interval", 6932 .data = &ipv6_devconf.rtr_solicit_max_interval, 6933 .maxlen = sizeof(int), 6934 .mode = 0644, 6935 .proc_handler = proc_dointvec_jiffies, 6936 }, 6937 { 6938 .procname = "router_solicitation_delay", 6939 .data = &ipv6_devconf.rtr_solicit_delay, 6940 .maxlen = sizeof(int), 6941 .mode = 0644, 6942 .proc_handler = proc_dointvec_jiffies, 6943 }, 6944 { 6945 .procname = "force_mld_version", 6946 .data = &ipv6_devconf.force_mld_version, 6947 .maxlen = sizeof(int), 6948 .mode = 0644, 6949 .proc_handler = proc_dointvec, 6950 }, 6951 { 6952 .procname = "mldv1_unsolicited_report_interval", 6953 .data = 6954 &ipv6_devconf.mldv1_unsolicited_report_interval, 6955 .maxlen = sizeof(int), 6956 .mode = 0644, 6957 .proc_handler = proc_dointvec_ms_jiffies, 6958 }, 6959 { 6960 .procname = "mldv2_unsolicited_report_interval", 6961 .data = 6962 &ipv6_devconf.mldv2_unsolicited_report_interval, 6963 .maxlen = sizeof(int), 6964 .mode = 0644, 6965 .proc_handler = proc_dointvec_ms_jiffies, 6966 }, 6967 { 6968 .procname = "use_tempaddr", 6969 .data = &ipv6_devconf.use_tempaddr, 6970 .maxlen = sizeof(int), 6971 .mode = 0644, 6972 .proc_handler = proc_dointvec, 6973 }, 6974 { 6975 .procname = "temp_valid_lft", 6976 .data = &ipv6_devconf.temp_valid_lft, 6977 .maxlen = sizeof(int), 6978 .mode = 0644, 6979 .proc_handler = proc_dointvec, 6980 }, 6981 { 6982 .procname = "temp_prefered_lft", 6983 .data = &ipv6_devconf.temp_prefered_lft, 6984 .maxlen = sizeof(int), 6985 .mode = 0644, 6986 .proc_handler = proc_dointvec, 6987 }, 6988 { 6989 .procname = "regen_min_advance", 6990 .data = &ipv6_devconf.regen_min_advance, 6991 .maxlen = sizeof(int), 6992 .mode = 0644, 6993 .proc_handler = proc_dointvec, 6994 }, 6995 { 6996 .procname = "regen_max_retry", 6997 .data = &ipv6_devconf.regen_max_retry, 6998 .maxlen = sizeof(int), 6999 .mode = 0644, 7000 .proc_handler = proc_dointvec, 7001 }, 7002 { 7003 .procname = "max_desync_factor", 7004 .data = &ipv6_devconf.max_desync_factor, 7005 .maxlen = sizeof(int), 7006 .mode = 0644, 7007 .proc_handler = proc_dointvec, 7008 }, 7009 { 7010 .procname = "max_addresses", 7011 .data = &ipv6_devconf.max_addresses, 7012 .maxlen = sizeof(int), 7013 .mode = 0644, 7014 .proc_handler = proc_dointvec, 7015 }, 7016 { 7017 .procname = "accept_ra_defrtr", 7018 .data = &ipv6_devconf.accept_ra_defrtr, 7019 .maxlen = sizeof(int), 7020 .mode = 0644, 7021 .proc_handler = proc_dointvec, 7022 }, 7023 { 7024 .procname = "ra_defrtr_metric", 7025 .data = &ipv6_devconf.ra_defrtr_metric, 7026 .maxlen = sizeof(u32), 7027 .mode = 0644, 7028 .proc_handler = proc_douintvec_minmax, 7029 .extra1 = (void *)SYSCTL_ONE, 7030 }, 7031 { 7032 .procname = "accept_ra_min_hop_limit", 7033 .data = &ipv6_devconf.accept_ra_min_hop_limit, 7034 .maxlen = sizeof(int), 7035 .mode = 0644, 7036 .proc_handler = proc_dointvec, 7037 }, 7038 { 7039 .procname = "accept_ra_min_lft", 7040 .data = &ipv6_devconf.accept_ra_min_lft, 7041 .maxlen = sizeof(int), 7042 .mode = 0644, 7043 .proc_handler = proc_dointvec, 7044 }, 7045 { 7046 .procname = "accept_ra_pinfo", 7047 .data = &ipv6_devconf.accept_ra_pinfo, 7048 .maxlen = sizeof(int), 7049 .mode = 0644, 7050 .proc_handler = proc_dointvec, 7051 }, 7052 { 7053 .procname = "ra_honor_pio_life", 7054 .data = &ipv6_devconf.ra_honor_pio_life, 7055 .maxlen = sizeof(u8), 7056 .mode = 0644, 7057 .proc_handler = proc_dou8vec_minmax, 7058 .extra1 = SYSCTL_ZERO, 7059 .extra2 = SYSCTL_ONE, 7060 }, 7061 { 7062 .procname = "ra_honor_pio_pflag", 7063 .data = &ipv6_devconf.ra_honor_pio_pflag, 7064 .maxlen = sizeof(u8), 7065 .mode = 0644, 7066 .proc_handler = proc_dou8vec_minmax, 7067 .extra1 = SYSCTL_ZERO, 7068 .extra2 = SYSCTL_ONE, 7069 }, 7070 #ifdef CONFIG_IPV6_ROUTER_PREF 7071 { 7072 .procname = "accept_ra_rtr_pref", 7073 .data = &ipv6_devconf.accept_ra_rtr_pref, 7074 .maxlen = sizeof(int), 7075 .mode = 0644, 7076 .proc_handler = proc_dointvec, 7077 }, 7078 { 7079 .procname = "router_probe_interval", 7080 .data = &ipv6_devconf.rtr_probe_interval, 7081 .maxlen = sizeof(int), 7082 .mode = 0644, 7083 .proc_handler = proc_dointvec_jiffies, 7084 }, 7085 #ifdef CONFIG_IPV6_ROUTE_INFO 7086 { 7087 .procname = "accept_ra_rt_info_min_plen", 7088 .data = &ipv6_devconf.accept_ra_rt_info_min_plen, 7089 .maxlen = sizeof(int), 7090 .mode = 0644, 7091 .proc_handler = proc_dointvec, 7092 }, 7093 { 7094 .procname = "accept_ra_rt_info_max_plen", 7095 .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 7096 .maxlen = sizeof(int), 7097 .mode = 0644, 7098 .proc_handler = proc_dointvec, 7099 }, 7100 #endif 7101 #endif 7102 { 7103 .procname = "proxy_ndp", 7104 .data = &ipv6_devconf.proxy_ndp, 7105 .maxlen = sizeof(int), 7106 .mode = 0644, 7107 .proc_handler = addrconf_sysctl_proxy_ndp, 7108 }, 7109 { 7110 .procname = "accept_source_route", 7111 .data = &ipv6_devconf.accept_source_route, 7112 .maxlen = sizeof(int), 7113 .mode = 0644, 7114 .proc_handler = proc_dointvec, 7115 }, 7116 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 7117 { 7118 .procname = "optimistic_dad", 7119 .data = &ipv6_devconf.optimistic_dad, 7120 .maxlen = sizeof(int), 7121 .mode = 0644, 7122 .proc_handler = proc_dointvec, 7123 }, 7124 { 7125 .procname = "use_optimistic", 7126 .data = &ipv6_devconf.use_optimistic, 7127 .maxlen = sizeof(int), 7128 .mode = 0644, 7129 .proc_handler = proc_dointvec, 7130 }, 7131 #endif 7132 #ifdef CONFIG_IPV6_MROUTE 7133 { 7134 .procname = "mc_forwarding", 7135 .data = &ipv6_devconf.mc_forwarding, 7136 .maxlen = sizeof(int), 7137 .mode = 0444, 7138 .proc_handler = proc_dointvec, 7139 }, 7140 #endif 7141 { 7142 .procname = "disable_ipv6", 7143 .data = &ipv6_devconf.disable_ipv6, 7144 .maxlen = sizeof(int), 7145 .mode = 0644, 7146 .proc_handler = addrconf_sysctl_disable, 7147 }, 7148 { 7149 .procname = "accept_dad", 7150 .data = &ipv6_devconf.accept_dad, 7151 .maxlen = sizeof(int), 7152 .mode = 0644, 7153 .proc_handler = proc_dointvec, 7154 }, 7155 { 7156 .procname = "force_tllao", 7157 .data = &ipv6_devconf.force_tllao, 7158 .maxlen = sizeof(int), 7159 .mode = 0644, 7160 .proc_handler = proc_dointvec 7161 }, 7162 { 7163 .procname = "ndisc_notify", 7164 .data = &ipv6_devconf.ndisc_notify, 7165 .maxlen = sizeof(int), 7166 .mode = 0644, 7167 .proc_handler = proc_dointvec 7168 }, 7169 { 7170 .procname = "suppress_frag_ndisc", 7171 .data = &ipv6_devconf.suppress_frag_ndisc, 7172 .maxlen = sizeof(int), 7173 .mode = 0644, 7174 .proc_handler = proc_dointvec 7175 }, 7176 { 7177 .procname = "accept_ra_from_local", 7178 .data = &ipv6_devconf.accept_ra_from_local, 7179 .maxlen = sizeof(int), 7180 .mode = 0644, 7181 .proc_handler = proc_dointvec, 7182 }, 7183 { 7184 .procname = "accept_ra_mtu", 7185 .data = &ipv6_devconf.accept_ra_mtu, 7186 .maxlen = sizeof(int), 7187 .mode = 0644, 7188 .proc_handler = proc_dointvec, 7189 }, 7190 { 7191 .procname = "stable_secret", 7192 .data = &ipv6_devconf.stable_secret, 7193 .maxlen = IPV6_MAX_STRLEN, 7194 .mode = 0600, 7195 .proc_handler = addrconf_sysctl_stable_secret, 7196 }, 7197 { 7198 .procname = "use_oif_addrs_only", 7199 .data = &ipv6_devconf.use_oif_addrs_only, 7200 .maxlen = sizeof(int), 7201 .mode = 0644, 7202 .proc_handler = proc_dointvec, 7203 }, 7204 { 7205 .procname = "ignore_routes_with_linkdown", 7206 .data = &ipv6_devconf.ignore_routes_with_linkdown, 7207 .maxlen = sizeof(int), 7208 .mode = 0644, 7209 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, 7210 }, 7211 { 7212 .procname = "drop_unicast_in_l2_multicast", 7213 .data = &ipv6_devconf.drop_unicast_in_l2_multicast, 7214 .maxlen = sizeof(int), 7215 .mode = 0644, 7216 .proc_handler = proc_dointvec, 7217 }, 7218 { 7219 .procname = "drop_unsolicited_na", 7220 .data = &ipv6_devconf.drop_unsolicited_na, 7221 .maxlen = sizeof(int), 7222 .mode = 0644, 7223 .proc_handler = proc_dointvec, 7224 }, 7225 { 7226 .procname = "keep_addr_on_down", 7227 .data = &ipv6_devconf.keep_addr_on_down, 7228 .maxlen = sizeof(int), 7229 .mode = 0644, 7230 .proc_handler = proc_dointvec, 7231 7232 }, 7233 { 7234 .procname = "seg6_enabled", 7235 .data = &ipv6_devconf.seg6_enabled, 7236 .maxlen = sizeof(int), 7237 .mode = 0644, 7238 .proc_handler = proc_dointvec, 7239 }, 7240 #ifdef CONFIG_IPV6_SEG6_HMAC 7241 { 7242 .procname = "seg6_require_hmac", 7243 .data = &ipv6_devconf.seg6_require_hmac, 7244 .maxlen = sizeof(int), 7245 .mode = 0644, 7246 .proc_handler = proc_dointvec, 7247 }, 7248 #endif 7249 { 7250 .procname = "enhanced_dad", 7251 .data = &ipv6_devconf.enhanced_dad, 7252 .maxlen = sizeof(int), 7253 .mode = 0644, 7254 .proc_handler = proc_dointvec, 7255 }, 7256 { 7257 .procname = "addr_gen_mode", 7258 .data = &ipv6_devconf.addr_gen_mode, 7259 .maxlen = sizeof(int), 7260 .mode = 0644, 7261 .proc_handler = addrconf_sysctl_addr_gen_mode, 7262 }, 7263 { 7264 .procname = "disable_policy", 7265 .data = &ipv6_devconf.disable_policy, 7266 .maxlen = sizeof(int), 7267 .mode = 0644, 7268 .proc_handler = addrconf_sysctl_disable_policy, 7269 }, 7270 { 7271 .procname = "ndisc_tclass", 7272 .data = &ipv6_devconf.ndisc_tclass, 7273 .maxlen = sizeof(int), 7274 .mode = 0644, 7275 .proc_handler = proc_dointvec_minmax, 7276 .extra1 = (void *)SYSCTL_ZERO, 7277 .extra2 = (void *)&two_five_five, 7278 }, 7279 { 7280 .procname = "rpl_seg_enabled", 7281 .data = &ipv6_devconf.rpl_seg_enabled, 7282 .maxlen = sizeof(int), 7283 .mode = 0644, 7284 .proc_handler = proc_dointvec_minmax, 7285 .extra1 = SYSCTL_ZERO, 7286 .extra2 = SYSCTL_ONE, 7287 }, 7288 { 7289 .procname = "ioam6_enabled", 7290 .data = &ipv6_devconf.ioam6_enabled, 7291 .maxlen = sizeof(u8), 7292 .mode = 0644, 7293 .proc_handler = proc_dou8vec_minmax, 7294 .extra1 = (void *)SYSCTL_ZERO, 7295 .extra2 = (void *)SYSCTL_ONE, 7296 }, 7297 { 7298 .procname = "ioam6_id", 7299 .data = &ipv6_devconf.ioam6_id, 7300 .maxlen = sizeof(u32), 7301 .mode = 0644, 7302 .proc_handler = proc_douintvec_minmax, 7303 .extra1 = (void *)SYSCTL_ZERO, 7304 .extra2 = (void *)&ioam6_if_id_max, 7305 }, 7306 { 7307 .procname = "ioam6_id_wide", 7308 .data = &ipv6_devconf.ioam6_id_wide, 7309 .maxlen = sizeof(u32), 7310 .mode = 0644, 7311 .proc_handler = proc_douintvec, 7312 }, 7313 { 7314 .procname = "ndisc_evict_nocarrier", 7315 .data = &ipv6_devconf.ndisc_evict_nocarrier, 7316 .maxlen = sizeof(u8), 7317 .mode = 0644, 7318 .proc_handler = proc_dou8vec_minmax, 7319 .extra1 = (void *)SYSCTL_ZERO, 7320 .extra2 = (void *)SYSCTL_ONE, 7321 }, 7322 { 7323 .procname = "accept_untracked_na", 7324 .data = &ipv6_devconf.accept_untracked_na, 7325 .maxlen = sizeof(int), 7326 .mode = 0644, 7327 .proc_handler = proc_dointvec_minmax, 7328 .extra1 = SYSCTL_ZERO, 7329 .extra2 = SYSCTL_TWO, 7330 }, 7331 { 7332 .procname = "force_forwarding", 7333 .data = &ipv6_devconf.force_forwarding, 7334 .maxlen = sizeof(int), 7335 .mode = 0644, 7336 .proc_handler = addrconf_sysctl_force_forwarding, 7337 }, 7338 }; 7339 7340 static int __addrconf_sysctl_register(struct net *net, char *dev_name, 7341 struct inet6_dev *idev, struct ipv6_devconf *p) 7342 { 7343 size_t table_size = ARRAY_SIZE(addrconf_sysctl); 7344 int i, ifindex; 7345 struct ctl_table *table; 7346 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 7347 7348 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL_ACCOUNT); 7349 if (!table) 7350 goto out; 7351 7352 for (i = 0; i < table_size; i++) { 7353 table[i].data += (char *)p - (char *)&ipv6_devconf; 7354 /* If one of these is already set, then it is not safe to 7355 * overwrite either of them: this makes proc_dointvec_minmax 7356 * usable. 7357 */ 7358 if (!table[i].extra1 && !table[i].extra2) { 7359 table[i].extra1 = idev; /* embedded; no ref */ 7360 table[i].extra2 = net; 7361 } 7362 } 7363 7364 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 7365 7366 p->sysctl_header = register_net_sysctl_sz(net, path, table, 7367 table_size); 7368 if (!p->sysctl_header) 7369 goto free; 7370 7371 if (!strcmp(dev_name, "all")) 7372 ifindex = NETCONFA_IFINDEX_ALL; 7373 else if (!strcmp(dev_name, "default")) 7374 ifindex = NETCONFA_IFINDEX_DEFAULT; 7375 else 7376 ifindex = idev->dev->ifindex; 7377 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, 7378 ifindex, p); 7379 return 0; 7380 7381 free: 7382 kfree(table); 7383 out: 7384 return -ENOBUFS; 7385 } 7386 7387 static void __addrconf_sysctl_unregister(struct net *net, 7388 struct ipv6_devconf *p, int ifindex) 7389 { 7390 const struct ctl_table *table; 7391 7392 if (!p->sysctl_header) 7393 return; 7394 7395 table = p->sysctl_header->ctl_table_arg; 7396 unregister_net_sysctl_table(p->sysctl_header); 7397 p->sysctl_header = NULL; 7398 kfree(table); 7399 7400 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL); 7401 } 7402 7403 static int addrconf_sysctl_register(struct inet6_dev *idev) 7404 { 7405 int err; 7406 7407 if (!sysctl_dev_name_is_allowed(idev->dev->name)) 7408 return -EINVAL; 7409 7410 err = neigh_sysctl_register(idev->dev, idev->nd_parms, 7411 &ndisc_ifinfo_sysctl_change); 7412 if (err) 7413 return err; 7414 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 7415 idev, &idev->cnf); 7416 if (err) 7417 neigh_sysctl_unregister(idev->nd_parms); 7418 7419 return err; 7420 } 7421 7422 static void addrconf_sysctl_unregister(struct inet6_dev *idev) 7423 { 7424 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf, 7425 idev->dev->ifindex); 7426 neigh_sysctl_unregister(idev->nd_parms); 7427 } 7428 7429 7430 #endif 7431 7432 static int __net_init addrconf_init_net(struct net *net) 7433 { 7434 int err = -ENOMEM; 7435 struct ipv6_devconf *all, *dflt; 7436 7437 spin_lock_init(&net->ipv6.addrconf_hash_lock); 7438 INIT_DEFERRABLE_WORK(&net->ipv6.addr_chk_work, addrconf_verify_work); 7439 net->ipv6.inet6_addr_lst = kzalloc_objs(struct hlist_head, 7440 IN6_ADDR_HSIZE); 7441 if (!net->ipv6.inet6_addr_lst) 7442 goto err_alloc_addr; 7443 7444 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 7445 if (!all) 7446 goto err_alloc_all; 7447 7448 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 7449 if (!dflt) 7450 goto err_alloc_dflt; 7451 7452 if (!net_eq(net, &init_net)) { 7453 switch (net_inherit_devconf()) { 7454 case 1: /* copy from init_net */ 7455 memcpy(all, init_net.ipv6.devconf_all, 7456 sizeof(ipv6_devconf)); 7457 memcpy(dflt, init_net.ipv6.devconf_dflt, 7458 sizeof(ipv6_devconf_dflt)); 7459 break; 7460 case 3: /* copy from the current netns */ 7461 memcpy(all, current->nsproxy->net_ns->ipv6.devconf_all, 7462 sizeof(ipv6_devconf)); 7463 memcpy(dflt, 7464 current->nsproxy->net_ns->ipv6.devconf_dflt, 7465 sizeof(ipv6_devconf_dflt)); 7466 break; 7467 case 0: 7468 case 2: 7469 /* use compiled values */ 7470 break; 7471 } 7472 } 7473 7474 /* these will be inherited by all namespaces */ 7475 dflt->autoconf = ipv6_defaults.autoconf; 7476 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 7477 7478 dflt->stable_secret.initialized = false; 7479 all->stable_secret.initialized = false; 7480 7481 net->ipv6.devconf_all = all; 7482 net->ipv6.devconf_dflt = dflt; 7483 7484 #ifdef CONFIG_SYSCTL 7485 err = __addrconf_sysctl_register(net, "all", NULL, all); 7486 if (err < 0) 7487 goto err_reg_all; 7488 7489 err = __addrconf_sysctl_register(net, "default", NULL, dflt); 7490 if (err < 0) 7491 goto err_reg_dflt; 7492 #endif 7493 return 0; 7494 7495 #ifdef CONFIG_SYSCTL 7496 err_reg_dflt: 7497 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL); 7498 err_reg_all: 7499 kfree(dflt); 7500 net->ipv6.devconf_dflt = NULL; 7501 #endif 7502 err_alloc_dflt: 7503 kfree(all); 7504 net->ipv6.devconf_all = NULL; 7505 err_alloc_all: 7506 kfree(net->ipv6.inet6_addr_lst); 7507 err_alloc_addr: 7508 return err; 7509 } 7510 7511 static void __net_exit addrconf_exit_net(struct net *net) 7512 { 7513 int i; 7514 7515 #ifdef CONFIG_SYSCTL 7516 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt, 7517 NETCONFA_IFINDEX_DEFAULT); 7518 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all, 7519 NETCONFA_IFINDEX_ALL); 7520 #endif 7521 kfree(net->ipv6.devconf_dflt); 7522 net->ipv6.devconf_dflt = NULL; 7523 kfree(net->ipv6.devconf_all); 7524 net->ipv6.devconf_all = NULL; 7525 7526 cancel_delayed_work_sync(&net->ipv6.addr_chk_work); 7527 /* 7528 * Check hash table, then free it. 7529 */ 7530 for (i = 0; i < IN6_ADDR_HSIZE; i++) 7531 WARN_ON_ONCE(!hlist_empty(&net->ipv6.inet6_addr_lst[i])); 7532 7533 kfree(net->ipv6.inet6_addr_lst); 7534 net->ipv6.inet6_addr_lst = NULL; 7535 } 7536 7537 static struct pernet_operations addrconf_ops = { 7538 .init = addrconf_init_net, 7539 .exit = addrconf_exit_net, 7540 }; 7541 7542 static struct rtnl_af_ops inet6_ops __read_mostly = { 7543 .family = AF_INET6, 7544 .fill_link_af = inet6_fill_link_af, 7545 .get_link_af_size = inet6_get_link_af_size, 7546 .validate_link_af = inet6_validate_link_af, 7547 .set_link_af = inet6_set_link_af, 7548 }; 7549 7550 static const struct rtnl_msg_handler addrconf_rtnl_msg_handlers[] __initconst_or_module = { 7551 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETLINK, 7552 .dumpit = inet6_dump_ifinfo, .flags = RTNL_FLAG_DUMP_UNLOCKED}, 7553 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_NEWADDR, 7554 .doit = inet6_rtm_newaddr, .flags = RTNL_FLAG_DOIT_PERNET}, 7555 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_DELADDR, 7556 .doit = inet6_rtm_deladdr, .flags = RTNL_FLAG_DOIT_PERNET}, 7557 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETADDR, 7558 .doit = inet6_rtm_getaddr, .dumpit = inet6_dump_ifaddr, 7559 .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED}, 7560 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETMULTICAST, 7561 .dumpit = inet6_dump_ifmcaddr, 7562 .flags = RTNL_FLAG_DUMP_UNLOCKED}, 7563 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETANYCAST, 7564 .dumpit = inet6_dump_ifacaddr, 7565 .flags = RTNL_FLAG_DUMP_UNLOCKED}, 7566 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETNETCONF, 7567 .doit = inet6_netconf_get_devconf, .dumpit = inet6_netconf_dump_devconf, 7568 .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED}, 7569 }; 7570 7571 /* 7572 * Init / cleanup code 7573 */ 7574 7575 int __init addrconf_init(void) 7576 { 7577 struct inet6_dev *idev; 7578 int err; 7579 7580 err = ipv6_addr_label_init(); 7581 if (err < 0) { 7582 pr_crit("%s: cannot initialize default policy table: %d\n", 7583 __func__, err); 7584 goto out; 7585 } 7586 7587 err = register_pernet_subsys(&addrconf_ops); 7588 if (err < 0) 7589 goto out_addrlabel; 7590 7591 /* All works using addrconf_wq need to lock rtnl. */ 7592 addrconf_wq = create_singlethread_workqueue("ipv6_addrconf"); 7593 if (!addrconf_wq) { 7594 err = -ENOMEM; 7595 goto out_nowq; 7596 } 7597 7598 rtnl_net_lock(&init_net); 7599 idev = ipv6_add_dev(blackhole_netdev); 7600 rtnl_net_unlock(&init_net); 7601 if (IS_ERR(idev)) { 7602 err = PTR_ERR(idev); 7603 goto errlo; 7604 } 7605 7606 ip6_route_init_special_entries(); 7607 7608 register_netdevice_notifier(&ipv6_dev_notf); 7609 7610 addrconf_verify(&init_net); 7611 7612 err = rtnl_af_register(&inet6_ops); 7613 if (err) 7614 goto erraf; 7615 7616 err = rtnl_register_many(addrconf_rtnl_msg_handlers); 7617 if (err) 7618 goto errout; 7619 7620 err = ipv6_addr_label_rtnl_register(); 7621 if (err < 0) 7622 goto errout; 7623 7624 return 0; 7625 errout: 7626 rtnl_unregister_all(PF_INET6); 7627 rtnl_af_unregister(&inet6_ops); 7628 erraf: 7629 unregister_netdevice_notifier(&ipv6_dev_notf); 7630 errlo: 7631 destroy_workqueue(addrconf_wq); 7632 out_nowq: 7633 unregister_pernet_subsys(&addrconf_ops); 7634 out_addrlabel: 7635 ipv6_addr_label_cleanup(); 7636 out: 7637 return err; 7638 } 7639 7640 void addrconf_cleanup(void) 7641 { 7642 struct net_device *dev; 7643 7644 unregister_netdevice_notifier(&ipv6_dev_notf); 7645 unregister_pernet_subsys(&addrconf_ops); 7646 ipv6_addr_label_cleanup(); 7647 7648 rtnl_af_unregister(&inet6_ops); 7649 7650 rtnl_net_lock(&init_net); 7651 7652 /* clean dev list */ 7653 for_each_netdev(&init_net, dev) { 7654 if (!__in6_dev_get_rtnl_net(dev)) 7655 continue; 7656 addrconf_ifdown(dev, true); 7657 } 7658 addrconf_ifdown(init_net.loopback_dev, true); 7659 7660 rtnl_net_unlock(&init_net); 7661 7662 destroy_workqueue(addrconf_wq); 7663 } 7664