blob: e90d52f199bc1d98d3cf8e2c8fe93008d60b02f6 [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
16#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/tcp.h> /* for tcphdr */
Herbert Xuc439cb22008-01-11 19:14:00 -080018#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <net/tcp.h> /* for csum_tcpudp_magic */
20#include <net/udp.h>
21#include <net/icmp.h> /* for icmp_send */
22#include <net/route.h> /* for ip_route_output */
Julius Volz38cdcc92008-09-02 15:55:44 +020023#include <net/ipv6.h>
24#include <net/ip6_route.h>
25#include <linux/icmpv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/netfilter.h>
27#include <linux/netfilter_ipv4.h>
28
29#include <net/ip_vs.h>
30
31
32/*
33 * Destination cache to speed up outgoing route lookup
34 */
35static inline void
36__ip_vs_dst_set(struct ip_vs_dest *dest, u32 rtos, struct dst_entry *dst)
37{
38 struct dst_entry *old_dst;
39
40 old_dst = dest->dst_cache;
41 dest->dst_cache = dst;
42 dest->dst_rtos = rtos;
43 dst_release(old_dst);
44}
45
46static inline struct dst_entry *
47__ip_vs_dst_check(struct ip_vs_dest *dest, u32 rtos, u32 cookie)
48{
49 struct dst_entry *dst = dest->dst_cache;
50
51 if (!dst)
52 return NULL;
Julius Volz38cdcc92008-09-02 15:55:44 +020053 if ((dst->obsolete
54 || (dest->af == AF_INET && rtos != dest->dst_rtos)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 dst->ops->check(dst, cookie) == NULL) {
56 dest->dst_cache = NULL;
57 dst_release(dst);
58 return NULL;
59 }
60 dst_hold(dst);
61 return dst;
62}
63
Ilpo Järvinenad1b30b2008-01-05 23:12:40 -080064static struct rtable *
Linus Torvalds1da177e2005-04-16 15:20:36 -070065__ip_vs_get_out_rt(struct ip_vs_conn *cp, u32 rtos)
66{
67 struct rtable *rt; /* Route to the other host */
68 struct ip_vs_dest *dest = cp->dest;
69
70 if (dest) {
71 spin_lock(&dest->dst_lock);
72 if (!(rt = (struct rtable *)
73 __ip_vs_dst_check(dest, rtos, 0))) {
74 struct flowi fl = {
75 .oif = 0,
76 .nl_u = {
77 .ip4_u = {
Julius Volze7ade462008-09-02 15:55:33 +020078 .daddr = dest->addr.ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 .saddr = 0,
80 .tos = rtos, } },
81 };
82
Denis V. Lunevf2063512008-01-22 22:07:34 -080083 if (ip_route_output_key(&init_net, &rt, &fl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 spin_unlock(&dest->dst_lock);
85 IP_VS_DBG_RL("ip_route_output error, "
86 "dest: %u.%u.%u.%u\n",
Julius Volze7ade462008-09-02 15:55:33 +020087 NIPQUAD(dest->addr.ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return NULL;
89 }
90 __ip_vs_dst_set(dest, rtos, dst_clone(&rt->u.dst));
91 IP_VS_DBG(10, "new dst %u.%u.%u.%u, refcnt=%d, rtos=%X\n",
Julius Volze7ade462008-09-02 15:55:33 +020092 NIPQUAD(dest->addr.ip),
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 atomic_read(&rt->u.dst.__refcnt), rtos);
94 }
95 spin_unlock(&dest->dst_lock);
96 } else {
97 struct flowi fl = {
98 .oif = 0,
99 .nl_u = {
100 .ip4_u = {
Julius Volze7ade462008-09-02 15:55:33 +0200101 .daddr = cp->daddr.ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .saddr = 0,
103 .tos = rtos, } },
104 };
105
Denis V. Lunevf2063512008-01-22 22:07:34 -0800106 if (ip_route_output_key(&init_net, &rt, &fl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 IP_VS_DBG_RL("ip_route_output error, dest: "
Julius Volze7ade462008-09-02 15:55:33 +0200108 "%u.%u.%u.%u\n", NIPQUAD(cp->daddr.ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return NULL;
110 }
111 }
112
113 return rt;
114}
115
Julius Volz38cdcc92008-09-02 15:55:44 +0200116#ifdef CONFIG_IP_VS_IPV6
117static struct rt6_info *
118__ip_vs_get_out_rt_v6(struct ip_vs_conn *cp)
119{
120 struct rt6_info *rt; /* Route to the other host */
121 struct ip_vs_dest *dest = cp->dest;
122
123 if (dest) {
124 spin_lock(&dest->dst_lock);
125 rt = (struct rt6_info *)__ip_vs_dst_check(dest, 0, 0);
126 if (!rt) {
127 struct flowi fl = {
128 .oif = 0,
129 .nl_u = {
130 .ip6_u = {
131 .daddr = dest->addr.in6,
132 .saddr = {
133 .s6_addr32 =
134 { 0, 0, 0, 0 },
135 },
136 },
137 },
138 };
139
140 rt = (struct rt6_info *)ip6_route_output(&init_net,
141 NULL, &fl);
142 if (!rt) {
143 spin_unlock(&dest->dst_lock);
144 IP_VS_DBG_RL("ip6_route_output error, "
145 "dest: " NIP6_FMT "\n",
146 NIP6(dest->addr.in6));
147 return NULL;
148 }
149 __ip_vs_dst_set(dest, 0, dst_clone(&rt->u.dst));
150 IP_VS_DBG(10, "new dst " NIP6_FMT ", refcnt=%d\n",
151 NIP6(dest->addr.in6),
152 atomic_read(&rt->u.dst.__refcnt));
153 }
154 spin_unlock(&dest->dst_lock);
155 } else {
156 struct flowi fl = {
157 .oif = 0,
158 .nl_u = {
159 .ip6_u = {
160 .daddr = cp->daddr.in6,
161 .saddr = {
162 .s6_addr32 = { 0, 0, 0, 0 },
163 },
164 },
165 },
166 };
167
168 rt = (struct rt6_info *)ip6_route_output(&init_net, NULL, &fl);
169 if (!rt) {
170 IP_VS_DBG_RL("ip6_route_output error, dest: "
171 NIP6_FMT "\n", NIP6(cp->daddr.in6));
172 return NULL;
173 }
174 }
175
176 return rt;
177}
178#endif
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181/*
182 * Release dest->dst_cache before a dest is removed
183 */
184void
185ip_vs_dst_reset(struct ip_vs_dest *dest)
186{
187 struct dst_entry *old_dst;
188
189 old_dst = dest->dst_cache;
190 dest->dst_cache = NULL;
191 dst_release(old_dst);
192}
193
Julius Volz38cdcc92008-09-02 15:55:44 +0200194#define IP_VS_XMIT(pf, skb, rt) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195do { \
Harald Welte6869c4d2005-08-09 19:24:19 -0700196 (skb)->ipvs_property = 1; \
Herbert Xuccc79112007-07-30 16:20:12 -0700197 skb_forward_csum(skb); \
Julius Volz38cdcc92008-09-02 15:55:44 +0200198 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 (rt)->u.dst.dev, dst_output); \
200} while (0)
201
202
203/*
204 * NULL transmitter (do nothing except return NF_ACCEPT)
205 */
206int
207ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
208 struct ip_vs_protocol *pp)
209{
210 /* we do not touch skb and do not need pskb ptr */
211 return NF_ACCEPT;
212}
213
214
215/*
216 * Bypass transmitter
217 * Let packets bypass the destination when the destination is not
218 * available, it may be only used in transparent cache cluster.
219 */
220int
221ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
222 struct ip_vs_protocol *pp)
223{
224 struct rtable *rt; /* Route to the other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700225 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 u8 tos = iph->tos;
227 int mtu;
228 struct flowi fl = {
229 .oif = 0,
230 .nl_u = {
231 .ip4_u = {
232 .daddr = iph->daddr,
233 .saddr = 0,
234 .tos = RT_TOS(tos), } },
235 };
236
237 EnterFunction(10);
238
Denis V. Lunevf2063512008-01-22 22:07:34 -0800239 if (ip_route_output_key(&init_net, &rt, &fl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 IP_VS_DBG_RL("ip_vs_bypass_xmit(): ip_route_output error, "
241 "dest: %u.%u.%u.%u\n", NIPQUAD(iph->daddr));
242 goto tx_error_icmp;
243 }
244
245 /* MTU checking */
246 mtu = dst_mtu(&rt->u.dst);
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900247 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 ip_rt_put(rt);
249 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
250 IP_VS_DBG_RL("ip_vs_bypass_xmit(): frag needed\n");
251 goto tx_error;
252 }
253
254 /*
255 * Call ip_send_check because we are not sure it is called
256 * after ip_defrag. Is copy-on-write needed?
257 */
258 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
259 ip_rt_put(rt);
260 return NF_STOLEN;
261 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700262 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /* drop old route */
265 dst_release(skb->dst);
266 skb->dst = &rt->u.dst;
267
268 /* Another hack: avoid icmp_send in ip_fragment */
269 skb->local_df = 1;
270
Julius Volz38cdcc92008-09-02 15:55:44 +0200271 IP_VS_XMIT(PF_INET, skb, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 LeaveFunction(10);
274 return NF_STOLEN;
275
276 tx_error_icmp:
277 dst_link_failure(skb);
278 tx_error:
279 kfree_skb(skb);
280 LeaveFunction(10);
281 return NF_STOLEN;
282}
283
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200284#ifdef CONFIG_IP_VS_IPV6
285int
286ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
287 struct ip_vs_protocol *pp)
288{
289 struct rt6_info *rt; /* Route to the other host */
290 struct ipv6hdr *iph = ipv6_hdr(skb);
291 int mtu;
292 struct flowi fl = {
293 .oif = 0,
294 .nl_u = {
295 .ip6_u = {
296 .daddr = iph->daddr,
297 .saddr = { .s6_addr32 = {0, 0, 0, 0} }, } },
298 };
299
300 EnterFunction(10);
301
302 rt = (struct rt6_info *)ip6_route_output(&init_net, NULL, &fl);
303 if (!rt) {
304 IP_VS_DBG_RL("ip_vs_bypass_xmit_v6(): ip6_route_output error, "
305 "dest: " NIP6_FMT "\n", NIP6(iph->daddr));
306 goto tx_error_icmp;
307 }
308
309 /* MTU checking */
310 mtu = dst_mtu(&rt->u.dst);
311 if (skb->len > mtu) {
312 dst_release(&rt->u.dst);
313 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
314 IP_VS_DBG_RL("ip_vs_bypass_xmit_v6(): frag needed\n");
315 goto tx_error;
316 }
317
318 /*
319 * Call ip_send_check because we are not sure it is called
320 * after ip_defrag. Is copy-on-write needed?
321 */
322 skb = skb_share_check(skb, GFP_ATOMIC);
323 if (unlikely(skb == NULL)) {
324 dst_release(&rt->u.dst);
325 return NF_STOLEN;
326 }
327
328 /* drop old route */
329 dst_release(skb->dst);
330 skb->dst = &rt->u.dst;
331
332 /* Another hack: avoid icmp_send in ip_fragment */
333 skb->local_df = 1;
334
335 IP_VS_XMIT(PF_INET6, skb, rt);
336
337 LeaveFunction(10);
338 return NF_STOLEN;
339
340 tx_error_icmp:
341 dst_link_failure(skb);
342 tx_error:
343 kfree_skb(skb);
344 LeaveFunction(10);
345 return NF_STOLEN;
346}
347#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349/*
350 * NAT transmitter (only for outside-to-inside nat forwarding)
351 * Not used for related ICMP
352 */
353int
354ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
355 struct ip_vs_protocol *pp)
356{
357 struct rtable *rt; /* Route to the other host */
358 int mtu;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700359 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 EnterFunction(10);
362
363 /* check if it is a connection of no-client-port */
364 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
Al Viro014d7302006-09-28 14:29:52 -0700365 __be16 _pt, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 p = skb_header_pointer(skb, iph->ihl*4, sizeof(_pt), &_pt);
367 if (p == NULL)
368 goto tx_error;
369 ip_vs_conn_fill_cport(cp, *p);
370 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
371 }
372
373 if (!(rt = __ip_vs_get_out_rt(cp, RT_TOS(iph->tos))))
374 goto tx_error_icmp;
375
376 /* MTU checking */
377 mtu = dst_mtu(&rt->u.dst);
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900378 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 ip_rt_put(rt);
380 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
381 IP_VS_DBG_RL_PKT(0, pp, skb, 0, "ip_vs_nat_xmit(): frag needed for");
382 goto tx_error;
383 }
384
385 /* copy-on-write the packet before mangling it */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -0700386 if (!skb_make_writable(skb, sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 goto tx_error_put;
388
389 if (skb_cow(skb, rt->u.dst.dev->hard_header_len))
390 goto tx_error_put;
391
392 /* drop old route */
393 dst_release(skb->dst);
394 skb->dst = &rt->u.dst;
395
396 /* mangle the packet */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700397 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 goto tx_error;
Julius Volze7ade462008-09-02 15:55:33 +0200399 ip_hdr(skb)->daddr = cp->daddr.ip;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700400 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 IP_VS_DBG_PKT(10, pp, skb, 0, "After DNAT");
403
404 /* FIXME: when application helper enlarges the packet and the length
405 is larger than the MTU of outgoing device, there will be still
406 MTU problem. */
407
408 /* Another hack: avoid icmp_send in ip_fragment */
409 skb->local_df = 1;
410
Julius Volz38cdcc92008-09-02 15:55:44 +0200411 IP_VS_XMIT(PF_INET, skb, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 LeaveFunction(10);
414 return NF_STOLEN;
415
416 tx_error_icmp:
417 dst_link_failure(skb);
418 tx_error:
419 LeaveFunction(10);
420 kfree_skb(skb);
421 return NF_STOLEN;
422 tx_error_put:
423 ip_rt_put(rt);
424 goto tx_error;
425}
426
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200427#ifdef CONFIG_IP_VS_IPV6
428int
429ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
430 struct ip_vs_protocol *pp)
431{
432 struct rt6_info *rt; /* Route to the other host */
433 int mtu;
434
435 EnterFunction(10);
436
437 /* check if it is a connection of no-client-port */
438 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
439 __be16 _pt, *p;
440 p = skb_header_pointer(skb, sizeof(struct ipv6hdr),
441 sizeof(_pt), &_pt);
442 if (p == NULL)
443 goto tx_error;
444 ip_vs_conn_fill_cport(cp, *p);
445 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
446 }
447
448 rt = __ip_vs_get_out_rt_v6(cp);
449 if (!rt)
450 goto tx_error_icmp;
451
452 /* MTU checking */
453 mtu = dst_mtu(&rt->u.dst);
454 if (skb->len > mtu) {
455 dst_release(&rt->u.dst);
456 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
457 IP_VS_DBG_RL_PKT(0, pp, skb, 0,
458 "ip_vs_nat_xmit_v6(): frag needed for");
459 goto tx_error;
460 }
461
462 /* copy-on-write the packet before mangling it */
463 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
464 goto tx_error_put;
465
466 if (skb_cow(skb, rt->u.dst.dev->hard_header_len))
467 goto tx_error_put;
468
469 /* drop old route */
470 dst_release(skb->dst);
471 skb->dst = &rt->u.dst;
472
473 /* mangle the packet */
474 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
475 goto tx_error;
476 ipv6_hdr(skb)->daddr = cp->daddr.in6;
477
478 IP_VS_DBG_PKT(10, pp, skb, 0, "After DNAT");
479
480 /* FIXME: when application helper enlarges the packet and the length
481 is larger than the MTU of outgoing device, there will be still
482 MTU problem. */
483
484 /* Another hack: avoid icmp_send in ip_fragment */
485 skb->local_df = 1;
486
487 IP_VS_XMIT(PF_INET6, skb, rt);
488
489 LeaveFunction(10);
490 return NF_STOLEN;
491
492tx_error_icmp:
493 dst_link_failure(skb);
494tx_error:
495 LeaveFunction(10);
496 kfree_skb(skb);
497 return NF_STOLEN;
498tx_error_put:
499 dst_release(&rt->u.dst);
500 goto tx_error;
501}
502#endif
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505/*
506 * IP Tunneling transmitter
507 *
508 * This function encapsulates the packet in a new IP packet, its
509 * destination will be set to cp->daddr. Most code of this function
510 * is taken from ipip.c.
511 *
512 * It is used in VS/TUN cluster. The load balancer selects a real
513 * server from a cluster based on a scheduling algorithm,
514 * encapsulates the request packet and forwards it to the selected
515 * server. For example, all real servers are configured with
516 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
517 * the encapsulated packet, it will decapsulate the packet, processe
518 * the request and return the response packets directly to the client
519 * without passing the load balancer. This can greatly increase the
520 * scalability of virtual server.
521 *
522 * Used for ANY protocol
523 */
524int
525ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
526 struct ip_vs_protocol *pp)
527{
528 struct rtable *rt; /* Route to the other host */
529 struct net_device *tdev; /* Device to other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700530 struct iphdr *old_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 u8 tos = old_iph->tos;
Alexey Dobriyan76ab6082006-01-06 13:24:29 -0800532 __be16 df = old_iph->frag_off;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700533 sk_buff_data_t old_transport_header = skb->transport_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct iphdr *iph; /* Our new IP header */
Chuck Leverc2636b42007-10-23 21:07:32 -0700535 unsigned int max_headroom; /* The extra header space needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 int mtu;
537
538 EnterFunction(10);
539
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900540 if (skb->protocol != htons(ETH_P_IP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 IP_VS_DBG_RL("ip_vs_tunnel_xmit(): protocol error, "
542 "ETH_P_IP: %d, skb protocol: %d\n",
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900543 htons(ETH_P_IP), skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 goto tx_error;
545 }
546
547 if (!(rt = __ip_vs_get_out_rt(cp, RT_TOS(tos))))
548 goto tx_error_icmp;
549
550 tdev = rt->u.dst.dev;
551
552 mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
553 if (mtu < 68) {
554 ip_rt_put(rt);
555 IP_VS_DBG_RL("ip_vs_tunnel_xmit(): mtu less than 68\n");
556 goto tx_error;
557 }
558 if (skb->dst)
559 skb->dst->ops->update_pmtu(skb->dst, mtu);
560
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900561 df |= (old_iph->frag_off & htons(IP_DF));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900563 if ((old_iph->frag_off & htons(IP_DF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 && mtu < ntohs(old_iph->tot_len)) {
565 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
566 ip_rt_put(rt);
567 IP_VS_DBG_RL("ip_vs_tunnel_xmit(): frag needed\n");
568 goto tx_error;
569 }
570
571 /*
572 * Okay, now see if we can stuff it in the buffer as-is.
573 */
574 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
575
576 if (skb_headroom(skb) < max_headroom
577 || skb_cloned(skb) || skb_shared(skb)) {
578 struct sk_buff *new_skb =
579 skb_realloc_headroom(skb, max_headroom);
580 if (!new_skb) {
581 ip_rt_put(rt);
582 kfree_skb(skb);
583 IP_VS_ERR_RL("ip_vs_tunnel_xmit(): no memory\n");
584 return NF_STOLEN;
585 }
586 kfree_skb(skb);
587 skb = new_skb;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700588 old_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700591 skb->transport_header = old_transport_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /* fix old IP header checksum */
594 ip_send_check(old_iph);
595
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700596 skb_push(skb, sizeof(struct iphdr));
597 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
599
600 /* drop old route */
601 dst_release(skb->dst);
602 skb->dst = &rt->u.dst;
603
604 /*
605 * Push down and install the IPIP header.
606 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700607 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 iph->version = 4;
609 iph->ihl = sizeof(struct iphdr)>>2;
610 iph->frag_off = df;
611 iph->protocol = IPPROTO_IPIP;
612 iph->tos = tos;
613 iph->daddr = rt->rt_dst;
614 iph->saddr = rt->rt_src;
615 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 ip_select_ident(iph, &rt->u.dst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /* Another hack: avoid icmp_send in ip_fragment */
619 skb->local_df = 1;
620
Herbert Xuc439cb22008-01-11 19:14:00 -0800621 ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 LeaveFunction(10);
624
625 return NF_STOLEN;
626
627 tx_error_icmp:
628 dst_link_failure(skb);
629 tx_error:
630 kfree_skb(skb);
631 LeaveFunction(10);
632 return NF_STOLEN;
633}
634
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200635#ifdef CONFIG_IP_VS_IPV6
636int
637ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
638 struct ip_vs_protocol *pp)
639{
640 struct rt6_info *rt; /* Route to the other host */
641 struct net_device *tdev; /* Device to other host */
642 struct ipv6hdr *old_iph = ipv6_hdr(skb);
643 sk_buff_data_t old_transport_header = skb->transport_header;
644 struct ipv6hdr *iph; /* Our new IP header */
645 unsigned int max_headroom; /* The extra header space needed */
646 int mtu;
647
648 EnterFunction(10);
649
650 if (skb->protocol != htons(ETH_P_IPV6)) {
651 IP_VS_DBG_RL("ip_vs_tunnel_xmit_v6(): protocol error, "
652 "ETH_P_IPV6: %d, skb protocol: %d\n",
653 htons(ETH_P_IPV6), skb->protocol);
654 goto tx_error;
655 }
656
657 rt = __ip_vs_get_out_rt_v6(cp);
658 if (!rt)
659 goto tx_error_icmp;
660
661 tdev = rt->u.dst.dev;
662
663 mtu = dst_mtu(&rt->u.dst) - sizeof(struct ipv6hdr);
664 /* TODO IPv6: do we need this check in IPv6? */
665 if (mtu < 1280) {
666 dst_release(&rt->u.dst);
667 IP_VS_DBG_RL("ip_vs_tunnel_xmit_v6(): mtu less than 1280\n");
668 goto tx_error;
669 }
670 if (skb->dst)
671 skb->dst->ops->update_pmtu(skb->dst, mtu);
672
673 if (mtu < ntohs(old_iph->payload_len) + sizeof(struct ipv6hdr)) {
674 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
675 dst_release(&rt->u.dst);
676 IP_VS_DBG_RL("ip_vs_tunnel_xmit_v6(): frag needed\n");
677 goto tx_error;
678 }
679
680 /*
681 * Okay, now see if we can stuff it in the buffer as-is.
682 */
683 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
684
685 if (skb_headroom(skb) < max_headroom
686 || skb_cloned(skb) || skb_shared(skb)) {
687 struct sk_buff *new_skb =
688 skb_realloc_headroom(skb, max_headroom);
689 if (!new_skb) {
690 dst_release(&rt->u.dst);
691 kfree_skb(skb);
692 IP_VS_ERR_RL("ip_vs_tunnel_xmit_v6(): no memory\n");
693 return NF_STOLEN;
694 }
695 kfree_skb(skb);
696 skb = new_skb;
697 old_iph = ipv6_hdr(skb);
698 }
699
700 skb->transport_header = old_transport_header;
701
702 skb_push(skb, sizeof(struct ipv6hdr));
703 skb_reset_network_header(skb);
704 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
705
706 /* drop old route */
707 dst_release(skb->dst);
708 skb->dst = &rt->u.dst;
709
710 /*
711 * Push down and install the IPIP header.
712 */
713 iph = ipv6_hdr(skb);
714 iph->version = 6;
715 iph->nexthdr = IPPROTO_IPV6;
Harvey Harrisonb7b45f42008-11-10 16:46:06 -0800716 iph->payload_len = old_iph->payload_len;
717 be16_add_cpu(&iph->payload_len, sizeof(*old_iph));
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200718 iph->priority = old_iph->priority;
719 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
720 iph->daddr = rt->rt6i_dst.addr;
721 iph->saddr = cp->vaddr.in6; /* rt->rt6i_src.addr; */
722 iph->hop_limit = old_iph->hop_limit;
723
724 /* Another hack: avoid icmp_send in ip_fragment */
725 skb->local_df = 1;
726
727 ip6_local_out(skb);
728
729 LeaveFunction(10);
730
731 return NF_STOLEN;
732
733tx_error_icmp:
734 dst_link_failure(skb);
735tx_error:
736 kfree_skb(skb);
737 LeaveFunction(10);
738 return NF_STOLEN;
739}
740#endif
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743/*
744 * Direct Routing transmitter
745 * Used for ANY protocol
746 */
747int
748ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
749 struct ip_vs_protocol *pp)
750{
751 struct rtable *rt; /* Route to the other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700752 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 int mtu;
754
755 EnterFunction(10);
756
757 if (!(rt = __ip_vs_get_out_rt(cp, RT_TOS(iph->tos))))
758 goto tx_error_icmp;
759
760 /* MTU checking */
761 mtu = dst_mtu(&rt->u.dst);
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900762 if ((iph->frag_off & htons(IP_DF)) && skb->len > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
764 ip_rt_put(rt);
765 IP_VS_DBG_RL("ip_vs_dr_xmit(): frag needed\n");
766 goto tx_error;
767 }
768
769 /*
770 * Call ip_send_check because we are not sure it is called
771 * after ip_defrag. Is copy-on-write needed?
772 */
773 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
774 ip_rt_put(rt);
775 return NF_STOLEN;
776 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700777 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 /* drop old route */
780 dst_release(skb->dst);
781 skb->dst = &rt->u.dst;
782
783 /* Another hack: avoid icmp_send in ip_fragment */
784 skb->local_df = 1;
785
Julius Volz38cdcc92008-09-02 15:55:44 +0200786 IP_VS_XMIT(PF_INET, skb, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 LeaveFunction(10);
789 return NF_STOLEN;
790
791 tx_error_icmp:
792 dst_link_failure(skb);
793 tx_error:
794 kfree_skb(skb);
795 LeaveFunction(10);
796 return NF_STOLEN;
797}
798
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200799#ifdef CONFIG_IP_VS_IPV6
800int
801ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
802 struct ip_vs_protocol *pp)
803{
804 struct rt6_info *rt; /* Route to the other host */
805 int mtu;
806
807 EnterFunction(10);
808
809 rt = __ip_vs_get_out_rt_v6(cp);
810 if (!rt)
811 goto tx_error_icmp;
812
813 /* MTU checking */
814 mtu = dst_mtu(&rt->u.dst);
815 if (skb->len > mtu) {
816 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
817 dst_release(&rt->u.dst);
818 IP_VS_DBG_RL("ip_vs_dr_xmit_v6(): frag needed\n");
819 goto tx_error;
820 }
821
822 /*
823 * Call ip_send_check because we are not sure it is called
824 * after ip_defrag. Is copy-on-write needed?
825 */
826 skb = skb_share_check(skb, GFP_ATOMIC);
827 if (unlikely(skb == NULL)) {
828 dst_release(&rt->u.dst);
829 return NF_STOLEN;
830 }
831
832 /* drop old route */
833 dst_release(skb->dst);
834 skb->dst = &rt->u.dst;
835
836 /* Another hack: avoid icmp_send in ip_fragment */
837 skb->local_df = 1;
838
839 IP_VS_XMIT(PF_INET6, skb, rt);
840
841 LeaveFunction(10);
842 return NF_STOLEN;
843
844tx_error_icmp:
845 dst_link_failure(skb);
846tx_error:
847 kfree_skb(skb);
848 LeaveFunction(10);
849 return NF_STOLEN;
850}
851#endif
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854/*
855 * ICMP packet transmitter
856 * called by the ip_vs_in_icmp
857 */
858int
859ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
860 struct ip_vs_protocol *pp, int offset)
861{
862 struct rtable *rt; /* Route to the other host */
863 int mtu;
864 int rc;
865
866 EnterFunction(10);
867
868 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
869 forwarded directly here, because there is no need to
870 translate address/port back */
871 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
872 if (cp->packet_xmit)
873 rc = cp->packet_xmit(skb, cp, pp);
874 else
875 rc = NF_ACCEPT;
876 /* do not touch skb anymore */
877 atomic_inc(&cp->in_pkts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 goto out;
879 }
880
881 /*
882 * mangle and send the packet here (only for VS/NAT)
883 */
884
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700885 if (!(rt = __ip_vs_get_out_rt(cp, RT_TOS(ip_hdr(skb)->tos))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 goto tx_error_icmp;
887
888 /* MTU checking */
889 mtu = dst_mtu(&rt->u.dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700890 if ((skb->len > mtu) && (ip_hdr(skb)->frag_off & htons(IP_DF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 ip_rt_put(rt);
892 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
893 IP_VS_DBG_RL("ip_vs_in_icmp(): frag needed\n");
894 goto tx_error;
895 }
896
897 /* copy-on-write the packet before mangling it */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -0700898 if (!skb_make_writable(skb, offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 goto tx_error_put;
900
901 if (skb_cow(skb, rt->u.dst.dev->hard_header_len))
902 goto tx_error_put;
903
904 /* drop the old route when skb is not shared */
905 dst_release(skb->dst);
906 skb->dst = &rt->u.dst;
907
908 ip_vs_nat_icmp(skb, pp, cp, 0);
909
910 /* Another hack: avoid icmp_send in ip_fragment */
911 skb->local_df = 1;
912
Julius Volz38cdcc92008-09-02 15:55:44 +0200913 IP_VS_XMIT(PF_INET, skb, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 rc = NF_STOLEN;
916 goto out;
917
918 tx_error_icmp:
919 dst_link_failure(skb);
920 tx_error:
921 dev_kfree_skb(skb);
922 rc = NF_STOLEN;
923 out:
924 LeaveFunction(10);
925 return rc;
926 tx_error_put:
927 ip_rt_put(rt);
928 goto tx_error;
929}
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200930
931#ifdef CONFIG_IP_VS_IPV6
932int
933ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
934 struct ip_vs_protocol *pp, int offset)
935{
936 struct rt6_info *rt; /* Route to the other host */
937 int mtu;
938 int rc;
939
940 EnterFunction(10);
941
942 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
943 forwarded directly here, because there is no need to
944 translate address/port back */
945 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
946 if (cp->packet_xmit)
947 rc = cp->packet_xmit(skb, cp, pp);
948 else
949 rc = NF_ACCEPT;
950 /* do not touch skb anymore */
951 atomic_inc(&cp->in_pkts);
952 goto out;
953 }
954
955 /*
956 * mangle and send the packet here (only for VS/NAT)
957 */
958
959 rt = __ip_vs_get_out_rt_v6(cp);
960 if (!rt)
961 goto tx_error_icmp;
962
963 /* MTU checking */
964 mtu = dst_mtu(&rt->u.dst);
965 if (skb->len > mtu) {
966 dst_release(&rt->u.dst);
967 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
968 IP_VS_DBG_RL("ip_vs_in_icmp(): frag needed\n");
969 goto tx_error;
970 }
971
972 /* copy-on-write the packet before mangling it */
973 if (!skb_make_writable(skb, offset))
974 goto tx_error_put;
975
976 if (skb_cow(skb, rt->u.dst.dev->hard_header_len))
977 goto tx_error_put;
978
979 /* drop the old route when skb is not shared */
980 dst_release(skb->dst);
981 skb->dst = &rt->u.dst;
982
983 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
984
985 /* Another hack: avoid icmp_send in ip_fragment */
986 skb->local_df = 1;
987
988 IP_VS_XMIT(PF_INET6, skb, rt);
989
990 rc = NF_STOLEN;
991 goto out;
992
993tx_error_icmp:
994 dst_link_failure(skb);
995tx_error:
996 dev_kfree_skb(skb);
997 rc = NF_STOLEN;
998out:
999 LeaveFunction(10);
1000 return rc;
1001tx_error_put:
1002 dst_release(&rt->u.dst);
1003 goto tx_error;
1004}
1005#endif