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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 24 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 26 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <net/sock.h> |
| 28 | |
| 29 | #include <net/ipv6.h> |
| 30 | #include <net/ndisc.h> |
| 31 | #include <net/protocol.h> |
| 32 | #include <net/ip6_route.h> |
| 33 | #include <net/addrconf.h> |
| 34 | #include <net/rawv6.h> |
| 35 | #include <net/icmp.h> |
| 36 | #include <net/transp_v6.h> |
| 37 | |
| 38 | #include <asm/uaccess.h> |
| 39 | |
| 40 | #define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified |
| 41 | in old IPv6 RFC. Well, it was reasonable value. |
| 42 | */ |
| 43 | #define FL_MAX_LINGER 60 /* Maximal linger timeout */ |
| 44 | |
| 45 | /* FL hash table */ |
| 46 | |
| 47 | #define FL_MAX_PER_SOCK 32 |
| 48 | #define FL_MAX_SIZE 4096 |
| 49 | #define FL_HASH_MASK 255 |
| 50 | #define FL_HASH(l) (ntohl(l)&FL_HASH_MASK) |
| 51 | |
| 52 | static atomic_t fl_size = ATOMIC_INIT(0); |
| 53 | static struct ip6_flowlabel *fl_ht[FL_HASH_MASK+1]; |
| 54 | |
| 55 | static void ip6_fl_gc(unsigned long dummy); |
Ingo Molnar | 8d06afa | 2005-09-09 13:10:40 -0700 | [diff] [blame] | 56 | static DEFINE_TIMER(ip6_fl_gc_timer, ip6_fl_gc, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | /* FL hash table lock: it protects only of GC */ |
| 59 | |
| 60 | static DEFINE_RWLOCK(ip6_fl_lock); |
| 61 | |
| 62 | /* Big socket sock */ |
| 63 | |
| 64 | static DEFINE_RWLOCK(ip6_sk_fl_lock); |
| 65 | |
| 66 | |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 67 | static inline struct ip6_flowlabel *__fl_lookup(struct net *net, __be32 label) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
| 69 | struct ip6_flowlabel *fl; |
| 70 | |
| 71 | for (fl=fl_ht[FL_HASH(label)]; fl; fl = fl->next) { |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 72 | if (fl->label == label && net_eq(fl->fl_net, net)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | return fl; |
| 74 | } |
| 75 | return NULL; |
| 76 | } |
| 77 | |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 78 | static struct ip6_flowlabel *fl_lookup(struct net *net, __be32 label) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
| 80 | struct ip6_flowlabel *fl; |
| 81 | |
| 82 | read_lock_bh(&ip6_fl_lock); |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 83 | fl = __fl_lookup(net, label); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | if (fl) |
| 85 | atomic_inc(&fl->users); |
| 86 | read_unlock_bh(&ip6_fl_lock); |
| 87 | return fl; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | static void fl_free(struct ip6_flowlabel *fl) |
| 92 | { |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 93 | if (fl) { |
| 94 | release_net(fl->fl_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | kfree(fl->opt); |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 96 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | kfree(fl); |
| 98 | } |
| 99 | |
| 100 | static void fl_release(struct ip6_flowlabel *fl) |
| 101 | { |
| 102 | write_lock_bh(&ip6_fl_lock); |
| 103 | |
| 104 | fl->lastuse = jiffies; |
| 105 | if (atomic_dec_and_test(&fl->users)) { |
| 106 | unsigned long ttd = fl->lastuse + fl->linger; |
| 107 | if (time_after(ttd, fl->expires)) |
| 108 | fl->expires = ttd; |
| 109 | ttd = fl->expires; |
| 110 | if (fl->opt && fl->share == IPV6_FL_S_EXCL) { |
| 111 | struct ipv6_txoptions *opt = fl->opt; |
| 112 | fl->opt = NULL; |
| 113 | kfree(opt); |
| 114 | } |
| 115 | if (!timer_pending(&ip6_fl_gc_timer) || |
| 116 | time_after(ip6_fl_gc_timer.expires, ttd)) |
| 117 | mod_timer(&ip6_fl_gc_timer, ttd); |
| 118 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | write_unlock_bh(&ip6_fl_lock); |
| 120 | } |
| 121 | |
| 122 | static void ip6_fl_gc(unsigned long dummy) |
| 123 | { |
| 124 | int i; |
| 125 | unsigned long now = jiffies; |
| 126 | unsigned long sched = 0; |
| 127 | |
| 128 | write_lock(&ip6_fl_lock); |
| 129 | |
| 130 | for (i=0; i<=FL_HASH_MASK; i++) { |
| 131 | struct ip6_flowlabel *fl, **flp; |
| 132 | flp = &fl_ht[i]; |
| 133 | while ((fl=*flp) != NULL) { |
| 134 | if (atomic_read(&fl->users) == 0) { |
| 135 | unsigned long ttd = fl->lastuse + fl->linger; |
| 136 | if (time_after(ttd, fl->expires)) |
| 137 | fl->expires = ttd; |
| 138 | ttd = fl->expires; |
| 139 | if (time_after_eq(now, ttd)) { |
| 140 | *flp = fl->next; |
| 141 | fl_free(fl); |
| 142 | atomic_dec(&fl_size); |
| 143 | continue; |
| 144 | } |
| 145 | if (!sched || time_before(ttd, sched)) |
| 146 | sched = ttd; |
| 147 | } |
| 148 | flp = &fl->next; |
| 149 | } |
| 150 | } |
| 151 | if (!sched && atomic_read(&fl_size)) |
| 152 | sched = now + FL_MAX_LINGER; |
| 153 | if (sched) { |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 154 | mod_timer(&ip6_fl_gc_timer, sched); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | write_unlock(&ip6_fl_lock); |
| 157 | } |
| 158 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 159 | static void __net_exit ip6_fl_purge(struct net *net) |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 160 | { |
| 161 | int i; |
| 162 | |
| 163 | write_lock(&ip6_fl_lock); |
| 164 | for (i = 0; i <= FL_HASH_MASK; i++) { |
| 165 | struct ip6_flowlabel *fl, **flp; |
| 166 | flp = &fl_ht[i]; |
| 167 | while ((fl = *flp) != NULL) { |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 168 | if (net_eq(fl->fl_net, net) && |
| 169 | atomic_read(&fl->users) == 0) { |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 170 | *flp = fl->next; |
| 171 | fl_free(fl); |
| 172 | atomic_dec(&fl_size); |
| 173 | continue; |
| 174 | } |
| 175 | flp = &fl->next; |
| 176 | } |
| 177 | } |
| 178 | write_unlock(&ip6_fl_lock); |
| 179 | } |
| 180 | |
| 181 | static struct ip6_flowlabel *fl_intern(struct net *net, |
| 182 | struct ip6_flowlabel *fl, __be32 label) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
Pavel Emelyanov | 78c2e50 | 2007-10-18 05:18:56 -0700 | [diff] [blame] | 184 | struct ip6_flowlabel *lfl; |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | fl->label = label & IPV6_FLOWLABEL_MASK; |
| 187 | |
| 188 | write_lock_bh(&ip6_fl_lock); |
| 189 | if (label == 0) { |
| 190 | for (;;) { |
| 191 | fl->label = htonl(net_random())&IPV6_FLOWLABEL_MASK; |
| 192 | if (fl->label) { |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 193 | lfl = __fl_lookup(net, fl->label); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | if (lfl == NULL) |
| 195 | break; |
| 196 | } |
| 197 | } |
Pavel Emelyanov | 78c2e50 | 2007-10-18 05:18:56 -0700 | [diff] [blame] | 198 | } else { |
| 199 | /* |
| 200 | * we dropper the ip6_fl_lock, so this entry could reappear |
| 201 | * and we need to recheck with it. |
| 202 | * |
| 203 | * OTOH no need to search the active socket first, like it is |
| 204 | * done in ipv6_flowlabel_opt - sock is locked, so new entry |
| 205 | * with the same label can only appear on another sock |
| 206 | */ |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 207 | lfl = __fl_lookup(net, fl->label); |
Pavel Emelyanov | 78c2e50 | 2007-10-18 05:18:56 -0700 | [diff] [blame] | 208 | if (lfl != NULL) { |
| 209 | atomic_inc(&lfl->users); |
| 210 | write_unlock_bh(&ip6_fl_lock); |
| 211 | return lfl; |
| 212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | fl->lastuse = jiffies; |
| 216 | fl->next = fl_ht[FL_HASH(fl->label)]; |
| 217 | fl_ht[FL_HASH(fl->label)] = fl; |
| 218 | atomic_inc(&fl_size); |
| 219 | write_unlock_bh(&ip6_fl_lock); |
Pavel Emelyanov | 78c2e50 | 2007-10-18 05:18:56 -0700 | [diff] [blame] | 220 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | |
| 224 | |
| 225 | /* Socket flowlabel lists */ |
| 226 | |
Al Viro | 90bcaf7 | 2006-11-08 00:25:17 -0800 | [diff] [blame] | 227 | struct ip6_flowlabel * fl6_sock_lookup(struct sock *sk, __be32 label) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
| 229 | struct ipv6_fl_socklist *sfl; |
| 230 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 231 | |
| 232 | label &= IPV6_FLOWLABEL_MASK; |
| 233 | |
Pavel Emelyanov | bd0bf57 | 2007-10-18 05:15:57 -0700 | [diff] [blame] | 234 | read_lock_bh(&ip6_sk_fl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | for (sfl=np->ipv6_fl_list; sfl; sfl = sfl->next) { |
| 236 | struct ip6_flowlabel *fl = sfl->fl; |
| 237 | if (fl->label == label) { |
| 238 | fl->lastuse = jiffies; |
| 239 | atomic_inc(&fl->users); |
Pavel Emelyanov | 52f095e | 2007-10-18 05:38:48 -0700 | [diff] [blame] | 240 | read_unlock_bh(&ip6_sk_fl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | return fl; |
| 242 | } |
| 243 | } |
Pavel Emelyanov | bd0bf57 | 2007-10-18 05:15:57 -0700 | [diff] [blame] | 244 | read_unlock_bh(&ip6_sk_fl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | return NULL; |
| 246 | } |
| 247 | |
Arnaldo Carvalho de Melo | 3cf3dc6 | 2005-12-13 23:23:20 -0800 | [diff] [blame] | 248 | EXPORT_SYMBOL_GPL(fl6_sock_lookup); |
| 249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | void fl6_free_socklist(struct sock *sk) |
| 251 | { |
| 252 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 253 | struct ipv6_fl_socklist *sfl; |
| 254 | |
| 255 | while ((sfl = np->ipv6_fl_list) != NULL) { |
| 256 | np->ipv6_fl_list = sfl->next; |
| 257 | fl_release(sfl->fl); |
| 258 | kfree(sfl); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* Service routines */ |
| 263 | |
| 264 | |
| 265 | /* |
| 266 | It is the only difficult place. flowlabel enforces equal headers |
| 267 | before and including routing header, however user may supply options |
| 268 | following rthdr. |
| 269 | */ |
| 270 | |
| 271 | struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions * opt_space, |
| 272 | struct ip6_flowlabel * fl, |
| 273 | struct ipv6_txoptions * fopt) |
| 274 | { |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 275 | struct ipv6_txoptions * fl_opt = fl->opt; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 276 | |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 277 | if (fopt == NULL || fopt->opt_flen == 0) |
| 278 | return fl_opt; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | if (fl_opt != NULL) { |
| 281 | opt_space->hopopt = fl_opt->hopopt; |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 282 | opt_space->dst0opt = fl_opt->dst0opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | opt_space->srcrt = fl_opt->srcrt; |
| 284 | opt_space->opt_nflen = fl_opt->opt_nflen; |
| 285 | } else { |
| 286 | if (fopt->opt_nflen == 0) |
| 287 | return fopt; |
| 288 | opt_space->hopopt = NULL; |
| 289 | opt_space->dst0opt = NULL; |
| 290 | opt_space->srcrt = NULL; |
| 291 | opt_space->opt_nflen = 0; |
| 292 | } |
| 293 | opt_space->dst1opt = fopt->dst1opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | opt_space->opt_flen = fopt->opt_flen; |
| 295 | return opt_space; |
| 296 | } |
| 297 | |
| 298 | static unsigned long check_linger(unsigned long ttl) |
| 299 | { |
| 300 | if (ttl < FL_MIN_LINGER) |
| 301 | return FL_MIN_LINGER*HZ; |
| 302 | if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN)) |
| 303 | return 0; |
| 304 | return ttl*HZ; |
| 305 | } |
| 306 | |
| 307 | static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires) |
| 308 | { |
| 309 | linger = check_linger(linger); |
| 310 | if (!linger) |
| 311 | return -EPERM; |
| 312 | expires = check_linger(expires); |
| 313 | if (!expires) |
| 314 | return -EPERM; |
| 315 | fl->lastuse = jiffies; |
| 316 | if (time_before(fl->linger, linger)) |
| 317 | fl->linger = linger; |
| 318 | if (time_before(expires, fl->linger)) |
| 319 | expires = fl->linger; |
| 320 | if (time_before(fl->expires, fl->lastuse + expires)) |
| 321 | fl->expires = fl->lastuse + expires; |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static struct ip6_flowlabel * |
Maciej Żenczykowski | ec0506d | 2011-08-28 12:35:31 +0000 | [diff] [blame] | 326 | fl_create(struct net *net, struct sock *sk, struct in6_flowlabel_req *freq, |
| 327 | char __user *optval, int optlen, int *err_p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { |
David S. Miller | 684de40 | 2009-02-06 00:49:55 -0800 | [diff] [blame] | 329 | struct ip6_flowlabel *fl = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | int olen; |
| 331 | int addr_type; |
| 332 | int err; |
| 333 | |
David S. Miller | 684de40 | 2009-02-06 00:49:55 -0800 | [diff] [blame] | 334 | olen = optlen - CMSG_ALIGN(sizeof(*freq)); |
| 335 | err = -EINVAL; |
| 336 | if (olen > 64 * 1024) |
| 337 | goto done; |
| 338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | err = -ENOMEM; |
Ingo Oeser | 0c600ed | 2006-03-20 23:01:32 -0800 | [diff] [blame] | 340 | fl = kzalloc(sizeof(*fl), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | if (fl == NULL) |
| 342 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | if (olen > 0) { |
| 345 | struct msghdr msg; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 346 | struct flowi6 flowi6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | int junk; |
| 348 | |
| 349 | err = -ENOMEM; |
| 350 | fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL); |
| 351 | if (fl->opt == NULL) |
| 352 | goto done; |
| 353 | |
| 354 | memset(fl->opt, 0, sizeof(*fl->opt)); |
| 355 | fl->opt->tot_len = sizeof(*fl->opt) + olen; |
| 356 | err = -EFAULT; |
| 357 | if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen)) |
| 358 | goto done; |
| 359 | |
| 360 | msg.msg_controllen = olen; |
| 361 | msg.msg_control = (void*)(fl->opt+1); |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 362 | memset(&flowi6, 0, sizeof(flowi6)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Maciej Żenczykowski | ec0506d | 2011-08-28 12:35:31 +0000 | [diff] [blame] | 364 | err = datagram_send_ctl(net, sk, &msg, &flowi6, fl->opt, &junk, |
Brian Haley | 13b52cd | 2010-04-23 11:26:08 +0000 | [diff] [blame] | 365 | &junk, &junk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (err) |
| 367 | goto done; |
| 368 | err = -EINVAL; |
| 369 | if (fl->opt->opt_flen) |
| 370 | goto done; |
| 371 | if (fl->opt->opt_nflen == 0) { |
| 372 | kfree(fl->opt); |
| 373 | fl->opt = NULL; |
| 374 | } |
| 375 | } |
| 376 | |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 377 | fl->fl_net = hold_net(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | fl->expires = jiffies; |
| 379 | err = fl6_renew(fl, freq->flr_linger, freq->flr_expires); |
| 380 | if (err) |
| 381 | goto done; |
| 382 | fl->share = freq->flr_share; |
| 383 | addr_type = ipv6_addr_type(&freq->flr_dst); |
Joe Perches | 3570021 | 2009-11-24 14:52:52 -0800 | [diff] [blame] | 384 | if ((addr_type & IPV6_ADDR_MAPPED) || |
| 385 | addr_type == IPV6_ADDR_ANY) { |
James Morris | c6817e4 | 2006-10-30 18:56:06 -0800 | [diff] [blame] | 386 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | goto done; |
James Morris | c6817e4 | 2006-10-30 18:56:06 -0800 | [diff] [blame] | 388 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | ipv6_addr_copy(&fl->dst, &freq->flr_dst); |
| 390 | atomic_set(&fl->users, 1); |
| 391 | switch (fl->share) { |
| 392 | case IPV6_FL_S_EXCL: |
| 393 | case IPV6_FL_S_ANY: |
| 394 | break; |
| 395 | case IPV6_FL_S_PROCESS: |
| 396 | fl->owner = current->pid; |
| 397 | break; |
| 398 | case IPV6_FL_S_USER: |
David Howells | f82b3590 | 2008-11-14 10:39:07 +1100 | [diff] [blame] | 399 | fl->owner = current_euid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | break; |
| 401 | default: |
| 402 | err = -EINVAL; |
| 403 | goto done; |
| 404 | } |
| 405 | return fl; |
| 406 | |
| 407 | done: |
| 408 | fl_free(fl); |
| 409 | *err_p = err; |
| 410 | return NULL; |
| 411 | } |
| 412 | |
| 413 | static int mem_check(struct sock *sk) |
| 414 | { |
| 415 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 416 | struct ipv6_fl_socklist *sfl; |
| 417 | int room = FL_MAX_SIZE - atomic_read(&fl_size); |
| 418 | int count = 0; |
| 419 | |
| 420 | if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK) |
| 421 | return 0; |
| 422 | |
| 423 | for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) |
| 424 | count++; |
| 425 | |
| 426 | if (room <= 0 || |
| 427 | ((count >= FL_MAX_PER_SOCK || |
Joe Perches | 3570021 | 2009-11-24 14:52:52 -0800 | [diff] [blame] | 428 | (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4) && |
| 429 | !capable(CAP_NET_ADMIN))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | return -ENOBUFS; |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static int ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2) |
| 436 | { |
| 437 | if (h1 == h2) |
| 438 | return 0; |
| 439 | if (h1 == NULL || h2 == NULL) |
| 440 | return 1; |
| 441 | if (h1->hdrlen != h2->hdrlen) |
| 442 | return 1; |
| 443 | return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1)); |
| 444 | } |
| 445 | |
| 446 | static int ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2) |
| 447 | { |
| 448 | if (o1 == o2) |
| 449 | return 0; |
| 450 | if (o1 == NULL || o2 == NULL) |
| 451 | return 1; |
| 452 | if (o1->opt_nflen != o2->opt_nflen) |
| 453 | return 1; |
| 454 | if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt)) |
| 455 | return 1; |
| 456 | if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt)) |
| 457 | return 1; |
| 458 | if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt)) |
| 459 | return 1; |
| 460 | return 0; |
| 461 | } |
| 462 | |
Pavel Emelyanov | 0402804 | 2007-10-18 05:14:58 -0700 | [diff] [blame] | 463 | static inline void fl_link(struct ipv6_pinfo *np, struct ipv6_fl_socklist *sfl, |
| 464 | struct ip6_flowlabel *fl) |
| 465 | { |
| 466 | write_lock_bh(&ip6_sk_fl_lock); |
| 467 | sfl->fl = fl; |
| 468 | sfl->next = np->ipv6_fl_list; |
| 469 | np->ipv6_fl_list = sfl; |
| 470 | write_unlock_bh(&ip6_sk_fl_lock); |
| 471 | } |
| 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen) |
| 474 | { |
Ingo Molnar | 55205d4 | 2008-11-25 16:50:30 -0800 | [diff] [blame] | 475 | int uninitialized_var(err); |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 476 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 478 | struct in6_flowlabel_req freq; |
| 479 | struct ipv6_fl_socklist *sfl1=NULL; |
| 480 | struct ipv6_fl_socklist *sfl, **sflp; |
Pavel Emelyanov | 78c2e50 | 2007-10-18 05:18:56 -0700 | [diff] [blame] | 481 | struct ip6_flowlabel *fl, *fl1 = NULL; |
| 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
| 484 | if (optlen < sizeof(freq)) |
| 485 | return -EINVAL; |
| 486 | |
| 487 | if (copy_from_user(&freq, optval, sizeof(freq))) |
| 488 | return -EFAULT; |
| 489 | |
| 490 | switch (freq.flr_action) { |
| 491 | case IPV6_FL_A_PUT: |
| 492 | write_lock_bh(&ip6_sk_fl_lock); |
| 493 | for (sflp = &np->ipv6_fl_list; (sfl=*sflp)!=NULL; sflp = &sfl->next) { |
| 494 | if (sfl->fl->label == freq.flr_label) { |
| 495 | if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK)) |
| 496 | np->flow_label &= ~IPV6_FLOWLABEL_MASK; |
| 497 | *sflp = sfl->next; |
| 498 | write_unlock_bh(&ip6_sk_fl_lock); |
| 499 | fl_release(sfl->fl); |
| 500 | kfree(sfl); |
| 501 | return 0; |
| 502 | } |
| 503 | } |
| 504 | write_unlock_bh(&ip6_sk_fl_lock); |
| 505 | return -ESRCH; |
| 506 | |
| 507 | case IPV6_FL_A_RENEW: |
| 508 | read_lock_bh(&ip6_sk_fl_lock); |
| 509 | for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) { |
| 510 | if (sfl->fl->label == freq.flr_label) { |
| 511 | err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires); |
| 512 | read_unlock_bh(&ip6_sk_fl_lock); |
| 513 | return err; |
| 514 | } |
| 515 | } |
| 516 | read_unlock_bh(&ip6_sk_fl_lock); |
| 517 | |
| 518 | if (freq.flr_share == IPV6_FL_S_NONE && capable(CAP_NET_ADMIN)) { |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 519 | fl = fl_lookup(net, freq.flr_label); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | if (fl) { |
| 521 | err = fl6_renew(fl, freq.flr_linger, freq.flr_expires); |
| 522 | fl_release(fl); |
| 523 | return err; |
| 524 | } |
| 525 | } |
| 526 | return -ESRCH; |
| 527 | |
| 528 | case IPV6_FL_A_GET: |
| 529 | if (freq.flr_label & ~IPV6_FLOWLABEL_MASK) |
| 530 | return -EINVAL; |
| 531 | |
Maciej Żenczykowski | ec0506d | 2011-08-28 12:35:31 +0000 | [diff] [blame] | 532 | fl = fl_create(net, sk, &freq, optval, optlen, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | if (fl == NULL) |
| 534 | return err; |
| 535 | sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL); |
| 536 | |
| 537 | if (freq.flr_label) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | err = -EEXIST; |
| 539 | read_lock_bh(&ip6_sk_fl_lock); |
| 540 | for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) { |
| 541 | if (sfl->fl->label == freq.flr_label) { |
| 542 | if (freq.flr_flags&IPV6_FL_F_EXCL) { |
| 543 | read_unlock_bh(&ip6_sk_fl_lock); |
| 544 | goto done; |
| 545 | } |
| 546 | fl1 = sfl->fl; |
Yan Zheng | 4ea6a80 | 2005-10-24 19:55:23 +0800 | [diff] [blame] | 547 | atomic_inc(&fl1->users); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | break; |
| 549 | } |
| 550 | } |
| 551 | read_unlock_bh(&ip6_sk_fl_lock); |
| 552 | |
| 553 | if (fl1 == NULL) |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 554 | fl1 = fl_lookup(net, freq.flr_label); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | if (fl1) { |
Pavel Emelyanov | 78c2e50 | 2007-10-18 05:18:56 -0700 | [diff] [blame] | 556 | recheck: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | err = -EEXIST; |
| 558 | if (freq.flr_flags&IPV6_FL_F_EXCL) |
| 559 | goto release; |
| 560 | err = -EPERM; |
| 561 | if (fl1->share == IPV6_FL_S_EXCL || |
| 562 | fl1->share != fl->share || |
| 563 | fl1->owner != fl->owner) |
| 564 | goto release; |
| 565 | |
| 566 | err = -EINVAL; |
| 567 | if (!ipv6_addr_equal(&fl1->dst, &fl->dst) || |
| 568 | ipv6_opt_cmp(fl1->opt, fl->opt)) |
| 569 | goto release; |
| 570 | |
| 571 | err = -ENOMEM; |
| 572 | if (sfl1 == NULL) |
| 573 | goto release; |
| 574 | if (fl->linger > fl1->linger) |
| 575 | fl1->linger = fl->linger; |
| 576 | if ((long)(fl->expires - fl1->expires) > 0) |
| 577 | fl1->expires = fl->expires; |
Pavel Emelyanov | 0402804 | 2007-10-18 05:14:58 -0700 | [diff] [blame] | 578 | fl_link(np, sfl1, fl1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | fl_free(fl); |
| 580 | return 0; |
| 581 | |
| 582 | release: |
| 583 | fl_release(fl1); |
| 584 | goto done; |
| 585 | } |
| 586 | } |
| 587 | err = -ENOENT; |
| 588 | if (!(freq.flr_flags&IPV6_FL_F_CREATE)) |
| 589 | goto done; |
| 590 | |
| 591 | err = -ENOMEM; |
| 592 | if (sfl1 == NULL || (err = mem_check(sk)) != 0) |
| 593 | goto done; |
| 594 | |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 595 | fl1 = fl_intern(net, fl, freq.flr_label); |
Pavel Emelyanov | 78c2e50 | 2007-10-18 05:18:56 -0700 | [diff] [blame] | 596 | if (fl1 != NULL) |
| 597 | goto recheck; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
David S. Miller | 6c94d36 | 2005-05-29 20:28:01 -0700 | [diff] [blame] | 599 | if (!freq.flr_label) { |
| 600 | if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label, |
| 601 | &fl->label, sizeof(fl->label))) { |
| 602 | /* Intentionally ignore fault. */ |
| 603 | } |
| 604 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
Pavel Emelyanov | 0402804 | 2007-10-18 05:14:58 -0700 | [diff] [blame] | 606 | fl_link(np, sfl1, fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | return 0; |
| 608 | |
| 609 | default: |
| 610 | return -EINVAL; |
| 611 | } |
| 612 | |
| 613 | done: |
| 614 | fl_free(fl); |
| 615 | kfree(sfl1); |
| 616 | return err; |
| 617 | } |
| 618 | |
| 619 | #ifdef CONFIG_PROC_FS |
| 620 | |
| 621 | struct ip6fl_iter_state { |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 622 | struct seq_net_private p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | int bucket; |
| 624 | }; |
| 625 | |
| 626 | #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private) |
| 627 | |
| 628 | static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq) |
| 629 | { |
| 630 | struct ip6_flowlabel *fl = NULL; |
| 631 | struct ip6fl_iter_state *state = ip6fl_seq_private(seq); |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 632 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
| 634 | for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) { |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 635 | fl = fl_ht[state->bucket]; |
| 636 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 637 | while (fl && !net_eq(fl->fl_net, net)) |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 638 | fl = fl->next; |
| 639 | if (fl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
| 642 | return fl; |
| 643 | } |
| 644 | |
| 645 | static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl) |
| 646 | { |
| 647 | struct ip6fl_iter_state *state = ip6fl_seq_private(seq); |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 648 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
| 650 | fl = fl->next; |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 651 | try_again: |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 652 | while (fl && !net_eq(fl->fl_net, net)) |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 653 | fl = fl->next; |
| 654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | while (!fl) { |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 656 | if (++state->bucket <= FL_HASH_MASK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | fl = fl_ht[state->bucket]; |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 658 | goto try_again; |
| 659 | } else |
James Morris | bcd6207 | 2006-10-30 15:08:42 -0800 | [diff] [blame] | 660 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | } |
| 662 | return fl; |
| 663 | } |
| 664 | |
| 665 | static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos) |
| 666 | { |
| 667 | struct ip6_flowlabel *fl = ip6fl_get_first(seq); |
| 668 | if (fl) |
| 669 | while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL) |
| 670 | --pos; |
| 671 | return pos ? NULL : fl; |
| 672 | } |
| 673 | |
| 674 | static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 675 | __acquires(ip6_fl_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | { |
| 677 | read_lock_bh(&ip6_fl_lock); |
| 678 | return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 679 | } |
| 680 | |
| 681 | static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 682 | { |
| 683 | struct ip6_flowlabel *fl; |
| 684 | |
| 685 | if (v == SEQ_START_TOKEN) |
| 686 | fl = ip6fl_get_first(seq); |
| 687 | else |
| 688 | fl = ip6fl_get_next(seq, v); |
| 689 | ++*pos; |
| 690 | return fl; |
| 691 | } |
| 692 | |
| 693 | static void ip6fl_seq_stop(struct seq_file *seq, void *v) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 694 | __releases(ip6_fl_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | { |
| 696 | read_unlock_bh(&ip6_fl_lock); |
| 697 | } |
| 698 | |
James Morris | 1b7c2db | 2006-10-31 00:43:44 -0800 | [diff] [blame] | 699 | static int ip6fl_seq_show(struct seq_file *seq, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | { |
James Morris | 1b7c2db | 2006-10-31 00:43:44 -0800 | [diff] [blame] | 701 | if (v == SEQ_START_TOKEN) |
| 702 | seq_printf(seq, "%-5s %-1s %-6s %-6s %-6s %-8s %-32s %s\n", |
| 703 | "Label", "S", "Owner", "Users", "Linger", "Expires", "Dst", "Opt"); |
| 704 | else { |
| 705 | struct ip6_flowlabel *fl = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | seq_printf(seq, |
Harvey Harrison | 4b7a427 | 2008-10-29 12:50:24 -0700 | [diff] [blame] | 707 | "%05X %-1d %-6d %-6d %-6ld %-8ld %pi6 %-4d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | (unsigned)ntohl(fl->label), |
| 709 | fl->share, |
| 710 | (unsigned)fl->owner, |
| 711 | atomic_read(&fl->users), |
| 712 | fl->linger/HZ, |
| 713 | (long)(fl->expires - jiffies)/HZ, |
Harvey Harrison | b071195 | 2008-10-28 16:05:40 -0700 | [diff] [blame] | 714 | &fl->dst, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | fl->opt ? fl->opt->opt_nflen : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | return 0; |
| 718 | } |
| 719 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 720 | static const struct seq_operations ip6fl_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | .start = ip6fl_seq_start, |
| 722 | .next = ip6fl_seq_next, |
| 723 | .stop = ip6fl_seq_stop, |
| 724 | .show = ip6fl_seq_show, |
| 725 | }; |
| 726 | |
| 727 | static int ip6fl_seq_open(struct inode *inode, struct file *file) |
| 728 | { |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 729 | return seq_open_net(inode, file, &ip6fl_seq_ops, |
| 730 | sizeof(struct ip6fl_iter_state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 733 | static const struct file_operations ip6fl_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | .owner = THIS_MODULE, |
| 735 | .open = ip6fl_seq_open, |
| 736 | .read = seq_read, |
| 737 | .llseek = seq_lseek, |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 738 | .release = seq_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 741 | static int __net_init ip6_flowlabel_proc_init(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | { |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 743 | if (!proc_net_fops_create(net, "ip6_flowlabel", |
| 744 | S_IRUGO, &ip6fl_seq_fops)) |
Daniel Lezcano | 0a3e78a | 2007-12-11 02:23:18 -0800 | [diff] [blame] | 745 | return -ENOMEM; |
| 746 | return 0; |
| 747 | } |
| 748 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 749 | static void __net_exit ip6_flowlabel_proc_fini(struct net *net) |
Daniel Lezcano | 0a3e78a | 2007-12-11 02:23:18 -0800 | [diff] [blame] | 750 | { |
| 751 | proc_net_remove(net, "ip6_flowlabel"); |
| 752 | } |
| 753 | #else |
| 754 | static inline int ip6_flowlabel_proc_init(struct net *net) |
| 755 | { |
| 756 | return 0; |
| 757 | } |
| 758 | static inline void ip6_flowlabel_proc_fini(struct net *net) |
| 759 | { |
Daniel Lezcano | 0a3e78a | 2007-12-11 02:23:18 -0800 | [diff] [blame] | 760 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | #endif |
Daniel Lezcano | 0a3e78a | 2007-12-11 02:23:18 -0800 | [diff] [blame] | 762 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 763 | static void __net_exit ip6_flowlabel_net_exit(struct net *net) |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 764 | { |
| 765 | ip6_fl_purge(net); |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 766 | ip6_flowlabel_proc_fini(net); |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | static struct pernet_operations ip6_flowlabel_net_ops = { |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 770 | .init = ip6_flowlabel_proc_init, |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 771 | .exit = ip6_flowlabel_net_exit, |
| 772 | }; |
| 773 | |
Daniel Lezcano | 0a3e78a | 2007-12-11 02:23:18 -0800 | [diff] [blame] | 774 | int ip6_flowlabel_init(void) |
| 775 | { |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 776 | return register_pernet_subsys(&ip6_flowlabel_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | void ip6_flowlabel_cleanup(void) |
| 780 | { |
| 781 | del_timer(&ip6_fl_gc_timer); |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 782 | unregister_pernet_subsys(&ip6_flowlabel_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | } |