| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *	ip6_flowlabel.c		IPv6 flowlabel manager. | 
|  | 3 | * | 
|  | 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. | 
|  | 8 | * | 
|  | 9 | *	Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | 
|  | 10 | */ | 
|  | 11 |  | 
|  | 12 | #include <linux/config.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 |  | 
|  | 24 | #include <net/sock.h> | 
|  | 25 |  | 
|  | 26 | #include <net/ipv6.h> | 
|  | 27 | #include <net/ndisc.h> | 
|  | 28 | #include <net/protocol.h> | 
|  | 29 | #include <net/ip6_route.h> | 
|  | 30 | #include <net/addrconf.h> | 
|  | 31 | #include <net/rawv6.h> | 
|  | 32 | #include <net/icmp.h> | 
|  | 33 | #include <net/transp_v6.h> | 
|  | 34 |  | 
|  | 35 | #include <asm/uaccess.h> | 
|  | 36 |  | 
|  | 37 | #define FL_MIN_LINGER	6	/* Minimal linger. It is set to 6sec specified | 
|  | 38 | in old IPv6 RFC. Well, it was reasonable value. | 
|  | 39 | */ | 
|  | 40 | #define FL_MAX_LINGER	60	/* Maximal linger timeout */ | 
|  | 41 |  | 
|  | 42 | /* FL hash table */ | 
|  | 43 |  | 
|  | 44 | #define FL_MAX_PER_SOCK	32 | 
|  | 45 | #define FL_MAX_SIZE	4096 | 
|  | 46 | #define FL_HASH_MASK	255 | 
|  | 47 | #define FL_HASH(l)	(ntohl(l)&FL_HASH_MASK) | 
|  | 48 |  | 
|  | 49 | static atomic_t fl_size = ATOMIC_INIT(0); | 
|  | 50 | static struct ip6_flowlabel *fl_ht[FL_HASH_MASK+1]; | 
|  | 51 |  | 
|  | 52 | static void ip6_fl_gc(unsigned long dummy); | 
|  | 53 | static struct timer_list ip6_fl_gc_timer = TIMER_INITIALIZER(ip6_fl_gc, 0, 0); | 
|  | 54 |  | 
|  | 55 | /* FL hash table lock: it protects only of GC */ | 
|  | 56 |  | 
|  | 57 | static DEFINE_RWLOCK(ip6_fl_lock); | 
|  | 58 |  | 
|  | 59 | /* Big socket sock */ | 
|  | 60 |  | 
|  | 61 | static DEFINE_RWLOCK(ip6_sk_fl_lock); | 
|  | 62 |  | 
|  | 63 |  | 
|  | 64 | static __inline__ struct ip6_flowlabel * __fl_lookup(u32 label) | 
|  | 65 | { | 
|  | 66 | struct ip6_flowlabel *fl; | 
|  | 67 |  | 
|  | 68 | for (fl=fl_ht[FL_HASH(label)]; fl; fl = fl->next) { | 
|  | 69 | if (fl->label == label) | 
|  | 70 | return fl; | 
|  | 71 | } | 
|  | 72 | return NULL; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | static struct ip6_flowlabel * fl_lookup(u32 label) | 
|  | 76 | { | 
|  | 77 | struct ip6_flowlabel *fl; | 
|  | 78 |  | 
|  | 79 | read_lock_bh(&ip6_fl_lock); | 
|  | 80 | fl = __fl_lookup(label); | 
|  | 81 | if (fl) | 
|  | 82 | atomic_inc(&fl->users); | 
|  | 83 | read_unlock_bh(&ip6_fl_lock); | 
|  | 84 | return fl; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 |  | 
|  | 88 | static void fl_free(struct ip6_flowlabel *fl) | 
|  | 89 | { | 
|  | 90 | if (fl) | 
|  | 91 | kfree(fl->opt); | 
|  | 92 | kfree(fl); | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | static void fl_release(struct ip6_flowlabel *fl) | 
|  | 96 | { | 
|  | 97 | write_lock_bh(&ip6_fl_lock); | 
|  | 98 |  | 
|  | 99 | fl->lastuse = jiffies; | 
|  | 100 | if (atomic_dec_and_test(&fl->users)) { | 
|  | 101 | unsigned long ttd = fl->lastuse + fl->linger; | 
|  | 102 | if (time_after(ttd, fl->expires)) | 
|  | 103 | fl->expires = ttd; | 
|  | 104 | ttd = fl->expires; | 
|  | 105 | if (fl->opt && fl->share == IPV6_FL_S_EXCL) { | 
|  | 106 | struct ipv6_txoptions *opt = fl->opt; | 
|  | 107 | fl->opt = NULL; | 
|  | 108 | kfree(opt); | 
|  | 109 | } | 
|  | 110 | if (!timer_pending(&ip6_fl_gc_timer) || | 
|  | 111 | time_after(ip6_fl_gc_timer.expires, ttd)) | 
|  | 112 | mod_timer(&ip6_fl_gc_timer, ttd); | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | write_unlock_bh(&ip6_fl_lock); | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | static void ip6_fl_gc(unsigned long dummy) | 
|  | 119 | { | 
|  | 120 | int i; | 
|  | 121 | unsigned long now = jiffies; | 
|  | 122 | unsigned long sched = 0; | 
|  | 123 |  | 
|  | 124 | write_lock(&ip6_fl_lock); | 
|  | 125 |  | 
|  | 126 | for (i=0; i<=FL_HASH_MASK; i++) { | 
|  | 127 | struct ip6_flowlabel *fl, **flp; | 
|  | 128 | flp = &fl_ht[i]; | 
|  | 129 | while ((fl=*flp) != NULL) { | 
|  | 130 | if (atomic_read(&fl->users) == 0) { | 
|  | 131 | unsigned long ttd = fl->lastuse + fl->linger; | 
|  | 132 | if (time_after(ttd, fl->expires)) | 
|  | 133 | fl->expires = ttd; | 
|  | 134 | ttd = fl->expires; | 
|  | 135 | if (time_after_eq(now, ttd)) { | 
|  | 136 | *flp = fl->next; | 
|  | 137 | fl_free(fl); | 
|  | 138 | atomic_dec(&fl_size); | 
|  | 139 | continue; | 
|  | 140 | } | 
|  | 141 | if (!sched || time_before(ttd, sched)) | 
|  | 142 | sched = ttd; | 
|  | 143 | } | 
|  | 144 | flp = &fl->next; | 
|  | 145 | } | 
|  | 146 | } | 
|  | 147 | if (!sched && atomic_read(&fl_size)) | 
|  | 148 | sched = now + FL_MAX_LINGER; | 
|  | 149 | if (sched) { | 
|  | 150 | ip6_fl_gc_timer.expires = sched; | 
|  | 151 | add_timer(&ip6_fl_gc_timer); | 
|  | 152 | } | 
|  | 153 | write_unlock(&ip6_fl_lock); | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | static int fl_intern(struct ip6_flowlabel *fl, __u32 label) | 
|  | 157 | { | 
|  | 158 | fl->label = label & IPV6_FLOWLABEL_MASK; | 
|  | 159 |  | 
|  | 160 | write_lock_bh(&ip6_fl_lock); | 
|  | 161 | if (label == 0) { | 
|  | 162 | for (;;) { | 
|  | 163 | fl->label = htonl(net_random())&IPV6_FLOWLABEL_MASK; | 
|  | 164 | if (fl->label) { | 
|  | 165 | struct ip6_flowlabel *lfl; | 
|  | 166 | lfl = __fl_lookup(fl->label); | 
|  | 167 | if (lfl == NULL) | 
|  | 168 | break; | 
|  | 169 | } | 
|  | 170 | } | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | fl->lastuse = jiffies; | 
|  | 174 | fl->next = fl_ht[FL_HASH(fl->label)]; | 
|  | 175 | fl_ht[FL_HASH(fl->label)] = fl; | 
|  | 176 | atomic_inc(&fl_size); | 
|  | 177 | write_unlock_bh(&ip6_fl_lock); | 
|  | 178 | return 0; | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 |  | 
|  | 182 |  | 
|  | 183 | /* Socket flowlabel lists */ | 
|  | 184 |  | 
|  | 185 | struct ip6_flowlabel * fl6_sock_lookup(struct sock *sk, u32 label) | 
|  | 186 | { | 
|  | 187 | struct ipv6_fl_socklist *sfl; | 
|  | 188 | struct ipv6_pinfo *np = inet6_sk(sk); | 
|  | 189 |  | 
|  | 190 | label &= IPV6_FLOWLABEL_MASK; | 
|  | 191 |  | 
|  | 192 | for (sfl=np->ipv6_fl_list; sfl; sfl = sfl->next) { | 
|  | 193 | struct ip6_flowlabel *fl = sfl->fl; | 
|  | 194 | if (fl->label == label) { | 
|  | 195 | fl->lastuse = jiffies; | 
|  | 196 | atomic_inc(&fl->users); | 
|  | 197 | return fl; | 
|  | 198 | } | 
|  | 199 | } | 
|  | 200 | return NULL; | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | void fl6_free_socklist(struct sock *sk) | 
|  | 204 | { | 
|  | 205 | struct ipv6_pinfo *np = inet6_sk(sk); | 
|  | 206 | struct ipv6_fl_socklist *sfl; | 
|  | 207 |  | 
|  | 208 | while ((sfl = np->ipv6_fl_list) != NULL) { | 
|  | 209 | np->ipv6_fl_list = sfl->next; | 
|  | 210 | fl_release(sfl->fl); | 
|  | 211 | kfree(sfl); | 
|  | 212 | } | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | /* Service routines */ | 
|  | 216 |  | 
|  | 217 |  | 
|  | 218 | /* | 
|  | 219 | It is the only difficult place. flowlabel enforces equal headers | 
|  | 220 | before and including routing header, however user may supply options | 
|  | 221 | following rthdr. | 
|  | 222 | */ | 
|  | 223 |  | 
|  | 224 | struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions * opt_space, | 
|  | 225 | struct ip6_flowlabel * fl, | 
|  | 226 | struct ipv6_txoptions * fopt) | 
|  | 227 | { | 
|  | 228 | struct ipv6_txoptions * fl_opt = fl->opt; | 
|  | 229 |  | 
|  | 230 | if (fopt == NULL || fopt->opt_flen == 0) | 
|  | 231 | return fl_opt; | 
|  | 232 |  | 
|  | 233 | if (fl_opt != NULL) { | 
|  | 234 | opt_space->hopopt = fl_opt->hopopt; | 
|  | 235 | opt_space->dst0opt = fl_opt->dst0opt; | 
|  | 236 | opt_space->srcrt = fl_opt->srcrt; | 
|  | 237 | opt_space->opt_nflen = fl_opt->opt_nflen; | 
|  | 238 | } else { | 
|  | 239 | if (fopt->opt_nflen == 0) | 
|  | 240 | return fopt; | 
|  | 241 | opt_space->hopopt = NULL; | 
|  | 242 | opt_space->dst0opt = NULL; | 
|  | 243 | opt_space->srcrt = NULL; | 
|  | 244 | opt_space->opt_nflen = 0; | 
|  | 245 | } | 
|  | 246 | opt_space->dst1opt = fopt->dst1opt; | 
|  | 247 | opt_space->auth = fopt->auth; | 
|  | 248 | opt_space->opt_flen = fopt->opt_flen; | 
|  | 249 | return opt_space; | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | static unsigned long check_linger(unsigned long ttl) | 
|  | 253 | { | 
|  | 254 | if (ttl < FL_MIN_LINGER) | 
|  | 255 | return FL_MIN_LINGER*HZ; | 
|  | 256 | if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN)) | 
|  | 257 | return 0; | 
|  | 258 | return ttl*HZ; | 
|  | 259 | } | 
|  | 260 |  | 
|  | 261 | static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires) | 
|  | 262 | { | 
|  | 263 | linger = check_linger(linger); | 
|  | 264 | if (!linger) | 
|  | 265 | return -EPERM; | 
|  | 266 | expires = check_linger(expires); | 
|  | 267 | if (!expires) | 
|  | 268 | return -EPERM; | 
|  | 269 | fl->lastuse = jiffies; | 
|  | 270 | if (time_before(fl->linger, linger)) | 
|  | 271 | fl->linger = linger; | 
|  | 272 | if (time_before(expires, fl->linger)) | 
|  | 273 | expires = fl->linger; | 
|  | 274 | if (time_before(fl->expires, fl->lastuse + expires)) | 
|  | 275 | fl->expires = fl->lastuse + expires; | 
|  | 276 | return 0; | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | static struct ip6_flowlabel * | 
|  | 280 | fl_create(struct in6_flowlabel_req *freq, char __user *optval, int optlen, int *err_p) | 
|  | 281 | { | 
|  | 282 | struct ip6_flowlabel *fl; | 
|  | 283 | int olen; | 
|  | 284 | int addr_type; | 
|  | 285 | int err; | 
|  | 286 |  | 
|  | 287 | err = -ENOMEM; | 
|  | 288 | fl = kmalloc(sizeof(*fl), GFP_KERNEL); | 
|  | 289 | if (fl == NULL) | 
|  | 290 | goto done; | 
|  | 291 | memset(fl, 0, sizeof(*fl)); | 
|  | 292 |  | 
|  | 293 | olen = optlen - CMSG_ALIGN(sizeof(*freq)); | 
|  | 294 | if (olen > 0) { | 
|  | 295 | struct msghdr msg; | 
|  | 296 | struct flowi flowi; | 
|  | 297 | int junk; | 
|  | 298 |  | 
|  | 299 | err = -ENOMEM; | 
|  | 300 | fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL); | 
|  | 301 | if (fl->opt == NULL) | 
|  | 302 | goto done; | 
|  | 303 |  | 
|  | 304 | memset(fl->opt, 0, sizeof(*fl->opt)); | 
|  | 305 | fl->opt->tot_len = sizeof(*fl->opt) + olen; | 
|  | 306 | err = -EFAULT; | 
|  | 307 | if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen)) | 
|  | 308 | goto done; | 
|  | 309 |  | 
|  | 310 | msg.msg_controllen = olen; | 
|  | 311 | msg.msg_control = (void*)(fl->opt+1); | 
|  | 312 | flowi.oif = 0; | 
|  | 313 |  | 
|  | 314 | err = datagram_send_ctl(&msg, &flowi, fl->opt, &junk); | 
|  | 315 | if (err) | 
|  | 316 | goto done; | 
|  | 317 | err = -EINVAL; | 
|  | 318 | if (fl->opt->opt_flen) | 
|  | 319 | goto done; | 
|  | 320 | if (fl->opt->opt_nflen == 0) { | 
|  | 321 | kfree(fl->opt); | 
|  | 322 | fl->opt = NULL; | 
|  | 323 | } | 
|  | 324 | } | 
|  | 325 |  | 
|  | 326 | fl->expires = jiffies; | 
|  | 327 | err = fl6_renew(fl, freq->flr_linger, freq->flr_expires); | 
|  | 328 | if (err) | 
|  | 329 | goto done; | 
|  | 330 | fl->share = freq->flr_share; | 
|  | 331 | addr_type = ipv6_addr_type(&freq->flr_dst); | 
|  | 332 | if ((addr_type&IPV6_ADDR_MAPPED) | 
|  | 333 | || addr_type == IPV6_ADDR_ANY) | 
|  | 334 | goto done; | 
|  | 335 | ipv6_addr_copy(&fl->dst, &freq->flr_dst); | 
|  | 336 | atomic_set(&fl->users, 1); | 
|  | 337 | switch (fl->share) { | 
|  | 338 | case IPV6_FL_S_EXCL: | 
|  | 339 | case IPV6_FL_S_ANY: | 
|  | 340 | break; | 
|  | 341 | case IPV6_FL_S_PROCESS: | 
|  | 342 | fl->owner = current->pid; | 
|  | 343 | break; | 
|  | 344 | case IPV6_FL_S_USER: | 
|  | 345 | fl->owner = current->euid; | 
|  | 346 | break; | 
|  | 347 | default: | 
|  | 348 | err = -EINVAL; | 
|  | 349 | goto done; | 
|  | 350 | } | 
|  | 351 | return fl; | 
|  | 352 |  | 
|  | 353 | done: | 
|  | 354 | fl_free(fl); | 
|  | 355 | *err_p = err; | 
|  | 356 | return NULL; | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | static int mem_check(struct sock *sk) | 
|  | 360 | { | 
|  | 361 | struct ipv6_pinfo *np = inet6_sk(sk); | 
|  | 362 | struct ipv6_fl_socklist *sfl; | 
|  | 363 | int room = FL_MAX_SIZE - atomic_read(&fl_size); | 
|  | 364 | int count = 0; | 
|  | 365 |  | 
|  | 366 | if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK) | 
|  | 367 | return 0; | 
|  | 368 |  | 
|  | 369 | for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) | 
|  | 370 | count++; | 
|  | 371 |  | 
|  | 372 | if (room <= 0 || | 
|  | 373 | ((count >= FL_MAX_PER_SOCK || | 
|  | 374 | (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4) | 
|  | 375 | && !capable(CAP_NET_ADMIN))) | 
|  | 376 | return -ENOBUFS; | 
|  | 377 |  | 
|  | 378 | return 0; | 
|  | 379 | } | 
|  | 380 |  | 
|  | 381 | static int ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2) | 
|  | 382 | { | 
|  | 383 | if (h1 == h2) | 
|  | 384 | return 0; | 
|  | 385 | if (h1 == NULL || h2 == NULL) | 
|  | 386 | return 1; | 
|  | 387 | if (h1->hdrlen != h2->hdrlen) | 
|  | 388 | return 1; | 
|  | 389 | return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1)); | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | static int ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2) | 
|  | 393 | { | 
|  | 394 | if (o1 == o2) | 
|  | 395 | return 0; | 
|  | 396 | if (o1 == NULL || o2 == NULL) | 
|  | 397 | return 1; | 
|  | 398 | if (o1->opt_nflen != o2->opt_nflen) | 
|  | 399 | return 1; | 
|  | 400 | if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt)) | 
|  | 401 | return 1; | 
|  | 402 | if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt)) | 
|  | 403 | return 1; | 
|  | 404 | if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt)) | 
|  | 405 | return 1; | 
|  | 406 | return 0; | 
|  | 407 | } | 
|  | 408 |  | 
|  | 409 | int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen) | 
|  | 410 | { | 
|  | 411 | int err; | 
|  | 412 | struct ipv6_pinfo *np = inet6_sk(sk); | 
|  | 413 | struct in6_flowlabel_req freq; | 
|  | 414 | struct ipv6_fl_socklist *sfl1=NULL; | 
|  | 415 | struct ipv6_fl_socklist *sfl, **sflp; | 
|  | 416 | struct ip6_flowlabel *fl; | 
|  | 417 |  | 
|  | 418 | if (optlen < sizeof(freq)) | 
|  | 419 | return -EINVAL; | 
|  | 420 |  | 
|  | 421 | if (copy_from_user(&freq, optval, sizeof(freq))) | 
|  | 422 | return -EFAULT; | 
|  | 423 |  | 
|  | 424 | switch (freq.flr_action) { | 
|  | 425 | case IPV6_FL_A_PUT: | 
|  | 426 | write_lock_bh(&ip6_sk_fl_lock); | 
|  | 427 | for (sflp = &np->ipv6_fl_list; (sfl=*sflp)!=NULL; sflp = &sfl->next) { | 
|  | 428 | if (sfl->fl->label == freq.flr_label) { | 
|  | 429 | if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK)) | 
|  | 430 | np->flow_label &= ~IPV6_FLOWLABEL_MASK; | 
|  | 431 | *sflp = sfl->next; | 
|  | 432 | write_unlock_bh(&ip6_sk_fl_lock); | 
|  | 433 | fl_release(sfl->fl); | 
|  | 434 | kfree(sfl); | 
|  | 435 | return 0; | 
|  | 436 | } | 
|  | 437 | } | 
|  | 438 | write_unlock_bh(&ip6_sk_fl_lock); | 
|  | 439 | return -ESRCH; | 
|  | 440 |  | 
|  | 441 | case IPV6_FL_A_RENEW: | 
|  | 442 | read_lock_bh(&ip6_sk_fl_lock); | 
|  | 443 | for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) { | 
|  | 444 | if (sfl->fl->label == freq.flr_label) { | 
|  | 445 | err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires); | 
|  | 446 | read_unlock_bh(&ip6_sk_fl_lock); | 
|  | 447 | return err; | 
|  | 448 | } | 
|  | 449 | } | 
|  | 450 | read_unlock_bh(&ip6_sk_fl_lock); | 
|  | 451 |  | 
|  | 452 | if (freq.flr_share == IPV6_FL_S_NONE && capable(CAP_NET_ADMIN)) { | 
|  | 453 | fl = fl_lookup(freq.flr_label); | 
|  | 454 | if (fl) { | 
|  | 455 | err = fl6_renew(fl, freq.flr_linger, freq.flr_expires); | 
|  | 456 | fl_release(fl); | 
|  | 457 | return err; | 
|  | 458 | } | 
|  | 459 | } | 
|  | 460 | return -ESRCH; | 
|  | 461 |  | 
|  | 462 | case IPV6_FL_A_GET: | 
|  | 463 | if (freq.flr_label & ~IPV6_FLOWLABEL_MASK) | 
|  | 464 | return -EINVAL; | 
|  | 465 |  | 
|  | 466 | fl = fl_create(&freq, optval, optlen, &err); | 
|  | 467 | if (fl == NULL) | 
|  | 468 | return err; | 
|  | 469 | sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL); | 
|  | 470 |  | 
|  | 471 | if (freq.flr_label) { | 
|  | 472 | struct ip6_flowlabel *fl1 = NULL; | 
|  | 473 |  | 
|  | 474 | err = -EEXIST; | 
|  | 475 | read_lock_bh(&ip6_sk_fl_lock); | 
|  | 476 | for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) { | 
|  | 477 | if (sfl->fl->label == freq.flr_label) { | 
|  | 478 | if (freq.flr_flags&IPV6_FL_F_EXCL) { | 
|  | 479 | read_unlock_bh(&ip6_sk_fl_lock); | 
|  | 480 | goto done; | 
|  | 481 | } | 
|  | 482 | fl1 = sfl->fl; | 
|  | 483 | atomic_inc(&fl->users); | 
|  | 484 | break; | 
|  | 485 | } | 
|  | 486 | } | 
|  | 487 | read_unlock_bh(&ip6_sk_fl_lock); | 
|  | 488 |  | 
|  | 489 | if (fl1 == NULL) | 
|  | 490 | fl1 = fl_lookup(freq.flr_label); | 
|  | 491 | if (fl1) { | 
|  | 492 | err = -EEXIST; | 
|  | 493 | if (freq.flr_flags&IPV6_FL_F_EXCL) | 
|  | 494 | goto release; | 
|  | 495 | err = -EPERM; | 
|  | 496 | if (fl1->share == IPV6_FL_S_EXCL || | 
|  | 497 | fl1->share != fl->share || | 
|  | 498 | fl1->owner != fl->owner) | 
|  | 499 | goto release; | 
|  | 500 |  | 
|  | 501 | err = -EINVAL; | 
|  | 502 | if (!ipv6_addr_equal(&fl1->dst, &fl->dst) || | 
|  | 503 | ipv6_opt_cmp(fl1->opt, fl->opt)) | 
|  | 504 | goto release; | 
|  | 505 |  | 
|  | 506 | err = -ENOMEM; | 
|  | 507 | if (sfl1 == NULL) | 
|  | 508 | goto release; | 
|  | 509 | if (fl->linger > fl1->linger) | 
|  | 510 | fl1->linger = fl->linger; | 
|  | 511 | if ((long)(fl->expires - fl1->expires) > 0) | 
|  | 512 | fl1->expires = fl->expires; | 
|  | 513 | write_lock_bh(&ip6_sk_fl_lock); | 
|  | 514 | sfl1->fl = fl1; | 
|  | 515 | sfl1->next = np->ipv6_fl_list; | 
|  | 516 | np->ipv6_fl_list = sfl1; | 
|  | 517 | write_unlock_bh(&ip6_sk_fl_lock); | 
|  | 518 | fl_free(fl); | 
|  | 519 | return 0; | 
|  | 520 |  | 
|  | 521 | release: | 
|  | 522 | fl_release(fl1); | 
|  | 523 | goto done; | 
|  | 524 | } | 
|  | 525 | } | 
|  | 526 | err = -ENOENT; | 
|  | 527 | if (!(freq.flr_flags&IPV6_FL_F_CREATE)) | 
|  | 528 | goto done; | 
|  | 529 |  | 
|  | 530 | err = -ENOMEM; | 
|  | 531 | if (sfl1 == NULL || (err = mem_check(sk)) != 0) | 
|  | 532 | goto done; | 
|  | 533 |  | 
|  | 534 | err = fl_intern(fl, freq.flr_label); | 
|  | 535 | if (err) | 
|  | 536 | goto done; | 
|  | 537 |  | 
| David S. Miller | 6c94d36 | 2005-05-29 20:28:01 -0700 | [diff] [blame] | 538 | if (!freq.flr_label) { | 
|  | 539 | if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label, | 
|  | 540 | &fl->label, sizeof(fl->label))) { | 
|  | 541 | /* Intentionally ignore fault. */ | 
|  | 542 | } | 
|  | 543 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 |  | 
|  | 545 | sfl1->fl = fl; | 
|  | 546 | sfl1->next = np->ipv6_fl_list; | 
|  | 547 | np->ipv6_fl_list = sfl1; | 
|  | 548 | return 0; | 
|  | 549 |  | 
|  | 550 | default: | 
|  | 551 | return -EINVAL; | 
|  | 552 | } | 
|  | 553 |  | 
|  | 554 | done: | 
|  | 555 | fl_free(fl); | 
|  | 556 | kfree(sfl1); | 
|  | 557 | return err; | 
|  | 558 | } | 
|  | 559 |  | 
|  | 560 | #ifdef CONFIG_PROC_FS | 
|  | 561 |  | 
|  | 562 | struct ip6fl_iter_state { | 
|  | 563 | int bucket; | 
|  | 564 | }; | 
|  | 565 |  | 
|  | 566 | #define ip6fl_seq_private(seq)	((struct ip6fl_iter_state *)(seq)->private) | 
|  | 567 |  | 
|  | 568 | static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq) | 
|  | 569 | { | 
|  | 570 | struct ip6_flowlabel *fl = NULL; | 
|  | 571 | struct ip6fl_iter_state *state = ip6fl_seq_private(seq); | 
|  | 572 |  | 
|  | 573 | for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) { | 
|  | 574 | if (fl_ht[state->bucket]) { | 
|  | 575 | fl = fl_ht[state->bucket]; | 
|  | 576 | break; | 
|  | 577 | } | 
|  | 578 | } | 
|  | 579 | return fl; | 
|  | 580 | } | 
|  | 581 |  | 
|  | 582 | static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl) | 
|  | 583 | { | 
|  | 584 | struct ip6fl_iter_state *state = ip6fl_seq_private(seq); | 
|  | 585 |  | 
|  | 586 | fl = fl->next; | 
|  | 587 | while (!fl) { | 
|  | 588 | if (++state->bucket <= FL_HASH_MASK) | 
|  | 589 | fl = fl_ht[state->bucket]; | 
|  | 590 | } | 
|  | 591 | return fl; | 
|  | 592 | } | 
|  | 593 |  | 
|  | 594 | static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos) | 
|  | 595 | { | 
|  | 596 | struct ip6_flowlabel *fl = ip6fl_get_first(seq); | 
|  | 597 | if (fl) | 
|  | 598 | while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL) | 
|  | 599 | --pos; | 
|  | 600 | return pos ? NULL : fl; | 
|  | 601 | } | 
|  | 602 |  | 
|  | 603 | static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos) | 
|  | 604 | { | 
|  | 605 | read_lock_bh(&ip6_fl_lock); | 
|  | 606 | return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; | 
|  | 607 | } | 
|  | 608 |  | 
|  | 609 | static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos) | 
|  | 610 | { | 
|  | 611 | struct ip6_flowlabel *fl; | 
|  | 612 |  | 
|  | 613 | if (v == SEQ_START_TOKEN) | 
|  | 614 | fl = ip6fl_get_first(seq); | 
|  | 615 | else | 
|  | 616 | fl = ip6fl_get_next(seq, v); | 
|  | 617 | ++*pos; | 
|  | 618 | return fl; | 
|  | 619 | } | 
|  | 620 |  | 
|  | 621 | static void ip6fl_seq_stop(struct seq_file *seq, void *v) | 
|  | 622 | { | 
|  | 623 | read_unlock_bh(&ip6_fl_lock); | 
|  | 624 | } | 
|  | 625 |  | 
|  | 626 | static void ip6fl_fl_seq_show(struct seq_file *seq, struct ip6_flowlabel *fl) | 
|  | 627 | { | 
|  | 628 | while(fl) { | 
|  | 629 | seq_printf(seq, | 
|  | 630 | "%05X %-1d %-6d %-6d %-6ld %-8ld " | 
|  | 631 | "%02x%02x%02x%02x%02x%02x%02x%02x " | 
|  | 632 | "%-4d\n", | 
|  | 633 | (unsigned)ntohl(fl->label), | 
|  | 634 | fl->share, | 
|  | 635 | (unsigned)fl->owner, | 
|  | 636 | atomic_read(&fl->users), | 
|  | 637 | fl->linger/HZ, | 
|  | 638 | (long)(fl->expires - jiffies)/HZ, | 
|  | 639 | NIP6(fl->dst), | 
|  | 640 | fl->opt ? fl->opt->opt_nflen : 0); | 
|  | 641 | fl = fl->next; | 
|  | 642 | } | 
|  | 643 | } | 
|  | 644 |  | 
|  | 645 | static int ip6fl_seq_show(struct seq_file *seq, void *v) | 
|  | 646 | { | 
|  | 647 | if (v == SEQ_START_TOKEN) | 
|  | 648 | seq_puts(seq, "Label S Owner  Users  Linger Expires  " | 
|  | 649 | "Dst                              Opt\n"); | 
|  | 650 | else | 
|  | 651 | ip6fl_fl_seq_show(seq, v); | 
|  | 652 | return 0; | 
|  | 653 | } | 
|  | 654 |  | 
|  | 655 | static struct seq_operations ip6fl_seq_ops = { | 
|  | 656 | .start	=	ip6fl_seq_start, | 
|  | 657 | .next	=	ip6fl_seq_next, | 
|  | 658 | .stop	=	ip6fl_seq_stop, | 
|  | 659 | .show	=	ip6fl_seq_show, | 
|  | 660 | }; | 
|  | 661 |  | 
|  | 662 | static int ip6fl_seq_open(struct inode *inode, struct file *file) | 
|  | 663 | { | 
|  | 664 | struct seq_file *seq; | 
|  | 665 | int rc = -ENOMEM; | 
|  | 666 | struct ip6fl_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL); | 
|  | 667 |  | 
|  | 668 | if (!s) | 
|  | 669 | goto out; | 
|  | 670 |  | 
|  | 671 | rc = seq_open(file, &ip6fl_seq_ops); | 
|  | 672 | if (rc) | 
|  | 673 | goto out_kfree; | 
|  | 674 |  | 
|  | 675 | seq = file->private_data; | 
|  | 676 | seq->private = s; | 
|  | 677 | memset(s, 0, sizeof(*s)); | 
|  | 678 | out: | 
|  | 679 | return rc; | 
|  | 680 | out_kfree: | 
|  | 681 | kfree(s); | 
|  | 682 | goto out; | 
|  | 683 | } | 
|  | 684 |  | 
|  | 685 | static struct file_operations ip6fl_seq_fops = { | 
|  | 686 | .owner		=	THIS_MODULE, | 
|  | 687 | .open		=	ip6fl_seq_open, | 
|  | 688 | .read		=	seq_read, | 
|  | 689 | .llseek		=	seq_lseek, | 
|  | 690 | .release	=	seq_release_private, | 
|  | 691 | }; | 
|  | 692 | #endif | 
|  | 693 |  | 
|  | 694 |  | 
|  | 695 | void ip6_flowlabel_init(void) | 
|  | 696 | { | 
|  | 697 | #ifdef CONFIG_PROC_FS | 
|  | 698 | proc_net_fops_create("ip6_flowlabel", S_IRUGO, &ip6fl_seq_fops); | 
|  | 699 | #endif | 
|  | 700 | } | 
|  | 701 |  | 
|  | 702 | void ip6_flowlabel_cleanup(void) | 
|  | 703 | { | 
|  | 704 | del_timer(&ip6_fl_gc_timer); | 
|  | 705 | #ifdef CONFIG_PROC_FS | 
|  | 706 | proc_net_remove("ip6_flowlabel"); | 
|  | 707 | #endif | 
|  | 708 | } |