]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * ip_vs_proto.c: transport protocol load balancing support for IPVS | |
3 | * | |
1da177e4 LT |
4 | * Authors: Wensong Zhang <wensong@linuxvirtualserver.org> |
5 | * Julian Anastasov <ja@ssi.bg> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License | |
9 | * as published by the Free Software Foundation; either version | |
10 | * 2 of the License, or (at your option) any later version. | |
11 | * | |
12 | * Changes: | |
13 | * | |
14 | */ | |
15 | ||
9aada7ac HE |
16 | #define KMSG_COMPONENT "IPVS" |
17 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
18 | ||
1da177e4 LT |
19 | #include <linux/module.h> |
20 | #include <linux/kernel.h> | |
21 | #include <linux/skbuff.h> | |
5a0e3ad6 | 22 | #include <linux/gfp.h> |
1da177e4 LT |
23 | #include <linux/in.h> |
24 | #include <linux/ip.h> | |
25 | #include <net/protocol.h> | |
26 | #include <net/tcp.h> | |
27 | #include <net/udp.h> | |
28 | #include <asm/system.h> | |
29 | #include <linux/stat.h> | |
30 | #include <linux/proc_fs.h> | |
31 | ||
32 | #include <net/ip_vs.h> | |
33 | ||
34 | ||
35 | /* | |
36 | * IPVS protocols can only be registered/unregistered when the ipvs | |
37 | * module is loaded/unloaded, so no lock is needed in accessing the | |
38 | * ipvs protocol table. | |
39 | */ | |
40 | ||
41 | #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */ | |
42 | #define IP_VS_PROTO_HASH(proto) ((proto) & (IP_VS_PROTO_TAB_SIZE-1)) | |
43 | ||
44 | static struct ip_vs_protocol *ip_vs_proto_table[IP_VS_PROTO_TAB_SIZE]; | |
45 | ||
46 | ||
47 | /* | |
48 | * register an ipvs protocol | |
49 | */ | |
048cf48b | 50 | static int __used __init register_ip_vs_protocol(struct ip_vs_protocol *pp) |
1da177e4 LT |
51 | { |
52 | unsigned hash = IP_VS_PROTO_HASH(pp->protocol); | |
53 | ||
54 | pp->next = ip_vs_proto_table[hash]; | |
55 | ip_vs_proto_table[hash] = pp; | |
56 | ||
57 | if (pp->init != NULL) | |
58 | pp->init(pp); | |
59 | ||
60 | return 0; | |
61 | } | |
62 | ||
63 | ||
64 | /* | |
65 | * unregister an ipvs protocol | |
66 | */ | |
67 | static int unregister_ip_vs_protocol(struct ip_vs_protocol *pp) | |
68 | { | |
69 | struct ip_vs_protocol **pp_p; | |
70 | unsigned hash = IP_VS_PROTO_HASH(pp->protocol); | |
71 | ||
72 | pp_p = &ip_vs_proto_table[hash]; | |
73 | for (; *pp_p; pp_p = &(*pp_p)->next) { | |
74 | if (*pp_p == pp) { | |
75 | *pp_p = pp->next; | |
76 | if (pp->exit != NULL) | |
77 | pp->exit(pp); | |
78 | return 0; | |
79 | } | |
80 | } | |
81 | ||
82 | return -ESRCH; | |
83 | } | |
84 | ||
85 | ||
86 | /* | |
87 | * get ip_vs_protocol object by its proto. | |
88 | */ | |
89 | struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto) | |
90 | { | |
91 | struct ip_vs_protocol *pp; | |
92 | unsigned hash = IP_VS_PROTO_HASH(proto); | |
93 | ||
94 | for (pp = ip_vs_proto_table[hash]; pp; pp = pp->next) { | |
95 | if (pp->protocol == proto) | |
96 | return pp; | |
97 | } | |
98 | ||
99 | return NULL; | |
100 | } | |
9c3e1c39 | 101 | EXPORT_SYMBOL(ip_vs_proto_get); |
1da177e4 LT |
102 | |
103 | ||
104 | /* | |
105 | * Propagate event for state change to all protocols | |
106 | */ | |
107 | void ip_vs_protocol_timeout_change(int flags) | |
108 | { | |
109 | struct ip_vs_protocol *pp; | |
110 | int i; | |
111 | ||
112 | for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) { | |
113 | for (pp = ip_vs_proto_table[i]; pp; pp = pp->next) { | |
114 | if (pp->timeout_change) | |
115 | pp->timeout_change(pp, flags); | |
116 | } | |
117 | } | |
118 | } | |
119 | ||
120 | ||
121 | int * | |
122 | ip_vs_create_timeout_table(int *table, int size) | |
123 | { | |
8b2ed4bb | 124 | return kmemdup(table, size, GFP_ATOMIC); |
1da177e4 LT |
125 | } |
126 | ||
127 | ||
128 | /* | |
129 | * Set timeout value for state specified by name | |
130 | */ | |
131 | int | |
36cbd3dc JE |
132 | ip_vs_set_state_timeout(int *table, int num, const char *const *names, |
133 | const char *name, int to) | |
1da177e4 LT |
134 | { |
135 | int i; | |
136 | ||
137 | if (!table || !name || !to) | |
138 | return -EINVAL; | |
139 | ||
140 | for (i = 0; i < num; i++) { | |
141 | if (strcmp(names[i], name)) | |
142 | continue; | |
143 | table[i] = to * HZ; | |
144 | return 0; | |
145 | } | |
146 | return -ENOENT; | |
147 | } | |
148 | ||
149 | ||
150 | const char * ip_vs_state_name(__u16 proto, int state) | |
151 | { | |
152 | struct ip_vs_protocol *pp = ip_vs_proto_get(proto); | |
153 | ||
154 | if (pp == NULL || pp->state_name == NULL) | |
2ad17def | 155 | return (IPPROTO_IP == proto) ? "NONE" : "ERR!"; |
1da177e4 LT |
156 | return pp->state_name(state); |
157 | } | |
158 | ||
159 | ||
77eb8516 | 160 | static void |
3b047d9d JV |
161 | ip_vs_tcpudp_debug_packet_v4(struct ip_vs_protocol *pp, |
162 | const struct sk_buff *skb, | |
163 | int offset, | |
164 | const char *msg) | |
1da177e4 LT |
165 | { |
166 | char buf[128]; | |
167 | struct iphdr _iph, *ih; | |
168 | ||
169 | ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); | |
170 | if (ih == NULL) | |
3d91c1a8 | 171 | sprintf(buf, "TRUNCATED"); |
5661df7b | 172 | else if (ih->frag_off & htons(IP_OFFSET)) |
3d91c1a8 | 173 | sprintf(buf, "%pI4->%pI4 frag", &ih->saddr, &ih->daddr); |
1da177e4 | 174 | else { |
0d79641a JA |
175 | __be16 _ports[2], *pptr; |
176 | ||
1da177e4 LT |
177 | pptr = skb_header_pointer(skb, offset + ih->ihl*4, |
178 | sizeof(_ports), _ports); | |
179 | if (pptr == NULL) | |
3d91c1a8 PM |
180 | sprintf(buf, "TRUNCATED %pI4->%pI4", |
181 | &ih->saddr, &ih->daddr); | |
1da177e4 | 182 | else |
3d91c1a8 | 183 | sprintf(buf, "%pI4:%u->%pI4:%u", |
14d5e834 HH |
184 | &ih->saddr, ntohs(pptr[0]), |
185 | &ih->daddr, ntohs(pptr[1])); | |
1da177e4 LT |
186 | } |
187 | ||
3d91c1a8 | 188 | pr_debug("%s: %s %s\n", msg, pp->name, buf); |
1da177e4 LT |
189 | } |
190 | ||
3b047d9d | 191 | #ifdef CONFIG_IP_VS_IPV6 |
77eb8516 | 192 | static void |
3b047d9d JV |
193 | ip_vs_tcpudp_debug_packet_v6(struct ip_vs_protocol *pp, |
194 | const struct sk_buff *skb, | |
195 | int offset, | |
196 | const char *msg) | |
197 | { | |
198 | char buf[192]; | |
199 | struct ipv6hdr _iph, *ih; | |
200 | ||
201 | ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); | |
202 | if (ih == NULL) | |
3d91c1a8 | 203 | sprintf(buf, "TRUNCATED"); |
3b047d9d | 204 | else if (ih->nexthdr == IPPROTO_FRAGMENT) |
3d91c1a8 | 205 | sprintf(buf, "%pI6->%pI6 frag", &ih->saddr, &ih->daddr); |
3b047d9d JV |
206 | else { |
207 | __be16 _ports[2], *pptr; | |
208 | ||
209 | pptr = skb_header_pointer(skb, offset + sizeof(struct ipv6hdr), | |
210 | sizeof(_ports), _ports); | |
211 | if (pptr == NULL) | |
3d91c1a8 PM |
212 | sprintf(buf, "TRUNCATED %pI6->%pI6", |
213 | &ih->saddr, &ih->daddr); | |
3b047d9d | 214 | else |
3d91c1a8 | 215 | sprintf(buf, "%pI6:%u->%pI6:%u", |
38ff4fa4 HH |
216 | &ih->saddr, ntohs(pptr[0]), |
217 | &ih->daddr, ntohs(pptr[1])); | |
3b047d9d JV |
218 | } |
219 | ||
3d91c1a8 | 220 | pr_debug("%s: %s %s\n", msg, pp->name, buf); |
3b047d9d JV |
221 | } |
222 | #endif | |
223 | ||
224 | ||
225 | void | |
0d79641a | 226 | ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp, |
3b047d9d JV |
227 | const struct sk_buff *skb, |
228 | int offset, | |
229 | const char *msg) | |
230 | { | |
231 | #ifdef CONFIG_IP_VS_IPV6 | |
0d79641a | 232 | if (af == AF_INET6) |
3b047d9d JV |
233 | ip_vs_tcpudp_debug_packet_v6(pp, skb, offset, msg); |
234 | else | |
235 | #endif | |
236 | ip_vs_tcpudp_debug_packet_v4(pp, skb, offset, msg); | |
237 | } | |
238 | ||
1da177e4 | 239 | |
048cf48b | 240 | int __init ip_vs_protocol_init(void) |
1da177e4 LT |
241 | { |
242 | char protocols[64]; | |
243 | #define REGISTER_PROTOCOL(p) \ | |
244 | do { \ | |
245 | register_ip_vs_protocol(p); \ | |
246 | strcat(protocols, ", "); \ | |
247 | strcat(protocols, (p)->name); \ | |
248 | } while (0) | |
249 | ||
250 | protocols[0] = '\0'; | |
251 | protocols[2] = '\0'; | |
252 | #ifdef CONFIG_IP_VS_PROTO_TCP | |
253 | REGISTER_PROTOCOL(&ip_vs_protocol_tcp); | |
254 | #endif | |
255 | #ifdef CONFIG_IP_VS_PROTO_UDP | |
256 | REGISTER_PROTOCOL(&ip_vs_protocol_udp); | |
257 | #endif | |
2906f66a VMR |
258 | #ifdef CONFIG_IP_VS_PROTO_SCTP |
259 | REGISTER_PROTOCOL(&ip_vs_protocol_sctp); | |
260 | #endif | |
1da177e4 LT |
261 | #ifdef CONFIG_IP_VS_PROTO_AH |
262 | REGISTER_PROTOCOL(&ip_vs_protocol_ah); | |
263 | #endif | |
264 | #ifdef CONFIG_IP_VS_PROTO_ESP | |
265 | REGISTER_PROTOCOL(&ip_vs_protocol_esp); | |
266 | #endif | |
1e3e238e | 267 | pr_info("Registered protocols (%s)\n", &protocols[2]); |
1da177e4 LT |
268 | |
269 | return 0; | |
270 | } | |
271 | ||
272 | ||
273 | void ip_vs_protocol_cleanup(void) | |
274 | { | |
275 | struct ip_vs_protocol *pp; | |
276 | int i; | |
277 | ||
278 | /* unregister all the ipvs protocols */ | |
279 | for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) { | |
280 | while ((pp = ip_vs_proto_table[i]) != NULL) | |
281 | unregister_ip_vs_protocol(pp); | |
282 | } | |
283 | } |