1 /* 2 * IPv6 Address [auto]configuration 3 * Linux INET6 implementation 4 * 5 * Authors: 6 * Pedro Roque <roque@di.fc.ul.pt> 7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 8 * 9 * This program is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU General Public License 11 * as published by the Free Software Foundation; either version 12 * 2 of the License, or (at your option) any later version. 13 */ 14 15 /* 16 * Changes: 17 * 18 * Janos Farkas : delete timer on ifdown 19 * <chexum@bankinf.banki.hu> 20 * Andi Kleen : kill double kfree on module 21 * unload. 22 * Maciej W. Rozycki : FDDI support 23 * sekiya@USAGI : Don't send too many RS 24 * packets. 25 * yoshfuji@USAGI : Fixed interval between DAD 26 * packets. 27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of 28 * address validation timer. 29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041) 30 * support. 31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6 32 * address on a same interface. 33 * YOSHIFUJI Hideaki @USAGI : ARCnet support 34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to 35 * seq_file. 36 * YOSHIFUJI Hideaki @USAGI : improved source address 37 * selection; consider scope, 38 * status etc. 39 */ 40 41 #define pr_fmt(fmt) "IPv6: " fmt 42 43 #include <linux/errno.h> 44 #include <linux/types.h> 45 #include <linux/kernel.h> 46 #include <linux/socket.h> 47 #include <linux/sockios.h> 48 #include <linux/net.h> 49 #include <linux/inet.h> 50 #include <linux/in6.h> 51 #include <linux/netdevice.h> 52 #include <linux/if_addr.h> 53 #include <linux/if_arp.h> 54 #include <linux/if_arcnet.h> 55 #include <linux/if_infiniband.h> 56 #include <linux/route.h> 57 #include <linux/inetdevice.h> 58 #include <linux/init.h> 59 #include <linux/slab.h> 60 #ifdef CONFIG_SYSCTL 61 #include <linux/sysctl.h> 62 #endif 63 #include <linux/capability.h> 64 #include <linux/delay.h> 65 #include <linux/notifier.h> 66 #include <linux/string.h> 67 #include <linux/hash.h> 68 69 #include <net/net_namespace.h> 70 #include <net/sock.h> 71 #include <net/snmp.h> 72 73 #include <net/af_ieee802154.h> 74 #include <net/firewire.h> 75 #include <net/ipv6.h> 76 #include <net/protocol.h> 77 #include <net/ndisc.h> 78 #include <net/ip6_route.h> 79 #include <net/addrconf.h> 80 #include <net/tcp.h> 81 #include <net/ip.h> 82 #include <net/netlink.h> 83 #include <net/pkt_sched.h> 84 #include <linux/if_tunnel.h> 85 #include <linux/rtnetlink.h> 86 #include <linux/netconf.h> 87 #include <linux/random.h> 88 #include <linux/uaccess.h> 89 #include <asm/unaligned.h> 90 91 #include <linux/proc_fs.h> 92 #include <linux/seq_file.h> 93 #include <linux/export.h> 94 95 /* Set to 3 to get tracing... */ 96 #define ACONF_DEBUG 2 97 98 #if ACONF_DEBUG >= 3 99 #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__) 100 #else 101 #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0) 102 #endif 103 104 #define INFINITY_LIFE_TIME 0xFFFFFFFF 105 106 #define IPV6_MAX_STRLEN \ 107 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") 108 109 static inline u32 cstamp_delta(unsigned long cstamp) 110 { 111 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; 112 } 113 114 #ifdef CONFIG_SYSCTL 115 static int addrconf_sysctl_register(struct inet6_dev *idev); 116 static void addrconf_sysctl_unregister(struct inet6_dev *idev); 117 #else 118 static inline int addrconf_sysctl_register(struct inet6_dev *idev) 119 { 120 return 0; 121 } 122 123 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) 124 { 125 } 126 #endif 127 128 static void __ipv6_regen_rndid(struct inet6_dev *idev); 129 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 130 static void ipv6_regen_rndid(unsigned long data); 131 132 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 133 static int ipv6_count_addresses(struct inet6_dev *idev); 134 static int ipv6_generate_stable_address(struct in6_addr *addr, 135 u8 dad_count, 136 const struct inet6_dev *idev); 137 138 /* 139 * Configured unicast address hash table 140 */ 141 static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE]; 142 static DEFINE_SPINLOCK(addrconf_hash_lock); 143 144 static void addrconf_verify(void); 145 static void addrconf_verify_rtnl(void); 146 static void addrconf_verify_work(struct work_struct *); 147 148 static struct workqueue_struct *addrconf_wq; 149 static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work); 150 151 static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 152 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 153 154 static void addrconf_type_change(struct net_device *dev, 155 unsigned long event); 156 static int addrconf_ifdown(struct net_device *dev, int how); 157 158 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 159 int plen, 160 const struct net_device *dev, 161 u32 flags, u32 noflags); 162 163 static void addrconf_dad_start(struct inet6_ifaddr *ifp); 164 static void addrconf_dad_work(struct work_struct *w); 165 static void addrconf_dad_completed(struct inet6_ifaddr *ifp); 166 static void addrconf_dad_run(struct inet6_dev *idev); 167 static void addrconf_rs_timer(unsigned long data); 168 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 169 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 170 171 static void inet6_prefix_notify(int event, struct inet6_dev *idev, 172 struct prefix_info *pinfo); 173 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 174 struct net_device *dev); 175 176 static struct ipv6_devconf ipv6_devconf __read_mostly = { 177 .forwarding = 0, 178 .hop_limit = IPV6_DEFAULT_HOPLIMIT, 179 .mtu6 = IPV6_MIN_MTU, 180 .accept_ra = 1, 181 .accept_redirects = 1, 182 .autoconf = 1, 183 .force_mld_version = 0, 184 .mldv1_unsolicited_report_interval = 10 * HZ, 185 .mldv2_unsolicited_report_interval = HZ, 186 .dad_transmits = 1, 187 .rtr_solicits = MAX_RTR_SOLICITATIONS, 188 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 189 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 190 .use_tempaddr = 0, 191 .temp_valid_lft = TEMP_VALID_LIFETIME, 192 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 193 .regen_max_retry = REGEN_MAX_RETRY, 194 .max_desync_factor = MAX_DESYNC_FACTOR, 195 .max_addresses = IPV6_MAX_ADDRESSES, 196 .accept_ra_defrtr = 1, 197 .accept_ra_from_local = 0, 198 .accept_ra_pinfo = 1, 199 #ifdef CONFIG_IPV6_ROUTER_PREF 200 .accept_ra_rtr_pref = 1, 201 .rtr_probe_interval = 60 * HZ, 202 #ifdef CONFIG_IPV6_ROUTE_INFO 203 .accept_ra_rt_info_max_plen = 0, 204 #endif 205 #endif 206 .proxy_ndp = 0, 207 .accept_source_route = 0, /* we do not accept RH0 by default. */ 208 .disable_ipv6 = 0, 209 .accept_dad = 1, 210 .suppress_frag_ndisc = 1, 211 .accept_ra_mtu = 1, 212 .stable_secret = { 213 .initialized = false, 214 } 215 }; 216 217 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 218 .forwarding = 0, 219 .hop_limit = IPV6_DEFAULT_HOPLIMIT, 220 .mtu6 = IPV6_MIN_MTU, 221 .accept_ra = 1, 222 .accept_redirects = 1, 223 .autoconf = 1, 224 .force_mld_version = 0, 225 .mldv1_unsolicited_report_interval = 10 * HZ, 226 .mldv2_unsolicited_report_interval = HZ, 227 .dad_transmits = 1, 228 .rtr_solicits = MAX_RTR_SOLICITATIONS, 229 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 230 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 231 .use_tempaddr = 0, 232 .temp_valid_lft = TEMP_VALID_LIFETIME, 233 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 234 .regen_max_retry = REGEN_MAX_RETRY, 235 .max_desync_factor = MAX_DESYNC_FACTOR, 236 .max_addresses = IPV6_MAX_ADDRESSES, 237 .accept_ra_defrtr = 1, 238 .accept_ra_from_local = 0, 239 .accept_ra_pinfo = 1, 240 #ifdef CONFIG_IPV6_ROUTER_PREF 241 .accept_ra_rtr_pref = 1, 242 .rtr_probe_interval = 60 * HZ, 243 #ifdef CONFIG_IPV6_ROUTE_INFO 244 .accept_ra_rt_info_max_plen = 0, 245 #endif 246 #endif 247 .proxy_ndp = 0, 248 .accept_source_route = 0, /* we do not accept RH0 by default. */ 249 .disable_ipv6 = 0, 250 .accept_dad = 1, 251 .suppress_frag_ndisc = 1, 252 .accept_ra_mtu = 1, 253 .stable_secret = { 254 .initialized = false, 255 }, 256 }; 257 258 /* Check if a valid qdisc is available */ 259 static inline bool addrconf_qdisc_ok(const struct net_device *dev) 260 { 261 return !qdisc_tx_is_noop(dev); 262 } 263 264 static void addrconf_del_rs_timer(struct inet6_dev *idev) 265 { 266 if (del_timer(&idev->rs_timer)) 267 __in6_dev_put(idev); 268 } 269 270 static void addrconf_del_dad_work(struct inet6_ifaddr *ifp) 271 { 272 if (cancel_delayed_work(&ifp->dad_work)) 273 __in6_ifa_put(ifp); 274 } 275 276 static void addrconf_mod_rs_timer(struct inet6_dev *idev, 277 unsigned long when) 278 { 279 if (!timer_pending(&idev->rs_timer)) 280 in6_dev_hold(idev); 281 mod_timer(&idev->rs_timer, jiffies + when); 282 } 283 284 static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp, 285 unsigned long delay) 286 { 287 if (!delayed_work_pending(&ifp->dad_work)) 288 in6_ifa_hold(ifp); 289 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay); 290 } 291 292 static int snmp6_alloc_dev(struct inet6_dev *idev) 293 { 294 int i; 295 296 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib); 297 if (!idev->stats.ipv6) 298 goto err_ip; 299 300 for_each_possible_cpu(i) { 301 struct ipstats_mib *addrconf_stats; 302 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i); 303 u64_stats_init(&addrconf_stats->syncp); 304 } 305 306 307 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device), 308 GFP_KERNEL); 309 if (!idev->stats.icmpv6dev) 310 goto err_icmp; 311 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device), 312 GFP_KERNEL); 313 if (!idev->stats.icmpv6msgdev) 314 goto err_icmpmsg; 315 316 return 0; 317 318 err_icmpmsg: 319 kfree(idev->stats.icmpv6dev); 320 err_icmp: 321 free_percpu(idev->stats.ipv6); 322 err_ip: 323 return -ENOMEM; 324 } 325 326 static struct inet6_dev *ipv6_add_dev(struct net_device *dev) 327 { 328 struct inet6_dev *ndev; 329 int err = -ENOMEM; 330 331 ASSERT_RTNL(); 332 333 if (dev->mtu < IPV6_MIN_MTU) 334 return ERR_PTR(-EINVAL); 335 336 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 337 if (!ndev) 338 return ERR_PTR(err); 339 340 rwlock_init(&ndev->lock); 341 ndev->dev = dev; 342 INIT_LIST_HEAD(&ndev->addr_list); 343 setup_timer(&ndev->rs_timer, addrconf_rs_timer, 344 (unsigned long)ndev); 345 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 346 ndev->cnf.mtu6 = dev->mtu; 347 ndev->cnf.sysctl = NULL; 348 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 349 if (!ndev->nd_parms) { 350 kfree(ndev); 351 return ERR_PTR(err); 352 } 353 if (ndev->cnf.forwarding) 354 dev_disable_lro(dev); 355 /* We refer to the device */ 356 dev_hold(dev); 357 358 if (snmp6_alloc_dev(ndev) < 0) { 359 ADBG(KERN_WARNING 360 "%s: cannot allocate memory for statistics; dev=%s.\n", 361 __func__, dev->name); 362 neigh_parms_release(&nd_tbl, ndev->nd_parms); 363 dev_put(dev); 364 kfree(ndev); 365 return ERR_PTR(err); 366 } 367 368 if (snmp6_register_dev(ndev) < 0) { 369 ADBG(KERN_WARNING 370 "%s: cannot create /proc/net/dev_snmp6/%s\n", 371 __func__, dev->name); 372 goto err_release; 373 } 374 375 /* One reference from device. We must do this before 376 * we invoke __ipv6_regen_rndid(). 377 */ 378 in6_dev_hold(ndev); 379 380 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 381 ndev->cnf.accept_dad = -1; 382 383 #if IS_ENABLED(CONFIG_IPV6_SIT) 384 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 385 pr_info("%s: Disabled Multicast RS\n", dev->name); 386 ndev->cnf.rtr_solicits = 0; 387 } 388 #endif 389 390 INIT_LIST_HEAD(&ndev->tempaddr_list); 391 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev); 392 if ((dev->flags&IFF_LOOPBACK) || 393 dev->type == ARPHRD_TUNNEL || 394 dev->type == ARPHRD_TUNNEL6 || 395 dev->type == ARPHRD_SIT || 396 dev->type == ARPHRD_NONE) { 397 ndev->cnf.use_tempaddr = -1; 398 } else { 399 in6_dev_hold(ndev); 400 ipv6_regen_rndid((unsigned long) ndev); 401 } 402 403 ndev->token = in6addr_any; 404 405 if (netif_running(dev) && addrconf_qdisc_ok(dev)) 406 ndev->if_flags |= IF_READY; 407 408 ipv6_mc_init_dev(ndev); 409 ndev->tstamp = jiffies; 410 err = addrconf_sysctl_register(ndev); 411 if (err) { 412 ipv6_mc_destroy_dev(ndev); 413 del_timer(&ndev->regen_timer); 414 goto err_release; 415 } 416 /* protected by rtnl_lock */ 417 rcu_assign_pointer(dev->ip6_ptr, ndev); 418 419 /* Join interface-local all-node multicast group */ 420 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes); 421 422 /* Join all-node multicast group */ 423 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 424 425 /* Join all-router multicast group if forwarding is set */ 426 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST)) 427 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 428 429 return ndev; 430 431 err_release: 432 neigh_parms_release(&nd_tbl, ndev->nd_parms); 433 ndev->dead = 1; 434 in6_dev_finish_destroy(ndev); 435 return ERR_PTR(err); 436 } 437 438 static struct inet6_dev *ipv6_find_idev(struct net_device *dev) 439 { 440 struct inet6_dev *idev; 441 442 ASSERT_RTNL(); 443 444 idev = __in6_dev_get(dev); 445 if (!idev) { 446 idev = ipv6_add_dev(dev); 447 if (IS_ERR(idev)) 448 return NULL; 449 } 450 451 if (dev->flags&IFF_UP) 452 ipv6_mc_up(idev); 453 return idev; 454 } 455 456 static int inet6_netconf_msgsize_devconf(int type) 457 { 458 int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) 459 + nla_total_size(4); /* NETCONFA_IFINDEX */ 460 461 /* type -1 is used for ALL */ 462 if (type == -1 || type == NETCONFA_FORWARDING) 463 size += nla_total_size(4); 464 #ifdef CONFIG_IPV6_MROUTE 465 if (type == -1 || type == NETCONFA_MC_FORWARDING) 466 size += nla_total_size(4); 467 #endif 468 if (type == -1 || type == NETCONFA_PROXY_NEIGH) 469 size += nla_total_size(4); 470 471 return size; 472 } 473 474 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex, 475 struct ipv6_devconf *devconf, u32 portid, 476 u32 seq, int event, unsigned int flags, 477 int type) 478 { 479 struct nlmsghdr *nlh; 480 struct netconfmsg *ncm; 481 482 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), 483 flags); 484 if (!nlh) 485 return -EMSGSIZE; 486 487 ncm = nlmsg_data(nlh); 488 ncm->ncm_family = AF_INET6; 489 490 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) 491 goto nla_put_failure; 492 493 /* type -1 is used for ALL */ 494 if ((type == -1 || type == NETCONFA_FORWARDING) && 495 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0) 496 goto nla_put_failure; 497 #ifdef CONFIG_IPV6_MROUTE 498 if ((type == -1 || type == NETCONFA_MC_FORWARDING) && 499 nla_put_s32(skb, NETCONFA_MC_FORWARDING, 500 devconf->mc_forwarding) < 0) 501 goto nla_put_failure; 502 #endif 503 if ((type == -1 || type == NETCONFA_PROXY_NEIGH) && 504 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0) 505 goto nla_put_failure; 506 507 nlmsg_end(skb, nlh); 508 return 0; 509 510 nla_put_failure: 511 nlmsg_cancel(skb, nlh); 512 return -EMSGSIZE; 513 } 514 515 void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex, 516 struct ipv6_devconf *devconf) 517 { 518 struct sk_buff *skb; 519 int err = -ENOBUFS; 520 521 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC); 522 if (!skb) 523 goto errout; 524 525 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, 526 RTM_NEWNETCONF, 0, type); 527 if (err < 0) { 528 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 529 WARN_ON(err == -EMSGSIZE); 530 kfree_skb(skb); 531 goto errout; 532 } 533 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC); 534 return; 535 errout: 536 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err); 537 } 538 539 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = { 540 [NETCONFA_IFINDEX] = { .len = sizeof(int) }, 541 [NETCONFA_FORWARDING] = { .len = sizeof(int) }, 542 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) }, 543 }; 544 545 static int inet6_netconf_get_devconf(struct sk_buff *in_skb, 546 struct nlmsghdr *nlh) 547 { 548 struct net *net = sock_net(in_skb->sk); 549 struct nlattr *tb[NETCONFA_MAX+1]; 550 struct netconfmsg *ncm; 551 struct sk_buff *skb; 552 struct ipv6_devconf *devconf; 553 struct inet6_dev *in6_dev; 554 struct net_device *dev; 555 int ifindex; 556 int err; 557 558 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX, 559 devconf_ipv6_policy); 560 if (err < 0) 561 goto errout; 562 563 err = EINVAL; 564 if (!tb[NETCONFA_IFINDEX]) 565 goto errout; 566 567 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); 568 switch (ifindex) { 569 case NETCONFA_IFINDEX_ALL: 570 devconf = net->ipv6.devconf_all; 571 break; 572 case NETCONFA_IFINDEX_DEFAULT: 573 devconf = net->ipv6.devconf_dflt; 574 break; 575 default: 576 dev = __dev_get_by_index(net, ifindex); 577 if (!dev) 578 goto errout; 579 in6_dev = __in6_dev_get(dev); 580 if (!in6_dev) 581 goto errout; 582 devconf = &in6_dev->cnf; 583 break; 584 } 585 586 err = -ENOBUFS; 587 skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC); 588 if (!skb) 589 goto errout; 590 591 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 592 NETLINK_CB(in_skb).portid, 593 nlh->nlmsg_seq, RTM_NEWNETCONF, 0, 594 -1); 595 if (err < 0) { 596 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 597 WARN_ON(err == -EMSGSIZE); 598 kfree_skb(skb); 599 goto errout; 600 } 601 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 602 errout: 603 return err; 604 } 605 606 static int inet6_netconf_dump_devconf(struct sk_buff *skb, 607 struct netlink_callback *cb) 608 { 609 struct net *net = sock_net(skb->sk); 610 int h, s_h; 611 int idx, s_idx; 612 struct net_device *dev; 613 struct inet6_dev *idev; 614 struct hlist_head *head; 615 616 s_h = cb->args[0]; 617 s_idx = idx = cb->args[1]; 618 619 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 620 idx = 0; 621 head = &net->dev_index_head[h]; 622 rcu_read_lock(); 623 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ 624 net->dev_base_seq; 625 hlist_for_each_entry_rcu(dev, head, index_hlist) { 626 if (idx < s_idx) 627 goto cont; 628 idev = __in6_dev_get(dev); 629 if (!idev) 630 goto cont; 631 632 if (inet6_netconf_fill_devconf(skb, dev->ifindex, 633 &idev->cnf, 634 NETLINK_CB(cb->skb).portid, 635 cb->nlh->nlmsg_seq, 636 RTM_NEWNETCONF, 637 NLM_F_MULTI, 638 -1) < 0) { 639 rcu_read_unlock(); 640 goto done; 641 } 642 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 643 cont: 644 idx++; 645 } 646 rcu_read_unlock(); 647 } 648 if (h == NETDEV_HASHENTRIES) { 649 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, 650 net->ipv6.devconf_all, 651 NETLINK_CB(cb->skb).portid, 652 cb->nlh->nlmsg_seq, 653 RTM_NEWNETCONF, NLM_F_MULTI, 654 -1) < 0) 655 goto done; 656 else 657 h++; 658 } 659 if (h == NETDEV_HASHENTRIES + 1) { 660 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, 661 net->ipv6.devconf_dflt, 662 NETLINK_CB(cb->skb).portid, 663 cb->nlh->nlmsg_seq, 664 RTM_NEWNETCONF, NLM_F_MULTI, 665 -1) < 0) 666 goto done; 667 else 668 h++; 669 } 670 done: 671 cb->args[0] = h; 672 cb->args[1] = idx; 673 674 return skb->len; 675 } 676 677 #ifdef CONFIG_SYSCTL 678 static void dev_forward_change(struct inet6_dev *idev) 679 { 680 struct net_device *dev; 681 struct inet6_ifaddr *ifa; 682 683 if (!idev) 684 return; 685 dev = idev->dev; 686 if (idev->cnf.forwarding) 687 dev_disable_lro(dev); 688 if (dev->flags & IFF_MULTICAST) { 689 if (idev->cnf.forwarding) { 690 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 691 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters); 692 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters); 693 } else { 694 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 695 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters); 696 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters); 697 } 698 } 699 700 list_for_each_entry(ifa, &idev->addr_list, if_list) { 701 if (ifa->flags&IFA_F_TENTATIVE) 702 continue; 703 if (idev->cnf.forwarding) 704 addrconf_join_anycast(ifa); 705 else 706 addrconf_leave_anycast(ifa); 707 } 708 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING, 709 dev->ifindex, &idev->cnf); 710 } 711 712 713 static void addrconf_forward_change(struct net *net, __s32 newf) 714 { 715 struct net_device *dev; 716 struct inet6_dev *idev; 717 718 for_each_netdev(net, dev) { 719 idev = __in6_dev_get(dev); 720 if (idev) { 721 int changed = (!idev->cnf.forwarding) ^ (!newf); 722 idev->cnf.forwarding = newf; 723 if (changed) 724 dev_forward_change(idev); 725 } 726 } 727 } 728 729 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) 730 { 731 struct net *net; 732 int old; 733 734 if (!rtnl_trylock()) 735 return restart_syscall(); 736 737 net = (struct net *)table->extra2; 738 old = *p; 739 *p = newf; 740 741 if (p == &net->ipv6.devconf_dflt->forwarding) { 742 if ((!newf) ^ (!old)) 743 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, 744 NETCONFA_IFINDEX_DEFAULT, 745 net->ipv6.devconf_dflt); 746 rtnl_unlock(); 747 return 0; 748 } 749 750 if (p == &net->ipv6.devconf_all->forwarding) { 751 net->ipv6.devconf_dflt->forwarding = newf; 752 addrconf_forward_change(net, newf); 753 if ((!newf) ^ (!old)) 754 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, 755 NETCONFA_IFINDEX_ALL, 756 net->ipv6.devconf_all); 757 } else if ((!newf) ^ (!old)) 758 dev_forward_change((struct inet6_dev *)table->extra1); 759 rtnl_unlock(); 760 761 if (newf) 762 rt6_purge_dflt_routers(net); 763 return 1; 764 } 765 #endif 766 767 /* Nobody refers to this ifaddr, destroy it */ 768 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 769 { 770 WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 771 772 #ifdef NET_REFCNT_DEBUG 773 pr_debug("%s\n", __func__); 774 #endif 775 776 in6_dev_put(ifp->idev); 777 778 if (cancel_delayed_work(&ifp->dad_work)) 779 pr_notice("delayed DAD work was pending while freeing ifa=%p\n", 780 ifp); 781 782 if (ifp->state != INET6_IFADDR_STATE_DEAD) { 783 pr_warn("Freeing alive inet6 address %p\n", ifp); 784 return; 785 } 786 ip6_rt_put(ifp->rt); 787 788 kfree_rcu(ifp, rcu); 789 } 790 791 static void 792 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 793 { 794 struct list_head *p; 795 int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 796 797 /* 798 * Each device address list is sorted in order of scope - 799 * global before linklocal. 800 */ 801 list_for_each(p, &idev->addr_list) { 802 struct inet6_ifaddr *ifa 803 = list_entry(p, struct inet6_ifaddr, if_list); 804 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 805 break; 806 } 807 808 list_add_tail(&ifp->if_list, p); 809 } 810 811 static u32 inet6_addr_hash(const struct in6_addr *addr) 812 { 813 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT); 814 } 815 816 /* On success it returns ifp with increased reference count */ 817 818 static struct inet6_ifaddr * 819 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 820 const struct in6_addr *peer_addr, int pfxlen, 821 int scope, u32 flags, u32 valid_lft, u32 prefered_lft) 822 { 823 struct inet6_ifaddr *ifa = NULL; 824 struct rt6_info *rt; 825 unsigned int hash; 826 int err = 0; 827 int addr_type = ipv6_addr_type(addr); 828 829 if (addr_type == IPV6_ADDR_ANY || 830 addr_type & IPV6_ADDR_MULTICAST || 831 (!(idev->dev->flags & IFF_LOOPBACK) && 832 addr_type & IPV6_ADDR_LOOPBACK)) 833 return ERR_PTR(-EADDRNOTAVAIL); 834 835 rcu_read_lock_bh(); 836 if (idev->dead) { 837 err = -ENODEV; /*XXX*/ 838 goto out2; 839 } 840 841 if (idev->cnf.disable_ipv6) { 842 err = -EACCES; 843 goto out2; 844 } 845 846 spin_lock(&addrconf_hash_lock); 847 848 /* Ignore adding duplicate addresses on an interface */ 849 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) { 850 ADBG("ipv6_add_addr: already assigned\n"); 851 err = -EEXIST; 852 goto out; 853 } 854 855 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 856 857 if (!ifa) { 858 ADBG("ipv6_add_addr: malloc failed\n"); 859 err = -ENOBUFS; 860 goto out; 861 } 862 863 rt = addrconf_dst_alloc(idev, addr, false); 864 if (IS_ERR(rt)) { 865 err = PTR_ERR(rt); 866 goto out; 867 } 868 869 neigh_parms_data_state_setall(idev->nd_parms); 870 871 ifa->addr = *addr; 872 if (peer_addr) 873 ifa->peer_addr = *peer_addr; 874 875 spin_lock_init(&ifa->lock); 876 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work); 877 INIT_HLIST_NODE(&ifa->addr_lst); 878 ifa->scope = scope; 879 ifa->prefix_len = pfxlen; 880 ifa->flags = flags | IFA_F_TENTATIVE; 881 ifa->valid_lft = valid_lft; 882 ifa->prefered_lft = prefered_lft; 883 ifa->cstamp = ifa->tstamp = jiffies; 884 ifa->tokenized = false; 885 886 ifa->rt = rt; 887 888 ifa->idev = idev; 889 in6_dev_hold(idev); 890 /* For caller */ 891 in6_ifa_hold(ifa); 892 893 /* Add to big hash table */ 894 hash = inet6_addr_hash(addr); 895 896 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); 897 spin_unlock(&addrconf_hash_lock); 898 899 write_lock(&idev->lock); 900 /* Add to inet6_dev unicast addr list. */ 901 ipv6_link_dev_addr(idev, ifa); 902 903 if (ifa->flags&IFA_F_TEMPORARY) { 904 list_add(&ifa->tmp_list, &idev->tempaddr_list); 905 in6_ifa_hold(ifa); 906 } 907 908 in6_ifa_hold(ifa); 909 write_unlock(&idev->lock); 910 out2: 911 rcu_read_unlock_bh(); 912 913 if (likely(err == 0)) 914 inet6addr_notifier_call_chain(NETDEV_UP, ifa); 915 else { 916 kfree(ifa); 917 ifa = ERR_PTR(err); 918 } 919 920 return ifa; 921 out: 922 spin_unlock(&addrconf_hash_lock); 923 goto out2; 924 } 925 926 enum cleanup_prefix_rt_t { 927 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */ 928 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */ 929 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */ 930 }; 931 932 /* 933 * Check, whether the prefix for ifp would still need a prefix route 934 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_* 935 * constants. 936 * 937 * 1) we don't purge prefix if address was not permanent. 938 * prefix is managed by its own lifetime. 939 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE. 940 * 3) if there are no addresses, delete prefix. 941 * 4) if there are still other permanent address(es), 942 * corresponding prefix is still permanent. 943 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE, 944 * don't purge the prefix, assume user space is managing it. 945 * 6) otherwise, update prefix lifetime to the 946 * longest valid lifetime among the corresponding 947 * addresses on the device. 948 * Note: subsequent RA will update lifetime. 949 **/ 950 static enum cleanup_prefix_rt_t 951 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires) 952 { 953 struct inet6_ifaddr *ifa; 954 struct inet6_dev *idev = ifp->idev; 955 unsigned long lifetime; 956 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL; 957 958 *expires = jiffies; 959 960 list_for_each_entry(ifa, &idev->addr_list, if_list) { 961 if (ifa == ifp) 962 continue; 963 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr, 964 ifp->prefix_len)) 965 continue; 966 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE)) 967 return CLEANUP_PREFIX_RT_NOP; 968 969 action = CLEANUP_PREFIX_RT_EXPIRE; 970 971 spin_lock(&ifa->lock); 972 973 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 974 /* 975 * Note: Because this address is 976 * not permanent, lifetime < 977 * LONG_MAX / HZ here. 978 */ 979 if (time_before(*expires, ifa->tstamp + lifetime * HZ)) 980 *expires = ifa->tstamp + lifetime * HZ; 981 spin_unlock(&ifa->lock); 982 } 983 984 return action; 985 } 986 987 static void 988 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt) 989 { 990 struct rt6_info *rt; 991 992 rt = addrconf_get_prefix_route(&ifp->addr, 993 ifp->prefix_len, 994 ifp->idev->dev, 995 0, RTF_GATEWAY | RTF_DEFAULT); 996 if (rt) { 997 if (del_rt) 998 ip6_del_rt(rt); 999 else { 1000 if (!(rt->rt6i_flags & RTF_EXPIRES)) 1001 rt6_set_expires(rt, expires); 1002 ip6_rt_put(rt); 1003 } 1004 } 1005 } 1006 1007 1008 /* This function wants to get referenced ifp and releases it before return */ 1009 1010 static void ipv6_del_addr(struct inet6_ifaddr *ifp) 1011 { 1012 int state; 1013 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP; 1014 unsigned long expires; 1015 1016 ASSERT_RTNL(); 1017 1018 spin_lock_bh(&ifp->lock); 1019 state = ifp->state; 1020 ifp->state = INET6_IFADDR_STATE_DEAD; 1021 spin_unlock_bh(&ifp->lock); 1022 1023 if (state == INET6_IFADDR_STATE_DEAD) 1024 goto out; 1025 1026 spin_lock_bh(&addrconf_hash_lock); 1027 hlist_del_init_rcu(&ifp->addr_lst); 1028 spin_unlock_bh(&addrconf_hash_lock); 1029 1030 write_lock_bh(&ifp->idev->lock); 1031 1032 if (ifp->flags&IFA_F_TEMPORARY) { 1033 list_del(&ifp->tmp_list); 1034 if (ifp->ifpub) { 1035 in6_ifa_put(ifp->ifpub); 1036 ifp->ifpub = NULL; 1037 } 1038 __in6_ifa_put(ifp); 1039 } 1040 1041 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE)) 1042 action = check_cleanup_prefix_route(ifp, &expires); 1043 1044 list_del_init(&ifp->if_list); 1045 __in6_ifa_put(ifp); 1046 1047 write_unlock_bh(&ifp->idev->lock); 1048 1049 addrconf_del_dad_work(ifp); 1050 1051 ipv6_ifa_notify(RTM_DELADDR, ifp); 1052 1053 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); 1054 1055 if (action != CLEANUP_PREFIX_RT_NOP) { 1056 cleanup_prefix_route(ifp, expires, 1057 action == CLEANUP_PREFIX_RT_DEL); 1058 } 1059 1060 /* clean up prefsrc entries */ 1061 rt6_remove_prefsrc(ifp); 1062 out: 1063 in6_ifa_put(ifp); 1064 } 1065 1066 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 1067 { 1068 struct inet6_dev *idev = ifp->idev; 1069 struct in6_addr addr, *tmpaddr; 1070 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age; 1071 unsigned long regen_advance; 1072 int tmp_plen; 1073 int ret = 0; 1074 u32 addr_flags; 1075 unsigned long now = jiffies; 1076 1077 write_lock_bh(&idev->lock); 1078 if (ift) { 1079 spin_lock_bh(&ift->lock); 1080 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 1081 spin_unlock_bh(&ift->lock); 1082 tmpaddr = &addr; 1083 } else { 1084 tmpaddr = NULL; 1085 } 1086 retry: 1087 in6_dev_hold(idev); 1088 if (idev->cnf.use_tempaddr <= 0) { 1089 write_unlock_bh(&idev->lock); 1090 pr_info("%s: use_tempaddr is disabled\n", __func__); 1091 in6_dev_put(idev); 1092 ret = -1; 1093 goto out; 1094 } 1095 spin_lock_bh(&ifp->lock); 1096 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 1097 idev->cnf.use_tempaddr = -1; /*XXX*/ 1098 spin_unlock_bh(&ifp->lock); 1099 write_unlock_bh(&idev->lock); 1100 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", 1101 __func__); 1102 in6_dev_put(idev); 1103 ret = -1; 1104 goto out; 1105 } 1106 in6_ifa_hold(ifp); 1107 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 1108 __ipv6_try_regen_rndid(idev, tmpaddr); 1109 memcpy(&addr.s6_addr[8], idev->rndid, 8); 1110 age = (now - ifp->tstamp) / HZ; 1111 tmp_valid_lft = min_t(__u32, 1112 ifp->valid_lft, 1113 idev->cnf.temp_valid_lft + age); 1114 tmp_prefered_lft = min_t(__u32, 1115 ifp->prefered_lft, 1116 idev->cnf.temp_prefered_lft + age - 1117 idev->cnf.max_desync_factor); 1118 tmp_plen = ifp->prefix_len; 1119 tmp_tstamp = ifp->tstamp; 1120 spin_unlock_bh(&ifp->lock); 1121 1122 regen_advance = idev->cnf.regen_max_retry * 1123 idev->cnf.dad_transmits * 1124 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ; 1125 write_unlock_bh(&idev->lock); 1126 1127 /* A temporary address is created only if this calculated Preferred 1128 * Lifetime is greater than REGEN_ADVANCE time units. In particular, 1129 * an implementation must not create a temporary address with a zero 1130 * Preferred Lifetime. 1131 * Use age calculation as in addrconf_verify to avoid unnecessary 1132 * temporary addresses being generated. 1133 */ 1134 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 1135 if (tmp_prefered_lft <= regen_advance + age) { 1136 in6_ifa_put(ifp); 1137 in6_dev_put(idev); 1138 ret = -1; 1139 goto out; 1140 } 1141 1142 addr_flags = IFA_F_TEMPORARY; 1143 /* set in addrconf_prefix_rcv() */ 1144 if (ifp->flags & IFA_F_OPTIMISTIC) 1145 addr_flags |= IFA_F_OPTIMISTIC; 1146 1147 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen, 1148 ipv6_addr_scope(&addr), addr_flags, 1149 tmp_valid_lft, tmp_prefered_lft); 1150 if (IS_ERR(ift)) { 1151 in6_ifa_put(ifp); 1152 in6_dev_put(idev); 1153 pr_info("%s: retry temporary address regeneration\n", __func__); 1154 tmpaddr = &addr; 1155 write_lock_bh(&idev->lock); 1156 goto retry; 1157 } 1158 1159 spin_lock_bh(&ift->lock); 1160 ift->ifpub = ifp; 1161 ift->cstamp = now; 1162 ift->tstamp = tmp_tstamp; 1163 spin_unlock_bh(&ift->lock); 1164 1165 addrconf_dad_start(ift); 1166 in6_ifa_put(ift); 1167 in6_dev_put(idev); 1168 out: 1169 return ret; 1170 } 1171 1172 /* 1173 * Choose an appropriate source address (RFC3484) 1174 */ 1175 enum { 1176 IPV6_SADDR_RULE_INIT = 0, 1177 IPV6_SADDR_RULE_LOCAL, 1178 IPV6_SADDR_RULE_SCOPE, 1179 IPV6_SADDR_RULE_PREFERRED, 1180 #ifdef CONFIG_IPV6_MIP6 1181 IPV6_SADDR_RULE_HOA, 1182 #endif 1183 IPV6_SADDR_RULE_OIF, 1184 IPV6_SADDR_RULE_LABEL, 1185 IPV6_SADDR_RULE_PRIVACY, 1186 IPV6_SADDR_RULE_ORCHID, 1187 IPV6_SADDR_RULE_PREFIX, 1188 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 1189 IPV6_SADDR_RULE_NOT_OPTIMISTIC, 1190 #endif 1191 IPV6_SADDR_RULE_MAX 1192 }; 1193 1194 struct ipv6_saddr_score { 1195 int rule; 1196 int addr_type; 1197 struct inet6_ifaddr *ifa; 1198 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 1199 int scopedist; 1200 int matchlen; 1201 }; 1202 1203 struct ipv6_saddr_dst { 1204 const struct in6_addr *addr; 1205 int ifindex; 1206 int scope; 1207 int label; 1208 unsigned int prefs; 1209 }; 1210 1211 static inline int ipv6_saddr_preferred(int type) 1212 { 1213 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 1214 return 1; 1215 return 0; 1216 } 1217 1218 static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev) 1219 { 1220 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 1221 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic; 1222 #else 1223 return false; 1224 #endif 1225 } 1226 1227 static int ipv6_get_saddr_eval(struct net *net, 1228 struct ipv6_saddr_score *score, 1229 struct ipv6_saddr_dst *dst, 1230 int i) 1231 { 1232 int ret; 1233 1234 if (i <= score->rule) { 1235 switch (i) { 1236 case IPV6_SADDR_RULE_SCOPE: 1237 ret = score->scopedist; 1238 break; 1239 case IPV6_SADDR_RULE_PREFIX: 1240 ret = score->matchlen; 1241 break; 1242 default: 1243 ret = !!test_bit(i, score->scorebits); 1244 } 1245 goto out; 1246 } 1247 1248 switch (i) { 1249 case IPV6_SADDR_RULE_INIT: 1250 /* Rule 0: remember if hiscore is not ready yet */ 1251 ret = !!score->ifa; 1252 break; 1253 case IPV6_SADDR_RULE_LOCAL: 1254 /* Rule 1: Prefer same address */ 1255 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1256 break; 1257 case IPV6_SADDR_RULE_SCOPE: 1258 /* Rule 2: Prefer appropriate scope 1259 * 1260 * ret 1261 * ^ 1262 * -1 | d 15 1263 * ---+--+-+---> scope 1264 * | 1265 * | d is scope of the destination. 1266 * B-d | \ 1267 * | \ <- smaller scope is better if 1268 * B-15 | \ if scope is enough for destination. 1269 * | ret = B - scope (-1 <= scope >= d <= 15). 1270 * d-C-1 | / 1271 * |/ <- greater is better 1272 * -C / if scope is not enough for destination. 1273 * /| ret = scope - C (-1 <= d < scope <= 15). 1274 * 1275 * d - C - 1 < B -15 (for all -1 <= d <= 15). 1276 * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1277 * Assume B = 0 and we get C > 29. 1278 */ 1279 ret = __ipv6_addr_src_scope(score->addr_type); 1280 if (ret >= dst->scope) 1281 ret = -ret; 1282 else 1283 ret -= 128; /* 30 is enough */ 1284 score->scopedist = ret; 1285 break; 1286 case IPV6_SADDR_RULE_PREFERRED: 1287 { 1288 /* Rule 3: Avoid deprecated and optimistic addresses */ 1289 u8 avoid = IFA_F_DEPRECATED; 1290 1291 if (!ipv6_use_optimistic_addr(score->ifa->idev)) 1292 avoid |= IFA_F_OPTIMISTIC; 1293 ret = ipv6_saddr_preferred(score->addr_type) || 1294 !(score->ifa->flags & avoid); 1295 break; 1296 } 1297 #ifdef CONFIG_IPV6_MIP6 1298 case IPV6_SADDR_RULE_HOA: 1299 { 1300 /* Rule 4: Prefer home address */ 1301 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 1302 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1303 break; 1304 } 1305 #endif 1306 case IPV6_SADDR_RULE_OIF: 1307 /* Rule 5: Prefer outgoing interface */ 1308 ret = (!dst->ifindex || 1309 dst->ifindex == score->ifa->idev->dev->ifindex); 1310 break; 1311 case IPV6_SADDR_RULE_LABEL: 1312 /* Rule 6: Prefer matching label */ 1313 ret = ipv6_addr_label(net, 1314 &score->ifa->addr, score->addr_type, 1315 score->ifa->idev->dev->ifindex) == dst->label; 1316 break; 1317 case IPV6_SADDR_RULE_PRIVACY: 1318 { 1319 /* Rule 7: Prefer public address 1320 * Note: prefer temporary address if use_tempaddr >= 2 1321 */ 1322 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 1323 !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 1324 score->ifa->idev->cnf.use_tempaddr >= 2; 1325 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1326 break; 1327 } 1328 case IPV6_SADDR_RULE_ORCHID: 1329 /* Rule 8-: Prefer ORCHID vs ORCHID or 1330 * non-ORCHID vs non-ORCHID 1331 */ 1332 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1333 ipv6_addr_orchid(dst->addr)); 1334 break; 1335 case IPV6_SADDR_RULE_PREFIX: 1336 /* Rule 8: Use longest matching prefix */ 1337 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); 1338 if (ret > score->ifa->prefix_len) 1339 ret = score->ifa->prefix_len; 1340 score->matchlen = ret; 1341 break; 1342 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 1343 case IPV6_SADDR_RULE_NOT_OPTIMISTIC: 1344 /* Optimistic addresses still have lower precedence than other 1345 * preferred addresses. 1346 */ 1347 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC); 1348 break; 1349 #endif 1350 default: 1351 ret = 0; 1352 } 1353 1354 if (ret) 1355 __set_bit(i, score->scorebits); 1356 score->rule = i; 1357 out: 1358 return ret; 1359 } 1360 1361 static int __ipv6_dev_get_saddr(struct net *net, 1362 struct ipv6_saddr_dst *dst, 1363 unsigned int prefs, 1364 const struct in6_addr *saddr, 1365 struct inet6_dev *idev, 1366 struct ipv6_saddr_score *scores, 1367 int hiscore_idx) 1368 { 1369 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx]; 1370 1371 read_lock_bh(&idev->lock); 1372 list_for_each_entry(score->ifa, &idev->addr_list, if_list) { 1373 int i; 1374 1375 /* 1376 * - Tentative Address (RFC2462 section 5.4) 1377 * - A tentative address is not considered 1378 * "assigned to an interface" in the traditional 1379 * sense, unless it is also flagged as optimistic. 1380 * - Candidate Source Address (section 4) 1381 * - In any case, anycast addresses, multicast 1382 * addresses, and the unspecified address MUST 1383 * NOT be included in a candidate set. 1384 */ 1385 if ((score->ifa->flags & IFA_F_TENTATIVE) && 1386 (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 1387 continue; 1388 1389 score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1390 1391 if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1392 score->addr_type & IPV6_ADDR_MULTICAST)) { 1393 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s", 1394 idev->dev->name); 1395 continue; 1396 } 1397 1398 score->rule = -1; 1399 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1400 1401 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1402 int minihiscore, miniscore; 1403 1404 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i); 1405 miniscore = ipv6_get_saddr_eval(net, score, dst, i); 1406 1407 if (minihiscore > miniscore) { 1408 if (i == IPV6_SADDR_RULE_SCOPE && 1409 score->scopedist > 0) { 1410 /* 1411 * special case: 1412 * each remaining entry 1413 * has too small (not enough) 1414 * scope, because ifa entries 1415 * are sorted by their scope 1416 * values. 1417 */ 1418 goto out; 1419 } 1420 break; 1421 } else if (minihiscore < miniscore) { 1422 if (hiscore->ifa) 1423 in6_ifa_put(hiscore->ifa); 1424 1425 in6_ifa_hold(score->ifa); 1426 1427 swap(hiscore, score); 1428 hiscore_idx = 1 - hiscore_idx; 1429 1430 /* restore our iterator */ 1431 score->ifa = hiscore->ifa; 1432 1433 break; 1434 } 1435 } 1436 } 1437 out: 1438 read_unlock_bh(&idev->lock); 1439 return hiscore_idx; 1440 } 1441 1442 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, 1443 const struct in6_addr *daddr, unsigned int prefs, 1444 struct in6_addr *saddr) 1445 { 1446 struct ipv6_saddr_score scores[2], *hiscore; 1447 struct ipv6_saddr_dst dst; 1448 struct inet6_dev *idev; 1449 struct net_device *dev; 1450 int dst_type; 1451 bool use_oif_addr = false; 1452 int hiscore_idx = 0; 1453 1454 dst_type = __ipv6_addr_type(daddr); 1455 dst.addr = daddr; 1456 dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 1457 dst.scope = __ipv6_addr_src_scope(dst_type); 1458 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 1459 dst.prefs = prefs; 1460 1461 scores[hiscore_idx].rule = -1; 1462 scores[hiscore_idx].ifa = NULL; 1463 1464 rcu_read_lock(); 1465 1466 /* Candidate Source Address (section 4) 1467 * - multicast and link-local destination address, 1468 * the set of candidate source address MUST only 1469 * include addresses assigned to interfaces 1470 * belonging to the same link as the outgoing 1471 * interface. 1472 * (- For site-local destination addresses, the 1473 * set of candidate source addresses MUST only 1474 * include addresses assigned to interfaces 1475 * belonging to the same site as the outgoing 1476 * interface.) 1477 */ 1478 if (dst_dev) { 1479 if ((dst_type & IPV6_ADDR_MULTICAST) || 1480 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) { 1481 idev = __in6_dev_get(dst_dev); 1482 use_oif_addr = true; 1483 } 1484 } 1485 1486 if (use_oif_addr) { 1487 if (idev) 1488 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, prefs, saddr, idev, scores, hiscore_idx); 1489 } else { 1490 for_each_netdev_rcu(net, dev) { 1491 idev = __in6_dev_get(dev); 1492 if (!idev) 1493 continue; 1494 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, prefs, saddr, idev, scores, hiscore_idx); 1495 } 1496 } 1497 rcu_read_unlock(); 1498 1499 hiscore = &scores[hiscore_idx]; 1500 if (!hiscore->ifa) 1501 return -EADDRNOTAVAIL; 1502 1503 *saddr = hiscore->ifa->addr; 1504 in6_ifa_put(hiscore->ifa); 1505 return 0; 1506 } 1507 EXPORT_SYMBOL(ipv6_dev_get_saddr); 1508 1509 int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, 1510 u32 banned_flags) 1511 { 1512 struct inet6_ifaddr *ifp; 1513 int err = -EADDRNOTAVAIL; 1514 1515 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 1516 if (ifp->scope > IFA_LINK) 1517 break; 1518 if (ifp->scope == IFA_LINK && 1519 !(ifp->flags & banned_flags)) { 1520 *addr = ifp->addr; 1521 err = 0; 1522 break; 1523 } 1524 } 1525 return err; 1526 } 1527 1528 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 1529 u32 banned_flags) 1530 { 1531 struct inet6_dev *idev; 1532 int err = -EADDRNOTAVAIL; 1533 1534 rcu_read_lock(); 1535 idev = __in6_dev_get(dev); 1536 if (idev) { 1537 read_lock_bh(&idev->lock); 1538 err = __ipv6_get_lladdr(idev, addr, banned_flags); 1539 read_unlock_bh(&idev->lock); 1540 } 1541 rcu_read_unlock(); 1542 return err; 1543 } 1544 1545 static int ipv6_count_addresses(struct inet6_dev *idev) 1546 { 1547 int cnt = 0; 1548 struct inet6_ifaddr *ifp; 1549 1550 read_lock_bh(&idev->lock); 1551 list_for_each_entry(ifp, &idev->addr_list, if_list) 1552 cnt++; 1553 read_unlock_bh(&idev->lock); 1554 return cnt; 1555 } 1556 1557 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, 1558 const struct net_device *dev, int strict) 1559 { 1560 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE); 1561 } 1562 EXPORT_SYMBOL(ipv6_chk_addr); 1563 1564 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, 1565 const struct net_device *dev, int strict, 1566 u32 banned_flags) 1567 { 1568 struct inet6_ifaddr *ifp; 1569 unsigned int hash = inet6_addr_hash(addr); 1570 u32 ifp_flags; 1571 1572 rcu_read_lock_bh(); 1573 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 1574 if (!net_eq(dev_net(ifp->idev->dev), net)) 1575 continue; 1576 /* Decouple optimistic from tentative for evaluation here. 1577 * Ban optimistic addresses explicitly, when required. 1578 */ 1579 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC) 1580 ? (ifp->flags&~IFA_F_TENTATIVE) 1581 : ifp->flags; 1582 if (ipv6_addr_equal(&ifp->addr, addr) && 1583 !(ifp_flags&banned_flags) && 1584 (!dev || ifp->idev->dev == dev || 1585 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 1586 rcu_read_unlock_bh(); 1587 return 1; 1588 } 1589 } 1590 1591 rcu_read_unlock_bh(); 1592 return 0; 1593 } 1594 EXPORT_SYMBOL(ipv6_chk_addr_and_flags); 1595 1596 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 1597 struct net_device *dev) 1598 { 1599 unsigned int hash = inet6_addr_hash(addr); 1600 struct inet6_ifaddr *ifp; 1601 1602 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) { 1603 if (!net_eq(dev_net(ifp->idev->dev), net)) 1604 continue; 1605 if (ipv6_addr_equal(&ifp->addr, addr)) { 1606 if (!dev || ifp->idev->dev == dev) 1607 return true; 1608 } 1609 } 1610 return false; 1611 } 1612 1613 /* Compares an address/prefix_len with addresses on device @dev. 1614 * If one is found it returns true. 1615 */ 1616 bool ipv6_chk_custom_prefix(const struct in6_addr *addr, 1617 const unsigned int prefix_len, struct net_device *dev) 1618 { 1619 struct inet6_dev *idev; 1620 struct inet6_ifaddr *ifa; 1621 bool ret = false; 1622 1623 rcu_read_lock(); 1624 idev = __in6_dev_get(dev); 1625 if (idev) { 1626 read_lock_bh(&idev->lock); 1627 list_for_each_entry(ifa, &idev->addr_list, if_list) { 1628 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len); 1629 if (ret) 1630 break; 1631 } 1632 read_unlock_bh(&idev->lock); 1633 } 1634 rcu_read_unlock(); 1635 1636 return ret; 1637 } 1638 EXPORT_SYMBOL(ipv6_chk_custom_prefix); 1639 1640 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) 1641 { 1642 struct inet6_dev *idev; 1643 struct inet6_ifaddr *ifa; 1644 int onlink; 1645 1646 onlink = 0; 1647 rcu_read_lock(); 1648 idev = __in6_dev_get(dev); 1649 if (idev) { 1650 read_lock_bh(&idev->lock); 1651 list_for_each_entry(ifa, &idev->addr_list, if_list) { 1652 onlink = ipv6_prefix_equal(addr, &ifa->addr, 1653 ifa->prefix_len); 1654 if (onlink) 1655 break; 1656 } 1657 read_unlock_bh(&idev->lock); 1658 } 1659 rcu_read_unlock(); 1660 return onlink; 1661 } 1662 EXPORT_SYMBOL(ipv6_chk_prefix); 1663 1664 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 1665 struct net_device *dev, int strict) 1666 { 1667 struct inet6_ifaddr *ifp, *result = NULL; 1668 unsigned int hash = inet6_addr_hash(addr); 1669 1670 rcu_read_lock_bh(); 1671 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 1672 if (!net_eq(dev_net(ifp->idev->dev), net)) 1673 continue; 1674 if (ipv6_addr_equal(&ifp->addr, addr)) { 1675 if (!dev || ifp->idev->dev == dev || 1676 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 1677 result = ifp; 1678 in6_ifa_hold(ifp); 1679 break; 1680 } 1681 } 1682 } 1683 rcu_read_unlock_bh(); 1684 1685 return result; 1686 } 1687 1688 /* Gets referenced address, destroys ifaddr */ 1689 1690 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 1691 { 1692 if (ifp->flags&IFA_F_PERMANENT) { 1693 spin_lock_bh(&ifp->lock); 1694 addrconf_del_dad_work(ifp); 1695 ifp->flags |= IFA_F_TENTATIVE; 1696 if (dad_failed) 1697 ifp->flags |= IFA_F_DADFAILED; 1698 spin_unlock_bh(&ifp->lock); 1699 if (dad_failed) 1700 ipv6_ifa_notify(0, ifp); 1701 in6_ifa_put(ifp); 1702 } else if (ifp->flags&IFA_F_TEMPORARY) { 1703 struct inet6_ifaddr *ifpub; 1704 spin_lock_bh(&ifp->lock); 1705 ifpub = ifp->ifpub; 1706 if (ifpub) { 1707 in6_ifa_hold(ifpub); 1708 spin_unlock_bh(&ifp->lock); 1709 ipv6_create_tempaddr(ifpub, ifp); 1710 in6_ifa_put(ifpub); 1711 } else { 1712 spin_unlock_bh(&ifp->lock); 1713 } 1714 ipv6_del_addr(ifp); 1715 } else { 1716 ipv6_del_addr(ifp); 1717 } 1718 } 1719 1720 static int addrconf_dad_end(struct inet6_ifaddr *ifp) 1721 { 1722 int err = -ENOENT; 1723 1724 spin_lock_bh(&ifp->lock); 1725 if (ifp->state == INET6_IFADDR_STATE_DAD) { 1726 ifp->state = INET6_IFADDR_STATE_POSTDAD; 1727 err = 0; 1728 } 1729 spin_unlock_bh(&ifp->lock); 1730 1731 return err; 1732 } 1733 1734 void addrconf_dad_failure(struct inet6_ifaddr *ifp) 1735 { 1736 struct in6_addr addr; 1737 struct inet6_dev *idev = ifp->idev; 1738 struct net *net = dev_net(ifp->idev->dev); 1739 1740 if (addrconf_dad_end(ifp)) { 1741 in6_ifa_put(ifp); 1742 return; 1743 } 1744 1745 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n", 1746 ifp->idev->dev->name, &ifp->addr); 1747 1748 spin_lock_bh(&ifp->lock); 1749 1750 if (ifp->flags & IFA_F_STABLE_PRIVACY) { 1751 int scope = ifp->scope; 1752 u32 flags = ifp->flags; 1753 struct in6_addr new_addr; 1754 struct inet6_ifaddr *ifp2; 1755 u32 valid_lft, preferred_lft; 1756 int pfxlen = ifp->prefix_len; 1757 int retries = ifp->stable_privacy_retry + 1; 1758 1759 if (retries > net->ipv6.sysctl.idgen_retries) { 1760 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n", 1761 ifp->idev->dev->name); 1762 goto errdad; 1763 } 1764 1765 new_addr = ifp->addr; 1766 if (ipv6_generate_stable_address(&new_addr, retries, 1767 idev)) 1768 goto errdad; 1769 1770 valid_lft = ifp->valid_lft; 1771 preferred_lft = ifp->prefered_lft; 1772 1773 spin_unlock_bh(&ifp->lock); 1774 1775 if (idev->cnf.max_addresses && 1776 ipv6_count_addresses(idev) >= 1777 idev->cnf.max_addresses) 1778 goto lock_errdad; 1779 1780 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n", 1781 ifp->idev->dev->name); 1782 1783 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen, 1784 scope, flags, valid_lft, 1785 preferred_lft); 1786 if (IS_ERR(ifp2)) 1787 goto lock_errdad; 1788 1789 spin_lock_bh(&ifp2->lock); 1790 ifp2->stable_privacy_retry = retries; 1791 ifp2->state = INET6_IFADDR_STATE_PREDAD; 1792 spin_unlock_bh(&ifp2->lock); 1793 1794 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay); 1795 in6_ifa_put(ifp2); 1796 lock_errdad: 1797 spin_lock_bh(&ifp->lock); 1798 } else if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) { 1799 addr.s6_addr32[0] = htonl(0xfe800000); 1800 addr.s6_addr32[1] = 0; 1801 1802 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 1803 ipv6_addr_equal(&ifp->addr, &addr)) { 1804 /* DAD failed for link-local based on MAC address */ 1805 idev->cnf.disable_ipv6 = 1; 1806 1807 pr_info("%s: IPv6 being disabled!\n", 1808 ifp->idev->dev->name); 1809 } 1810 } 1811 1812 errdad: 1813 /* transition from _POSTDAD to _ERRDAD */ 1814 ifp->state = INET6_IFADDR_STATE_ERRDAD; 1815 spin_unlock_bh(&ifp->lock); 1816 1817 addrconf_mod_dad_work(ifp, 0); 1818 } 1819 1820 /* Join to solicited addr multicast group. 1821 * caller must hold RTNL */ 1822 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) 1823 { 1824 struct in6_addr maddr; 1825 1826 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 1827 return; 1828 1829 addrconf_addr_solict_mult(addr, &maddr); 1830 ipv6_dev_mc_inc(dev, &maddr); 1831 } 1832 1833 /* caller must hold RTNL */ 1834 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) 1835 { 1836 struct in6_addr maddr; 1837 1838 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 1839 return; 1840 1841 addrconf_addr_solict_mult(addr, &maddr); 1842 __ipv6_dev_mc_dec(idev, &maddr); 1843 } 1844 1845 /* caller must hold RTNL */ 1846 static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 1847 { 1848 struct in6_addr addr; 1849 1850 if (ifp->prefix_len >= 127) /* RFC 6164 */ 1851 return; 1852 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 1853 if (ipv6_addr_any(&addr)) 1854 return; 1855 __ipv6_dev_ac_inc(ifp->idev, &addr); 1856 } 1857 1858 /* caller must hold RTNL */ 1859 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 1860 { 1861 struct in6_addr addr; 1862 1863 if (ifp->prefix_len >= 127) /* RFC 6164 */ 1864 return; 1865 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 1866 if (ipv6_addr_any(&addr)) 1867 return; 1868 __ipv6_dev_ac_dec(ifp->idev, &addr); 1869 } 1870 1871 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev) 1872 { 1873 if (dev->addr_len != ETH_ALEN) 1874 return -1; 1875 memcpy(eui, dev->dev_addr, 3); 1876 memcpy(eui + 5, dev->dev_addr + 3, 3); 1877 1878 /* 1879 * The zSeries OSA network cards can be shared among various 1880 * OS instances, but the OSA cards have only one MAC address. 1881 * This leads to duplicate address conflicts in conjunction 1882 * with IPv6 if more than one instance uses the same card. 1883 * 1884 * The driver for these cards can deliver a unique 16-bit 1885 * identifier for each instance sharing the same card. It is 1886 * placed instead of 0xFFFE in the interface identifier. The 1887 * "u" bit of the interface identifier is not inverted in this 1888 * case. Hence the resulting interface identifier has local 1889 * scope according to RFC2373. 1890 */ 1891 if (dev->dev_id) { 1892 eui[3] = (dev->dev_id >> 8) & 0xFF; 1893 eui[4] = dev->dev_id & 0xFF; 1894 } else { 1895 eui[3] = 0xFF; 1896 eui[4] = 0xFE; 1897 eui[0] ^= 2; 1898 } 1899 return 0; 1900 } 1901 1902 static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev) 1903 { 1904 if (dev->addr_len != IEEE802154_ADDR_LEN) 1905 return -1; 1906 memcpy(eui, dev->dev_addr, 8); 1907 eui[0] ^= 2; 1908 return 0; 1909 } 1910 1911 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) 1912 { 1913 union fwnet_hwaddr *ha; 1914 1915 if (dev->addr_len != FWNET_ALEN) 1916 return -1; 1917 1918 ha = (union fwnet_hwaddr *)dev->dev_addr; 1919 1920 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); 1921 eui[0] ^= 2; 1922 return 0; 1923 } 1924 1925 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 1926 { 1927 /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 1928 if (dev->addr_len != ARCNET_ALEN) 1929 return -1; 1930 memset(eui, 0, 7); 1931 eui[7] = *(u8 *)dev->dev_addr; 1932 return 0; 1933 } 1934 1935 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 1936 { 1937 if (dev->addr_len != INFINIBAND_ALEN) 1938 return -1; 1939 memcpy(eui, dev->dev_addr + 12, 8); 1940 eui[0] |= 2; 1941 return 0; 1942 } 1943 1944 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 1945 { 1946 if (addr == 0) 1947 return -1; 1948 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 1949 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 1950 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 1951 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 1952 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 1953 ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 1954 eui[1] = 0; 1955 eui[2] = 0x5E; 1956 eui[3] = 0xFE; 1957 memcpy(eui + 4, &addr, 4); 1958 return 0; 1959 } 1960 1961 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 1962 { 1963 if (dev->priv_flags & IFF_ISATAP) 1964 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 1965 return -1; 1966 } 1967 1968 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) 1969 { 1970 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 1971 } 1972 1973 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev) 1974 { 1975 memcpy(eui, dev->perm_addr, 3); 1976 memcpy(eui + 5, dev->perm_addr + 3, 3); 1977 eui[3] = 0xFF; 1978 eui[4] = 0xFE; 1979 eui[0] ^= 2; 1980 return 0; 1981 } 1982 1983 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 1984 { 1985 switch (dev->type) { 1986 case ARPHRD_ETHER: 1987 case ARPHRD_FDDI: 1988 return addrconf_ifid_eui48(eui, dev); 1989 case ARPHRD_ARCNET: 1990 return addrconf_ifid_arcnet(eui, dev); 1991 case ARPHRD_INFINIBAND: 1992 return addrconf_ifid_infiniband(eui, dev); 1993 case ARPHRD_SIT: 1994 return addrconf_ifid_sit(eui, dev); 1995 case ARPHRD_IPGRE: 1996 return addrconf_ifid_gre(eui, dev); 1997 case ARPHRD_6LOWPAN: 1998 case ARPHRD_IEEE802154: 1999 return addrconf_ifid_eui64(eui, dev); 2000 case ARPHRD_IEEE1394: 2001 return addrconf_ifid_ieee1394(eui, dev); 2002 case ARPHRD_TUNNEL6: 2003 return addrconf_ifid_ip6tnl(eui, dev); 2004 } 2005 return -1; 2006 } 2007 2008 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 2009 { 2010 int err = -1; 2011 struct inet6_ifaddr *ifp; 2012 2013 read_lock_bh(&idev->lock); 2014 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { 2015 if (ifp->scope > IFA_LINK) 2016 break; 2017 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 2018 memcpy(eui, ifp->addr.s6_addr+8, 8); 2019 err = 0; 2020 break; 2021 } 2022 } 2023 read_unlock_bh(&idev->lock); 2024 return err; 2025 } 2026 2027 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 2028 static void __ipv6_regen_rndid(struct inet6_dev *idev) 2029 { 2030 regen: 2031 get_random_bytes(idev->rndid, sizeof(idev->rndid)); 2032 idev->rndid[0] &= ~0x02; 2033 2034 /* 2035 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 2036 * check if generated address is not inappropriate 2037 * 2038 * - Reserved subnet anycast (RFC 2526) 2039 * 11111101 11....11 1xxxxxxx 2040 * - ISATAP (RFC4214) 6.1 2041 * 00-00-5E-FE-xx-xx-xx-xx 2042 * - value 0 2043 * - XXX: already assigned to an address on the device 2044 */ 2045 if (idev->rndid[0] == 0xfd && 2046 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 2047 (idev->rndid[7]&0x80)) 2048 goto regen; 2049 if ((idev->rndid[0]|idev->rndid[1]) == 0) { 2050 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 2051 goto regen; 2052 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 2053 goto regen; 2054 } 2055 } 2056 2057 static void ipv6_regen_rndid(unsigned long data) 2058 { 2059 struct inet6_dev *idev = (struct inet6_dev *) data; 2060 unsigned long expires; 2061 2062 rcu_read_lock_bh(); 2063 write_lock_bh(&idev->lock); 2064 2065 if (idev->dead) 2066 goto out; 2067 2068 __ipv6_regen_rndid(idev); 2069 2070 expires = jiffies + 2071 idev->cnf.temp_prefered_lft * HZ - 2072 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * 2073 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) - 2074 idev->cnf.max_desync_factor * HZ; 2075 if (time_before(expires, jiffies)) { 2076 pr_warn("%s: too short regeneration interval; timer disabled for %s\n", 2077 __func__, idev->dev->name); 2078 goto out; 2079 } 2080 2081 if (!mod_timer(&idev->regen_timer, expires)) 2082 in6_dev_hold(idev); 2083 2084 out: 2085 write_unlock_bh(&idev->lock); 2086 rcu_read_unlock_bh(); 2087 in6_dev_put(idev); 2088 } 2089 2090 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) 2091 { 2092 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 2093 __ipv6_regen_rndid(idev); 2094 } 2095 2096 /* 2097 * Add prefix route. 2098 */ 2099 2100 static void 2101 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 2102 unsigned long expires, u32 flags) 2103 { 2104 struct fib6_config cfg = { 2105 .fc_table = RT6_TABLE_PREFIX, 2106 .fc_metric = IP6_RT_PRIO_ADDRCONF, 2107 .fc_ifindex = dev->ifindex, 2108 .fc_expires = expires, 2109 .fc_dst_len = plen, 2110 .fc_flags = RTF_UP | flags, 2111 .fc_nlinfo.nl_net = dev_net(dev), 2112 .fc_protocol = RTPROT_KERNEL, 2113 }; 2114 2115 cfg.fc_dst = *pfx; 2116 2117 /* Prevent useless cloning on PtP SIT. 2118 This thing is done here expecting that the whole 2119 class of non-broadcast devices need not cloning. 2120 */ 2121 #if IS_ENABLED(CONFIG_IPV6_SIT) 2122 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 2123 cfg.fc_flags |= RTF_NONEXTHOP; 2124 #endif 2125 2126 ip6_route_add(&cfg); 2127 } 2128 2129 2130 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 2131 int plen, 2132 const struct net_device *dev, 2133 u32 flags, u32 noflags) 2134 { 2135 struct fib6_node *fn; 2136 struct rt6_info *rt = NULL; 2137 struct fib6_table *table; 2138 2139 table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX); 2140 if (!table) 2141 return NULL; 2142 2143 read_lock_bh(&table->tb6_lock); 2144 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0); 2145 if (!fn) 2146 goto out; 2147 2148 noflags |= RTF_CACHE; 2149 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 2150 if (rt->dst.dev->ifindex != dev->ifindex) 2151 continue; 2152 if ((rt->rt6i_flags & flags) != flags) 2153 continue; 2154 if ((rt->rt6i_flags & noflags) != 0) 2155 continue; 2156 dst_hold(&rt->dst); 2157 break; 2158 } 2159 out: 2160 read_unlock_bh(&table->tb6_lock); 2161 return rt; 2162 } 2163 2164 2165 /* Create "default" multicast route to the interface */ 2166 2167 static void addrconf_add_mroute(struct net_device *dev) 2168 { 2169 struct fib6_config cfg = { 2170 .fc_table = RT6_TABLE_LOCAL, 2171 .fc_metric = IP6_RT_PRIO_ADDRCONF, 2172 .fc_ifindex = dev->ifindex, 2173 .fc_dst_len = 8, 2174 .fc_flags = RTF_UP, 2175 .fc_nlinfo.nl_net = dev_net(dev), 2176 }; 2177 2178 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 2179 2180 ip6_route_add(&cfg); 2181 } 2182 2183 static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 2184 { 2185 struct inet6_dev *idev; 2186 2187 ASSERT_RTNL(); 2188 2189 idev = ipv6_find_idev(dev); 2190 if (!idev) 2191 return ERR_PTR(-ENOBUFS); 2192 2193 if (idev->cnf.disable_ipv6) 2194 return ERR_PTR(-EACCES); 2195 2196 /* Add default multicast route */ 2197 if (!(dev->flags & IFF_LOOPBACK)) 2198 addrconf_add_mroute(dev); 2199 2200 return idev; 2201 } 2202 2203 static void manage_tempaddrs(struct inet6_dev *idev, 2204 struct inet6_ifaddr *ifp, 2205 __u32 valid_lft, __u32 prefered_lft, 2206 bool create, unsigned long now) 2207 { 2208 u32 flags; 2209 struct inet6_ifaddr *ift; 2210 2211 read_lock_bh(&idev->lock); 2212 /* update all temporary addresses in the list */ 2213 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) { 2214 int age, max_valid, max_prefered; 2215 2216 if (ifp != ift->ifpub) 2217 continue; 2218 2219 /* RFC 4941 section 3.3: 2220 * If a received option will extend the lifetime of a public 2221 * address, the lifetimes of temporary addresses should 2222 * be extended, subject to the overall constraint that no 2223 * temporary addresses should ever remain "valid" or "preferred" 2224 * for a time longer than (TEMP_VALID_LIFETIME) or 2225 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively. 2226 */ 2227 age = (now - ift->cstamp) / HZ; 2228 max_valid = idev->cnf.temp_valid_lft - age; 2229 if (max_valid < 0) 2230 max_valid = 0; 2231 2232 max_prefered = idev->cnf.temp_prefered_lft - 2233 idev->cnf.max_desync_factor - age; 2234 if (max_prefered < 0) 2235 max_prefered = 0; 2236 2237 if (valid_lft > max_valid) 2238 valid_lft = max_valid; 2239 2240 if (prefered_lft > max_prefered) 2241 prefered_lft = max_prefered; 2242 2243 spin_lock(&ift->lock); 2244 flags = ift->flags; 2245 ift->valid_lft = valid_lft; 2246 ift->prefered_lft = prefered_lft; 2247 ift->tstamp = now; 2248 if (prefered_lft > 0) 2249 ift->flags &= ~IFA_F_DEPRECATED; 2250 2251 spin_unlock(&ift->lock); 2252 if (!(flags&IFA_F_TENTATIVE)) 2253 ipv6_ifa_notify(0, ift); 2254 } 2255 2256 if ((create || list_empty(&idev->tempaddr_list)) && 2257 idev->cnf.use_tempaddr > 0) { 2258 /* When a new public address is created as described 2259 * in [ADDRCONF], also create a new temporary address. 2260 * Also create a temporary address if it's enabled but 2261 * no temporary address currently exists. 2262 */ 2263 read_unlock_bh(&idev->lock); 2264 ipv6_create_tempaddr(ifp, NULL); 2265 } else { 2266 read_unlock_bh(&idev->lock); 2267 } 2268 } 2269 2270 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) 2271 { 2272 struct prefix_info *pinfo; 2273 __u32 valid_lft; 2274 __u32 prefered_lft; 2275 int addr_type; 2276 u32 addr_flags = 0; 2277 struct inet6_dev *in6_dev; 2278 struct net *net = dev_net(dev); 2279 2280 pinfo = (struct prefix_info *) opt; 2281 2282 if (len < sizeof(struct prefix_info)) { 2283 ADBG("addrconf: prefix option too short\n"); 2284 return; 2285 } 2286 2287 /* 2288 * Validation checks ([ADDRCONF], page 19) 2289 */ 2290 2291 addr_type = ipv6_addr_type(&pinfo->prefix); 2292 2293 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 2294 return; 2295 2296 valid_lft = ntohl(pinfo->valid); 2297 prefered_lft = ntohl(pinfo->prefered); 2298 2299 if (prefered_lft > valid_lft) { 2300 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); 2301 return; 2302 } 2303 2304 in6_dev = in6_dev_get(dev); 2305 2306 if (!in6_dev) { 2307 net_dbg_ratelimited("addrconf: device %s not configured\n", 2308 dev->name); 2309 return; 2310 } 2311 2312 /* 2313 * Two things going on here: 2314 * 1) Add routes for on-link prefixes 2315 * 2) Configure prefixes with the auto flag set 2316 */ 2317 2318 if (pinfo->onlink) { 2319 struct rt6_info *rt; 2320 unsigned long rt_expires; 2321 2322 /* Avoid arithmetic overflow. Really, we could 2323 * save rt_expires in seconds, likely valid_lft, 2324 * but it would require division in fib gc, that it 2325 * not good. 2326 */ 2327 if (HZ > USER_HZ) 2328 rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 2329 else 2330 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 2331 2332 if (addrconf_finite_timeout(rt_expires)) 2333 rt_expires *= HZ; 2334 2335 rt = addrconf_get_prefix_route(&pinfo->prefix, 2336 pinfo->prefix_len, 2337 dev, 2338 RTF_ADDRCONF | RTF_PREFIX_RT, 2339 RTF_GATEWAY | RTF_DEFAULT); 2340 2341 if (rt) { 2342 /* Autoconf prefix route */ 2343 if (valid_lft == 0) { 2344 ip6_del_rt(rt); 2345 rt = NULL; 2346 } else if (addrconf_finite_timeout(rt_expires)) { 2347 /* not infinity */ 2348 rt6_set_expires(rt, jiffies + rt_expires); 2349 } else { 2350 rt6_clean_expires(rt); 2351 } 2352 } else if (valid_lft) { 2353 clock_t expires = 0; 2354 int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 2355 if (addrconf_finite_timeout(rt_expires)) { 2356 /* not infinity */ 2357 flags |= RTF_EXPIRES; 2358 expires = jiffies_to_clock_t(rt_expires); 2359 } 2360 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 2361 dev, expires, flags); 2362 } 2363 ip6_rt_put(rt); 2364 } 2365 2366 /* Try to figure out our local address for this prefix */ 2367 2368 if (pinfo->autoconf && in6_dev->cnf.autoconf) { 2369 struct inet6_ifaddr *ifp; 2370 struct in6_addr addr; 2371 int create = 0, update_lft = 0; 2372 bool tokenized = false; 2373 2374 if (pinfo->prefix_len == 64) { 2375 memcpy(&addr, &pinfo->prefix, 8); 2376 2377 if (!ipv6_addr_any(&in6_dev->token)) { 2378 read_lock_bh(&in6_dev->lock); 2379 memcpy(addr.s6_addr + 8, 2380 in6_dev->token.s6_addr + 8, 8); 2381 read_unlock_bh(&in6_dev->lock); 2382 tokenized = true; 2383 } else if (in6_dev->addr_gen_mode == 2384 IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 2385 !ipv6_generate_stable_address(&addr, 0, 2386 in6_dev)) { 2387 addr_flags |= IFA_F_STABLE_PRIVACY; 2388 goto ok; 2389 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 2390 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 2391 in6_dev_put(in6_dev); 2392 return; 2393 } 2394 goto ok; 2395 } 2396 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", 2397 pinfo->prefix_len); 2398 in6_dev_put(in6_dev); 2399 return; 2400 2401 ok: 2402 2403 ifp = ipv6_get_ifaddr(net, &addr, dev, 1); 2404 2405 if (!ifp && valid_lft) { 2406 int max_addresses = in6_dev->cnf.max_addresses; 2407 2408 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 2409 if (in6_dev->cnf.optimistic_dad && 2410 !net->ipv6.devconf_all->forwarding && sllao) 2411 addr_flags = IFA_F_OPTIMISTIC; 2412 #endif 2413 2414 /* Do not allow to create too much of autoconfigured 2415 * addresses; this would be too easy way to crash kernel. 2416 */ 2417 if (!max_addresses || 2418 ipv6_count_addresses(in6_dev) < max_addresses) 2419 ifp = ipv6_add_addr(in6_dev, &addr, NULL, 2420 pinfo->prefix_len, 2421 addr_type&IPV6_ADDR_SCOPE_MASK, 2422 addr_flags, valid_lft, 2423 prefered_lft); 2424 2425 if (IS_ERR_OR_NULL(ifp)) { 2426 in6_dev_put(in6_dev); 2427 return; 2428 } 2429 2430 update_lft = 0; 2431 create = 1; 2432 spin_lock_bh(&ifp->lock); 2433 ifp->flags |= IFA_F_MANAGETEMPADDR; 2434 ifp->cstamp = jiffies; 2435 ifp->tokenized = tokenized; 2436 spin_unlock_bh(&ifp->lock); 2437 addrconf_dad_start(ifp); 2438 } 2439 2440 if (ifp) { 2441 u32 flags; 2442 unsigned long now; 2443 u32 stored_lft; 2444 2445 /* update lifetime (RFC2462 5.5.3 e) */ 2446 spin_lock_bh(&ifp->lock); 2447 now = jiffies; 2448 if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 2449 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 2450 else 2451 stored_lft = 0; 2452 if (!update_lft && !create && stored_lft) { 2453 const u32 minimum_lft = min_t(u32, 2454 stored_lft, MIN_VALID_LIFETIME); 2455 valid_lft = max(valid_lft, minimum_lft); 2456 2457 /* RFC4862 Section 5.5.3e: 2458 * "Note that the preferred lifetime of the 2459 * corresponding address is always reset to 2460 * the Preferred Lifetime in the received 2461 * Prefix Information option, regardless of 2462 * whether the valid lifetime is also reset or 2463 * ignored." 2464 * 2465 * So we should always update prefered_lft here. 2466 */ 2467 update_lft = 1; 2468 } 2469 2470 if (update_lft) { 2471 ifp->valid_lft = valid_lft; 2472 ifp->prefered_lft = prefered_lft; 2473 ifp->tstamp = now; 2474 flags = ifp->flags; 2475 ifp->flags &= ~IFA_F_DEPRECATED; 2476 spin_unlock_bh(&ifp->lock); 2477 2478 if (!(flags&IFA_F_TENTATIVE)) 2479 ipv6_ifa_notify(0, ifp); 2480 } else 2481 spin_unlock_bh(&ifp->lock); 2482 2483 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, 2484 create, now); 2485 2486 in6_ifa_put(ifp); 2487 addrconf_verify(); 2488 } 2489 } 2490 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 2491 in6_dev_put(in6_dev); 2492 } 2493 2494 /* 2495 * Set destination address. 2496 * Special case for SIT interfaces where we create a new "virtual" 2497 * device. 2498 */ 2499 int addrconf_set_dstaddr(struct net *net, void __user *arg) 2500 { 2501 struct in6_ifreq ireq; 2502 struct net_device *dev; 2503 int err = -EINVAL; 2504 2505 rtnl_lock(); 2506 2507 err = -EFAULT; 2508 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 2509 goto err_exit; 2510 2511 dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 2512 2513 err = -ENODEV; 2514 if (!dev) 2515 goto err_exit; 2516 2517 #if IS_ENABLED(CONFIG_IPV6_SIT) 2518 if (dev->type == ARPHRD_SIT) { 2519 const struct net_device_ops *ops = dev->netdev_ops; 2520 struct ifreq ifr; 2521 struct ip_tunnel_parm p; 2522 2523 err = -EADDRNOTAVAIL; 2524 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 2525 goto err_exit; 2526 2527 memset(&p, 0, sizeof(p)); 2528 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 2529 p.iph.saddr = 0; 2530 p.iph.version = 4; 2531 p.iph.ihl = 5; 2532 p.iph.protocol = IPPROTO_IPV6; 2533 p.iph.ttl = 64; 2534 ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 2535 2536 if (ops->ndo_do_ioctl) { 2537 mm_segment_t oldfs = get_fs(); 2538 2539 set_fs(KERNEL_DS); 2540 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 2541 set_fs(oldfs); 2542 } else 2543 err = -EOPNOTSUPP; 2544 2545 if (err == 0) { 2546 err = -ENOBUFS; 2547 dev = __dev_get_by_name(net, p.name); 2548 if (!dev) 2549 goto err_exit; 2550 err = dev_open(dev); 2551 } 2552 } 2553 #endif 2554 2555 err_exit: 2556 rtnl_unlock(); 2557 return err; 2558 } 2559 2560 static int ipv6_mc_config(struct sock *sk, bool join, 2561 const struct in6_addr *addr, int ifindex) 2562 { 2563 int ret; 2564 2565 ASSERT_RTNL(); 2566 2567 lock_sock(sk); 2568 if (join) 2569 ret = ipv6_sock_mc_join(sk, ifindex, addr); 2570 else 2571 ret = ipv6_sock_mc_drop(sk, ifindex, addr); 2572 release_sock(sk); 2573 2574 return ret; 2575 } 2576 2577 /* 2578 * Manual configuration of address on an interface 2579 */ 2580 static int inet6_addr_add(struct net *net, int ifindex, 2581 const struct in6_addr *pfx, 2582 const struct in6_addr *peer_pfx, 2583 unsigned int plen, __u32 ifa_flags, 2584 __u32 prefered_lft, __u32 valid_lft) 2585 { 2586 struct inet6_ifaddr *ifp; 2587 struct inet6_dev *idev; 2588 struct net_device *dev; 2589 unsigned long timeout; 2590 clock_t expires; 2591 int scope; 2592 u32 flags; 2593 2594 ASSERT_RTNL(); 2595 2596 if (plen > 128) 2597 return -EINVAL; 2598 2599 /* check the lifetime */ 2600 if (!valid_lft || prefered_lft > valid_lft) 2601 return -EINVAL; 2602 2603 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64) 2604 return -EINVAL; 2605 2606 dev = __dev_get_by_index(net, ifindex); 2607 if (!dev) 2608 return -ENODEV; 2609 2610 idev = addrconf_add_dev(dev); 2611 if (IS_ERR(idev)) 2612 return PTR_ERR(idev); 2613 2614 if (ifa_flags & IFA_F_MCAUTOJOIN) { 2615 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, 2616 true, pfx, ifindex); 2617 2618 if (ret < 0) 2619 return ret; 2620 } 2621 2622 scope = ipv6_addr_scope(pfx); 2623 2624 timeout = addrconf_timeout_fixup(valid_lft, HZ); 2625 if (addrconf_finite_timeout(timeout)) { 2626 expires = jiffies_to_clock_t(timeout * HZ); 2627 valid_lft = timeout; 2628 flags = RTF_EXPIRES; 2629 } else { 2630 expires = 0; 2631 flags = 0; 2632 ifa_flags |= IFA_F_PERMANENT; 2633 } 2634 2635 timeout = addrconf_timeout_fixup(prefered_lft, HZ); 2636 if (addrconf_finite_timeout(timeout)) { 2637 if (timeout == 0) 2638 ifa_flags |= IFA_F_DEPRECATED; 2639 prefered_lft = timeout; 2640 } 2641 2642 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags, 2643 valid_lft, prefered_lft); 2644 2645 if (!IS_ERR(ifp)) { 2646 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) { 2647 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 2648 expires, flags); 2649 } 2650 2651 /* 2652 * Note that section 3.1 of RFC 4429 indicates 2653 * that the Optimistic flag should not be set for 2654 * manually configured addresses 2655 */ 2656 addrconf_dad_start(ifp); 2657 if (ifa_flags & IFA_F_MANAGETEMPADDR) 2658 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft, 2659 true, jiffies); 2660 in6_ifa_put(ifp); 2661 addrconf_verify_rtnl(); 2662 return 0; 2663 } else if (ifa_flags & IFA_F_MCAUTOJOIN) { 2664 ipv6_mc_config(net->ipv6.mc_autojoin_sk, 2665 false, pfx, ifindex); 2666 } 2667 2668 return PTR_ERR(ifp); 2669 } 2670 2671 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, 2672 const struct in6_addr *pfx, unsigned int plen) 2673 { 2674 struct inet6_ifaddr *ifp; 2675 struct inet6_dev *idev; 2676 struct net_device *dev; 2677 2678 if (plen > 128) 2679 return -EINVAL; 2680 2681 dev = __dev_get_by_index(net, ifindex); 2682 if (!dev) 2683 return -ENODEV; 2684 2685 idev = __in6_dev_get(dev); 2686 if (!idev) 2687 return -ENXIO; 2688 2689 read_lock_bh(&idev->lock); 2690 list_for_each_entry(ifp, &idev->addr_list, if_list) { 2691 if (ifp->prefix_len == plen && 2692 ipv6_addr_equal(pfx, &ifp->addr)) { 2693 in6_ifa_hold(ifp); 2694 read_unlock_bh(&idev->lock); 2695 2696 if (!(ifp->flags & IFA_F_TEMPORARY) && 2697 (ifa_flags & IFA_F_MANAGETEMPADDR)) 2698 manage_tempaddrs(idev, ifp, 0, 0, false, 2699 jiffies); 2700 ipv6_del_addr(ifp); 2701 addrconf_verify_rtnl(); 2702 if (ipv6_addr_is_multicast(pfx)) { 2703 ipv6_mc_config(net->ipv6.mc_autojoin_sk, 2704 false, pfx, dev->ifindex); 2705 } 2706 return 0; 2707 } 2708 } 2709 read_unlock_bh(&idev->lock); 2710 return -EADDRNOTAVAIL; 2711 } 2712 2713 2714 int addrconf_add_ifaddr(struct net *net, void __user *arg) 2715 { 2716 struct in6_ifreq ireq; 2717 int err; 2718 2719 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 2720 return -EPERM; 2721 2722 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 2723 return -EFAULT; 2724 2725 rtnl_lock(); 2726 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL, 2727 ireq.ifr6_prefixlen, IFA_F_PERMANENT, 2728 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 2729 rtnl_unlock(); 2730 return err; 2731 } 2732 2733 int addrconf_del_ifaddr(struct net *net, void __user *arg) 2734 { 2735 struct in6_ifreq ireq; 2736 int err; 2737 2738 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 2739 return -EPERM; 2740 2741 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 2742 return -EFAULT; 2743 2744 rtnl_lock(); 2745 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, 2746 ireq.ifr6_prefixlen); 2747 rtnl_unlock(); 2748 return err; 2749 } 2750 2751 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 2752 int plen, int scope) 2753 { 2754 struct inet6_ifaddr *ifp; 2755 2756 ifp = ipv6_add_addr(idev, addr, NULL, plen, 2757 scope, IFA_F_PERMANENT, 2758 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 2759 if (!IS_ERR(ifp)) { 2760 spin_lock_bh(&ifp->lock); 2761 ifp->flags &= ~IFA_F_TENTATIVE; 2762 spin_unlock_bh(&ifp->lock); 2763 ipv6_ifa_notify(RTM_NEWADDR, ifp); 2764 in6_ifa_put(ifp); 2765 } 2766 } 2767 2768 #if IS_ENABLED(CONFIG_IPV6_SIT) 2769 static void sit_add_v4_addrs(struct inet6_dev *idev) 2770 { 2771 struct in6_addr addr; 2772 struct net_device *dev; 2773 struct net *net = dev_net(idev->dev); 2774 int scope, plen; 2775 u32 pflags = 0; 2776 2777 ASSERT_RTNL(); 2778 2779 memset(&addr, 0, sizeof(struct in6_addr)); 2780 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 2781 2782 if (idev->dev->flags&IFF_POINTOPOINT) { 2783 addr.s6_addr32[0] = htonl(0xfe800000); 2784 scope = IFA_LINK; 2785 plen = 64; 2786 } else { 2787 scope = IPV6_ADDR_COMPATv4; 2788 plen = 96; 2789 pflags |= RTF_NONEXTHOP; 2790 } 2791 2792 if (addr.s6_addr32[3]) { 2793 add_addr(idev, &addr, plen, scope); 2794 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags); 2795 return; 2796 } 2797 2798 for_each_netdev(net, dev) { 2799 struct in_device *in_dev = __in_dev_get_rtnl(dev); 2800 if (in_dev && (dev->flags & IFF_UP)) { 2801 struct in_ifaddr *ifa; 2802 2803 int flag = scope; 2804 2805 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 2806 2807 addr.s6_addr32[3] = ifa->ifa_local; 2808 2809 if (ifa->ifa_scope == RT_SCOPE_LINK) 2810 continue; 2811 if (ifa->ifa_scope >= RT_SCOPE_HOST) { 2812 if (idev->dev->flags&IFF_POINTOPOINT) 2813 continue; 2814 flag |= IFA_HOST; 2815 } 2816 2817 add_addr(idev, &addr, plen, flag); 2818 addrconf_prefix_route(&addr, plen, idev->dev, 0, 2819 pflags); 2820 } 2821 } 2822 } 2823 } 2824 #endif 2825 2826 static void init_loopback(struct net_device *dev) 2827 { 2828 struct inet6_dev *idev; 2829 struct net_device *sp_dev; 2830 struct inet6_ifaddr *sp_ifa; 2831 struct rt6_info *sp_rt; 2832 2833 /* ::1 */ 2834 2835 ASSERT_RTNL(); 2836 2837 idev = ipv6_find_idev(dev); 2838 if (!idev) { 2839 pr_debug("%s: add_dev failed\n", __func__); 2840 return; 2841 } 2842 2843 add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 2844 2845 /* Add routes to other interface's IPv6 addresses */ 2846 for_each_netdev(dev_net(dev), sp_dev) { 2847 if (!strcmp(sp_dev->name, dev->name)) 2848 continue; 2849 2850 idev = __in6_dev_get(sp_dev); 2851 if (!idev) 2852 continue; 2853 2854 read_lock_bh(&idev->lock); 2855 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) { 2856 2857 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE)) 2858 continue; 2859 2860 if (sp_ifa->rt) { 2861 /* This dst has been added to garbage list when 2862 * lo device down, release this obsolete dst and 2863 * reallocate a new router for ifa. 2864 */ 2865 if (sp_ifa->rt->dst.obsolete > 0) { 2866 ip6_rt_put(sp_ifa->rt); 2867 sp_ifa->rt = NULL; 2868 } else { 2869 continue; 2870 } 2871 } 2872 2873 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false); 2874 2875 /* Failure cases are ignored */ 2876 if (!IS_ERR(sp_rt)) { 2877 sp_ifa->rt = sp_rt; 2878 ip6_ins_rt(sp_rt); 2879 } 2880 } 2881 read_unlock_bh(&idev->lock); 2882 } 2883 } 2884 2885 static void addrconf_add_linklocal(struct inet6_dev *idev, 2886 const struct in6_addr *addr, u32 flags) 2887 { 2888 struct inet6_ifaddr *ifp; 2889 u32 addr_flags = flags | IFA_F_PERMANENT; 2890 2891 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 2892 if (idev->cnf.optimistic_dad && 2893 !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 2894 addr_flags |= IFA_F_OPTIMISTIC; 2895 #endif 2896 2897 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags, 2898 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 2899 if (!IS_ERR(ifp)) { 2900 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); 2901 addrconf_dad_start(ifp); 2902 in6_ifa_put(ifp); 2903 } 2904 } 2905 2906 static bool ipv6_reserved_interfaceid(struct in6_addr address) 2907 { 2908 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0) 2909 return true; 2910 2911 if (address.s6_addr32[2] == htonl(0x02005eff) && 2912 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000))) 2913 return true; 2914 2915 if (address.s6_addr32[2] == htonl(0xfdffffff) && 2916 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80))) 2917 return true; 2918 2919 return false; 2920 } 2921 2922 static int ipv6_generate_stable_address(struct in6_addr *address, 2923 u8 dad_count, 2924 const struct inet6_dev *idev) 2925 { 2926 static DEFINE_SPINLOCK(lock); 2927 static __u32 digest[SHA_DIGEST_WORDS]; 2928 static __u32 workspace[SHA_WORKSPACE_WORDS]; 2929 2930 static union { 2931 char __data[SHA_MESSAGE_BYTES]; 2932 struct { 2933 struct in6_addr secret; 2934 __be32 prefix[2]; 2935 unsigned char hwaddr[MAX_ADDR_LEN]; 2936 u8 dad_count; 2937 } __packed; 2938 } data; 2939 2940 struct in6_addr secret; 2941 struct in6_addr temp; 2942 struct net *net = dev_net(idev->dev); 2943 2944 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data)); 2945 2946 if (idev->cnf.stable_secret.initialized) 2947 secret = idev->cnf.stable_secret.secret; 2948 else if (net->ipv6.devconf_dflt->stable_secret.initialized) 2949 secret = net->ipv6.devconf_dflt->stable_secret.secret; 2950 else 2951 return -1; 2952 2953 retry: 2954 spin_lock_bh(&lock); 2955 2956 sha_init(digest); 2957 memset(&data, 0, sizeof(data)); 2958 memset(workspace, 0, sizeof(workspace)); 2959 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len); 2960 data.prefix[0] = address->s6_addr32[0]; 2961 data.prefix[1] = address->s6_addr32[1]; 2962 data.secret = secret; 2963 data.dad_count = dad_count; 2964 2965 sha_transform(digest, data.__data, workspace); 2966 2967 temp = *address; 2968 temp.s6_addr32[2] = (__force __be32)digest[0]; 2969 temp.s6_addr32[3] = (__force __be32)digest[1]; 2970 2971 spin_unlock_bh(&lock); 2972 2973 if (ipv6_reserved_interfaceid(temp)) { 2974 dad_count++; 2975 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries) 2976 return -1; 2977 goto retry; 2978 } 2979 2980 *address = temp; 2981 return 0; 2982 } 2983 2984 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) 2985 { 2986 struct in6_addr addr; 2987 2988 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 2989 2990 if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY) { 2991 if (!ipv6_generate_stable_address(&addr, 0, idev)) 2992 addrconf_add_linklocal(idev, &addr, 2993 IFA_F_STABLE_PRIVACY); 2994 else if (prefix_route) 2995 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); 2996 } else if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) { 2997 /* addrconf_add_linklocal also adds a prefix_route and we 2998 * only need to care about prefix routes if ipv6_generate_eui64 2999 * couldn't generate one. 3000 */ 3001 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) 3002 addrconf_add_linklocal(idev, &addr, 0); 3003 else if (prefix_route) 3004 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); 3005 } 3006 } 3007 3008 static void addrconf_dev_config(struct net_device *dev) 3009 { 3010 struct inet6_dev *idev; 3011 3012 ASSERT_RTNL(); 3013 3014 if ((dev->type != ARPHRD_ETHER) && 3015 (dev->type != ARPHRD_FDDI) && 3016 (dev->type != ARPHRD_ARCNET) && 3017 (dev->type != ARPHRD_INFINIBAND) && 3018 (dev->type != ARPHRD_IEEE802154) && 3019 (dev->type != ARPHRD_IEEE1394) && 3020 (dev->type != ARPHRD_TUNNEL6) && 3021 (dev->type != ARPHRD_6LOWPAN)) { 3022 /* Alas, we support only Ethernet autoconfiguration. */ 3023 return; 3024 } 3025 3026 idev = addrconf_add_dev(dev); 3027 if (IS_ERR(idev)) 3028 return; 3029 3030 addrconf_addr_gen(idev, false); 3031 } 3032 3033 #if IS_ENABLED(CONFIG_IPV6_SIT) 3034 static void addrconf_sit_config(struct net_device *dev) 3035 { 3036 struct inet6_dev *idev; 3037 3038 ASSERT_RTNL(); 3039 3040 /* 3041 * Configure the tunnel with one of our IPv4 3042 * addresses... we should configure all of 3043 * our v4 addrs in the tunnel 3044 */ 3045 3046 idev = ipv6_find_idev(dev); 3047 if (!idev) { 3048 pr_debug("%s: add_dev failed\n", __func__); 3049 return; 3050 } 3051 3052 if (dev->priv_flags & IFF_ISATAP) { 3053 addrconf_addr_gen(idev, false); 3054 return; 3055 } 3056 3057 sit_add_v4_addrs(idev); 3058 3059 if (dev->flags&IFF_POINTOPOINT) 3060 addrconf_add_mroute(dev); 3061 } 3062 #endif 3063 3064 #if IS_ENABLED(CONFIG_NET_IPGRE) 3065 static void addrconf_gre_config(struct net_device *dev) 3066 { 3067 struct inet6_dev *idev; 3068 3069 ASSERT_RTNL(); 3070 3071 idev = ipv6_find_idev(dev); 3072 if (!idev) { 3073 pr_debug("%s: add_dev failed\n", __func__); 3074 return; 3075 } 3076 3077 addrconf_addr_gen(idev, true); 3078 } 3079 #endif 3080 3081 static int addrconf_notify(struct notifier_block *this, unsigned long event, 3082 void *ptr) 3083 { 3084 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3085 struct inet6_dev *idev = __in6_dev_get(dev); 3086 int run_pending = 0; 3087 int err; 3088 3089 switch (event) { 3090 case NETDEV_REGISTER: 3091 if (!idev && dev->mtu >= IPV6_MIN_MTU) { 3092 idev = ipv6_add_dev(dev); 3093 if (IS_ERR(idev)) 3094 return notifier_from_errno(PTR_ERR(idev)); 3095 } 3096 break; 3097 3098 case NETDEV_UP: 3099 case NETDEV_CHANGE: 3100 if (dev->flags & IFF_SLAVE) 3101 break; 3102 3103 if (idev && idev->cnf.disable_ipv6) 3104 break; 3105 3106 if (event == NETDEV_UP) { 3107 if (!addrconf_qdisc_ok(dev)) { 3108 /* device is not ready yet. */ 3109 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 3110 dev->name); 3111 break; 3112 } 3113 3114 if (!idev && dev->mtu >= IPV6_MIN_MTU) 3115 idev = ipv6_add_dev(dev); 3116 3117 if (!IS_ERR_OR_NULL(idev)) { 3118 idev->if_flags |= IF_READY; 3119 run_pending = 1; 3120 } 3121 } else { 3122 if (!addrconf_qdisc_ok(dev)) { 3123 /* device is still not ready. */ 3124 break; 3125 } 3126 3127 if (idev) { 3128 if (idev->if_flags & IF_READY) 3129 /* device is already configured. */ 3130 break; 3131 idev->if_flags |= IF_READY; 3132 } 3133 3134 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 3135 dev->name); 3136 3137 run_pending = 1; 3138 } 3139 3140 switch (dev->type) { 3141 #if IS_ENABLED(CONFIG_IPV6_SIT) 3142 case ARPHRD_SIT: 3143 addrconf_sit_config(dev); 3144 break; 3145 #endif 3146 #if IS_ENABLED(CONFIG_NET_IPGRE) 3147 case ARPHRD_IPGRE: 3148 addrconf_gre_config(dev); 3149 break; 3150 #endif 3151 case ARPHRD_LOOPBACK: 3152 init_loopback(dev); 3153 break; 3154 3155 default: 3156 addrconf_dev_config(dev); 3157 break; 3158 } 3159 3160 if (!IS_ERR_OR_NULL(idev)) { 3161 if (run_pending) 3162 addrconf_dad_run(idev); 3163 3164 /* 3165 * If the MTU changed during the interface down, 3166 * when the interface up, the changed MTU must be 3167 * reflected in the idev as well as routers. 3168 */ 3169 if (idev->cnf.mtu6 != dev->mtu && 3170 dev->mtu >= IPV6_MIN_MTU) { 3171 rt6_mtu_change(dev, dev->mtu); 3172 idev->cnf.mtu6 = dev->mtu; 3173 } 3174 idev->tstamp = jiffies; 3175 inet6_ifinfo_notify(RTM_NEWLINK, idev); 3176 3177 /* 3178 * If the changed mtu during down is lower than 3179 * IPV6_MIN_MTU stop IPv6 on this interface. 3180 */ 3181 if (dev->mtu < IPV6_MIN_MTU) 3182 addrconf_ifdown(dev, 1); 3183 } 3184 break; 3185 3186 case NETDEV_CHANGEMTU: 3187 if (idev && dev->mtu >= IPV6_MIN_MTU) { 3188 rt6_mtu_change(dev, dev->mtu); 3189 idev->cnf.mtu6 = dev->mtu; 3190 break; 3191 } 3192 3193 if (!idev && dev->mtu >= IPV6_MIN_MTU) { 3194 idev = ipv6_add_dev(dev); 3195 if (!IS_ERR(idev)) 3196 break; 3197 } 3198 3199 /* 3200 * if MTU under IPV6_MIN_MTU. 3201 * Stop IPv6 on this interface. 3202 */ 3203 3204 case NETDEV_DOWN: 3205 case NETDEV_UNREGISTER: 3206 /* 3207 * Remove all addresses from this interface. 3208 */ 3209 addrconf_ifdown(dev, event != NETDEV_DOWN); 3210 break; 3211 3212 case NETDEV_CHANGENAME: 3213 if (idev) { 3214 snmp6_unregister_dev(idev); 3215 addrconf_sysctl_unregister(idev); 3216 err = addrconf_sysctl_register(idev); 3217 if (err) 3218 return notifier_from_errno(err); 3219 err = snmp6_register_dev(idev); 3220 if (err) { 3221 addrconf_sysctl_unregister(idev); 3222 return notifier_from_errno(err); 3223 } 3224 } 3225 break; 3226 3227 case NETDEV_PRE_TYPE_CHANGE: 3228 case NETDEV_POST_TYPE_CHANGE: 3229 addrconf_type_change(dev, event); 3230 break; 3231 } 3232 3233 return NOTIFY_OK; 3234 } 3235 3236 /* 3237 * addrconf module should be notified of a device going up 3238 */ 3239 static struct notifier_block ipv6_dev_notf = { 3240 .notifier_call = addrconf_notify, 3241 }; 3242 3243 static void addrconf_type_change(struct net_device *dev, unsigned long event) 3244 { 3245 struct inet6_dev *idev; 3246 ASSERT_RTNL(); 3247 3248 idev = __in6_dev_get(dev); 3249 3250 if (event == NETDEV_POST_TYPE_CHANGE) 3251 ipv6_mc_remap(idev); 3252 else if (event == NETDEV_PRE_TYPE_CHANGE) 3253 ipv6_mc_unmap(idev); 3254 } 3255 3256 static int addrconf_ifdown(struct net_device *dev, int how) 3257 { 3258 struct net *net = dev_net(dev); 3259 struct inet6_dev *idev; 3260 struct inet6_ifaddr *ifa; 3261 int state, i; 3262 3263 ASSERT_RTNL(); 3264 3265 rt6_ifdown(net, dev); 3266 neigh_ifdown(&nd_tbl, dev); 3267 3268 idev = __in6_dev_get(dev); 3269 if (!idev) 3270 return -ENODEV; 3271 3272 /* 3273 * Step 1: remove reference to ipv6 device from parent device. 3274 * Do not dev_put! 3275 */ 3276 if (how) { 3277 idev->dead = 1; 3278 3279 /* protected by rtnl_lock */ 3280 RCU_INIT_POINTER(dev->ip6_ptr, NULL); 3281 3282 /* Step 1.5: remove snmp6 entry */ 3283 snmp6_unregister_dev(idev); 3284 3285 } 3286 3287 /* Step 2: clear hash table */ 3288 for (i = 0; i < IN6_ADDR_HSIZE; i++) { 3289 struct hlist_head *h = &inet6_addr_lst[i]; 3290 3291 spin_lock_bh(&addrconf_hash_lock); 3292 restart: 3293 hlist_for_each_entry_rcu(ifa, h, addr_lst) { 3294 if (ifa->idev == idev) { 3295 hlist_del_init_rcu(&ifa->addr_lst); 3296 addrconf_del_dad_work(ifa); 3297 goto restart; 3298 } 3299 } 3300 spin_unlock_bh(&addrconf_hash_lock); 3301 } 3302 3303 write_lock_bh(&idev->lock); 3304 3305 addrconf_del_rs_timer(idev); 3306 3307 /* Step 2: clear flags for stateless addrconf */ 3308 if (!how) 3309 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 3310 3311 if (how && del_timer(&idev->regen_timer)) 3312 in6_dev_put(idev); 3313 3314 /* Step 3: clear tempaddr list */ 3315 while (!list_empty(&idev->tempaddr_list)) { 3316 ifa = list_first_entry(&idev->tempaddr_list, 3317 struct inet6_ifaddr, tmp_list); 3318 list_del(&ifa->tmp_list); 3319 write_unlock_bh(&idev->lock); 3320 spin_lock_bh(&ifa->lock); 3321 3322 if (ifa->ifpub) { 3323 in6_ifa_put(ifa->ifpub); 3324 ifa->ifpub = NULL; 3325 } 3326 spin_unlock_bh(&ifa->lock); 3327 in6_ifa_put(ifa); 3328 write_lock_bh(&idev->lock); 3329 } 3330 3331 while (!list_empty(&idev->addr_list)) { 3332 ifa = list_first_entry(&idev->addr_list, 3333 struct inet6_ifaddr, if_list); 3334 addrconf_del_dad_work(ifa); 3335 3336 list_del(&ifa->if_list); 3337 3338 write_unlock_bh(&idev->lock); 3339 3340 spin_lock_bh(&ifa->lock); 3341 state = ifa->state; 3342 ifa->state = INET6_IFADDR_STATE_DEAD; 3343 spin_unlock_bh(&ifa->lock); 3344 3345 if (state != INET6_IFADDR_STATE_DEAD) { 3346 __ipv6_ifa_notify(RTM_DELADDR, ifa); 3347 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); 3348 } 3349 in6_ifa_put(ifa); 3350 3351 write_lock_bh(&idev->lock); 3352 } 3353 3354 write_unlock_bh(&idev->lock); 3355 3356 /* Step 5: Discard anycast and multicast list */ 3357 if (how) { 3358 ipv6_ac_destroy_dev(idev); 3359 ipv6_mc_destroy_dev(idev); 3360 } else { 3361 ipv6_mc_down(idev); 3362 } 3363 3364 idev->tstamp = jiffies; 3365 3366 /* Last: Shot the device (if unregistered) */ 3367 if (how) { 3368 addrconf_sysctl_unregister(idev); 3369 neigh_parms_release(&nd_tbl, idev->nd_parms); 3370 neigh_ifdown(&nd_tbl, dev); 3371 in6_dev_put(idev); 3372 } 3373 return 0; 3374 } 3375 3376 static void addrconf_rs_timer(unsigned long data) 3377 { 3378 struct inet6_dev *idev = (struct inet6_dev *)data; 3379 struct net_device *dev = idev->dev; 3380 struct in6_addr lladdr; 3381 3382 write_lock(&idev->lock); 3383 if (idev->dead || !(idev->if_flags & IF_READY)) 3384 goto out; 3385 3386 if (!ipv6_accept_ra(idev)) 3387 goto out; 3388 3389 /* Announcement received after solicitation was sent */ 3390 if (idev->if_flags & IF_RA_RCVD) 3391 goto out; 3392 3393 if (idev->rs_probes++ < idev->cnf.rtr_solicits) { 3394 write_unlock(&idev->lock); 3395 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3396 ndisc_send_rs(dev, &lladdr, 3397 &in6addr_linklocal_allrouters); 3398 else 3399 goto put; 3400 3401 write_lock(&idev->lock); 3402 /* The wait after the last probe can be shorter */ 3403 addrconf_mod_rs_timer(idev, (idev->rs_probes == 3404 idev->cnf.rtr_solicits) ? 3405 idev->cnf.rtr_solicit_delay : 3406 idev->cnf.rtr_solicit_interval); 3407 } else { 3408 /* 3409 * Note: we do not support deprecated "all on-link" 3410 * assumption any longer. 3411 */ 3412 pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 3413 } 3414 3415 out: 3416 write_unlock(&idev->lock); 3417 put: 3418 in6_dev_put(idev); 3419 } 3420 3421 /* 3422 * Duplicate Address Detection 3423 */ 3424 static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 3425 { 3426 unsigned long rand_num; 3427 struct inet6_dev *idev = ifp->idev; 3428 3429 if (ifp->flags & IFA_F_OPTIMISTIC) 3430 rand_num = 0; 3431 else 3432 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1); 3433 3434 ifp->dad_probes = idev->cnf.dad_transmits; 3435 addrconf_mod_dad_work(ifp, rand_num); 3436 } 3437 3438 static void addrconf_dad_begin(struct inet6_ifaddr *ifp) 3439 { 3440 struct inet6_dev *idev = ifp->idev; 3441 struct net_device *dev = idev->dev; 3442 3443 addrconf_join_solict(dev, &ifp->addr); 3444 3445 prandom_seed((__force u32) ifp->addr.s6_addr32[3]); 3446 3447 read_lock_bh(&idev->lock); 3448 spin_lock(&ifp->lock); 3449 if (ifp->state == INET6_IFADDR_STATE_DEAD) 3450 goto out; 3451 3452 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 3453 idev->cnf.accept_dad < 1 || 3454 !(ifp->flags&IFA_F_TENTATIVE) || 3455 ifp->flags & IFA_F_NODAD) { 3456 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 3457 spin_unlock(&ifp->lock); 3458 read_unlock_bh(&idev->lock); 3459 3460 addrconf_dad_completed(ifp); 3461 return; 3462 } 3463 3464 if (!(idev->if_flags & IF_READY)) { 3465 spin_unlock(&ifp->lock); 3466 read_unlock_bh(&idev->lock); 3467 /* 3468 * If the device is not ready: 3469 * - keep it tentative if it is a permanent address. 3470 * - otherwise, kill it. 3471 */ 3472 in6_ifa_hold(ifp); 3473 addrconf_dad_stop(ifp, 0); 3474 return; 3475 } 3476 3477 /* 3478 * Optimistic nodes can start receiving 3479 * Frames right away 3480 */ 3481 if (ifp->flags & IFA_F_OPTIMISTIC) { 3482 ip6_ins_rt(ifp->rt); 3483 if (ipv6_use_optimistic_addr(idev)) { 3484 /* Because optimistic nodes can use this address, 3485 * notify listeners. If DAD fails, RTM_DELADDR is sent. 3486 */ 3487 ipv6_ifa_notify(RTM_NEWADDR, ifp); 3488 } 3489 } 3490 3491 addrconf_dad_kick(ifp); 3492 out: 3493 spin_unlock(&ifp->lock); 3494 read_unlock_bh(&idev->lock); 3495 } 3496 3497 static void addrconf_dad_start(struct inet6_ifaddr *ifp) 3498 { 3499 bool begin_dad = false; 3500 3501 spin_lock_bh(&ifp->lock); 3502 if (ifp->state != INET6_IFADDR_STATE_DEAD) { 3503 ifp->state = INET6_IFADDR_STATE_PREDAD; 3504 begin_dad = true; 3505 } 3506 spin_unlock_bh(&ifp->lock); 3507 3508 if (begin_dad) 3509 addrconf_mod_dad_work(ifp, 0); 3510 } 3511 3512 static void addrconf_dad_work(struct work_struct *w) 3513 { 3514 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), 3515 struct inet6_ifaddr, 3516 dad_work); 3517 struct inet6_dev *idev = ifp->idev; 3518 struct in6_addr mcaddr; 3519 3520 enum { 3521 DAD_PROCESS, 3522 DAD_BEGIN, 3523 DAD_ABORT, 3524 } action = DAD_PROCESS; 3525 3526 rtnl_lock(); 3527 3528 spin_lock_bh(&ifp->lock); 3529 if (ifp->state == INET6_IFADDR_STATE_PREDAD) { 3530 action = DAD_BEGIN; 3531 ifp->state = INET6_IFADDR_STATE_DAD; 3532 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { 3533 action = DAD_ABORT; 3534 ifp->state = INET6_IFADDR_STATE_POSTDAD; 3535 } 3536 spin_unlock_bh(&ifp->lock); 3537 3538 if (action == DAD_BEGIN) { 3539 addrconf_dad_begin(ifp); 3540 goto out; 3541 } else if (action == DAD_ABORT) { 3542 addrconf_dad_stop(ifp, 1); 3543 goto out; 3544 } 3545 3546 if (!ifp->dad_probes && addrconf_dad_end(ifp)) 3547 goto out; 3548 3549 write_lock_bh(&idev->lock); 3550 if (idev->dead || !(idev->if_flags & IF_READY)) { 3551 write_unlock_bh(&idev->lock); 3552 goto out; 3553 } 3554 3555 spin_lock(&ifp->lock); 3556 if (ifp->state == INET6_IFADDR_STATE_DEAD) { 3557 spin_unlock(&ifp->lock); 3558 write_unlock_bh(&idev->lock); 3559 goto out; 3560 } 3561 3562 if (ifp->dad_probes == 0) { 3563 /* 3564 * DAD was successful 3565 */ 3566 3567 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 3568 spin_unlock(&ifp->lock); 3569 write_unlock_bh(&idev->lock); 3570 3571 addrconf_dad_completed(ifp); 3572 3573 goto out; 3574 } 3575 3576 ifp->dad_probes--; 3577 addrconf_mod_dad_work(ifp, 3578 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME)); 3579 spin_unlock(&ifp->lock); 3580 write_unlock_bh(&idev->lock); 3581 3582 /* send a neighbour solicitation for our addr */ 3583 addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 3584 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any); 3585 out: 3586 in6_ifa_put(ifp); 3587 rtnl_unlock(); 3588 } 3589 3590 /* ifp->idev must be at least read locked */ 3591 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) 3592 { 3593 struct inet6_ifaddr *ifpiter; 3594 struct inet6_dev *idev = ifp->idev; 3595 3596 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { 3597 if (ifpiter->scope > IFA_LINK) 3598 break; 3599 if (ifp != ifpiter && ifpiter->scope == IFA_LINK && 3600 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| 3601 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == 3602 IFA_F_PERMANENT) 3603 return false; 3604 } 3605 return true; 3606 } 3607 3608 static void addrconf_dad_completed(struct inet6_ifaddr *ifp) 3609 { 3610 struct net_device *dev = ifp->idev->dev; 3611 struct in6_addr lladdr; 3612 bool send_rs, send_mld; 3613 3614 addrconf_del_dad_work(ifp); 3615 3616 /* 3617 * Configure the address for reception. Now it is valid. 3618 */ 3619 3620 ipv6_ifa_notify(RTM_NEWADDR, ifp); 3621 3622 /* If added prefix is link local and we are prepared to process 3623 router advertisements, start sending router solicitations. 3624 */ 3625 3626 read_lock_bh(&ifp->idev->lock); 3627 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); 3628 send_rs = send_mld && 3629 ipv6_accept_ra(ifp->idev) && 3630 ifp->idev->cnf.rtr_solicits > 0 && 3631 (dev->flags&IFF_LOOPBACK) == 0; 3632 read_unlock_bh(&ifp->idev->lock); 3633 3634 /* While dad is in progress mld report's source address is in6_addrany. 3635 * Resend with proper ll now. 3636 */ 3637 if (send_mld) 3638 ipv6_mc_dad_complete(ifp->idev); 3639 3640 if (send_rs) { 3641 /* 3642 * If a host as already performed a random delay 3643 * [...] as part of DAD [...] there is no need 3644 * to delay again before sending the first RS 3645 */ 3646 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3647 return; 3648 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); 3649 3650 write_lock_bh(&ifp->idev->lock); 3651 spin_lock(&ifp->lock); 3652 ifp->idev->rs_probes = 1; 3653 ifp->idev->if_flags |= IF_RS_SENT; 3654 addrconf_mod_rs_timer(ifp->idev, 3655 ifp->idev->cnf.rtr_solicit_interval); 3656 spin_unlock(&ifp->lock); 3657 write_unlock_bh(&ifp->idev->lock); 3658 } 3659 } 3660 3661 static void addrconf_dad_run(struct inet6_dev *idev) 3662 { 3663 struct inet6_ifaddr *ifp; 3664 3665 read_lock_bh(&idev->lock); 3666 list_for_each_entry(ifp, &idev->addr_list, if_list) { 3667 spin_lock(&ifp->lock); 3668 if (ifp->flags & IFA_F_TENTATIVE && 3669 ifp->state == INET6_IFADDR_STATE_DAD) 3670 addrconf_dad_kick(ifp); 3671 spin_unlock(&ifp->lock); 3672 } 3673 read_unlock_bh(&idev->lock); 3674 } 3675 3676 #ifdef CONFIG_PROC_FS 3677 struct if6_iter_state { 3678 struct seq_net_private p; 3679 int bucket; 3680 int offset; 3681 }; 3682 3683 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 3684 { 3685 struct inet6_ifaddr *ifa = NULL; 3686 struct if6_iter_state *state = seq->private; 3687 struct net *net = seq_file_net(seq); 3688 int p = 0; 3689 3690 /* initial bucket if pos is 0 */ 3691 if (pos == 0) { 3692 state->bucket = 0; 3693 state->offset = 0; 3694 } 3695 3696 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 3697 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket], 3698 addr_lst) { 3699 if (!net_eq(dev_net(ifa->idev->dev), net)) 3700 continue; 3701 /* sync with offset */ 3702 if (p < state->offset) { 3703 p++; 3704 continue; 3705 } 3706 state->offset++; 3707 return ifa; 3708 } 3709 3710 /* prepare for next bucket */ 3711 state->offset = 0; 3712 p = 0; 3713 } 3714 return NULL; 3715 } 3716 3717 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 3718 struct inet6_ifaddr *ifa) 3719 { 3720 struct if6_iter_state *state = seq->private; 3721 struct net *net = seq_file_net(seq); 3722 3723 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) { 3724 if (!net_eq(dev_net(ifa->idev->dev), net)) 3725 continue; 3726 state->offset++; 3727 return ifa; 3728 } 3729 3730 while (++state->bucket < IN6_ADDR_HSIZE) { 3731 state->offset = 0; 3732 hlist_for_each_entry_rcu_bh(ifa, 3733 &inet6_addr_lst[state->bucket], addr_lst) { 3734 if (!net_eq(dev_net(ifa->idev->dev), net)) 3735 continue; 3736 state->offset++; 3737 return ifa; 3738 } 3739 } 3740 3741 return NULL; 3742 } 3743 3744 static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 3745 __acquires(rcu_bh) 3746 { 3747 rcu_read_lock_bh(); 3748 return if6_get_first(seq, *pos); 3749 } 3750 3751 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 3752 { 3753 struct inet6_ifaddr *ifa; 3754 3755 ifa = if6_get_next(seq, v); 3756 ++*pos; 3757 return ifa; 3758 } 3759 3760 static void if6_seq_stop(struct seq_file *seq, void *v) 3761 __releases(rcu_bh) 3762 { 3763 rcu_read_unlock_bh(); 3764 } 3765 3766 static int if6_seq_show(struct seq_file *seq, void *v) 3767 { 3768 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 3769 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 3770 &ifp->addr, 3771 ifp->idev->dev->ifindex, 3772 ifp->prefix_len, 3773 ifp->scope, 3774 (u8) ifp->flags, 3775 ifp->idev->dev->name); 3776 return 0; 3777 } 3778 3779 static const struct seq_operations if6_seq_ops = { 3780 .start = if6_seq_start, 3781 .next = if6_seq_next, 3782 .show = if6_seq_show, 3783 .stop = if6_seq_stop, 3784 }; 3785 3786 static int if6_seq_open(struct inode *inode, struct file *file) 3787 { 3788 return seq_open_net(inode, file, &if6_seq_ops, 3789 sizeof(struct if6_iter_state)); 3790 } 3791 3792 static const struct file_operations if6_fops = { 3793 .owner = THIS_MODULE, 3794 .open = if6_seq_open, 3795 .read = seq_read, 3796 .llseek = seq_lseek, 3797 .release = seq_release_net, 3798 }; 3799 3800 static int __net_init if6_proc_net_init(struct net *net) 3801 { 3802 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops)) 3803 return -ENOMEM; 3804 return 0; 3805 } 3806 3807 static void __net_exit if6_proc_net_exit(struct net *net) 3808 { 3809 remove_proc_entry("if_inet6", net->proc_net); 3810 } 3811 3812 static struct pernet_operations if6_proc_net_ops = { 3813 .init = if6_proc_net_init, 3814 .exit = if6_proc_net_exit, 3815 }; 3816 3817 int __init if6_proc_init(void) 3818 { 3819 return register_pernet_subsys(&if6_proc_net_ops); 3820 } 3821 3822 void if6_proc_exit(void) 3823 { 3824 unregister_pernet_subsys(&if6_proc_net_ops); 3825 } 3826 #endif /* CONFIG_PROC_FS */ 3827 3828 #if IS_ENABLED(CONFIG_IPV6_MIP6) 3829 /* Check if address is a home address configured on any interface. */ 3830 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 3831 { 3832 int ret = 0; 3833 struct inet6_ifaddr *ifp = NULL; 3834 unsigned int hash = inet6_addr_hash(addr); 3835 3836 rcu_read_lock_bh(); 3837 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 3838 if (!net_eq(dev_net(ifp->idev->dev), net)) 3839 continue; 3840 if (ipv6_addr_equal(&ifp->addr, addr) && 3841 (ifp->flags & IFA_F_HOMEADDRESS)) { 3842 ret = 1; 3843 break; 3844 } 3845 } 3846 rcu_read_unlock_bh(); 3847 return ret; 3848 } 3849 #endif 3850 3851 /* 3852 * Periodic address status verification 3853 */ 3854 3855 static void addrconf_verify_rtnl(void) 3856 { 3857 unsigned long now, next, next_sec, next_sched; 3858 struct inet6_ifaddr *ifp; 3859 int i; 3860 3861 ASSERT_RTNL(); 3862 3863 rcu_read_lock_bh(); 3864 now = jiffies; 3865 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 3866 3867 cancel_delayed_work(&addr_chk_work); 3868 3869 for (i = 0; i < IN6_ADDR_HSIZE; i++) { 3870 restart: 3871 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) { 3872 unsigned long age; 3873 3874 /* When setting preferred_lft to a value not zero or 3875 * infinity, while valid_lft is infinity 3876 * IFA_F_PERMANENT has a non-infinity life time. 3877 */ 3878 if ((ifp->flags & IFA_F_PERMANENT) && 3879 (ifp->prefered_lft == INFINITY_LIFE_TIME)) 3880 continue; 3881 3882 spin_lock(&ifp->lock); 3883 /* We try to batch several events at once. */ 3884 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 3885 3886 if (ifp->valid_lft != INFINITY_LIFE_TIME && 3887 age >= ifp->valid_lft) { 3888 spin_unlock(&ifp->lock); 3889 in6_ifa_hold(ifp); 3890 ipv6_del_addr(ifp); 3891 goto restart; 3892 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 3893 spin_unlock(&ifp->lock); 3894 continue; 3895 } else if (age >= ifp->prefered_lft) { 3896 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 3897 int deprecate = 0; 3898 3899 if (!(ifp->flags&IFA_F_DEPRECATED)) { 3900 deprecate = 1; 3901 ifp->flags |= IFA_F_DEPRECATED; 3902 } 3903 3904 if ((ifp->valid_lft != INFINITY_LIFE_TIME) && 3905 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) 3906 next = ifp->tstamp + ifp->valid_lft * HZ; 3907 3908 spin_unlock(&ifp->lock); 3909 3910 if (deprecate) { 3911 in6_ifa_hold(ifp); 3912 3913 ipv6_ifa_notify(0, ifp); 3914 in6_ifa_put(ifp); 3915 goto restart; 3916 } 3917 } else if ((ifp->flags&IFA_F_TEMPORARY) && 3918 !(ifp->flags&IFA_F_TENTATIVE)) { 3919 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 3920 ifp->idev->cnf.dad_transmits * 3921 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ; 3922 3923 if (age >= ifp->prefered_lft - regen_advance) { 3924 struct inet6_ifaddr *ifpub = ifp->ifpub; 3925 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 3926 next = ifp->tstamp + ifp->prefered_lft * HZ; 3927 if (!ifp->regen_count && ifpub) { 3928 ifp->regen_count++; 3929 in6_ifa_hold(ifp); 3930 in6_ifa_hold(ifpub); 3931 spin_unlock(&ifp->lock); 3932 3933 spin_lock(&ifpub->lock); 3934 ifpub->regen_count = 0; 3935 spin_unlock(&ifpub->lock); 3936 ipv6_create_tempaddr(ifpub, ifp); 3937 in6_ifa_put(ifpub); 3938 in6_ifa_put(ifp); 3939 goto restart; 3940 } 3941 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 3942 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 3943 spin_unlock(&ifp->lock); 3944 } else { 3945 /* ifp->prefered_lft <= ifp->valid_lft */ 3946 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 3947 next = ifp->tstamp + ifp->prefered_lft * HZ; 3948 spin_unlock(&ifp->lock); 3949 } 3950 } 3951 } 3952 3953 next_sec = round_jiffies_up(next); 3954 next_sched = next; 3955 3956 /* If rounded timeout is accurate enough, accept it. */ 3957 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 3958 next_sched = next_sec; 3959 3960 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 3961 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 3962 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 3963 3964 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 3965 now, next, next_sec, next_sched); 3966 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now); 3967 rcu_read_unlock_bh(); 3968 } 3969 3970 static void addrconf_verify_work(struct work_struct *w) 3971 { 3972 rtnl_lock(); 3973 addrconf_verify_rtnl(); 3974 rtnl_unlock(); 3975 } 3976 3977 static void addrconf_verify(void) 3978 { 3979 mod_delayed_work(addrconf_wq, &addr_chk_work, 0); 3980 } 3981 3982 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 3983 struct in6_addr **peer_pfx) 3984 { 3985 struct in6_addr *pfx = NULL; 3986 3987 *peer_pfx = NULL; 3988 3989 if (addr) 3990 pfx = nla_data(addr); 3991 3992 if (local) { 3993 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 3994 *peer_pfx = pfx; 3995 pfx = nla_data(local); 3996 } 3997 3998 return pfx; 3999 } 4000 4001 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 4002 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 4003 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 4004 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 4005 [IFA_FLAGS] = { .len = sizeof(u32) }, 4006 }; 4007 4008 static int 4009 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh) 4010 { 4011 struct net *net = sock_net(skb->sk); 4012 struct ifaddrmsg *ifm; 4013 struct nlattr *tb[IFA_MAX+1]; 4014 struct in6_addr *pfx, *peer_pfx; 4015 u32 ifa_flags; 4016 int err; 4017 4018 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 4019 if (err < 0) 4020 return err; 4021 4022 ifm = nlmsg_data(nlh); 4023 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 4024 if (!pfx) 4025 return -EINVAL; 4026 4027 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 4028 4029 /* We ignore other flags so far. */ 4030 ifa_flags &= IFA_F_MANAGETEMPADDR; 4031 4032 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, 4033 ifm->ifa_prefixlen); 4034 } 4035 4036 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags, 4037 u32 prefered_lft, u32 valid_lft) 4038 { 4039 u32 flags; 4040 clock_t expires; 4041 unsigned long timeout; 4042 bool was_managetempaddr; 4043 bool had_prefixroute; 4044 4045 ASSERT_RTNL(); 4046 4047 if (!valid_lft || (prefered_lft > valid_lft)) 4048 return -EINVAL; 4049 4050 if (ifa_flags & IFA_F_MANAGETEMPADDR && 4051 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) 4052 return -EINVAL; 4053 4054 timeout = addrconf_timeout_fixup(valid_lft, HZ); 4055 if (addrconf_finite_timeout(timeout)) { 4056 expires = jiffies_to_clock_t(timeout * HZ); 4057 valid_lft = timeout; 4058 flags = RTF_EXPIRES; 4059 } else { 4060 expires = 0; 4061 flags = 0; 4062 ifa_flags |= IFA_F_PERMANENT; 4063 } 4064 4065 timeout = addrconf_timeout_fixup(prefered_lft, HZ); 4066 if (addrconf_finite_timeout(timeout)) { 4067 if (timeout == 0) 4068 ifa_flags |= IFA_F_DEPRECATED; 4069 prefered_lft = timeout; 4070 } 4071 4072 spin_lock_bh(&ifp->lock); 4073 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; 4074 had_prefixroute = ifp->flags & IFA_F_PERMANENT && 4075 !(ifp->flags & IFA_F_NOPREFIXROUTE); 4076 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | 4077 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4078 IFA_F_NOPREFIXROUTE); 4079 ifp->flags |= ifa_flags; 4080 ifp->tstamp = jiffies; 4081 ifp->valid_lft = valid_lft; 4082 ifp->prefered_lft = prefered_lft; 4083 4084 spin_unlock_bh(&ifp->lock); 4085 if (!(ifp->flags&IFA_F_TENTATIVE)) 4086 ipv6_ifa_notify(0, ifp); 4087 4088 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) { 4089 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, 4090 expires, flags); 4091 } else if (had_prefixroute) { 4092 enum cleanup_prefix_rt_t action; 4093 unsigned long rt_expires; 4094 4095 write_lock_bh(&ifp->idev->lock); 4096 action = check_cleanup_prefix_route(ifp, &rt_expires); 4097 write_unlock_bh(&ifp->idev->lock); 4098 4099 if (action != CLEANUP_PREFIX_RT_NOP) { 4100 cleanup_prefix_route(ifp, rt_expires, 4101 action == CLEANUP_PREFIX_RT_DEL); 4102 } 4103 } 4104 4105 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { 4106 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR)) 4107 valid_lft = prefered_lft = 0; 4108 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft, 4109 !was_managetempaddr, jiffies); 4110 } 4111 4112 addrconf_verify_rtnl(); 4113 4114 return 0; 4115 } 4116 4117 static int 4118 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh) 4119 { 4120 struct net *net = sock_net(skb->sk); 4121 struct ifaddrmsg *ifm; 4122 struct nlattr *tb[IFA_MAX+1]; 4123 struct in6_addr *pfx, *peer_pfx; 4124 struct inet6_ifaddr *ifa; 4125 struct net_device *dev; 4126 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; 4127 u32 ifa_flags; 4128 int err; 4129 4130 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 4131 if (err < 0) 4132 return err; 4133 4134 ifm = nlmsg_data(nlh); 4135 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 4136 if (!pfx) 4137 return -EINVAL; 4138 4139 if (tb[IFA_CACHEINFO]) { 4140 struct ifa_cacheinfo *ci; 4141 4142 ci = nla_data(tb[IFA_CACHEINFO]); 4143 valid_lft = ci->ifa_valid; 4144 preferred_lft = ci->ifa_prefered; 4145 } else { 4146 preferred_lft = INFINITY_LIFE_TIME; 4147 valid_lft = INFINITY_LIFE_TIME; 4148 } 4149 4150 dev = __dev_get_by_index(net, ifm->ifa_index); 4151 if (!dev) 4152 return -ENODEV; 4153 4154 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 4155 4156 /* We ignore other flags so far. */ 4157 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4158 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN; 4159 4160 ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 4161 if (!ifa) { 4162 /* 4163 * It would be best to check for !NLM_F_CREATE here but 4164 * userspace already relies on not having to provide this. 4165 */ 4166 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx, 4167 ifm->ifa_prefixlen, ifa_flags, 4168 preferred_lft, valid_lft); 4169 } 4170 4171 if (nlh->nlmsg_flags & NLM_F_EXCL || 4172 !(nlh->nlmsg_flags & NLM_F_REPLACE)) 4173 err = -EEXIST; 4174 else 4175 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); 4176 4177 in6_ifa_put(ifa); 4178 4179 return err; 4180 } 4181 4182 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, 4183 u8 scope, int ifindex) 4184 { 4185 struct ifaddrmsg *ifm; 4186 4187 ifm = nlmsg_data(nlh); 4188 ifm->ifa_family = AF_INET6; 4189 ifm->ifa_prefixlen = prefixlen; 4190 ifm->ifa_flags = flags; 4191 ifm->ifa_scope = scope; 4192 ifm->ifa_index = ifindex; 4193 } 4194 4195 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 4196 unsigned long tstamp, u32 preferred, u32 valid) 4197 { 4198 struct ifa_cacheinfo ci; 4199 4200 ci.cstamp = cstamp_delta(cstamp); 4201 ci.tstamp = cstamp_delta(tstamp); 4202 ci.ifa_prefered = preferred; 4203 ci.ifa_valid = valid; 4204 4205 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 4206 } 4207 4208 static inline int rt_scope(int ifa_scope) 4209 { 4210 if (ifa_scope & IFA_HOST) 4211 return RT_SCOPE_HOST; 4212 else if (ifa_scope & IFA_LINK) 4213 return RT_SCOPE_LINK; 4214 else if (ifa_scope & IFA_SITE) 4215 return RT_SCOPE_SITE; 4216 else 4217 return RT_SCOPE_UNIVERSE; 4218 } 4219 4220 static inline int inet6_ifaddr_msgsize(void) 4221 { 4222 return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 4223 + nla_total_size(16) /* IFA_LOCAL */ 4224 + nla_total_size(16) /* IFA_ADDRESS */ 4225 + nla_total_size(sizeof(struct ifa_cacheinfo)) 4226 + nla_total_size(4) /* IFA_FLAGS */; 4227 } 4228 4229 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 4230 u32 portid, u32 seq, int event, unsigned int flags) 4231 { 4232 struct nlmsghdr *nlh; 4233 u32 preferred, valid; 4234 4235 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 4236 if (!nlh) 4237 return -EMSGSIZE; 4238 4239 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 4240 ifa->idev->dev->ifindex); 4241 4242 if (!((ifa->flags&IFA_F_PERMANENT) && 4243 (ifa->prefered_lft == INFINITY_LIFE_TIME))) { 4244 preferred = ifa->prefered_lft; 4245 valid = ifa->valid_lft; 4246 if (preferred != INFINITY_LIFE_TIME) { 4247 long tval = (jiffies - ifa->tstamp)/HZ; 4248 if (preferred > tval) 4249 preferred -= tval; 4250 else 4251 preferred = 0; 4252 if (valid != INFINITY_LIFE_TIME) { 4253 if (valid > tval) 4254 valid -= tval; 4255 else 4256 valid = 0; 4257 } 4258 } 4259 } else { 4260 preferred = INFINITY_LIFE_TIME; 4261 valid = INFINITY_LIFE_TIME; 4262 } 4263 4264 if (!ipv6_addr_any(&ifa->peer_addr)) { 4265 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || 4266 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) 4267 goto error; 4268 } else 4269 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) 4270 goto error; 4271 4272 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 4273 goto error; 4274 4275 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) 4276 goto error; 4277 4278 nlmsg_end(skb, nlh); 4279 return 0; 4280 4281 error: 4282 nlmsg_cancel(skb, nlh); 4283 return -EMSGSIZE; 4284 } 4285 4286 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 4287 u32 portid, u32 seq, int event, u16 flags) 4288 { 4289 struct nlmsghdr *nlh; 4290 u8 scope = RT_SCOPE_UNIVERSE; 4291 int ifindex = ifmca->idev->dev->ifindex; 4292 4293 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 4294 scope = RT_SCOPE_SITE; 4295 4296 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 4297 if (!nlh) 4298 return -EMSGSIZE; 4299 4300 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4301 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || 4302 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 4303 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 4304 nlmsg_cancel(skb, nlh); 4305 return -EMSGSIZE; 4306 } 4307 4308 nlmsg_end(skb, nlh); 4309 return 0; 4310 } 4311 4312 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 4313 u32 portid, u32 seq, int event, unsigned int flags) 4314 { 4315 struct nlmsghdr *nlh; 4316 u8 scope = RT_SCOPE_UNIVERSE; 4317 int ifindex = ifaca->aca_idev->dev->ifindex; 4318 4319 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 4320 scope = RT_SCOPE_SITE; 4321 4322 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 4323 if (!nlh) 4324 return -EMSGSIZE; 4325 4326 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4327 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || 4328 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 4329 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 4330 nlmsg_cancel(skb, nlh); 4331 return -EMSGSIZE; 4332 } 4333 4334 nlmsg_end(skb, nlh); 4335 return 0; 4336 } 4337 4338 enum addr_type_t { 4339 UNICAST_ADDR, 4340 MULTICAST_ADDR, 4341 ANYCAST_ADDR, 4342 }; 4343 4344 /* called with rcu_read_lock() */ 4345 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 4346 struct netlink_callback *cb, enum addr_type_t type, 4347 int s_ip_idx, int *p_ip_idx) 4348 { 4349 struct ifmcaddr6 *ifmca; 4350 struct ifacaddr6 *ifaca; 4351 int err = 1; 4352 int ip_idx = *p_ip_idx; 4353 4354 read_lock_bh(&idev->lock); 4355 switch (type) { 4356 case UNICAST_ADDR: { 4357 struct inet6_ifaddr *ifa; 4358 4359 /* unicast address incl. temp addr */ 4360 list_for_each_entry(ifa, &idev->addr_list, if_list) { 4361 if (++ip_idx < s_ip_idx) 4362 continue; 4363 err = inet6_fill_ifaddr(skb, ifa, 4364 NETLINK_CB(cb->skb).portid, 4365 cb->nlh->nlmsg_seq, 4366 RTM_NEWADDR, 4367 NLM_F_MULTI); 4368 if (err < 0) 4369 break; 4370 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 4371 } 4372 break; 4373 } 4374 case MULTICAST_ADDR: 4375 /* multicast address */ 4376 for (ifmca = idev->mc_list; ifmca; 4377 ifmca = ifmca->next, ip_idx++) { 4378 if (ip_idx < s_ip_idx) 4379 continue; 4380 err = inet6_fill_ifmcaddr(skb, ifmca, 4381 NETLINK_CB(cb->skb).portid, 4382 cb->nlh->nlmsg_seq, 4383 RTM_GETMULTICAST, 4384 NLM_F_MULTI); 4385 if (err < 0) 4386 break; 4387 } 4388 break; 4389 case ANYCAST_ADDR: 4390 /* anycast address */ 4391 for (ifaca = idev->ac_list; ifaca; 4392 ifaca = ifaca->aca_next, ip_idx++) { 4393 if (ip_idx < s_ip_idx) 4394 continue; 4395 err = inet6_fill_ifacaddr(skb, ifaca, 4396 NETLINK_CB(cb->skb).portid, 4397 cb->nlh->nlmsg_seq, 4398 RTM_GETANYCAST, 4399 NLM_F_MULTI); 4400 if (err < 0) 4401 break; 4402 } 4403 break; 4404 default: 4405 break; 4406 } 4407 read_unlock_bh(&idev->lock); 4408 *p_ip_idx = ip_idx; 4409 return err; 4410 } 4411 4412 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 4413 enum addr_type_t type) 4414 { 4415 struct net *net = sock_net(skb->sk); 4416 int h, s_h; 4417 int idx, ip_idx; 4418 int s_idx, s_ip_idx; 4419 struct net_device *dev; 4420 struct inet6_dev *idev; 4421 struct hlist_head *head; 4422 4423 s_h = cb->args[0]; 4424 s_idx = idx = cb->args[1]; 4425 s_ip_idx = ip_idx = cb->args[2]; 4426 4427 rcu_read_lock(); 4428 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq; 4429 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4430 idx = 0; 4431 head = &net->dev_index_head[h]; 4432 hlist_for_each_entry_rcu(dev, head, index_hlist) { 4433 if (idx < s_idx) 4434 goto cont; 4435 if (h > s_h || idx > s_idx) 4436 s_ip_idx = 0; 4437 ip_idx = 0; 4438 idev = __in6_dev_get(dev); 4439 if (!idev) 4440 goto cont; 4441 4442 if (in6_dump_addrs(idev, skb, cb, type, 4443 s_ip_idx, &ip_idx) < 0) 4444 goto done; 4445 cont: 4446 idx++; 4447 } 4448 } 4449 done: 4450 rcu_read_unlock(); 4451 cb->args[0] = h; 4452 cb->args[1] = idx; 4453 cb->args[2] = ip_idx; 4454 4455 return skb->len; 4456 } 4457 4458 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 4459 { 4460 enum addr_type_t type = UNICAST_ADDR; 4461 4462 return inet6_dump_addr(skb, cb, type); 4463 } 4464 4465 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 4466 { 4467 enum addr_type_t type = MULTICAST_ADDR; 4468 4469 return inet6_dump_addr(skb, cb, type); 4470 } 4471 4472 4473 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 4474 { 4475 enum addr_type_t type = ANYCAST_ADDR; 4476 4477 return inet6_dump_addr(skb, cb, type); 4478 } 4479 4480 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh) 4481 { 4482 struct net *net = sock_net(in_skb->sk); 4483 struct ifaddrmsg *ifm; 4484 struct nlattr *tb[IFA_MAX+1]; 4485 struct in6_addr *addr = NULL, *peer; 4486 struct net_device *dev = NULL; 4487 struct inet6_ifaddr *ifa; 4488 struct sk_buff *skb; 4489 int err; 4490 4491 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 4492 if (err < 0) 4493 goto errout; 4494 4495 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 4496 if (!addr) { 4497 err = -EINVAL; 4498 goto errout; 4499 } 4500 4501 ifm = nlmsg_data(nlh); 4502 if (ifm->ifa_index) 4503 dev = __dev_get_by_index(net, ifm->ifa_index); 4504 4505 ifa = ipv6_get_ifaddr(net, addr, dev, 1); 4506 if (!ifa) { 4507 err = -EADDRNOTAVAIL; 4508 goto errout; 4509 } 4510 4511 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 4512 if (!skb) { 4513 err = -ENOBUFS; 4514 goto errout_ifa; 4515 } 4516 4517 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid, 4518 nlh->nlmsg_seq, RTM_NEWADDR, 0); 4519 if (err < 0) { 4520 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 4521 WARN_ON(err == -EMSGSIZE); 4522 kfree_skb(skb); 4523 goto errout_ifa; 4524 } 4525 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 4526 errout_ifa: 4527 in6_ifa_put(ifa); 4528 errout: 4529 return err; 4530 } 4531 4532 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 4533 { 4534 struct sk_buff *skb; 4535 struct net *net = dev_net(ifa->idev->dev); 4536 int err = -ENOBUFS; 4537 4538 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 4539 if (!skb) 4540 goto errout; 4541 4542 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 4543 if (err < 0) { 4544 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 4545 WARN_ON(err == -EMSGSIZE); 4546 kfree_skb(skb); 4547 goto errout; 4548 } 4549 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 4550 return; 4551 errout: 4552 if (err < 0) 4553 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 4554 } 4555 4556 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 4557 __s32 *array, int bytes) 4558 { 4559 BUG_ON(bytes < (DEVCONF_MAX * 4)); 4560 4561 memset(array, 0, bytes); 4562 array[DEVCONF_FORWARDING] = cnf->forwarding; 4563 array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 4564 array[DEVCONF_MTU6] = cnf->mtu6; 4565 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 4566 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 4567 array[DEVCONF_AUTOCONF] = cnf->autoconf; 4568 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 4569 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 4570 array[DEVCONF_RTR_SOLICIT_INTERVAL] = 4571 jiffies_to_msecs(cnf->rtr_solicit_interval); 4572 array[DEVCONF_RTR_SOLICIT_DELAY] = 4573 jiffies_to_msecs(cnf->rtr_solicit_delay); 4574 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 4575 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = 4576 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 4577 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 4578 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 4579 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 4580 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 4581 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 4582 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 4583 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 4584 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 4585 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 4586 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 4587 #ifdef CONFIG_IPV6_ROUTER_PREF 4588 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 4589 array[DEVCONF_RTR_PROBE_INTERVAL] = 4590 jiffies_to_msecs(cnf->rtr_probe_interval); 4591 #ifdef CONFIG_IPV6_ROUTE_INFO 4592 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 4593 #endif 4594 #endif 4595 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 4596 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 4597 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 4598 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 4599 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; 4600 #endif 4601 #ifdef CONFIG_IPV6_MROUTE 4602 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 4603 #endif 4604 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 4605 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 4606 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 4607 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 4608 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; 4609 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; 4610 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; 4611 /* we omit DEVCONF_STABLE_SECRET for now */ 4612 } 4613 4614 static inline size_t inet6_ifla6_size(void) 4615 { 4616 return nla_total_size(4) /* IFLA_INET6_FLAGS */ 4617 + nla_total_size(sizeof(struct ifla_cacheinfo)) 4618 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 4619 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 4620 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 4621 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */ 4622 } 4623 4624 static inline size_t inet6_if_nlmsg_size(void) 4625 { 4626 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 4627 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 4628 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 4629 + nla_total_size(4) /* IFLA_MTU */ 4630 + nla_total_size(4) /* IFLA_LINK */ 4631 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 4632 } 4633 4634 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 4635 int items, int bytes) 4636 { 4637 int i; 4638 int pad = bytes - sizeof(u64) * items; 4639 BUG_ON(pad < 0); 4640 4641 /* Use put_unaligned() because stats may not be aligned for u64. */ 4642 put_unaligned(items, &stats[0]); 4643 for (i = 1; i < items; i++) 4644 put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 4645 4646 memset(&stats[items], 0, pad); 4647 } 4648 4649 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, 4650 int items, int bytes, size_t syncpoff) 4651 { 4652 int i; 4653 int pad = bytes - sizeof(u64) * items; 4654 BUG_ON(pad < 0); 4655 4656 /* Use put_unaligned() because stats may not be aligned for u64. */ 4657 put_unaligned(items, &stats[0]); 4658 for (i = 1; i < items; i++) 4659 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]); 4660 4661 memset(&stats[items], 0, pad); 4662 } 4663 4664 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 4665 int bytes) 4666 { 4667 switch (attrtype) { 4668 case IFLA_INET6_STATS: 4669 __snmp6_fill_stats64(stats, idev->stats.ipv6, 4670 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp)); 4671 break; 4672 case IFLA_INET6_ICMP6STATS: 4673 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes); 4674 break; 4675 } 4676 } 4677 4678 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev) 4679 { 4680 struct nlattr *nla; 4681 struct ifla_cacheinfo ci; 4682 4683 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 4684 goto nla_put_failure; 4685 ci.max_reasm_len = IPV6_MAXPLEN; 4686 ci.tstamp = cstamp_delta(idev->tstamp); 4687 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 4688 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); 4689 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 4690 goto nla_put_failure; 4691 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 4692 if (!nla) 4693 goto nla_put_failure; 4694 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 4695 4696 /* XXX - MC not implemented */ 4697 4698 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 4699 if (!nla) 4700 goto nla_put_failure; 4701 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 4702 4703 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 4704 if (!nla) 4705 goto nla_put_failure; 4706 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 4707 4708 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 4709 if (!nla) 4710 goto nla_put_failure; 4711 4712 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode)) 4713 goto nla_put_failure; 4714 4715 read_lock_bh(&idev->lock); 4716 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 4717 read_unlock_bh(&idev->lock); 4718 4719 return 0; 4720 4721 nla_put_failure: 4722 return -EMSGSIZE; 4723 } 4724 4725 static size_t inet6_get_link_af_size(const struct net_device *dev) 4726 { 4727 if (!__in6_dev_get(dev)) 4728 return 0; 4729 4730 return inet6_ifla6_size(); 4731 } 4732 4733 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev) 4734 { 4735 struct inet6_dev *idev = __in6_dev_get(dev); 4736 4737 if (!idev) 4738 return -ENODATA; 4739 4740 if (inet6_fill_ifla6_attrs(skb, idev) < 0) 4741 return -EMSGSIZE; 4742 4743 return 0; 4744 } 4745 4746 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) 4747 { 4748 struct inet6_ifaddr *ifp; 4749 struct net_device *dev = idev->dev; 4750 bool update_rs = false; 4751 struct in6_addr ll_addr; 4752 4753 ASSERT_RTNL(); 4754 4755 if (!token) 4756 return -EINVAL; 4757 if (ipv6_addr_any(token)) 4758 return -EINVAL; 4759 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) 4760 return -EINVAL; 4761 if (!ipv6_accept_ra(idev)) 4762 return -EINVAL; 4763 if (idev->cnf.rtr_solicits <= 0) 4764 return -EINVAL; 4765 4766 write_lock_bh(&idev->lock); 4767 4768 BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 4769 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 4770 4771 write_unlock_bh(&idev->lock); 4772 4773 if (!idev->dead && (idev->if_flags & IF_READY) && 4774 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 4775 IFA_F_OPTIMISTIC)) { 4776 4777 /* If we're not ready, then normal ifup will take care 4778 * of this. Otherwise, we need to request our rs here. 4779 */ 4780 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 4781 update_rs = true; 4782 } 4783 4784 write_lock_bh(&idev->lock); 4785 4786 if (update_rs) { 4787 idev->if_flags |= IF_RS_SENT; 4788 idev->rs_probes = 1; 4789 addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval); 4790 } 4791 4792 /* Well, that's kinda nasty ... */ 4793 list_for_each_entry(ifp, &idev->addr_list, if_list) { 4794 spin_lock(&ifp->lock); 4795 if (ifp->tokenized) { 4796 ifp->valid_lft = 0; 4797 ifp->prefered_lft = 0; 4798 } 4799 spin_unlock(&ifp->lock); 4800 } 4801 4802 write_unlock_bh(&idev->lock); 4803 inet6_ifinfo_notify(RTM_NEWLINK, idev); 4804 addrconf_verify_rtnl(); 4805 return 0; 4806 } 4807 4808 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { 4809 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, 4810 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, 4811 }; 4812 4813 static int inet6_validate_link_af(const struct net_device *dev, 4814 const struct nlattr *nla) 4815 { 4816 struct nlattr *tb[IFLA_INET6_MAX + 1]; 4817 4818 if (dev && !__in6_dev_get(dev)) 4819 return -EAFNOSUPPORT; 4820 4821 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy); 4822 } 4823 4824 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) 4825 { 4826 int err = -EINVAL; 4827 struct inet6_dev *idev = __in6_dev_get(dev); 4828 struct nlattr *tb[IFLA_INET6_MAX + 1]; 4829 4830 if (!idev) 4831 return -EAFNOSUPPORT; 4832 4833 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0) 4834 BUG(); 4835 4836 if (tb[IFLA_INET6_TOKEN]) { 4837 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); 4838 if (err) 4839 return err; 4840 } 4841 4842 if (tb[IFLA_INET6_ADDR_GEN_MODE]) { 4843 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 4844 4845 if (mode != IN6_ADDR_GEN_MODE_EUI64 && 4846 mode != IN6_ADDR_GEN_MODE_NONE && 4847 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY) 4848 return -EINVAL; 4849 4850 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && 4851 !idev->cnf.stable_secret.initialized && 4852 !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized) 4853 return -EINVAL; 4854 4855 idev->addr_gen_mode = mode; 4856 err = 0; 4857 } 4858 4859 return err; 4860 } 4861 4862 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 4863 u32 portid, u32 seq, int event, unsigned int flags) 4864 { 4865 struct net_device *dev = idev->dev; 4866 struct ifinfomsg *hdr; 4867 struct nlmsghdr *nlh; 4868 void *protoinfo; 4869 4870 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 4871 if (!nlh) 4872 return -EMSGSIZE; 4873 4874 hdr = nlmsg_data(nlh); 4875 hdr->ifi_family = AF_INET6; 4876 hdr->__ifi_pad = 0; 4877 hdr->ifi_type = dev->type; 4878 hdr->ifi_index = dev->ifindex; 4879 hdr->ifi_flags = dev_get_flags(dev); 4880 hdr->ifi_change = 0; 4881 4882 if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4883 (dev->addr_len && 4884 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4885 nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4886 (dev->ifindex != dev_get_iflink(dev) && 4887 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 4888 goto nla_put_failure; 4889 protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 4890 if (!protoinfo) 4891 goto nla_put_failure; 4892 4893 if (inet6_fill_ifla6_attrs(skb, idev) < 0) 4894 goto nla_put_failure; 4895 4896 nla_nest_end(skb, protoinfo); 4897 nlmsg_end(skb, nlh); 4898 return 0; 4899 4900 nla_put_failure: 4901 nlmsg_cancel(skb, nlh); 4902 return -EMSGSIZE; 4903 } 4904 4905 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 4906 { 4907 struct net *net = sock_net(skb->sk); 4908 int h, s_h; 4909 int idx = 0, s_idx; 4910 struct net_device *dev; 4911 struct inet6_dev *idev; 4912 struct hlist_head *head; 4913 4914 s_h = cb->args[0]; 4915 s_idx = cb->args[1]; 4916 4917 rcu_read_lock(); 4918 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4919 idx = 0; 4920 head = &net->dev_index_head[h]; 4921 hlist_for_each_entry_rcu(dev, head, index_hlist) { 4922 if (idx < s_idx) 4923 goto cont; 4924 idev = __in6_dev_get(dev); 4925 if (!idev) 4926 goto cont; 4927 if (inet6_fill_ifinfo(skb, idev, 4928 NETLINK_CB(cb->skb).portid, 4929 cb->nlh->nlmsg_seq, 4930 RTM_NEWLINK, NLM_F_MULTI) < 0) 4931 goto out; 4932 cont: 4933 idx++; 4934 } 4935 } 4936 out: 4937 rcu_read_unlock(); 4938 cb->args[1] = idx; 4939 cb->args[0] = h; 4940 4941 return skb->len; 4942 } 4943 4944 void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 4945 { 4946 struct sk_buff *skb; 4947 struct net *net = dev_net(idev->dev); 4948 int err = -ENOBUFS; 4949 4950 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 4951 if (!skb) 4952 goto errout; 4953 4954 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 4955 if (err < 0) { 4956 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 4957 WARN_ON(err == -EMSGSIZE); 4958 kfree_skb(skb); 4959 goto errout; 4960 } 4961 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 4962 return; 4963 errout: 4964 if (err < 0) 4965 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 4966 } 4967 4968 static inline size_t inet6_prefix_nlmsg_size(void) 4969 { 4970 return NLMSG_ALIGN(sizeof(struct prefixmsg)) 4971 + nla_total_size(sizeof(struct in6_addr)) 4972 + nla_total_size(sizeof(struct prefix_cacheinfo)); 4973 } 4974 4975 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 4976 struct prefix_info *pinfo, u32 portid, u32 seq, 4977 int event, unsigned int flags) 4978 { 4979 struct prefixmsg *pmsg; 4980 struct nlmsghdr *nlh; 4981 struct prefix_cacheinfo ci; 4982 4983 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 4984 if (!nlh) 4985 return -EMSGSIZE; 4986 4987 pmsg = nlmsg_data(nlh); 4988 pmsg->prefix_family = AF_INET6; 4989 pmsg->prefix_pad1 = 0; 4990 pmsg->prefix_pad2 = 0; 4991 pmsg->prefix_ifindex = idev->dev->ifindex; 4992 pmsg->prefix_len = pinfo->prefix_len; 4993 pmsg->prefix_type = pinfo->type; 4994 pmsg->prefix_pad3 = 0; 4995 pmsg->prefix_flags = 0; 4996 if (pinfo->onlink) 4997 pmsg->prefix_flags |= IF_PREFIX_ONLINK; 4998 if (pinfo->autoconf) 4999 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 5000 5001 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 5002 goto nla_put_failure; 5003 ci.preferred_time = ntohl(pinfo->prefered); 5004 ci.valid_time = ntohl(pinfo->valid); 5005 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 5006 goto nla_put_failure; 5007 nlmsg_end(skb, nlh); 5008 return 0; 5009 5010 nla_put_failure: 5011 nlmsg_cancel(skb, nlh); 5012 return -EMSGSIZE; 5013 } 5014 5015 static void inet6_prefix_notify(int event, struct inet6_dev *idev, 5016 struct prefix_info *pinfo) 5017 { 5018 struct sk_buff *skb; 5019 struct net *net = dev_net(idev->dev); 5020 int err = -ENOBUFS; 5021 5022 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 5023 if (!skb) 5024 goto errout; 5025 5026 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 5027 if (err < 0) { 5028 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 5029 WARN_ON(err == -EMSGSIZE); 5030 kfree_skb(skb); 5031 goto errout; 5032 } 5033 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 5034 return; 5035 errout: 5036 if (err < 0) 5037 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 5038 } 5039 5040 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 5041 { 5042 struct net *net = dev_net(ifp->idev->dev); 5043 5044 if (event) 5045 ASSERT_RTNL(); 5046 5047 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 5048 5049 switch (event) { 5050 case RTM_NEWADDR: 5051 /* 5052 * If the address was optimistic 5053 * we inserted the route at the start of 5054 * our DAD process, so we don't need 5055 * to do it again 5056 */ 5057 if (!(ifp->rt->rt6i_node)) 5058 ip6_ins_rt(ifp->rt); 5059 if (ifp->idev->cnf.forwarding) 5060 addrconf_join_anycast(ifp); 5061 if (!ipv6_addr_any(&ifp->peer_addr)) 5062 addrconf_prefix_route(&ifp->peer_addr, 128, 5063 ifp->idev->dev, 0, 0); 5064 break; 5065 case RTM_DELADDR: 5066 if (ifp->idev->cnf.forwarding) 5067 addrconf_leave_anycast(ifp); 5068 addrconf_leave_solict(ifp->idev, &ifp->addr); 5069 if (!ipv6_addr_any(&ifp->peer_addr)) { 5070 struct rt6_info *rt; 5071 5072 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, 5073 ifp->idev->dev, 0, 0); 5074 if (rt && ip6_del_rt(rt)) 5075 dst_free(&rt->dst); 5076 } 5077 dst_hold(&ifp->rt->dst); 5078 5079 if (ip6_del_rt(ifp->rt)) 5080 dst_free(&ifp->rt->dst); 5081 5082 rt_genid_bump_ipv6(net); 5083 break; 5084 } 5085 atomic_inc(&net->ipv6.dev_addr_genid); 5086 } 5087 5088 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 5089 { 5090 rcu_read_lock_bh(); 5091 if (likely(ifp->idev->dead == 0)) 5092 __ipv6_ifa_notify(event, ifp); 5093 rcu_read_unlock_bh(); 5094 } 5095 5096 #ifdef CONFIG_SYSCTL 5097 5098 static 5099 int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 5100 void __user *buffer, size_t *lenp, loff_t *ppos) 5101 { 5102 int *valp = ctl->data; 5103 int val = *valp; 5104 loff_t pos = *ppos; 5105 struct ctl_table lctl; 5106 int ret; 5107 5108 /* 5109 * ctl->data points to idev->cnf.forwarding, we should 5110 * not modify it until we get the rtnl lock. 5111 */ 5112 lctl = *ctl; 5113 lctl.data = &val; 5114 5115 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 5116 5117 if (write) 5118 ret = addrconf_fixup_forwarding(ctl, valp, val); 5119 if (ret) 5120 *ppos = pos; 5121 return ret; 5122 } 5123 5124 static 5125 int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, 5126 void __user *buffer, size_t *lenp, loff_t *ppos) 5127 { 5128 struct inet6_dev *idev = ctl->extra1; 5129 int min_mtu = IPV6_MIN_MTU; 5130 struct ctl_table lctl; 5131 5132 lctl = *ctl; 5133 lctl.extra1 = &min_mtu; 5134 lctl.extra2 = idev ? &idev->dev->mtu : NULL; 5135 5136 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); 5137 } 5138 5139 static void dev_disable_change(struct inet6_dev *idev) 5140 { 5141 struct netdev_notifier_info info; 5142 5143 if (!idev || !idev->dev) 5144 return; 5145 5146 netdev_notifier_info_init(&info, idev->dev); 5147 if (idev->cnf.disable_ipv6) 5148 addrconf_notify(NULL, NETDEV_DOWN, &info); 5149 else 5150 addrconf_notify(NULL, NETDEV_UP, &info); 5151 } 5152 5153 static void addrconf_disable_change(struct net *net, __s32 newf) 5154 { 5155 struct net_device *dev; 5156 struct inet6_dev *idev; 5157 5158 rcu_read_lock(); 5159 for_each_netdev_rcu(net, dev) { 5160 idev = __in6_dev_get(dev); 5161 if (idev) { 5162 int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 5163 idev->cnf.disable_ipv6 = newf; 5164 if (changed) 5165 dev_disable_change(idev); 5166 } 5167 } 5168 rcu_read_unlock(); 5169 } 5170 5171 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 5172 { 5173 struct net *net; 5174 int old; 5175 5176 if (!rtnl_trylock()) 5177 return restart_syscall(); 5178 5179 net = (struct net *)table->extra2; 5180 old = *p; 5181 *p = newf; 5182 5183 if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 5184 rtnl_unlock(); 5185 return 0; 5186 } 5187 5188 if (p == &net->ipv6.devconf_all->disable_ipv6) { 5189 net->ipv6.devconf_dflt->disable_ipv6 = newf; 5190 addrconf_disable_change(net, newf); 5191 } else if ((!newf) ^ (!old)) 5192 dev_disable_change((struct inet6_dev *)table->extra1); 5193 5194 rtnl_unlock(); 5195 return 0; 5196 } 5197 5198 static 5199 int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 5200 void __user *buffer, size_t *lenp, loff_t *ppos) 5201 { 5202 int *valp = ctl->data; 5203 int val = *valp; 5204 loff_t pos = *ppos; 5205 struct ctl_table lctl; 5206 int ret; 5207 5208 /* 5209 * ctl->data points to idev->cnf.disable_ipv6, we should 5210 * not modify it until we get the rtnl lock. 5211 */ 5212 lctl = *ctl; 5213 lctl.data = &val; 5214 5215 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 5216 5217 if (write) 5218 ret = addrconf_disable_ipv6(ctl, valp, val); 5219 if (ret) 5220 *ppos = pos; 5221 return ret; 5222 } 5223 5224 static 5225 int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, 5226 void __user *buffer, size_t *lenp, loff_t *ppos) 5227 { 5228 int *valp = ctl->data; 5229 int ret; 5230 int old, new; 5231 5232 old = *valp; 5233 ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 5234 new = *valp; 5235 5236 if (write && old != new) { 5237 struct net *net = ctl->extra2; 5238 5239 if (!rtnl_trylock()) 5240 return restart_syscall(); 5241 5242 if (valp == &net->ipv6.devconf_dflt->proxy_ndp) 5243 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5244 NETCONFA_IFINDEX_DEFAULT, 5245 net->ipv6.devconf_dflt); 5246 else if (valp == &net->ipv6.devconf_all->proxy_ndp) 5247 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5248 NETCONFA_IFINDEX_ALL, 5249 net->ipv6.devconf_all); 5250 else { 5251 struct inet6_dev *idev = ctl->extra1; 5252 5253 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH, 5254 idev->dev->ifindex, 5255 &idev->cnf); 5256 } 5257 rtnl_unlock(); 5258 } 5259 5260 return ret; 5261 } 5262 5263 static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, 5264 void __user *buffer, size_t *lenp, 5265 loff_t *ppos) 5266 { 5267 int err; 5268 struct in6_addr addr; 5269 char str[IPV6_MAX_STRLEN]; 5270 struct ctl_table lctl = *ctl; 5271 struct net *net = ctl->extra2; 5272 struct ipv6_stable_secret *secret = ctl->data; 5273 5274 if (&net->ipv6.devconf_all->stable_secret == ctl->data) 5275 return -EIO; 5276 5277 lctl.maxlen = IPV6_MAX_STRLEN; 5278 lctl.data = str; 5279 5280 if (!rtnl_trylock()) 5281 return restart_syscall(); 5282 5283 if (!write && !secret->initialized) { 5284 err = -EIO; 5285 goto out; 5286 } 5287 5288 if (!write) { 5289 err = snprintf(str, sizeof(str), "%pI6", 5290 &secret->secret); 5291 if (err >= sizeof(str)) { 5292 err = -EIO; 5293 goto out; 5294 } 5295 } 5296 5297 err = proc_dostring(&lctl, write, buffer, lenp, ppos); 5298 if (err || !write) 5299 goto out; 5300 5301 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { 5302 err = -EIO; 5303 goto out; 5304 } 5305 5306 secret->initialized = true; 5307 secret->secret = addr; 5308 5309 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { 5310 struct net_device *dev; 5311 5312 for_each_netdev(net, dev) { 5313 struct inet6_dev *idev = __in6_dev_get(dev); 5314 5315 if (idev) { 5316 idev->addr_gen_mode = 5317 IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 5318 } 5319 } 5320 } else { 5321 struct inet6_dev *idev = ctl->extra1; 5322 5323 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; 5324 } 5325 5326 out: 5327 rtnl_unlock(); 5328 5329 return err; 5330 } 5331 5332 static struct addrconf_sysctl_table 5333 { 5334 struct ctl_table_header *sysctl_header; 5335 struct ctl_table addrconf_vars[DEVCONF_MAX+1]; 5336 } addrconf_sysctl __read_mostly = { 5337 .sysctl_header = NULL, 5338 .addrconf_vars = { 5339 { 5340 .procname = "forwarding", 5341 .data = &ipv6_devconf.forwarding, 5342 .maxlen = sizeof(int), 5343 .mode = 0644, 5344 .proc_handler = addrconf_sysctl_forward, 5345 }, 5346 { 5347 .procname = "hop_limit", 5348 .data = &ipv6_devconf.hop_limit, 5349 .maxlen = sizeof(int), 5350 .mode = 0644, 5351 .proc_handler = proc_dointvec, 5352 }, 5353 { 5354 .procname = "mtu", 5355 .data = &ipv6_devconf.mtu6, 5356 .maxlen = sizeof(int), 5357 .mode = 0644, 5358 .proc_handler = addrconf_sysctl_mtu, 5359 }, 5360 { 5361 .procname = "accept_ra", 5362 .data = &ipv6_devconf.accept_ra, 5363 .maxlen = sizeof(int), 5364 .mode = 0644, 5365 .proc_handler = proc_dointvec, 5366 }, 5367 { 5368 .procname = "accept_redirects", 5369 .data = &ipv6_devconf.accept_redirects, 5370 .maxlen = sizeof(int), 5371 .mode = 0644, 5372 .proc_handler = proc_dointvec, 5373 }, 5374 { 5375 .procname = "autoconf", 5376 .data = &ipv6_devconf.autoconf, 5377 .maxlen = sizeof(int), 5378 .mode = 0644, 5379 .proc_handler = proc_dointvec, 5380 }, 5381 { 5382 .procname = "dad_transmits", 5383 .data = &ipv6_devconf.dad_transmits, 5384 .maxlen = sizeof(int), 5385 .mode = 0644, 5386 .proc_handler = proc_dointvec, 5387 }, 5388 { 5389 .procname = "router_solicitations", 5390 .data = &ipv6_devconf.rtr_solicits, 5391 .maxlen = sizeof(int), 5392 .mode = 0644, 5393 .proc_handler = proc_dointvec, 5394 }, 5395 { 5396 .procname = "router_solicitation_interval", 5397 .data = &ipv6_devconf.rtr_solicit_interval, 5398 .maxlen = sizeof(int), 5399 .mode = 0644, 5400 .proc_handler = proc_dointvec_jiffies, 5401 }, 5402 { 5403 .procname = "router_solicitation_delay", 5404 .data = &ipv6_devconf.rtr_solicit_delay, 5405 .maxlen = sizeof(int), 5406 .mode = 0644, 5407 .proc_handler = proc_dointvec_jiffies, 5408 }, 5409 { 5410 .procname = "force_mld_version", 5411 .data = &ipv6_devconf.force_mld_version, 5412 .maxlen = sizeof(int), 5413 .mode = 0644, 5414 .proc_handler = proc_dointvec, 5415 }, 5416 { 5417 .procname = "mldv1_unsolicited_report_interval", 5418 .data = 5419 &ipv6_devconf.mldv1_unsolicited_report_interval, 5420 .maxlen = sizeof(int), 5421 .mode = 0644, 5422 .proc_handler = proc_dointvec_ms_jiffies, 5423 }, 5424 { 5425 .procname = "mldv2_unsolicited_report_interval", 5426 .data = 5427 &ipv6_devconf.mldv2_unsolicited_report_interval, 5428 .maxlen = sizeof(int), 5429 .mode = 0644, 5430 .proc_handler = proc_dointvec_ms_jiffies, 5431 }, 5432 { 5433 .procname = "use_tempaddr", 5434 .data = &ipv6_devconf.use_tempaddr, 5435 .maxlen = sizeof(int), 5436 .mode = 0644, 5437 .proc_handler = proc_dointvec, 5438 }, 5439 { 5440 .procname = "temp_valid_lft", 5441 .data = &ipv6_devconf.temp_valid_lft, 5442 .maxlen = sizeof(int), 5443 .mode = 0644, 5444 .proc_handler = proc_dointvec, 5445 }, 5446 { 5447 .procname = "temp_prefered_lft", 5448 .data = &ipv6_devconf.temp_prefered_lft, 5449 .maxlen = sizeof(int), 5450 .mode = 0644, 5451 .proc_handler = proc_dointvec, 5452 }, 5453 { 5454 .procname = "regen_max_retry", 5455 .data = &ipv6_devconf.regen_max_retry, 5456 .maxlen = sizeof(int), 5457 .mode = 0644, 5458 .proc_handler = proc_dointvec, 5459 }, 5460 { 5461 .procname = "max_desync_factor", 5462 .data = &ipv6_devconf.max_desync_factor, 5463 .maxlen = sizeof(int), 5464 .mode = 0644, 5465 .proc_handler = proc_dointvec, 5466 }, 5467 { 5468 .procname = "max_addresses", 5469 .data = &ipv6_devconf.max_addresses, 5470 .maxlen = sizeof(int), 5471 .mode = 0644, 5472 .proc_handler = proc_dointvec, 5473 }, 5474 { 5475 .procname = "accept_ra_defrtr", 5476 .data = &ipv6_devconf.accept_ra_defrtr, 5477 .maxlen = sizeof(int), 5478 .mode = 0644, 5479 .proc_handler = proc_dointvec, 5480 }, 5481 { 5482 .procname = "accept_ra_pinfo", 5483 .data = &ipv6_devconf.accept_ra_pinfo, 5484 .maxlen = sizeof(int), 5485 .mode = 0644, 5486 .proc_handler = proc_dointvec, 5487 }, 5488 #ifdef CONFIG_IPV6_ROUTER_PREF 5489 { 5490 .procname = "accept_ra_rtr_pref", 5491 .data = &ipv6_devconf.accept_ra_rtr_pref, 5492 .maxlen = sizeof(int), 5493 .mode = 0644, 5494 .proc_handler = proc_dointvec, 5495 }, 5496 { 5497 .procname = "router_probe_interval", 5498 .data = &ipv6_devconf.rtr_probe_interval, 5499 .maxlen = sizeof(int), 5500 .mode = 0644, 5501 .proc_handler = proc_dointvec_jiffies, 5502 }, 5503 #ifdef CONFIG_IPV6_ROUTE_INFO 5504 { 5505 .procname = "accept_ra_rt_info_max_plen", 5506 .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 5507 .maxlen = sizeof(int), 5508 .mode = 0644, 5509 .proc_handler = proc_dointvec, 5510 }, 5511 #endif 5512 #endif 5513 { 5514 .procname = "proxy_ndp", 5515 .data = &ipv6_devconf.proxy_ndp, 5516 .maxlen = sizeof(int), 5517 .mode = 0644, 5518 .proc_handler = addrconf_sysctl_proxy_ndp, 5519 }, 5520 { 5521 .procname = "accept_source_route", 5522 .data = &ipv6_devconf.accept_source_route, 5523 .maxlen = sizeof(int), 5524 .mode = 0644, 5525 .proc_handler = proc_dointvec, 5526 }, 5527 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 5528 { 5529 .procname = "optimistic_dad", 5530 .data = &ipv6_devconf.optimistic_dad, 5531 .maxlen = sizeof(int), 5532 .mode = 0644, 5533 .proc_handler = proc_dointvec, 5534 5535 }, 5536 { 5537 .procname = "use_optimistic", 5538 .data = &ipv6_devconf.use_optimistic, 5539 .maxlen = sizeof(int), 5540 .mode = 0644, 5541 .proc_handler = proc_dointvec, 5542 5543 }, 5544 #endif 5545 #ifdef CONFIG_IPV6_MROUTE 5546 { 5547 .procname = "mc_forwarding", 5548 .data = &ipv6_devconf.mc_forwarding, 5549 .maxlen = sizeof(int), 5550 .mode = 0444, 5551 .proc_handler = proc_dointvec, 5552 }, 5553 #endif 5554 { 5555 .procname = "disable_ipv6", 5556 .data = &ipv6_devconf.disable_ipv6, 5557 .maxlen = sizeof(int), 5558 .mode = 0644, 5559 .proc_handler = addrconf_sysctl_disable, 5560 }, 5561 { 5562 .procname = "accept_dad", 5563 .data = &ipv6_devconf.accept_dad, 5564 .maxlen = sizeof(int), 5565 .mode = 0644, 5566 .proc_handler = proc_dointvec, 5567 }, 5568 { 5569 .procname = "force_tllao", 5570 .data = &ipv6_devconf.force_tllao, 5571 .maxlen = sizeof(int), 5572 .mode = 0644, 5573 .proc_handler = proc_dointvec 5574 }, 5575 { 5576 .procname = "ndisc_notify", 5577 .data = &ipv6_devconf.ndisc_notify, 5578 .maxlen = sizeof(int), 5579 .mode = 0644, 5580 .proc_handler = proc_dointvec 5581 }, 5582 { 5583 .procname = "suppress_frag_ndisc", 5584 .data = &ipv6_devconf.suppress_frag_ndisc, 5585 .maxlen = sizeof(int), 5586 .mode = 0644, 5587 .proc_handler = proc_dointvec 5588 }, 5589 { 5590 .procname = "accept_ra_from_local", 5591 .data = &ipv6_devconf.accept_ra_from_local, 5592 .maxlen = sizeof(int), 5593 .mode = 0644, 5594 .proc_handler = proc_dointvec, 5595 }, 5596 { 5597 .procname = "accept_ra_mtu", 5598 .data = &ipv6_devconf.accept_ra_mtu, 5599 .maxlen = sizeof(int), 5600 .mode = 0644, 5601 .proc_handler = proc_dointvec, 5602 }, 5603 { 5604 .procname = "stable_secret", 5605 .data = &ipv6_devconf.stable_secret, 5606 .maxlen = IPV6_MAX_STRLEN, 5607 .mode = 0600, 5608 .proc_handler = addrconf_sysctl_stable_secret, 5609 }, 5610 { 5611 /* sentinel */ 5612 } 5613 }, 5614 }; 5615 5616 static int __addrconf_sysctl_register(struct net *net, char *dev_name, 5617 struct inet6_dev *idev, struct ipv6_devconf *p) 5618 { 5619 int i; 5620 struct addrconf_sysctl_table *t; 5621 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 5622 5623 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL); 5624 if (!t) 5625 goto out; 5626 5627 for (i = 0; t->addrconf_vars[i].data; i++) { 5628 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf; 5629 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */ 5630 t->addrconf_vars[i].extra2 = net; 5631 } 5632 5633 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 5634 5635 t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars); 5636 if (!t->sysctl_header) 5637 goto free; 5638 5639 p->sysctl = t; 5640 return 0; 5641 5642 free: 5643 kfree(t); 5644 out: 5645 return -ENOBUFS; 5646 } 5647 5648 static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) 5649 { 5650 struct addrconf_sysctl_table *t; 5651 5652 if (!p->sysctl) 5653 return; 5654 5655 t = p->sysctl; 5656 p->sysctl = NULL; 5657 unregister_net_sysctl_table(t->sysctl_header); 5658 kfree(t); 5659 } 5660 5661 static int addrconf_sysctl_register(struct inet6_dev *idev) 5662 { 5663 int err; 5664 5665 if (!sysctl_dev_name_is_allowed(idev->dev->name)) 5666 return -EINVAL; 5667 5668 err = neigh_sysctl_register(idev->dev, idev->nd_parms, 5669 &ndisc_ifinfo_sysctl_change); 5670 if (err) 5671 return err; 5672 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 5673 idev, &idev->cnf); 5674 if (err) 5675 neigh_sysctl_unregister(idev->nd_parms); 5676 5677 return err; 5678 } 5679 5680 static void addrconf_sysctl_unregister(struct inet6_dev *idev) 5681 { 5682 __addrconf_sysctl_unregister(&idev->cnf); 5683 neigh_sysctl_unregister(idev->nd_parms); 5684 } 5685 5686 5687 #endif 5688 5689 static int __net_init addrconf_init_net(struct net *net) 5690 { 5691 int err = -ENOMEM; 5692 struct ipv6_devconf *all, *dflt; 5693 5694 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 5695 if (!all) 5696 goto err_alloc_all; 5697 5698 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 5699 if (!dflt) 5700 goto err_alloc_dflt; 5701 5702 /* these will be inherited by all namespaces */ 5703 dflt->autoconf = ipv6_defaults.autoconf; 5704 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 5705 5706 dflt->stable_secret.initialized = false; 5707 all->stable_secret.initialized = false; 5708 5709 net->ipv6.devconf_all = all; 5710 net->ipv6.devconf_dflt = dflt; 5711 5712 #ifdef CONFIG_SYSCTL 5713 err = __addrconf_sysctl_register(net, "all", NULL, all); 5714 if (err < 0) 5715 goto err_reg_all; 5716 5717 err = __addrconf_sysctl_register(net, "default", NULL, dflt); 5718 if (err < 0) 5719 goto err_reg_dflt; 5720 #endif 5721 return 0; 5722 5723 #ifdef CONFIG_SYSCTL 5724 err_reg_dflt: 5725 __addrconf_sysctl_unregister(all); 5726 err_reg_all: 5727 kfree(dflt); 5728 #endif 5729 err_alloc_dflt: 5730 kfree(all); 5731 err_alloc_all: 5732 return err; 5733 } 5734 5735 static void __net_exit addrconf_exit_net(struct net *net) 5736 { 5737 #ifdef CONFIG_SYSCTL 5738 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); 5739 __addrconf_sysctl_unregister(net->ipv6.devconf_all); 5740 #endif 5741 kfree(net->ipv6.devconf_dflt); 5742 kfree(net->ipv6.devconf_all); 5743 } 5744 5745 static struct pernet_operations addrconf_ops = { 5746 .init = addrconf_init_net, 5747 .exit = addrconf_exit_net, 5748 }; 5749 5750 static struct rtnl_af_ops inet6_ops __read_mostly = { 5751 .family = AF_INET6, 5752 .fill_link_af = inet6_fill_link_af, 5753 .get_link_af_size = inet6_get_link_af_size, 5754 .validate_link_af = inet6_validate_link_af, 5755 .set_link_af = inet6_set_link_af, 5756 }; 5757 5758 /* 5759 * Init / cleanup code 5760 */ 5761 5762 int __init addrconf_init(void) 5763 { 5764 struct inet6_dev *idev; 5765 int i, err; 5766 5767 err = ipv6_addr_label_init(); 5768 if (err < 0) { 5769 pr_crit("%s: cannot initialize default policy table: %d\n", 5770 __func__, err); 5771 goto out; 5772 } 5773 5774 err = register_pernet_subsys(&addrconf_ops); 5775 if (err < 0) 5776 goto out_addrlabel; 5777 5778 addrconf_wq = create_workqueue("ipv6_addrconf"); 5779 if (!addrconf_wq) { 5780 err = -ENOMEM; 5781 goto out_nowq; 5782 } 5783 5784 /* The addrconf netdev notifier requires that loopback_dev 5785 * has it's ipv6 private information allocated and setup 5786 * before it can bring up and give link-local addresses 5787 * to other devices which are up. 5788 * 5789 * Unfortunately, loopback_dev is not necessarily the first 5790 * entry in the global dev_base list of net devices. In fact, 5791 * it is likely to be the very last entry on that list. 5792 * So this causes the notifier registry below to try and 5793 * give link-local addresses to all devices besides loopback_dev 5794 * first, then loopback_dev, which cases all the non-loopback_dev 5795 * devices to fail to get a link-local address. 5796 * 5797 * So, as a temporary fix, allocate the ipv6 structure for 5798 * loopback_dev first by hand. 5799 * Longer term, all of the dependencies ipv6 has upon the loopback 5800 * device and it being up should be removed. 5801 */ 5802 rtnl_lock(); 5803 idev = ipv6_add_dev(init_net.loopback_dev); 5804 rtnl_unlock(); 5805 if (IS_ERR(idev)) { 5806 err = PTR_ERR(idev); 5807 goto errlo; 5808 } 5809 5810 for (i = 0; i < IN6_ADDR_HSIZE; i++) 5811 INIT_HLIST_HEAD(&inet6_addr_lst[i]); 5812 5813 register_netdevice_notifier(&ipv6_dev_notf); 5814 5815 addrconf_verify(); 5816 5817 rtnl_af_register(&inet6_ops); 5818 5819 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo, 5820 NULL); 5821 if (err < 0) 5822 goto errout; 5823 5824 /* Only the first call to __rtnl_register can fail */ 5825 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL); 5826 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL); 5827 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, 5828 inet6_dump_ifaddr, NULL); 5829 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, 5830 inet6_dump_ifmcaddr, NULL); 5831 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, 5832 inet6_dump_ifacaddr, NULL); 5833 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf, 5834 inet6_netconf_dump_devconf, NULL); 5835 5836 ipv6_addr_label_rtnl_register(); 5837 5838 return 0; 5839 errout: 5840 rtnl_af_unregister(&inet6_ops); 5841 unregister_netdevice_notifier(&ipv6_dev_notf); 5842 errlo: 5843 destroy_workqueue(addrconf_wq); 5844 out_nowq: 5845 unregister_pernet_subsys(&addrconf_ops); 5846 out_addrlabel: 5847 ipv6_addr_label_cleanup(); 5848 out: 5849 return err; 5850 } 5851 5852 void addrconf_cleanup(void) 5853 { 5854 struct net_device *dev; 5855 int i; 5856 5857 unregister_netdevice_notifier(&ipv6_dev_notf); 5858 unregister_pernet_subsys(&addrconf_ops); 5859 ipv6_addr_label_cleanup(); 5860 5861 rtnl_lock(); 5862 5863 __rtnl_af_unregister(&inet6_ops); 5864 5865 /* clean dev list */ 5866 for_each_netdev(&init_net, dev) { 5867 if (__in6_dev_get(dev) == NULL) 5868 continue; 5869 addrconf_ifdown(dev, 1); 5870 } 5871 addrconf_ifdown(init_net.loopback_dev, 2); 5872 5873 /* 5874 * Check hash table. 5875 */ 5876 spin_lock_bh(&addrconf_hash_lock); 5877 for (i = 0; i < IN6_ADDR_HSIZE; i++) 5878 WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 5879 spin_unlock_bh(&addrconf_hash_lock); 5880 cancel_delayed_work(&addr_chk_work); 5881 rtnl_unlock(); 5882 5883 destroy_workqueue(addrconf_wq); 5884 } 5885