1 /* 2 * Anycast support for IPv6 3 * Linux INET6 implementation 4 * 5 * Authors: 6 * David L Stevens (dlstevens@us.ibm.com) 7 * 8 * based heavily on net/ipv6/mcast.c 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 13 * 2 of the License, or (at your option) any later version. 14 */ 15 16 #include <linux/capability.h> 17 #include <linux/module.h> 18 #include <linux/errno.h> 19 #include <linux/types.h> 20 #include <linux/random.h> 21 #include <linux/string.h> 22 #include <linux/socket.h> 23 #include <linux/sockios.h> 24 #include <linux/net.h> 25 #include <linux/in6.h> 26 #include <linux/netdevice.h> 27 #include <linux/if_arp.h> 28 #include <linux/route.h> 29 #include <linux/init.h> 30 #include <linux/proc_fs.h> 31 #include <linux/seq_file.h> 32 #include <linux/slab.h> 33 34 #include <net/net_namespace.h> 35 #include <net/sock.h> 36 #include <net/snmp.h> 37 38 #include <net/ipv6.h> 39 #include <net/protocol.h> 40 #include <net/if_inet6.h> 41 #include <net/ndisc.h> 42 #include <net/addrconf.h> 43 #include <net/ip6_route.h> 44 45 #include <net/checksum.h> 46 47 #define IN6_ADDR_HSIZE_SHIFT 8 48 #define IN6_ADDR_HSIZE BIT(IN6_ADDR_HSIZE_SHIFT) 49 /* anycast address hash table 50 */ 51 static struct hlist_head inet6_acaddr_lst[IN6_ADDR_HSIZE]; 52 static DEFINE_SPINLOCK(acaddr_hash_lock); 53 54 static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr); 55 56 static u32 inet6_acaddr_hash(struct net *net, const struct in6_addr *addr) 57 { 58 u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net); 59 60 return hash_32(val, IN6_ADDR_HSIZE_SHIFT); 61 } 62 63 /* 64 * socket join an anycast group 65 */ 66 67 int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr) 68 { 69 struct ipv6_pinfo *np = inet6_sk(sk); 70 struct net_device *dev = NULL; 71 struct inet6_dev *idev; 72 struct ipv6_ac_socklist *pac; 73 struct net *net = sock_net(sk); 74 int ishost = !net->ipv6.devconf_all->forwarding; 75 int err = 0; 76 77 ASSERT_RTNL(); 78 79 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 80 return -EPERM; 81 if (ipv6_addr_is_multicast(addr)) 82 return -EINVAL; 83 84 if (ifindex) 85 dev = __dev_get_by_index(net, ifindex); 86 87 if (ipv6_chk_addr_and_flags(net, addr, dev, true, 0, IFA_F_TENTATIVE)) 88 return -EINVAL; 89 90 pac = sock_kmalloc(sk, sizeof(struct ipv6_ac_socklist), GFP_KERNEL); 91 if (!pac) 92 return -ENOMEM; 93 pac->acl_next = NULL; 94 pac->acl_addr = *addr; 95 96 if (ifindex == 0) { 97 struct rt6_info *rt; 98 99 rt = rt6_lookup(net, addr, NULL, 0, NULL, 0); 100 if (rt) { 101 dev = rt->dst.dev; 102 ip6_rt_put(rt); 103 } else if (ishost) { 104 err = -EADDRNOTAVAIL; 105 goto error; 106 } else { 107 /* router, no matching interface: just pick one */ 108 dev = __dev_get_by_flags(net, IFF_UP, 109 IFF_UP | IFF_LOOPBACK); 110 } 111 } 112 113 if (!dev) { 114 err = -ENODEV; 115 goto error; 116 } 117 118 idev = __in6_dev_get(dev); 119 if (!idev) { 120 if (ifindex) 121 err = -ENODEV; 122 else 123 err = -EADDRNOTAVAIL; 124 goto error; 125 } 126 /* reset ishost, now that we have a specific device */ 127 ishost = !idev->cnf.forwarding; 128 129 pac->acl_ifindex = dev->ifindex; 130 131 /* XXX 132 * For hosts, allow link-local or matching prefix anycasts. 133 * This obviates the need for propagating anycast routes while 134 * still allowing some non-router anycast participation. 135 */ 136 if (!ipv6_chk_prefix(addr, dev)) { 137 if (ishost) 138 err = -EADDRNOTAVAIL; 139 if (err) 140 goto error; 141 } 142 143 err = __ipv6_dev_ac_inc(idev, addr); 144 if (!err) { 145 pac->acl_next = np->ipv6_ac_list; 146 np->ipv6_ac_list = pac; 147 pac = NULL; 148 } 149 150 error: 151 if (pac) 152 sock_kfree_s(sk, pac, sizeof(*pac)); 153 return err; 154 } 155 156 /* 157 * socket leave an anycast group 158 */ 159 int ipv6_sock_ac_drop(struct sock *sk, int ifindex, const struct in6_addr *addr) 160 { 161 struct ipv6_pinfo *np = inet6_sk(sk); 162 struct net_device *dev; 163 struct ipv6_ac_socklist *pac, *prev_pac; 164 struct net *net = sock_net(sk); 165 166 ASSERT_RTNL(); 167 168 prev_pac = NULL; 169 for (pac = np->ipv6_ac_list; pac; pac = pac->acl_next) { 170 if ((ifindex == 0 || pac->acl_ifindex == ifindex) && 171 ipv6_addr_equal(&pac->acl_addr, addr)) 172 break; 173 prev_pac = pac; 174 } 175 if (!pac) 176 return -ENOENT; 177 if (prev_pac) 178 prev_pac->acl_next = pac->acl_next; 179 else 180 np->ipv6_ac_list = pac->acl_next; 181 182 dev = __dev_get_by_index(net, pac->acl_ifindex); 183 if (dev) 184 ipv6_dev_ac_dec(dev, &pac->acl_addr); 185 186 sock_kfree_s(sk, pac, sizeof(*pac)); 187 return 0; 188 } 189 190 void ipv6_sock_ac_close(struct sock *sk) 191 { 192 struct ipv6_pinfo *np = inet6_sk(sk); 193 struct net_device *dev = NULL; 194 struct ipv6_ac_socklist *pac; 195 struct net *net = sock_net(sk); 196 int prev_index; 197 198 if (!np->ipv6_ac_list) 199 return; 200 201 rtnl_lock(); 202 pac = np->ipv6_ac_list; 203 np->ipv6_ac_list = NULL; 204 205 prev_index = 0; 206 while (pac) { 207 struct ipv6_ac_socklist *next = pac->acl_next; 208 209 if (pac->acl_ifindex != prev_index) { 210 dev = __dev_get_by_index(net, pac->acl_ifindex); 211 prev_index = pac->acl_ifindex; 212 } 213 if (dev) 214 ipv6_dev_ac_dec(dev, &pac->acl_addr); 215 sock_kfree_s(sk, pac, sizeof(*pac)); 216 pac = next; 217 } 218 rtnl_unlock(); 219 } 220 221 static void ipv6_add_acaddr_hash(struct net *net, struct ifacaddr6 *aca) 222 { 223 unsigned int hash = inet6_acaddr_hash(net, &aca->aca_addr); 224 225 spin_lock(&acaddr_hash_lock); 226 hlist_add_head_rcu(&aca->aca_addr_lst, &inet6_acaddr_lst[hash]); 227 spin_unlock(&acaddr_hash_lock); 228 } 229 230 static void ipv6_del_acaddr_hash(struct ifacaddr6 *aca) 231 { 232 spin_lock(&acaddr_hash_lock); 233 hlist_del_init_rcu(&aca->aca_addr_lst); 234 spin_unlock(&acaddr_hash_lock); 235 } 236 237 static void aca_get(struct ifacaddr6 *aca) 238 { 239 refcount_inc(&aca->aca_refcnt); 240 } 241 242 static void aca_free_rcu(struct rcu_head *h) 243 { 244 struct ifacaddr6 *aca = container_of(h, struct ifacaddr6, rcu); 245 246 fib6_info_release(aca->aca_rt); 247 kfree(aca); 248 } 249 250 static void aca_put(struct ifacaddr6 *ac) 251 { 252 if (refcount_dec_and_test(&ac->aca_refcnt)) { 253 call_rcu(&ac->rcu, aca_free_rcu); 254 } 255 } 256 257 static struct ifacaddr6 *aca_alloc(struct fib6_info *f6i, 258 const struct in6_addr *addr) 259 { 260 struct ifacaddr6 *aca; 261 262 aca = kzalloc(sizeof(*aca), GFP_ATOMIC); 263 if (!aca) 264 return NULL; 265 266 aca->aca_addr = *addr; 267 fib6_info_hold(f6i); 268 aca->aca_rt = f6i; 269 INIT_HLIST_NODE(&aca->aca_addr_lst); 270 aca->aca_users = 1; 271 /* aca_tstamp should be updated upon changes */ 272 aca->aca_cstamp = aca->aca_tstamp = jiffies; 273 refcount_set(&aca->aca_refcnt, 1); 274 275 return aca; 276 } 277 278 /* 279 * device anycast group inc (add if not found) 280 */ 281 int __ipv6_dev_ac_inc(struct inet6_dev *idev, const struct in6_addr *addr) 282 { 283 struct ifacaddr6 *aca; 284 struct fib6_info *f6i; 285 struct net *net; 286 int err; 287 288 ASSERT_RTNL(); 289 290 write_lock_bh(&idev->lock); 291 if (idev->dead) { 292 err = -ENODEV; 293 goto out; 294 } 295 296 for (aca = idev->ac_list; aca; aca = aca->aca_next) { 297 if (ipv6_addr_equal(&aca->aca_addr, addr)) { 298 aca->aca_users++; 299 err = 0; 300 goto out; 301 } 302 } 303 304 net = dev_net(idev->dev); 305 f6i = addrconf_f6i_alloc(net, idev, addr, true, GFP_ATOMIC); 306 if (IS_ERR(f6i)) { 307 err = PTR_ERR(f6i); 308 goto out; 309 } 310 aca = aca_alloc(f6i, addr); 311 if (!aca) { 312 fib6_info_release(f6i); 313 err = -ENOMEM; 314 goto out; 315 } 316 317 aca->aca_next = idev->ac_list; 318 idev->ac_list = aca; 319 320 /* Hold this for addrconf_join_solict() below before we unlock, 321 * it is already exposed via idev->ac_list. 322 */ 323 aca_get(aca); 324 write_unlock_bh(&idev->lock); 325 326 ipv6_add_acaddr_hash(net, aca); 327 328 ip6_ins_rt(net, f6i); 329 330 addrconf_join_solict(idev->dev, &aca->aca_addr); 331 332 aca_put(aca); 333 return 0; 334 out: 335 write_unlock_bh(&idev->lock); 336 return err; 337 } 338 339 /* 340 * device anycast group decrement 341 */ 342 int __ipv6_dev_ac_dec(struct inet6_dev *idev, const struct in6_addr *addr) 343 { 344 struct ifacaddr6 *aca, *prev_aca; 345 346 ASSERT_RTNL(); 347 348 write_lock_bh(&idev->lock); 349 prev_aca = NULL; 350 for (aca = idev->ac_list; aca; aca = aca->aca_next) { 351 if (ipv6_addr_equal(&aca->aca_addr, addr)) 352 break; 353 prev_aca = aca; 354 } 355 if (!aca) { 356 write_unlock_bh(&idev->lock); 357 return -ENOENT; 358 } 359 if (--aca->aca_users > 0) { 360 write_unlock_bh(&idev->lock); 361 return 0; 362 } 363 if (prev_aca) 364 prev_aca->aca_next = aca->aca_next; 365 else 366 idev->ac_list = aca->aca_next; 367 write_unlock_bh(&idev->lock); 368 ipv6_del_acaddr_hash(aca); 369 addrconf_leave_solict(idev, &aca->aca_addr); 370 371 ip6_del_rt(dev_net(idev->dev), aca->aca_rt); 372 373 aca_put(aca); 374 return 0; 375 } 376 377 /* called with rtnl_lock() */ 378 static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr) 379 { 380 struct inet6_dev *idev = __in6_dev_get(dev); 381 382 if (!idev) 383 return -ENODEV; 384 return __ipv6_dev_ac_dec(idev, addr); 385 } 386 387 void ipv6_ac_destroy_dev(struct inet6_dev *idev) 388 { 389 struct ifacaddr6 *aca; 390 391 write_lock_bh(&idev->lock); 392 while ((aca = idev->ac_list) != NULL) { 393 idev->ac_list = aca->aca_next; 394 write_unlock_bh(&idev->lock); 395 396 ipv6_del_acaddr_hash(aca); 397 398 addrconf_leave_solict(idev, &aca->aca_addr); 399 400 ip6_del_rt(dev_net(idev->dev), aca->aca_rt); 401 402 aca_put(aca); 403 404 write_lock_bh(&idev->lock); 405 } 406 write_unlock_bh(&idev->lock); 407 } 408 409 /* 410 * check if the interface has this anycast address 411 * called with rcu_read_lock() 412 */ 413 static bool ipv6_chk_acast_dev(struct net_device *dev, const struct in6_addr *addr) 414 { 415 struct inet6_dev *idev; 416 struct ifacaddr6 *aca; 417 418 idev = __in6_dev_get(dev); 419 if (idev) { 420 read_lock_bh(&idev->lock); 421 for (aca = idev->ac_list; aca; aca = aca->aca_next) 422 if (ipv6_addr_equal(&aca->aca_addr, addr)) 423 break; 424 read_unlock_bh(&idev->lock); 425 return aca != NULL; 426 } 427 return false; 428 } 429 430 /* 431 * check if given interface (or any, if dev==0) has this anycast address 432 */ 433 bool ipv6_chk_acast_addr(struct net *net, struct net_device *dev, 434 const struct in6_addr *addr) 435 { 436 struct net_device *nh_dev; 437 struct ifacaddr6 *aca; 438 bool found = false; 439 440 rcu_read_lock(); 441 if (dev) 442 found = ipv6_chk_acast_dev(dev, addr); 443 else { 444 unsigned int hash = inet6_acaddr_hash(net, addr); 445 446 hlist_for_each_entry_rcu(aca, &inet6_acaddr_lst[hash], 447 aca_addr_lst) { 448 nh_dev = fib6_info_nh_dev(aca->aca_rt); 449 if (!nh_dev || !net_eq(dev_net(nh_dev), net)) 450 continue; 451 if (ipv6_addr_equal(&aca->aca_addr, addr)) { 452 found = true; 453 break; 454 } 455 } 456 } 457 rcu_read_unlock(); 458 return found; 459 } 460 461 /* check if this anycast address is link-local on given interface or 462 * is global 463 */ 464 bool ipv6_chk_acast_addr_src(struct net *net, struct net_device *dev, 465 const struct in6_addr *addr) 466 { 467 return ipv6_chk_acast_addr(net, 468 (ipv6_addr_type(addr) & IPV6_ADDR_LINKLOCAL ? 469 dev : NULL), 470 addr); 471 } 472 473 #ifdef CONFIG_PROC_FS 474 struct ac6_iter_state { 475 struct seq_net_private p; 476 struct net_device *dev; 477 struct inet6_dev *idev; 478 }; 479 480 #define ac6_seq_private(seq) ((struct ac6_iter_state *)(seq)->private) 481 482 static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq) 483 { 484 struct ifacaddr6 *im = NULL; 485 struct ac6_iter_state *state = ac6_seq_private(seq); 486 struct net *net = seq_file_net(seq); 487 488 state->idev = NULL; 489 for_each_netdev_rcu(net, state->dev) { 490 struct inet6_dev *idev; 491 idev = __in6_dev_get(state->dev); 492 if (!idev) 493 continue; 494 read_lock_bh(&idev->lock); 495 im = idev->ac_list; 496 if (im) { 497 state->idev = idev; 498 break; 499 } 500 read_unlock_bh(&idev->lock); 501 } 502 return im; 503 } 504 505 static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im) 506 { 507 struct ac6_iter_state *state = ac6_seq_private(seq); 508 509 im = im->aca_next; 510 while (!im) { 511 if (likely(state->idev != NULL)) 512 read_unlock_bh(&state->idev->lock); 513 514 state->dev = next_net_device_rcu(state->dev); 515 if (!state->dev) { 516 state->idev = NULL; 517 break; 518 } 519 state->idev = __in6_dev_get(state->dev); 520 if (!state->idev) 521 continue; 522 read_lock_bh(&state->idev->lock); 523 im = state->idev->ac_list; 524 } 525 return im; 526 } 527 528 static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos) 529 { 530 struct ifacaddr6 *im = ac6_get_first(seq); 531 if (im) 532 while (pos && (im = ac6_get_next(seq, im)) != NULL) 533 --pos; 534 return pos ? NULL : im; 535 } 536 537 static void *ac6_seq_start(struct seq_file *seq, loff_t *pos) 538 __acquires(RCU) 539 { 540 rcu_read_lock(); 541 return ac6_get_idx(seq, *pos); 542 } 543 544 static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 545 { 546 struct ifacaddr6 *im = ac6_get_next(seq, v); 547 548 ++*pos; 549 return im; 550 } 551 552 static void ac6_seq_stop(struct seq_file *seq, void *v) 553 __releases(RCU) 554 { 555 struct ac6_iter_state *state = ac6_seq_private(seq); 556 557 if (likely(state->idev != NULL)) { 558 read_unlock_bh(&state->idev->lock); 559 state->idev = NULL; 560 } 561 rcu_read_unlock(); 562 } 563 564 static int ac6_seq_show(struct seq_file *seq, void *v) 565 { 566 struct ifacaddr6 *im = (struct ifacaddr6 *)v; 567 struct ac6_iter_state *state = ac6_seq_private(seq); 568 569 seq_printf(seq, "%-4d %-15s %pi6 %5d\n", 570 state->dev->ifindex, state->dev->name, 571 &im->aca_addr, im->aca_users); 572 return 0; 573 } 574 575 static const struct seq_operations ac6_seq_ops = { 576 .start = ac6_seq_start, 577 .next = ac6_seq_next, 578 .stop = ac6_seq_stop, 579 .show = ac6_seq_show, 580 }; 581 582 int __net_init ac6_proc_init(struct net *net) 583 { 584 if (!proc_create_net("anycast6", 0444, net->proc_net, &ac6_seq_ops, 585 sizeof(struct ac6_iter_state))) 586 return -ENOMEM; 587 588 return 0; 589 } 590 591 void ac6_proc_exit(struct net *net) 592 { 593 remove_proc_entry("anycast6", net->proc_net); 594 } 595 #endif 596 597 /* Init / cleanup code 598 */ 599 int __init ipv6_anycast_init(void) 600 { 601 int i; 602 603 for (i = 0; i < IN6_ADDR_HSIZE; i++) 604 INIT_HLIST_HEAD(&inet6_acaddr_lst[i]); 605 return 0; 606 } 607 608 void ipv6_anycast_cleanup(void) 609 { 610 int i; 611 612 spin_lock(&acaddr_hash_lock); 613 for (i = 0; i < IN6_ADDR_HSIZE; i++) 614 WARN_ON(!hlist_empty(&inet6_acaddr_lst[i])); 615 spin_unlock(&acaddr_hash_lock); 616 } 617