1 /* 2 * ip6_flowlabel.c IPv6 flowlabel manager. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 7 * 2 of the License, or (at your option) any later version. 8 * 9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 10 */ 11 12 #include <linux/config.h> 13 #include <linux/errno.h> 14 #include <linux/types.h> 15 #include <linux/socket.h> 16 #include <linux/net.h> 17 #include <linux/netdevice.h> 18 #include <linux/if_arp.h> 19 #include <linux/in6.h> 20 #include <linux/route.h> 21 #include <linux/proc_fs.h> 22 #include <linux/seq_file.h> 23 24 #include <net/sock.h> 25 26 #include <net/ipv6.h> 27 #include <net/ndisc.h> 28 #include <net/protocol.h> 29 #include <net/ip6_route.h> 30 #include <net/addrconf.h> 31 #include <net/rawv6.h> 32 #include <net/icmp.h> 33 #include <net/transp_v6.h> 34 35 #include <asm/uaccess.h> 36 37 #define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified 38 in old IPv6 RFC. Well, it was reasonable value. 39 */ 40 #define FL_MAX_LINGER 60 /* Maximal linger timeout */ 41 42 /* FL hash table */ 43 44 #define FL_MAX_PER_SOCK 32 45 #define FL_MAX_SIZE 4096 46 #define FL_HASH_MASK 255 47 #define FL_HASH(l) (ntohl(l)&FL_HASH_MASK) 48 49 static atomic_t fl_size = ATOMIC_INIT(0); 50 static struct ip6_flowlabel *fl_ht[FL_HASH_MASK+1]; 51 52 static void ip6_fl_gc(unsigned long dummy); 53 static DEFINE_TIMER(ip6_fl_gc_timer, ip6_fl_gc, 0, 0); 54 55 /* FL hash table lock: it protects only of GC */ 56 57 static DEFINE_RWLOCK(ip6_fl_lock); 58 59 /* Big socket sock */ 60 61 static DEFINE_RWLOCK(ip6_sk_fl_lock); 62 63 64 static __inline__ struct ip6_flowlabel * __fl_lookup(u32 label) 65 { 66 struct ip6_flowlabel *fl; 67 68 for (fl=fl_ht[FL_HASH(label)]; fl; fl = fl->next) { 69 if (fl->label == label) 70 return fl; 71 } 72 return NULL; 73 } 74 75 static struct ip6_flowlabel * fl_lookup(u32 label) 76 { 77 struct ip6_flowlabel *fl; 78 79 read_lock_bh(&ip6_fl_lock); 80 fl = __fl_lookup(label); 81 if (fl) 82 atomic_inc(&fl->users); 83 read_unlock_bh(&ip6_fl_lock); 84 return fl; 85 } 86 87 88 static void fl_free(struct ip6_flowlabel *fl) 89 { 90 if (fl) 91 kfree(fl->opt); 92 kfree(fl); 93 } 94 95 static void fl_release(struct ip6_flowlabel *fl) 96 { 97 write_lock_bh(&ip6_fl_lock); 98 99 fl->lastuse = jiffies; 100 if (atomic_dec_and_test(&fl->users)) { 101 unsigned long ttd = fl->lastuse + fl->linger; 102 if (time_after(ttd, fl->expires)) 103 fl->expires = ttd; 104 ttd = fl->expires; 105 if (fl->opt && fl->share == IPV6_FL_S_EXCL) { 106 struct ipv6_txoptions *opt = fl->opt; 107 fl->opt = NULL; 108 kfree(opt); 109 } 110 if (!timer_pending(&ip6_fl_gc_timer) || 111 time_after(ip6_fl_gc_timer.expires, ttd)) 112 mod_timer(&ip6_fl_gc_timer, ttd); 113 } 114 115 write_unlock_bh(&ip6_fl_lock); 116 } 117 118 static void ip6_fl_gc(unsigned long dummy) 119 { 120 int i; 121 unsigned long now = jiffies; 122 unsigned long sched = 0; 123 124 write_lock(&ip6_fl_lock); 125 126 for (i=0; i<=FL_HASH_MASK; i++) { 127 struct ip6_flowlabel *fl, **flp; 128 flp = &fl_ht[i]; 129 while ((fl=*flp) != NULL) { 130 if (atomic_read(&fl->users) == 0) { 131 unsigned long ttd = fl->lastuse + fl->linger; 132 if (time_after(ttd, fl->expires)) 133 fl->expires = ttd; 134 ttd = fl->expires; 135 if (time_after_eq(now, ttd)) { 136 *flp = fl->next; 137 fl_free(fl); 138 atomic_dec(&fl_size); 139 continue; 140 } 141 if (!sched || time_before(ttd, sched)) 142 sched = ttd; 143 } 144 flp = &fl->next; 145 } 146 } 147 if (!sched && atomic_read(&fl_size)) 148 sched = now + FL_MAX_LINGER; 149 if (sched) { 150 ip6_fl_gc_timer.expires = sched; 151 add_timer(&ip6_fl_gc_timer); 152 } 153 write_unlock(&ip6_fl_lock); 154 } 155 156 static int fl_intern(struct ip6_flowlabel *fl, __u32 label) 157 { 158 fl->label = label & IPV6_FLOWLABEL_MASK; 159 160 write_lock_bh(&ip6_fl_lock); 161 if (label == 0) { 162 for (;;) { 163 fl->label = htonl(net_random())&IPV6_FLOWLABEL_MASK; 164 if (fl->label) { 165 struct ip6_flowlabel *lfl; 166 lfl = __fl_lookup(fl->label); 167 if (lfl == NULL) 168 break; 169 } 170 } 171 } 172 173 fl->lastuse = jiffies; 174 fl->next = fl_ht[FL_HASH(fl->label)]; 175 fl_ht[FL_HASH(fl->label)] = fl; 176 atomic_inc(&fl_size); 177 write_unlock_bh(&ip6_fl_lock); 178 return 0; 179 } 180 181 182 183 /* Socket flowlabel lists */ 184 185 struct ip6_flowlabel * fl6_sock_lookup(struct sock *sk, u32 label) 186 { 187 struct ipv6_fl_socklist *sfl; 188 struct ipv6_pinfo *np = inet6_sk(sk); 189 190 label &= IPV6_FLOWLABEL_MASK; 191 192 for (sfl=np->ipv6_fl_list; sfl; sfl = sfl->next) { 193 struct ip6_flowlabel *fl = sfl->fl; 194 if (fl->label == label) { 195 fl->lastuse = jiffies; 196 atomic_inc(&fl->users); 197 return fl; 198 } 199 } 200 return NULL; 201 } 202 203 void fl6_free_socklist(struct sock *sk) 204 { 205 struct ipv6_pinfo *np = inet6_sk(sk); 206 struct ipv6_fl_socklist *sfl; 207 208 while ((sfl = np->ipv6_fl_list) != NULL) { 209 np->ipv6_fl_list = sfl->next; 210 fl_release(sfl->fl); 211 kfree(sfl); 212 } 213 } 214 215 /* Service routines */ 216 217 218 /* 219 It is the only difficult place. flowlabel enforces equal headers 220 before and including routing header, however user may supply options 221 following rthdr. 222 */ 223 224 struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions * opt_space, 225 struct ip6_flowlabel * fl, 226 struct ipv6_txoptions * fopt) 227 { 228 struct ipv6_txoptions * fl_opt = fl->opt; 229 230 if (fopt == NULL || fopt->opt_flen == 0) 231 return fl_opt; 232 233 if (fl_opt != NULL) { 234 opt_space->hopopt = fl_opt->hopopt; 235 opt_space->dst0opt = fl_opt->dst0opt; 236 opt_space->srcrt = fl_opt->srcrt; 237 opt_space->opt_nflen = fl_opt->opt_nflen; 238 } else { 239 if (fopt->opt_nflen == 0) 240 return fopt; 241 opt_space->hopopt = NULL; 242 opt_space->dst0opt = NULL; 243 opt_space->srcrt = NULL; 244 opt_space->opt_nflen = 0; 245 } 246 opt_space->dst1opt = fopt->dst1opt; 247 opt_space->opt_flen = fopt->opt_flen; 248 return opt_space; 249 } 250 251 static unsigned long check_linger(unsigned long ttl) 252 { 253 if (ttl < FL_MIN_LINGER) 254 return FL_MIN_LINGER*HZ; 255 if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN)) 256 return 0; 257 return ttl*HZ; 258 } 259 260 static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires) 261 { 262 linger = check_linger(linger); 263 if (!linger) 264 return -EPERM; 265 expires = check_linger(expires); 266 if (!expires) 267 return -EPERM; 268 fl->lastuse = jiffies; 269 if (time_before(fl->linger, linger)) 270 fl->linger = linger; 271 if (time_before(expires, fl->linger)) 272 expires = fl->linger; 273 if (time_before(fl->expires, fl->lastuse + expires)) 274 fl->expires = fl->lastuse + expires; 275 return 0; 276 } 277 278 static struct ip6_flowlabel * 279 fl_create(struct in6_flowlabel_req *freq, char __user *optval, int optlen, int *err_p) 280 { 281 struct ip6_flowlabel *fl; 282 int olen; 283 int addr_type; 284 int err; 285 286 err = -ENOMEM; 287 fl = kmalloc(sizeof(*fl), GFP_KERNEL); 288 if (fl == NULL) 289 goto done; 290 memset(fl, 0, sizeof(*fl)); 291 292 olen = optlen - CMSG_ALIGN(sizeof(*freq)); 293 if (olen > 0) { 294 struct msghdr msg; 295 struct flowi flowi; 296 int junk; 297 298 err = -ENOMEM; 299 fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL); 300 if (fl->opt == NULL) 301 goto done; 302 303 memset(fl->opt, 0, sizeof(*fl->opt)); 304 fl->opt->tot_len = sizeof(*fl->opt) + olen; 305 err = -EFAULT; 306 if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen)) 307 goto done; 308 309 msg.msg_controllen = olen; 310 msg.msg_control = (void*)(fl->opt+1); 311 flowi.oif = 0; 312 313 err = datagram_send_ctl(&msg, &flowi, fl->opt, &junk, &junk); 314 if (err) 315 goto done; 316 err = -EINVAL; 317 if (fl->opt->opt_flen) 318 goto done; 319 if (fl->opt->opt_nflen == 0) { 320 kfree(fl->opt); 321 fl->opt = NULL; 322 } 323 } 324 325 fl->expires = jiffies; 326 err = fl6_renew(fl, freq->flr_linger, freq->flr_expires); 327 if (err) 328 goto done; 329 fl->share = freq->flr_share; 330 addr_type = ipv6_addr_type(&freq->flr_dst); 331 if ((addr_type&IPV6_ADDR_MAPPED) 332 || addr_type == IPV6_ADDR_ANY) 333 goto done; 334 ipv6_addr_copy(&fl->dst, &freq->flr_dst); 335 atomic_set(&fl->users, 1); 336 switch (fl->share) { 337 case IPV6_FL_S_EXCL: 338 case IPV6_FL_S_ANY: 339 break; 340 case IPV6_FL_S_PROCESS: 341 fl->owner = current->pid; 342 break; 343 case IPV6_FL_S_USER: 344 fl->owner = current->euid; 345 break; 346 default: 347 err = -EINVAL; 348 goto done; 349 } 350 return fl; 351 352 done: 353 fl_free(fl); 354 *err_p = err; 355 return NULL; 356 } 357 358 static int mem_check(struct sock *sk) 359 { 360 struct ipv6_pinfo *np = inet6_sk(sk); 361 struct ipv6_fl_socklist *sfl; 362 int room = FL_MAX_SIZE - atomic_read(&fl_size); 363 int count = 0; 364 365 if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK) 366 return 0; 367 368 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) 369 count++; 370 371 if (room <= 0 || 372 ((count >= FL_MAX_PER_SOCK || 373 (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4) 374 && !capable(CAP_NET_ADMIN))) 375 return -ENOBUFS; 376 377 return 0; 378 } 379 380 static int ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2) 381 { 382 if (h1 == h2) 383 return 0; 384 if (h1 == NULL || h2 == NULL) 385 return 1; 386 if (h1->hdrlen != h2->hdrlen) 387 return 1; 388 return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1)); 389 } 390 391 static int ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2) 392 { 393 if (o1 == o2) 394 return 0; 395 if (o1 == NULL || o2 == NULL) 396 return 1; 397 if (o1->opt_nflen != o2->opt_nflen) 398 return 1; 399 if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt)) 400 return 1; 401 if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt)) 402 return 1; 403 if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt)) 404 return 1; 405 return 0; 406 } 407 408 int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen) 409 { 410 int err; 411 struct ipv6_pinfo *np = inet6_sk(sk); 412 struct in6_flowlabel_req freq; 413 struct ipv6_fl_socklist *sfl1=NULL; 414 struct ipv6_fl_socklist *sfl, **sflp; 415 struct ip6_flowlabel *fl; 416 417 if (optlen < sizeof(freq)) 418 return -EINVAL; 419 420 if (copy_from_user(&freq, optval, sizeof(freq))) 421 return -EFAULT; 422 423 switch (freq.flr_action) { 424 case IPV6_FL_A_PUT: 425 write_lock_bh(&ip6_sk_fl_lock); 426 for (sflp = &np->ipv6_fl_list; (sfl=*sflp)!=NULL; sflp = &sfl->next) { 427 if (sfl->fl->label == freq.flr_label) { 428 if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK)) 429 np->flow_label &= ~IPV6_FLOWLABEL_MASK; 430 *sflp = sfl->next; 431 write_unlock_bh(&ip6_sk_fl_lock); 432 fl_release(sfl->fl); 433 kfree(sfl); 434 return 0; 435 } 436 } 437 write_unlock_bh(&ip6_sk_fl_lock); 438 return -ESRCH; 439 440 case IPV6_FL_A_RENEW: 441 read_lock_bh(&ip6_sk_fl_lock); 442 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) { 443 if (sfl->fl->label == freq.flr_label) { 444 err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires); 445 read_unlock_bh(&ip6_sk_fl_lock); 446 return err; 447 } 448 } 449 read_unlock_bh(&ip6_sk_fl_lock); 450 451 if (freq.flr_share == IPV6_FL_S_NONE && capable(CAP_NET_ADMIN)) { 452 fl = fl_lookup(freq.flr_label); 453 if (fl) { 454 err = fl6_renew(fl, freq.flr_linger, freq.flr_expires); 455 fl_release(fl); 456 return err; 457 } 458 } 459 return -ESRCH; 460 461 case IPV6_FL_A_GET: 462 if (freq.flr_label & ~IPV6_FLOWLABEL_MASK) 463 return -EINVAL; 464 465 fl = fl_create(&freq, optval, optlen, &err); 466 if (fl == NULL) 467 return err; 468 sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL); 469 470 if (freq.flr_label) { 471 struct ip6_flowlabel *fl1 = NULL; 472 473 err = -EEXIST; 474 read_lock_bh(&ip6_sk_fl_lock); 475 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) { 476 if (sfl->fl->label == freq.flr_label) { 477 if (freq.flr_flags&IPV6_FL_F_EXCL) { 478 read_unlock_bh(&ip6_sk_fl_lock); 479 goto done; 480 } 481 fl1 = sfl->fl; 482 atomic_inc(&fl1->users); 483 break; 484 } 485 } 486 read_unlock_bh(&ip6_sk_fl_lock); 487 488 if (fl1 == NULL) 489 fl1 = fl_lookup(freq.flr_label); 490 if (fl1) { 491 err = -EEXIST; 492 if (freq.flr_flags&IPV6_FL_F_EXCL) 493 goto release; 494 err = -EPERM; 495 if (fl1->share == IPV6_FL_S_EXCL || 496 fl1->share != fl->share || 497 fl1->owner != fl->owner) 498 goto release; 499 500 err = -EINVAL; 501 if (!ipv6_addr_equal(&fl1->dst, &fl->dst) || 502 ipv6_opt_cmp(fl1->opt, fl->opt)) 503 goto release; 504 505 err = -ENOMEM; 506 if (sfl1 == NULL) 507 goto release; 508 if (fl->linger > fl1->linger) 509 fl1->linger = fl->linger; 510 if ((long)(fl->expires - fl1->expires) > 0) 511 fl1->expires = fl->expires; 512 write_lock_bh(&ip6_sk_fl_lock); 513 sfl1->fl = fl1; 514 sfl1->next = np->ipv6_fl_list; 515 np->ipv6_fl_list = sfl1; 516 write_unlock_bh(&ip6_sk_fl_lock); 517 fl_free(fl); 518 return 0; 519 520 release: 521 fl_release(fl1); 522 goto done; 523 } 524 } 525 err = -ENOENT; 526 if (!(freq.flr_flags&IPV6_FL_F_CREATE)) 527 goto done; 528 529 err = -ENOMEM; 530 if (sfl1 == NULL || (err = mem_check(sk)) != 0) 531 goto done; 532 533 err = fl_intern(fl, freq.flr_label); 534 if (err) 535 goto done; 536 537 if (!freq.flr_label) { 538 if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label, 539 &fl->label, sizeof(fl->label))) { 540 /* Intentionally ignore fault. */ 541 } 542 } 543 544 sfl1->fl = fl; 545 sfl1->next = np->ipv6_fl_list; 546 np->ipv6_fl_list = sfl1; 547 return 0; 548 549 default: 550 return -EINVAL; 551 } 552 553 done: 554 fl_free(fl); 555 kfree(sfl1); 556 return err; 557 } 558 559 #ifdef CONFIG_PROC_FS 560 561 struct ip6fl_iter_state { 562 int bucket; 563 }; 564 565 #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private) 566 567 static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq) 568 { 569 struct ip6_flowlabel *fl = NULL; 570 struct ip6fl_iter_state *state = ip6fl_seq_private(seq); 571 572 for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) { 573 if (fl_ht[state->bucket]) { 574 fl = fl_ht[state->bucket]; 575 break; 576 } 577 } 578 return fl; 579 } 580 581 static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl) 582 { 583 struct ip6fl_iter_state *state = ip6fl_seq_private(seq); 584 585 fl = fl->next; 586 while (!fl) { 587 if (++state->bucket <= FL_HASH_MASK) 588 fl = fl_ht[state->bucket]; 589 } 590 return fl; 591 } 592 593 static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos) 594 { 595 struct ip6_flowlabel *fl = ip6fl_get_first(seq); 596 if (fl) 597 while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL) 598 --pos; 599 return pos ? NULL : fl; 600 } 601 602 static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos) 603 { 604 read_lock_bh(&ip6_fl_lock); 605 return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 606 } 607 608 static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos) 609 { 610 struct ip6_flowlabel *fl; 611 612 if (v == SEQ_START_TOKEN) 613 fl = ip6fl_get_first(seq); 614 else 615 fl = ip6fl_get_next(seq, v); 616 ++*pos; 617 return fl; 618 } 619 620 static void ip6fl_seq_stop(struct seq_file *seq, void *v) 621 { 622 read_unlock_bh(&ip6_fl_lock); 623 } 624 625 static void ip6fl_fl_seq_show(struct seq_file *seq, struct ip6_flowlabel *fl) 626 { 627 while(fl) { 628 seq_printf(seq, 629 "%05X %-1d %-6d %-6d %-6ld %-8ld " 630 "%02x%02x%02x%02x%02x%02x%02x%02x " 631 "%-4d\n", 632 (unsigned)ntohl(fl->label), 633 fl->share, 634 (unsigned)fl->owner, 635 atomic_read(&fl->users), 636 fl->linger/HZ, 637 (long)(fl->expires - jiffies)/HZ, 638 NIP6(fl->dst), 639 fl->opt ? fl->opt->opt_nflen : 0); 640 fl = fl->next; 641 } 642 } 643 644 static int ip6fl_seq_show(struct seq_file *seq, void *v) 645 { 646 if (v == SEQ_START_TOKEN) 647 seq_puts(seq, "Label S Owner Users Linger Expires " 648 "Dst Opt\n"); 649 else 650 ip6fl_fl_seq_show(seq, v); 651 return 0; 652 } 653 654 static struct seq_operations ip6fl_seq_ops = { 655 .start = ip6fl_seq_start, 656 .next = ip6fl_seq_next, 657 .stop = ip6fl_seq_stop, 658 .show = ip6fl_seq_show, 659 }; 660 661 static int ip6fl_seq_open(struct inode *inode, struct file *file) 662 { 663 struct seq_file *seq; 664 int rc = -ENOMEM; 665 struct ip6fl_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL); 666 667 if (!s) 668 goto out; 669 670 rc = seq_open(file, &ip6fl_seq_ops); 671 if (rc) 672 goto out_kfree; 673 674 seq = file->private_data; 675 seq->private = s; 676 memset(s, 0, sizeof(*s)); 677 out: 678 return rc; 679 out_kfree: 680 kfree(s); 681 goto out; 682 } 683 684 static struct file_operations ip6fl_seq_fops = { 685 .owner = THIS_MODULE, 686 .open = ip6fl_seq_open, 687 .read = seq_read, 688 .llseek = seq_lseek, 689 .release = seq_release_private, 690 }; 691 #endif 692 693 694 void ip6_flowlabel_init(void) 695 { 696 #ifdef CONFIG_PROC_FS 697 proc_net_fops_create("ip6_flowlabel", S_IRUGO, &ip6fl_seq_fops); 698 #endif 699 } 700 701 void ip6_flowlabel_cleanup(void) 702 { 703 del_timer(&ip6_fl_gc_timer); 704 #ifdef CONFIG_PROC_FS 705 proc_net_remove("ip6_flowlabel"); 706 #endif 707 } 708