ip_vs_proto_udp.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * ip_vs_proto_udp.c: UDP load balancing support for IPVS
  3. *
  4. * Version: $Id: ip_vs_proto_udp.c,v 1.3 2002/11/30 01:50:35 wensong Exp $
  5. *
  6. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  7. * Julian Anastasov <ja@ssi.bg>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Changes:
  15. *
  16. */
  17. #include <linux/in.h>
  18. #include <linux/ip.h>
  19. #include <linux/kernel.h>
  20. #include <linux/netfilter_ipv4.h>
  21. #include <linux/udp.h>
  22. #include <net/ip_vs.h>
  23. static struct ip_vs_conn *
  24. udp_conn_in_get(const struct sk_buff *skb, struct ip_vs_protocol *pp,
  25. const struct iphdr *iph, unsigned int proto_off, int inverse)
  26. {
  27. struct ip_vs_conn *cp;
  28. __be16 _ports[2], *pptr;
  29. pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
  30. if (pptr == NULL)
  31. return NULL;
  32. if (likely(!inverse)) {
  33. cp = ip_vs_conn_in_get(iph->protocol,
  34. iph->saddr, pptr[0],
  35. iph->daddr, pptr[1]);
  36. } else {
  37. cp = ip_vs_conn_in_get(iph->protocol,
  38. iph->daddr, pptr[1],
  39. iph->saddr, pptr[0]);
  40. }
  41. return cp;
  42. }
  43. static struct ip_vs_conn *
  44. udp_conn_out_get(const struct sk_buff *skb, struct ip_vs_protocol *pp,
  45. const struct iphdr *iph, unsigned int proto_off, int inverse)
  46. {
  47. struct ip_vs_conn *cp;
  48. __be16 _ports[2], *pptr;
  49. pptr = skb_header_pointer(skb, skb->nh.iph->ihl*4,
  50. sizeof(_ports), _ports);
  51. if (pptr == NULL)
  52. return NULL;
  53. if (likely(!inverse)) {
  54. cp = ip_vs_conn_out_get(iph->protocol,
  55. iph->saddr, pptr[0],
  56. iph->daddr, pptr[1]);
  57. } else {
  58. cp = ip_vs_conn_out_get(iph->protocol,
  59. iph->daddr, pptr[1],
  60. iph->saddr, pptr[0]);
  61. }
  62. return cp;
  63. }
  64. static int
  65. udp_conn_schedule(struct sk_buff *skb, struct ip_vs_protocol *pp,
  66. int *verdict, struct ip_vs_conn **cpp)
  67. {
  68. struct ip_vs_service *svc;
  69. struct udphdr _udph, *uh;
  70. uh = skb_header_pointer(skb, skb->nh.iph->ihl*4,
  71. sizeof(_udph), &_udph);
  72. if (uh == NULL) {
  73. *verdict = NF_DROP;
  74. return 0;
  75. }
  76. if ((svc = ip_vs_service_get(skb->nfmark, skb->nh.iph->protocol,
  77. skb->nh.iph->daddr, uh->dest))) {
  78. if (ip_vs_todrop()) {
  79. /*
  80. * It seems that we are very loaded.
  81. * We have to drop this packet :(
  82. */
  83. ip_vs_service_put(svc);
  84. *verdict = NF_DROP;
  85. return 0;
  86. }
  87. /*
  88. * Let the virtual server select a real server for the
  89. * incoming connection, and create a connection entry.
  90. */
  91. *cpp = ip_vs_schedule(svc, skb);
  92. if (!*cpp) {
  93. *verdict = ip_vs_leave(svc, skb, pp);
  94. return 0;
  95. }
  96. ip_vs_service_put(svc);
  97. }
  98. return 1;
  99. }
  100. static inline void
  101. udp_fast_csum_update(struct udphdr *uhdr, __be32 oldip, __be32 newip,
  102. __be16 oldport, __be16 newport)
  103. {
  104. uhdr->check =
  105. ip_vs_check_diff(~oldip, newip,
  106. ip_vs_check_diff(oldport ^ htonl(0xFFFF),
  107. newport, uhdr->check));
  108. if (!uhdr->check)
  109. uhdr->check = htonl(0xFFFF);
  110. }
  111. static int
  112. udp_snat_handler(struct sk_buff **pskb,
  113. struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
  114. {
  115. struct udphdr *udph;
  116. unsigned int udphoff = (*pskb)->nh.iph->ihl * 4;
  117. /* csum_check requires unshared skb */
  118. if (!ip_vs_make_skb_writable(pskb, udphoff+sizeof(*udph)))
  119. return 0;
  120. if (unlikely(cp->app != NULL)) {
  121. /* Some checks before mangling */
  122. if (pp->csum_check && !pp->csum_check(*pskb, pp))
  123. return 0;
  124. /*
  125. * Call application helper if needed
  126. */
  127. if (!ip_vs_app_pkt_out(cp, pskb))
  128. return 0;
  129. }
  130. udph = (void *)(*pskb)->nh.iph + udphoff;
  131. udph->source = cp->vport;
  132. /*
  133. * Adjust UDP checksums
  134. */
  135. if (!cp->app && (udph->check != 0)) {
  136. /* Only port and addr are changed, do fast csum update */
  137. udp_fast_csum_update(udph, cp->daddr, cp->vaddr,
  138. cp->dport, cp->vport);
  139. if ((*pskb)->ip_summed == CHECKSUM_COMPLETE)
  140. (*pskb)->ip_summed = CHECKSUM_NONE;
  141. } else {
  142. /* full checksum calculation */
  143. udph->check = 0;
  144. (*pskb)->csum = skb_checksum(*pskb, udphoff,
  145. (*pskb)->len - udphoff, 0);
  146. udph->check = csum_tcpudp_magic(cp->vaddr, cp->caddr,
  147. (*pskb)->len - udphoff,
  148. cp->protocol,
  149. (*pskb)->csum);
  150. if (udph->check == 0)
  151. udph->check = htonl(0xFFFF);
  152. IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
  153. pp->name, udph->check,
  154. (char*)&(udph->check) - (char*)udph);
  155. }
  156. return 1;
  157. }
  158. static int
  159. udp_dnat_handler(struct sk_buff **pskb,
  160. struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
  161. {
  162. struct udphdr *udph;
  163. unsigned int udphoff = (*pskb)->nh.iph->ihl * 4;
  164. /* csum_check requires unshared skb */
  165. if (!ip_vs_make_skb_writable(pskb, udphoff+sizeof(*udph)))
  166. return 0;
  167. if (unlikely(cp->app != NULL)) {
  168. /* Some checks before mangling */
  169. if (pp->csum_check && !pp->csum_check(*pskb, pp))
  170. return 0;
  171. /*
  172. * Attempt ip_vs_app call.
  173. * It will fix ip_vs_conn
  174. */
  175. if (!ip_vs_app_pkt_in(cp, pskb))
  176. return 0;
  177. }
  178. udph = (void *)(*pskb)->nh.iph + udphoff;
  179. udph->dest = cp->dport;
  180. /*
  181. * Adjust UDP checksums
  182. */
  183. if (!cp->app && (udph->check != 0)) {
  184. /* Only port and addr are changed, do fast csum update */
  185. udp_fast_csum_update(udph, cp->vaddr, cp->daddr,
  186. cp->vport, cp->dport);
  187. if ((*pskb)->ip_summed == CHECKSUM_COMPLETE)
  188. (*pskb)->ip_summed = CHECKSUM_NONE;
  189. } else {
  190. /* full checksum calculation */
  191. udph->check = 0;
  192. (*pskb)->csum = skb_checksum(*pskb, udphoff,
  193. (*pskb)->len - udphoff, 0);
  194. udph->check = csum_tcpudp_magic(cp->caddr, cp->daddr,
  195. (*pskb)->len - udphoff,
  196. cp->protocol,
  197. (*pskb)->csum);
  198. if (udph->check == 0)
  199. udph->check = 0xFFFF;
  200. (*pskb)->ip_summed = CHECKSUM_UNNECESSARY;
  201. }
  202. return 1;
  203. }
  204. static int
  205. udp_csum_check(struct sk_buff *skb, struct ip_vs_protocol *pp)
  206. {
  207. struct udphdr _udph, *uh;
  208. unsigned int udphoff = skb->nh.iph->ihl*4;
  209. uh = skb_header_pointer(skb, udphoff, sizeof(_udph), &_udph);
  210. if (uh == NULL)
  211. return 0;
  212. if (uh->check != 0) {
  213. switch (skb->ip_summed) {
  214. case CHECKSUM_NONE:
  215. skb->csum = skb_checksum(skb, udphoff,
  216. skb->len - udphoff, 0);
  217. case CHECKSUM_COMPLETE:
  218. if (csum_tcpudp_magic(skb->nh.iph->saddr,
  219. skb->nh.iph->daddr,
  220. skb->len - udphoff,
  221. skb->nh.iph->protocol,
  222. skb->csum)) {
  223. IP_VS_DBG_RL_PKT(0, pp, skb, 0,
  224. "Failed checksum for");
  225. return 0;
  226. }
  227. break;
  228. default:
  229. /* No need to checksum. */
  230. break;
  231. }
  232. }
  233. return 1;
  234. }
  235. /*
  236. * Note: the caller guarantees that only one of register_app,
  237. * unregister_app or app_conn_bind is called each time.
  238. */
  239. #define UDP_APP_TAB_BITS 4
  240. #define UDP_APP_TAB_SIZE (1 << UDP_APP_TAB_BITS)
  241. #define UDP_APP_TAB_MASK (UDP_APP_TAB_SIZE - 1)
  242. static struct list_head udp_apps[UDP_APP_TAB_SIZE];
  243. static DEFINE_SPINLOCK(udp_app_lock);
  244. static inline __u16 udp_app_hashkey(__u16 port)
  245. {
  246. return ((port >> UDP_APP_TAB_BITS) ^ port) & UDP_APP_TAB_MASK;
  247. }
  248. static int udp_register_app(struct ip_vs_app *inc)
  249. {
  250. struct ip_vs_app *i;
  251. __u16 hash, port = inc->port;
  252. int ret = 0;
  253. hash = udp_app_hashkey(port);
  254. spin_lock_bh(&udp_app_lock);
  255. list_for_each_entry(i, &udp_apps[hash], p_list) {
  256. if (i->port == port) {
  257. ret = -EEXIST;
  258. goto out;
  259. }
  260. }
  261. list_add(&inc->p_list, &udp_apps[hash]);
  262. atomic_inc(&ip_vs_protocol_udp.appcnt);
  263. out:
  264. spin_unlock_bh(&udp_app_lock);
  265. return ret;
  266. }
  267. static void
  268. udp_unregister_app(struct ip_vs_app *inc)
  269. {
  270. spin_lock_bh(&udp_app_lock);
  271. atomic_dec(&ip_vs_protocol_udp.appcnt);
  272. list_del(&inc->p_list);
  273. spin_unlock_bh(&udp_app_lock);
  274. }
  275. static int udp_app_conn_bind(struct ip_vs_conn *cp)
  276. {
  277. int hash;
  278. struct ip_vs_app *inc;
  279. int result = 0;
  280. /* Default binding: bind app only for NAT */
  281. if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
  282. return 0;
  283. /* Lookup application incarnations and bind the right one */
  284. hash = udp_app_hashkey(cp->vport);
  285. spin_lock(&udp_app_lock);
  286. list_for_each_entry(inc, &udp_apps[hash], p_list) {
  287. if (inc->port == cp->vport) {
  288. if (unlikely(!ip_vs_app_inc_get(inc)))
  289. break;
  290. spin_unlock(&udp_app_lock);
  291. IP_VS_DBG(9, "%s: Binding conn %u.%u.%u.%u:%u->"
  292. "%u.%u.%u.%u:%u to app %s on port %u\n",
  293. __FUNCTION__,
  294. NIPQUAD(cp->caddr), ntohs(cp->cport),
  295. NIPQUAD(cp->vaddr), ntohs(cp->vport),
  296. inc->name, ntohs(inc->port));
  297. cp->app = inc;
  298. if (inc->init_conn)
  299. result = inc->init_conn(inc, cp);
  300. goto out;
  301. }
  302. }
  303. spin_unlock(&udp_app_lock);
  304. out:
  305. return result;
  306. }
  307. static int udp_timeouts[IP_VS_UDP_S_LAST+1] = {
  308. [IP_VS_UDP_S_NORMAL] = 5*60*HZ,
  309. [IP_VS_UDP_S_LAST] = 2*HZ,
  310. };
  311. static char * udp_state_name_table[IP_VS_UDP_S_LAST+1] = {
  312. [IP_VS_UDP_S_NORMAL] = "UDP",
  313. [IP_VS_UDP_S_LAST] = "BUG!",
  314. };
  315. static int
  316. udp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to)
  317. {
  318. return ip_vs_set_state_timeout(pp->timeout_table, IP_VS_UDP_S_LAST,
  319. udp_state_name_table, sname, to);
  320. }
  321. static const char * udp_state_name(int state)
  322. {
  323. if (state >= IP_VS_UDP_S_LAST)
  324. return "ERR!";
  325. return udp_state_name_table[state] ? udp_state_name_table[state] : "?";
  326. }
  327. static int
  328. udp_state_transition(struct ip_vs_conn *cp, int direction,
  329. const struct sk_buff *skb,
  330. struct ip_vs_protocol *pp)
  331. {
  332. cp->timeout = pp->timeout_table[IP_VS_UDP_S_NORMAL];
  333. return 1;
  334. }
  335. static void udp_init(struct ip_vs_protocol *pp)
  336. {
  337. IP_VS_INIT_HASH_TABLE(udp_apps);
  338. pp->timeout_table = udp_timeouts;
  339. }
  340. static void udp_exit(struct ip_vs_protocol *pp)
  341. {
  342. }
  343. struct ip_vs_protocol ip_vs_protocol_udp = {
  344. .name = "UDP",
  345. .protocol = IPPROTO_UDP,
  346. .dont_defrag = 0,
  347. .init = udp_init,
  348. .exit = udp_exit,
  349. .conn_schedule = udp_conn_schedule,
  350. .conn_in_get = udp_conn_in_get,
  351. .conn_out_get = udp_conn_out_get,
  352. .snat_handler = udp_snat_handler,
  353. .dnat_handler = udp_dnat_handler,
  354. .csum_check = udp_csum_check,
  355. .state_transition = udp_state_transition,
  356. .state_name = udp_state_name,
  357. .register_app = udp_register_app,
  358. .unregister_app = udp_unregister_app,
  359. .app_conn_bind = udp_app_conn_bind,
  360. .debug_packet = ip_vs_tcpudp_debug_packet,
  361. .timeout_change = NULL,
  362. .set_state_timeout = udp_set_state_timeout,
  363. };