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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Authors: Wensong Zhang <wensong@linuxvirtualserver.org> |
| 5 | * Julian Anastasov <ja@ssi.bg> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 12 | * Changes: Hans Schillstrom <hans.schillstrom@ericsson.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Hannes Eder | 9aada7a | 2009-07-30 14:29:44 -0700 | [diff] [blame] | 20 | #define KMSG_COMPONENT "IPVS" |
| 21 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Stephen Rothwell | 63f2c04 | 2008-09-12 23:23:50 -0700 | [diff] [blame] | 28 | #include <net/ip6_checksum.h> |
Herbert Xu | af1e1cf | 2007-10-14 00:39:33 -0700 | [diff] [blame] | 29 | #include <linux/netfilter.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/netfilter_ipv4.h> |
| 31 | |
| 32 | #include <net/ip_vs.h> |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | static int |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 35 | tcp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | int *verdict, struct ip_vs_conn **cpp) |
| 37 | { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 38 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | struct ip_vs_service *svc; |
| 40 | struct tcphdr _tcph, *th; |
Julius Volz | 3c2e050 | 2008-09-02 15:55:38 +0200 | [diff] [blame] | 41 | struct ip_vs_iphdr iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 43 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); |
Julius Volz | 3c2e050 | 2008-09-02 15:55:38 +0200 | [diff] [blame] | 44 | |
| 45 | th = skb_header_pointer(skb, iph.len, sizeof(_tcph), &_tcph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | if (th == NULL) { |
| 47 | *verdict = NF_DROP; |
| 48 | return 0; |
| 49 | } |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 50 | net = skb_net(skb); |
Julian Anastasov | 190ecd2 | 2010-10-17 16:24:37 +0300 | [diff] [blame] | 51 | /* No !th->ack check to allow scheduling on SYN+ACK for Active FTP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | if (th->syn && |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 53 | (svc = ip_vs_service_get(net, af, skb->mark, iph.protocol, |
| 54 | &iph.daddr, th->dest))) { |
Julian Anastasov | 190ecd2 | 2010-10-17 16:24:37 +0300 | [diff] [blame] | 55 | int ignored; |
| 56 | |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 57 | if (ip_vs_todrop(net_ipvs(net))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /* |
| 59 | * It seems that we are very loaded. |
| 60 | * We have to drop this packet :( |
| 61 | */ |
| 62 | ip_vs_service_put(svc); |
| 63 | *verdict = NF_DROP; |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Let the virtual server select a real server for the |
| 69 | * incoming connection, and create a connection entry. |
| 70 | */ |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 71 | *cpp = ip_vs_schedule(svc, skb, pd, &ignored); |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 72 | if (!*cpp && ignored <= 0) { |
| 73 | if (!ignored) |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 74 | *verdict = ip_vs_leave(svc, skb, pd); |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 75 | else { |
| 76 | ip_vs_service_put(svc); |
| 77 | *verdict = NF_DROP; |
| 78 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | ip_vs_service_put(svc); |
| 82 | } |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 83 | /* NF_ACCEPT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | static inline void |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 89 | tcp_fast_csum_update(int af, struct tcphdr *tcph, |
| 90 | const union nf_inet_addr *oldip, |
| 91 | const union nf_inet_addr *newip, |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 92 | __be16 oldport, __be16 newport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | tcph->check = |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 103 | csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip, |
Al Viro | f9214b2 | 2006-11-16 02:41:18 -0800 | [diff] [blame] | 104 | ip_vs_check_diff2(oldport, newport, |
| 105 | ~csum_unfold(tcph->check)))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 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 = |
Julian Anastasov | 5bc9068 | 2010-10-17 16:14:31 +0300 | [diff] [blame] | 118 | ~csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6, |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 119 | ip_vs_check_diff2(oldlen, newlen, |
Julian Anastasov | 5bc9068 | 2010-10-17 16:14:31 +0300 | [diff] [blame] | 120 | csum_unfold(tcph->check)))); |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 121 | else |
| 122 | #endif |
| 123 | tcph->check = |
Julian Anastasov | 5bc9068 | 2010-10-17 16:14:31 +0300 | [diff] [blame] | 124 | ~csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip, |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 125 | ip_vs_check_diff2(oldlen, newlen, |
Julian Anastasov | 5bc9068 | 2010-10-17 16:14:31 +0300 | [diff] [blame] | 126 | csum_unfold(tcph->check)))); |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | static int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 131 | tcp_snat_handler(struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | struct ip_vs_protocol *pp, struct ip_vs_conn *cp) |
| 133 | { |
| 134 | struct tcphdr *tcph; |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 135 | unsigned int tcphoff; |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 136 | int oldlen; |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 137 | int payload_csum = 0; |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 138 | |
| 139 | #ifdef CONFIG_IP_VS_IPV6 |
| 140 | if (cp->af == AF_INET6) |
| 141 | tcphoff = sizeof(struct ipv6hdr); |
| 142 | else |
| 143 | #endif |
| 144 | tcphoff = ip_hdrlen(skb); |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 145 | oldlen = skb->len - tcphoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | /* csum_check requires unshared skb */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 148 | if (!skb_make_writable(skb, tcphoff+sizeof(*tcph))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | return 0; |
| 150 | |
| 151 | if (unlikely(cp->app != NULL)) { |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 152 | int ret; |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | /* Some checks before mangling */ |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 155 | if (pp->csum_check && !pp->csum_check(cp->af, skb, pp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | return 0; |
| 157 | |
| 158 | /* Call application helper if needed */ |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 159 | if (!(ret = ip_vs_app_pkt_out(cp, skb))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | return 0; |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 161 | /* ret=2: csum update is needed after payload mangling */ |
| 162 | if (ret == 1) |
| 163 | oldlen = skb->len - tcphoff; |
| 164 | else |
| 165 | payload_csum = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 168 | tcph = (void *)skb_network_header(skb) + tcphoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | tcph->source = cp->vport; |
| 170 | |
| 171 | /* Adjust TCP checksums */ |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 172 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 173 | tcp_partial_csum_update(cp->af, tcph, &cp->daddr, &cp->vaddr, |
Harvey Harrison | ca62059 | 2008-11-06 23:09:56 -0800 | [diff] [blame] | 174 | htons(oldlen), |
| 175 | htons(skb->len - tcphoff)); |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 176 | } else if (!payload_csum) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | /* Only port and addr are changed, do fast csum update */ |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 178 | tcp_fast_csum_update(cp->af, tcph, &cp->daddr, &cp->vaddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | cp->dport, cp->vport); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 180 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 181 | skb->ip_summed = (cp->app && pp->csum_check) ? |
| 182 | CHECKSUM_UNNECESSARY : CHECKSUM_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } else { |
| 184 | /* full checksum calculation */ |
| 185 | tcph->check = 0; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 186 | skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0); |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 187 | #ifdef CONFIG_IP_VS_IPV6 |
| 188 | if (cp->af == AF_INET6) |
| 189 | tcph->check = csum_ipv6_magic(&cp->vaddr.in6, |
| 190 | &cp->caddr.in6, |
| 191 | skb->len - tcphoff, |
| 192 | cp->protocol, skb->csum); |
| 193 | else |
| 194 | #endif |
| 195 | tcph->check = csum_tcpudp_magic(cp->vaddr.ip, |
| 196 | cp->caddr.ip, |
| 197 | skb->len - tcphoff, |
| 198 | cp->protocol, |
| 199 | skb->csum); |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 200 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n", |
| 203 | pp->name, tcph->check, |
| 204 | (char*)&(tcph->check) - (char*)tcph); |
| 205 | } |
| 206 | return 1; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | static int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 211 | tcp_dnat_handler(struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | struct ip_vs_protocol *pp, struct ip_vs_conn *cp) |
| 213 | { |
| 214 | struct tcphdr *tcph; |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 215 | unsigned int tcphoff; |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 216 | int oldlen; |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 217 | int payload_csum = 0; |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 218 | |
| 219 | #ifdef CONFIG_IP_VS_IPV6 |
| 220 | if (cp->af == AF_INET6) |
| 221 | tcphoff = sizeof(struct ipv6hdr); |
| 222 | else |
| 223 | #endif |
| 224 | tcphoff = ip_hdrlen(skb); |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 225 | oldlen = skb->len - tcphoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | /* csum_check requires unshared skb */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 228 | if (!skb_make_writable(skb, tcphoff+sizeof(*tcph))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | return 0; |
| 230 | |
| 231 | if (unlikely(cp->app != NULL)) { |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 232 | int ret; |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | /* Some checks before mangling */ |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 235 | if (pp->csum_check && !pp->csum_check(cp->af, skb, pp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | return 0; |
| 237 | |
| 238 | /* |
| 239 | * Attempt ip_vs_app call. |
| 240 | * It will fix ip_vs_conn and iph ack_seq stuff |
| 241 | */ |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 242 | if (!(ret = ip_vs_app_pkt_in(cp, skb))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | return 0; |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 244 | /* ret=2: csum update is needed after payload mangling */ |
| 245 | if (ret == 1) |
| 246 | oldlen = skb->len - tcphoff; |
| 247 | else |
| 248 | payload_csum = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 251 | tcph = (void *)skb_network_header(skb) + tcphoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | tcph->dest = cp->dport; |
| 253 | |
| 254 | /* |
| 255 | * Adjust TCP checksums |
| 256 | */ |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 257 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
Julian Anastasov | 5bc9068 | 2010-10-17 16:14:31 +0300 | [diff] [blame] | 258 | tcp_partial_csum_update(cp->af, tcph, &cp->vaddr, &cp->daddr, |
Harvey Harrison | ca62059 | 2008-11-06 23:09:56 -0800 | [diff] [blame] | 259 | htons(oldlen), |
| 260 | htons(skb->len - tcphoff)); |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 261 | } else if (!payload_csum) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | /* Only port and addr are changed, do fast csum update */ |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 263 | tcp_fast_csum_update(cp->af, tcph, &cp->vaddr, &cp->daddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | cp->vport, cp->dport); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 265 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 266 | skb->ip_summed = (cp->app && pp->csum_check) ? |
| 267 | CHECKSUM_UNNECESSARY : CHECKSUM_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } else { |
| 269 | /* full checksum calculation */ |
| 270 | tcph->check = 0; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 271 | skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0); |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 272 | #ifdef CONFIG_IP_VS_IPV6 |
| 273 | if (cp->af == AF_INET6) |
| 274 | tcph->check = csum_ipv6_magic(&cp->caddr.in6, |
| 275 | &cp->daddr.in6, |
| 276 | skb->len - tcphoff, |
| 277 | cp->protocol, skb->csum); |
| 278 | else |
| 279 | #endif |
| 280 | tcph->check = csum_tcpudp_magic(cp->caddr.ip, |
| 281 | cp->daddr.ip, |
| 282 | skb->len - tcphoff, |
| 283 | cp->protocol, |
| 284 | skb->csum); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 285 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | return 1; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | static int |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 292 | tcp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 294 | unsigned int tcphoff; |
| 295 | |
| 296 | #ifdef CONFIG_IP_VS_IPV6 |
| 297 | if (af == AF_INET6) |
| 298 | tcphoff = sizeof(struct ipv6hdr); |
| 299 | else |
| 300 | #endif |
| 301 | tcphoff = ip_hdrlen(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | switch (skb->ip_summed) { |
| 304 | case CHECKSUM_NONE: |
| 305 | skb->csum = skb_checksum(skb, tcphoff, skb->len - tcphoff, 0); |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 306 | case CHECKSUM_COMPLETE: |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 307 | #ifdef CONFIG_IP_VS_IPV6 |
| 308 | if (af == AF_INET6) { |
| 309 | if (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
| 310 | &ipv6_hdr(skb)->daddr, |
| 311 | skb->len - tcphoff, |
| 312 | ipv6_hdr(skb)->nexthdr, |
| 313 | skb->csum)) { |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 314 | IP_VS_DBG_RL_PKT(0, af, pp, skb, 0, |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 315 | "Failed checksum for"); |
| 316 | return 0; |
| 317 | } |
| 318 | } else |
| 319 | #endif |
| 320 | if (csum_tcpudp_magic(ip_hdr(skb)->saddr, |
| 321 | ip_hdr(skb)->daddr, |
| 322 | skb->len - tcphoff, |
| 323 | ip_hdr(skb)->protocol, |
| 324 | skb->csum)) { |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 325 | IP_VS_DBG_RL_PKT(0, af, pp, skb, 0, |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 326 | "Failed checksum for"); |
| 327 | return 0; |
| 328 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | break; |
| 330 | default: |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 331 | /* No need to checksum. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | break; |
| 333 | } |
| 334 | |
| 335 | return 1; |
| 336 | } |
| 337 | |
| 338 | |
| 339 | #define TCP_DIR_INPUT 0 |
| 340 | #define TCP_DIR_OUTPUT 4 |
| 341 | #define TCP_DIR_INPUT_ONLY 8 |
| 342 | |
Arjan van de Ven | 9b5b5cf | 2005-11-29 16:21:38 -0800 | [diff] [blame] | 343 | static const int tcp_state_off[IP_VS_DIR_LAST] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | [IP_VS_DIR_INPUT] = TCP_DIR_INPUT, |
| 345 | [IP_VS_DIR_OUTPUT] = TCP_DIR_OUTPUT, |
| 346 | [IP_VS_DIR_INPUT_ONLY] = TCP_DIR_INPUT_ONLY, |
| 347 | }; |
| 348 | |
| 349 | /* |
| 350 | * Timeout table[state] |
| 351 | */ |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 352 | static const int tcp_timeouts[IP_VS_TCP_S_LAST+1] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | [IP_VS_TCP_S_NONE] = 2*HZ, |
| 354 | [IP_VS_TCP_S_ESTABLISHED] = 15*60*HZ, |
| 355 | [IP_VS_TCP_S_SYN_SENT] = 2*60*HZ, |
| 356 | [IP_VS_TCP_S_SYN_RECV] = 1*60*HZ, |
| 357 | [IP_VS_TCP_S_FIN_WAIT] = 2*60*HZ, |
| 358 | [IP_VS_TCP_S_TIME_WAIT] = 2*60*HZ, |
| 359 | [IP_VS_TCP_S_CLOSE] = 10*HZ, |
| 360 | [IP_VS_TCP_S_CLOSE_WAIT] = 60*HZ, |
| 361 | [IP_VS_TCP_S_LAST_ACK] = 30*HZ, |
| 362 | [IP_VS_TCP_S_LISTEN] = 2*60*HZ, |
| 363 | [IP_VS_TCP_S_SYNACK] = 120*HZ, |
| 364 | [IP_VS_TCP_S_LAST] = 2*HZ, |
| 365 | }; |
| 366 | |
Jan Engelhardt | 36cbd3d | 2009-08-05 10:42:58 -0700 | [diff] [blame] | 367 | static const char *const tcp_state_name_table[IP_VS_TCP_S_LAST+1] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | [IP_VS_TCP_S_NONE] = "NONE", |
| 369 | [IP_VS_TCP_S_ESTABLISHED] = "ESTABLISHED", |
| 370 | [IP_VS_TCP_S_SYN_SENT] = "SYN_SENT", |
| 371 | [IP_VS_TCP_S_SYN_RECV] = "SYN_RECV", |
| 372 | [IP_VS_TCP_S_FIN_WAIT] = "FIN_WAIT", |
| 373 | [IP_VS_TCP_S_TIME_WAIT] = "TIME_WAIT", |
| 374 | [IP_VS_TCP_S_CLOSE] = "CLOSE", |
| 375 | [IP_VS_TCP_S_CLOSE_WAIT] = "CLOSE_WAIT", |
| 376 | [IP_VS_TCP_S_LAST_ACK] = "LAST_ACK", |
| 377 | [IP_VS_TCP_S_LISTEN] = "LISTEN", |
| 378 | [IP_VS_TCP_S_SYNACK] = "SYNACK", |
| 379 | [IP_VS_TCP_S_LAST] = "BUG!", |
| 380 | }; |
| 381 | |
| 382 | #define sNO IP_VS_TCP_S_NONE |
| 383 | #define sES IP_VS_TCP_S_ESTABLISHED |
| 384 | #define sSS IP_VS_TCP_S_SYN_SENT |
| 385 | #define sSR IP_VS_TCP_S_SYN_RECV |
| 386 | #define sFW IP_VS_TCP_S_FIN_WAIT |
| 387 | #define sTW IP_VS_TCP_S_TIME_WAIT |
| 388 | #define sCL IP_VS_TCP_S_CLOSE |
| 389 | #define sCW IP_VS_TCP_S_CLOSE_WAIT |
| 390 | #define sLA IP_VS_TCP_S_LAST_ACK |
| 391 | #define sLI IP_VS_TCP_S_LISTEN |
| 392 | #define sSA IP_VS_TCP_S_SYNACK |
| 393 | |
| 394 | struct tcp_states_t { |
| 395 | int next_state[IP_VS_TCP_S_LAST]; |
| 396 | }; |
| 397 | |
| 398 | static const char * tcp_state_name(int state) |
| 399 | { |
| 400 | if (state >= IP_VS_TCP_S_LAST) |
| 401 | return "ERR!"; |
| 402 | return tcp_state_name_table[state] ? tcp_state_name_table[state] : "?"; |
| 403 | } |
| 404 | |
| 405 | static struct tcp_states_t tcp_states [] = { |
| 406 | /* INPUT */ |
| 407 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ |
| 408 | /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }}, |
| 409 | /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sTW }}, |
| 410 | /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }}, |
| 411 | /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sSR }}, |
| 412 | |
| 413 | /* OUTPUT */ |
| 414 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ |
| 415 | /*syn*/ {{sSS, sES, sSS, sSR, sSS, sSS, sSS, sSS, sSS, sLI, sSR }}, |
| 416 | /*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }}, |
| 417 | /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }}, |
| 418 | /*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }}, |
| 419 | |
| 420 | /* INPUT-ONLY */ |
| 421 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ |
| 422 | /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSR }}, |
| 423 | /*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }}, |
| 424 | /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }}, |
| 425 | /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }}, |
| 426 | }; |
| 427 | |
| 428 | static struct tcp_states_t tcp_states_dos [] = { |
| 429 | /* INPUT */ |
| 430 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ |
| 431 | /*syn*/ {{sSR, sES, sES, sSR, sSR, sSR, sSR, sSR, sSR, sSR, sSA }}, |
| 432 | /*fin*/ {{sCL, sCW, sSS, sTW, sTW, sTW, sCL, sCW, sLA, sLI, sSA }}, |
| 433 | /*ack*/ {{sCL, sES, sSS, sSR, sFW, sTW, sCL, sCW, sCL, sLI, sSA }}, |
| 434 | /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }}, |
| 435 | |
| 436 | /* OUTPUT */ |
| 437 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ |
| 438 | /*syn*/ {{sSS, sES, sSS, sSA, sSS, sSS, sSS, sSS, sSS, sLI, sSA }}, |
| 439 | /*fin*/ {{sTW, sFW, sSS, sTW, sFW, sTW, sCL, sTW, sLA, sLI, sTW }}, |
| 440 | /*ack*/ {{sES, sES, sSS, sES, sFW, sTW, sCL, sCW, sLA, sES, sES }}, |
| 441 | /*rst*/ {{sCL, sCL, sSS, sCL, sCL, sTW, sCL, sCL, sCL, sCL, sCL }}, |
| 442 | |
| 443 | /* INPUT-ONLY */ |
| 444 | /* sNO, sES, sSS, sSR, sFW, sTW, sCL, sCW, sLA, sLI, sSA */ |
| 445 | /*syn*/ {{sSA, sES, sES, sSR, sSA, sSA, sSA, sSA, sSA, sSA, sSA }}, |
| 446 | /*fin*/ {{sCL, sFW, sSS, sTW, sFW, sTW, sCL, sCW, sLA, sLI, sTW }}, |
| 447 | /*ack*/ {{sCL, sES, sSS, sES, sFW, sTW, sCL, sCW, sCL, sLI, sES }}, |
| 448 | /*rst*/ {{sCL, sCL, sCL, sSR, sCL, sCL, sCL, sCL, sLA, sLI, sCL }}, |
| 449 | }; |
| 450 | |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 451 | static void tcp_timeout_change(struct ip_vs_proto_data *pd, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | { |
| 453 | int on = (flags & 1); /* secure_tcp */ |
| 454 | |
| 455 | /* |
| 456 | ** FIXME: change secure_tcp to independent sysctl var |
| 457 | ** or make it per-service or per-app because it is valid |
| 458 | ** for most if not for all of the applications. Something |
| 459 | ** like "capabilities" (flags) for each object. |
| 460 | */ |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 461 | pd->tcp_state_table = (on ? tcp_states_dos : tcp_states); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | static inline int tcp_state_idx(struct tcphdr *th) |
| 465 | { |
| 466 | if (th->rst) |
| 467 | return 3; |
| 468 | if (th->syn) |
| 469 | return 0; |
| 470 | if (th->fin) |
| 471 | return 1; |
| 472 | if (th->ack) |
| 473 | return 2; |
| 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | static inline void |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 478 | set_tcp_state(struct ip_vs_proto_data *pd, struct ip_vs_conn *cp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | int direction, struct tcphdr *th) |
| 480 | { |
| 481 | int state_idx; |
| 482 | int new_state = IP_VS_TCP_S_CLOSE; |
| 483 | int state_off = tcp_state_off[direction]; |
| 484 | |
| 485 | /* |
| 486 | * Update state offset to INPUT_ONLY if necessary |
| 487 | * or delete NO_OUTPUT flag if output packet detected |
| 488 | */ |
| 489 | if (cp->flags & IP_VS_CONN_F_NOOUTPUT) { |
| 490 | if (state_off == TCP_DIR_OUTPUT) |
| 491 | cp->flags &= ~IP_VS_CONN_F_NOOUTPUT; |
| 492 | else |
| 493 | state_off = TCP_DIR_INPUT_ONLY; |
| 494 | } |
| 495 | |
| 496 | if ((state_idx = tcp_state_idx(th)) < 0) { |
| 497 | IP_VS_DBG(8, "tcp_state_idx=%d!!!\n", state_idx); |
| 498 | goto tcp_state_out; |
| 499 | } |
| 500 | |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 501 | new_state = |
| 502 | pd->tcp_state_table[state_off+state_idx].next_state[cp->state]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
| 504 | tcp_state_out: |
| 505 | if (new_state != cp->state) { |
| 506 | struct ip_vs_dest *dest = cp->dest; |
| 507 | |
Julius Volz | cfc78c5 | 2008-09-02 15:55:53 +0200 | [diff] [blame] | 508 | IP_VS_DBG_BUF(8, "%s %s [%c%c%c%c] %s:%d->" |
| 509 | "%s:%d state: %s->%s conn->refcnt:%d\n", |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 510 | pd->pp->name, |
Julius Volz | cfc78c5 | 2008-09-02 15:55:53 +0200 | [diff] [blame] | 511 | ((state_off == TCP_DIR_OUTPUT) ? |
| 512 | "output " : "input "), |
| 513 | th->syn ? 'S' : '.', |
| 514 | th->fin ? 'F' : '.', |
| 515 | th->ack ? 'A' : '.', |
| 516 | th->rst ? 'R' : '.', |
| 517 | IP_VS_DBG_ADDR(cp->af, &cp->daddr), |
| 518 | ntohs(cp->dport), |
| 519 | IP_VS_DBG_ADDR(cp->af, &cp->caddr), |
| 520 | ntohs(cp->cport), |
| 521 | tcp_state_name(cp->state), |
| 522 | tcp_state_name(new_state), |
| 523 | atomic_read(&cp->refcnt)); |
| 524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | if (dest) { |
| 526 | if (!(cp->flags & IP_VS_CONN_F_INACTIVE) && |
| 527 | (new_state != IP_VS_TCP_S_ESTABLISHED)) { |
| 528 | atomic_dec(&dest->activeconns); |
| 529 | atomic_inc(&dest->inactconns); |
| 530 | cp->flags |= IP_VS_CONN_F_INACTIVE; |
| 531 | } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) && |
| 532 | (new_state == IP_VS_TCP_S_ESTABLISHED)) { |
| 533 | atomic_inc(&dest->activeconns); |
| 534 | atomic_dec(&dest->inactconns); |
| 535 | cp->flags &= ~IP_VS_CONN_F_INACTIVE; |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 540 | if (likely(pd)) |
| 541 | cp->timeout = pd->timeout_table[cp->state = new_state]; |
| 542 | else /* What to do ? */ |
| 543 | cp->timeout = tcp_timeouts[cp->state = new_state]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | /* |
| 547 | * Handle state transitions |
| 548 | */ |
Simon Horman | 4a516f1 | 2011-09-16 14:11:49 +0900 | [diff] [blame] | 549 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | tcp_state_transition(struct ip_vs_conn *cp, int direction, |
| 551 | const struct sk_buff *skb, |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 552 | struct ip_vs_proto_data *pd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | { |
| 554 | struct tcphdr _tcph, *th; |
| 555 | |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 556 | #ifdef CONFIG_IP_VS_IPV6 |
| 557 | int ihl = cp->af == AF_INET ? ip_hdrlen(skb) : sizeof(struct ipv6hdr); |
| 558 | #else |
| 559 | int ihl = ip_hdrlen(skb); |
| 560 | #endif |
| 561 | |
| 562 | th = skb_header_pointer(skb, ihl, sizeof(_tcph), &_tcph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | if (th == NULL) |
Simon Horman | 4a516f1 | 2011-09-16 14:11:49 +0900 | [diff] [blame] | 564 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
| 566 | spin_lock(&cp->lock); |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 567 | set_tcp_state(pd, cp, direction, th); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | spin_unlock(&cp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Al Viro | 75e7ce6 | 2006-11-14 21:13:28 -0800 | [diff] [blame] | 571 | static inline __u16 tcp_app_hashkey(__be16 port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | { |
Al Viro | 75e7ce6 | 2006-11-14 21:13:28 -0800 | [diff] [blame] | 573 | return (((__force u16)port >> TCP_APP_TAB_BITS) ^ (__force u16)port) |
| 574 | & TCP_APP_TAB_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 578 | static int tcp_register_app(struct net *net, struct ip_vs_app *inc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | { |
| 580 | struct ip_vs_app *i; |
Al Viro | 75e7ce6 | 2006-11-14 21:13:28 -0800 | [diff] [blame] | 581 | __u16 hash; |
| 582 | __be16 port = inc->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | int ret = 0; |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 584 | struct netns_ipvs *ipvs = net_ipvs(net); |
| 585 | struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net, IPPROTO_TCP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
| 587 | hash = tcp_app_hashkey(port); |
| 588 | |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 589 | spin_lock_bh(&ipvs->tcp_app_lock); |
| 590 | list_for_each_entry(i, &ipvs->tcp_apps[hash], p_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | if (i->port == port) { |
| 592 | ret = -EEXIST; |
| 593 | goto out; |
| 594 | } |
| 595 | } |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 596 | list_add(&inc->p_list, &ipvs->tcp_apps[hash]); |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 597 | atomic_inc(&pd->appcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
| 599 | out: |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 600 | spin_unlock_bh(&ipvs->tcp_app_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | |
| 605 | static void |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 606 | tcp_unregister_app(struct net *net, struct ip_vs_app *inc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
Hans Schillstrom | ab8a5e8 | 2011-01-03 14:44:53 +0100 | [diff] [blame] | 608 | struct netns_ipvs *ipvs = net_ipvs(net); |
| 609 | struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net, IPPROTO_TCP); |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 610 | |
| 611 | spin_lock_bh(&ipvs->tcp_app_lock); |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 612 | atomic_dec(&pd->appcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | list_del(&inc->p_list); |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 614 | spin_unlock_bh(&ipvs->tcp_app_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | |
| 618 | static int |
| 619 | tcp_app_conn_bind(struct ip_vs_conn *cp) |
| 620 | { |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 621 | struct netns_ipvs *ipvs = net_ipvs(ip_vs_conn_net(cp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | int hash; |
| 623 | struct ip_vs_app *inc; |
| 624 | int result = 0; |
| 625 | |
| 626 | /* Default binding: bind app only for NAT */ |
| 627 | if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) |
| 628 | return 0; |
| 629 | |
| 630 | /* Lookup application incarnations and bind the right one */ |
| 631 | hash = tcp_app_hashkey(cp->vport); |
| 632 | |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 633 | spin_lock(&ipvs->tcp_app_lock); |
| 634 | list_for_each_entry(inc, &ipvs->tcp_apps[hash], p_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | if (inc->port == cp->vport) { |
| 636 | if (unlikely(!ip_vs_app_inc_get(inc))) |
| 637 | break; |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 638 | spin_unlock(&ipvs->tcp_app_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 640 | IP_VS_DBG_BUF(9, "%s(): Binding conn %s:%u->" |
Julius Volz | cfc78c5 | 2008-09-02 15:55:53 +0200 | [diff] [blame] | 641 | "%s:%u to app %s on port %u\n", |
| 642 | __func__, |
| 643 | IP_VS_DBG_ADDR(cp->af, &cp->caddr), |
| 644 | ntohs(cp->cport), |
| 645 | IP_VS_DBG_ADDR(cp->af, &cp->vaddr), |
| 646 | ntohs(cp->vport), |
| 647 | inc->name, ntohs(inc->port)); |
| 648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | cp->app = inc; |
| 650 | if (inc->init_conn) |
| 651 | result = inc->init_conn(inc, cp); |
| 652 | goto out; |
| 653 | } |
| 654 | } |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 655 | spin_unlock(&ipvs->tcp_app_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
| 657 | out: |
| 658 | return result; |
| 659 | } |
| 660 | |
| 661 | |
| 662 | /* |
| 663 | * Set LISTEN timeout. (ip_vs_conn_put will setup timer) |
| 664 | */ |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 665 | void ip_vs_tcp_conn_listen(struct net *net, struct ip_vs_conn *cp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | { |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 667 | struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net, IPPROTO_TCP); |
| 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | spin_lock(&cp->lock); |
| 670 | cp->state = IP_VS_TCP_S_LISTEN; |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 671 | cp->timeout = (pd ? pd->timeout_table[IP_VS_TCP_S_LISTEN] |
| 672 | : tcp_timeouts[IP_VS_TCP_S_LISTEN]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | spin_unlock(&cp->lock); |
| 674 | } |
| 675 | |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 676 | /* --------------------------------------------- |
| 677 | * timeouts is netns related now. |
| 678 | * --------------------------------------------- |
| 679 | */ |
| 680 | static void __ip_vs_tcp_init(struct net *net, struct ip_vs_proto_data *pd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | { |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 682 | struct netns_ipvs *ipvs = net_ipvs(net); |
| 683 | |
| 684 | ip_vs_init_hash_table(ipvs->tcp_apps, TCP_APP_TAB_SIZE); |
| 685 | spin_lock_init(&ipvs->tcp_app_lock); |
| 686 | pd->timeout_table = ip_vs_create_timeout_table((int *)tcp_timeouts, |
| 687 | sizeof(tcp_timeouts)); |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 688 | pd->tcp_state_table = tcp_states; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 691 | static void __ip_vs_tcp_exit(struct net *net, struct ip_vs_proto_data *pd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | { |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 693 | kfree(pd->timeout_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | |
| 697 | struct ip_vs_protocol ip_vs_protocol_tcp = { |
| 698 | .name = "TCP", |
| 699 | .protocol = IPPROTO_TCP, |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 700 | .num_states = IP_VS_TCP_S_LAST, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | .dont_defrag = 0, |
Hans Schillstrom | 4a85b96 | 2011-01-03 14:44:47 +0100 | [diff] [blame] | 702 | .init = NULL, |
| 703 | .exit = NULL, |
| 704 | .init_netns = __ip_vs_tcp_init, |
| 705 | .exit_netns = __ip_vs_tcp_exit, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | .register_app = tcp_register_app, |
| 707 | .unregister_app = tcp_unregister_app, |
| 708 | .conn_schedule = tcp_conn_schedule, |
Simon Horman | 5c0d237 | 2010-08-02 17:12:44 +0200 | [diff] [blame] | 709 | .conn_in_get = ip_vs_conn_in_get_proto, |
| 710 | .conn_out_get = ip_vs_conn_out_get_proto, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | .snat_handler = tcp_snat_handler, |
| 712 | .dnat_handler = tcp_dnat_handler, |
| 713 | .csum_check = tcp_csum_check, |
| 714 | .state_name = tcp_state_name, |
| 715 | .state_transition = tcp_state_transition, |
| 716 | .app_conn_bind = tcp_app_conn_bind, |
| 717 | .debug_packet = ip_vs_tcpudp_debug_packet, |
| 718 | .timeout_change = tcp_timeout_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | }; |