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
23 #include <net/sock.h>
24
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>
33
34 #include <asm/uaccess.h>
35
36 #define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified
37 in old IPv6 RFC. Well, it was reasonable value.
38 */
39 #define FL_MAX_LINGER 60 /* Maximal linger timeout */
40
41 /* FL hash table */
42
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)
47
48 static atomic_t fl_size = ATOMIC_INIT(0);
49 static struct ip6_flowlabel *fl_ht[FL_HASH_MASK+1];
50
51 static struct timer_list ip6_fl_gc_timer;
52
53 /* FL hash table lock: it protects only of GC */
54
55 static rwlock_t ip6_fl_lock = RW_LOCK_UNLOCKED;
56
57 /* Big socket sock */
58
59 static rwlock_t ip6_sk_fl_lock = RW_LOCK_UNLOCKED;
60
61
__fl_lookup(u32 label)62 static __inline__ struct ip6_flowlabel * __fl_lookup(u32 label)
63 {
64 struct ip6_flowlabel *fl;
65
66 for (fl=fl_ht[FL_HASH(label)]; fl; fl = fl->next) {
67 if (fl->label == label)
68 return fl;
69 }
70 return NULL;
71 }
72
fl_lookup(u32 label)73 static struct ip6_flowlabel * fl_lookup(u32 label)
74 {
75 struct ip6_flowlabel *fl;
76
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;
83 }
84
85
fl_free(struct ip6_flowlabel * fl)86 static void fl_free(struct ip6_flowlabel *fl)
87 {
88 if (fl->opt)
89 kfree(fl->opt);
90 kfree(fl);
91 }
92
fl_release(struct ip6_flowlabel * fl)93 static void fl_release(struct ip6_flowlabel *fl)
94 {
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);
105 }
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);
110 }
111 }
112
ip6_fl_gc(unsigned long dummy)113 static void ip6_fl_gc(unsigned long dummy)
114 {
115 int i;
116 unsigned long now = jiffies;
117 unsigned long sched = 0;
118
119 write_lock(&ip6_fl_lock);
120
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;
135 }
136 if (!sched || (long)(ttd - sched) < 0)
137 sched = ttd;
138 }
139 flp = &fl->next;
140 }
141 }
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);
147 }
148 write_unlock(&ip6_fl_lock);
149 }
150
fl_intern(struct ip6_flowlabel * fl,__u32 label)151 static int fl_intern(struct ip6_flowlabel *fl, __u32 label)
152 {
153 fl->label = label & IPV6_FLOWLABEL_MASK;
154
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;
164 }
165 }
166 }
167
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;
174 }
175
176
177
178 /* Socket flowlabel lists */
179
fl6_sock_lookup(struct sock * sk,u32 label)180 struct ip6_flowlabel * fl6_sock_lookup(struct sock *sk, u32 label)
181 {
182 struct ipv6_fl_socklist *sfl;
183 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6;
184
185 label &= IPV6_FLOWLABEL_MASK;
186
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;
193 }
194 }
195 return NULL;
196 }
197
fl6_free_socklist(struct sock * sk)198 void fl6_free_socklist(struct sock *sk)
199 {
200 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6;
201 struct ipv6_fl_socklist *sfl;
202
203 while ((sfl = np->ipv6_fl_list) != NULL) {
204 np->ipv6_fl_list = sfl->next;
205 fl_release(sfl->fl);
206 kfree(sfl);
207 }
208 }
209
210 /* Service routines */
211
212
213 /*
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.
217 */
218
fl6_merge_options(struct ipv6_txoptions * opt_space,struct ip6_flowlabel * fl,struct ipv6_txoptions * fopt)219 struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions * opt_space,
220 struct ip6_flowlabel * fl,
221 struct ipv6_txoptions * fopt)
222 {
223 struct ipv6_txoptions * fl_opt = fl->opt;
224
225 if (fopt == NULL || fopt->opt_flen == 0)
226 return fl_opt;
227
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;
240 }
241 opt_space->dst1opt = fopt->dst1opt;
242 opt_space->auth = fopt->auth;
243 opt_space->opt_flen = fopt->opt_flen;
244 return opt_space;
245 }
246
check_linger(__u16 ttl)247 static __u32 check_linger(__u16 ttl)
248 {
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;
254 }
255
fl6_renew(struct ip6_flowlabel * fl,unsigned linger,unsigned expires)256 static int fl6_renew(struct ip6_flowlabel *fl, unsigned linger, unsigned expires)
257 {
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;
272 }
273
274 static struct ip6_flowlabel *
fl_create(struct in6_flowlabel_req * freq,char * optval,int optlen,int * err_p)275 fl_create(struct in6_flowlabel_req *freq, char *optval, int optlen, int *err_p)
276 {
277 struct ip6_flowlabel *fl = NULL;
278 int olen;
279 int addr_type;
280 int err;
281
282 olen = optlen - CMSG_ALIGN(sizeof(*freq));
283 err = -EINVAL;
284 if (olen > 64 * 1024)
285 goto done;
286
287 err = -ENOMEM;
288 fl = kmalloc(sizeof(*fl), GFP_KERNEL);
289 if (fl == NULL)
290 goto done;
291 memset(fl, 0, sizeof(*fl));
292
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);
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 if (fl)
354 fl_free(fl);
355 *err_p = err;
356 return NULL;
357 }
358
mem_check(struct sock * sk)359 static int mem_check(struct sock *sk)
360 {
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 = sk->net_pinfo.af_inet6.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
ipv6_hdr_cmp(struct ipv6_opt_hdr * h1,struct ipv6_opt_hdr * h2)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
ipv6_opt_cmp(struct ipv6_txoptions * o1,struct ipv6_txoptions * o2)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
ipv6_flowlabel_opt(struct sock * sk,char * optval,int optlen)408 int ipv6_flowlabel_opt(struct sock *sk, char *optval, int optlen)
409 {
410 int err;
411 struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6;
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_cmp(&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 /* Do not check for fault */
538 if (!freq.flr_label)
539 copy_to_user(optval + ((u8*)&freq.flr_label - (u8*)&freq), &fl->label, sizeof(fl->label));
540
541 sfl1->fl = fl;
542 sfl1->next = np->ipv6_fl_list;
543 np->ipv6_fl_list = sfl1;
544 return 0;
545
546 default:
547 return -EINVAL;
548 }
549
550 done:
551 if (fl)
552 fl_free(fl);
553 if (sfl1)
554 kfree(sfl1);
555 return err;
556 }
557
558 #ifdef CONFIG_PROC_FS
559
560
ip6_fl_read_proc(char * buffer,char ** start,off_t offset,int length,int * eof,void * data)561 static int ip6_fl_read_proc(char *buffer, char **start, off_t offset,
562 int length, int *eof, void *data)
563 {
564 off_t pos=0;
565 off_t begin=0;
566 int len=0;
567 int i, k;
568 struct ip6_flowlabel *fl;
569
570 len+= sprintf(buffer,"Label S Owner Users Linger Expires "
571 "Dst Opt\n");
572
573 read_lock_bh(&ip6_fl_lock);
574 for (i=0; i<=FL_HASH_MASK; i++) {
575 for (fl = fl_ht[i]; fl; fl = fl->next) {
576 len+=sprintf(buffer+len,"%05X %-1d %-6d %-6d %-6d %-8ld ",
577 (unsigned)ntohl(fl->label),
578 fl->share,
579 (unsigned)fl->owner,
580 atomic_read(&fl->users),
581 fl->linger/HZ,
582 (long)(fl->expires - jiffies)/HZ);
583
584 for (k=0; k<16; k++)
585 len+=sprintf(buffer+len, "%02x", fl->dst.s6_addr[k]);
586 buffer[len++]=' ';
587 len+=sprintf(buffer+len, "%-4d", fl->opt ? fl->opt->opt_nflen : 0);
588 buffer[len++]='\n';
589
590 pos=begin+len;
591 if(pos<offset) {
592 len=0;
593 begin=pos;
594 }
595 if(pos>offset+length)
596 goto done;
597 }
598 }
599 *eof = 1;
600
601 done:
602 read_unlock_bh(&ip6_fl_lock);
603 *start=buffer+(offset-begin);
604 len-=(offset-begin);
605 if(len>length)
606 len=length;
607 if(len<0)
608 len=0;
609 return len;
610 }
611 #endif
612
613
ip6_flowlabel_init()614 void ip6_flowlabel_init()
615 {
616 init_timer(&ip6_fl_gc_timer);
617 ip6_fl_gc_timer.function = ip6_fl_gc;
618 #ifdef CONFIG_PROC_FS
619 create_proc_read_entry("net/ip6_flowlabel", 0, 0, ip6_fl_read_proc, NULL);
620 #endif
621 }
622
ip6_flowlabel_cleanup()623 void ip6_flowlabel_cleanup()
624 {
625 del_timer(&ip6_fl_gc_timer);
626 #ifdef CONFIG_PROC_FS
627 remove_proc_entry("net/ip6_flowlabel", 0);
628 #endif
629 }
630