11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * ip6_flowlabel.c IPv6 flowlabel manager. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 51da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 61da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 71da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <linux/config.h> 131da177e4SLinus Torvalds #include <linux/errno.h> 141da177e4SLinus Torvalds #include <linux/types.h> 151da177e4SLinus Torvalds #include <linux/socket.h> 161da177e4SLinus Torvalds #include <linux/net.h> 171da177e4SLinus Torvalds #include <linux/netdevice.h> 181da177e4SLinus Torvalds #include <linux/if_arp.h> 191da177e4SLinus Torvalds #include <linux/in6.h> 201da177e4SLinus Torvalds #include <linux/route.h> 211da177e4SLinus Torvalds #include <linux/proc_fs.h> 221da177e4SLinus Torvalds #include <linux/seq_file.h> 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds #include <net/sock.h> 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #include <net/ipv6.h> 271da177e4SLinus Torvalds #include <net/ndisc.h> 281da177e4SLinus Torvalds #include <net/protocol.h> 291da177e4SLinus Torvalds #include <net/ip6_route.h> 301da177e4SLinus Torvalds #include <net/addrconf.h> 311da177e4SLinus Torvalds #include <net/rawv6.h> 321da177e4SLinus Torvalds #include <net/icmp.h> 331da177e4SLinus Torvalds #include <net/transp_v6.h> 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds #include <asm/uaccess.h> 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds #define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified 381da177e4SLinus Torvalds in old IPv6 RFC. Well, it was reasonable value. 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds #define FL_MAX_LINGER 60 /* Maximal linger timeout */ 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds /* FL hash table */ 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds #define FL_MAX_PER_SOCK 32 451da177e4SLinus Torvalds #define FL_MAX_SIZE 4096 461da177e4SLinus Torvalds #define FL_HASH_MASK 255 471da177e4SLinus Torvalds #define FL_HASH(l) (ntohl(l)&FL_HASH_MASK) 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds static atomic_t fl_size = ATOMIC_INIT(0); 501da177e4SLinus Torvalds static struct ip6_flowlabel *fl_ht[FL_HASH_MASK+1]; 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds static void ip6_fl_gc(unsigned long dummy); 531da177e4SLinus Torvalds static struct timer_list ip6_fl_gc_timer = TIMER_INITIALIZER(ip6_fl_gc, 0, 0); 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds /* FL hash table lock: it protects only of GC */ 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds static DEFINE_RWLOCK(ip6_fl_lock); 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds /* Big socket sock */ 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds static DEFINE_RWLOCK(ip6_sk_fl_lock); 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds static __inline__ struct ip6_flowlabel * __fl_lookup(u32 label) 651da177e4SLinus Torvalds { 661da177e4SLinus Torvalds struct ip6_flowlabel *fl; 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds for (fl=fl_ht[FL_HASH(label)]; fl; fl = fl->next) { 691da177e4SLinus Torvalds if (fl->label == label) 701da177e4SLinus Torvalds return fl; 711da177e4SLinus Torvalds } 721da177e4SLinus Torvalds return NULL; 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds static struct ip6_flowlabel * fl_lookup(u32 label) 761da177e4SLinus Torvalds { 771da177e4SLinus Torvalds struct ip6_flowlabel *fl; 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds read_lock_bh(&ip6_fl_lock); 801da177e4SLinus Torvalds fl = __fl_lookup(label); 811da177e4SLinus Torvalds if (fl) 821da177e4SLinus Torvalds atomic_inc(&fl->users); 831da177e4SLinus Torvalds read_unlock_bh(&ip6_fl_lock); 841da177e4SLinus Torvalds return fl; 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds static void fl_free(struct ip6_flowlabel *fl) 891da177e4SLinus Torvalds { 901da177e4SLinus Torvalds if (fl) 911da177e4SLinus Torvalds kfree(fl->opt); 921da177e4SLinus Torvalds kfree(fl); 931da177e4SLinus Torvalds } 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds static void fl_release(struct ip6_flowlabel *fl) 961da177e4SLinus Torvalds { 971da177e4SLinus Torvalds write_lock_bh(&ip6_fl_lock); 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds fl->lastuse = jiffies; 1001da177e4SLinus Torvalds if (atomic_dec_and_test(&fl->users)) { 1011da177e4SLinus Torvalds unsigned long ttd = fl->lastuse + fl->linger; 1021da177e4SLinus Torvalds if (time_after(ttd, fl->expires)) 1031da177e4SLinus Torvalds fl->expires = ttd; 1041da177e4SLinus Torvalds ttd = fl->expires; 1051da177e4SLinus Torvalds if (fl->opt && fl->share == IPV6_FL_S_EXCL) { 1061da177e4SLinus Torvalds struct ipv6_txoptions *opt = fl->opt; 1071da177e4SLinus Torvalds fl->opt = NULL; 1081da177e4SLinus Torvalds kfree(opt); 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds if (!timer_pending(&ip6_fl_gc_timer) || 1111da177e4SLinus Torvalds time_after(ip6_fl_gc_timer.expires, ttd)) 1121da177e4SLinus Torvalds mod_timer(&ip6_fl_gc_timer, ttd); 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds write_unlock_bh(&ip6_fl_lock); 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds static void ip6_fl_gc(unsigned long dummy) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds int i; 1211da177e4SLinus Torvalds unsigned long now = jiffies; 1221da177e4SLinus Torvalds unsigned long sched = 0; 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds write_lock(&ip6_fl_lock); 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds for (i=0; i<=FL_HASH_MASK; i++) { 1271da177e4SLinus Torvalds struct ip6_flowlabel *fl, **flp; 1281da177e4SLinus Torvalds flp = &fl_ht[i]; 1291da177e4SLinus Torvalds while ((fl=*flp) != NULL) { 1301da177e4SLinus Torvalds if (atomic_read(&fl->users) == 0) { 1311da177e4SLinus Torvalds unsigned long ttd = fl->lastuse + fl->linger; 1321da177e4SLinus Torvalds if (time_after(ttd, fl->expires)) 1331da177e4SLinus Torvalds fl->expires = ttd; 1341da177e4SLinus Torvalds ttd = fl->expires; 1351da177e4SLinus Torvalds if (time_after_eq(now, ttd)) { 1361da177e4SLinus Torvalds *flp = fl->next; 1371da177e4SLinus Torvalds fl_free(fl); 1381da177e4SLinus Torvalds atomic_dec(&fl_size); 1391da177e4SLinus Torvalds continue; 1401da177e4SLinus Torvalds } 1411da177e4SLinus Torvalds if (!sched || time_before(ttd, sched)) 1421da177e4SLinus Torvalds sched = ttd; 1431da177e4SLinus Torvalds } 1441da177e4SLinus Torvalds flp = &fl->next; 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds if (!sched && atomic_read(&fl_size)) 1481da177e4SLinus Torvalds sched = now + FL_MAX_LINGER; 1491da177e4SLinus Torvalds if (sched) { 1501da177e4SLinus Torvalds ip6_fl_gc_timer.expires = sched; 1511da177e4SLinus Torvalds add_timer(&ip6_fl_gc_timer); 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds write_unlock(&ip6_fl_lock); 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds static int fl_intern(struct ip6_flowlabel *fl, __u32 label) 1571da177e4SLinus Torvalds { 1581da177e4SLinus Torvalds fl->label = label & IPV6_FLOWLABEL_MASK; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds write_lock_bh(&ip6_fl_lock); 1611da177e4SLinus Torvalds if (label == 0) { 1621da177e4SLinus Torvalds for (;;) { 1631da177e4SLinus Torvalds fl->label = htonl(net_random())&IPV6_FLOWLABEL_MASK; 1641da177e4SLinus Torvalds if (fl->label) { 1651da177e4SLinus Torvalds struct ip6_flowlabel *lfl; 1661da177e4SLinus Torvalds lfl = __fl_lookup(fl->label); 1671da177e4SLinus Torvalds if (lfl == NULL) 1681da177e4SLinus Torvalds break; 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds fl->lastuse = jiffies; 1741da177e4SLinus Torvalds fl->next = fl_ht[FL_HASH(fl->label)]; 1751da177e4SLinus Torvalds fl_ht[FL_HASH(fl->label)] = fl; 1761da177e4SLinus Torvalds atomic_inc(&fl_size); 1771da177e4SLinus Torvalds write_unlock_bh(&ip6_fl_lock); 1781da177e4SLinus Torvalds return 0; 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds /* Socket flowlabel lists */ 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds struct ip6_flowlabel * fl6_sock_lookup(struct sock *sk, u32 label) 1861da177e4SLinus Torvalds { 1871da177e4SLinus Torvalds struct ipv6_fl_socklist *sfl; 1881da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds label &= IPV6_FLOWLABEL_MASK; 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds for (sfl=np->ipv6_fl_list; sfl; sfl = sfl->next) { 1931da177e4SLinus Torvalds struct ip6_flowlabel *fl = sfl->fl; 1941da177e4SLinus Torvalds if (fl->label == label) { 1951da177e4SLinus Torvalds fl->lastuse = jiffies; 1961da177e4SLinus Torvalds atomic_inc(&fl->users); 1971da177e4SLinus Torvalds return fl; 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds return NULL; 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds void fl6_free_socklist(struct sock *sk) 2041da177e4SLinus Torvalds { 2051da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 2061da177e4SLinus Torvalds struct ipv6_fl_socklist *sfl; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds while ((sfl = np->ipv6_fl_list) != NULL) { 2091da177e4SLinus Torvalds np->ipv6_fl_list = sfl->next; 2101da177e4SLinus Torvalds fl_release(sfl->fl); 2111da177e4SLinus Torvalds kfree(sfl); 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds /* Service routines */ 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds /* 2191da177e4SLinus Torvalds It is the only difficult place. flowlabel enforces equal headers 2201da177e4SLinus Torvalds before and including routing header, however user may supply options 2211da177e4SLinus Torvalds following rthdr. 2221da177e4SLinus Torvalds */ 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions * opt_space, 2251da177e4SLinus Torvalds struct ip6_flowlabel * fl, 2261da177e4SLinus Torvalds struct ipv6_txoptions * fopt) 2271da177e4SLinus Torvalds { 228333fad53SYOSHIFUJI Hideaki struct ipv6_txoptions * fl_opt = fl ? fl->opt : NULL; 2291da177e4SLinus Torvalds 230333fad53SYOSHIFUJI Hideaki if (fopt == NULL || fopt->opt_flen == 0) { 231333fad53SYOSHIFUJI Hideaki if (!fl_opt || !fl_opt->dst0opt || fl_opt->srcrt) 2321da177e4SLinus Torvalds return fl_opt; 233333fad53SYOSHIFUJI Hideaki } 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds if (fl_opt != NULL) { 2361da177e4SLinus Torvalds opt_space->hopopt = fl_opt->hopopt; 237333fad53SYOSHIFUJI Hideaki opt_space->dst0opt = fl_opt->srcrt ? fl_opt->dst0opt : NULL; 2381da177e4SLinus Torvalds opt_space->srcrt = fl_opt->srcrt; 2391da177e4SLinus Torvalds opt_space->opt_nflen = fl_opt->opt_nflen; 240333fad53SYOSHIFUJI Hideaki if (fl_opt->dst0opt && !fl_opt->srcrt) 241333fad53SYOSHIFUJI Hideaki opt_space->opt_nflen -= ipv6_optlen(fl_opt->dst0opt); 2421da177e4SLinus Torvalds } else { 2431da177e4SLinus Torvalds if (fopt->opt_nflen == 0) 2441da177e4SLinus Torvalds return fopt; 2451da177e4SLinus Torvalds opt_space->hopopt = NULL; 2461da177e4SLinus Torvalds opt_space->dst0opt = NULL; 2471da177e4SLinus Torvalds opt_space->srcrt = NULL; 2481da177e4SLinus Torvalds opt_space->opt_nflen = 0; 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds opt_space->dst1opt = fopt->dst1opt; 2511da177e4SLinus Torvalds opt_space->opt_flen = fopt->opt_flen; 2521da177e4SLinus Torvalds return opt_space; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds static unsigned long check_linger(unsigned long ttl) 2561da177e4SLinus Torvalds { 2571da177e4SLinus Torvalds if (ttl < FL_MIN_LINGER) 2581da177e4SLinus Torvalds return FL_MIN_LINGER*HZ; 2591da177e4SLinus Torvalds if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN)) 2601da177e4SLinus Torvalds return 0; 2611da177e4SLinus Torvalds return ttl*HZ; 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires) 2651da177e4SLinus Torvalds { 2661da177e4SLinus Torvalds linger = check_linger(linger); 2671da177e4SLinus Torvalds if (!linger) 2681da177e4SLinus Torvalds return -EPERM; 2691da177e4SLinus Torvalds expires = check_linger(expires); 2701da177e4SLinus Torvalds if (!expires) 2711da177e4SLinus Torvalds return -EPERM; 2721da177e4SLinus Torvalds fl->lastuse = jiffies; 2731da177e4SLinus Torvalds if (time_before(fl->linger, linger)) 2741da177e4SLinus Torvalds fl->linger = linger; 2751da177e4SLinus Torvalds if (time_before(expires, fl->linger)) 2761da177e4SLinus Torvalds expires = fl->linger; 2771da177e4SLinus Torvalds if (time_before(fl->expires, fl->lastuse + expires)) 2781da177e4SLinus Torvalds fl->expires = fl->lastuse + expires; 2791da177e4SLinus Torvalds return 0; 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds static struct ip6_flowlabel * 2831da177e4SLinus Torvalds fl_create(struct in6_flowlabel_req *freq, char __user *optval, int optlen, int *err_p) 2841da177e4SLinus Torvalds { 2851da177e4SLinus Torvalds struct ip6_flowlabel *fl; 2861da177e4SLinus Torvalds int olen; 2871da177e4SLinus Torvalds int addr_type; 2881da177e4SLinus Torvalds int err; 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds err = -ENOMEM; 2911da177e4SLinus Torvalds fl = kmalloc(sizeof(*fl), GFP_KERNEL); 2921da177e4SLinus Torvalds if (fl == NULL) 2931da177e4SLinus Torvalds goto done; 2941da177e4SLinus Torvalds memset(fl, 0, sizeof(*fl)); 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds olen = optlen - CMSG_ALIGN(sizeof(*freq)); 2971da177e4SLinus Torvalds if (olen > 0) { 2981da177e4SLinus Torvalds struct msghdr msg; 2991da177e4SLinus Torvalds struct flowi flowi; 3001da177e4SLinus Torvalds int junk; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds err = -ENOMEM; 3031da177e4SLinus Torvalds fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL); 3041da177e4SLinus Torvalds if (fl->opt == NULL) 3051da177e4SLinus Torvalds goto done; 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds memset(fl->opt, 0, sizeof(*fl->opt)); 3081da177e4SLinus Torvalds fl->opt->tot_len = sizeof(*fl->opt) + olen; 3091da177e4SLinus Torvalds err = -EFAULT; 3101da177e4SLinus Torvalds if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen)) 3111da177e4SLinus Torvalds goto done; 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds msg.msg_controllen = olen; 3141da177e4SLinus Torvalds msg.msg_control = (void*)(fl->opt+1); 3151da177e4SLinus Torvalds flowi.oif = 0; 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds err = datagram_send_ctl(&msg, &flowi, fl->opt, &junk); 3181da177e4SLinus Torvalds if (err) 3191da177e4SLinus Torvalds goto done; 3201da177e4SLinus Torvalds err = -EINVAL; 3211da177e4SLinus Torvalds if (fl->opt->opt_flen) 3221da177e4SLinus Torvalds goto done; 3231da177e4SLinus Torvalds if (fl->opt->opt_nflen == 0) { 3241da177e4SLinus Torvalds kfree(fl->opt); 3251da177e4SLinus Torvalds fl->opt = NULL; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds fl->expires = jiffies; 3301da177e4SLinus Torvalds err = fl6_renew(fl, freq->flr_linger, freq->flr_expires); 3311da177e4SLinus Torvalds if (err) 3321da177e4SLinus Torvalds goto done; 3331da177e4SLinus Torvalds fl->share = freq->flr_share; 3341da177e4SLinus Torvalds addr_type = ipv6_addr_type(&freq->flr_dst); 3351da177e4SLinus Torvalds if ((addr_type&IPV6_ADDR_MAPPED) 3361da177e4SLinus Torvalds || addr_type == IPV6_ADDR_ANY) 3371da177e4SLinus Torvalds goto done; 3381da177e4SLinus Torvalds ipv6_addr_copy(&fl->dst, &freq->flr_dst); 3391da177e4SLinus Torvalds atomic_set(&fl->users, 1); 3401da177e4SLinus Torvalds switch (fl->share) { 3411da177e4SLinus Torvalds case IPV6_FL_S_EXCL: 3421da177e4SLinus Torvalds case IPV6_FL_S_ANY: 3431da177e4SLinus Torvalds break; 3441da177e4SLinus Torvalds case IPV6_FL_S_PROCESS: 3451da177e4SLinus Torvalds fl->owner = current->pid; 3461da177e4SLinus Torvalds break; 3471da177e4SLinus Torvalds case IPV6_FL_S_USER: 3481da177e4SLinus Torvalds fl->owner = current->euid; 3491da177e4SLinus Torvalds break; 3501da177e4SLinus Torvalds default: 3511da177e4SLinus Torvalds err = -EINVAL; 3521da177e4SLinus Torvalds goto done; 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds return fl; 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds done: 3571da177e4SLinus Torvalds fl_free(fl); 3581da177e4SLinus Torvalds *err_p = err; 3591da177e4SLinus Torvalds return NULL; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds static int mem_check(struct sock *sk) 3631da177e4SLinus Torvalds { 3641da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 3651da177e4SLinus Torvalds struct ipv6_fl_socklist *sfl; 3661da177e4SLinus Torvalds int room = FL_MAX_SIZE - atomic_read(&fl_size); 3671da177e4SLinus Torvalds int count = 0; 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK) 3701da177e4SLinus Torvalds return 0; 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) 3731da177e4SLinus Torvalds count++; 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds if (room <= 0 || 3761da177e4SLinus Torvalds ((count >= FL_MAX_PER_SOCK || 3771da177e4SLinus Torvalds (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4) 3781da177e4SLinus Torvalds && !capable(CAP_NET_ADMIN))) 3791da177e4SLinus Torvalds return -ENOBUFS; 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds return 0; 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds static int ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2) 3851da177e4SLinus Torvalds { 3861da177e4SLinus Torvalds if (h1 == h2) 3871da177e4SLinus Torvalds return 0; 3881da177e4SLinus Torvalds if (h1 == NULL || h2 == NULL) 3891da177e4SLinus Torvalds return 1; 3901da177e4SLinus Torvalds if (h1->hdrlen != h2->hdrlen) 3911da177e4SLinus Torvalds return 1; 3921da177e4SLinus Torvalds return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1)); 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds static int ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2) 3961da177e4SLinus Torvalds { 3971da177e4SLinus Torvalds if (o1 == o2) 3981da177e4SLinus Torvalds return 0; 3991da177e4SLinus Torvalds if (o1 == NULL || o2 == NULL) 4001da177e4SLinus Torvalds return 1; 4011da177e4SLinus Torvalds if (o1->opt_nflen != o2->opt_nflen) 4021da177e4SLinus Torvalds return 1; 4031da177e4SLinus Torvalds if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt)) 4041da177e4SLinus Torvalds return 1; 4051da177e4SLinus Torvalds if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt)) 4061da177e4SLinus Torvalds return 1; 4071da177e4SLinus Torvalds if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt)) 4081da177e4SLinus Torvalds return 1; 4091da177e4SLinus Torvalds return 0; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen) 4131da177e4SLinus Torvalds { 4141da177e4SLinus Torvalds int err; 4151da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 4161da177e4SLinus Torvalds struct in6_flowlabel_req freq; 4171da177e4SLinus Torvalds struct ipv6_fl_socklist *sfl1=NULL; 4181da177e4SLinus Torvalds struct ipv6_fl_socklist *sfl, **sflp; 4191da177e4SLinus Torvalds struct ip6_flowlabel *fl; 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds if (optlen < sizeof(freq)) 4221da177e4SLinus Torvalds return -EINVAL; 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds if (copy_from_user(&freq, optval, sizeof(freq))) 4251da177e4SLinus Torvalds return -EFAULT; 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds switch (freq.flr_action) { 4281da177e4SLinus Torvalds case IPV6_FL_A_PUT: 4291da177e4SLinus Torvalds write_lock_bh(&ip6_sk_fl_lock); 4301da177e4SLinus Torvalds for (sflp = &np->ipv6_fl_list; (sfl=*sflp)!=NULL; sflp = &sfl->next) { 4311da177e4SLinus Torvalds if (sfl->fl->label == freq.flr_label) { 4321da177e4SLinus Torvalds if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK)) 4331da177e4SLinus Torvalds np->flow_label &= ~IPV6_FLOWLABEL_MASK; 4341da177e4SLinus Torvalds *sflp = sfl->next; 4351da177e4SLinus Torvalds write_unlock_bh(&ip6_sk_fl_lock); 4361da177e4SLinus Torvalds fl_release(sfl->fl); 4371da177e4SLinus Torvalds kfree(sfl); 4381da177e4SLinus Torvalds return 0; 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds write_unlock_bh(&ip6_sk_fl_lock); 4421da177e4SLinus Torvalds return -ESRCH; 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds case IPV6_FL_A_RENEW: 4451da177e4SLinus Torvalds read_lock_bh(&ip6_sk_fl_lock); 4461da177e4SLinus Torvalds for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) { 4471da177e4SLinus Torvalds if (sfl->fl->label == freq.flr_label) { 4481da177e4SLinus Torvalds err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires); 4491da177e4SLinus Torvalds read_unlock_bh(&ip6_sk_fl_lock); 4501da177e4SLinus Torvalds return err; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds read_unlock_bh(&ip6_sk_fl_lock); 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds if (freq.flr_share == IPV6_FL_S_NONE && capable(CAP_NET_ADMIN)) { 4561da177e4SLinus Torvalds fl = fl_lookup(freq.flr_label); 4571da177e4SLinus Torvalds if (fl) { 4581da177e4SLinus Torvalds err = fl6_renew(fl, freq.flr_linger, freq.flr_expires); 4591da177e4SLinus Torvalds fl_release(fl); 4601da177e4SLinus Torvalds return err; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds return -ESRCH; 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds case IPV6_FL_A_GET: 4661da177e4SLinus Torvalds if (freq.flr_label & ~IPV6_FLOWLABEL_MASK) 4671da177e4SLinus Torvalds return -EINVAL; 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds fl = fl_create(&freq, optval, optlen, &err); 4701da177e4SLinus Torvalds if (fl == NULL) 4711da177e4SLinus Torvalds return err; 4721da177e4SLinus Torvalds sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL); 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds if (freq.flr_label) { 4751da177e4SLinus Torvalds struct ip6_flowlabel *fl1 = NULL; 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds err = -EEXIST; 4781da177e4SLinus Torvalds read_lock_bh(&ip6_sk_fl_lock); 4791da177e4SLinus Torvalds for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) { 4801da177e4SLinus Torvalds if (sfl->fl->label == freq.flr_label) { 4811da177e4SLinus Torvalds if (freq.flr_flags&IPV6_FL_F_EXCL) { 4821da177e4SLinus Torvalds read_unlock_bh(&ip6_sk_fl_lock); 4831da177e4SLinus Torvalds goto done; 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds fl1 = sfl->fl; 4861da177e4SLinus Torvalds atomic_inc(&fl->users); 4871da177e4SLinus Torvalds break; 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds read_unlock_bh(&ip6_sk_fl_lock); 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds if (fl1 == NULL) 4931da177e4SLinus Torvalds fl1 = fl_lookup(freq.flr_label); 4941da177e4SLinus Torvalds if (fl1) { 4951da177e4SLinus Torvalds err = -EEXIST; 4961da177e4SLinus Torvalds if (freq.flr_flags&IPV6_FL_F_EXCL) 4971da177e4SLinus Torvalds goto release; 4981da177e4SLinus Torvalds err = -EPERM; 4991da177e4SLinus Torvalds if (fl1->share == IPV6_FL_S_EXCL || 5001da177e4SLinus Torvalds fl1->share != fl->share || 5011da177e4SLinus Torvalds fl1->owner != fl->owner) 5021da177e4SLinus Torvalds goto release; 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds err = -EINVAL; 5051da177e4SLinus Torvalds if (!ipv6_addr_equal(&fl1->dst, &fl->dst) || 5061da177e4SLinus Torvalds ipv6_opt_cmp(fl1->opt, fl->opt)) 5071da177e4SLinus Torvalds goto release; 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds err = -ENOMEM; 5101da177e4SLinus Torvalds if (sfl1 == NULL) 5111da177e4SLinus Torvalds goto release; 5121da177e4SLinus Torvalds if (fl->linger > fl1->linger) 5131da177e4SLinus Torvalds fl1->linger = fl->linger; 5141da177e4SLinus Torvalds if ((long)(fl->expires - fl1->expires) > 0) 5151da177e4SLinus Torvalds fl1->expires = fl->expires; 5161da177e4SLinus Torvalds write_lock_bh(&ip6_sk_fl_lock); 5171da177e4SLinus Torvalds sfl1->fl = fl1; 5181da177e4SLinus Torvalds sfl1->next = np->ipv6_fl_list; 5191da177e4SLinus Torvalds np->ipv6_fl_list = sfl1; 5201da177e4SLinus Torvalds write_unlock_bh(&ip6_sk_fl_lock); 5211da177e4SLinus Torvalds fl_free(fl); 5221da177e4SLinus Torvalds return 0; 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds release: 5251da177e4SLinus Torvalds fl_release(fl1); 5261da177e4SLinus Torvalds goto done; 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds err = -ENOENT; 5301da177e4SLinus Torvalds if (!(freq.flr_flags&IPV6_FL_F_CREATE)) 5311da177e4SLinus Torvalds goto done; 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds err = -ENOMEM; 5341da177e4SLinus Torvalds if (sfl1 == NULL || (err = mem_check(sk)) != 0) 5351da177e4SLinus Torvalds goto done; 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds err = fl_intern(fl, freq.flr_label); 5381da177e4SLinus Torvalds if (err) 5391da177e4SLinus Torvalds goto done; 5401da177e4SLinus Torvalds 5416c94d361SDavid S. Miller if (!freq.flr_label) { 5426c94d361SDavid S. Miller if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label, 5436c94d361SDavid S. Miller &fl->label, sizeof(fl->label))) { 5446c94d361SDavid S. Miller /* Intentionally ignore fault. */ 5456c94d361SDavid S. Miller } 5466c94d361SDavid S. Miller } 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds sfl1->fl = fl; 5491da177e4SLinus Torvalds sfl1->next = np->ipv6_fl_list; 5501da177e4SLinus Torvalds np->ipv6_fl_list = sfl1; 5511da177e4SLinus Torvalds return 0; 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds default: 5541da177e4SLinus Torvalds return -EINVAL; 5551da177e4SLinus Torvalds } 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds done: 5581da177e4SLinus Torvalds fl_free(fl); 5591da177e4SLinus Torvalds kfree(sfl1); 5601da177e4SLinus Torvalds return err; 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds struct ip6fl_iter_state { 5661da177e4SLinus Torvalds int bucket; 5671da177e4SLinus Torvalds }; 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private) 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq) 5721da177e4SLinus Torvalds { 5731da177e4SLinus Torvalds struct ip6_flowlabel *fl = NULL; 5741da177e4SLinus Torvalds struct ip6fl_iter_state *state = ip6fl_seq_private(seq); 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) { 5771da177e4SLinus Torvalds if (fl_ht[state->bucket]) { 5781da177e4SLinus Torvalds fl = fl_ht[state->bucket]; 5791da177e4SLinus Torvalds break; 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds return fl; 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl) 5861da177e4SLinus Torvalds { 5871da177e4SLinus Torvalds struct ip6fl_iter_state *state = ip6fl_seq_private(seq); 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds fl = fl->next; 5901da177e4SLinus Torvalds while (!fl) { 5911da177e4SLinus Torvalds if (++state->bucket <= FL_HASH_MASK) 5921da177e4SLinus Torvalds fl = fl_ht[state->bucket]; 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds return fl; 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos) 5981da177e4SLinus Torvalds { 5991da177e4SLinus Torvalds struct ip6_flowlabel *fl = ip6fl_get_first(seq); 6001da177e4SLinus Torvalds if (fl) 6011da177e4SLinus Torvalds while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL) 6021da177e4SLinus Torvalds --pos; 6031da177e4SLinus Torvalds return pos ? NULL : fl; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos) 6071da177e4SLinus Torvalds { 6081da177e4SLinus Torvalds read_lock_bh(&ip6_fl_lock); 6091da177e4SLinus Torvalds return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos) 6131da177e4SLinus Torvalds { 6141da177e4SLinus Torvalds struct ip6_flowlabel *fl; 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 6171da177e4SLinus Torvalds fl = ip6fl_get_first(seq); 6181da177e4SLinus Torvalds else 6191da177e4SLinus Torvalds fl = ip6fl_get_next(seq, v); 6201da177e4SLinus Torvalds ++*pos; 6211da177e4SLinus Torvalds return fl; 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds static void ip6fl_seq_stop(struct seq_file *seq, void *v) 6251da177e4SLinus Torvalds { 6261da177e4SLinus Torvalds read_unlock_bh(&ip6_fl_lock); 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds static void ip6fl_fl_seq_show(struct seq_file *seq, struct ip6_flowlabel *fl) 6301da177e4SLinus Torvalds { 6311da177e4SLinus Torvalds while(fl) { 6321da177e4SLinus Torvalds seq_printf(seq, 6331da177e4SLinus Torvalds "%05X %-1d %-6d %-6d %-6ld %-8ld " 6341da177e4SLinus Torvalds "%02x%02x%02x%02x%02x%02x%02x%02x " 6351da177e4SLinus Torvalds "%-4d\n", 6361da177e4SLinus Torvalds (unsigned)ntohl(fl->label), 6371da177e4SLinus Torvalds fl->share, 6381da177e4SLinus Torvalds (unsigned)fl->owner, 6391da177e4SLinus Torvalds atomic_read(&fl->users), 6401da177e4SLinus Torvalds fl->linger/HZ, 6411da177e4SLinus Torvalds (long)(fl->expires - jiffies)/HZ, 6421da177e4SLinus Torvalds NIP6(fl->dst), 6431da177e4SLinus Torvalds fl->opt ? fl->opt->opt_nflen : 0); 6441da177e4SLinus Torvalds fl = fl->next; 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds } 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds static int ip6fl_seq_show(struct seq_file *seq, void *v) 6491da177e4SLinus Torvalds { 6501da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 6511da177e4SLinus Torvalds seq_puts(seq, "Label S Owner Users Linger Expires " 6521da177e4SLinus Torvalds "Dst Opt\n"); 6531da177e4SLinus Torvalds else 6541da177e4SLinus Torvalds ip6fl_fl_seq_show(seq, v); 6551da177e4SLinus Torvalds return 0; 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds static struct seq_operations ip6fl_seq_ops = { 6591da177e4SLinus Torvalds .start = ip6fl_seq_start, 6601da177e4SLinus Torvalds .next = ip6fl_seq_next, 6611da177e4SLinus Torvalds .stop = ip6fl_seq_stop, 6621da177e4SLinus Torvalds .show = ip6fl_seq_show, 6631da177e4SLinus Torvalds }; 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds static int ip6fl_seq_open(struct inode *inode, struct file *file) 6661da177e4SLinus Torvalds { 6671da177e4SLinus Torvalds struct seq_file *seq; 6681da177e4SLinus Torvalds int rc = -ENOMEM; 6691da177e4SLinus Torvalds struct ip6fl_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL); 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds if (!s) 6721da177e4SLinus Torvalds goto out; 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds rc = seq_open(file, &ip6fl_seq_ops); 6751da177e4SLinus Torvalds if (rc) 6761da177e4SLinus Torvalds goto out_kfree; 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds seq = file->private_data; 6791da177e4SLinus Torvalds seq->private = s; 6801da177e4SLinus Torvalds memset(s, 0, sizeof(*s)); 6811da177e4SLinus Torvalds out: 6821da177e4SLinus Torvalds return rc; 6831da177e4SLinus Torvalds out_kfree: 6841da177e4SLinus Torvalds kfree(s); 6851da177e4SLinus Torvalds goto out; 6861da177e4SLinus Torvalds } 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds static struct file_operations ip6fl_seq_fops = { 6891da177e4SLinus Torvalds .owner = THIS_MODULE, 6901da177e4SLinus Torvalds .open = ip6fl_seq_open, 6911da177e4SLinus Torvalds .read = seq_read, 6921da177e4SLinus Torvalds .llseek = seq_lseek, 6931da177e4SLinus Torvalds .release = seq_release_private, 6941da177e4SLinus Torvalds }; 6951da177e4SLinus Torvalds #endif 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds void ip6_flowlabel_init(void) 6991da177e4SLinus Torvalds { 7001da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 7011da177e4SLinus Torvalds proc_net_fops_create("ip6_flowlabel", S_IRUGO, &ip6fl_seq_fops); 7021da177e4SLinus Torvalds #endif 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds void ip6_flowlabel_cleanup(void) 7061da177e4SLinus Torvalds { 7071da177e4SLinus Torvalds del_timer(&ip6_fl_gc_timer); 7081da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 7091da177e4SLinus Torvalds proc_net_remove("ip6_flowlabel"); 7101da177e4SLinus Torvalds #endif 7111da177e4SLinus Torvalds } 712