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