Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ip_vs_app.c: Application module support for IPVS |
| 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Authors: Wensong Zhang <wensong@linuxvirtualserver.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * Most code here is taken from ip_masq_app.c in kernel 2.2. The difference |
| 12 | * is that ip_vs_app module handles the reverse direction (incoming requests |
| 13 | * and outgoing responses). |
| 14 | * |
| 15 | * IP_MASQ_APP application masquerading module |
| 16 | * |
| 17 | * Author: Juan Jose Ciarlante, <jjciarla@raiz.uncu.edu.ar> |
| 18 | * |
| 19 | */ |
| 20 | |
Hannes Eder | 9aada7a | 2009-07-30 14:29:44 -0700 | [diff] [blame] | 21 | #define KMSG_COMPONENT "IPVS" |
| 22 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/skbuff.h> |
| 27 | #include <linux/in.h> |
| 28 | #include <linux/ip.h> |
Herbert Xu | af1e1cf | 2007-10-14 00:39:33 -0700 | [diff] [blame] | 29 | #include <linux/netfilter.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 31 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <net/protocol.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 33 | #include <net/tcp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/stat.h> |
| 35 | #include <linux/proc_fs.h> |
| 36 | #include <linux/seq_file.h> |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 37 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | #include <net/ip_vs.h> |
| 40 | |
| 41 | EXPORT_SYMBOL(register_ip_vs_app); |
| 42 | EXPORT_SYMBOL(unregister_ip_vs_app); |
| 43 | EXPORT_SYMBOL(register_ip_vs_app_inc); |
| 44 | |
Simon Horman | 736561a | 2011-03-21 15:18:01 +0000 | [diff] [blame] | 45 | static DEFINE_MUTEX(__ip_vs_app_mutex); |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* |
| 48 | * Get an ip_vs_app object |
| 49 | */ |
| 50 | static inline int ip_vs_app_get(struct ip_vs_app *app) |
| 51 | { |
Pavel Emelyanov | 85b6068 | 2007-11-19 22:52:41 -0800 | [diff] [blame] | 52 | return try_module_get(app->module); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
| 56 | static inline void ip_vs_app_put(struct ip_vs_app *app) |
| 57 | { |
Pavel Emelyanov | 85b6068 | 2007-11-19 22:52:41 -0800 | [diff] [blame] | 58 | module_put(app->module); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Julian Anastasov | 363c97d | 2013-03-21 11:58:07 +0200 | [diff] [blame] | 61 | static void ip_vs_app_inc_destroy(struct ip_vs_app *inc) |
| 62 | { |
| 63 | kfree(inc->timeout_table); |
| 64 | kfree(inc); |
| 65 | } |
| 66 | |
| 67 | static void ip_vs_app_inc_rcu_free(struct rcu_head *head) |
| 68 | { |
| 69 | struct ip_vs_app *inc = container_of(head, struct ip_vs_app, rcu_head); |
| 70 | |
| 71 | ip_vs_app_inc_destroy(inc); |
| 72 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * Allocate/initialize app incarnation and register it in proto apps. |
| 76 | */ |
| 77 | static int |
Eric W. Biederman | a080ce3 | 2015-09-21 13:02:30 -0500 | [diff] [blame^] | 78 | ip_vs_app_inc_new(struct netns_ipvs *ipvs, struct ip_vs_app *app, __u16 proto, |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 79 | __u16 port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 81 | struct ip_vs_protocol *pp; |
| 82 | struct ip_vs_app *inc; |
| 83 | int ret; |
| 84 | |
| 85 | if (!(pp = ip_vs_proto_get(proto))) |
| 86 | return -EPROTONOSUPPORT; |
| 87 | |
| 88 | if (!pp->unregister_app) |
| 89 | return -EOPNOTSUPP; |
| 90 | |
Arnaldo Carvalho de Melo | 8b2ed4b | 2006-11-21 01:17:18 -0200 | [diff] [blame] | 91 | inc = kmemdup(app, sizeof(*inc), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | if (!inc) |
| 93 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | INIT_LIST_HEAD(&inc->p_list); |
| 95 | INIT_LIST_HEAD(&inc->incs_list); |
| 96 | inc->app = app; |
| 97 | inc->port = htons(port); |
| 98 | atomic_set(&inc->usecnt, 0); |
| 99 | |
| 100 | if (app->timeouts) { |
| 101 | inc->timeout_table = |
| 102 | ip_vs_create_timeout_table(app->timeouts, |
| 103 | app->timeouts_size); |
| 104 | if (!inc->timeout_table) { |
| 105 | ret = -ENOMEM; |
| 106 | goto out; |
| 107 | } |
| 108 | } |
| 109 | |
Eric W. Biederman | 1964891 | 2015-09-21 13:02:29 -0500 | [diff] [blame] | 110 | ret = pp->register_app(ipvs, inc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | if (ret) |
| 112 | goto out; |
| 113 | |
| 114 | list_add(&inc->a_list, &app->incs_list); |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 115 | IP_VS_DBG(9, "%s App %s:%u registered\n", |
| 116 | pp->name, inc->name, ntohs(inc->port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | return 0; |
| 119 | |
| 120 | out: |
Julian Anastasov | 363c97d | 2013-03-21 11:58:07 +0200 | [diff] [blame] | 121 | ip_vs_app_inc_destroy(inc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /* |
| 127 | * Release app incarnation |
| 128 | */ |
| 129 | static void |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 130 | ip_vs_app_inc_release(struct net *net, struct ip_vs_app *inc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
Eric W. Biederman | 1964891 | 2015-09-21 13:02:29 -0500 | [diff] [blame] | 132 | struct netns_ipvs *ipvs = net_ipvs(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | struct ip_vs_protocol *pp; |
| 134 | |
| 135 | if (!(pp = ip_vs_proto_get(inc->protocol))) |
| 136 | return; |
| 137 | |
| 138 | if (pp->unregister_app) |
Eric W. Biederman | 1964891 | 2015-09-21 13:02:29 -0500 | [diff] [blame] | 139 | pp->unregister_app(ipvs, inc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | IP_VS_DBG(9, "%s App %s:%u unregistered\n", |
Julian Anastasov | 26c15cf | 2010-09-21 18:12:30 +0200 | [diff] [blame] | 142 | pp->name, inc->name, ntohs(inc->port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
| 144 | list_del(&inc->a_list); |
| 145 | |
Julian Anastasov | 363c97d | 2013-03-21 11:58:07 +0200 | [diff] [blame] | 146 | call_rcu(&inc->rcu_head, ip_vs_app_inc_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
| 150 | /* |
| 151 | * Get reference to app inc (only called from softirq) |
| 152 | * |
| 153 | */ |
| 154 | int ip_vs_app_inc_get(struct ip_vs_app *inc) |
| 155 | { |
| 156 | int result; |
| 157 | |
Julian Anastasov | 363c97d | 2013-03-21 11:58:07 +0200 | [diff] [blame] | 158 | result = ip_vs_app_get(inc->app); |
| 159 | if (result) |
| 160 | atomic_inc(&inc->usecnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return result; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | /* |
| 166 | * Put the app inc (only called from timer or net softirq) |
| 167 | */ |
| 168 | void ip_vs_app_inc_put(struct ip_vs_app *inc) |
| 169 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | atomic_dec(&inc->usecnt); |
Julian Anastasov | 363c97d | 2013-03-21 11:58:07 +0200 | [diff] [blame] | 171 | ip_vs_app_put(inc->app); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | |
| 175 | /* |
| 176 | * Register an application incarnation in protocol applications |
| 177 | */ |
| 178 | int |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 179 | register_ip_vs_app_inc(struct net *net, struct ip_vs_app *app, __u16 proto, |
| 180 | __u16 port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
Eric W. Biederman | a080ce3 | 2015-09-21 13:02:30 -0500 | [diff] [blame^] | 182 | struct netns_ipvs *ipvs = net_ipvs(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | int result; |
| 184 | |
Simon Horman | 736561a | 2011-03-21 15:18:01 +0000 | [diff] [blame] | 185 | mutex_lock(&__ip_vs_app_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Eric W. Biederman | a080ce3 | 2015-09-21 13:02:30 -0500 | [diff] [blame^] | 187 | result = ip_vs_app_inc_new(ipvs, app, proto, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Simon Horman | 736561a | 2011-03-21 15:18:01 +0000 | [diff] [blame] | 189 | mutex_unlock(&__ip_vs_app_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 191 | return result; |
| 192 | } |
| 193 | |
| 194 | |
Julian Anastasov | be97fdb | 2012-07-12 23:06:20 +0300 | [diff] [blame] | 195 | /* Register application for netns */ |
| 196 | struct ip_vs_app *register_ip_vs_app(struct net *net, struct ip_vs_app *app) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 198 | struct netns_ipvs *ipvs = net_ipvs(net); |
Julian Anastasov | be97fdb | 2012-07-12 23:06:20 +0300 | [diff] [blame] | 199 | struct ip_vs_app *a; |
| 200 | int err = 0; |
| 201 | |
| 202 | if (!ipvs) |
| 203 | return ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Simon Horman | 736561a | 2011-03-21 15:18:01 +0000 | [diff] [blame] | 205 | mutex_lock(&__ip_vs_app_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Julian Anastasov | be97fdb | 2012-07-12 23:06:20 +0300 | [diff] [blame] | 207 | list_for_each_entry(a, &ipvs->app_list, a_list) { |
| 208 | if (!strcmp(app->name, a->name)) { |
| 209 | err = -EEXIST; |
| 210 | goto out_unlock; |
| 211 | } |
| 212 | } |
| 213 | a = kmemdup(app, sizeof(*app), GFP_KERNEL); |
| 214 | if (!a) { |
| 215 | err = -ENOMEM; |
| 216 | goto out_unlock; |
| 217 | } |
| 218 | INIT_LIST_HEAD(&a->incs_list); |
| 219 | list_add(&a->a_list, &ipvs->app_list); |
| 220 | /* increase the module use count */ |
| 221 | ip_vs_use_count_inc(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Julian Anastasov | be97fdb | 2012-07-12 23:06:20 +0300 | [diff] [blame] | 223 | out_unlock: |
Simon Horman | 736561a | 2011-03-21 15:18:01 +0000 | [diff] [blame] | 224 | mutex_unlock(&__ip_vs_app_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Julian Anastasov | be97fdb | 2012-07-12 23:06:20 +0300 | [diff] [blame] | 226 | return err ? ERR_PTR(err) : a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | |
| 230 | /* |
| 231 | * ip_vs_app unregistration routine |
| 232 | * We are sure there are no app incarnations attached to services |
Julian Anastasov | 363c97d | 2013-03-21 11:58:07 +0200 | [diff] [blame] | 233 | * Caller should use synchronize_rcu() or rcu_barrier() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | */ |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 235 | void unregister_ip_vs_app(struct net *net, struct ip_vs_app *app) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
Julian Anastasov | be97fdb | 2012-07-12 23:06:20 +0300 | [diff] [blame] | 237 | struct netns_ipvs *ipvs = net_ipvs(net); |
| 238 | struct ip_vs_app *a, *anxt, *inc, *nxt; |
| 239 | |
| 240 | if (!ipvs) |
| 241 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Simon Horman | 736561a | 2011-03-21 15:18:01 +0000 | [diff] [blame] | 243 | mutex_lock(&__ip_vs_app_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Julian Anastasov | be97fdb | 2012-07-12 23:06:20 +0300 | [diff] [blame] | 245 | list_for_each_entry_safe(a, anxt, &ipvs->app_list, a_list) { |
| 246 | if (app && strcmp(app->name, a->name)) |
| 247 | continue; |
| 248 | list_for_each_entry_safe(inc, nxt, &a->incs_list, a_list) { |
| 249 | ip_vs_app_inc_release(net, inc); |
| 250 | } |
| 251 | |
| 252 | list_del(&a->a_list); |
| 253 | kfree(a); |
| 254 | |
| 255 | /* decrease the module use count */ |
| 256 | ip_vs_use_count_dec(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Simon Horman | 736561a | 2011-03-21 15:18:01 +0000 | [diff] [blame] | 259 | mutex_unlock(&__ip_vs_app_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | /* |
| 264 | * Bind ip_vs_conn to its ip_vs_app (called by cp constructor) |
| 265 | */ |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 266 | int ip_vs_bind_app(struct ip_vs_conn *cp, |
| 267 | struct ip_vs_protocol *pp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
| 269 | return pp->app_conn_bind(cp); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | /* |
| 274 | * Unbind cp from application incarnation (called by cp destructor) |
| 275 | */ |
| 276 | void ip_vs_unbind_app(struct ip_vs_conn *cp) |
| 277 | { |
| 278 | struct ip_vs_app *inc = cp->app; |
| 279 | |
| 280 | if (!inc) |
| 281 | return; |
| 282 | |
| 283 | if (inc->unbind_conn) |
| 284 | inc->unbind_conn(inc, cp); |
| 285 | if (inc->done_conn) |
| 286 | inc->done_conn(inc, cp); |
| 287 | ip_vs_app_inc_put(inc); |
| 288 | cp->app = NULL; |
| 289 | } |
| 290 | |
| 291 | |
| 292 | /* |
| 293 | * Fixes th->seq based on ip_vs_seq info. |
| 294 | */ |
| 295 | static inline void vs_fix_seq(const struct ip_vs_seq *vseq, struct tcphdr *th) |
| 296 | { |
| 297 | __u32 seq = ntohl(th->seq); |
| 298 | |
| 299 | /* |
| 300 | * Adjust seq with delta-offset for all packets after |
| 301 | * the most recent resized pkt seq and with previous_delta offset |
| 302 | * for all packets before most recent resized pkt seq. |
| 303 | */ |
| 304 | if (vseq->delta || vseq->previous_delta) { |
| 305 | if(after(seq, vseq->init_seq)) { |
| 306 | th->seq = htonl(seq + vseq->delta); |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 307 | IP_VS_DBG(9, "%s(): added delta (%d) to seq\n", |
| 308 | __func__, vseq->delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } else { |
| 310 | th->seq = htonl(seq + vseq->previous_delta); |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 311 | IP_VS_DBG(9, "%s(): added previous_delta (%d) to seq\n", |
| 312 | __func__, vseq->previous_delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | |
| 318 | /* |
| 319 | * Fixes th->ack_seq based on ip_vs_seq info. |
| 320 | */ |
| 321 | static inline void |
| 322 | vs_fix_ack_seq(const struct ip_vs_seq *vseq, struct tcphdr *th) |
| 323 | { |
| 324 | __u32 ack_seq = ntohl(th->ack_seq); |
| 325 | |
| 326 | /* |
| 327 | * Adjust ack_seq with delta-offset for |
| 328 | * the packets AFTER most recent resized pkt has caused a shift |
| 329 | * for packets before most recent resized pkt, use previous_delta |
| 330 | */ |
| 331 | if (vseq->delta || vseq->previous_delta) { |
| 332 | /* since ack_seq is the number of octet that is expected |
| 333 | to receive next, so compare it with init_seq+delta */ |
| 334 | if(after(ack_seq, vseq->init_seq+vseq->delta)) { |
| 335 | th->ack_seq = htonl(ack_seq - vseq->delta); |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 336 | IP_VS_DBG(9, "%s(): subtracted delta " |
| 337 | "(%d) from ack_seq\n", __func__, vseq->delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
| 339 | } else { |
| 340 | th->ack_seq = htonl(ack_seq - vseq->previous_delta); |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 341 | IP_VS_DBG(9, "%s(): subtracted " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | "previous_delta (%d) from ack_seq\n", |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 343 | __func__, vseq->previous_delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | |
| 349 | /* |
| 350 | * Updates ip_vs_seq if pkt has been resized |
| 351 | * Assumes already checked proto==IPPROTO_TCP and diff!=0. |
| 352 | */ |
| 353 | static inline void vs_seq_update(struct ip_vs_conn *cp, struct ip_vs_seq *vseq, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 354 | unsigned int flag, __u32 seq, int diff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { |
| 356 | /* spinlock is to keep updating cp->flags atomic */ |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 357 | spin_lock_bh(&cp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | if (!(cp->flags & flag) || after(seq, vseq->init_seq)) { |
| 359 | vseq->previous_delta = vseq->delta; |
| 360 | vseq->delta += diff; |
| 361 | vseq->init_seq = seq; |
| 362 | cp->flags |= flag; |
| 363 | } |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 364 | spin_unlock_bh(&cp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 367 | static inline int app_tcp_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | struct ip_vs_app *app) |
| 369 | { |
| 370 | int diff; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 371 | const unsigned int tcp_offset = ip_hdrlen(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | struct tcphdr *th; |
| 373 | __u32 seq; |
| 374 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 375 | if (!skb_make_writable(skb, tcp_offset + sizeof(*th))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | return 0; |
| 377 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 378 | th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
| 380 | /* |
| 381 | * Remember seq number in case this pkt gets resized |
| 382 | */ |
| 383 | seq = ntohl(th->seq); |
| 384 | |
| 385 | /* |
| 386 | * Fix seq stuff if flagged as so. |
| 387 | */ |
| 388 | if (cp->flags & IP_VS_CONN_F_OUT_SEQ) |
| 389 | vs_fix_seq(&cp->out_seq, th); |
| 390 | if (cp->flags & IP_VS_CONN_F_IN_SEQ) |
| 391 | vs_fix_ack_seq(&cp->in_seq, th); |
| 392 | |
| 393 | /* |
| 394 | * Call private output hook function |
| 395 | */ |
| 396 | if (app->pkt_out == NULL) |
| 397 | return 1; |
| 398 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 399 | if (!app->pkt_out(app, cp, skb, &diff)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | return 0; |
| 401 | |
| 402 | /* |
| 403 | * Update ip_vs seq stuff if len has changed. |
| 404 | */ |
| 405 | if (diff != 0) |
| 406 | vs_seq_update(cp, &cp->out_seq, |
| 407 | IP_VS_CONN_F_OUT_SEQ, seq, diff); |
| 408 | |
| 409 | return 1; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Output pkt hook. Will call bound ip_vs_app specific function |
| 414 | * called by ipvs packet handler, assumes previously checked cp!=NULL |
| 415 | * returns false if it can't handle packet (oom) |
| 416 | */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 417 | int ip_vs_app_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | { |
| 419 | struct ip_vs_app *app; |
| 420 | |
| 421 | /* |
| 422 | * check if application module is bound to |
| 423 | * this ip_vs_conn. |
| 424 | */ |
| 425 | if ((app = cp->app) == NULL) |
| 426 | return 1; |
| 427 | |
| 428 | /* TCP is complicated */ |
| 429 | if (cp->protocol == IPPROTO_TCP) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 430 | return app_tcp_pkt_out(cp, skb, app); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | /* |
| 433 | * Call private output hook function |
| 434 | */ |
| 435 | if (app->pkt_out == NULL) |
| 436 | return 1; |
| 437 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 438 | return app->pkt_out(app, cp, skb, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 442 | static inline int app_tcp_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | struct ip_vs_app *app) |
| 444 | { |
| 445 | int diff; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 446 | const unsigned int tcp_offset = ip_hdrlen(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | struct tcphdr *th; |
| 448 | __u32 seq; |
| 449 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 450 | if (!skb_make_writable(skb, tcp_offset + sizeof(*th))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | return 0; |
| 452 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 453 | th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
| 455 | /* |
| 456 | * Remember seq number in case this pkt gets resized |
| 457 | */ |
| 458 | seq = ntohl(th->seq); |
| 459 | |
| 460 | /* |
| 461 | * Fix seq stuff if flagged as so. |
| 462 | */ |
| 463 | if (cp->flags & IP_VS_CONN_F_IN_SEQ) |
| 464 | vs_fix_seq(&cp->in_seq, th); |
| 465 | if (cp->flags & IP_VS_CONN_F_OUT_SEQ) |
| 466 | vs_fix_ack_seq(&cp->out_seq, th); |
| 467 | |
| 468 | /* |
| 469 | * Call private input hook function |
| 470 | */ |
| 471 | if (app->pkt_in == NULL) |
| 472 | return 1; |
| 473 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 474 | if (!app->pkt_in(app, cp, skb, &diff)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | return 0; |
| 476 | |
| 477 | /* |
| 478 | * Update ip_vs seq stuff if len has changed. |
| 479 | */ |
| 480 | if (diff != 0) |
| 481 | vs_seq_update(cp, &cp->in_seq, |
| 482 | IP_VS_CONN_F_IN_SEQ, seq, diff); |
| 483 | |
| 484 | return 1; |
| 485 | } |
| 486 | |
| 487 | /* |
| 488 | * Input pkt hook. Will call bound ip_vs_app specific function |
| 489 | * called by ipvs packet handler, assumes previously checked cp!=NULL. |
| 490 | * returns false if can't handle packet (oom). |
| 491 | */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 492 | int ip_vs_app_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | { |
| 494 | struct ip_vs_app *app; |
| 495 | |
| 496 | /* |
| 497 | * check if application module is bound to |
| 498 | * this ip_vs_conn. |
| 499 | */ |
| 500 | if ((app = cp->app) == NULL) |
| 501 | return 1; |
| 502 | |
| 503 | /* TCP is complicated */ |
| 504 | if (cp->protocol == IPPROTO_TCP) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 505 | return app_tcp_pkt_in(cp, skb, app); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
| 507 | /* |
| 508 | * Call private input hook function |
| 509 | */ |
| 510 | if (app->pkt_in == NULL) |
| 511 | return 1; |
| 512 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 513 | return app->pkt_in(app, cp, skb, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | |
| 517 | #ifdef CONFIG_PROC_FS |
| 518 | /* |
| 519 | * /proc/net/ip_vs_app entry function |
| 520 | */ |
| 521 | |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 522 | static struct ip_vs_app *ip_vs_app_idx(struct netns_ipvs *ipvs, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
| 524 | struct ip_vs_app *app, *inc; |
| 525 | |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 526 | list_for_each_entry(app, &ipvs->app_list, a_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | list_for_each_entry(inc, &app->incs_list, a_list) { |
| 528 | if (pos-- == 0) |
| 529 | return inc; |
| 530 | } |
| 531 | } |
| 532 | return NULL; |
| 533 | |
| 534 | } |
| 535 | |
| 536 | static void *ip_vs_app_seq_start(struct seq_file *seq, loff_t *pos) |
| 537 | { |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 538 | struct net *net = seq_file_net(seq); |
| 539 | struct netns_ipvs *ipvs = net_ipvs(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
Simon Horman | 736561a | 2011-03-21 15:18:01 +0000 | [diff] [blame] | 541 | mutex_lock(&__ip_vs_app_mutex); |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 542 | |
| 543 | return *pos ? ip_vs_app_idx(ipvs, *pos - 1) : SEQ_START_TOKEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | static void *ip_vs_app_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 547 | { |
| 548 | struct ip_vs_app *inc, *app; |
| 549 | struct list_head *e; |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 550 | struct net *net = seq_file_net(seq); |
| 551 | struct netns_ipvs *ipvs = net_ipvs(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
| 553 | ++*pos; |
| 554 | if (v == SEQ_START_TOKEN) |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 555 | return ip_vs_app_idx(ipvs, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
| 557 | inc = v; |
| 558 | app = inc->app; |
| 559 | |
| 560 | if ((e = inc->a_list.next) != &app->incs_list) |
| 561 | return list_entry(e, struct ip_vs_app, a_list); |
| 562 | |
| 563 | /* go on to next application */ |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 564 | for (e = app->a_list.next; e != &ipvs->app_list; e = e->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | app = list_entry(e, struct ip_vs_app, a_list); |
| 566 | list_for_each_entry(inc, &app->incs_list, a_list) { |
| 567 | return inc; |
| 568 | } |
| 569 | } |
| 570 | return NULL; |
| 571 | } |
| 572 | |
| 573 | static void ip_vs_app_seq_stop(struct seq_file *seq, void *v) |
| 574 | { |
Simon Horman | 736561a | 2011-03-21 15:18:01 +0000 | [diff] [blame] | 575 | mutex_unlock(&__ip_vs_app_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static int ip_vs_app_seq_show(struct seq_file *seq, void *v) |
| 579 | { |
| 580 | if (v == SEQ_START_TOKEN) |
| 581 | seq_puts(seq, "prot port usecnt name\n"); |
| 582 | else { |
| 583 | const struct ip_vs_app *inc = v; |
| 584 | |
| 585 | seq_printf(seq, "%-3s %-7u %-6d %-17s\n", |
| 586 | ip_vs_proto_name(inc->protocol), |
| 587 | ntohs(inc->port), |
| 588 | atomic_read(&inc->usecnt), |
| 589 | inc->name); |
| 590 | } |
| 591 | return 0; |
| 592 | } |
| 593 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 594 | static const struct seq_operations ip_vs_app_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | .start = ip_vs_app_seq_start, |
| 596 | .next = ip_vs_app_seq_next, |
| 597 | .stop = ip_vs_app_seq_stop, |
| 598 | .show = ip_vs_app_seq_show, |
| 599 | }; |
| 600 | |
| 601 | static int ip_vs_app_open(struct inode *inode, struct file *file) |
| 602 | { |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 603 | return seq_open_net(inode, file, &ip_vs_app_seq_ops, |
| 604 | sizeof(struct seq_net_private)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 607 | static const struct file_operations ip_vs_app_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | .owner = THIS_MODULE, |
| 609 | .open = ip_vs_app_open, |
| 610 | .read = seq_read, |
| 611 | .llseek = seq_lseek, |
Hans Schillstrom | 0f08190 | 2011-05-15 17:20:29 +0200 | [diff] [blame] | 612 | .release = seq_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | }; |
| 614 | #endif |
| 615 | |
Hans Schillstrom | 503cf15 | 2011-05-01 18:50:16 +0200 | [diff] [blame] | 616 | int __net_init ip_vs_app_net_init(struct net *net) |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 617 | { |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 618 | struct netns_ipvs *ipvs = net_ipvs(net); |
| 619 | |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 620 | INIT_LIST_HEAD(&ipvs->app_list); |
Gao feng | d4beaa6 | 2013-02-18 01:34:54 +0000 | [diff] [blame] | 621 | proc_create("ip_vs_app", 0, net->proc_net, &ip_vs_app_fops); |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 622 | return 0; |
| 623 | } |
| 624 | |
Hans Schillstrom | 503cf15 | 2011-05-01 18:50:16 +0200 | [diff] [blame] | 625 | void __net_exit ip_vs_app_net_cleanup(struct net *net) |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 626 | { |
Julian Anastasov | be97fdb | 2012-07-12 23:06:20 +0300 | [diff] [blame] | 627 | unregister_ip_vs_app(net, NULL /* all */); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 628 | remove_proc_entry("ip_vs_app", net->proc_net); |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 629 | } |