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