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