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