ip_vs_app.c 13 KB

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