ip_vs_app.c 13 KB

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