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