1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#define KMSG_COMPONENT "IPVS"
22#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
23
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>
29#include <linux/netfilter.h>
30#include <net/net_namespace.h>
31#include <net/protocol.h>
32#include <net/tcp.h>
33#include <asm/system.h>
34#include <linux/stat.h>
35#include <linux/proc_fs.h>
36#include <linux/seq_file.h>
37#include <linux/mutex.h>
38
39#include <net/ip_vs.h>
40
41EXPORT_SYMBOL(register_ip_vs_app);
42EXPORT_SYMBOL(unregister_ip_vs_app);
43EXPORT_SYMBOL(register_ip_vs_app_inc);
44
45
46static LIST_HEAD(ip_vs_app_list);
47static DEFINE_MUTEX(__ip_vs_app_mutex);
48
49
50
51
52
53static inline int ip_vs_app_get(struct ip_vs_app *app)
54{
55 return try_module_get(app->module);
56}
57
58
59static inline void ip_vs_app_put(struct ip_vs_app *app)
60{
61 module_put(app->module);
62}
63
64
65
66
67
68static int
69ip_vs_app_inc_new(struct ip_vs_app *app, __u16 proto, __u16 port)
70{
71 struct ip_vs_protocol *pp;
72 struct ip_vs_app *inc;
73 int ret;
74
75 if (!(pp = ip_vs_proto_get(proto)))
76 return -EPROTONOSUPPORT;
77
78 if (!pp->unregister_app)
79 return -EOPNOTSUPP;
80
81 inc = kmemdup(app, sizeof(*inc), GFP_KERNEL);
82 if (!inc)
83 return -ENOMEM;
84 INIT_LIST_HEAD(&inc->p_list);
85 INIT_LIST_HEAD(&inc->incs_list);
86 inc->app = app;
87 inc->port = htons(port);
88 atomic_set(&inc->usecnt, 0);
89
90 if (app->timeouts) {
91 inc->timeout_table =
92 ip_vs_create_timeout_table(app->timeouts,
93 app->timeouts_size);
94 if (!inc->timeout_table) {
95 ret = -ENOMEM;
96 goto out;
97 }
98 }
99
100 ret = pp->register_app(inc);
101 if (ret)
102 goto out;
103
104 list_add(&inc->a_list, &app->incs_list);
105 IP_VS_DBG(9, "%s application %s:%u registered\n",
106 pp->name, inc->name, inc->port);
107
108 return 0;
109
110 out:
111 kfree(inc->timeout_table);
112 kfree(inc);
113 return ret;
114}
115
116
117
118
119
120static void
121ip_vs_app_inc_release(struct ip_vs_app *inc)
122{
123 struct ip_vs_protocol *pp;
124
125 if (!(pp = ip_vs_proto_get(inc->protocol)))
126 return;
127
128 if (pp->unregister_app)
129 pp->unregister_app(inc);
130
131 IP_VS_DBG(9, "%s App %s:%u unregistered\n",
132 pp->name, inc->name, inc->port);
133
134 list_del(&inc->a_list);
135
136 kfree(inc->timeout_table);
137 kfree(inc);
138}
139
140
141
142
143
144
145int ip_vs_app_inc_get(struct ip_vs_app *inc)
146{
147 int result;
148
149 atomic_inc(&inc->usecnt);
150 if (unlikely((result = ip_vs_app_get(inc->app)) != 1))
151 atomic_dec(&inc->usecnt);
152 return result;
153}
154
155
156
157
158
159void ip_vs_app_inc_put(struct ip_vs_app *inc)
160{
161 ip_vs_app_put(inc->app);
162 atomic_dec(&inc->usecnt);
163}
164
165
166
167
168
169int
170register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port)
171{
172 int result;
173
174 mutex_lock(&__ip_vs_app_mutex);
175
176 result = ip_vs_app_inc_new(app, proto, port);
177
178 mutex_unlock(&__ip_vs_app_mutex);
179
180 return result;
181}
182
183
184
185
186
187int register_ip_vs_app(struct ip_vs_app *app)
188{
189
190 ip_vs_use_count_inc();
191
192 mutex_lock(&__ip_vs_app_mutex);
193
194 list_add(&app->a_list, &ip_vs_app_list);
195
196 mutex_unlock(&__ip_vs_app_mutex);
197
198 return 0;
199}
200
201
202
203
204
205
206void unregister_ip_vs_app(struct ip_vs_app *app)
207{
208 struct ip_vs_app *inc, *nxt;
209
210 mutex_lock(&__ip_vs_app_mutex);
211
212 list_for_each_entry_safe(inc, nxt, &app->incs_list, a_list) {
213 ip_vs_app_inc_release(inc);
214 }
215
216 list_del(&app->a_list);
217
218 mutex_unlock(&__ip_vs_app_mutex);
219
220
221 ip_vs_use_count_dec();
222}
223
224
225
226
227
228int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp)
229{
230 return pp->app_conn_bind(cp);
231}
232
233
234
235
236
237void ip_vs_unbind_app(struct ip_vs_conn *cp)
238{
239 struct ip_vs_app *inc = cp->app;
240
241 if (!inc)
242 return;
243
244 if (inc->unbind_conn)
245 inc->unbind_conn(inc, cp);
246 if (inc->done_conn)
247 inc->done_conn(inc, cp);
248 ip_vs_app_inc_put(inc);
249 cp->app = NULL;
250}
251
252
253
254
255
256static inline void vs_fix_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
257{
258 __u32 seq = ntohl(th->seq);
259
260
261
262
263
264
265 if (vseq->delta || vseq->previous_delta) {
266 if(after(seq, vseq->init_seq)) {
267 th->seq = htonl(seq + vseq->delta);
268 IP_VS_DBG(9, "%s(): added delta (%d) to seq\n",
269 __func__, vseq->delta);
270 } else {
271 th->seq = htonl(seq + vseq->previous_delta);
272 IP_VS_DBG(9, "%s(): added previous_delta (%d) to seq\n",
273 __func__, vseq->previous_delta);
274 }
275 }
276}
277
278
279
280
281
282static inline void
283vs_fix_ack_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
284{
285 __u32 ack_seq = ntohl(th->ack_seq);
286
287
288
289
290
291
292 if (vseq->delta || vseq->previous_delta) {
293
294
295 if(after(ack_seq, vseq->init_seq+vseq->delta)) {
296 th->ack_seq = htonl(ack_seq - vseq->delta);
297 IP_VS_DBG(9, "%s(): subtracted delta "
298 "(%d) from ack_seq\n", __func__, vseq->delta);
299
300 } else {
301 th->ack_seq = htonl(ack_seq - vseq->previous_delta);
302 IP_VS_DBG(9, "%s(): subtracted "
303 "previous_delta (%d) from ack_seq\n",
304 __func__, vseq->previous_delta);
305 }
306 }
307}
308
309
310
311
312
313
314static inline void vs_seq_update(struct ip_vs_conn *cp, struct ip_vs_seq *vseq,
315 unsigned flag, __u32 seq, int diff)
316{
317
318 spin_lock(&cp->lock);
319 if (!(cp->flags & flag) || after(seq, vseq->init_seq)) {
320 vseq->previous_delta = vseq->delta;
321 vseq->delta += diff;
322 vseq->init_seq = seq;
323 cp->flags |= flag;
324 }
325 spin_unlock(&cp->lock);
326}
327
328static inline int app_tcp_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb,
329 struct ip_vs_app *app)
330{
331 int diff;
332 const unsigned int tcp_offset = ip_hdrlen(skb);
333 struct tcphdr *th;
334 __u32 seq;
335
336 if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
337 return 0;
338
339 th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
340
341
342
343
344 seq = ntohl(th->seq);
345
346
347
348
349 if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
350 vs_fix_seq(&cp->out_seq, th);
351 if (cp->flags & IP_VS_CONN_F_IN_SEQ)
352 vs_fix_ack_seq(&cp->in_seq, th);
353
354
355
356
357 if (app->pkt_out == NULL)
358 return 1;
359
360 if (!app->pkt_out(app, cp, skb, &diff))
361 return 0;
362
363
364
365
366 if (diff != 0)
367 vs_seq_update(cp, &cp->out_seq,
368 IP_VS_CONN_F_OUT_SEQ, seq, diff);
369
370 return 1;
371}
372
373
374
375
376
377
378int ip_vs_app_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb)
379{
380 struct ip_vs_app *app;
381
382
383
384
385
386 if ((app = cp->app) == NULL)
387 return 1;
388
389
390 if (cp->protocol == IPPROTO_TCP)
391 return app_tcp_pkt_out(cp, skb, app);
392
393
394
395
396 if (app->pkt_out == NULL)
397 return 1;
398
399 return app->pkt_out(app, cp, skb, NULL);
400}
401
402
403static inline int app_tcp_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb,
404 struct ip_vs_app *app)
405{
406 int diff;
407 const unsigned int tcp_offset = ip_hdrlen(skb);
408 struct tcphdr *th;
409 __u32 seq;
410
411 if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
412 return 0;
413
414 th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
415
416
417
418
419 seq = ntohl(th->seq);
420
421
422
423
424 if (cp->flags & IP_VS_CONN_F_IN_SEQ)
425 vs_fix_seq(&cp->in_seq, th);
426 if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
427 vs_fix_ack_seq(&cp->out_seq, th);
428
429
430
431
432 if (app->pkt_in == NULL)
433 return 1;
434
435 if (!app->pkt_in(app, cp, skb, &diff))
436 return 0;
437
438
439
440
441 if (diff != 0)
442 vs_seq_update(cp, &cp->in_seq,
443 IP_VS_CONN_F_IN_SEQ, seq, diff);
444
445 return 1;
446}
447
448
449
450
451
452
453int ip_vs_app_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb)
454{
455 struct ip_vs_app *app;
456
457
458
459
460
461 if ((app = cp->app) == NULL)
462 return 1;
463
464
465 if (cp->protocol == IPPROTO_TCP)
466 return app_tcp_pkt_in(cp, skb, app);
467
468
469
470
471 if (app->pkt_in == NULL)
472 return 1;
473
474 return app->pkt_in(app, cp, skb, NULL);
475}
476
477
478#ifdef CONFIG_PROC_FS
479
480
481
482
483static struct ip_vs_app *ip_vs_app_idx(loff_t pos)
484{
485 struct ip_vs_app *app, *inc;
486
487 list_for_each_entry(app, &ip_vs_app_list, a_list) {
488 list_for_each_entry(inc, &app->incs_list, a_list) {
489 if (pos-- == 0)
490 return inc;
491 }
492 }
493 return NULL;
494
495}
496
497static void *ip_vs_app_seq_start(struct seq_file *seq, loff_t *pos)
498{
499 mutex_lock(&__ip_vs_app_mutex);
500
501 return *pos ? ip_vs_app_idx(*pos - 1) : SEQ_START_TOKEN;
502}
503
504static void *ip_vs_app_seq_next(struct seq_file *seq, void *v, loff_t *pos)
505{
506 struct ip_vs_app *inc, *app;
507 struct list_head *e;
508
509 ++*pos;
510 if (v == SEQ_START_TOKEN)
511 return ip_vs_app_idx(0);
512
513 inc = v;
514 app = inc->app;
515
516 if ((e = inc->a_list.next) != &app->incs_list)
517 return list_entry(e, struct ip_vs_app, a_list);
518
519
520 for (e = app->a_list.next; e != &ip_vs_app_list; e = e->next) {
521 app = list_entry(e, struct ip_vs_app, a_list);
522 list_for_each_entry(inc, &app->incs_list, a_list) {
523 return inc;
524 }
525 }
526 return NULL;
527}
528
529static void ip_vs_app_seq_stop(struct seq_file *seq, void *v)
530{
531 mutex_unlock(&__ip_vs_app_mutex);
532}
533
534static int ip_vs_app_seq_show(struct seq_file *seq, void *v)
535{
536 if (v == SEQ_START_TOKEN)
537 seq_puts(seq, "prot port usecnt name\n");
538 else {
539 const struct ip_vs_app *inc = v;
540
541 seq_printf(seq, "%-3s %-7u %-6d %-17s\n",
542 ip_vs_proto_name(inc->protocol),
543 ntohs(inc->port),
544 atomic_read(&inc->usecnt),
545 inc->name);
546 }
547 return 0;
548}
549
550static const struct seq_operations ip_vs_app_seq_ops = {
551 .start = ip_vs_app_seq_start,
552 .next = ip_vs_app_seq_next,
553 .stop = ip_vs_app_seq_stop,
554 .show = ip_vs_app_seq_show,
555};
556
557static int ip_vs_app_open(struct inode *inode, struct file *file)
558{
559 return seq_open(file, &ip_vs_app_seq_ops);
560}
561
562static const struct file_operations ip_vs_app_fops = {
563 .owner = THIS_MODULE,
564 .open = ip_vs_app_open,
565 .read = seq_read,
566 .llseek = seq_lseek,
567 .release = seq_release,
568};
569#endif
570
571
572
573
574
575int ip_vs_skb_replace(struct sk_buff *skb, gfp_t pri,
576 char *o_buf, int o_len, char *n_buf, int n_len)
577{
578 int diff;
579 int o_offset;
580 int o_left;
581
582 EnterFunction(9);
583
584 diff = n_len - o_len;
585 o_offset = o_buf - (char *)skb->data;
586
587 o_left = skb->len - (o_offset + o_len);
588
589 if (diff <= 0) {
590 memmove(o_buf + n_len, o_buf + o_len, o_left);
591 memcpy(o_buf, n_buf, n_len);
592 skb_trim(skb, skb->len + diff);
593 } else if (diff <= skb_tailroom(skb)) {
594 skb_put(skb, diff);
595 memmove(o_buf + n_len, o_buf + o_len, o_left);
596 memcpy(o_buf, n_buf, n_len);
597 } else {
598 if (pskb_expand_head(skb, skb_headroom(skb), diff, pri))
599 return -ENOMEM;
600 skb_put(skb, diff);
601 memmove(skb->data + o_offset + n_len,
602 skb->data + o_offset + o_len, o_left);
603 skb_copy_to_linear_data_offset(skb, o_offset, n_buf, n_len);
604 }
605
606
607 ip_hdr(skb)->tot_len = htons(skb->len);
608
609 LeaveFunction(9);
610 return 0;
611}
612
613
614int __init ip_vs_app_init(void)
615{
616
617 proc_net_fops_create(&init_net, "ip_vs_app", 0, &ip_vs_app_fops);
618 return 0;
619}
620
621
622void ip_vs_app_cleanup(void)
623{
624 proc_net_remove(&init_net, "ip_vs_app");
625}
626