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