ip_vs_proto_udp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * ip_vs_proto_udp.c: UDP load balancing support for IPVS
  3. *
  4. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  5. * Julian Anastasov <ja@ssi.bg>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Changes:
  13. *
  14. */
  15. #include <linux/in.h>
  16. #include <linux/ip.h>
  17. #include <linux/kernel.h>
  18. #include <linux/netfilter.h>
  19. #include <linux/netfilter_ipv4.h>
  20. #include <linux/udp.h>
  21. #include <net/ip_vs.h>
  22. #include <net/ip.h>
  23. static struct ip_vs_conn *
  24. udp_conn_in_get(int af, const struct sk_buff *skb, struct ip_vs_protocol *pp,
  25. const struct ip_vs_iphdr *iph, unsigned int proto_off,
  26. 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(af, iph->protocol,
  35. &iph->saddr, pptr[0],
  36. &iph->daddr, pptr[1]);
  37. } else {
  38. cp = ip_vs_conn_in_get(af, 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(int af, const struct sk_buff *skb, struct ip_vs_protocol *pp,
  46. const struct ip_vs_iphdr *iph, unsigned int proto_off,
  47. int inverse)
  48. {
  49. struct ip_vs_conn *cp;
  50. __be16 _ports[2], *pptr;
  51. pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
  52. if (pptr == NULL)
  53. return NULL;
  54. if (likely(!inverse)) {
  55. cp = ip_vs_conn_out_get(af, iph->protocol,
  56. &iph->saddr, pptr[0],
  57. &iph->daddr, pptr[1]);
  58. } else {
  59. cp = ip_vs_conn_out_get(af, iph->protocol,
  60. &iph->daddr, pptr[1],
  61. &iph->saddr, pptr[0]);
  62. }
  63. return cp;
  64. }
  65. static int
  66. udp_conn_schedule(int af, 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. struct ip_vs_iphdr iph;
  72. ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  73. uh = skb_header_pointer(skb, iph.len, sizeof(_udph), &_udph);
  74. if (uh == NULL) {
  75. *verdict = NF_DROP;
  76. return 0;
  77. }
  78. svc = ip_vs_service_get(af, skb->mark, iph.protocol,
  79. &iph.daddr, uh->dest);
  80. if (svc) {
  81. if (ip_vs_todrop()) {
  82. /*
  83. * It seems that we are very loaded.
  84. * We have to drop this packet :(
  85. */
  86. ip_vs_service_put(svc);
  87. *verdict = NF_DROP;
  88. return 0;
  89. }
  90. /*
  91. * Let the virtual server select a real server for the
  92. * incoming connection, and create a connection entry.
  93. */
  94. *cpp = ip_vs_schedule(svc, skb);
  95. if (!*cpp) {
  96. *verdict = ip_vs_leave(svc, skb, pp);
  97. return 0;
  98. }
  99. ip_vs_service_put(svc);
  100. }
  101. return 1;
  102. }
  103. static inline void
  104. udp_fast_csum_update(int af, struct udphdr *uhdr,
  105. const union nf_inet_addr *oldip,
  106. const union nf_inet_addr *newip,
  107. __be16 oldport, __be16 newport)
  108. {
  109. #ifdef CONFIG_IP_VS_IPV6
  110. if (af == AF_INET6)
  111. uhdr->check =
  112. csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
  113. ip_vs_check_diff2(oldport, newport,
  114. ~csum_unfold(uhdr->check))));
  115. else
  116. #endif
  117. uhdr->check =
  118. csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
  119. ip_vs_check_diff2(oldport, newport,
  120. ~csum_unfold(uhdr->check))));
  121. if (!uhdr->check)
  122. uhdr->check = CSUM_MANGLED_0;
  123. }
  124. static inline void
  125. udp_partial_csum_update(int af, struct udphdr *uhdr,
  126. const union nf_inet_addr *oldip,
  127. const union nf_inet_addr *newip,
  128. __be16 oldlen, __be16 newlen)
  129. {
  130. #ifdef CONFIG_IP_VS_IPV6
  131. if (af == AF_INET6)
  132. uhdr->check =
  133. csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
  134. ip_vs_check_diff2(oldlen, newlen,
  135. ~csum_unfold(uhdr->check))));
  136. else
  137. #endif
  138. uhdr->check =
  139. csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
  140. ip_vs_check_diff2(oldlen, newlen,
  141. ~csum_unfold(uhdr->check))));
  142. }
  143. static int
  144. udp_snat_handler(struct sk_buff *skb,
  145. struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
  146. {
  147. struct udphdr *udph;
  148. unsigned int udphoff;
  149. int oldlen;
  150. #ifdef CONFIG_IP_VS_IPV6
  151. if (cp->af == AF_INET6)
  152. udphoff = sizeof(struct ipv6hdr);
  153. else
  154. #endif
  155. udphoff = ip_hdrlen(skb);
  156. oldlen = skb->len - udphoff;
  157. /* csum_check requires unshared skb */
  158. if (!skb_make_writable(skb, udphoff+sizeof(*udph)))
  159. return 0;
  160. if (unlikely(cp->app != NULL)) {
  161. /* Some checks before mangling */
  162. if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
  163. return 0;
  164. /*
  165. * Call application helper if needed
  166. */
  167. if (!ip_vs_app_pkt_out(cp, skb))
  168. return 0;
  169. }
  170. udph = (void *)skb_network_header(skb) + udphoff;
  171. udph->source = cp->vport;
  172. /*
  173. * Adjust UDP checksums
  174. */
  175. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  176. udp_partial_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
  177. htonl(oldlen),
  178. htonl(skb->len - udphoff));
  179. } else if (!cp->app && (udph->check != 0)) {
  180. /* Only port and addr are changed, do fast csum update */
  181. udp_fast_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
  182. cp->dport, cp->vport);
  183. if (skb->ip_summed == CHECKSUM_COMPLETE)
  184. skb->ip_summed = CHECKSUM_NONE;
  185. } else {
  186. /* full checksum calculation */
  187. udph->check = 0;
  188. skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0);
  189. #ifdef CONFIG_IP_VS_IPV6
  190. if (cp->af == AF_INET6)
  191. udph->check = csum_ipv6_magic(&cp->vaddr.in6,
  192. &cp->caddr.in6,
  193. skb->len - udphoff,
  194. cp->protocol, skb->csum);
  195. else
  196. #endif
  197. udph->check = csum_tcpudp_magic(cp->vaddr.ip,
  198. cp->caddr.ip,
  199. skb->len - udphoff,
  200. cp->protocol,
  201. skb->csum);
  202. if (udph->check == 0)
  203. udph->check = CSUM_MANGLED_0;
  204. IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
  205. pp->name, udph->check,
  206. (char*)&(udph->check) - (char*)udph);
  207. }
  208. return 1;
  209. }
  210. static int
  211. udp_dnat_handler(struct sk_buff *skb,
  212. struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
  213. {
  214. struct udphdr *udph;
  215. unsigned int udphoff;
  216. int oldlen;
  217. #ifdef CONFIG_IP_VS_IPV6
  218. if (cp->af == AF_INET6)
  219. udphoff = sizeof(struct ipv6hdr);
  220. else
  221. #endif
  222. udphoff = ip_hdrlen(skb);
  223. oldlen = skb->len - udphoff;
  224. /* csum_check requires unshared skb */
  225. if (!skb_make_writable(skb, udphoff+sizeof(*udph)))
  226. return 0;
  227. if (unlikely(cp->app != NULL)) {
  228. /* Some checks before mangling */
  229. if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
  230. return 0;
  231. /*
  232. * Attempt ip_vs_app call.
  233. * It will fix ip_vs_conn
  234. */
  235. if (!ip_vs_app_pkt_in(cp, skb))
  236. return 0;
  237. }
  238. udph = (void *)skb_network_header(skb) + udphoff;
  239. udph->dest = cp->dport;
  240. /*
  241. * Adjust UDP checksums
  242. */
  243. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  244. udp_partial_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr,
  245. htonl(oldlen),
  246. htonl(skb->len - udphoff));
  247. } else if (!cp->app && (udph->check != 0)) {
  248. /* Only port and addr are changed, do fast csum update */
  249. udp_fast_csum_update(cp->af, udph, &cp->vaddr, &cp->daddr,
  250. cp->vport, cp->dport);
  251. if (skb->ip_summed == CHECKSUM_COMPLETE)
  252. skb->ip_summed = CHECKSUM_NONE;
  253. } else {
  254. /* full checksum calculation */
  255. udph->check = 0;
  256. skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0);
  257. #ifdef CONFIG_IP_VS_IPV6
  258. if (cp->af == AF_INET6)
  259. udph->check = csum_ipv6_magic(&cp->caddr.in6,
  260. &cp->daddr.in6,
  261. skb->len - udphoff,
  262. cp->protocol, skb->csum);
  263. else
  264. #endif
  265. udph->check = csum_tcpudp_magic(cp->caddr.ip,
  266. cp->daddr.ip,
  267. skb->len - udphoff,
  268. cp->protocol,
  269. skb->csum);
  270. if (udph->check == 0)
  271. udph->check = CSUM_MANGLED_0;
  272. skb->ip_summed = CHECKSUM_UNNECESSARY;
  273. }
  274. return 1;
  275. }
  276. static int
  277. udp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp)
  278. {
  279. struct udphdr _udph, *uh;
  280. unsigned int udphoff;
  281. #ifdef CONFIG_IP_VS_IPV6
  282. if (af == AF_INET6)
  283. udphoff = sizeof(struct ipv6hdr);
  284. else
  285. #endif
  286. udphoff = ip_hdrlen(skb);
  287. uh = skb_header_pointer(skb, udphoff, sizeof(_udph), &_udph);
  288. if (uh == NULL)
  289. return 0;
  290. if (uh->check != 0) {
  291. switch (skb->ip_summed) {
  292. case CHECKSUM_NONE:
  293. skb->csum = skb_checksum(skb, udphoff,
  294. skb->len - udphoff, 0);
  295. case CHECKSUM_COMPLETE:
  296. #ifdef CONFIG_IP_VS_IPV6
  297. if (af == AF_INET6) {
  298. if (csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  299. &ipv6_hdr(skb)->daddr,
  300. skb->len - udphoff,
  301. ipv6_hdr(skb)->nexthdr,
  302. skb->csum)) {
  303. IP_VS_DBG_RL_PKT(0, pp, skb, 0,
  304. "Failed checksum for");
  305. return 0;
  306. }
  307. } else
  308. #endif
  309. if (csum_tcpudp_magic(ip_hdr(skb)->saddr,
  310. ip_hdr(skb)->daddr,
  311. skb->len - udphoff,
  312. ip_hdr(skb)->protocol,
  313. skb->csum)) {
  314. IP_VS_DBG_RL_PKT(0, pp, skb, 0,
  315. "Failed checksum for");
  316. return 0;
  317. }
  318. break;
  319. default:
  320. /* No need to checksum. */
  321. break;
  322. }
  323. }
  324. return 1;
  325. }
  326. /*
  327. * Note: the caller guarantees that only one of register_app,
  328. * unregister_app or app_conn_bind is called each time.
  329. */
  330. #define UDP_APP_TAB_BITS 4
  331. #define UDP_APP_TAB_SIZE (1 << UDP_APP_TAB_BITS)
  332. #define UDP_APP_TAB_MASK (UDP_APP_TAB_SIZE - 1)
  333. static struct list_head udp_apps[UDP_APP_TAB_SIZE];
  334. static DEFINE_SPINLOCK(udp_app_lock);
  335. static inline __u16 udp_app_hashkey(__be16 port)
  336. {
  337. return (((__force u16)port >> UDP_APP_TAB_BITS) ^ (__force u16)port)
  338. & UDP_APP_TAB_MASK;
  339. }
  340. static int udp_register_app(struct ip_vs_app *inc)
  341. {
  342. struct ip_vs_app *i;
  343. __u16 hash;
  344. __be16 port = inc->port;
  345. int ret = 0;
  346. hash = udp_app_hashkey(port);
  347. spin_lock_bh(&udp_app_lock);
  348. list_for_each_entry(i, &udp_apps[hash], p_list) {
  349. if (i->port == port) {
  350. ret = -EEXIST;
  351. goto out;
  352. }
  353. }
  354. list_add(&inc->p_list, &udp_apps[hash]);
  355. atomic_inc(&ip_vs_protocol_udp.appcnt);
  356. out:
  357. spin_unlock_bh(&udp_app_lock);
  358. return ret;
  359. }
  360. static void
  361. udp_unregister_app(struct ip_vs_app *inc)
  362. {
  363. spin_lock_bh(&udp_app_lock);
  364. atomic_dec(&ip_vs_protocol_udp.appcnt);
  365. list_del(&inc->p_list);
  366. spin_unlock_bh(&udp_app_lock);
  367. }
  368. static int udp_app_conn_bind(struct ip_vs_conn *cp)
  369. {
  370. int hash;
  371. struct ip_vs_app *inc;
  372. int result = 0;
  373. /* Default binding: bind app only for NAT */
  374. if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
  375. return 0;
  376. /* Lookup application incarnations and bind the right one */
  377. hash = udp_app_hashkey(cp->vport);
  378. spin_lock(&udp_app_lock);
  379. list_for_each_entry(inc, &udp_apps[hash], p_list) {
  380. if (inc->port == cp->vport) {
  381. if (unlikely(!ip_vs_app_inc_get(inc)))
  382. break;
  383. spin_unlock(&udp_app_lock);
  384. IP_VS_DBG_BUF(9, "%s: Binding conn %s:%u->"
  385. "%s:%u to app %s on port %u\n",
  386. __func__,
  387. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  388. ntohs(cp->cport),
  389. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  390. ntohs(cp->vport),
  391. inc->name, ntohs(inc->port));
  392. cp->app = inc;
  393. if (inc->init_conn)
  394. result = inc->init_conn(inc, cp);
  395. goto out;
  396. }
  397. }
  398. spin_unlock(&udp_app_lock);
  399. out:
  400. return result;
  401. }
  402. static int udp_timeouts[IP_VS_UDP_S_LAST+1] = {
  403. [IP_VS_UDP_S_NORMAL] = 5*60*HZ,
  404. [IP_VS_UDP_S_LAST] = 2*HZ,
  405. };
  406. static char * udp_state_name_table[IP_VS_UDP_S_LAST+1] = {
  407. [IP_VS_UDP_S_NORMAL] = "UDP",
  408. [IP_VS_UDP_S_LAST] = "BUG!",
  409. };
  410. static int
  411. udp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to)
  412. {
  413. return ip_vs_set_state_timeout(pp->timeout_table, IP_VS_UDP_S_LAST,
  414. udp_state_name_table, sname, to);
  415. }
  416. static const char * udp_state_name(int state)
  417. {
  418. if (state >= IP_VS_UDP_S_LAST)
  419. return "ERR!";
  420. return udp_state_name_table[state] ? udp_state_name_table[state] : "?";
  421. }
  422. static int
  423. udp_state_transition(struct ip_vs_conn *cp, int direction,
  424. const struct sk_buff *skb,
  425. struct ip_vs_protocol *pp)
  426. {
  427. cp->timeout = pp->timeout_table[IP_VS_UDP_S_NORMAL];
  428. return 1;
  429. }
  430. static void udp_init(struct ip_vs_protocol *pp)
  431. {
  432. IP_VS_INIT_HASH_TABLE(udp_apps);
  433. pp->timeout_table = udp_timeouts;
  434. }
  435. static void udp_exit(struct ip_vs_protocol *pp)
  436. {
  437. }
  438. struct ip_vs_protocol ip_vs_protocol_udp = {
  439. .name = "UDP",
  440. .protocol = IPPROTO_UDP,
  441. .num_states = IP_VS_UDP_S_LAST,
  442. .dont_defrag = 0,
  443. .init = udp_init,
  444. .exit = udp_exit,
  445. .conn_schedule = udp_conn_schedule,
  446. .conn_in_get = udp_conn_in_get,
  447. .conn_out_get = udp_conn_out_get,
  448. .snat_handler = udp_snat_handler,
  449. .dnat_handler = udp_dnat_handler,
  450. .csum_check = udp_csum_check,
  451. .state_transition = udp_state_transition,
  452. .state_name = udp_state_name,
  453. .register_app = udp_register_app,
  454. .unregister_app = udp_unregister_app,
  455. .app_conn_bind = udp_app_conn_bind,
  456. .debug_packet = ip_vs_tcpudp_debug_packet,
  457. .timeout_change = NULL,
  458. .set_state_timeout = udp_set_state_timeout,
  459. };