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; |
8f88ea68 | 43 | __be16 _ports[2], *ports = NULL; |
1da177e4 | 44 | |
8f88ea68 AG |
45 | net = skb_net(skb); |
46 | ipvs = net_ipvs(net); | |
47 | ||
48 | /* In the event of icmp, we're only guaranteed to have the first 8 | |
49 | * bytes of the transport header, so we only check the rest of the | |
50 | * TCP packet for non-ICMP packets | |
51 | */ | |
52 | if (likely(!ip_vs_iph_icmp(iph))) { | |
53 | th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph); | |
54 | if (th) { | |
55 | if (th->rst || !(sysctl_sloppy_tcp(ipvs) || th->syn)) | |
56 | return 1; | |
57 | ports = &th->source; | |
58 | } | |
59 | } else { | |
60 | ports = skb_header_pointer( | |
61 | skb, iph->len, sizeof(_ports), &_ports); | |
6044eeff AG |
62 | } |
63 | ||
8f88ea68 | 64 | if (!ports) { |
1da177e4 LT |
65 | *verdict = NF_DROP; |
66 | return 0; | |
67 | } | |
6044eeff | 68 | |
190ecd27 | 69 | /* No !th->ack check to allow scheduling on SYN+ACK for Active FTP */ |
ceec4c38 | 70 | rcu_read_lock(); |
8f88ea68 AG |
71 | |
72 | if (likely(!ip_vs_iph_inverse(iph))) | |
0a4fd6ce | 73 | svc = ip_vs_service_find(ipvs, af, skb->mark, iph->protocol, |
8f88ea68 AG |
74 | &iph->daddr, ports[1]); |
75 | else | |
0a4fd6ce | 76 | svc = ip_vs_service_find(ipvs, af, skb->mark, iph->protocol, |
8f88ea68 AG |
77 | &iph->saddr, ports[0]); |
78 | ||
79 | if (svc) { | |
190ecd27 JA |
80 | int ignored; |
81 | ||
c6c96c18 | 82 | if (ip_vs_todrop(ipvs)) { |
1da177e4 LT |
83 | /* |
84 | * It seems that we are very loaded. | |
85 | * We have to drop this packet :( | |
86 | */ | |
ceec4c38 | 87 | rcu_read_unlock(); |
1da177e4 LT |
88 | *verdict = NF_DROP; |
89 | return 0; | |
90 | } | |
91 | ||
92 | /* | |
93 | * Let the virtual server select a real server for the | |
94 | * incoming connection, and create a connection entry. | |
95 | */ | |
d4383f04 | 96 | *cpp = ip_vs_schedule(svc, skb, pd, &ignored, iph); |
a5959d53 HS |
97 | if (!*cpp && ignored <= 0) { |
98 | if (!ignored) | |
d4383f04 | 99 | *verdict = ip_vs_leave(svc, skb, pd, iph); |
ceec4c38 | 100 | else |
a5959d53 | 101 | *verdict = NF_DROP; |
ceec4c38 | 102 | rcu_read_unlock(); |
1da177e4 LT |
103 | return 0; |
104 | } | |
1da177e4 | 105 | } |
ceec4c38 | 106 | rcu_read_unlock(); |
a5959d53 | 107 | /* NF_ACCEPT */ |
1da177e4 LT |
108 | return 1; |
109 | } | |
110 | ||
111 | ||
112 | static inline void | |
0bbdd42b JV |
113 | tcp_fast_csum_update(int af, struct tcphdr *tcph, |
114 | const union nf_inet_addr *oldip, | |
115 | const union nf_inet_addr *newip, | |
014d730d | 116 | __be16 oldport, __be16 newport) |
1da177e4 | 117 | { |
0bbdd42b JV |
118 | #ifdef CONFIG_IP_VS_IPV6 |
119 | if (af == AF_INET6) | |
120 | tcph->check = | |
121 | csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6, | |
122 | ip_vs_check_diff2(oldport, newport, | |
123 | ~csum_unfold(tcph->check)))); | |
124 | else | |
125 | #endif | |
1da177e4 | 126 | tcph->check = |
0bbdd42b | 127 | csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip, |
f9214b26 AV |
128 | ip_vs_check_diff2(oldport, newport, |
129 | ~csum_unfold(tcph->check)))); | |
1da177e4 LT |
130 | } |
131 | ||
132 | ||
503e81f6 SH |
133 | static inline void |
134 | tcp_partial_csum_update(int af, struct tcphdr *tcph, | |
135 | const union nf_inet_addr *oldip, | |
136 | const union nf_inet_addr *newip, | |
137 | __be16 oldlen, __be16 newlen) | |
138 | { | |
139 | #ifdef CONFIG_IP_VS_IPV6 | |
140 | if (af == AF_INET6) | |
141 | tcph->check = | |
5bc9068e | 142 | ~csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6, |
503e81f6 | 143 | ip_vs_check_diff2(oldlen, newlen, |
5bc9068e | 144 | csum_unfold(tcph->check)))); |
503e81f6 SH |
145 | else |
146 | #endif | |
147 | tcph->check = | |
5bc9068e | 148 | ~csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip, |
503e81f6 | 149 | ip_vs_check_diff2(oldlen, newlen, |
5bc9068e | 150 | csum_unfold(tcph->check)))); |
503e81f6 SH |
151 | } |
152 | ||
153 | ||
1da177e4 | 154 | static int |
d4383f04 JDB |
155 | tcp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp, |
156 | struct ip_vs_conn *cp, struct ip_vs_iphdr *iph) | |
1da177e4 LT |
157 | { |
158 | struct tcphdr *tcph; | |
d4383f04 | 159 | unsigned int tcphoff = iph->len; |
503e81f6 | 160 | int oldlen; |
8b27b10f | 161 | int payload_csum = 0; |
0bbdd42b JV |
162 | |
163 | #ifdef CONFIG_IP_VS_IPV6 | |
d4383f04 | 164 | if (cp->af == AF_INET6 && iph->fragoffs) |
63dca2c0 | 165 | return 1; |
0bbdd42b | 166 | #endif |
503e81f6 | 167 | oldlen = skb->len - tcphoff; |
1da177e4 LT |
168 | |
169 | /* csum_check requires unshared skb */ | |
3db05fea | 170 | if (!skb_make_writable(skb, tcphoff+sizeof(*tcph))) |
1da177e4 LT |
171 | return 0; |
172 | ||
173 | if (unlikely(cp->app != NULL)) { | |
8b27b10f JA |
174 | int ret; |
175 | ||
1da177e4 | 176 | /* Some checks before mangling */ |
0bbdd42b | 177 | if (pp->csum_check && !pp->csum_check(cp->af, skb, pp)) |
1da177e4 LT |
178 | return 0; |
179 | ||
180 | /* Call application helper if needed */ | |
8b27b10f | 181 | if (!(ret = ip_vs_app_pkt_out(cp, skb))) |
1da177e4 | 182 | return 0; |
8b27b10f JA |
183 | /* ret=2: csum update is needed after payload mangling */ |
184 | if (ret == 1) | |
185 | oldlen = skb->len - tcphoff; | |
186 | else | |
187 | payload_csum = 1; | |
1da177e4 LT |
188 | } |
189 | ||
0bbdd42b | 190 | tcph = (void *)skb_network_header(skb) + tcphoff; |
1da177e4 LT |
191 | tcph->source = cp->vport; |
192 | ||
193 | /* Adjust TCP checksums */ | |
503e81f6 SH |
194 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
195 | tcp_partial_csum_update(cp->af, tcph, &cp->daddr, &cp->vaddr, | |
ca62059b HH |
196 | htons(oldlen), |
197 | htons(skb->len - tcphoff)); | |
8b27b10f | 198 | } else if (!payload_csum) { |
1da177e4 | 199 | /* Only port and addr are changed, do fast csum update */ |
0bbdd42b | 200 | tcp_fast_csum_update(cp->af, tcph, &cp->daddr, &cp->vaddr, |
1da177e4 | 201 | cp->dport, cp->vport); |
3db05fea | 202 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
8b27b10f JA |
203 | skb->ip_summed = (cp->app && pp->csum_check) ? |
204 | CHECKSUM_UNNECESSARY : CHECKSUM_NONE; | |
1da177e4 LT |
205 | } else { |
206 | /* full checksum calculation */ | |
207 | tcph->check = 0; | |
3db05fea | 208 | skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0); |
0bbdd42b JV |
209 | #ifdef CONFIG_IP_VS_IPV6 |
210 | if (cp->af == AF_INET6) | |
211 | tcph->check = csum_ipv6_magic(&cp->vaddr.in6, | |
212 | &cp->caddr.in6, | |
213 | skb->len - tcphoff, | |
214 | cp->protocol, skb->csum); | |
215 | else | |
216 | #endif | |
217 | tcph->check = csum_tcpudp_magic(cp->vaddr.ip, | |
218 | cp->caddr.ip, | |
219 | skb->len - tcphoff, | |
220 | cp->protocol, | |
221 | skb->csum); | |
8b27b10f | 222 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
0bbdd42b | 223 | |
1da177e4 LT |
224 | IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n", |
225 | pp->name, tcph->check, | |
226 | (char*)&(tcph->check) - (char*)tcph); | |
227 | } | |
228 | return 1; | |
229 | } | |
230 | ||
231 | ||
232 | static int | |
d4383f04 JDB |
233 | tcp_dnat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp, |
234 | struct ip_vs_conn *cp, struct ip_vs_iphdr *iph) | |
1da177e4 LT |
235 | { |
236 | struct tcphdr *tcph; | |
d4383f04 | 237 | unsigned int tcphoff = iph->len; |
503e81f6 | 238 | int oldlen; |
8b27b10f | 239 | int payload_csum = 0; |
0bbdd42b JV |
240 | |
241 | #ifdef CONFIG_IP_VS_IPV6 | |
d4383f04 | 242 | if (cp->af == AF_INET6 && iph->fragoffs) |
63dca2c0 | 243 | return 1; |
0bbdd42b | 244 | #endif |
503e81f6 | 245 | oldlen = skb->len - tcphoff; |
1da177e4 LT |
246 | |
247 | /* csum_check requires unshared skb */ | |
3db05fea | 248 | if (!skb_make_writable(skb, tcphoff+sizeof(*tcph))) |
1da177e4 LT |
249 | return 0; |
250 | ||
251 | if (unlikely(cp->app != NULL)) { | |
8b27b10f JA |
252 | int ret; |
253 | ||
1da177e4 | 254 | /* Some checks before mangling */ |
0bbdd42b | 255 | if (pp->csum_check && !pp->csum_check(cp->af, skb, pp)) |
1da177e4 LT |
256 | return 0; |
257 | ||
258 | /* | |
259 | * Attempt ip_vs_app call. | |
260 | * It will fix ip_vs_conn and iph ack_seq stuff | |
261 | */ | |
8b27b10f | 262 | if (!(ret = ip_vs_app_pkt_in(cp, skb))) |
1da177e4 | 263 | return 0; |
8b27b10f JA |
264 | /* ret=2: csum update is needed after payload mangling */ |
265 | if (ret == 1) | |
266 | oldlen = skb->len - tcphoff; | |
267 | else | |
268 | payload_csum = 1; | |
1da177e4 LT |
269 | } |
270 | ||
0bbdd42b | 271 | tcph = (void *)skb_network_header(skb) + tcphoff; |
1da177e4 LT |
272 | tcph->dest = cp->dport; |
273 | ||
274 | /* | |
275 | * Adjust TCP checksums | |
276 | */ | |
503e81f6 | 277 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
5bc9068e | 278 | tcp_partial_csum_update(cp->af, tcph, &cp->vaddr, &cp->daddr, |
ca62059b HH |
279 | htons(oldlen), |
280 | htons(skb->len - tcphoff)); | |
8b27b10f | 281 | } else if (!payload_csum) { |
1da177e4 | 282 | /* Only port and addr are changed, do fast csum update */ |
0bbdd42b | 283 | tcp_fast_csum_update(cp->af, tcph, &cp->vaddr, &cp->daddr, |
1da177e4 | 284 | cp->vport, cp->dport); |
3db05fea | 285 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
8b27b10f JA |
286 | skb->ip_summed = (cp->app && pp->csum_check) ? |
287 | CHECKSUM_UNNECESSARY : CHECKSUM_NONE; | |
1da177e4 LT |
288 | } else { |
289 | /* full checksum calculation */ | |
290 | tcph->check = 0; | |
3db05fea | 291 | skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0); |
0bbdd42b JV |
292 | #ifdef CONFIG_IP_VS_IPV6 |
293 | if (cp->af == AF_INET6) | |
294 | tcph->check = csum_ipv6_magic(&cp->caddr.in6, | |
295 | &cp->daddr.in6, | |
296 | skb->len - tcphoff, | |
297 | cp->protocol, skb->csum); | |
298 | else | |
299 | #endif | |
300 | tcph->check = csum_tcpudp_magic(cp->caddr.ip, | |
301 | cp->daddr.ip, | |
302 | skb->len - tcphoff, | |
303 | cp->protocol, | |
304 | skb->csum); | |
3db05fea | 305 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
1da177e4 LT |
306 | } |
307 | return 1; | |
308 | } | |
309 | ||
310 | ||
311 | static int | |
51ef348b | 312 | tcp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp) |
1da177e4 | 313 | { |
51ef348b JV |
314 | unsigned int tcphoff; |
315 | ||
316 | #ifdef CONFIG_IP_VS_IPV6 | |
317 | if (af == AF_INET6) | |
318 | tcphoff = sizeof(struct ipv6hdr); | |
319 | else | |
320 | #endif | |
321 | tcphoff = ip_hdrlen(skb); | |
1da177e4 LT |
322 | |
323 | switch (skb->ip_summed) { | |
324 | case CHECKSUM_NONE: | |
325 | skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0); | |
84fa7933 | 326 | case CHECKSUM_COMPLETE: |
51ef348b JV |
327 | #ifdef CONFIG_IP_VS_IPV6 |
328 | if (af == AF_INET6) { | |
329 | if (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, | |
330 | &ipv6_hdr(skb)->daddr, | |
331 | skb->len - tcphoff, | |
332 | ipv6_hdr(skb)->nexthdr, | |
333 | skb->csum)) { | |
0d79641a | 334 | IP_VS_DBG_RL_PKT(0, af, pp, skb, 0, |
51ef348b JV |
335 | "Failed checksum for"); |
336 | return 0; | |
337 | } | |
338 | } else | |
339 | #endif | |
340 | if (csum_tcpudp_magic(ip_hdr(skb)->saddr, | |
341 | ip_hdr(skb)->daddr, | |
342 | skb->len - tcphoff, | |
343 | ip_hdr(skb)->protocol, | |
344 | skb->csum)) { | |
0d79641a | 345 | IP_VS_DBG_RL_PKT(0, af, pp, skb, 0, |
51ef348b JV |
346 | "Failed checksum for"); |
347 | return 0; | |
348 | } | |
1da177e4 LT |
349 | break; |
350 | default: | |
84fa7933 | 351 | /* No need to checksum. */ |
1da177e4 LT |
352 | break; |
353 | } | |
354 | ||
355 | return 1; | |
356 | } | |
357 | ||
358 | ||
359 | #define TCP_DIR_INPUT 0 | |
360 | #define TCP_DIR_OUTPUT 4 | |
361 | #define TCP_DIR_INPUT_ONLY 8 | |
362 | ||
9b5b5cff | 363 | static const int tcp_state_off[IP_VS_DIR_LAST] = { |
1da177e4 LT |
364 | [IP_VS_DIR_INPUT] = TCP_DIR_INPUT, |
365 | [IP_VS_DIR_OUTPUT] = TCP_DIR_OUTPUT, | |
366 | [IP_VS_DIR_INPUT_ONLY] = TCP_DIR_INPUT_ONLY, | |
367 | }; | |
368 | ||
369 | /* | |
370 | * Timeout table[state] | |
371 | */ | |
4a85b96c | 372 | static const int tcp_timeouts[IP_VS_TCP_S_LAST+1] = { |
1da177e4 LT |
373 | [IP_VS_TCP_S_NONE] = 2*HZ, |
374 | [IP_VS_TCP_S_ESTABLISHED] = 15*60*HZ, | |
375 | [IP_VS_TCP_S_SYN_SENT] = 2*60*HZ, | |
376 | [IP_VS_TCP_S_SYN_RECV] = 1*60*HZ, | |
377 | [IP_VS_TCP_S_FIN_WAIT] = 2*60*HZ, | |
378 | [IP_VS_TCP_S_TIME_WAIT] = 2*60*HZ, | |
379 | [IP_VS_TCP_S_CLOSE] = 10*HZ, | |
380 | [IP_VS_TCP_S_CLOSE_WAIT] = 60*HZ, | |
381 | [IP_VS_TCP_S_LAST_ACK] = 30*HZ, | |
382 | [IP_VS_TCP_S_LISTEN] = 2*60*HZ, | |
383 | [IP_VS_TCP_S_SYNACK] = 120*HZ, | |
384 | [IP_VS_TCP_S_LAST] = 2*HZ, | |
385 | }; | |
386 | ||
36cbd3dc | 387 | static const char *const tcp_state_name_table[IP_VS_TCP_S_LAST+1] = { |
1da177e4 LT |
388 | [IP_VS_TCP_S_NONE] = "NONE", |
389 | [IP_VS_TCP_S_ESTABLISHED] = "ESTABLISHED", | |
390 | [IP_VS_TCP_S_SYN_SENT] = "SYN_SENT", | |
391 | [IP_VS_TCP_S_SYN_RECV] = "SYN_RECV", | |
392 | [IP_VS_TCP_S_FIN_WAIT] = "FIN_WAIT", | |
393 | [IP_VS_TCP_S_TIME_WAIT] = "TIME_WAIT", | |
394 | [IP_VS_TCP_S_CLOSE] = "CLOSE", | |
395 | [IP_VS_TCP_S_CLOSE_WAIT] = "CLOSE_WAIT", | |
396 | [IP_VS_TCP_S_LAST_ACK] = "LAST_ACK", | |
397 | [IP_VS_TCP_S_LISTEN] = "LISTEN", | |
398 | [IP_VS_TCP_S_SYNACK] = "SYNACK", | |
399 | [IP_VS_TCP_S_LAST] = "BUG!", | |
400 | }; | |
401 | ||
402 | #define sNO IP_VS_TCP_S_NONE | |
403 | #define sES IP_VS_TCP_S_ESTABLISHED | |
404 | #define sSS IP_VS_TCP_S_SYN_SENT | |
405 | #define sSR IP_VS_TCP_S_SYN_RECV | |
406 | #define sFW IP_VS_TCP_S_FIN_WAIT | |
407 | #define sTW IP_VS_TCP_S_TIME_WAIT | |
408 | #define sCL IP_VS_TCP_S_CLOSE | |
409 | #define sCW IP_VS_TCP_S_CLOSE_WAIT | |
410 | #define sLA IP_VS_TCP_S_LAST_ACK | |
411 | #define sLI IP_VS_TCP_S_LISTEN | |
412 | #define sSA IP_VS_TCP_S_SYNACK | |
413 | ||
414 | struct tcp_states_t { | |
415 | int next_state[IP_VS_TCP_S_LAST]; | |
416 | }; | |
417 | ||
418 | static const char * tcp_state_name(int state) | |
419 | { | |
420 | if (state >= IP_VS_TCP_S_LAST) | |
421 | return "ERR!"; | |
422 | return tcp_state_name_table[state] ? tcp_state_name_table[state] : "?"; | |
423 | } | |
424 | ||
425 | static struct tcp_states_t tcp_states [] = { | |
426 | /* INPUT */ | |
427 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ | |
428 | /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }}, | |
429 | /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sTW }}, | |
c6c96c18 | 430 | /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }}, |
1da177e4 LT |
431 | /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sSR }}, |
432 | ||
433 | /* OUTPUT */ | |
434 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ | |
435 | /*syn*/ {{sSS, sES, sSS, sSR, sSS, sSS, sSS, sSS, sSS, sLI, sSR }}, | |
436 | /*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }}, | |
437 | /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }}, | |
438 | /*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }}, | |
439 | ||
440 | /* INPUT-ONLY */ | |
441 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ | |
442 | /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }}, | |
443 | /*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }}, | |
c6c96c18 | 444 | /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }}, |
1da177e4 LT |
445 | /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }}, |
446 | }; | |
447 | ||
448 | static struct tcp_states_t tcp_states_dos [] = { | |
449 | /* INPUT */ | |
450 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ | |
451 | /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSA }}, | |
452 | /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sSA }}, | |
c6c96c18 | 453 | /*ack*/ {{sES, sES, sSS, sSR, sFW, sTW, sCL, sCW, sCL, sLI, sSA }}, |
1da177e4 LT |
454 | /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }}, |
455 | ||
456 | /* OUTPUT */ | |
457 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ | |
458 | /*syn*/ {{sSS, sES, sSS, sSA, sSS, sSS, sSS, sSS, sSS, sLI, sSA }}, | |
459 | /*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }}, | |
460 | /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }}, | |
461 | /*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }}, | |
462 | ||
463 | /* INPUT-ONLY */ | |
464 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ | |
465 | /*syn*/ {{sSA, sES, sES, sSR, sSA, sSA, sSA, sSA, sSA, sSA, sSA }}, | |
466 | /*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }}, | |
c6c96c18 | 467 | /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }}, |
1da177e4 LT |
468 | /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }}, |
469 | }; | |
470 | ||
9330419d | 471 | static void tcp_timeout_change(struct ip_vs_proto_data *pd, int flags) |
1da177e4 LT |
472 | { |
473 | int on = (flags & 1); /* secure_tcp */ | |
474 | ||
475 | /* | |
476 | ** FIXME: change secure_tcp to independent sysctl var | |
477 | ** or make it per-service or per-app because it is valid | |
478 | ** for most if not for all of the applications. Something | |
479 | ** like "capabilities" (flags) for each object. | |
480 | */ | |
9330419d | 481 | pd->tcp_state_table = (on ? tcp_states_dos : tcp_states); |
1da177e4 LT |
482 | } |
483 | ||
1da177e4 LT |
484 | static inline int tcp_state_idx(struct tcphdr *th) |
485 | { | |
486 | if (th->rst) | |
487 | return 3; | |
488 | if (th->syn) | |
489 | return 0; | |
490 | if (th->fin) | |
491 | return 1; | |
492 | if (th->ack) | |
493 | return 2; | |
494 | return -1; | |
495 | } | |
496 | ||
497 | static inline void | |
9330419d | 498 | set_tcp_state(struct ip_vs_proto_data *pd, struct ip_vs_conn *cp, |
1da177e4 LT |
499 | int direction, struct tcphdr *th) |
500 | { | |
501 | int state_idx; | |
502 | int new_state = IP_VS_TCP_S_CLOSE; | |
503 | int state_off = tcp_state_off[direction]; | |
504 | ||
505 | /* | |
506 | * Update state offset to INPUT_ONLY if necessary | |
507 | * or delete NO_OUTPUT flag if output packet detected | |
508 | */ | |
509 | if (cp->flags & IP_VS_CONN_F_NOOUTPUT) { | |
510 | if (state_off == TCP_DIR_OUTPUT) | |
511 | cp->flags &= ~IP_VS_CONN_F_NOOUTPUT; | |
512 | else | |
513 | state_off = TCP_DIR_INPUT_ONLY; | |
514 | } | |
515 | ||
516 | if ((state_idx = tcp_state_idx(th)) < 0) { | |
517 | IP_VS_DBG(8, "tcp_state_idx=%d!!!\n", state_idx); | |
518 | goto tcp_state_out; | |
519 | } | |
520 | ||
9330419d HS |
521 | new_state = |
522 | pd->tcp_state_table[state_off+state_idx].next_state[cp->state]; | |
1da177e4 LT |
523 | |
524 | tcp_state_out: | |
525 | if (new_state != cp->state) { | |
526 | struct ip_vs_dest *dest = cp->dest; | |
527 | ||
cfc78c5a JV |
528 | IP_VS_DBG_BUF(8, "%s %s [%c%c%c%c] %s:%d->" |
529 | "%s:%d state: %s->%s conn->refcnt:%d\n", | |
9330419d | 530 | pd->pp->name, |
cfc78c5a JV |
531 | ((state_off == TCP_DIR_OUTPUT) ? |
532 | "output " : "input "), | |
533 | th->syn ? 'S' : '.', | |
534 | th->fin ? 'F' : '.', | |
535 | th->ack ? 'A' : '.', | |
536 | th->rst ? 'R' : '.', | |
f18ae720 | 537 | IP_VS_DBG_ADDR(cp->daf, &cp->daddr), |
cfc78c5a JV |
538 | ntohs(cp->dport), |
539 | IP_VS_DBG_ADDR(cp->af, &cp->caddr), | |
540 | ntohs(cp->cport), | |
541 | tcp_state_name(cp->state), | |
542 | tcp_state_name(new_state), | |
543 | atomic_read(&cp->refcnt)); | |
544 | ||
1da177e4 LT |
545 | if (dest) { |
546 | if (!(cp->flags & IP_VS_CONN_F_INACTIVE) && | |
547 | (new_state != IP_VS_TCP_S_ESTABLISHED)) { | |
548 | atomic_dec(&dest->activeconns); | |
549 | atomic_inc(&dest->inactconns); | |
550 | cp->flags |= IP_VS_CONN_F_INACTIVE; | |
551 | } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) && | |
552 | (new_state == IP_VS_TCP_S_ESTABLISHED)) { | |
553 | atomic_inc(&dest->activeconns); | |
554 | atomic_dec(&dest->inactconns); | |
555 | cp->flags &= ~IP_VS_CONN_F_INACTIVE; | |
556 | } | |
557 | } | |
558 | } | |
559 | ||
4a85b96c HS |
560 | if (likely(pd)) |
561 | cp->timeout = pd->timeout_table[cp->state = new_state]; | |
562 | else /* What to do ? */ | |
563 | cp->timeout = tcp_timeouts[cp->state = new_state]; | |
1da177e4 LT |
564 | } |
565 | ||
1da177e4 LT |
566 | /* |
567 | * Handle state transitions | |
568 | */ | |
4a516f11 | 569 | static void |
1da177e4 LT |
570 | tcp_state_transition(struct ip_vs_conn *cp, int direction, |
571 | const struct sk_buff *skb, | |
9330419d | 572 | struct ip_vs_proto_data *pd) |
1da177e4 LT |
573 | { |
574 | struct tcphdr _tcph, *th; | |
575 | ||
0bbdd42b JV |
576 | #ifdef CONFIG_IP_VS_IPV6 |
577 | int ihl = cp->af == AF_INET ? ip_hdrlen(skb) : sizeof(struct ipv6hdr); | |
578 | #else | |
579 | int ihl = ip_hdrlen(skb); | |
580 | #endif | |
581 | ||
582 | th = skb_header_pointer(skb, ihl, sizeof(_tcph), &_tcph); | |
1da177e4 | 583 | if (th == NULL) |
4a516f11 | 584 | return; |
1da177e4 | 585 | |
ac69269a | 586 | spin_lock_bh(&cp->lock); |
9330419d | 587 | set_tcp_state(pd, cp, direction, th); |
ac69269a | 588 | spin_unlock_bh(&cp->lock); |
1da177e4 LT |
589 | } |
590 | ||
75e7ce66 | 591 | static inline __u16 tcp_app_hashkey(__be16 port) |
1da177e4 | 592 | { |
75e7ce66 AV |
593 | return (((__force u16)port >> TCP_APP_TAB_BITS) ^ (__force u16)port) |
594 | & TCP_APP_TAB_MASK; | |
1da177e4 LT |
595 | } |
596 | ||
597 | ||
ab8a5e84 | 598 | static int tcp_register_app(struct net *net, struct ip_vs_app *inc) |
1da177e4 LT |
599 | { |
600 | struct ip_vs_app *i; | |
75e7ce66 AV |
601 | __u16 hash; |
602 | __be16 port = inc->port; | |
1da177e4 | 603 | int ret = 0; |
ab8a5e84 | 604 | struct netns_ipvs *ipvs = net_ipvs(net); |
18d6ade6 | 605 | struct ip_vs_proto_data *pd = ip_vs_proto_data_get(ipvs, IPPROTO_TCP); |
1da177e4 LT |
606 | |
607 | hash = tcp_app_hashkey(port); | |
608 | ||
4a85b96c | 609 | list_for_each_entry(i, &ipvs->tcp_apps[hash], p_list) { |
1da177e4 LT |
610 | if (i->port == port) { |
611 | ret = -EEXIST; | |
612 | goto out; | |
613 | } | |
614 | } | |
363c97d7 | 615 | list_add_rcu(&inc->p_list, &ipvs->tcp_apps[hash]); |
9bbac6a9 | 616 | atomic_inc(&pd->appcnt); |
1da177e4 LT |
617 | |
618 | out: | |
1da177e4 LT |
619 | return ret; |
620 | } | |
621 | ||
622 | ||
623 | static void | |
ab8a5e84 | 624 | tcp_unregister_app(struct net *net, struct ip_vs_app *inc) |
1da177e4 | 625 | { |
18d6ade6 | 626 | struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net_ipvs(net), IPPROTO_TCP); |
4a85b96c | 627 | |
9bbac6a9 | 628 | atomic_dec(&pd->appcnt); |
363c97d7 | 629 | list_del_rcu(&inc->p_list); |
1da177e4 LT |
630 | } |
631 | ||
632 | ||
633 | static int | |
634 | tcp_app_conn_bind(struct ip_vs_conn *cp) | |
635 | { | |
58dbc6f2 | 636 | struct netns_ipvs *ipvs = cp->ipvs; |
1da177e4 LT |
637 | int hash; |
638 | struct ip_vs_app *inc; | |
639 | int result = 0; | |
640 | ||
641 | /* Default binding: bind app only for NAT */ | |
642 | if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) | |
643 | return 0; | |
644 | ||
645 | /* Lookup application incarnations and bind the right one */ | |
646 | hash = tcp_app_hashkey(cp->vport); | |
647 | ||
363c97d7 JA |
648 | rcu_read_lock(); |
649 | list_for_each_entry_rcu(inc, &ipvs->tcp_apps[hash], p_list) { | |
1da177e4 LT |
650 | if (inc->port == cp->vport) { |
651 | if (unlikely(!ip_vs_app_inc_get(inc))) | |
652 | break; | |
363c97d7 | 653 | rcu_read_unlock(); |
1da177e4 | 654 | |
1e3e238e | 655 | IP_VS_DBG_BUF(9, "%s(): Binding conn %s:%u->" |
cfc78c5a JV |
656 | "%s:%u to app %s on port %u\n", |
657 | __func__, | |
658 | IP_VS_DBG_ADDR(cp->af, &cp->caddr), | |
659 | ntohs(cp->cport), | |
660 | IP_VS_DBG_ADDR(cp->af, &cp->vaddr), | |
661 | ntohs(cp->vport), | |
662 | inc->name, ntohs(inc->port)); | |
663 | ||
1da177e4 LT |
664 | cp->app = inc; |
665 | if (inc->init_conn) | |
666 | result = inc->init_conn(inc, cp); | |
667 | goto out; | |
668 | } | |
669 | } | |
363c97d7 | 670 | rcu_read_unlock(); |
1da177e4 LT |
671 | |
672 | out: | |
673 | return result; | |
674 | } | |
675 | ||
676 | ||
677 | /* | |
678 | * Set LISTEN timeout. (ip_vs_conn_put will setup timer) | |
679 | */ | |
4a85b96c | 680 | void ip_vs_tcp_conn_listen(struct net *net, struct ip_vs_conn *cp) |
1da177e4 | 681 | { |
18d6ade6 | 682 | struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net_ipvs(net), IPPROTO_TCP); |
4a85b96c | 683 | |
ac69269a | 684 | spin_lock_bh(&cp->lock); |
1da177e4 | 685 | cp->state = IP_VS_TCP_S_LISTEN; |
4a85b96c HS |
686 | cp->timeout = (pd ? pd->timeout_table[IP_VS_TCP_S_LISTEN] |
687 | : tcp_timeouts[IP_VS_TCP_S_LISTEN]); | |
ac69269a | 688 | spin_unlock_bh(&cp->lock); |
1da177e4 LT |
689 | } |
690 | ||
4a85b96c HS |
691 | /* --------------------------------------------- |
692 | * timeouts is netns related now. | |
693 | * --------------------------------------------- | |
694 | */ | |
582b8e3e | 695 | static int __ip_vs_tcp_init(struct net *net, struct ip_vs_proto_data *pd) |
1da177e4 | 696 | { |
4a85b96c | 697 | struct netns_ipvs *ipvs = net_ipvs(net); |
1da177e4 | 698 | |
4a85b96c | 699 | ip_vs_init_hash_table(ipvs->tcp_apps, TCP_APP_TAB_SIZE); |
4a85b96c HS |
700 | pd->timeout_table = ip_vs_create_timeout_table((int *)tcp_timeouts, |
701 | sizeof(tcp_timeouts)); | |
582b8e3e HS |
702 | if (!pd->timeout_table) |
703 | return -ENOMEM; | |
9330419d | 704 | pd->tcp_state_table = tcp_states; |
582b8e3e | 705 | return 0; |
4a85b96c | 706 | } |
1da177e4 | 707 | |
4a85b96c | 708 | static void __ip_vs_tcp_exit(struct net *net, struct ip_vs_proto_data *pd) |
1da177e4 | 709 | { |
4a85b96c | 710 | kfree(pd->timeout_table); |
1da177e4 LT |
711 | } |
712 | ||
713 | ||
714 | struct ip_vs_protocol ip_vs_protocol_tcp = { | |
715 | .name = "TCP", | |
716 | .protocol = IPPROTO_TCP, | |
2ad17def | 717 | .num_states = IP_VS_TCP_S_LAST, |
1da177e4 | 718 | .dont_defrag = 0, |
4a85b96c HS |
719 | .init = NULL, |
720 | .exit = NULL, | |
721 | .init_netns = __ip_vs_tcp_init, | |
722 | .exit_netns = __ip_vs_tcp_exit, | |
1da177e4 LT |
723 | .register_app = tcp_register_app, |
724 | .unregister_app = tcp_unregister_app, | |
725 | .conn_schedule = tcp_conn_schedule, | |
5c0d2374 SH |
726 | .conn_in_get = ip_vs_conn_in_get_proto, |
727 | .conn_out_get = ip_vs_conn_out_get_proto, | |
1da177e4 LT |
728 | .snat_handler = tcp_snat_handler, |
729 | .dnat_handler = tcp_dnat_handler, | |
730 | .csum_check = tcp_csum_check, | |
731 | .state_name = tcp_state_name, | |
732 | .state_transition = tcp_state_transition, | |
733 | .app_conn_bind = tcp_app_conn_bind, | |
734 | .debug_packet = ip_vs_tcpudp_debug_packet, | |
735 | .timeout_change = tcp_timeout_change, | |
1da177e4 | 736 | }; |