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