ip_vs_app.c 14 KB

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