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