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