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