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