[MIPS] Remove unused system type name for DDB5074 and DDB5476.
[linux-2.6.git] / net / ipv6 / ip6_flowlabel.c
blobf9ca63912fbf378f3ed4d8bd1461776a5ccc3d33
1 /*
2 * ip6_flowlabel.c IPv6 flowlabel manager.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 #include <linux/capability.h>
13 #include <linux/config.h>
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/socket.h>
17 #include <linux/net.h>
18 #include <linux/netdevice.h>
19 #include <linux/if_arp.h>
20 #include <linux/in6.h>
21 #include <linux/route.h>
22 #include <linux/proc_fs.h>
23 #include <linux/seq_file.h>
25 #include <net/sock.h>
27 #include <net/ipv6.h>
28 #include <net/ndisc.h>
29 #include <net/protocol.h>
30 #include <net/ip6_route.h>
31 #include <net/addrconf.h>
32 #include <net/rawv6.h>
33 #include <net/icmp.h>
34 #include <net/transp_v6.h>
36 #include <asm/uaccess.h>
38 #define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified
39 in old IPv6 RFC. Well, it was reasonable value.
41 #define FL_MAX_LINGER 60 /* Maximal linger timeout */
43 /* FL hash table */
45 #define FL_MAX_PER_SOCK 32
46 #define FL_MAX_SIZE 4096
47 #define FL_HASH_MASK 255
48 #define FL_HASH(l) (ntohl(l)&FL_HASH_MASK)
50 static atomic_t fl_size = ATOMIC_INIT(0);
51 static struct ip6_flowlabel *fl_ht[FL_HASH_MASK+1];
53 static void ip6_fl_gc(unsigned long dummy);
54 static DEFINE_TIMER(ip6_fl_gc_timer, ip6_fl_gc, 0, 0);
56 /* FL hash table lock: it protects only of GC */
58 static DEFINE_RWLOCK(ip6_fl_lock);
60 /* Big socket sock */
62 static DEFINE_RWLOCK(ip6_sk_fl_lock);
65 static __inline__ struct ip6_flowlabel * __fl_lookup(u32 label)
67 struct ip6_flowlabel *fl;
69 for (fl=fl_ht[FL_HASH(label)]; fl; fl = fl->next) {
70 if (fl->label == label)
71 return fl;
73 return NULL;
76 static struct ip6_flowlabel * fl_lookup(u32 label)
78 struct ip6_flowlabel *fl;
80 read_lock_bh(&ip6_fl_lock);
81 fl = __fl_lookup(label);
82 if (fl)
83 atomic_inc(&fl->users);
84 read_unlock_bh(&ip6_fl_lock);
85 return fl;
89 static void fl_free(struct ip6_flowlabel *fl)
91 if (fl)
92 kfree(fl->opt);
93 kfree(fl);
96 static void fl_release(struct ip6_flowlabel *fl)
98 write_lock_bh(&ip6_fl_lock);
100 fl->lastuse = jiffies;
101 if (atomic_dec_and_test(&fl->users)) {
102 unsigned long ttd = fl->lastuse + fl->linger;
103 if (time_after(ttd, fl->expires))
104 fl->expires = ttd;
105 ttd = fl->expires;
106 if (fl->opt && fl->share == IPV6_FL_S_EXCL) {
107 struct ipv6_txoptions *opt = fl->opt;
108 fl->opt = NULL;
109 kfree(opt);
111 if (!timer_pending(&ip6_fl_gc_timer) ||
112 time_after(ip6_fl_gc_timer.expires, ttd))
113 mod_timer(&ip6_fl_gc_timer, ttd);
116 write_unlock_bh(&ip6_fl_lock);
119 static void ip6_fl_gc(unsigned long dummy)
121 int i;
122 unsigned long now = jiffies;
123 unsigned long sched = 0;
125 write_lock(&ip6_fl_lock);
127 for (i=0; i<=FL_HASH_MASK; i++) {
128 struct ip6_flowlabel *fl, **flp;
129 flp = &fl_ht[i];
130 while ((fl=*flp) != NULL) {
131 if (atomic_read(&fl->users) == 0) {
132 unsigned long ttd = fl->lastuse + fl->linger;
133 if (time_after(ttd, fl->expires))
134 fl->expires = ttd;
135 ttd = fl->expires;
136 if (time_after_eq(now, ttd)) {
137 *flp = fl->next;
138 fl_free(fl);
139 atomic_dec(&fl_size);
140 continue;
142 if (!sched || time_before(ttd, sched))
143 sched = ttd;
145 flp = &fl->next;
148 if (!sched && atomic_read(&fl_size))
149 sched = now + FL_MAX_LINGER;
150 if (sched) {
151 ip6_fl_gc_timer.expires = sched;
152 add_timer(&ip6_fl_gc_timer);
154 write_unlock(&ip6_fl_lock);
157 static int fl_intern(struct ip6_flowlabel *fl, __u32 label)
159 fl->label = label & IPV6_FLOWLABEL_MASK;
161 write_lock_bh(&ip6_fl_lock);
162 if (label == 0) {
163 for (;;) {
164 fl->label = htonl(net_random())&IPV6_FLOWLABEL_MASK;
165 if (fl->label) {
166 struct ip6_flowlabel *lfl;
167 lfl = __fl_lookup(fl->label);
168 if (lfl == NULL)
169 break;
174 fl->lastuse = jiffies;
175 fl->next = fl_ht[FL_HASH(fl->label)];
176 fl_ht[FL_HASH(fl->label)] = fl;
177 atomic_inc(&fl_size);
178 write_unlock_bh(&ip6_fl_lock);
179 return 0;
184 /* Socket flowlabel lists */
186 struct ip6_flowlabel * fl6_sock_lookup(struct sock *sk, u32 label)
188 struct ipv6_fl_socklist *sfl;
189 struct ipv6_pinfo *np = inet6_sk(sk);
191 label &= IPV6_FLOWLABEL_MASK;
193 for (sfl=np->ipv6_fl_list; sfl; sfl = sfl->next) {
194 struct ip6_flowlabel *fl = sfl->fl;
195 if (fl->label == label) {
196 fl->lastuse = jiffies;
197 atomic_inc(&fl->users);
198 return fl;
201 return NULL;
204 EXPORT_SYMBOL_GPL(fl6_sock_lookup);
206 void fl6_free_socklist(struct sock *sk)
208 struct ipv6_pinfo *np = inet6_sk(sk);
209 struct ipv6_fl_socklist *sfl;
211 while ((sfl = np->ipv6_fl_list) != NULL) {
212 np->ipv6_fl_list = sfl->next;
213 fl_release(sfl->fl);
214 kfree(sfl);
218 /* Service routines */
222 It is the only difficult place. flowlabel enforces equal headers
223 before and including routing header, however user may supply options
224 following rthdr.
227 struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions * opt_space,
228 struct ip6_flowlabel * fl,
229 struct ipv6_txoptions * fopt)
231 struct ipv6_txoptions * fl_opt = fl->opt;
233 if (fopt == NULL || fopt->opt_flen == 0)
234 return fl_opt;
236 if (fl_opt != NULL) {
237 opt_space->hopopt = fl_opt->hopopt;
238 opt_space->dst0opt = fl_opt->dst0opt;
239 opt_space->srcrt = fl_opt->srcrt;
240 opt_space->opt_nflen = fl_opt->opt_nflen;
241 } else {
242 if (fopt->opt_nflen == 0)
243 return fopt;
244 opt_space->hopopt = NULL;
245 opt_space->dst0opt = NULL;
246 opt_space->srcrt = NULL;
247 opt_space->opt_nflen = 0;
249 opt_space->dst1opt = fopt->dst1opt;
250 opt_space->opt_flen = fopt->opt_flen;
251 return opt_space;
254 static unsigned long check_linger(unsigned long ttl)
256 if (ttl < FL_MIN_LINGER)
257 return FL_MIN_LINGER*HZ;
258 if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN))
259 return 0;
260 return ttl*HZ;
263 static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires)
265 linger = check_linger(linger);
266 if (!linger)
267 return -EPERM;
268 expires = check_linger(expires);
269 if (!expires)
270 return -EPERM;
271 fl->lastuse = jiffies;
272 if (time_before(fl->linger, linger))
273 fl->linger = linger;
274 if (time_before(expires, fl->linger))
275 expires = fl->linger;
276 if (time_before(fl->expires, fl->lastuse + expires))
277 fl->expires = fl->lastuse + expires;
278 return 0;
281 static struct ip6_flowlabel *
282 fl_create(struct in6_flowlabel_req *freq, char __user *optval, int optlen, int *err_p)
284 struct ip6_flowlabel *fl;
285 int olen;
286 int addr_type;
287 int err;
289 err = -ENOMEM;
290 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
291 if (fl == NULL)
292 goto done;
294 olen = optlen - CMSG_ALIGN(sizeof(*freq));
295 if (olen > 0) {
296 struct msghdr msg;
297 struct flowi flowi;
298 int junk;
300 err = -ENOMEM;
301 fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
302 if (fl->opt == NULL)
303 goto done;
305 memset(fl->opt, 0, sizeof(*fl->opt));
306 fl->opt->tot_len = sizeof(*fl->opt) + olen;
307 err = -EFAULT;
308 if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen))
309 goto done;
311 msg.msg_controllen = olen;
312 msg.msg_control = (void*)(fl->opt+1);
313 flowi.oif = 0;
315 err = datagram_send_ctl(&msg, &flowi, fl->opt, &junk, &junk);
316 if (err)
317 goto done;
318 err = -EINVAL;
319 if (fl->opt->opt_flen)
320 goto done;
321 if (fl->opt->opt_nflen == 0) {
322 kfree(fl->opt);
323 fl->opt = NULL;
327 fl->expires = jiffies;
328 err = fl6_renew(fl, freq->flr_linger, freq->flr_expires);
329 if (err)
330 goto done;
331 fl->share = freq->flr_share;
332 addr_type = ipv6_addr_type(&freq->flr_dst);
333 if ((addr_type&IPV6_ADDR_MAPPED)
334 || addr_type == IPV6_ADDR_ANY)
335 goto done;
336 ipv6_addr_copy(&fl->dst, &freq->flr_dst);
337 atomic_set(&fl->users, 1);
338 switch (fl->share) {
339 case IPV6_FL_S_EXCL:
340 case IPV6_FL_S_ANY:
341 break;
342 case IPV6_FL_S_PROCESS:
343 fl->owner = current->pid;
344 break;
345 case IPV6_FL_S_USER:
346 fl->owner = current->euid;
347 break;
348 default:
349 err = -EINVAL;
350 goto done;
352 return fl;
354 done:
355 fl_free(fl);
356 *err_p = err;
357 return NULL;
360 static int mem_check(struct sock *sk)
362 struct ipv6_pinfo *np = inet6_sk(sk);
363 struct ipv6_fl_socklist *sfl;
364 int room = FL_MAX_SIZE - atomic_read(&fl_size);
365 int count = 0;
367 if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
368 return 0;
370 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next)
371 count++;
373 if (room <= 0 ||
374 ((count >= FL_MAX_PER_SOCK ||
375 (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4)
376 && !capable(CAP_NET_ADMIN)))
377 return -ENOBUFS;
379 return 0;
382 static int ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2)
384 if (h1 == h2)
385 return 0;
386 if (h1 == NULL || h2 == NULL)
387 return 1;
388 if (h1->hdrlen != h2->hdrlen)
389 return 1;
390 return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1));
393 static int ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2)
395 if (o1 == o2)
396 return 0;
397 if (o1 == NULL || o2 == NULL)
398 return 1;
399 if (o1->opt_nflen != o2->opt_nflen)
400 return 1;
401 if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt))
402 return 1;
403 if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt))
404 return 1;
405 if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt))
406 return 1;
407 return 0;
410 int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen)
412 int err;
413 struct ipv6_pinfo *np = inet6_sk(sk);
414 struct in6_flowlabel_req freq;
415 struct ipv6_fl_socklist *sfl1=NULL;
416 struct ipv6_fl_socklist *sfl, **sflp;
417 struct ip6_flowlabel *fl;
419 if (optlen < sizeof(freq))
420 return -EINVAL;
422 if (copy_from_user(&freq, optval, sizeof(freq)))
423 return -EFAULT;
425 switch (freq.flr_action) {
426 case IPV6_FL_A_PUT:
427 write_lock_bh(&ip6_sk_fl_lock);
428 for (sflp = &np->ipv6_fl_list; (sfl=*sflp)!=NULL; sflp = &sfl->next) {
429 if (sfl->fl->label == freq.flr_label) {
430 if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK))
431 np->flow_label &= ~IPV6_FLOWLABEL_MASK;
432 *sflp = sfl->next;
433 write_unlock_bh(&ip6_sk_fl_lock);
434 fl_release(sfl->fl);
435 kfree(sfl);
436 return 0;
439 write_unlock_bh(&ip6_sk_fl_lock);
440 return -ESRCH;
442 case IPV6_FL_A_RENEW:
443 read_lock_bh(&ip6_sk_fl_lock);
444 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
445 if (sfl->fl->label == freq.flr_label) {
446 err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires);
447 read_unlock_bh(&ip6_sk_fl_lock);
448 return err;
451 read_unlock_bh(&ip6_sk_fl_lock);
453 if (freq.flr_share == IPV6_FL_S_NONE && capable(CAP_NET_ADMIN)) {
454 fl = fl_lookup(freq.flr_label);
455 if (fl) {
456 err = fl6_renew(fl, freq.flr_linger, freq.flr_expires);
457 fl_release(fl);
458 return err;
461 return -ESRCH;
463 case IPV6_FL_A_GET:
464 if (freq.flr_label & ~IPV6_FLOWLABEL_MASK)
465 return -EINVAL;
467 fl = fl_create(&freq, optval, optlen, &err);
468 if (fl == NULL)
469 return err;
470 sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL);
472 if (freq.flr_label) {
473 struct ip6_flowlabel *fl1 = NULL;
475 err = -EEXIST;
476 read_lock_bh(&ip6_sk_fl_lock);
477 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
478 if (sfl->fl->label == freq.flr_label) {
479 if (freq.flr_flags&IPV6_FL_F_EXCL) {
480 read_unlock_bh(&ip6_sk_fl_lock);
481 goto done;
483 fl1 = sfl->fl;
484 atomic_inc(&fl1->users);
485 break;
488 read_unlock_bh(&ip6_sk_fl_lock);
490 if (fl1 == NULL)
491 fl1 = fl_lookup(freq.flr_label);
492 if (fl1) {
493 err = -EEXIST;
494 if (freq.flr_flags&IPV6_FL_F_EXCL)
495 goto release;
496 err = -EPERM;
497 if (fl1->share == IPV6_FL_S_EXCL ||
498 fl1->share != fl->share ||
499 fl1->owner != fl->owner)
500 goto release;
502 err = -EINVAL;
503 if (!ipv6_addr_equal(&fl1->dst, &fl->dst) ||
504 ipv6_opt_cmp(fl1->opt, fl->opt))
505 goto release;
507 err = -ENOMEM;
508 if (sfl1 == NULL)
509 goto release;
510 if (fl->linger > fl1->linger)
511 fl1->linger = fl->linger;
512 if ((long)(fl->expires - fl1->expires) > 0)
513 fl1->expires = fl->expires;
514 write_lock_bh(&ip6_sk_fl_lock);
515 sfl1->fl = fl1;
516 sfl1->next = np->ipv6_fl_list;
517 np->ipv6_fl_list = sfl1;
518 write_unlock_bh(&ip6_sk_fl_lock);
519 fl_free(fl);
520 return 0;
522 release:
523 fl_release(fl1);
524 goto done;
527 err = -ENOENT;
528 if (!(freq.flr_flags&IPV6_FL_F_CREATE))
529 goto done;
531 err = -ENOMEM;
532 if (sfl1 == NULL || (err = mem_check(sk)) != 0)
533 goto done;
535 err = fl_intern(fl, freq.flr_label);
536 if (err)
537 goto done;
539 if (!freq.flr_label) {
540 if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label,
541 &fl->label, sizeof(fl->label))) {
542 /* Intentionally ignore fault. */
546 sfl1->fl = fl;
547 sfl1->next = np->ipv6_fl_list;
548 np->ipv6_fl_list = sfl1;
549 return 0;
551 default:
552 return -EINVAL;
555 done:
556 fl_free(fl);
557 kfree(sfl1);
558 return err;
561 #ifdef CONFIG_PROC_FS
563 struct ip6fl_iter_state {
564 int bucket;
567 #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
569 static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq)
571 struct ip6_flowlabel *fl = NULL;
572 struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
574 for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) {
575 if (fl_ht[state->bucket]) {
576 fl = fl_ht[state->bucket];
577 break;
580 return fl;
583 static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl)
585 struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
587 fl = fl->next;
588 while (!fl) {
589 if (++state->bucket <= FL_HASH_MASK)
590 fl = fl_ht[state->bucket];
592 return fl;
595 static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos)
597 struct ip6_flowlabel *fl = ip6fl_get_first(seq);
598 if (fl)
599 while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL)
600 --pos;
601 return pos ? NULL : fl;
604 static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos)
606 read_lock_bh(&ip6_fl_lock);
607 return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
610 static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos)
612 struct ip6_flowlabel *fl;
614 if (v == SEQ_START_TOKEN)
615 fl = ip6fl_get_first(seq);
616 else
617 fl = ip6fl_get_next(seq, v);
618 ++*pos;
619 return fl;
622 static void ip6fl_seq_stop(struct seq_file *seq, void *v)
624 read_unlock_bh(&ip6_fl_lock);
627 static void ip6fl_fl_seq_show(struct seq_file *seq, struct ip6_flowlabel *fl)
629 while(fl) {
630 seq_printf(seq,
631 "%05X %-1d %-6d %-6d %-6ld %-8ld " NIP6_SEQFMT " %-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;
644 static int ip6fl_seq_show(struct seq_file *seq, void *v)
646 if (v == SEQ_START_TOKEN)
647 seq_printf(seq, "%-5s %-1s %-6s %-6s %-6s %-8s %-32s %s\n",
648 "Label", "S", "Owner", "Users", "Linger", "Expires", "Dst", "Opt");
649 else
650 ip6fl_fl_seq_show(seq, v);
651 return 0;
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,
661 static int ip6fl_seq_open(struct inode *inode, struct file *file)
663 struct seq_file *seq;
664 int rc = -ENOMEM;
665 struct ip6fl_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
667 if (!s)
668 goto out;
670 rc = seq_open(file, &ip6fl_seq_ops);
671 if (rc)
672 goto out_kfree;
674 seq = file->private_data;
675 seq->private = s;
676 out:
677 return rc;
678 out_kfree:
679 kfree(s);
680 goto out;
683 static struct file_operations ip6fl_seq_fops = {
684 .owner = THIS_MODULE,
685 .open = ip6fl_seq_open,
686 .read = seq_read,
687 .llseek = seq_lseek,
688 .release = seq_release_private,
690 #endif
693 void ip6_flowlabel_init(void)
695 #ifdef CONFIG_PROC_FS
696 proc_net_fops_create("ip6_flowlabel", S_IRUGO, &ip6fl_seq_fops);
697 #endif
700 void ip6_flowlabel_cleanup(void)
702 del_timer(&ip6_fl_gc_timer);
703 #ifdef CONFIG_PROC_FS
704 proc_net_remove("ip6_flowlabel");
705 #endif