Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ip_vs_proto_udp.c: UDP 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 | * |
| 12 | * Changes: |
| 13 | * |
| 14 | */ |
| 15 | |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 16 | #include <linux/in.h> |
| 17 | #include <linux/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/kernel.h> |
Herbert Xu | af1e1cf | 2007-10-14 00:39:33 -0700 | [diff] [blame] | 19 | #include <linux/netfilter.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/netfilter_ipv4.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 21 | #include <linux/udp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #include <net/ip_vs.h> |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 24 | #include <net/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | static struct ip_vs_conn * |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 27 | udp_conn_in_get(int af, const struct sk_buff *skb, struct ip_vs_protocol *pp, |
| 28 | const struct ip_vs_iphdr *iph, unsigned int proto_off, |
| 29 | int inverse) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
| 31 | struct ip_vs_conn *cp; |
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)) { |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 39 | cp = ip_vs_conn_in_get(af, iph->protocol, |
| 40 | &iph->saddr, pptr[0], |
| 41 | &iph->daddr, pptr[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | } else { |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 43 | cp = ip_vs_conn_in_get(af, iph->protocol, |
| 44 | &iph->daddr, pptr[1], |
| 45 | &iph->saddr, pptr[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | return cp; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | static struct ip_vs_conn * |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 53 | udp_conn_out_get(int af, const struct sk_buff *skb, struct ip_vs_protocol *pp, |
| 54 | const struct ip_vs_iphdr *iph, unsigned int proto_off, |
| 55 | int inverse) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
| 57 | struct ip_vs_conn *cp; |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 58 | __be16 _ports[2], *pptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 60 | pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | if (pptr == NULL) |
| 62 | return NULL; |
| 63 | |
| 64 | if (likely(!inverse)) { |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 65 | cp = ip_vs_conn_out_get(af, iph->protocol, |
| 66 | &iph->saddr, pptr[0], |
| 67 | &iph->daddr, pptr[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } else { |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 69 | cp = ip_vs_conn_out_get(af, iph->protocol, |
| 70 | &iph->daddr, pptr[1], |
| 71 | &iph->saddr, pptr[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | return cp; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | static int |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 79 | udp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_protocol *pp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | int *verdict, struct ip_vs_conn **cpp) |
| 81 | { |
| 82 | struct ip_vs_service *svc; |
| 83 | struct udphdr _udph, *uh; |
Julius Volz | 3c2e050 | 2008-09-02 15:55:38 +0200 | [diff] [blame] | 84 | struct ip_vs_iphdr iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 86 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); |
Julius Volz | 3c2e050 | 2008-09-02 15:55:38 +0200 | [diff] [blame] | 87 | |
| 88 | uh = skb_header_pointer(skb, iph.len, sizeof(_udph), &_udph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | if (uh == NULL) { |
| 90 | *verdict = NF_DROP; |
| 91 | return 0; |
| 92 | } |
| 93 | |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 94 | svc = ip_vs_service_get(af, skb->mark, iph.protocol, |
Julius Volz | 3c2e050 | 2008-09-02 15:55:38 +0200 | [diff] [blame] | 95 | &iph.daddr, uh->dest); |
| 96 | if (svc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | if (ip_vs_todrop()) { |
| 98 | /* |
| 99 | * It seems that we are very loaded. |
| 100 | * We have to drop this packet :( |
| 101 | */ |
| 102 | ip_vs_service_put(svc); |
| 103 | *verdict = NF_DROP; |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Let the virtual server select a real server for the |
| 109 | * incoming connection, and create a connection entry. |
| 110 | */ |
| 111 | *cpp = ip_vs_schedule(svc, skb); |
| 112 | if (!*cpp) { |
| 113 | *verdict = ip_vs_leave(svc, skb, pp); |
| 114 | return 0; |
| 115 | } |
| 116 | ip_vs_service_put(svc); |
| 117 | } |
| 118 | return 1; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | static inline void |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 123 | udp_fast_csum_update(int af, struct udphdr *uhdr, |
| 124 | const union nf_inet_addr *oldip, |
| 125 | const union nf_inet_addr *newip, |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 126 | __be16 oldport, __be16 newport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 128 | #ifdef CONFIG_IP_VS_IPV6 |
| 129 | if (af == AF_INET6) |
| 130 | uhdr->check = |
| 131 | csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6, |
| 132 | ip_vs_check_diff2(oldport, newport, |
| 133 | ~csum_unfold(uhdr->check)))); |
| 134 | else |
| 135 | #endif |
| 136 | uhdr->check = |
| 137 | csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip, |
| 138 | ip_vs_check_diff2(oldport, newport, |
| 139 | ~csum_unfold(uhdr->check)))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | if (!uhdr->check) |
Al Viro | f6ab028 | 2006-11-16 02:36:50 -0800 | [diff] [blame] | 141 | uhdr->check = CSUM_MANGLED_0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 145 | udp_snat_handler(struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | struct ip_vs_protocol *pp, struct ip_vs_conn *cp) |
| 147 | { |
| 148 | struct udphdr *udph; |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 149 | unsigned int udphoff; |
| 150 | |
| 151 | #ifdef CONFIG_IP_VS_IPV6 |
| 152 | if (cp->af == AF_INET6) |
| 153 | udphoff = sizeof(struct ipv6hdr); |
| 154 | else |
| 155 | #endif |
| 156 | udphoff = ip_hdrlen(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | /* csum_check requires unshared skb */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 159 | if (!skb_make_writable(skb, udphoff+sizeof(*udph))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | return 0; |
| 161 | |
| 162 | if (unlikely(cp->app != NULL)) { |
| 163 | /* Some checks before mangling */ |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 164 | if (pp->csum_check && !pp->csum_check(cp->af, skb, pp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | return 0; |
| 166 | |
| 167 | /* |
| 168 | * Call application helper if needed |
| 169 | */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 170 | if (!ip_vs_app_pkt_out(cp, skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 174 | udph = (void *)skb_network_header(skb) + udphoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | udph->source = cp->vport; |
| 176 | |
| 177 | /* |
| 178 | * Adjust UDP checksums |
| 179 | */ |
| 180 | if (!cp->app && (udph->check != 0)) { |
| 181 | /* Only port and addr are changed, do fast csum update */ |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 182 | udp_fast_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | cp->dport, cp->vport); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 184 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 185 | skb->ip_summed = CHECKSUM_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } else { |
| 187 | /* full checksum calculation */ |
| 188 | udph->check = 0; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 189 | skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0); |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 190 | #ifdef CONFIG_IP_VS_IPV6 |
| 191 | if (cp->af == AF_INET6) |
| 192 | udph->check = csum_ipv6_magic(&cp->vaddr.in6, |
| 193 | &cp->caddr.in6, |
| 194 | skb->len - udphoff, |
| 195 | cp->protocol, skb->csum); |
| 196 | else |
| 197 | #endif |
| 198 | udph->check = csum_tcpudp_magic(cp->vaddr.ip, |
| 199 | cp->caddr.ip, |
| 200 | skb->len - udphoff, |
| 201 | cp->protocol, |
| 202 | skb->csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | if (udph->check == 0) |
Al Viro | f6ab028 | 2006-11-16 02:36:50 -0800 | [diff] [blame] | 204 | udph->check = CSUM_MANGLED_0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n", |
| 206 | pp->name, udph->check, |
| 207 | (char*)&(udph->check) - (char*)udph); |
| 208 | } |
| 209 | return 1; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | static int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 214 | udp_dnat_handler(struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | struct ip_vs_protocol *pp, struct ip_vs_conn *cp) |
| 216 | { |
| 217 | struct udphdr *udph; |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 218 | unsigned int udphoff; |
| 219 | |
| 220 | #ifdef CONFIG_IP_VS_IPV6 |
| 221 | if (cp->af == AF_INET6) |
| 222 | udphoff = sizeof(struct ipv6hdr); |
| 223 | else |
| 224 | #endif |
| 225 | udphoff = ip_hdrlen(skb); |
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, udphoff+sizeof(*udph))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | return 0; |
| 230 | |
| 231 | if (unlikely(cp->app != NULL)) { |
| 232 | /* Some checks before mangling */ |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 233 | if (pp->csum_check && !pp->csum_check(cp->af, skb, pp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | return 0; |
| 235 | |
| 236 | /* |
| 237 | * Attempt ip_vs_app call. |
| 238 | * It will fix ip_vs_conn |
| 239 | */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 240 | if (!ip_vs_app_pkt_in(cp, skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 244 | udph = (void *)skb_network_header(skb) + udphoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | udph->dest = cp->dport; |
| 246 | |
| 247 | /* |
| 248 | * Adjust UDP checksums |
| 249 | */ |
| 250 | if (!cp->app && (udph->check != 0)) { |
| 251 | /* Only port and addr are changed, do fast csum update */ |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 252 | udp_fast_csum_update(cp->af, udph, &cp->vaddr, &cp->daddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | cp->vport, cp->dport); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 254 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 255 | skb->ip_summed = CHECKSUM_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } else { |
| 257 | /* full checksum calculation */ |
| 258 | udph->check = 0; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 259 | skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0); |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 260 | #ifdef CONFIG_IP_VS_IPV6 |
| 261 | if (cp->af == AF_INET6) |
| 262 | udph->check = csum_ipv6_magic(&cp->caddr.in6, |
| 263 | &cp->daddr.in6, |
| 264 | skb->len - udphoff, |
| 265 | cp->protocol, skb->csum); |
| 266 | else |
| 267 | #endif |
| 268 | udph->check = csum_tcpudp_magic(cp->caddr.ip, |
| 269 | cp->daddr.ip, |
| 270 | skb->len - udphoff, |
| 271 | cp->protocol, |
| 272 | skb->csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | if (udph->check == 0) |
Al Viro | f6ab028 | 2006-11-16 02:36:50 -0800 | [diff] [blame] | 274 | udph->check = CSUM_MANGLED_0; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 275 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | static int |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 282 | udp_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] | 283 | { |
| 284 | struct udphdr _udph, *uh; |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 285 | unsigned int udphoff; |
| 286 | |
| 287 | #ifdef CONFIG_IP_VS_IPV6 |
| 288 | if (af == AF_INET6) |
| 289 | udphoff = sizeof(struct ipv6hdr); |
| 290 | else |
| 291 | #endif |
| 292 | udphoff = ip_hdrlen(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | uh = skb_header_pointer(skb, udphoff, sizeof(_udph), &_udph); |
| 295 | if (uh == NULL) |
| 296 | return 0; |
| 297 | |
| 298 | if (uh->check != 0) { |
| 299 | switch (skb->ip_summed) { |
| 300 | case CHECKSUM_NONE: |
| 301 | skb->csum = skb_checksum(skb, udphoff, |
| 302 | skb->len - udphoff, 0); |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 303 | case CHECKSUM_COMPLETE: |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 304 | #ifdef CONFIG_IP_VS_IPV6 |
| 305 | if (af == AF_INET6) { |
| 306 | if (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
| 307 | &ipv6_hdr(skb)->daddr, |
| 308 | skb->len - udphoff, |
| 309 | ipv6_hdr(skb)->nexthdr, |
| 310 | skb->csum)) { |
| 311 | IP_VS_DBG_RL_PKT(0, pp, skb, 0, |
| 312 | "Failed checksum for"); |
| 313 | return 0; |
| 314 | } |
| 315 | } else |
| 316 | #endif |
| 317 | if (csum_tcpudp_magic(ip_hdr(skb)->saddr, |
| 318 | ip_hdr(skb)->daddr, |
| 319 | skb->len - udphoff, |
| 320 | ip_hdr(skb)->protocol, |
| 321 | skb->csum)) { |
| 322 | IP_VS_DBG_RL_PKT(0, pp, skb, 0, |
| 323 | "Failed checksum for"); |
| 324 | return 0; |
| 325 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | break; |
| 327 | default: |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 328 | /* No need to checksum. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | break; |
| 330 | } |
| 331 | } |
| 332 | return 1; |
| 333 | } |
| 334 | |
| 335 | |
| 336 | /* |
| 337 | * Note: the caller guarantees that only one of register_app, |
| 338 | * unregister_app or app_conn_bind is called each time. |
| 339 | */ |
| 340 | |
| 341 | #define UDP_APP_TAB_BITS 4 |
| 342 | #define UDP_APP_TAB_SIZE (1 << UDP_APP_TAB_BITS) |
| 343 | #define UDP_APP_TAB_MASK (UDP_APP_TAB_SIZE - 1) |
| 344 | |
| 345 | static struct list_head udp_apps[UDP_APP_TAB_SIZE]; |
| 346 | static DEFINE_SPINLOCK(udp_app_lock); |
| 347 | |
Al Viro | 75e7ce6 | 2006-11-14 21:13:28 -0800 | [diff] [blame] | 348 | static inline __u16 udp_app_hashkey(__be16 port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | { |
Al Viro | 75e7ce6 | 2006-11-14 21:13:28 -0800 | [diff] [blame] | 350 | return (((__force u16)port >> UDP_APP_TAB_BITS) ^ (__force u16)port) |
| 351 | & UDP_APP_TAB_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | |
| 355 | static int udp_register_app(struct ip_vs_app *inc) |
| 356 | { |
| 357 | struct ip_vs_app *i; |
Al Viro | 75e7ce6 | 2006-11-14 21:13:28 -0800 | [diff] [blame] | 358 | __u16 hash; |
| 359 | __be16 port = inc->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | int ret = 0; |
| 361 | |
| 362 | hash = udp_app_hashkey(port); |
| 363 | |
| 364 | |
| 365 | spin_lock_bh(&udp_app_lock); |
| 366 | list_for_each_entry(i, &udp_apps[hash], p_list) { |
| 367 | if (i->port == port) { |
| 368 | ret = -EEXIST; |
| 369 | goto out; |
| 370 | } |
| 371 | } |
| 372 | list_add(&inc->p_list, &udp_apps[hash]); |
| 373 | atomic_inc(&ip_vs_protocol_udp.appcnt); |
| 374 | |
| 375 | out: |
| 376 | spin_unlock_bh(&udp_app_lock); |
| 377 | return ret; |
| 378 | } |
| 379 | |
| 380 | |
| 381 | static void |
| 382 | udp_unregister_app(struct ip_vs_app *inc) |
| 383 | { |
| 384 | spin_lock_bh(&udp_app_lock); |
| 385 | atomic_dec(&ip_vs_protocol_udp.appcnt); |
| 386 | list_del(&inc->p_list); |
| 387 | spin_unlock_bh(&udp_app_lock); |
| 388 | } |
| 389 | |
| 390 | |
| 391 | static int udp_app_conn_bind(struct ip_vs_conn *cp) |
| 392 | { |
| 393 | int hash; |
| 394 | struct ip_vs_app *inc; |
| 395 | int result = 0; |
| 396 | |
| 397 | /* Default binding: bind app only for NAT */ |
| 398 | if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) |
| 399 | return 0; |
| 400 | |
| 401 | /* Lookup application incarnations and bind the right one */ |
| 402 | hash = udp_app_hashkey(cp->vport); |
| 403 | |
| 404 | spin_lock(&udp_app_lock); |
| 405 | list_for_each_entry(inc, &udp_apps[hash], p_list) { |
| 406 | if (inc->port == cp->vport) { |
| 407 | if (unlikely(!ip_vs_app_inc_get(inc))) |
| 408 | break; |
| 409 | spin_unlock(&udp_app_lock); |
| 410 | |
Julius Volz | cfc78c5 | 2008-09-02 15:55:53 +0200 | [diff] [blame^] | 411 | IP_VS_DBG_BUF(9, "%s: Binding conn %s:%u->" |
| 412 | "%s:%u to app %s on port %u\n", |
| 413 | __func__, |
| 414 | IP_VS_DBG_ADDR(cp->af, &cp->caddr), |
| 415 | ntohs(cp->cport), |
| 416 | IP_VS_DBG_ADDR(cp->af, &cp->vaddr), |
| 417 | ntohs(cp->vport), |
| 418 | inc->name, ntohs(inc->port)); |
| 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | cp->app = inc; |
| 421 | if (inc->init_conn) |
| 422 | result = inc->init_conn(inc, cp); |
| 423 | goto out; |
| 424 | } |
| 425 | } |
| 426 | spin_unlock(&udp_app_lock); |
| 427 | |
| 428 | out: |
| 429 | return result; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | static int udp_timeouts[IP_VS_UDP_S_LAST+1] = { |
| 434 | [IP_VS_UDP_S_NORMAL] = 5*60*HZ, |
| 435 | [IP_VS_UDP_S_LAST] = 2*HZ, |
| 436 | }; |
| 437 | |
| 438 | static char * udp_state_name_table[IP_VS_UDP_S_LAST+1] = { |
| 439 | [IP_VS_UDP_S_NORMAL] = "UDP", |
| 440 | [IP_VS_UDP_S_LAST] = "BUG!", |
| 441 | }; |
| 442 | |
| 443 | |
| 444 | static int |
| 445 | udp_set_state_timeout(struct ip_vs_protocol *pp, char *sname, int to) |
| 446 | { |
| 447 | return ip_vs_set_state_timeout(pp->timeout_table, IP_VS_UDP_S_LAST, |
| 448 | udp_state_name_table, sname, to); |
| 449 | } |
| 450 | |
| 451 | static const char * udp_state_name(int state) |
| 452 | { |
| 453 | if (state >= IP_VS_UDP_S_LAST) |
| 454 | return "ERR!"; |
| 455 | return udp_state_name_table[state] ? udp_state_name_table[state] : "?"; |
| 456 | } |
| 457 | |
| 458 | static int |
| 459 | udp_state_transition(struct ip_vs_conn *cp, int direction, |
| 460 | const struct sk_buff *skb, |
| 461 | struct ip_vs_protocol *pp) |
| 462 | { |
| 463 | cp->timeout = pp->timeout_table[IP_VS_UDP_S_NORMAL]; |
| 464 | return 1; |
| 465 | } |
| 466 | |
| 467 | static void udp_init(struct ip_vs_protocol *pp) |
| 468 | { |
| 469 | IP_VS_INIT_HASH_TABLE(udp_apps); |
| 470 | pp->timeout_table = udp_timeouts; |
| 471 | } |
| 472 | |
| 473 | static void udp_exit(struct ip_vs_protocol *pp) |
| 474 | { |
| 475 | } |
| 476 | |
| 477 | |
| 478 | struct ip_vs_protocol ip_vs_protocol_udp = { |
| 479 | .name = "UDP", |
| 480 | .protocol = IPPROTO_UDP, |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 481 | .num_states = IP_VS_UDP_S_LAST, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | .dont_defrag = 0, |
| 483 | .init = udp_init, |
| 484 | .exit = udp_exit, |
| 485 | .conn_schedule = udp_conn_schedule, |
| 486 | .conn_in_get = udp_conn_in_get, |
| 487 | .conn_out_get = udp_conn_out_get, |
| 488 | .snat_handler = udp_snat_handler, |
| 489 | .dnat_handler = udp_dnat_handler, |
| 490 | .csum_check = udp_csum_check, |
| 491 | .state_transition = udp_state_transition, |
| 492 | .state_name = udp_state_name, |
| 493 | .register_app = udp_register_app, |
| 494 | .unregister_app = udp_unregister_app, |
| 495 | .app_conn_bind = udp_app_conn_bind, |
| 496 | .debug_packet = ip_vs_tcpudp_debug_packet, |
| 497 | .timeout_change = NULL, |
| 498 | .set_state_timeout = udp_set_state_timeout, |
| 499 | }; |