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(__be32 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(__be32 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, __be32 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, __be32 label)
188 struct ipv6_fl_socklist *sfl;
189 struct ipv6_pinfo *np = inet6_sk(sk);
191 label &= IPV6_FLOWLABEL_MASK;
193 read_lock_bh(&ip6_sk_fl_lock);
194 for (sfl=np->ipv6_fl_list; sfl; sfl = sfl->next) {
195 struct ip6_flowlabel *fl = sfl->fl;
196 if (fl->label == label) {
197 read_unlock_bh(&ip6_sk_fl_lock);
198 fl->lastuse = jiffies;
199 atomic_inc(&fl->users);
203 read_unlock_bh(&ip6_sk_fl_lock);
207 EXPORT_SYMBOL_GPL(fl6_sock_lookup);
209 void fl6_free_socklist(struct sock *sk)
211 struct ipv6_pinfo *np = inet6_sk(sk);
212 struct ipv6_fl_socklist *sfl;
214 while ((sfl = np->ipv6_fl_list) != NULL) {
215 np->ipv6_fl_list = sfl->next;
221 /* Service routines */
225 It is the only difficult place. flowlabel enforces equal headers
226 before and including routing header, however user may supply options
230 struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions * opt_space,
231 struct ip6_flowlabel * fl,
232 struct ipv6_txoptions * fopt)
234 struct ipv6_txoptions * fl_opt = fl->opt;
236 if (fopt == NULL || fopt->opt_flen == 0)
239 if (fl_opt != NULL) {
240 opt_space->hopopt = fl_opt->hopopt;
241 opt_space->dst0opt = fl_opt->dst0opt;
242 opt_space->srcrt = fl_opt->srcrt;
243 opt_space->opt_nflen = fl_opt->opt_nflen;
245 if (fopt->opt_nflen == 0)
247 opt_space->hopopt = NULL;
248 opt_space->dst0opt = NULL;
249 opt_space->srcrt = NULL;
250 opt_space->opt_nflen = 0;
252 opt_space->dst1opt = fopt->dst1opt;
253 opt_space->opt_flen = fopt->opt_flen;
257 static unsigned long check_linger(unsigned long ttl)
259 if (ttl < FL_MIN_LINGER)
260 return FL_MIN_LINGER*HZ;
261 if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN))
266 static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires)
268 linger = check_linger(linger);
271 expires = check_linger(expires);
274 fl->lastuse = jiffies;
275 if (time_before(fl->linger, linger))
277 if (time_before(expires, fl->linger))
278 expires = fl->linger;
279 if (time_before(fl->expires, fl->lastuse + expires))
280 fl->expires = fl->lastuse + expires;
284 static struct ip6_flowlabel *
285 fl_create(struct in6_flowlabel_req *freq, char __user *optval, int optlen, int *err_p)
287 struct ip6_flowlabel *fl;
293 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
297 olen = optlen - CMSG_ALIGN(sizeof(*freq));
304 fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
308 memset(fl->opt, 0, sizeof(*fl->opt));
309 fl->opt->tot_len = sizeof(*fl->opt) + olen;
311 if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen))
314 msg.msg_controllen = olen;
315 msg.msg_control = (void*)(fl->opt+1);
318 err = datagram_send_ctl(&msg, &flowi, fl->opt, &junk, &junk);
322 if (fl->opt->opt_flen)
324 if (fl->opt->opt_nflen == 0) {
330 fl->expires = jiffies;
331 err = fl6_renew(fl, freq->flr_linger, freq->flr_expires);
334 fl->share = freq->flr_share;
335 addr_type = ipv6_addr_type(&freq->flr_dst);
336 if ((addr_type&IPV6_ADDR_MAPPED)
337 || addr_type == IPV6_ADDR_ANY) {
341 ipv6_addr_copy(&fl->dst, &freq->flr_dst);
342 atomic_set(&fl->users, 1);
347 case IPV6_FL_S_PROCESS:
348 fl->owner = current->pid;
351 fl->owner = current->euid;
365 static int mem_check(struct sock *sk)
367 struct ipv6_pinfo *np = inet6_sk(sk);
368 struct ipv6_fl_socklist *sfl;
369 int room = FL_MAX_SIZE - atomic_read(&fl_size);
372 if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
375 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next)
379 ((count >= FL_MAX_PER_SOCK ||
380 (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4)
381 && !capable(CAP_NET_ADMIN)))
387 static int ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2)
391 if (h1 == NULL || h2 == NULL)
393 if (h1->hdrlen != h2->hdrlen)
395 return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1));
398 static int ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2)
402 if (o1 == NULL || o2 == NULL)
404 if (o1->opt_nflen != o2->opt_nflen)
406 if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt))
408 if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt))
410 if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt))
415 static inline void fl_link(struct ipv6_pinfo *np, struct ipv6_fl_socklist *sfl,
416 struct ip6_flowlabel *fl)
418 write_lock_bh(&ip6_sk_fl_lock);
420 sfl->next = np->ipv6_fl_list;
421 np->ipv6_fl_list = sfl;
422 write_unlock_bh(&ip6_sk_fl_lock);
425 int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen)
428 struct ipv6_pinfo *np = inet6_sk(sk);
429 struct in6_flowlabel_req freq;
430 struct ipv6_fl_socklist *sfl1=NULL;
431 struct ipv6_fl_socklist *sfl, **sflp;
432 struct ip6_flowlabel *fl;
434 if (optlen < sizeof(freq))
437 if (copy_from_user(&freq, optval, sizeof(freq)))
440 switch (freq.flr_action) {
442 write_lock_bh(&ip6_sk_fl_lock);
443 for (sflp = &np->ipv6_fl_list; (sfl=*sflp)!=NULL; sflp = &sfl->next) {
444 if (sfl->fl->label == freq.flr_label) {
445 if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK))
446 np->flow_label &= ~IPV6_FLOWLABEL_MASK;
448 write_unlock_bh(&ip6_sk_fl_lock);
454 write_unlock_bh(&ip6_sk_fl_lock);
457 case IPV6_FL_A_RENEW:
458 read_lock_bh(&ip6_sk_fl_lock);
459 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
460 if (sfl->fl->label == freq.flr_label) {
461 err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires);
462 read_unlock_bh(&ip6_sk_fl_lock);
466 read_unlock_bh(&ip6_sk_fl_lock);
468 if (freq.flr_share == IPV6_FL_S_NONE && capable(CAP_NET_ADMIN)) {
469 fl = fl_lookup(freq.flr_label);
471 err = fl6_renew(fl, freq.flr_linger, freq.flr_expires);
479 if (freq.flr_label & ~IPV6_FLOWLABEL_MASK)
482 fl = fl_create(&freq, optval, optlen, &err);
485 sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL);
487 if (freq.flr_label) {
488 struct ip6_flowlabel *fl1 = NULL;
491 read_lock_bh(&ip6_sk_fl_lock);
492 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
493 if (sfl->fl->label == freq.flr_label) {
494 if (freq.flr_flags&IPV6_FL_F_EXCL) {
495 read_unlock_bh(&ip6_sk_fl_lock);
499 atomic_inc(&fl1->users);
503 read_unlock_bh(&ip6_sk_fl_lock);
506 fl1 = fl_lookup(freq.flr_label);
509 if (freq.flr_flags&IPV6_FL_F_EXCL)
512 if (fl1->share == IPV6_FL_S_EXCL ||
513 fl1->share != fl->share ||
514 fl1->owner != fl->owner)
518 if (!ipv6_addr_equal(&fl1->dst, &fl->dst) ||
519 ipv6_opt_cmp(fl1->opt, fl->opt))
525 if (fl->linger > fl1->linger)
526 fl1->linger = fl->linger;
527 if ((long)(fl->expires - fl1->expires) > 0)
528 fl1->expires = fl->expires;
529 fl_link(np, sfl1, fl1);
539 if (!(freq.flr_flags&IPV6_FL_F_CREATE))
543 if (sfl1 == NULL || (err = mem_check(sk)) != 0)
546 err = fl_intern(fl, freq.flr_label);
550 if (!freq.flr_label) {
551 if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label,
552 &fl->label, sizeof(fl->label))) {
553 /* Intentionally ignore fault. */
557 fl_link(np, sfl1, fl);
570 #ifdef CONFIG_PROC_FS
572 struct ip6fl_iter_state {
576 #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
578 static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq)
580 struct ip6_flowlabel *fl = NULL;
581 struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
583 for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) {
584 if (fl_ht[state->bucket]) {
585 fl = fl_ht[state->bucket];
592 static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl)
594 struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
598 if (++state->bucket <= FL_HASH_MASK)
599 fl = fl_ht[state->bucket];
606 static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos)
608 struct ip6_flowlabel *fl = ip6fl_get_first(seq);
610 while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL)
612 return pos ? NULL : fl;
615 static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos)
617 read_lock_bh(&ip6_fl_lock);
618 return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
621 static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos)
623 struct ip6_flowlabel *fl;
625 if (v == SEQ_START_TOKEN)
626 fl = ip6fl_get_first(seq);
628 fl = ip6fl_get_next(seq, v);
633 static void ip6fl_seq_stop(struct seq_file *seq, void *v)
635 read_unlock_bh(&ip6_fl_lock);
638 static int ip6fl_seq_show(struct seq_file *seq, void *v)
640 if (v == SEQ_START_TOKEN)
641 seq_printf(seq, "%-5s %-1s %-6s %-6s %-6s %-8s %-32s %s\n",
642 "Label", "S", "Owner", "Users", "Linger", "Expires", "Dst", "Opt");
644 struct ip6_flowlabel *fl = v;
646 "%05X %-1d %-6d %-6d %-6ld %-8ld " NIP6_SEQFMT " %-4d\n",
647 (unsigned)ntohl(fl->label),
650 atomic_read(&fl->users),
652 (long)(fl->expires - jiffies)/HZ,
654 fl->opt ? fl->opt->opt_nflen : 0);
659 static const struct seq_operations ip6fl_seq_ops = {
660 .start = ip6fl_seq_start,
661 .next = ip6fl_seq_next,
662 .stop = ip6fl_seq_stop,
663 .show = ip6fl_seq_show,
666 static int ip6fl_seq_open(struct inode *inode, struct file *file)
668 return seq_open_private(file, &ip6fl_seq_ops,
669 sizeof(struct ip6fl_iter_state));
672 static const struct file_operations ip6fl_seq_fops = {
673 .owner = THIS_MODULE,
674 .open = ip6fl_seq_open,
677 .release = seq_release_private,
682 void ip6_flowlabel_init(void)
684 #ifdef CONFIG_PROC_FS
685 proc_net_fops_create(&init_net, "ip6_flowlabel", S_IRUGO, &ip6fl_seq_fops);
689 void ip6_flowlabel_cleanup(void)
691 del_timer(&ip6_fl_gc_timer);
692 #ifdef CONFIG_PROC_FS
693 proc_net_remove(&init_net, "ip6_flowlabel");