ip_vs_xmit.c 13 KB

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