blob: e3a697234a988cb9c2f03afbf122a56f99a454aa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ip_vs_proto_tcp.c: TCP load balancing support for IPVS
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
5 * Julian Anastasov <ja@ssi.bg>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Hans Schillstrom4a85b962011-01-03 14:44:47 +010012 * Changes: Hans Schillstrom <hans.schillstrom@ericsson.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Hans Schillstrom4a85b962011-01-03 14:44:47 +010014 * Network name space (netns) aware.
15 * Global data moved to netns i.e struct netns_ipvs
16 * tcp_timeouts table has copy per netns in a hash table per
17 * protocol ip_vs_proto_data and is handled by netns
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Hannes Eder9aada7a2009-07-30 14:29:44 -070020#define KMSG_COMPONENT "IPVS"
21#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/kernel.h>
24#include <linux/ip.h>
25#include <linux/tcp.h> /* for tcphdr */
26#include <net/ip.h>
27#include <net/tcp.h> /* for csum_tcpudp_magic */
Stephen Rothwell63f2c042008-09-12 23:23:50 -070028#include <net/ip6_checksum.h>
Herbert Xuaf1e1cf2007-10-14 00:39:33 -070029#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/netfilter_ipv4.h>
31
32#include <net/ip_vs.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static int
Hans Schillstrom93304192011-01-03 14:44:51 +010035tcp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +020036 int *verdict, struct ip_vs_conn **cpp,
37 struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Hans Schillstromfc723252011-01-03 14:44:43 +010039 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct ip_vs_service *svc;
41 struct tcphdr _tcph, *th;
Alexander Frolkinc6c96c12013-06-13 08:56:15 +010042 struct netns_ipvs *ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +020044 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (th == NULL) {
46 *verdict = NF_DROP;
47 return 0;
48 }
Hans Schillstromfc723252011-01-03 14:44:43 +010049 net = skb_net(skb);
Alexander Frolkinc6c96c12013-06-13 08:56:15 +010050 ipvs = net_ipvs(net);
Julian Anastasov190ecd22010-10-17 16:24:37 +030051 /* No !th->ack check to allow scheduling on SYN+ACK for Active FTP */
Julian Anastasovceec4c32013-03-22 11:46:53 +020052 rcu_read_lock();
Alexander Frolkinc6c96c12013-06-13 08:56:15 +010053 if ((th->syn || sysctl_sloppy_tcp(ipvs)) && !th->rst &&
Julian Anastasovceec4c32013-03-22 11:46:53 +020054 (svc = ip_vs_service_find(net, af, skb->mark, iph->protocol,
55 &iph->daddr, th->dest))) {
Julian Anastasov190ecd22010-10-17 16:24:37 +030056 int ignored;
57
Alexander Frolkinc6c96c12013-06-13 08:56:15 +010058 if (ip_vs_todrop(ipvs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 /*
60 * It seems that we are very loaded.
61 * We have to drop this packet :(
62 */
Julian Anastasovceec4c32013-03-22 11:46:53 +020063 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 *verdict = NF_DROP;
65 return 0;
66 }
67
68 /*
69 * Let the virtual server select a real server for the
70 * incoming connection, and create a connection entry.
71 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +020072 *cpp = ip_vs_schedule(svc, skb, pd, &ignored, iph);
Hans Schillstroma5959d52010-11-19 14:25:10 +010073 if (!*cpp && ignored <= 0) {
74 if (!ignored)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +020075 *verdict = ip_vs_leave(svc, skb, pd, iph);
Julian Anastasovceec4c32013-03-22 11:46:53 +020076 else
Hans Schillstroma5959d52010-11-19 14:25:10 +010077 *verdict = NF_DROP;
Julian Anastasovceec4c32013-03-22 11:46:53 +020078 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return 0;
80 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
Julian Anastasovceec4c32013-03-22 11:46:53 +020082 rcu_read_unlock();
Hans Schillstroma5959d52010-11-19 14:25:10 +010083 /* NF_ACCEPT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return 1;
85}
86
87
88static inline void
Julius Volz0bbdd422008-09-02 15:55:42 +020089tcp_fast_csum_update(int af, struct tcphdr *tcph,
90 const union nf_inet_addr *oldip,
91 const union nf_inet_addr *newip,
Al Viro014d7302006-09-28 14:29:52 -070092 __be16 oldport, __be16 newport)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Julius Volz0bbdd422008-09-02 15:55:42 +020094#ifdef CONFIG_IP_VS_IPV6
95 if (af == AF_INET6)
96 tcph->check =
97 csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
98 ip_vs_check_diff2(oldport, newport,
99 ~csum_unfold(tcph->check))));
100 else
101#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 tcph->check =
Julius Volz0bbdd422008-09-02 15:55:42 +0200103 csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
Al Virof9214b22006-11-16 02:41:18 -0800104 ip_vs_check_diff2(oldport, newport,
105 ~csum_unfold(tcph->check))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108
Simon Horman503e81f2008-09-08 12:04:21 +1000109static inline void
110tcp_partial_csum_update(int af, struct tcphdr *tcph,
111 const union nf_inet_addr *oldip,
112 const union nf_inet_addr *newip,
113 __be16 oldlen, __be16 newlen)
114{
115#ifdef CONFIG_IP_VS_IPV6
116 if (af == AF_INET6)
117 tcph->check =
Julian Anastasov5bc90682010-10-17 16:14:31 +0300118 ~csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6,
Simon Horman503e81f2008-09-08 12:04:21 +1000119 ip_vs_check_diff2(oldlen, newlen,
Julian Anastasov5bc90682010-10-17 16:14:31 +0300120 csum_unfold(tcph->check))));
Simon Horman503e81f2008-09-08 12:04:21 +1000121 else
122#endif
123 tcph->check =
Julian Anastasov5bc90682010-10-17 16:14:31 +0300124 ~csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip,
Simon Horman503e81f2008-09-08 12:04:21 +1000125 ip_vs_check_diff2(oldlen, newlen,
Julian Anastasov5bc90682010-10-17 16:14:31 +0300126 csum_unfold(tcph->check))));
Simon Horman503e81f2008-09-08 12:04:21 +1000127}
128
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static int
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200131tcp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
132 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct tcphdr *tcph;
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200135 unsigned int tcphoff = iph->len;
Simon Horman503e81f2008-09-08 12:04:21 +1000136 int oldlen;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300137 int payload_csum = 0;
Julius Volz0bbdd422008-09-02 15:55:42 +0200138
139#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200140 if (cp->af == AF_INET6 && iph->fragoffs)
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200141 return 1;
Julius Volz0bbdd422008-09-02 15:55:42 +0200142#endif
Simon Horman503e81f2008-09-08 12:04:21 +1000143 oldlen = skb->len - tcphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /* csum_check requires unshared skb */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700146 if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return 0;
148
149 if (unlikely(cp->app != NULL)) {
Julian Anastasov8b27b102010-10-17 16:17:20 +0300150 int ret;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /* Some checks before mangling */
Julius Volz0bbdd422008-09-02 15:55:42 +0200153 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return 0;
155
156 /* Call application helper if needed */
Julian Anastasov8b27b102010-10-17 16:17:20 +0300157 if (!(ret = ip_vs_app_pkt_out(cp, skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return 0;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300159 /* ret=2: csum update is needed after payload mangling */
160 if (ret == 1)
161 oldlen = skb->len - tcphoff;
162 else
163 payload_csum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165
Julius Volz0bbdd422008-09-02 15:55:42 +0200166 tcph = (void *)skb_network_header(skb) + tcphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 tcph->source = cp->vport;
168
169 /* Adjust TCP checksums */
Simon Horman503e81f2008-09-08 12:04:21 +1000170 if (skb->ip_summed == CHECKSUM_PARTIAL) {
171 tcp_partial_csum_update(cp->af, tcph, &cp->daddr, &cp->vaddr,
Harvey Harrisonca620592008-11-06 23:09:56 -0800172 htons(oldlen),
173 htons(skb->len - tcphoff));
Julian Anastasov8b27b102010-10-17 16:17:20 +0300174 } else if (!payload_csum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /* Only port and addr are changed, do fast csum update */
Julius Volz0bbdd422008-09-02 15:55:42 +0200176 tcp_fast_csum_update(cp->af, tcph, &cp->daddr, &cp->vaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 cp->dport, cp->vport);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700178 if (skb->ip_summed == CHECKSUM_COMPLETE)
Julian Anastasov8b27b102010-10-17 16:17:20 +0300179 skb->ip_summed = (cp->app && pp->csum_check) ?
180 CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 } else {
182 /* full checksum calculation */
183 tcph->check = 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700184 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
Julius Volz0bbdd422008-09-02 15:55:42 +0200185#ifdef CONFIG_IP_VS_IPV6
186 if (cp->af == AF_INET6)
187 tcph->check = csum_ipv6_magic(&cp->vaddr.in6,
188 &cp->caddr.in6,
189 skb->len - tcphoff,
190 cp->protocol, skb->csum);
191 else
192#endif
193 tcph->check = csum_tcpudp_magic(cp->vaddr.ip,
194 cp->caddr.ip,
195 skb->len - tcphoff,
196 cp->protocol,
197 skb->csum);
Julian Anastasov8b27b102010-10-17 16:17:20 +0300198 skb->ip_summed = CHECKSUM_UNNECESSARY;
Julius Volz0bbdd422008-09-02 15:55:42 +0200199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n",
201 pp->name, tcph->check,
202 (char*)&(tcph->check) - (char*)tcph);
203 }
204 return 1;
205}
206
207
208static int
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200209tcp_dnat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
210 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 struct tcphdr *tcph;
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200213 unsigned int tcphoff = iph->len;
Simon Horman503e81f2008-09-08 12:04:21 +1000214 int oldlen;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300215 int payload_csum = 0;
Julius Volz0bbdd422008-09-02 15:55:42 +0200216
217#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200218 if (cp->af == AF_INET6 && iph->fragoffs)
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200219 return 1;
Julius Volz0bbdd422008-09-02 15:55:42 +0200220#endif
Simon Horman503e81f2008-09-08 12:04:21 +1000221 oldlen = skb->len - tcphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 /* csum_check requires unshared skb */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700224 if (!skb_make_writable(skb, tcphoff+sizeof(*tcph)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return 0;
226
227 if (unlikely(cp->app != NULL)) {
Julian Anastasov8b27b102010-10-17 16:17:20 +0300228 int ret;
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* Some checks before mangling */
Julius Volz0bbdd422008-09-02 15:55:42 +0200231 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return 0;
233
234 /*
235 * Attempt ip_vs_app call.
236 * It will fix ip_vs_conn and iph ack_seq stuff
237 */
Julian Anastasov8b27b102010-10-17 16:17:20 +0300238 if (!(ret = ip_vs_app_pkt_in(cp, skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return 0;
Julian Anastasov8b27b102010-10-17 16:17:20 +0300240 /* ret=2: csum update is needed after payload mangling */
241 if (ret == 1)
242 oldlen = skb->len - tcphoff;
243 else
244 payload_csum = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
Julius Volz0bbdd422008-09-02 15:55:42 +0200247 tcph = (void *)skb_network_header(skb) + tcphoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 tcph->dest = cp->dport;
249
250 /*
251 * Adjust TCP checksums
252 */
Simon Horman503e81f2008-09-08 12:04:21 +1000253 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Julian Anastasov5bc90682010-10-17 16:14:31 +0300254 tcp_partial_csum_update(cp->af, tcph, &cp->vaddr, &cp->daddr,
Harvey Harrisonca620592008-11-06 23:09:56 -0800255 htons(oldlen),
256 htons(skb->len - tcphoff));
Julian Anastasov8b27b102010-10-17 16:17:20 +0300257 } else if (!payload_csum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* Only port and addr are changed, do fast csum update */
Julius Volz0bbdd422008-09-02 15:55:42 +0200259 tcp_fast_csum_update(cp->af, tcph, &cp->vaddr, &cp->daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 cp->vport, cp->dport);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700261 if (skb->ip_summed == CHECKSUM_COMPLETE)
Julian Anastasov8b27b102010-10-17 16:17:20 +0300262 skb->ip_summed = (cp->app && pp->csum_check) ?
263 CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 } else {
265 /* full checksum calculation */
266 tcph->check = 0;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700267 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
Julius Volz0bbdd422008-09-02 15:55:42 +0200268#ifdef CONFIG_IP_VS_IPV6
269 if (cp->af == AF_INET6)
270 tcph->check = csum_ipv6_magic(&cp->caddr.in6,
271 &cp->daddr.in6,
272 skb->len - tcphoff,
273 cp->protocol, skb->csum);
274 else
275#endif
276 tcph->check = csum_tcpudp_magic(cp->caddr.ip,
277 cp->daddr.ip,
278 skb->len - tcphoff,
279 cp->protocol,
280 skb->csum);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700281 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 return 1;
284}
285
286
287static int
Julius Volz51ef3482008-09-02 15:55:40 +0200288tcp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Julius Volz51ef3482008-09-02 15:55:40 +0200290 unsigned int tcphoff;
291
292#ifdef CONFIG_IP_VS_IPV6
293 if (af == AF_INET6)
294 tcphoff = sizeof(struct ipv6hdr);
295 else
296#endif
297 tcphoff = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 switch (skb->ip_summed) {
300 case CHECKSUM_NONE:
301 skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0);
Patrick McHardy84fa7932006-08-29 16:44:56 -0700302 case CHECKSUM_COMPLETE:
Julius Volz51ef3482008-09-02 15:55:40 +0200303#ifdef CONFIG_IP_VS_IPV6
304 if (af == AF_INET6) {
305 if (csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
306 &ipv6_hdr(skb)->daddr,
307 skb->len - tcphoff,
308 ipv6_hdr(skb)->nexthdr,
309 skb->csum)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300310 IP_VS_DBG_RL_PKT(0, af, pp, skb, 0,
Julius Volz51ef3482008-09-02 15:55:40 +0200311 "Failed checksum for");
312 return 0;
313 }
314 } else
315#endif
316 if (csum_tcpudp_magic(ip_hdr(skb)->saddr,
317 ip_hdr(skb)->daddr,
318 skb->len - tcphoff,
319 ip_hdr(skb)->protocol,
320 skb->csum)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300321 IP_VS_DBG_RL_PKT(0, af, pp, skb, 0,
Julius Volz51ef3482008-09-02 15:55:40 +0200322 "Failed checksum for");
323 return 0;
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 break;
326 default:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700327 /* No need to checksum. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 break;
329 }
330
331 return 1;
332}
333
334
335#define TCP_DIR_INPUT 0
336#define TCP_DIR_OUTPUT 4
337#define TCP_DIR_INPUT_ONLY 8
338
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -0800339static const int tcp_state_off[IP_VS_DIR_LAST] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 [IP_VS_DIR_INPUT] = TCP_DIR_INPUT,
341 [IP_VS_DIR_OUTPUT] = TCP_DIR_OUTPUT,
342 [IP_VS_DIR_INPUT_ONLY] = TCP_DIR_INPUT_ONLY,
343};
344
345/*
346 * Timeout table[state]
347 */
Hans Schillstrom4a85b962011-01-03 14:44:47 +0100348static const int tcp_timeouts[IP_VS_TCP_S_LAST+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 [IP_VS_TCP_S_NONE] = 2*HZ,
350 [IP_VS_TCP_S_ESTABLISHED] = 15*60*HZ,
351 [IP_VS_TCP_S_SYN_SENT] = 2*60*HZ,
352 [IP_VS_TCP_S_SYN_RECV] = 1*60*HZ,
353 [IP_VS_TCP_S_FIN_WAIT] = 2*60*HZ,
354 [IP_VS_TCP_S_TIME_WAIT] = 2*60*HZ,
355 [IP_VS_TCP_S_CLOSE] = 10*HZ,
356 [IP_VS_TCP_S_CLOSE_WAIT] = 60*HZ,
357 [IP_VS_TCP_S_LAST_ACK] = 30*HZ,
358 [IP_VS_TCP_S_LISTEN] = 2*60*HZ,
359 [IP_VS_TCP_S_SYNACK] = 120*HZ,
360 [IP_VS_TCP_S_LAST] = 2*HZ,
361};
362
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700363static const char *const tcp_state_name_table[IP_VS_TCP_S_LAST+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 [IP_VS_TCP_S_NONE] = "NONE",
365 [IP_VS_TCP_S_ESTABLISHED] = "ESTABLISHED",
366 [IP_VS_TCP_S_SYN_SENT] = "SYN_SENT",
367 [IP_VS_TCP_S_SYN_RECV] = "SYN_RECV",
368 [IP_VS_TCP_S_FIN_WAIT] = "FIN_WAIT",
369 [IP_VS_TCP_S_TIME_WAIT] = "TIME_WAIT",
370 [IP_VS_TCP_S_CLOSE] = "CLOSE",
371 [IP_VS_TCP_S_CLOSE_WAIT] = "CLOSE_WAIT",
372 [IP_VS_TCP_S_LAST_ACK] = "LAST_ACK",
373 [IP_VS_TCP_S_LISTEN] = "LISTEN",
374 [IP_VS_TCP_S_SYNACK] = "SYNACK",
375 [IP_VS_TCP_S_LAST] = "BUG!",
376};
377
378#define sNO IP_VS_TCP_S_NONE
379#define sES IP_VS_TCP_S_ESTABLISHED
380#define sSS IP_VS_TCP_S_SYN_SENT
381#define sSR IP_VS_TCP_S_SYN_RECV
382#define sFW IP_VS_TCP_S_FIN_WAIT
383#define sTW IP_VS_TCP_S_TIME_WAIT
384#define sCL IP_VS_TCP_S_CLOSE
385#define sCW IP_VS_TCP_S_CLOSE_WAIT
386#define sLA IP_VS_TCP_S_LAST_ACK
387#define sLI IP_VS_TCP_S_LISTEN
388#define sSA IP_VS_TCP_S_SYNACK
389
390struct tcp_states_t {
391 int next_state[IP_VS_TCP_S_LAST];
392};
393
394static const char * tcp_state_name(int state)
395{
396 if (state >= IP_VS_TCP_S_LAST)
397 return "ERR!";
398 return tcp_state_name_table[state] ? tcp_state_name_table[state] : "?";
399}
400
401static struct tcp_states_t tcp_states [] = {
402/* INPUT */
403/* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
404/*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }},
405/*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sTW }},
Alexander Frolkinc6c96c12013-06-13 08:56:15 +0100406/*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sSR }},
408
409/* OUTPUT */
410/* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
411/*syn*/ {{sSS, sES, sSS, sSR, sSS, sSS, sSS, sSS, sSS, sLI, sSR }},
412/*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }},
413/*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }},
414/*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }},
415
416/* INPUT-ONLY */
417/* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
418/*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }},
419/*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }},
Alexander Frolkinc6c96c12013-06-13 08:56:15 +0100420/*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
422};
423
424static struct tcp_states_t tcp_states_dos [] = {
425/* INPUT */
426/* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
427/*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSA }},
428/*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sSA }},
Alexander Frolkinc6c96c12013-06-13 08:56:15 +0100429/*ack*/ {{sES, sES, sSS, sSR, sFW, sTW, sCL, sCW, sCL, sLI, sSA }},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430/*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
431
432/* OUTPUT */
433/* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
434/*syn*/ {{sSS, sES, sSS, sSA, sSS, sSS, sSS, sSS, sSS, sLI, sSA }},
435/*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }},
436/*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }},
437/*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }},
438
439/* INPUT-ONLY */
440/* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */
441/*syn*/ {{sSA, sES, sES, sSR, sSA, sSA, sSA, sSA, sSA, sSA, sSA }},
442/*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }},
Alexander Frolkinc6c96c12013-06-13 08:56:15 +0100443/*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }},
445};
446
Hans Schillstrom93304192011-01-03 14:44:51 +0100447static void tcp_timeout_change(struct ip_vs_proto_data *pd, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 int on = (flags & 1); /* secure_tcp */
450
451 /*
452 ** FIXME: change secure_tcp to independent sysctl var
453 ** or make it per-service or per-app because it is valid
454 ** for most if not for all of the applications. Something
455 ** like "capabilities" (flags) for each object.
456 */
Hans Schillstrom93304192011-01-03 14:44:51 +0100457 pd->tcp_state_table = (on ? tcp_states_dos : tcp_states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460static inline int tcp_state_idx(struct tcphdr *th)
461{
462 if (th->rst)
463 return 3;
464 if (th->syn)
465 return 0;
466 if (th->fin)
467 return 1;
468 if (th->ack)
469 return 2;
470 return -1;
471}
472
473static inline void
Hans Schillstrom93304192011-01-03 14:44:51 +0100474set_tcp_state(struct ip_vs_proto_data *pd, struct ip_vs_conn *cp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 int direction, struct tcphdr *th)
476{
477 int state_idx;
478 int new_state = IP_VS_TCP_S_CLOSE;
479 int state_off = tcp_state_off[direction];
480
481 /*
482 * Update state offset to INPUT_ONLY if necessary
483 * or delete NO_OUTPUT flag if output packet detected
484 */
485 if (cp->flags & IP_VS_CONN_F_NOOUTPUT) {
486 if (state_off == TCP_DIR_OUTPUT)
487 cp->flags &= ~IP_VS_CONN_F_NOOUTPUT;
488 else
489 state_off = TCP_DIR_INPUT_ONLY;
490 }
491
492 if ((state_idx = tcp_state_idx(th)) < 0) {
493 IP_VS_DBG(8, "tcp_state_idx=%d!!!\n", state_idx);
494 goto tcp_state_out;
495 }
496
Hans Schillstrom93304192011-01-03 14:44:51 +0100497 new_state =
498 pd->tcp_state_table[state_off+state_idx].next_state[cp->state];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 tcp_state_out:
501 if (new_state != cp->state) {
502 struct ip_vs_dest *dest = cp->dest;
503
Julius Volzcfc78c52008-09-02 15:55:53 +0200504 IP_VS_DBG_BUF(8, "%s %s [%c%c%c%c] %s:%d->"
505 "%s:%d state: %s->%s conn->refcnt:%d\n",
Hans Schillstrom93304192011-01-03 14:44:51 +0100506 pd->pp->name,
Julius Volzcfc78c52008-09-02 15:55:53 +0200507 ((state_off == TCP_DIR_OUTPUT) ?
508 "output " : "input "),
509 th->syn ? 'S' : '.',
510 th->fin ? 'F' : '.',
511 th->ack ? 'A' : '.',
512 th->rst ? 'R' : '.',
513 IP_VS_DBG_ADDR(cp->af, &cp->daddr),
514 ntohs(cp->dport),
515 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
516 ntohs(cp->cport),
517 tcp_state_name(cp->state),
518 tcp_state_name(new_state),
519 atomic_read(&cp->refcnt));
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (dest) {
522 if (!(cp->flags & IP_VS_CONN_F_INACTIVE) &&
523 (new_state != IP_VS_TCP_S_ESTABLISHED)) {
524 atomic_dec(&dest->activeconns);
525 atomic_inc(&dest->inactconns);
526 cp->flags |= IP_VS_CONN_F_INACTIVE;
527 } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) &&
528 (new_state == IP_VS_TCP_S_ESTABLISHED)) {
529 atomic_inc(&dest->activeconns);
530 atomic_dec(&dest->inactconns);
531 cp->flags &= ~IP_VS_CONN_F_INACTIVE;
532 }
533 }
534 }
535
Hans Schillstrom4a85b962011-01-03 14:44:47 +0100536 if (likely(pd))
537 cp->timeout = pd->timeout_table[cp->state = new_state];
538 else /* What to do ? */
539 cp->timeout = tcp_timeouts[cp->state = new_state];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542/*
543 * Handle state transitions
544 */
Simon Horman4a516f12011-09-16 14:11:49 +0900545static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546tcp_state_transition(struct ip_vs_conn *cp, int direction,
547 const struct sk_buff *skb,
Hans Schillstrom93304192011-01-03 14:44:51 +0100548 struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct tcphdr _tcph, *th;
551
Julius Volz0bbdd422008-09-02 15:55:42 +0200552#ifdef CONFIG_IP_VS_IPV6
553 int ihl = cp->af == AF_INET ? ip_hdrlen(skb) : sizeof(struct ipv6hdr);
554#else
555 int ihl = ip_hdrlen(skb);
556#endif
557
558 th = skb_header_pointer(skb, ihl, sizeof(_tcph), &_tcph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (th == NULL)
Simon Horman4a516f12011-09-16 14:11:49 +0900560 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Julian Anastasovac692692013-03-22 11:46:54 +0200562 spin_lock_bh(&cp->lock);
Hans Schillstrom93304192011-01-03 14:44:51 +0100563 set_tcp_state(pd, cp, direction, th);
Julian Anastasovac692692013-03-22 11:46:54 +0200564 spin_unlock_bh(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Al Viro75e7ce62006-11-14 21:13:28 -0800567static inline __u16 tcp_app_hashkey(__be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Al Viro75e7ce62006-11-14 21:13:28 -0800569 return (((__force u16)port >> TCP_APP_TAB_BITS) ^ (__force u16)port)
570 & TCP_APP_TAB_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100574static int tcp_register_app(struct net *net, struct ip_vs_app *inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 struct ip_vs_app *i;
Al Viro75e7ce62006-11-14 21:13:28 -0800577 __u16 hash;
578 __be16 port = inc->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 int ret = 0;
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100580 struct netns_ipvs *ipvs = net_ipvs(net);
581 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 hash = tcp_app_hashkey(port);
584
Hans Schillstrom4a85b962011-01-03 14:44:47 +0100585 list_for_each_entry(i, &ipvs->tcp_apps[hash], p_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (i->port == port) {
587 ret = -EEXIST;
588 goto out;
589 }
590 }
Julian Anastasov363c97d2013-03-21 11:58:07 +0200591 list_add_rcu(&inc->p_list, &ipvs->tcp_apps[hash]);
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100592 atomic_inc(&pd->appcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return ret;
596}
597
598
599static void
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100600tcp_unregister_app(struct net *net, struct ip_vs_app *inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Hans Schillstromab8a5e82011-01-03 14:44:53 +0100602 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
Hans Schillstrom4a85b962011-01-03 14:44:47 +0100603
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100604 atomic_dec(&pd->appcnt);
Julian Anastasov363c97d2013-03-21 11:58:07 +0200605 list_del_rcu(&inc->p_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608
609static int
610tcp_app_conn_bind(struct ip_vs_conn *cp)
611{
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100612 struct netns_ipvs *ipvs = net_ipvs(ip_vs_conn_net(cp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int hash;
614 struct ip_vs_app *inc;
615 int result = 0;
616
617 /* Default binding: bind app only for NAT */
618 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
619 return 0;
620
621 /* Lookup application incarnations and bind the right one */
622 hash = tcp_app_hashkey(cp->vport);
623
Julian Anastasov363c97d2013-03-21 11:58:07 +0200624 rcu_read_lock();
625 list_for_each_entry_rcu(inc, &ipvs->tcp_apps[hash], p_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (inc->port == cp->vport) {
627 if (unlikely(!ip_vs_app_inc_get(inc)))
628 break;
Julian Anastasov363c97d2013-03-21 11:58:07 +0200629 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Hannes Eder1e3e2382009-08-02 11:05:41 +0000631 IP_VS_DBG_BUF(9, "%s(): Binding conn %s:%u->"
Julius Volzcfc78c52008-09-02 15:55:53 +0200632 "%s:%u to app %s on port %u\n",
633 __func__,
634 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
635 ntohs(cp->cport),
636 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
637 ntohs(cp->vport),
638 inc->name, ntohs(inc->port));
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 cp->app = inc;
641 if (inc->init_conn)
642 result = inc->init_conn(inc, cp);
643 goto out;
644 }
645 }
Julian Anastasov363c97d2013-03-21 11:58:07 +0200646 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 out:
649 return result;
650}
651
652
653/*
654 * Set LISTEN timeout. (ip_vs_conn_put will setup timer)
655 */
Hans Schillstrom4a85b962011-01-03 14:44:47 +0100656void ip_vs_tcp_conn_listen(struct net *net, struct ip_vs_conn *cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Hans Schillstrom4a85b962011-01-03 14:44:47 +0100658 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
659
Julian Anastasovac692692013-03-22 11:46:54 +0200660 spin_lock_bh(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 cp->state = IP_VS_TCP_S_LISTEN;
Hans Schillstrom4a85b962011-01-03 14:44:47 +0100662 cp->timeout = (pd ? pd->timeout_table[IP_VS_TCP_S_LISTEN]
663 : tcp_timeouts[IP_VS_TCP_S_LISTEN]);
Julian Anastasovac692692013-03-22 11:46:54 +0200664 spin_unlock_bh(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Hans Schillstrom4a85b962011-01-03 14:44:47 +0100667/* ---------------------------------------------
668 * timeouts is netns related now.
669 * ---------------------------------------------
670 */
Hans Schillstrom582b8e32012-04-26 09:45:35 +0200671static int __ip_vs_tcp_init(struct net *net, struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Hans Schillstrom4a85b962011-01-03 14:44:47 +0100673 struct netns_ipvs *ipvs = net_ipvs(net);
674
675 ip_vs_init_hash_table(ipvs->tcp_apps, TCP_APP_TAB_SIZE);
Hans Schillstrom4a85b962011-01-03 14:44:47 +0100676 pd->timeout_table = ip_vs_create_timeout_table((int *)tcp_timeouts,
677 sizeof(tcp_timeouts));
Hans Schillstrom582b8e32012-04-26 09:45:35 +0200678 if (!pd->timeout_table)
679 return -ENOMEM;
Hans Schillstrom93304192011-01-03 14:44:51 +0100680 pd->tcp_state_table = tcp_states;
Hans Schillstrom582b8e32012-04-26 09:45:35 +0200681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Hans Schillstrom4a85b962011-01-03 14:44:47 +0100684static void __ip_vs_tcp_exit(struct net *net, struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Hans Schillstrom4a85b962011-01-03 14:44:47 +0100686 kfree(pd->timeout_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689
690struct ip_vs_protocol ip_vs_protocol_tcp = {
691 .name = "TCP",
692 .protocol = IPPROTO_TCP,
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700693 .num_states = IP_VS_TCP_S_LAST,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 .dont_defrag = 0,
Hans Schillstrom4a85b962011-01-03 14:44:47 +0100695 .init = NULL,
696 .exit = NULL,
697 .init_netns = __ip_vs_tcp_init,
698 .exit_netns = __ip_vs_tcp_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 .register_app = tcp_register_app,
700 .unregister_app = tcp_unregister_app,
701 .conn_schedule = tcp_conn_schedule,
Simon Horman5c0d2372010-08-02 17:12:44 +0200702 .conn_in_get = ip_vs_conn_in_get_proto,
703 .conn_out_get = ip_vs_conn_out_get_proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 .snat_handler = tcp_snat_handler,
705 .dnat_handler = tcp_dnat_handler,
706 .csum_check = tcp_csum_check,
707 .state_name = tcp_state_name,
708 .state_transition = tcp_state_transition,
709 .app_conn_bind = tcp_app_conn_bind,
710 .debug_packet = ip_vs_tcpudp_debug_packet,
711 .timeout_change = tcp_timeout_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712};