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