wccp2.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2022 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. */
230  struct wccp2_mask_element_t mask;
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 
348 static struct wccp2_i_see_you_t wccp2_i_see_you;
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 
439  struct wccp2_service_info_t info;
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 != NULL) {
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 NULL;
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 != NULL) {
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 = NULL;
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, NULL)) {
942  eventAdd("wccp2HereIam", wccp2HereIam, NULL, 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 != NULL) {
1006  for (router_list_ptr = &service_list_ptr->router_list_head; router_list_ptr->next != NULL; 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 != NULL) {
1074  for (router_list_ptr = &service_list_ptr->router_list_head; router_list_ptr != NULL; 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 
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 = NULL;
1182 
1183  struct wccp2_service_info_t *service_info = NULL;
1184 
1185  struct router_identity_info_t *router_identity_info = NULL;
1186 
1187  struct router_view_t *router_view_header = NULL;
1188 
1189  struct wccp2_cache_mask_identity_info_t *cache_mask_identity = NULL;
1190 
1191  struct cache_mask_info_t *cache_mask_info = NULL;
1192 
1193  struct wccp2_cache_identity_info_t *cache_identity = NULL;
1194 
1195  struct wccp2_capability_info_header_t *router_capability_header = NULL;
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 
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 != NULL) {
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 == NULL) {
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 != NULL; 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 == NULL) {
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 = NULL;
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 = NULL;
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);
1558  eventAdd("wccp2AssignBuckets", wccp2AssignBuckets, NULL, 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 {
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, NULL, 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 != NULL) {
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 != NULL; 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, NULL, 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  unsigned short port = WCCP_PORT;
1673 
1674  /* Packet segments */
1675 
1676  struct wccp2_message_header_t *main_header;
1677 
1678  struct wccp2_security_md5_t *security = NULL;
1679  /* service from service struct */
1680 
1681  struct wccp2_item_header_t *assignment_header;
1682 
1683  struct wccp2_item_header_t *alt_assignment_type_header = NULL;
1684 
1685  struct assignment_key_t *assignment_key;
1686  /* number of routers */
1687 
1688  struct wccp2_router_assign_element_t *router_assign;
1689  /* number of caches */
1690 
1691  struct in_addr *cache_address;
1692  /* Alternative assignment mask/values */
1693  int num_maskval;
1694 
1695  struct wccp2_mask_element_t *mask_element;
1696 
1697  struct wccp2_value_element_t *value_element;
1698  int valuecounter, value;
1699  char *buckets;
1700 
1701  assignment_offset = alt_assignment_offset = 0;
1702 
1703  router_len = sizeof(router);
1704  memset(&router, '\0', router_len);
1705  router.sin_family = AF_INET;
1706  router.sin_port = htons(port);
1707 
1708  /* Start main header - fill in length later */
1709  offset = 0;
1710 
1711  main_header = (struct wccp2_message_header_t *) &wccp_packet[offset];
1712  main_header->type = htonl(WCCP2_REDIRECT_ASSIGN);
1713  main_header->version = htons(WCCP2_VERSION);
1714 
1715  debugs(80, 2, "Running wccp2AssignBuckets");
1716  service_list_ptr = wccp2_service_list_head;
1717 
1718  while (service_list_ptr != NULL) {
1719  /* If we're not the lowest, we don't need to worry */
1720 
1721  if (service_list_ptr->lowest_ip == 0) {
1722  /* XXX eww */
1723  service_list_ptr = service_list_ptr->next;
1724  continue;
1725  }
1726 
1727  /* reset the offset */
1728 
1729  offset = sizeof(struct wccp2_message_header_t);
1730 
1731  /* build packet header from hereIam packet */
1732  /* Security info */
1733  /* XXX this should be made more generic! */
1734  /* XXX and I hate magic numbers! */
1735  switch (service_list_ptr->wccp2_security_type) {
1736 
1737  case WCCP2_NO_SECURITY:
1738 
1739  security = (struct wccp2_security_md5_t *) &wccp_packet[offset];
1740  memcpy(security, service_list_ptr->security_info, 8);
1741  offset += 8;
1742  break;
1743 
1744  case WCCP2_MD5_SECURITY:
1745 
1746  security = (struct wccp2_security_md5_t *) &wccp_packet[offset];
1747 
1748  memcpy(security, service_list_ptr->security_info, sizeof(struct wccp2_security_md5_t));
1749 
1750  offset += sizeof(struct wccp2_security_md5_t);
1751  break;
1752 
1753  default:
1754  fatalf("Unknown Wccp2 security type\n");
1755  }
1756 
1757  /* Service info */
1758 
1759  memcpy(&wccp_packet[offset], service_list_ptr->service_info, sizeof(struct wccp2_service_info_t));
1760 
1761  offset += sizeof(struct wccp2_service_info_t);
1762 
1763  /* assignment header - fill in length later */
1764 
1765  assignment_header = (struct wccp2_item_header_t *) &wccp_packet[offset];
1766 
1767  switch (Config.Wccp2.assignment_method) {
1768 
1770  assignment_header->type = htons(WCCP2_REDIRECT_ASSIGNMENT);
1771 
1772  offset += sizeof(struct wccp2_item_header_t);
1773  assignment_offset = offset;
1774  break;
1775 
1777  assignment_header->type = htons(WCCP2_ALT_ASSIGNMENT);
1778 
1779  offset += sizeof(struct wccp2_item_header_t);
1780  assignment_offset = offset;
1781 
1782  /* The alternative assignment has an extra header, fill in length later */
1783 
1784  alt_assignment_type_header = (struct wccp2_item_header_t *) &wccp_packet[offset];
1785  alt_assignment_type_header->type = htons(WCCP2_MASK_ASSIGNMENT);
1786 
1787  offset += sizeof(struct wccp2_item_header_t);
1788  alt_assignment_offset = offset;
1789 
1790  break;
1791 
1792  default:
1793  fatalf("Unknown Wccp2 assignment method\n");
1794  }
1795 
1796  /* Assignment key - fill in master ip later */
1797 
1798  assignment_key = (struct assignment_key_t *) &wccp_packet[offset];
1799 
1800  ++service_list_ptr->change_num;
1801  assignment_key->master_number = htonl(service_list_ptr->change_num);
1802 
1803  offset += sizeof(struct assignment_key_t);
1804 
1805  /* Number of routers */
1806  memcpy(&wccp_packet[offset], &service_list_ptr->num_routers, sizeof(service_list_ptr->num_routers));
1807 
1808  offset += sizeof(service_list_ptr->num_routers);
1809 
1810  for (router_list_ptr = &service_list_ptr->router_list_head; router_list_ptr->next != NULL; router_list_ptr = router_list_ptr->next) {
1811 
1812  /* Add routers */
1813 
1814  router_assign = (struct wccp2_router_assign_element_t *) &wccp_packet[offset];
1815  router_assign->router_address = router_list_ptr->info->router_address;
1816  router_assign->received_id = router_list_ptr->info->received_id;
1817  router_assign->change_number = htonl(router_list_ptr->member_change);
1818 
1819  offset += sizeof(struct wccp2_router_assign_element_t);
1820  }
1821 
1822  saved_offset = offset;
1823 
1824  for (router_list_ptr = &service_list_ptr->router_list_head; router_list_ptr->next != NULL; router_list_ptr = router_list_ptr->next) {
1825  unsigned long *weight = (unsigned long *)xcalloc(sizeof(*weight), ntohl(router_list_ptr->num_caches));
1826  unsigned long total_weight = 0;
1827  int num_caches = ntohl(router_list_ptr->num_caches);
1828 
1829  offset = saved_offset;
1830 
1831  switch (Config.Wccp2.assignment_method) {
1832 
1834  /* Number of caches */
1835  memcpy(&wccp_packet[offset], &router_list_ptr->num_caches, sizeof(router_list_ptr->num_caches));
1836  offset += sizeof(router_list_ptr->num_caches);
1837 
1838  if (num_caches) {
1839  int cache;
1840 
1841  for (cache = 0, cache_list_ptr = &router_list_ptr->cache_list_head; cache_list_ptr->next; cache_list_ptr = cache_list_ptr->next, ++cache) {
1842  /* add caches */
1843 
1844  cache_address = (struct in_addr *) &wccp_packet[offset];
1845 
1846  memcpy(cache_address, &cache_list_ptr->cache_ip, sizeof(struct in_addr));
1847  total_weight += cache_list_ptr->weight << 12;
1848  weight[cache] = cache_list_ptr->weight << 12;
1849 
1850  offset += sizeof(struct in_addr);
1851  }
1852  }
1853 
1854  /* Add buckets */
1855  buckets = (char *) &wccp_packet[offset];
1856 
1857  memset(buckets, '\0', WCCP_BUCKETS);
1858 
1859  if (num_caches != 0) {
1860  if (total_weight == 0) {
1861  for (bucket_counter = 0; bucket_counter < WCCP_BUCKETS; ++bucket_counter) {
1862  buckets[bucket_counter] = (char) (bucket_counter % num_caches);
1863  }
1864  } else {
1865  unsigned long *assigned = (unsigned long *)xcalloc(sizeof(*assigned), num_caches);
1866  unsigned long done = 0;
1867  int cache = -1;
1868  unsigned long per_bucket = total_weight / WCCP_BUCKETS;
1869 
1870  for (bucket_counter = 0; bucket_counter < WCCP_BUCKETS; ++bucket_counter) {
1871  int n;
1872  unsigned long step;
1873 
1874  for (n = num_caches; n; --n) {
1875  ++cache;
1876 
1877  if (cache >= num_caches)
1878  cache = 0;
1879 
1880  if (!weight[cache]) {
1881  ++n;
1882  continue;
1883  }
1884 
1885  if (assigned[cache] <= done)
1886  break;
1887  }
1888 
1889  buckets[bucket_counter] = (char) cache;
1890  step = per_bucket * total_weight / weight[cache];
1891  assigned[cache] += step;
1892  done += per_bucket;
1893  }
1894 
1895  safe_free(assigned);
1896  }
1897  }
1898 
1899  offset += (WCCP_BUCKETS * sizeof(char));
1900  safe_free(weight);
1901  break;
1902 
1904  num_maskval = htonl(1);
1905  memcpy(&wccp_packet[offset], &num_maskval, sizeof(int));
1906  offset += sizeof(int);
1907 
1908  mask_element = (struct wccp2_mask_element_t *) &wccp_packet[offset];
1909  service_flags = ntohl(service_list_ptr->service_info->service_flags);
1910 
1911  if ((service_flags & WCCP2_SERVICE_SRC_IP_HASH) || (service_flags & WCCP2_SERVICE_SRC_IP_ALT_HASH)) {
1912  mask_element->source_ip_mask = htonl(0x00001741);
1913  mask_element->dest_ip_mask = 0;
1914  mask_element->source_port_mask = 0;
1915  mask_element->dest_port_mask = 0;
1916  } else if ((service_list_ptr->info.service == WCCP2_SERVICE_STANDARD) || (service_flags & WCCP2_SERVICE_DST_IP_HASH) || (service_flags & WCCP2_SERVICE_DST_IP_ALT_HASH)) {
1917  mask_element->source_ip_mask = 0;
1918  mask_element->dest_ip_mask = htonl(0x00001741);
1919  mask_element->source_port_mask = 0;
1920  mask_element->dest_port_mask = 0;
1921  } else if ((service_flags & WCCP2_SERVICE_SRC_PORT_HASH) || (service_flags & WCCP2_SERVICE_SRC_PORT_ALT_HASH)) {
1922  mask_element->source_ip_mask = 0;
1923  mask_element->dest_ip_mask = 0;
1924  mask_element->source_port_mask = htons(0x1741);
1925  mask_element->dest_port_mask = 0;
1926  } else if ((service_flags & WCCP2_SERVICE_DST_PORT_HASH) || (service_flags & WCCP2_SERVICE_DST_PORT_ALT_HASH)) {
1927  mask_element->source_ip_mask = 0;
1928  mask_element->dest_ip_mask = 0;
1929  mask_element->source_port_mask = 0;
1930  mask_element->dest_port_mask = htons(0x1741);
1931  } else {
1932  fatalf("Unknown service hash method\n");
1933  }
1934 
1935  mask_element->number_values = htonl(64);
1936 
1937  offset += sizeof(struct wccp2_mask_element_t);
1938 
1939  cache_list_ptr = &router_list_ptr->cache_list_head;
1940  value = 0;
1941 
1942  for (valuecounter = 0; valuecounter < 64; ++valuecounter) {
1943 
1944  value_element = (struct wccp2_value_element_t *) &wccp_packet[offset];
1945 
1946  /* Update the value according the the "correct" formula */
1947 
1948  for (; (value & 0x1741) != value; ++value) {
1949  assert(value <= 0x1741);
1950  }
1951 
1952  if ((service_flags & WCCP2_SERVICE_SRC_IP_HASH) || (service_flags & WCCP2_SERVICE_SRC_IP_ALT_HASH)) {
1953  value_element->source_ip_value = htonl(value);
1954  value_element->dest_ip_value = 0;
1955  value_element->source_port_value = 0;
1956  value_element->dest_port_value = 0;
1957  } else if ((service_list_ptr->info.service == WCCP2_SERVICE_STANDARD) || (service_flags & WCCP2_SERVICE_DST_IP_HASH) || (service_flags & WCCP2_SERVICE_DST_IP_ALT_HASH)) {
1958  value_element->source_ip_value = 0;
1959  value_element->dest_ip_value = htonl(value);
1960  value_element->source_port_value = 0;
1961  value_element->dest_port_value = 0;
1962  } else if ((service_flags & WCCP2_SERVICE_SRC_PORT_HASH) || (service_flags & WCCP2_SERVICE_SRC_PORT_ALT_HASH)) {
1963  value_element->source_ip_value = 0;
1964  value_element->dest_ip_value = 0;
1965  value_element->source_port_value = htons(value);
1966  value_element->dest_port_value = 0;
1967  } else if ((service_flags & WCCP2_SERVICE_DST_PORT_HASH) || (service_flags & WCCP2_SERVICE_DST_PORT_ALT_HASH)) {
1968  value_element->source_ip_value = 0;
1969  value_element->dest_ip_value = 0;
1970  value_element->source_port_value = 0;
1971  value_element->dest_port_value = htons(value);
1972  } else {
1973  fatalf("Unknown service hash method\n");
1974  }
1975 
1976  value_element->cache_ip = cache_list_ptr->cache_ip;
1977 
1978  offset += sizeof(struct wccp2_value_element_t);
1979  ++value;
1980 
1981  /* Assign the next value to the next cache */
1982 
1983  if ((cache_list_ptr->next) && (cache_list_ptr->next->next))
1984  cache_list_ptr = cache_list_ptr->next;
1985  else
1986  cache_list_ptr = &router_list_ptr->cache_list_head;
1987  }
1988 
1989  /* Fill in length */
1990  alt_assignment_type_header->length = htons(offset - alt_assignment_offset);
1991 
1992  break;
1993 
1994  default:
1995  fatalf("Unknown Wccp2 assignment method\n");
1996  }
1997 
1998  /* Fill in length */
1999 
2000  assignment_header->length = htons(offset - assignment_offset);
2001 
2002  /* Fill in assignment key */
2003  assignment_key->master_ip = router_list_ptr->local_ip;
2004 
2005  /* finish length */
2006 
2007  main_header->length = htons(offset - sizeof(struct wccp2_message_header_t));
2008 
2009  /* set the destination address */
2010  router.sin_addr = router_list_ptr->router_sendto_address;
2011 
2012  /* Security update, if needed */
2013 
2014  if (service_list_ptr->wccp2_security_type == WCCP2_MD5_SECURITY) {
2015  wccp2_update_md5_security(service_list_ptr->wccp_password, (char *) security, wccp_packet, offset);
2016  }
2017 
2018  if (ntohl(router_list_ptr->num_caches)) {
2019  /* send packet */
2020 
2021  // XXX: drop temp conversion
2022  Ip::Address tmp_rtr(router);
2023 
2024  if (wccp2_numrouters > 1) {
2026  tmp_rtr,
2027  &wccp_packet,
2028  offset);
2029  } else {
2030  if (send(theWccp2Connection, &wccp_packet, offset, 0) < static_cast<int>(offset)) {
2031  int xerrno = errno;
2032  debugs(80, 2, "ERROR: failed to send WCCPv2 HERE_I_AM packet to " << tmp_rtr << " : " << xstrerr(xerrno));
2033  }
2034  }
2035  }
2036  safe_free(weight);
2037  }
2038 
2039  service_list_ptr = service_list_ptr->next;
2040  }
2041 }
2042 
2043 /*
2044  * Configuration option parsing code
2045  */
2046 
2052 void
2054 {
2055  char *t;
2056 
2057  /* Snarf the method */
2058  if ((t = ConfigParser::NextToken()) == NULL) {
2059  debugs(80, DBG_CRITICAL, "ERROR: wccp2_*_method: missing setting.");
2060  self_destruct();
2061  return;
2062  }
2063 
2064  /* update configuration if its valid */
2065  if (strcmp(t, "gre") == 0 || strcmp(t, "1") == 0) {
2066  *method = WCCP2_METHOD_GRE;
2067  } else if (strcmp(t, "l2") == 0 || strcmp(t, "2") == 0) {
2068  *method = WCCP2_METHOD_L2;
2069  } else {
2070  debugs(80, DBG_CRITICAL, "ERROR: wccp2_*_method: unknown setting, got " << t );
2071  self_destruct();
2072  }
2073 }
2074 
2075 void
2076 dump_wccp2_method(StoreEntry * e, const char *label, int v)
2077 {
2078  switch (v) {
2079  case WCCP2_METHOD_GRE:
2080  storeAppendPrintf(e, "%s gre\n", label);
2081  break;
2082  case WCCP2_METHOD_L2:
2083  storeAppendPrintf(e, "%s l2\n", label);
2084  break;
2085  default:
2086  debugs(80, DBG_CRITICAL, "FATAL: WCCPv2 configured method (" << v << ") is not valid.");
2087  self_destruct();
2088  }
2089 }
2090 
2091 void
2093 { }
2094 
2100 void
2102 {
2103  char *t;
2104 
2105  /* Snarf the method */
2106  if ((t = ConfigParser::NextToken()) == NULL) {
2107  debugs(80, DBG_CRITICAL, "ERROR: wccp2_assignment_method: missing setting.");
2108  self_destruct();
2109  return;
2110  }
2111 
2112  /* update configuration if its valid */
2113  if (strcmp(t, "hash") == 0 || strcmp(t, "1") == 0) {
2114  *method = WCCP2_ASSIGNMENT_METHOD_HASH;
2115  } else if (strcmp(t, "mask") == 0 || strcmp(t, "2") == 0) {
2116  *method = WCCP2_ASSIGNMENT_METHOD_MASK;
2117  } else {
2118  debugs(80, DBG_CRITICAL, "ERROR: wccp2_assignment_method: unknown setting, got " << t );
2119  self_destruct();
2120  }
2121 }
2122 
2123 void
2124 dump_wccp2_amethod(StoreEntry * e, const char *label, int v)
2125 {
2126  switch (v) {
2128  storeAppendPrintf(e, "%s hash\n", label);
2129  break;
2131  storeAppendPrintf(e, "%s mask\n", label);
2132  break;
2133  default:
2134  debugs(80, DBG_CRITICAL, "FATAL: WCCPv2 configured " << label << " (" << v << ") is not valid.");
2135  self_destruct();
2136  }
2137 }
2138 
2139 void
2141 {}
2142 
2143 /*
2144  * Format:
2145  *
2146  * wccp2_service {standard|dynamic} {id} (password=password)
2147  */
2148 void
2150 {
2151  char *t;
2152  int service = 0;
2153  int service_id = 0;
2154  int security_type = WCCP2_NO_SECURITY;
2155  char wccp_password[WCCP2_PASSWORD_LEN];
2156 
2157  if (wccp2_connected == 1) {
2158  debugs(80, DBG_IMPORTANT, "WCCPv2: Somehow reparsing the configuration without having shut down WCCP! Try reloading squid again.");
2159  return;
2160  }
2161 
2162  /* Snarf the type */
2163  if ((t = ConfigParser::NextToken()) == NULL) {
2164  debugs(80, DBG_CRITICAL, "ERROR: wccp2ParseServiceInfo: missing service info type (standard|dynamic)");
2165  self_destruct();
2166  return;
2167  }
2168 
2169  if (strcmp(t, "standard") == 0) {
2170  service = WCCP2_SERVICE_STANDARD;
2171  } else if (strcmp(t, "dynamic") == 0) {
2172  service = WCCP2_SERVICE_DYNAMIC;
2173  } else {
2174  debugs(80, DBG_CRITICAL, "ERROR: wccp2ParseServiceInfo: bad service info type (expected standard|dynamic, got " << t << ")");
2175  self_destruct();
2176  return;
2177  }
2178 
2179  /* Snarf the ID */
2180  service_id = GetInteger();
2181 
2182  if (service_id < 0 || service_id > 255) {
2183  debugs(80, DBG_CRITICAL, "ERROR: invalid WCCP service id " << service_id << " (must be between 0 .. 255)");
2184  self_destruct();
2185  return;
2186  }
2187 
2188  memset(wccp_password, 0, sizeof(wccp_password));
2189  /* Handle password, if any */
2190 
2191  if ((t = ConfigParser::NextToken()) != NULL) {
2192  if (strncmp(t, "password=", 9) == 0) {
2193  security_type = WCCP2_MD5_SECURITY;
2194  xstrncpy(wccp_password, t + 9, sizeof(wccp_password));
2195  }
2196  }
2197 
2198  /* Create a placeholder service record */
2199  wccp2_add_service_list(service, service_id, 0, 0, 0, empty_portlist, security_type, wccp_password);
2200 }
2201 
2202 void
2203 dump_wccp2_service(StoreEntry * e, const char *label, void *)
2204 {
2205  struct wccp2_service_list_t *srv;
2207 
2208  while (srv != NULL) {
2209  debugs(80, 3, "dump_wccp2_service: id " << srv->info.service_id << ", type " << srv->info.service);
2210  storeAppendPrintf(e, "%s %s %d", label,
2211  (srv->info.service == WCCP2_SERVICE_DYNAMIC) ? "dynamic" : "standard",
2212  srv->info.service_id);
2213 
2215  storeAppendPrintf(e, " %s", srv->wccp_password);
2216  }
2217 
2218  storeAppendPrintf(e, "\n");
2219 
2220  srv = srv->next;
2221  }
2222 }
2223 
2224 void
2226 {}
2227 
2228 int
2230 {
2231  return !wccp2_service_list_head;
2232 }
2233 
2234 /*
2235  * Format:
2236  *
2237  * wccp2_service_info {id} stuff..
2238  *
2239  * Where stuff is:
2240  *
2241  * + flags=flag,flag,flag..
2242  * + proto=protocol (tcp|udp)
2243  * + ports=port,port,port (up to a max of 8)
2244  * + priority=priority (0->255)
2245  *
2246  * The flags here are:
2247  * src_ip_hash, dst_ip_hash, source_port_hash, dst_port_hash, ports_defined,
2248  * ports_source, src_ip_alt_hash, dst_ip_alt_hash, src_port_alt_hash, dst_port_alt_hash
2249  */
2250 static int
2252 {
2253  if (!flags)
2254  return 0;
2255 
2256  char *flag = flags;
2257  int retflag = 0;
2258 
2259  while (size_t len = strcspn(flag, ",")) {
2260 
2261  if (strncmp(flag, "src_ip_hash", len) == 0) {
2262  retflag |= WCCP2_SERVICE_SRC_IP_HASH;
2263  } else if (strncmp(flag, "dst_ip_hash", len) == 0) {
2264  retflag |= WCCP2_SERVICE_DST_IP_HASH;
2265  } else if (strncmp(flag, "source_port_hash", len) == 0) {
2266  retflag |= WCCP2_SERVICE_SRC_PORT_HASH;
2267  } else if (strncmp(flag, "dst_port_hash", len) == 0) {
2268  retflag |= WCCP2_SERVICE_DST_PORT_HASH;
2269  } else if (strncmp(flag, "ports_source", len) == 0) {
2270  retflag |= WCCP2_SERVICE_PORTS_SOURCE;
2271  } else if (strncmp(flag, "src_ip_alt_hash", len) == 0) {
2272  retflag |= WCCP2_SERVICE_SRC_IP_ALT_HASH;
2273  } else if (strncmp(flag, "dst_ip_alt_hash", len) == 0) {
2274  retflag |= WCCP2_SERVICE_DST_IP_ALT_HASH;
2275  } else if (strncmp(flag, "src_port_alt_hash", len) == 0) {
2277  } else if (strncmp(flag, "dst_port_alt_hash", len) == 0) {
2279  } else {
2280  flag[len] = '\0';
2281  fatalf("Unknown wccp2 service flag: %s\n", flag);
2282  }
2283 
2284  if (flag[len] == '\0')
2285  break;
2286 
2287  flag += len+1;
2288  }
2289 
2290  return retflag;
2291 }
2292 
2293 static void
2294 parse_wccp2_service_ports(char *options, int portlist[])
2295 {
2296  if (!options) {
2297  return;
2298  }
2299 
2300  int i = 0;
2301  char *tmp = options;
2302  static char copy[10];
2303 
2304  while (size_t len = strcspn(tmp, ",")) {
2305  if (i >= WCCP2_NUMPORTS) {
2306  fatalf("parse_wccp2_service_ports: too many ports (maximum: 8) in list '%s'\n", options);
2307  }
2308  if (len > 6) { // 6 because "65535,"
2309  fatalf("parse_wccp2_service_ports: port value '%s' isn't valid (1..65535)\n", tmp);
2310  }
2311 
2312  memcpy(copy, tmp, len);
2313  copy[len] = '\0';
2314  int p = xatoi(copy);
2315 
2316  if (p < 1 || p > 65535) {
2317  fatalf("parse_wccp2_service_ports: port value '%s' isn't valid (1..65535)\n", tmp);
2318  }
2319 
2320  portlist[i] = p;
2321  ++i;
2322  if (tmp[len] == '\0')
2323  return;
2324  tmp += len+1;
2325  }
2326 }
2327 
2328 void
2330 {
2331  char *t, *end;
2332  int service_id = 0;
2333  int flags = 0;
2334  int portlist[WCCP2_NUMPORTS];
2335  int protocol = -1; /* IPPROTO_TCP | IPPROTO_UDP */
2336 
2337  struct wccp2_service_list_t *srv;
2338  int priority = -1;
2339 
2340  if (wccp2_connected == 1) {
2341  debugs(80, DBG_IMPORTANT, "WCCPv2: Somehow reparsing the configuration without having shut down WCCP! Try reloading squid again.");
2342  return;
2343  }
2344 
2345  debugs(80, 5, "parse_wccp2_service_info: called");
2346  memset(portlist, 0, sizeof(portlist));
2347  /* First argument: id */
2348  service_id = GetInteger();
2349 
2350  if (service_id < 0 || service_id > 255) {
2351  debugs(80, DBG_CRITICAL, "ERROR: invalid WCCP service id " << service_id << " (must be between 0 .. 255)");
2352  self_destruct();
2353  return;
2354  }
2355 
2356  /* Next: find the (hopefully!) existing service */
2358 
2359  if (srv == NULL) {
2360  fatalf("parse_wccp2_service_info: unknown dynamic service id %d: you need to define it using wccp2_service (and make sure you wish to configure it as a dynamic service.)\n", service_id);
2361  }
2362 
2363  /* Next: loop until we don't have any more tokens */
2364  while ((t = ConfigParser::NextToken()) != NULL) {
2365  if (strncmp(t, "flags=", 6) == 0) {
2366  /* XXX eww, string pointer math */
2367  flags = parse_wccp2_service_flags(t + 6);
2368  } else if (strncmp(t, "ports=", 6) == 0) {
2369  parse_wccp2_service_ports(t + 6, portlist);
2370  flags |= WCCP2_SERVICE_PORTS_DEFINED;
2371  } else if (strncmp(t, "protocol=tcp", 12) == 0) {
2372  protocol = IPPROTO_TCP;
2373  } else if (strncmp(t, "protocol=udp", 12) == 0) {
2374  protocol = IPPROTO_UDP;
2375  } else if (strncmp(t, "protocol=", 9) == 0) {
2376  fatalf("parse_wccp2_service_info: id %d: unknown protocol (%s) - must be tcp or udp!\n", service_id, t);
2377  } else if (strncmp(t, "priority=", 9) == 0) {
2378  priority = strtol(t + 9, &end, 0);
2379 
2380  if (priority < 0 || priority > 255) {
2381  fatalf("parse_wccp2_service_info: id %d: %s out of range (0..255)!\n", service_id, t);
2382  }
2383  } else {
2384  fatalf("parse_wccp2_service_info: id %d: unknown option '%s'\n", service_id, t);
2385  }
2386  }
2387 
2388  /* Check everything is set */
2389  if (priority == -1) {
2390  fatalf("parse_wccp2_service_info: service %d: no priority defined (valid: 0..255)!\n", service_id);
2391  }
2392 
2393  if (protocol == -1) {
2394  fatalf("parse_wccp2_service_info: service %d: no protocol defined (valid: tcp or udp)!\n", service_id);
2395  }
2396 
2397  if (!(flags & WCCP2_SERVICE_PORTS_DEFINED)) {
2398  fatalf("parse_wccp2_service_info: service %d: no ports defined!\n", service_id);
2399  }
2400 
2401  /* rightio! now we can update */
2402  wccp2_update_service(srv, WCCP2_SERVICE_DYNAMIC, service_id, priority,
2403  protocol, flags, portlist);
2404 
2405  /* Done! */
2406 }
2407 
2408 void
2409 dump_wccp2_service_info(StoreEntry * e, const char *label, void *)
2410 {
2411  struct wccp2_service_list_t *srv;
2412  int flags;
2414 
2415  while (srv != NULL) {
2416  debugs(80, 3, "dump_wccp2_service_info: id " << srv->info.service_id << " (type " << srv->info.service << ")");
2417 
2418  /* We don't need to spit out information for standard services */
2419 
2420  if (srv->info.service == WCCP2_SERVICE_STANDARD) {
2421  debugs(80, 3, "dump_wccp2_service_info: id " << srv->info.service_id << ": standard service, not dumping info");
2422 
2423  /* XXX eww */
2424  srv = srv->next;
2425  continue;
2426  }
2427 
2428  storeAppendPrintf(e, "%s %d", label, srv->info.service_id);
2429 
2430  /* priority */
2431  storeAppendPrintf(e, " priority=%d", srv->info.service_priority);
2432 
2433  /* flags */
2434  flags = ntohl(srv->info.service_flags);
2435 
2436  bool comma = false;
2437  if (flags != 0) {
2438  storeAppendPrintf(e, " flags=");
2439 
2440  if (flags & WCCP2_SERVICE_SRC_IP_HASH) {
2441  storeAppendPrintf(e, "src_ip_hash");
2442  comma = true;
2443  }
2444 
2445  if (flags & WCCP2_SERVICE_DST_IP_HASH) {
2446  storeAppendPrintf(e, "%sdst_ip_hash", comma ? "," : "");
2447  comma = true;
2448  }
2449 
2450  if (flags & WCCP2_SERVICE_SRC_PORT_HASH) {
2451  storeAppendPrintf(e, "%ssource_port_hash", comma ? "," : "");
2452  comma = true;
2453  }
2454 
2455  if (flags & WCCP2_SERVICE_DST_PORT_HASH) {
2456  storeAppendPrintf(e, "%sdst_port_hash", comma ? "," : "");
2457  comma = true;
2458  }
2459 
2460  if (flags & WCCP2_SERVICE_PORTS_DEFINED) {
2461  storeAppendPrintf(e, "%sports_defined", comma ? "," : "");
2462  comma = true;
2463  }
2464 
2465  if (flags & WCCP2_SERVICE_PORTS_SOURCE) {
2466  storeAppendPrintf(e, "%sports_source", comma ? "," : "");
2467  comma = true;
2468  }
2469 
2470  if (flags & WCCP2_SERVICE_SRC_IP_ALT_HASH) {
2471  storeAppendPrintf(e, "%ssrc_ip_alt_hash", comma ? "," : "");
2472  comma = true;
2473  }
2474 
2475  if (flags & WCCP2_SERVICE_DST_IP_ALT_HASH) {
2476  storeAppendPrintf(e, "%ssrc_ip_alt_hash", comma ? "," : "");
2477  comma = true;
2478  }
2479 
2480  if (flags & WCCP2_SERVICE_SRC_PORT_ALT_HASH) {
2481  storeAppendPrintf(e, "%ssrc_port_alt_hash", comma ? "," : "");
2482  comma = true;
2483  }
2484 
2485  if (flags & WCCP2_SERVICE_DST_PORT_ALT_HASH) {
2486  storeAppendPrintf(e, "%sdst_port_alt_hash", comma ? "," : "");
2487  //comma = true; // uncomment if more options added
2488  }
2489  }
2490 
2491  /* ports */
2492  comma = false;
2493 
2494  if (srv->info.port0 != 0) {
2495  storeAppendPrintf(e, " ports=%d", ntohs(srv->info.port0));
2496  comma = true;
2497  }
2498 
2499  if (srv->info.port1 != 0) {
2500  storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port1));
2501  comma = true;
2502  }
2503 
2504  if (srv->info.port2 != 0) {
2505  storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port2));
2506  comma = true;
2507  }
2508 
2509  if (srv->info.port3 != 0) {
2510  storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port3));
2511  comma = true;
2512  }
2513 
2514  if (srv->info.port4 != 0) {
2515  storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port4));
2516  comma = true;
2517  }
2518 
2519  if (srv->info.port5 != 0) {
2520  storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port5));
2521  comma = true;
2522  }
2523 
2524  if (srv->info.port6 != 0) {
2525  storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port6));
2526  comma = true;
2527  }
2528 
2529  if (srv->info.port7 != 0) {
2530  storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port7));
2531  // comma = true; // uncomment if more options are added
2532  }
2533 
2534  /* protocol */
2535  storeAppendPrintf(e, " protocol=%s", (srv->info.service_protocol == IPPROTO_TCP) ? "tcp" : "udp");
2536 
2537  storeAppendPrintf(e, "\n");
2538 
2539  srv = srv->next;
2540  }
2541 }
2542 
2543 /* Sort the cache list by doing a "selection sort" by IP address */
2544 static void
2546 {
2547  struct wccp2_cache_list_t tmp;
2548  struct wccp2_cache_list_t *this_item;
2549  struct wccp2_cache_list_t *find_item;
2550  struct wccp2_cache_list_t *next_lowest;
2551 
2552  /* Go through each position in the list one at a time */
2553  for (this_item = head; this_item->next; this_item = this_item->next) {
2554  /* Find the item with the lowest IP */
2555  next_lowest = this_item;
2556 
2557  for (find_item = this_item; find_item->next; find_item = find_item->next) {
2558  if (find_item->cache_ip.s_addr < next_lowest->cache_ip.s_addr) {
2559  next_lowest = find_item;
2560  }
2561  }
2562  /* Swap if we need to */
2563  if (next_lowest != this_item) {
2564  /* First make a copy of the current item */
2565  memcpy(&tmp, this_item, sizeof(struct wccp2_cache_list_t));
2566 
2567  /* Next update the pointers to maintain the linked list */
2568  tmp.next = next_lowest->next;
2569  next_lowest->next = this_item->next;
2570 
2571  /* Finally copy the updated items to their correct location */
2572  memcpy(this_item, next_lowest, sizeof(struct wccp2_cache_list_t));
2573  memcpy(next_lowest, &tmp, sizeof(struct wccp2_cache_list_t));
2574  }
2575  }
2576 }
2577 
2578 void
2580 {}
2581 
2582 #endif /* USE_WCCPv2 */
2583 
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:153
void dump_wccp2_method(StoreEntry *e, const char *label, int v)
Definition: wccp2.cc:2076
#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:40
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:2124
#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:2140
uint8_t service_protocol
Definition: wccp2.cc:137
void eventDelete(EVH *func, void *arg)
Definition: event.cc:129
#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:2092
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:170
#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:830
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:890
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:370
#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:2294
uint16_t source_port_value
Definition: wccp2.cc:330
int assignment_method
Definition: SquidConfig.h:172
uint32_t member_change
Definition: wccp2.cc:426
int check_null_wccp2_service(void *)
Definition: wccp2.cc:2229
bool getInAddr(struct in_addr &) const
Definition: Address.cc:1020
void parse_wccp2_method(int *method)
Definition: wccp2.cc:2053
struct wccp2_router_id_element_t router_id_element
Definition: wccp2.cc:368
void parse_wccp2_amethod(int *method)
Definition: wccp2.cc:2101
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:27
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:65
static int port
Definition: ldap_backend.cc:69
static int parse_wccp2_service_flags(char *flags)
Definition: wccp2.cc:2251
int socklen_t
Definition: types.h:158
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:233
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:170
static struct wccp2_capability_element_t wccp2_capability_element
Definition: wccp2.cc:302
#define NULL
Definition: types.h:166
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:160
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:778
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:2225
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:19
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:2329
bool setIPv4()
Definition: Address.cc:224
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:2203
#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:171
static int store_dirs_rebuilding
the number of cache_dirs being rebuilt; TODO: move to Disks::Rebuilding
Definition: Controller.h:139
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
#define WCCP2_CAPABILITY_INFO
Definition: wccp2.cc:75
struct SquidConfig::@100 Wccp2
uint16_t cache_identity_type
Definition: wccp2.cc:241
void dump_wccp2_service_info(StoreEntry *e, const char *label, void *)
Definition: wccp2.cc:2409
#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:2579
#define WCCP2_SERVICE_DYNAMIC
Definition: wccp2.cc:150
squidaio_request_t * head
Definition: aiops.cc:126
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:21
void SetSelect(int, unsigned int, PF *, void *, time_t)
Mark an FD to be watched for its IO status.
Definition: ModDevPoll.cc:223
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:161
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:41
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:122
#define WCCP2_ALT_ASSIGNMENT
Definition: wccp2.cc:76
void parse_wccp2_service(void *)
Definition: wccp2.cc:2149
#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:924
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:196
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:2545
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

Web Site Translations

Mirrors