blob: 30b3189bd29c9aa982d2bf2e1a5ba9d7e0309cdd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ip_vs_xmit.c: various packet transmitters for IPVS
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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
Hannes Eder9aada7a2009-07-30 14:29:44 -070016#define KMSG_COMPONENT "IPVS"
17#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/tcp.h> /* for tcphdr */
Herbert Xuc439cb22008-01-11 19:14:00 -080021#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <net/tcp.h> /* for csum_tcpudp_magic */
23#include <net/udp.h>
24#include <net/icmp.h> /* for icmp_send */
25#include <net/route.h> /* for ip_route_output */
Julius Volz38cdcc92008-09-02 15:55:44 +020026#include <net/ipv6.h>
27#include <net/ip6_route.h>
28#include <linux/icmpv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/netfilter.h>
30#include <linux/netfilter_ipv4.h>
31
32#include <net/ip_vs.h>
33
34
35/*
36 * Destination cache to speed up outgoing route lookup
37 */
38static inline void
39__ip_vs_dst_set(struct ip_vs_dest *dest, u32 rtos, struct dst_entry *dst)
40{
41 struct dst_entry *old_dst;
42
43 old_dst = dest->dst_cache;
44 dest->dst_cache = dst;
45 dest->dst_rtos = rtos;
46 dst_release(old_dst);
47}
48
49static inline struct dst_entry *
50__ip_vs_dst_check(struct ip_vs_dest *dest, u32 rtos, u32 cookie)
51{
52 struct dst_entry *dst = dest->dst_cache;
53
54 if (!dst)
55 return NULL;
Julius Volz38cdcc92008-09-02 15:55:44 +020056 if ((dst->obsolete
57 || (dest->af == AF_INET && rtos != dest->dst_rtos)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 dst->ops->check(dst, cookie) == NULL) {
59 dest->dst_cache = NULL;
60 dst_release(dst);
61 return NULL;
62 }
63 dst_hold(dst);
64 return dst;
65}
66
Ilpo Järvinenad1b30b2008-01-05 23:12:40 -080067static struct rtable *
Linus Torvalds1da177e2005-04-16 15:20:36 -070068__ip_vs_get_out_rt(struct ip_vs_conn *cp, u32 rtos)
69{
70 struct rtable *rt; /* Route to the other host */
71 struct ip_vs_dest *dest = cp->dest;
72
73 if (dest) {
74 spin_lock(&dest->dst_lock);
75 if (!(rt = (struct rtable *)
76 __ip_vs_dst_check(dest, rtos, 0))) {
77 struct flowi fl = {
78 .oif = 0,
79 .nl_u = {
80 .ip4_u = {
Julius Volze7ade462008-09-02 15:55:33 +020081 .daddr = dest->addr.ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 .saddr = 0,
83 .tos = rtos, } },
84 };
85
Denis V. Lunevf2063512008-01-22 22:07:34 -080086 if (ip_route_output_key(&init_net, &rt, &fl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 spin_unlock(&dest->dst_lock);
Harvey Harrison14d5e832008-10-31 00:54:29 -070088 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
89 &dest->addr.ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return NULL;
91 }
92 __ip_vs_dst_set(dest, rtos, dst_clone(&rt->u.dst));
Harvey Harrison14d5e832008-10-31 00:54:29 -070093 IP_VS_DBG(10, "new dst %pI4, refcnt=%d, rtos=%X\n",
94 &dest->addr.ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 atomic_read(&rt->u.dst.__refcnt), rtos);
96 }
97 spin_unlock(&dest->dst_lock);
98 } else {
99 struct flowi fl = {
100 .oif = 0,
101 .nl_u = {
102 .ip4_u = {
Julius Volze7ade462008-09-02 15:55:33 +0200103 .daddr = cp->daddr.ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .saddr = 0,
105 .tos = rtos, } },
106 };
107
Denis V. Lunevf2063512008-01-22 22:07:34 -0800108 if (ip_route_output_key(&init_net, &rt, &fl)) {
Harvey Harrison14d5e832008-10-31 00:54:29 -0700109 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
110 &cp->daddr.ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return NULL;
112 }
113 }
114
115 return rt;
116}
117
Julius Volz38cdcc92008-09-02 15:55:44 +0200118#ifdef CONFIG_IP_VS_IPV6
119static struct rt6_info *
120__ip_vs_get_out_rt_v6(struct ip_vs_conn *cp)
121{
122 struct rt6_info *rt; /* Route to the other host */
123 struct ip_vs_dest *dest = cp->dest;
124
125 if (dest) {
126 spin_lock(&dest->dst_lock);
127 rt = (struct rt6_info *)__ip_vs_dst_check(dest, 0, 0);
128 if (!rt) {
129 struct flowi fl = {
130 .oif = 0,
131 .nl_u = {
132 .ip6_u = {
133 .daddr = dest->addr.in6,
134 .saddr = {
135 .s6_addr32 =
136 { 0, 0, 0, 0 },
137 },
138 },
139 },
140 };
141
142 rt = (struct rt6_info *)ip6_route_output(&init_net,
143 NULL, &fl);
144 if (!rt) {
145 spin_unlock(&dest->dst_lock);
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700146 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n",
Harvey Harrison38ff4fa2008-10-28 16:08:13 -0700147 &dest->addr.in6);
Julius Volz38cdcc92008-09-02 15:55:44 +0200148 return NULL;
149 }
150 __ip_vs_dst_set(dest, 0, dst_clone(&rt->u.dst));
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700151 IP_VS_DBG(10, "new dst %pI6, refcnt=%d\n",
Harvey Harrison38ff4fa2008-10-28 16:08:13 -0700152 &dest->addr.in6,
Julius Volz38cdcc92008-09-02 15:55:44 +0200153 atomic_read(&rt->u.dst.__refcnt));
154 }
155 spin_unlock(&dest->dst_lock);
156 } else {
157 struct flowi fl = {
158 .oif = 0,
159 .nl_u = {
160 .ip6_u = {
161 .daddr = cp->daddr.in6,
162 .saddr = {
163 .s6_addr32 = { 0, 0, 0, 0 },
164 },
165 },
166 },
167 };
168
169 rt = (struct rt6_info *)ip6_route_output(&init_net, NULL, &fl);
170 if (!rt) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700171 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n",
Harvey Harrison38ff4fa2008-10-28 16:08:13 -0700172 &cp->daddr.in6);
Julius Volz38cdcc92008-09-02 15:55:44 +0200173 return NULL;
174 }
175 }
176
177 return rt;
178}
179#endif
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/*
183 * Release dest->dst_cache before a dest is removed
184 */
185void
186ip_vs_dst_reset(struct ip_vs_dest *dest)
187{
188 struct dst_entry *old_dst;
189
190 old_dst = dest->dst_cache;
191 dest->dst_cache = NULL;
192 dst_release(old_dst);
193}
194
Julius Volz38cdcc92008-09-02 15:55:44 +0200195#define IP_VS_XMIT(pf, skb, rt) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196do { \
Harald Welte6869c4d2005-08-09 19:24:19 -0700197 (skb)->ipvs_property = 1; \
Herbert Xuccc79112007-07-30 16:20:12 -0700198 skb_forward_csum(skb); \
Julius Volz38cdcc92008-09-02 15:55:44 +0200199 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 (rt)->u.dst.dev, dst_output); \
201} while (0)
202
203
204/*
205 * NULL transmitter (do nothing except return NF_ACCEPT)
206 */
207int
208ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
209 struct ip_vs_protocol *pp)
210{
211 /* we do not touch skb and do not need pskb ptr */
212 return NF_ACCEPT;
213}
214
215
216/*
217 * Bypass transmitter
218 * Let packets bypass the destination when the destination is not
219 * available, it may be only used in transparent cache cluster.
220 */
221int
222ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
223 struct ip_vs_protocol *pp)
224{
225 struct rtable *rt; /* Route to the other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700226 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 u8 tos = iph->tos;
228 int mtu;
229 struct flowi fl = {
230 .oif = 0,
231 .nl_u = {
232 .ip4_u = {
233 .daddr = iph->daddr,
234 .saddr = 0,
235 .tos = RT_TOS(tos), } },
236 };
237
238 EnterFunction(10);
239
Denis V. Lunevf2063512008-01-22 22:07:34 -0800240 if (ip_route_output_key(&init_net, &rt, &fl)) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000241 IP_VS_DBG_RL("%s(): ip_route_output error, dest: %pI4\n",
242 __func__, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto tx_error_icmp;
244 }
245
246 /* MTU checking */
247 mtu = dst_mtu(&rt->u.dst);
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900248 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 ip_rt_put(rt);
250 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
Hannes Eder1e3e2382009-08-02 11:05:41 +0000251 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 goto tx_error;
253 }
254
255 /*
256 * Call ip_send_check because we are not sure it is called
257 * after ip_defrag. Is copy-on-write needed?
258 */
259 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
260 ip_rt_put(rt);
261 return NF_STOLEN;
262 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700263 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000266 skb_dst_drop(skb);
267 skb_dst_set(skb, &rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /* Another hack: avoid icmp_send in ip_fragment */
270 skb->local_df = 1;
271
Julius Volz38cdcc92008-09-02 15:55:44 +0200272 IP_VS_XMIT(PF_INET, skb, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 LeaveFunction(10);
275 return NF_STOLEN;
276
277 tx_error_icmp:
278 dst_link_failure(skb);
279 tx_error:
280 kfree_skb(skb);
281 LeaveFunction(10);
282 return NF_STOLEN;
283}
284
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200285#ifdef CONFIG_IP_VS_IPV6
286int
287ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
288 struct ip_vs_protocol *pp)
289{
290 struct rt6_info *rt; /* Route to the other host */
291 struct ipv6hdr *iph = ipv6_hdr(skb);
292 int mtu;
293 struct flowi fl = {
294 .oif = 0,
295 .nl_u = {
296 .ip6_u = {
297 .daddr = iph->daddr,
298 .saddr = { .s6_addr32 = {0, 0, 0, 0} }, } },
299 };
300
301 EnterFunction(10);
302
303 rt = (struct rt6_info *)ip6_route_output(&init_net, NULL, &fl);
304 if (!rt) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000305 IP_VS_DBG_RL("%s(): ip6_route_output error, dest: %pI6\n",
306 __func__, &iph->daddr);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200307 goto tx_error_icmp;
308 }
309
310 /* MTU checking */
311 mtu = dst_mtu(&rt->u.dst);
312 if (skb->len > mtu) {
313 dst_release(&rt->u.dst);
314 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000315 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200316 goto tx_error;
317 }
318
319 /*
320 * Call ip_send_check because we are not sure it is called
321 * after ip_defrag. Is copy-on-write needed?
322 */
323 skb = skb_share_check(skb, GFP_ATOMIC);
324 if (unlikely(skb == NULL)) {
325 dst_release(&rt->u.dst);
326 return NF_STOLEN;
327 }
328
329 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000330 skb_dst_drop(skb);
331 skb_dst_set(skb, &rt->u.dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200332
333 /* Another hack: avoid icmp_send in ip_fragment */
334 skb->local_df = 1;
335
336 IP_VS_XMIT(PF_INET6, skb, rt);
337
338 LeaveFunction(10);
339 return NF_STOLEN;
340
341 tx_error_icmp:
342 dst_link_failure(skb);
343 tx_error:
344 kfree_skb(skb);
345 LeaveFunction(10);
346 return NF_STOLEN;
347}
348#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350/*
351 * NAT transmitter (only for outside-to-inside nat forwarding)
352 * Not used for related ICMP
353 */
354int
355ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
356 struct ip_vs_protocol *pp)
357{
358 struct rtable *rt; /* Route to the other host */
359 int mtu;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700360 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 EnterFunction(10);
363
364 /* check if it is a connection of no-client-port */
365 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
Al Viro014d7302006-09-28 14:29:52 -0700366 __be16 _pt, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 p = skb_header_pointer(skb, iph->ihl*4, sizeof(_pt), &_pt);
368 if (p == NULL)
369 goto tx_error;
370 ip_vs_conn_fill_cport(cp, *p);
371 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
372 }
373
374 if (!(rt = __ip_vs_get_out_rt(cp, RT_TOS(iph->tos))))
375 goto tx_error_icmp;
376
377 /* MTU checking */
378 mtu = dst_mtu(&rt->u.dst);
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900379 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 ip_rt_put(rt);
381 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
382 IP_VS_DBG_RL_PKT(0, pp, skb, 0, "ip_vs_nat_xmit(): frag needed for");
383 goto tx_error;
384 }
385
386 /* copy-on-write the packet before mangling it */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -0700387 if (!skb_make_writable(skb, sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 goto tx_error_put;
389
390 if (skb_cow(skb, rt->u.dst.dev->hard_header_len))
391 goto tx_error_put;
392
393 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000394 skb_dst_drop(skb);
395 skb_dst_set(skb, &rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /* mangle the packet */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700398 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 goto tx_error;
Julius Volze7ade462008-09-02 15:55:33 +0200400 ip_hdr(skb)->daddr = cp->daddr.ip;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700401 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 IP_VS_DBG_PKT(10, pp, skb, 0, "After DNAT");
404
405 /* FIXME: when application helper enlarges the packet and the length
406 is larger than the MTU of outgoing device, there will be still
407 MTU problem. */
408
409 /* Another hack: avoid icmp_send in ip_fragment */
410 skb->local_df = 1;
411
Julius Volz38cdcc92008-09-02 15:55:44 +0200412 IP_VS_XMIT(PF_INET, skb, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 LeaveFunction(10);
415 return NF_STOLEN;
416
417 tx_error_icmp:
418 dst_link_failure(skb);
419 tx_error:
420 LeaveFunction(10);
421 kfree_skb(skb);
422 return NF_STOLEN;
423 tx_error_put:
424 ip_rt_put(rt);
425 goto tx_error;
426}
427
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200428#ifdef CONFIG_IP_VS_IPV6
429int
430ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
431 struct ip_vs_protocol *pp)
432{
433 struct rt6_info *rt; /* Route to the other host */
434 int mtu;
435
436 EnterFunction(10);
437
438 /* check if it is a connection of no-client-port */
439 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
440 __be16 _pt, *p;
441 p = skb_header_pointer(skb, sizeof(struct ipv6hdr),
442 sizeof(_pt), &_pt);
443 if (p == NULL)
444 goto tx_error;
445 ip_vs_conn_fill_cport(cp, *p);
446 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
447 }
448
449 rt = __ip_vs_get_out_rt_v6(cp);
450 if (!rt)
451 goto tx_error_icmp;
452
453 /* MTU checking */
454 mtu = dst_mtu(&rt->u.dst);
455 if (skb->len > mtu) {
456 dst_release(&rt->u.dst);
457 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
458 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
459 "ip_vs_nat_xmit_v6(): frag needed for");
460 goto tx_error;
461 }
462
463 /* copy-on-write the packet before mangling it */
464 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
465 goto tx_error_put;
466
467 if (skb_cow(skb, rt->u.dst.dev->hard_header_len))
468 goto tx_error_put;
469
470 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000471 skb_dst_drop(skb);
472 skb_dst_set(skb, &rt->u.dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200473
474 /* mangle the packet */
475 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
476 goto tx_error;
477 ipv6_hdr(skb)->daddr = cp->daddr.in6;
478
479 IP_VS_DBG_PKT(10, pp, skb, 0, "After DNAT");
480
481 /* FIXME: when application helper enlarges the packet and the length
482 is larger than the MTU of outgoing device, there will be still
483 MTU problem. */
484
485 /* Another hack: avoid icmp_send in ip_fragment */
486 skb->local_df = 1;
487
488 IP_VS_XMIT(PF_INET6, skb, rt);
489
490 LeaveFunction(10);
491 return NF_STOLEN;
492
493tx_error_icmp:
494 dst_link_failure(skb);
495tx_error:
496 LeaveFunction(10);
497 kfree_skb(skb);
498 return NF_STOLEN;
499tx_error_put:
500 dst_release(&rt->u.dst);
501 goto tx_error;
502}
503#endif
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506/*
507 * IP Tunneling transmitter
508 *
509 * This function encapsulates the packet in a new IP packet, its
510 * destination will be set to cp->daddr. Most code of this function
511 * is taken from ipip.c.
512 *
513 * It is used in VS/TUN cluster. The load balancer selects a real
514 * server from a cluster based on a scheduling algorithm,
515 * encapsulates the request packet and forwards it to the selected
516 * server. For example, all real servers are configured with
517 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
518 * the encapsulated packet, it will decapsulate the packet, processe
519 * the request and return the response packets directly to the client
520 * without passing the load balancer. This can greatly increase the
521 * scalability of virtual server.
522 *
523 * Used for ANY protocol
524 */
525int
526ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
527 struct ip_vs_protocol *pp)
528{
529 struct rtable *rt; /* Route to the other host */
530 struct net_device *tdev; /* Device to other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700531 struct iphdr *old_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 u8 tos = old_iph->tos;
Alexey Dobriyan76ab6082006-01-06 13:24:29 -0800533 __be16 df = old_iph->frag_off;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700534 sk_buff_data_t old_transport_header = skb->transport_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 struct iphdr *iph; /* Our new IP header */
Chuck Leverc2636b42007-10-23 21:07:32 -0700536 unsigned int max_headroom; /* The extra header space needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int mtu;
538
539 EnterFunction(10);
540
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900541 if (skb->protocol != htons(ETH_P_IP)) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000542 IP_VS_DBG_RL("%s(): protocol error, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 "ETH_P_IP: %d, skb protocol: %d\n",
Hannes Eder1e3e2382009-08-02 11:05:41 +0000544 __func__, htons(ETH_P_IP), skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 goto tx_error;
546 }
547
548 if (!(rt = __ip_vs_get_out_rt(cp, RT_TOS(tos))))
549 goto tx_error_icmp;
550
551 tdev = rt->u.dst.dev;
552
553 mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
554 if (mtu < 68) {
555 ip_rt_put(rt);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000556 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 goto tx_error;
558 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000559 if (skb_dst(skb))
560 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900562 df |= (old_iph->frag_off & htons(IP_DF));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900564 if ((old_iph->frag_off & htons(IP_DF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 && mtu < ntohs(old_iph->tot_len)) {
566 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
567 ip_rt_put(rt);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000568 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto tx_error;
570 }
571
572 /*
573 * Okay, now see if we can stuff it in the buffer as-is.
574 */
575 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
576
577 if (skb_headroom(skb) < max_headroom
578 || skb_cloned(skb) || skb_shared(skb)) {
579 struct sk_buff *new_skb =
580 skb_realloc_headroom(skb, max_headroom);
581 if (!new_skb) {
582 ip_rt_put(rt);
583 kfree_skb(skb);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000584 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return NF_STOLEN;
586 }
587 kfree_skb(skb);
588 skb = new_skb;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700589 old_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700592 skb->transport_header = old_transport_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* fix old IP header checksum */
595 ip_send_check(old_iph);
596
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700597 skb_push(skb, sizeof(struct iphdr));
598 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
600
601 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000602 skb_dst_drop(skb);
603 skb_dst_set(skb, &rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /*
606 * Push down and install the IPIP header.
607 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700608 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 iph->version = 4;
610 iph->ihl = sizeof(struct iphdr)>>2;
611 iph->frag_off = df;
612 iph->protocol = IPPROTO_IPIP;
613 iph->tos = tos;
614 iph->daddr = rt->rt_dst;
615 iph->saddr = rt->rt_src;
616 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 ip_select_ident(iph, &rt->u.dst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 /* Another hack: avoid icmp_send in ip_fragment */
620 skb->local_df = 1;
621
Herbert Xuc439cb22008-01-11 19:14:00 -0800622 ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 LeaveFunction(10);
625
626 return NF_STOLEN;
627
628 tx_error_icmp:
629 dst_link_failure(skb);
630 tx_error:
631 kfree_skb(skb);
632 LeaveFunction(10);
633 return NF_STOLEN;
634}
635
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200636#ifdef CONFIG_IP_VS_IPV6
637int
638ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
639 struct ip_vs_protocol *pp)
640{
641 struct rt6_info *rt; /* Route to the other host */
642 struct net_device *tdev; /* Device to other host */
643 struct ipv6hdr *old_iph = ipv6_hdr(skb);
644 sk_buff_data_t old_transport_header = skb->transport_header;
645 struct ipv6hdr *iph; /* Our new IP header */
646 unsigned int max_headroom; /* The extra header space needed */
647 int mtu;
648
649 EnterFunction(10);
650
651 if (skb->protocol != htons(ETH_P_IPV6)) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000652 IP_VS_DBG_RL("%s(): protocol error, "
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200653 "ETH_P_IPV6: %d, skb protocol: %d\n",
Hannes Eder1e3e2382009-08-02 11:05:41 +0000654 __func__, htons(ETH_P_IPV6), skb->protocol);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200655 goto tx_error;
656 }
657
658 rt = __ip_vs_get_out_rt_v6(cp);
659 if (!rt)
660 goto tx_error_icmp;
661
662 tdev = rt->u.dst.dev;
663
664 mtu = dst_mtu(&rt->u.dst) - sizeof(struct ipv6hdr);
665 /* TODO IPv6: do we need this check in IPv6? */
666 if (mtu < 1280) {
667 dst_release(&rt->u.dst);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000668 IP_VS_DBG_RL("%s(): mtu less than 1280\n", __func__);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200669 goto tx_error;
670 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000671 if (skb_dst(skb))
672 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200673
674 if (mtu < ntohs(old_iph->payload_len) + sizeof(struct ipv6hdr)) {
675 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
676 dst_release(&rt->u.dst);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000677 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200678 goto tx_error;
679 }
680
681 /*
682 * Okay, now see if we can stuff it in the buffer as-is.
683 */
684 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
685
686 if (skb_headroom(skb) < max_headroom
687 || skb_cloned(skb) || skb_shared(skb)) {
688 struct sk_buff *new_skb =
689 skb_realloc_headroom(skb, max_headroom);
690 if (!new_skb) {
691 dst_release(&rt->u.dst);
692 kfree_skb(skb);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000693 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200694 return NF_STOLEN;
695 }
696 kfree_skb(skb);
697 skb = new_skb;
698 old_iph = ipv6_hdr(skb);
699 }
700
701 skb->transport_header = old_transport_header;
702
703 skb_push(skb, sizeof(struct ipv6hdr));
704 skb_reset_network_header(skb);
705 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
706
707 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000708 skb_dst_drop(skb);
709 skb_dst_set(skb, &rt->u.dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200710
711 /*
712 * Push down and install the IPIP header.
713 */
714 iph = ipv6_hdr(skb);
715 iph->version = 6;
716 iph->nexthdr = IPPROTO_IPV6;
Harvey Harrisonb7b45f42008-11-10 16:46:06 -0800717 iph->payload_len = old_iph->payload_len;
718 be16_add_cpu(&iph->payload_len, sizeof(*old_iph));
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200719 iph->priority = old_iph->priority;
720 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
721 iph->daddr = rt->rt6i_dst.addr;
722 iph->saddr = cp->vaddr.in6; /* rt->rt6i_src.addr; */
723 iph->hop_limit = old_iph->hop_limit;
724
725 /* Another hack: avoid icmp_send in ip_fragment */
726 skb->local_df = 1;
727
728 ip6_local_out(skb);
729
730 LeaveFunction(10);
731
732 return NF_STOLEN;
733
734tx_error_icmp:
735 dst_link_failure(skb);
736tx_error:
737 kfree_skb(skb);
738 LeaveFunction(10);
739 return NF_STOLEN;
740}
741#endif
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744/*
745 * Direct Routing transmitter
746 * Used for ANY protocol
747 */
748int
749ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
750 struct ip_vs_protocol *pp)
751{
752 struct rtable *rt; /* Route to the other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700753 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 int mtu;
755
756 EnterFunction(10);
757
758 if (!(rt = __ip_vs_get_out_rt(cp, RT_TOS(iph->tos))))
759 goto tx_error_icmp;
760
761 /* MTU checking */
762 mtu = dst_mtu(&rt->u.dst);
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900763 if ((iph->frag_off & htons(IP_DF)) && skb->len > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
765 ip_rt_put(rt);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000766 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 goto tx_error;
768 }
769
770 /*
771 * Call ip_send_check because we are not sure it is called
772 * after ip_defrag. Is copy-on-write needed?
773 */
774 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
775 ip_rt_put(rt);
776 return NF_STOLEN;
777 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700778 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000781 skb_dst_drop(skb);
782 skb_dst_set(skb, &rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 /* Another hack: avoid icmp_send in ip_fragment */
785 skb->local_df = 1;
786
Julius Volz38cdcc92008-09-02 15:55:44 +0200787 IP_VS_XMIT(PF_INET, skb, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 LeaveFunction(10);
790 return NF_STOLEN;
791
792 tx_error_icmp:
793 dst_link_failure(skb);
794 tx_error:
795 kfree_skb(skb);
796 LeaveFunction(10);
797 return NF_STOLEN;
798}
799
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200800#ifdef CONFIG_IP_VS_IPV6
801int
802ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
803 struct ip_vs_protocol *pp)
804{
805 struct rt6_info *rt; /* Route to the other host */
806 int mtu;
807
808 EnterFunction(10);
809
810 rt = __ip_vs_get_out_rt_v6(cp);
811 if (!rt)
812 goto tx_error_icmp;
813
814 /* MTU checking */
815 mtu = dst_mtu(&rt->u.dst);
816 if (skb->len > mtu) {
817 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
818 dst_release(&rt->u.dst);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000819 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200820 goto tx_error;
821 }
822
823 /*
824 * Call ip_send_check because we are not sure it is called
825 * after ip_defrag. Is copy-on-write needed?
826 */
827 skb = skb_share_check(skb, GFP_ATOMIC);
828 if (unlikely(skb == NULL)) {
829 dst_release(&rt->u.dst);
830 return NF_STOLEN;
831 }
832
833 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000834 skb_dst_drop(skb);
835 skb_dst_set(skb, &rt->u.dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200836
837 /* Another hack: avoid icmp_send in ip_fragment */
838 skb->local_df = 1;
839
840 IP_VS_XMIT(PF_INET6, skb, rt);
841
842 LeaveFunction(10);
843 return NF_STOLEN;
844
845tx_error_icmp:
846 dst_link_failure(skb);
847tx_error:
848 kfree_skb(skb);
849 LeaveFunction(10);
850 return NF_STOLEN;
851}
852#endif
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855/*
856 * ICMP packet transmitter
857 * called by the ip_vs_in_icmp
858 */
859int
860ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
861 struct ip_vs_protocol *pp, int offset)
862{
863 struct rtable *rt; /* Route to the other host */
864 int mtu;
865 int rc;
866
867 EnterFunction(10);
868
869 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
870 forwarded directly here, because there is no need to
871 translate address/port back */
872 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
873 if (cp->packet_xmit)
874 rc = cp->packet_xmit(skb, cp, pp);
875 else
876 rc = NF_ACCEPT;
877 /* do not touch skb anymore */
878 atomic_inc(&cp->in_pkts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 goto out;
880 }
881
882 /*
883 * mangle and send the packet here (only for VS/NAT)
884 */
885
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700886 if (!(rt = __ip_vs_get_out_rt(cp, RT_TOS(ip_hdr(skb)->tos))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 goto tx_error_icmp;
888
889 /* MTU checking */
890 mtu = dst_mtu(&rt->u.dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700891 if ((skb->len > mtu) && (ip_hdr(skb)->frag_off & htons(IP_DF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 ip_rt_put(rt);
893 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
Hannes Eder1e3e2382009-08-02 11:05:41 +0000894 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 goto tx_error;
896 }
897
898 /* copy-on-write the packet before mangling it */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -0700899 if (!skb_make_writable(skb, offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 goto tx_error_put;
901
902 if (skb_cow(skb, rt->u.dst.dev->hard_header_len))
903 goto tx_error_put;
904
905 /* drop the old route when skb is not shared */
Eric Dumazetadf30902009-06-02 05:19:30 +0000906 skb_dst_drop(skb);
907 skb_dst_set(skb, &rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 ip_vs_nat_icmp(skb, pp, cp, 0);
910
911 /* Another hack: avoid icmp_send in ip_fragment */
912 skb->local_df = 1;
913
Julius Volz38cdcc92008-09-02 15:55:44 +0200914 IP_VS_XMIT(PF_INET, skb, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 rc = NF_STOLEN;
917 goto out;
918
919 tx_error_icmp:
920 dst_link_failure(skb);
921 tx_error:
922 dev_kfree_skb(skb);
923 rc = NF_STOLEN;
924 out:
925 LeaveFunction(10);
926 return rc;
927 tx_error_put:
928 ip_rt_put(rt);
929 goto tx_error;
930}
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200931
932#ifdef CONFIG_IP_VS_IPV6
933int
934ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
935 struct ip_vs_protocol *pp, int offset)
936{
937 struct rt6_info *rt; /* Route to the other host */
938 int mtu;
939 int rc;
940
941 EnterFunction(10);
942
943 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
944 forwarded directly here, because there is no need to
945 translate address/port back */
946 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
947 if (cp->packet_xmit)
948 rc = cp->packet_xmit(skb, cp, pp);
949 else
950 rc = NF_ACCEPT;
951 /* do not touch skb anymore */
952 atomic_inc(&cp->in_pkts);
953 goto out;
954 }
955
956 /*
957 * mangle and send the packet here (only for VS/NAT)
958 */
959
960 rt = __ip_vs_get_out_rt_v6(cp);
961 if (!rt)
962 goto tx_error_icmp;
963
964 /* MTU checking */
965 mtu = dst_mtu(&rt->u.dst);
966 if (skb->len > mtu) {
967 dst_release(&rt->u.dst);
968 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000969 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200970 goto tx_error;
971 }
972
973 /* copy-on-write the packet before mangling it */
974 if (!skb_make_writable(skb, offset))
975 goto tx_error_put;
976
977 if (skb_cow(skb, rt->u.dst.dev->hard_header_len))
978 goto tx_error_put;
979
980 /* drop the old route when skb is not shared */
Eric Dumazetadf30902009-06-02 05:19:30 +0000981 skb_dst_drop(skb);
982 skb_dst_set(skb, &rt->u.dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200983
984 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
985
986 /* Another hack: avoid icmp_send in ip_fragment */
987 skb->local_df = 1;
988
989 IP_VS_XMIT(PF_INET6, skb, rt);
990
991 rc = NF_STOLEN;
992 goto out;
993
994tx_error_icmp:
995 dst_link_failure(skb);
996tx_error:
997 dev_kfree_skb(skb);
998 rc = NF_STOLEN;
999out:
1000 LeaveFunction(10);
1001 return rc;
1002tx_error_put:
1003 dst_release(&rt->u.dst);
1004 goto tx_error;
1005}
1006#endif