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

 

Introduction

Documentation

Support

Miscellaneous