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

 

Introduction

Documentation

Support

Miscellaneous