ip_vs_xmit.c 13 KB

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