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>
22 #include <linux/seq_file.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>
33 #include <net/transp_v6.h>
35 #include <asm/uaccess.h>
37 #define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified
38 in old IPv6 RFC. Well, it was reasonable value.
40 #define FL_MAX_LINGER 60 /* Maximal linger timeout */
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)
49 static atomic_t fl_size
= ATOMIC_INIT(0);
50 static struct ip6_flowlabel
*fl_ht
[FL_HASH_MASK
+1];
52 static void ip6_fl_gc(unsigned long dummy
);
53 static struct timer_list ip6_fl_gc_timer
= TIMER_INITIALIZER(ip6_fl_gc
, 0, 0);
55 /* FL hash table lock: it protects only of GC */
57 static DEFINE_RWLOCK(ip6_fl_lock
);
61 static DEFINE_RWLOCK(ip6_sk_fl_lock
);
64 static __inline__
struct ip6_flowlabel
* __fl_lookup(u32 label
)
66 struct ip6_flowlabel
*fl
;
68 for (fl
=fl_ht
[FL_HASH(label
)]; fl
; fl
= fl
->next
) {
69 if (fl
->label
== label
)
75 static struct ip6_flowlabel
* fl_lookup(u32 label
)
77 struct ip6_flowlabel
*fl
;
79 read_lock_bh(&ip6_fl_lock
);
80 fl
= __fl_lookup(label
);
82 atomic_inc(&fl
->users
);
83 read_unlock_bh(&ip6_fl_lock
);
88 static void fl_free(struct ip6_flowlabel
*fl
)
95 static void fl_release(struct ip6_flowlabel
*fl
)
97 write_lock_bh(&ip6_fl_lock
);
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
))
105 if (fl
->opt
&& fl
->share
== IPV6_FL_S_EXCL
) {
106 struct ipv6_txoptions
*opt
= fl
->opt
;
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
);
115 write_unlock_bh(&ip6_fl_lock
);
118 static void ip6_fl_gc(unsigned long dummy
)
121 unsigned long now
= jiffies
;
122 unsigned long sched
= 0;
124 write_lock(&ip6_fl_lock
);
126 for (i
=0; i
<=FL_HASH_MASK
; i
++) {
127 struct ip6_flowlabel
*fl
, **flp
;
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
))
135 if (time_after_eq(now
, ttd
)) {
138 atomic_dec(&fl_size
);
141 if (!sched
|| time_before(ttd
, sched
))
147 if (!sched
&& atomic_read(&fl_size
))
148 sched
= now
+ FL_MAX_LINGER
;
150 ip6_fl_gc_timer
.expires
= sched
;
151 add_timer(&ip6_fl_gc_timer
);
153 write_unlock(&ip6_fl_lock
);
156 static int fl_intern(struct ip6_flowlabel
*fl
, __u32 label
)
158 fl
->label
= label
& IPV6_FLOWLABEL_MASK
;
160 write_lock_bh(&ip6_fl_lock
);
163 fl
->label
= htonl(net_random())&IPV6_FLOWLABEL_MASK
;
165 struct ip6_flowlabel
*lfl
;
166 lfl
= __fl_lookup(fl
->label
);
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
);
183 /* Socket flowlabel lists */
185 struct ip6_flowlabel
* fl6_sock_lookup(struct sock
*sk
, u32 label
)
187 struct ipv6_fl_socklist
*sfl
;
188 struct ipv6_pinfo
*np
= inet6_sk(sk
);
190 label
&= IPV6_FLOWLABEL_MASK
;
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
);
203 void fl6_free_socklist(struct sock
*sk
)
205 struct ipv6_pinfo
*np
= inet6_sk(sk
);
206 struct ipv6_fl_socklist
*sfl
;
208 while ((sfl
= np
->ipv6_fl_list
) != NULL
) {
209 np
->ipv6_fl_list
= sfl
->next
;
215 /* Service routines */
219 It is the only difficult place. flowlabel enforces equal headers
220 before and including routing header, however user may supply options
224 struct ipv6_txoptions
*fl6_merge_options(struct ipv6_txoptions
* opt_space
,
225 struct ip6_flowlabel
* fl
,
226 struct ipv6_txoptions
* fopt
)
228 struct ipv6_txoptions
* fl_opt
= fl
->opt
;
230 if (fopt
== NULL
|| fopt
->opt_flen
== 0)
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
;
239 if (fopt
->opt_nflen
== 0)
241 opt_space
->hopopt
= NULL
;
242 opt_space
->dst0opt
= NULL
;
243 opt_space
->srcrt
= NULL
;
244 opt_space
->opt_nflen
= 0;
246 opt_space
->dst1opt
= fopt
->dst1opt
;
247 opt_space
->auth
= fopt
->auth
;
248 opt_space
->opt_flen
= fopt
->opt_flen
;
252 static unsigned long check_linger(unsigned long ttl
)
254 if (ttl
< FL_MIN_LINGER
)
255 return FL_MIN_LINGER
*HZ
;
256 if (ttl
> FL_MAX_LINGER
&& !capable(CAP_NET_ADMIN
))
261 static int fl6_renew(struct ip6_flowlabel
*fl
, unsigned long linger
, unsigned long expires
)
263 linger
= check_linger(linger
);
266 expires
= check_linger(expires
);
269 fl
->lastuse
= jiffies
;
270 if (time_before(fl
->linger
, linger
))
272 if (time_before(expires
, fl
->linger
))
273 expires
= fl
->linger
;
274 if (time_before(fl
->expires
, fl
->lastuse
+ expires
))
275 fl
->expires
= fl
->lastuse
+ expires
;
279 static struct ip6_flowlabel
*
280 fl_create(struct in6_flowlabel_req
*freq
, char __user
*optval
, int optlen
, int *err_p
)
282 struct ip6_flowlabel
*fl
;
288 fl
= kmalloc(sizeof(*fl
), GFP_KERNEL
);
291 memset(fl
, 0, sizeof(*fl
));
293 olen
= optlen
- CMSG_ALIGN(sizeof(*freq
));
300 fl
->opt
= kmalloc(sizeof(*fl
->opt
) + olen
, GFP_KERNEL
);
304 memset(fl
->opt
, 0, sizeof(*fl
->opt
));
305 fl
->opt
->tot_len
= sizeof(*fl
->opt
) + olen
;
307 if (copy_from_user(fl
->opt
+1, optval
+CMSG_ALIGN(sizeof(*freq
)), olen
))
310 msg
.msg_controllen
= olen
;
311 msg
.msg_control
= (void*)(fl
->opt
+1);
314 err
= datagram_send_ctl(&msg
, &flowi
, fl
->opt
, &junk
);
318 if (fl
->opt
->opt_flen
)
320 if (fl
->opt
->opt_nflen
== 0) {
326 fl
->expires
= jiffies
;
327 err
= fl6_renew(fl
, freq
->flr_linger
, freq
->flr_expires
);
330 fl
->share
= freq
->flr_share
;
331 addr_type
= ipv6_addr_type(&freq
->flr_dst
);
332 if ((addr_type
&IPV6_ADDR_MAPPED
)
333 || addr_type
== IPV6_ADDR_ANY
)
335 ipv6_addr_copy(&fl
->dst
, &freq
->flr_dst
);
336 atomic_set(&fl
->users
, 1);
341 case IPV6_FL_S_PROCESS
:
342 fl
->owner
= current
->pid
;
345 fl
->owner
= current
->euid
;
359 static int mem_check(struct sock
*sk
)
361 struct ipv6_pinfo
*np
= inet6_sk(sk
);
362 struct ipv6_fl_socklist
*sfl
;
363 int room
= FL_MAX_SIZE
- atomic_read(&fl_size
);
366 if (room
> FL_MAX_SIZE
- FL_MAX_PER_SOCK
)
369 for (sfl
= np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
)
373 ((count
>= FL_MAX_PER_SOCK
||
374 (count
> 0 && room
< FL_MAX_SIZE
/2) || room
< FL_MAX_SIZE
/4)
375 && !capable(CAP_NET_ADMIN
)))
381 static int ipv6_hdr_cmp(struct ipv6_opt_hdr
*h1
, struct ipv6_opt_hdr
*h2
)
385 if (h1
== NULL
|| h2
== NULL
)
387 if (h1
->hdrlen
!= h2
->hdrlen
)
389 return memcmp(h1
+1, h2
+1, ((h1
->hdrlen
+1)<<3) - sizeof(*h1
));
392 static int ipv6_opt_cmp(struct ipv6_txoptions
*o1
, struct ipv6_txoptions
*o2
)
396 if (o1
== NULL
|| o2
== NULL
)
398 if (o1
->opt_nflen
!= o2
->opt_nflen
)
400 if (ipv6_hdr_cmp(o1
->hopopt
, o2
->hopopt
))
402 if (ipv6_hdr_cmp(o1
->dst0opt
, o2
->dst0opt
))
404 if (ipv6_hdr_cmp((struct ipv6_opt_hdr
*)o1
->srcrt
, (struct ipv6_opt_hdr
*)o2
->srcrt
))
409 int ipv6_flowlabel_opt(struct sock
*sk
, char __user
*optval
, int optlen
)
412 struct ipv6_pinfo
*np
= inet6_sk(sk
);
413 struct in6_flowlabel_req freq
;
414 struct ipv6_fl_socklist
*sfl1
=NULL
;
415 struct ipv6_fl_socklist
*sfl
, **sflp
;
416 struct ip6_flowlabel
*fl
;
418 if (optlen
< sizeof(freq
))
421 if (copy_from_user(&freq
, optval
, sizeof(freq
)))
424 switch (freq
.flr_action
) {
426 write_lock_bh(&ip6_sk_fl_lock
);
427 for (sflp
= &np
->ipv6_fl_list
; (sfl
=*sflp
)!=NULL
; sflp
= &sfl
->next
) {
428 if (sfl
->fl
->label
== freq
.flr_label
) {
429 if (freq
.flr_label
== (np
->flow_label
&IPV6_FLOWLABEL_MASK
))
430 np
->flow_label
&= ~IPV6_FLOWLABEL_MASK
;
432 write_unlock_bh(&ip6_sk_fl_lock
);
438 write_unlock_bh(&ip6_sk_fl_lock
);
441 case IPV6_FL_A_RENEW
:
442 read_lock_bh(&ip6_sk_fl_lock
);
443 for (sfl
= np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
) {
444 if (sfl
->fl
->label
== freq
.flr_label
) {
445 err
= fl6_renew(sfl
->fl
, freq
.flr_linger
, freq
.flr_expires
);
446 read_unlock_bh(&ip6_sk_fl_lock
);
450 read_unlock_bh(&ip6_sk_fl_lock
);
452 if (freq
.flr_share
== IPV6_FL_S_NONE
&& capable(CAP_NET_ADMIN
)) {
453 fl
= fl_lookup(freq
.flr_label
);
455 err
= fl6_renew(fl
, freq
.flr_linger
, freq
.flr_expires
);
463 if (freq
.flr_label
& ~IPV6_FLOWLABEL_MASK
)
466 fl
= fl_create(&freq
, optval
, optlen
, &err
);
469 sfl1
= kmalloc(sizeof(*sfl1
), GFP_KERNEL
);
471 if (freq
.flr_label
) {
472 struct ip6_flowlabel
*fl1
= NULL
;
475 read_lock_bh(&ip6_sk_fl_lock
);
476 for (sfl
= np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
) {
477 if (sfl
->fl
->label
== freq
.flr_label
) {
478 if (freq
.flr_flags
&IPV6_FL_F_EXCL
) {
479 read_unlock_bh(&ip6_sk_fl_lock
);
483 atomic_inc(&fl
->users
);
487 read_unlock_bh(&ip6_sk_fl_lock
);
490 fl1
= fl_lookup(freq
.flr_label
);
493 if (freq
.flr_flags
&IPV6_FL_F_EXCL
)
496 if (fl1
->share
== IPV6_FL_S_EXCL
||
497 fl1
->share
!= fl
->share
||
498 fl1
->owner
!= fl
->owner
)
502 if (!ipv6_addr_equal(&fl1
->dst
, &fl
->dst
) ||
503 ipv6_opt_cmp(fl1
->opt
, fl
->opt
))
509 if (fl
->linger
> fl1
->linger
)
510 fl1
->linger
= fl
->linger
;
511 if ((long)(fl
->expires
- fl1
->expires
) > 0)
512 fl1
->expires
= fl
->expires
;
513 write_lock_bh(&ip6_sk_fl_lock
);
515 sfl1
->next
= np
->ipv6_fl_list
;
516 np
->ipv6_fl_list
= sfl1
;
517 write_unlock_bh(&ip6_sk_fl_lock
);
527 if (!(freq
.flr_flags
&IPV6_FL_F_CREATE
))
531 if (sfl1
== NULL
|| (err
= mem_check(sk
)) != 0)
534 err
= fl_intern(fl
, freq
.flr_label
);
538 /* Do not check for fault */
540 copy_to_user(&((struct in6_flowlabel_req __user
*) optval
)->flr_label
,
541 &fl
->label
, sizeof(fl
->label
));
544 sfl1
->next
= np
->ipv6_fl_list
;
545 np
->ipv6_fl_list
= sfl1
;
558 #ifdef CONFIG_PROC_FS
560 struct ip6fl_iter_state
{
564 #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
566 static struct ip6_flowlabel
*ip6fl_get_first(struct seq_file
*seq
)
568 struct ip6_flowlabel
*fl
= NULL
;
569 struct ip6fl_iter_state
*state
= ip6fl_seq_private(seq
);
571 for (state
->bucket
= 0; state
->bucket
<= FL_HASH_MASK
; ++state
->bucket
) {
572 if (fl_ht
[state
->bucket
]) {
573 fl
= fl_ht
[state
->bucket
];
580 static struct ip6_flowlabel
*ip6fl_get_next(struct seq_file
*seq
, struct ip6_flowlabel
*fl
)
582 struct ip6fl_iter_state
*state
= ip6fl_seq_private(seq
);
586 if (++state
->bucket
<= FL_HASH_MASK
)
587 fl
= fl_ht
[state
->bucket
];
592 static struct ip6_flowlabel
*ip6fl_get_idx(struct seq_file
*seq
, loff_t pos
)
594 struct ip6_flowlabel
*fl
= ip6fl_get_first(seq
);
596 while (pos
&& (fl
= ip6fl_get_next(seq
, fl
)) != NULL
)
598 return pos
? NULL
: fl
;
601 static void *ip6fl_seq_start(struct seq_file
*seq
, loff_t
*pos
)
603 read_lock_bh(&ip6_fl_lock
);
604 return *pos
? ip6fl_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
607 static void *ip6fl_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
609 struct ip6_flowlabel
*fl
;
611 if (v
== SEQ_START_TOKEN
)
612 fl
= ip6fl_get_first(seq
);
614 fl
= ip6fl_get_next(seq
, v
);
619 static void ip6fl_seq_stop(struct seq_file
*seq
, void *v
)
621 read_unlock_bh(&ip6_fl_lock
);
624 static void ip6fl_fl_seq_show(struct seq_file
*seq
, struct ip6_flowlabel
*fl
)
628 "%05X %-1d %-6d %-6d %-6ld %-8ld "
629 "%02x%02x%02x%02x%02x%02x%02x%02x "
631 (unsigned)ntohl(fl
->label
),
634 atomic_read(&fl
->users
),
636 (long)(fl
->expires
- jiffies
)/HZ
,
638 fl
->opt
? fl
->opt
->opt_nflen
: 0);
643 static int ip6fl_seq_show(struct seq_file
*seq
, void *v
)
645 if (v
== SEQ_START_TOKEN
)
646 seq_puts(seq
, "Label S Owner Users Linger Expires "
649 ip6fl_fl_seq_show(seq
, v
);
653 static struct seq_operations ip6fl_seq_ops
= {
654 .start
= ip6fl_seq_start
,
655 .next
= ip6fl_seq_next
,
656 .stop
= ip6fl_seq_stop
,
657 .show
= ip6fl_seq_show
,
660 static int ip6fl_seq_open(struct inode
*inode
, struct file
*file
)
662 struct seq_file
*seq
;
664 struct ip6fl_iter_state
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
669 rc
= seq_open(file
, &ip6fl_seq_ops
);
673 seq
= file
->private_data
;
675 memset(s
, 0, sizeof(*s
));
683 static struct file_operations ip6fl_seq_fops
= {
684 .owner
= THIS_MODULE
,
685 .open
= ip6fl_seq_open
,
688 .release
= seq_release_private
,
693 void ip6_flowlabel_init(void)
695 #ifdef CONFIG_PROC_FS
696 proc_net_fops_create("ip6_flowlabel", S_IRUGO
, &ip6fl_seq_fops
);
700 void ip6_flowlabel_cleanup(void)
702 del_timer(&ip6_fl_gc_timer
);
703 #ifdef CONFIG_PROC_FS
704 proc_net_remove("ip6_flowlabel");