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>
25 #include <net/net_namespace.h>
29 #include <net/ndisc.h>
30 #include <net/protocol.h>
31 #include <net/ip6_route.h>
32 #include <net/addrconf.h>
33 #include <net/rawv6.h>
35 #include <net/transp_v6.h>
37 #include <asm/uaccess.h>
39 #define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified
40 in old IPv6 RFC. Well, it was reasonable value.
42 #define FL_MAX_LINGER 60 /* Maximal linger timeout */
46 #define FL_MAX_PER_SOCK 32
47 #define FL_MAX_SIZE 4096
48 #define FL_HASH_MASK 255
49 #define FL_HASH(l) (ntohl(l)&FL_HASH_MASK)
51 static atomic_t fl_size
= ATOMIC_INIT(0);
52 static struct ip6_flowlabel
*fl_ht
[FL_HASH_MASK
+1];
54 static void ip6_fl_gc(unsigned long dummy
);
55 static DEFINE_TIMER(ip6_fl_gc_timer
, ip6_fl_gc
, 0, 0);
57 /* FL hash table lock: it protects only of GC */
59 static DEFINE_RWLOCK(ip6_fl_lock
);
63 static DEFINE_RWLOCK(ip6_sk_fl_lock
);
66 static inline struct ip6_flowlabel
*__fl_lookup(struct net
*net
, __be32 label
)
68 struct ip6_flowlabel
*fl
;
70 for (fl
=fl_ht
[FL_HASH(label
)]; fl
; fl
= fl
->next
) {
71 if (fl
->label
== label
&& net_eq(fl
->fl_net
, net
))
77 static struct ip6_flowlabel
*fl_lookup(struct net
*net
, __be32 label
)
79 struct ip6_flowlabel
*fl
;
81 read_lock_bh(&ip6_fl_lock
);
82 fl
= __fl_lookup(net
, label
);
84 atomic_inc(&fl
->users
);
85 read_unlock_bh(&ip6_fl_lock
);
90 static void fl_free(struct ip6_flowlabel
*fl
)
93 release_net(fl
->fl_net
);
99 static void fl_release(struct ip6_flowlabel
*fl
)
101 write_lock_bh(&ip6_fl_lock
);
103 fl
->lastuse
= jiffies
;
104 if (atomic_dec_and_test(&fl
->users
)) {
105 unsigned long ttd
= fl
->lastuse
+ fl
->linger
;
106 if (time_after(ttd
, fl
->expires
))
109 if (fl
->opt
&& fl
->share
== IPV6_FL_S_EXCL
) {
110 struct ipv6_txoptions
*opt
= fl
->opt
;
114 if (!timer_pending(&ip6_fl_gc_timer
) ||
115 time_after(ip6_fl_gc_timer
.expires
, ttd
))
116 mod_timer(&ip6_fl_gc_timer
, ttd
);
118 write_unlock_bh(&ip6_fl_lock
);
121 static void ip6_fl_gc(unsigned long dummy
)
124 unsigned long now
= jiffies
;
125 unsigned long sched
= 0;
127 write_lock(&ip6_fl_lock
);
129 for (i
=0; i
<=FL_HASH_MASK
; i
++) {
130 struct ip6_flowlabel
*fl
, **flp
;
132 while ((fl
=*flp
) != NULL
) {
133 if (atomic_read(&fl
->users
) == 0) {
134 unsigned long ttd
= fl
->lastuse
+ fl
->linger
;
135 if (time_after(ttd
, fl
->expires
))
138 if (time_after_eq(now
, ttd
)) {
141 atomic_dec(&fl_size
);
144 if (!sched
|| time_before(ttd
, sched
))
150 if (!sched
&& atomic_read(&fl_size
))
151 sched
= now
+ FL_MAX_LINGER
;
153 mod_timer(&ip6_fl_gc_timer
, sched
);
155 write_unlock(&ip6_fl_lock
);
158 static void __net_exit
ip6_fl_purge(struct net
*net
)
162 write_lock(&ip6_fl_lock
);
163 for (i
= 0; i
<= FL_HASH_MASK
; i
++) {
164 struct ip6_flowlabel
*fl
, **flp
;
166 while ((fl
= *flp
) != NULL
) {
167 if (net_eq(fl
->fl_net
, net
) &&
168 atomic_read(&fl
->users
) == 0) {
171 atomic_dec(&fl_size
);
177 write_unlock(&ip6_fl_lock
);
180 static struct ip6_flowlabel
*fl_intern(struct net
*net
,
181 struct ip6_flowlabel
*fl
, __be32 label
)
183 struct ip6_flowlabel
*lfl
;
185 fl
->label
= label
& IPV6_FLOWLABEL_MASK
;
187 write_lock_bh(&ip6_fl_lock
);
190 fl
->label
= htonl(net_random())&IPV6_FLOWLABEL_MASK
;
192 lfl
= __fl_lookup(net
, fl
->label
);
199 * we dropper the ip6_fl_lock, so this entry could reappear
200 * and we need to recheck with it.
202 * OTOH no need to search the active socket first, like it is
203 * done in ipv6_flowlabel_opt - sock is locked, so new entry
204 * with the same label can only appear on another sock
206 lfl
= __fl_lookup(net
, fl
->label
);
208 atomic_inc(&lfl
->users
);
209 write_unlock_bh(&ip6_fl_lock
);
214 fl
->lastuse
= jiffies
;
215 fl
->next
= fl_ht
[FL_HASH(fl
->label
)];
216 fl_ht
[FL_HASH(fl
->label
)] = fl
;
217 atomic_inc(&fl_size
);
218 write_unlock_bh(&ip6_fl_lock
);
224 /* Socket flowlabel lists */
226 struct ip6_flowlabel
* fl6_sock_lookup(struct sock
*sk
, __be32 label
)
228 struct ipv6_fl_socklist
*sfl
;
229 struct ipv6_pinfo
*np
= inet6_sk(sk
);
231 label
&= IPV6_FLOWLABEL_MASK
;
233 read_lock_bh(&ip6_sk_fl_lock
);
234 for (sfl
=np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
) {
235 struct ip6_flowlabel
*fl
= sfl
->fl
;
236 if (fl
->label
== label
) {
237 fl
->lastuse
= jiffies
;
238 atomic_inc(&fl
->users
);
239 read_unlock_bh(&ip6_sk_fl_lock
);
243 read_unlock_bh(&ip6_sk_fl_lock
);
247 EXPORT_SYMBOL_GPL(fl6_sock_lookup
);
249 void fl6_free_socklist(struct sock
*sk
)
251 struct ipv6_pinfo
*np
= inet6_sk(sk
);
252 struct ipv6_fl_socklist
*sfl
;
254 while ((sfl
= np
->ipv6_fl_list
) != NULL
) {
255 np
->ipv6_fl_list
= sfl
->next
;
261 /* Service routines */
265 It is the only difficult place. flowlabel enforces equal headers
266 before and including routing header, however user may supply options
270 struct ipv6_txoptions
*fl6_merge_options(struct ipv6_txoptions
* opt_space
,
271 struct ip6_flowlabel
* fl
,
272 struct ipv6_txoptions
* fopt
)
274 struct ipv6_txoptions
* fl_opt
= fl
->opt
;
276 if (fopt
== NULL
|| fopt
->opt_flen
== 0)
279 if (fl_opt
!= NULL
) {
280 opt_space
->hopopt
= fl_opt
->hopopt
;
281 opt_space
->dst0opt
= fl_opt
->dst0opt
;
282 opt_space
->srcrt
= fl_opt
->srcrt
;
283 opt_space
->opt_nflen
= fl_opt
->opt_nflen
;
285 if (fopt
->opt_nflen
== 0)
287 opt_space
->hopopt
= NULL
;
288 opt_space
->dst0opt
= NULL
;
289 opt_space
->srcrt
= NULL
;
290 opt_space
->opt_nflen
= 0;
292 opt_space
->dst1opt
= fopt
->dst1opt
;
293 opt_space
->opt_flen
= fopt
->opt_flen
;
297 static unsigned long check_linger(unsigned long ttl
)
299 if (ttl
< FL_MIN_LINGER
)
300 return FL_MIN_LINGER
*HZ
;
301 if (ttl
> FL_MAX_LINGER
&& !capable(CAP_NET_ADMIN
))
306 static int fl6_renew(struct ip6_flowlabel
*fl
, unsigned long linger
, unsigned long expires
)
308 linger
= check_linger(linger
);
311 expires
= check_linger(expires
);
314 fl
->lastuse
= jiffies
;
315 if (time_before(fl
->linger
, linger
))
317 if (time_before(expires
, fl
->linger
))
318 expires
= fl
->linger
;
319 if (time_before(fl
->expires
, fl
->lastuse
+ expires
))
320 fl
->expires
= fl
->lastuse
+ expires
;
324 static struct ip6_flowlabel
*
325 fl_create(struct net
*net
, struct in6_flowlabel_req
*freq
, char __user
*optval
,
326 int optlen
, int *err_p
)
328 struct ip6_flowlabel
*fl
= NULL
;
333 olen
= optlen
- CMSG_ALIGN(sizeof(*freq
));
335 if (olen
> 64 * 1024)
339 fl
= kzalloc(sizeof(*fl
), GFP_KERNEL
);
349 fl
->opt
= kmalloc(sizeof(*fl
->opt
) + olen
, GFP_KERNEL
);
353 memset(fl
->opt
, 0, sizeof(*fl
->opt
));
354 fl
->opt
->tot_len
= sizeof(*fl
->opt
) + olen
;
356 if (copy_from_user(fl
->opt
+1, optval
+CMSG_ALIGN(sizeof(*freq
)), olen
))
359 msg
.msg_controllen
= olen
;
360 msg
.msg_control
= (void*)(fl
->opt
+1);
363 err
= datagram_send_ctl(net
, &msg
, &flowi
, fl
->opt
, &junk
,
368 if (fl
->opt
->opt_flen
)
370 if (fl
->opt
->opt_nflen
== 0) {
376 fl
->fl_net
= hold_net(net
);
377 fl
->expires
= jiffies
;
378 err
= fl6_renew(fl
, freq
->flr_linger
, freq
->flr_expires
);
381 fl
->share
= freq
->flr_share
;
382 addr_type
= ipv6_addr_type(&freq
->flr_dst
);
383 if ((addr_type
& IPV6_ADDR_MAPPED
) ||
384 addr_type
== IPV6_ADDR_ANY
) {
388 ipv6_addr_copy(&fl
->dst
, &freq
->flr_dst
);
389 atomic_set(&fl
->users
, 1);
394 case IPV6_FL_S_PROCESS
:
395 fl
->owner
= current
->pid
;
398 fl
->owner
= current_euid();
412 static int mem_check(struct sock
*sk
)
414 struct ipv6_pinfo
*np
= inet6_sk(sk
);
415 struct ipv6_fl_socklist
*sfl
;
416 int room
= FL_MAX_SIZE
- atomic_read(&fl_size
);
419 if (room
> FL_MAX_SIZE
- FL_MAX_PER_SOCK
)
422 for (sfl
= np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
)
426 ((count
>= FL_MAX_PER_SOCK
||
427 (count
> 0 && room
< FL_MAX_SIZE
/2) || room
< FL_MAX_SIZE
/4) &&
428 !capable(CAP_NET_ADMIN
)))
434 static int ipv6_hdr_cmp(struct ipv6_opt_hdr
*h1
, struct ipv6_opt_hdr
*h2
)
438 if (h1
== NULL
|| h2
== NULL
)
440 if (h1
->hdrlen
!= h2
->hdrlen
)
442 return memcmp(h1
+1, h2
+1, ((h1
->hdrlen
+1)<<3) - sizeof(*h1
));
445 static int ipv6_opt_cmp(struct ipv6_txoptions
*o1
, struct ipv6_txoptions
*o2
)
449 if (o1
== NULL
|| o2
== NULL
)
451 if (o1
->opt_nflen
!= o2
->opt_nflen
)
453 if (ipv6_hdr_cmp(o1
->hopopt
, o2
->hopopt
))
455 if (ipv6_hdr_cmp(o1
->dst0opt
, o2
->dst0opt
))
457 if (ipv6_hdr_cmp((struct ipv6_opt_hdr
*)o1
->srcrt
, (struct ipv6_opt_hdr
*)o2
->srcrt
))
462 static inline void fl_link(struct ipv6_pinfo
*np
, struct ipv6_fl_socklist
*sfl
,
463 struct ip6_flowlabel
*fl
)
465 write_lock_bh(&ip6_sk_fl_lock
);
467 sfl
->next
= np
->ipv6_fl_list
;
468 np
->ipv6_fl_list
= sfl
;
469 write_unlock_bh(&ip6_sk_fl_lock
);
472 int ipv6_flowlabel_opt(struct sock
*sk
, char __user
*optval
, int optlen
)
474 int uninitialized_var(err
);
475 struct net
*net
= sock_net(sk
);
476 struct ipv6_pinfo
*np
= inet6_sk(sk
);
477 struct in6_flowlabel_req freq
;
478 struct ipv6_fl_socklist
*sfl1
=NULL
;
479 struct ipv6_fl_socklist
*sfl
, **sflp
;
480 struct ip6_flowlabel
*fl
, *fl1
= NULL
;
483 if (optlen
< sizeof(freq
))
486 if (copy_from_user(&freq
, optval
, sizeof(freq
)))
489 switch (freq
.flr_action
) {
491 write_lock_bh(&ip6_sk_fl_lock
);
492 for (sflp
= &np
->ipv6_fl_list
; (sfl
=*sflp
)!=NULL
; sflp
= &sfl
->next
) {
493 if (sfl
->fl
->label
== freq
.flr_label
) {
494 if (freq
.flr_label
== (np
->flow_label
&IPV6_FLOWLABEL_MASK
))
495 np
->flow_label
&= ~IPV6_FLOWLABEL_MASK
;
497 write_unlock_bh(&ip6_sk_fl_lock
);
503 write_unlock_bh(&ip6_sk_fl_lock
);
506 case IPV6_FL_A_RENEW
:
507 read_lock_bh(&ip6_sk_fl_lock
);
508 for (sfl
= np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
) {
509 if (sfl
->fl
->label
== freq
.flr_label
) {
510 err
= fl6_renew(sfl
->fl
, freq
.flr_linger
, freq
.flr_expires
);
511 read_unlock_bh(&ip6_sk_fl_lock
);
515 read_unlock_bh(&ip6_sk_fl_lock
);
517 if (freq
.flr_share
== IPV6_FL_S_NONE
&& capable(CAP_NET_ADMIN
)) {
518 fl
= fl_lookup(net
, freq
.flr_label
);
520 err
= fl6_renew(fl
, freq
.flr_linger
, freq
.flr_expires
);
528 if (freq
.flr_label
& ~IPV6_FLOWLABEL_MASK
)
531 fl
= fl_create(net
, &freq
, optval
, optlen
, &err
);
534 sfl1
= kmalloc(sizeof(*sfl1
), GFP_KERNEL
);
536 if (freq
.flr_label
) {
538 read_lock_bh(&ip6_sk_fl_lock
);
539 for (sfl
= np
->ipv6_fl_list
; sfl
; sfl
= sfl
->next
) {
540 if (sfl
->fl
->label
== freq
.flr_label
) {
541 if (freq
.flr_flags
&IPV6_FL_F_EXCL
) {
542 read_unlock_bh(&ip6_sk_fl_lock
);
546 atomic_inc(&fl1
->users
);
550 read_unlock_bh(&ip6_sk_fl_lock
);
553 fl1
= fl_lookup(net
, freq
.flr_label
);
557 if (freq
.flr_flags
&IPV6_FL_F_EXCL
)
560 if (fl1
->share
== IPV6_FL_S_EXCL
||
561 fl1
->share
!= fl
->share
||
562 fl1
->owner
!= fl
->owner
)
566 if (!ipv6_addr_equal(&fl1
->dst
, &fl
->dst
) ||
567 ipv6_opt_cmp(fl1
->opt
, fl
->opt
))
573 if (fl
->linger
> fl1
->linger
)
574 fl1
->linger
= fl
->linger
;
575 if ((long)(fl
->expires
- fl1
->expires
) > 0)
576 fl1
->expires
= fl
->expires
;
577 fl_link(np
, sfl1
, fl1
);
587 if (!(freq
.flr_flags
&IPV6_FL_F_CREATE
))
591 if (sfl1
== NULL
|| (err
= mem_check(sk
)) != 0)
594 fl1
= fl_intern(net
, fl
, freq
.flr_label
);
598 if (!freq
.flr_label
) {
599 if (copy_to_user(&((struct in6_flowlabel_req __user
*) optval
)->flr_label
,
600 &fl
->label
, sizeof(fl
->label
))) {
601 /* Intentionally ignore fault. */
605 fl_link(np
, sfl1
, fl
);
618 #ifdef CONFIG_PROC_FS
620 struct ip6fl_iter_state
{
621 struct seq_net_private p
;
625 #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
627 static struct ip6_flowlabel
*ip6fl_get_first(struct seq_file
*seq
)
629 struct ip6_flowlabel
*fl
= NULL
;
630 struct ip6fl_iter_state
*state
= ip6fl_seq_private(seq
);
631 struct net
*net
= seq_file_net(seq
);
633 for (state
->bucket
= 0; state
->bucket
<= FL_HASH_MASK
; ++state
->bucket
) {
634 fl
= fl_ht
[state
->bucket
];
636 while (fl
&& !net_eq(fl
->fl_net
, net
))
644 static struct ip6_flowlabel
*ip6fl_get_next(struct seq_file
*seq
, struct ip6_flowlabel
*fl
)
646 struct ip6fl_iter_state
*state
= ip6fl_seq_private(seq
);
647 struct net
*net
= seq_file_net(seq
);
651 while (fl
&& !net_eq(fl
->fl_net
, net
))
655 if (++state
->bucket
<= FL_HASH_MASK
) {
656 fl
= fl_ht
[state
->bucket
];
664 static struct ip6_flowlabel
*ip6fl_get_idx(struct seq_file
*seq
, loff_t pos
)
666 struct ip6_flowlabel
*fl
= ip6fl_get_first(seq
);
668 while (pos
&& (fl
= ip6fl_get_next(seq
, fl
)) != NULL
)
670 return pos
? NULL
: fl
;
673 static void *ip6fl_seq_start(struct seq_file
*seq
, loff_t
*pos
)
674 __acquires(ip6_fl_lock
)
676 read_lock_bh(&ip6_fl_lock
);
677 return *pos
? ip6fl_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
680 static void *ip6fl_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
682 struct ip6_flowlabel
*fl
;
684 if (v
== SEQ_START_TOKEN
)
685 fl
= ip6fl_get_first(seq
);
687 fl
= ip6fl_get_next(seq
, v
);
692 static void ip6fl_seq_stop(struct seq_file
*seq
, void *v
)
693 __releases(ip6_fl_lock
)
695 read_unlock_bh(&ip6_fl_lock
);
698 static int ip6fl_seq_show(struct seq_file
*seq
, void *v
)
700 if (v
== SEQ_START_TOKEN
)
701 seq_printf(seq
, "%-5s %-1s %-6s %-6s %-6s %-8s %-32s %s\n",
702 "Label", "S", "Owner", "Users", "Linger", "Expires", "Dst", "Opt");
704 struct ip6_flowlabel
*fl
= v
;
706 "%05X %-1d %-6d %-6d %-6ld %-8ld %pi6 %-4d\n",
707 (unsigned)ntohl(fl
->label
),
710 atomic_read(&fl
->users
),
712 (long)(fl
->expires
- jiffies
)/HZ
,
714 fl
->opt
? fl
->opt
->opt_nflen
: 0);
719 static const struct seq_operations ip6fl_seq_ops
= {
720 .start
= ip6fl_seq_start
,
721 .next
= ip6fl_seq_next
,
722 .stop
= ip6fl_seq_stop
,
723 .show
= ip6fl_seq_show
,
726 static int ip6fl_seq_open(struct inode
*inode
, struct file
*file
)
728 return seq_open_net(inode
, file
, &ip6fl_seq_ops
,
729 sizeof(struct ip6fl_iter_state
));
732 static const struct file_operations ip6fl_seq_fops
= {
733 .owner
= THIS_MODULE
,
734 .open
= ip6fl_seq_open
,
737 .release
= seq_release_net
,
740 static int __net_init
ip6_flowlabel_proc_init(struct net
*net
)
742 if (!proc_net_fops_create(net
, "ip6_flowlabel",
743 S_IRUGO
, &ip6fl_seq_fops
))
748 static void __net_exit
ip6_flowlabel_proc_fini(struct net
*net
)
750 proc_net_remove(net
, "ip6_flowlabel");
753 static inline int ip6_flowlabel_proc_init(struct net
*net
)
757 static inline void ip6_flowlabel_proc_fini(struct net
*net
)
762 static void __net_exit
ip6_flowlabel_net_exit(struct net
*net
)
765 ip6_flowlabel_proc_fini(net
);
768 static struct pernet_operations ip6_flowlabel_net_ops
= {
769 .init
= ip6_flowlabel_proc_init
,
770 .exit
= ip6_flowlabel_net_exit
,
773 int ip6_flowlabel_init(void)
775 return register_pernet_subsys(&ip6_flowlabel_net_ops
);
778 void ip6_flowlabel_cleanup(void)
780 del_timer(&ip6_fl_gc_timer
);
781 unregister_pernet_subsys(&ip6_flowlabel_net_ops
);