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