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>
24 #include <net/net_namespace.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(struct net
*net
, __be32 label
)
67 struct ip6_flowlabel
*fl
;
69 for (fl
=fl_ht
[FL_HASH(label
)]; fl
; fl
= fl
->next
) {
70 if (fl
->label
== label
&& net_eq(fl
->fl_net
, net
))
76 static struct ip6_flowlabel
*fl_lookup(struct net
*net
, __be32 label
)
78 struct ip6_flowlabel
*fl
;
80 read_lock_bh(&ip6_fl_lock
);
81 fl
= __fl_lookup(net
, label
);
83 atomic_inc(&fl
->users
);
84 read_unlock_bh(&ip6_fl_lock
);
89 static void fl_free(struct ip6_flowlabel
*fl
)
92 release_net(fl
->fl_net
);
98 static void fl_release(struct ip6_flowlabel
*fl
)
100 write_lock_bh(&ip6_fl_lock
);
102 fl
->lastuse
= jiffies
;
103 if (atomic_dec_and_test(&fl
->users
)) {
104 unsigned long ttd
= fl
->lastuse
+ fl
->linger
;
105 if (time_after(ttd
, fl
->expires
))
108 if (fl
->opt
&& fl
->share
== IPV6_FL_S_EXCL
) {
109 struct ipv6_txoptions
*opt
= fl
->opt
;
113 if (!timer_pending(&ip6_fl_gc_timer
) ||
114 time_after(ip6_fl_gc_timer
.expires
, ttd
))
115 mod_timer(&ip6_fl_gc_timer
, ttd
);
117 write_unlock_bh(&ip6_fl_lock
);
120 static void ip6_fl_gc(unsigned long dummy
)
123 unsigned long now
= jiffies
;
124 unsigned long sched
= 0;
126 write_lock(&ip6_fl_lock
);
128 for (i
=0; i
<=FL_HASH_MASK
; i
++) {
129 struct ip6_flowlabel
*fl
, **flp
;
131 while ((fl
=*flp
) != NULL
) {
132 if (atomic_read(&fl
->users
) == 0) {
133 unsigned long ttd
= fl
->lastuse
+ fl
->linger
;
134 if (time_after(ttd
, fl
->expires
))
137 if (time_after_eq(now
, ttd
)) {
140 atomic_dec(&fl_size
);
143 if (!sched
|| time_before(ttd
, sched
))
149 if (!sched
&& atomic_read(&fl_size
))
150 sched
= now
+ FL_MAX_LINGER
;
152 mod_timer(&ip6_fl_gc_timer
, sched
);
154 write_unlock(&ip6_fl_lock
);
157 static void ip6_fl_purge(struct net
*net
)
161 write_lock(&ip6_fl_lock
);
162 for (i
= 0; i
<= FL_HASH_MASK
; i
++) {
163 struct ip6_flowlabel
*fl
, **flp
;
165 while ((fl
= *flp
) != NULL
) {
166 if (net_eq(fl
->fl_net
, net
) &&
167 atomic_read(&fl
->users
) == 0) {
170 atomic_dec(&fl_size
);
176 write_unlock(&ip6_fl_lock
);
179 static struct ip6_flowlabel
*fl_intern(struct net
*net
,
180 struct ip6_flowlabel
*fl
, __be32 label
)
182 struct ip6_flowlabel
*lfl
;
184 fl
->label
= label
& IPV6_FLOWLABEL_MASK
;
186 write_lock_bh(&ip6_fl_lock
);
189 fl
->label
= htonl(net_random())&IPV6_FLOWLABEL_MASK
;
191 lfl
= __fl_lookup(net
, fl
->label
);
198 * we dropper the ip6_fl_lock, so this entry could reappear
199 * and we need to recheck with it.
201 * OTOH no need to search the active socket first, like it is
202 * done in ipv6_flowlabel_opt - sock is locked, so new entry
203 * with the same label can only appear on another sock
205 lfl
= __fl_lookup(net
, fl
->label
);
207 atomic_inc(&lfl
->users
);
208 write_unlock_bh(&ip6_fl_lock
);
213 fl
->lastuse
= jiffies
;
214 fl
->next
= fl_ht
[FL_HASH(fl
->label
)];
215 fl_ht
[FL_HASH(fl
->label
)] = fl
;
216 atomic_inc(&fl_size
);
217 write_unlock_bh(&ip6_fl_lock
);
223 /* Socket flowlabel lists */
225 struct ip6_flowlabel
* fl6_sock_lookup(struct sock
*sk
, __be32 label
)
227 struct ipv6_fl_socklist
*sfl
;
228 struct ipv6_pinfo
*np
= inet6_sk(sk
);
230 label
&= IPV6_FLOWLABEL_MASK
;
232 read_lock_bh(&ip6_sk_fl_lock
);
233 for (sfl
=np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
) {
234 struct ip6_flowlabel
*fl
= sfl
->fl
;
235 if (fl
->label
== label
) {
236 fl
->lastuse
= jiffies
;
237 atomic_inc(&fl
->users
);
238 read_unlock_bh(&ip6_sk_fl_lock
);
242 read_unlock_bh(&ip6_sk_fl_lock
);
246 EXPORT_SYMBOL_GPL(fl6_sock_lookup
);
248 void fl6_free_socklist(struct sock
*sk
)
250 struct ipv6_pinfo
*np
= inet6_sk(sk
);
251 struct ipv6_fl_socklist
*sfl
;
253 while ((sfl
= np
->ipv6_fl_list
) != NULL
) {
254 np
->ipv6_fl_list
= sfl
->next
;
260 /* Service routines */
264 It is the only difficult place. flowlabel enforces equal headers
265 before and including routing header, however user may supply options
269 struct ipv6_txoptions
*fl6_merge_options(struct ipv6_txoptions
* opt_space
,
270 struct ip6_flowlabel
* fl
,
271 struct ipv6_txoptions
* fopt
)
273 struct ipv6_txoptions
* fl_opt
= fl
->opt
;
275 if (fopt
== NULL
|| fopt
->opt_flen
== 0)
278 if (fl_opt
!= NULL
) {
279 opt_space
->hopopt
= fl_opt
->hopopt
;
280 opt_space
->dst0opt
= fl_opt
->dst0opt
;
281 opt_space
->srcrt
= fl_opt
->srcrt
;
282 opt_space
->opt_nflen
= fl_opt
->opt_nflen
;
284 if (fopt
->opt_nflen
== 0)
286 opt_space
->hopopt
= NULL
;
287 opt_space
->dst0opt
= NULL
;
288 opt_space
->srcrt
= NULL
;
289 opt_space
->opt_nflen
= 0;
291 opt_space
->dst1opt
= fopt
->dst1opt
;
292 opt_space
->opt_flen
= fopt
->opt_flen
;
296 static unsigned long check_linger(unsigned long ttl
)
298 if (ttl
< FL_MIN_LINGER
)
299 return FL_MIN_LINGER
*HZ
;
300 if (ttl
> FL_MAX_LINGER
&& !capable(CAP_NET_ADMIN
))
305 static int fl6_renew(struct ip6_flowlabel
*fl
, unsigned long linger
, unsigned long expires
)
307 linger
= check_linger(linger
);
310 expires
= check_linger(expires
);
313 fl
->lastuse
= jiffies
;
314 if (time_before(fl
->linger
, linger
))
316 if (time_before(expires
, fl
->linger
))
317 expires
= fl
->linger
;
318 if (time_before(fl
->expires
, fl
->lastuse
+ expires
))
319 fl
->expires
= fl
->lastuse
+ expires
;
323 static struct ip6_flowlabel
*
324 fl_create(struct net
*net
, struct in6_flowlabel_req
*freq
, char __user
*optval
,
325 int optlen
, int *err_p
)
327 struct ip6_flowlabel
*fl
= NULL
;
332 olen
= optlen
- CMSG_ALIGN(sizeof(*freq
));
334 if (olen
> 64 * 1024)
338 fl
= kzalloc(sizeof(*fl
), GFP_KERNEL
);
348 fl
->opt
= kmalloc(sizeof(*fl
->opt
) + olen
, GFP_KERNEL
);
352 memset(fl
->opt
, 0, sizeof(*fl
->opt
));
353 fl
->opt
->tot_len
= sizeof(*fl
->opt
) + olen
;
355 if (copy_from_user(fl
->opt
+1, optval
+CMSG_ALIGN(sizeof(*freq
)), olen
))
358 msg
.msg_controllen
= olen
;
359 msg
.msg_control
= (void*)(fl
->opt
+1);
362 err
= datagram_send_ctl(net
, &msg
, &flowi
, fl
->opt
, &junk
, &junk
);
366 if (fl
->opt
->opt_flen
)
368 if (fl
->opt
->opt_nflen
== 0) {
374 fl
->fl_net
= hold_net(net
);
375 fl
->expires
= jiffies
;
376 err
= fl6_renew(fl
, freq
->flr_linger
, freq
->flr_expires
);
379 fl
->share
= freq
->flr_share
;
380 addr_type
= ipv6_addr_type(&freq
->flr_dst
);
381 if ((addr_type
& IPV6_ADDR_MAPPED
) ||
382 addr_type
== IPV6_ADDR_ANY
) {
386 ipv6_addr_copy(&fl
->dst
, &freq
->flr_dst
);
387 atomic_set(&fl
->users
, 1);
392 case IPV6_FL_S_PROCESS
:
393 fl
->owner
= current
->pid
;
396 fl
->owner
= current_euid();
410 static int mem_check(struct sock
*sk
)
412 struct ipv6_pinfo
*np
= inet6_sk(sk
);
413 struct ipv6_fl_socklist
*sfl
;
414 int room
= FL_MAX_SIZE
- atomic_read(&fl_size
);
417 if (room
> FL_MAX_SIZE
- FL_MAX_PER_SOCK
)
420 for (sfl
= np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
)
424 ((count
>= FL_MAX_PER_SOCK
||
425 (count
> 0 && room
< FL_MAX_SIZE
/2) || room
< FL_MAX_SIZE
/4) &&
426 !capable(CAP_NET_ADMIN
)))
432 static int ipv6_hdr_cmp(struct ipv6_opt_hdr
*h1
, struct ipv6_opt_hdr
*h2
)
436 if (h1
== NULL
|| h2
== NULL
)
438 if (h1
->hdrlen
!= h2
->hdrlen
)
440 return memcmp(h1
+1, h2
+1, ((h1
->hdrlen
+1)<<3) - sizeof(*h1
));
443 static int ipv6_opt_cmp(struct ipv6_txoptions
*o1
, struct ipv6_txoptions
*o2
)
447 if (o1
== NULL
|| o2
== NULL
)
449 if (o1
->opt_nflen
!= o2
->opt_nflen
)
451 if (ipv6_hdr_cmp(o1
->hopopt
, o2
->hopopt
))
453 if (ipv6_hdr_cmp(o1
->dst0opt
, o2
->dst0opt
))
455 if (ipv6_hdr_cmp((struct ipv6_opt_hdr
*)o1
->srcrt
, (struct ipv6_opt_hdr
*)o2
->srcrt
))
460 static inline void fl_link(struct ipv6_pinfo
*np
, struct ipv6_fl_socklist
*sfl
,
461 struct ip6_flowlabel
*fl
)
463 write_lock_bh(&ip6_sk_fl_lock
);
465 sfl
->next
= np
->ipv6_fl_list
;
466 np
->ipv6_fl_list
= sfl
;
467 write_unlock_bh(&ip6_sk_fl_lock
);
470 int ipv6_flowlabel_opt(struct sock
*sk
, char __user
*optval
, int optlen
)
472 int uninitialized_var(err
);
473 struct net
*net
= sock_net(sk
);
474 struct ipv6_pinfo
*np
= inet6_sk(sk
);
475 struct in6_flowlabel_req freq
;
476 struct ipv6_fl_socklist
*sfl1
=NULL
;
477 struct ipv6_fl_socklist
*sfl
, **sflp
;
478 struct ip6_flowlabel
*fl
, *fl1
= NULL
;
481 if (optlen
< sizeof(freq
))
484 if (copy_from_user(&freq
, optval
, sizeof(freq
)))
487 switch (freq
.flr_action
) {
489 write_lock_bh(&ip6_sk_fl_lock
);
490 for (sflp
= &np
->ipv6_fl_list
; (sfl
=*sflp
)!=NULL
; sflp
= &sfl
->next
) {
491 if (sfl
->fl
->label
== freq
.flr_label
) {
492 if (freq
.flr_label
== (np
->flow_label
&IPV6_FLOWLABEL_MASK
))
493 np
->flow_label
&= ~IPV6_FLOWLABEL_MASK
;
495 write_unlock_bh(&ip6_sk_fl_lock
);
501 write_unlock_bh(&ip6_sk_fl_lock
);
504 case IPV6_FL_A_RENEW
:
505 read_lock_bh(&ip6_sk_fl_lock
);
506 for (sfl
= np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
) {
507 if (sfl
->fl
->label
== freq
.flr_label
) {
508 err
= fl6_renew(sfl
->fl
, freq
.flr_linger
, freq
.flr_expires
);
509 read_unlock_bh(&ip6_sk_fl_lock
);
513 read_unlock_bh(&ip6_sk_fl_lock
);
515 if (freq
.flr_share
== IPV6_FL_S_NONE
&& capable(CAP_NET_ADMIN
)) {
516 fl
= fl_lookup(net
, freq
.flr_label
);
518 err
= fl6_renew(fl
, freq
.flr_linger
, freq
.flr_expires
);
526 if (freq
.flr_label
& ~IPV6_FLOWLABEL_MASK
)
529 fl
= fl_create(net
, &freq
, optval
, optlen
, &err
);
532 sfl1
= kmalloc(sizeof(*sfl1
), GFP_KERNEL
);
534 if (freq
.flr_label
) {
536 read_lock_bh(&ip6_sk_fl_lock
);
537 for (sfl
= np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
) {
538 if (sfl
->fl
->label
== freq
.flr_label
) {
539 if (freq
.flr_flags
&IPV6_FL_F_EXCL
) {
540 read_unlock_bh(&ip6_sk_fl_lock
);
544 atomic_inc(&fl1
->users
);
548 read_unlock_bh(&ip6_sk_fl_lock
);
551 fl1
= fl_lookup(net
, freq
.flr_label
);
555 if (freq
.flr_flags
&IPV6_FL_F_EXCL
)
558 if (fl1
->share
== IPV6_FL_S_EXCL
||
559 fl1
->share
!= fl
->share
||
560 fl1
->owner
!= fl
->owner
)
564 if (!ipv6_addr_equal(&fl1
->dst
, &fl
->dst
) ||
565 ipv6_opt_cmp(fl1
->opt
, fl
->opt
))
571 if (fl
->linger
> fl1
->linger
)
572 fl1
->linger
= fl
->linger
;
573 if ((long)(fl
->expires
- fl1
->expires
) > 0)
574 fl1
->expires
= fl
->expires
;
575 fl_link(np
, sfl1
, fl1
);
585 if (!(freq
.flr_flags
&IPV6_FL_F_CREATE
))
589 if (sfl1
== NULL
|| (err
= mem_check(sk
)) != 0)
592 fl1
= fl_intern(net
, fl
, freq
.flr_label
);
596 if (!freq
.flr_label
) {
597 if (copy_to_user(&((struct in6_flowlabel_req __user
*) optval
)->flr_label
,
598 &fl
->label
, sizeof(fl
->label
))) {
599 /* Intentionally ignore fault. */
603 fl_link(np
, sfl1
, fl
);
616 #ifdef CONFIG_PROC_FS
618 struct ip6fl_iter_state
{
619 struct seq_net_private p
;
623 #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
625 static struct ip6_flowlabel
*ip6fl_get_first(struct seq_file
*seq
)
627 struct ip6_flowlabel
*fl
= NULL
;
628 struct ip6fl_iter_state
*state
= ip6fl_seq_private(seq
);
629 struct net
*net
= seq_file_net(seq
);
631 for (state
->bucket
= 0; state
->bucket
<= FL_HASH_MASK
; ++state
->bucket
) {
632 fl
= fl_ht
[state
->bucket
];
634 while (fl
&& !net_eq(fl
->fl_net
, net
))
642 static struct ip6_flowlabel
*ip6fl_get_next(struct seq_file
*seq
, struct ip6_flowlabel
*fl
)
644 struct ip6fl_iter_state
*state
= ip6fl_seq_private(seq
);
645 struct net
*net
= seq_file_net(seq
);
649 while (fl
&& !net_eq(fl
->fl_net
, net
))
653 if (++state
->bucket
<= FL_HASH_MASK
) {
654 fl
= fl_ht
[state
->bucket
];
662 static struct ip6_flowlabel
*ip6fl_get_idx(struct seq_file
*seq
, loff_t pos
)
664 struct ip6_flowlabel
*fl
= ip6fl_get_first(seq
);
666 while (pos
&& (fl
= ip6fl_get_next(seq
, fl
)) != NULL
)
668 return pos
? NULL
: fl
;
671 static void *ip6fl_seq_start(struct seq_file
*seq
, loff_t
*pos
)
672 __acquires(ip6_fl_lock
)
674 read_lock_bh(&ip6_fl_lock
);
675 return *pos
? ip6fl_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
678 static void *ip6fl_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
680 struct ip6_flowlabel
*fl
;
682 if (v
== SEQ_START_TOKEN
)
683 fl
= ip6fl_get_first(seq
);
685 fl
= ip6fl_get_next(seq
, v
);
690 static void ip6fl_seq_stop(struct seq_file
*seq
, void *v
)
691 __releases(ip6_fl_lock
)
693 read_unlock_bh(&ip6_fl_lock
);
696 static int ip6fl_seq_show(struct seq_file
*seq
, void *v
)
698 if (v
== SEQ_START_TOKEN
)
699 seq_printf(seq
, "%-5s %-1s %-6s %-6s %-6s %-8s %-32s %s\n",
700 "Label", "S", "Owner", "Users", "Linger", "Expires", "Dst", "Opt");
702 struct ip6_flowlabel
*fl
= v
;
704 "%05X %-1d %-6d %-6d %-6ld %-8ld %pi6 %-4d\n",
705 (unsigned)ntohl(fl
->label
),
708 atomic_read(&fl
->users
),
710 (long)(fl
->expires
- jiffies
)/HZ
,
712 fl
->opt
? fl
->opt
->opt_nflen
: 0);
717 static const struct seq_operations ip6fl_seq_ops
= {
718 .start
= ip6fl_seq_start
,
719 .next
= ip6fl_seq_next
,
720 .stop
= ip6fl_seq_stop
,
721 .show
= ip6fl_seq_show
,
724 static int ip6fl_seq_open(struct inode
*inode
, struct file
*file
)
726 return seq_open_net(inode
, file
, &ip6fl_seq_ops
,
727 sizeof(struct ip6fl_iter_state
));
730 static const struct file_operations ip6fl_seq_fops
= {
731 .owner
= THIS_MODULE
,
732 .open
= ip6fl_seq_open
,
735 .release
= seq_release_net
,
738 static int ip6_flowlabel_proc_init(struct net
*net
)
740 if (!proc_net_fops_create(net
, "ip6_flowlabel",
741 S_IRUGO
, &ip6fl_seq_fops
))
746 static void ip6_flowlabel_proc_fini(struct net
*net
)
748 proc_net_remove(net
, "ip6_flowlabel");
751 static inline int ip6_flowlabel_proc_init(struct net
*net
)
755 static inline void ip6_flowlabel_proc_fini(struct net
*net
)
761 static inline void ip6_flowlabel_net_exit(struct net
*net
)
764 ip6_flowlabel_proc_fini(net
);
767 static struct pernet_operations ip6_flowlabel_net_ops
= {
768 .init
= ip6_flowlabel_proc_init
,
769 .exit
= ip6_flowlabel_net_exit
,
772 int ip6_flowlabel_init(void)
774 return register_pernet_subsys(&ip6_flowlabel_net_ops
);
777 void ip6_flowlabel_cleanup(void)
779 del_timer(&ip6_fl_gc_timer
);
780 unregister_pernet_subsys(&ip6_flowlabel_net_ops
);