]>
Commit | Line | Data |
---|---|---|
0b0cfcf2 | 1 | |
2 | /* | |
2adffc6f | 3 | * $Id: wccp2.cc,v 1.22 2008/02/05 22:39:42 amosjeffries Exp $ |
0b0cfcf2 | 4 | * |
5 | * DEBUG: section 80 WCCP Support | |
47c151e0 | 6 | * AUTHOR: Steven Wilton |
0b0cfcf2 | 7 | * |
8 | * SQUID Web Proxy Cache http://www.squid-cache.org/ | |
9 | * ---------------------------------------------------------- | |
10 | * | |
11 | * Squid is the result of efforts by numerous individuals from | |
12 | * the Internet community; see the CONTRIBUTORS file for full | |
13 | * details. Many organizations have provided support for Squid's | |
14 | * development; see the SPONSORS file for full details. Squid is | |
15 | * Copyrighted (C) 2001 by the Regents of the University of | |
16 | * California; see the COPYRIGHT file for full details. Squid | |
17 | * incorporates software developed and/or copyrighted by other | |
18 | * sources; see the CREDITS file for full details. | |
19 | * | |
20 | * This program is free software; you can redistribute it and/or modify | |
21 | * it under the terms of the GNU General Public License as published by | |
22 | * the Free Software Foundation; either version 2 of the License, or | |
23 | * (at your option) any later version. | |
24 | * | |
25 | * This program is distributed in the hope that it will be useful, | |
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
28 | * GNU General Public License for more details. | |
29 | * | |
30 | * You should have received a copy of the GNU General Public License | |
31 | * along with this program; if not, write to the Free Software | |
32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
33 | * | |
34 | */ | |
bf8fe701 | 35 | |
0b0cfcf2 | 36 | #include "squid.h" |
37 | #include "comm.h" | |
a553a5a3 | 38 | #include "event.h" |
0b0cfcf2 | 39 | #include "Parsing.h" |
a553a5a3 | 40 | #include "Store.h" |
bef81ea5 | 41 | #include "SwapDir.h" |
cc192b50 | 42 | #include "IPAddress.h" |
0b0cfcf2 | 43 | |
44 | #if USE_WCCPv2 | |
45 | #include <netdb.h> | |
46 | ||
47 | #define WCCP_PORT 2048 | |
0b0cfcf2 | 48 | #define WCCP_RESPONSE_SIZE 12448 |
0b0cfcf2 | 49 | #define WCCP_BUCKETS 256 |
50 | ||
0b0cfcf2 | 51 | static int theWccp2Connection = -1; |
52 | static int wccp2_connected = 0; | |
53 | ||
54 | static PF wccp2HandleUdp; | |
55 | static EVH wccp2HereIam; | |
56 | static EVH wccp2AssignBuckets; | |
57 | ||
58 | /* KDW WCCP V2 */ | |
59 | #define WCCP2_HERE_I_AM 10 | |
60 | #define WCCP2_I_SEE_YOU 11 | |
61 | #define WCCP2_REDIRECT_ASSIGN 12 | |
62 | #define WCCP2_REMOVAL_QUERY 13 | |
c1f55201 | 63 | |
0b0cfcf2 | 64 | #define WCCP2_VERSION 0x200 |
65 | ||
66 | #define WCCP2_SECURITY_INFO 0 | |
67 | #define WCCP2_NO_SECURITY 0 | |
68 | #define WCCP2_MD5_SECURITY 1 | |
69 | ||
70 | #define WCCP2_SERVICE_INFO 1 | |
71 | #define WCCP2_SERVICE_STANDARD 0 | |
72 | #define WCCP2_SERVICE_DYNAMIC 1 | |
73 | #define WCCP2_SERVICE_ID_HTTP 0x00 | |
74 | ||
75 | #define WCCP2_SERVICE_SRC_IP_HASH 0x1 | |
76 | #define WCCP2_SERVICE_DST_IP_HASH 0x2 | |
b7d249f9 | 77 | #define WCCP2_SERVICE_SRC_PORT_HASH 0x4 |
0b0cfcf2 | 78 | #define WCCP2_SERVICE_DST_PORT_HASH 0x8 |
79 | #define WCCP2_SERVICE_PORTS_DEFINED 0x10 | |
80 | #define WCCP2_SERVICE_PORTS_SOURCE 0x20 | |
81 | #define WCCP2_SERVICE_SRC_IP_ALT_HASH 0x100 | |
82 | #define WCCP2_SERVICE_DST_IP_ALT_HASH 0x200 | |
83 | #define WCCP2_SERVICE_SRC_PORT_ALT_HASH 0x400 | |
84 | #define WCCP2_SERVICE_DST_PORT_ALT_HASH 0x800 | |
85 | ||
86 | #define WCCP2_ROUTER_ID_INFO 2 | |
87 | ||
88 | #define WCCP2_WC_ID_INFO 3 | |
89 | ||
90 | #define WCCP2_RTR_VIEW_INFO 4 | |
91 | ||
92 | #define WCCP2_WC_VIEW_INFO 5 | |
93 | ||
94 | #define WCCP2_REDIRECT_ASSIGNMENT 6 | |
95 | ||
96 | #define WCCP2_QUERY_INFO 7 | |
97 | ||
98 | #define WCCP2_CAPABILITY_INFO 8 | |
99 | ||
b7d249f9 | 100 | #define WCCP2_ALT_ASSIGNMENT 13 |
101 | ||
102 | #define WCCP2_ASSIGN_MAP 14 | |
103 | ||
104 | #define WCCP2_COMMAND_EXTENSION 15 | |
105 | ||
0b0cfcf2 | 106 | #define WCCP2_CAPABILITY_FORWARDING_METHOD 0x01 |
107 | #define WCCP2_CAPABILITY_ASSIGNMENT_METHOD 0x02 | |
108 | #define WCCP2_CAPABILITY_RETURN_METHOD 0x03 | |
109 | ||
c1f55201 | 110 | #define WCCP2_FORWARDING_METHOD_GRE 0x00000001 |
111 | #define WCCP2_FORWARDING_METHOD_L2 0x00000002 | |
112 | ||
113 | #define WCCP2_ASSIGNMENT_METHOD_HASH 0x00000001 | |
114 | #define WCCP2_ASSIGNMENT_METHOD_MASK 0x00000002 | |
115 | ||
116 | #define WCCP2_PACKET_RETURN_METHOD_GRE 0x00000001 | |
117 | #define WCCP2_PACKET_RETURN_METHOD_L2 0x00000002 | |
0b0cfcf2 | 118 | |
b7d249f9 | 119 | #define WCCP2_HASH_ASSIGNMENT 0x00 |
120 | #define WCCP2_MASK_ASSIGNMENT 0x01 | |
121 | ||
0b0cfcf2 | 122 | #define WCCP2_NONE_SECURITY_LEN 0 |
123 | #define WCCP2_MD5_SECURITY_LEN 16 | |
124 | ||
125 | /* Useful defines */ | |
126 | #define WCCP2_NUMPORTS 8 | |
127 | #define WCCP2_PASSWORD_LEN 8 | |
128 | ||
129 | /* WCCP v2 packet header */ | |
130 | ||
cc192b50 | 131 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 132 | struct wccp2_here_i_am_header_t |
133 | { | |
134 | uint32_t type; | |
135 | uint16_t version; | |
136 | uint16_t length; | |
137 | }; | |
138 | ||
139 | static struct wccp2_here_i_am_header_t wccp2_here_i_am_header; | |
140 | ||
141 | /* Security struct for the "no security" option */ | |
142 | ||
143 | struct wccp2_security_none_t | |
144 | { | |
145 | uint16_t security_type; | |
146 | uint16_t security_length; | |
147 | uint32_t security_option; | |
148 | }; | |
149 | ||
cc192b50 | 150 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 151 | struct wccp2_security_md5_t |
152 | { | |
153 | uint16_t security_type; | |
154 | uint16_t security_length; | |
155 | uint32_t security_option; | |
156 | uint8_t security_implementation[WCCP2_MD5_SECURITY_LEN]; | |
157 | }; | |
158 | ||
159 | /* Service info struct */ | |
160 | ||
cc192b50 | 161 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 162 | struct wccp2_service_info_t |
163 | { | |
164 | uint16_t service_type; | |
165 | uint16_t service_length; | |
166 | uint8_t service; | |
167 | uint8_t service_id; | |
168 | uint8_t service_priority; | |
169 | uint8_t service_protocol; | |
170 | uint32_t service_flags; | |
171 | uint16_t port0; | |
172 | uint16_t port1; | |
173 | uint16_t port2; | |
174 | uint16_t port3; | |
175 | uint16_t port4; | |
176 | uint16_t port5; | |
177 | uint16_t port6; | |
178 | uint16_t port7; | |
179 | }; | |
180 | ||
cc192b50 | 181 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 182 | struct wccp2_cache_identity_info_t |
183 | { | |
cc192b50 | 184 | struct in_addr addr; |
0b0cfcf2 | 185 | uint16_t hash_revision; |
186 | char bits[2]; | |
187 | char buckets[32]; | |
188 | uint16_t weight; | |
189 | uint16_t status; | |
190 | }; | |
191 | ||
192 | /* Web Cache identity info */ | |
193 | ||
cc192b50 | 194 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 195 | struct wccp2_identity_info_t |
196 | { | |
197 | uint16_t cache_identity_type; | |
198 | uint16_t cache_identity_length; | |
199 | ||
200 | struct wccp2_cache_identity_info_t cache_identity; | |
201 | }; | |
202 | ||
203 | static struct wccp2_identity_info_t wccp2_identity_info; | |
204 | ||
cc192b50 | 205 | /// \interface WCCPv2 Protocol |
b7d249f9 | 206 | struct wccp2_cache_mask_identity_info_t |
207 | { | |
cc192b50 | 208 | struct in_addr addr; |
b7d249f9 | 209 | uint32_t num1; |
210 | uint32_t num2; | |
211 | uint32_t source_ip_mask; | |
212 | uint32_t dest_ip_mask; | |
213 | uint16_t source_port_mask; | |
214 | uint16_t dest_port_mask; | |
215 | uint32_t num3; | |
216 | uint32_t num4; | |
217 | }; | |
218 | ||
219 | /* Web Cache identity info */ | |
220 | ||
cc192b50 | 221 | /// \interface WCCPv2 Protocol |
b7d249f9 | 222 | struct wccp2_mask_identity_info_t |
223 | { | |
224 | uint16_t cache_identity_type; | |
225 | uint16_t cache_identity_length; | |
226 | ||
227 | struct wccp2_cache_mask_identity_info_t cache_identity; | |
228 | }; | |
229 | ||
230 | static struct wccp2_mask_identity_info_t wccp2_mask_identity_info; | |
231 | ||
0b0cfcf2 | 232 | /* View header */ |
233 | ||
cc192b50 | 234 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 235 | struct wccp2_cache_view_header_t |
236 | { | |
237 | uint16_t cache_view_type; | |
238 | uint16_t cache_view_length; | |
239 | uint32_t cache_view_version; | |
240 | }; | |
241 | ||
242 | static struct wccp2_cache_view_header_t wccp2_cache_view_header; | |
243 | ||
244 | /* View info */ | |
245 | ||
cc192b50 | 246 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 247 | struct wccp2_cache_view_info_t |
248 | { | |
249 | uint32_t num_routers; | |
250 | uint32_t num_caches; | |
251 | }; | |
252 | ||
253 | static struct wccp2_cache_view_info_t wccp2_cache_view_info; | |
254 | ||
255 | /* Router ID element */ | |
256 | ||
cc192b50 | 257 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 258 | struct wccp2_router_id_element_t |
259 | { | |
cc192b50 | 260 | struct in_addr router_address; |
0b0cfcf2 | 261 | uint32_t received_id; |
262 | }; | |
263 | ||
264 | static struct wccp2_router_id_element_t wccp2_router_id_element; | |
265 | ||
266 | /* Capability info header */ | |
267 | ||
cc192b50 | 268 | /// \interface WCCpv2 Protocol |
0b0cfcf2 | 269 | struct wccp2_capability_info_header_t |
270 | { | |
271 | uint16_t capability_info_type; | |
272 | uint16_t capability_info_length; | |
273 | }; | |
274 | ||
275 | static struct wccp2_capability_info_header_t wccp2_capability_info_header; | |
276 | ||
c1f55201 | 277 | /* Capability element header */ |
278 | ||
cc192b50 | 279 | /// \interface WCCPv2 Protocol |
c1f55201 | 280 | struct wccp2_capability_element_header_t |
281 | { | |
282 | uint16_t capability_type; | |
283 | uint16_t capability_length; | |
284 | }; | |
285 | ||
0b0cfcf2 | 286 | /* Capability element */ |
287 | ||
cc192b50 | 288 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 289 | struct wccp2_capability_element_t |
290 | { | |
291 | uint16_t capability_type; | |
292 | uint16_t capability_length; | |
293 | uint32_t capability_value; | |
294 | }; | |
295 | ||
296 | static struct wccp2_capability_element_t wccp2_capability_element; | |
297 | ||
b7d249f9 | 298 | /* Mask Element */ |
299 | ||
cc192b50 | 300 | /// \interface WCCPv2 Protocol |
b7d249f9 | 301 | struct wccp2_mask_element_t |
302 | { | |
303 | uint32_t source_ip_mask; | |
304 | uint32_t dest_ip_mask; | |
305 | uint16_t source_port_mask; | |
306 | uint16_t dest_port_mask; | |
307 | uint32_t number_values; | |
308 | }; | |
309 | ||
310 | /* Value Element */ | |
311 | ||
cc192b50 | 312 | /// \interface WCCPv2 Protocol |
b7d249f9 | 313 | struct wccp2_value_element_t |
314 | { | |
315 | uint32_t source_ip_value; | |
316 | uint32_t dest_ip_value; | |
317 | uint16_t source_port_value; | |
318 | uint16_t dest_port_value; | |
319 | ||
cc192b50 | 320 | struct in_addr cache_ip; |
b7d249f9 | 321 | }; |
0b0cfcf2 | 322 | |
323 | /* RECEIVED PACKET STRUCTURE */ | |
324 | ||
cc192b50 | 325 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 326 | struct wccp2_i_see_you_t |
327 | { | |
328 | uint32_t type; | |
329 | uint16_t version; | |
330 | uint16_t length; | |
331 | char data[WCCP_RESPONSE_SIZE]; | |
332 | }; | |
333 | ||
334 | static struct wccp2_i_see_you_t wccp2_i_see_you; | |
335 | ||
336 | /* Router ID element */ | |
337 | ||
cc192b50 | 338 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 339 | struct wccp2_router_assign_element_t |
340 | { | |
cc192b50 | 341 | struct in_addr router_address; |
0b0cfcf2 | 342 | uint32_t received_id; |
343 | uint32_t change_number; | |
344 | }; | |
345 | ||
346 | /* Generic header struct */ | |
347 | ||
cc192b50 | 348 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 349 | struct wccp2_item_header_t |
350 | { | |
351 | uint16_t type; | |
352 | uint16_t length; | |
353 | }; | |
354 | ||
355 | /* Router identity struct */ | |
356 | ||
cc192b50 | 357 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 358 | struct router_identity_info_t |
359 | { | |
360 | ||
361 | struct wccp2_item_header_t header; | |
362 | ||
363 | struct wccp2_router_id_element_t router_id_element; | |
364 | ||
2adffc6f | 365 | struct in_addr router_address; |
0b0cfcf2 | 366 | uint32_t number_caches; |
367 | }; | |
368 | ||
b7d249f9 | 369 | /* The received packet for a mask assignment is unusual */ |
370 | ||
cc192b50 | 371 | /// \interface WCCPv2 Protocol |
b7d249f9 | 372 | struct cache_mask_info_t |
373 | { | |
cc192b50 | 374 | struct in_addr addr; |
b7d249f9 | 375 | uint32_t num1; |
376 | uint32_t num2; | |
377 | uint32_t num3; | |
378 | }; | |
379 | ||
0b0cfcf2 | 380 | /* assigment key */ |
381 | ||
cc192b50 | 382 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 383 | struct assignment_key_t |
384 | { | |
cc192b50 | 385 | struct in_addr master_ip; |
0b0cfcf2 | 386 | uint32_t master_number; |
387 | }; | |
388 | ||
389 | /* Router view of WCCP */ | |
390 | ||
cc192b50 | 391 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 392 | struct router_view_t |
393 | { | |
394 | ||
395 | struct wccp2_item_header_t header; | |
396 | uint32_t change_number; | |
397 | ||
398 | struct assignment_key_t assignment_key; | |
399 | }; | |
400 | ||
0b0cfcf2 | 401 | /* Lists used to keep track of caches, routers and services */ |
402 | ||
cc192b50 | 403 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 404 | struct wccp2_cache_list_t |
405 | { | |
406 | ||
cc192b50 | 407 | struct in_addr cache_ip; |
0b0cfcf2 | 408 | |
f16fbc82 | 409 | int weight; |
410 | ||
0b0cfcf2 | 411 | struct wccp2_cache_list_t *next; |
412 | }; | |
413 | ||
cc192b50 | 414 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 415 | struct wccp2_router_list_t |
416 | { | |
417 | ||
418 | struct wccp2_router_id_element_t *info; | |
419 | ||
cc192b50 | 420 | struct in_addr local_ip; |
0b0cfcf2 | 421 | |
cc192b50 | 422 | struct in_addr router_sendto_address; |
0b0cfcf2 | 423 | uint32_t member_change; |
424 | uint32_t num_caches; | |
425 | ||
426 | struct wccp2_cache_list_t cache_list_head; | |
427 | ||
428 | struct wccp2_router_list_t *next; | |
429 | }; | |
430 | ||
431 | static int wccp2_numrouters; | |
432 | ||
cc192b50 | 433 | /// \interface WCCPv2 Protocol |
0b0cfcf2 | 434 | struct wccp2_service_list_t |
435 | { | |
436 | ||
437 | struct wccp2_service_info_t info; | |
438 | uint32_t num_routers; | |
439 | ||
440 | struct wccp2_router_list_t router_list_head; | |
441 | int lowest_ip; | |
442 | uint32_t change_num; | |
443 | ||
b7d249f9 | 444 | char *wccp2_identity_info_ptr; |
445 | ; | |
0b0cfcf2 | 446 | |
447 | struct wccp2_security_md5_t *security_info; | |
448 | ||
449 | struct wccp2_service_info_t *service_info; | |
450 | char wccp_packet[WCCP_RESPONSE_SIZE]; | |
451 | size_t wccp_packet_size; | |
452 | ||
453 | struct wccp2_service_list_t *next; | |
454 | char wccp_password[WCCP2_PASSWORD_LEN + 1]; /* hold the trailing C-string NUL */ | |
455 | uint32_t wccp2_security_type; | |
456 | }; | |
457 | ||
458 | static struct wccp2_service_list_t *wccp2_service_list_head = NULL; | |
459 | ||
460 | int empty_portlist[WCCP2_NUMPORTS] = | |
461 | {0, 0, 0, 0, 0, 0, 0, 0}; | |
462 | ||
463 | /* END WCCP V2 */ | |
464 | void wccp2_add_service_list(int service, int service_id, int service_priority, | |
465 | int service_proto, int service_flags, int ports[], int security_type, char *password); | |
466 | ||
467 | /* | |
468 | * The functions used during startup: | |
469 | * wccp2Init | |
470 | * wccp2ConnectionOpen | |
471 | * wccp2ConnectionClose | |
472 | */ | |
473 | ||
474 | static void | |
475 | wccp2InitServices(void) | |
476 | { | |
bf8fe701 | 477 | debugs(80, 5, "wccp2InitServices: called"); |
0b0cfcf2 | 478 | } |
479 | ||
480 | static void | |
481 | ||
482 | wccp2_update_service(struct wccp2_service_list_t *srv, int service, | |
483 | int service_id, int service_priority, int service_proto, int service_flags, | |
484 | int ports[]) | |
485 | { | |
486 | /* XXX check what needs to be wrapped in htons()! */ | |
487 | srv->info.service = service; | |
488 | srv->info.service_id = service_id; | |
489 | srv->info.service_priority = service_priority; | |
490 | srv->info.service_protocol = service_proto; | |
491 | srv->info.service_flags = htonl(service_flags); | |
492 | srv->info.port0 = htons(ports[0]); | |
493 | srv->info.port1 = htons(ports[1]); | |
494 | srv->info.port2 = htons(ports[2]); | |
495 | srv->info.port3 = htons(ports[3]); | |
496 | srv->info.port4 = htons(ports[4]); | |
497 | srv->info.port5 = htons(ports[5]); | |
498 | srv->info.port6 = htons(ports[6]); | |
499 | srv->info.port7 = htons(ports[7]); | |
500 | } | |
501 | ||
502 | void | |
503 | wccp2_add_service_list(int service, int service_id, int service_priority, | |
504 | int service_proto, int service_flags, int ports[], int security_type, | |
505 | char *password) | |
506 | { | |
507 | ||
508 | struct wccp2_service_list_t *wccp2_service_list_ptr; | |
509 | ||
510 | wccp2_service_list_ptr = (wccp2_service_list_t *) xcalloc(1, sizeof(struct wccp2_service_list_t)); | |
511 | ||
bf8fe701 | 512 | debugs(80, 5, "wccp2_add_service_list: added service id " << service_id); |
0b0cfcf2 | 513 | |
514 | /* XXX check what needs to be wrapped in htons()! */ | |
515 | wccp2_service_list_ptr->info.service_type = htons(WCCP2_SERVICE_INFO); | |
516 | ||
517 | wccp2_service_list_ptr->info.service_length = htons(sizeof(struct wccp2_service_info_t) - 4); | |
518 | wccp2_service_list_ptr->change_num = 0; | |
519 | wccp2_update_service(wccp2_service_list_ptr, service, service_id, | |
520 | service_priority, service_proto, service_flags, ports); | |
521 | wccp2_service_list_ptr->wccp2_security_type = security_type; | |
a302ad3c | 522 | memset(wccp2_service_list_ptr->wccp_password, 0, WCCP2_PASSWORD_LEN + 1); |
0b0cfcf2 | 523 | strncpy(wccp2_service_list_ptr->wccp_password, password, WCCP2_PASSWORD_LEN); |
524 | /* add to linked list - XXX this should use the Squid dlink* routines! */ | |
525 | wccp2_service_list_ptr->next = wccp2_service_list_head; | |
526 | wccp2_service_list_head = wccp2_service_list_ptr; | |
527 | } | |
528 | ||
529 | static struct wccp2_service_list_t * | |
530 | wccp2_get_service_by_id(int service, int service_id) | |
531 | { | |
532 | ||
533 | struct wccp2_service_list_t *p; | |
534 | ||
535 | p = wccp2_service_list_head; | |
536 | ||
537 | while (p != NULL) | |
538 | { | |
539 | if (p->info.service == service && p->info.service_id == service_id) { | |
540 | return p; | |
541 | } | |
542 | ||
543 | p = p->next; | |
544 | } | |
545 | ||
546 | return NULL; | |
547 | } | |
548 | ||
549 | /* | |
550 | * Update the md5 security header, if possible | |
551 | * | |
552 | * Returns: 1 if we set it, 0 if not (eg, no security section, or non-md5) | |
553 | */ | |
554 | static char | |
555 | wccp2_update_md5_security(char *password, char *ptr, char *packet, int len) | |
556 | { | |
557 | u_int8_t md5_digest[16]; | |
558 | char pwd[WCCP2_PASSWORD_LEN]; | |
c3031d67 | 559 | SquidMD5_CTX M; |
0b0cfcf2 | 560 | |
561 | struct wccp2_security_md5_t *ws; | |
562 | ||
bf8fe701 | 563 | debugs(80, 5, "wccp2_update_md5_security: called"); |
0b0cfcf2 | 564 | |
565 | /* The password field, for the MD5 hash, needs to be 8 bytes and NUL padded. */ | |
a302ad3c | 566 | memset(pwd, 0, sizeof(pwd)); |
0b0cfcf2 | 567 | strncpy(pwd, password, sizeof(pwd)); |
568 | ||
569 | ws = (struct wccp2_security_md5_t *) ptr; | |
570 | assert(ntohs(ws->security_type) == WCCP2_SECURITY_INFO); | |
571 | /* Its the security part */ | |
572 | ||
573 | if (ntohl(ws->security_option) != WCCP2_MD5_SECURITY) { | |
bf8fe701 | 574 | debugs(80, 5, "wccp2_update_md5_security: this service ain't md5'ing, abort"); |
0b0cfcf2 | 575 | return 0; |
576 | } | |
577 | ||
578 | /* And now its the MD5 section! */ | |
579 | /* According to the draft, the MD5 security hash is the combination of | |
580 | * the 8-octet password (padded w/ NUL bytes) and the entire WCCP packet, | |
581 | * including the WCCP message header. The WCCP security implementation | |
582 | * area should be zero'ed before calculating the MD5 hash. | |
583 | */ | |
584 | /* XXX eventually we should be able to kill md5_digest and blit it directly in */ | |
a302ad3c | 585 | memset(ws->security_implementation, 0, sizeof(ws->security_implementation)); |
0b0cfcf2 | 586 | |
c3031d67 | 587 | SquidMD5Init(&M); |
0b0cfcf2 | 588 | |
c3031d67 | 589 | SquidMD5Update(&M, pwd, 8); |
0b0cfcf2 | 590 | |
c3031d67 | 591 | SquidMD5Update(&M, packet, len); |
0b0cfcf2 | 592 | |
c3031d67 | 593 | SquidMD5Final(md5_digest, &M); |
0b0cfcf2 | 594 | |
595 | memcpy(ws->security_implementation, md5_digest, sizeof(md5_digest)); | |
596 | ||
597 | /* Finished! */ | |
598 | return 1; | |
599 | } | |
600 | ||
601 | ||
602 | /* | |
603 | * Check the given WCCP2 packet against the given password. | |
604 | */ | |
605 | static char | |
606 | ||
607 | wccp2_check_security(struct wccp2_service_list_t *srv, char *security, char *packet, int len) | |
608 | { | |
609 | ||
610 | struct wccp2_security_md5_t *ws = (struct wccp2_security_md5_t *) security; | |
611 | u_int8_t md5_digest[16], md5_challenge[16]; | |
612 | char pwd[WCCP2_PASSWORD_LEN]; | |
c3031d67 | 613 | SquidMD5_CTX M; |
0b0cfcf2 | 614 | |
615 | /* Make sure the security type matches what we expect */ | |
616 | ||
617 | if (ntohl(ws->security_option) != srv->wccp2_security_type) | |
618 | { | |
bf8fe701 | 619 | debugs(80, 1, "wccp2_check_security: received packet has the wrong security option"); |
0b0cfcf2 | 620 | return 0; |
621 | } | |
622 | ||
623 | if (srv->wccp2_security_type == WCCP2_NO_SECURITY) | |
624 | { | |
625 | return 1; | |
626 | } | |
627 | ||
628 | if (srv->wccp2_security_type != WCCP2_MD5_SECURITY) | |
629 | { | |
bf8fe701 | 630 | debugs(80, 1, "wccp2_check_security: invalid security option"); |
0b0cfcf2 | 631 | return 0; |
632 | } | |
633 | ||
634 | /* If execution makes it here then we have an MD5 security */ | |
635 | ||
636 | /* The password field, for the MD5 hash, needs to be 8 bytes and NUL padded. */ | |
a302ad3c | 637 | memset(pwd, 0, sizeof(pwd)); |
0b0cfcf2 | 638 | |
639 | strncpy(pwd, srv->wccp_password, sizeof(pwd)); | |
640 | ||
641 | /* Take a copy of the challenge: we need to NUL it before comparing */ | |
642 | memcpy(md5_challenge, ws->security_implementation, 16); | |
643 | ||
a302ad3c | 644 | memset(ws->security_implementation, 0, sizeof(ws->security_implementation)); |
0b0cfcf2 | 645 | |
c3031d67 | 646 | SquidMD5Init(&M); |
0b0cfcf2 | 647 | |
c3031d67 | 648 | SquidMD5Update(&M, pwd, 8); |
0b0cfcf2 | 649 | |
c3031d67 | 650 | SquidMD5Update(&M, packet, len); |
0b0cfcf2 | 651 | |
c3031d67 | 652 | SquidMD5Final(md5_digest, &M); |
0b0cfcf2 | 653 | |
654 | return (memcmp(md5_digest, md5_challenge, 16) == 0); | |
655 | } | |
656 | ||
657 | ||
658 | void | |
659 | wccp2Init(void) | |
660 | { | |
cc192b50 | 661 | IPAddress_list *s; |
0b0cfcf2 | 662 | char *ptr; |
b7d249f9 | 663 | uint32_t service_flags; |
0b0cfcf2 | 664 | |
665 | struct wccp2_service_list_t *service_list_ptr; | |
666 | ||
667 | struct wccp2_router_list_t *router_list_ptr; | |
668 | ||
669 | struct wccp2_security_md5_t wccp2_security_md5; | |
670 | ||
bf8fe701 | 671 | debugs(80, 5, "wccp2Init: Called"); |
0b0cfcf2 | 672 | |
673 | if (wccp2_connected == 1) | |
674 | return; | |
675 | ||
676 | wccp2_numrouters = 0; | |
677 | ||
678 | /* Calculate the number of routers configured in the config file */ | |
679 | for (s = Config.Wccp2.router; s; s = s->next) { | |
cc192b50 | 680 | if (!s->s.IsAnyAddr()) { |
0b0cfcf2 | 681 | /* Increment the counter */ |
682 | wccp2_numrouters++; | |
683 | } | |
684 | } | |
685 | ||
686 | if (wccp2_numrouters == 0) { | |
687 | return; | |
688 | } | |
689 | ||
690 | /* Initialise the list of services */ | |
691 | wccp2InitServices(); | |
692 | ||
693 | service_list_ptr = wccp2_service_list_head; | |
694 | ||
695 | while (service_list_ptr != NULL) { | |
696 | /* Set up our list pointers */ | |
697 | router_list_ptr = &service_list_ptr->router_list_head; | |
698 | ||
699 | /* start the wccp header */ | |
700 | wccp2_here_i_am_header.type = htonl(WCCP2_HERE_I_AM); | |
701 | wccp2_here_i_am_header.version = htons(WCCP2_VERSION); | |
702 | wccp2_here_i_am_header.length = 0; | |
703 | ptr = service_list_ptr->wccp_packet + sizeof(wccp2_here_i_am_header); | |
704 | ||
705 | /* add the security section */ | |
706 | /* XXX this is ugly */ | |
707 | ||
708 | if (service_list_ptr->wccp2_security_type == WCCP2_MD5_SECURITY) { | |
709 | wccp2_security_md5.security_option = htonl(WCCP2_MD5_SECURITY); | |
710 | ||
711 | wccp2_security_md5.security_length = htons(sizeof(struct wccp2_security_md5_t) - 4); | |
712 | } else if (service_list_ptr->wccp2_security_type == WCCP2_NO_SECURITY) { | |
713 | wccp2_security_md5.security_option = htonl(WCCP2_NO_SECURITY); | |
714 | /* XXX I hate magic length numbers! */ | |
715 | wccp2_security_md5.security_length = htons(4); | |
716 | } else { | |
717 | fatalf("Bad WCCP2 security type\n"); | |
718 | } | |
719 | ||
720 | wccp2_here_i_am_header.length += ntohs(wccp2_security_md5.security_length) + 4; | |
721 | assert(wccp2_here_i_am_header.length <= WCCP_RESPONSE_SIZE); | |
722 | wccp2_security_md5.security_type = htons(WCCP2_SECURITY_INFO); | |
723 | ||
724 | service_list_ptr->security_info = (struct wccp2_security_md5_t *) ptr; | |
725 | ||
726 | if (service_list_ptr->wccp2_security_type == WCCP2_MD5_SECURITY) { | |
727 | ||
728 | xmemcpy(ptr, &wccp2_security_md5, sizeof(struct wccp2_security_md5_t)); | |
729 | ||
730 | ptr += sizeof(struct wccp2_security_md5_t); | |
731 | } else { | |
732 | /* assume NONE, and XXX I hate magic length numbers */ | |
733 | xmemcpy(ptr, &wccp2_security_md5, 8); | |
734 | ptr += 8; | |
735 | } | |
736 | ||
737 | /* Add the service info section */ | |
738 | ||
739 | wccp2_here_i_am_header.length += sizeof(struct wccp2_service_info_t); | |
740 | ||
741 | assert(wccp2_here_i_am_header.length <= WCCP_RESPONSE_SIZE); | |
742 | ||
743 | xmemcpy(ptr, &service_list_ptr->info, sizeof(struct wccp2_service_info_t)); | |
744 | ||
745 | service_list_ptr->service_info = (struct wccp2_service_info_t *) ptr; | |
746 | ||
747 | ptr += sizeof(struct wccp2_service_info_t); | |
748 | ||
749 | /* Add the cache identity section */ | |
750 | ||
b7d249f9 | 751 | switch (Config.Wccp2.assignment_method) { |
0b0cfcf2 | 752 | |
b7d249f9 | 753 | case WCCP2_ASSIGNMENT_METHOD_HASH: |
0b0cfcf2 | 754 | |
b7d249f9 | 755 | wccp2_here_i_am_header.length += sizeof(struct wccp2_identity_info_t); |
756 | assert(wccp2_here_i_am_header.length <= WCCP_RESPONSE_SIZE); | |
757 | wccp2_identity_info.cache_identity_type = htons(WCCP2_WC_ID_INFO); | |
758 | wccp2_identity_info.cache_identity_length = htons(sizeof(wccp2_identity_info.cache_identity)); | |
cc192b50 | 759 | memset(&wccp2_identity_info.cache_identity.addr, '\0', sizeof(struct in_addr)); |
b7d249f9 | 760 | memset(&wccp2_identity_info.cache_identity.hash_revision, '\0', sizeof(wccp2_identity_info.cache_identity.hash_revision)); |
761 | memset(&wccp2_identity_info.cache_identity.bits, '\0', sizeof(wccp2_identity_info.cache_identity.bits)); | |
762 | memset(&wccp2_identity_info.cache_identity.buckets, '\0', sizeof(wccp2_identity_info.cache_identity.buckets)); | |
763 | wccp2_identity_info.cache_identity.weight = htons(Config.Wccp2.weight); | |
764 | memset(&wccp2_identity_info.cache_identity.status, '\0', sizeof(wccp2_identity_info.cache_identity.status)); | |
0b0cfcf2 | 765 | |
b7d249f9 | 766 | xmemcpy(ptr, &wccp2_identity_info, sizeof(struct wccp2_identity_info_t)); |
767 | service_list_ptr->wccp2_identity_info_ptr = ptr; | |
0b0cfcf2 | 768 | |
b7d249f9 | 769 | ptr += sizeof(struct wccp2_identity_info_t); |
770 | break; | |
0b0cfcf2 | 771 | |
b7d249f9 | 772 | case WCCP2_ASSIGNMENT_METHOD_MASK: |
773 | ||
774 | wccp2_here_i_am_header.length += sizeof(struct wccp2_mask_identity_info_t); | |
775 | assert(wccp2_here_i_am_header.length <= WCCP_RESPONSE_SIZE); | |
776 | wccp2_mask_identity_info.cache_identity_type = htons(WCCP2_WC_ID_INFO); | |
777 | wccp2_mask_identity_info.cache_identity_length = htons(sizeof(wccp2_mask_identity_info.cache_identity)); | |
cc192b50 | 778 | memset(&wccp2_mask_identity_info.cache_identity.addr, '\0', sizeof(struct in_addr)); |
b7d249f9 | 779 | wccp2_mask_identity_info.cache_identity.num1 = htonl(2); |
780 | wccp2_mask_identity_info.cache_identity.num2 = htonl(1); | |
781 | service_flags = ntohl(service_list_ptr->service_info->service_flags); | |
782 | ||
783 | if ((service_flags & WCCP2_SERVICE_SRC_IP_HASH) || (service_flags & WCCP2_SERVICE_SRC_IP_ALT_HASH)) { | |
784 | wccp2_mask_identity_info.cache_identity.source_ip_mask = htonl(0x00001741); | |
785 | wccp2_mask_identity_info.cache_identity.dest_ip_mask = 0; | |
786 | wccp2_mask_identity_info.cache_identity.source_port_mask = 0; | |
787 | wccp2_mask_identity_info.cache_identity.dest_port_mask = 0; | |
788 | } else if ((service_list_ptr->info.service == WCCP2_SERVICE_STANDARD) || (service_flags & WCCP2_SERVICE_DST_IP_HASH) || (service_flags & WCCP2_SERVICE_DST_IP_ALT_HASH)) { | |
789 | wccp2_mask_identity_info.cache_identity.source_ip_mask = 0; | |
790 | wccp2_mask_identity_info.cache_identity.dest_ip_mask = htonl(0x00001741); | |
791 | wccp2_mask_identity_info.cache_identity.source_port_mask = 0; | |
792 | wccp2_mask_identity_info.cache_identity.dest_port_mask = 0; | |
793 | } else if ((service_flags & WCCP2_SERVICE_SRC_PORT_HASH) || (service_flags & WCCP2_SERVICE_SRC_PORT_ALT_HASH)) { | |
794 | wccp2_mask_identity_info.cache_identity.source_ip_mask = 0; | |
795 | wccp2_mask_identity_info.cache_identity.dest_ip_mask = 0; | |
796 | wccp2_mask_identity_info.cache_identity.source_port_mask = htons(0x1741); | |
797 | wccp2_mask_identity_info.cache_identity.dest_port_mask = 0; | |
798 | } else if ((service_flags & WCCP2_SERVICE_DST_PORT_HASH) || (service_flags & WCCP2_SERVICE_DST_PORT_ALT_HASH)) { | |
799 | wccp2_mask_identity_info.cache_identity.source_ip_mask = 0; | |
800 | wccp2_mask_identity_info.cache_identity.dest_ip_mask = 0; | |
801 | wccp2_mask_identity_info.cache_identity.source_port_mask = 0; | |
802 | wccp2_mask_identity_info.cache_identity.dest_port_mask = htons(0x1741); | |
803 | } else { | |
804 | fatalf("Unknown service hash method\n"); | |
805 | } | |
0b0cfcf2 | 806 | |
b7d249f9 | 807 | wccp2_mask_identity_info.cache_identity.num3 = 0; |
808 | wccp2_mask_identity_info.cache_identity.num4 = 0; | |
0b0cfcf2 | 809 | |
b7d249f9 | 810 | xmemcpy(ptr, &wccp2_mask_identity_info, sizeof(struct wccp2_mask_identity_info_t)); |
811 | service_list_ptr->wccp2_identity_info_ptr = ptr; | |
0b0cfcf2 | 812 | |
b7d249f9 | 813 | ptr += sizeof(struct wccp2_mask_identity_info_t); |
814 | break; | |
0b0cfcf2 | 815 | |
b7d249f9 | 816 | default: |
817 | fatalf("Unknown Wccp2 assignment method\n"); | |
818 | } | |
0b0cfcf2 | 819 | |
820 | /* Add the cache view section */ | |
821 | wccp2_here_i_am_header.length += sizeof(wccp2_cache_view_header); | |
822 | ||
823 | assert(wccp2_here_i_am_header.length <= WCCP_RESPONSE_SIZE); | |
824 | ||
825 | wccp2_cache_view_header.cache_view_type = htons(WCCP2_WC_VIEW_INFO); | |
826 | ||
827 | wccp2_cache_view_header.cache_view_length = htons(sizeof(wccp2_cache_view_header) - 4 + | |
828 | sizeof(wccp2_cache_view_info) + (wccp2_numrouters * sizeof(wccp2_router_id_element))); | |
829 | ||
830 | wccp2_cache_view_header.cache_view_version = htonl(1); | |
831 | ||
832 | xmemcpy(ptr, &wccp2_cache_view_header, sizeof(wccp2_cache_view_header)); | |
833 | ||
834 | ptr += sizeof(wccp2_cache_view_header); | |
835 | ||
836 | /* Add the number of routers to the packet */ | |
837 | wccp2_here_i_am_header.length += sizeof(service_list_ptr->num_routers); | |
838 | ||
839 | assert(wccp2_here_i_am_header.length <= WCCP_RESPONSE_SIZE); | |
840 | ||
841 | service_list_ptr->num_routers = htonl(wccp2_numrouters); | |
842 | ||
843 | xmemcpy(ptr, &service_list_ptr->num_routers, sizeof(service_list_ptr->num_routers)); | |
844 | ||
845 | ptr += sizeof(service_list_ptr->num_routers); | |
846 | ||
847 | /* Add each router. Keep this functionality here to make sure the received_id can be updated in the packet */ | |
848 | for (s = Config.Wccp2.router; s; s = s->next) { | |
cc192b50 | 849 | if (!s->s.IsAnyAddr()) { |
0b0cfcf2 | 850 | |
851 | wccp2_here_i_am_header.length += sizeof(struct wccp2_router_id_element_t); | |
852 | assert(wccp2_here_i_am_header.length <= WCCP_RESPONSE_SIZE); | |
853 | ||
854 | /* Add a pointer to the router list for this router */ | |
855 | ||
856 | router_list_ptr->info = (struct wccp2_router_id_element_t *) ptr; | |
cc192b50 | 857 | s->s.GetInAddr(router_list_ptr->info->router_address); |
0b0cfcf2 | 858 | router_list_ptr->info->received_id = htonl(0); |
cc192b50 | 859 | s->s.GetInAddr(router_list_ptr->router_sendto_address); |
0b0cfcf2 | 860 | router_list_ptr->member_change = htonl(0); |
861 | ||
862 | /* Build the next struct */ | |
863 | ||
864 | router_list_ptr->next = (wccp2_router_list_t*) xcalloc(1, sizeof(struct wccp2_router_list_t)); | |
865 | ||
866 | /* update the pointer */ | |
867 | router_list_ptr = router_list_ptr->next; | |
868 | router_list_ptr->next = NULL; | |
869 | ||
870 | /* no need to copy memory - we've just set the values directly in the packet above */ | |
871 | ||
872 | ptr += sizeof(struct wccp2_router_id_element_t); | |
873 | } | |
874 | } | |
875 | ||
876 | /* Add the number of caches (0) */ | |
877 | wccp2_here_i_am_header.length += sizeof(wccp2_cache_view_info.num_caches); | |
878 | ||
879 | assert(wccp2_here_i_am_header.length <= WCCP_RESPONSE_SIZE); | |
880 | ||
881 | wccp2_cache_view_info.num_caches = htonl(0); | |
882 | ||
883 | xmemcpy(ptr, &wccp2_cache_view_info.num_caches, sizeof(wccp2_cache_view_info.num_caches)); | |
884 | ||
885 | ptr += sizeof(wccp2_cache_view_info.num_caches); | |
886 | ||
887 | /* Add the extra capability header */ | |
888 | wccp2_here_i_am_header.length += sizeof(wccp2_capability_info_header); | |
889 | ||
890 | assert(wccp2_here_i_am_header.length <= WCCP_RESPONSE_SIZE); | |
891 | ||
892 | wccp2_capability_info_header.capability_info_type = htons(WCCP2_CAPABILITY_INFO); | |
893 | ||
c1f55201 | 894 | wccp2_capability_info_header.capability_info_length = htons(3 * sizeof(wccp2_capability_element)); |
0b0cfcf2 | 895 | |
896 | xmemcpy(ptr, &wccp2_capability_info_header, sizeof(wccp2_capability_info_header)); | |
897 | ||
898 | ptr += sizeof(wccp2_capability_info_header); | |
899 | ||
900 | /* Add the forwarding method */ | |
901 | wccp2_here_i_am_header.length += sizeof(wccp2_capability_element); | |
902 | ||
903 | assert(wccp2_here_i_am_header.length <= WCCP_RESPONSE_SIZE); | |
904 | ||
905 | wccp2_capability_element.capability_type = htons(WCCP2_CAPABILITY_FORWARDING_METHOD); | |
906 | ||
907 | wccp2_capability_element.capability_length = htons(sizeof(wccp2_capability_element.capability_value)); | |
908 | ||
909 | wccp2_capability_element.capability_value = htonl(Config.Wccp2.forwarding_method); | |
910 | ||
911 | xmemcpy(ptr, &wccp2_capability_element, sizeof(wccp2_capability_element)); | |
912 | ||
913 | ptr += sizeof(wccp2_capability_element); | |
914 | ||
c1f55201 | 915 | /* Add the assignment method */ |
916 | wccp2_here_i_am_header.length += sizeof(wccp2_capability_element); | |
917 | ||
918 | assert(wccp2_here_i_am_header.length <= WCCP_RESPONSE_SIZE); | |
919 | ||
920 | wccp2_capability_element.capability_type = htons(WCCP2_CAPABILITY_ASSIGNMENT_METHOD); | |
921 | ||
922 | wccp2_capability_element.capability_length = htons(sizeof(wccp2_capability_element.capability_value)); | |
923 | ||
b7d249f9 | 924 | wccp2_capability_element.capability_value = htonl(Config.Wccp2.assignment_method); |
c1f55201 | 925 | |
926 | xmemcpy(ptr, &wccp2_capability_element, sizeof(wccp2_capability_element)); | |
927 | ||
928 | ptr += sizeof(wccp2_capability_element); | |
929 | ||
0b0cfcf2 | 930 | /* Add the return method */ |
931 | wccp2_here_i_am_header.length += sizeof(wccp2_capability_element); | |
932 | ||
933 | assert(wccp2_here_i_am_header.length <= WCCP_RESPONSE_SIZE); | |
934 | ||
935 | wccp2_capability_element.capability_type = htons(WCCP2_CAPABILITY_RETURN_METHOD); | |
936 | ||
937 | wccp2_capability_element.capability_length = htons(sizeof(wccp2_capability_element.capability_value)); | |
938 | ||
939 | wccp2_capability_element.capability_value = htonl(Config.Wccp2.return_method); | |
940 | ||
941 | xmemcpy(ptr, &wccp2_capability_element, sizeof(wccp2_capability_element)); | |
942 | ||
943 | ptr += sizeof(wccp2_capability_element); | |
944 | ||
945 | /* Finally, fix the total length to network order, and copy to the appropriate memory blob */ | |
946 | wccp2_here_i_am_header.length = htons(wccp2_here_i_am_header.length); | |
947 | ||
948 | memcpy(&service_list_ptr->wccp_packet, &wccp2_here_i_am_header, sizeof(wccp2_here_i_am_header)); | |
949 | ||
950 | service_list_ptr->wccp_packet_size = ntohs(wccp2_here_i_am_header.length) + sizeof(wccp2_here_i_am_header); | |
951 | ||
952 | /* Add the event if everything initialised correctly */ | |
953 | if (wccp2_numrouters) { | |
954 | if (!eventFind(wccp2HereIam, NULL)) { | |
955 | eventAdd("wccp2HereIam", wccp2HereIam, NULL, 1, 1); | |
956 | } | |
957 | } | |
958 | ||
959 | service_list_ptr = service_list_ptr->next; | |
960 | } | |
961 | } | |
962 | ||
963 | void | |
964 | wccp2ConnectionOpen(void) | |
965 | { | |
c1f55201 | 966 | struct sockaddr_in router, local, null; |
0b0cfcf2 | 967 | socklen_t local_len, router_len; |
968 | ||
969 | struct wccp2_service_list_t *service_list_ptr; | |
970 | ||
971 | struct wccp2_router_list_t *router_list_ptr; | |
972 | ||
bf8fe701 | 973 | debugs(80, 5, "wccp2ConnectionOpen: Called"); |
0b0cfcf2 | 974 | |
975 | if (wccp2_numrouters == 0 || !wccp2_service_list_head) { | |
bf8fe701 | 976 | debugs(80, 2, "WCCPv2 Disabled."); |
0b0cfcf2 | 977 | return; |
978 | } | |
979 | ||
cc192b50 | 980 | if( !Config.Wccp2.address.SetIPv4() ) { |
981 | debugs(80, 0, "WCCPv2 Disabled. " << Config.Wccp2.address << " is not an IPv4 address."); | |
982 | return; | |
983 | } | |
984 | ||
985 | Config.Wccp2.address.SetPort(WCCP_PORT); | |
0b0cfcf2 | 986 | theWccp2Connection = comm_open(SOCK_DGRAM, |
987 | 0, | |
988 | Config.Wccp2.address, | |
0b0cfcf2 | 989 | COMM_NONBLOCKING, |
990 | "WCCPv2 Socket"); | |
991 | ||
992 | if (theWccp2Connection < 0) | |
993 | fatal("Cannot open WCCP Port"); | |
994 | ||
85d25b95 | 995 | #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT) |
996 | { | |
997 | int i = IP_PMTUDISC_DONT; | |
998 | setsockopt(theWccp2Connection, SOL_IP, IP_MTU_DISCOVER, &i, sizeof i); | |
999 | } | |
1000 | ||
1001 | #endif | |
0b0cfcf2 | 1002 | commSetSelect(theWccp2Connection, |
1003 | COMM_SELECT_READ, | |
1004 | wccp2HandleUdp, | |
1005 | NULL, | |
1006 | 0); | |
1007 | ||
cc192b50 | 1008 | debugs(80, 1, "Accepting WCCPv2 messages on port " << WCCP_PORT << ", FD " << theWccp2Connection << "."); |
bf8fe701 | 1009 | debugs(80, 1, "Initialising all WCCPv2 lists"); |
0b0cfcf2 | 1010 | |
1011 | /* Initialise all routers on all services */ | |
c1f55201 | 1012 | memset(&null, 0, sizeof(null)); |
1013 | ||
1014 | null.sin_family = AF_UNSPEC; | |
1015 | ||
0b0cfcf2 | 1016 | service_list_ptr = wccp2_service_list_head; |
1017 | ||
1018 | while (service_list_ptr != NULL) { | |
1019 | for (router_list_ptr = &service_list_ptr->router_list_head; router_list_ptr->next != NULL; router_list_ptr = router_list_ptr->next) { | |
1020 | router_len = sizeof(router); | |
1021 | memset(&router, '\0', router_len); | |
1022 | router.sin_family = AF_INET; | |
cc192b50 | 1023 | router.sin_port = htons(WCCP_PORT); |
0b0cfcf2 | 1024 | router.sin_addr = router_list_ptr->router_sendto_address; |
1025 | ||
1026 | if (connect(theWccp2Connection, (struct sockaddr *) &router, router_len)) | |
1027 | fatal("Unable to connect WCCP out socket"); | |
1028 | ||
1029 | local_len = sizeof(local); | |
1030 | ||
1031 | memset(&local, '\0', local_len); | |
1032 | ||
1033 | if (getsockname(theWccp2Connection, (struct sockaddr *) &local, &local_len)) | |
1034 | fatal("Unable to getsockname on WCCP out socket"); | |
1035 | ||
1036 | router_list_ptr->local_ip = local.sin_addr; | |
1037 | ||
c1f55201 | 1038 | /* Disconnect the sending socket. Note: FreeBSD returns error |
1039 | * but disconnects anyway so we have to just assume it worked | |
1040 | */ | |
1041 | if (wccp2_numrouters > 1) | |
c1f55201 | 1042 | connect(theWccp2Connection, (struct sockaddr *) &null, router_len); |
0b0cfcf2 | 1043 | } |
1044 | ||
1045 | service_list_ptr = service_list_ptr->next; | |
1046 | } | |
1047 | ||
0b0cfcf2 | 1048 | wccp2_connected = 1; |
1049 | } | |
1050 | ||
1051 | void | |
1052 | wccp2ConnectionClose(void) | |
1053 | { | |
1054 | ||
1055 | struct wccp2_service_list_t *service_list_ptr; | |
1056 | ||
1057 | struct wccp2_service_list_t *service_list_ptr_next; | |
1058 | ||
1059 | struct wccp2_router_list_t *router_list_ptr; | |
1060 | ||
1061 | struct wccp2_router_list_t *router_list_next; | |
1062 | ||
1063 | struct wccp2_cache_list_t *cache_list_ptr; | |
1064 | ||
1065 | struct wccp2_cache_list_t *cache_list_ptr_next; | |
1066 | ||
1067 | if (wccp2_connected == 0) { | |
1068 | return; | |
1069 | } | |
1070 | ||
1071 | if (theWccp2Connection > -1) { | |
76dc4ca3 | 1072 | debugs(80, 1, "FD " << theWccp2Connection << " Closing WCCPv2 socket"); |
0b0cfcf2 | 1073 | comm_close(theWccp2Connection); |
1074 | theWccp2Connection = -1; | |
1075 | } | |
1076 | ||
1077 | /* for each router on each service send a packet */ | |
1078 | service_list_ptr = wccp2_service_list_head; | |
1079 | ||
1080 | while (service_list_ptr != NULL) { | |
1081 | for (router_list_ptr = &service_list_ptr->router_list_head; router_list_ptr != NULL; router_list_ptr = router_list_next) { | |
1082 | for (cache_list_ptr = &router_list_ptr->cache_list_head; cache_list_ptr; cache_list_ptr = cache_list_ptr_next) { | |
1083 | cache_list_ptr_next = cache_list_ptr->next; | |
1084 | ||
1085 | if (cache_list_ptr != &router_list_ptr->cache_list_head) { | |
1086 | xfree(cache_list_ptr); | |
1087 | } else { | |
1088 | ||
1089 | memset(cache_list_ptr, '\0', sizeof(struct wccp2_cache_list_t)); | |
1090 | } | |
1091 | } | |
1092 | ||
1093 | router_list_next = router_list_ptr->next; | |
1094 | ||
1095 | if (router_list_ptr != &service_list_ptr->router_list_head) { | |
1096 | xfree(router_list_ptr); | |
1097 | } else { | |
1098 | ||
1099 | memset(router_list_ptr, '\0', sizeof(struct wccp2_router_list_t)); | |
1100 | } | |
1101 | } | |
1102 | ||
1103 | service_list_ptr_next = service_list_ptr->next; | |
1104 | xfree(service_list_ptr); | |
1105 | service_list_ptr = service_list_ptr_next; | |
1106 | } | |
1107 | ||
1108 | wccp2_service_list_head = NULL; | |
1109 | eventDelete(wccp2HereIam, NULL); | |
1110 | eventDelete(wccp2AssignBuckets, NULL); | |
1111 | eventDelete(wccp2HereIam, NULL); | |
1112 | wccp2_connected = 0; | |
1113 | } | |
1114 | ||
1115 | /* | |
1116 | * Functions for handling the requests. | |
1117 | */ | |
1118 | ||
1119 | /* | |
1120 | * Accept the UDP packet | |
1121 | */ | |
1122 | static void | |
1123 | wccp2HandleUdp(int sock, void *not_used) | |
1124 | { | |
1125 | ||
1126 | struct wccp2_service_list_t *service_list_ptr; | |
1127 | ||
1128 | struct wccp2_router_list_t *router_list_ptr; | |
1129 | ||
1130 | struct wccp2_cache_list_t *cache_list_ptr; | |
1131 | ||
1132 | struct wccp2_cache_list_t *cache_list_ptr_next; | |
1133 | ||
1134 | /* These structs form the parts of the packet */ | |
1135 | ||
1136 | struct wccp2_item_header_t *header = NULL; | |
1137 | ||
1138 | struct wccp2_security_none_t *security_info = NULL; | |
1139 | ||
1140 | struct wccp2_service_info_t *service_info = NULL; | |
1141 | ||
1142 | struct router_identity_info_t *router_identity_info = NULL; | |
1143 | ||
1144 | struct router_view_t *router_view_header = NULL; | |
1145 | ||
b7d249f9 | 1146 | struct wccp2_cache_mask_identity_info_t *cache_mask_identity = NULL; |
1147 | ||
1148 | struct cache_mask_info_t *cache_mask_info = NULL; | |
1149 | ||
0b0cfcf2 | 1150 | struct wccp2_cache_identity_info_t *cache_identity = NULL; |
1151 | ||
1152 | struct wccp2_capability_info_header_t *router_capability_header = NULL; | |
1153 | ||
1154 | struct wccp2_capability_element_t *router_capability_element; | |
1155 | ||
1156 | struct sockaddr_in from; | |
1157 | ||
2adffc6f | 1158 | struct in_addr cache_address; |
0b0cfcf2 | 1159 | int len, found; |
1160 | short int data_length, offset; | |
1161 | uint32_t tmp; | |
1162 | char *ptr; | |
1163 | int num_caches; | |
0b0cfcf2 | 1164 | |
bf8fe701 | 1165 | debugs(80, 6, "wccp2HandleUdp: Called."); |
0b0cfcf2 | 1166 | |
1167 | commSetSelect(sock, COMM_SELECT_READ, wccp2HandleUdp, NULL, 0); | |
1168 | ||
cc192b50 | 1169 | /* FIXME INET6 : drop conversion boundary */ IPAddress from_tmp; |
0b0cfcf2 | 1170 | |
1171 | len = comm_udp_recvfrom(sock, | |
1172 | &wccp2_i_see_you, | |
1173 | WCCP_RESPONSE_SIZE, | |
1174 | 0, | |
cc192b50 | 1175 | from_tmp); |
1176 | /* FIXME INET6 : drop conversion boundary */ from_tmp.GetSockAddr(from); | |
0b0cfcf2 | 1177 | |
1178 | if (len < 0) | |
1179 | return; | |
1180 | ||
1181 | if (ntohs(wccp2_i_see_you.version) != WCCP2_VERSION) | |
1182 | return; | |
1183 | ||
1184 | if (ntohl(wccp2_i_see_you.type) != WCCP2_I_SEE_YOU) | |
1185 | return; | |
1186 | ||
bf8fe701 | 1187 | debugs(80, 3, "Incoming WCCPv2 I_SEE_YOU length " << ntohs(wccp2_i_see_you.length) << "."); |
0b0cfcf2 | 1188 | |
1189 | /* Record the total data length */ | |
1190 | data_length = ntohs(wccp2_i_see_you.length); | |
1191 | ||
1192 | offset = 0; | |
1193 | ||
1194 | if (data_length > len) { | |
bf8fe701 | 1195 | debugs(80, 1, "ERROR: Malformed WCCPv2 packet claiming it's bigger than received data"); |
0b0cfcf2 | 1196 | return; |
1197 | } | |
1198 | ||
1199 | /* Go through the data structure */ | |
1200 | while (data_length > offset) { | |
1201 | ||
1202 | header = (struct wccp2_item_header_t *) &wccp2_i_see_you.data[offset]; | |
1203 | ||
1204 | switch (ntohs(header->type)) { | |
1205 | ||
1206 | case WCCP2_SECURITY_INFO: | |
1207 | ||
1208 | if (security_info != NULL) { | |
bf8fe701 | 1209 | debugs(80, 1, "Duplicate security definition"); |
0b0cfcf2 | 1210 | return; |
1211 | } | |
1212 | ||
1213 | security_info = (struct wccp2_security_none_t *) &wccp2_i_see_you.data[offset]; | |
1214 | break; | |
1215 | ||
1216 | case WCCP2_SERVICE_INFO: | |
1217 | ||
1218 | if (service_info != NULL) { | |
bf8fe701 | 1219 | debugs(80, 1, "Duplicate service_info definition"); |
0b0cfcf2 | 1220 | return; |
1221 | } | |
1222 | ||
1223 | service_info = (struct wccp2_service_info_t *) &wccp2_i_see_you.data[offset]; | |
1224 | break; | |
1225 | ||
1226 | case WCCP2_ROUTER_ID_INFO: | |
1227 | ||
1228 | if (router_identity_info != NULL) { | |
bf8fe701 | 1229 | debugs(80, 1, "Duplicate router_identity_info definition"); |
0b0cfcf2 | 1230 | return; |
1231 | } | |
1232 | ||
1233 | router_identity_info = (struct router_identity_info_t *) &wccp2_i_see_you.data[offset]; | |
1234 | break; | |
1235 | ||
1236 | case WCCP2_RTR_VIEW_INFO: | |
1237 | ||
1238 | if (router_view_header != NULL) { | |
bf8fe701 | 1239 | debugs(80, 1, "Duplicate router_view definition"); |
0b0cfcf2 | 1240 | return; |
1241 | } | |
1242 | ||
1243 | router_view_header = (struct router_view_t *) &wccp2_i_see_you.data[offset]; | |
1244 | break; | |
1245 | ||
1246 | case WCCP2_CAPABILITY_INFO: | |
1247 | ||
1248 | if (router_capability_header != NULL) { | |
bf8fe701 | 1249 | debugs(80, 1, "Duplicate router_capability definition"); |
0b0cfcf2 | 1250 | return; |
1251 | } | |
1252 | ||
1253 | router_capability_header = (struct wccp2_capability_info_header_t *) &wccp2_i_see_you.data[offset]; | |
c1f55201 | 1254 | break; |
0b0cfcf2 | 1255 | |
b7d249f9 | 1256 | /* Nothing to do for the types below */ |
1257 | ||
1258 | case WCCP2_ASSIGN_MAP: | |
1259 | break; | |
1260 | ||
0b0cfcf2 | 1261 | default: |
bf8fe701 | 1262 | debugs(80, 1, "Unknown record type in WCCPv2 Packet (" << ntohs(header->type) << ")."); |
0b0cfcf2 | 1263 | } |
1264 | ||
1265 | offset += sizeof(struct wccp2_item_header_t); | |
1266 | offset += ntohs(header->length); | |
1267 | ||
1268 | if (offset > data_length) { | |
bf8fe701 | 1269 | debugs(80, 1, "Error: WCCPv2 packet tried to tell us there is data beyond the end of the packet"); |
0b0cfcf2 | 1270 | return; |
1271 | } | |
1272 | } | |
1273 | ||
1274 | if ((security_info == NULL) || (service_info == NULL) || (router_identity_info == NULL) || (router_view_header == NULL)) { | |
bf8fe701 | 1275 | debugs(80, 1, "Incomplete WCCPv2 Packet"); |
0b0cfcf2 | 1276 | return; |
1277 | } | |
1278 | ||
bf8fe701 | 1279 | debugs(80, 5, "Complete packet received"); |
0b0cfcf2 | 1280 | |
1281 | /* Check that the service in the packet is configured on this router */ | |
1282 | service_list_ptr = wccp2_service_list_head; | |
1283 | ||
1284 | while (service_list_ptr != NULL) { | |
1285 | if (service_info->service_id == service_list_ptr->service_info->service_id) { | |
1286 | break; | |
1287 | } | |
1288 | ||
1289 | service_list_ptr = service_list_ptr->next; | |
1290 | } | |
1291 | ||
1292 | if (service_list_ptr == NULL) { | |
bf8fe701 | 1293 | debugs(80, 1, "WCCPv2 Unknown service received from router (" << service_info->service_id << ")"); |
0b0cfcf2 | 1294 | return; |
1295 | } | |
1296 | ||
1297 | if (ntohl(security_info->security_option) != ntohl(service_list_ptr->security_info->security_option)) { | |
6013702c | 1298 | debugs(80, 1, "Invalid security option in WCCPv2 Packet (" << ntohl(security_info->security_option) << " vs " << ntohl(service_list_ptr->security_info->security_option) << ")."); |
0b0cfcf2 | 1299 | return; |
1300 | } | |
1301 | ||
1302 | if (!wccp2_check_security(service_list_ptr, (char *) security_info, (char *) &wccp2_i_see_you, len)) { | |
bf8fe701 | 1303 | debugs(80, 1, "Received WCCPv2 Packet failed authentication"); |
0b0cfcf2 | 1304 | return; |
1305 | } | |
1306 | ||
1307 | /* Check that the router address is configured on this router */ | |
1308 | for (router_list_ptr = &service_list_ptr->router_list_head; router_list_ptr->next != NULL; router_list_ptr = router_list_ptr->next) { | |
1309 | if (router_list_ptr->router_sendto_address.s_addr == from.sin_addr.s_addr) | |
1310 | break; | |
1311 | } | |
1312 | ||
1313 | if (router_list_ptr->next == NULL) { | |
bf8fe701 | 1314 | debugs(80, 1, "WCCPv2 Packet received from unknown router"); |
0b0cfcf2 | 1315 | return; |
1316 | } | |
1317 | ||
1318 | /* Set the router id */ | |
1319 | router_list_ptr->info->router_address = router_identity_info->router_id_element.router_address; | |
1320 | ||
1321 | /* Increment the received id in the packet */ | |
1322 | if (ntohl(router_list_ptr->info->received_id) != ntohl(router_identity_info->router_id_element.received_id)) { | |
6013702c | 1323 | debugs(80, 3, "Incoming WCCP2_I_SEE_YOU Received ID old=" << ntohl(router_list_ptr->info->received_id) << " new=" << ntohl(router_identity_info->router_id_element.received_id) << "."); |
0b0cfcf2 | 1324 | router_list_ptr->info->received_id = router_identity_info->router_id_element.received_id; |
1325 | } | |
1326 | ||
1327 | /* TODO: check return/forwarding methods */ | |
1328 | if (router_capability_header == NULL) { | |
c1f55201 | 1329 | if ((Config.Wccp2.return_method != WCCP2_PACKET_RETURN_METHOD_GRE) || (Config.Wccp2.forwarding_method != WCCP2_FORWARDING_METHOD_GRE)) { |
bf8fe701 | 1330 | debugs(80, 1, "wccp2HandleUdp: fatal error - A WCCP router does not support the forwarding method specified, only GRE supported"); |
0b0cfcf2 | 1331 | wccp2ConnectionClose(); |
1332 | return; | |
1333 | } | |
1334 | } else { | |
0b0cfcf2 | 1335 | |
c1f55201 | 1336 | char *end = ((char *) router_capability_header) + sizeof(*router_capability_header) + ntohs(router_capability_header->capability_info_length) - sizeof(struct wccp2_capability_info_header_t); |
1337 | ||
1338 | router_capability_element = (struct wccp2_capability_element_t *) (((char *) router_capability_header) + sizeof(*router_capability_header)); | |
0b0cfcf2 | 1339 | |
c1f55201 | 1340 | while ((char *) router_capability_element <= end) { |
0b0cfcf2 | 1341 | |
1342 | switch (ntohs(router_capability_element->capability_type)) { | |
1343 | ||
1344 | case WCCP2_CAPABILITY_FORWARDING_METHOD: | |
1345 | ||
c1f55201 | 1346 | if (!(ntohl(router_capability_element->capability_value) & Config.Wccp2.forwarding_method)) { |
6013702c | 1347 | debugs(80, 1, "wccp2HandleUdp: fatal error - A WCCP router has specified a different forwarding method " << ntohl(router_capability_element->capability_value) << ", expected " << Config.Wccp2.forwarding_method); |
0b0cfcf2 | 1348 | wccp2ConnectionClose(); |
1349 | return; | |
1350 | } | |
1351 | ||
1352 | break; | |
1353 | ||
1354 | case WCCP2_CAPABILITY_ASSIGNMENT_METHOD: | |
c1f55201 | 1355 | |
b7d249f9 | 1356 | if (!(ntohl(router_capability_element->capability_value) & Config.Wccp2.assignment_method)) { |
1357 | debugs(80, 1, "wccp2HandleUdp: fatal error - A WCCP router has specified a different assignment method " << ntohl(router_capability_element->capability_value) << ", expected "<< Config.Wccp2.assignment_method); | |
c1f55201 | 1358 | wccp2ConnectionClose(); |
1359 | return; | |
1360 | } | |
1361 | ||
0b0cfcf2 | 1362 | break; |
1363 | ||
1364 | case WCCP2_CAPABILITY_RETURN_METHOD: | |
1365 | ||
c1f55201 | 1366 | if (!(ntohl(router_capability_element->capability_value) & Config.Wccp2.return_method)) { |
6013702c | 1367 | debugs(80, 1, "wccp2HandleUdp: fatal error - A WCCP router has specified a different return method " << ntohl(router_capability_element->capability_value) << ", expected " << Config.Wccp2.return_method); |
0b0cfcf2 | 1368 | wccp2ConnectionClose(); |
1369 | return; | |
1370 | } | |
1371 | ||
1372 | break; | |
1373 | ||
1374 | default: | |
bf8fe701 | 1375 | debugs(80, 1, "Unknown capability type in WCCPv2 Packet (" << ntohs(router_capability_element->capability_type) << ")."); |
0b0cfcf2 | 1376 | } |
c1f55201 | 1377 | |
1378 | router_capability_element = (struct wccp2_capability_element_t *) (((char *) router_capability_element) + sizeof(struct wccp2_capability_element_header_t) + ntohs(router_capability_element->capability_length)); | |
0b0cfcf2 | 1379 | } |
1380 | } | |
1381 | ||
bf8fe701 | 1382 | debugs(80, 5, "Cleaning out cache list"); |
0b0cfcf2 | 1383 | /* clean out the old cache list */ |
1384 | ||
1385 | for (cache_list_ptr = &router_list_ptr->cache_list_head; cache_list_ptr; cache_list_ptr = cache_list_ptr_next) { | |
1386 | cache_list_ptr_next = cache_list_ptr->next; | |
1387 | ||
1388 | if (cache_list_ptr != &router_list_ptr->cache_list_head) { | |
1389 | xfree(cache_list_ptr); | |
1390 | } | |
1391 | } | |
1392 | ||
1393 | router_list_ptr->num_caches = htonl(0); | |
1394 | num_caches = 0; | |
1395 | ||
1396 | /* Check to see if we're the master cache and update the cache list */ | |
1397 | found = 0; | |
1398 | service_list_ptr->lowest_ip = 1; | |
1399 | cache_list_ptr = &router_list_ptr->cache_list_head; | |
1400 | ||
1401 | /* to find the list of caches, we start at the end of the router view header */ | |
1402 | ||
1403 | ptr = (char *) (router_view_header) + sizeof(struct router_view_t); | |
1404 | ||
1405 | /* Then we read the number of routers */ | |
1406 | memcpy(&tmp, ptr, sizeof(tmp)); | |
1407 | ||
1408 | /* skip the number plus all the ip's */ | |
1409 | ||
2adffc6f | 1410 | ptr += sizeof(tmp) + (ntohl(tmp) * sizeof(struct in_addr)); |
0b0cfcf2 | 1411 | |
1412 | /* Then read the number of caches */ | |
1413 | memcpy(&tmp, ptr, sizeof(tmp)); | |
1414 | ptr += sizeof(tmp); | |
1415 | ||
1416 | if (ntohl(tmp) != 0) { | |
1417 | /* search through the list of received-from ip addresses */ | |
1418 | ||
1419 | for (num_caches = 0; num_caches < (int) ntohl(tmp); num_caches++) { | |
1420 | /* Get a copy of the ip */ | |
1421 | ||
b7d249f9 | 1422 | switch (Config.Wccp2.assignment_method) { |
1423 | ||
1424 | case WCCP2_ASSIGNMENT_METHOD_HASH: | |
1425 | ||
1426 | cache_identity = (struct wccp2_cache_identity_info_t *) ptr; | |
1427 | ||
1428 | ptr += sizeof(struct wccp2_cache_identity_info_t); | |
1429 | ||
2adffc6f | 1430 | memcpy(&cache_address, &cache_identity->addr, sizeof(struct in_addr)); |
b7d249f9 | 1431 | |
1432 | cache_list_ptr->weight = ntohs(cache_identity->weight); | |
1433 | break; | |
1434 | ||
1435 | case WCCP2_ASSIGNMENT_METHOD_MASK: | |
1436 | ||
1437 | cache_mask_info = (struct cache_mask_info_t *) ptr; | |
1438 | ||
1439 | /* The mask assignment has an undocumented variable length entry here */ | |
1440 | ||
1441 | if (ntohl(cache_mask_info->num1) == 3) { | |
1442 | ||
1443 | cache_mask_identity = (struct wccp2_cache_mask_identity_info_t *) ptr; | |
1444 | ||
1445 | ptr += sizeof(struct wccp2_cache_mask_identity_info_t); | |
0b0cfcf2 | 1446 | |
2adffc6f | 1447 | memcpy(&cache_address, &cache_mask_identity->addr, sizeof(struct in_addr)); |
b7d249f9 | 1448 | } else { |
1449 | ||
1450 | ptr += sizeof(struct cache_mask_info_t); | |
1451 | ||
2adffc6f | 1452 | memcpy(&cache_address, &cache_mask_info->addr, sizeof(struct in_addr)); |
b7d249f9 | 1453 | } |
0b0cfcf2 | 1454 | |
b7d249f9 | 1455 | cache_list_ptr->weight = 0; |
1456 | break; | |
1457 | ||
1458 | default: | |
1459 | fatalf("Unknown Wccp2 assignment method\n"); | |
1460 | } | |
0b0cfcf2 | 1461 | |
1462 | /* Update the cache list */ | |
1463 | cache_list_ptr->cache_ip = cache_address; | |
b7d249f9 | 1464 | |
0b0cfcf2 | 1465 | cache_list_ptr->next = (wccp2_cache_list_t*) xcalloc(1, sizeof(struct wccp2_cache_list_t)); |
b7d249f9 | 1466 | |
0b0cfcf2 | 1467 | cache_list_ptr = cache_list_ptr->next; |
b7d249f9 | 1468 | |
0b0cfcf2 | 1469 | cache_list_ptr->next = NULL; |
1470 | ||
bf8fe701 | 1471 | debugs (80, 5, "checking cache list: (" << std::hex << cache_address.s_addr << ":" << router_list_ptr->local_ip.s_addr << ")"); |
b7d249f9 | 1472 | |
0b0cfcf2 | 1473 | /* Check to see if it's the master, or us */ |
1474 | ||
1475 | if (cache_address.s_addr == router_list_ptr->local_ip.s_addr) { | |
1476 | found = 1; | |
1477 | } | |
1478 | ||
1479 | if (cache_address.s_addr < router_list_ptr->local_ip.s_addr) { | |
1480 | service_list_ptr->lowest_ip = 0; | |
1481 | } | |
1482 | } | |
1483 | } else { | |
bf8fe701 | 1484 | debugs(80, 5, "Adding ourselves as the only cache"); |
0b0cfcf2 | 1485 | |
1486 | /* Update the cache list */ | |
1487 | cache_list_ptr->cache_ip = router_list_ptr->local_ip; | |
1488 | ||
1489 | cache_list_ptr->next = (wccp2_cache_list_t*) xcalloc(1, sizeof(struct wccp2_cache_list_t)); | |
1490 | cache_list_ptr = cache_list_ptr->next; | |
1491 | cache_list_ptr->next = NULL; | |
1492 | ||
b7d249f9 | 1493 | service_list_ptr->lowest_ip = 1; |
0b0cfcf2 | 1494 | found = 1; |
1495 | num_caches = 1; | |
1496 | } | |
1497 | ||
1498 | router_list_ptr->num_caches = htonl(num_caches); | |
1499 | ||
1500 | if ((found == 1) && (service_list_ptr->lowest_ip == 1)) { | |
1501 | if (ntohl(router_view_header->change_number) != router_list_ptr->member_change) { | |
bf8fe701 | 1502 | debugs(80, 4, "Change detected - queueing up new assignment"); |
0b0cfcf2 | 1503 | router_list_ptr->member_change = ntohl(router_view_header->change_number); |
c1f55201 | 1504 | eventDelete(wccp2AssignBuckets, NULL); |
1505 | eventAdd("wccp2AssignBuckets", wccp2AssignBuckets, NULL, 15.0, 1); | |
b7d249f9 | 1506 | } else { |
4a7a3d56 | 1507 | debugs(80, 5, "Change not detected (" << ntohl(router_view_header->change_number) << " = " << router_list_ptr->member_change << ")"); |
0b0cfcf2 | 1508 | } |
1509 | } else { | |
b7d249f9 | 1510 | eventDelete(wccp2AssignBuckets, NULL); |
bf8fe701 | 1511 | debugs(80, 5, "I am not the lowest ip cache - not assigning buckets"); |
0b0cfcf2 | 1512 | } |
1513 | } | |
1514 | ||
1515 | static void | |
1516 | wccp2HereIam(void *voidnotused) | |
1517 | { | |
1518 | ||
1519 | struct wccp2_service_list_t *service_list_ptr; | |
1520 | ||
1521 | struct wccp2_router_list_t *router_list_ptr; | |
1522 | ||
b7d249f9 | 1523 | struct wccp2_identity_info_t *wccp2_identity_info_ptr; |
1524 | ||
1525 | struct wccp2_mask_identity_info_t *wccp2_mask_identity_info_ptr; | |
1526 | ||
cc192b50 | 1527 | IPAddress router; |
0b0cfcf2 | 1528 | |
bf8fe701 | 1529 | debugs(80, 6, "wccp2HereIam: Called"); |
0b0cfcf2 | 1530 | |
1531 | if (wccp2_connected == 0) { | |
bf8fe701 | 1532 | debugs(80, 1, "wccp2HereIam: wccp2 socket closed. Shutting down WCCP2"); |
0b0cfcf2 | 1533 | return; |
1534 | } | |
1535 | ||
bef81ea5 | 1536 | /* Wait if store dirs are rebuilding */ |
1537 | if (StoreController::store_dirs_rebuilding && Config.Wccp2.rebuildwait) { | |
f67332d3 | 1538 | eventAdd("wccp2HereIam", wccp2HereIam, NULL, 1.0, 1); |
1539 | return; | |
1540 | } | |
1541 | ||
cc192b50 | 1542 | router.SetPort(WCCP_PORT); |
0b0cfcf2 | 1543 | |
1544 | /* for each router on each service send a packet */ | |
1545 | service_list_ptr = wccp2_service_list_head; | |
1546 | ||
1547 | while (service_list_ptr != NULL) { | |
bf8fe701 | 1548 | debugs(80, 5, "wccp2HereIam: sending to service id " << service_list_ptr->info.service_id); |
0b0cfcf2 | 1549 | |
1550 | for (router_list_ptr = &service_list_ptr->router_list_head; router_list_ptr->next != NULL; router_list_ptr = router_list_ptr->next) { | |
cc192b50 | 1551 | router = router_list_ptr->router_sendto_address; |
0b0cfcf2 | 1552 | |
1553 | /* Set the cache id (ip) */ | |
b7d249f9 | 1554 | |
1555 | switch (Config.Wccp2.assignment_method) { | |
1556 | ||
1557 | case WCCP2_ASSIGNMENT_METHOD_HASH: | |
1558 | ||
1559 | wccp2_identity_info_ptr = (struct wccp2_identity_info_t *) service_list_ptr->wccp2_identity_info_ptr; | |
1560 | wccp2_identity_info_ptr->cache_identity.addr = router_list_ptr->local_ip; | |
1561 | break; | |
1562 | ||
1563 | case WCCP2_ASSIGNMENT_METHOD_MASK: | |
1564 | ||
1565 | wccp2_mask_identity_info_ptr = (struct wccp2_mask_identity_info_t *) service_list_ptr->wccp2_identity_info_ptr; | |
1566 | wccp2_mask_identity_info_ptr->cache_identity.addr = router_list_ptr->local_ip; | |
1567 | break; | |
1568 | ||
1569 | default: | |
1570 | fatalf("Unknown Wccp2 assignment method\n"); | |
1571 | } | |
1572 | ||
0b0cfcf2 | 1573 | /* Security update, if needed */ |
1574 | ||
1575 | if (service_list_ptr->wccp2_security_type == WCCP2_MD5_SECURITY) { | |
1576 | wccp2_update_md5_security(service_list_ptr->wccp_password, (char *) service_list_ptr->security_info, service_list_ptr->wccp_packet, service_list_ptr->wccp_packet_size); | |
1577 | } | |
1578 | ||
4a7a3d56 | 1579 | debugs(80, 3, "Sending HereIam packet size " << service_list_ptr->wccp_packet_size); |
0b0cfcf2 | 1580 | /* Send the packet */ |
0b0cfcf2 | 1581 | |
c1f55201 | 1582 | if (wccp2_numrouters > 1) { |
1583 | comm_udp_sendto(theWccp2Connection, | |
cc192b50 | 1584 | router, |
c1f55201 | 1585 | &service_list_ptr->wccp_packet, |
1586 | service_list_ptr->wccp_packet_size); | |
1587 | } else { | |
1588 | send(theWccp2Connection, | |
1589 | &service_list_ptr->wccp_packet, | |
1590 | service_list_ptr->wccp_packet_size, | |
1591 | 0); | |
1592 | } | |
0b0cfcf2 | 1593 | } |
1594 | ||
1595 | service_list_ptr = service_list_ptr->next; | |
1596 | } | |
1597 | ||
f67332d3 | 1598 | eventAdd("wccp2HereIam", wccp2HereIam, NULL, 10.0, 1); |
0b0cfcf2 | 1599 | } |
1600 | ||
1601 | static void | |
1602 | wccp2AssignBuckets(void *voidnotused) | |
1603 | { | |
1604 | ||
1605 | struct wccp2_service_list_t *service_list_ptr; | |
1606 | ||
1607 | struct wccp2_router_list_t *router_list_ptr; | |
1608 | ||
1609 | struct wccp2_cache_list_t *cache_list_ptr; | |
1610 | char wccp_packet[WCCP_RESPONSE_SIZE]; | |
b7d249f9 | 1611 | short int offset, saved_offset, assignment_offset, alt_assignment_offset; |
0b0cfcf2 | 1612 | |
1613 | struct sockaddr_in router; | |
1614 | int router_len; | |
1615 | int bucket_counter; | |
b7d249f9 | 1616 | uint32_t service_flags; |
0b0cfcf2 | 1617 | u_short port = WCCP_PORT; |
1618 | ||
1619 | /* Packet segments */ | |
1620 | ||
1621 | struct wccp2_here_i_am_header_t *main_header; | |
1622 | ||
1623 | struct wccp2_security_md5_t *security = NULL; | |
1624 | /* service from service struct */ | |
1625 | ||
1626 | struct wccp2_item_header_t *assignment_header; | |
1627 | ||
b7d249f9 | 1628 | struct wccp2_item_header_t *alt_assignment_type_header = NULL; |
1629 | ||
0b0cfcf2 | 1630 | struct assignment_key_t *assignment_key; |
1631 | /* number of routers */ | |
1632 | ||
1633 | struct wccp2_router_assign_element_t *router_assign; | |
1634 | /* number of caches */ | |
1635 | ||
2adffc6f | 1636 | struct in_addr *cache_address; |
b7d249f9 | 1637 | /* Alternative assignement mask/values */ |
1638 | int num_maskval; | |
1639 | ||
1640 | struct wccp2_mask_element_t *mask_element; | |
1641 | ||
1642 | struct wccp2_value_element_t *value_element; | |
1643 | int valuecounter, value; | |
0b0cfcf2 | 1644 | char *buckets; |
1645 | ||
b7d249f9 | 1646 | assignment_offset = alt_assignment_offset = 0; |
1647 | ||
0b0cfcf2 | 1648 | router_len = sizeof(router); |
1649 | memset(&router, '\0', router_len); | |
1650 | router.sin_family = AF_INET; | |
1651 | router.sin_port = htons(port); | |
1652 | ||
1653 | /* Start main header - fill in length later */ | |
1654 | offset = 0; | |
1655 | ||
1656 | main_header = (struct wccp2_here_i_am_header_t *) &wccp_packet[offset]; | |
1657 | main_header->type = htonl(WCCP2_REDIRECT_ASSIGN); | |
1658 | main_header->version = htons(WCCP2_VERSION); | |
1659 | ||
bf8fe701 | 1660 | debugs(80, 2, "Running wccp2AssignBuckets"); |
0b0cfcf2 | 1661 | service_list_ptr = wccp2_service_list_head; |
1662 | ||
1663 | while (service_list_ptr != NULL) { | |
1664 | /* If we're not the lowest, we don't need to worry */ | |
1665 | ||
1666 | if (service_list_ptr->lowest_ip == 0) { | |
1667 | /* XXX eww */ | |
1668 | service_list_ptr = service_list_ptr->next; | |
1669 | continue; | |
1670 | } | |
1671 | ||
1672 | /* reset the offset */ | |
1673 | ||
1674 | offset = sizeof(struct wccp2_here_i_am_header_t); | |
1675 | ||
1676 | /* build packet header from hereIam packet */ | |
1677 | /* Security info */ | |
1678 | /* XXX this should be made more generic! */ | |
1679 | /* XXX and I hate magic numbers! */ | |
1680 | switch (service_list_ptr->wccp2_security_type) { | |
1681 | ||
1682 | case WCCP2_NO_SECURITY: | |
1683 | ||
1684 | security = (struct wccp2_security_md5_t *) &wccp_packet[offset]; | |
1685 | memcpy(security, service_list_ptr->security_info, 8); | |
1686 | offset += 8; | |
1687 | break; | |
1688 | ||
1689 | case WCCP2_MD5_SECURITY: | |
1690 | ||
1691 | security = (struct wccp2_security_md5_t *) &wccp_packet[offset]; | |
1692 | ||
1693 | memcpy(security, service_list_ptr->security_info, sizeof(struct wccp2_security_md5_t)); | |
1694 | ||
1695 | offset += sizeof(struct wccp2_security_md5_t); | |
1696 | break; | |
1697 | ||
1698 | default: | |
1699 | fatalf("Unknown Wccp2 security type\n"); | |
1700 | } | |
1701 | ||
1702 | /* Service info */ | |
1703 | ||
1704 | memcpy(&wccp_packet[offset], service_list_ptr->service_info, sizeof(struct wccp2_service_info_t)); | |
1705 | ||
1706 | offset += sizeof(struct wccp2_service_info_t); | |
1707 | ||
1708 | /* assignment header - fill in length later */ | |
1709 | ||
1710 | assignment_header = (struct wccp2_item_header_t *) &wccp_packet[offset]; | |
1711 | ||
b7d249f9 | 1712 | switch (Config.Wccp2.assignment_method) { |
0b0cfcf2 | 1713 | |
b7d249f9 | 1714 | case WCCP2_ASSIGNMENT_METHOD_HASH: |
1715 | assignment_header->type = htons(WCCP2_REDIRECT_ASSIGNMENT); | |
1716 | ||
1717 | offset += sizeof(struct wccp2_item_header_t); | |
1718 | assignment_offset = offset; | |
1719 | break; | |
1720 | ||
1721 | case WCCP2_ASSIGNMENT_METHOD_MASK: | |
1722 | assignment_header->type = htons(WCCP2_ALT_ASSIGNMENT); | |
1723 | ||
1724 | offset += sizeof(struct wccp2_item_header_t); | |
1725 | assignment_offset = offset; | |
1726 | ||
1727 | /* The alternative assignment has an extra header, fill in length later */ | |
1728 | ||
1729 | alt_assignment_type_header = (struct wccp2_item_header_t *) &wccp_packet[offset]; | |
1730 | alt_assignment_type_header->type = htons(WCCP2_MASK_ASSIGNMENT); | |
1731 | ||
1732 | offset += sizeof(struct wccp2_item_header_t); | |
1733 | alt_assignment_offset = offset; | |
1734 | ||
1735 | break; | |
1736 | ||
1737 | default: | |
1738 | fatalf("Unknown Wccp2 assignment method\n"); | |
1739 | } | |
0b0cfcf2 | 1740 | |
1741 | /* Assignment key - fill in master ip later */ | |
1742 | ||
1743 | assignment_key = (struct assignment_key_t *) &wccp_packet[offset]; | |
1744 | ||
1745 | assignment_key->master_number = htonl(++service_list_ptr->change_num); | |
1746 | ||
1747 | offset += sizeof(struct assignment_key_t); | |
1748 | ||
1749 | /* Number of routers */ | |
1750 | xmemcpy(&wccp_packet[offset], &service_list_ptr->num_routers, sizeof(service_list_ptr->num_routers)); | |
1751 | ||
1752 | offset += sizeof(service_list_ptr->num_routers); | |
1753 | ||
1754 | for (router_list_ptr = &service_list_ptr->router_list_head; router_list_ptr->next != NULL; router_list_ptr = router_list_ptr->next) { | |
1755 | ||
1756 | /* Add routers */ | |
1757 | ||
1758 | router_assign = (struct wccp2_router_assign_element_t *) &wccp_packet[offset]; | |
1759 | router_assign->router_address = router_list_ptr->info->router_address; | |
1760 | router_assign->received_id = router_list_ptr->info->received_id; | |
1761 | router_assign->change_number = htonl(router_list_ptr->member_change); | |
1762 | ||
1763 | offset += sizeof(struct wccp2_router_assign_element_t); | |
1764 | } | |
1765 | ||
1766 | saved_offset = offset; | |
1767 | ||
1768 | for (router_list_ptr = &service_list_ptr->router_list_head; router_list_ptr->next != NULL; router_list_ptr = router_list_ptr->next) { | |
b7d249f9 | 1769 | unsigned long *weight = (unsigned long *)xcalloc(sizeof(*weight), ntohl(router_list_ptr->num_caches)); |
1770 | unsigned long total_weight = 0; | |
1771 | int num_caches = ntohl(router_list_ptr->num_caches); | |
f16fbc82 | 1772 | |
0b0cfcf2 | 1773 | offset = saved_offset; |
1774 | ||
b7d249f9 | 1775 | switch (Config.Wccp2.assignment_method) { |
1776 | ||
1777 | case WCCP2_ASSIGNMENT_METHOD_HASH: | |
1778 | /* Number of caches */ | |
1779 | xmemcpy(&wccp_packet[offset], &router_list_ptr->num_caches, sizeof(router_list_ptr->num_caches)); | |
1780 | offset += sizeof(router_list_ptr->num_caches); | |
1781 | ||
1782 | if (num_caches) { | |
1783 | int cache; | |
1784 | ||
1785 | for (cache = 0, cache_list_ptr = &router_list_ptr->cache_list_head; cache_list_ptr->next; cache_list_ptr = cache_list_ptr->next, cache++) { | |
1786 | /* add caches */ | |
0b0cfcf2 | 1787 | |
2adffc6f | 1788 | cache_address = (struct in_addr *) &wccp_packet[offset]; |
0b0cfcf2 | 1789 | |
2adffc6f | 1790 | xmemcpy(cache_address, &cache_list_ptr->cache_ip, sizeof(struct in_addr)); |
b7d249f9 | 1791 | total_weight += cache_list_ptr->weight << 12; |
1792 | weight[cache] = cache_list_ptr->weight << 12; | |
0b0cfcf2 | 1793 | |
2adffc6f | 1794 | offset += sizeof(struct in_addr); |
b7d249f9 | 1795 | } |
0b0cfcf2 | 1796 | } |
0b0cfcf2 | 1797 | |
b7d249f9 | 1798 | /* Add buckets */ |
1799 | buckets = (char *) &wccp_packet[offset]; | |
1800 | ||
1801 | memset(buckets, '\0', WCCP_BUCKETS); | |
1802 | ||
1803 | if (num_caches != 0) { | |
1804 | if (total_weight == 0) { | |
1805 | for (bucket_counter = 0; bucket_counter < WCCP_BUCKETS; bucket_counter++) { | |
1806 | buckets[bucket_counter] = (char) (bucket_counter % num_caches); | |
1807 | } | |
1808 | } else { | |
1809 | unsigned long *assigned = (unsigned long *)xcalloc(sizeof(*assigned), num_caches); | |
1810 | unsigned long done = 0; | |
1811 | int cache = -1; | |
1812 | unsigned long per_bucket = total_weight / WCCP_BUCKETS; | |
1813 | ||
1814 | for (bucket_counter = 0; bucket_counter < WCCP_BUCKETS; bucket_counter++) { | |
1815 | int n; | |
1816 | unsigned long step; | |
1817 | ||
1818 | for (n = num_caches; n; n--) { | |
1819 | cache++; | |
1820 | ||
1821 | if (cache >= num_caches) | |
1822 | cache = 0; | |
1823 | ||
1824 | if (!weight[cache]) { | |
1825 | n++; | |
1826 | continue; | |
1827 | } | |
1828 | ||
1829 | if (assigned[cache] <= done) | |
1830 | break; | |
1831 | } | |
1832 | ||
1833 | buckets[bucket_counter] = (char) cache; | |
1834 | step = per_bucket * total_weight / weight[cache]; | |
1835 | assigned[cache] += step; | |
1836 | done += per_bucket; | |
1837 | } | |
1838 | ||
1839 | safe_free(assigned); | |
1840 | } | |
1841 | } | |
0b0cfcf2 | 1842 | |
b7d249f9 | 1843 | offset += (WCCP_BUCKETS * sizeof(char)); |
1844 | safe_free(weight); | |
1845 | break; | |
0b0cfcf2 | 1846 | |
b7d249f9 | 1847 | case WCCP2_ASSIGNMENT_METHOD_MASK: |
1848 | num_maskval = htonl(1); | |
1849 | xmemcpy(&wccp_packet[offset], &num_maskval, sizeof(int)); | |
1850 | offset += sizeof(int); | |
1851 | ||
1852 | mask_element = (struct wccp2_mask_element_t *) &wccp_packet[offset]; | |
1853 | service_flags = ntohl(service_list_ptr->service_info->service_flags); | |
1854 | ||
1855 | if ((service_flags & WCCP2_SERVICE_SRC_IP_HASH) || (service_flags & WCCP2_SERVICE_SRC_IP_ALT_HASH)) { | |
1856 | mask_element->source_ip_mask = htonl(0x00001741); | |
1857 | mask_element->dest_ip_mask = 0; | |
1858 | mask_element->source_port_mask = 0; | |
1859 | mask_element->dest_port_mask = 0; | |
1860 | } else if ((service_list_ptr->info.service == WCCP2_SERVICE_STANDARD) || (service_flags & WCCP2_SERVICE_DST_IP_HASH) || (service_flags & WCCP2_SERVICE_DST_IP_ALT_HASH)) { | |
1861 | mask_element->source_ip_mask = 0; | |
1862 | mask_element->dest_ip_mask = htonl(0x00001741); | |
1863 | mask_element->source_port_mask = 0; | |
1864 | mask_element->dest_port_mask = 0; | |
1865 | } else if ((service_flags & WCCP2_SERVICE_SRC_PORT_HASH) || (service_flags & WCCP2_SERVICE_SRC_PORT_ALT_HASH)) { | |
1866 | mask_element->source_ip_mask = 0; | |
1867 | mask_element->dest_ip_mask = 0; | |
1868 | mask_element->source_port_mask = htons(0x1741); | |
1869 | mask_element->dest_port_mask = 0; | |
1870 | } else if ((service_flags & WCCP2_SERVICE_DST_PORT_HASH) || (service_flags & WCCP2_SERVICE_DST_PORT_ALT_HASH)) { | |
1871 | mask_element->source_ip_mask = 0; | |
1872 | mask_element->dest_ip_mask = 0; | |
1873 | mask_element->source_port_mask = 0; | |
1874 | mask_element->dest_port_mask = htons(0x1741); | |
1875 | } else { | |
1876 | fatalf("Unknown service hash method\n"); | |
1877 | } | |
1878 | ||
1879 | mask_element->number_values = htonl(64); | |
1880 | ||
1881 | offset += sizeof(struct wccp2_mask_element_t); | |
1882 | ||
1883 | cache_list_ptr = &router_list_ptr->cache_list_head; | |
1884 | value = 0; | |
1885 | ||
1886 | for (valuecounter = 0; valuecounter < 64; valuecounter++) { | |
1887 | ||
1888 | value_element = (struct wccp2_value_element_t *) &wccp_packet[offset]; | |
1889 | ||
25e26086 | 1890 | /* Update the value according the the "correct" formula */ |
1891 | ||
9366bab7 | 1892 | for (; (value & 0x1741) != value; value++) { |
25e26086 | 1893 | assert(value <= 0x1741); |
1894 | } | |
1895 | ||
b7d249f9 | 1896 | if ((service_flags & WCCP2_SERVICE_SRC_IP_HASH) || (service_flags & WCCP2_SERVICE_SRC_IP_ALT_HASH)) { |
1897 | value_element->source_ip_value = htonl(value); | |
1898 | value_element->dest_ip_value = 0; | |
1899 | value_element->source_port_value = 0; | |
1900 | value_element->dest_port_value = 0; | |
1901 | } else if ((service_list_ptr->info.service == WCCP2_SERVICE_STANDARD) || (service_flags & WCCP2_SERVICE_DST_IP_HASH) || (service_flags & WCCP2_SERVICE_DST_IP_ALT_HASH)) { | |
1902 | value_element->source_ip_value = 0; | |
1903 | value_element->dest_ip_value = htonl(value); | |
1904 | value_element->source_port_value = 0; | |
1905 | value_element->dest_port_value = 0; | |
1906 | } else if ((service_flags & WCCP2_SERVICE_SRC_PORT_HASH) || (service_flags & WCCP2_SERVICE_SRC_PORT_ALT_HASH)) { | |
1907 | value_element->source_ip_value = 0; | |
1908 | value_element->dest_ip_value = 0; | |
1909 | value_element->source_port_value = htons(value); | |
1910 | value_element->dest_port_value = 0; | |
1911 | } else if ((service_flags & WCCP2_SERVICE_DST_PORT_HASH) || (service_flags & WCCP2_SERVICE_DST_PORT_ALT_HASH)) { | |
1912 | value_element->source_ip_value = 0; | |
1913 | value_element->dest_ip_value = 0; | |
1914 | value_element->source_port_value = 0; | |
1915 | value_element->dest_port_value = htons(value); | |
1916 | } else { | |
1917 | fatalf("Unknown service hash method\n"); | |
1918 | } | |
1919 | ||
1920 | value_element->cache_ip = cache_list_ptr->cache_ip; | |
1921 | ||
1922 | offset += sizeof(struct wccp2_value_element_t); | |
25e26086 | 1923 | value++; |
b7d249f9 | 1924 | |
1925 | /* Assign the next value to the next cache */ | |
25e26086 | 1926 | |
b7d249f9 | 1927 | if ((cache_list_ptr->next) && (cache_list_ptr->next->next)) |
1928 | cache_list_ptr = cache_list_ptr->next; | |
1929 | else | |
1930 | cache_list_ptr = &router_list_ptr->cache_list_head; | |
1931 | } | |
0b0cfcf2 | 1932 | |
b7d249f9 | 1933 | /* Fill in length */ |
1934 | alt_assignment_type_header->length = htons(offset - alt_assignment_offset); | |
1935 | ||
1936 | break; | |
1937 | ||
1938 | default: | |
1939 | fatalf("Unknown Wccp2 assignment method\n"); | |
1940 | } | |
1941 | ||
1942 | /* Fill in length */ | |
0b0cfcf2 | 1943 | |
b7d249f9 | 1944 | assignment_header->length = htons(offset - assignment_offset); |
0b0cfcf2 | 1945 | |
1946 | /* Fill in assignment key */ | |
1947 | assignment_key->master_ip = router_list_ptr->local_ip; | |
1948 | ||
1949 | /* finish length */ | |
1950 | ||
1951 | main_header->length = htons(offset - sizeof(struct wccp2_here_i_am_header_t)); | |
1952 | ||
1953 | /* set the destination address */ | |
1954 | router.sin_addr = router_list_ptr->router_sendto_address; | |
1955 | ||
1956 | /* Security update, if needed */ | |
1957 | ||
1958 | if (service_list_ptr->wccp2_security_type == WCCP2_MD5_SECURITY) { | |
1959 | wccp2_update_md5_security(service_list_ptr->wccp_password, (char *) security, wccp_packet, offset); | |
1960 | } | |
1961 | ||
1962 | if (ntohl(router_list_ptr->num_caches)) { | |
1963 | /* send packet */ | |
c1f55201 | 1964 | |
1965 | if (wccp2_numrouters > 1) { | |
cc192b50 | 1966 | /* FIXME INET6 : drop temp conversion */ IPAddress tmp_rtr(router); |
c1f55201 | 1967 | comm_udp_sendto(theWccp2Connection, |
cc192b50 | 1968 | tmp_rtr, |
c1f55201 | 1969 | &wccp_packet, |
1970 | offset); | |
1971 | } else { | |
1972 | send(theWccp2Connection, | |
1973 | &wccp_packet, | |
1974 | offset, | |
1975 | 0); | |
1976 | } | |
0b0cfcf2 | 1977 | } |
1978 | } | |
1979 | ||
1980 | service_list_ptr = service_list_ptr->next; | |
1981 | } | |
1982 | } | |
1983 | ||
1984 | ||
1985 | /* | |
1986 | * Configuration option parsing code | |
1987 | */ | |
1988 | ||
1989 | /* | |
1990 | * Format: | |
1991 | * | |
1992 | * wccp2_service {standard|dynamic} {id} (password=password) | |
1993 | */ | |
1994 | void | |
1995 | parse_wccp2_service(void *v) | |
1996 | { | |
1997 | char *t; | |
1998 | int service = 0; | |
1999 | int service_id = 0; | |
2000 | int security_type = WCCP2_NO_SECURITY; | |
2001 | char wccp_password[WCCP2_PASSWORD_LEN + 1]; | |
2002 | ||
2003 | if (wccp2_connected == 1) { | |
bf8fe701 | 2004 | debugs(80, 1, "WCCPv2: Somehow reparsing the configuration without having shut down WCCP! Try reloading squid again."); |
0b0cfcf2 | 2005 | return; |
2006 | } | |
2007 | ||
2008 | /* Snarf the type */ | |
2009 | if ((t = strtok(NULL, w_space)) == NULL) { | |
bf8fe701 | 2010 | debugs(80, 0, "wccp2ParseServiceInfo: missing service info type (standard|dynamic)"); |
0b0cfcf2 | 2011 | self_destruct(); |
2012 | } | |
2013 | ||
2014 | if (strcmp(t, "standard") == 0) { | |
2015 | service = WCCP2_SERVICE_STANDARD; | |
2016 | } else if (strcmp(t, "dynamic") == 0) { | |
2017 | service = WCCP2_SERVICE_DYNAMIC; | |
2018 | } else { | |
bf8fe701 | 2019 | debugs(80, 0, "wccp2ParseServiceInfo: bad service info type (expected standard|dynamic, got " << t << ")"); |
0b0cfcf2 | 2020 | self_destruct(); |
2021 | } | |
2022 | ||
2023 | /* Snarf the ID */ | |
2024 | service_id = GetInteger(); | |
2025 | ||
2026 | if (service_id < 0 || service_id > 255) { | |
bf8fe701 | 2027 | debugs(80, 0, "wccp2ParseServiceInfo: service info id " << service_id << " is out of range (0..255)"); |
0b0cfcf2 | 2028 | self_destruct(); |
2029 | } | |
2030 | ||
2031 | memset(wccp_password, 0, sizeof(wccp_password)); | |
2032 | /* Handle password, if any */ | |
2033 | ||
2034 | if ((t = strtok(NULL, w_space)) != NULL) { | |
2035 | if (strncmp(t, "password=", 9) == 0) { | |
2036 | security_type = WCCP2_MD5_SECURITY; | |
2037 | strncpy(wccp_password, t + 9, WCCP2_PASSWORD_LEN); | |
2038 | } | |
2039 | } | |
2040 | ||
2041 | /* Create a placeholder service record */ | |
2042 | wccp2_add_service_list(service, service_id, 0, 0, 0, empty_portlist, security_type, wccp_password); | |
2043 | } | |
2044 | ||
2045 | void | |
2046 | dump_wccp2_service(StoreEntry * e, const char *label, void *v) | |
2047 | { | |
2048 | ||
2049 | struct wccp2_service_list_t *srv; | |
2050 | srv = wccp2_service_list_head; | |
2051 | ||
2052 | while (srv != NULL) { | |
bf8fe701 | 2053 | debugs(80, 3, "dump_wccp2_service: id " << srv->info.service_id << ", type " << srv->info.service); |
0b0cfcf2 | 2054 | storeAppendPrintf(e, "%s %s %d", label, |
2055 | (srv->info.service == WCCP2_SERVICE_DYNAMIC) ? "dynamic" : "standard", | |
2056 | srv->info.service_id); | |
2057 | ||
2058 | if (srv->wccp2_security_type == WCCP2_MD5_SECURITY) { | |
2059 | storeAppendPrintf(e, " %s", srv->wccp_password); | |
2060 | } | |
2061 | ||
c208fd0e | 2062 | storeAppendPrintf(e, "\n"); |
2063 | ||
0b0cfcf2 | 2064 | srv = srv->next; |
2065 | } | |
2066 | } | |
2067 | ||
2068 | void | |
2069 | free_wccp2_service(void *v) | |
2070 | {} | |
2071 | ||
2072 | int | |
2073 | check_null_wccp2_service(void *v) | |
2074 | { | |
2075 | return !wccp2_service_list_head; | |
2076 | } | |
2077 | ||
2078 | /* | |
2079 | * Format: | |
2080 | * | |
2081 | * wccp2_service_info {id} stuff.. | |
2082 | * | |
2083 | * Where stuff is: | |
2084 | * | |
2085 | * + flags=flag,flag,flag.. | |
2086 | * + proto=protocol (tcp|udp) | |
2087 | * + ports=port,port,port (up to a max of 8) | |
2088 | * + priority=priority (0->255) | |
2089 | * | |
2090 | * The flags here are: | |
2091 | * src_ip_hash, dst_ip_hash, source_port_hash, dst_port_hash, ports_defined, | |
2092 | * ports_source, src_ip_alt_hash, dst_ip_alt_hash, src_port_alt_hash, dst_port_alt_hash | |
2093 | */ | |
2094 | static int | |
2095 | parse_wccp2_service_flags(char *flags) | |
2096 | { | |
2097 | char *tmp, *tmp2; | |
2098 | char *flag; | |
2099 | int retflag = 0; | |
2100 | ||
2101 | if (!flags) { | |
2102 | return 0; | |
2103 | } | |
2104 | ||
2105 | tmp = xstrdup(flags); | |
2106 | tmp2 = tmp; | |
2107 | ||
2108 | flag = strsep(&tmp2, ","); | |
2109 | ||
2110 | while (flag) { | |
2111 | if (strcmp(flag, "src_ip_hash") == 0) { | |
2112 | retflag |= WCCP2_SERVICE_SRC_IP_HASH; | |
2113 | } else if (strcmp(flag, "dst_ip_hash") == 0) { | |
2114 | retflag |= WCCP2_SERVICE_DST_IP_HASH; | |
2115 | } else if (strcmp(flag, "source_port_hash") == 0) { | |
b7d249f9 | 2116 | retflag |= WCCP2_SERVICE_SRC_PORT_HASH; |
0b0cfcf2 | 2117 | } else if (strcmp(flag, "dst_port_hash") == 0) { |
2118 | retflag |= WCCP2_SERVICE_DST_PORT_HASH; | |
2119 | } else if (strcmp(flag, "ports_source") == 0) { | |
2120 | retflag |= WCCP2_SERVICE_PORTS_SOURCE; | |
2121 | } else if (strcmp(flag, "src_ip_alt_hash") == 0) { | |
2122 | retflag |= WCCP2_SERVICE_SRC_IP_ALT_HASH; | |
2123 | } else if (strcmp(flag, "dst_ip_alt_hash") == 0) { | |
2124 | retflag |= WCCP2_SERVICE_DST_IP_ALT_HASH; | |
2125 | } else if (strcmp(flag, "src_port_alt_hash") == 0) { | |
2126 | retflag |= WCCP2_SERVICE_SRC_PORT_ALT_HASH; | |
2127 | } else if (strcmp(flag, "dst_port_alt_hash") == 0) { | |
2128 | retflag |= WCCP2_SERVICE_DST_PORT_ALT_HASH; | |
2129 | } else { | |
2130 | fatalf("Unknown wccp2 service flag: %s\n", flag); | |
2131 | } | |
2132 | ||
2133 | flag = strsep(&tmp2, ","); | |
2134 | } | |
2135 | ||
2136 | xfree(tmp); | |
2137 | return retflag; | |
2138 | } | |
2139 | ||
2140 | static void | |
2141 | parse_wccp2_service_ports(char *options, int portlist[]) | |
2142 | { | |
2143 | int i = 0; | |
2144 | int p; | |
2145 | char *tmp, *tmp2, *port, *end; | |
2146 | ||
2147 | if (!options) { | |
2148 | return; | |
2149 | } | |
2150 | ||
2151 | tmp = xstrdup(options); | |
2152 | tmp2 = tmp; | |
2153 | ||
2154 | port = strsep(&tmp2, ","); | |
2155 | ||
2156 | while (port && i < WCCP2_NUMPORTS) { | |
2157 | p = strtol(port, &end, 0); | |
2158 | ||
2159 | if (p < 1 || p > 65535) { | |
2160 | fatalf("parse_wccp2_service_ports: port value '%s' isn't valid (1..65535)\n", port); | |
2161 | } | |
2162 | ||
2163 | portlist[i] = p; | |
2164 | i++; | |
2165 | port = strsep(&tmp2, ","); | |
2166 | } | |
2167 | ||
2168 | if (i == 8) { | |
2169 | fatalf("parse_wccp2_service_ports: too many ports (maximum: 8) in list '%s'\n", options); | |
2170 | } | |
2171 | ||
2172 | xfree(tmp); | |
2173 | } | |
2174 | ||
2175 | void | |
2176 | parse_wccp2_service_info(void *v) | |
2177 | { | |
2178 | char *t, *end; | |
2179 | int service_id = 0; | |
2180 | int flags = 0; | |
2181 | int portlist[WCCP2_NUMPORTS]; | |
2182 | int protocol = -1; /* IPPROTO_TCP | IPPROTO_UDP */ | |
2183 | ||
2184 | struct wccp2_service_list_t *srv; | |
2185 | int priority = -1; | |
2186 | ||
2187 | if (wccp2_connected == 1) { | |
bf8fe701 | 2188 | debugs(80, 1, "WCCPv2: Somehow reparsing the configuration without having shut down WCCP! Try reloading squid again."); |
0b0cfcf2 | 2189 | return; |
2190 | } | |
2191 | ||
bf8fe701 | 2192 | debugs(80, 5, "parse_wccp2_service_info: called"); |
a302ad3c | 2193 | memset(portlist, 0, sizeof(portlist)); |
0b0cfcf2 | 2194 | /* First argument: id */ |
2195 | service_id = GetInteger(); | |
2196 | ||
2197 | if (service_id < 0 || service_id > 255) { | |
bf8fe701 | 2198 | debugs(80, 1, "parse_wccp2_service_info: invalid service id " << service_id << " (must be between 0 .. 255)"); |
0b0cfcf2 | 2199 | self_destruct(); |
2200 | } | |
2201 | ||
2202 | /* Next: find the (hopefully!) existing service */ | |
2203 | srv = wccp2_get_service_by_id(WCCP2_SERVICE_DYNAMIC, service_id); | |
2204 | ||
2205 | if (srv == NULL) { | |
2206 | fatalf("parse_wccp2_service_info: unknown dynamic service id %d: you need to define it using wccp2_service (and make sure you wish to configure it as a dynamic service.)\n", service_id); | |
2207 | } | |
2208 | ||
2209 | /* Next: loop until we don't have any more tokens */ | |
2210 | while ((t = strtok(NULL, w_space)) != NULL) { | |
2211 | if (strncmp(t, "flags=", 6) == 0) { | |
2212 | /* XXX eww, string pointer math */ | |
2213 | flags = parse_wccp2_service_flags(t + 6); | |
2214 | } else if (strncmp(t, "ports=", 6) == 0) { | |
2215 | parse_wccp2_service_ports(t + 6, portlist); | |
2216 | flags |= WCCP2_SERVICE_PORTS_DEFINED; | |
2217 | } else if (strncmp(t, "protocol=tcp", 12) == 0) { | |
2218 | protocol = IPPROTO_TCP; | |
2219 | } else if (strncmp(t, "protocol=udp", 12) == 0) { | |
2220 | protocol = IPPROTO_UDP; | |
2221 | } else if (strncmp(t, "protocol=", 9) == 0) { | |
2222 | fatalf("parse_wccp2_service_info: id %d: unknown protocol (%s) - must be tcp or udp!\n", service_id, t); | |
2223 | } else if (strncmp(t, "priority=", 9) == 0) { | |
2224 | priority = strtol(t + 9, &end, 0); | |
2225 | ||
2226 | if (priority < 0 || priority > 255) { | |
2227 | fatalf("parse_wccp2_service_info: id %d: %s out of range (0..255)!\n", service_id, t); | |
2228 | } | |
2229 | } else { | |
2230 | fatalf("parse_wccp2_service_info: id %d: unknown option '%s'\n", service_id, t); | |
2231 | } | |
2232 | } | |
2233 | ||
2234 | /* Check everything is set */ | |
2235 | if (priority == -1) { | |
2236 | fatalf("parse_wccp2_service_info: service %d: no priority defined (valid: 0..255)!\n", service_id); | |
2237 | } | |
2238 | ||
2239 | if (protocol == -1) { | |
2240 | fatalf("parse_wccp2_service_info: service %d: no protocol defined (valid: tcp or udp)!\n", service_id); | |
2241 | } | |
2242 | ||
2243 | if (!(flags & WCCP2_SERVICE_PORTS_DEFINED)) { | |
2244 | fatalf("parse_wccp2_service_info: service %d: no ports defined!\n", service_id); | |
2245 | } | |
2246 | ||
2247 | /* rightio! now we can update */ | |
2248 | wccp2_update_service(srv, WCCP2_SERVICE_DYNAMIC, service_id, priority, | |
2249 | protocol, flags, portlist); | |
2250 | ||
2251 | /* Done! */ | |
2252 | } | |
2253 | ||
2254 | void | |
2255 | dump_wccp2_service_info(StoreEntry * e, const char *label, void *v) | |
2256 | { | |
2257 | char comma; | |
2258 | ||
2259 | struct wccp2_service_list_t *srv; | |
2260 | int flags; | |
2261 | srv = wccp2_service_list_head; | |
2262 | ||
2263 | while (srv != NULL) { | |
bf8fe701 | 2264 | debugs(80, 3, "dump_wccp2_service_info: id " << srv->info.service_id << " (type " << srv->info.service << ")"); |
0b0cfcf2 | 2265 | |
2266 | /* We don't need to spit out information for standard services */ | |
2267 | ||
2268 | if (srv->info.service == WCCP2_SERVICE_STANDARD) { | |
bf8fe701 | 2269 | debugs(80, 3, "dump_wccp2_service_info: id " << srv->info.service_id << ": standard service, not dumping info"); |
2270 | ||
0b0cfcf2 | 2271 | /* XXX eww */ |
2272 | srv = srv->next; | |
2273 | continue; | |
2274 | } | |
2275 | ||
2276 | storeAppendPrintf(e, "%s %d", label, srv->info.service_id); | |
2277 | ||
2278 | /* priority */ | |
2279 | storeAppendPrintf(e, " priority=%d", srv->info.service_priority); | |
2280 | ||
2281 | /* flags */ | |
2282 | flags = ntohl(srv->info.service_flags); | |
2283 | ||
2284 | if (flags != 0) { | |
2285 | comma = 0; | |
2286 | storeAppendPrintf(e, " flags="); | |
2287 | ||
2288 | if (flags & WCCP2_SERVICE_SRC_IP_HASH) { | |
2289 | storeAppendPrintf(e, "%ssrc_ip_hash", comma ? "," : ""); | |
2290 | comma = 1; | |
2291 | } | |
2292 | ||
2293 | if (flags & WCCP2_SERVICE_DST_IP_HASH) { | |
2294 | storeAppendPrintf(e, "%sdst_ip_hash", comma ? "," : ""); | |
2295 | comma = 1; | |
2296 | } | |
2297 | ||
b7d249f9 | 2298 | if (flags & WCCP2_SERVICE_SRC_PORT_HASH) { |
0b0cfcf2 | 2299 | storeAppendPrintf(e, "%ssource_port_hash", comma ? "," : ""); |
2300 | comma = 1; | |
2301 | } | |
2302 | ||
2303 | if (flags & WCCP2_SERVICE_DST_PORT_HASH) { | |
2304 | storeAppendPrintf(e, "%sdst_port_hash", comma ? "," : ""); | |
2305 | comma = 1; | |
2306 | } | |
2307 | ||
2308 | if (flags & WCCP2_SERVICE_PORTS_DEFINED) { | |
2309 | storeAppendPrintf(e, "%sports_defined", comma ? "," : ""); | |
2310 | comma = 1; | |
2311 | } | |
2312 | ||
2313 | if (flags & WCCP2_SERVICE_PORTS_SOURCE) { | |
2314 | storeAppendPrintf(e, "%sports_source", comma ? "," : ""); | |
2315 | comma = 1; | |
2316 | } | |
2317 | ||
2318 | if (flags & WCCP2_SERVICE_SRC_IP_ALT_HASH) { | |
2319 | storeAppendPrintf(e, "%ssrc_ip_alt_hash", comma ? "," : ""); | |
2320 | comma = 1; | |
2321 | } | |
2322 | ||
2323 | if (flags & WCCP2_SERVICE_DST_IP_ALT_HASH) { | |
2324 | storeAppendPrintf(e, "%ssrc_ip_alt_hash", comma ? "," : ""); | |
2325 | comma = 1; | |
2326 | } | |
2327 | ||
2328 | if (flags & WCCP2_SERVICE_SRC_PORT_ALT_HASH) { | |
2329 | storeAppendPrintf(e, "%ssrc_port_alt_hash", comma ? "," : ""); | |
2330 | comma = 1; | |
2331 | } | |
2332 | ||
2333 | if (flags & WCCP2_SERVICE_DST_PORT_ALT_HASH) { | |
2334 | storeAppendPrintf(e, "%sdst_port_alt_hash", comma ? "," : ""); | |
2335 | comma = 1; | |
2336 | } | |
2337 | } | |
2338 | ||
2339 | /* ports */ | |
2340 | comma = 0; | |
2341 | ||
2342 | if (srv->info.port0 != 0) { | |
2343 | storeAppendPrintf(e, "%s%d", comma ? "," : " ports=", ntohs(srv->info.port0)); | |
2344 | comma = 1; | |
2345 | } | |
2346 | ||
2347 | if (srv->info.port1 != 0) { | |
2348 | storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port1)); | |
2349 | comma = 1; | |
2350 | } | |
2351 | ||
2352 | if (srv->info.port2 != 0) { | |
2353 | storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port2)); | |
2354 | comma = 1; | |
2355 | } | |
2356 | ||
2357 | if (srv->info.port3 != 0) { | |
2358 | storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port3)); | |
2359 | comma = 1; | |
2360 | } | |
2361 | ||
2362 | if (srv->info.port4 != 0) { | |
2363 | storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port4)); | |
2364 | comma = 1; | |
2365 | } | |
2366 | ||
2367 | if (srv->info.port5 != 0) { | |
2368 | storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port5)); | |
2369 | comma = 1; | |
2370 | } | |
2371 | ||
2372 | if (srv->info.port6 != 0) { | |
2373 | storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port6)); | |
2374 | comma = 1; | |
2375 | } | |
2376 | ||
2377 | if (srv->info.port7 != 0) { | |
2378 | storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port7)); | |
2379 | comma = 1; | |
2380 | } | |
2381 | ||
2382 | /* protocol */ | |
2383 | storeAppendPrintf(e, " protocol=%s", (srv->info.service_protocol == IPPROTO_TCP) ? "tcp" : "udp"); | |
2384 | ||
2385 | storeAppendPrintf(e, "\n"); | |
2386 | ||
2387 | srv = srv->next; | |
2388 | } | |
2389 | } | |
2390 | ||
2391 | void | |
2392 | free_wccp2_service_info(void *v) | |
2393 | {} | |
2394 | ||
2395 | #endif /* USE_WCCPv2 */ |