ip_vs_xmit.c 13 KB

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