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