blob: bf02932b7188d71ea0c0f1ea438c1b33766b7979 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ip_vs_xmit.c: various packet transmitters for IPVS
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
5 * Julian Anastasov <ja@ssi.bg>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * Changes:
13 *
Julian Anastasovcb591552010-10-17 16:40:51 +030014 * 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
Julian Anastasov026ace02013-03-21 11:58:06 +020020 * - 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
Julian Anastasovcb591552010-10-17 16:40:51 +030022 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 */
27
Hannes Eder9aada7a2009-07-30 14:29:44 -070028#define KMSG_COMPONENT "IPVS"
29#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/tcp.h> /* for tcphdr */
Herbert Xuc439cb22008-01-11 19:14:00 -080034#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#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 */
Julius Volz38cdcc92008-09-02 15:55:44 +020039#include <net/ipv6.h>
40#include <net/ip6_route.h>
Julian Anastasovea1d5d72014-08-01 10:36:17 +030041#include <net/ip_tunnels.h>
Hans Schillstrom714f0952010-10-19 10:38:48 +020042#include <net/addrconf.h>
Julius Volz38cdcc92008-09-02 15:55:44 +020043#include <linux/icmpv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/netfilter.h>
45#include <linux/netfilter_ipv4.h>
46
47#include <net/ip_vs.h>
48
Changli Gao17a8f8e2011-02-24 08:19:57 +080049enum {
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 */
Julian Anastasovf2edb9f2012-07-20 11:59:52 +030055 IP_VS_RT_MODE_CONNECT = 8, /* Always bind route to saddr */
Julian Anastasovad4d3ef2012-10-08 11:41:20 +000056 IP_VS_RT_MODE_KNOWN_NH = 16,/* Route via remote addr */
Julian Anastasov4115ded2013-03-21 11:58:05 +020057 IP_VS_RT_MODE_TUNNEL = 32,/* Tunnel mode */
Changli Gao17a8f8e2011-02-24 08:19:57 +080058};
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Julian Anastasov026ace02013-03-21 11:58:06 +020060static 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
65static inline void ip_vs_dest_dst_free(struct ip_vs_dest_dst *dest_dst)
66{
67 kfree(dest_dst);
68}
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
71 * Destination cache to speed up outgoing route lookup
72 */
73static inline void
Julian Anastasov026ace02013-03-21 11:58:06 +020074__ip_vs_dst_set(struct ip_vs_dest *dest, struct ip_vs_dest_dst *dest_dst,
75 struct dst_entry *dst, u32 dst_cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Julian Anastasov026ace02013-03-21 11:58:06 +020077 struct ip_vs_dest_dst *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Julian Anastasov026ace02013-03-21 11:58:06 +020079 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Julian Anastasov026ace02013-03-21 11:58:06 +020092static inline struct ip_vs_dest_dst *
Julian Anastasovc90558d2013-03-21 11:57:59 +020093__ip_vs_dst_check(struct ip_vs_dest *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Julian Anastasov026ace02013-03-21 11:58:06 +020095 struct ip_vs_dest_dst *dest_dst = rcu_dereference(dest->dest_dst);
96 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Julian Anastasov026ace02013-03-21 11:58:06 +020098 if (!dest_dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return NULL;
Julian Anastasov026ace02013-03-21 11:58:06 +0200100 dst = dest_dst->dst_cache;
101 if (dst->obsolete &&
102 dst->ops->check(dst, dest_dst->dst_cookie) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return NULL;
Julian Anastasov026ace02013-03-21 11:58:06 +0200104 return dest_dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Jesper Dangaard Brouer590e3f72012-08-28 22:05:51 +0200107static inline bool
108__mtu_check_toobig_v6(const struct sk_buff *skb, u32 mtu)
109{
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200110 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)) {
Jesper Dangaard Brouer590e3f72012-08-28 22:05:51 +0200118 return true; /* Packet size violate MTU size */
119 }
120 return false;
121}
122
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300123/* Get route to daddr, update *saddr, optionally bind route to saddr */
124static struct rtable *do_output_route4(struct net *net, __be32 daddr,
Julian Anastasovc90558d2013-03-21 11:57:59 +0200125 int rt_mode, __be32 *saddr)
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300126{
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.saddr = (rt_mode & IP_VS_RT_MODE_CONNECT) ? *saddr : 0;
Julian Anastasovad4d3ef2012-10-08 11:41:20 +0000134 fl4.flowi4_flags = (rt_mode & IP_VS_RT_MODE_KNOWN_NH) ?
135 FLOWI_FLAG_KNOWN_NH : 0;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300136
137retry:
138 rt = ip_route_output_key(net, &fl4);
139 if (IS_ERR(rt)) {
140 /* Invalid saddr ? */
141 if (PTR_ERR(rt) == -EINVAL && *saddr &&
142 rt_mode & IP_VS_RT_MODE_CONNECT && !loop) {
143 *saddr = 0;
Julian Anastasovc90558d2013-03-21 11:57:59 +0200144 flowi4_update_output(&fl4, 0, 0, daddr, 0);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300145 goto retry;
146 }
147 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n", &daddr);
148 return NULL;
149 } else if (!*saddr && rt_mode & IP_VS_RT_MODE_CONNECT && fl4.saddr) {
150 ip_rt_put(rt);
151 *saddr = fl4.saddr;
Julian Anastasovc90558d2013-03-21 11:57:59 +0200152 flowi4_update_output(&fl4, 0, 0, daddr, fl4.saddr);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300153 loop++;
154 goto retry;
155 }
156 *saddr = fl4.saddr;
157 return rt;
158}
159
Alex Gartrell4a4739d52014-09-09 16:40:25 -0700160#ifdef CONFIG_IP_VS_IPV6
161static inline int __ip_vs_is_local_route6(struct rt6_info *rt)
162{
163 return rt->dst.dev && rt->dst.dev->flags & IFF_LOOPBACK;
164}
165#endif
166
167static inline bool crosses_local_route_boundary(int skb_af, struct sk_buff *skb,
168 int rt_mode,
169 bool new_rt_is_local)
170{
171 bool rt_mode_allow_local = !!(rt_mode & IP_VS_RT_MODE_LOCAL);
172 bool rt_mode_allow_non_local = !!(rt_mode & IP_VS_RT_MODE_LOCAL);
173 bool rt_mode_allow_redirect = !!(rt_mode & IP_VS_RT_MODE_RDR);
174 bool source_is_loopback;
175 bool old_rt_is_local;
176
177#ifdef CONFIG_IP_VS_IPV6
178 if (skb_af == AF_INET6) {
179 int addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
180
181 source_is_loopback =
182 (!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
183 (addr_type & IPV6_ADDR_LOOPBACK);
184 old_rt_is_local = __ip_vs_is_local_route6(
185 (struct rt6_info *)skb_dst(skb));
186 } else
187#endif
188 {
189 source_is_loopback = ipv4_is_loopback(ip_hdr(skb)->saddr);
190 old_rt_is_local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
191 }
192
193 if (unlikely(new_rt_is_local)) {
194 if (!rt_mode_allow_local)
195 return true;
196 if (!rt_mode_allow_redirect && !old_rt_is_local)
197 return true;
198 } else {
199 if (!rt_mode_allow_non_local)
200 return true;
201 if (source_is_loopback)
202 return true;
203 }
204 return false;
205}
206
Alex Gartrell919aa0b2014-09-09 16:40:26 -0700207static inline void maybe_update_pmtu(int skb_af, struct sk_buff *skb, int mtu)
208{
209 struct sock *sk = skb->sk;
210 struct rtable *ort = skb_rtable(skb);
211
Eric Dumazeta8399232015-03-16 21:06:15 -0700212 if (!skb->dev && sk && sk_fullsock(sk))
Alex Gartrell919aa0b2014-09-09 16:40:26 -0700213 ort->dst.ops->update_pmtu(&ort->dst, sk, NULL, mtu);
214}
215
Alex Gartrellc63e4de2014-09-09 16:40:27 -0700216static inline bool ensure_mtu_is_adequate(int skb_af, int rt_mode,
217 struct ip_vs_iphdr *ipvsh,
218 struct sk_buff *skb, int mtu)
219{
220#ifdef CONFIG_IP_VS_IPV6
221 if (skb_af == AF_INET6) {
222 struct net *net = dev_net(skb_dst(skb)->dev);
223
224 if (unlikely(__mtu_check_toobig_v6(skb, mtu))) {
225 if (!skb->dev)
226 skb->dev = net->loopback_dev;
227 /* only send ICMP too big on first fragment */
228 if (!ipvsh->fragoffs)
229 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
230 IP_VS_DBG(1, "frag needed for %pI6c\n",
231 &ipv6_hdr(skb)->saddr);
232 return false;
233 }
234 } else
235#endif
236 {
237 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
238
239 /* If we're going to tunnel the packet and pmtu discovery
240 * is disabled, we'll just fragment it anyway
241 */
242 if ((rt_mode & IP_VS_RT_MODE_TUNNEL) && !sysctl_pmtu_disc(ipvs))
243 return true;
244
245 if (unlikely(ip_hdr(skb)->frag_off & htons(IP_DF) &&
246 skb->len > mtu && !skb_is_gso(skb))) {
247 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
248 htonl(mtu));
249 IP_VS_DBG(1, "frag needed for %pI4\n",
250 &ip_hdr(skb)->saddr);
251 return false;
252 }
253 }
254
255 return true;
256}
257
Changli Gao17a8f8e2011-02-24 08:19:57 +0800258/* Get route to destination or remote server */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200259static int
Alex Gartrell4a4739d52014-09-09 16:40:25 -0700260__ip_vs_get_out_rt(int skb_af, struct sk_buff *skb, struct ip_vs_dest *dest,
Alex Gartrellc63e4de2014-09-09 16:40:27 -0700261 __be32 daddr, int rt_mode, __be32 *ret_saddr,
262 struct ip_vs_iphdr *ipvsh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Julian Anastasovfc604762010-10-17 16:38:15 +0300264 struct net *net = dev_net(skb_dst(skb)->dev);
Julian Anastasov026ace02013-03-21 11:58:06 +0200265 struct ip_vs_dest_dst *dest_dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct rtable *rt; /* Route to the other host */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200267 int mtu;
Julian Anastasov026ace02013-03-21 11:58:06 +0200268 int local, noref = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 if (dest) {
Julian Anastasov026ace02013-03-21 11:58:06 +0200271 dest_dst = __ip_vs_dst_check(dest);
272 if (likely(dest_dst))
273 rt = (struct rtable *) dest_dst->dst_cache;
274 else {
275 dest_dst = ip_vs_dest_dst_alloc();
Julian Anastasovac692692013-03-22 11:46:54 +0200276 spin_lock_bh(&dest->dst_lock);
Julian Anastasov026ace02013-03-21 11:58:06 +0200277 if (!dest_dst) {
278 __ip_vs_dst_set(dest, NULL, NULL, 0);
Julian Anastasovac692692013-03-22 11:46:54 +0200279 spin_unlock_bh(&dest->dst_lock);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200280 goto err_unreach;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
Julian Anastasov026ace02013-03-21 11:58:06 +0200282 rt = do_output_route4(net, dest->addr.ip, rt_mode,
283 &dest_dst->dst_saddr.ip);
284 if (!rt) {
285 __ip_vs_dst_set(dest, NULL, NULL, 0);
Julian Anastasovac692692013-03-22 11:46:54 +0200286 spin_unlock_bh(&dest->dst_lock);
Julian Anastasov026ace02013-03-21 11:58:06 +0200287 ip_vs_dest_dst_free(dest_dst);
288 goto err_unreach;
289 }
290 __ip_vs_dst_set(dest, dest_dst, &rt->dst, 0);
Julian Anastasovac692692013-03-22 11:46:54 +0200291 spin_unlock_bh(&dest->dst_lock);
Julian Anastasovc90558d2013-03-21 11:57:59 +0200292 IP_VS_DBG(10, "new dst %pI4, src %pI4, refcnt=%d\n",
Julian Anastasov026ace02013-03-21 11:58:06 +0200293 &dest->addr.ip, &dest_dst->dst_saddr.ip,
Julian Anastasovc90558d2013-03-21 11:57:59 +0200294 atomic_read(&rt->dst.__refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000296 if (ret_saddr)
Julian Anastasov026ace02013-03-21 11:58:06 +0200297 *ret_saddr = dest_dst->dst_saddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 } else {
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300299 __be32 saddr = htonl(INADDR_ANY);
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000300
Julian Anastasov026ace02013-03-21 11:58:06 +0200301 noref = 0;
302
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300303 /* 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;
Julian Anastasovc90558d2013-03-21 11:57:59 +0200307 rt = do_output_route4(net, daddr, rt_mode, &saddr);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300308 if (!rt)
Julian Anastasov4115ded2013-03-21 11:58:05 +0200309 goto err_unreach;
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000310 if (ret_saddr)
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300311 *ret_saddr = saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
Julian Anastasov4115ded2013-03-21 11:58:05 +0200314 local = (rt->rt_flags & RTCF_LOCAL) ? 1 : 0;
Alex Gartrell4a4739d52014-09-09 16:40:25 -0700315 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"
Alex Gartrell3d536662014-10-06 08:46:19 -0700318 " daddr=%pI4\n", &daddr);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200319 goto err_put;
Julian Anastasovfc604762010-10-17 16:38:15 +0300320 }
Alex Gartrell4a4739d52014-09-09 16:40:25 -0700321
322 if (unlikely(local)) {
Julian Anastasov4115ded2013-03-21 11:58:05 +0200323 /* skb to local stack, preserve old route */
Julian Anastasov026ace02013-03-21 11:58:06 +0200324 if (!noref)
325 ip_rt_put(rt);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200326 return local;
Julian Anastasovfc604762010-10-17 16:38:15 +0300327 }
328
Julian Anastasov4115ded2013-03-21 11:58:05 +0200329 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL))) {
330 mtu = dst_mtu(&rt->dst);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200331 } else {
Julian Anastasov4115ded2013-03-21 11:58:05 +0200332 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 }
Alex Gartrell919aa0b2014-09-09 16:40:26 -0700337 maybe_update_pmtu(skb_af, skb, mtu);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200338 }
339
Alex Gartrellc63e4de2014-09-09 16:40:27 -0700340 if (!ensure_mtu_is_adequate(skb_af, rt_mode, ipvsh, skb, mtu))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200341 goto err_put;
Julian Anastasov4115ded2013-03-21 11:58:05 +0200342
343 skb_dst_drop(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200344 if (noref) {
345 if (!local)
Hannes Frederic Sowadbfc4fb2014-12-06 19:19:42 +0100346 skb_dst_set_noref(skb, &rt->dst);
Julian Anastasov026ace02013-03-21 11:58:06 +0200347 else
348 skb_dst_set(skb, dst_clone(&rt->dst));
349 } else
350 skb_dst_set(skb, &rt->dst);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200351
352 return local;
353
354err_put:
Julian Anastasov026ace02013-03-21 11:58:06 +0200355 if (!noref)
356 ip_rt_put(rt);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200357 return -1;
358
359err_unreach:
360 dst_link_failure(skb);
361 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Julius Volz38cdcc92008-09-02 15:55:44 +0200364#ifdef CONFIG_IP_VS_IPV6
Hans Schillstrom714f0952010-10-19 10:38:48 +0200365static struct dst_entry *
366__ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
367 struct in6_addr *ret_saddr, int do_xfrm)
Julius Volz38cdcc92008-09-02 15:55:44 +0200368{
Hans Schillstrom714f0952010-10-19 10:38:48 +0200369 struct dst_entry *dst;
David S. Miller4c9483b2011-03-12 16:22:43 -0500370 struct flowi6 fl6 = {
371 .daddr = *daddr,
Hans Schillstrom714f0952010-10-19 10:38:48 +0200372 };
373
David S. Miller4c9483b2011-03-12 16:22:43 -0500374 dst = ip6_route_output(net, NULL, &fl6);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200375 if (dst->error)
376 goto out_err;
377 if (!ret_saddr)
378 return dst;
David S. Miller4c9483b2011-03-12 16:22:43 -0500379 if (ipv6_addr_any(&fl6.saddr) &&
Hans Schillstrom714f0952010-10-19 10:38:48 +0200380 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
David S. Miller4c9483b2011-03-12 16:22:43 -0500381 &fl6.daddr, 0, &fl6.saddr) < 0)
Hans Schillstrom714f0952010-10-19 10:38:48 +0200382 goto out_err;
David S. Miller452edd52011-03-02 13:27:41 -0800383 if (do_xfrm) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500384 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
David S. Miller452edd52011-03-02 13:27:41 -0800385 if (IS_ERR(dst)) {
386 dst = NULL;
387 goto out_err;
388 }
389 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000390 *ret_saddr = fl6.saddr;
Hans Schillstrom714f0952010-10-19 10:38:48 +0200391 return dst;
392
393out_err:
394 dst_release(dst);
395 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
396 return NULL;
397}
398
Julian Anastasovfc604762010-10-17 16:38:15 +0300399/*
400 * Get route to destination or remote server
Julian Anastasovfc604762010-10-17 16:38:15 +0300401 */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200402static int
Alex Gartrell4a4739d52014-09-09 16:40:25 -0700403__ip_vs_get_out_rt_v6(int skb_af, struct sk_buff *skb, struct ip_vs_dest *dest,
Julian Anastasovfc604762010-10-17 16:38:15 +0300404 struct in6_addr *daddr, struct in6_addr *ret_saddr,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200405 struct ip_vs_iphdr *ipvsh, int do_xfrm, int rt_mode)
Hans Schillstrom714f0952010-10-19 10:38:48 +0200406{
Julian Anastasovfc604762010-10-17 16:38:15 +0300407 struct net *net = dev_net(skb_dst(skb)->dev);
Julian Anastasov026ace02013-03-21 11:58:06 +0200408 struct ip_vs_dest_dst *dest_dst;
Julius Volz38cdcc92008-09-02 15:55:44 +0200409 struct rt6_info *rt; /* Route to the other host */
Hans Schillstrom714f0952010-10-19 10:38:48 +0200410 struct dst_entry *dst;
Julian Anastasov4115ded2013-03-21 11:58:05 +0200411 int mtu;
Julian Anastasov026ace02013-03-21 11:58:06 +0200412 int local, noref = 1;
Julius Volz38cdcc92008-09-02 15:55:44 +0200413
414 if (dest) {
Julian Anastasov026ace02013-03-21 11:58:06 +0200415 dest_dst = __ip_vs_dst_check(dest);
416 if (likely(dest_dst))
417 rt = (struct rt6_info *) dest_dst->dst_cache;
418 else {
Hans Schillstrom714f0952010-10-19 10:38:48 +0200419 u32 cookie;
Julius Volz38cdcc92008-09-02 15:55:44 +0200420
Julian Anastasov026ace02013-03-21 11:58:06 +0200421 dest_dst = ip_vs_dest_dst_alloc();
Julian Anastasovac692692013-03-22 11:46:54 +0200422 spin_lock_bh(&dest->dst_lock);
Julian Anastasov026ace02013-03-21 11:58:06 +0200423 if (!dest_dst) {
424 __ip_vs_dst_set(dest, NULL, NULL, 0);
Julian Anastasovac692692013-03-22 11:46:54 +0200425 spin_unlock_bh(&dest->dst_lock);
Julian Anastasov026ace02013-03-21 11:58:06 +0200426 goto err_unreach;
427 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200428 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
Julian Anastasov026ace02013-03-21 11:58:06 +0200429 &dest_dst->dst_saddr.in6,
Hans Schillstrom714f0952010-10-19 10:38:48 +0200430 do_xfrm);
431 if (!dst) {
Julian Anastasov026ace02013-03-21 11:58:06 +0200432 __ip_vs_dst_set(dest, NULL, NULL, 0);
Julian Anastasovac692692013-03-22 11:46:54 +0200433 spin_unlock_bh(&dest->dst_lock);
Julian Anastasov026ace02013-03-21 11:58:06 +0200434 ip_vs_dest_dst_free(dest_dst);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200435 goto err_unreach;
Julius Volz38cdcc92008-09-02 15:55:44 +0200436 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200437 rt = (struct rt6_info *) dst;
438 cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
Julian Anastasov026ace02013-03-21 11:58:06 +0200439 __ip_vs_dst_set(dest, dest_dst, &rt->dst, cookie);
Julian Anastasovac692692013-03-22 11:46:54 +0200440 spin_unlock_bh(&dest->dst_lock);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200441 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
Julian Anastasov026ace02013-03-21 11:58:06 +0200442 &dest->addr.in6, &dest_dst->dst_saddr.in6,
Changli Gaod8d1f302010-06-10 23:31:35 -0700443 atomic_read(&rt->dst.__refcnt));
Julius Volz38cdcc92008-09-02 15:55:44 +0200444 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200445 if (ret_saddr)
Julian Anastasov026ace02013-03-21 11:58:06 +0200446 *ret_saddr = dest_dst->dst_saddr.in6;
Julius Volz38cdcc92008-09-02 15:55:44 +0200447 } else {
Julian Anastasov026ace02013-03-21 11:58:06 +0200448 noref = 0;
Julian Anastasovfc604762010-10-17 16:38:15 +0300449 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200450 if (!dst)
Julian Anastasov4115ded2013-03-21 11:58:05 +0200451 goto err_unreach;
Hans Schillstrom714f0952010-10-19 10:38:48 +0200452 rt = (struct rt6_info *) dst;
Julius Volz38cdcc92008-09-02 15:55:44 +0200453 }
454
Julian Anastasovfc604762010-10-17 16:38:15 +0300455 local = __ip_vs_is_local_route6(rt);
Alex Gartrell4a4739d52014-09-09 16:40:25 -0700456
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"
Alex Gartrell3d536662014-10-06 08:46:19 -0700460 " daddr=%pI6\n", daddr);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200461 goto err_put;
Julian Anastasovfc604762010-10-17 16:38:15 +0300462 }
Alex Gartrell4a4739d52014-09-09 16:40:25 -0700463
464 if (unlikely(local)) {
Julian Anastasov4115ded2013-03-21 11:58:05 +0200465 /* skb to local stack, preserve old route */
Julian Anastasov026ace02013-03-21 11:58:06 +0200466 if (!noref)
467 dst_release(&rt->dst);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200468 return local;
Julian Anastasovfc604762010-10-17 16:38:15 +0300469 }
470
Julian Anastasov4115ded2013-03-21 11:58:05 +0200471 /* MTU checking */
472 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL)))
473 mtu = dst_mtu(&rt->dst);
474 else {
Julian Anastasov4115ded2013-03-21 11:58:05 +0200475 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 }
Alex Gartrell919aa0b2014-09-09 16:40:26 -0700481 maybe_update_pmtu(skb_af, skb, mtu);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200482 }
483
Alex Gartrellc63e4de2014-09-09 16:40:27 -0700484 if (!ensure_mtu_is_adequate(skb_af, rt_mode, ipvsh, skb, mtu))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200485 goto err_put;
Julian Anastasov4115ded2013-03-21 11:58:05 +0200486
487 skb_dst_drop(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200488 if (noref) {
489 if (!local)
Hannes Frederic Sowadbfc4fb2014-12-06 19:19:42 +0100490 skb_dst_set_noref(skb, &rt->dst);
Julian Anastasov026ace02013-03-21 11:58:06 +0200491 else
492 skb_dst_set(skb, dst_clone(&rt->dst));
493 } else
494 skb_dst_set(skb, &rt->dst);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200495
496 return local;
497
498err_put:
Julian Anastasov026ace02013-03-21 11:58:06 +0200499 if (!noref)
500 dst_release(&rt->dst);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200501 return -1;
502
503err_unreach:
504 dst_link_failure(skb);
505 return -1;
Julius Volz38cdcc92008-09-02 15:55:44 +0200506}
507#endif
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200510/* return NF_ACCEPT to allow forwarding or other NF_xxx on error */
511static inline int ip_vs_tunnel_xmit_prepare(struct sk_buff *skb,
512 struct ip_vs_conn *cp)
513{
514 int ret = NF_ACCEPT;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200515
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200516 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}
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200525
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200526/* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
527static inline int ip_vs_nat_send_or_cont(int pf, struct sk_buff *skb,
528 struct ip_vs_conn *cp, int local)
529{
530 int ret = NF_STOLEN;
531
532 skb->ipvs_property = 1;
533 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
534 ip_vs_notrack(skb);
535 else
536 ip_vs_update_conntrack(skb, cp, 1);
537 if (!local) {
538 skb_forward_csum(skb);
539 NF_HOOK(pf, NF_INET_LOCAL_OUT, skb, NULL, skb_dst(skb)->dev,
540 dst_output);
541 } else
542 ret = NF_ACCEPT;
543 return ret;
544}
545
546/* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
547static inline int ip_vs_send_or_cont(int pf, struct sk_buff *skb,
548 struct ip_vs_conn *cp, int local)
549{
550 int ret = NF_STOLEN;
551
552 skb->ipvs_property = 1;
553 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
554 ip_vs_notrack(skb);
555 if (!local) {
556 skb_forward_csum(skb);
557 NF_HOOK(pf, NF_INET_LOCAL_OUT, skb, NULL, skb_dst(skb)->dev,
558 dst_output);
559 } else
560 ret = NF_ACCEPT;
561 return ret;
562}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564
565/*
566 * NULL transmitter (do nothing except return NF_ACCEPT)
567 */
568int
569ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200570 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 /* we do not touch skb and do not need pskb ptr */
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200573 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576
577/*
578 * Bypass transmitter
579 * Let packets bypass the destination when the destination is not
580 * available, it may be only used in transparent cache cluster.
581 */
582int
583ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200584 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700586 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 EnterFunction(10);
589
Julian Anastasov026ace02013-03-21 11:58:06 +0200590 rcu_read_lock();
Alex Gartrell4a4739d52014-09-09 16:40:25 -0700591 if (__ip_vs_get_out_rt(cp->af, skb, NULL, iph->daddr,
Alex Gartrellc63e4de2014-09-09 16:40:27 -0700592 IP_VS_RT_MODE_NON_LOCAL, NULL, ipvsh) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Julian Anastasov4115ded2013-03-21 11:58:05 +0200595 ip_send_check(iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -0700598 skb->ignore_df = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200600 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
Julian Anastasov026ace02013-03-21 11:58:06 +0200601 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 LeaveFunction(10);
604 return NF_STOLEN;
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 tx_error:
607 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200608 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 LeaveFunction(10);
610 return NF_STOLEN;
611}
612
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200613#ifdef CONFIG_IP_VS_IPV6
614int
615ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200616 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200617{
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200618 EnterFunction(10);
619
Julian Anastasov026ace02013-03-21 11:58:06 +0200620 rcu_read_lock();
Alex Gartrell4a4739d52014-09-09 16:40:25 -0700621 if (__ip_vs_get_out_rt_v6(cp->af, skb, NULL, &ipvsh->daddr.in6, NULL,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200622 ipvsh, 0, IP_VS_RT_MODE_NON_LOCAL) < 0)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200623 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200624
625 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -0700626 skb->ignore_df = 1;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200627
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200628 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
Julian Anastasov026ace02013-03-21 11:58:06 +0200629 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200630
631 LeaveFunction(10);
632 return NF_STOLEN;
633
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200634 tx_error:
635 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200636 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200637 LeaveFunction(10);
638 return NF_STOLEN;
639}
640#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642/*
643 * NAT transmitter (only for outside-to-inside nat forwarding)
644 * Not used for related ICMP
645 */
646int
647ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200648 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 struct rtable *rt; /* Route to the other host */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200651 int local, rc, was_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 EnterFunction(10);
654
Julian Anastasov026ace02013-03-21 11:58:06 +0200655 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* check if it is a connection of no-client-port */
657 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
Al Viro014d7302006-09-28 14:29:52 -0700658 __be16 _pt, *p;
Julian Anastasov4115ded2013-03-21 11:58:05 +0200659
660 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (p == NULL)
662 goto tx_error;
663 ip_vs_conn_fill_cport(cp, *p);
664 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
665 }
666
Julian Anastasov4115ded2013-03-21 11:58:05 +0200667 was_input = rt_is_input_route(skb_rtable(skb));
Alex Gartrell4a4739d52014-09-09 16:40:25 -0700668 local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200669 IP_VS_RT_MODE_LOCAL |
670 IP_VS_RT_MODE_NON_LOCAL |
Alex Gartrellc63e4de2014-09-09 16:40:27 -0700671 IP_VS_RT_MODE_RDR, NULL, ipvsh);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200672 if (local < 0)
673 goto tx_error;
674 rt = skb_rtable(skb);
Julian Anastasovfc604762010-10-17 16:38:15 +0300675 /*
676 * Avoid duplicate tuple in reply direction for NAT traffic
677 * to local address when connection is sync-ed
678 */
Igor Maravićc0cd1152011-12-12 02:58:24 +0000679#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Julian Anastasovfc604762010-10-17 16:38:15 +0300680 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
681 enum ip_conntrack_info ctinfo;
Alan Cox05b4b062012-10-26 00:13:27 +0000682 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Julian Anastasovfc604762010-10-17 16:38:15 +0300683
684 if (ct && !nf_ct_is_untracked(ct)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300685 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
686 "ip_vs_nat_xmit(): "
Julian Anastasovfc604762010-10-17 16:38:15 +0300687 "stopping DNAT to local address");
Julian Anastasov4115ded2013-03-21 11:58:05 +0200688 goto tx_error;
Julian Anastasovfc604762010-10-17 16:38:15 +0300689 }
690 }
691#endif
692
693 /* From world but DNAT to loopback address? */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200694 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300695 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
Julian Anastasovfc604762010-10-17 16:38:15 +0300696 "stopping DNAT to loopback address");
Julian Anastasov4115ded2013-03-21 11:58:05 +0200697 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699
700 /* copy-on-write the packet before mangling it */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -0700701 if (!skb_make_writable(skb, sizeof(struct iphdr)))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200702 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Changli Gaod8d1f302010-06-10 23:31:35 -0700704 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200705 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /* mangle the packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200708 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200709 goto tx_error;
Julius Volze7ade462008-09-02 15:55:33 +0200710 ip_hdr(skb)->daddr = cp->daddr.ip;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700711 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Julian Anastasov0d796412010-10-17 16:46:17 +0300713 IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 /* FIXME: when application helper enlarges the packet and the length
716 is larger than the MTU of outgoing device, there will be still
717 MTU problem. */
718
719 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -0700720 skb->ignore_df = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200722 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
Julian Anastasov026ace02013-03-21 11:58:06 +0200723 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 LeaveFunction(10);
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200726 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 tx_error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200730 rcu_read_unlock();
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200731 LeaveFunction(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200735#ifdef CONFIG_IP_VS_IPV6
736int
737ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200738 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200739{
740 struct rt6_info *rt; /* Route to the other host */
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200741 int local, rc;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200742
743 EnterFunction(10);
744
Julian Anastasov026ace02013-03-21 11:58:06 +0200745 rcu_read_lock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200746 /* check if it is a connection of no-client-port */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200747 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT && !ipvsh->fragoffs)) {
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200748 __be16 _pt, *p;
Julian Anastasov4115ded2013-03-21 11:58:05 +0200749 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200750 if (p == NULL)
751 goto tx_error;
752 ip_vs_conn_fill_cport(cp, *p);
753 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
754 }
755
Alex Gartrell4a4739d52014-09-09 16:40:25 -0700756 local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
757 NULL, ipvsh, 0,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200758 IP_VS_RT_MODE_LOCAL |
759 IP_VS_RT_MODE_NON_LOCAL |
760 IP_VS_RT_MODE_RDR);
761 if (local < 0)
762 goto tx_error;
763 rt = (struct rt6_info *) skb_dst(skb);
Julian Anastasovfc604762010-10-17 16:38:15 +0300764 /*
765 * Avoid duplicate tuple in reply direction for NAT traffic
766 * to local address when connection is sync-ed
767 */
Igor Maravićc0cd1152011-12-12 02:58:24 +0000768#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Julian Anastasovfc604762010-10-17 16:38:15 +0300769 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
770 enum ip_conntrack_info ctinfo;
Alan Cox05b4b062012-10-26 00:13:27 +0000771 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Julian Anastasovfc604762010-10-17 16:38:15 +0300772
773 if (ct && !nf_ct_is_untracked(ct)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300774 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
Julian Anastasovfc604762010-10-17 16:38:15 +0300775 "ip_vs_nat_xmit_v6(): "
776 "stopping DNAT to local address");
Julian Anastasov4115ded2013-03-21 11:58:05 +0200777 goto tx_error;
Julian Anastasovfc604762010-10-17 16:38:15 +0300778 }
779 }
780#endif
781
782 /* From world but DNAT to loopback address? */
783 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
784 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300785 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
Julian Anastasovfc604762010-10-17 16:38:15 +0300786 "ip_vs_nat_xmit_v6(): "
787 "stopping DNAT to loopback address");
Julian Anastasov4115ded2013-03-21 11:58:05 +0200788 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200789 }
790
791 /* copy-on-write the packet before mangling it */
792 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200793 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200794
Changli Gaod8d1f302010-06-10 23:31:35 -0700795 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200796 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200797
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200798 /* mangle the packet */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200799 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200800 goto tx_error;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000801 ipv6_hdr(skb)->daddr = cp->daddr.in6;
Julian Anastasovfc604762010-10-17 16:38:15 +0300802
Julian Anastasov0d796412010-10-17 16:46:17 +0300803 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200804
805 /* FIXME: when application helper enlarges the packet and the length
806 is larger than the MTU of outgoing device, there will be still
807 MTU problem. */
808
809 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -0700810 skb->ignore_df = 1;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200811
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200812 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
Julian Anastasov026ace02013-03-21 11:58:06 +0200813 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200814
815 LeaveFunction(10);
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200816 return rc;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200817
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200818tx_error:
819 LeaveFunction(10);
820 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200821 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200822 return NF_STOLEN;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200823}
824#endif
825
Alex Gartrell8052ba22014-09-09 16:40:28 -0700826/* When forwarding a packet, we must ensure that we've got enough headroom
827 * for the encapsulation packet in the skb. This also gives us an
828 * opportunity to figure out what the payload_len, dsfield, ttl, and df
829 * values should be, so that we won't need to look at the old ip header
830 * again
831 */
832static struct sk_buff *
833ip_vs_prepare_tunneled_skb(struct sk_buff *skb, int skb_af,
834 unsigned int max_headroom, __u8 *next_protocol,
835 __u32 *payload_len, __u8 *dsfield, __u8 *ttl,
836 __be16 *df)
837{
838 struct sk_buff *new_skb = NULL;
839 struct iphdr *old_iph = NULL;
840#ifdef CONFIG_IP_VS_IPV6
841 struct ipv6hdr *old_ipv6h = NULL;
842#endif
843
844 if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
845 new_skb = skb_realloc_headroom(skb, max_headroom);
846 if (!new_skb)
847 goto error;
Calvin Owens50656d92014-11-04 16:37:40 -0800848 if (skb->sk)
849 skb_set_owner_w(new_skb, skb->sk);
Alex Gartrell8052ba22014-09-09 16:40:28 -0700850 consume_skb(skb);
851 skb = new_skb;
852 }
853
854#ifdef CONFIG_IP_VS_IPV6
855 if (skb_af == AF_INET6) {
856 old_ipv6h = ipv6_hdr(skb);
857 *next_protocol = IPPROTO_IPV6;
858 if (payload_len)
859 *payload_len =
860 ntohs(old_ipv6h->payload_len) +
861 sizeof(*old_ipv6h);
862 *dsfield = ipv6_get_dsfield(old_ipv6h);
863 *ttl = old_ipv6h->hop_limit;
864 if (df)
865 *df = 0;
866 } else
867#endif
868 {
869 old_iph = ip_hdr(skb);
870 /* Copy DF, reset fragment offset and MF */
871 if (df)
872 *df = (old_iph->frag_off & htons(IP_DF));
873 *next_protocol = IPPROTO_IPIP;
874
875 /* fix old IP header checksum */
876 ip_send_check(old_iph);
877 *dsfield = ipv4_get_dsfield(old_iph);
878 *ttl = old_iph->ttl;
879 if (payload_len)
880 *payload_len = ntohs(old_iph->tot_len);
881 }
882
883 return skb;
884error:
885 kfree_skb(skb);
886 return ERR_PTR(-ENOMEM);
887}
888
889static inline int __tun_gso_type_mask(int encaps_af, int orig_af)
890{
891 if (encaps_af == AF_INET) {
892 if (orig_af == AF_INET)
893 return SKB_GSO_IPIP;
894
895 return SKB_GSO_SIT;
896 }
897
898 /* GSO: we need to provide proper SKB_GSO_ value for IPv6:
899 * SKB_GSO_SIT/IPV6
900 */
901 return 0;
902}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904/*
905 * IP Tunneling transmitter
906 *
907 * This function encapsulates the packet in a new IP packet, its
908 * destination will be set to cp->daddr. Most code of this function
909 * is taken from ipip.c.
910 *
911 * It is used in VS/TUN cluster. The load balancer selects a real
912 * server from a cluster based on a scheduling algorithm,
913 * encapsulates the request packet and forwards it to the selected
914 * server. For example, all real servers are configured with
915 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
916 * the encapsulated packet, it will decapsulate the packet, processe
917 * the request and return the response packets directly to the client
918 * without passing the load balancer. This can greatly increase the
919 * scalability of virtual server.
920 *
921 * Used for ANY protocol
922 */
923int
924ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200925 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100927 struct net *net = skb_net(skb);
928 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 struct rtable *rt; /* Route to the other host */
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000930 __be32 saddr; /* Source for tunnel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 struct net_device *tdev; /* Device to other host */
Alex Gartrell8052ba22014-09-09 16:40:28 -0700932 __u8 next_protocol = 0;
933 __u8 dsfield = 0;
934 __u8 ttl = 0;
935 __be16 df = 0;
936 __be16 *dfp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 struct iphdr *iph; /* Our new IP header */
Chuck Leverc2636b42007-10-23 21:07:32 -0700938 unsigned int max_headroom; /* The extra header space needed */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200939 int ret, local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 EnterFunction(10);
942
Julian Anastasov026ace02013-03-21 11:58:06 +0200943 rcu_read_lock();
Alex Gartrell4a4739d52014-09-09 16:40:25 -0700944 local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200945 IP_VS_RT_MODE_LOCAL |
946 IP_VS_RT_MODE_NON_LOCAL |
947 IP_VS_RT_MODE_CONNECT |
Alex Gartrellc63e4de2014-09-09 16:40:27 -0700948 IP_VS_RT_MODE_TUNNEL, &saddr, ipvsh);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200949 if (local < 0)
950 goto tx_error;
Julian Anastasov026ace02013-03-21 11:58:06 +0200951 if (local) {
952 rcu_read_unlock();
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200953 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
Julian Anastasov026ace02013-03-21 11:58:06 +0200954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Julian Anastasov4115ded2013-03-21 11:58:05 +0200956 rt = skb_rtable(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700957 tdev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 /*
960 * Okay, now see if we can stuff it in the buffer as-is.
961 */
962 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
963
Alex Gartrell8052ba22014-09-09 16:40:28 -0700964 /* We only care about the df field if sysctl_pmtu_disc(ipvs) is set */
965 dfp = sysctl_pmtu_disc(ipvs) ? &df : NULL;
966 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
967 &next_protocol, NULL, &dsfield,
968 &ttl, dfp);
969 if (IS_ERR(skb))
970 goto tx_error;
Julian Anastasov4115ded2013-03-21 11:58:05 +0200971
Alex Gartrell8052ba22014-09-09 16:40:28 -0700972 skb = iptunnel_handle_offloads(
973 skb, false, __tun_gso_type_mask(AF_INET, cp->af));
Julian Anastasovea1d5d72014-08-01 10:36:17 +0300974 if (IS_ERR(skb))
975 goto tx_error;
976
977 skb->transport_header = skb->network_header;
978
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700979 skb_push(skb, sizeof(struct iphdr));
980 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /*
984 * Push down and install the IPIP header.
985 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700986 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 iph->version = 4;
988 iph->ihl = sizeof(struct iphdr)>>2;
989 iph->frag_off = df;
Alex Gartrell8052ba22014-09-09 16:40:28 -0700990 iph->protocol = next_protocol;
991 iph->tos = dsfield;
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000992 iph->daddr = cp->daddr.ip;
993 iph->saddr = saddr;
Alex Gartrell8052ba22014-09-09 16:40:28 -0700994 iph->ttl = ttl;
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100995 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -0700998 skb->ignore_df = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001000 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001001 if (ret == NF_ACCEPT)
1002 ip_local_out(skb);
1003 else if (ret == NF_DROP)
1004 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +02001005 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 LeaveFunction(10);
1008
1009 return NF_STOLEN;
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 tx_error:
Julian Anastasovea1d5d72014-08-01 10:36:17 +03001012 if (!IS_ERR(skb))
1013 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +02001014 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 LeaveFunction(10);
1016 return NF_STOLEN;
1017}
1018
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001019#ifdef CONFIG_IP_VS_IPV6
1020int
1021ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001022 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001023{
1024 struct rt6_info *rt; /* Route to the other host */
Hans Schillstrom714f0952010-10-19 10:38:48 +02001025 struct in6_addr saddr; /* Source for tunnel */
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001026 struct net_device *tdev; /* Device to other host */
Alex Gartrell8052ba22014-09-09 16:40:28 -07001027 __u8 next_protocol = 0;
1028 __u32 payload_len = 0;
1029 __u8 dsfield = 0;
1030 __u8 ttl = 0;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001031 struct ipv6hdr *iph; /* Our new IP header */
1032 unsigned int max_headroom; /* The extra header space needed */
Julian Anastasov4115ded2013-03-21 11:58:05 +02001033 int ret, local;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001034
1035 EnterFunction(10);
1036
Julian Anastasov026ace02013-03-21 11:58:06 +02001037 rcu_read_lock();
Alex Gartrell4a4739d52014-09-09 16:40:25 -07001038 local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
Julian Anastasov4115ded2013-03-21 11:58:05 +02001039 &saddr, ipvsh, 1,
1040 IP_VS_RT_MODE_LOCAL |
1041 IP_VS_RT_MODE_NON_LOCAL |
1042 IP_VS_RT_MODE_TUNNEL);
1043 if (local < 0)
1044 goto tx_error;
Julian Anastasov026ace02013-03-21 11:58:06 +02001045 if (local) {
1046 rcu_read_unlock();
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001047 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
Julian Anastasov026ace02013-03-21 11:58:06 +02001048 }
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001049
Julian Anastasov4115ded2013-03-21 11:58:05 +02001050 rt = (struct rt6_info *) skb_dst(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001051 tdev = rt->dst.dev;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001052
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001053 /*
1054 * Okay, now see if we can stuff it in the buffer as-is.
1055 */
1056 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
1057
Alex Gartrell8052ba22014-09-09 16:40:28 -07001058 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1059 &next_protocol, &payload_len,
1060 &dsfield, &ttl, NULL);
1061 if (IS_ERR(skb))
1062 goto tx_error;
Julian Anastasov4115ded2013-03-21 11:58:05 +02001063
Alex Gartrell8052ba22014-09-09 16:40:28 -07001064 skb = iptunnel_handle_offloads(
1065 skb, false, __tun_gso_type_mask(AF_INET6, cp->af));
Julian Anastasovea1d5d72014-08-01 10:36:17 +03001066 if (IS_ERR(skb))
1067 goto tx_error;
1068
Hans Schillstrom714f0952010-10-19 10:38:48 +02001069 skb->transport_header = skb->network_header;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001070
1071 skb_push(skb, sizeof(struct ipv6hdr));
1072 skb_reset_network_header(skb);
1073 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1074
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001075 /*
1076 * Push down and install the IPIP header.
1077 */
1078 iph = ipv6_hdr(skb);
1079 iph->version = 6;
Alex Gartrell8052ba22014-09-09 16:40:28 -07001080 iph->nexthdr = next_protocol;
1081 iph->payload_len = htons(payload_len);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001082 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
Alex Gartrell8052ba22014-09-09 16:40:28 -07001083 ipv6_change_dsfield(iph, 0, dsfield);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001084 iph->daddr = cp->daddr.in6;
1085 iph->saddr = saddr;
Alex Gartrell8052ba22014-09-09 16:40:28 -07001086 iph->hop_limit = ttl;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001087
1088 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -07001089 skb->ignore_df = 1;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001090
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001091 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001092 if (ret == NF_ACCEPT)
1093 ip6_local_out(skb);
1094 else if (ret == NF_DROP)
1095 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +02001096 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001097
1098 LeaveFunction(10);
1099
1100 return NF_STOLEN;
1101
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001102tx_error:
Julian Anastasovea1d5d72014-08-01 10:36:17 +03001103 if (!IS_ERR(skb))
1104 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +02001105 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001106 LeaveFunction(10);
1107 return NF_STOLEN;
1108}
1109#endif
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112/*
1113 * Direct Routing transmitter
1114 * Used for ANY protocol
1115 */
1116int
1117ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001118 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Julian Anastasov4115ded2013-03-21 11:58:05 +02001120 int local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 EnterFunction(10);
1123
Julian Anastasov026ace02013-03-21 11:58:06 +02001124 rcu_read_lock();
Alex Gartrell4a4739d52014-09-09 16:40:25 -07001125 local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip,
Julian Anastasov4115ded2013-03-21 11:58:05 +02001126 IP_VS_RT_MODE_LOCAL |
1127 IP_VS_RT_MODE_NON_LOCAL |
Alex Gartrellc63e4de2014-09-09 16:40:27 -07001128 IP_VS_RT_MODE_KNOWN_NH, NULL, ipvsh);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001129 if (local < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 goto tx_error;
Julian Anastasov026ace02013-03-21 11:58:06 +02001131 if (local) {
1132 rcu_read_unlock();
Julian Anastasov4115ded2013-03-21 11:58:05 +02001133 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
Julian Anastasov026ace02013-03-21 11:58:06 +02001134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001136 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -07001139 skb->ignore_df = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001141 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
Julian Anastasov026ace02013-03-21 11:58:06 +02001142 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 LeaveFunction(10);
1145 return NF_STOLEN;
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 tx_error:
1148 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +02001149 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 LeaveFunction(10);
1151 return NF_STOLEN;
1152}
1153
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001154#ifdef CONFIG_IP_VS_IPV6
1155int
1156ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
Julian Anastasov4115ded2013-03-21 11:58:05 +02001157 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001158{
Julian Anastasov4115ded2013-03-21 11:58:05 +02001159 int local;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001160
1161 EnterFunction(10);
1162
Julian Anastasov026ace02013-03-21 11:58:06 +02001163 rcu_read_lock();
Alex Gartrell4a4739d52014-09-09 16:40:25 -07001164 local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
1165 NULL, ipvsh, 0,
Julian Anastasov4115ded2013-03-21 11:58:05 +02001166 IP_VS_RT_MODE_LOCAL |
1167 IP_VS_RT_MODE_NON_LOCAL);
1168 if (local < 0)
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001169 goto tx_error;
Julian Anastasov026ace02013-03-21 11:58:06 +02001170 if (local) {
1171 rcu_read_unlock();
Julian Anastasov4115ded2013-03-21 11:58:05 +02001172 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
Julian Anastasov026ace02013-03-21 11:58:06 +02001173 }
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001174
1175 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -07001176 skb->ignore_df = 1;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001177
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001178 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
Julian Anastasov026ace02013-03-21 11:58:06 +02001179 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001180
1181 LeaveFunction(10);
1182 return NF_STOLEN;
1183
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001184tx_error:
1185 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +02001186 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001187 LeaveFunction(10);
1188 return NF_STOLEN;
1189}
1190#endif
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193/*
1194 * ICMP packet transmitter
1195 * called by the ip_vs_in_icmp
1196 */
1197int
1198ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001199 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1200 struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 struct rtable *rt; /* Route to the other host */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 int rc;
Julian Anastasovfc604762010-10-17 16:38:15 +03001204 int local;
Julian Anastasov4115ded2013-03-21 11:58:05 +02001205 int rt_mode, was_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 EnterFunction(10);
1208
1209 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1210 forwarded directly here, because there is no need to
1211 translate address/port back */
1212 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1213 if (cp->packet_xmit)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001214 rc = cp->packet_xmit(skb, cp, pp, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 else
1216 rc = NF_ACCEPT;
1217 /* do not touch skb anymore */
1218 atomic_inc(&cp->in_pkts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 goto out;
1220 }
1221
1222 /*
1223 * mangle and send the packet here (only for VS/NAT)
1224 */
Julian Anastasov4115ded2013-03-21 11:58:05 +02001225 was_input = rt_is_input_route(skb_rtable(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001227 /* LOCALNODE from FORWARD hook is not supported */
1228 rt_mode = (hooknum != NF_INET_FORWARD) ?
1229 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1230 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
Julian Anastasov026ace02013-03-21 11:58:06 +02001231 rcu_read_lock();
Alex Gartrell4a4739d52014-09-09 16:40:25 -07001232 local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip, rt_mode,
Alex Gartrellc63e4de2014-09-09 16:40:27 -07001233 NULL, iph);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001234 if (local < 0)
1235 goto tx_error;
1236 rt = skb_rtable(skb);
Julian Anastasovfc604762010-10-17 16:38:15 +03001237
1238 /*
1239 * Avoid duplicate tuple in reply direction for NAT traffic
1240 * to local address when connection is sync-ed
1241 */
Igor Maravićc0cd1152011-12-12 02:58:24 +00001242#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Julian Anastasovfc604762010-10-17 16:38:15 +03001243 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1244 enum ip_conntrack_info ctinfo;
Alan Cox05b4b062012-10-26 00:13:27 +00001245 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Julian Anastasovfc604762010-10-17 16:38:15 +03001246
1247 if (ct && !nf_ct_is_untracked(ct)) {
1248 IP_VS_DBG(10, "%s(): "
1249 "stopping DNAT to local address %pI4\n",
1250 __func__, &cp->daddr.ip);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001251 goto tx_error;
Julian Anastasovfc604762010-10-17 16:38:15 +03001252 }
1253 }
1254#endif
1255
1256 /* From world but DNAT to loopback address? */
Julian Anastasov4115ded2013-03-21 11:58:05 +02001257 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
Julian Anastasovfc604762010-10-17 16:38:15 +03001258 IP_VS_DBG(1, "%s(): "
1259 "stopping DNAT to loopback %pI4\n",
1260 __func__, &cp->daddr.ip);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001261 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263
1264 /* copy-on-write the packet before mangling it */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -07001265 if (!skb_make_writable(skb, offset))
Julian Anastasov4115ded2013-03-21 11:58:05 +02001266 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Changli Gaod8d1f302010-06-10 23:31:35 -07001268 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julian Anastasov4115ded2013-03-21 11:58:05 +02001269 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 ip_vs_nat_icmp(skb, pp, cp, 0);
1272
1273 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -07001274 skb->ignore_df = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001276 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
Julian Anastasov026ace02013-03-21 11:58:06 +02001277 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 goto out;
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 tx_error:
Julian Anastasov026ace02013-03-21 11:58:06 +02001281 kfree_skb(skb);
1282 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 rc = NF_STOLEN;
1284 out:
1285 LeaveFunction(10);
1286 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001288
1289#ifdef CONFIG_IP_VS_IPV6
1290int
1291ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001292 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
Julian Anastasov4115ded2013-03-21 11:58:05 +02001293 struct ip_vs_iphdr *ipvsh)
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001294{
1295 struct rt6_info *rt; /* Route to the other host */
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001296 int rc;
Julian Anastasovfc604762010-10-17 16:38:15 +03001297 int local;
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001298 int rt_mode;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001299
1300 EnterFunction(10);
1301
1302 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1303 forwarded directly here, because there is no need to
1304 translate address/port back */
1305 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1306 if (cp->packet_xmit)
Julian Anastasov4115ded2013-03-21 11:58:05 +02001307 rc = cp->packet_xmit(skb, cp, pp, ipvsh);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001308 else
1309 rc = NF_ACCEPT;
1310 /* do not touch skb anymore */
1311 atomic_inc(&cp->in_pkts);
1312 goto out;
1313 }
1314
1315 /*
1316 * mangle and send the packet here (only for VS/NAT)
1317 */
1318
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001319 /* LOCALNODE from FORWARD hook is not supported */
1320 rt_mode = (hooknum != NF_INET_FORWARD) ?
1321 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1322 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
Julian Anastasov026ace02013-03-21 11:58:06 +02001323 rcu_read_lock();
Alex Gartrell4a4739d52014-09-09 16:40:25 -07001324 local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
1325 NULL, ipvsh, 0, rt_mode);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001326 if (local < 0)
1327 goto tx_error;
1328 rt = (struct rt6_info *) skb_dst(skb);
Julian Anastasovfc604762010-10-17 16:38:15 +03001329 /*
1330 * Avoid duplicate tuple in reply direction for NAT traffic
1331 * to local address when connection is sync-ed
1332 */
Igor Maravićc0cd1152011-12-12 02:58:24 +00001333#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Julian Anastasovfc604762010-10-17 16:38:15 +03001334 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1335 enum ip_conntrack_info ctinfo;
Alan Cox05b4b062012-10-26 00:13:27 +00001336 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Julian Anastasovfc604762010-10-17 16:38:15 +03001337
1338 if (ct && !nf_ct_is_untracked(ct)) {
1339 IP_VS_DBG(10, "%s(): "
1340 "stopping DNAT to local address %pI6\n",
1341 __func__, &cp->daddr.in6);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001342 goto tx_error;
Julian Anastasovfc604762010-10-17 16:38:15 +03001343 }
1344 }
1345#endif
1346
1347 /* From world but DNAT to loopback address? */
1348 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1349 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
1350 IP_VS_DBG(1, "%s(): "
1351 "stopping DNAT to loopback %pI6\n",
1352 __func__, &cp->daddr.in6);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001353 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001354 }
1355
1356 /* copy-on-write the packet before mangling it */
1357 if (!skb_make_writable(skb, offset))
Julian Anastasov4115ded2013-03-21 11:58:05 +02001358 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001359
Changli Gaod8d1f302010-06-10 23:31:35 -07001360 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julian Anastasov4115ded2013-03-21 11:58:05 +02001361 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001362
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001363 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1364
1365 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -07001366 skb->ignore_df = 1;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001367
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001368 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
Julian Anastasov026ace02013-03-21 11:58:06 +02001369 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001370 goto out;
1371
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001372tx_error:
Julian Anastasov026ace02013-03-21 11:58:06 +02001373 kfree_skb(skb);
1374 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001375 rc = NF_STOLEN;
1376out:
1377 LeaveFunction(10);
1378 return rc;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001379}
1380#endif