ip_vs_app.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * ip_vs_app.c: Application module support for IPVS
  3. *
  4. * Version: $Id: ip_vs_app.c,v 1.17 2003/03/22 06:31:21 wensong Exp $
  5. *
  6. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * Most code here is taken from ip_masq_app.c in kernel 2.2. The difference
  14. * is that ip_vs_app module handles the reverse direction (incoming requests
  15. * and outgoing responses).
  16. *
  17. * IP_MASQ_APP application masquerading module
  18. *
  19. * Author: Juan Jose Ciarlante, <jjciarla@raiz.uncu.edu.ar>
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/in.h>
  26. #include <linux/ip.h>
  27. #include <linux/netfilter.h>
  28. #include <net/net_namespace.h>
  29. #include <net/protocol.h>
  30. #include <net/tcp.h>
  31. #include <asm/system.h>
  32. #include <linux/stat.h>
  33. #include <linux/proc_fs.h>
  34. #include <linux/seq_file.h>
  35. #include <linux/mutex.h>
  36. #include <net/ip_vs.h>
  37. EXPORT_SYMBOL(register_ip_vs_app);
  38. EXPORT_SYMBOL(unregister_ip_vs_app);
  39. EXPORT_SYMBOL(register_ip_vs_app_inc);
  40. /* ipvs application list head */
  41. static LIST_HEAD(ip_vs_app_list);
  42. static DEFINE_MUTEX(__ip_vs_app_mutex);
  43. /*
  44. * Get an ip_vs_app object
  45. */
  46. static inline int ip_vs_app_get(struct ip_vs_app *app)
  47. {
  48. /* test and get the module atomically */
  49. if (app->module)
  50. return try_module_get(app->module);
  51. else
  52. return 1;
  53. }
  54. static inline void ip_vs_app_put(struct ip_vs_app *app)
  55. {
  56. if (app->module)
  57. module_put(app->module);
  58. }
  59. /*
  60. * Allocate/initialize app incarnation and register it in proto apps.
  61. */
  62. static int
  63. ip_vs_app_inc_new(struct ip_vs_app *app, __u16 proto, __u16 port)
  64. {
  65. struct ip_vs_protocol *pp;
  66. struct ip_vs_app *inc;
  67. int ret;
  68. if (!(pp = ip_vs_proto_get(proto)))
  69. return -EPROTONOSUPPORT;
  70. if (!pp->unregister_app)
  71. return -EOPNOTSUPP;
  72. inc = kmemdup(app, sizeof(*inc), GFP_KERNEL);
  73. if (!inc)
  74. return -ENOMEM;
  75. INIT_LIST_HEAD(&inc->p_list);
  76. INIT_LIST_HEAD(&inc->incs_list);
  77. inc->app = app;
  78. inc->port = htons(port);
  79. atomic_set(&inc->usecnt, 0);
  80. if (app->timeouts) {
  81. inc->timeout_table =
  82. ip_vs_create_timeout_table(app->timeouts,
  83. app->timeouts_size);
  84. if (!inc->timeout_table) {
  85. ret = -ENOMEM;
  86. goto out;
  87. }
  88. }
  89. ret = pp->register_app(inc);
  90. if (ret)
  91. goto out;
  92. list_add(&inc->a_list, &app->incs_list);
  93. IP_VS_DBG(9, "%s application %s:%u registered\n",
  94. pp->name, inc->name, inc->port);
  95. return 0;
  96. out:
  97. kfree(inc->timeout_table);
  98. kfree(inc);
  99. return ret;
  100. }
  101. /*
  102. * Release app incarnation
  103. */
  104. static void
  105. ip_vs_app_inc_release(struct ip_vs_app *inc)
  106. {
  107. struct ip_vs_protocol *pp;
  108. if (!(pp = ip_vs_proto_get(inc->protocol)))
  109. return;
  110. if (pp->unregister_app)
  111. pp->unregister_app(inc);
  112. IP_VS_DBG(9, "%s App %s:%u unregistered\n",
  113. pp->name, inc->name, inc->port);
  114. list_del(&inc->a_list);
  115. kfree(inc->timeout_table);
  116. kfree(inc);
  117. }
  118. /*
  119. * Get reference to app inc (only called from softirq)
  120. *
  121. */
  122. int ip_vs_app_inc_get(struct ip_vs_app *inc)
  123. {
  124. int result;
  125. atomic_inc(&inc->usecnt);
  126. if (unlikely((result = ip_vs_app_get(inc->app)) != 1))
  127. atomic_dec(&inc->usecnt);
  128. return result;
  129. }
  130. /*
  131. * Put the app inc (only called from timer or net softirq)
  132. */
  133. void ip_vs_app_inc_put(struct ip_vs_app *inc)
  134. {
  135. ip_vs_app_put(inc->app);
  136. atomic_dec(&inc->usecnt);
  137. }
  138. /*
  139. * Register an application incarnation in protocol applications
  140. */
  141. int
  142. register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port)
  143. {
  144. int result;
  145. mutex_lock(&__ip_vs_app_mutex);
  146. result = ip_vs_app_inc_new(app, proto, port);
  147. mutex_unlock(&__ip_vs_app_mutex);
  148. return result;
  149. }
  150. /*
  151. * ip_vs_app registration routine
  152. */
  153. int register_ip_vs_app(struct ip_vs_app *app)
  154. {
  155. /* increase the module use count */
  156. ip_vs_use_count_inc();
  157. mutex_lock(&__ip_vs_app_mutex);
  158. list_add(&app->a_list, &ip_vs_app_list);
  159. mutex_unlock(&__ip_vs_app_mutex);
  160. return 0;
  161. }
  162. /*
  163. * ip_vs_app unregistration routine
  164. * We are sure there are no app incarnations attached to services
  165. */
  166. void unregister_ip_vs_app(struct ip_vs_app *app)
  167. {
  168. struct ip_vs_app *inc, *nxt;
  169. mutex_lock(&__ip_vs_app_mutex);
  170. list_for_each_entry_safe(inc, nxt, &app->incs_list, a_list) {
  171. ip_vs_app_inc_release(inc);
  172. }
  173. list_del(&app->a_list);
  174. mutex_unlock(&__ip_vs_app_mutex);
  175. /* decrease the module use count */
  176. ip_vs_use_count_dec();
  177. }
  178. /*
  179. * Bind ip_vs_conn to its ip_vs_app (called by cp constructor)
  180. */
  181. int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp)
  182. {
  183. return pp->app_conn_bind(cp);
  184. }
  185. /*
  186. * Unbind cp from application incarnation (called by cp destructor)
  187. */
  188. void ip_vs_unbind_app(struct ip_vs_conn *cp)
  189. {
  190. struct ip_vs_app *inc = cp->app;
  191. if (!inc)
  192. return;
  193. if (inc->unbind_conn)
  194. inc->unbind_conn(inc, cp);
  195. if (inc->done_conn)
  196. inc->done_conn(inc, cp);
  197. ip_vs_app_inc_put(inc);
  198. cp->app = NULL;
  199. }
  200. /*
  201. * Fixes th->seq based on ip_vs_seq info.
  202. */
  203. static inline void vs_fix_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
  204. {
  205. __u32 seq = ntohl(th->seq);
  206. /*
  207. * Adjust seq with delta-offset for all packets after
  208. * the most recent resized pkt seq and with previous_delta offset
  209. * for all packets before most recent resized pkt seq.
  210. */
  211. if (vseq->delta || vseq->previous_delta) {
  212. if(after(seq, vseq->init_seq)) {
  213. th->seq = htonl(seq + vseq->delta);
  214. IP_VS_DBG(9, "vs_fix_seq(): added delta (%d) to seq\n",
  215. vseq->delta);
  216. } else {
  217. th->seq = htonl(seq + vseq->previous_delta);
  218. IP_VS_DBG(9, "vs_fix_seq(): added previous_delta "
  219. "(%d) to seq\n", vseq->previous_delta);
  220. }
  221. }
  222. }
  223. /*
  224. * Fixes th->ack_seq based on ip_vs_seq info.
  225. */
  226. static inline void
  227. vs_fix_ack_seq(const struct ip_vs_seq *vseq, struct tcphdr *th)
  228. {
  229. __u32 ack_seq = ntohl(th->ack_seq);
  230. /*
  231. * Adjust ack_seq with delta-offset for
  232. * the packets AFTER most recent resized pkt has caused a shift
  233. * for packets before most recent resized pkt, use previous_delta
  234. */
  235. if (vseq->delta || vseq->previous_delta) {
  236. /* since ack_seq is the number of octet that is expected
  237. to receive next, so compare it with init_seq+delta */
  238. if(after(ack_seq, vseq->init_seq+vseq->delta)) {
  239. th->ack_seq = htonl(ack_seq - vseq->delta);
  240. IP_VS_DBG(9, "vs_fix_ack_seq(): subtracted delta "
  241. "(%d) from ack_seq\n", vseq->delta);
  242. } else {
  243. th->ack_seq = htonl(ack_seq - vseq->previous_delta);
  244. IP_VS_DBG(9, "vs_fix_ack_seq(): subtracted "
  245. "previous_delta (%d) from ack_seq\n",
  246. vseq->previous_delta);
  247. }
  248. }
  249. }
  250. /*
  251. * Updates ip_vs_seq if pkt has been resized
  252. * Assumes already checked proto==IPPROTO_TCP and diff!=0.
  253. */
  254. static inline void vs_seq_update(struct ip_vs_conn *cp, struct ip_vs_seq *vseq,
  255. unsigned flag, __u32 seq, int diff)
  256. {
  257. /* spinlock is to keep updating cp->flags atomic */
  258. spin_lock(&cp->lock);
  259. if (!(cp->flags & flag) || after(seq, vseq->init_seq)) {
  260. vseq->previous_delta = vseq->delta;
  261. vseq->delta += diff;
  262. vseq->init_seq = seq;
  263. cp->flags |= flag;
  264. }
  265. spin_unlock(&cp->lock);
  266. }
  267. static inline int app_tcp_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb,
  268. struct ip_vs_app *app)
  269. {
  270. int diff;
  271. const unsigned int tcp_offset = ip_hdrlen(skb);
  272. struct tcphdr *th;
  273. __u32 seq;
  274. if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
  275. return 0;
  276. th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
  277. /*
  278. * Remember seq number in case this pkt gets resized
  279. */
  280. seq = ntohl(th->seq);
  281. /*
  282. * Fix seq stuff if flagged as so.
  283. */
  284. if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
  285. vs_fix_seq(&cp->out_seq, th);
  286. if (cp->flags & IP_VS_CONN_F_IN_SEQ)
  287. vs_fix_ack_seq(&cp->in_seq, th);
  288. /*
  289. * Call private output hook function
  290. */
  291. if (app->pkt_out == NULL)
  292. return 1;
  293. if (!app->pkt_out(app, cp, skb, &diff))
  294. return 0;
  295. /*
  296. * Update ip_vs seq stuff if len has changed.
  297. */
  298. if (diff != 0)
  299. vs_seq_update(cp, &cp->out_seq,
  300. IP_VS_CONN_F_OUT_SEQ, seq, diff);
  301. return 1;
  302. }
  303. /*
  304. * Output pkt hook. Will call bound ip_vs_app specific function
  305. * called by ipvs packet handler, assumes previously checked cp!=NULL
  306. * returns false if it can't handle packet (oom)
  307. */
  308. int ip_vs_app_pkt_out(struct ip_vs_conn *cp, struct sk_buff *skb)
  309. {
  310. struct ip_vs_app *app;
  311. /*
  312. * check if application module is bound to
  313. * this ip_vs_conn.
  314. */
  315. if ((app = cp->app) == NULL)
  316. return 1;
  317. /* TCP is complicated */
  318. if (cp->protocol == IPPROTO_TCP)
  319. return app_tcp_pkt_out(cp, skb, app);
  320. /*
  321. * Call private output hook function
  322. */
  323. if (app->pkt_out == NULL)
  324. return 1;
  325. return app->pkt_out(app, cp, skb, NULL);
  326. }
  327. static inline int app_tcp_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb,
  328. struct ip_vs_app *app)
  329. {
  330. int diff;
  331. const unsigned int tcp_offset = ip_hdrlen(skb);
  332. struct tcphdr *th;
  333. __u32 seq;
  334. if (!skb_make_writable(skb, tcp_offset + sizeof(*th)))
  335. return 0;
  336. th = (struct tcphdr *)(skb_network_header(skb) + tcp_offset);
  337. /*
  338. * Remember seq number in case this pkt gets resized
  339. */
  340. seq = ntohl(th->seq);
  341. /*
  342. * Fix seq stuff if flagged as so.
  343. */
  344. if (cp->flags & IP_VS_CONN_F_IN_SEQ)
  345. vs_fix_seq(&cp->in_seq, th);
  346. if (cp->flags & IP_VS_CONN_F_OUT_SEQ)
  347. vs_fix_ack_seq(&cp->out_seq, th);
  348. /*
  349. * Call private input hook function
  350. */
  351. if (app->pkt_in == NULL)
  352. return 1;
  353. if (!app->pkt_in(app, cp, skb, &diff))
  354. return 0;
  355. /*
  356. * Update ip_vs seq stuff if len has changed.
  357. */
  358. if (diff != 0)
  359. vs_seq_update(cp, &cp->in_seq,
  360. IP_VS_CONN_F_IN_SEQ, seq, diff);
  361. return 1;
  362. }
  363. /*
  364. * Input pkt hook. Will call bound ip_vs_app specific function
  365. * called by ipvs packet handler, assumes previously checked cp!=NULL.
  366. * returns false if can't handle packet (oom).
  367. */
  368. int ip_vs_app_pkt_in(struct ip_vs_conn *cp, struct sk_buff *skb)
  369. {
  370. struct ip_vs_app *app;
  371. /*
  372. * check if application module is bound to
  373. * this ip_vs_conn.
  374. */
  375. if ((app = cp->app) == NULL)
  376. return 1;
  377. /* TCP is complicated */
  378. if (cp->protocol == IPPROTO_TCP)
  379. return app_tcp_pkt_in(cp, skb, app);
  380. /*
  381. * Call private input hook function
  382. */
  383. if (app->pkt_in == NULL)
  384. return 1;
  385. return app->pkt_in(app, cp, skb, NULL);
  386. }
  387. #ifdef CONFIG_PROC_FS
  388. /*
  389. * /proc/net/ip_vs_app entry function
  390. */
  391. static struct ip_vs_app *ip_vs_app_idx(loff_t pos)
  392. {
  393. struct ip_vs_app *app, *inc;
  394. list_for_each_entry(app, &ip_vs_app_list, a_list) {
  395. list_for_each_entry(inc, &app->incs_list, a_list) {
  396. if (pos-- == 0)
  397. return inc;
  398. }
  399. }
  400. return NULL;
  401. }
  402. static void *ip_vs_app_seq_start(struct seq_file *seq, loff_t *pos)
  403. {
  404. mutex_lock(&__ip_vs_app_mutex);
  405. return *pos ? ip_vs_app_idx(*pos - 1) : SEQ_START_TOKEN;
  406. }
  407. static void *ip_vs_app_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  408. {
  409. struct ip_vs_app *inc, *app;
  410. struct list_head *e;
  411. ++*pos;
  412. if (v == SEQ_START_TOKEN)
  413. return ip_vs_app_idx(0);
  414. inc = v;
  415. app = inc->app;
  416. if ((e = inc->a_list.next) != &app->incs_list)
  417. return list_entry(e, struct ip_vs_app, a_list);
  418. /* go on to next application */
  419. for (e = app->a_list.next; e != &ip_vs_app_list; e = e->next) {
  420. app = list_entry(e, struct ip_vs_app, a_list);
  421. list_for_each_entry(inc, &app->incs_list, a_list) {
  422. return inc;
  423. }
  424. }
  425. return NULL;
  426. }
  427. static void ip_vs_app_seq_stop(struct seq_file *seq, void *v)
  428. {
  429. mutex_unlock(&__ip_vs_app_mutex);
  430. }
  431. static int ip_vs_app_seq_show(struct seq_file *seq, void *v)
  432. {
  433. if (v == SEQ_START_TOKEN)
  434. seq_puts(seq, "prot port usecnt name\n");
  435. else {
  436. const struct ip_vs_app *inc = v;
  437. seq_printf(seq, "%-3s %-7u %-6d %-17s\n",
  438. ip_vs_proto_name(inc->protocol),
  439. ntohs(inc->port),
  440. atomic_read(&inc->usecnt),
  441. inc->name);
  442. }
  443. return 0;
  444. }
  445. static const struct seq_operations ip_vs_app_seq_ops = {
  446. .start = ip_vs_app_seq_start,
  447. .next = ip_vs_app_seq_next,
  448. .stop = ip_vs_app_seq_stop,
  449. .show = ip_vs_app_seq_show,
  450. };
  451. static int ip_vs_app_open(struct inode *inode, struct file *file)
  452. {
  453. return seq_open(file, &ip_vs_app_seq_ops);
  454. }
  455. static const struct file_operations ip_vs_app_fops = {
  456. .owner = THIS_MODULE,
  457. .open = ip_vs_app_open,
  458. .read = seq_read,
  459. .llseek = seq_lseek,
  460. .release = seq_release,
  461. };
  462. #endif
  463. /*
  464. * Replace a segment of data with a new segment
  465. */
  466. int ip_vs_skb_replace(struct sk_buff *skb, gfp_t pri,
  467. char *o_buf, int o_len, char *n_buf, int n_len)
  468. {
  469. int diff;
  470. int o_offset;
  471. int o_left;
  472. EnterFunction(9);
  473. diff = n_len - o_len;
  474. o_offset = o_buf - (char *)skb->data;
  475. /* The length of left data after o_buf+o_len in the skb data */
  476. o_left = skb->len - (o_offset + o_len);
  477. if (diff <= 0) {
  478. memmove(o_buf + n_len, o_buf + o_len, o_left);
  479. memcpy(o_buf, n_buf, n_len);
  480. skb_trim(skb, skb->len + diff);
  481. } else if (diff <= skb_tailroom(skb)) {
  482. skb_put(skb, diff);
  483. memmove(o_buf + n_len, o_buf + o_len, o_left);
  484. memcpy(o_buf, n_buf, n_len);
  485. } else {
  486. if (pskb_expand_head(skb, skb_headroom(skb), diff, pri))
  487. return -ENOMEM;
  488. skb_put(skb, diff);
  489. memmove(skb->data + o_offset + n_len,
  490. skb->data + o_offset + o_len, o_left);
  491. skb_copy_to_linear_data_offset(skb, o_offset, n_buf, n_len);
  492. }
  493. /* must update the iph total length here */
  494. ip_hdr(skb)->tot_len = htons(skb->len);
  495. LeaveFunction(9);
  496. return 0;
  497. }
  498. int ip_vs_app_init(void)
  499. {
  500. /* we will replace it with proc_net_ipvs_create() soon */
  501. proc_net_fops_create(&init_net, "ip_vs_app", 0, &ip_vs_app_fops);
  502. return 0;
  503. }
  504. void ip_vs_app_cleanup(void)
  505. {
  506. proc_net_remove(&init_net, "ip_vs_app");
  507. }