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