blob: 01d3d894de4630b345084c8e0079a235923974b5 [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;
Julian Anastasovad4d3ef2012-10-08 11:41:20 +0000133 fl4.flowi4_flags = (rt_mode & IP_VS_RT_MODE_KNOWN_NH) ?
134 FLOWI_FLAG_KNOWN_NH : 0;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300135
136retry:
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;
Julian Anastasovc90558d2013-03-21 11:57:59 +0200143 flowi4_update_output(&fl4, 0, 0, daddr, 0);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300144 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;
Julian Anastasovc90558d2013-03-21 11:57:59 +0200151 flowi4_update_output(&fl4, 0, 0, daddr, fl4.saddr);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300152 loop++;
153 goto retry;
154 }
155 *saddr = fl4.saddr;
156 return rt;
157}
158
Alex Gartrell4a4739d52014-09-09 16:40:25 -0700159#ifdef CONFIG_IP_VS_IPV6
160static 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
166static 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
Alex Gartrell919aa0b2014-09-09 16:40:26 -0700206static 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
Eric Dumazeta8399232015-03-16 21:06:15 -0700211 if (!skb->dev && sk && sk_fullsock(sk))
Alex Gartrell919aa0b2014-09-09 16:40:26 -0700212 ort->dst.ops->update_pmtu(&ort->dst, sk, NULL, mtu);
213}
214
Eric W. Biederman20868a42015-09-21 13:02:47 -0500215static inline bool ensure_mtu_is_adequate(struct netns_ipvs *ipvs, int skb_af,
216 int rt_mode,
Alex Gartrellc63e4de2014-09-09 16:40:27 -0700217 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) {
Eric W. Biederman20868a42015-09-21 13:02:47 -0500222 struct net *net = ipvs->net;
Alex Gartrellc63e4de2014-09-09 16:40:27 -0700223
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 */
Alex Gartrell89621f32015-08-26 09:40:38 -0700228 if (!ipvsh->fragoffs && !ip_vs_iph_icmp(ipvsh))
Alex Gartrellc63e4de2014-09-09 16:40:27 -0700229 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 {
Alex Gartrellc63e4de2014-09-09 16:40:27 -0700237 /* If we're going to tunnel the packet and pmtu discovery
238 * is disabled, we'll just fragment it anyway
239 */
240 if ((rt_mode & IP_VS_RT_MODE_TUNNEL) && !sysctl_pmtu_disc(ipvs))
241 return true;
242
243 if (unlikely(ip_hdr(skb)->frag_off & htons(IP_DF) &&
Alex Gartrell89621f32015-08-26 09:40:38 -0700244 skb->len > mtu && !skb_is_gso(skb) &&
245 !ip_vs_iph_icmp(ipvsh))) {
Alex Gartrellc63e4de2014-09-09 16:40:27 -0700246 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
Changli Gao17a8f8e2011-02-24 08:19:57 +0800257/* Get route to destination or remote server */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200258static int
Eric W. Biedermanecfe87b2015-09-21 13:02:45 -0500259__ip_vs_get_out_rt(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,
260 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{
Eric W. Biedermanecfe87b2015-09-21 13:02:45 -0500264 struct net *net = ipvs->net;
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
Eric W. Biederman20868a42015-09-21 13:02:47 -0500340 if (!ensure_mtu_is_adequate(ipvs, 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,
Martin KaFai Lau48e8aa62015-05-22 20:56:02 -0700367 struct in6_addr *ret_saddr, int do_xfrm, int rt_mode)
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
Martin KaFai Lau48e8aa62015-05-22 20:56:02 -0700374 if (rt_mode & IP_VS_RT_MODE_KNOWN_NH)
375 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
376
David S. Miller4c9483b2011-03-12 16:22:43 -0500377 dst = ip6_route_output(net, NULL, &fl6);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200378 if (dst->error)
379 goto out_err;
380 if (!ret_saddr)
381 return dst;
David S. Miller4c9483b2011-03-12 16:22:43 -0500382 if (ipv6_addr_any(&fl6.saddr) &&
Hans Schillstrom714f0952010-10-19 10:38:48 +0200383 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
David S. Miller4c9483b2011-03-12 16:22:43 -0500384 &fl6.daddr, 0, &fl6.saddr) < 0)
Hans Schillstrom714f0952010-10-19 10:38:48 +0200385 goto out_err;
David S. Miller452edd52011-03-02 13:27:41 -0800386 if (do_xfrm) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500387 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
David S. Miller452edd52011-03-02 13:27:41 -0800388 if (IS_ERR(dst)) {
389 dst = NULL;
390 goto out_err;
391 }
392 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000393 *ret_saddr = fl6.saddr;
Hans Schillstrom714f0952010-10-19 10:38:48 +0200394 return dst;
395
396out_err:
397 dst_release(dst);
398 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
399 return NULL;
400}
401
Julian Anastasovfc604762010-10-17 16:38:15 +0300402/*
403 * Get route to destination or remote server
Julian Anastasovfc604762010-10-17 16:38:15 +0300404 */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200405static int
Eric W. Biedermanf5745f82015-09-21 13:02:46 -0500406__ip_vs_get_out_rt_v6(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,
407 struct ip_vs_dest *dest,
Julian Anastasovfc604762010-10-17 16:38:15 +0300408 struct in6_addr *daddr, struct in6_addr *ret_saddr,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200409 struct ip_vs_iphdr *ipvsh, int do_xfrm, int rt_mode)
Hans Schillstrom714f0952010-10-19 10:38:48 +0200410{
Eric W. Biedermanf5745f82015-09-21 13:02:46 -0500411 struct net *net = ipvs->net;
Julian Anastasov026ace02013-03-21 11:58:06 +0200412 struct ip_vs_dest_dst *dest_dst;
Julius Volz38cdcc92008-09-02 15:55:44 +0200413 struct rt6_info *rt; /* Route to the other host */
Hans Schillstrom714f0952010-10-19 10:38:48 +0200414 struct dst_entry *dst;
Julian Anastasov4115ded2013-03-21 11:58:05 +0200415 int mtu;
Julian Anastasov026ace02013-03-21 11:58:06 +0200416 int local, noref = 1;
Julius Volz38cdcc92008-09-02 15:55:44 +0200417
418 if (dest) {
Julian Anastasov026ace02013-03-21 11:58:06 +0200419 dest_dst = __ip_vs_dst_check(dest);
420 if (likely(dest_dst))
421 rt = (struct rt6_info *) dest_dst->dst_cache;
422 else {
Hans Schillstrom714f0952010-10-19 10:38:48 +0200423 u32 cookie;
Julius Volz38cdcc92008-09-02 15:55:44 +0200424
Julian Anastasov026ace02013-03-21 11:58:06 +0200425 dest_dst = ip_vs_dest_dst_alloc();
Julian Anastasovac692692013-03-22 11:46:54 +0200426 spin_lock_bh(&dest->dst_lock);
Julian Anastasov026ace02013-03-21 11:58:06 +0200427 if (!dest_dst) {
428 __ip_vs_dst_set(dest, NULL, NULL, 0);
Julian Anastasovac692692013-03-22 11:46:54 +0200429 spin_unlock_bh(&dest->dst_lock);
Julian Anastasov026ace02013-03-21 11:58:06 +0200430 goto err_unreach;
431 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200432 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
Julian Anastasov026ace02013-03-21 11:58:06 +0200433 &dest_dst->dst_saddr.in6,
Martin KaFai Lau48e8aa62015-05-22 20:56:02 -0700434 do_xfrm, rt_mode);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200435 if (!dst) {
Julian Anastasov026ace02013-03-21 11:58:06 +0200436 __ip_vs_dst_set(dest, NULL, NULL, 0);
Julian Anastasovac692692013-03-22 11:46:54 +0200437 spin_unlock_bh(&dest->dst_lock);
Julian Anastasov026ace02013-03-21 11:58:06 +0200438 ip_vs_dest_dst_free(dest_dst);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200439 goto err_unreach;
Julius Volz38cdcc92008-09-02 15:55:44 +0200440 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200441 rt = (struct rt6_info *) dst;
Martin KaFai Laub197df42015-05-22 20:56:01 -0700442 cookie = rt6_get_cookie(rt);
Julian Anastasov026ace02013-03-21 11:58:06 +0200443 __ip_vs_dst_set(dest, dest_dst, &rt->dst, cookie);
Julian Anastasovac692692013-03-22 11:46:54 +0200444 spin_unlock_bh(&dest->dst_lock);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200445 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
Julian Anastasov026ace02013-03-21 11:58:06 +0200446 &dest->addr.in6, &dest_dst->dst_saddr.in6,
Changli Gaod8d1f302010-06-10 23:31:35 -0700447 atomic_read(&rt->dst.__refcnt));
Julius Volz38cdcc92008-09-02 15:55:44 +0200448 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200449 if (ret_saddr)
Julian Anastasov026ace02013-03-21 11:58:06 +0200450 *ret_saddr = dest_dst->dst_saddr.in6;
Julius Volz38cdcc92008-09-02 15:55:44 +0200451 } else {
Julian Anastasov026ace02013-03-21 11:58:06 +0200452 noref = 0;
Martin KaFai Lau48e8aa62015-05-22 20:56:02 -0700453 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm,
454 rt_mode);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200455 if (!dst)
Julian Anastasov4115ded2013-03-21 11:58:05 +0200456 goto err_unreach;
Hans Schillstrom714f0952010-10-19 10:38:48 +0200457 rt = (struct rt6_info *) dst;
Julius Volz38cdcc92008-09-02 15:55:44 +0200458 }
459
Julian Anastasovfc604762010-10-17 16:38:15 +0300460 local = __ip_vs_is_local_route6(rt);
Alex Gartrell4a4739d52014-09-09 16:40:25 -0700461
462 if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
463 local))) {
464 IP_VS_DBG_RL("We are crossing local and non-local addresses"
Alex Gartrell3d536662014-10-06 08:46:19 -0700465 " daddr=%pI6\n", daddr);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200466 goto err_put;
Julian Anastasovfc604762010-10-17 16:38:15 +0300467 }
Alex Gartrell4a4739d52014-09-09 16:40:25 -0700468
469 if (unlikely(local)) {
Julian Anastasov4115ded2013-03-21 11:58:05 +0200470 /* skb to local stack, preserve old route */
Julian Anastasov026ace02013-03-21 11:58:06 +0200471 if (!noref)
472 dst_release(&rt->dst);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200473 return local;
Julian Anastasovfc604762010-10-17 16:38:15 +0300474 }
475
Julian Anastasov4115ded2013-03-21 11:58:05 +0200476 /* MTU checking */
477 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL)))
478 mtu = dst_mtu(&rt->dst);
479 else {
Julian Anastasov4115ded2013-03-21 11:58:05 +0200480 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
481 if (mtu < IPV6_MIN_MTU) {
482 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
483 IPV6_MIN_MTU);
484 goto err_put;
485 }
Alex Gartrell919aa0b2014-09-09 16:40:26 -0700486 maybe_update_pmtu(skb_af, skb, mtu);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200487 }
488
Eric W. Biederman20868a42015-09-21 13:02:47 -0500489 if (!ensure_mtu_is_adequate(ipvs, skb_af, rt_mode, ipvsh, skb, mtu))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200490 goto err_put;
Julian Anastasov4115ded2013-03-21 11:58:05 +0200491
492 skb_dst_drop(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200493 if (noref) {
494 if (!local)
Hannes Frederic Sowadbfc4fb2014-12-06 19:19:42 +0100495 skb_dst_set_noref(skb, &rt->dst);
Julian Anastasov026ace02013-03-21 11:58:06 +0200496 else
497 skb_dst_set(skb, dst_clone(&rt->dst));
498 } else
499 skb_dst_set(skb, &rt->dst);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200500
501 return local;
502
503err_put:
Julian Anastasov026ace02013-03-21 11:58:06 +0200504 if (!noref)
505 dst_release(&rt->dst);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200506 return -1;
507
508err_unreach:
Alex Gartrell326bf172015-06-26 03:18:45 -0700509 /* The ip6_link_failure function requires the dev field to be set
510 * in order to get the net (further for the sake of fwmark
511 * reflection).
512 */
513 if (!skb->dev)
514 skb->dev = skb_dst(skb)->dev;
515
Julian Anastasov4115ded2013-03-21 11:58:05 +0200516 dst_link_failure(skb);
517 return -1;
Julius Volz38cdcc92008-09-02 15:55:44 +0200518}
519#endif
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200522/* return NF_ACCEPT to allow forwarding or other NF_xxx on error */
523static inline int ip_vs_tunnel_xmit_prepare(struct sk_buff *skb,
524 struct ip_vs_conn *cp)
525{
526 int ret = NF_ACCEPT;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200527
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200528 skb->ipvs_property = 1;
529 if (unlikely(cp->flags & IP_VS_CONN_F_NFCT))
530 ret = ip_vs_confirm_conntrack(skb);
531 if (ret == NF_ACCEPT) {
532 nf_reset(skb);
533 skb_forward_csum(skb);
534 }
535 return ret;
536}
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200537
Alex Gartrell71563f32015-07-05 14:28:26 -0700538/* In the event of a remote destination, it's possible that we would have
539 * matches against an old socket (particularly a TIME-WAIT socket). This
540 * causes havoc down the line (ip_local_out et. al. expect regular sockets
541 * and invalid memory accesses will happen) so simply drop the association
542 * in this case.
543*/
544static inline void ip_vs_drop_early_demux_sk(struct sk_buff *skb)
545{
546 /* If dev is set, the packet came from the LOCAL_IN callback and
547 * not from a local TCP socket.
548 */
549 if (skb->dev)
550 skb_orphan(skb);
551}
552
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200553/* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
554static inline int ip_vs_nat_send_or_cont(int pf, struct sk_buff *skb,
555 struct ip_vs_conn *cp, int local)
556{
557 int ret = NF_STOLEN;
558
559 skb->ipvs_property = 1;
560 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
561 ip_vs_notrack(skb);
562 else
563 ip_vs_update_conntrack(skb, cp, 1);
Alex Gartrell71563f32015-07-05 14:28:26 -0700564
565 /* Remove the early_demux association unless it's bound for the
566 * exact same port and address on this host after translation.
567 */
568 if (!local || cp->vport != cp->dport ||
569 !ip_vs_addr_equal(cp->af, &cp->vaddr, &cp->daddr))
570 ip_vs_drop_early_demux_sk(skb);
571
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200572 if (!local) {
573 skb_forward_csum(skb);
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -0500574 NF_HOOK(pf, NF_INET_LOCAL_OUT, cp->ipvs->net, NULL, skb,
Eric W. Biederman13206b62015-10-07 16:48:35 -0500575 NULL, skb_dst(skb)->dev, dst_output);
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200576 } else
577 ret = NF_ACCEPT;
Alex Gartrell71563f32015-07-05 14:28:26 -0700578
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200579 return ret;
580}
581
582/* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
583static inline int ip_vs_send_or_cont(int pf, struct sk_buff *skb,
584 struct ip_vs_conn *cp, int local)
585{
586 int ret = NF_STOLEN;
587
588 skb->ipvs_property = 1;
589 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
590 ip_vs_notrack(skb);
591 if (!local) {
Alex Gartrell71563f32015-07-05 14:28:26 -0700592 ip_vs_drop_early_demux_sk(skb);
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200593 skb_forward_csum(skb);
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -0500594 NF_HOOK(pf, NF_INET_LOCAL_OUT, cp->ipvs->net, NULL, skb,
Eric W. Biederman13206b62015-10-07 16:48:35 -0500595 NULL, skb_dst(skb)->dev, dst_output);
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200596 } else
597 ret = NF_ACCEPT;
598 return ret;
599}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601
602/*
603 * NULL transmitter (do nothing except return NF_ACCEPT)
604 */
605int
606ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200607 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 /* we do not touch skb and do not need pskb ptr */
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200610 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613
614/*
615 * Bypass transmitter
616 * Let packets bypass the destination when the destination is not
617 * available, it may be only used in transparent cache cluster.
618 */
619int
620ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200621 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700623 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 EnterFunction(10);
626
Julian Anastasov026ace02013-03-21 11:58:06 +0200627 rcu_read_lock();
Eric W. Biedermanecfe87b2015-09-21 13:02:45 -0500628 if (__ip_vs_get_out_rt(cp->ipvs, cp->af, skb, NULL, iph->daddr,
Alex Gartrellc63e4de2014-09-09 16:40:27 -0700629 IP_VS_RT_MODE_NON_LOCAL, NULL, ipvsh) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Julian Anastasov4115ded2013-03-21 11:58:05 +0200632 ip_send_check(iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -0700635 skb->ignore_df = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200637 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
Julian Anastasov026ace02013-03-21 11:58:06 +0200638 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 LeaveFunction(10);
641 return NF_STOLEN;
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 tx_error:
644 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200645 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 LeaveFunction(10);
647 return NF_STOLEN;
648}
649
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200650#ifdef CONFIG_IP_VS_IPV6
651int
652ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200653 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200654{
Alex Gartrell34818942015-08-26 09:40:35 -0700655 struct ipv6hdr *iph = ipv6_hdr(skb);
656
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200657 EnterFunction(10);
658
Julian Anastasov026ace02013-03-21 11:58:06 +0200659 rcu_read_lock();
Eric W. Biedermanf5745f82015-09-21 13:02:46 -0500660 if (__ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, NULL,
661 &iph->daddr, NULL,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200662 ipvsh, 0, IP_VS_RT_MODE_NON_LOCAL) < 0)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200663 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200664
665 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -0700666 skb->ignore_df = 1;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200667
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200668 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
Julian Anastasov026ace02013-03-21 11:58:06 +0200669 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200670
671 LeaveFunction(10);
672 return NF_STOLEN;
673
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200674 tx_error:
675 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200676 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200677 LeaveFunction(10);
678 return NF_STOLEN;
679}
680#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682/*
683 * NAT transmitter (only for outside-to-inside nat forwarding)
684 * Not used for related ICMP
685 */
686int
687ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200688 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 struct rtable *rt; /* Route to the other host */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200691 int local, rc, was_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 EnterFunction(10);
694
Julian Anastasov026ace02013-03-21 11:58:06 +0200695 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* check if it is a connection of no-client-port */
697 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
Al Viro014d7302006-09-28 14:29:52 -0700698 __be16 _pt, *p;
Julian Anastasov4115ded2013-03-21 11:58:05 +0200699
700 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (p == NULL)
702 goto tx_error;
703 ip_vs_conn_fill_cport(cp, *p);
704 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
705 }
706
Julian Anastasov4115ded2013-03-21 11:58:05 +0200707 was_input = rt_is_input_route(skb_rtable(skb));
Eric W. Biedermanecfe87b2015-09-21 13:02:45 -0500708 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200709 IP_VS_RT_MODE_LOCAL |
710 IP_VS_RT_MODE_NON_LOCAL |
Alex Gartrellc63e4de2014-09-09 16:40:27 -0700711 IP_VS_RT_MODE_RDR, NULL, ipvsh);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200712 if (local < 0)
713 goto tx_error;
714 rt = skb_rtable(skb);
Julian Anastasovfc604762010-10-17 16:38:15 +0300715 /*
716 * Avoid duplicate tuple in reply direction for NAT traffic
717 * to local address when connection is sync-ed
718 */
Igor Maravićc0cd1152011-12-12 02:58:24 +0000719#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Julian Anastasovfc604762010-10-17 16:38:15 +0300720 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
721 enum ip_conntrack_info ctinfo;
Alan Cox05b4b062012-10-26 00:13:27 +0000722 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Julian Anastasovfc604762010-10-17 16:38:15 +0300723
724 if (ct && !nf_ct_is_untracked(ct)) {
Alex Gartrellb0e010c2015-08-26 09:40:28 -0700725 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, ipvsh->off,
Julian Anastasov0d796412010-10-17 16:46:17 +0300726 "ip_vs_nat_xmit(): "
Julian Anastasovfc604762010-10-17 16:38:15 +0300727 "stopping DNAT to local address");
Julian Anastasov4115ded2013-03-21 11:58:05 +0200728 goto tx_error;
Julian Anastasovfc604762010-10-17 16:38:15 +0300729 }
730 }
731#endif
732
733 /* From world but DNAT to loopback address? */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200734 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
Alex Gartrellb0e010c2015-08-26 09:40:28 -0700735 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, ipvsh->off,
736 "ip_vs_nat_xmit(): stopping DNAT to loopback "
737 "address");
Julian Anastasov4115ded2013-03-21 11:58:05 +0200738 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740
741 /* copy-on-write the packet before mangling it */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -0700742 if (!skb_make_writable(skb, sizeof(struct iphdr)))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200743 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Changli Gaod8d1f302010-06-10 23:31:35 -0700745 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200746 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /* mangle the packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200749 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200750 goto tx_error;
Julius Volze7ade462008-09-02 15:55:33 +0200751 ip_hdr(skb)->daddr = cp->daddr.ip;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700752 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Alex Gartrellb0e010c2015-08-26 09:40:28 -0700754 IP_VS_DBG_PKT(10, AF_INET, pp, skb, ipvsh->off, "After DNAT");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /* FIXME: when application helper enlarges the packet and the length
757 is larger than the MTU of outgoing device, there will be still
758 MTU problem. */
759
760 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -0700761 skb->ignore_df = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200763 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
Julian Anastasov026ace02013-03-21 11:58:06 +0200764 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 LeaveFunction(10);
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200767 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 tx_error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200771 rcu_read_unlock();
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200772 LeaveFunction(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200776#ifdef CONFIG_IP_VS_IPV6
777int
778ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200779 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200780{
781 struct rt6_info *rt; /* Route to the other host */
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200782 int local, rc;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200783
784 EnterFunction(10);
785
Julian Anastasov026ace02013-03-21 11:58:06 +0200786 rcu_read_lock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200787 /* check if it is a connection of no-client-port */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200788 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT && !ipvsh->fragoffs)) {
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200789 __be16 _pt, *p;
Julian Anastasov4115ded2013-03-21 11:58:05 +0200790 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200791 if (p == NULL)
792 goto tx_error;
793 ip_vs_conn_fill_cport(cp, *p);
794 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
795 }
796
Eric W. Biedermanf5745f82015-09-21 13:02:46 -0500797 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
798 &cp->daddr.in6,
Alex Gartrell4a4739d52014-09-09 16:40:25 -0700799 NULL, ipvsh, 0,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200800 IP_VS_RT_MODE_LOCAL |
801 IP_VS_RT_MODE_NON_LOCAL |
802 IP_VS_RT_MODE_RDR);
803 if (local < 0)
804 goto tx_error;
805 rt = (struct rt6_info *) skb_dst(skb);
Julian Anastasovfc604762010-10-17 16:38:15 +0300806 /*
807 * Avoid duplicate tuple in reply direction for NAT traffic
808 * to local address when connection is sync-ed
809 */
Igor Maravićc0cd1152011-12-12 02:58:24 +0000810#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Julian Anastasovfc604762010-10-17 16:38:15 +0300811 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
812 enum ip_conntrack_info ctinfo;
Alan Cox05b4b062012-10-26 00:13:27 +0000813 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Julian Anastasovfc604762010-10-17 16:38:15 +0300814
815 if (ct && !nf_ct_is_untracked(ct)) {
Alex Gartrellb0e010c2015-08-26 09:40:28 -0700816 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, ipvsh->off,
Julian Anastasovfc604762010-10-17 16:38:15 +0300817 "ip_vs_nat_xmit_v6(): "
818 "stopping DNAT to local address");
Julian Anastasov4115ded2013-03-21 11:58:05 +0200819 goto tx_error;
Julian Anastasovfc604762010-10-17 16:38:15 +0300820 }
821 }
822#endif
823
824 /* From world but DNAT to loopback address? */
825 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
Martin KaFai Laufd0273d2015-05-22 20:55:57 -0700826 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
Alex Gartrellb0e010c2015-08-26 09:40:28 -0700827 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, ipvsh->off,
Julian Anastasovfc604762010-10-17 16:38:15 +0300828 "ip_vs_nat_xmit_v6(): "
829 "stopping DNAT to loopback address");
Julian Anastasov4115ded2013-03-21 11:58:05 +0200830 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200831 }
832
833 /* copy-on-write the packet before mangling it */
834 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200835 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200836
Changli Gaod8d1f302010-06-10 23:31:35 -0700837 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200838 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200839
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200840 /* mangle the packet */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200841 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200842 goto tx_error;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000843 ipv6_hdr(skb)->daddr = cp->daddr.in6;
Julian Anastasovfc604762010-10-17 16:38:15 +0300844
Alex Gartrellb0e010c2015-08-26 09:40:28 -0700845 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, ipvsh->off, "After DNAT");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200846
847 /* FIXME: when application helper enlarges the packet and the length
848 is larger than the MTU of outgoing device, there will be still
849 MTU problem. */
850
851 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -0700852 skb->ignore_df = 1;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200853
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200854 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
Julian Anastasov026ace02013-03-21 11:58:06 +0200855 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200856
857 LeaveFunction(10);
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200858 return rc;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200859
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200860tx_error:
861 LeaveFunction(10);
862 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200863 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200864 return NF_STOLEN;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200865}
866#endif
867
Alex Gartrell8052ba22014-09-09 16:40:28 -0700868/* When forwarding a packet, we must ensure that we've got enough headroom
869 * for the encapsulation packet in the skb. This also gives us an
870 * opportunity to figure out what the payload_len, dsfield, ttl, and df
871 * values should be, so that we won't need to look at the old ip header
872 * again
873 */
874static struct sk_buff *
875ip_vs_prepare_tunneled_skb(struct sk_buff *skb, int skb_af,
876 unsigned int max_headroom, __u8 *next_protocol,
877 __u32 *payload_len, __u8 *dsfield, __u8 *ttl,
878 __be16 *df)
879{
880 struct sk_buff *new_skb = NULL;
881 struct iphdr *old_iph = NULL;
882#ifdef CONFIG_IP_VS_IPV6
883 struct ipv6hdr *old_ipv6h = NULL;
884#endif
885
Alex Gartrell71563f32015-07-05 14:28:26 -0700886 ip_vs_drop_early_demux_sk(skb);
887
Alex Gartrell8052ba22014-09-09 16:40:28 -0700888 if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
889 new_skb = skb_realloc_headroom(skb, max_headroom);
890 if (!new_skb)
891 goto error;
Calvin Owens50656d92014-11-04 16:37:40 -0800892 if (skb->sk)
893 skb_set_owner_w(new_skb, skb->sk);
Alex Gartrell8052ba22014-09-09 16:40:28 -0700894 consume_skb(skb);
895 skb = new_skb;
896 }
897
898#ifdef CONFIG_IP_VS_IPV6
899 if (skb_af == AF_INET6) {
900 old_ipv6h = ipv6_hdr(skb);
901 *next_protocol = IPPROTO_IPV6;
902 if (payload_len)
903 *payload_len =
904 ntohs(old_ipv6h->payload_len) +
905 sizeof(*old_ipv6h);
906 *dsfield = ipv6_get_dsfield(old_ipv6h);
907 *ttl = old_ipv6h->hop_limit;
908 if (df)
909 *df = 0;
910 } else
911#endif
912 {
913 old_iph = ip_hdr(skb);
914 /* Copy DF, reset fragment offset and MF */
915 if (df)
916 *df = (old_iph->frag_off & htons(IP_DF));
917 *next_protocol = IPPROTO_IPIP;
918
919 /* fix old IP header checksum */
920 ip_send_check(old_iph);
921 *dsfield = ipv4_get_dsfield(old_iph);
922 *ttl = old_iph->ttl;
923 if (payload_len)
924 *payload_len = ntohs(old_iph->tot_len);
925 }
926
927 return skb;
928error:
929 kfree_skb(skb);
930 return ERR_PTR(-ENOMEM);
931}
932
933static inline int __tun_gso_type_mask(int encaps_af, int orig_af)
934{
Tom Herbert7e133182016-05-18 09:06:10 -0700935 switch (encaps_af) {
936 case AF_INET:
937 return SKB_GSO_IPXIP4;
938 case AF_INET6:
939 return SKB_GSO_IPXIP6;
940 default:
941 return 0;
Alex Gartrell8052ba22014-09-09 16:40:28 -0700942 }
Alex Gartrell8052ba22014-09-09 16:40:28 -0700943}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945/*
946 * IP Tunneling transmitter
947 *
948 * This function encapsulates the packet in a new IP packet, its
949 * destination will be set to cp->daddr. Most code of this function
950 * is taken from ipip.c.
951 *
952 * It is used in VS/TUN cluster. The load balancer selects a real
953 * server from a cluster based on a scheduling algorithm,
954 * encapsulates the request packet and forwards it to the selected
955 * server. For example, all real servers are configured with
956 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
957 * the encapsulated packet, it will decapsulate the packet, processe
958 * the request and return the response packets directly to the client
959 * without passing the load balancer. This can greatly increase the
960 * scalability of virtual server.
961 *
962 * Used for ANY protocol
963 */
964int
965ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200966 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Eric W. Biederman361c3f52015-09-21 13:02:44 -0500968 struct netns_ipvs *ipvs = cp->ipvs;
969 struct net *net = ipvs->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 struct rtable *rt; /* Route to the other host */
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000971 __be32 saddr; /* Source for tunnel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 struct net_device *tdev; /* Device to other host */
Alex Gartrell8052ba22014-09-09 16:40:28 -0700973 __u8 next_protocol = 0;
974 __u8 dsfield = 0;
975 __u8 ttl = 0;
976 __be16 df = 0;
977 __be16 *dfp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 struct iphdr *iph; /* Our new IP header */
Chuck Leverc2636b42007-10-23 21:07:32 -0700979 unsigned int max_headroom; /* The extra header space needed */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200980 int ret, local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 EnterFunction(10);
983
Julian Anastasov026ace02013-03-21 11:58:06 +0200984 rcu_read_lock();
Eric W. Biedermanecfe87b2015-09-21 13:02:45 -0500985 local = __ip_vs_get_out_rt(ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200986 IP_VS_RT_MODE_LOCAL |
987 IP_VS_RT_MODE_NON_LOCAL |
988 IP_VS_RT_MODE_CONNECT |
Alex Gartrellc63e4de2014-09-09 16:40:27 -0700989 IP_VS_RT_MODE_TUNNEL, &saddr, ipvsh);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200990 if (local < 0)
991 goto tx_error;
Julian Anastasov026ace02013-03-21 11:58:06 +0200992 if (local) {
993 rcu_read_unlock();
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200994 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
Julian Anastasov026ace02013-03-21 11:58:06 +0200995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Julian Anastasov4115ded2013-03-21 11:58:05 +0200997 rt = skb_rtable(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700998 tdev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /*
1001 * Okay, now see if we can stuff it in the buffer as-is.
1002 */
1003 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
1004
Alex Gartrell8052ba22014-09-09 16:40:28 -07001005 /* We only care about the df field if sysctl_pmtu_disc(ipvs) is set */
1006 dfp = sysctl_pmtu_disc(ipvs) ? &df : NULL;
1007 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1008 &next_protocol, NULL, &dsfield,
1009 &ttl, dfp);
1010 if (IS_ERR(skb))
1011 goto tx_error;
Julian Anastasov4115ded2013-03-21 11:58:05 +02001012
Alexander Duyckaed069d2016-04-14 15:33:37 -04001013 if (iptunnel_handle_offloads(skb, __tun_gso_type_mask(AF_INET, cp->af)))
Julian Anastasovea1d5d72014-08-01 10:36:17 +03001014 goto tx_error;
1015
1016 skb->transport_header = skb->network_header;
1017
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001018 skb_push(skb, sizeof(struct iphdr));
1019 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 /*
1023 * Push down and install the IPIP header.
1024 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001025 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 iph->version = 4;
1027 iph->ihl = sizeof(struct iphdr)>>2;
1028 iph->frag_off = df;
Alex Gartrell8052ba22014-09-09 16:40:28 -07001029 iph->protocol = next_protocol;
1030 iph->tos = dsfield;
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001031 iph->daddr = cp->daddr.ip;
1032 iph->saddr = saddr;
Alex Gartrell8052ba22014-09-09 16:40:28 -07001033 iph->ttl = ttl;
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001034 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -07001037 skb->ignore_df = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001039 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001040 if (ret == NF_ACCEPT)
Eric W. Biederman33224b12015-10-07 16:48:46 -05001041 ip_local_out(net, skb->sk, skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001042 else if (ret == NF_DROP)
1043 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +02001044 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 LeaveFunction(10);
1047
1048 return NF_STOLEN;
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 tx_error:
Julian Anastasovea1d5d72014-08-01 10:36:17 +03001051 if (!IS_ERR(skb))
1052 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +02001053 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 LeaveFunction(10);
1055 return NF_STOLEN;
1056}
1057
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001058#ifdef CONFIG_IP_VS_IPV6
1059int
1060ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001061 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001062{
1063 struct rt6_info *rt; /* Route to the other host */
Hans Schillstrom714f0952010-10-19 10:38:48 +02001064 struct in6_addr saddr; /* Source for tunnel */
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001065 struct net_device *tdev; /* Device to other host */
Alex Gartrell8052ba22014-09-09 16:40:28 -07001066 __u8 next_protocol = 0;
1067 __u32 payload_len = 0;
1068 __u8 dsfield = 0;
1069 __u8 ttl = 0;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001070 struct ipv6hdr *iph; /* Our new IP header */
1071 unsigned int max_headroom; /* The extra header space needed */
Julian Anastasov4115ded2013-03-21 11:58:05 +02001072 int ret, local;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001073
1074 EnterFunction(10);
1075
Julian Anastasov026ace02013-03-21 11:58:06 +02001076 rcu_read_lock();
Eric W. Biedermanf5745f82015-09-21 13:02:46 -05001077 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1078 &cp->daddr.in6,
Julian Anastasov4115ded2013-03-21 11:58:05 +02001079 &saddr, ipvsh, 1,
1080 IP_VS_RT_MODE_LOCAL |
1081 IP_VS_RT_MODE_NON_LOCAL |
1082 IP_VS_RT_MODE_TUNNEL);
1083 if (local < 0)
1084 goto tx_error;
Julian Anastasov026ace02013-03-21 11:58:06 +02001085 if (local) {
1086 rcu_read_unlock();
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001087 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
Julian Anastasov026ace02013-03-21 11:58:06 +02001088 }
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001089
Julian Anastasov4115ded2013-03-21 11:58:05 +02001090 rt = (struct rt6_info *) skb_dst(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001091 tdev = rt->dst.dev;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001092
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001093 /*
1094 * Okay, now see if we can stuff it in the buffer as-is.
1095 */
1096 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
1097
Alex Gartrell8052ba22014-09-09 16:40:28 -07001098 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1099 &next_protocol, &payload_len,
1100 &dsfield, &ttl, NULL);
1101 if (IS_ERR(skb))
1102 goto tx_error;
Julian Anastasov4115ded2013-03-21 11:58:05 +02001103
Alexander Duyckaed069d2016-04-14 15:33:37 -04001104 if (iptunnel_handle_offloads(skb, __tun_gso_type_mask(AF_INET6, cp->af)))
Julian Anastasovea1d5d72014-08-01 10:36:17 +03001105 goto tx_error;
1106
Hans Schillstrom714f0952010-10-19 10:38:48 +02001107 skb->transport_header = skb->network_header;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001108
1109 skb_push(skb, sizeof(struct ipv6hdr));
1110 skb_reset_network_header(skb);
1111 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1112
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001113 /*
1114 * Push down and install the IPIP header.
1115 */
1116 iph = ipv6_hdr(skb);
1117 iph->version = 6;
Alex Gartrell8052ba22014-09-09 16:40:28 -07001118 iph->nexthdr = next_protocol;
1119 iph->payload_len = htons(payload_len);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001120 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
Alex Gartrell8052ba22014-09-09 16:40:28 -07001121 ipv6_change_dsfield(iph, 0, dsfield);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001122 iph->daddr = cp->daddr.in6;
1123 iph->saddr = saddr;
Alex Gartrell8052ba22014-09-09 16:40:28 -07001124 iph->hop_limit = ttl;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001125
1126 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -07001127 skb->ignore_df = 1;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001128
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001129 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001130 if (ret == NF_ACCEPT)
Eric W. Biederman33224b12015-10-07 16:48:46 -05001131 ip6_local_out(cp->ipvs->net, skb->sk, skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001132 else if (ret == NF_DROP)
1133 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +02001134 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001135
1136 LeaveFunction(10);
1137
1138 return NF_STOLEN;
1139
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001140tx_error:
Julian Anastasovea1d5d72014-08-01 10:36:17 +03001141 if (!IS_ERR(skb))
1142 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +02001143 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001144 LeaveFunction(10);
1145 return NF_STOLEN;
1146}
1147#endif
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150/*
1151 * Direct Routing transmitter
1152 * Used for ANY protocol
1153 */
1154int
1155ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001156 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Julian Anastasov4115ded2013-03-21 11:58:05 +02001158 int local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 EnterFunction(10);
1161
Julian Anastasov026ace02013-03-21 11:58:06 +02001162 rcu_read_lock();
Eric W. Biedermanecfe87b2015-09-21 13:02:45 -05001163 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
Julian Anastasov4115ded2013-03-21 11:58:05 +02001164 IP_VS_RT_MODE_LOCAL |
1165 IP_VS_RT_MODE_NON_LOCAL |
Alex Gartrellc63e4de2014-09-09 16:40:27 -07001166 IP_VS_RT_MODE_KNOWN_NH, NULL, ipvsh);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001167 if (local < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 goto tx_error;
Julian Anastasov026ace02013-03-21 11:58:06 +02001169 if (local) {
1170 rcu_read_unlock();
Julian Anastasov4115ded2013-03-21 11:58:05 +02001171 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
Julian Anastasov026ace02013-03-21 11:58:06 +02001172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001174 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -07001177 skb->ignore_df = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001179 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
Julian Anastasov026ace02013-03-21 11:58:06 +02001180 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 LeaveFunction(10);
1183 return NF_STOLEN;
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 tx_error:
1186 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +02001187 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 LeaveFunction(10);
1189 return NF_STOLEN;
1190}
1191
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001192#ifdef CONFIG_IP_VS_IPV6
1193int
1194ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
Julian Anastasov4115ded2013-03-21 11:58:05 +02001195 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001196{
Julian Anastasov4115ded2013-03-21 11:58:05 +02001197 int local;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001198
1199 EnterFunction(10);
1200
Julian Anastasov026ace02013-03-21 11:58:06 +02001201 rcu_read_lock();
Eric W. Biedermanf5745f82015-09-21 13:02:46 -05001202 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1203 &cp->daddr.in6,
Alex Gartrell4a4739d52014-09-09 16:40:25 -07001204 NULL, ipvsh, 0,
Julian Anastasov4115ded2013-03-21 11:58:05 +02001205 IP_VS_RT_MODE_LOCAL |
Martin KaFai Lau48e8aa62015-05-22 20:56:02 -07001206 IP_VS_RT_MODE_NON_LOCAL |
1207 IP_VS_RT_MODE_KNOWN_NH);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001208 if (local < 0)
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001209 goto tx_error;
Julian Anastasov026ace02013-03-21 11:58:06 +02001210 if (local) {
1211 rcu_read_unlock();
Julian Anastasov4115ded2013-03-21 11:58:05 +02001212 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
Julian Anastasov026ace02013-03-21 11:58:06 +02001213 }
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001214
1215 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -07001216 skb->ignore_df = 1;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001217
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001218 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
Julian Anastasov026ace02013-03-21 11:58:06 +02001219 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001220
1221 LeaveFunction(10);
1222 return NF_STOLEN;
1223
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001224tx_error:
1225 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +02001226 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001227 LeaveFunction(10);
1228 return NF_STOLEN;
1229}
1230#endif
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233/*
1234 * ICMP packet transmitter
1235 * called by the ip_vs_in_icmp
1236 */
1237int
1238ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001239 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1240 struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 struct rtable *rt; /* Route to the other host */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int rc;
Julian Anastasovfc604762010-10-17 16:38:15 +03001244 int local;
Julian Anastasov4115ded2013-03-21 11:58:05 +02001245 int rt_mode, was_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 EnterFunction(10);
1248
1249 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1250 forwarded directly here, because there is no need to
1251 translate address/port back */
1252 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1253 if (cp->packet_xmit)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001254 rc = cp->packet_xmit(skb, cp, pp, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 else
1256 rc = NF_ACCEPT;
1257 /* do not touch skb anymore */
1258 atomic_inc(&cp->in_pkts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 goto out;
1260 }
1261
1262 /*
1263 * mangle and send the packet here (only for VS/NAT)
1264 */
Julian Anastasov4115ded2013-03-21 11:58:05 +02001265 was_input = rt_is_input_route(skb_rtable(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001267 /* LOCALNODE from FORWARD hook is not supported */
1268 rt_mode = (hooknum != NF_INET_FORWARD) ?
1269 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1270 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
Julian Anastasov026ace02013-03-21 11:58:06 +02001271 rcu_read_lock();
Eric W. Biedermanecfe87b2015-09-21 13:02:45 -05001272 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip, rt_mode,
Alex Gartrellc63e4de2014-09-09 16:40:27 -07001273 NULL, iph);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001274 if (local < 0)
1275 goto tx_error;
1276 rt = skb_rtable(skb);
Julian Anastasovfc604762010-10-17 16:38:15 +03001277
1278 /*
1279 * Avoid duplicate tuple in reply direction for NAT traffic
1280 * to local address when connection is sync-ed
1281 */
Igor Maravićc0cd1152011-12-12 02:58:24 +00001282#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Julian Anastasovfc604762010-10-17 16:38:15 +03001283 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1284 enum ip_conntrack_info ctinfo;
Alan Cox05b4b062012-10-26 00:13:27 +00001285 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Julian Anastasovfc604762010-10-17 16:38:15 +03001286
1287 if (ct && !nf_ct_is_untracked(ct)) {
1288 IP_VS_DBG(10, "%s(): "
1289 "stopping DNAT to local address %pI4\n",
1290 __func__, &cp->daddr.ip);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001291 goto tx_error;
Julian Anastasovfc604762010-10-17 16:38:15 +03001292 }
1293 }
1294#endif
1295
1296 /* From world but DNAT to loopback address? */
Julian Anastasov4115ded2013-03-21 11:58:05 +02001297 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
Julian Anastasovfc604762010-10-17 16:38:15 +03001298 IP_VS_DBG(1, "%s(): "
1299 "stopping DNAT to loopback %pI4\n",
1300 __func__, &cp->daddr.ip);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001301 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303
1304 /* copy-on-write the packet before mangling it */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -07001305 if (!skb_make_writable(skb, offset))
Julian Anastasov4115ded2013-03-21 11:58:05 +02001306 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Changli Gaod8d1f302010-06-10 23:31:35 -07001308 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julian Anastasov4115ded2013-03-21 11:58:05 +02001309 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 ip_vs_nat_icmp(skb, pp, cp, 0);
1312
1313 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -07001314 skb->ignore_df = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001316 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
Julian Anastasov026ace02013-03-21 11:58:06 +02001317 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 goto out;
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 tx_error:
Julian Anastasov026ace02013-03-21 11:58:06 +02001321 kfree_skb(skb);
1322 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 rc = NF_STOLEN;
1324 out:
1325 LeaveFunction(10);
1326 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001328
1329#ifdef CONFIG_IP_VS_IPV6
1330int
1331ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001332 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
Julian Anastasov4115ded2013-03-21 11:58:05 +02001333 struct ip_vs_iphdr *ipvsh)
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001334{
1335 struct rt6_info *rt; /* Route to the other host */
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001336 int rc;
Julian Anastasovfc604762010-10-17 16:38:15 +03001337 int local;
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001338 int rt_mode;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001339
1340 EnterFunction(10);
1341
1342 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1343 forwarded directly here, because there is no need to
1344 translate address/port back */
1345 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1346 if (cp->packet_xmit)
Julian Anastasov4115ded2013-03-21 11:58:05 +02001347 rc = cp->packet_xmit(skb, cp, pp, ipvsh);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001348 else
1349 rc = NF_ACCEPT;
1350 /* do not touch skb anymore */
1351 atomic_inc(&cp->in_pkts);
1352 goto out;
1353 }
1354
1355 /*
1356 * mangle and send the packet here (only for VS/NAT)
1357 */
1358
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001359 /* LOCALNODE from FORWARD hook is not supported */
1360 rt_mode = (hooknum != NF_INET_FORWARD) ?
1361 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1362 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
Julian Anastasov026ace02013-03-21 11:58:06 +02001363 rcu_read_lock();
Eric W. Biedermanf5745f82015-09-21 13:02:46 -05001364 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1365 &cp->daddr.in6, NULL, ipvsh, 0, rt_mode);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001366 if (local < 0)
1367 goto tx_error;
1368 rt = (struct rt6_info *) skb_dst(skb);
Julian Anastasovfc604762010-10-17 16:38:15 +03001369 /*
1370 * Avoid duplicate tuple in reply direction for NAT traffic
1371 * to local address when connection is sync-ed
1372 */
Igor Maravićc0cd1152011-12-12 02:58:24 +00001373#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Julian Anastasovfc604762010-10-17 16:38:15 +03001374 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1375 enum ip_conntrack_info ctinfo;
Alan Cox05b4b062012-10-26 00:13:27 +00001376 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Julian Anastasovfc604762010-10-17 16:38:15 +03001377
1378 if (ct && !nf_ct_is_untracked(ct)) {
1379 IP_VS_DBG(10, "%s(): "
1380 "stopping DNAT to local address %pI6\n",
1381 __func__, &cp->daddr.in6);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001382 goto tx_error;
Julian Anastasovfc604762010-10-17 16:38:15 +03001383 }
1384 }
1385#endif
1386
1387 /* From world but DNAT to loopback address? */
1388 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
Martin KaFai Laufd0273d2015-05-22 20:55:57 -07001389 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
Julian Anastasovfc604762010-10-17 16:38:15 +03001390 IP_VS_DBG(1, "%s(): "
1391 "stopping DNAT to loopback %pI6\n",
1392 __func__, &cp->daddr.in6);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001393 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001394 }
1395
1396 /* copy-on-write the packet before mangling it */
1397 if (!skb_make_writable(skb, offset))
Julian Anastasov4115ded2013-03-21 11:58:05 +02001398 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001399
Changli Gaod8d1f302010-06-10 23:31:35 -07001400 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julian Anastasov4115ded2013-03-21 11:58:05 +02001401 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001402
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001403 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1404
1405 /* Another hack: avoid icmp_send in ip_fragment */
WANG Cong60ff7462014-05-04 16:39:18 -07001406 skb->ignore_df = 1;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001407
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001408 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
Julian Anastasov026ace02013-03-21 11:58:06 +02001409 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001410 goto out;
1411
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001412tx_error:
Julian Anastasov026ace02013-03-21 11:58:06 +02001413 kfree_skb(skb);
1414 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001415 rc = NF_STOLEN;
1416out:
1417 LeaveFunction(10);
1418 return rc;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001419}
1420#endif