Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ip_vs_proto_tcp.c: TCP load balancing support for IPVS |
| 3 | * |
| 4 | * Version: $Id: ip_vs_proto_tcp.c,v 1.3 2002/11/30 01:50:35 wensong Exp $ |
| 5 | * |
| 6 | * Authors: Wensong Zhang <wensong@linuxvirtualserver.org> |
| 7 | * Julian Anastasov <ja@ssi.bg> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * as published by the Free Software Foundation; either version |
| 12 | * 2 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * Changes: |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/ip.h> |
| 20 | #include <linux/tcp.h> /* for tcphdr */ |
| 21 | #include <net/ip.h> |
| 22 | #include <net/tcp.h> /* for csum_tcpudp_magic */ |
| 23 | #include <linux/netfilter_ipv4.h> |
| 24 | |
| 25 | #include <net/ip_vs.h> |
| 26 | |
| 27 | |
| 28 | static struct ip_vs_conn * |
| 29 | tcp_conn_in_get(const struct sk_buff *skb, struct ip_vs_protocol *pp, |
| 30 | const struct iphdr *iph, unsigned int proto_off, int inverse) |
| 31 | { |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 32 | __be16 _ports[2], *pptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports); |
| 35 | if (pptr == NULL) |
| 36 | return NULL; |
| 37 | |
| 38 | if (likely(!inverse)) { |
| 39 | return ip_vs_conn_in_get(iph->protocol, |
| 40 | iph->saddr, pptr[0], |
| 41 | iph->daddr, pptr[1]); |
| 42 | } else { |
| 43 | return ip_vs_conn_in_get(iph->protocol, |
| 44 | iph->daddr, pptr[1], |
| 45 | iph->saddr, pptr[0]); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | static struct ip_vs_conn * |
| 50 | tcp_conn_out_get(const struct sk_buff *skb, struct ip_vs_protocol *pp, |
| 51 | const struct iphdr *iph, unsigned int proto_off, int inverse) |
| 52 | { |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 53 | __be16 _ports[2], *pptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports); |
| 56 | if (pptr == NULL) |
| 57 | return NULL; |
| 58 | |
| 59 | if (likely(!inverse)) { |
| 60 | return ip_vs_conn_out_get(iph->protocol, |
| 61 | iph->saddr, pptr[0], |
| 62 | iph->daddr, pptr[1]); |
| 63 | } else { |
| 64 | return ip_vs_conn_out_get(iph->protocol, |
| 65 | iph->daddr, pptr[1], |
| 66 | iph->saddr, pptr[0]); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | static int |
| 72 | tcp_conn_schedule(struct sk_buff *skb, |
| 73 | struct ip_vs_protocol *pp, |
| 74 | int *verdict, struct ip_vs_conn **cpp) |
| 75 | { |
| 76 | struct ip_vs_service *svc; |
| 77 | struct tcphdr _tcph, *th; |
| 78 | |
| 79 | th = skb_header_pointer(skb, skb->nh.iph->ihl*4, |
| 80 | sizeof(_tcph), &_tcph); |
| 81 | if (th == NULL) { |
| 82 | *verdict = NF_DROP; |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | if (th->syn && |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 87 | (svc = ip_vs_service_get(skb->mark, skb->nh.iph->protocol, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | skb->nh.iph->daddr, th->dest))) { |
| 89 | if (ip_vs_todrop()) { |
| 90 | /* |
| 91 | * It seems that we are very loaded. |
| 92 | * We have to drop this packet :( |
| 93 | */ |
| 94 | ip_vs_service_put(svc); |
| 95 | *verdict = NF_DROP; |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Let the virtual server select a real server for the |
| 101 | * incoming connection, and create a connection entry. |
| 102 | */ |
| 103 | *cpp = ip_vs_schedule(svc, skb); |
| 104 | if (!*cpp) { |
| 105 | *verdict = ip_vs_leave(svc, skb, pp); |
| 106 | return 0; |
| 107 | } |
| 108 | ip_vs_service_put(svc); |
| 109 | } |
| 110 | return 1; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | static inline void |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 115 | tcp_fast_csum_update(struct tcphdr *tcph, __be32 oldip, __be32 newip, |
| 116 | __be16 oldport, __be16 newport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
| 118 | tcph->check = |
Al Viro | f9214b2 | 2006-11-16 02:41:18 -0800 | [diff] [blame^] | 119 | csum_fold(ip_vs_check_diff4(oldip, newip, |
| 120 | ip_vs_check_diff2(oldport, newport, |
| 121 | ~csum_unfold(tcph->check)))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
| 125 | static int |
| 126 | tcp_snat_handler(struct sk_buff **pskb, |
| 127 | struct ip_vs_protocol *pp, struct ip_vs_conn *cp) |
| 128 | { |
| 129 | struct tcphdr *tcph; |
| 130 | unsigned int tcphoff = (*pskb)->nh.iph->ihl * 4; |
| 131 | |
| 132 | /* csum_check requires unshared skb */ |
| 133 | if (!ip_vs_make_skb_writable(pskb, tcphoff+sizeof(*tcph))) |
| 134 | return 0; |
| 135 | |
| 136 | if (unlikely(cp->app != NULL)) { |
| 137 | /* Some checks before mangling */ |
| 138 | if (pp->csum_check && !pp->csum_check(*pskb, pp)) |
| 139 | return 0; |
| 140 | |
| 141 | /* Call application helper if needed */ |
| 142 | if (!ip_vs_app_pkt_out(cp, pskb)) |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | tcph = (void *)(*pskb)->nh.iph + tcphoff; |
| 147 | tcph->source = cp->vport; |
| 148 | |
| 149 | /* Adjust TCP checksums */ |
| 150 | if (!cp->app) { |
| 151 | /* Only port and addr are changed, do fast csum update */ |
| 152 | tcp_fast_csum_update(tcph, cp->daddr, cp->vaddr, |
| 153 | cp->dport, cp->vport); |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 154 | if ((*pskb)->ip_summed == CHECKSUM_COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | (*pskb)->ip_summed = CHECKSUM_NONE; |
| 156 | } else { |
| 157 | /* full checksum calculation */ |
| 158 | tcph->check = 0; |
| 159 | (*pskb)->csum = skb_checksum(*pskb, tcphoff, |
| 160 | (*pskb)->len - tcphoff, 0); |
| 161 | tcph->check = csum_tcpudp_magic(cp->vaddr, cp->caddr, |
| 162 | (*pskb)->len - tcphoff, |
| 163 | cp->protocol, |
| 164 | (*pskb)->csum); |
| 165 | IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n", |
| 166 | pp->name, tcph->check, |
| 167 | (char*)&(tcph->check) - (char*)tcph); |
| 168 | } |
| 169 | return 1; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | static int |
| 174 | tcp_dnat_handler(struct sk_buff **pskb, |
| 175 | struct ip_vs_protocol *pp, struct ip_vs_conn *cp) |
| 176 | { |
| 177 | struct tcphdr *tcph; |
| 178 | unsigned int tcphoff = (*pskb)->nh.iph->ihl * 4; |
| 179 | |
| 180 | /* csum_check requires unshared skb */ |
| 181 | if (!ip_vs_make_skb_writable(pskb, tcphoff+sizeof(*tcph))) |
| 182 | return 0; |
| 183 | |
| 184 | if (unlikely(cp->app != NULL)) { |
| 185 | /* Some checks before mangling */ |
| 186 | if (pp->csum_check && !pp->csum_check(*pskb, pp)) |
| 187 | return 0; |
| 188 | |
| 189 | /* |
| 190 | * Attempt ip_vs_app call. |
| 191 | * It will fix ip_vs_conn and iph ack_seq stuff |
| 192 | */ |
| 193 | if (!ip_vs_app_pkt_in(cp, pskb)) |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | tcph = (void *)(*pskb)->nh.iph + tcphoff; |
| 198 | tcph->dest = cp->dport; |
| 199 | |
| 200 | /* |
| 201 | * Adjust TCP checksums |
| 202 | */ |
| 203 | if (!cp->app) { |
| 204 | /* Only port and addr are changed, do fast csum update */ |
| 205 | tcp_fast_csum_update(tcph, cp->vaddr, cp->daddr, |
| 206 | cp->vport, cp->dport); |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 207 | if ((*pskb)->ip_summed == CHECKSUM_COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | (*pskb)->ip_summed = CHECKSUM_NONE; |
| 209 | } else { |
| 210 | /* full checksum calculation */ |
| 211 | tcph->check = 0; |
| 212 | (*pskb)->csum = skb_checksum(*pskb, tcphoff, |
| 213 | (*pskb)->len - tcphoff, 0); |
| 214 | tcph->check = csum_tcpudp_magic(cp->caddr, cp->daddr, |
| 215 | (*pskb)->len - tcphoff, |
| 216 | cp->protocol, |
| 217 | (*pskb)->csum); |
| 218 | (*pskb)->ip_summed = CHECKSUM_UNNECESSARY; |
| 219 | } |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | static int |
| 225 | tcp_csum_check(struct sk_buff *skb, struct ip_vs_protocol *pp) |
| 226 | { |
| 227 | unsigned int tcphoff = skb->nh.iph->ihl*4; |
| 228 | |
| 229 | switch (skb->ip_summed) { |
| 230 | case CHECKSUM_NONE: |
| 231 | skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0); |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 232 | case CHECKSUM_COMPLETE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | if (csum_tcpudp_magic(skb->nh.iph->saddr, skb->nh.iph->daddr, |
| 234 | skb->len - tcphoff, |
| 235 | skb->nh.iph->protocol, skb->csum)) { |
| 236 | IP_VS_DBG_RL_PKT(0, pp, skb, 0, |
| 237 | "Failed checksum for"); |
| 238 | return 0; |
| 239 | } |
| 240 | break; |
| 241 | default: |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 242 | /* No need to checksum. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | break; |
| 244 | } |
| 245 | |
| 246 | return 1; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | #define TCP_DIR_INPUT 0 |
| 251 | #define TCP_DIR_OUTPUT 4 |
| 252 | #define TCP_DIR_INPUT_ONLY 8 |
| 253 | |
Arjan van de Ven | 9b5b5cf | 2005-11-29 16:21:38 -0800 | [diff] [blame] | 254 | static const int tcp_state_off[IP_VS_DIR_LAST] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | [IP_VS_DIR_INPUT] = TCP_DIR_INPUT, |
| 256 | [IP_VS_DIR_OUTPUT] = TCP_DIR_OUTPUT, |
| 257 | [IP_VS_DIR_INPUT_ONLY] = TCP_DIR_INPUT_ONLY, |
| 258 | }; |
| 259 | |
| 260 | /* |
| 261 | * Timeout table[state] |
| 262 | */ |
| 263 | static int tcp_timeouts[IP_VS_TCP_S_LAST+1] = { |
| 264 | [IP_VS_TCP_S_NONE] = 2*HZ, |
| 265 | [IP_VS_TCP_S_ESTABLISHED] = 15*60*HZ, |
| 266 | [IP_VS_TCP_S_SYN_SENT] = 2*60*HZ, |
| 267 | [IP_VS_TCP_S_SYN_RECV] = 1*60*HZ, |
| 268 | [IP_VS_TCP_S_FIN_WAIT] = 2*60*HZ, |
| 269 | [IP_VS_TCP_S_TIME_WAIT] = 2*60*HZ, |
| 270 | [IP_VS_TCP_S_CLOSE] = 10*HZ, |
| 271 | [IP_VS_TCP_S_CLOSE_WAIT] = 60*HZ, |
| 272 | [IP_VS_TCP_S_LAST_ACK] = 30*HZ, |
| 273 | [IP_VS_TCP_S_LISTEN] = 2*60*HZ, |
| 274 | [IP_VS_TCP_S_SYNACK] = 120*HZ, |
| 275 | [IP_VS_TCP_S_LAST] = 2*HZ, |
| 276 | }; |
| 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | static char * tcp_state_name_table[IP_VS_TCP_S_LAST+1] = { |
| 279 | [IP_VS_TCP_S_NONE] = "NONE", |
| 280 | [IP_VS_TCP_S_ESTABLISHED] = "ESTABLISHED", |
| 281 | [IP_VS_TCP_S_SYN_SENT] = "SYN_SENT", |
| 282 | [IP_VS_TCP_S_SYN_RECV] = "SYN_RECV", |
| 283 | [IP_VS_TCP_S_FIN_WAIT] = "FIN_WAIT", |
| 284 | [IP_VS_TCP_S_TIME_WAIT] = "TIME_WAIT", |
| 285 | [IP_VS_TCP_S_CLOSE] = "CLOSE", |
| 286 | [IP_VS_TCP_S_CLOSE_WAIT] = "CLOSE_WAIT", |
| 287 | [IP_VS_TCP_S_LAST_ACK] = "LAST_ACK", |
| 288 | [IP_VS_TCP_S_LISTEN] = "LISTEN", |
| 289 | [IP_VS_TCP_S_SYNACK] = "SYNACK", |
| 290 | [IP_VS_TCP_S_LAST] = "BUG!", |
| 291 | }; |
| 292 | |
| 293 | #define sNO IP_VS_TCP_S_NONE |
| 294 | #define sES IP_VS_TCP_S_ESTABLISHED |
| 295 | #define sSS IP_VS_TCP_S_SYN_SENT |
| 296 | #define sSR IP_VS_TCP_S_SYN_RECV |
| 297 | #define sFW IP_VS_TCP_S_FIN_WAIT |
| 298 | #define sTW IP_VS_TCP_S_TIME_WAIT |
| 299 | #define sCL IP_VS_TCP_S_CLOSE |
| 300 | #define sCW IP_VS_TCP_S_CLOSE_WAIT |
| 301 | #define sLA IP_VS_TCP_S_LAST_ACK |
| 302 | #define sLI IP_VS_TCP_S_LISTEN |
| 303 | #define sSA IP_VS_TCP_S_SYNACK |
| 304 | |
| 305 | struct tcp_states_t { |
| 306 | int next_state[IP_VS_TCP_S_LAST]; |
| 307 | }; |
| 308 | |
| 309 | static const char * tcp_state_name(int state) |
| 310 | { |
| 311 | if (state >= IP_VS_TCP_S_LAST) |
| 312 | return "ERR!"; |
| 313 | return tcp_state_name_table[state] ? tcp_state_name_table[state] : "?"; |
| 314 | } |
| 315 | |
| 316 | static struct tcp_states_t tcp_states [] = { |
| 317 | /* INPUT */ |
| 318 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ |
| 319 | /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }}, |
| 320 | /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sTW }}, |
| 321 | /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }}, |
| 322 | /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sSR }}, |
| 323 | |
| 324 | /* OUTPUT */ |
| 325 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ |
| 326 | /*syn*/ {{sSS, sES, sSS, sSR, sSS, sSS, sSS, sSS, sSS, sLI, sSR }}, |
| 327 | /*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }}, |
| 328 | /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }}, |
| 329 | /*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }}, |
| 330 | |
| 331 | /* INPUT-ONLY */ |
| 332 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ |
| 333 | /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }}, |
| 334 | /*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }}, |
| 335 | /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }}, |
| 336 | /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }}, |
| 337 | }; |
| 338 | |
| 339 | static struct tcp_states_t tcp_states_dos [] = { |
| 340 | /* INPUT */ |
| 341 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ |
| 342 | /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSA }}, |
| 343 | /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sSA }}, |
| 344 | /*ack*/ {{sCL, sES, sSS, sSR, sFW, sTW, sCL, sCW, sCL, sLI, sSA }}, |
| 345 | /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }}, |
| 346 | |
| 347 | /* OUTPUT */ |
| 348 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ |
| 349 | /*syn*/ {{sSS, sES, sSS, sSA, sSS, sSS, sSS, sSS, sSS, sLI, sSA }}, |
| 350 | /*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }}, |
| 351 | /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }}, |
| 352 | /*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }}, |
| 353 | |
| 354 | /* INPUT-ONLY */ |
| 355 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ |
| 356 | /*syn*/ {{sSA, sES, sES, sSR, sSA, sSA, sSA, sSA, sSA, sSA, sSA }}, |
| 357 | /*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }}, |
| 358 | /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }}, |
| 359 | /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }}, |
| 360 | }; |
| 361 | |
| 362 | static struct tcp_states_t *tcp_state_table = tcp_states; |
| 363 | |
| 364 | |
| 365 | static void tcp_timeout_change(struct ip_vs_protocol *pp, int flags) |
| 366 | { |
| 367 | int on = (flags & 1); /* secure_tcp */ |
| 368 | |
| 369 | /* |
| 370 | ** FIXME: change secure_tcp to independent sysctl var |
| 371 | ** or make it per-service or per-app because it is valid |
| 372 | ** for most if not for all of the applications. Something |
| 373 | ** like "capabilities" (flags) for each object. |
| 374 | */ |
| 375 | tcp_state_table = (on? tcp_states_dos : tcp_states); |
| 376 | } |
| 377 | |
| 378 | static int |
| 379 | tcp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to) |
| 380 | { |
| 381 | return ip_vs_set_state_timeout(pp->timeout_table, IP_VS_TCP_S_LAST, |
| 382 | tcp_state_name_table, sname, to); |
| 383 | } |
| 384 | |
| 385 | static inline int tcp_state_idx(struct tcphdr *th) |
| 386 | { |
| 387 | if (th->rst) |
| 388 | return 3; |
| 389 | if (th->syn) |
| 390 | return 0; |
| 391 | if (th->fin) |
| 392 | return 1; |
| 393 | if (th->ack) |
| 394 | return 2; |
| 395 | return -1; |
| 396 | } |
| 397 | |
| 398 | static inline void |
| 399 | set_tcp_state(struct ip_vs_protocol *pp, struct ip_vs_conn *cp, |
| 400 | int direction, struct tcphdr *th) |
| 401 | { |
| 402 | int state_idx; |
| 403 | int new_state = IP_VS_TCP_S_CLOSE; |
| 404 | int state_off = tcp_state_off[direction]; |
| 405 | |
| 406 | /* |
| 407 | * Update state offset to INPUT_ONLY if necessary |
| 408 | * or delete NO_OUTPUT flag if output packet detected |
| 409 | */ |
| 410 | if (cp->flags & IP_VS_CONN_F_NOOUTPUT) { |
| 411 | if (state_off == TCP_DIR_OUTPUT) |
| 412 | cp->flags &= ~IP_VS_CONN_F_NOOUTPUT; |
| 413 | else |
| 414 | state_off = TCP_DIR_INPUT_ONLY; |
| 415 | } |
| 416 | |
| 417 | if ((state_idx = tcp_state_idx(th)) < 0) { |
| 418 | IP_VS_DBG(8, "tcp_state_idx=%d!!!\n", state_idx); |
| 419 | goto tcp_state_out; |
| 420 | } |
| 421 | |
| 422 | new_state = tcp_state_table[state_off+state_idx].next_state[cp->state]; |
| 423 | |
| 424 | tcp_state_out: |
| 425 | if (new_state != cp->state) { |
| 426 | struct ip_vs_dest *dest = cp->dest; |
| 427 | |
| 428 | IP_VS_DBG(8, "%s %s [%c%c%c%c] %u.%u.%u.%u:%d->" |
Roberto Nibali | 4b5bdf5 | 2006-01-03 14:22:59 -0800 | [diff] [blame] | 429 | "%u.%u.%u.%u:%d state: %s->%s conn->refcnt:%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | pp->name, |
| 431 | (state_off==TCP_DIR_OUTPUT)?"output ":"input ", |
| 432 | th->syn? 'S' : '.', |
| 433 | th->fin? 'F' : '.', |
| 434 | th->ack? 'A' : '.', |
| 435 | th->rst? 'R' : '.', |
| 436 | NIPQUAD(cp->daddr), ntohs(cp->dport), |
| 437 | NIPQUAD(cp->caddr), ntohs(cp->cport), |
| 438 | tcp_state_name(cp->state), |
| 439 | tcp_state_name(new_state), |
| 440 | atomic_read(&cp->refcnt)); |
| 441 | if (dest) { |
| 442 | if (!(cp->flags & IP_VS_CONN_F_INACTIVE) && |
| 443 | (new_state != IP_VS_TCP_S_ESTABLISHED)) { |
| 444 | atomic_dec(&dest->activeconns); |
| 445 | atomic_inc(&dest->inactconns); |
| 446 | cp->flags |= IP_VS_CONN_F_INACTIVE; |
| 447 | } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) && |
| 448 | (new_state == IP_VS_TCP_S_ESTABLISHED)) { |
| 449 | atomic_inc(&dest->activeconns); |
| 450 | atomic_dec(&dest->inactconns); |
| 451 | cp->flags &= ~IP_VS_CONN_F_INACTIVE; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | cp->timeout = pp->timeout_table[cp->state = new_state]; |
| 457 | } |
| 458 | |
| 459 | |
| 460 | /* |
| 461 | * Handle state transitions |
| 462 | */ |
| 463 | static int |
| 464 | tcp_state_transition(struct ip_vs_conn *cp, int direction, |
| 465 | const struct sk_buff *skb, |
| 466 | struct ip_vs_protocol *pp) |
| 467 | { |
| 468 | struct tcphdr _tcph, *th; |
| 469 | |
| 470 | th = skb_header_pointer(skb, skb->nh.iph->ihl*4, |
| 471 | sizeof(_tcph), &_tcph); |
| 472 | if (th == NULL) |
| 473 | return 0; |
| 474 | |
| 475 | spin_lock(&cp->lock); |
| 476 | set_tcp_state(pp, cp, direction, th); |
| 477 | spin_unlock(&cp->lock); |
| 478 | |
| 479 | return 1; |
| 480 | } |
| 481 | |
| 482 | |
| 483 | /* |
| 484 | * Hash table for TCP application incarnations |
| 485 | */ |
| 486 | #define TCP_APP_TAB_BITS 4 |
| 487 | #define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS) |
| 488 | #define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1) |
| 489 | |
| 490 | static struct list_head tcp_apps[TCP_APP_TAB_SIZE]; |
| 491 | static DEFINE_SPINLOCK(tcp_app_lock); |
| 492 | |
Al Viro | 75e7ce6 | 2006-11-14 21:13:28 -0800 | [diff] [blame] | 493 | static inline __u16 tcp_app_hashkey(__be16 port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | { |
Al Viro | 75e7ce6 | 2006-11-14 21:13:28 -0800 | [diff] [blame] | 495 | return (((__force u16)port >> TCP_APP_TAB_BITS) ^ (__force u16)port) |
| 496 | & TCP_APP_TAB_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | |
| 500 | static int tcp_register_app(struct ip_vs_app *inc) |
| 501 | { |
| 502 | struct ip_vs_app *i; |
Al Viro | 75e7ce6 | 2006-11-14 21:13:28 -0800 | [diff] [blame] | 503 | __u16 hash; |
| 504 | __be16 port = inc->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | int ret = 0; |
| 506 | |
| 507 | hash = tcp_app_hashkey(port); |
| 508 | |
| 509 | spin_lock_bh(&tcp_app_lock); |
| 510 | list_for_each_entry(i, &tcp_apps[hash], p_list) { |
| 511 | if (i->port == port) { |
| 512 | ret = -EEXIST; |
| 513 | goto out; |
| 514 | } |
| 515 | } |
| 516 | list_add(&inc->p_list, &tcp_apps[hash]); |
| 517 | atomic_inc(&ip_vs_protocol_tcp.appcnt); |
| 518 | |
| 519 | out: |
| 520 | spin_unlock_bh(&tcp_app_lock); |
| 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | |
| 525 | static void |
| 526 | tcp_unregister_app(struct ip_vs_app *inc) |
| 527 | { |
| 528 | spin_lock_bh(&tcp_app_lock); |
| 529 | atomic_dec(&ip_vs_protocol_tcp.appcnt); |
| 530 | list_del(&inc->p_list); |
| 531 | spin_unlock_bh(&tcp_app_lock); |
| 532 | } |
| 533 | |
| 534 | |
| 535 | static int |
| 536 | tcp_app_conn_bind(struct ip_vs_conn *cp) |
| 537 | { |
| 538 | int hash; |
| 539 | struct ip_vs_app *inc; |
| 540 | int result = 0; |
| 541 | |
| 542 | /* Default binding: bind app only for NAT */ |
| 543 | if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) |
| 544 | return 0; |
| 545 | |
| 546 | /* Lookup application incarnations and bind the right one */ |
| 547 | hash = tcp_app_hashkey(cp->vport); |
| 548 | |
| 549 | spin_lock(&tcp_app_lock); |
| 550 | list_for_each_entry(inc, &tcp_apps[hash], p_list) { |
| 551 | if (inc->port == cp->vport) { |
| 552 | if (unlikely(!ip_vs_app_inc_get(inc))) |
| 553 | break; |
| 554 | spin_unlock(&tcp_app_lock); |
| 555 | |
| 556 | IP_VS_DBG(9, "%s: Binding conn %u.%u.%u.%u:%u->" |
| 557 | "%u.%u.%u.%u:%u to app %s on port %u\n", |
| 558 | __FUNCTION__, |
| 559 | NIPQUAD(cp->caddr), ntohs(cp->cport), |
| 560 | NIPQUAD(cp->vaddr), ntohs(cp->vport), |
| 561 | inc->name, ntohs(inc->port)); |
| 562 | cp->app = inc; |
| 563 | if (inc->init_conn) |
| 564 | result = inc->init_conn(inc, cp); |
| 565 | goto out; |
| 566 | } |
| 567 | } |
| 568 | spin_unlock(&tcp_app_lock); |
| 569 | |
| 570 | out: |
| 571 | return result; |
| 572 | } |
| 573 | |
| 574 | |
| 575 | /* |
| 576 | * Set LISTEN timeout. (ip_vs_conn_put will setup timer) |
| 577 | */ |
| 578 | void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp) |
| 579 | { |
| 580 | spin_lock(&cp->lock); |
| 581 | cp->state = IP_VS_TCP_S_LISTEN; |
| 582 | cp->timeout = ip_vs_protocol_tcp.timeout_table[IP_VS_TCP_S_LISTEN]; |
| 583 | spin_unlock(&cp->lock); |
| 584 | } |
| 585 | |
| 586 | |
David S. Miller | ba602a8 | 2005-08-16 20:50:16 -0700 | [diff] [blame] | 587 | static void ip_vs_tcp_init(struct ip_vs_protocol *pp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | { |
| 589 | IP_VS_INIT_HASH_TABLE(tcp_apps); |
| 590 | pp->timeout_table = tcp_timeouts; |
| 591 | } |
| 592 | |
| 593 | |
David S. Miller | ba602a8 | 2005-08-16 20:50:16 -0700 | [diff] [blame] | 594 | static void ip_vs_tcp_exit(struct ip_vs_protocol *pp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | { |
| 596 | } |
| 597 | |
| 598 | |
| 599 | struct ip_vs_protocol ip_vs_protocol_tcp = { |
| 600 | .name = "TCP", |
| 601 | .protocol = IPPROTO_TCP, |
| 602 | .dont_defrag = 0, |
| 603 | .appcnt = ATOMIC_INIT(0), |
David S. Miller | ba602a8 | 2005-08-16 20:50:16 -0700 | [diff] [blame] | 604 | .init = ip_vs_tcp_init, |
| 605 | .exit = ip_vs_tcp_exit, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | .register_app = tcp_register_app, |
| 607 | .unregister_app = tcp_unregister_app, |
| 608 | .conn_schedule = tcp_conn_schedule, |
| 609 | .conn_in_get = tcp_conn_in_get, |
| 610 | .conn_out_get = tcp_conn_out_get, |
| 611 | .snat_handler = tcp_snat_handler, |
| 612 | .dnat_handler = tcp_dnat_handler, |
| 613 | .csum_check = tcp_csum_check, |
| 614 | .state_name = tcp_state_name, |
| 615 | .state_transition = tcp_state_transition, |
| 616 | .app_conn_bind = tcp_app_conn_bind, |
| 617 | .debug_packet = ip_vs_tcpudp_debug_packet, |
| 618 | .timeout_change = tcp_timeout_change, |
| 619 | .set_state_timeout = tcp_set_state_timeout, |
| 620 | }; |