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/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>
23 #include <linux/slab.h>
24 #include <linux/export.h>
26 #include <net/net_namespace.h>
30 #include <net/ndisc.h>
31 #include <net/protocol.h>
32 #include <net/ip6_route.h>
33 #include <net/addrconf.h>
34 #include <net/rawv6.h>
36 #include <net/transp_v6.h>
38 #include <asm/uaccess.h>
40 #define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified
41 in old IPv6 RFC. Well, it was reasonable value.
43 #define FL_MAX_LINGER 60 /* Maximal linger timeout */
47 #define FL_MAX_PER_SOCK 32
48 #define FL_MAX_SIZE 4096
49 #define FL_HASH_MASK 255
50 #define FL_HASH(l) (ntohl(l)&FL_HASH_MASK)
52 static atomic_t fl_size
= ATOMIC_INIT(0);
53 static struct ip6_flowlabel
*fl_ht
[FL_HASH_MASK
+1];
55 static void ip6_fl_gc(unsigned long dummy
);
56 static DEFINE_TIMER(ip6_fl_gc_timer
, ip6_fl_gc
, 0, 0);
58 /* FL hash table lock: it protects only of GC */
60 static DEFINE_RWLOCK(ip6_fl_lock
);
64 static DEFINE_RWLOCK(ip6_sk_fl_lock
);
67 static inline struct ip6_flowlabel
*__fl_lookup(struct net
*net
, __be32 label
)
69 struct ip6_flowlabel
*fl
;
71 for (fl
=fl_ht
[FL_HASH(label
)]; fl
; fl
= fl
->next
) {
72 if (fl
->label
== label
&& net_eq(fl
->fl_net
, net
))
78 static struct ip6_flowlabel
*fl_lookup(struct net
*net
, __be32 label
)
80 struct ip6_flowlabel
*fl
;
82 read_lock_bh(&ip6_fl_lock
);
83 fl
= __fl_lookup(net
, label
);
85 atomic_inc(&fl
->users
);
86 read_unlock_bh(&ip6_fl_lock
);
91 static void fl_free(struct ip6_flowlabel
*fl
)
94 release_net(fl
->fl_net
);
100 static void fl_release(struct ip6_flowlabel
*fl
)
102 write_lock_bh(&ip6_fl_lock
);
104 fl
->lastuse
= jiffies
;
105 if (atomic_dec_and_test(&fl
->users
)) {
106 unsigned long ttd
= fl
->lastuse
+ fl
->linger
;
107 if (time_after(ttd
, fl
->expires
))
110 if (fl
->opt
&& fl
->share
== IPV6_FL_S_EXCL
) {
111 struct ipv6_txoptions
*opt
= fl
->opt
;
115 if (!timer_pending(&ip6_fl_gc_timer
) ||
116 time_after(ip6_fl_gc_timer
.expires
, ttd
))
117 mod_timer(&ip6_fl_gc_timer
, ttd
);
119 write_unlock_bh(&ip6_fl_lock
);
122 static void ip6_fl_gc(unsigned long dummy
)
125 unsigned long now
= jiffies
;
126 unsigned long sched
= 0;
128 write_lock(&ip6_fl_lock
);
130 for (i
=0; i
<=FL_HASH_MASK
; i
++) {
131 struct ip6_flowlabel
*fl
, **flp
;
133 while ((fl
=*flp
) != NULL
) {
134 if (atomic_read(&fl
->users
) == 0) {
135 unsigned long ttd
= fl
->lastuse
+ fl
->linger
;
136 if (time_after(ttd
, fl
->expires
))
139 if (time_after_eq(now
, ttd
)) {
142 atomic_dec(&fl_size
);
145 if (!sched
|| time_before(ttd
, sched
))
151 if (!sched
&& atomic_read(&fl_size
))
152 sched
= now
+ FL_MAX_LINGER
;
154 mod_timer(&ip6_fl_gc_timer
, sched
);
156 write_unlock(&ip6_fl_lock
);
159 static void __net_exit
ip6_fl_purge(struct net
*net
)
163 write_lock(&ip6_fl_lock
);
164 for (i
= 0; i
<= FL_HASH_MASK
; i
++) {
165 struct ip6_flowlabel
*fl
, **flp
;
167 while ((fl
= *flp
) != NULL
) {
168 if (net_eq(fl
->fl_net
, net
) &&
169 atomic_read(&fl
->users
) == 0) {
172 atomic_dec(&fl_size
);
178 write_unlock(&ip6_fl_lock
);
181 static struct ip6_flowlabel
*fl_intern(struct net
*net
,
182 struct ip6_flowlabel
*fl
, __be32 label
)
184 struct ip6_flowlabel
*lfl
;
186 fl
->label
= label
& IPV6_FLOWLABEL_MASK
;
188 write_lock_bh(&ip6_fl_lock
);
191 fl
->label
= htonl(net_random())&IPV6_FLOWLABEL_MASK
;
193 lfl
= __fl_lookup(net
, fl
->label
);
200 * we dropper the ip6_fl_lock, so this entry could reappear
201 * and we need to recheck with it.
203 * OTOH no need to search the active socket first, like it is
204 * done in ipv6_flowlabel_opt - sock is locked, so new entry
205 * with the same label can only appear on another sock
207 lfl
= __fl_lookup(net
, fl
->label
);
209 atomic_inc(&lfl
->users
);
210 write_unlock_bh(&ip6_fl_lock
);
215 fl
->lastuse
= jiffies
;
216 fl
->next
= fl_ht
[FL_HASH(fl
->label
)];
217 fl_ht
[FL_HASH(fl
->label
)] = fl
;
218 atomic_inc(&fl_size
);
219 write_unlock_bh(&ip6_fl_lock
);
225 /* Socket flowlabel lists */
227 struct ip6_flowlabel
* fl6_sock_lookup(struct sock
*sk
, __be32 label
)
229 struct ipv6_fl_socklist
*sfl
;
230 struct ipv6_pinfo
*np
= inet6_sk(sk
);
232 label
&= IPV6_FLOWLABEL_MASK
;
234 read_lock_bh(&ip6_sk_fl_lock
);
235 for (sfl
=np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
) {
236 struct ip6_flowlabel
*fl
= sfl
->fl
;
237 if (fl
->label
== label
) {
238 fl
->lastuse
= jiffies
;
239 atomic_inc(&fl
->users
);
240 read_unlock_bh(&ip6_sk_fl_lock
);
244 read_unlock_bh(&ip6_sk_fl_lock
);
248 EXPORT_SYMBOL_GPL(fl6_sock_lookup
);
250 void fl6_free_socklist(struct sock
*sk
)
252 struct ipv6_pinfo
*np
= inet6_sk(sk
);
253 struct ipv6_fl_socklist
*sfl
;
255 while ((sfl
= np
->ipv6_fl_list
) != NULL
) {
256 np
->ipv6_fl_list
= sfl
->next
;
262 /* Service routines */
266 It is the only difficult place. flowlabel enforces equal headers
267 before and including routing header, however user may supply options
271 struct ipv6_txoptions
*fl6_merge_options(struct ipv6_txoptions
* opt_space
,
272 struct ip6_flowlabel
* fl
,
273 struct ipv6_txoptions
* fopt
)
275 struct ipv6_txoptions
* fl_opt
= fl
->opt
;
277 if (fopt
== NULL
|| fopt
->opt_flen
== 0)
280 if (fl_opt
!= NULL
) {
281 opt_space
->hopopt
= fl_opt
->hopopt
;
282 opt_space
->dst0opt
= fl_opt
->dst0opt
;
283 opt_space
->srcrt
= fl_opt
->srcrt
;
284 opt_space
->opt_nflen
= fl_opt
->opt_nflen
;
286 if (fopt
->opt_nflen
== 0)
288 opt_space
->hopopt
= NULL
;
289 opt_space
->dst0opt
= NULL
;
290 opt_space
->srcrt
= NULL
;
291 opt_space
->opt_nflen
= 0;
293 opt_space
->dst1opt
= fopt
->dst1opt
;
294 opt_space
->opt_flen
= fopt
->opt_flen
;
298 static unsigned long check_linger(unsigned long ttl
)
300 if (ttl
< FL_MIN_LINGER
)
301 return FL_MIN_LINGER
*HZ
;
302 if (ttl
> FL_MAX_LINGER
&& !capable(CAP_NET_ADMIN
))
307 static int fl6_renew(struct ip6_flowlabel
*fl
, unsigned long linger
, unsigned long expires
)
309 linger
= check_linger(linger
);
312 expires
= check_linger(expires
);
315 fl
->lastuse
= jiffies
;
316 if (time_before(fl
->linger
, linger
))
318 if (time_before(expires
, fl
->linger
))
319 expires
= fl
->linger
;
320 if (time_before(fl
->expires
, fl
->lastuse
+ expires
))
321 fl
->expires
= fl
->lastuse
+ expires
;
325 static struct ip6_flowlabel
*
326 fl_create(struct net
*net
, struct sock
*sk
, struct in6_flowlabel_req
*freq
,
327 char __user
*optval
, int optlen
, int *err_p
)
329 struct ip6_flowlabel
*fl
= NULL
;
334 olen
= optlen
- CMSG_ALIGN(sizeof(*freq
));
336 if (olen
> 64 * 1024)
340 fl
= kzalloc(sizeof(*fl
), GFP_KERNEL
);
346 struct flowi6 flowi6
;
350 fl
->opt
= kmalloc(sizeof(*fl
->opt
) + olen
, GFP_KERNEL
);
354 memset(fl
->opt
, 0, sizeof(*fl
->opt
));
355 fl
->opt
->tot_len
= sizeof(*fl
->opt
) + olen
;
357 if (copy_from_user(fl
->opt
+1, optval
+CMSG_ALIGN(sizeof(*freq
)), olen
))
360 msg
.msg_controllen
= olen
;
361 msg
.msg_control
= (void*)(fl
->opt
+1);
362 memset(&flowi6
, 0, sizeof(flowi6
));
364 err
= datagram_send_ctl(net
, sk
, &msg
, &flowi6
, fl
->opt
, &junk
,
369 if (fl
->opt
->opt_flen
)
371 if (fl
->opt
->opt_nflen
== 0) {
377 fl
->fl_net
= hold_net(net
);
378 fl
->expires
= jiffies
;
379 err
= fl6_renew(fl
, freq
->flr_linger
, freq
->flr_expires
);
382 fl
->share
= freq
->flr_share
;
383 addr_type
= ipv6_addr_type(&freq
->flr_dst
);
384 if ((addr_type
& IPV6_ADDR_MAPPED
) ||
385 addr_type
== IPV6_ADDR_ANY
) {
389 fl
->dst
= freq
->flr_dst
;
390 atomic_set(&fl
->users
, 1);
395 case IPV6_FL_S_PROCESS
:
396 fl
->owner
= current
->pid
;
399 fl
->owner
= current_euid();
413 static int mem_check(struct sock
*sk
)
415 struct ipv6_pinfo
*np
= inet6_sk(sk
);
416 struct ipv6_fl_socklist
*sfl
;
417 int room
= FL_MAX_SIZE
- atomic_read(&fl_size
);
420 if (room
> FL_MAX_SIZE
- FL_MAX_PER_SOCK
)
423 for (sfl
= np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
)
427 ((count
>= FL_MAX_PER_SOCK
||
428 (count
> 0 && room
< FL_MAX_SIZE
/2) || room
< FL_MAX_SIZE
/4) &&
429 !capable(CAP_NET_ADMIN
)))
435 static int ipv6_hdr_cmp(struct ipv6_opt_hdr
*h1
, struct ipv6_opt_hdr
*h2
)
439 if (h1
== NULL
|| h2
== NULL
)
441 if (h1
->hdrlen
!= h2
->hdrlen
)
443 return memcmp(h1
+1, h2
+1, ((h1
->hdrlen
+1)<<3) - sizeof(*h1
));
446 static int ipv6_opt_cmp(struct ipv6_txoptions
*o1
, struct ipv6_txoptions
*o2
)
450 if (o1
== NULL
|| o2
== NULL
)
452 if (o1
->opt_nflen
!= o2
->opt_nflen
)
454 if (ipv6_hdr_cmp(o1
->hopopt
, o2
->hopopt
))
456 if (ipv6_hdr_cmp(o1
->dst0opt
, o2
->dst0opt
))
458 if (ipv6_hdr_cmp((struct ipv6_opt_hdr
*)o1
->srcrt
, (struct ipv6_opt_hdr
*)o2
->srcrt
))
463 static inline void fl_link(struct ipv6_pinfo
*np
, struct ipv6_fl_socklist
*sfl
,
464 struct ip6_flowlabel
*fl
)
466 write_lock_bh(&ip6_sk_fl_lock
);
468 sfl
->next
= np
->ipv6_fl_list
;
469 np
->ipv6_fl_list
= sfl
;
470 write_unlock_bh(&ip6_sk_fl_lock
);
473 int ipv6_flowlabel_opt(struct sock
*sk
, char __user
*optval
, int optlen
)
475 int uninitialized_var(err
);
476 struct net
*net
= sock_net(sk
);
477 struct ipv6_pinfo
*np
= inet6_sk(sk
);
478 struct in6_flowlabel_req freq
;
479 struct ipv6_fl_socklist
*sfl1
=NULL
;
480 struct ipv6_fl_socklist
*sfl
, **sflp
;
481 struct ip6_flowlabel
*fl
, *fl1
= NULL
;
484 if (optlen
< sizeof(freq
))
487 if (copy_from_user(&freq
, optval
, sizeof(freq
)))
490 switch (freq
.flr_action
) {
492 write_lock_bh(&ip6_sk_fl_lock
);
493 for (sflp
= &np
->ipv6_fl_list
; (sfl
=*sflp
)!=NULL
; sflp
= &sfl
->next
) {
494 if (sfl
->fl
->label
== freq
.flr_label
) {
495 if (freq
.flr_label
== (np
->flow_label
&IPV6_FLOWLABEL_MASK
))
496 np
->flow_label
&= ~IPV6_FLOWLABEL_MASK
;
498 write_unlock_bh(&ip6_sk_fl_lock
);
504 write_unlock_bh(&ip6_sk_fl_lock
);
507 case IPV6_FL_A_RENEW
:
508 read_lock_bh(&ip6_sk_fl_lock
);
509 for (sfl
= np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
) {
510 if (sfl
->fl
->label
== freq
.flr_label
) {
511 err
= fl6_renew(sfl
->fl
, freq
.flr_linger
, freq
.flr_expires
);
512 read_unlock_bh(&ip6_sk_fl_lock
);
516 read_unlock_bh(&ip6_sk_fl_lock
);
518 if (freq
.flr_share
== IPV6_FL_S_NONE
&& capable(CAP_NET_ADMIN
)) {
519 fl
= fl_lookup(net
, freq
.flr_label
);
521 err
= fl6_renew(fl
, freq
.flr_linger
, freq
.flr_expires
);
529 if (freq
.flr_label
& ~IPV6_FLOWLABEL_MASK
)
532 fl
= fl_create(net
, sk
, &freq
, optval
, optlen
, &err
);
535 sfl1
= kmalloc(sizeof(*sfl1
), GFP_KERNEL
);
537 if (freq
.flr_label
) {
539 read_lock_bh(&ip6_sk_fl_lock
);
540 for (sfl
= np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
) {
541 if (sfl
->fl
->label
== freq
.flr_label
) {
542 if (freq
.flr_flags
&IPV6_FL_F_EXCL
) {
543 read_unlock_bh(&ip6_sk_fl_lock
);
547 atomic_inc(&fl1
->users
);
551 read_unlock_bh(&ip6_sk_fl_lock
);
554 fl1
= fl_lookup(net
, freq
.flr_label
);
558 if (freq
.flr_flags
&IPV6_FL_F_EXCL
)
561 if (fl1
->share
== IPV6_FL_S_EXCL
||
562 fl1
->share
!= fl
->share
||
563 fl1
->owner
!= fl
->owner
)
567 if (!ipv6_addr_equal(&fl1
->dst
, &fl
->dst
) ||
568 ipv6_opt_cmp(fl1
->opt
, fl
->opt
))
574 if (fl
->linger
> fl1
->linger
)
575 fl1
->linger
= fl
->linger
;
576 if ((long)(fl
->expires
- fl1
->expires
) > 0)
577 fl1
->expires
= fl
->expires
;
578 fl_link(np
, sfl1
, fl1
);
588 if (!(freq
.flr_flags
&IPV6_FL_F_CREATE
))
592 if (sfl1
== NULL
|| (err
= mem_check(sk
)) != 0)
595 fl1
= fl_intern(net
, fl
, freq
.flr_label
);
599 if (!freq
.flr_label
) {
600 if (copy_to_user(&((struct in6_flowlabel_req __user
*) optval
)->flr_label
,
601 &fl
->label
, sizeof(fl
->label
))) {
602 /* Intentionally ignore fault. */
606 fl_link(np
, sfl1
, fl
);
619 #ifdef CONFIG_PROC_FS
621 struct ip6fl_iter_state
{
622 struct seq_net_private p
;
626 #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
628 static struct ip6_flowlabel
*ip6fl_get_first(struct seq_file
*seq
)
630 struct ip6_flowlabel
*fl
= NULL
;
631 struct ip6fl_iter_state
*state
= ip6fl_seq_private(seq
);
632 struct net
*net
= seq_file_net(seq
);
634 for (state
->bucket
= 0; state
->bucket
<= FL_HASH_MASK
; ++state
->bucket
) {
635 fl
= fl_ht
[state
->bucket
];
637 while (fl
&& !net_eq(fl
->fl_net
, net
))
645 static struct ip6_flowlabel
*ip6fl_get_next(struct seq_file
*seq
, struct ip6_flowlabel
*fl
)
647 struct ip6fl_iter_state
*state
= ip6fl_seq_private(seq
);
648 struct net
*net
= seq_file_net(seq
);
652 while (fl
&& !net_eq(fl
->fl_net
, net
))
656 if (++state
->bucket
<= FL_HASH_MASK
) {
657 fl
= fl_ht
[state
->bucket
];
665 static struct ip6_flowlabel
*ip6fl_get_idx(struct seq_file
*seq
, loff_t pos
)
667 struct ip6_flowlabel
*fl
= ip6fl_get_first(seq
);
669 while (pos
&& (fl
= ip6fl_get_next(seq
, fl
)) != NULL
)
671 return pos
? NULL
: fl
;
674 static void *ip6fl_seq_start(struct seq_file
*seq
, loff_t
*pos
)
675 __acquires(ip6_fl_lock
)
677 read_lock_bh(&ip6_fl_lock
);
678 return *pos
? ip6fl_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
681 static void *ip6fl_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
683 struct ip6_flowlabel
*fl
;
685 if (v
== SEQ_START_TOKEN
)
686 fl
= ip6fl_get_first(seq
);
688 fl
= ip6fl_get_next(seq
, v
);
693 static void ip6fl_seq_stop(struct seq_file
*seq
, void *v
)
694 __releases(ip6_fl_lock
)
696 read_unlock_bh(&ip6_fl_lock
);
699 static int ip6fl_seq_show(struct seq_file
*seq
, void *v
)
701 if (v
== SEQ_START_TOKEN
)
702 seq_printf(seq
, "%-5s %-1s %-6s %-6s %-6s %-8s %-32s %s\n",
703 "Label", "S", "Owner", "Users", "Linger", "Expires", "Dst", "Opt");
705 struct ip6_flowlabel
*fl
= v
;
707 "%05X %-1d %-6d %-6d %-6ld %-8ld %pi6 %-4d\n",
708 (unsigned)ntohl(fl
->label
),
711 atomic_read(&fl
->users
),
713 (long)(fl
->expires
- jiffies
)/HZ
,
715 fl
->opt
? fl
->opt
->opt_nflen
: 0);
720 static const struct seq_operations ip6fl_seq_ops
= {
721 .start
= ip6fl_seq_start
,
722 .next
= ip6fl_seq_next
,
723 .stop
= ip6fl_seq_stop
,
724 .show
= ip6fl_seq_show
,
727 static int ip6fl_seq_open(struct inode
*inode
, struct file
*file
)
729 return seq_open_net(inode
, file
, &ip6fl_seq_ops
,
730 sizeof(struct ip6fl_iter_state
));
733 static const struct file_operations ip6fl_seq_fops
= {
734 .owner
= THIS_MODULE
,
735 .open
= ip6fl_seq_open
,
738 .release
= seq_release_net
,
741 static int __net_init
ip6_flowlabel_proc_init(struct net
*net
)
743 if (!proc_net_fops_create(net
, "ip6_flowlabel",
744 S_IRUGO
, &ip6fl_seq_fops
))
749 static void __net_exit
ip6_flowlabel_proc_fini(struct net
*net
)
751 proc_net_remove(net
, "ip6_flowlabel");
754 static inline int ip6_flowlabel_proc_init(struct net
*net
)
758 static inline void ip6_flowlabel_proc_fini(struct net
*net
)
763 static void __net_exit
ip6_flowlabel_net_exit(struct net
*net
)
766 ip6_flowlabel_proc_fini(net
);
769 static struct pernet_operations ip6_flowlabel_net_ops
= {
770 .init
= ip6_flowlabel_proc_init
,
771 .exit
= ip6_flowlabel_net_exit
,
774 int ip6_flowlabel_init(void)
776 return register_pernet_subsys(&ip6_flowlabel_net_ops
);
779 void ip6_flowlabel_cleanup(void)
781 del_timer(&ip6_fl_gc_timer
);
782 unregister_pernet_subsys(&ip6_flowlabel_net_ops
);