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