ip_vs_xmit.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * ip_vs_xmit.c: various packet transmitters 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/kernel.h>
  16. #include <linux/tcp.h> /* for tcphdr */
  17. #include <net/ip.h>
  18. #include <net/tcp.h> /* for csum_tcpudp_magic */
  19. #include <net/udp.h>
  20. #include <net/icmp.h> /* for icmp_send */
  21. #include <net/route.h> /* for ip_route_output */
  22. #include <net/ipv6.h>
  23. #include <net/ip6_route.h>
  24. #include <linux/icmpv6.h>
  25. #include <linux/netfilter.h>
  26. #include <linux/netfilter_ipv4.h>
  27. #include <net/ip_vs.h>
  28. /*
  29. * Destination cache to speed up outgoing route lookup
  30. */
  31. static inline void
  32. __ip_vs_dst_set(struct ip_vs_dest *dest, u32 rtos, struct dst_entry *dst)
  33. {
  34. struct dst_entry *old_dst;
  35. old_dst = dest->dst_cache;
  36. dest->dst_cache = dst;
  37. dest->dst_rtos = rtos;
  38. dst_release(old_dst);
  39. }
  40. static inline struct dst_entry *
  41. __ip_vs_dst_check(struct ip_vs_dest *dest, u32 rtos, u32 cookie)
  42. {
  43. struct dst_entry *dst = dest->dst_cache;
  44. if (!dst)
  45. return NULL;
  46. if ((dst->obsolete
  47. || (dest->af == AF_INET && rtos != dest->dst_rtos)) &&
  48. dst->ops->check(dst, cookie) == NULL) {
  49. dest->dst_cache = NULL;
  50. dst_release(dst);
  51. return NULL;
  52. }
  53. dst_hold(dst);
  54. return dst;
  55. }
  56. static struct rtable *
  57. __ip_vs_get_out_rt(struct ip_vs_conn *cp, u32 rtos)
  58. {
  59. struct rtable *rt; /* Route to the other host */
  60. struct ip_vs_dest *dest = cp->dest;
  61. if (dest) {
  62. spin_lock(&dest->dst_lock);
  63. if (!(rt = (struct rtable *)
  64. __ip_vs_dst_check(dest, rtos, 0))) {
  65. struct flowi fl = {
  66. .oif = 0,
  67. .nl_u = {
  68. .ip4_u = {
  69. .daddr = dest->addr.ip,
  70. .saddr = 0,
  71. .tos = rtos, } },
  72. };
  73. if (ip_route_output_key(&init_net, &rt, &fl)) {
  74. spin_unlock(&dest->dst_lock);
  75. IP_VS_DBG_RL("ip_route_output error, "
  76. "dest: %u.%u.%u.%u\n",
  77. NIPQUAD(dest->addr.ip));
  78. return NULL;
  79. }
  80. __ip_vs_dst_set(dest, rtos, dst_clone(&rt->u.dst));
  81. IP_VS_DBG(10, "new dst %u.%u.%u.%u, refcnt=%d, rtos=%X\n",
  82. NIPQUAD(dest->addr.ip),
  83. atomic_read(&rt->u.dst.__refcnt), rtos);
  84. }
  85. spin_unlock(&dest->dst_lock);
  86. } else {
  87. struct flowi fl = {
  88. .oif = 0,
  89. .nl_u = {
  90. .ip4_u = {
  91. .daddr = cp->daddr.ip,
  92. .saddr = 0,
  93. .tos = rtos, } },
  94. };
  95. if (ip_route_output_key(&init_net, &rt, &fl)) {
  96. IP_VS_DBG_RL("ip_route_output error, dest: "
  97. "%u.%u.%u.%u\n", NIPQUAD(cp->daddr.ip));
  98. return NULL;
  99. }
  100. }
  101. return rt;
  102. }
  103. #ifdef CONFIG_IP_VS_IPV6
  104. static struct rt6_info *
  105. __ip_vs_get_out_rt_v6(struct ip_vs_conn *cp)
  106. {
  107. struct rt6_info *rt; /* Route to the other host */
  108. struct ip_vs_dest *dest = cp->dest;
  109. if (dest) {
  110. spin_lock(&dest->dst_lock);
  111. rt = (struct rt6_info *)__ip_vs_dst_check(dest, 0, 0);
  112. if (!rt) {
  113. struct flowi fl = {
  114. .oif = 0,
  115. .nl_u = {
  116. .ip6_u = {
  117. .daddr = dest->addr.in6,
  118. .saddr = {
  119. .s6_addr32 =
  120. { 0, 0, 0, 0 },
  121. },
  122. },
  123. },
  124. };
  125. rt = (struct rt6_info *)ip6_route_output(&init_net,
  126. NULL, &fl);
  127. if (!rt) {
  128. spin_unlock(&dest->dst_lock);
  129. IP_VS_DBG_RL("ip6_route_output error, "
  130. "dest: " NIP6_FMT "\n",
  131. NIP6(dest->addr.in6));
  132. return NULL;
  133. }
  134. __ip_vs_dst_set(dest, 0, dst_clone(&rt->u.dst));
  135. IP_VS_DBG(10, "new dst " NIP6_FMT ", refcnt=%d\n",
  136. NIP6(dest->addr.in6),
  137. atomic_read(&rt->u.dst.__refcnt));
  138. }
  139. spin_unlock(&dest->dst_lock);
  140. } else {
  141. struct flowi fl = {
  142. .oif = 0,
  143. .nl_u = {
  144. .ip6_u = {
  145. .daddr = cp->daddr.in6,
  146. .saddr = {
  147. .s6_addr32 = { 0, 0, 0, 0 },
  148. },
  149. },
  150. },
  151. };
  152. rt = (struct rt6_info *)ip6_route_output(&init_net, NULL, &fl);
  153. if (!rt) {
  154. IP_VS_DBG_RL("ip6_route_output error, dest: "
  155. NIP6_FMT "\n", NIP6(cp->daddr.in6));
  156. return NULL;
  157. }
  158. }
  159. return rt;
  160. }
  161. #endif
  162. /*
  163. * Release dest->dst_cache before a dest is removed
  164. */
  165. void
  166. ip_vs_dst_reset(struct ip_vs_dest *dest)
  167. {
  168. struct dst_entry *old_dst;
  169. old_dst = dest->dst_cache;
  170. dest->dst_cache = NULL;
  171. dst_release(old_dst);
  172. }
  173. #define IP_VS_XMIT(pf, skb, rt) \
  174. do { \
  175. (skb)->ipvs_property = 1; \
  176. skb_forward_csum(skb); \
  177. NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
  178. (rt)->u.dst.dev, dst_output); \
  179. } while (0)
  180. /*
  181. * NULL transmitter (do nothing except return NF_ACCEPT)
  182. */
  183. int
  184. ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  185. struct ip_vs_protocol *pp)
  186. {
  187. /* we do not touch skb and do not need pskb ptr */
  188. return NF_ACCEPT;
  189. }
  190. /*
  191. * Bypass transmitter
  192. * Let packets bypass the destination when the destination is not
  193. * available, it may be only used in transparent cache cluster.
  194. */
  195. int
  196. ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  197. struct ip_vs_protocol *pp)
  198. {
  199. struct rtable *rt; /* Route to the other host */
  200. struct iphdr *iph = ip_hdr(skb);
  201. u8 tos = iph->tos;
  202. int mtu;
  203. struct flowi fl = {
  204. .oif = 0,
  205. .nl_u = {
  206. .ip4_u = {
  207. .daddr = iph->daddr,
  208. .saddr = 0,
  209. .tos = RT_TOS(tos), } },
  210. };
  211. EnterFunction(10);
  212. if (ip_route_output_key(&init_net, &rt, &fl)) {
  213. IP_VS_DBG_RL("ip_vs_bypass_xmit(): ip_route_output error, "
  214. "dest: %u.%u.%u.%u\n", NIPQUAD(iph->daddr));
  215. goto tx_error_icmp;
  216. }
  217. /* MTU checking */
  218. mtu = dst_mtu(&rt->u.dst);
  219. if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF))) {
  220. ip_rt_put(rt);
  221. icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
  222. IP_VS_DBG_RL("ip_vs_bypass_xmit(): frag needed\n");
  223. goto tx_error;
  224. }
  225. /*
  226. * Call ip_send_check because we are not sure it is called
  227. * after ip_defrag. Is copy-on-write needed?
  228. */
  229. if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
  230. ip_rt_put(rt);
  231. return NF_STOLEN;
  232. }
  233. ip_send_check(ip_hdr(skb));
  234. /* drop old route */
  235. dst_release(skb->dst);
  236. skb->dst = &rt->u.dst;
  237. /* Another hack: avoid icmp_send in ip_fragment */
  238. skb->local_df = 1;
  239. IP_VS_XMIT(PF_INET, skb, rt);
  240. LeaveFunction(10);
  241. return NF_STOLEN;
  242. tx_error_icmp:
  243. dst_link_failure(skb);
  244. tx_error:
  245. kfree_skb(skb);
  246. LeaveFunction(10);
  247. return NF_STOLEN;
  248. }
  249. /*
  250. * NAT transmitter (only for outside-to-inside nat forwarding)
  251. * Not used for related ICMP
  252. */
  253. int
  254. ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  255. struct ip_vs_protocol *pp)
  256. {
  257. struct rtable *rt; /* Route to the other host */
  258. int mtu;
  259. struct iphdr *iph = ip_hdr(skb);
  260. EnterFunction(10);
  261. /* check if it is a connection of no-client-port */
  262. if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
  263. __be16 _pt, *p;
  264. p = skb_header_pointer(skb, iph->ihl*4, sizeof(_pt), &_pt);
  265. if (p == NULL)
  266. goto tx_error;
  267. ip_vs_conn_fill_cport(cp, *p);
  268. IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
  269. }
  270. if (!(rt = __ip_vs_get_out_rt(cp, RT_TOS(iph->tos))))
  271. goto tx_error_icmp;
  272. /* MTU checking */
  273. mtu = dst_mtu(&rt->u.dst);
  274. if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF))) {
  275. ip_rt_put(rt);
  276. icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
  277. IP_VS_DBG_RL_PKT(0, pp, skb, 0, "ip_vs_nat_xmit(): frag needed for");
  278. goto tx_error;
  279. }
  280. /* copy-on-write the packet before mangling it */
  281. if (!skb_make_writable(skb, sizeof(struct iphdr)))
  282. goto tx_error_put;
  283. if (skb_cow(skb, rt->u.dst.dev->hard_header_len))
  284. goto tx_error_put;
  285. /* drop old route */
  286. dst_release(skb->dst);
  287. skb->dst = &rt->u.dst;
  288. /* mangle the packet */
  289. if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
  290. goto tx_error;
  291. ip_hdr(skb)->daddr = cp->daddr.ip;
  292. ip_send_check(ip_hdr(skb));
  293. IP_VS_DBG_PKT(10, pp, skb, 0, "After DNAT");
  294. /* FIXME: when application helper enlarges the packet and the length
  295. is larger than the MTU of outgoing device, there will be still
  296. MTU problem. */
  297. /* Another hack: avoid icmp_send in ip_fragment */
  298. skb->local_df = 1;
  299. IP_VS_XMIT(PF_INET, skb, rt);
  300. LeaveFunction(10);
  301. return NF_STOLEN;
  302. tx_error_icmp:
  303. dst_link_failure(skb);
  304. tx_error:
  305. LeaveFunction(10);
  306. kfree_skb(skb);
  307. return NF_STOLEN;
  308. tx_error_put:
  309. ip_rt_put(rt);
  310. goto tx_error;
  311. }
  312. /*
  313. * IP Tunneling transmitter
  314. *
  315. * This function encapsulates the packet in a new IP packet, its
  316. * destination will be set to cp->daddr. Most code of this function
  317. * is taken from ipip.c.
  318. *
  319. * It is used in VS/TUN cluster. The load balancer selects a real
  320. * server from a cluster based on a scheduling algorithm,
  321. * encapsulates the request packet and forwards it to the selected
  322. * server. For example, all real servers are configured with
  323. * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
  324. * the encapsulated packet, it will decapsulate the packet, processe
  325. * the request and return the response packets directly to the client
  326. * without passing the load balancer. This can greatly increase the
  327. * scalability of virtual server.
  328. *
  329. * Used for ANY protocol
  330. */
  331. int
  332. ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  333. struct ip_vs_protocol *pp)
  334. {
  335. struct rtable *rt; /* Route to the other host */
  336. struct net_device *tdev; /* Device to other host */
  337. struct iphdr *old_iph = ip_hdr(skb);
  338. u8 tos = old_iph->tos;
  339. __be16 df = old_iph->frag_off;
  340. sk_buff_data_t old_transport_header = skb->transport_header;
  341. struct iphdr *iph; /* Our new IP header */
  342. unsigned int max_headroom; /* The extra header space needed */
  343. int mtu;
  344. EnterFunction(10);
  345. if (skb->protocol != htons(ETH_P_IP)) {
  346. IP_VS_DBG_RL("ip_vs_tunnel_xmit(): protocol error, "
  347. "ETH_P_IP: %d, skb protocol: %d\n",
  348. htons(ETH_P_IP), skb->protocol);
  349. goto tx_error;
  350. }
  351. if (!(rt = __ip_vs_get_out_rt(cp, RT_TOS(tos))))
  352. goto tx_error_icmp;
  353. tdev = rt->u.dst.dev;
  354. mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
  355. if (mtu < 68) {
  356. ip_rt_put(rt);
  357. IP_VS_DBG_RL("ip_vs_tunnel_xmit(): mtu less than 68\n");
  358. goto tx_error;
  359. }
  360. if (skb->dst)
  361. skb->dst->ops->update_pmtu(skb->dst, mtu);
  362. df |= (old_iph->frag_off & htons(IP_DF));
  363. if ((old_iph->frag_off & htons(IP_DF))
  364. && mtu < ntohs(old_iph->tot_len)) {
  365. icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
  366. ip_rt_put(rt);
  367. IP_VS_DBG_RL("ip_vs_tunnel_xmit(): frag needed\n");
  368. goto tx_error;
  369. }
  370. /*
  371. * Okay, now see if we can stuff it in the buffer as-is.
  372. */
  373. max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
  374. if (skb_headroom(skb) < max_headroom
  375. || skb_cloned(skb) || skb_shared(skb)) {
  376. struct sk_buff *new_skb =
  377. skb_realloc_headroom(skb, max_headroom);
  378. if (!new_skb) {
  379. ip_rt_put(rt);
  380. kfree_skb(skb);
  381. IP_VS_ERR_RL("ip_vs_tunnel_xmit(): no memory\n");
  382. return NF_STOLEN;
  383. }
  384. kfree_skb(skb);
  385. skb = new_skb;
  386. old_iph = ip_hdr(skb);
  387. }
  388. skb->transport_header = old_transport_header;
  389. /* fix old IP header checksum */
  390. ip_send_check(old_iph);
  391. skb_push(skb, sizeof(struct iphdr));
  392. skb_reset_network_header(skb);
  393. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  394. /* drop old route */
  395. dst_release(skb->dst);
  396. skb->dst = &rt->u.dst;
  397. /*
  398. * Push down and install the IPIP header.
  399. */
  400. iph = ip_hdr(skb);
  401. iph->version = 4;
  402. iph->ihl = sizeof(struct iphdr)>>2;
  403. iph->frag_off = df;
  404. iph->protocol = IPPROTO_IPIP;
  405. iph->tos = tos;
  406. iph->daddr = rt->rt_dst;
  407. iph->saddr = rt->rt_src;
  408. iph->ttl = old_iph->ttl;
  409. ip_select_ident(iph, &rt->u.dst, NULL);
  410. /* Another hack: avoid icmp_send in ip_fragment */
  411. skb->local_df = 1;
  412. ip_local_out(skb);
  413. LeaveFunction(10);
  414. return NF_STOLEN;
  415. tx_error_icmp:
  416. dst_link_failure(skb);
  417. tx_error:
  418. kfree_skb(skb);
  419. LeaveFunction(10);
  420. return NF_STOLEN;
  421. }
  422. /*
  423. * Direct Routing transmitter
  424. * Used for ANY protocol
  425. */
  426. int
  427. ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  428. struct ip_vs_protocol *pp)
  429. {
  430. struct rtable *rt; /* Route to the other host */
  431. struct iphdr *iph = ip_hdr(skb);
  432. int mtu;
  433. EnterFunction(10);
  434. if (!(rt = __ip_vs_get_out_rt(cp, RT_TOS(iph->tos))))
  435. goto tx_error_icmp;
  436. /* MTU checking */
  437. mtu = dst_mtu(&rt->u.dst);
  438. if ((iph->frag_off & htons(IP_DF)) && skb->len > mtu) {
  439. icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
  440. ip_rt_put(rt);
  441. IP_VS_DBG_RL("ip_vs_dr_xmit(): frag needed\n");
  442. goto tx_error;
  443. }
  444. /*
  445. * Call ip_send_check because we are not sure it is called
  446. * after ip_defrag. Is copy-on-write needed?
  447. */
  448. if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
  449. ip_rt_put(rt);
  450. return NF_STOLEN;
  451. }
  452. ip_send_check(ip_hdr(skb));
  453. /* drop old route */
  454. dst_release(skb->dst);
  455. skb->dst = &rt->u.dst;
  456. /* Another hack: avoid icmp_send in ip_fragment */
  457. skb->local_df = 1;
  458. IP_VS_XMIT(PF_INET, skb, rt);
  459. LeaveFunction(10);
  460. return NF_STOLEN;
  461. tx_error_icmp:
  462. dst_link_failure(skb);
  463. tx_error:
  464. kfree_skb(skb);
  465. LeaveFunction(10);
  466. return NF_STOLEN;
  467. }
  468. /*
  469. * ICMP packet transmitter
  470. * called by the ip_vs_in_icmp
  471. */
  472. int
  473. ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  474. struct ip_vs_protocol *pp, int offset)
  475. {
  476. struct rtable *rt; /* Route to the other host */
  477. int mtu;
  478. int rc;
  479. EnterFunction(10);
  480. /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
  481. forwarded directly here, because there is no need to
  482. translate address/port back */
  483. if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
  484. if (cp->packet_xmit)
  485. rc = cp->packet_xmit(skb, cp, pp);
  486. else
  487. rc = NF_ACCEPT;
  488. /* do not touch skb anymore */
  489. atomic_inc(&cp->in_pkts);
  490. goto out;
  491. }
  492. /*
  493. * mangle and send the packet here (only for VS/NAT)
  494. */
  495. if (!(rt = __ip_vs_get_out_rt(cp, RT_TOS(ip_hdr(skb)->tos))))
  496. goto tx_error_icmp;
  497. /* MTU checking */
  498. mtu = dst_mtu(&rt->u.dst);
  499. if ((skb->len > mtu) && (ip_hdr(skb)->frag_off & htons(IP_DF))) {
  500. ip_rt_put(rt);
  501. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
  502. IP_VS_DBG_RL("ip_vs_in_icmp(): frag needed\n");
  503. goto tx_error;
  504. }
  505. /* copy-on-write the packet before mangling it */
  506. if (!skb_make_writable(skb, offset))
  507. goto tx_error_put;
  508. if (skb_cow(skb, rt->u.dst.dev->hard_header_len))
  509. goto tx_error_put;
  510. /* drop the old route when skb is not shared */
  511. dst_release(skb->dst);
  512. skb->dst = &rt->u.dst;
  513. ip_vs_nat_icmp(skb, pp, cp, 0);
  514. /* Another hack: avoid icmp_send in ip_fragment */
  515. skb->local_df = 1;
  516. IP_VS_XMIT(PF_INET, skb, rt);
  517. rc = NF_STOLEN;
  518. goto out;
  519. tx_error_icmp:
  520. dst_link_failure(skb);
  521. tx_error:
  522. dev_kfree_skb(skb);
  523. rc = NF_STOLEN;
  524. out:
  525. LeaveFunction(10);
  526. return rc;
  527. tx_error_put:
  528. ip_rt_put(rt);
  529. goto tx_error;
  530. }