ip_vs_proto_udp.c 12 KB

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