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