ip_vs_app.c 13 KB

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