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