Import 2.3.41pre2
[davej-history.git] / net / ipv6 / ip6_flowlabel.c
blob4dd29b60e0a25ae86700b55d67d921b0ac37d269
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/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>
23 #include <net/sock.h>
25 #include <net/ipv6.h>
26 #include <net/ndisc.h>
27 #include <net/protocol.h>
28 #include <net/ip6_route.h>
29 #include <net/addrconf.h>
30 #include <net/rawv6.h>
31 #include <net/icmp.h>
32 #include <net/transp_v6.h>
34 #include <asm/uaccess.h>
36 #define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified
37 in old IPv6 RFC. Well, it was reasonable value.
39 #define FL_MAX_LINGER 60 /* Maximal linger timeout */
41 /* FL hash table */
43 #define FL_MAX_PER_SOCK 32
44 #define FL_MAX_SIZE 4096
45 #define FL_HASH_MASK 255
46 #define FL_HASH(l) (ntohl(l)&FL_HASH_MASK)
48 static atomic_t fl_size = ATOMIC_INIT(0);
49 static struct ip6_flowlabel *fl_ht[FL_HASH_MASK+1];
51 static struct timer_list ip6_fl_gc_timer;
53 /* FL hash table lock: it protects only of GC */
55 static rwlock_t ip6_fl_lock = RW_LOCK_UNLOCKED;
57 /* Big socket sock */
59 static rwlock_t ip6_sk_fl_lock = RW_LOCK_UNLOCKED;
62 static __inline__ struct ip6_flowlabel * __fl_lookup(u32 label)
64 struct ip6_flowlabel *fl;
66 for (fl=fl_ht[FL_HASH(label)]; fl; fl = fl->next) {
67 if (fl->label == label)
68 return fl;
70 return NULL;
73 static struct ip6_flowlabel * fl_lookup(u32 label)
75 struct ip6_flowlabel *fl;
77 read_lock_bh(&ip6_fl_lock);
78 fl = __fl_lookup(label);
79 if (fl)
80 atomic_inc(&fl->users);
81 read_unlock_bh(&ip6_fl_lock);
82 return fl;
86 static void fl_free(struct ip6_flowlabel *fl)
88 if (fl->opt)
89 kfree(fl->opt);
90 kfree(fl);
93 static void fl_release(struct ip6_flowlabel *fl)
95 fl->lastuse = jiffies;
96 if (atomic_dec_and_test(&fl->users)) {
97 unsigned long ttd = fl->lastuse + fl->linger;
98 if ((long)(ttd - fl->expires) > 0)
99 fl->expires = ttd;
100 ttd = fl->expires;
101 if (fl->opt && fl->share == IPV6_FL_S_EXCL) {
102 struct ipv6_txoptions *opt = fl->opt;
103 fl->opt = NULL;
104 kfree(opt);
106 if (!del_timer(&ip6_fl_gc_timer) ||
107 (long)(ip6_fl_gc_timer.expires - ttd) > 0)
108 ip6_fl_gc_timer.expires = ttd;
109 add_timer(&ip6_fl_gc_timer);
113 static void ip6_fl_gc(unsigned long dummy)
115 int i;
116 unsigned long now = jiffies;
117 unsigned long sched = 0;
119 write_lock(&ip6_fl_lock);
121 for (i=0; i<=FL_HASH_MASK; i++) {
122 struct ip6_flowlabel *fl, **flp;
123 flp = &fl_ht[i];
124 while ((fl=*flp) != NULL) {
125 if (atomic_read(&fl->users) == 0) {
126 unsigned long ttd = fl->lastuse + fl->linger;
127 if ((long)(ttd - fl->expires) > 0)
128 fl->expires = ttd;
129 ttd = fl->expires;
130 if ((long)(now - ttd) >= 0) {
131 *flp = fl->next;
132 fl_free(fl);
133 atomic_dec(&fl_size);
134 continue;
136 if (!sched || (long)(ttd - sched) < 0)
137 sched = ttd;
139 flp = &fl->next;
142 if (!sched && atomic_read(&fl_size))
143 sched = now + FL_MAX_LINGER;
144 if (sched) {
145 ip6_fl_gc_timer.expires = sched;
146 add_timer(&ip6_fl_gc_timer);
148 write_unlock(&ip6_fl_lock);
151 static int fl_intern(struct ip6_flowlabel *fl, __u32 label)
153 fl->label = label & IPV6_FLOWLABEL_MASK;
155 write_lock_bh(&ip6_fl_lock);
156 if (label == 0) {
157 for (;;) {
158 fl->label = htonl(net_random())&IPV6_FLOWLABEL_MASK;
159 if (fl->label) {
160 struct ip6_flowlabel *lfl;
161 lfl = __fl_lookup(fl->label);
162 if (lfl == NULL)
163 break;
168 fl->lastuse = jiffies;
169 fl->next = fl_ht[FL_HASH(fl->label)];
170 fl_ht[FL_HASH(fl->label)] = fl;
171 atomic_inc(&fl_size);
172 write_unlock_bh(&ip6_fl_lock);
173 return 0;
178 /* Socket flowlabel lists */
180 struct ip6_flowlabel * fl6_sock_lookup(struct sock *sk, u32 label)
182 struct ipv6_fl_socklist *sfl;
183 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6;
185 label &= IPV6_FLOWLABEL_MASK;
187 for (sfl=np->ipv6_fl_list; sfl; sfl = sfl->next) {
188 struct ip6_flowlabel *fl = sfl->fl;
189 if (fl->label == label) {
190 fl->lastuse = jiffies;
191 atomic_inc(&fl->users);
192 return fl;
195 return NULL;
198 void fl6_free_socklist(struct sock *sk)
200 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6;
201 struct ipv6_fl_socklist *sfl;
203 while ((sfl = np->ipv6_fl_list) != NULL) {
204 np->ipv6_fl_list = sfl->next;
205 fl_release(sfl->fl);
206 kfree(sfl);
210 /* Service routines */
214 It is the only difficult place. flowlabel enforces equal headers
215 before and including routing header, however user may supply options
216 following rthdr.
219 struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions * opt_space,
220 struct ip6_flowlabel * fl,
221 struct ipv6_txoptions * fopt)
223 struct ipv6_txoptions * fl_opt = fl->opt;
225 if (fopt == NULL || fopt->opt_flen == 0)
226 return fl_opt;
228 if (fl_opt != NULL) {
229 opt_space->hopopt = fl_opt->hopopt;
230 opt_space->dst0opt = fl_opt->dst0opt;
231 opt_space->srcrt = fl_opt->srcrt;
232 opt_space->opt_nflen = fl_opt->opt_nflen;
233 } else {
234 if (fopt->opt_nflen == 0)
235 return fopt;
236 opt_space->hopopt = NULL;
237 opt_space->dst0opt = NULL;
238 opt_space->srcrt = NULL;
239 opt_space->opt_nflen = 0;
241 opt_space->dst1opt = fopt->dst1opt;
242 opt_space->auth = fopt->auth;
243 opt_space->opt_flen = fopt->opt_flen;
244 return opt_space;
247 static __u32 check_linger(__u16 ttl)
249 if (ttl < FL_MIN_LINGER)
250 return FL_MIN_LINGER*HZ;
251 if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN))
252 return 0;
253 return ttl*HZ;
256 static int fl6_renew(struct ip6_flowlabel *fl, unsigned linger, unsigned expires)
258 linger = check_linger(linger);
259 if (!linger)
260 return -EPERM;
261 expires = check_linger(expires);
262 if (!expires)
263 return -EPERM;
264 fl->lastuse = jiffies;
265 if (fl->linger < linger)
266 fl->linger = linger;
267 if (expires < fl->linger)
268 expires = fl->linger;
269 if ((long)(fl->expires - (fl->lastuse+expires)) < 0)
270 fl->expires = fl->lastuse + expires;
271 return 0;
274 static struct ip6_flowlabel *
275 fl_create(struct in6_flowlabel_req *freq, char *optval, int optlen, int *err_p)
277 struct ip6_flowlabel *fl;
278 int olen;
279 int addr_type;
280 int err;
282 err = -ENOMEM;
283 fl = kmalloc(sizeof(*fl), GFP_KERNEL);
284 if (fl == NULL)
285 goto done;
286 memset(fl, 0, sizeof(*fl));
288 olen = optlen - CMSG_ALIGN(sizeof(*freq));
289 if (olen > 0) {
290 struct msghdr msg;
291 struct flowi flowi;
292 int junk;
294 err = -ENOMEM;
295 fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
296 if (fl->opt == NULL)
297 goto done;
299 memset(fl->opt, 0, sizeof(*fl->opt));
300 fl->opt->tot_len = sizeof(*fl->opt) + olen;
301 err = -EFAULT;
302 if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen))
303 goto done;
305 msg.msg_controllen = olen;
306 msg.msg_control = (void*)(fl->opt+1);
307 flowi.oif = 0;
309 err = datagram_send_ctl(&msg, &flowi, fl->opt, &junk);
310 if (err)
311 goto done;
312 err = -EINVAL;
313 if (fl->opt->opt_flen)
314 goto done;
315 if (fl->opt->opt_nflen == 0) {
316 kfree(fl->opt);
317 fl->opt = NULL;
321 fl->expires = jiffies;
322 err = fl6_renew(fl, freq->flr_linger, freq->flr_expires);
323 if (err)
324 goto done;
325 fl->share = freq->flr_share;
326 addr_type = ipv6_addr_type(&freq->flr_dst);
327 if ((addr_type&IPV6_ADDR_MAPPED)
328 || addr_type == IPV6_ADDR_ANY)
329 goto done;
330 ipv6_addr_copy(&fl->dst, &freq->flr_dst);
331 atomic_set(&fl->users, 1);
332 switch (fl->share) {
333 case IPV6_FL_S_EXCL:
334 case IPV6_FL_S_ANY:
335 break;
336 case IPV6_FL_S_PROCESS:
337 fl->owner = current->pid;
338 break;
339 case IPV6_FL_S_USER:
340 fl->owner = current->euid;
341 break;
342 default:
343 err = -EINVAL;
344 goto done;
346 return fl;
348 done:
349 if (fl)
350 fl_free(fl);
351 *err_p = err;
352 return NULL;
355 static int mem_check(struct sock *sk)
357 struct ipv6_fl_socklist *sfl;
358 int room = FL_MAX_SIZE - atomic_read(&fl_size);
359 int count = 0;
361 if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
362 return 0;
364 for (sfl = sk->net_pinfo.af_inet6.ipv6_fl_list; sfl; sfl = sfl->next)
365 count++;
367 if (room <= 0 ||
368 ((count >= FL_MAX_PER_SOCK ||
369 (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4)
370 && !capable(CAP_NET_ADMIN)))
371 return -ENOBUFS;
373 return 0;
376 static int ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2)
378 if (h1 == h2)
379 return 0;
380 if (h1 == NULL || h2 == NULL)
381 return 1;
382 if (h1->hdrlen != h2->hdrlen)
383 return 1;
384 return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1));
387 static int ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2)
389 if (o1 == o2)
390 return 0;
391 if (o1 == NULL || o2 == NULL)
392 return 1;
393 if (o1->opt_nflen != o2->opt_nflen)
394 return 1;
395 if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt))
396 return 1;
397 if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt))
398 return 1;
399 if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt))
400 return 1;
401 return 0;
404 int ipv6_flowlabel_opt(struct sock *sk, char *optval, int optlen)
406 int err;
407 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6;
408 struct in6_flowlabel_req freq;
409 struct ipv6_fl_socklist *sfl1=NULL;
410 struct ipv6_fl_socklist *sfl, **sflp;
411 struct ip6_flowlabel *fl;
413 if (optlen < sizeof(freq))
414 return -EINVAL;
416 if (copy_from_user(&freq, optval, sizeof(freq)))
417 return -EFAULT;
419 switch (freq.flr_action) {
420 case IPV6_FL_A_PUT:
421 write_lock_bh(&ip6_sk_fl_lock);
422 for (sflp = &np->ipv6_fl_list; (sfl=*sflp)!=NULL; sflp = &sfl->next) {
423 if (sfl->fl->label == freq.flr_label) {
424 if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK))
425 np->flow_label &= ~IPV6_FLOWLABEL_MASK;
426 *sflp = sfl->next;
427 write_unlock_bh(&ip6_sk_fl_lock);
428 fl_release(sfl->fl);
429 kfree(sfl);
430 return 0;
433 write_unlock_bh(&ip6_sk_fl_lock);
434 return -ESRCH;
436 case IPV6_FL_A_RENEW:
437 read_lock_bh(&ip6_sk_fl_lock);
438 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
439 if (sfl->fl->label == freq.flr_label)
440 return fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires);
442 read_unlock_bh(&ip6_sk_fl_lock);
444 if (freq.flr_share == IPV6_FL_S_NONE && capable(CAP_NET_ADMIN)) {
445 fl = fl_lookup(freq.flr_label);
446 if (fl) {
447 err = fl6_renew(fl, freq.flr_linger, freq.flr_expires);
448 fl_release(fl);
449 return err;
452 return -ESRCH;
454 case IPV6_FL_A_GET:
455 if (freq.flr_label & ~IPV6_FLOWLABEL_MASK)
456 return -EINVAL;
458 fl = fl_create(&freq, optval, optlen, &err);
459 if (fl == NULL)
460 return err;
461 sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL);
463 if (freq.flr_label) {
464 struct ip6_flowlabel *fl1 = NULL;
466 err = -EEXIST;
467 read_lock_bh(&ip6_sk_fl_lock);
468 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
469 if (sfl->fl->label == freq.flr_label) {
470 if (freq.flr_flags&IPV6_FL_F_EXCL) {
471 read_unlock_bh(&ip6_sk_fl_lock);
472 goto done;
474 fl1 = sfl->fl;
475 atomic_inc(&fl->users);
476 break;
479 read_unlock_bh(&ip6_sk_fl_lock);
481 if (fl1 == NULL)
482 fl1 = fl_lookup(freq.flr_label);
483 if (fl1) {
484 err = -EEXIST;
485 if (freq.flr_flags&IPV6_FL_F_EXCL)
486 goto release;
487 err = -EPERM;
488 if (fl1->share == IPV6_FL_S_EXCL ||
489 fl1->share != fl->share ||
490 fl1->owner != fl->owner)
491 goto release;
493 err = -EINVAL;
494 if (ipv6_addr_cmp(&fl1->dst, &fl->dst) ||
495 ipv6_opt_cmp(fl1->opt, fl->opt))
496 goto release;
498 err = -ENOMEM;
499 if (sfl1 == NULL)
500 goto release;
501 if (fl->linger > fl1->linger)
502 fl1->linger = fl->linger;
503 if ((long)(fl->expires - fl1->expires) > 0)
504 fl1->expires = fl->expires;
505 write_lock_bh(&ip6_sk_fl_lock);
506 sfl1->fl = fl1;
507 sfl1->next = np->ipv6_fl_list;
508 np->ipv6_fl_list = sfl1;
509 write_unlock_bh(&ip6_sk_fl_lock);
510 fl_free(fl);
511 return 0;
513 release:
514 fl_release(fl1);
515 goto done;
518 err = -ENOENT;
519 if (!(freq.flr_flags&IPV6_FL_F_CREATE))
520 goto done;
522 err = -ENOMEM;
523 if (sfl1 == NULL || (err = mem_check(sk)) != 0)
524 goto done;
526 err = fl_intern(fl, freq.flr_label);
527 if (err)
528 goto done;
530 /* Do not check for fault */
531 if (!freq.flr_label)
532 copy_to_user(optval + ((u8*)&freq.flr_label - (u8*)&freq), &fl->label, sizeof(fl->label));
534 sfl1->fl = fl;
535 sfl1->next = np->ipv6_fl_list;
536 np->ipv6_fl_list = sfl1;
537 return 0;
539 default:
540 return -EINVAL;
543 done:
544 if (fl)
545 fl_free(fl);
546 if (sfl1)
547 kfree(sfl1);
548 return err;
551 #ifdef CONFIG_PROC_FS
554 static int ip6_fl_read_proc(char *buffer, char **start, off_t offset,
555 int length, int *eof, void *data)
557 off_t pos=0;
558 off_t begin=0;
559 int len=0;
560 int i, k;
561 struct ip6_flowlabel *fl;
563 len+= sprintf(buffer,"Label S Owner Users Linger Expires "
564 "Dst Opt\n");
566 read_lock_bh(&ip6_fl_lock);
567 for (i=0; i<=FL_HASH_MASK; i++) {
568 for (fl = fl_ht[i]; fl; fl = fl->next) {
569 len+=sprintf(buffer+len,"%05X %-1d %-6d %-6d %-6d %-8ld ",
570 (unsigned)ntohl(fl->label),
571 fl->share,
572 (unsigned)fl->owner,
573 atomic_read(&fl->users),
574 fl->linger/HZ,
575 (long)(fl->expires - jiffies)/HZ);
577 for (k=0; k<16; k++)
578 len+=sprintf(buffer+len, "%02x", fl->dst.s6_addr[k]);
579 buffer[len++]=' ';
580 len+=sprintf(buffer+len, "%-4d", fl->opt ? fl->opt->opt_nflen : 0);
581 buffer[len++]='\n';
583 pos=begin+len;
584 if(pos<offset) {
585 len=0;
586 begin=pos;
588 if(pos>offset+length)
589 goto done;
592 *eof = 1;
594 done:
595 read_unlock_bh(&ip6_fl_lock);
596 *start=buffer+(offset-begin);
597 len-=(offset-begin);
598 if(len>length)
599 len=length;
600 if(len<0)
601 len=0;
602 return len;
604 #endif
607 void ip6_flowlabel_init()
609 init_timer(&ip6_fl_gc_timer);
610 ip6_fl_gc_timer.function = ip6_fl_gc;
611 #ifdef CONFIG_PROC_FS
612 create_proc_read_entry("net/ip6_flowlabel", 0, 0, ip6_fl_read_proc, NULL);
613 #endif
616 void ip6_flowlabel_cleanup()
618 del_timer(&ip6_fl_gc_timer);
619 #ifdef CONFIG_PROC_FS
620 remove_proc_entry("net/ip6_flowlabel", 0);
621 #endif