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