2 * DEBUG: section 80 WCCP Support
3 * AUTHOR: Steven Wilton
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
38 #include "comm/Connection.h"
39 #include "comm/Loops.h"
40 #include "compat/strsep.h"
42 #include "ip/Address.h"
53 #define WCCP_PORT 2048
54 #define WCCP_RESPONSE_SIZE 12448
55 #define WCCP_BUCKETS 256
57 static int theWccp2Connection
= -1;
58 static int wccp2_connected
= 0;
60 static PF wccp2HandleUdp
;
61 static EVH wccp2HereIam
;
62 static EVH wccp2AssignBuckets
;
66 #define WCCP2_HASH_ASSIGNMENT 0x00
67 #define WCCP2_MASK_ASSIGNMENT 0x01
69 #define WCCP2_NONE_SECURITY_LEN 0
70 #define WCCP2_MD5_SECURITY_LEN 16
73 #define WCCP2_NUMPORTS 8
74 #define WCCP2_PASSWORD_LEN 8
77 /* WCCPv2 Pakcet format structures */
78 /* Defined in draft-wilson-wccp-v2-12-oct-2001.txt */
81 /** \interface WCCPv2_Protocol
82 * Generic header struct
84 struct wccp2_item_header_t
{
89 /* item type values */
90 #define WCCP2_SECURITY_INFO 0
91 #define WCCP2_SERVICE_INFO 1
92 #define WCCP2_ROUTER_ID_INFO 2
93 #define WCCP2_WC_ID_INFO 3
94 #define WCCP2_RTR_VIEW_INFO 4
95 #define WCCP2_WC_VIEW_INFO 5
96 #define WCCP2_REDIRECT_ASSIGNMENT 6
97 #define WCCP2_QUERY_INFO 7
98 #define WCCP2_CAPABILITY_INFO 8
99 #define WCCP2_ALT_ASSIGNMENT 13
100 #define WCCP2_ASSIGN_MAP 14
101 #define WCCP2_COMMAND_EXTENSION 15
105 /** \interface WCCPv2_Protocol
106 * Sect 5.5 WCCP Message Header
108 struct wccp2_message_header_t
{
111 #define WCCP2_VERSION 0x200
115 static struct wccp2_message_header_t wccp2_here_i_am_header
;
118 #define WCCP2_HERE_I_AM 10
119 #define WCCP2_I_SEE_YOU 11
120 #define WCCP2_REDIRECT_ASSIGN 12
121 #define WCCP2_REMOVAL_QUERY 13
124 /** \interface WCCPv2_Protocol
125 * Sect 5.6.1 Security Info Component
127 * Basic security Header. Matches "no security" case exactly.
129 struct wccp2_security_none_t
{
130 uint16_t security_type
;
131 uint16_t security_length
;
132 uint32_t security_option
;
135 /* security options */
136 #define WCCP2_NO_SECURITY 0
137 #define WCCP2_MD5_SECURITY 1
140 /** \interface WCCPv2_Protocol
141 * Sect 5.6.1 Security Info Component
143 * Extended security section. Matches "MD5 security" type exactly.
144 * Including the security header.
146 struct wccp2_security_md5_t
{
147 uint16_t security_type
;
148 uint16_t security_length
;
149 uint32_t security_option
;
150 uint8_t security_implementation
[WCCP2_MD5_SECURITY_LEN
];
153 /* Service info struct */
155 /** \interface WCCPv2_Protocol
156 * Sect 5.6.2 Service Info Component
158 struct wccp2_service_info_t
{
159 uint16_t service_type
;
160 uint16_t service_length
;
163 uint8_t service_priority
;
164 uint8_t service_protocol
;
165 uint32_t service_flags
;
176 #define WCCP2_SERVICE_STANDARD 0
177 #define WCCP2_SERVICE_DYNAMIC 1
180 #define WCCP2_SERVICE_ID_HTTP 0x00
183 #define WCCP2_SERVICE_SRC_IP_HASH 0x1
184 #define WCCP2_SERVICE_DST_IP_HASH 0x2
185 #define WCCP2_SERVICE_SRC_PORT_HASH 0x4
186 #define WCCP2_SERVICE_DST_PORT_HASH 0x8
187 #define WCCP2_SERVICE_PORTS_DEFINED 0x10
188 #define WCCP2_SERVICE_PORTS_SOURCE 0x20
189 #define WCCP2_SERVICE_SRC_IP_ALT_HASH 0x100
190 #define WCCP2_SERVICE_DST_IP_ALT_HASH 0x200
191 #define WCCP2_SERVICE_SRC_PORT_ALT_HASH 0x400
192 #define WCCP2_SERVICE_DST_PORT_ALT_HASH 0x800
195 /* TODO the following structures need to be re-defined for correct full operation.
196 wccp2_cache_identity_element needs to be merged as a sub-struct of
197 wccp2_identity_info_t (identity_type); which frees up the identifty info
198 structures so mask_assigment_data_element can become variable length
199 and cope with multiple fail-over caches hanging off one router.
202 /** \interface WCCPv2_Protocol
203 * Sect 5.7.2 Web-Cache Identity Element
205 struct wccp2_cache_identity_info_t
{
207 uint16_t hash_revision
;
209 //#define WCCP2_HASH_ASSIGNMENT_DATA 0x0
211 /* 5.7.2 Hash Assignment Data Element */
212 char buckets
[32]; /* Draft indicates 8x 32-bit buckets but it's just a mask so doesn't matter how we define. */
217 /** \interface WCCPv2_Protocol
218 * Sect 5.6.4 Web-Cache Identity Info Component
220 struct wccp2_identity_info_t
{
221 uint16_t cache_identity_type
;
222 uint16_t cache_identity_length
;
224 struct wccp2_cache_identity_info_t cache_identity
;
227 static struct wccp2_identity_info_t wccp2_identity_info
;
229 /** \interface WCCPv2_Protocol
230 * Sect 5.7.7 Mask Element
232 struct wccp2_mask_element_t
{
233 uint32_t source_ip_mask
;
234 uint32_t dest_ip_mask
;
235 uint16_t source_port_mask
;
236 uint16_t dest_port_mask
;
237 uint32_t number_values
;
241 /** \interface WCCPv2_Protocol
242 * Sect 5.7.2 Web-Cache Identity Element
244 struct wccp2_cache_mask_identity_info_t
{
246 uint16_t hash_revision
;
248 #define WCCP2_MASK_ASSIGNMENT_DATA (0x2)
250 /* Sect 5.7.2 Mask Assignment Data Element
252 * NP: draft specifies a variable-length set of keys here.
253 * the following fields only matche the special case Squid sends outbound (single-cache).
255 uint32_t mask_element_count
;
257 /* Sect 5.7.6 Mask/Value Set Element */
258 /* special case: single mask element. no values. */
259 struct wccp2_mask_element_t mask
;
261 /* Sect 5.7.2 Mask Assignment Data Element */
266 /** \interface WCCPv2_Protocol
267 * Sect 5.6.4 Web-Cache Identity Info Component
269 struct wccp2_mask_identity_info_t
{
270 uint16_t cache_identity_type
;
271 uint16_t cache_identity_length
;
273 struct wccp2_cache_mask_identity_info_t cache_identity
;
276 static struct wccp2_mask_identity_info_t wccp2_mask_identity_info
;
278 /** \interface WCCPv2_Protocol
279 * Sect 5.6.5 Router View Info Component
280 * Sect 5.6.6 Web Cache View Info Component
282 * first three fields. (shared by both view components)
284 struct wccp2_cache_view_header_t
{
285 uint16_t cache_view_type
;
286 uint16_t cache_view_length
;
287 uint32_t cache_view_version
;
290 static struct wccp2_cache_view_header_t wccp2_cache_view_header
;
292 /// \interface WCCPv2_Protocol
293 /* NP: special-case 5.6.5 or 5.6.6 * View Info when no routers or caches are advertised? */
294 struct wccp2_cache_view_info_t
{
295 uint32_t num_routers
;
299 static struct wccp2_cache_view_info_t wccp2_cache_view_info
;
301 /** \interface WCCPv2_Protocol
302 * Sect 5.7.1 Router ID Element
304 struct wccp2_router_id_element_t
{
305 struct in_addr router_address
;
306 uint32_t received_id
;
309 static struct wccp2_router_id_element_t wccp2_router_id_element
;
311 /** \interface WCCPv2_Protocol
312 * Sect 5.6.9 Capabilities Info Component
314 struct wccp2_capability_info_header_t
{
315 uint16_t capability_info_type
;
316 uint16_t capability_info_length
;
317 /* dynamic length capabilities list */
320 static struct wccp2_capability_info_header_t wccp2_capability_info_header
;
322 /** \interface WCCPv2_Protocol
323 * 5.7.5 Capability Element
325 struct wccp2_capability_element_t
{
326 uint16_t capability_type
;
327 uint16_t capability_length
;
328 uint32_t capability_value
;
330 static struct wccp2_capability_element_t wccp2_capability_element
;
332 /* capability types */
333 #define WCCP2_CAPABILITY_FORWARDING_METHOD 0x01
334 #define WCCP2_CAPABILITY_ASSIGNMENT_METHOD 0x02
335 #define WCCP2_CAPABILITY_RETURN_METHOD 0x03
336 // 0x04 ?? - advertised by a 4507 (ios v15.1) Cisco switch
337 // 0x05 ?? - advertised by a 4507 (ios v15.1) Cisco switch
339 /* capability values */
340 #define WCCP2_METHOD_GRE 0x00000001
341 #define WCCP2_METHOD_L2 0x00000002
342 /* when type=WCCP2_CAPABILITY_FORWARDING_METHOD */
343 #define WCCP2_FORWARDING_METHOD_GRE WCCP2_METHOD_GRE
344 #define WCCP2_FORWARDING_METHOD_L2 WCCP2_METHOD_L2
345 /* when type=WCCP2_CAPABILITY_ASSIGNMENT_METHOD */
346 #define WCCP2_ASSIGNMENT_METHOD_HASH 0x00000001
347 #define WCCP2_ASSIGNMENT_METHOD_MASK 0x00000002
348 /* when type=WCCP2_CAPABILITY_RETURN_METHOD */
349 #define WCCP2_PACKET_RETURN_METHOD_GRE WCCP2_METHOD_GRE
350 #define WCCP2_PACKET_RETURN_METHOD_L2 WCCP2_METHOD_L2
354 /** \interface WCCPv2_Protocol
355 * 5.7.8 Value Element
357 struct wccp2_value_element_t
{
358 uint32_t source_ip_value
;
359 uint32_t dest_ip_value
;
360 uint16_t source_port_value
;
361 uint16_t dest_port_value
;
363 struct in_addr cache_ip
;
366 /* RECEIVED PACKET STRUCTURE */
368 /** \interface WCCPv2_Protocol
369 * 5.2 'I See You' Message
371 struct wccp2_i_see_you_t
{
375 char data
[WCCP_RESPONSE_SIZE
];
378 static struct wccp2_i_see_you_t wccp2_i_see_you
;
380 /** \interface WCCPv2_Protocol
381 * 5.7.4 Router Assignment Element
383 struct wccp2_router_assign_element_t
{
384 struct in_addr router_address
;
385 uint32_t received_id
;
386 uint32_t change_number
;
389 /* Router identity struct */
391 /** \interface WCCPv2_Protocol
392 * 5.6.3 Router Identity Info Component (partial)
394 struct router_identity_info_t
{
396 struct wccp2_item_header_t header
;
398 struct wccp2_router_id_element_t router_id_element
;
400 struct in_addr router_address
;
401 uint32_t number_caches
;
402 /* dynamic list of cache IP addresses */
405 /* The received packet for a mask assignment is unusual */
407 /** \interface WCCPv2_Protocol
408 * Sect 5.7.7 Mask Element ???
409 * see code below. apparently the supposed IP address at position num1 can be equal to 3.
411 struct cache_mask_info_t
{
418 /** \interface WCCPv2_Protocol
419 * 5.7.3 Assignment Key Element
421 struct assignment_key_t
{
422 struct in_addr master_ip
;
423 uint32_t master_number
;
426 /** \interface WCCPv2_Protocol
427 * 5.6.5 Router View Info Component (first three fields)
429 struct router_view_t
{
430 struct wccp2_item_header_t header
;
431 uint32_t change_number
;
432 struct assignment_key_t assignment_key
;
433 /* dynamic lists of routers and caches elided */
436 /* Lists used to keep track of caches, routers and services */
438 /// \interface WCCPv2_Protocol
439 struct wccp2_cache_list_t
{
441 struct in_addr cache_ip
;
445 struct wccp2_cache_list_t
*next
;
448 /// \interface WCCPv2_Protocol
449 struct wccp2_router_list_t
{
451 struct wccp2_router_id_element_t
*info
;
453 struct in_addr local_ip
;
455 struct in_addr router_sendto_address
;
456 uint32_t member_change
;
459 struct wccp2_cache_list_t cache_list_head
;
461 struct wccp2_router_list_t
*next
;
464 static int wccp2_numrouters
;
466 /// \interface WCCPv2_Protocol
467 struct wccp2_service_list_t
{
469 struct wccp2_service_info_t info
;
470 uint32_t num_routers
;
472 struct wccp2_router_list_t router_list_head
;
476 char *wccp2_identity_info_ptr
;
478 struct wccp2_security_md5_t
*security_info
;
480 struct wccp2_service_info_t
*service_info
;
481 char wccp_packet
[WCCP_RESPONSE_SIZE
];
482 size_t wccp_packet_size
;
484 struct wccp2_service_list_t
*next
;
485 char wccp_password
[WCCP2_PASSWORD_LEN
+ 1]; /* hold the trailing C-string NUL */
486 uint32_t wccp2_security_type
;
489 static struct wccp2_service_list_t
*wccp2_service_list_head
= NULL
;
491 int empty_portlist
[WCCP2_NUMPORTS
] = {0, 0, 0, 0, 0, 0, 0, 0};
493 /* END WCCP V2 PROTOCL TYPES DEFINITION */
495 void wccp2_add_service_list(int service
, int service_id
, int service_priority
,
496 int service_proto
, int service_flags
, int ports
[], int security_type
, char *password
);
497 static void wccp2SortCacheList(struct wccp2_cache_list_t
*head
);
500 * The functions used during startup:
502 * wccp2ConnectionOpen
503 * wccp2ConnectionClose
507 wccp2InitServices(void)
509 debugs(80, 5, "wccp2InitServices: called");
513 wccp2_update_service(struct wccp2_service_list_t
*srv
, int service
,
514 int service_id
, int service_priority
, int service_proto
, int service_flags
,
517 /* XXX check what needs to be wrapped in htons()! */
518 srv
->info
.service
= service
;
519 srv
->info
.service_id
= service_id
;
520 srv
->info
.service_priority
= service_priority
;
521 srv
->info
.service_protocol
= service_proto
;
522 srv
->info
.service_flags
= htonl(service_flags
);
523 srv
->info
.port0
= htons(ports
[0]);
524 srv
->info
.port1
= htons(ports
[1]);
525 srv
->info
.port2
= htons(ports
[2]);
526 srv
->info
.port3
= htons(ports
[3]);
527 srv
->info
.port4
= htons(ports
[4]);
528 srv
->info
.port5
= htons(ports
[5]);
529 srv
->info
.port6
= htons(ports
[6]);
530 srv
->info
.port7
= htons(ports
[7]);
534 wccp2_add_service_list(int service
, int service_id
, int service_priority
,
535 int service_proto
, int service_flags
, int ports
[], int security_type
,
539 struct wccp2_service_list_t
*wccp2_service_list_ptr
;
541 wccp2_service_list_ptr
= (wccp2_service_list_t
*) xcalloc(1, sizeof(struct wccp2_service_list_t
));
543 debugs(80, 5, "wccp2_add_service_list: added service id " << service_id
);
545 /* XXX check what needs to be wrapped in htons()! */
546 wccp2_service_list_ptr
->info
.service_type
= htons(WCCP2_SERVICE_INFO
);
548 wccp2_service_list_ptr
->info
.service_length
= htons(sizeof(struct wccp2_service_info_t
) - 4);
549 wccp2_service_list_ptr
->change_num
= 0;
550 wccp2_update_service(wccp2_service_list_ptr
, service
, service_id
,
551 service_priority
, service_proto
, service_flags
, ports
);
552 wccp2_service_list_ptr
->wccp2_security_type
= security_type
;
553 memset(wccp2_service_list_ptr
->wccp_password
, 0, WCCP2_PASSWORD_LEN
+ 1);
554 strncpy(wccp2_service_list_ptr
->wccp_password
, password
, WCCP2_PASSWORD_LEN
);
555 /* add to linked list - XXX this should use the Squid dlink* routines! */
556 wccp2_service_list_ptr
->next
= wccp2_service_list_head
;
557 wccp2_service_list_head
= wccp2_service_list_ptr
;
560 static struct wccp2_service_list_t
*
561 wccp2_get_service_by_id(int service
, int service_id
) {
563 struct wccp2_service_list_t
*p
;
565 p
= wccp2_service_list_head
;
568 if (p
->info
.service
== service
&& p
->info
.service_id
== service_id
) {
579 * Update the md5 security header, if possible
581 * Returns: 1 if we set it, 0 if not (eg, no security section, or non-md5)
584 wccp2_update_md5_security(char *password
, char *ptr
, char *packet
, int len
)
586 uint8_t md5_digest
[16];
587 char pwd
[WCCP2_PASSWORD_LEN
];
590 struct wccp2_security_md5_t
*ws
;
592 debugs(80, 5, "wccp2_update_md5_security: called");
594 /* The password field, for the MD5 hash, needs to be 8 bytes and NUL padded. */
595 memset(pwd
, 0, sizeof(pwd
));
596 strncpy(pwd
, password
, sizeof(pwd
));
598 ws
= (struct wccp2_security_md5_t
*) ptr
;
599 assert(ntohs(ws
->security_type
) == WCCP2_SECURITY_INFO
);
600 /* Its the security part */
602 if (ntohl(ws
->security_option
) != WCCP2_MD5_SECURITY
) {
603 debugs(80, 5, "wccp2_update_md5_security: this service ain't md5'ing, abort");
607 /* And now its the MD5 section! */
608 /* According to the draft, the MD5 security hash is the combination of
609 * the 8-octet password (padded w/ NUL bytes) and the entire WCCP packet,
610 * including the WCCP message header. The WCCP security implementation
611 * area should be zero'ed before calculating the MD5 hash.
613 /* XXX eventually we should be able to kill md5_digest and blit it directly in */
614 memset(ws
->security_implementation
, 0, sizeof(ws
->security_implementation
));
618 SquidMD5Update(&M
, pwd
, 8);
620 SquidMD5Update(&M
, packet
, len
);
622 SquidMD5Final(md5_digest
, &M
);
624 memcpy(ws
->security_implementation
, md5_digest
, sizeof(md5_digest
));
632 * Check the given WCCP2 packet against the given password.
636 wccp2_check_security(struct wccp2_service_list_t
*srv
, char *security
, char *packet
, int len
)
639 struct wccp2_security_md5_t
*ws
= (struct wccp2_security_md5_t
*) security
;
640 uint8_t md5_digest
[16], md5_challenge
[16];
641 char pwd
[WCCP2_PASSWORD_LEN
];
644 /* Make sure the security type matches what we expect */
646 if (ntohl(ws
->security_option
) != srv
->wccp2_security_type
) {
647 debugs(80, DBG_IMPORTANT
, "wccp2_check_security: received packet has the wrong security option");
651 if (srv
->wccp2_security_type
== WCCP2_NO_SECURITY
) {
655 if (srv
->wccp2_security_type
!= WCCP2_MD5_SECURITY
) {
656 debugs(80, DBG_IMPORTANT
, "wccp2_check_security: invalid security option");
660 /* If execution makes it here then we have an MD5 security */
662 /* The password field, for the MD5 hash, needs to be 8 bytes and NUL padded. */
663 memset(pwd
, 0, sizeof(pwd
));
665 strncpy(pwd
, srv
->wccp_password
, sizeof(pwd
));
667 /* Take a copy of the challenge: we need to NUL it before comparing */
668 memcpy(md5_challenge
, ws
->security_implementation
, 16);
670 memset(ws
->security_implementation
, 0, sizeof(ws
->security_implementation
));
674 SquidMD5Update(&M
, pwd
, 8);
676 SquidMD5Update(&M
, packet
, len
);
678 SquidMD5Final(md5_digest
, &M
);
680 return (memcmp(md5_digest
, md5_challenge
, 16) == 0);
689 uint32_t service_flags
;
691 struct wccp2_service_list_t
*service_list_ptr
;
693 struct wccp2_router_list_t
*router_list_ptr
;
695 struct wccp2_security_md5_t wccp2_security_md5
;
697 debugs(80, 5, "wccp2Init: Called");
699 if (wccp2_connected
== 1)
702 wccp2_numrouters
= 0;
704 /* Calculate the number of routers configured in the config file */
705 for (s
= Config
.Wccp2
.router
; s
; s
= s
->next
) {
706 if (!s
->s
.IsAnyAddr()) {
707 /* Increment the counter */
712 if (wccp2_numrouters
== 0) {
716 /* Initialise the list of services */
719 service_list_ptr
= wccp2_service_list_head
;
721 while (service_list_ptr
!= NULL
) {
722 /* Set up our list pointers */
723 router_list_ptr
= &service_list_ptr
->router_list_head
;
725 /* start the wccp header */
726 wccp2_here_i_am_header
.type
= htonl(WCCP2_HERE_I_AM
);
727 wccp2_here_i_am_header
.version
= htons(WCCP2_VERSION
);
728 wccp2_here_i_am_header
.length
= 0;
729 ptr
= service_list_ptr
->wccp_packet
+ sizeof(wccp2_here_i_am_header
);
731 /* add the security section */
732 /* XXX this is ugly */
734 if (service_list_ptr
->wccp2_security_type
== WCCP2_MD5_SECURITY
) {
735 wccp2_security_md5
.security_option
= htonl(WCCP2_MD5_SECURITY
);
737 wccp2_security_md5
.security_length
= htons(sizeof(struct wccp2_security_md5_t
) - 4);
738 } else if (service_list_ptr
->wccp2_security_type
== WCCP2_NO_SECURITY
) {
739 wccp2_security_md5
.security_option
= htonl(WCCP2_NO_SECURITY
);
740 /* XXX I hate magic length numbers! */
741 wccp2_security_md5
.security_length
= htons(4);
743 fatalf("Bad WCCP2 security type\n");
746 wccp2_here_i_am_header
.length
+= ntohs(wccp2_security_md5
.security_length
) + 4;
747 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
748 wccp2_security_md5
.security_type
= htons(WCCP2_SECURITY_INFO
);
750 service_list_ptr
->security_info
= (struct wccp2_security_md5_t
*) ptr
;
752 if (service_list_ptr
->wccp2_security_type
== WCCP2_MD5_SECURITY
) {
753 memcpy(ptr
, &wccp2_security_md5
, sizeof(struct wccp2_security_md5_t
));
754 ptr
+= sizeof(struct wccp2_security_md5_t
);
756 /* assume NONE, and XXX I hate magic length numbers */
757 memcpy(ptr
, &wccp2_security_md5
, 8);
761 /* Add the service info section */
763 wccp2_here_i_am_header
.length
+= sizeof(struct wccp2_service_info_t
);
765 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
767 memcpy(ptr
, &service_list_ptr
->info
, sizeof(struct wccp2_service_info_t
));
769 service_list_ptr
->service_info
= (struct wccp2_service_info_t
*) ptr
;
771 ptr
+= sizeof(struct wccp2_service_info_t
);
773 /* Add the cache identity section */
775 switch (Config
.Wccp2
.assignment_method
) {
777 case WCCP2_ASSIGNMENT_METHOD_HASH
:
779 wccp2_here_i_am_header
.length
+= sizeof(struct wccp2_identity_info_t
);
780 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
781 wccp2_identity_info
.cache_identity_type
= htons(WCCP2_WC_ID_INFO
);
782 wccp2_identity_info
.cache_identity_length
= htons(sizeof(wccp2_identity_info
.cache_identity
));
783 memset(&wccp2_identity_info
.cache_identity
.addr
, '\0', sizeof(struct in_addr
));
784 memset(&wccp2_identity_info
.cache_identity
.hash_revision
, '\0', sizeof(wccp2_identity_info
.cache_identity
.hash_revision
));
785 memset(&wccp2_identity_info
.cache_identity
.bits
, '\0', sizeof(wccp2_identity_info
.cache_identity
.bits
));
786 memset(&wccp2_identity_info
.cache_identity
.buckets
, '\0', sizeof(wccp2_identity_info
.cache_identity
.buckets
));
787 wccp2_identity_info
.cache_identity
.weight
= htons(Config
.Wccp2
.weight
);
788 memset(&wccp2_identity_info
.cache_identity
.status
, '\0', sizeof(wccp2_identity_info
.cache_identity
.status
));
790 memcpy(ptr
, &wccp2_identity_info
, sizeof(struct wccp2_identity_info_t
));
791 service_list_ptr
->wccp2_identity_info_ptr
= ptr
;
793 ptr
+= sizeof(struct wccp2_identity_info_t
);
796 case WCCP2_ASSIGNMENT_METHOD_MASK
:
798 wccp2_here_i_am_header
.length
+= sizeof(struct wccp2_mask_identity_info_t
);
799 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
800 wccp2_mask_identity_info
.cache_identity_type
= htons(WCCP2_WC_ID_INFO
);
801 wccp2_mask_identity_info
.cache_identity_length
= htons(sizeof(wccp2_mask_identity_info
.cache_identity
));
802 memset(&wccp2_mask_identity_info
.cache_identity
.addr
, '\0', sizeof(struct in_addr
));
803 wccp2_mask_identity_info
.cache_identity
.bits
= htons(WCCP2_MASK_ASSIGNMENT_DATA
);
804 wccp2_mask_identity_info
.cache_identity
.mask_element_count
= htonl(1);
805 service_flags
= ntohl(service_list_ptr
->service_info
->service_flags
);
807 memset(&wccp2_mask_identity_info
.cache_identity
.mask
, 0, sizeof(struct wccp2_mask_element_t
));
809 if ((service_flags
& WCCP2_SERVICE_SRC_IP_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_IP_ALT_HASH
)) {
810 wccp2_mask_identity_info
.cache_identity
.mask
.source_ip_mask
= htonl(0x00001741);
811 } 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
)) {
812 wccp2_mask_identity_info
.cache_identity
.mask
.dest_ip_mask
= htonl(0x00001741);
813 } else if ((service_flags
& WCCP2_SERVICE_SRC_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_PORT_ALT_HASH
)) {
814 wccp2_mask_identity_info
.cache_identity
.mask
.source_port_mask
= htons(0x1741);
815 } else if ((service_flags
& WCCP2_SERVICE_DST_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_DST_PORT_ALT_HASH
)) {
816 wccp2_mask_identity_info
.cache_identity
.mask
.dest_port_mask
= htons(0x1741);
818 fatalf("Unknown service hash method\n");
821 wccp2_mask_identity_info
.cache_identity
.weight
= 0;
822 wccp2_mask_identity_info
.cache_identity
.status
= 0;
824 memcpy(ptr
, &wccp2_mask_identity_info
, sizeof(struct wccp2_mask_identity_info_t
));
825 service_list_ptr
->wccp2_identity_info_ptr
= ptr
;
827 ptr
+= sizeof(struct wccp2_mask_identity_info_t
);
831 fatalf("Unknown Wccp2 assignment method\n");
834 /* Add the cache view section */
835 wccp2_here_i_am_header
.length
+= sizeof(wccp2_cache_view_header
);
837 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
839 wccp2_cache_view_header
.cache_view_type
= htons(WCCP2_WC_VIEW_INFO
);
841 wccp2_cache_view_header
.cache_view_length
= htons(sizeof(wccp2_cache_view_header
) - 4 +
842 sizeof(wccp2_cache_view_info
) + (wccp2_numrouters
* sizeof(wccp2_router_id_element
)));
844 wccp2_cache_view_header
.cache_view_version
= htonl(1);
846 memcpy(ptr
, &wccp2_cache_view_header
, sizeof(wccp2_cache_view_header
));
848 ptr
+= sizeof(wccp2_cache_view_header
);
850 /* Add the number of routers to the packet */
851 wccp2_here_i_am_header
.length
+= sizeof(service_list_ptr
->num_routers
);
853 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
855 service_list_ptr
->num_routers
= htonl(wccp2_numrouters
);
857 memcpy(ptr
, &service_list_ptr
->num_routers
, sizeof(service_list_ptr
->num_routers
));
859 ptr
+= sizeof(service_list_ptr
->num_routers
);
861 /* Add each router. Keep this functionality here to make sure the received_id can be updated in the packet */
862 for (s
= Config
.Wccp2
.router
; s
; s
= s
->next
) {
863 if (!s
->s
.IsAnyAddr()) {
865 wccp2_here_i_am_header
.length
+= sizeof(struct wccp2_router_id_element_t
);
866 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
868 /* Add a pointer to the router list for this router */
870 router_list_ptr
->info
= (struct wccp2_router_id_element_t
*) ptr
;
871 s
->s
.GetInAddr(router_list_ptr
->info
->router_address
);
872 router_list_ptr
->info
->received_id
= htonl(0);
873 s
->s
.GetInAddr(router_list_ptr
->router_sendto_address
);
874 router_list_ptr
->member_change
= htonl(0);
876 /* Build the next struct */
878 router_list_ptr
->next
= (wccp2_router_list_t
*) xcalloc(1, sizeof(struct wccp2_router_list_t
));
880 /* update the pointer */
881 router_list_ptr
= router_list_ptr
->next
;
882 router_list_ptr
->next
= NULL
;
884 /* no need to copy memory - we've just set the values directly in the packet above */
886 ptr
+= sizeof(struct wccp2_router_id_element_t
);
890 /* Add the number of caches (0) */
891 wccp2_here_i_am_header
.length
+= sizeof(wccp2_cache_view_info
.num_caches
);
893 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
895 wccp2_cache_view_info
.num_caches
= htonl(0);
897 memcpy(ptr
, &wccp2_cache_view_info
.num_caches
, sizeof(wccp2_cache_view_info
.num_caches
));
899 ptr
+= sizeof(wccp2_cache_view_info
.num_caches
);
901 /* Add the extra capability header */
902 wccp2_here_i_am_header
.length
+= sizeof(wccp2_capability_info_header
);
904 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
906 wccp2_capability_info_header
.capability_info_type
= htons(WCCP2_CAPABILITY_INFO
);
908 wccp2_capability_info_header
.capability_info_length
= htons(3 * sizeof(wccp2_capability_element
));
910 memcpy(ptr
, &wccp2_capability_info_header
, sizeof(wccp2_capability_info_header
));
912 ptr
+= sizeof(wccp2_capability_info_header
);
914 /* Add the forwarding method */
915 wccp2_here_i_am_header
.length
+= sizeof(wccp2_capability_element
);
917 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
919 wccp2_capability_element
.capability_type
= htons(WCCP2_CAPABILITY_FORWARDING_METHOD
);
921 wccp2_capability_element
.capability_length
= htons(sizeof(wccp2_capability_element
.capability_value
));
923 wccp2_capability_element
.capability_value
= htonl(Config
.Wccp2
.forwarding_method
);
925 memcpy(ptr
, &wccp2_capability_element
, sizeof(wccp2_capability_element
));
927 ptr
+= sizeof(wccp2_capability_element
);
929 /* Add the assignment method */
930 wccp2_here_i_am_header
.length
+= sizeof(wccp2_capability_element
);
932 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
934 wccp2_capability_element
.capability_type
= htons(WCCP2_CAPABILITY_ASSIGNMENT_METHOD
);
936 wccp2_capability_element
.capability_length
= htons(sizeof(wccp2_capability_element
.capability_value
));
938 wccp2_capability_element
.capability_value
= htonl(Config
.Wccp2
.assignment_method
);
940 memcpy(ptr
, &wccp2_capability_element
, sizeof(wccp2_capability_element
));
942 ptr
+= sizeof(wccp2_capability_element
);
944 /* Add the return method */
945 wccp2_here_i_am_header
.length
+= sizeof(wccp2_capability_element
);
947 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
949 wccp2_capability_element
.capability_type
= htons(WCCP2_CAPABILITY_RETURN_METHOD
);
951 wccp2_capability_element
.capability_length
= htons(sizeof(wccp2_capability_element
.capability_value
));
953 wccp2_capability_element
.capability_value
= htonl(Config
.Wccp2
.return_method
);
955 memcpy(ptr
, &wccp2_capability_element
, sizeof(wccp2_capability_element
));
957 ptr
+= sizeof(wccp2_capability_element
);
959 /* Finally, fix the total length to network order, and copy to the appropriate memory blob */
960 wccp2_here_i_am_header
.length
= htons(wccp2_here_i_am_header
.length
);
962 memcpy(&service_list_ptr
->wccp_packet
, &wccp2_here_i_am_header
, sizeof(wccp2_here_i_am_header
));
964 service_list_ptr
->wccp_packet_size
= ntohs(wccp2_here_i_am_header
.length
) + sizeof(wccp2_here_i_am_header
);
966 /* Add the event if everything initialised correctly */
967 debugs(80,3,"wccp2Init: scheduled 'HERE_I_AM' message to " << wccp2_numrouters
<< "routers.");
968 if (wccp2_numrouters
) {
969 if (!eventFind(wccp2HereIam
, NULL
)) {
970 eventAdd("wccp2HereIam", wccp2HereIam
, NULL
, 1, 1);
972 debugs(80,3,"wccp2Init: skip duplicate 'HERE_I_AM'.");
975 service_list_ptr
= service_list_ptr
->next
;
980 wccp2ConnectionOpen(void)
982 struct sockaddr_in router
, local
, null
;
983 socklen_t local_len
, router_len
;
985 struct wccp2_service_list_t
*service_list_ptr
;
987 struct wccp2_router_list_t
*router_list_ptr
;
989 debugs(80, 5, "wccp2ConnectionOpen: Called");
991 if (wccp2_numrouters
== 0 || !wccp2_service_list_head
) {
992 debugs(80, 2, "WCCPv2 Disabled. No IPv4 Router(s) configured.");
996 if ( !Config
.Wccp2
.address
.SetIPv4() ) {
997 debugs(80, DBG_CRITICAL
, "WCCPv2 Disabled. Local address " << Config
.Wccp2
.address
<< " is not an IPv4 address.");
1001 Config
.Wccp2
.address
.SetPort(WCCP_PORT
);
1002 theWccp2Connection
= comm_open_listener(SOCK_DGRAM
,
1004 Config
.Wccp2
.address
,
1008 if (theWccp2Connection
< 0)
1009 fatal("Cannot open WCCP Port");
1011 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
1013 int i
= IP_PMTUDISC_DONT
;
1014 setsockopt(theWccp2Connection
, SOL_IP
, IP_MTU_DISCOVER
, &i
, sizeof i
);
1018 Comm::SetSelect(theWccp2Connection
, COMM_SELECT_READ
, wccp2HandleUdp
, NULL
, 0);
1020 debugs(80, DBG_IMPORTANT
, "Accepting WCCPv2 messages on port " << WCCP_PORT
<< ", FD " << theWccp2Connection
<< ".");
1021 debugs(80, DBG_IMPORTANT
, "Initialising all WCCPv2 lists");
1023 /* Initialise all routers on all services */
1024 memset(&null
, 0, sizeof(null
));
1026 null
.sin_family
= AF_UNSPEC
;
1028 service_list_ptr
= wccp2_service_list_head
;
1030 while (service_list_ptr
!= NULL
) {
1031 for (router_list_ptr
= &service_list_ptr
->router_list_head
; router_list_ptr
->next
!= NULL
; router_list_ptr
= router_list_ptr
->next
) {
1032 router_len
= sizeof(router
);
1033 memset(&router
, '\0', router_len
);
1034 router
.sin_family
= AF_INET
;
1035 router
.sin_port
= htons(WCCP_PORT
);
1036 router
.sin_addr
= router_list_ptr
->router_sendto_address
;
1038 if (connect(theWccp2Connection
, (struct sockaddr
*) &router
, router_len
))
1039 fatal("Unable to connect WCCP out socket");
1041 local_len
= sizeof(local
);
1043 memset(&local
, '\0', local_len
);
1045 if (getsockname(theWccp2Connection
, (struct sockaddr
*) &local
, &local_len
))
1046 fatal("Unable to getsockname on WCCP out socket");
1048 router_list_ptr
->local_ip
= local
.sin_addr
;
1050 /* Disconnect the sending socket. Note: FreeBSD returns error
1051 * but disconnects anyway so we have to just assume it worked
1053 if (wccp2_numrouters
> 1)
1054 connect(theWccp2Connection
, (struct sockaddr
*) &null
, router_len
);
1057 service_list_ptr
= service_list_ptr
->next
;
1060 wccp2_connected
= 1;
1064 wccp2ConnectionClose(void)
1067 struct wccp2_service_list_t
*service_list_ptr
;
1069 struct wccp2_service_list_t
*service_list_ptr_next
;
1071 struct wccp2_router_list_t
*router_list_ptr
;
1073 struct wccp2_router_list_t
*router_list_next
;
1075 struct wccp2_cache_list_t
*cache_list_ptr
;
1077 struct wccp2_cache_list_t
*cache_list_ptr_next
;
1079 if (wccp2_connected
== 0) {
1083 if (theWccp2Connection
> -1) {
1084 debugs(80, DBG_IMPORTANT
, "FD " << theWccp2Connection
<< " Closing WCCPv2 socket");
1085 comm_close(theWccp2Connection
);
1086 theWccp2Connection
= -1;
1089 /* for each router on each service send a packet */
1090 service_list_ptr
= wccp2_service_list_head
;
1092 while (service_list_ptr
!= NULL
) {
1093 for (router_list_ptr
= &service_list_ptr
->router_list_head
; router_list_ptr
!= NULL
; router_list_ptr
= router_list_next
) {
1094 for (cache_list_ptr
= &router_list_ptr
->cache_list_head
; cache_list_ptr
; cache_list_ptr
= cache_list_ptr_next
) {
1095 cache_list_ptr_next
= cache_list_ptr
->next
;
1097 if (cache_list_ptr
!= &router_list_ptr
->cache_list_head
) {
1098 xfree(cache_list_ptr
);
1101 memset(cache_list_ptr
, '\0', sizeof(struct wccp2_cache_list_t
));
1105 router_list_next
= router_list_ptr
->next
;
1107 if (router_list_ptr
!= &service_list_ptr
->router_list_head
) {
1108 xfree(router_list_ptr
);
1111 memset(router_list_ptr
, '\0', sizeof(struct wccp2_router_list_t
));
1115 service_list_ptr_next
= service_list_ptr
->next
;
1116 xfree(service_list_ptr
);
1117 service_list_ptr
= service_list_ptr_next
;
1120 wccp2_service_list_head
= NULL
;
1121 eventDelete(wccp2HereIam
, NULL
);
1122 eventDelete(wccp2AssignBuckets
, NULL
);
1123 eventDelete(wccp2HereIam
, NULL
);
1124 wccp2_connected
= 0;
1128 * Functions for handling the requests.
1132 * Accept the UDP packet
1135 wccp2HandleUdp(int sock
, void *not_used
)
1138 struct wccp2_service_list_t
*service_list_ptr
;
1140 struct wccp2_router_list_t
*router_list_ptr
;
1142 struct wccp2_cache_list_t
*cache_list_ptr
;
1144 struct wccp2_cache_list_t
*cache_list_ptr_next
;
1146 /* These structs form the parts of the packet */
1148 struct wccp2_item_header_t
*header
= NULL
;
1150 struct wccp2_security_none_t
*security_info
= NULL
;
1152 struct wccp2_service_info_t
*service_info
= NULL
;
1154 struct router_identity_info_t
*router_identity_info
= NULL
;
1156 struct router_view_t
*router_view_header
= NULL
;
1158 struct wccp2_cache_mask_identity_info_t
*cache_mask_identity
= NULL
;
1160 struct cache_mask_info_t
*cache_mask_info
= NULL
;
1162 struct wccp2_cache_identity_info_t
*cache_identity
= NULL
;
1164 struct wccp2_capability_info_header_t
*router_capability_header
= NULL
;
1166 struct wccp2_capability_element_t
*router_capability_element
;
1168 struct sockaddr_in from
;
1170 struct in_addr cache_address
;
1172 short int data_length
, offset
;
1177 debugs(80, 6, "wccp2HandleUdp: Called.");
1179 Comm::SetSelect(sock
, COMM_SELECT_READ
, wccp2HandleUdp
, NULL
, 0);
1181 /* FIXME INET6 : drop conversion boundary */
1182 Ip::Address from_tmp
;
1184 len
= comm_udp_recvfrom(sock
,
1189 /* FIXME INET6 : drop conversion boundary */
1190 from_tmp
.GetSockAddr(from
);
1195 if (ntohs(wccp2_i_see_you
.version
) != WCCP2_VERSION
)
1198 if (ntohl(wccp2_i_see_you
.type
) != WCCP2_I_SEE_YOU
)
1201 debugs(80, 3, "Incoming WCCPv2 I_SEE_YOU length " << ntohs(wccp2_i_see_you
.length
) << ".");
1203 /* Record the total data length */
1204 data_length
= ntohs(wccp2_i_see_you
.length
);
1208 if (data_length
> len
) {
1209 debugs(80, DBG_IMPORTANT
, "ERROR: Malformed WCCPv2 packet claiming it's bigger than received data");
1213 /* Go through the data structure */
1214 while (data_length
> offset
) {
1216 char *data
= wccp2_i_see_you
.data
;
1218 header
= (struct wccp2_item_header_t
*) &data
[offset
];
1220 switch (ntohs(header
->type
)) {
1222 case WCCP2_SECURITY_INFO
:
1224 if (security_info
!= NULL
) {
1225 debugs(80, DBG_IMPORTANT
, "Duplicate security definition");
1229 security_info
= (struct wccp2_security_none_t
*) &wccp2_i_see_you
.data
[offset
];
1232 case WCCP2_SERVICE_INFO
:
1234 if (service_info
!= NULL
) {
1235 debugs(80, DBG_IMPORTANT
, "Duplicate service_info definition");
1239 service_info
= (struct wccp2_service_info_t
*) &wccp2_i_see_you
.data
[offset
];
1242 case WCCP2_ROUTER_ID_INFO
:
1244 if (router_identity_info
!= NULL
) {
1245 debugs(80, DBG_IMPORTANT
, "Duplicate router_identity_info definition");
1249 router_identity_info
= (struct router_identity_info_t
*) &wccp2_i_see_you
.data
[offset
];
1252 case WCCP2_RTR_VIEW_INFO
:
1254 if (router_view_header
!= NULL
) {
1255 debugs(80, DBG_IMPORTANT
, "Duplicate router_view definition");
1259 router_view_header
= (struct router_view_t
*) &wccp2_i_see_you
.data
[offset
];
1262 case WCCP2_CAPABILITY_INFO
:
1264 if (router_capability_header
!= NULL
) {
1265 debugs(80, DBG_IMPORTANT
, "Duplicate router_capability definition");
1269 router_capability_header
= (struct wccp2_capability_info_header_t
*) &wccp2_i_see_you
.data
[offset
];
1272 /* Nothing to do for the types below */
1274 case WCCP2_ASSIGN_MAP
:
1275 case WCCP2_REDIRECT_ASSIGNMENT
:
1279 debugs(80, DBG_IMPORTANT
, "Unknown record type in WCCPv2 Packet (" << ntohs(header
->type
) << ").");
1282 offset
+= sizeof(struct wccp2_item_header_t
);
1283 offset
+= ntohs(header
->length
);
1285 if (offset
> data_length
) {
1286 debugs(80, DBG_IMPORTANT
, "Error: WCCPv2 packet tried to tell us there is data beyond the end of the packet");
1291 if ((security_info
== NULL
) || (service_info
== NULL
) || (router_identity_info
== NULL
) || (router_view_header
== NULL
)) {
1292 debugs(80, DBG_IMPORTANT
, "Incomplete WCCPv2 Packet");
1296 debugs(80, 5, "Complete packet received");
1298 /* Check that the service in the packet is configured on this router */
1299 service_list_ptr
= wccp2_service_list_head
;
1301 while (service_list_ptr
!= NULL
) {
1302 if (service_info
->service_id
== service_list_ptr
->service_info
->service_id
) {
1306 service_list_ptr
= service_list_ptr
->next
;
1309 if (service_list_ptr
== NULL
) {
1310 debugs(80, DBG_IMPORTANT
, "WCCPv2 Unknown service received from router (" << service_info
->service_id
<< ")");
1314 if (ntohl(security_info
->security_option
) != ntohl(service_list_ptr
->security_info
->security_option
)) {
1315 debugs(80, DBG_IMPORTANT
, "Invalid security option in WCCPv2 Packet (" << ntohl(security_info
->security_option
) << " vs " << ntohl(service_list_ptr
->security_info
->security_option
) << ").");
1319 if (!wccp2_check_security(service_list_ptr
, (char *) security_info
, (char *) &wccp2_i_see_you
, len
)) {
1320 debugs(80, DBG_IMPORTANT
, "Received WCCPv2 Packet failed authentication");
1324 /* Check that the router address is configured on this router */
1325 for (router_list_ptr
= &service_list_ptr
->router_list_head
; router_list_ptr
->next
!= NULL
; router_list_ptr
= router_list_ptr
->next
) {
1326 if (router_list_ptr
->router_sendto_address
.s_addr
== from
.sin_addr
.s_addr
)
1330 if (router_list_ptr
->next
== NULL
) {
1331 debugs(80, DBG_IMPORTANT
, "WCCPv2 Packet received from unknown router");
1335 /* Set the router id */
1336 router_list_ptr
->info
->router_address
= router_identity_info
->router_id_element
.router_address
;
1338 /* Increment the received id in the packet */
1339 if (ntohl(router_list_ptr
->info
->received_id
) != ntohl(router_identity_info
->router_id_element
.received_id
)) {
1340 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
) << ".");
1341 router_list_ptr
->info
->received_id
= router_identity_info
->router_id_element
.received_id
;
1344 /* TODO: check return/forwarding methods */
1345 if (router_capability_header
== NULL
) {
1346 if ((Config
.Wccp2
.return_method
!= WCCP2_PACKET_RETURN_METHOD_GRE
) || (Config
.Wccp2
.forwarding_method
!= WCCP2_FORWARDING_METHOD_GRE
)) {
1347 debugs(80, DBG_IMPORTANT
, "wccp2HandleUdp: fatal error - A WCCP router does not support the forwarding method specified, only GRE supported");
1348 wccp2ConnectionClose();
1353 char *end
= ((char *) router_capability_header
) + sizeof(*router_capability_header
) + ntohs(router_capability_header
->capability_info_length
) - sizeof(struct wccp2_capability_info_header_t
);
1355 router_capability_element
= (struct wccp2_capability_element_t
*) (((char *) router_capability_header
) + sizeof(*router_capability_header
));
1357 while ((char *) router_capability_element
<= end
) {
1359 switch (ntohs(router_capability_element
->capability_type
)) {
1361 case WCCP2_CAPABILITY_FORWARDING_METHOD
:
1363 if (!(ntohl(router_capability_element
->capability_value
) & Config
.Wccp2
.forwarding_method
)) {
1364 debugs(80, DBG_IMPORTANT
, "wccp2HandleUdp: fatal error - A WCCP router has specified a different forwarding method " << ntohl(router_capability_element
->capability_value
) << ", expected " << Config
.Wccp2
.forwarding_method
);
1365 wccp2ConnectionClose();
1371 case WCCP2_CAPABILITY_ASSIGNMENT_METHOD
:
1373 if (!(ntohl(router_capability_element
->capability_value
) & Config
.Wccp2
.assignment_method
)) {
1374 debugs(80, DBG_IMPORTANT
, "wccp2HandleUdp: fatal error - A WCCP router has specified a different assignment method " << ntohl(router_capability_element
->capability_value
) << ", expected "<< Config
.Wccp2
.assignment_method
);
1375 wccp2ConnectionClose();
1381 case WCCP2_CAPABILITY_RETURN_METHOD
:
1383 if (!(ntohl(router_capability_element
->capability_value
) & Config
.Wccp2
.return_method
)) {
1384 debugs(80, DBG_IMPORTANT
, "wccp2HandleUdp: fatal error - A WCCP router has specified a different return method " << ntohl(router_capability_element
->capability_value
) << ", expected " << Config
.Wccp2
.return_method
);
1385 wccp2ConnectionClose();
1393 break; // ignore silently for now
1396 debugs(80, DBG_IMPORTANT
, "Unknown capability type in WCCPv2 Packet (" << ntohs(router_capability_element
->capability_type
) << ").");
1399 router_capability_element
= (struct wccp2_capability_element_t
*) (((char *) router_capability_element
) + sizeof(struct wccp2_item_header_t
) + ntohs(router_capability_element
->capability_length
));
1403 debugs(80, 5, "Cleaning out cache list");
1404 /* clean out the old cache list */
1406 for (cache_list_ptr
= &router_list_ptr
->cache_list_head
; cache_list_ptr
; cache_list_ptr
= cache_list_ptr_next
) {
1407 cache_list_ptr_next
= cache_list_ptr
->next
;
1409 if (cache_list_ptr
!= &router_list_ptr
->cache_list_head
) {
1410 xfree(cache_list_ptr
);
1414 router_list_ptr
->num_caches
= htonl(0);
1417 /* Check to see if we're the master cache and update the cache list */
1419 service_list_ptr
->lowest_ip
= 1;
1420 cache_list_ptr
= &router_list_ptr
->cache_list_head
;
1422 /* to find the list of caches, we start at the end of the router view header */
1424 ptr
= (char *) (router_view_header
) + sizeof(struct router_view_t
);
1426 /* Then we read the number of routers */
1427 memcpy(&tmp
, ptr
, sizeof(tmp
));
1429 /* skip the number plus all the ip's */
1431 ptr
+= sizeof(tmp
) + (ntohl(tmp
) * sizeof(struct in_addr
));
1433 /* Then read the number of caches */
1434 memcpy(&tmp
, ptr
, sizeof(tmp
));
1437 if (ntohl(tmp
) != 0) {
1438 /* search through the list of received-from ip addresses */
1440 for (num_caches
= 0; num_caches
< (int) ntohl(tmp
); ++num_caches
) {
1441 /* Get a copy of the ip */
1442 memset(&cache_address
, 0, sizeof(cache_address
)); // Make GCC happy
1444 switch (Config
.Wccp2
.assignment_method
) {
1446 case WCCP2_ASSIGNMENT_METHOD_HASH
:
1448 cache_identity
= (struct wccp2_cache_identity_info_t
*) ptr
;
1450 ptr
+= sizeof(struct wccp2_cache_identity_info_t
);
1452 memcpy(&cache_address
, &cache_identity
->addr
, sizeof(struct in_addr
));
1454 cache_list_ptr
->weight
= ntohs(cache_identity
->weight
);
1457 case WCCP2_ASSIGNMENT_METHOD_MASK
:
1459 cache_mask_info
= (struct cache_mask_info_t
*) ptr
;
1461 /* The mask assignment has an undocumented variable length entry here */
1463 if (ntohl(cache_mask_info
->num1
) == 3) {
1465 cache_mask_identity
= (struct wccp2_cache_mask_identity_info_t
*) ptr
;
1467 ptr
+= sizeof(struct wccp2_cache_mask_identity_info_t
);
1469 memcpy(&cache_address
, &cache_mask_identity
->addr
, sizeof(struct in_addr
));
1472 ptr
+= sizeof(struct cache_mask_info_t
);
1474 memcpy(&cache_address
, &cache_mask_info
->addr
, sizeof(struct in_addr
));
1477 cache_list_ptr
->weight
= 0;
1481 fatalf("Unknown Wccp2 assignment method\n");
1484 /* Update the cache list */
1485 cache_list_ptr
->cache_ip
= cache_address
;
1487 cache_list_ptr
->next
= (wccp2_cache_list_t
*) xcalloc(1, sizeof(struct wccp2_cache_list_t
));
1489 cache_list_ptr
= cache_list_ptr
->next
;
1491 cache_list_ptr
->next
= NULL
;
1493 debugs (80, 5, "checking cache list: (" << std::hex
<< cache_address
.s_addr
<< ":" << router_list_ptr
->local_ip
.s_addr
<< ")");
1495 /* Check to see if it's the master, or us */
1497 if (cache_address
.s_addr
== router_list_ptr
->local_ip
.s_addr
) {
1501 if (cache_address
.s_addr
< router_list_ptr
->local_ip
.s_addr
) {
1502 service_list_ptr
->lowest_ip
= 0;
1506 debugs(80, 5, "Adding ourselves as the only cache");
1508 /* Update the cache list */
1509 cache_list_ptr
->cache_ip
= router_list_ptr
->local_ip
;
1511 cache_list_ptr
->next
= (wccp2_cache_list_t
*) xcalloc(1, sizeof(struct wccp2_cache_list_t
));
1512 cache_list_ptr
= cache_list_ptr
->next
;
1513 cache_list_ptr
->next
= NULL
;
1515 service_list_ptr
->lowest_ip
= 1;
1520 wccp2SortCacheList(&router_list_ptr
->cache_list_head
);
1522 router_list_ptr
->num_caches
= htonl(num_caches
);
1524 if ((found
== 1) && (service_list_ptr
->lowest_ip
== 1)) {
1525 if (ntohl(router_view_header
->change_number
) != router_list_ptr
->member_change
) {
1526 debugs(80, 4, "Change detected - queueing up new assignment");
1527 router_list_ptr
->member_change
= ntohl(router_view_header
->change_number
);
1528 eventDelete(wccp2AssignBuckets
, NULL
);
1529 eventAdd("wccp2AssignBuckets", wccp2AssignBuckets
, NULL
, 15.0, 1);
1531 debugs(80, 5, "Change not detected (" << ntohl(router_view_header
->change_number
) << " = " << router_list_ptr
->member_change
<< ")");
1534 eventDelete(wccp2AssignBuckets
, NULL
);
1535 debugs(80, 5, "I am not the lowest ip cache - not assigning buckets");
1540 wccp2HereIam(void *voidnotused
)
1543 struct wccp2_service_list_t
*service_list_ptr
;
1545 struct wccp2_router_list_t
*router_list_ptr
;
1547 struct wccp2_identity_info_t
*wccp2_identity_info_ptr
;
1549 struct wccp2_mask_identity_info_t
*wccp2_mask_identity_info_ptr
;
1553 debugs(80, 6, "wccp2HereIam: Called");
1555 if (wccp2_connected
== 0) {
1556 debugs(80, DBG_IMPORTANT
, "wccp2HereIam: wccp2 socket closed. Shutting down WCCP2");
1560 /* Wait if store dirs are rebuilding */
1561 if (StoreController::store_dirs_rebuilding
&& Config
.Wccp2
.rebuildwait
) {
1562 eventAdd("wccp2HereIam", wccp2HereIam
, NULL
, 1.0, 1);
1566 router
.SetPort(WCCP_PORT
);
1568 /* for each router on each service send a packet */
1569 service_list_ptr
= wccp2_service_list_head
;
1571 while (service_list_ptr
!= NULL
) {
1572 debugs(80, 5, "wccp2HereIam: sending to service id " << service_list_ptr
->info
.service_id
);
1574 for (router_list_ptr
= &service_list_ptr
->router_list_head
; router_list_ptr
->next
!= NULL
; router_list_ptr
= router_list_ptr
->next
) {
1575 router
= router_list_ptr
->router_sendto_address
;
1577 /* Set the cache id (ip) */
1579 switch (Config
.Wccp2
.assignment_method
) {
1581 case WCCP2_ASSIGNMENT_METHOD_HASH
:
1583 wccp2_identity_info_ptr
= (struct wccp2_identity_info_t
*) service_list_ptr
->wccp2_identity_info_ptr
;
1584 wccp2_identity_info_ptr
->cache_identity
.addr
= router_list_ptr
->local_ip
;
1587 case WCCP2_ASSIGNMENT_METHOD_MASK
:
1589 wccp2_mask_identity_info_ptr
= (struct wccp2_mask_identity_info_t
*) service_list_ptr
->wccp2_identity_info_ptr
;
1590 wccp2_mask_identity_info_ptr
->cache_identity
.addr
= router_list_ptr
->local_ip
;
1594 fatalf("Unknown Wccp2 assignment method\n");
1597 /* Security update, if needed */
1599 if (service_list_ptr
->wccp2_security_type
== WCCP2_MD5_SECURITY
) {
1600 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
);
1603 debugs(80, 3, "Sending HereIam packet size " << service_list_ptr
->wccp_packet_size
);
1604 /* Send the packet */
1606 if (wccp2_numrouters
> 1) {
1607 comm_udp_sendto(theWccp2Connection
,
1609 &service_list_ptr
->wccp_packet
,
1610 service_list_ptr
->wccp_packet_size
);
1612 send(theWccp2Connection
,
1613 &service_list_ptr
->wccp_packet
,
1614 service_list_ptr
->wccp_packet_size
,
1619 service_list_ptr
= service_list_ptr
->next
;
1622 eventAdd("wccp2HereIam", wccp2HereIam
, NULL
, 10.0, 1);
1626 wccp2AssignBuckets(void *voidnotused
)
1629 struct wccp2_service_list_t
*service_list_ptr
;
1631 struct wccp2_router_list_t
*router_list_ptr
;
1633 struct wccp2_cache_list_t
*cache_list_ptr
;
1634 char wccp_packet
[WCCP_RESPONSE_SIZE
];
1635 short int offset
, saved_offset
, assignment_offset
, alt_assignment_offset
;
1637 struct sockaddr_in router
;
1640 uint32_t service_flags
;
1641 unsigned short port
= WCCP_PORT
;
1643 /* Packet segments */
1645 struct wccp2_message_header_t
*main_header
;
1647 struct wccp2_security_md5_t
*security
= NULL
;
1648 /* service from service struct */
1650 struct wccp2_item_header_t
*assignment_header
;
1652 struct wccp2_item_header_t
*alt_assignment_type_header
= NULL
;
1654 struct assignment_key_t
*assignment_key
;
1655 /* number of routers */
1657 struct wccp2_router_assign_element_t
*router_assign
;
1658 /* number of caches */
1660 struct in_addr
*cache_address
;
1661 /* Alternative assignement mask/values */
1664 struct wccp2_mask_element_t
*mask_element
;
1666 struct wccp2_value_element_t
*value_element
;
1667 int valuecounter
, value
;
1670 assignment_offset
= alt_assignment_offset
= 0;
1672 router_len
= sizeof(router
);
1673 memset(&router
, '\0', router_len
);
1674 router
.sin_family
= AF_INET
;
1675 router
.sin_port
= htons(port
);
1677 /* Start main header - fill in length later */
1680 main_header
= (struct wccp2_message_header_t
*) &wccp_packet
[offset
];
1681 main_header
->type
= htonl(WCCP2_REDIRECT_ASSIGN
);
1682 main_header
->version
= htons(WCCP2_VERSION
);
1684 debugs(80, 2, "Running wccp2AssignBuckets");
1685 service_list_ptr
= wccp2_service_list_head
;
1687 while (service_list_ptr
!= NULL
) {
1688 /* If we're not the lowest, we don't need to worry */
1690 if (service_list_ptr
->lowest_ip
== 0) {
1692 service_list_ptr
= service_list_ptr
->next
;
1696 /* reset the offset */
1698 offset
= sizeof(struct wccp2_message_header_t
);
1700 /* build packet header from hereIam packet */
1702 /* XXX this should be made more generic! */
1703 /* XXX and I hate magic numbers! */
1704 switch (service_list_ptr
->wccp2_security_type
) {
1706 case WCCP2_NO_SECURITY
:
1708 security
= (struct wccp2_security_md5_t
*) &wccp_packet
[offset
];
1709 memcpy(security
, service_list_ptr
->security_info
, 8);
1713 case WCCP2_MD5_SECURITY
:
1715 security
= (struct wccp2_security_md5_t
*) &wccp_packet
[offset
];
1717 memcpy(security
, service_list_ptr
->security_info
, sizeof(struct wccp2_security_md5_t
));
1719 offset
+= sizeof(struct wccp2_security_md5_t
);
1723 fatalf("Unknown Wccp2 security type\n");
1728 memcpy(&wccp_packet
[offset
], service_list_ptr
->service_info
, sizeof(struct wccp2_service_info_t
));
1730 offset
+= sizeof(struct wccp2_service_info_t
);
1732 /* assignment header - fill in length later */
1734 assignment_header
= (struct wccp2_item_header_t
*) &wccp_packet
[offset
];
1736 switch (Config
.Wccp2
.assignment_method
) {
1738 case WCCP2_ASSIGNMENT_METHOD_HASH
:
1739 assignment_header
->type
= htons(WCCP2_REDIRECT_ASSIGNMENT
);
1741 offset
+= sizeof(struct wccp2_item_header_t
);
1742 assignment_offset
= offset
;
1745 case WCCP2_ASSIGNMENT_METHOD_MASK
:
1746 assignment_header
->type
= htons(WCCP2_ALT_ASSIGNMENT
);
1748 offset
+= sizeof(struct wccp2_item_header_t
);
1749 assignment_offset
= offset
;
1751 /* The alternative assignment has an extra header, fill in length later */
1753 alt_assignment_type_header
= (struct wccp2_item_header_t
*) &wccp_packet
[offset
];
1754 alt_assignment_type_header
->type
= htons(WCCP2_MASK_ASSIGNMENT
);
1756 offset
+= sizeof(struct wccp2_item_header_t
);
1757 alt_assignment_offset
= offset
;
1762 fatalf("Unknown Wccp2 assignment method\n");
1765 /* Assignment key - fill in master ip later */
1767 assignment_key
= (struct assignment_key_t
*) &wccp_packet
[offset
];
1769 assignment_key
->master_number
= htonl(++service_list_ptr
->change_num
);
1771 offset
+= sizeof(struct assignment_key_t
);
1773 /* Number of routers */
1774 memcpy(&wccp_packet
[offset
], &service_list_ptr
->num_routers
, sizeof(service_list_ptr
->num_routers
));
1776 offset
+= sizeof(service_list_ptr
->num_routers
);
1778 for (router_list_ptr
= &service_list_ptr
->router_list_head
; router_list_ptr
->next
!= NULL
; router_list_ptr
= router_list_ptr
->next
) {
1782 router_assign
= (struct wccp2_router_assign_element_t
*) &wccp_packet
[offset
];
1783 router_assign
->router_address
= router_list_ptr
->info
->router_address
;
1784 router_assign
->received_id
= router_list_ptr
->info
->received_id
;
1785 router_assign
->change_number
= htonl(router_list_ptr
->member_change
);
1787 offset
+= sizeof(struct wccp2_router_assign_element_t
);
1790 saved_offset
= offset
;
1792 for (router_list_ptr
= &service_list_ptr
->router_list_head
; router_list_ptr
->next
!= NULL
; router_list_ptr
= router_list_ptr
->next
) {
1793 unsigned long *weight
= (unsigned long *)xcalloc(sizeof(*weight
), ntohl(router_list_ptr
->num_caches
));
1794 unsigned long total_weight
= 0;
1795 int num_caches
= ntohl(router_list_ptr
->num_caches
);
1797 offset
= saved_offset
;
1799 switch (Config
.Wccp2
.assignment_method
) {
1801 case WCCP2_ASSIGNMENT_METHOD_HASH
:
1802 /* Number of caches */
1803 memcpy(&wccp_packet
[offset
], &router_list_ptr
->num_caches
, sizeof(router_list_ptr
->num_caches
));
1804 offset
+= sizeof(router_list_ptr
->num_caches
);
1809 for (cache
= 0, cache_list_ptr
= &router_list_ptr
->cache_list_head
; cache_list_ptr
->next
; cache_list_ptr
= cache_list_ptr
->next
, ++cache
) {
1812 cache_address
= (struct in_addr
*) &wccp_packet
[offset
];
1814 memcpy(cache_address
, &cache_list_ptr
->cache_ip
, sizeof(struct in_addr
));
1815 total_weight
+= cache_list_ptr
->weight
<< 12;
1816 weight
[cache
] = cache_list_ptr
->weight
<< 12;
1818 offset
+= sizeof(struct in_addr
);
1823 buckets
= (char *) &wccp_packet
[offset
];
1825 memset(buckets
, '\0', WCCP_BUCKETS
);
1827 if (num_caches
!= 0) {
1828 if (total_weight
== 0) {
1829 for (bucket_counter
= 0; bucket_counter
< WCCP_BUCKETS
; ++bucket_counter
) {
1830 buckets
[bucket_counter
] = (char) (bucket_counter
% num_caches
);
1833 unsigned long *assigned
= (unsigned long *)xcalloc(sizeof(*assigned
), num_caches
);
1834 unsigned long done
= 0;
1836 unsigned long per_bucket
= total_weight
/ WCCP_BUCKETS
;
1838 for (bucket_counter
= 0; bucket_counter
< WCCP_BUCKETS
; ++bucket_counter
) {
1842 for (n
= num_caches
; n
; --n
) {
1845 if (cache
>= num_caches
)
1848 if (!weight
[cache
]) {
1853 if (assigned
[cache
] <= done
)
1857 buckets
[bucket_counter
] = (char) cache
;
1858 step
= per_bucket
* total_weight
/ weight
[cache
];
1859 assigned
[cache
] += step
;
1863 safe_free(assigned
);
1867 offset
+= (WCCP_BUCKETS
* sizeof(char));
1871 case WCCP2_ASSIGNMENT_METHOD_MASK
:
1872 num_maskval
= htonl(1);
1873 memcpy(&wccp_packet
[offset
], &num_maskval
, sizeof(int));
1874 offset
+= sizeof(int);
1876 mask_element
= (struct wccp2_mask_element_t
*) &wccp_packet
[offset
];
1877 service_flags
= ntohl(service_list_ptr
->service_info
->service_flags
);
1879 if ((service_flags
& WCCP2_SERVICE_SRC_IP_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_IP_ALT_HASH
)) {
1880 mask_element
->source_ip_mask
= htonl(0x00001741);
1881 mask_element
->dest_ip_mask
= 0;
1882 mask_element
->source_port_mask
= 0;
1883 mask_element
->dest_port_mask
= 0;
1884 } 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
)) {
1885 mask_element
->source_ip_mask
= 0;
1886 mask_element
->dest_ip_mask
= htonl(0x00001741);
1887 mask_element
->source_port_mask
= 0;
1888 mask_element
->dest_port_mask
= 0;
1889 } else if ((service_flags
& WCCP2_SERVICE_SRC_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_PORT_ALT_HASH
)) {
1890 mask_element
->source_ip_mask
= 0;
1891 mask_element
->dest_ip_mask
= 0;
1892 mask_element
->source_port_mask
= htons(0x1741);
1893 mask_element
->dest_port_mask
= 0;
1894 } else if ((service_flags
& WCCP2_SERVICE_DST_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_DST_PORT_ALT_HASH
)) {
1895 mask_element
->source_ip_mask
= 0;
1896 mask_element
->dest_ip_mask
= 0;
1897 mask_element
->source_port_mask
= 0;
1898 mask_element
->dest_port_mask
= htons(0x1741);
1900 fatalf("Unknown service hash method\n");
1903 mask_element
->number_values
= htonl(64);
1905 offset
+= sizeof(struct wccp2_mask_element_t
);
1907 cache_list_ptr
= &router_list_ptr
->cache_list_head
;
1910 for (valuecounter
= 0; valuecounter
< 64; ++valuecounter
) {
1912 value_element
= (struct wccp2_value_element_t
*) &wccp_packet
[offset
];
1914 /* Update the value according the the "correct" formula */
1916 for (; (value
& 0x1741) != value
; ++value
) {
1917 assert(value
<= 0x1741);
1920 if ((service_flags
& WCCP2_SERVICE_SRC_IP_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_IP_ALT_HASH
)) {
1921 value_element
->source_ip_value
= htonl(value
);
1922 value_element
->dest_ip_value
= 0;
1923 value_element
->source_port_value
= 0;
1924 value_element
->dest_port_value
= 0;
1925 } 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
)) {
1926 value_element
->source_ip_value
= 0;
1927 value_element
->dest_ip_value
= htonl(value
);
1928 value_element
->source_port_value
= 0;
1929 value_element
->dest_port_value
= 0;
1930 } else if ((service_flags
& WCCP2_SERVICE_SRC_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_PORT_ALT_HASH
)) {
1931 value_element
->source_ip_value
= 0;
1932 value_element
->dest_ip_value
= 0;
1933 value_element
->source_port_value
= htons(value
);
1934 value_element
->dest_port_value
= 0;
1935 } else if ((service_flags
& WCCP2_SERVICE_DST_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_DST_PORT_ALT_HASH
)) {
1936 value_element
->source_ip_value
= 0;
1937 value_element
->dest_ip_value
= 0;
1938 value_element
->source_port_value
= 0;
1939 value_element
->dest_port_value
= htons(value
);
1941 fatalf("Unknown service hash method\n");
1944 value_element
->cache_ip
= cache_list_ptr
->cache_ip
;
1946 offset
+= sizeof(struct wccp2_value_element_t
);
1949 /* Assign the next value to the next cache */
1951 if ((cache_list_ptr
->next
) && (cache_list_ptr
->next
->next
))
1952 cache_list_ptr
= cache_list_ptr
->next
;
1954 cache_list_ptr
= &router_list_ptr
->cache_list_head
;
1957 /* Fill in length */
1958 alt_assignment_type_header
->length
= htons(offset
- alt_assignment_offset
);
1963 fatalf("Unknown Wccp2 assignment method\n");
1966 /* Fill in length */
1968 assignment_header
->length
= htons(offset
- assignment_offset
);
1970 /* Fill in assignment key */
1971 assignment_key
->master_ip
= router_list_ptr
->local_ip
;
1975 main_header
->length
= htons(offset
- sizeof(struct wccp2_message_header_t
));
1977 /* set the destination address */
1978 router
.sin_addr
= router_list_ptr
->router_sendto_address
;
1980 /* Security update, if needed */
1982 if (service_list_ptr
->wccp2_security_type
== WCCP2_MD5_SECURITY
) {
1983 wccp2_update_md5_security(service_list_ptr
->wccp_password
, (char *) security
, wccp_packet
, offset
);
1986 if (ntohl(router_list_ptr
->num_caches
)) {
1989 if (wccp2_numrouters
> 1) {
1990 /* FIXME INET6 : drop temp conversion */
1991 Ip::Address
tmp_rtr(router
);
1992 comm_udp_sendto(theWccp2Connection
,
1997 send(theWccp2Connection
,
2005 service_list_ptr
= service_list_ptr
->next
;
2011 * Configuration option parsing code
2015 * Parse wccp2_return_method and wccp2_forwarding_method options
2016 * they can be '1' aka 'gre' or '2' aka 'l2'
2017 * repesenting the integer numeric of the same.
2020 parse_wccp2_method(int *method
)
2024 /* Snarf the method */
2025 if ((t
= strtok(NULL
, w_space
)) == NULL
) {
2026 debugs(80, DBG_CRITICAL
, "wccp2_*_method: missing setting.");
2030 /* update configuration if its valid */
2031 if (strcmp(t
, "gre") == 0 || strcmp(t
, "1") == 0) {
2032 *method
= WCCP2_METHOD_GRE
;
2033 } else if (strcmp(t
, "l2") == 0 || strcmp(t
, "2") == 0) {
2034 *method
= WCCP2_METHOD_L2
;
2036 debugs(80, DBG_CRITICAL
, "wccp2_*_method: unknown setting, got " << t
);
2042 dump_wccp2_method(StoreEntry
* e
, const char *label
, int v
)
2045 case WCCP2_METHOD_GRE
:
2046 storeAppendPrintf(e
, "%s gre\n", label
);
2048 case WCCP2_METHOD_L2
:
2049 storeAppendPrintf(e
, "%s l2\n", label
);
2052 debugs(80, DBG_CRITICAL
, "FATAL: WCCPv2 configured method (" << v
<< ") is not valid.");
2058 free_wccp2_method(int *v
)
2062 * Parse wccp2_assignment_method option
2063 * they can be '1' aka 'hash' or '2' aka 'mask'
2064 * repesenting the integer numeric of the same.
2067 parse_wccp2_amethod(int *method
)
2071 /* Snarf the method */
2072 if ((t
= strtok(NULL
, w_space
)) == NULL
) {
2073 debugs(80, DBG_CRITICAL
, "wccp2_assignment_method: missing setting.");
2077 /* update configuration if its valid */
2078 if (strcmp(t
, "hash") == 0 || strcmp(t
, "1") == 0) {
2079 *method
= WCCP2_ASSIGNMENT_METHOD_HASH
;
2080 } else if (strcmp(t
, "mask") == 0 || strcmp(t
, "2") == 0) {
2081 *method
= WCCP2_ASSIGNMENT_METHOD_MASK
;
2083 debugs(80, DBG_CRITICAL
, "wccp2_assignment_method: unknown setting, got " << t
);
2089 dump_wccp2_amethod(StoreEntry
* e
, const char *label
, int v
)
2092 case WCCP2_ASSIGNMENT_METHOD_HASH
:
2093 storeAppendPrintf(e
, "%s hash\n", label
);
2095 case WCCP2_ASSIGNMENT_METHOD_MASK
:
2096 storeAppendPrintf(e
, "%s mask\n", label
);
2099 debugs(80, DBG_CRITICAL
, "FATAL: WCCPv2 configured " << label
<< " (" << v
<< ") is not valid.");
2105 free_wccp2_amethod(int *v
)
2111 * wccp2_service {standard|dynamic} {id} (password=password)
2114 parse_wccp2_service(void *v
)
2119 int security_type
= WCCP2_NO_SECURITY
;
2120 char wccp_password
[WCCP2_PASSWORD_LEN
+ 1];
2122 if (wccp2_connected
== 1) {
2123 debugs(80, DBG_IMPORTANT
, "WCCPv2: Somehow reparsing the configuration without having shut down WCCP! Try reloading squid again.");
2127 /* Snarf the type */
2128 if ((t
= strtok(NULL
, w_space
)) == NULL
) {
2129 debugs(80, DBG_CRITICAL
, "wccp2ParseServiceInfo: missing service info type (standard|dynamic)");
2133 if (strcmp(t
, "standard") == 0) {
2134 service
= WCCP2_SERVICE_STANDARD
;
2135 } else if (strcmp(t
, "dynamic") == 0) {
2136 service
= WCCP2_SERVICE_DYNAMIC
;
2138 debugs(80, DBG_CRITICAL
, "wccp2ParseServiceInfo: bad service info type (expected standard|dynamic, got " << t
<< ")");
2143 service_id
= GetInteger();
2145 if (service_id
< 0 || service_id
> 255) {
2146 debugs(80, DBG_CRITICAL
, "ERROR: invalid WCCP service id " << service_id
<< " (must be between 0 .. 255)");
2150 memset(wccp_password
, 0, sizeof(wccp_password
));
2151 /* Handle password, if any */
2153 if ((t
= strtok(NULL
, w_space
)) != NULL
) {
2154 if (strncmp(t
, "password=", 9) == 0) {
2155 security_type
= WCCP2_MD5_SECURITY
;
2156 strncpy(wccp_password
, t
+ 9, WCCP2_PASSWORD_LEN
);
2160 /* Create a placeholder service record */
2161 wccp2_add_service_list(service
, service_id
, 0, 0, 0, empty_portlist
, security_type
, wccp_password
);
2165 dump_wccp2_service(StoreEntry
* e
, const char *label
, void *v
)
2168 struct wccp2_service_list_t
*srv
;
2169 srv
= wccp2_service_list_head
;
2171 while (srv
!= NULL
) {
2172 debugs(80, 3, "dump_wccp2_service: id " << srv
->info
.service_id
<< ", type " << srv
->info
.service
);
2173 storeAppendPrintf(e
, "%s %s %d", label
,
2174 (srv
->info
.service
== WCCP2_SERVICE_DYNAMIC
) ? "dynamic" : "standard",
2175 srv
->info
.service_id
);
2177 if (srv
->wccp2_security_type
== WCCP2_MD5_SECURITY
) {
2178 storeAppendPrintf(e
, " %s", srv
->wccp_password
);
2181 storeAppendPrintf(e
, "\n");
2188 free_wccp2_service(void *v
)
2192 check_null_wccp2_service(void *v
)
2194 return !wccp2_service_list_head
;
2200 * wccp2_service_info {id} stuff..
2204 * + flags=flag,flag,flag..
2205 * + proto=protocol (tcp|udp)
2206 * + ports=port,port,port (up to a max of 8)
2207 * + priority=priority (0->255)
2209 * The flags here are:
2210 * src_ip_hash, dst_ip_hash, source_port_hash, dst_port_hash, ports_defined,
2211 * ports_source, src_ip_alt_hash, dst_ip_alt_hash, src_port_alt_hash, dst_port_alt_hash
2214 parse_wccp2_service_flags(char *flags
)
2224 tmp
= xstrdup(flags
);
2227 flag
= strsep(&tmp2
, ",");
2230 if (strcmp(flag
, "src_ip_hash") == 0) {
2231 retflag
|= WCCP2_SERVICE_SRC_IP_HASH
;
2232 } else if (strcmp(flag
, "dst_ip_hash") == 0) {
2233 retflag
|= WCCP2_SERVICE_DST_IP_HASH
;
2234 } else if (strcmp(flag
, "source_port_hash") == 0) {
2235 retflag
|= WCCP2_SERVICE_SRC_PORT_HASH
;
2236 } else if (strcmp(flag
, "dst_port_hash") == 0) {
2237 retflag
|= WCCP2_SERVICE_DST_PORT_HASH
;
2238 } else if (strcmp(flag
, "ports_source") == 0) {
2239 retflag
|= WCCP2_SERVICE_PORTS_SOURCE
;
2240 } else if (strcmp(flag
, "src_ip_alt_hash") == 0) {
2241 retflag
|= WCCP2_SERVICE_SRC_IP_ALT_HASH
;
2242 } else if (strcmp(flag
, "dst_ip_alt_hash") == 0) {
2243 retflag
|= WCCP2_SERVICE_DST_IP_ALT_HASH
;
2244 } else if (strcmp(flag
, "src_port_alt_hash") == 0) {
2245 retflag
|= WCCP2_SERVICE_SRC_PORT_ALT_HASH
;
2246 } else if (strcmp(flag
, "dst_port_alt_hash") == 0) {
2247 retflag
|= WCCP2_SERVICE_DST_PORT_ALT_HASH
;
2249 fatalf("Unknown wccp2 service flag: %s\n", flag
);
2252 flag
= strsep(&tmp2
, ",");
2260 parse_wccp2_service_ports(char *options
, int portlist
[])
2264 char *tmp
, *tmp2
, *port
, *end
;
2270 tmp
= xstrdup(options
);
2273 port
= strsep(&tmp2
, ",");
2275 while (port
&& i
< WCCP2_NUMPORTS
) {
2276 p
= strtol(port
, &end
, 0);
2278 if (p
< 1 || p
> 65535) {
2279 fatalf("parse_wccp2_service_ports: port value '%s' isn't valid (1..65535)\n", port
);
2284 port
= strsep(&tmp2
, ",");
2287 if (i
== WCCP2_NUMPORTS
&& port
) {
2288 fatalf("parse_wccp2_service_ports: too many ports (maximum: 8) in list '%s'\n", options
);
2295 parse_wccp2_service_info(void *v
)
2300 int portlist
[WCCP2_NUMPORTS
];
2301 int protocol
= -1; /* IPPROTO_TCP | IPPROTO_UDP */
2303 struct wccp2_service_list_t
*srv
;
2306 if (wccp2_connected
== 1) {
2307 debugs(80, DBG_IMPORTANT
, "WCCPv2: Somehow reparsing the configuration without having shut down WCCP! Try reloading squid again.");
2311 debugs(80, 5, "parse_wccp2_service_info: called");
2312 memset(portlist
, 0, sizeof(portlist
));
2313 /* First argument: id */
2314 service_id
= GetInteger();
2316 if (service_id
< 0 || service_id
> 255) {
2317 debugs(80, DBG_CRITICAL
, "ERROR: invalid WCCP service id " << service_id
<< " (must be between 0 .. 255)");
2321 /* Next: find the (hopefully!) existing service */
2322 srv
= wccp2_get_service_by_id(WCCP2_SERVICE_DYNAMIC
, service_id
);
2325 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
);
2328 /* Next: loop until we don't have any more tokens */
2329 while ((t
= strtok(NULL
, w_space
)) != NULL
) {
2330 if (strncmp(t
, "flags=", 6) == 0) {
2331 /* XXX eww, string pointer math */
2332 flags
= parse_wccp2_service_flags(t
+ 6);
2333 } else if (strncmp(t
, "ports=", 6) == 0) {
2334 parse_wccp2_service_ports(t
+ 6, portlist
);
2335 flags
|= WCCP2_SERVICE_PORTS_DEFINED
;
2336 } else if (strncmp(t
, "protocol=tcp", 12) == 0) {
2337 protocol
= IPPROTO_TCP
;
2338 } else if (strncmp(t
, "protocol=udp", 12) == 0) {
2339 protocol
= IPPROTO_UDP
;
2340 } else if (strncmp(t
, "protocol=", 9) == 0) {
2341 fatalf("parse_wccp2_service_info: id %d: unknown protocol (%s) - must be tcp or udp!\n", service_id
, t
);
2342 } else if (strncmp(t
, "priority=", 9) == 0) {
2343 priority
= strtol(t
+ 9, &end
, 0);
2345 if (priority
< 0 || priority
> 255) {
2346 fatalf("parse_wccp2_service_info: id %d: %s out of range (0..255)!\n", service_id
, t
);
2349 fatalf("parse_wccp2_service_info: id %d: unknown option '%s'\n", service_id
, t
);
2353 /* Check everything is set */
2354 if (priority
== -1) {
2355 fatalf("parse_wccp2_service_info: service %d: no priority defined (valid: 0..255)!\n", service_id
);
2358 if (protocol
== -1) {
2359 fatalf("parse_wccp2_service_info: service %d: no protocol defined (valid: tcp or udp)!\n", service_id
);
2362 if (!(flags
& WCCP2_SERVICE_PORTS_DEFINED
)) {
2363 fatalf("parse_wccp2_service_info: service %d: no ports defined!\n", service_id
);
2366 /* rightio! now we can update */
2367 wccp2_update_service(srv
, WCCP2_SERVICE_DYNAMIC
, service_id
, priority
,
2368 protocol
, flags
, portlist
);
2374 dump_wccp2_service_info(StoreEntry
* e
, const char *label
, void *v
)
2378 struct wccp2_service_list_t
*srv
;
2380 srv
= wccp2_service_list_head
;
2382 while (srv
!= NULL
) {
2383 debugs(80, 3, "dump_wccp2_service_info: id " << srv
->info
.service_id
<< " (type " << srv
->info
.service
<< ")");
2385 /* We don't need to spit out information for standard services */
2387 if (srv
->info
.service
== WCCP2_SERVICE_STANDARD
) {
2388 debugs(80, 3, "dump_wccp2_service_info: id " << srv
->info
.service_id
<< ": standard service, not dumping info");
2395 storeAppendPrintf(e
, "%s %d", label
, srv
->info
.service_id
);
2398 storeAppendPrintf(e
, " priority=%d", srv
->info
.service_priority
);
2401 flags
= ntohl(srv
->info
.service_flags
);
2405 storeAppendPrintf(e
, " flags=");
2407 if (flags
& WCCP2_SERVICE_SRC_IP_HASH
) {
2408 storeAppendPrintf(e
, "%ssrc_ip_hash", comma
? "," : "");
2412 if (flags
& WCCP2_SERVICE_DST_IP_HASH
) {
2413 storeAppendPrintf(e
, "%sdst_ip_hash", comma
? "," : "");
2417 if (flags
& WCCP2_SERVICE_SRC_PORT_HASH
) {
2418 storeAppendPrintf(e
, "%ssource_port_hash", comma
? "," : "");
2422 if (flags
& WCCP2_SERVICE_DST_PORT_HASH
) {
2423 storeAppendPrintf(e
, "%sdst_port_hash", comma
? "," : "");
2427 if (flags
& WCCP2_SERVICE_PORTS_DEFINED
) {
2428 storeAppendPrintf(e
, "%sports_defined", comma
? "," : "");
2432 if (flags
& WCCP2_SERVICE_PORTS_SOURCE
) {
2433 storeAppendPrintf(e
, "%sports_source", comma
? "," : "");
2437 if (flags
& WCCP2_SERVICE_SRC_IP_ALT_HASH
) {
2438 storeAppendPrintf(e
, "%ssrc_ip_alt_hash", comma
? "," : "");
2442 if (flags
& WCCP2_SERVICE_DST_IP_ALT_HASH
) {
2443 storeAppendPrintf(e
, "%ssrc_ip_alt_hash", comma
? "," : "");
2447 if (flags
& WCCP2_SERVICE_SRC_PORT_ALT_HASH
) {
2448 storeAppendPrintf(e
, "%ssrc_port_alt_hash", comma
? "," : "");
2452 if (flags
& WCCP2_SERVICE_DST_PORT_ALT_HASH
) {
2453 storeAppendPrintf(e
, "%sdst_port_alt_hash", comma
? "," : "");
2461 if (srv
->info
.port0
!= 0) {
2462 storeAppendPrintf(e
, "%s%d", comma
? "," : " ports=", ntohs(srv
->info
.port0
));
2466 if (srv
->info
.port1
!= 0) {
2467 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port1
));
2471 if (srv
->info
.port2
!= 0) {
2472 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port2
));
2476 if (srv
->info
.port3
!= 0) {
2477 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port3
));
2481 if (srv
->info
.port4
!= 0) {
2482 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port4
));
2486 if (srv
->info
.port5
!= 0) {
2487 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port5
));
2491 if (srv
->info
.port6
!= 0) {
2492 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port6
));
2496 if (srv
->info
.port7
!= 0) {
2497 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port7
));
2502 storeAppendPrintf(e
, " protocol=%s", (srv
->info
.service_protocol
== IPPROTO_TCP
) ? "tcp" : "udp");
2504 storeAppendPrintf(e
, "\n");
2510 /* Sort the cache list by doing a "selection sort" by IP address */
2512 wccp2SortCacheList(struct wccp2_cache_list_t
*head
)
2514 struct wccp2_cache_list_t tmp
;
2515 struct wccp2_cache_list_t
*this_item
;
2516 struct wccp2_cache_list_t
*find_item
;
2517 struct wccp2_cache_list_t
*next_lowest
;
2519 /* Go through each position in the list one at a time */
2520 for (this_item
= head
; this_item
->next
; this_item
= this_item
->next
) {
2521 /* Find the item with the lowest IP */
2522 next_lowest
= this_item
;
2524 for (find_item
= this_item
; find_item
->next
; find_item
= find_item
->next
) {
2525 if (find_item
->cache_ip
.s_addr
< next_lowest
->cache_ip
.s_addr
) {
2526 next_lowest
= find_item
;
2529 /* Swap if we need to */
2530 if (next_lowest
!= this_item
) {
2531 /* First make a copy of the current item */
2532 memcpy(&tmp
, this_item
, sizeof(struct wccp2_cache_list_t
));
2534 /* Next update the pointers to maintain the linked list */
2535 tmp
.next
= next_lowest
->next
;
2536 next_lowest
->next
= this_item
->next
;
2538 /* Finally copy the updated items to their correct location */
2539 memcpy(this_item
, next_lowest
, sizeof(struct wccp2_cache_list_t
));
2540 memcpy(next_lowest
, &tmp
, sizeof(struct wccp2_cache_list_t
));
2546 free_wccp2_service_info(void *v
)
2549 #endif /* USE_WCCPv2 */