Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * ip_vs_proto_tcp.c: TCP load balancing support for IPVS | |
3 | * | |
1da177e4 LT |
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 | * | |
4a85b96c | 12 | * Changes: Hans Schillstrom <hans.schillstrom@ericsson.com> |
1da177e4 | 13 | * |
4a85b96c HS |
14 | * 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 | |
1da177e4 LT |
18 | */ |
19 | ||
9aada7ac HE |
20 | #define KMSG_COMPONENT "IPVS" |
21 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
22 | ||
1da177e4 LT |
23 | #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 */ | |
63f2c046 | 28 | #include <net/ip6_checksum.h> |
af1e1cf0 | 29 | #include <linux/netfilter.h> |
1da177e4 LT |
30 | #include <linux/netfilter_ipv4.h> |
31 | ||
32 | #include <net/ip_vs.h> | |
33 | ||
1da177e4 | 34 | static int |
9330419d | 35 | tcp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd, |
d4383f04 JDB |
36 | int *verdict, struct ip_vs_conn **cpp, |
37 | struct ip_vs_iphdr *iph) | |
1da177e4 | 38 | { |
fc723250 | 39 | struct net *net; |
1da177e4 LT |
40 | struct ip_vs_service *svc; |
41 | struct tcphdr _tcph, *th; | |
c6c96c18 | 42 | struct netns_ipvs *ipvs; |
1da177e4 | 43 | |
d4383f04 | 44 | th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph); |
1da177e4 LT |
45 | if (th == NULL) { |
46 | *verdict = NF_DROP; | |
47 | return 0; | |
48 | } | |
fc723250 | 49 | net = skb_net(skb); |
c6c96c18 | 50 | ipvs = net_ipvs(net); |
190ecd27 | 51 | /* No !th->ack check to allow scheduling on SYN+ACK for Active FTP */ |
ceec4c38 | 52 | rcu_read_lock(); |
c6c96c18 | 53 | if ((th->syn || sysctl_sloppy_tcp(ipvs)) && !th->rst && |
ceec4c38 JA |
54 | (svc = ip_vs_service_find(net, af, skb->mark, iph->protocol, |
55 | &iph->daddr, th->dest))) { | |
190ecd27 JA |
56 | int ignored; |
57 | ||
c6c96c18 | 58 | if (ip_vs_todrop(ipvs)) { |
1da177e4 LT |
59 | /* |
60 | * It seems that we are very loaded. | |
61 | * We have to drop this packet :( | |
62 | */ | |
ceec4c38 | 63 | rcu_read_unlock(); |
1da177e4 LT |
64 | *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 | */ | |
d4383f04 | 72 | *cpp = ip_vs_schedule(svc, skb, pd, &ignored, iph); |
a5959d53 HS |
73 | if (!*cpp && ignored <= 0) { |
74 | if (!ignored) | |
d4383f04 | 75 | *verdict = ip_vs_leave(svc, skb, pd, iph); |
ceec4c38 | 76 | else |
a5959d53 | 77 | *verdict = NF_DROP; |
ceec4c38 | 78 | rcu_read_unlock(); |
1da177e4 LT |
79 | return 0; |
80 | } | |
1da177e4 | 81 | } |
ceec4c38 | 82 | rcu_read_unlock(); |
a5959d53 | 83 | /* NF_ACCEPT */ |
1da177e4 LT |
84 | return 1; |
85 | } | |
86 | ||
87 | ||
88 | static inline void | |
0bbdd42b JV |
89 | tcp_fast_csum_update(int af, struct tcphdr *tcph, |
90 | const union nf_inet_addr *oldip, | |
91 | const union nf_inet_addr *newip, | |
014d730d | 92 | __be16 oldport, __be16 newport) |
1da177e4 | 93 | { |
0bbdd42b JV |
94 | #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 | |
1da177e4 | 102 | tcph->check = |
0bbdd42b | 103 | csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip, |
f9214b26 AV |
104 | ip_vs_check_diff2(oldport, newport, |
105 | ~csum_unfold(tcph->check)))); | |
1da177e4 LT |
106 | } |
107 | ||
108 | ||
503e81f6 SH |
109 | static inline void |
110 | tcp_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 = | |
5bc9068e | 118 | ~csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6, |
503e81f6 | 119 | ip_vs_check_diff2(oldlen, newlen, |
5bc9068e | 120 | csum_unfold(tcph->check)))); |
503e81f6 SH |
121 | else |
122 | #endif | |
123 | tcph->check = | |
5bc9068e | 124 | ~csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip, |
503e81f6 | 125 | ip_vs_check_diff2(oldlen, newlen, |
5bc9068e | 126 | csum_unfold(tcph->check)))); |
503e81f6 SH |
127 | } |
128 | ||
129 | ||
1da177e4 | 130 | static int |
d4383f04 JDB |
131 | tcp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp, |
132 | struct ip_vs_conn *cp, struct ip_vs_iphdr *iph) | |
1da177e4 LT |
133 | { |
134 | struct tcphdr *tcph; | |
d4383f04 | 135 | unsigned int tcphoff = iph->len; |
503e81f6 | 136 | int oldlen; |
8b27b10f | 137 | int payload_csum = 0; |
0bbdd42b JV |
138 | |
139 | #ifdef CONFIG_IP_VS_IPV6 | |
d4383f04 | 140 | if (cp->af == AF_INET6 && iph->fragoffs) |
63dca2c0 | 141 | return 1; |
0bbdd42b | 142 | #endif |
503e81f6 | 143 | oldlen = skb->len - tcphoff; |
1da177e4 LT |
144 | |
145 | /* csum_check requires unshared skb */ | |
3db05fea | 146 | if (!skb_make_writable(skb, tcphoff+sizeof(*tcph))) |
1da177e4 LT |
147 | return 0; |
148 | ||
149 | if (unlikely(cp->app != NULL)) { | |
8b27b10f JA |
150 | int ret; |
151 | ||
1da177e4 | 152 | /* Some checks before mangling */ |
0bbdd42b | 153 | if (pp->csum_check && !pp->csum_check(cp->af, skb, pp)) |
1da177e4 LT |
154 | return 0; |
155 | ||
156 | /* Call application helper if needed */ | |
8b27b10f | 157 | if (!(ret = ip_vs_app_pkt_out(cp, skb))) |
1da177e4 | 158 | return 0; |
8b27b10f JA |
159 | /* ret=2: csum update is needed after payload mangling */ |
160 | if (ret == 1) | |
161 | oldlen = skb->len - tcphoff; | |
162 | else | |
163 | payload_csum = 1; | |
1da177e4 LT |
164 | } |
165 | ||
0bbdd42b | 166 | tcph = (void *)skb_network_header(skb) + tcphoff; |
1da177e4 LT |
167 | tcph->source = cp->vport; |
168 | ||
169 | /* Adjust TCP checksums */ | |
503e81f6 SH |
170 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
171 | tcp_partial_csum_update(cp->af, tcph, &cp->daddr, &cp->vaddr, | |
ca62059b HH |
172 | htons(oldlen), |
173 | htons(skb->len - tcphoff)); | |
8b27b10f | 174 | } else if (!payload_csum) { |
1da177e4 | 175 | /* Only port and addr are changed, do fast csum update */ |
0bbdd42b | 176 | tcp_fast_csum_update(cp->af, tcph, &cp->daddr, &cp->vaddr, |
1da177e4 | 177 | cp->dport, cp->vport); |
3db05fea | 178 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
8b27b10f JA |
179 | skb->ip_summed = (cp->app && pp->csum_check) ? |
180 | CHECKSUM_UNNECESSARY : CHECKSUM_NONE; | |
1da177e4 LT |
181 | } else { |
182 | /* full checksum calculation */ | |
183 | tcph->check = 0; | |
3db05fea | 184 | skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0); |
0bbdd42b JV |
185 | #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); | |
8b27b10f | 198 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
0bbdd42b | 199 | |
1da177e4 LT |
200 | 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 | ||
208 | static int | |
d4383f04 JDB |
209 | tcp_dnat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp, |
210 | struct ip_vs_conn *cp, struct ip_vs_iphdr *iph) | |
1da177e4 LT |
211 | { |
212 | struct tcphdr *tcph; | |
d4383f04 | 213 | unsigned int tcphoff = iph->len; |
503e81f6 | 214 | int oldlen; |
8b27b10f | 215 | int payload_csum = 0; |
0bbdd42b JV |
216 | |
217 | #ifdef CONFIG_IP_VS_IPV6 | |
d4383f04 | 218 | if (cp->af == AF_INET6 && iph->fragoffs) |
63dca2c0 | 219 | return 1; |
0bbdd42b | 220 | #endif |
503e81f6 | 221 | oldlen = skb->len - tcphoff; |
1da177e4 LT |
222 | |
223 | /* csum_check requires unshared skb */ | |
3db05fea | 224 | if (!skb_make_writable(skb, tcphoff+sizeof(*tcph))) |
1da177e4 LT |
225 | return 0; |
226 | ||
227 | if (unlikely(cp->app != NULL)) { | |
8b27b10f JA |
228 | int ret; |
229 | ||
1da177e4 | 230 | /* Some checks before mangling */ |
0bbdd42b | 231 | if (pp->csum_check && !pp->csum_check(cp->af, skb, pp)) |
1da177e4 LT |
232 | return 0; |
233 | ||
234 | /* | |
235 | * Attempt ip_vs_app call. | |
236 | * It will fix ip_vs_conn and iph ack_seq stuff | |
237 | */ | |
8b27b10f | 238 | if (!(ret = ip_vs_app_pkt_in(cp, skb))) |
1da177e4 | 239 | return 0; |
8b27b10f JA |
240 | /* ret=2: csum update is needed after payload mangling */ |
241 | if (ret == 1) | |
242 | oldlen = skb->len - tcphoff; | |
243 | else | |
244 | payload_csum = 1; | |
1da177e4 LT |
245 | } |
246 | ||
0bbdd42b | 247 | tcph = (void *)skb_network_header(skb) + tcphoff; |
1da177e4 LT |
248 | tcph->dest = cp->dport; |
249 | ||
250 | /* | |
251 | * Adjust TCP checksums | |
252 | */ | |
503e81f6 | 253 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
5bc9068e | 254 | tcp_partial_csum_update(cp->af, tcph, &cp->vaddr, &cp->daddr, |
ca62059b HH |
255 | htons(oldlen), |
256 | htons(skb->len - tcphoff)); | |
8b27b10f | 257 | } else if (!payload_csum) { |
1da177e4 | 258 | /* Only port and addr are changed, do fast csum update */ |
0bbdd42b | 259 | tcp_fast_csum_update(cp->af, tcph, &cp->vaddr, &cp->daddr, |
1da177e4 | 260 | cp->vport, cp->dport); |
3db05fea | 261 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
8b27b10f JA |
262 | skb->ip_summed = (cp->app && pp->csum_check) ? |
263 | CHECKSUM_UNNECESSARY : CHECKSUM_NONE; | |
1da177e4 LT |
264 | } else { |
265 | /* full checksum calculation */ | |
266 | tcph->check = 0; | |
3db05fea | 267 | skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0); |
0bbdd42b JV |
268 | #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); | |
3db05fea | 281 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
1da177e4 LT |
282 | } |
283 | return 1; | |
284 | } | |
285 | ||
286 | ||
287 | static int | |
51ef348b | 288 | tcp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp) |
1da177e4 | 289 | { |
51ef348b JV |
290 | 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); | |
1da177e4 LT |
298 | |
299 | switch (skb->ip_summed) { | |
300 | case CHECKSUM_NONE: | |
301 | skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0); | |
84fa7933 | 302 | case CHECKSUM_COMPLETE: |
51ef348b JV |
303 | #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)) { | |
0d79641a | 310 | IP_VS_DBG_RL_PKT(0, af, pp, skb, 0, |
51ef348b JV |
311 | "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)) { | |
0d79641a | 321 | IP_VS_DBG_RL_PKT(0, af, pp, skb, 0, |
51ef348b JV |
322 | "Failed checksum for"); |
323 | return 0; | |
324 | } | |
1da177e4 LT |
325 | break; |
326 | default: | |
84fa7933 | 327 | /* No need to checksum. */ |
1da177e4 LT |
328 | 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 | ||
9b5b5cff | 339 | static const int tcp_state_off[IP_VS_DIR_LAST] = { |
1da177e4 LT |
340 | [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 | */ | |
4a85b96c | 348 | static const int tcp_timeouts[IP_VS_TCP_S_LAST+1] = { |
1da177e4 LT |
349 | [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 | ||
36cbd3dc | 363 | static const char *const tcp_state_name_table[IP_VS_TCP_S_LAST+1] = { |
1da177e4 LT |
364 | [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 | ||
390 | struct tcp_states_t { | |
391 | int next_state[IP_VS_TCP_S_LAST]; | |
392 | }; | |
393 | ||
394 | static 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 | ||
401 | static 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 }}, | |
c6c96c18 | 406 | /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }}, |
1da177e4 LT |
407 | /*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 }}, | |
c6c96c18 | 420 | /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }}, |
1da177e4 LT |
421 | /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }}, |
422 | }; | |
423 | ||
424 | static 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 }}, | |
c6c96c18 | 429 | /*ack*/ {{sES, sES, sSS, sSR, sFW, sTW, sCL, sCW, sCL, sLI, sSA }}, |
1da177e4 LT |
430 | /*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 }}, | |
c6c96c18 | 443 | /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }}, |
1da177e4 LT |
444 | /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }}, |
445 | }; | |
446 | ||
9330419d | 447 | static void tcp_timeout_change(struct ip_vs_proto_data *pd, int flags) |
1da177e4 LT |
448 | { |
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 | */ | |
9330419d | 457 | pd->tcp_state_table = (on ? tcp_states_dos : tcp_states); |
1da177e4 LT |
458 | } |
459 | ||
1da177e4 LT |
460 | static 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 | ||
473 | static inline void | |
9330419d | 474 | set_tcp_state(struct ip_vs_proto_data *pd, struct ip_vs_conn *cp, |
1da177e4 LT |
475 | 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 | ||
9330419d HS |
497 | new_state = |
498 | pd->tcp_state_table[state_off+state_idx].next_state[cp->state]; | |
1da177e4 LT |
499 | |
500 | tcp_state_out: | |
501 | if (new_state != cp->state) { | |
502 | struct ip_vs_dest *dest = cp->dest; | |
503 | ||
cfc78c5a JV |
504 | IP_VS_DBG_BUF(8, "%s %s [%c%c%c%c] %s:%d->" |
505 | "%s:%d state: %s->%s conn->refcnt:%d\n", | |
9330419d | 506 | pd->pp->name, |
cfc78c5a JV |
507 | ((state_off == TCP_DIR_OUTPUT) ? |
508 | "output " : "input "), | |
509 | th->syn ? 'S' : '.', | |
510 | th->fin ? 'F' : '.', | |
511 | th->ack ? 'A' : '.', | |
512 | th->rst ? 'R' : '.', | |
f18ae720 | 513 | IP_VS_DBG_ADDR(cp->daf, &cp->daddr), |
cfc78c5a JV |
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 | ||
1da177e4 LT |
521 | 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 | ||
4a85b96c HS |
536 | 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]; | |
1da177e4 LT |
540 | } |
541 | ||
1da177e4 LT |
542 | /* |
543 | * Handle state transitions | |
544 | */ | |
4a516f11 | 545 | static void |
1da177e4 LT |
546 | tcp_state_transition(struct ip_vs_conn *cp, int direction, |
547 | const struct sk_buff *skb, | |
9330419d | 548 | struct ip_vs_proto_data *pd) |
1da177e4 LT |
549 | { |
550 | struct tcphdr _tcph, *th; | |
551 | ||
0bbdd42b JV |
552 | #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); | |
1da177e4 | 559 | if (th == NULL) |
4a516f11 | 560 | return; |
1da177e4 | 561 | |
ac69269a | 562 | spin_lock_bh(&cp->lock); |
9330419d | 563 | set_tcp_state(pd, cp, direction, th); |
ac69269a | 564 | spin_unlock_bh(&cp->lock); |
1da177e4 LT |
565 | } |
566 | ||
75e7ce66 | 567 | static inline __u16 tcp_app_hashkey(__be16 port) |
1da177e4 | 568 | { |
75e7ce66 AV |
569 | return (((__force u16)port >> TCP_APP_TAB_BITS) ^ (__force u16)port) |
570 | & TCP_APP_TAB_MASK; | |
1da177e4 LT |
571 | } |
572 | ||
573 | ||
ab8a5e84 | 574 | static int tcp_register_app(struct net *net, struct ip_vs_app *inc) |
1da177e4 LT |
575 | { |
576 | struct ip_vs_app *i; | |
75e7ce66 AV |
577 | __u16 hash; |
578 | __be16 port = inc->port; | |
1da177e4 | 579 | int ret = 0; |
ab8a5e84 HS |
580 | struct netns_ipvs *ipvs = net_ipvs(net); |
581 | struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net, IPPROTO_TCP); | |
1da177e4 LT |
582 | |
583 | hash = tcp_app_hashkey(port); | |
584 | ||
4a85b96c | 585 | list_for_each_entry(i, &ipvs->tcp_apps[hash], p_list) { |
1da177e4 LT |
586 | if (i->port == port) { |
587 | ret = -EEXIST; | |
588 | goto out; | |
589 | } | |
590 | } | |
363c97d7 | 591 | list_add_rcu(&inc->p_list, &ipvs->tcp_apps[hash]); |
9bbac6a9 | 592 | atomic_inc(&pd->appcnt); |
1da177e4 LT |
593 | |
594 | out: | |
1da177e4 LT |
595 | return ret; |
596 | } | |
597 | ||
598 | ||
599 | static void | |
ab8a5e84 | 600 | tcp_unregister_app(struct net *net, struct ip_vs_app *inc) |
1da177e4 | 601 | { |
ab8a5e84 | 602 | struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net, IPPROTO_TCP); |
4a85b96c | 603 | |
9bbac6a9 | 604 | atomic_dec(&pd->appcnt); |
363c97d7 | 605 | list_del_rcu(&inc->p_list); |
1da177e4 LT |
606 | } |
607 | ||
608 | ||
609 | static int | |
610 | tcp_app_conn_bind(struct ip_vs_conn *cp) | |
611 | { | |
6e67e586 | 612 | struct netns_ipvs *ipvs = net_ipvs(ip_vs_conn_net(cp)); |
1da177e4 LT |
613 | 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 | ||
363c97d7 JA |
624 | rcu_read_lock(); |
625 | list_for_each_entry_rcu(inc, &ipvs->tcp_apps[hash], p_list) { | |
1da177e4 LT |
626 | if (inc->port == cp->vport) { |
627 | if (unlikely(!ip_vs_app_inc_get(inc))) | |
628 | break; | |
363c97d7 | 629 | rcu_read_unlock(); |
1da177e4 | 630 | |
1e3e238e | 631 | IP_VS_DBG_BUF(9, "%s(): Binding conn %s:%u->" |
cfc78c5a JV |
632 | "%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 | ||
1da177e4 LT |
640 | cp->app = inc; |
641 | if (inc->init_conn) | |
642 | result = inc->init_conn(inc, cp); | |
643 | goto out; | |
644 | } | |
645 | } | |
363c97d7 | 646 | rcu_read_unlock(); |
1da177e4 LT |
647 | |
648 | out: | |
649 | return result; | |
650 | } | |
651 | ||
652 | ||
653 | /* | |
654 | * Set LISTEN timeout. (ip_vs_conn_put will setup timer) | |
655 | */ | |
4a85b96c | 656 | void ip_vs_tcp_conn_listen(struct net *net, struct ip_vs_conn *cp) |
1da177e4 | 657 | { |
4a85b96c HS |
658 | struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net, IPPROTO_TCP); |
659 | ||
ac69269a | 660 | spin_lock_bh(&cp->lock); |
1da177e4 | 661 | cp->state = IP_VS_TCP_S_LISTEN; |
4a85b96c HS |
662 | cp->timeout = (pd ? pd->timeout_table[IP_VS_TCP_S_LISTEN] |
663 | : tcp_timeouts[IP_VS_TCP_S_LISTEN]); | |
ac69269a | 664 | spin_unlock_bh(&cp->lock); |
1da177e4 LT |
665 | } |
666 | ||
4a85b96c HS |
667 | /* --------------------------------------------- |
668 | * timeouts is netns related now. | |
669 | * --------------------------------------------- | |
670 | */ | |
582b8e3e | 671 | static int __ip_vs_tcp_init(struct net *net, struct ip_vs_proto_data *pd) |
1da177e4 | 672 | { |
4a85b96c | 673 | struct netns_ipvs *ipvs = net_ipvs(net); |
1da177e4 | 674 | |
4a85b96c | 675 | ip_vs_init_hash_table(ipvs->tcp_apps, TCP_APP_TAB_SIZE); |
4a85b96c HS |
676 | pd->timeout_table = ip_vs_create_timeout_table((int *)tcp_timeouts, |
677 | sizeof(tcp_timeouts)); | |
582b8e3e HS |
678 | if (!pd->timeout_table) |
679 | return -ENOMEM; | |
9330419d | 680 | pd->tcp_state_table = tcp_states; |
582b8e3e | 681 | return 0; |
4a85b96c | 682 | } |
1da177e4 | 683 | |
4a85b96c | 684 | static void __ip_vs_tcp_exit(struct net *net, struct ip_vs_proto_data *pd) |
1da177e4 | 685 | { |
4a85b96c | 686 | kfree(pd->timeout_table); |
1da177e4 LT |
687 | } |
688 | ||
689 | ||
690 | struct ip_vs_protocol ip_vs_protocol_tcp = { | |
691 | .name = "TCP", | |
692 | .protocol = IPPROTO_TCP, | |
2ad17def | 693 | .num_states = IP_VS_TCP_S_LAST, |
1da177e4 | 694 | .dont_defrag = 0, |
4a85b96c HS |
695 | .init = NULL, |
696 | .exit = NULL, | |
697 | .init_netns = __ip_vs_tcp_init, | |
698 | .exit_netns = __ip_vs_tcp_exit, | |
1da177e4 LT |
699 | .register_app = tcp_register_app, |
700 | .unregister_app = tcp_unregister_app, | |
701 | .conn_schedule = tcp_conn_schedule, | |
5c0d2374 SH |
702 | .conn_in_get = ip_vs_conn_in_get_proto, |
703 | .conn_out_get = ip_vs_conn_out_get_proto, | |
1da177e4 LT |
704 | .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, | |
1da177e4 | 712 | }; |