ip_vs_app.c 13 KB

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