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