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