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>
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>
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 */
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
);
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
)
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
);
83 atomic_inc(&fl
->users
);
84 read_unlock_bh(&ip6_fl_lock
);
89 static void fl_free(struct ip6_flowlabel
*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
))
106 if (fl
->opt
&& fl
->share
== IPV6_FL_S_EXCL
) {
107 struct ipv6_txoptions
*opt
= fl
->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
)
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
;
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
))
136 if (time_after_eq(now
, ttd
)) {
139 atomic_dec(&fl_size
);
142 if (!sched
|| time_before(ttd
, sched
))
148 if (!sched
&& atomic_read(&fl_size
))
149 sched
= now
+ FL_MAX_LINGER
;
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
);
164 fl
->label
= htonl(net_random())&IPV6_FLOWLABEL_MASK
;
166 struct ip6_flowlabel
*lfl
;
167 lfl
= __fl_lookup(fl
->label
);
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
);
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
);
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
;
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
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)
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
;
242 if (fopt
->opt_nflen
== 0)
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
;
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
))
263 static int fl6_renew(struct ip6_flowlabel
*fl
, unsigned long linger
, unsigned long expires
)
265 linger
= check_linger(linger
);
268 expires
= check_linger(expires
);
271 fl
->lastuse
= jiffies
;
272 if (time_before(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
;
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
;
290 fl
= kmalloc(sizeof(*fl
), GFP_KERNEL
);
293 memset(fl
, 0, sizeof(*fl
));
295 olen
= optlen
- CMSG_ALIGN(sizeof(*freq
));
302 fl
->opt
= kmalloc(sizeof(*fl
->opt
) + olen
, GFP_KERNEL
);
306 memset(fl
->opt
, 0, sizeof(*fl
->opt
));
307 fl
->opt
->tot_len
= sizeof(*fl
->opt
) + olen
;
309 if (copy_from_user(fl
->opt
+1, optval
+CMSG_ALIGN(sizeof(*freq
)), olen
))
312 msg
.msg_controllen
= olen
;
313 msg
.msg_control
= (void*)(fl
->opt
+1);
316 err
= datagram_send_ctl(&msg
, &flowi
, fl
->opt
, &junk
, &junk
);
320 if (fl
->opt
->opt_flen
)
322 if (fl
->opt
->opt_nflen
== 0) {
328 fl
->expires
= jiffies
;
329 err
= fl6_renew(fl
, freq
->flr_linger
, freq
->flr_expires
);
332 fl
->share
= freq
->flr_share
;
333 addr_type
= ipv6_addr_type(&freq
->flr_dst
);
334 if ((addr_type
&IPV6_ADDR_MAPPED
)
335 || addr_type
== IPV6_ADDR_ANY
)
337 ipv6_addr_copy(&fl
->dst
, &freq
->flr_dst
);
338 atomic_set(&fl
->users
, 1);
343 case IPV6_FL_S_PROCESS
:
344 fl
->owner
= current
->pid
;
347 fl
->owner
= current
->euid
;
361 static int mem_check(struct sock
*sk
)
363 struct ipv6_pinfo
*np
= inet6_sk(sk
);
364 struct ipv6_fl_socklist
*sfl
;
365 int room
= FL_MAX_SIZE
- atomic_read(&fl_size
);
368 if (room
> FL_MAX_SIZE
- FL_MAX_PER_SOCK
)
371 for (sfl
= np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
)
375 ((count
>= FL_MAX_PER_SOCK
||
376 (count
> 0 && room
< FL_MAX_SIZE
/2) || room
< FL_MAX_SIZE
/4)
377 && !capable(CAP_NET_ADMIN
)))
383 static int ipv6_hdr_cmp(struct ipv6_opt_hdr
*h1
, struct ipv6_opt_hdr
*h2
)
387 if (h1
== NULL
|| h2
== NULL
)
389 if (h1
->hdrlen
!= h2
->hdrlen
)
391 return memcmp(h1
+1, h2
+1, ((h1
->hdrlen
+1)<<3) - sizeof(*h1
));
394 static int ipv6_opt_cmp(struct ipv6_txoptions
*o1
, struct ipv6_txoptions
*o2
)
398 if (o1
== NULL
|| o2
== NULL
)
400 if (o1
->opt_nflen
!= o2
->opt_nflen
)
402 if (ipv6_hdr_cmp(o1
->hopopt
, o2
->hopopt
))
404 if (ipv6_hdr_cmp(o1
->dst0opt
, o2
->dst0opt
))
406 if (ipv6_hdr_cmp((struct ipv6_opt_hdr
*)o1
->srcrt
, (struct ipv6_opt_hdr
*)o2
->srcrt
))
411 int ipv6_flowlabel_opt(struct sock
*sk
, char __user
*optval
, int optlen
)
414 struct ipv6_pinfo
*np
= inet6_sk(sk
);
415 struct in6_flowlabel_req freq
;
416 struct ipv6_fl_socklist
*sfl1
=NULL
;
417 struct ipv6_fl_socklist
*sfl
, **sflp
;
418 struct ip6_flowlabel
*fl
;
420 if (optlen
< sizeof(freq
))
423 if (copy_from_user(&freq
, optval
, sizeof(freq
)))
426 switch (freq
.flr_action
) {
428 write_lock_bh(&ip6_sk_fl_lock
);
429 for (sflp
= &np
->ipv6_fl_list
; (sfl
=*sflp
)!=NULL
; sflp
= &sfl
->next
) {
430 if (sfl
->fl
->label
== freq
.flr_label
) {
431 if (freq
.flr_label
== (np
->flow_label
&IPV6_FLOWLABEL_MASK
))
432 np
->flow_label
&= ~IPV6_FLOWLABEL_MASK
;
434 write_unlock_bh(&ip6_sk_fl_lock
);
440 write_unlock_bh(&ip6_sk_fl_lock
);
443 case IPV6_FL_A_RENEW
:
444 read_lock_bh(&ip6_sk_fl_lock
);
445 for (sfl
= np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
) {
446 if (sfl
->fl
->label
== freq
.flr_label
) {
447 err
= fl6_renew(sfl
->fl
, freq
.flr_linger
, freq
.flr_expires
);
448 read_unlock_bh(&ip6_sk_fl_lock
);
452 read_unlock_bh(&ip6_sk_fl_lock
);
454 if (freq
.flr_share
== IPV6_FL_S_NONE
&& capable(CAP_NET_ADMIN
)) {
455 fl
= fl_lookup(freq
.flr_label
);
457 err
= fl6_renew(fl
, freq
.flr_linger
, freq
.flr_expires
);
465 if (freq
.flr_label
& ~IPV6_FLOWLABEL_MASK
)
468 fl
= fl_create(&freq
, optval
, optlen
, &err
);
471 sfl1
= kmalloc(sizeof(*sfl1
), GFP_KERNEL
);
473 if (freq
.flr_label
) {
474 struct ip6_flowlabel
*fl1
= NULL
;
477 read_lock_bh(&ip6_sk_fl_lock
);
478 for (sfl
= np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
) {
479 if (sfl
->fl
->label
== freq
.flr_label
) {
480 if (freq
.flr_flags
&IPV6_FL_F_EXCL
) {
481 read_unlock_bh(&ip6_sk_fl_lock
);
485 atomic_inc(&fl1
->users
);
489 read_unlock_bh(&ip6_sk_fl_lock
);
492 fl1
= fl_lookup(freq
.flr_label
);
495 if (freq
.flr_flags
&IPV6_FL_F_EXCL
)
498 if (fl1
->share
== IPV6_FL_S_EXCL
||
499 fl1
->share
!= fl
->share
||
500 fl1
->owner
!= fl
->owner
)
504 if (!ipv6_addr_equal(&fl1
->dst
, &fl
->dst
) ||
505 ipv6_opt_cmp(fl1
->opt
, fl
->opt
))
511 if (fl
->linger
> fl1
->linger
)
512 fl1
->linger
= fl
->linger
;
513 if ((long)(fl
->expires
- fl1
->expires
) > 0)
514 fl1
->expires
= fl
->expires
;
515 write_lock_bh(&ip6_sk_fl_lock
);
517 sfl1
->next
= np
->ipv6_fl_list
;
518 np
->ipv6_fl_list
= sfl1
;
519 write_unlock_bh(&ip6_sk_fl_lock
);
529 if (!(freq
.flr_flags
&IPV6_FL_F_CREATE
))
533 if (sfl1
== NULL
|| (err
= mem_check(sk
)) != 0)
536 err
= fl_intern(fl
, freq
.flr_label
);
540 if (!freq
.flr_label
) {
541 if (copy_to_user(&((struct in6_flowlabel_req __user
*) optval
)->flr_label
,
542 &fl
->label
, sizeof(fl
->label
))) {
543 /* Intentionally ignore fault. */
548 sfl1
->next
= np
->ipv6_fl_list
;
549 np
->ipv6_fl_list
= sfl1
;
562 #ifdef CONFIG_PROC_FS
564 struct ip6fl_iter_state
{
568 #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
570 static struct ip6_flowlabel
*ip6fl_get_first(struct seq_file
*seq
)
572 struct ip6_flowlabel
*fl
= NULL
;
573 struct ip6fl_iter_state
*state
= ip6fl_seq_private(seq
);
575 for (state
->bucket
= 0; state
->bucket
<= FL_HASH_MASK
; ++state
->bucket
) {
576 if (fl_ht
[state
->bucket
]) {
577 fl
= fl_ht
[state
->bucket
];
584 static struct ip6_flowlabel
*ip6fl_get_next(struct seq_file
*seq
, struct ip6_flowlabel
*fl
)
586 struct ip6fl_iter_state
*state
= ip6fl_seq_private(seq
);
590 if (++state
->bucket
<= FL_HASH_MASK
)
591 fl
= fl_ht
[state
->bucket
];
596 static struct ip6_flowlabel
*ip6fl_get_idx(struct seq_file
*seq
, loff_t pos
)
598 struct ip6_flowlabel
*fl
= ip6fl_get_first(seq
);
600 while (pos
&& (fl
= ip6fl_get_next(seq
, fl
)) != NULL
)
602 return pos
? NULL
: fl
;
605 static void *ip6fl_seq_start(struct seq_file
*seq
, loff_t
*pos
)
607 read_lock_bh(&ip6_fl_lock
);
608 return *pos
? ip6fl_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
611 static void *ip6fl_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
613 struct ip6_flowlabel
*fl
;
615 if (v
== SEQ_START_TOKEN
)
616 fl
= ip6fl_get_first(seq
);
618 fl
= ip6fl_get_next(seq
, v
);
623 static void ip6fl_seq_stop(struct seq_file
*seq
, void *v
)
625 read_unlock_bh(&ip6_fl_lock
);
628 static void ip6fl_fl_seq_show(struct seq_file
*seq
, struct ip6_flowlabel
*fl
)
632 "%05X %-1d %-6d %-6d %-6ld %-8ld " NIP6_SEQFMT
" %-4d\n",
633 (unsigned)ntohl(fl
->label
),
636 atomic_read(&fl
->users
),
638 (long)(fl
->expires
- jiffies
)/HZ
,
640 fl
->opt
? fl
->opt
->opt_nflen
: 0);
645 static int ip6fl_seq_show(struct seq_file
*seq
, void *v
)
647 if (v
== SEQ_START_TOKEN
)
648 seq_printf(seq
, "%-5s %-1s %-6s %-6s %-6s %-8s %-32s %s\n",
649 "Label", "S", "Owner", "Users", "Linger", "Expires", "Dst", "Opt");
651 ip6fl_fl_seq_show(seq
, v
);
655 static struct seq_operations ip6fl_seq_ops
= {
656 .start
= ip6fl_seq_start
,
657 .next
= ip6fl_seq_next
,
658 .stop
= ip6fl_seq_stop
,
659 .show
= ip6fl_seq_show
,
662 static int ip6fl_seq_open(struct inode
*inode
, struct file
*file
)
664 struct seq_file
*seq
;
666 struct ip6fl_iter_state
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
671 rc
= seq_open(file
, &ip6fl_seq_ops
);
675 seq
= file
->private_data
;
677 memset(s
, 0, sizeof(*s
));
685 static struct file_operations ip6fl_seq_fops
= {
686 .owner
= THIS_MODULE
,
687 .open
= ip6fl_seq_open
,
690 .release
= seq_release_private
,
695 void ip6_flowlabel_init(void)
697 #ifdef CONFIG_PROC_FS
698 proc_net_fops_create("ip6_flowlabel", S_IRUGO
, &ip6fl_seq_fops
);
702 void ip6_flowlabel_cleanup(void)
704 del_timer(&ip6_fl_gc_timer
);
705 #ifdef CONFIG_PROC_FS
706 proc_net_remove("ip6_flowlabel");