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