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 | |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 12 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Ingo Molnar | 8d06afa | 2005-09-09 13:10:40 -0700 | [diff] [blame] | 53 | static DEFINE_TIMER(ip6_fl_gc_timer, ip6_fl_gc, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Al Viro | 90bcaf7 | 2006-11-08 00:25:17 -0800 | [diff] [blame] | 64 | static __inline__ struct ip6_flowlabel * __fl_lookup(__be32 label) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Al Viro | 90bcaf7 | 2006-11-08 00:25:17 -0800 | [diff] [blame] | 75 | static struct ip6_flowlabel * fl_lookup(__be32 label) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Al Viro | 90bcaf7 | 2006-11-08 00:25:17 -0800 | [diff] [blame] | 156 | static int fl_intern(struct ip6_flowlabel *fl, __be32 label) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Al Viro | 90bcaf7 | 2006-11-08 00:25:17 -0800 | [diff] [blame] | 185 | struct ip6_flowlabel * fl6_sock_lookup(struct sock *sk, __be32 label) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | 3cf3dc6 | 2005-12-13 23:23:20 -0800 | [diff] [blame] | 203 | EXPORT_SYMBOL_GPL(fl6_sock_lookup); |
| 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | void fl6_free_socklist(struct sock *sk) |
| 206 | { |
| 207 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 208 | struct ipv6_fl_socklist *sfl; |
| 209 | |
| 210 | while ((sfl = np->ipv6_fl_list) != NULL) { |
| 211 | np->ipv6_fl_list = sfl->next; |
| 212 | fl_release(sfl->fl); |
| 213 | kfree(sfl); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /* Service routines */ |
| 218 | |
| 219 | |
| 220 | /* |
| 221 | It is the only difficult place. flowlabel enforces equal headers |
| 222 | before and including routing header, however user may supply options |
| 223 | following rthdr. |
| 224 | */ |
| 225 | |
| 226 | struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions * opt_space, |
| 227 | struct ip6_flowlabel * fl, |
| 228 | struct ipv6_txoptions * fopt) |
| 229 | { |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 230 | struct ipv6_txoptions * fl_opt = fl->opt; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 231 | |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 232 | if (fopt == NULL || fopt->opt_flen == 0) |
| 233 | return fl_opt; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | if (fl_opt != NULL) { |
| 236 | opt_space->hopopt = fl_opt->hopopt; |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 237 | opt_space->dst0opt = fl_opt->dst0opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | opt_space->srcrt = fl_opt->srcrt; |
| 239 | opt_space->opt_nflen = fl_opt->opt_nflen; |
| 240 | } else { |
| 241 | if (fopt->opt_nflen == 0) |
| 242 | return fopt; |
| 243 | opt_space->hopopt = NULL; |
| 244 | opt_space->dst0opt = NULL; |
| 245 | opt_space->srcrt = NULL; |
| 246 | opt_space->opt_nflen = 0; |
| 247 | } |
| 248 | opt_space->dst1opt = fopt->dst1opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | opt_space->opt_flen = fopt->opt_flen; |
| 250 | return opt_space; |
| 251 | } |
| 252 | |
| 253 | static unsigned long check_linger(unsigned long ttl) |
| 254 | { |
| 255 | if (ttl < FL_MIN_LINGER) |
| 256 | return FL_MIN_LINGER*HZ; |
| 257 | if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN)) |
| 258 | return 0; |
| 259 | return ttl*HZ; |
| 260 | } |
| 261 | |
| 262 | static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires) |
| 263 | { |
| 264 | linger = check_linger(linger); |
| 265 | if (!linger) |
| 266 | return -EPERM; |
| 267 | expires = check_linger(expires); |
| 268 | if (!expires) |
| 269 | return -EPERM; |
| 270 | fl->lastuse = jiffies; |
| 271 | if (time_before(fl->linger, linger)) |
| 272 | fl->linger = linger; |
| 273 | if (time_before(expires, fl->linger)) |
| 274 | expires = fl->linger; |
| 275 | if (time_before(fl->expires, fl->lastuse + expires)) |
| 276 | fl->expires = fl->lastuse + expires; |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static struct ip6_flowlabel * |
| 281 | fl_create(struct in6_flowlabel_req *freq, char __user *optval, int optlen, int *err_p) |
| 282 | { |
| 283 | struct ip6_flowlabel *fl; |
| 284 | int olen; |
| 285 | int addr_type; |
| 286 | int err; |
| 287 | |
| 288 | err = -ENOMEM; |
Ingo Oeser | 0c600ed | 2006-03-20 23:01:32 -0800 | [diff] [blame] | 289 | fl = kzalloc(sizeof(*fl), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | if (fl == NULL) |
| 291 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 314 | err = datagram_send_ctl(&msg, &flowi, fl->opt, &junk, &junk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
James Morris | c6817e4 | 2006-10-30 18:56:06 -0800 | [diff] [blame] | 333 | || addr_type == IPV6_ADDR_ANY) { |
| 334 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | goto done; |
James Morris | c6817e4 | 2006-10-30 18:56:06 -0800 | [diff] [blame] | 336 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | ipv6_addr_copy(&fl->dst, &freq->flr_dst); |
| 338 | atomic_set(&fl->users, 1); |
| 339 | switch (fl->share) { |
| 340 | case IPV6_FL_S_EXCL: |
| 341 | case IPV6_FL_S_ANY: |
| 342 | break; |
| 343 | case IPV6_FL_S_PROCESS: |
| 344 | fl->owner = current->pid; |
| 345 | break; |
| 346 | case IPV6_FL_S_USER: |
| 347 | fl->owner = current->euid; |
| 348 | break; |
| 349 | default: |
| 350 | err = -EINVAL; |
| 351 | goto done; |
| 352 | } |
| 353 | return fl; |
| 354 | |
| 355 | done: |
| 356 | fl_free(fl); |
| 357 | *err_p = err; |
| 358 | return NULL; |
| 359 | } |
| 360 | |
| 361 | static int mem_check(struct sock *sk) |
| 362 | { |
| 363 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 364 | struct ipv6_fl_socklist *sfl; |
| 365 | int room = FL_MAX_SIZE - atomic_read(&fl_size); |
| 366 | int count = 0; |
| 367 | |
| 368 | if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK) |
| 369 | return 0; |
| 370 | |
| 371 | for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) |
| 372 | count++; |
| 373 | |
| 374 | if (room <= 0 || |
| 375 | ((count >= FL_MAX_PER_SOCK || |
| 376 | (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4) |
| 377 | && !capable(CAP_NET_ADMIN))) |
| 378 | return -ENOBUFS; |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static int ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2) |
| 384 | { |
| 385 | if (h1 == h2) |
| 386 | return 0; |
| 387 | if (h1 == NULL || h2 == NULL) |
| 388 | return 1; |
| 389 | if (h1->hdrlen != h2->hdrlen) |
| 390 | return 1; |
| 391 | return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1)); |
| 392 | } |
| 393 | |
| 394 | static int ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2) |
| 395 | { |
| 396 | if (o1 == o2) |
| 397 | return 0; |
| 398 | if (o1 == NULL || o2 == NULL) |
| 399 | return 1; |
| 400 | if (o1->opt_nflen != o2->opt_nflen) |
| 401 | return 1; |
| 402 | if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt)) |
| 403 | return 1; |
| 404 | if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt)) |
| 405 | return 1; |
| 406 | if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt)) |
| 407 | return 1; |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen) |
| 412 | { |
| 413 | int err; |
| 414 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 415 | struct in6_flowlabel_req freq; |
| 416 | struct ipv6_fl_socklist *sfl1=NULL; |
| 417 | struct ipv6_fl_socklist *sfl, **sflp; |
| 418 | struct ip6_flowlabel *fl; |
| 419 | |
| 420 | if (optlen < sizeof(freq)) |
| 421 | return -EINVAL; |
| 422 | |
| 423 | if (copy_from_user(&freq, optval, sizeof(freq))) |
| 424 | return -EFAULT; |
| 425 | |
| 426 | switch (freq.flr_action) { |
| 427 | case IPV6_FL_A_PUT: |
| 428 | write_lock_bh(&ip6_sk_fl_lock); |
| 429 | for (sflp = &np->ipv6_fl_list; (sfl=*sflp)!=NULL; sflp = &sfl->next) { |
| 430 | if (sfl->fl->label == freq.flr_label) { |
| 431 | if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK)) |
| 432 | np->flow_label &= ~IPV6_FLOWLABEL_MASK; |
| 433 | *sflp = sfl->next; |
| 434 | write_unlock_bh(&ip6_sk_fl_lock); |
| 435 | fl_release(sfl->fl); |
| 436 | kfree(sfl); |
| 437 | return 0; |
| 438 | } |
| 439 | } |
| 440 | write_unlock_bh(&ip6_sk_fl_lock); |
| 441 | return -ESRCH; |
| 442 | |
| 443 | case IPV6_FL_A_RENEW: |
| 444 | read_lock_bh(&ip6_sk_fl_lock); |
| 445 | for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) { |
| 446 | if (sfl->fl->label == freq.flr_label) { |
| 447 | err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires); |
| 448 | read_unlock_bh(&ip6_sk_fl_lock); |
| 449 | return err; |
| 450 | } |
| 451 | } |
| 452 | read_unlock_bh(&ip6_sk_fl_lock); |
| 453 | |
| 454 | if (freq.flr_share == IPV6_FL_S_NONE && capable(CAP_NET_ADMIN)) { |
| 455 | fl = fl_lookup(freq.flr_label); |
| 456 | if (fl) { |
| 457 | err = fl6_renew(fl, freq.flr_linger, freq.flr_expires); |
| 458 | fl_release(fl); |
| 459 | return err; |
| 460 | } |
| 461 | } |
| 462 | return -ESRCH; |
| 463 | |
| 464 | case IPV6_FL_A_GET: |
| 465 | if (freq.flr_label & ~IPV6_FLOWLABEL_MASK) |
| 466 | return -EINVAL; |
| 467 | |
| 468 | fl = fl_create(&freq, optval, optlen, &err); |
| 469 | if (fl == NULL) |
| 470 | return err; |
| 471 | sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL); |
| 472 | |
| 473 | if (freq.flr_label) { |
| 474 | struct ip6_flowlabel *fl1 = NULL; |
| 475 | |
| 476 | err = -EEXIST; |
| 477 | read_lock_bh(&ip6_sk_fl_lock); |
| 478 | for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) { |
| 479 | if (sfl->fl->label == freq.flr_label) { |
| 480 | if (freq.flr_flags&IPV6_FL_F_EXCL) { |
| 481 | read_unlock_bh(&ip6_sk_fl_lock); |
| 482 | goto done; |
| 483 | } |
| 484 | fl1 = sfl->fl; |
Yan Zheng | 4ea6a80 | 2005-10-24 19:55:23 +0800 | [diff] [blame] | 485 | atomic_inc(&fl1->users); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | break; |
| 487 | } |
| 488 | } |
| 489 | read_unlock_bh(&ip6_sk_fl_lock); |
| 490 | |
| 491 | if (fl1 == NULL) |
| 492 | fl1 = fl_lookup(freq.flr_label); |
| 493 | if (fl1) { |
| 494 | err = -EEXIST; |
| 495 | if (freq.flr_flags&IPV6_FL_F_EXCL) |
| 496 | goto release; |
| 497 | err = -EPERM; |
| 498 | if (fl1->share == IPV6_FL_S_EXCL || |
| 499 | fl1->share != fl->share || |
| 500 | fl1->owner != fl->owner) |
| 501 | goto release; |
| 502 | |
| 503 | err = -EINVAL; |
| 504 | if (!ipv6_addr_equal(&fl1->dst, &fl->dst) || |
| 505 | ipv6_opt_cmp(fl1->opt, fl->opt)) |
| 506 | goto release; |
| 507 | |
| 508 | err = -ENOMEM; |
| 509 | if (sfl1 == NULL) |
| 510 | goto release; |
| 511 | if (fl->linger > fl1->linger) |
| 512 | fl1->linger = fl->linger; |
| 513 | if ((long)(fl->expires - fl1->expires) > 0) |
| 514 | fl1->expires = fl->expires; |
| 515 | write_lock_bh(&ip6_sk_fl_lock); |
| 516 | sfl1->fl = fl1; |
| 517 | sfl1->next = np->ipv6_fl_list; |
| 518 | np->ipv6_fl_list = sfl1; |
| 519 | write_unlock_bh(&ip6_sk_fl_lock); |
| 520 | fl_free(fl); |
| 521 | return 0; |
| 522 | |
| 523 | release: |
| 524 | fl_release(fl1); |
| 525 | goto done; |
| 526 | } |
| 527 | } |
| 528 | err = -ENOENT; |
| 529 | if (!(freq.flr_flags&IPV6_FL_F_CREATE)) |
| 530 | goto done; |
| 531 | |
| 532 | err = -ENOMEM; |
| 533 | if (sfl1 == NULL || (err = mem_check(sk)) != 0) |
| 534 | goto done; |
| 535 | |
| 536 | err = fl_intern(fl, freq.flr_label); |
| 537 | if (err) |
| 538 | goto done; |
| 539 | |
David S. Miller | 6c94d36 | 2005-05-29 20:28:01 -0700 | [diff] [blame] | 540 | if (!freq.flr_label) { |
| 541 | if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label, |
| 542 | &fl->label, sizeof(fl->label))) { |
| 543 | /* Intentionally ignore fault. */ |
| 544 | } |
| 545 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
| 547 | sfl1->fl = fl; |
| 548 | sfl1->next = np->ipv6_fl_list; |
| 549 | np->ipv6_fl_list = sfl1; |
| 550 | return 0; |
| 551 | |
| 552 | default: |
| 553 | return -EINVAL; |
| 554 | } |
| 555 | |
| 556 | done: |
| 557 | fl_free(fl); |
| 558 | kfree(sfl1); |
| 559 | return err; |
| 560 | } |
| 561 | |
| 562 | #ifdef CONFIG_PROC_FS |
| 563 | |
| 564 | struct ip6fl_iter_state { |
| 565 | int bucket; |
| 566 | }; |
| 567 | |
| 568 | #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private) |
| 569 | |
| 570 | static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq) |
| 571 | { |
| 572 | struct ip6_flowlabel *fl = NULL; |
| 573 | struct ip6fl_iter_state *state = ip6fl_seq_private(seq); |
| 574 | |
| 575 | for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) { |
| 576 | if (fl_ht[state->bucket]) { |
| 577 | fl = fl_ht[state->bucket]; |
| 578 | break; |
| 579 | } |
| 580 | } |
| 581 | return fl; |
| 582 | } |
| 583 | |
| 584 | static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl) |
| 585 | { |
| 586 | struct ip6fl_iter_state *state = ip6fl_seq_private(seq); |
| 587 | |
| 588 | fl = fl->next; |
| 589 | while (!fl) { |
| 590 | if (++state->bucket <= FL_HASH_MASK) |
| 591 | fl = fl_ht[state->bucket]; |
James Morris | bcd6207 | 2006-10-30 15:08:42 -0800 | [diff] [blame] | 592 | else |
| 593 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } |
| 595 | return fl; |
| 596 | } |
| 597 | |
| 598 | static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos) |
| 599 | { |
| 600 | struct ip6_flowlabel *fl = ip6fl_get_first(seq); |
| 601 | if (fl) |
| 602 | while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL) |
| 603 | --pos; |
| 604 | return pos ? NULL : fl; |
| 605 | } |
| 606 | |
| 607 | static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos) |
| 608 | { |
| 609 | read_lock_bh(&ip6_fl_lock); |
| 610 | return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 611 | } |
| 612 | |
| 613 | static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 614 | { |
| 615 | struct ip6_flowlabel *fl; |
| 616 | |
| 617 | if (v == SEQ_START_TOKEN) |
| 618 | fl = ip6fl_get_first(seq); |
| 619 | else |
| 620 | fl = ip6fl_get_next(seq, v); |
| 621 | ++*pos; |
| 622 | return fl; |
| 623 | } |
| 624 | |
| 625 | static void ip6fl_seq_stop(struct seq_file *seq, void *v) |
| 626 | { |
| 627 | read_unlock_bh(&ip6_fl_lock); |
| 628 | } |
| 629 | |
James Morris | 1b7c2db | 2006-10-31 00:43:44 -0800 | [diff] [blame] | 630 | static int ip6fl_seq_show(struct seq_file *seq, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | { |
James Morris | 1b7c2db | 2006-10-31 00:43:44 -0800 | [diff] [blame] | 632 | if (v == SEQ_START_TOKEN) |
| 633 | seq_printf(seq, "%-5s %-1s %-6s %-6s %-6s %-8s %-32s %s\n", |
| 634 | "Label", "S", "Owner", "Users", "Linger", "Expires", "Dst", "Opt"); |
| 635 | else { |
| 636 | struct ip6_flowlabel *fl = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | seq_printf(seq, |
YOSHIFUJI Hideaki | 9343e79 | 2006-01-17 02:10:53 -0800 | [diff] [blame] | 638 | "%05X %-1d %-6d %-6d %-6ld %-8ld " NIP6_SEQFMT " %-4d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | (unsigned)ntohl(fl->label), |
| 640 | fl->share, |
| 641 | (unsigned)fl->owner, |
| 642 | atomic_read(&fl->users), |
| 643 | fl->linger/HZ, |
| 644 | (long)(fl->expires - jiffies)/HZ, |
| 645 | NIP6(fl->dst), |
| 646 | fl->opt ? fl->opt->opt_nflen : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | return 0; |
| 649 | } |
| 650 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame^] | 651 | static const struct seq_operations ip6fl_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | .start = ip6fl_seq_start, |
| 653 | .next = ip6fl_seq_next, |
| 654 | .stop = ip6fl_seq_stop, |
| 655 | .show = ip6fl_seq_show, |
| 656 | }; |
| 657 | |
| 658 | static int ip6fl_seq_open(struct inode *inode, struct file *file) |
| 659 | { |
| 660 | struct seq_file *seq; |
| 661 | int rc = -ENOMEM; |
Ingo Oeser | 0c600ed | 2006-03-20 23:01:32 -0800 | [diff] [blame] | 662 | struct ip6fl_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
| 664 | if (!s) |
| 665 | goto out; |
| 666 | |
| 667 | rc = seq_open(file, &ip6fl_seq_ops); |
| 668 | if (rc) |
| 669 | goto out_kfree; |
| 670 | |
| 671 | seq = file->private_data; |
| 672 | seq->private = s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | out: |
| 674 | return rc; |
| 675 | out_kfree: |
| 676 | kfree(s); |
| 677 | goto out; |
| 678 | } |
| 679 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 680 | static const struct file_operations ip6fl_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | .owner = THIS_MODULE, |
| 682 | .open = ip6fl_seq_open, |
| 683 | .read = seq_read, |
| 684 | .llseek = seq_lseek, |
| 685 | .release = seq_release_private, |
| 686 | }; |
| 687 | #endif |
| 688 | |
| 689 | |
| 690 | void ip6_flowlabel_init(void) |
| 691 | { |
| 692 | #ifdef CONFIG_PROC_FS |
| 693 | proc_net_fops_create("ip6_flowlabel", S_IRUGO, &ip6fl_seq_fops); |
| 694 | #endif |
| 695 | } |
| 696 | |
| 697 | void ip6_flowlabel_cleanup(void) |
| 698 | { |
| 699 | del_timer(&ip6_fl_gc_timer); |
| 700 | #ifdef CONFIG_PROC_FS |
| 701 | proc_net_remove("ip6_flowlabel"); |
| 702 | #endif |
| 703 | } |