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