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