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