]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * ip_vs_xmit.c: various packet transmitters for IPVS | |
3 | * | |
1da177e4 LT |
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 | ||
9aada7ac HE |
16 | #define KMSG_COMPONENT "IPVS" |
17 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
18 | ||
1da177e4 | 19 | #include <linux/kernel.h> |
5a0e3ad6 | 20 | #include <linux/slab.h> |
1da177e4 | 21 | #include <linux/tcp.h> /* for tcphdr */ |
c439cb2e | 22 | #include <net/ip.h> |
1da177e4 LT |
23 | #include <net/tcp.h> /* for csum_tcpudp_magic */ |
24 | #include <net/udp.h> | |
25 | #include <net/icmp.h> /* for icmp_send */ | |
26 | #include <net/route.h> /* for ip_route_output */ | |
38cdcc9a JV |
27 | #include <net/ipv6.h> |
28 | #include <net/ip6_route.h> | |
714f095f | 29 | #include <net/addrconf.h> |
38cdcc9a | 30 | #include <linux/icmpv6.h> |
1da177e4 LT |
31 | #include <linux/netfilter.h> |
32 | #include <linux/netfilter_ipv4.h> | |
33 | ||
34 | #include <net/ip_vs.h> | |
35 | ||
36 | ||
37 | /* | |
38 | * Destination cache to speed up outgoing route lookup | |
39 | */ | |
40 | static inline void | |
714f095f HS |
41 | __ip_vs_dst_set(struct ip_vs_dest *dest, u32 rtos, struct dst_entry *dst, |
42 | u32 dst_cookie) | |
1da177e4 LT |
43 | { |
44 | struct dst_entry *old_dst; | |
45 | ||
46 | old_dst = dest->dst_cache; | |
47 | dest->dst_cache = dst; | |
48 | dest->dst_rtos = rtos; | |
714f095f | 49 | dest->dst_cookie = dst_cookie; |
1da177e4 LT |
50 | dst_release(old_dst); |
51 | } | |
52 | ||
53 | static inline struct dst_entry * | |
714f095f | 54 | __ip_vs_dst_check(struct ip_vs_dest *dest, u32 rtos) |
1da177e4 LT |
55 | { |
56 | struct dst_entry *dst = dest->dst_cache; | |
57 | ||
58 | if (!dst) | |
59 | return NULL; | |
714f095f HS |
60 | if ((dst->obsolete || rtos != dest->dst_rtos) && |
61 | dst->ops->check(dst, dest->dst_cookie) == NULL) { | |
1da177e4 LT |
62 | dest->dst_cache = NULL; |
63 | dst_release(dst); | |
64 | return NULL; | |
65 | } | |
66 | dst_hold(dst); | |
67 | return dst; | |
68 | } | |
69 | ||
fc604767 JA |
70 | /* |
71 | * Get route to destination or remote server | |
72 | * rt_mode: flags, &1=Allow local dest, &2=Allow non-local dest, | |
73 | * &4=Allow redirect from remote daddr to local | |
74 | */ | |
ad1b30b1 | 75 | static struct rtable * |
fc604767 JA |
76 | __ip_vs_get_out_rt(struct sk_buff *skb, struct ip_vs_dest *dest, |
77 | __be32 daddr, u32 rtos, int rt_mode) | |
1da177e4 | 78 | { |
fc604767 | 79 | struct net *net = dev_net(skb_dst(skb)->dev); |
1da177e4 | 80 | struct rtable *rt; /* Route to the other host */ |
fc604767 JA |
81 | struct rtable *ort; /* Original route */ |
82 | int local; | |
1da177e4 LT |
83 | |
84 | if (dest) { | |
85 | spin_lock(&dest->dst_lock); | |
86 | if (!(rt = (struct rtable *) | |
714f095f | 87 | __ip_vs_dst_check(dest, rtos))) { |
1da177e4 LT |
88 | struct flowi fl = { |
89 | .oif = 0, | |
90 | .nl_u = { | |
91 | .ip4_u = { | |
e7ade46a | 92 | .daddr = dest->addr.ip, |
1da177e4 LT |
93 | .saddr = 0, |
94 | .tos = rtos, } }, | |
95 | }; | |
96 | ||
714f095f | 97 | if (ip_route_output_key(net, &rt, &fl)) { |
1da177e4 | 98 | spin_unlock(&dest->dst_lock); |
14d5e834 HH |
99 | IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n", |
100 | &dest->addr.ip); | |
1da177e4 LT |
101 | return NULL; |
102 | } | |
714f095f | 103 | __ip_vs_dst_set(dest, rtos, dst_clone(&rt->dst), 0); |
14d5e834 HH |
104 | IP_VS_DBG(10, "new dst %pI4, refcnt=%d, rtos=%X\n", |
105 | &dest->addr.ip, | |
d8d1f30b | 106 | atomic_read(&rt->dst.__refcnt), rtos); |
1da177e4 LT |
107 | } |
108 | spin_unlock(&dest->dst_lock); | |
109 | } else { | |
110 | struct flowi fl = { | |
111 | .oif = 0, | |
112 | .nl_u = { | |
113 | .ip4_u = { | |
fc604767 | 114 | .daddr = daddr, |
1da177e4 LT |
115 | .saddr = 0, |
116 | .tos = rtos, } }, | |
117 | }; | |
118 | ||
714f095f | 119 | if (ip_route_output_key(net, &rt, &fl)) { |
14d5e834 | 120 | IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n", |
fc604767 | 121 | &daddr); |
1da177e4 LT |
122 | return NULL; |
123 | } | |
124 | } | |
125 | ||
fc604767 JA |
126 | local = rt->rt_flags & RTCF_LOCAL; |
127 | if (!((local ? 1 : 2) & rt_mode)) { | |
128 | IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI4\n", | |
129 | (rt->rt_flags & RTCF_LOCAL) ? | |
130 | "local":"non-local", &rt->rt_dst); | |
131 | ip_rt_put(rt); | |
132 | return NULL; | |
133 | } | |
134 | if (local && !(rt_mode & 4) && !((ort = skb_rtable(skb)) && | |
135 | ort->rt_flags & RTCF_LOCAL)) { | |
136 | IP_VS_DBG_RL("Redirect from non-local address %pI4 to local " | |
137 | "requires NAT method, dest: %pI4\n", | |
138 | &ip_hdr(skb)->daddr, &rt->rt_dst); | |
139 | ip_rt_put(rt); | |
140 | return NULL; | |
141 | } | |
142 | if (unlikely(!local && ipv4_is_loopback(ip_hdr(skb)->saddr))) { | |
143 | IP_VS_DBG_RL("Stopping traffic from loopback address %pI4 " | |
144 | "to non-local address, dest: %pI4\n", | |
145 | &ip_hdr(skb)->saddr, &rt->rt_dst); | |
146 | ip_rt_put(rt); | |
147 | return NULL; | |
148 | } | |
149 | ||
1da177e4 LT |
150 | return rt; |
151 | } | |
152 | ||
fc604767 JA |
153 | /* Reroute packet to local IPv4 stack after DNAT */ |
154 | static int | |
155 | __ip_vs_reroute_locally(struct sk_buff *skb) | |
156 | { | |
157 | struct rtable *rt = skb_rtable(skb); | |
158 | struct net_device *dev = rt->dst.dev; | |
159 | struct net *net = dev_net(dev); | |
160 | struct iphdr *iph = ip_hdr(skb); | |
161 | ||
162 | if (rt->fl.iif) { | |
163 | unsigned long orefdst = skb->_skb_refdst; | |
164 | ||
165 | if (ip_route_input(skb, iph->daddr, iph->saddr, | |
166 | iph->tos, skb->dev)) | |
167 | return 0; | |
168 | refdst_drop(orefdst); | |
169 | } else { | |
170 | struct flowi fl = { | |
171 | .oif = 0, | |
172 | .nl_u = { | |
173 | .ip4_u = { | |
174 | .daddr = iph->daddr, | |
175 | .saddr = iph->saddr, | |
176 | .tos = RT_TOS(iph->tos), | |
177 | } | |
178 | }, | |
179 | .mark = skb->mark, | |
180 | }; | |
181 | struct rtable *rt; | |
182 | ||
183 | if (ip_route_output_key(net, &rt, &fl)) | |
184 | return 0; | |
185 | if (!(rt->rt_flags & RTCF_LOCAL)) { | |
186 | ip_rt_put(rt); | |
187 | return 0; | |
188 | } | |
189 | /* Drop old route. */ | |
190 | skb_dst_drop(skb); | |
191 | skb_dst_set(skb, &rt->dst); | |
192 | } | |
193 | return 1; | |
194 | } | |
195 | ||
38cdcc9a | 196 | #ifdef CONFIG_IP_VS_IPV6 |
714f095f | 197 | |
fc604767 JA |
198 | static inline int __ip_vs_is_local_route6(struct rt6_info *rt) |
199 | { | |
200 | return rt->rt6i_dev && rt->rt6i_dev->flags & IFF_LOOPBACK; | |
201 | } | |
202 | ||
714f095f HS |
203 | static struct dst_entry * |
204 | __ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr, | |
205 | struct in6_addr *ret_saddr, int do_xfrm) | |
206 | { | |
207 | struct dst_entry *dst; | |
208 | struct flowi fl = { | |
209 | .oif = 0, | |
210 | .nl_u = { | |
211 | .ip6_u = { | |
212 | .daddr = *daddr, | |
213 | }, | |
214 | }, | |
215 | }; | |
216 | ||
217 | dst = ip6_route_output(net, NULL, &fl); | |
218 | if (dst->error) | |
219 | goto out_err; | |
220 | if (!ret_saddr) | |
221 | return dst; | |
222 | if (ipv6_addr_any(&fl.fl6_src) && | |
223 | ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev, | |
224 | &fl.fl6_dst, 0, &fl.fl6_src) < 0) | |
225 | goto out_err; | |
226 | if (do_xfrm && xfrm_lookup(net, &dst, &fl, NULL, 0) < 0) | |
227 | goto out_err; | |
228 | ipv6_addr_copy(ret_saddr, &fl.fl6_src); | |
229 | return dst; | |
230 | ||
231 | out_err: | |
232 | dst_release(dst); | |
233 | IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr); | |
234 | return NULL; | |
235 | } | |
236 | ||
fc604767 JA |
237 | /* |
238 | * Get route to destination or remote server | |
239 | * rt_mode: flags, &1=Allow local dest, &2=Allow non-local dest, | |
240 | * &4=Allow redirect from remote daddr to local | |
241 | */ | |
38cdcc9a | 242 | static struct rt6_info * |
fc604767 JA |
243 | __ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest, |
244 | struct in6_addr *daddr, struct in6_addr *ret_saddr, | |
245 | int do_xfrm, int rt_mode) | |
38cdcc9a | 246 | { |
fc604767 | 247 | struct net *net = dev_net(skb_dst(skb)->dev); |
38cdcc9a | 248 | struct rt6_info *rt; /* Route to the other host */ |
fc604767 | 249 | struct rt6_info *ort; /* Original route */ |
714f095f | 250 | struct dst_entry *dst; |
fc604767 | 251 | int local; |
38cdcc9a JV |
252 | |
253 | if (dest) { | |
254 | spin_lock(&dest->dst_lock); | |
714f095f | 255 | rt = (struct rt6_info *)__ip_vs_dst_check(dest, 0); |
38cdcc9a | 256 | if (!rt) { |
714f095f | 257 | u32 cookie; |
38cdcc9a | 258 | |
714f095f HS |
259 | dst = __ip_vs_route_output_v6(net, &dest->addr.in6, |
260 | &dest->dst_saddr, | |
261 | do_xfrm); | |
262 | if (!dst) { | |
38cdcc9a | 263 | spin_unlock(&dest->dst_lock); |
38cdcc9a JV |
264 | return NULL; |
265 | } | |
714f095f HS |
266 | rt = (struct rt6_info *) dst; |
267 | cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0; | |
268 | __ip_vs_dst_set(dest, 0, dst_clone(&rt->dst), cookie); | |
269 | IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n", | |
270 | &dest->addr.in6, &dest->dst_saddr, | |
d8d1f30b | 271 | atomic_read(&rt->dst.__refcnt)); |
38cdcc9a | 272 | } |
714f095f HS |
273 | if (ret_saddr) |
274 | ipv6_addr_copy(ret_saddr, &dest->dst_saddr); | |
38cdcc9a JV |
275 | spin_unlock(&dest->dst_lock); |
276 | } else { | |
fc604767 | 277 | dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm); |
714f095f | 278 | if (!dst) |
38cdcc9a | 279 | return NULL; |
714f095f | 280 | rt = (struct rt6_info *) dst; |
38cdcc9a JV |
281 | } |
282 | ||
fc604767 JA |
283 | local = __ip_vs_is_local_route6(rt); |
284 | if (!((local ? 1 : 2) & rt_mode)) { | |
285 | IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI6\n", | |
286 | local ? "local":"non-local", daddr); | |
287 | dst_release(&rt->dst); | |
288 | return NULL; | |
289 | } | |
290 | if (local && !(rt_mode & 4) && | |
291 | !((ort = (struct rt6_info *) skb_dst(skb)) && | |
292 | __ip_vs_is_local_route6(ort))) { | |
293 | IP_VS_DBG_RL("Redirect from non-local address %pI6 to local " | |
294 | "requires NAT method, dest: %pI6\n", | |
295 | &ipv6_hdr(skb)->daddr, daddr); | |
296 | dst_release(&rt->dst); | |
297 | return NULL; | |
298 | } | |
299 | if (unlikely(!local && (!skb->dev || skb->dev->flags & IFF_LOOPBACK) && | |
300 | ipv6_addr_type(&ipv6_hdr(skb)->saddr) & | |
301 | IPV6_ADDR_LOOPBACK)) { | |
302 | IP_VS_DBG_RL("Stopping traffic from loopback address %pI6 " | |
303 | "to non-local address, dest: %pI6\n", | |
304 | &ipv6_hdr(skb)->saddr, daddr); | |
305 | dst_release(&rt->dst); | |
306 | return NULL; | |
307 | } | |
308 | ||
38cdcc9a JV |
309 | return rt; |
310 | } | |
311 | #endif | |
312 | ||
1da177e4 LT |
313 | |
314 | /* | |
315 | * Release dest->dst_cache before a dest is removed | |
316 | */ | |
317 | void | |
318 | ip_vs_dst_reset(struct ip_vs_dest *dest) | |
319 | { | |
320 | struct dst_entry *old_dst; | |
321 | ||
322 | old_dst = dest->dst_cache; | |
323 | dest->dst_cache = NULL; | |
324 | dst_release(old_dst); | |
325 | } | |
326 | ||
f4bc17cd JA |
327 | #define IP_VS_XMIT_TUNNEL(skb, cp) \ |
328 | ({ \ | |
329 | int __ret = NF_ACCEPT; \ | |
330 | \ | |
cf356d69 | 331 | (skb)->ipvs_property = 1; \ |
f4bc17cd JA |
332 | if (unlikely((cp)->flags & IP_VS_CONN_F_NFCT)) \ |
333 | __ret = ip_vs_confirm_conntrack(skb, cp); \ | |
334 | if (__ret == NF_ACCEPT) { \ | |
335 | nf_reset(skb); \ | |
4256f1aa | 336 | skb_forward_csum(skb); \ |
f4bc17cd JA |
337 | } \ |
338 | __ret; \ | |
339 | }) | |
340 | ||
fc604767 | 341 | #define IP_VS_XMIT_NAT(pf, skb, cp, local) \ |
1da177e4 | 342 | do { \ |
cf356d69 | 343 | (skb)->ipvs_property = 1; \ |
f4bc17cd | 344 | if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \ |
cf356d69 | 345 | ip_vs_notrack(skb); \ |
f4bc17cd JA |
346 | else \ |
347 | ip_vs_update_conntrack(skb, cp, 1); \ | |
fc604767 JA |
348 | if (local) \ |
349 | return NF_ACCEPT; \ | |
ccc7911f | 350 | skb_forward_csum(skb); \ |
38cdcc9a | 351 | NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \ |
f4bc17cd JA |
352 | skb_dst(skb)->dev, dst_output); \ |
353 | } while (0) | |
354 | ||
fc604767 | 355 | #define IP_VS_XMIT(pf, skb, cp, local) \ |
f4bc17cd | 356 | do { \ |
cf356d69 | 357 | (skb)->ipvs_property = 1; \ |
f4bc17cd | 358 | if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \ |
cf356d69 | 359 | ip_vs_notrack(skb); \ |
fc604767 JA |
360 | if (local) \ |
361 | return NF_ACCEPT; \ | |
f4bc17cd JA |
362 | skb_forward_csum(skb); \ |
363 | NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \ | |
364 | skb_dst(skb)->dev, dst_output); \ | |
1da177e4 LT |
365 | } while (0) |
366 | ||
367 | ||
368 | /* | |
369 | * NULL transmitter (do nothing except return NF_ACCEPT) | |
370 | */ | |
371 | int | |
372 | ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, | |
373 | struct ip_vs_protocol *pp) | |
374 | { | |
375 | /* we do not touch skb and do not need pskb ptr */ | |
fc604767 | 376 | IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1); |
1da177e4 LT |
377 | } |
378 | ||
379 | ||
380 | /* | |
381 | * Bypass transmitter | |
382 | * Let packets bypass the destination when the destination is not | |
383 | * available, it may be only used in transparent cache cluster. | |
384 | */ | |
385 | int | |
386 | ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, | |
387 | struct ip_vs_protocol *pp) | |
388 | { | |
389 | struct rtable *rt; /* Route to the other host */ | |
eddc9ec5 | 390 | struct iphdr *iph = ip_hdr(skb); |
1da177e4 | 391 | int mtu; |
1da177e4 LT |
392 | |
393 | EnterFunction(10); | |
394 | ||
fc604767 JA |
395 | if (!(rt = __ip_vs_get_out_rt(skb, NULL, iph->daddr, |
396 | RT_TOS(iph->tos), 2))) | |
1da177e4 | 397 | goto tx_error_icmp; |
1da177e4 LT |
398 | |
399 | /* MTU checking */ | |
d8d1f30b | 400 | mtu = dst_mtu(&rt->dst); |
4412ec49 | 401 | if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF))) { |
1da177e4 LT |
402 | ip_rt_put(rt); |
403 | icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu)); | |
1e3e238e | 404 | IP_VS_DBG_RL("%s(): frag needed\n", __func__); |
1da177e4 LT |
405 | goto tx_error; |
406 | } | |
407 | ||
408 | /* | |
409 | * Call ip_send_check because we are not sure it is called | |
410 | * after ip_defrag. Is copy-on-write needed? | |
411 | */ | |
412 | if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) { | |
413 | ip_rt_put(rt); | |
414 | return NF_STOLEN; | |
415 | } | |
eddc9ec5 | 416 | ip_send_check(ip_hdr(skb)); |
1da177e4 LT |
417 | |
418 | /* drop old route */ | |
adf30907 | 419 | skb_dst_drop(skb); |
d8d1f30b | 420 | skb_dst_set(skb, &rt->dst); |
1da177e4 LT |
421 | |
422 | /* Another hack: avoid icmp_send in ip_fragment */ | |
423 | skb->local_df = 1; | |
424 | ||
fc604767 | 425 | IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0); |
1da177e4 LT |
426 | |
427 | LeaveFunction(10); | |
428 | return NF_STOLEN; | |
429 | ||
430 | tx_error_icmp: | |
431 | dst_link_failure(skb); | |
432 | tx_error: | |
433 | kfree_skb(skb); | |
434 | LeaveFunction(10); | |
435 | return NF_STOLEN; | |
436 | } | |
437 | ||
b3cdd2a7 JV |
438 | #ifdef CONFIG_IP_VS_IPV6 |
439 | int | |
440 | ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, | |
441 | struct ip_vs_protocol *pp) | |
442 | { | |
443 | struct rt6_info *rt; /* Route to the other host */ | |
444 | struct ipv6hdr *iph = ipv6_hdr(skb); | |
445 | int mtu; | |
b3cdd2a7 JV |
446 | |
447 | EnterFunction(10); | |
448 | ||
fc604767 | 449 | if (!(rt = __ip_vs_get_out_rt_v6(skb, NULL, &iph->daddr, NULL, 0, 2))) |
b3cdd2a7 | 450 | goto tx_error_icmp; |
b3cdd2a7 JV |
451 | |
452 | /* MTU checking */ | |
d8d1f30b | 453 | mtu = dst_mtu(&rt->dst); |
b3cdd2a7 | 454 | if (skb->len > mtu) { |
d8d1f30b | 455 | dst_release(&rt->dst); |
3ffe533c | 456 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
1e3e238e | 457 | IP_VS_DBG_RL("%s(): frag needed\n", __func__); |
b3cdd2a7 JV |
458 | goto tx_error; |
459 | } | |
460 | ||
461 | /* | |
462 | * Call ip_send_check because we are not sure it is called | |
463 | * after ip_defrag. Is copy-on-write needed? | |
464 | */ | |
465 | skb = skb_share_check(skb, GFP_ATOMIC); | |
466 | if (unlikely(skb == NULL)) { | |
d8d1f30b | 467 | dst_release(&rt->dst); |
b3cdd2a7 JV |
468 | return NF_STOLEN; |
469 | } | |
470 | ||
471 | /* drop old route */ | |
adf30907 | 472 | skb_dst_drop(skb); |
d8d1f30b | 473 | skb_dst_set(skb, &rt->dst); |
b3cdd2a7 JV |
474 | |
475 | /* Another hack: avoid icmp_send in ip_fragment */ | |
476 | skb->local_df = 1; | |
477 | ||
fc604767 | 478 | IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0); |
b3cdd2a7 JV |
479 | |
480 | LeaveFunction(10); | |
481 | return NF_STOLEN; | |
482 | ||
483 | tx_error_icmp: | |
484 | dst_link_failure(skb); | |
485 | tx_error: | |
486 | kfree_skb(skb); | |
487 | LeaveFunction(10); | |
488 | return NF_STOLEN; | |
489 | } | |
490 | #endif | |
1da177e4 LT |
491 | |
492 | /* | |
493 | * NAT transmitter (only for outside-to-inside nat forwarding) | |
494 | * Not used for related ICMP | |
495 | */ | |
496 | int | |
497 | ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, | |
498 | struct ip_vs_protocol *pp) | |
499 | { | |
500 | struct rtable *rt; /* Route to the other host */ | |
501 | int mtu; | |
eddc9ec5 | 502 | struct iphdr *iph = ip_hdr(skb); |
fc604767 | 503 | int local; |
1da177e4 LT |
504 | |
505 | EnterFunction(10); | |
506 | ||
507 | /* check if it is a connection of no-client-port */ | |
508 | if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) { | |
014d730d | 509 | __be16 _pt, *p; |
1da177e4 LT |
510 | p = skb_header_pointer(skb, iph->ihl*4, sizeof(_pt), &_pt); |
511 | if (p == NULL) | |
512 | goto tx_error; | |
513 | ip_vs_conn_fill_cport(cp, *p); | |
514 | IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p)); | |
515 | } | |
516 | ||
fc604767 JA |
517 | if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip, |
518 | RT_TOS(iph->tos), 1|2|4))) | |
1da177e4 | 519 | goto tx_error_icmp; |
fc604767 JA |
520 | local = rt->rt_flags & RTCF_LOCAL; |
521 | /* | |
522 | * Avoid duplicate tuple in reply direction for NAT traffic | |
523 | * to local address when connection is sync-ed | |
524 | */ | |
525 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) | |
526 | if (cp->flags & IP_VS_CONN_F_SYNC && local) { | |
527 | enum ip_conntrack_info ctinfo; | |
528 | struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo); | |
529 | ||
530 | if (ct && !nf_ct_is_untracked(ct)) { | |
531 | IP_VS_DBG_RL_PKT(10, pp, skb, 0, "ip_vs_nat_xmit(): " | |
532 | "stopping DNAT to local address"); | |
533 | goto tx_error_put; | |
534 | } | |
535 | } | |
536 | #endif | |
537 | ||
538 | /* From world but DNAT to loopback address? */ | |
539 | if (local && ipv4_is_loopback(rt->rt_dst) && skb_rtable(skb)->fl.iif) { | |
540 | IP_VS_DBG_RL_PKT(1, pp, skb, 0, "ip_vs_nat_xmit(): " | |
541 | "stopping DNAT to loopback address"); | |
542 | goto tx_error_put; | |
543 | } | |
1da177e4 LT |
544 | |
545 | /* MTU checking */ | |
d8d1f30b | 546 | mtu = dst_mtu(&rt->dst); |
4412ec49 | 547 | if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF))) { |
1da177e4 LT |
548 | icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu)); |
549 | IP_VS_DBG_RL_PKT(0, pp, skb, 0, "ip_vs_nat_xmit(): frag needed for"); | |
fc604767 | 550 | goto tx_error_put; |
1da177e4 LT |
551 | } |
552 | ||
553 | /* copy-on-write the packet before mangling it */ | |
af1e1cf0 | 554 | if (!skb_make_writable(skb, sizeof(struct iphdr))) |
1da177e4 LT |
555 | goto tx_error_put; |
556 | ||
d8d1f30b | 557 | if (skb_cow(skb, rt->dst.dev->hard_header_len)) |
1da177e4 LT |
558 | goto tx_error_put; |
559 | ||
1da177e4 | 560 | /* mangle the packet */ |
3db05fea | 561 | if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp)) |
fc604767 | 562 | goto tx_error_put; |
e7ade46a | 563 | ip_hdr(skb)->daddr = cp->daddr.ip; |
eddc9ec5 | 564 | ip_send_check(ip_hdr(skb)); |
1da177e4 | 565 | |
fc604767 JA |
566 | if (!local) { |
567 | /* drop old route */ | |
568 | skb_dst_drop(skb); | |
569 | skb_dst_set(skb, &rt->dst); | |
570 | } else { | |
571 | ip_rt_put(rt); | |
572 | /* | |
573 | * Some IPv4 replies get local address from routes, | |
574 | * not from iph, so while we DNAT after routing | |
575 | * we need this second input/output route. | |
576 | */ | |
577 | if (!__ip_vs_reroute_locally(skb)) | |
578 | goto tx_error; | |
579 | } | |
580 | ||
1da177e4 LT |
581 | IP_VS_DBG_PKT(10, pp, skb, 0, "After DNAT"); |
582 | ||
583 | /* FIXME: when application helper enlarges the packet and the length | |
584 | is larger than the MTU of outgoing device, there will be still | |
585 | MTU problem. */ | |
586 | ||
587 | /* Another hack: avoid icmp_send in ip_fragment */ | |
588 | skb->local_df = 1; | |
589 | ||
fc604767 | 590 | IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local); |
1da177e4 LT |
591 | |
592 | LeaveFunction(10); | |
593 | return NF_STOLEN; | |
594 | ||
595 | tx_error_icmp: | |
596 | dst_link_failure(skb); | |
597 | tx_error: | |
1da177e4 | 598 | kfree_skb(skb); |
f4bc17cd | 599 | LeaveFunction(10); |
1da177e4 LT |
600 | return NF_STOLEN; |
601 | tx_error_put: | |
602 | ip_rt_put(rt); | |
603 | goto tx_error; | |
604 | } | |
605 | ||
b3cdd2a7 JV |
606 | #ifdef CONFIG_IP_VS_IPV6 |
607 | int | |
608 | ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, | |
609 | struct ip_vs_protocol *pp) | |
610 | { | |
611 | struct rt6_info *rt; /* Route to the other host */ | |
612 | int mtu; | |
fc604767 | 613 | int local; |
b3cdd2a7 JV |
614 | |
615 | EnterFunction(10); | |
616 | ||
617 | /* check if it is a connection of no-client-port */ | |
618 | if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) { | |
619 | __be16 _pt, *p; | |
620 | p = skb_header_pointer(skb, sizeof(struct ipv6hdr), | |
621 | sizeof(_pt), &_pt); | |
622 | if (p == NULL) | |
623 | goto tx_error; | |
624 | ip_vs_conn_fill_cport(cp, *p); | |
625 | IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p)); | |
626 | } | |
627 | ||
fc604767 JA |
628 | if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL, |
629 | 0, 1|2|4))) | |
b3cdd2a7 | 630 | goto tx_error_icmp; |
fc604767 JA |
631 | local = __ip_vs_is_local_route6(rt); |
632 | /* | |
633 | * Avoid duplicate tuple in reply direction for NAT traffic | |
634 | * to local address when connection is sync-ed | |
635 | */ | |
636 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) | |
637 | if (cp->flags & IP_VS_CONN_F_SYNC && local) { | |
638 | enum ip_conntrack_info ctinfo; | |
639 | struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo); | |
640 | ||
641 | if (ct && !nf_ct_is_untracked(ct)) { | |
642 | IP_VS_DBG_RL_PKT(10, pp, skb, 0, | |
643 | "ip_vs_nat_xmit_v6(): " | |
644 | "stopping DNAT to local address"); | |
645 | goto tx_error_put; | |
646 | } | |
647 | } | |
648 | #endif | |
649 | ||
650 | /* From world but DNAT to loopback address? */ | |
651 | if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) && | |
652 | ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) { | |
653 | IP_VS_DBG_RL_PKT(1, pp, skb, 0, | |
654 | "ip_vs_nat_xmit_v6(): " | |
655 | "stopping DNAT to loopback address"); | |
656 | goto tx_error_put; | |
657 | } | |
b3cdd2a7 JV |
658 | |
659 | /* MTU checking */ | |
d8d1f30b | 660 | mtu = dst_mtu(&rt->dst); |
b3cdd2a7 | 661 | if (skb->len > mtu) { |
3ffe533c | 662 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
b3cdd2a7 JV |
663 | IP_VS_DBG_RL_PKT(0, pp, skb, 0, |
664 | "ip_vs_nat_xmit_v6(): frag needed for"); | |
fc604767 | 665 | goto tx_error_put; |
b3cdd2a7 JV |
666 | } |
667 | ||
668 | /* copy-on-write the packet before mangling it */ | |
669 | if (!skb_make_writable(skb, sizeof(struct ipv6hdr))) | |
670 | goto tx_error_put; | |
671 | ||
d8d1f30b | 672 | if (skb_cow(skb, rt->dst.dev->hard_header_len)) |
b3cdd2a7 JV |
673 | goto tx_error_put; |
674 | ||
b3cdd2a7 JV |
675 | /* mangle the packet */ |
676 | if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp)) | |
677 | goto tx_error; | |
fc604767 JA |
678 | ipv6_addr_copy(&ipv6_hdr(skb)->daddr, &cp->daddr.in6); |
679 | ||
680 | if (!local || !skb->dev) { | |
681 | /* drop the old route when skb is not shared */ | |
682 | skb_dst_drop(skb); | |
683 | skb_dst_set(skb, &rt->dst); | |
684 | } else { | |
685 | /* destined to loopback, do we need to change route? */ | |
686 | dst_release(&rt->dst); | |
687 | } | |
b3cdd2a7 JV |
688 | |
689 | IP_VS_DBG_PKT(10, pp, skb, 0, "After DNAT"); | |
690 | ||
691 | /* FIXME: when application helper enlarges the packet and the length | |
692 | is larger than the MTU of outgoing device, there will be still | |
693 | MTU problem. */ | |
694 | ||
695 | /* Another hack: avoid icmp_send in ip_fragment */ | |
696 | skb->local_df = 1; | |
697 | ||
fc604767 | 698 | IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local); |
b3cdd2a7 JV |
699 | |
700 | LeaveFunction(10); | |
701 | return NF_STOLEN; | |
702 | ||
703 | tx_error_icmp: | |
704 | dst_link_failure(skb); | |
705 | tx_error: | |
706 | LeaveFunction(10); | |
707 | kfree_skb(skb); | |
708 | return NF_STOLEN; | |
709 | tx_error_put: | |
d8d1f30b | 710 | dst_release(&rt->dst); |
b3cdd2a7 JV |
711 | goto tx_error; |
712 | } | |
713 | #endif | |
714 | ||
1da177e4 LT |
715 | |
716 | /* | |
717 | * IP Tunneling transmitter | |
718 | * | |
719 | * This function encapsulates the packet in a new IP packet, its | |
720 | * destination will be set to cp->daddr. Most code of this function | |
721 | * is taken from ipip.c. | |
722 | * | |
723 | * It is used in VS/TUN cluster. The load balancer selects a real | |
724 | * server from a cluster based on a scheduling algorithm, | |
725 | * encapsulates the request packet and forwards it to the selected | |
726 | * server. For example, all real servers are configured with | |
727 | * "ifconfig tunl0 <Virtual IP Address> up". When the server receives | |
728 | * the encapsulated packet, it will decapsulate the packet, processe | |
729 | * the request and return the response packets directly to the client | |
730 | * without passing the load balancer. This can greatly increase the | |
731 | * scalability of virtual server. | |
732 | * | |
733 | * Used for ANY protocol | |
734 | */ | |
735 | int | |
736 | ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, | |
737 | struct ip_vs_protocol *pp) | |
738 | { | |
739 | struct rtable *rt; /* Route to the other host */ | |
740 | struct net_device *tdev; /* Device to other host */ | |
eddc9ec5 | 741 | struct iphdr *old_iph = ip_hdr(skb); |
1da177e4 | 742 | u8 tos = old_iph->tos; |
76ab608d | 743 | __be16 df = old_iph->frag_off; |
1da177e4 | 744 | struct iphdr *iph; /* Our new IP header */ |
c2636b4d | 745 | unsigned int max_headroom; /* The extra header space needed */ |
1da177e4 | 746 | int mtu; |
f4bc17cd | 747 | int ret; |
1da177e4 LT |
748 | |
749 | EnterFunction(10); | |
750 | ||
4412ec49 | 751 | if (skb->protocol != htons(ETH_P_IP)) { |
1e3e238e | 752 | IP_VS_DBG_RL("%s(): protocol error, " |
1da177e4 | 753 | "ETH_P_IP: %d, skb protocol: %d\n", |
1e3e238e | 754 | __func__, htons(ETH_P_IP), skb->protocol); |
1da177e4 LT |
755 | goto tx_error; |
756 | } | |
757 | ||
fc604767 JA |
758 | if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip, |
759 | RT_TOS(tos), 1|2))) | |
1da177e4 | 760 | goto tx_error_icmp; |
fc604767 JA |
761 | if (rt->rt_flags & RTCF_LOCAL) { |
762 | ip_rt_put(rt); | |
763 | IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1); | |
764 | } | |
1da177e4 | 765 | |
d8d1f30b | 766 | tdev = rt->dst.dev; |
1da177e4 | 767 | |
d8d1f30b | 768 | mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr); |
1da177e4 | 769 | if (mtu < 68) { |
1e3e238e | 770 | IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__); |
fc604767 | 771 | goto tx_error_put; |
1da177e4 | 772 | } |
adf30907 ED |
773 | if (skb_dst(skb)) |
774 | skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu); | |
1da177e4 | 775 | |
4412ec49 | 776 | df |= (old_iph->frag_off & htons(IP_DF)); |
1da177e4 | 777 | |
4412ec49 | 778 | if ((old_iph->frag_off & htons(IP_DF)) |
1da177e4 LT |
779 | && mtu < ntohs(old_iph->tot_len)) { |
780 | icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu)); | |
1e3e238e | 781 | IP_VS_DBG_RL("%s(): frag needed\n", __func__); |
fc604767 | 782 | goto tx_error_put; |
1da177e4 LT |
783 | } |
784 | ||
785 | /* | |
786 | * Okay, now see if we can stuff it in the buffer as-is. | |
787 | */ | |
788 | max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr); | |
789 | ||
790 | if (skb_headroom(skb) < max_headroom | |
791 | || skb_cloned(skb) || skb_shared(skb)) { | |
792 | struct sk_buff *new_skb = | |
793 | skb_realloc_headroom(skb, max_headroom); | |
794 | if (!new_skb) { | |
795 | ip_rt_put(rt); | |
796 | kfree_skb(skb); | |
1e3e238e | 797 | IP_VS_ERR_RL("%s(): no memory\n", __func__); |
1da177e4 LT |
798 | return NF_STOLEN; |
799 | } | |
800 | kfree_skb(skb); | |
801 | skb = new_skb; | |
eddc9ec5 | 802 | old_iph = ip_hdr(skb); |
1da177e4 LT |
803 | } |
804 | ||
714f095f | 805 | skb->transport_header = skb->network_header; |
1da177e4 LT |
806 | |
807 | /* fix old IP header checksum */ | |
808 | ip_send_check(old_iph); | |
809 | ||
e2d1bca7 ACM |
810 | skb_push(skb, sizeof(struct iphdr)); |
811 | skb_reset_network_header(skb); | |
1da177e4 LT |
812 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
813 | ||
814 | /* drop old route */ | |
adf30907 | 815 | skb_dst_drop(skb); |
d8d1f30b | 816 | skb_dst_set(skb, &rt->dst); |
1da177e4 LT |
817 | |
818 | /* | |
819 | * Push down and install the IPIP header. | |
820 | */ | |
eddc9ec5 | 821 | iph = ip_hdr(skb); |
1da177e4 LT |
822 | iph->version = 4; |
823 | iph->ihl = sizeof(struct iphdr)>>2; | |
824 | iph->frag_off = df; | |
825 | iph->protocol = IPPROTO_IPIP; | |
826 | iph->tos = tos; | |
827 | iph->daddr = rt->rt_dst; | |
828 | iph->saddr = rt->rt_src; | |
829 | iph->ttl = old_iph->ttl; | |
d8d1f30b | 830 | ip_select_ident(iph, &rt->dst, NULL); |
1da177e4 LT |
831 | |
832 | /* Another hack: avoid icmp_send in ip_fragment */ | |
833 | skb->local_df = 1; | |
834 | ||
f4bc17cd JA |
835 | ret = IP_VS_XMIT_TUNNEL(skb, cp); |
836 | if (ret == NF_ACCEPT) | |
837 | ip_local_out(skb); | |
838 | else if (ret == NF_DROP) | |
839 | kfree_skb(skb); | |
1da177e4 LT |
840 | |
841 | LeaveFunction(10); | |
842 | ||
843 | return NF_STOLEN; | |
844 | ||
845 | tx_error_icmp: | |
846 | dst_link_failure(skb); | |
847 | tx_error: | |
848 | kfree_skb(skb); | |
849 | LeaveFunction(10); | |
850 | return NF_STOLEN; | |
fc604767 JA |
851 | tx_error_put: |
852 | ip_rt_put(rt); | |
853 | goto tx_error; | |
1da177e4 LT |
854 | } |
855 | ||
b3cdd2a7 JV |
856 | #ifdef CONFIG_IP_VS_IPV6 |
857 | int | |
858 | ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, | |
859 | struct ip_vs_protocol *pp) | |
860 | { | |
861 | struct rt6_info *rt; /* Route to the other host */ | |
714f095f | 862 | struct in6_addr saddr; /* Source for tunnel */ |
b3cdd2a7 JV |
863 | struct net_device *tdev; /* Device to other host */ |
864 | struct ipv6hdr *old_iph = ipv6_hdr(skb); | |
b3cdd2a7 JV |
865 | struct ipv6hdr *iph; /* Our new IP header */ |
866 | unsigned int max_headroom; /* The extra header space needed */ | |
867 | int mtu; | |
f4bc17cd | 868 | int ret; |
b3cdd2a7 JV |
869 | |
870 | EnterFunction(10); | |
871 | ||
872 | if (skb->protocol != htons(ETH_P_IPV6)) { | |
1e3e238e | 873 | IP_VS_DBG_RL("%s(): protocol error, " |
b3cdd2a7 | 874 | "ETH_P_IPV6: %d, skb protocol: %d\n", |
1e3e238e | 875 | __func__, htons(ETH_P_IPV6), skb->protocol); |
b3cdd2a7 JV |
876 | goto tx_error; |
877 | } | |
878 | ||
fc604767 JA |
879 | if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, |
880 | &saddr, 1, 1|2))) | |
b3cdd2a7 | 881 | goto tx_error_icmp; |
fc604767 JA |
882 | if (__ip_vs_is_local_route6(rt)) { |
883 | dst_release(&rt->dst); | |
884 | IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1); | |
885 | } | |
b3cdd2a7 | 886 | |
d8d1f30b | 887 | tdev = rt->dst.dev; |
b3cdd2a7 | 888 | |
d8d1f30b | 889 | mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr); |
714f095f | 890 | if (mtu < IPV6_MIN_MTU) { |
714f095f HS |
891 | IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__, |
892 | IPV6_MIN_MTU); | |
fc604767 | 893 | goto tx_error_put; |
b3cdd2a7 | 894 | } |
adf30907 ED |
895 | if (skb_dst(skb)) |
896 | skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu); | |
b3cdd2a7 JV |
897 | |
898 | if (mtu < ntohs(old_iph->payload_len) + sizeof(struct ipv6hdr)) { | |
3ffe533c | 899 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
1e3e238e | 900 | IP_VS_DBG_RL("%s(): frag needed\n", __func__); |
fc604767 | 901 | goto tx_error_put; |
b3cdd2a7 JV |
902 | } |
903 | ||
904 | /* | |
905 | * Okay, now see if we can stuff it in the buffer as-is. | |
906 | */ | |
907 | max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr); | |
908 | ||
909 | if (skb_headroom(skb) < max_headroom | |
910 | || skb_cloned(skb) || skb_shared(skb)) { | |
911 | struct sk_buff *new_skb = | |
912 | skb_realloc_headroom(skb, max_headroom); | |
913 | if (!new_skb) { | |
d8d1f30b | 914 | dst_release(&rt->dst); |
b3cdd2a7 | 915 | kfree_skb(skb); |
1e3e238e | 916 | IP_VS_ERR_RL("%s(): no memory\n", __func__); |
b3cdd2a7 JV |
917 | return NF_STOLEN; |
918 | } | |
919 | kfree_skb(skb); | |
920 | skb = new_skb; | |
921 | old_iph = ipv6_hdr(skb); | |
922 | } | |
923 | ||
714f095f | 924 | skb->transport_header = skb->network_header; |
b3cdd2a7 JV |
925 | |
926 | skb_push(skb, sizeof(struct ipv6hdr)); | |
927 | skb_reset_network_header(skb); | |
928 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); | |
929 | ||
930 | /* drop old route */ | |
adf30907 | 931 | skb_dst_drop(skb); |
d8d1f30b | 932 | skb_dst_set(skb, &rt->dst); |
b3cdd2a7 JV |
933 | |
934 | /* | |
935 | * Push down and install the IPIP header. | |
936 | */ | |
937 | iph = ipv6_hdr(skb); | |
938 | iph->version = 6; | |
939 | iph->nexthdr = IPPROTO_IPV6; | |
b7b45f47 HH |
940 | iph->payload_len = old_iph->payload_len; |
941 | be16_add_cpu(&iph->payload_len, sizeof(*old_iph)); | |
b3cdd2a7 JV |
942 | iph->priority = old_iph->priority; |
943 | memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl)); | |
714f095f HS |
944 | ipv6_addr_copy(&iph->daddr, &cp->daddr.in6); |
945 | ipv6_addr_copy(&iph->saddr, &saddr); | |
b3cdd2a7 JV |
946 | iph->hop_limit = old_iph->hop_limit; |
947 | ||
948 | /* Another hack: avoid icmp_send in ip_fragment */ | |
949 | skb->local_df = 1; | |
950 | ||
f4bc17cd JA |
951 | ret = IP_VS_XMIT_TUNNEL(skb, cp); |
952 | if (ret == NF_ACCEPT) | |
953 | ip6_local_out(skb); | |
954 | else if (ret == NF_DROP) | |
955 | kfree_skb(skb); | |
b3cdd2a7 JV |
956 | |
957 | LeaveFunction(10); | |
958 | ||
959 | return NF_STOLEN; | |
960 | ||
961 | tx_error_icmp: | |
962 | dst_link_failure(skb); | |
963 | tx_error: | |
964 | kfree_skb(skb); | |
965 | LeaveFunction(10); | |
966 | return NF_STOLEN; | |
fc604767 JA |
967 | tx_error_put: |
968 | dst_release(&rt->dst); | |
969 | goto tx_error; | |
b3cdd2a7 JV |
970 | } |
971 | #endif | |
972 | ||
1da177e4 LT |
973 | |
974 | /* | |
975 | * Direct Routing transmitter | |
976 | * Used for ANY protocol | |
977 | */ | |
978 | int | |
979 | ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, | |
980 | struct ip_vs_protocol *pp) | |
981 | { | |
982 | struct rtable *rt; /* Route to the other host */ | |
eddc9ec5 | 983 | struct iphdr *iph = ip_hdr(skb); |
1da177e4 LT |
984 | int mtu; |
985 | ||
986 | EnterFunction(10); | |
987 | ||
fc604767 JA |
988 | if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip, |
989 | RT_TOS(iph->tos), 1|2))) | |
1da177e4 | 990 | goto tx_error_icmp; |
fc604767 JA |
991 | if (rt->rt_flags & RTCF_LOCAL) { |
992 | ip_rt_put(rt); | |
993 | IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1); | |
994 | } | |
1da177e4 LT |
995 | |
996 | /* MTU checking */ | |
d8d1f30b | 997 | mtu = dst_mtu(&rt->dst); |
4412ec49 | 998 | if ((iph->frag_off & htons(IP_DF)) && skb->len > mtu) { |
1da177e4 LT |
999 | icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu)); |
1000 | ip_rt_put(rt); | |
1e3e238e | 1001 | IP_VS_DBG_RL("%s(): frag needed\n", __func__); |
1da177e4 LT |
1002 | goto tx_error; |
1003 | } | |
1004 | ||
1005 | /* | |
1006 | * Call ip_send_check because we are not sure it is called | |
1007 | * after ip_defrag. Is copy-on-write needed? | |
1008 | */ | |
1009 | if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) { | |
1010 | ip_rt_put(rt); | |
1011 | return NF_STOLEN; | |
1012 | } | |
eddc9ec5 | 1013 | ip_send_check(ip_hdr(skb)); |
1da177e4 LT |
1014 | |
1015 | /* drop old route */ | |
adf30907 | 1016 | skb_dst_drop(skb); |
d8d1f30b | 1017 | skb_dst_set(skb, &rt->dst); |
1da177e4 LT |
1018 | |
1019 | /* Another hack: avoid icmp_send in ip_fragment */ | |
1020 | skb->local_df = 1; | |
1021 | ||
fc604767 | 1022 | IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0); |
1da177e4 LT |
1023 | |
1024 | LeaveFunction(10); | |
1025 | return NF_STOLEN; | |
1026 | ||
1027 | tx_error_icmp: | |
1028 | dst_link_failure(skb); | |
1029 | tx_error: | |
1030 | kfree_skb(skb); | |
1031 | LeaveFunction(10); | |
1032 | return NF_STOLEN; | |
1033 | } | |
1034 | ||
b3cdd2a7 JV |
1035 | #ifdef CONFIG_IP_VS_IPV6 |
1036 | int | |
1037 | ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, | |
1038 | struct ip_vs_protocol *pp) | |
1039 | { | |
1040 | struct rt6_info *rt; /* Route to the other host */ | |
1041 | int mtu; | |
1042 | ||
1043 | EnterFunction(10); | |
1044 | ||
fc604767 JA |
1045 | if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL, |
1046 | 0, 1|2))) | |
b3cdd2a7 | 1047 | goto tx_error_icmp; |
fc604767 JA |
1048 | if (__ip_vs_is_local_route6(rt)) { |
1049 | dst_release(&rt->dst); | |
1050 | IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1); | |
1051 | } | |
b3cdd2a7 JV |
1052 | |
1053 | /* MTU checking */ | |
d8d1f30b | 1054 | mtu = dst_mtu(&rt->dst); |
b3cdd2a7 | 1055 | if (skb->len > mtu) { |
3ffe533c | 1056 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
d8d1f30b | 1057 | dst_release(&rt->dst); |
1e3e238e | 1058 | IP_VS_DBG_RL("%s(): frag needed\n", __func__); |
b3cdd2a7 JV |
1059 | goto tx_error; |
1060 | } | |
1061 | ||
1062 | /* | |
1063 | * Call ip_send_check because we are not sure it is called | |
1064 | * after ip_defrag. Is copy-on-write needed? | |
1065 | */ | |
1066 | skb = skb_share_check(skb, GFP_ATOMIC); | |
1067 | if (unlikely(skb == NULL)) { | |
d8d1f30b | 1068 | dst_release(&rt->dst); |
b3cdd2a7 JV |
1069 | return NF_STOLEN; |
1070 | } | |
1071 | ||
1072 | /* drop old route */ | |
adf30907 | 1073 | skb_dst_drop(skb); |
d8d1f30b | 1074 | skb_dst_set(skb, &rt->dst); |
b3cdd2a7 JV |
1075 | |
1076 | /* Another hack: avoid icmp_send in ip_fragment */ | |
1077 | skb->local_df = 1; | |
1078 | ||
fc604767 | 1079 | IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0); |
b3cdd2a7 JV |
1080 | |
1081 | LeaveFunction(10); | |
1082 | return NF_STOLEN; | |
1083 | ||
1084 | tx_error_icmp: | |
1085 | dst_link_failure(skb); | |
1086 | tx_error: | |
1087 | kfree_skb(skb); | |
1088 | LeaveFunction(10); | |
1089 | return NF_STOLEN; | |
1090 | } | |
1091 | #endif | |
1092 | ||
1da177e4 LT |
1093 | |
1094 | /* | |
1095 | * ICMP packet transmitter | |
1096 | * called by the ip_vs_in_icmp | |
1097 | */ | |
1098 | int | |
1099 | ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, | |
1100 | struct ip_vs_protocol *pp, int offset) | |
1101 | { | |
1102 | struct rtable *rt; /* Route to the other host */ | |
1103 | int mtu; | |
1104 | int rc; | |
fc604767 | 1105 | int local; |
1da177e4 LT |
1106 | |
1107 | EnterFunction(10); | |
1108 | ||
1109 | /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be | |
1110 | forwarded directly here, because there is no need to | |
1111 | translate address/port back */ | |
1112 | if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) { | |
1113 | if (cp->packet_xmit) | |
1114 | rc = cp->packet_xmit(skb, cp, pp); | |
1115 | else | |
1116 | rc = NF_ACCEPT; | |
1117 | /* do not touch skb anymore */ | |
1118 | atomic_inc(&cp->in_pkts); | |
1da177e4 LT |
1119 | goto out; |
1120 | } | |
1121 | ||
1122 | /* | |
1123 | * mangle and send the packet here (only for VS/NAT) | |
1124 | */ | |
1125 | ||
fc604767 JA |
1126 | if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip, |
1127 | RT_TOS(ip_hdr(skb)->tos), 1|2|4))) | |
1da177e4 | 1128 | goto tx_error_icmp; |
fc604767 JA |
1129 | local = rt->rt_flags & RTCF_LOCAL; |
1130 | ||
1131 | /* | |
1132 | * Avoid duplicate tuple in reply direction for NAT traffic | |
1133 | * to local address when connection is sync-ed | |
1134 | */ | |
1135 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) | |
1136 | if (cp->flags & IP_VS_CONN_F_SYNC && local) { | |
1137 | enum ip_conntrack_info ctinfo; | |
1138 | struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo); | |
1139 | ||
1140 | if (ct && !nf_ct_is_untracked(ct)) { | |
1141 | IP_VS_DBG(10, "%s(): " | |
1142 | "stopping DNAT to local address %pI4\n", | |
1143 | __func__, &cp->daddr.ip); | |
1144 | goto tx_error_put; | |
1145 | } | |
1146 | } | |
1147 | #endif | |
1148 | ||
1149 | /* From world but DNAT to loopback address? */ | |
1150 | if (local && ipv4_is_loopback(rt->rt_dst) && skb_rtable(skb)->fl.iif) { | |
1151 | IP_VS_DBG(1, "%s(): " | |
1152 | "stopping DNAT to loopback %pI4\n", | |
1153 | __func__, &cp->daddr.ip); | |
1154 | goto tx_error_put; | |
1155 | } | |
1da177e4 LT |
1156 | |
1157 | /* MTU checking */ | |
d8d1f30b | 1158 | mtu = dst_mtu(&rt->dst); |
eddc9ec5 | 1159 | if ((skb->len > mtu) && (ip_hdr(skb)->frag_off & htons(IP_DF))) { |
1da177e4 | 1160 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu)); |
1e3e238e | 1161 | IP_VS_DBG_RL("%s(): frag needed\n", __func__); |
fc604767 | 1162 | goto tx_error_put; |
1da177e4 LT |
1163 | } |
1164 | ||
1165 | /* copy-on-write the packet before mangling it */ | |
af1e1cf0 | 1166 | if (!skb_make_writable(skb, offset)) |
1da177e4 LT |
1167 | goto tx_error_put; |
1168 | ||
d8d1f30b | 1169 | if (skb_cow(skb, rt->dst.dev->hard_header_len)) |
1da177e4 LT |
1170 | goto tx_error_put; |
1171 | ||
1da177e4 LT |
1172 | ip_vs_nat_icmp(skb, pp, cp, 0); |
1173 | ||
fc604767 JA |
1174 | if (!local) { |
1175 | /* drop the old route when skb is not shared */ | |
1176 | skb_dst_drop(skb); | |
1177 | skb_dst_set(skb, &rt->dst); | |
1178 | } else { | |
1179 | ip_rt_put(rt); | |
1180 | /* | |
1181 | * Some IPv4 replies get local address from routes, | |
1182 | * not from iph, so while we DNAT after routing | |
1183 | * we need this second input/output route. | |
1184 | */ | |
1185 | if (!__ip_vs_reroute_locally(skb)) | |
1186 | goto tx_error; | |
1187 | } | |
1188 | ||
1da177e4 LT |
1189 | /* Another hack: avoid icmp_send in ip_fragment */ |
1190 | skb->local_df = 1; | |
1191 | ||
fc604767 | 1192 | IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local); |
1da177e4 LT |
1193 | |
1194 | rc = NF_STOLEN; | |
1195 | goto out; | |
1196 | ||
1197 | tx_error_icmp: | |
1198 | dst_link_failure(skb); | |
1199 | tx_error: | |
1200 | dev_kfree_skb(skb); | |
1201 | rc = NF_STOLEN; | |
1202 | out: | |
1203 | LeaveFunction(10); | |
1204 | return rc; | |
1205 | tx_error_put: | |
1206 | ip_rt_put(rt); | |
1207 | goto tx_error; | |
1208 | } | |
b3cdd2a7 JV |
1209 | |
1210 | #ifdef CONFIG_IP_VS_IPV6 | |
1211 | int | |
1212 | ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, | |
1213 | struct ip_vs_protocol *pp, int offset) | |
1214 | { | |
1215 | struct rt6_info *rt; /* Route to the other host */ | |
1216 | int mtu; | |
1217 | int rc; | |
fc604767 | 1218 | int local; |
b3cdd2a7 JV |
1219 | |
1220 | EnterFunction(10); | |
1221 | ||
1222 | /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be | |
1223 | forwarded directly here, because there is no need to | |
1224 | translate address/port back */ | |
1225 | if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) { | |
1226 | if (cp->packet_xmit) | |
1227 | rc = cp->packet_xmit(skb, cp, pp); | |
1228 | else | |
1229 | rc = NF_ACCEPT; | |
1230 | /* do not touch skb anymore */ | |
1231 | atomic_inc(&cp->in_pkts); | |
1232 | goto out; | |
1233 | } | |
1234 | ||
1235 | /* | |
1236 | * mangle and send the packet here (only for VS/NAT) | |
1237 | */ | |
1238 | ||
fc604767 JA |
1239 | if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL, |
1240 | 0, 1|2|4))) | |
b3cdd2a7 JV |
1241 | goto tx_error_icmp; |
1242 | ||
fc604767 JA |
1243 | local = __ip_vs_is_local_route6(rt); |
1244 | /* | |
1245 | * Avoid duplicate tuple in reply direction for NAT traffic | |
1246 | * to local address when connection is sync-ed | |
1247 | */ | |
1248 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) | |
1249 | if (cp->flags & IP_VS_CONN_F_SYNC && local) { | |
1250 | enum ip_conntrack_info ctinfo; | |
1251 | struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo); | |
1252 | ||
1253 | if (ct && !nf_ct_is_untracked(ct)) { | |
1254 | IP_VS_DBG(10, "%s(): " | |
1255 | "stopping DNAT to local address %pI6\n", | |
1256 | __func__, &cp->daddr.in6); | |
1257 | goto tx_error_put; | |
1258 | } | |
1259 | } | |
1260 | #endif | |
1261 | ||
1262 | /* From world but DNAT to loopback address? */ | |
1263 | if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) && | |
1264 | ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) { | |
1265 | IP_VS_DBG(1, "%s(): " | |
1266 | "stopping DNAT to loopback %pI6\n", | |
1267 | __func__, &cp->daddr.in6); | |
1268 | goto tx_error_put; | |
1269 | } | |
1270 | ||
b3cdd2a7 | 1271 | /* MTU checking */ |
d8d1f30b | 1272 | mtu = dst_mtu(&rt->dst); |
b3cdd2a7 | 1273 | if (skb->len > mtu) { |
3ffe533c | 1274 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
1e3e238e | 1275 | IP_VS_DBG_RL("%s(): frag needed\n", __func__); |
fc604767 | 1276 | goto tx_error_put; |
b3cdd2a7 JV |
1277 | } |
1278 | ||
1279 | /* copy-on-write the packet before mangling it */ | |
1280 | if (!skb_make_writable(skb, offset)) | |
1281 | goto tx_error_put; | |
1282 | ||
d8d1f30b | 1283 | if (skb_cow(skb, rt->dst.dev->hard_header_len)) |
b3cdd2a7 JV |
1284 | goto tx_error_put; |
1285 | ||
b3cdd2a7 JV |
1286 | ip_vs_nat_icmp_v6(skb, pp, cp, 0); |
1287 | ||
fc604767 JA |
1288 | if (!local || !skb->dev) { |
1289 | /* drop the old route when skb is not shared */ | |
1290 | skb_dst_drop(skb); | |
1291 | skb_dst_set(skb, &rt->dst); | |
1292 | } else { | |
1293 | /* destined to loopback, do we need to change route? */ | |
1294 | dst_release(&rt->dst); | |
1295 | } | |
1296 | ||
b3cdd2a7 JV |
1297 | /* Another hack: avoid icmp_send in ip_fragment */ |
1298 | skb->local_df = 1; | |
1299 | ||
fc604767 | 1300 | IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local); |
b3cdd2a7 JV |
1301 | |
1302 | rc = NF_STOLEN; | |
1303 | goto out; | |
1304 | ||
1305 | tx_error_icmp: | |
1306 | dst_link_failure(skb); | |
1307 | tx_error: | |
1308 | dev_kfree_skb(skb); | |
1309 | rc = NF_STOLEN; | |
1310 | out: | |
1311 | LeaveFunction(10); | |
1312 | return rc; | |
1313 | tx_error_put: | |
d8d1f30b | 1314 | dst_release(&rt->dst); |
b3cdd2a7 JV |
1315 | goto tx_error; |
1316 | } | |
1317 | #endif |