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 |
| 20 | * LOCAL_OUT rules: |
| 21 | * - skb->dev is NULL, skb->protocol is not set (both are set in POST_ROUTING) |
| 22 | * - skb->pkt_type is not set yet |
| 23 | * - the only place where we can see skb->sk != NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
Hannes Eder | 9aada7a | 2009-07-30 14:29:44 -0700 | [diff] [blame] | 26 | #define KMSG_COMPONENT "IPVS" |
| 27 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/tcp.h> /* for tcphdr */ |
Herbert Xu | c439cb2 | 2008-01-11 19:14:00 -0800 | [diff] [blame] | 32 | #include <net/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <net/tcp.h> /* for csum_tcpudp_magic */ |
| 34 | #include <net/udp.h> |
| 35 | #include <net/icmp.h> /* for icmp_send */ |
| 36 | #include <net/route.h> /* for ip_route_output */ |
Julius Volz | 38cdcc9 | 2008-09-02 15:55:44 +0200 | [diff] [blame] | 37 | #include <net/ipv6.h> |
| 38 | #include <net/ip6_route.h> |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 39 | #include <net/addrconf.h> |
Julius Volz | 38cdcc9 | 2008-09-02 15:55:44 +0200 | [diff] [blame] | 40 | #include <linux/icmpv6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/netfilter.h> |
| 42 | #include <linux/netfilter_ipv4.h> |
| 43 | |
| 44 | #include <net/ip_vs.h> |
| 45 | |
Changli Gao | 17a8f8e | 2011-02-24 08:19:57 +0800 | [diff] [blame] | 46 | enum { |
| 47 | IP_VS_RT_MODE_LOCAL = 1, /* Allow local dest */ |
| 48 | IP_VS_RT_MODE_NON_LOCAL = 2, /* Allow non-local dest */ |
| 49 | IP_VS_RT_MODE_RDR = 4, /* Allow redirect from remote daddr to |
| 50 | * local |
| 51 | */ |
| 52 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * Destination cache to speed up outgoing route lookup |
| 56 | */ |
| 57 | static inline void |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 58 | __ip_vs_dst_set(struct ip_vs_dest *dest, u32 rtos, struct dst_entry *dst, |
| 59 | u32 dst_cookie) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
| 61 | struct dst_entry *old_dst; |
| 62 | |
| 63 | old_dst = dest->dst_cache; |
| 64 | dest->dst_cache = dst; |
| 65 | dest->dst_rtos = rtos; |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 66 | dest->dst_cookie = dst_cookie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | dst_release(old_dst); |
| 68 | } |
| 69 | |
| 70 | static inline struct dst_entry * |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 71 | __ip_vs_dst_check(struct ip_vs_dest *dest, u32 rtos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
| 73 | struct dst_entry *dst = dest->dst_cache; |
| 74 | |
| 75 | if (!dst) |
| 76 | return NULL; |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 77 | if ((dst->obsolete || rtos != dest->dst_rtos) && |
| 78 | dst->ops->check(dst, dest->dst_cookie) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | dest->dst_cache = NULL; |
| 80 | dst_release(dst); |
| 81 | return NULL; |
| 82 | } |
| 83 | dst_hold(dst); |
| 84 | return dst; |
| 85 | } |
| 86 | |
Changli Gao | 17a8f8e | 2011-02-24 08:19:57 +0800 | [diff] [blame] | 87 | /* Get route to destination or remote server */ |
Ilpo Järvinen | ad1b30b | 2008-01-05 23:12:40 -0800 | [diff] [blame] | 88 | static struct rtable * |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 89 | __ip_vs_get_out_rt(struct sk_buff *skb, struct ip_vs_dest *dest, |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 90 | __be32 daddr, u32 rtos, int rt_mode, __be32 *ret_saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 92 | struct net *net = dev_net(skb_dst(skb)->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | struct rtable *rt; /* Route to the other host */ |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 94 | struct rtable *ort; /* Original route */ |
| 95 | int local; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | if (dest) { |
| 98 | spin_lock(&dest->dst_lock); |
| 99 | if (!(rt = (struct rtable *) |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 100 | __ip_vs_dst_check(dest, rtos))) { |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 101 | struct flowi4 fl4; |
| 102 | |
| 103 | memset(&fl4, 0, sizeof(fl4)); |
| 104 | fl4.daddr = dest->addr.ip; |
| 105 | fl4.flowi4_tos = rtos; |
| 106 | rt = ip_route_output_key(net, &fl4); |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 107 | if (IS_ERR(rt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | spin_unlock(&dest->dst_lock); |
Harvey Harrison | 14d5e83 | 2008-10-31 00:54:29 -0700 | [diff] [blame] | 109 | IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n", |
| 110 | &dest->addr.ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | return NULL; |
| 112 | } |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 113 | __ip_vs_dst_set(dest, rtos, dst_clone(&rt->dst), 0); |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 114 | dest->dst_saddr.ip = fl4.saddr; |
| 115 | IP_VS_DBG(10, "new dst %pI4, src %pI4, refcnt=%d, " |
| 116 | "rtos=%X\n", |
| 117 | &dest->addr.ip, &dest->dst_saddr.ip, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 118 | atomic_read(&rt->dst.__refcnt), rtos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
David S. Miller | 44e3125 | 2011-05-09 14:38:06 -0700 | [diff] [blame] | 120 | daddr = dest->addr.ip; |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 121 | if (ret_saddr) |
| 122 | *ret_saddr = dest->dst_saddr.ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | spin_unlock(&dest->dst_lock); |
| 124 | } else { |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 125 | struct flowi4 fl4; |
| 126 | |
| 127 | memset(&fl4, 0, sizeof(fl4)); |
| 128 | fl4.daddr = daddr; |
| 129 | fl4.flowi4_tos = rtos; |
| 130 | rt = ip_route_output_key(net, &fl4); |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 131 | if (IS_ERR(rt)) { |
Harvey Harrison | 14d5e83 | 2008-10-31 00:54:29 -0700 | [diff] [blame] | 132 | IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n", |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 133 | &daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | return NULL; |
| 135 | } |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 136 | if (ret_saddr) |
| 137 | *ret_saddr = fl4.saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 140 | local = rt->rt_flags & RTCF_LOCAL; |
Changli Gao | 17a8f8e | 2011-02-24 08:19:57 +0800 | [diff] [blame] | 141 | if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) & |
| 142 | rt_mode)) { |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 143 | IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI4\n", |
| 144 | (rt->rt_flags & RTCF_LOCAL) ? |
David S. Miller | 44e3125 | 2011-05-09 14:38:06 -0700 | [diff] [blame] | 145 | "local":"non-local", &daddr); |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 146 | ip_rt_put(rt); |
| 147 | return NULL; |
| 148 | } |
Changli Gao | 17a8f8e | 2011-02-24 08:19:57 +0800 | [diff] [blame] | 149 | if (local && !(rt_mode & IP_VS_RT_MODE_RDR) && |
| 150 | !((ort = skb_rtable(skb)) && ort->rt_flags & RTCF_LOCAL)) { |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 151 | IP_VS_DBG_RL("Redirect from non-local address %pI4 to local " |
| 152 | "requires NAT method, dest: %pI4\n", |
David S. Miller | 44e3125 | 2011-05-09 14:38:06 -0700 | [diff] [blame] | 153 | &ip_hdr(skb)->daddr, &daddr); |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 154 | ip_rt_put(rt); |
| 155 | return NULL; |
| 156 | } |
| 157 | if (unlikely(!local && ipv4_is_loopback(ip_hdr(skb)->saddr))) { |
| 158 | IP_VS_DBG_RL("Stopping traffic from loopback address %pI4 " |
| 159 | "to non-local address, dest: %pI4\n", |
David S. Miller | 44e3125 | 2011-05-09 14:38:06 -0700 | [diff] [blame] | 160 | &ip_hdr(skb)->saddr, &daddr); |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 161 | ip_rt_put(rt); |
| 162 | return NULL; |
| 163 | } |
| 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | return rt; |
| 166 | } |
| 167 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 168 | /* Reroute packet to local IPv4 stack after DNAT */ |
| 169 | static int |
| 170 | __ip_vs_reroute_locally(struct sk_buff *skb) |
| 171 | { |
| 172 | struct rtable *rt = skb_rtable(skb); |
| 173 | struct net_device *dev = rt->dst.dev; |
| 174 | struct net *net = dev_net(dev); |
| 175 | struct iphdr *iph = ip_hdr(skb); |
| 176 | |
David S. Miller | c753796 | 2010-11-11 17:07:48 -0800 | [diff] [blame] | 177 | if (rt_is_input_route(rt)) { |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 178 | unsigned long orefdst = skb->_skb_refdst; |
| 179 | |
| 180 | if (ip_route_input(skb, iph->daddr, iph->saddr, |
| 181 | iph->tos, skb->dev)) |
| 182 | return 0; |
| 183 | refdst_drop(orefdst); |
| 184 | } else { |
David S. Miller | 9d6ec93 | 2011-03-12 01:12:47 -0500 | [diff] [blame] | 185 | struct flowi4 fl4 = { |
| 186 | .daddr = iph->daddr, |
| 187 | .saddr = iph->saddr, |
| 188 | .flowi4_tos = RT_TOS(iph->tos), |
| 189 | .flowi4_mark = skb->mark, |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 190 | }; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 191 | |
David S. Miller | 9d6ec93 | 2011-03-12 01:12:47 -0500 | [diff] [blame] | 192 | rt = ip_route_output_key(net, &fl4); |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 193 | if (IS_ERR(rt)) |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 194 | return 0; |
| 195 | if (!(rt->rt_flags & RTCF_LOCAL)) { |
| 196 | ip_rt_put(rt); |
| 197 | return 0; |
| 198 | } |
| 199 | /* Drop old route. */ |
| 200 | skb_dst_drop(skb); |
| 201 | skb_dst_set(skb, &rt->dst); |
| 202 | } |
| 203 | return 1; |
| 204 | } |
| 205 | |
Julius Volz | 38cdcc9 | 2008-09-02 15:55:44 +0200 | [diff] [blame] | 206 | #ifdef CONFIG_IP_VS_IPV6 |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 207 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 208 | static inline int __ip_vs_is_local_route6(struct rt6_info *rt) |
| 209 | { |
David S. Miller | d191854 | 2011-12-28 20:19:20 -0500 | [diff] [blame] | 210 | return rt->dst.dev && rt->dst.dev->flags & IFF_LOOPBACK; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 211 | } |
| 212 | |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 213 | static struct dst_entry * |
| 214 | __ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr, |
| 215 | struct in6_addr *ret_saddr, int do_xfrm) |
Julius Volz | 38cdcc9 | 2008-09-02 15:55:44 +0200 | [diff] [blame] | 216 | { |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 217 | struct dst_entry *dst; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 218 | struct flowi6 fl6 = { |
| 219 | .daddr = *daddr, |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 220 | }; |
| 221 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 222 | dst = ip6_route_output(net, NULL, &fl6); |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 223 | if (dst->error) |
| 224 | goto out_err; |
| 225 | if (!ret_saddr) |
| 226 | return dst; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 227 | if (ipv6_addr_any(&fl6.saddr) && |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 228 | ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev, |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 229 | &fl6.daddr, 0, &fl6.saddr) < 0) |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 230 | goto out_err; |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 231 | if (do_xfrm) { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 232 | dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0); |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 233 | if (IS_ERR(dst)) { |
| 234 | dst = NULL; |
| 235 | goto out_err; |
| 236 | } |
| 237 | } |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 238 | *ret_saddr = fl6.saddr; |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 239 | return dst; |
| 240 | |
| 241 | out_err: |
| 242 | dst_release(dst); |
| 243 | IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr); |
| 244 | return NULL; |
| 245 | } |
| 246 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 247 | /* |
| 248 | * Get route to destination or remote server |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 249 | */ |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 250 | static struct rt6_info * |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 251 | __ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest, |
| 252 | struct in6_addr *daddr, struct in6_addr *ret_saddr, |
| 253 | int do_xfrm, int rt_mode) |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 254 | { |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 255 | struct net *net = dev_net(skb_dst(skb)->dev); |
Julius Volz | 38cdcc9 | 2008-09-02 15:55:44 +0200 | [diff] [blame] | 256 | struct rt6_info *rt; /* Route to the other host */ |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 257 | struct rt6_info *ort; /* Original route */ |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 258 | struct dst_entry *dst; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 259 | int local; |
Julius Volz | 38cdcc9 | 2008-09-02 15:55:44 +0200 | [diff] [blame] | 260 | |
| 261 | if (dest) { |
| 262 | spin_lock(&dest->dst_lock); |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 263 | rt = (struct rt6_info *)__ip_vs_dst_check(dest, 0); |
Julius Volz | 38cdcc9 | 2008-09-02 15:55:44 +0200 | [diff] [blame] | 264 | if (!rt) { |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 265 | u32 cookie; |
Julius Volz | 38cdcc9 | 2008-09-02 15:55:44 +0200 | [diff] [blame] | 266 | |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 267 | dst = __ip_vs_route_output_v6(net, &dest->addr.in6, |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 268 | &dest->dst_saddr.in6, |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 269 | do_xfrm); |
| 270 | if (!dst) { |
Julius Volz | 38cdcc9 | 2008-09-02 15:55:44 +0200 | [diff] [blame] | 271 | spin_unlock(&dest->dst_lock); |
Julius Volz | 38cdcc9 | 2008-09-02 15:55:44 +0200 | [diff] [blame] | 272 | return NULL; |
| 273 | } |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 274 | rt = (struct rt6_info *) dst; |
| 275 | cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0; |
| 276 | __ip_vs_dst_set(dest, 0, dst_clone(&rt->dst), cookie); |
| 277 | IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n", |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 278 | &dest->addr.in6, &dest->dst_saddr.in6, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 279 | atomic_read(&rt->dst.__refcnt)); |
Julius Volz | 38cdcc9 | 2008-09-02 15:55:44 +0200 | [diff] [blame] | 280 | } |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 281 | if (ret_saddr) |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 282 | *ret_saddr = dest->dst_saddr.in6; |
Julius Volz | 38cdcc9 | 2008-09-02 15:55:44 +0200 | [diff] [blame] | 283 | spin_unlock(&dest->dst_lock); |
| 284 | } else { |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 285 | dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm); |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 286 | if (!dst) |
Julius Volz | 38cdcc9 | 2008-09-02 15:55:44 +0200 | [diff] [blame] | 287 | return NULL; |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 288 | rt = (struct rt6_info *) dst; |
Julius Volz | 38cdcc9 | 2008-09-02 15:55:44 +0200 | [diff] [blame] | 289 | } |
| 290 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 291 | local = __ip_vs_is_local_route6(rt); |
David S. Miller | e58b344 | 2011-05-12 18:22:34 -0400 | [diff] [blame] | 292 | if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) & |
| 293 | rt_mode)) { |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 294 | IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI6\n", |
| 295 | local ? "local":"non-local", daddr); |
| 296 | dst_release(&rt->dst); |
| 297 | return NULL; |
| 298 | } |
David S. Miller | e58b344 | 2011-05-12 18:22:34 -0400 | [diff] [blame] | 299 | if (local && !(rt_mode & IP_VS_RT_MODE_RDR) && |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 300 | !((ort = (struct rt6_info *) skb_dst(skb)) && |
| 301 | __ip_vs_is_local_route6(ort))) { |
| 302 | IP_VS_DBG_RL("Redirect from non-local address %pI6 to local " |
| 303 | "requires NAT method, dest: %pI6\n", |
| 304 | &ipv6_hdr(skb)->daddr, daddr); |
| 305 | dst_release(&rt->dst); |
| 306 | return NULL; |
| 307 | } |
| 308 | if (unlikely(!local && (!skb->dev || skb->dev->flags & IFF_LOOPBACK) && |
| 309 | ipv6_addr_type(&ipv6_hdr(skb)->saddr) & |
| 310 | IPV6_ADDR_LOOPBACK)) { |
| 311 | IP_VS_DBG_RL("Stopping traffic from loopback address %pI6 " |
| 312 | "to non-local address, dest: %pI6\n", |
| 313 | &ipv6_hdr(skb)->saddr, daddr); |
| 314 | dst_release(&rt->dst); |
| 315 | return NULL; |
| 316 | } |
| 317 | |
Julius Volz | 38cdcc9 | 2008-09-02 15:55:44 +0200 | [diff] [blame] | 318 | return rt; |
| 319 | } |
| 320 | #endif |
| 321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
| 323 | /* |
| 324 | * Release dest->dst_cache before a dest is removed |
| 325 | */ |
| 326 | void |
| 327 | ip_vs_dst_reset(struct ip_vs_dest *dest) |
| 328 | { |
| 329 | struct dst_entry *old_dst; |
| 330 | |
| 331 | old_dst = dest->dst_cache; |
| 332 | dest->dst_cache = NULL; |
| 333 | dst_release(old_dst); |
| 334 | } |
| 335 | |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 336 | #define IP_VS_XMIT_TUNNEL(skb, cp) \ |
| 337 | ({ \ |
| 338 | int __ret = NF_ACCEPT; \ |
| 339 | \ |
Julian Anastasov | cf356d6 | 2010-10-17 16:21:07 +0300 | [diff] [blame] | 340 | (skb)->ipvs_property = 1; \ |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 341 | if (unlikely((cp)->flags & IP_VS_CONN_F_NFCT)) \ |
Simon Horman | 3c2de2a | 2011-09-16 14:02:19 +0900 | [diff] [blame] | 342 | __ret = ip_vs_confirm_conntrack(skb); \ |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 343 | if (__ret == NF_ACCEPT) { \ |
| 344 | nf_reset(skb); \ |
Julian Anastasov | 4256f1a | 2010-10-17 16:29:40 +0300 | [diff] [blame] | 345 | skb_forward_csum(skb); \ |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 346 | } \ |
| 347 | __ret; \ |
| 348 | }) |
| 349 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 350 | #define IP_VS_XMIT_NAT(pf, skb, cp, local) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | do { \ |
Julian Anastasov | cf356d6 | 2010-10-17 16:21:07 +0300 | [diff] [blame] | 352 | (skb)->ipvs_property = 1; \ |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 353 | if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \ |
Julian Anastasov | cf356d6 | 2010-10-17 16:21:07 +0300 | [diff] [blame] | 354 | ip_vs_notrack(skb); \ |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 355 | else \ |
| 356 | ip_vs_update_conntrack(skb, cp, 1); \ |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 357 | if (local) \ |
| 358 | return NF_ACCEPT; \ |
Herbert Xu | ccc7911 | 2007-07-30 16:20:12 -0700 | [diff] [blame] | 359 | skb_forward_csum(skb); \ |
Julius Volz | 38cdcc9 | 2008-09-02 15:55:44 +0200 | [diff] [blame] | 360 | NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \ |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 361 | skb_dst(skb)->dev, dst_output); \ |
| 362 | } while (0) |
| 363 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 364 | #define IP_VS_XMIT(pf, skb, cp, local) \ |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 365 | do { \ |
Julian Anastasov | cf356d6 | 2010-10-17 16:21:07 +0300 | [diff] [blame] | 366 | (skb)->ipvs_property = 1; \ |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 367 | if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \ |
Julian Anastasov | cf356d6 | 2010-10-17 16:21:07 +0300 | [diff] [blame] | 368 | ip_vs_notrack(skb); \ |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 369 | if (local) \ |
| 370 | return NF_ACCEPT; \ |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 371 | skb_forward_csum(skb); \ |
| 372 | NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \ |
| 373 | skb_dst(skb)->dev, dst_output); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } while (0) |
| 375 | |
| 376 | |
| 377 | /* |
| 378 | * NULL transmitter (do nothing except return NF_ACCEPT) |
| 379 | */ |
| 380 | int |
| 381 | ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, |
| 382 | struct ip_vs_protocol *pp) |
| 383 | { |
| 384 | /* we do not touch skb and do not need pskb ptr */ |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 385 | IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | |
| 389 | /* |
| 390 | * Bypass transmitter |
| 391 | * Let packets bypass the destination when the destination is not |
| 392 | * available, it may be only used in transparent cache cluster. |
| 393 | */ |
| 394 | int |
| 395 | ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, |
| 396 | struct ip_vs_protocol *pp) |
| 397 | { |
| 398 | struct rtable *rt; /* Route to the other host */ |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 399 | struct iphdr *iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | int mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
| 402 | EnterFunction(10); |
| 403 | |
Changli Gao | 17a8f8e | 2011-02-24 08:19:57 +0800 | [diff] [blame] | 404 | if (!(rt = __ip_vs_get_out_rt(skb, NULL, iph->daddr, RT_TOS(iph->tos), |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 405 | IP_VS_RT_MODE_NON_LOCAL, NULL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | goto tx_error_icmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
| 408 | /* MTU checking */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 409 | mtu = dst_mtu(&rt->dst); |
Simon Horman | 8f1b03a | 2010-11-09 10:08:49 +0900 | [diff] [blame] | 410 | if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) && |
| 411 | !skb_is_gso(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | ip_rt_put(rt); |
| 413 | icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu)); |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 414 | IP_VS_DBG_RL("%s(): frag needed\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | goto tx_error; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Call ip_send_check because we are not sure it is called |
| 420 | * after ip_defrag. Is copy-on-write needed? |
| 421 | */ |
| 422 | if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) { |
| 423 | ip_rt_put(rt); |
| 424 | return NF_STOLEN; |
| 425 | } |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 426 | ip_send_check(ip_hdr(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
| 428 | /* drop old route */ |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 429 | skb_dst_drop(skb); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 430 | skb_dst_set(skb, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | /* Another hack: avoid icmp_send in ip_fragment */ |
| 433 | skb->local_df = 1; |
| 434 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 435 | IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
| 437 | LeaveFunction(10); |
| 438 | return NF_STOLEN; |
| 439 | |
| 440 | tx_error_icmp: |
| 441 | dst_link_failure(skb); |
| 442 | tx_error: |
| 443 | kfree_skb(skb); |
| 444 | LeaveFunction(10); |
| 445 | return NF_STOLEN; |
| 446 | } |
| 447 | |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 448 | #ifdef CONFIG_IP_VS_IPV6 |
| 449 | int |
| 450 | ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, |
| 451 | struct ip_vs_protocol *pp) |
| 452 | { |
| 453 | struct rt6_info *rt; /* Route to the other host */ |
| 454 | struct ipv6hdr *iph = ipv6_hdr(skb); |
| 455 | int mtu; |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 456 | |
| 457 | EnterFunction(10); |
| 458 | |
David S. Miller | e58b344 | 2011-05-12 18:22:34 -0400 | [diff] [blame] | 459 | if (!(rt = __ip_vs_get_out_rt_v6(skb, NULL, &iph->daddr, NULL, 0, |
| 460 | IP_VS_RT_MODE_NON_LOCAL))) |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 461 | goto tx_error_icmp; |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 462 | |
| 463 | /* MTU checking */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 464 | mtu = dst_mtu(&rt->dst); |
Simon Horman | 8f1b03a | 2010-11-09 10:08:49 +0900 | [diff] [blame] | 465 | if (skb->len > mtu && !skb_is_gso(skb)) { |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 466 | if (!skb->dev) { |
| 467 | struct net *net = dev_net(skb_dst(skb)->dev); |
| 468 | |
| 469 | skb->dev = net->loopback_dev; |
| 470 | } |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 471 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 472 | dst_release(&rt->dst); |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 473 | IP_VS_DBG_RL("%s(): frag needed\n", __func__); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 474 | goto tx_error; |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * Call ip_send_check because we are not sure it is called |
| 479 | * after ip_defrag. Is copy-on-write needed? |
| 480 | */ |
| 481 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 482 | if (unlikely(skb == NULL)) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 483 | dst_release(&rt->dst); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 484 | return NF_STOLEN; |
| 485 | } |
| 486 | |
| 487 | /* drop old route */ |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 488 | skb_dst_drop(skb); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 489 | skb_dst_set(skb, &rt->dst); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 490 | |
| 491 | /* Another hack: avoid icmp_send in ip_fragment */ |
| 492 | skb->local_df = 1; |
| 493 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 494 | IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 495 | |
| 496 | LeaveFunction(10); |
| 497 | return NF_STOLEN; |
| 498 | |
| 499 | tx_error_icmp: |
| 500 | dst_link_failure(skb); |
| 501 | tx_error: |
| 502 | kfree_skb(skb); |
| 503 | LeaveFunction(10); |
| 504 | return NF_STOLEN; |
| 505 | } |
| 506 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
| 508 | /* |
| 509 | * NAT transmitter (only for outside-to-inside nat forwarding) |
| 510 | * Not used for related ICMP |
| 511 | */ |
| 512 | int |
| 513 | ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, |
| 514 | struct ip_vs_protocol *pp) |
| 515 | { |
| 516 | struct rtable *rt; /* Route to the other host */ |
| 517 | int mtu; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 518 | struct iphdr *iph = ip_hdr(skb); |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 519 | int local; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
| 521 | EnterFunction(10); |
| 522 | |
| 523 | /* check if it is a connection of no-client-port */ |
| 524 | if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) { |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 525 | __be16 _pt, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | p = skb_header_pointer(skb, iph->ihl*4, sizeof(_pt), &_pt); |
| 527 | if (p == NULL) |
| 528 | goto tx_error; |
| 529 | ip_vs_conn_fill_cport(cp, *p); |
| 530 | IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p)); |
| 531 | } |
| 532 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 533 | if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip, |
Changli Gao | 17a8f8e | 2011-02-24 08:19:57 +0800 | [diff] [blame] | 534 | RT_TOS(iph->tos), |
| 535 | IP_VS_RT_MODE_LOCAL | |
| 536 | IP_VS_RT_MODE_NON_LOCAL | |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 537 | IP_VS_RT_MODE_RDR, NULL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | goto tx_error_icmp; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 539 | local = rt->rt_flags & RTCF_LOCAL; |
| 540 | /* |
| 541 | * Avoid duplicate tuple in reply direction for NAT traffic |
| 542 | * to local address when connection is sync-ed |
| 543 | */ |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 544 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 545 | if (cp->flags & IP_VS_CONN_F_SYNC && local) { |
| 546 | enum ip_conntrack_info ctinfo; |
| 547 | struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo); |
| 548 | |
| 549 | if (ct && !nf_ct_is_untracked(ct)) { |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 550 | IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0, |
| 551 | "ip_vs_nat_xmit(): " |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 552 | "stopping DNAT to local address"); |
| 553 | goto tx_error_put; |
| 554 | } |
| 555 | } |
| 556 | #endif |
| 557 | |
| 558 | /* From world but DNAT to loopback address? */ |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 559 | if (local && ipv4_is_loopback(cp->daddr.ip) && |
David S. Miller | c753796 | 2010-11-11 17:07:48 -0800 | [diff] [blame] | 560 | rt_is_input_route(skb_rtable(skb))) { |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 561 | IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): " |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 562 | "stopping DNAT to loopback address"); |
| 563 | goto tx_error_put; |
| 564 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
| 566 | /* MTU checking */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 567 | mtu = dst_mtu(&rt->dst); |
Simon Horman | 8f1b03a | 2010-11-09 10:08:49 +0900 | [diff] [blame] | 568 | if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) && |
| 569 | !skb_is_gso(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu)); |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 571 | IP_VS_DBG_RL_PKT(0, AF_INET, pp, skb, 0, |
| 572 | "ip_vs_nat_xmit(): frag needed for"); |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 573 | goto tx_error_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* copy-on-write the packet before mangling it */ |
Herbert Xu | af1e1cf | 2007-10-14 00:39:33 -0700 | [diff] [blame] | 577 | if (!skb_make_writable(skb, sizeof(struct iphdr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | goto tx_error_put; |
| 579 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 580 | if (skb_cow(skb, rt->dst.dev->hard_header_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | goto tx_error_put; |
| 582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | /* mangle the packet */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 584 | if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp)) |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 585 | goto tx_error_put; |
Julius Volz | e7ade46 | 2008-09-02 15:55:33 +0200 | [diff] [blame] | 586 | ip_hdr(skb)->daddr = cp->daddr.ip; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 587 | ip_send_check(ip_hdr(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 589 | if (!local) { |
| 590 | /* drop old route */ |
| 591 | skb_dst_drop(skb); |
| 592 | skb_dst_set(skb, &rt->dst); |
| 593 | } else { |
| 594 | ip_rt_put(rt); |
| 595 | /* |
| 596 | * Some IPv4 replies get local address from routes, |
| 597 | * not from iph, so while we DNAT after routing |
| 598 | * we need this second input/output route. |
| 599 | */ |
| 600 | if (!__ip_vs_reroute_locally(skb)) |
| 601 | goto tx_error; |
| 602 | } |
| 603 | |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 604 | IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
| 606 | /* FIXME: when application helper enlarges the packet and the length |
| 607 | is larger than the MTU of outgoing device, there will be still |
| 608 | MTU problem. */ |
| 609 | |
| 610 | /* Another hack: avoid icmp_send in ip_fragment */ |
| 611 | skb->local_df = 1; |
| 612 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 613 | IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
| 615 | LeaveFunction(10); |
| 616 | return NF_STOLEN; |
| 617 | |
| 618 | tx_error_icmp: |
| 619 | dst_link_failure(skb); |
| 620 | tx_error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | kfree_skb(skb); |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 622 | LeaveFunction(10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | return NF_STOLEN; |
| 624 | tx_error_put: |
| 625 | ip_rt_put(rt); |
| 626 | goto tx_error; |
| 627 | } |
| 628 | |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 629 | #ifdef CONFIG_IP_VS_IPV6 |
| 630 | int |
| 631 | ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, |
| 632 | struct ip_vs_protocol *pp) |
| 633 | { |
| 634 | struct rt6_info *rt; /* Route to the other host */ |
| 635 | int mtu; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 636 | int local; |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 637 | |
| 638 | EnterFunction(10); |
| 639 | |
| 640 | /* check if it is a connection of no-client-port */ |
| 641 | if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) { |
| 642 | __be16 _pt, *p; |
| 643 | p = skb_header_pointer(skb, sizeof(struct ipv6hdr), |
| 644 | sizeof(_pt), &_pt); |
| 645 | if (p == NULL) |
| 646 | goto tx_error; |
| 647 | ip_vs_conn_fill_cport(cp, *p); |
| 648 | IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p)); |
| 649 | } |
| 650 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 651 | if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL, |
David S. Miller | e58b344 | 2011-05-12 18:22:34 -0400 | [diff] [blame] | 652 | 0, (IP_VS_RT_MODE_LOCAL | |
| 653 | IP_VS_RT_MODE_NON_LOCAL | |
| 654 | IP_VS_RT_MODE_RDR)))) |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 655 | goto tx_error_icmp; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 656 | local = __ip_vs_is_local_route6(rt); |
| 657 | /* |
| 658 | * Avoid duplicate tuple in reply direction for NAT traffic |
| 659 | * to local address when connection is sync-ed |
| 660 | */ |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 661 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 662 | if (cp->flags & IP_VS_CONN_F_SYNC && local) { |
| 663 | enum ip_conntrack_info ctinfo; |
| 664 | struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo); |
| 665 | |
| 666 | if (ct && !nf_ct_is_untracked(ct)) { |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 667 | IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0, |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 668 | "ip_vs_nat_xmit_v6(): " |
| 669 | "stopping DNAT to local address"); |
| 670 | goto tx_error_put; |
| 671 | } |
| 672 | } |
| 673 | #endif |
| 674 | |
| 675 | /* From world but DNAT to loopback address? */ |
| 676 | if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) && |
| 677 | ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) { |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 678 | IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0, |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 679 | "ip_vs_nat_xmit_v6(): " |
| 680 | "stopping DNAT to loopback address"); |
| 681 | goto tx_error_put; |
| 682 | } |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 683 | |
| 684 | /* MTU checking */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 685 | mtu = dst_mtu(&rt->dst); |
Simon Horman | 8f1b03a | 2010-11-09 10:08:49 +0900 | [diff] [blame] | 686 | if (skb->len > mtu && !skb_is_gso(skb)) { |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 687 | if (!skb->dev) { |
| 688 | struct net *net = dev_net(skb_dst(skb)->dev); |
| 689 | |
| 690 | skb->dev = net->loopback_dev; |
| 691 | } |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 692 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 693 | IP_VS_DBG_RL_PKT(0, AF_INET6, pp, skb, 0, |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 694 | "ip_vs_nat_xmit_v6(): frag needed for"); |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 695 | goto tx_error_put; |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* copy-on-write the packet before mangling it */ |
| 699 | if (!skb_make_writable(skb, sizeof(struct ipv6hdr))) |
| 700 | goto tx_error_put; |
| 701 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 702 | if (skb_cow(skb, rt->dst.dev->hard_header_len)) |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 703 | goto tx_error_put; |
| 704 | |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 705 | /* mangle the packet */ |
| 706 | if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp)) |
| 707 | goto tx_error; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 708 | ipv6_hdr(skb)->daddr = cp->daddr.in6; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 709 | |
| 710 | if (!local || !skb->dev) { |
| 711 | /* drop the old route when skb is not shared */ |
| 712 | skb_dst_drop(skb); |
| 713 | skb_dst_set(skb, &rt->dst); |
| 714 | } else { |
| 715 | /* destined to loopback, do we need to change route? */ |
| 716 | dst_release(&rt->dst); |
| 717 | } |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 718 | |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 719 | IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT"); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 720 | |
| 721 | /* FIXME: when application helper enlarges the packet and the length |
| 722 | is larger than the MTU of outgoing device, there will be still |
| 723 | MTU problem. */ |
| 724 | |
| 725 | /* Another hack: avoid icmp_send in ip_fragment */ |
| 726 | skb->local_df = 1; |
| 727 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 728 | IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 729 | |
| 730 | LeaveFunction(10); |
| 731 | return NF_STOLEN; |
| 732 | |
| 733 | tx_error_icmp: |
| 734 | dst_link_failure(skb); |
| 735 | tx_error: |
| 736 | LeaveFunction(10); |
| 737 | kfree_skb(skb); |
| 738 | return NF_STOLEN; |
| 739 | tx_error_put: |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 740 | dst_release(&rt->dst); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 741 | goto tx_error; |
| 742 | } |
| 743 | #endif |
| 744 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | |
| 746 | /* |
| 747 | * IP Tunneling transmitter |
| 748 | * |
| 749 | * This function encapsulates the packet in a new IP packet, its |
| 750 | * destination will be set to cp->daddr. Most code of this function |
| 751 | * is taken from ipip.c. |
| 752 | * |
| 753 | * It is used in VS/TUN cluster. The load balancer selects a real |
| 754 | * server from a cluster based on a scheduling algorithm, |
| 755 | * encapsulates the request packet and forwards it to the selected |
| 756 | * server. For example, all real servers are configured with |
| 757 | * "ifconfig tunl0 <Virtual IP Address> up". When the server receives |
| 758 | * the encapsulated packet, it will decapsulate the packet, processe |
| 759 | * the request and return the response packets directly to the client |
| 760 | * without passing the load balancer. This can greatly increase the |
| 761 | * scalability of virtual server. |
| 762 | * |
| 763 | * Used for ANY protocol |
| 764 | */ |
| 765 | int |
| 766 | ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, |
| 767 | struct ip_vs_protocol *pp) |
| 768 | { |
| 769 | struct rtable *rt; /* Route to the other host */ |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 770 | __be32 saddr; /* Source for tunnel */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | struct net_device *tdev; /* Device to other host */ |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 772 | struct iphdr *old_iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | u8 tos = old_iph->tos; |
Alexey Dobriyan | 76ab608 | 2006-01-06 13:24:29 -0800 | [diff] [blame] | 774 | __be16 df = old_iph->frag_off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | struct iphdr *iph; /* Our new IP header */ |
Chuck Lever | c2636b4 | 2007-10-23 21:07:32 -0700 | [diff] [blame] | 776 | unsigned int max_headroom; /* The extra header space needed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | int mtu; |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 778 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
| 780 | EnterFunction(10); |
| 781 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 782 | if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip, |
Changli Gao | 17a8f8e | 2011-02-24 08:19:57 +0800 | [diff] [blame] | 783 | RT_TOS(tos), IP_VS_RT_MODE_LOCAL | |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 784 | IP_VS_RT_MODE_NON_LOCAL, |
| 785 | &saddr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | goto tx_error_icmp; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 787 | if (rt->rt_flags & RTCF_LOCAL) { |
| 788 | ip_rt_put(rt); |
| 789 | IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1); |
| 790 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 792 | tdev = rt->dst.dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 794 | mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | if (mtu < 68) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 796 | IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__); |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 797 | goto tx_error_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 799 | if (skb_dst(skb)) |
| 800 | skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
YOSHIFUJI Hideaki | 4412ec4 | 2007-03-07 14:19:10 +0900 | [diff] [blame] | 802 | df |= (old_iph->frag_off & htons(IP_DF)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
Simon Horman | 8f1b03a | 2010-11-09 10:08:49 +0900 | [diff] [blame] | 804 | if ((old_iph->frag_off & htons(IP_DF) && |
| 805 | mtu < ntohs(old_iph->tot_len) && !skb_is_gso(skb))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu)); |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 807 | IP_VS_DBG_RL("%s(): frag needed\n", __func__); |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 808 | goto tx_error_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /* |
| 812 | * Okay, now see if we can stuff it in the buffer as-is. |
| 813 | */ |
| 814 | max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr); |
| 815 | |
| 816 | if (skb_headroom(skb) < max_headroom |
| 817 | || skb_cloned(skb) || skb_shared(skb)) { |
| 818 | struct sk_buff *new_skb = |
| 819 | skb_realloc_headroom(skb, max_headroom); |
| 820 | if (!new_skb) { |
| 821 | ip_rt_put(rt); |
| 822 | kfree_skb(skb); |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 823 | IP_VS_ERR_RL("%s(): no memory\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | return NF_STOLEN; |
| 825 | } |
Eric Dumazet | 5d0ba55 | 2012-06-04 01:17:19 +0000 | [diff] [blame^] | 826 | consume_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | skb = new_skb; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 828 | old_iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 831 | skb->transport_header = skb->network_header; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | |
| 833 | /* fix old IP header checksum */ |
| 834 | ip_send_check(old_iph); |
| 835 | |
Arnaldo Carvalho de Melo | e2d1bca | 2007-04-10 20:46:21 -0700 | [diff] [blame] | 836 | skb_push(skb, sizeof(struct iphdr)); |
| 837 | skb_reset_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
| 839 | |
| 840 | /* drop old route */ |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 841 | skb_dst_drop(skb); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 842 | skb_dst_set(skb, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
| 844 | /* |
| 845 | * Push down and install the IPIP header. |
| 846 | */ |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 847 | iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | iph->version = 4; |
| 849 | iph->ihl = sizeof(struct iphdr)>>2; |
| 850 | iph->frag_off = df; |
| 851 | iph->protocol = IPPROTO_IPIP; |
| 852 | iph->tos = tos; |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 853 | iph->daddr = cp->daddr.ip; |
| 854 | iph->saddr = saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | iph->ttl = old_iph->ttl; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 856 | ip_select_ident(iph, &rt->dst, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | |
| 858 | /* Another hack: avoid icmp_send in ip_fragment */ |
| 859 | skb->local_df = 1; |
| 860 | |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 861 | ret = IP_VS_XMIT_TUNNEL(skb, cp); |
| 862 | if (ret == NF_ACCEPT) |
| 863 | ip_local_out(skb); |
| 864 | else if (ret == NF_DROP) |
| 865 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
| 867 | LeaveFunction(10); |
| 868 | |
| 869 | return NF_STOLEN; |
| 870 | |
| 871 | tx_error_icmp: |
| 872 | dst_link_failure(skb); |
| 873 | tx_error: |
| 874 | kfree_skb(skb); |
| 875 | LeaveFunction(10); |
| 876 | return NF_STOLEN; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 877 | tx_error_put: |
| 878 | ip_rt_put(rt); |
| 879 | goto tx_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 882 | #ifdef CONFIG_IP_VS_IPV6 |
| 883 | int |
| 884 | ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, |
| 885 | struct ip_vs_protocol *pp) |
| 886 | { |
| 887 | struct rt6_info *rt; /* Route to the other host */ |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 888 | struct in6_addr saddr; /* Source for tunnel */ |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 889 | struct net_device *tdev; /* Device to other host */ |
| 890 | struct ipv6hdr *old_iph = ipv6_hdr(skb); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 891 | struct ipv6hdr *iph; /* Our new IP header */ |
| 892 | unsigned int max_headroom; /* The extra header space needed */ |
| 893 | int mtu; |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 894 | int ret; |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 895 | |
| 896 | EnterFunction(10); |
| 897 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 898 | if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, |
David S. Miller | e58b344 | 2011-05-12 18:22:34 -0400 | [diff] [blame] | 899 | &saddr, 1, (IP_VS_RT_MODE_LOCAL | |
| 900 | IP_VS_RT_MODE_NON_LOCAL)))) |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 901 | goto tx_error_icmp; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 902 | if (__ip_vs_is_local_route6(rt)) { |
| 903 | dst_release(&rt->dst); |
| 904 | IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1); |
| 905 | } |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 906 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 907 | tdev = rt->dst.dev; |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 908 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 909 | mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr); |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 910 | if (mtu < IPV6_MIN_MTU) { |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 911 | IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__, |
| 912 | IPV6_MIN_MTU); |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 913 | goto tx_error_put; |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 914 | } |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 915 | if (skb_dst(skb)) |
| 916 | skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 917 | |
Simon Horman | 8f1b03a | 2010-11-09 10:08:49 +0900 | [diff] [blame] | 918 | if (mtu < ntohs(old_iph->payload_len) + sizeof(struct ipv6hdr) && |
| 919 | !skb_is_gso(skb)) { |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 920 | if (!skb->dev) { |
| 921 | struct net *net = dev_net(skb_dst(skb)->dev); |
| 922 | |
| 923 | skb->dev = net->loopback_dev; |
| 924 | } |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 925 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 926 | IP_VS_DBG_RL("%s(): frag needed\n", __func__); |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 927 | goto tx_error_put; |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | /* |
| 931 | * Okay, now see if we can stuff it in the buffer as-is. |
| 932 | */ |
| 933 | max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr); |
| 934 | |
| 935 | if (skb_headroom(skb) < max_headroom |
| 936 | || skb_cloned(skb) || skb_shared(skb)) { |
| 937 | struct sk_buff *new_skb = |
| 938 | skb_realloc_headroom(skb, max_headroom); |
| 939 | if (!new_skb) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 940 | dst_release(&rt->dst); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 941 | kfree_skb(skb); |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 942 | IP_VS_ERR_RL("%s(): no memory\n", __func__); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 943 | return NF_STOLEN; |
| 944 | } |
Eric Dumazet | 5d0ba55 | 2012-06-04 01:17:19 +0000 | [diff] [blame^] | 945 | consume_skb(skb); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 946 | skb = new_skb; |
| 947 | old_iph = ipv6_hdr(skb); |
| 948 | } |
| 949 | |
Hans Schillstrom | 714f095 | 2010-10-19 10:38:48 +0200 | [diff] [blame] | 950 | skb->transport_header = skb->network_header; |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 951 | |
| 952 | skb_push(skb, sizeof(struct ipv6hdr)); |
| 953 | skb_reset_network_header(skb); |
| 954 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
| 955 | |
| 956 | /* drop old route */ |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 957 | skb_dst_drop(skb); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 958 | skb_dst_set(skb, &rt->dst); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 959 | |
| 960 | /* |
| 961 | * Push down and install the IPIP header. |
| 962 | */ |
| 963 | iph = ipv6_hdr(skb); |
| 964 | iph->version = 6; |
| 965 | iph->nexthdr = IPPROTO_IPV6; |
Harvey Harrison | b7b45f4 | 2008-11-10 16:46:06 -0800 | [diff] [blame] | 966 | iph->payload_len = old_iph->payload_len; |
| 967 | be16_add_cpu(&iph->payload_len, sizeof(*old_iph)); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 968 | iph->priority = old_iph->priority; |
| 969 | memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl)); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 970 | iph->daddr = cp->daddr.in6; |
| 971 | iph->saddr = saddr; |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 972 | iph->hop_limit = old_iph->hop_limit; |
| 973 | |
| 974 | /* Another hack: avoid icmp_send in ip_fragment */ |
| 975 | skb->local_df = 1; |
| 976 | |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 977 | ret = IP_VS_XMIT_TUNNEL(skb, cp); |
| 978 | if (ret == NF_ACCEPT) |
| 979 | ip6_local_out(skb); |
| 980 | else if (ret == NF_DROP) |
| 981 | kfree_skb(skb); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 982 | |
| 983 | LeaveFunction(10); |
| 984 | |
| 985 | return NF_STOLEN; |
| 986 | |
| 987 | tx_error_icmp: |
| 988 | dst_link_failure(skb); |
| 989 | tx_error: |
| 990 | kfree_skb(skb); |
| 991 | LeaveFunction(10); |
| 992 | return NF_STOLEN; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 993 | tx_error_put: |
| 994 | dst_release(&rt->dst); |
| 995 | goto tx_error; |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 996 | } |
| 997 | #endif |
| 998 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | |
| 1000 | /* |
| 1001 | * Direct Routing transmitter |
| 1002 | * Used for ANY protocol |
| 1003 | */ |
| 1004 | int |
| 1005 | ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, |
| 1006 | struct ip_vs_protocol *pp) |
| 1007 | { |
| 1008 | struct rtable *rt; /* Route to the other host */ |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1009 | struct iphdr *iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | int mtu; |
| 1011 | |
| 1012 | EnterFunction(10); |
| 1013 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1014 | if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip, |
Changli Gao | 17a8f8e | 2011-02-24 08:19:57 +0800 | [diff] [blame] | 1015 | RT_TOS(iph->tos), |
| 1016 | IP_VS_RT_MODE_LOCAL | |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 1017 | IP_VS_RT_MODE_NON_LOCAL, NULL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | goto tx_error_icmp; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1019 | if (rt->rt_flags & RTCF_LOCAL) { |
| 1020 | ip_rt_put(rt); |
| 1021 | IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1); |
| 1022 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
| 1024 | /* MTU checking */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1025 | mtu = dst_mtu(&rt->dst); |
Simon Horman | 8f1b03a | 2010-11-09 10:08:49 +0900 | [diff] [blame] | 1026 | if ((iph->frag_off & htons(IP_DF)) && skb->len > mtu && |
| 1027 | !skb_is_gso(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu)); |
| 1029 | ip_rt_put(rt); |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1030 | IP_VS_DBG_RL("%s(): frag needed\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | goto tx_error; |
| 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | * Call ip_send_check because we are not sure it is called |
| 1036 | * after ip_defrag. Is copy-on-write needed? |
| 1037 | */ |
| 1038 | if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) { |
| 1039 | ip_rt_put(rt); |
| 1040 | return NF_STOLEN; |
| 1041 | } |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1042 | ip_send_check(ip_hdr(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | |
| 1044 | /* drop old route */ |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1045 | skb_dst_drop(skb); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1046 | skb_dst_set(skb, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | |
| 1048 | /* Another hack: avoid icmp_send in ip_fragment */ |
| 1049 | skb->local_df = 1; |
| 1050 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1051 | IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | |
| 1053 | LeaveFunction(10); |
| 1054 | return NF_STOLEN; |
| 1055 | |
| 1056 | tx_error_icmp: |
| 1057 | dst_link_failure(skb); |
| 1058 | tx_error: |
| 1059 | kfree_skb(skb); |
| 1060 | LeaveFunction(10); |
| 1061 | return NF_STOLEN; |
| 1062 | } |
| 1063 | |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1064 | #ifdef CONFIG_IP_VS_IPV6 |
| 1065 | int |
| 1066 | ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, |
| 1067 | struct ip_vs_protocol *pp) |
| 1068 | { |
| 1069 | struct rt6_info *rt; /* Route to the other host */ |
| 1070 | int mtu; |
| 1071 | |
| 1072 | EnterFunction(10); |
| 1073 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1074 | if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL, |
David S. Miller | e58b344 | 2011-05-12 18:22:34 -0400 | [diff] [blame] | 1075 | 0, (IP_VS_RT_MODE_LOCAL | |
| 1076 | IP_VS_RT_MODE_NON_LOCAL)))) |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1077 | goto tx_error_icmp; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1078 | if (__ip_vs_is_local_route6(rt)) { |
| 1079 | dst_release(&rt->dst); |
| 1080 | IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1); |
| 1081 | } |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1082 | |
| 1083 | /* MTU checking */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1084 | mtu = dst_mtu(&rt->dst); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1085 | if (skb->len > mtu) { |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1086 | if (!skb->dev) { |
| 1087 | struct net *net = dev_net(skb_dst(skb)->dev); |
| 1088 | |
| 1089 | skb->dev = net->loopback_dev; |
| 1090 | } |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 1091 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1092 | dst_release(&rt->dst); |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1093 | IP_VS_DBG_RL("%s(): frag needed\n", __func__); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1094 | goto tx_error; |
| 1095 | } |
| 1096 | |
| 1097 | /* |
| 1098 | * Call ip_send_check because we are not sure it is called |
| 1099 | * after ip_defrag. Is copy-on-write needed? |
| 1100 | */ |
| 1101 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 1102 | if (unlikely(skb == NULL)) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1103 | dst_release(&rt->dst); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1104 | return NF_STOLEN; |
| 1105 | } |
| 1106 | |
| 1107 | /* drop old route */ |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1108 | skb_dst_drop(skb); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1109 | skb_dst_set(skb, &rt->dst); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1110 | |
| 1111 | /* Another hack: avoid icmp_send in ip_fragment */ |
| 1112 | skb->local_df = 1; |
| 1113 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1114 | IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1115 | |
| 1116 | LeaveFunction(10); |
| 1117 | return NF_STOLEN; |
| 1118 | |
| 1119 | tx_error_icmp: |
| 1120 | dst_link_failure(skb); |
| 1121 | tx_error: |
| 1122 | kfree_skb(skb); |
| 1123 | LeaveFunction(10); |
| 1124 | return NF_STOLEN; |
| 1125 | } |
| 1126 | #endif |
| 1127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | |
| 1129 | /* |
| 1130 | * ICMP packet transmitter |
| 1131 | * called by the ip_vs_in_icmp |
| 1132 | */ |
| 1133 | int |
| 1134 | ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 1135 | struct ip_vs_protocol *pp, int offset, unsigned int hooknum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | { |
| 1137 | struct rtable *rt; /* Route to the other host */ |
| 1138 | int mtu; |
| 1139 | int rc; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1140 | int local; |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 1141 | int rt_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | |
| 1143 | EnterFunction(10); |
| 1144 | |
| 1145 | /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be |
| 1146 | forwarded directly here, because there is no need to |
| 1147 | translate address/port back */ |
| 1148 | if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) { |
| 1149 | if (cp->packet_xmit) |
| 1150 | rc = cp->packet_xmit(skb, cp, pp); |
| 1151 | else |
| 1152 | rc = NF_ACCEPT; |
| 1153 | /* do not touch skb anymore */ |
| 1154 | atomic_inc(&cp->in_pkts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | goto out; |
| 1156 | } |
| 1157 | |
| 1158 | /* |
| 1159 | * mangle and send the packet here (only for VS/NAT) |
| 1160 | */ |
| 1161 | |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 1162 | /* LOCALNODE from FORWARD hook is not supported */ |
| 1163 | rt_mode = (hooknum != NF_INET_FORWARD) ? |
| 1164 | IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL | |
| 1165 | IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1166 | if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip, |
Changli Gao | 17a8f8e | 2011-02-24 08:19:57 +0800 | [diff] [blame] | 1167 | RT_TOS(ip_hdr(skb)->tos), |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 1168 | rt_mode, NULL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | goto tx_error_icmp; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1170 | local = rt->rt_flags & RTCF_LOCAL; |
| 1171 | |
| 1172 | /* |
| 1173 | * Avoid duplicate tuple in reply direction for NAT traffic |
| 1174 | * to local address when connection is sync-ed |
| 1175 | */ |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 1176 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1177 | if (cp->flags & IP_VS_CONN_F_SYNC && local) { |
| 1178 | enum ip_conntrack_info ctinfo; |
| 1179 | struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo); |
| 1180 | |
| 1181 | if (ct && !nf_ct_is_untracked(ct)) { |
| 1182 | IP_VS_DBG(10, "%s(): " |
| 1183 | "stopping DNAT to local address %pI4\n", |
| 1184 | __func__, &cp->daddr.ip); |
| 1185 | goto tx_error_put; |
| 1186 | } |
| 1187 | } |
| 1188 | #endif |
| 1189 | |
| 1190 | /* From world but DNAT to loopback address? */ |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 1191 | if (local && ipv4_is_loopback(cp->daddr.ip) && |
David S. Miller | c753796 | 2010-11-11 17:07:48 -0800 | [diff] [blame] | 1192 | rt_is_input_route(skb_rtable(skb))) { |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1193 | IP_VS_DBG(1, "%s(): " |
| 1194 | "stopping DNAT to loopback %pI4\n", |
| 1195 | __func__, &cp->daddr.ip); |
| 1196 | goto tx_error_put; |
| 1197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | |
| 1199 | /* MTU checking */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1200 | mtu = dst_mtu(&rt->dst); |
Simon Horman | 8f1b03a | 2010-11-09 10:08:49 +0900 | [diff] [blame] | 1201 | if ((skb->len > mtu) && (ip_hdr(skb)->frag_off & htons(IP_DF)) && |
| 1202 | !skb_is_gso(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu)); |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1204 | IP_VS_DBG_RL("%s(): frag needed\n", __func__); |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1205 | goto tx_error_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | /* copy-on-write the packet before mangling it */ |
Herbert Xu | af1e1cf | 2007-10-14 00:39:33 -0700 | [diff] [blame] | 1209 | if (!skb_make_writable(skb, offset)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | goto tx_error_put; |
| 1211 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1212 | if (skb_cow(skb, rt->dst.dev->hard_header_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | goto tx_error_put; |
| 1214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | ip_vs_nat_icmp(skb, pp, cp, 0); |
| 1216 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1217 | if (!local) { |
| 1218 | /* drop the old route when skb is not shared */ |
| 1219 | skb_dst_drop(skb); |
| 1220 | skb_dst_set(skb, &rt->dst); |
| 1221 | } else { |
| 1222 | ip_rt_put(rt); |
| 1223 | /* |
| 1224 | * Some IPv4 replies get local address from routes, |
| 1225 | * not from iph, so while we DNAT after routing |
| 1226 | * we need this second input/output route. |
| 1227 | */ |
| 1228 | if (!__ip_vs_reroute_locally(skb)) |
| 1229 | goto tx_error; |
| 1230 | } |
| 1231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | /* Another hack: avoid icmp_send in ip_fragment */ |
| 1233 | skb->local_df = 1; |
| 1234 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1235 | IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | |
| 1237 | rc = NF_STOLEN; |
| 1238 | goto out; |
| 1239 | |
| 1240 | tx_error_icmp: |
| 1241 | dst_link_failure(skb); |
| 1242 | tx_error: |
| 1243 | dev_kfree_skb(skb); |
| 1244 | rc = NF_STOLEN; |
| 1245 | out: |
| 1246 | LeaveFunction(10); |
| 1247 | return rc; |
| 1248 | tx_error_put: |
| 1249 | ip_rt_put(rt); |
| 1250 | goto tx_error; |
| 1251 | } |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1252 | |
| 1253 | #ifdef CONFIG_IP_VS_IPV6 |
| 1254 | int |
| 1255 | ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 1256 | struct ip_vs_protocol *pp, int offset, unsigned int hooknum) |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1257 | { |
| 1258 | struct rt6_info *rt; /* Route to the other host */ |
| 1259 | int mtu; |
| 1260 | int rc; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1261 | int local; |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 1262 | int rt_mode; |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1263 | |
| 1264 | EnterFunction(10); |
| 1265 | |
| 1266 | /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be |
| 1267 | forwarded directly here, because there is no need to |
| 1268 | translate address/port back */ |
| 1269 | if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) { |
| 1270 | if (cp->packet_xmit) |
| 1271 | rc = cp->packet_xmit(skb, cp, pp); |
| 1272 | else |
| 1273 | rc = NF_ACCEPT; |
| 1274 | /* do not touch skb anymore */ |
| 1275 | atomic_inc(&cp->in_pkts); |
| 1276 | goto out; |
| 1277 | } |
| 1278 | |
| 1279 | /* |
| 1280 | * mangle and send the packet here (only for VS/NAT) |
| 1281 | */ |
| 1282 | |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 1283 | /* LOCALNODE from FORWARD hook is not supported */ |
| 1284 | rt_mode = (hooknum != NF_INET_FORWARD) ? |
| 1285 | IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL | |
| 1286 | IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL; |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1287 | if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL, |
Julian Anastasov | c92f5ca | 2011-05-10 12:46:05 +0000 | [diff] [blame] | 1288 | 0, rt_mode))) |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1289 | goto tx_error_icmp; |
| 1290 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1291 | local = __ip_vs_is_local_route6(rt); |
| 1292 | /* |
| 1293 | * Avoid duplicate tuple in reply direction for NAT traffic |
| 1294 | * to local address when connection is sync-ed |
| 1295 | */ |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 1296 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1297 | if (cp->flags & IP_VS_CONN_F_SYNC && local) { |
| 1298 | enum ip_conntrack_info ctinfo; |
| 1299 | struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo); |
| 1300 | |
| 1301 | if (ct && !nf_ct_is_untracked(ct)) { |
| 1302 | IP_VS_DBG(10, "%s(): " |
| 1303 | "stopping DNAT to local address %pI6\n", |
| 1304 | __func__, &cp->daddr.in6); |
| 1305 | goto tx_error_put; |
| 1306 | } |
| 1307 | } |
| 1308 | #endif |
| 1309 | |
| 1310 | /* From world but DNAT to loopback address? */ |
| 1311 | if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) && |
| 1312 | ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) { |
| 1313 | IP_VS_DBG(1, "%s(): " |
| 1314 | "stopping DNAT to loopback %pI6\n", |
| 1315 | __func__, &cp->daddr.in6); |
| 1316 | goto tx_error_put; |
| 1317 | } |
| 1318 | |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1319 | /* MTU checking */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1320 | mtu = dst_mtu(&rt->dst); |
Simon Horman | 8f1b03a | 2010-11-09 10:08:49 +0900 | [diff] [blame] | 1321 | if (skb->len > mtu && !skb_is_gso(skb)) { |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1322 | if (!skb->dev) { |
| 1323 | struct net *net = dev_net(skb_dst(skb)->dev); |
| 1324 | |
| 1325 | skb->dev = net->loopback_dev; |
| 1326 | } |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 1327 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1328 | IP_VS_DBG_RL("%s(): frag needed\n", __func__); |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1329 | goto tx_error_put; |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | /* copy-on-write the packet before mangling it */ |
| 1333 | if (!skb_make_writable(skb, offset)) |
| 1334 | goto tx_error_put; |
| 1335 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1336 | if (skb_cow(skb, rt->dst.dev->hard_header_len)) |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1337 | goto tx_error_put; |
| 1338 | |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1339 | ip_vs_nat_icmp_v6(skb, pp, cp, 0); |
| 1340 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1341 | if (!local || !skb->dev) { |
| 1342 | /* drop the old route when skb is not shared */ |
| 1343 | skb_dst_drop(skb); |
| 1344 | skb_dst_set(skb, &rt->dst); |
| 1345 | } else { |
| 1346 | /* destined to loopback, do we need to change route? */ |
| 1347 | dst_release(&rt->dst); |
| 1348 | } |
| 1349 | |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1350 | /* Another hack: avoid icmp_send in ip_fragment */ |
| 1351 | skb->local_df = 1; |
| 1352 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1353 | IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1354 | |
| 1355 | rc = NF_STOLEN; |
| 1356 | goto out; |
| 1357 | |
| 1358 | tx_error_icmp: |
| 1359 | dst_link_failure(skb); |
| 1360 | tx_error: |
| 1361 | dev_kfree_skb(skb); |
| 1362 | rc = NF_STOLEN; |
| 1363 | out: |
| 1364 | LeaveFunction(10); |
| 1365 | return rc; |
| 1366 | tx_error_put: |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1367 | dst_release(&rt->dst); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 1368 | goto tx_error; |
| 1369 | } |
| 1370 | #endif |