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