ip_vs_proto_udp.c 10.0 KB

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