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.
42 #include "ip/IpAddress.h"
48 #define WCCP_PORT 2048
49 #define WCCP_RESPONSE_SIZE 12448
50 #define WCCP_BUCKETS 256
52 static int theWccp2Connection
= -1;
53 static int wccp2_connected
= 0;
55 static PF wccp2HandleUdp
;
56 static EVH wccp2HereIam
;
57 static EVH wccp2AssignBuckets
;
61 #define WCCP2_HASH_ASSIGNMENT 0x00
62 #define WCCP2_MASK_ASSIGNMENT 0x01
64 #define WCCP2_NONE_SECURITY_LEN 0
65 #define WCCP2_MD5_SECURITY_LEN 16
68 #define WCCP2_NUMPORTS 8
69 #define WCCP2_PASSWORD_LEN 8
72 /* WCCPv2 Pakcet format structures */
73 /* Defined in draft-wilson-wccp-v2-12-oct-2001.txt */
76 /** \interface WCCPv2_Protocol
77 * Generic header struct
79 struct wccp2_item_header_t
{
84 /* item type values */
85 #define WCCP2_SECURITY_INFO 0
86 #define WCCP2_SERVICE_INFO 1
87 #define WCCP2_ROUTER_ID_INFO 2
88 #define WCCP2_WC_ID_INFO 3
89 #define WCCP2_RTR_VIEW_INFO 4
90 #define WCCP2_WC_VIEW_INFO 5
91 #define WCCP2_REDIRECT_ASSIGNMENT 6
92 #define WCCP2_QUERY_INFO 7
93 #define WCCP2_CAPABILITY_INFO 8
94 #define WCCP2_ALT_ASSIGNMENT 13
95 #define WCCP2_ASSIGN_MAP 14
96 #define WCCP2_COMMAND_EXTENSION 15
100 /** \interface WCCPv2_Protocol
101 * Sect 5.5 WCCP Message Header
103 struct wccp2_message_header_t
{
106 #define WCCP2_VERSION 0x200
110 static struct wccp2_message_header_t wccp2_here_i_am_header
;
113 #define WCCP2_HERE_I_AM 10
114 #define WCCP2_I_SEE_YOU 11
115 #define WCCP2_REDIRECT_ASSIGN 12
116 #define WCCP2_REMOVAL_QUERY 13
119 /** \interface WCCPv2_Protocol
120 * Sect 5.6.1 Security Info Component
122 * Basic security Header. Matches "no security" case exactly.
124 struct wccp2_security_none_t
{
125 uint16_t security_type
;
126 uint16_t security_length
;
127 uint32_t security_option
;
130 /* security options */
131 #define WCCP2_NO_SECURITY 0
132 #define WCCP2_MD5_SECURITY 1
135 /** \interface WCCPv2_Protocol
136 * Sect 5.6.1 Security Info Component
138 * Extended security section. Matches "MD5 security" type exactly.
139 * Including the security header.
141 struct wccp2_security_md5_t
{
142 uint16_t security_type
;
143 uint16_t security_length
;
144 uint32_t security_option
;
145 uint8_t security_implementation
[WCCP2_MD5_SECURITY_LEN
];
148 /* Service info struct */
150 /** \interface WCCPv2_Protocol
151 * Sect 5.6.2 Service Info Component
153 struct wccp2_service_info_t
{
154 uint16_t service_type
;
155 uint16_t service_length
;
158 uint8_t service_priority
;
159 uint8_t service_protocol
;
160 uint32_t service_flags
;
171 #define WCCP2_SERVICE_STANDARD 0
172 #define WCCP2_SERVICE_DYNAMIC 1
175 #define WCCP2_SERVICE_ID_HTTP 0x00
178 #define WCCP2_SERVICE_SRC_IP_HASH 0x1
179 #define WCCP2_SERVICE_DST_IP_HASH 0x2
180 #define WCCP2_SERVICE_SRC_PORT_HASH 0x4
181 #define WCCP2_SERVICE_DST_PORT_HASH 0x8
182 #define WCCP2_SERVICE_PORTS_DEFINED 0x10
183 #define WCCP2_SERVICE_PORTS_SOURCE 0x20
184 #define WCCP2_SERVICE_SRC_IP_ALT_HASH 0x100
185 #define WCCP2_SERVICE_DST_IP_ALT_HASH 0x200
186 #define WCCP2_SERVICE_SRC_PORT_ALT_HASH 0x400
187 #define WCCP2_SERVICE_DST_PORT_ALT_HASH 0x800
190 /* TODO the following structures need to be re-defined for correct full operation.
191 wccp2_cache_identity_element needs to be merged as a sub-struct of
192 wccp2_identity_info_t (identity_type); which frees up the identifty info
193 structures so mask_assigment_data_element can become variable length
194 and cope with multiple fail-over caches hanging off one router.
197 /** \interface WCCPv2_Protocol
198 * Sect 5.7.2 Web-Cache Identity Element
200 struct wccp2_cache_identity_info_t
{
202 uint16_t hash_revision
;
204 //#define WCCP2_HASH_ASSIGNMENT_DATA 0x0
206 /* 5.7.2 Hash Assignment Data Element */
207 char buckets
[32]; /* Draft indicates 8x 32-bit buckets but it's just a mask so doesn't matter how we define. */
212 /** \interface WCCPv2_Protocol
213 * Sect 5.6.4 Web-Cache Identity Info Component
215 struct wccp2_identity_info_t
{
216 uint16_t cache_identity_type
;
217 uint16_t cache_identity_length
;
219 struct wccp2_cache_identity_info_t cache_identity
;
222 static struct wccp2_identity_info_t wccp2_identity_info
;
224 /** \interface WCCPv2_Protocol
225 * Sect 5.7.7 Mask Element
227 struct wccp2_mask_element_t
{
228 uint32_t source_ip_mask
;
229 uint32_t dest_ip_mask
;
230 uint16_t source_port_mask
;
231 uint16_t dest_port_mask
;
232 uint32_t number_values
;
236 /** \interface WCCPv2_Protocol
237 * Sect 5.7.2 Web-Cache Identity Element
239 struct wccp2_cache_mask_identity_info_t
{
241 uint16_t hash_revision
;
243 #define WCCP2_MASK_ASSIGNMENT_DATA (0x2)
245 /* Sect 5.7.2 Mask Assignment Data Element
247 * NP: draft specifies a variable-length set of keys here.
248 * the following fields only matche the special case Squid sends outbound (single-cache).
250 uint32_t mask_element_count
;
252 /* Sect 5.7.6 Mask/Value Set Element */
253 /* special case: single mask element. no values. */
254 struct wccp2_mask_element_t mask
;
256 /* Sect 5.7.2 Mask Assignment Data Element */
261 /** \interface WCCPv2_Protocol
262 * Sect 5.6.4 Web-Cache Identity Info Component
264 struct wccp2_mask_identity_info_t
{
265 uint16_t cache_identity_type
;
266 uint16_t cache_identity_length
;
268 struct wccp2_cache_mask_identity_info_t cache_identity
;
271 static struct wccp2_mask_identity_info_t wccp2_mask_identity_info
;
273 /** \interface WCCPv2_Protocol
274 * Sect 5.6.5 Router View Info Component
275 * Sect 5.6.6 Web Cache View Info Component
277 * first three fields. (shared by both view components)
279 struct wccp2_cache_view_header_t
{
280 uint16_t cache_view_type
;
281 uint16_t cache_view_length
;
282 uint32_t cache_view_version
;
285 static struct wccp2_cache_view_header_t wccp2_cache_view_header
;
287 /// \interface WCCPv2_Protocol
288 /* NP: special-case 5.6.5 or 5.6.6 * View Info when no routers or caches are advertised? */
289 struct wccp2_cache_view_info_t
{
290 uint32_t num_routers
;
294 static struct wccp2_cache_view_info_t wccp2_cache_view_info
;
296 /** \interface WCCPv2_Protocol
297 * Sect 5.7.1 Router ID Element
299 struct wccp2_router_id_element_t
{
300 struct in_addr router_address
;
301 uint32_t received_id
;
304 static struct wccp2_router_id_element_t wccp2_router_id_element
;
306 /** \interface WCCpv2_Protocol
307 * Sect 5.6.9 Capabilities Info Component
309 struct wccp2_capability_info_header_t
{
310 uint16_t capability_info_type
;
311 uint16_t capability_info_length
;
312 /* dynamic length capabilities list */
315 static struct wccp2_capability_info_header_t wccp2_capability_info_header
;
317 /** \interface WCCPv2_Protocol
318 * 5.7.5 Capability Element
320 struct wccp2_capability_element_t
{
321 uint16_t capability_type
;
322 uint16_t capability_length
;
323 uint32_t capability_value
;
325 static struct wccp2_capability_element_t wccp2_capability_element
;
327 /* capability types */
328 #define WCCP2_CAPABILITY_FORWARDING_METHOD 0x01
329 #define WCCP2_CAPABILITY_ASSIGNMENT_METHOD 0x02
330 #define WCCP2_CAPABILITY_RETURN_METHOD 0x03
332 /* capability values */
333 #define WCCP2_METHOD_GRE 0x00000001
334 #define WCCP2_METHOD_L2 0x00000002
335 /* when type=WCCP2_CAPABILITY_FORWARDING_METHOD */
336 #define WCCP2_FORWARDING_METHOD_GRE WCCP2_METHOD_GRE
337 #define WCCP2_FORWARDING_METHOD_L2 WCCP2_METHOD_L2
338 /* when type=WCCP2_CAPABILITY_ASSIGNMENT_METHOD */
339 #define WCCP2_ASSIGNMENT_METHOD_HASH 0x00000001
340 #define WCCP2_ASSIGNMENT_METHOD_MASK 0x00000002
341 /* when type=WCCP2_CAPABILITY_RETURN_METHOD */
342 #define WCCP2_PACKET_RETURN_METHOD_GRE WCCP2_METHOD_GRE
343 #define WCCP2_PACKET_RETURN_METHOD_L2 WCCP2_METHOD_L2
347 /** \interface WCCPv2_Protocol
348 * 5.7.8 Value Element
350 struct wccp2_value_element_t
{
351 uint32_t source_ip_value
;
352 uint32_t dest_ip_value
;
353 uint16_t source_port_value
;
354 uint16_t dest_port_value
;
356 struct in_addr cache_ip
;
359 /* RECEIVED PACKET STRUCTURE */
361 /** \interface WCCPv2_Protocol
362 * 5.2 'I See You' Message
364 struct wccp2_i_see_you_t
{
368 char data
[WCCP_RESPONSE_SIZE
];
371 static struct wccp2_i_see_you_t wccp2_i_see_you
;
373 /** \interface WCCPv2_Protocol
374 * 5.7.4 Router Assignment Element
376 struct wccp2_router_assign_element_t
{
377 struct in_addr router_address
;
378 uint32_t received_id
;
379 uint32_t change_number
;
382 /* Router identity struct */
384 /** \interface WCCPv2_Protocol
385 * 5.6.3 Router Identity Info Component (partial)
387 struct router_identity_info_t
{
389 struct wccp2_item_header_t header
;
391 struct wccp2_router_id_element_t router_id_element
;
393 struct in_addr router_address
;
394 uint32_t number_caches
;
395 /* dynamic list of cache IP addresses */
398 /* The received packet for a mask assignment is unusual */
400 /** \interface WCCPv2_Protocol
401 * Sect 5.7.7 Mask Element ???
402 * see code below. apparently the supposed IP address at position num1 can be equal to 3.
404 struct cache_mask_info_t
{
411 /** \interface WCCPv2_Protocol
412 * 5.7.3 Assignment Key Element
414 struct assignment_key_t
{
415 struct in_addr master_ip
;
416 uint32_t master_number
;
419 /** \interface WCCPv2_Protocol
420 * 5.6.5 Router View Info Component (first three fields)
422 struct router_view_t
{
423 struct wccp2_item_header_t header
;
424 uint32_t change_number
;
425 struct assignment_key_t assignment_key
;
426 /* dynamic lists of routers and caches elided */
429 /* Lists used to keep track of caches, routers and services */
431 /// \interface WCCPv2_Protocol
432 struct wccp2_cache_list_t
{
434 struct in_addr cache_ip
;
438 struct wccp2_cache_list_t
*next
;
441 /// \interface WCCPv2_Protocol
442 struct wccp2_router_list_t
{
444 struct wccp2_router_id_element_t
*info
;
446 struct in_addr local_ip
;
448 struct in_addr router_sendto_address
;
449 uint32_t member_change
;
452 struct wccp2_cache_list_t cache_list_head
;
454 struct wccp2_router_list_t
*next
;
457 static int wccp2_numrouters
;
459 /// \interface WCCPv2_Protocol
460 struct wccp2_service_list_t
{
462 struct wccp2_service_info_t info
;
463 uint32_t num_routers
;
465 struct wccp2_router_list_t router_list_head
;
469 char *wccp2_identity_info_ptr
;
471 struct wccp2_security_md5_t
*security_info
;
473 struct wccp2_service_info_t
*service_info
;
474 char wccp_packet
[WCCP_RESPONSE_SIZE
];
475 size_t wccp_packet_size
;
477 struct wccp2_service_list_t
*next
;
478 char wccp_password
[WCCP2_PASSWORD_LEN
+ 1]; /* hold the trailing C-string NUL */
479 uint32_t wccp2_security_type
;
482 static struct wccp2_service_list_t
*wccp2_service_list_head
= NULL
;
484 int empty_portlist
[WCCP2_NUMPORTS
] = {0, 0, 0, 0, 0, 0, 0, 0};
486 /* END WCCP V2 PROTOCL TYPES DEFINITION */
488 void wccp2_add_service_list(int service
, int service_id
, int service_priority
,
489 int service_proto
, int service_flags
, int ports
[], int security_type
, char *password
);
490 static void wccp2SortCacheList(struct wccp2_cache_list_t
*head
);
493 * The functions used during startup:
495 * wccp2ConnectionOpen
496 * wccp2ConnectionClose
500 wccp2InitServices(void)
502 debugs(80, 5, "wccp2InitServices: called");
506 wccp2_update_service(struct wccp2_service_list_t
*srv
, int service
,
507 int service_id
, int service_priority
, int service_proto
, int service_flags
,
510 /* XXX check what needs to be wrapped in htons()! */
511 srv
->info
.service
= service
;
512 srv
->info
.service_id
= service_id
;
513 srv
->info
.service_priority
= service_priority
;
514 srv
->info
.service_protocol
= service_proto
;
515 srv
->info
.service_flags
= htonl(service_flags
);
516 srv
->info
.port0
= htons(ports
[0]);
517 srv
->info
.port1
= htons(ports
[1]);
518 srv
->info
.port2
= htons(ports
[2]);
519 srv
->info
.port3
= htons(ports
[3]);
520 srv
->info
.port4
= htons(ports
[4]);
521 srv
->info
.port5
= htons(ports
[5]);
522 srv
->info
.port6
= htons(ports
[6]);
523 srv
->info
.port7
= htons(ports
[7]);
527 wccp2_add_service_list(int service
, int service_id
, int service_priority
,
528 int service_proto
, int service_flags
, int ports
[], int security_type
,
532 struct wccp2_service_list_t
*wccp2_service_list_ptr
;
534 wccp2_service_list_ptr
= (wccp2_service_list_t
*) xcalloc(1, sizeof(struct wccp2_service_list_t
));
536 debugs(80, 5, "wccp2_add_service_list: added service id " << service_id
);
538 /* XXX check what needs to be wrapped in htons()! */
539 wccp2_service_list_ptr
->info
.service_type
= htons(WCCP2_SERVICE_INFO
);
541 wccp2_service_list_ptr
->info
.service_length
= htons(sizeof(struct wccp2_service_info_t
) - 4);
542 wccp2_service_list_ptr
->change_num
= 0;
543 wccp2_update_service(wccp2_service_list_ptr
, service
, service_id
,
544 service_priority
, service_proto
, service_flags
, ports
);
545 wccp2_service_list_ptr
->wccp2_security_type
= security_type
;
546 memset(wccp2_service_list_ptr
->wccp_password
, 0, WCCP2_PASSWORD_LEN
+ 1);
547 strncpy(wccp2_service_list_ptr
->wccp_password
, password
, WCCP2_PASSWORD_LEN
);
548 /* add to linked list - XXX this should use the Squid dlink* routines! */
549 wccp2_service_list_ptr
->next
= wccp2_service_list_head
;
550 wccp2_service_list_head
= wccp2_service_list_ptr
;
553 static struct wccp2_service_list_t
*
554 wccp2_get_service_by_id(int service
, int service_id
) {
556 struct wccp2_service_list_t
*p
;
558 p
= wccp2_service_list_head
;
561 if (p
->info
.service
== service
&& p
->info
.service_id
== service_id
) {
572 * Update the md5 security header, if possible
574 * Returns: 1 if we set it, 0 if not (eg, no security section, or non-md5)
577 wccp2_update_md5_security(char *password
, char *ptr
, char *packet
, int len
)
579 u_int8_t md5_digest
[16];
580 char pwd
[WCCP2_PASSWORD_LEN
];
583 struct wccp2_security_md5_t
*ws
;
585 debugs(80, 5, "wccp2_update_md5_security: called");
587 /* The password field, for the MD5 hash, needs to be 8 bytes and NUL padded. */
588 memset(pwd
, 0, sizeof(pwd
));
589 strncpy(pwd
, password
, sizeof(pwd
));
591 ws
= (struct wccp2_security_md5_t
*) ptr
;
592 assert(ntohs(ws
->security_type
) == WCCP2_SECURITY_INFO
);
593 /* Its the security part */
595 if (ntohl(ws
->security_option
) != WCCP2_MD5_SECURITY
) {
596 debugs(80, 5, "wccp2_update_md5_security: this service ain't md5'ing, abort");
600 /* And now its the MD5 section! */
601 /* According to the draft, the MD5 security hash is the combination of
602 * the 8-octet password (padded w/ NUL bytes) and the entire WCCP packet,
603 * including the WCCP message header. The WCCP security implementation
604 * area should be zero'ed before calculating the MD5 hash.
606 /* XXX eventually we should be able to kill md5_digest and blit it directly in */
607 memset(ws
->security_implementation
, 0, sizeof(ws
->security_implementation
));
611 SquidMD5Update(&M
, pwd
, 8);
613 SquidMD5Update(&M
, packet
, len
);
615 SquidMD5Final(md5_digest
, &M
);
617 memcpy(ws
->security_implementation
, md5_digest
, sizeof(md5_digest
));
625 * Check the given WCCP2 packet against the given password.
629 wccp2_check_security(struct wccp2_service_list_t
*srv
, char *security
, char *packet
, int len
)
632 struct wccp2_security_md5_t
*ws
= (struct wccp2_security_md5_t
*) security
;
633 u_int8_t md5_digest
[16], md5_challenge
[16];
634 char pwd
[WCCP2_PASSWORD_LEN
];
637 /* Make sure the security type matches what we expect */
639 if (ntohl(ws
->security_option
) != srv
->wccp2_security_type
) {
640 debugs(80, 1, "wccp2_check_security: received packet has the wrong security option");
644 if (srv
->wccp2_security_type
== WCCP2_NO_SECURITY
) {
648 if (srv
->wccp2_security_type
!= WCCP2_MD5_SECURITY
) {
649 debugs(80, 1, "wccp2_check_security: invalid security option");
653 /* If execution makes it here then we have an MD5 security */
655 /* The password field, for the MD5 hash, needs to be 8 bytes and NUL padded. */
656 memset(pwd
, 0, sizeof(pwd
));
658 strncpy(pwd
, srv
->wccp_password
, sizeof(pwd
));
660 /* Take a copy of the challenge: we need to NUL it before comparing */
661 memcpy(md5_challenge
, ws
->security_implementation
, 16);
663 memset(ws
->security_implementation
, 0, sizeof(ws
->security_implementation
));
667 SquidMD5Update(&M
, pwd
, 8);
669 SquidMD5Update(&M
, packet
, len
);
671 SquidMD5Final(md5_digest
, &M
);
673 return (memcmp(md5_digest
, md5_challenge
, 16) == 0);
682 uint32_t service_flags
;
684 struct wccp2_service_list_t
*service_list_ptr
;
686 struct wccp2_router_list_t
*router_list_ptr
;
688 struct wccp2_security_md5_t wccp2_security_md5
;
690 debugs(80, 5, "wccp2Init: Called");
692 if (wccp2_connected
== 1)
695 wccp2_numrouters
= 0;
697 /* Calculate the number of routers configured in the config file */
698 for (s
= Config
.Wccp2
.router
; s
; s
= s
->next
) {
699 if (!s
->s
.IsAnyAddr()) {
700 /* Increment the counter */
705 if (wccp2_numrouters
== 0) {
709 /* Initialise the list of services */
712 service_list_ptr
= wccp2_service_list_head
;
714 while (service_list_ptr
!= NULL
) {
715 /* Set up our list pointers */
716 router_list_ptr
= &service_list_ptr
->router_list_head
;
718 /* start the wccp header */
719 wccp2_here_i_am_header
.type
= htonl(WCCP2_HERE_I_AM
);
720 wccp2_here_i_am_header
.version
= htons(WCCP2_VERSION
);
721 wccp2_here_i_am_header
.length
= 0;
722 ptr
= service_list_ptr
->wccp_packet
+ sizeof(wccp2_here_i_am_header
);
724 /* add the security section */
725 /* XXX this is ugly */
727 if (service_list_ptr
->wccp2_security_type
== WCCP2_MD5_SECURITY
) {
728 wccp2_security_md5
.security_option
= htonl(WCCP2_MD5_SECURITY
);
730 wccp2_security_md5
.security_length
= htons(sizeof(struct wccp2_security_md5_t
) - 4);
731 } else if (service_list_ptr
->wccp2_security_type
== WCCP2_NO_SECURITY
) {
732 wccp2_security_md5
.security_option
= htonl(WCCP2_NO_SECURITY
);
733 /* XXX I hate magic length numbers! */
734 wccp2_security_md5
.security_length
= htons(4);
736 fatalf("Bad WCCP2 security type\n");
739 wccp2_here_i_am_header
.length
+= ntohs(wccp2_security_md5
.security_length
) + 4;
740 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
741 wccp2_security_md5
.security_type
= htons(WCCP2_SECURITY_INFO
);
743 service_list_ptr
->security_info
= (struct wccp2_security_md5_t
*) ptr
;
745 if (service_list_ptr
->wccp2_security_type
== WCCP2_MD5_SECURITY
) {
747 xmemcpy(ptr
, &wccp2_security_md5
, sizeof(struct wccp2_security_md5_t
));
749 ptr
+= sizeof(struct wccp2_security_md5_t
);
751 /* assume NONE, and XXX I hate magic length numbers */
752 xmemcpy(ptr
, &wccp2_security_md5
, 8);
756 /* Add the service info section */
758 wccp2_here_i_am_header
.length
+= sizeof(struct wccp2_service_info_t
);
760 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
762 xmemcpy(ptr
, &service_list_ptr
->info
, sizeof(struct wccp2_service_info_t
));
764 service_list_ptr
->service_info
= (struct wccp2_service_info_t
*) ptr
;
766 ptr
+= sizeof(struct wccp2_service_info_t
);
768 /* Add the cache identity section */
770 switch (Config
.Wccp2
.assignment_method
) {
772 case WCCP2_ASSIGNMENT_METHOD_HASH
:
774 wccp2_here_i_am_header
.length
+= sizeof(struct wccp2_identity_info_t
);
775 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
776 wccp2_identity_info
.cache_identity_type
= htons(WCCP2_WC_ID_INFO
);
777 wccp2_identity_info
.cache_identity_length
= htons(sizeof(wccp2_identity_info
.cache_identity
));
778 memset(&wccp2_identity_info
.cache_identity
.addr
, '\0', sizeof(struct in_addr
));
779 memset(&wccp2_identity_info
.cache_identity
.hash_revision
, '\0', sizeof(wccp2_identity_info
.cache_identity
.hash_revision
));
780 memset(&wccp2_identity_info
.cache_identity
.bits
, '\0', sizeof(wccp2_identity_info
.cache_identity
.bits
));
781 memset(&wccp2_identity_info
.cache_identity
.buckets
, '\0', sizeof(wccp2_identity_info
.cache_identity
.buckets
));
782 wccp2_identity_info
.cache_identity
.weight
= htons(Config
.Wccp2
.weight
);
783 memset(&wccp2_identity_info
.cache_identity
.status
, '\0', sizeof(wccp2_identity_info
.cache_identity
.status
));
785 xmemcpy(ptr
, &wccp2_identity_info
, sizeof(struct wccp2_identity_info_t
));
786 service_list_ptr
->wccp2_identity_info_ptr
= ptr
;
788 ptr
+= sizeof(struct wccp2_identity_info_t
);
791 case WCCP2_ASSIGNMENT_METHOD_MASK
:
793 wccp2_here_i_am_header
.length
+= sizeof(struct wccp2_mask_identity_info_t
);
794 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
795 wccp2_mask_identity_info
.cache_identity_type
= htons(WCCP2_WC_ID_INFO
);
796 wccp2_mask_identity_info
.cache_identity_length
= htons(sizeof(wccp2_mask_identity_info
.cache_identity
));
797 memset(&wccp2_mask_identity_info
.cache_identity
.addr
, '\0', sizeof(struct in_addr
));
798 wccp2_mask_identity_info
.cache_identity
.bits
= htons(WCCP2_MASK_ASSIGNMENT_DATA
);
799 wccp2_mask_identity_info
.cache_identity
.mask_element_count
= htonl(1);
800 service_flags
= ntohl(service_list_ptr
->service_info
->service_flags
);
802 memset(&wccp2_mask_identity_info
.cache_identity
.mask
, 0, sizeof(struct wccp2_mask_element_t
));
804 if ((service_flags
& WCCP2_SERVICE_SRC_IP_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_IP_ALT_HASH
)) {
805 wccp2_mask_identity_info
.cache_identity
.mask
.source_ip_mask
= htonl(0x00001741);
806 } 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
)) {
807 wccp2_mask_identity_info
.cache_identity
.mask
.dest_ip_mask
= htonl(0x00001741);
808 } else if ((service_flags
& WCCP2_SERVICE_SRC_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_PORT_ALT_HASH
)) {
809 wccp2_mask_identity_info
.cache_identity
.mask
.source_port_mask
= htons(0x1741);
810 } else if ((service_flags
& WCCP2_SERVICE_DST_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_DST_PORT_ALT_HASH
)) {
811 wccp2_mask_identity_info
.cache_identity
.mask
.dest_port_mask
= htons(0x1741);
813 fatalf("Unknown service hash method\n");
816 wccp2_mask_identity_info
.cache_identity
.weight
= 0;
817 wccp2_mask_identity_info
.cache_identity
.status
= 0;
819 xmemcpy(ptr
, &wccp2_mask_identity_info
, sizeof(struct wccp2_mask_identity_info_t
));
820 service_list_ptr
->wccp2_identity_info_ptr
= ptr
;
822 ptr
+= sizeof(struct wccp2_mask_identity_info_t
);
826 fatalf("Unknown Wccp2 assignment method\n");
829 /* Add the cache view section */
830 wccp2_here_i_am_header
.length
+= sizeof(wccp2_cache_view_header
);
832 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
834 wccp2_cache_view_header
.cache_view_type
= htons(WCCP2_WC_VIEW_INFO
);
836 wccp2_cache_view_header
.cache_view_length
= htons(sizeof(wccp2_cache_view_header
) - 4 +
837 sizeof(wccp2_cache_view_info
) + (wccp2_numrouters
* sizeof(wccp2_router_id_element
)));
839 wccp2_cache_view_header
.cache_view_version
= htonl(1);
841 xmemcpy(ptr
, &wccp2_cache_view_header
, sizeof(wccp2_cache_view_header
));
843 ptr
+= sizeof(wccp2_cache_view_header
);
845 /* Add the number of routers to the packet */
846 wccp2_here_i_am_header
.length
+= sizeof(service_list_ptr
->num_routers
);
848 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
850 service_list_ptr
->num_routers
= htonl(wccp2_numrouters
);
852 xmemcpy(ptr
, &service_list_ptr
->num_routers
, sizeof(service_list_ptr
->num_routers
));
854 ptr
+= sizeof(service_list_ptr
->num_routers
);
856 /* Add each router. Keep this functionality here to make sure the received_id can be updated in the packet */
857 for (s
= Config
.Wccp2
.router
; s
; s
= s
->next
) {
858 if (!s
->s
.IsAnyAddr()) {
860 wccp2_here_i_am_header
.length
+= sizeof(struct wccp2_router_id_element_t
);
861 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
863 /* Add a pointer to the router list for this router */
865 router_list_ptr
->info
= (struct wccp2_router_id_element_t
*) ptr
;
866 s
->s
.GetInAddr(router_list_ptr
->info
->router_address
);
867 router_list_ptr
->info
->received_id
= htonl(0);
868 s
->s
.GetInAddr(router_list_ptr
->router_sendto_address
);
869 router_list_ptr
->member_change
= htonl(0);
871 /* Build the next struct */
873 router_list_ptr
->next
= (wccp2_router_list_t
*) xcalloc(1, sizeof(struct wccp2_router_list_t
));
875 /* update the pointer */
876 router_list_ptr
= router_list_ptr
->next
;
877 router_list_ptr
->next
= NULL
;
879 /* no need to copy memory - we've just set the values directly in the packet above */
881 ptr
+= sizeof(struct wccp2_router_id_element_t
);
885 /* Add the number of caches (0) */
886 wccp2_here_i_am_header
.length
+= sizeof(wccp2_cache_view_info
.num_caches
);
888 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
890 wccp2_cache_view_info
.num_caches
= htonl(0);
892 xmemcpy(ptr
, &wccp2_cache_view_info
.num_caches
, sizeof(wccp2_cache_view_info
.num_caches
));
894 ptr
+= sizeof(wccp2_cache_view_info
.num_caches
);
896 /* Add the extra capability header */
897 wccp2_here_i_am_header
.length
+= sizeof(wccp2_capability_info_header
);
899 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
901 wccp2_capability_info_header
.capability_info_type
= htons(WCCP2_CAPABILITY_INFO
);
903 wccp2_capability_info_header
.capability_info_length
= htons(3 * sizeof(wccp2_capability_element
));
905 xmemcpy(ptr
, &wccp2_capability_info_header
, sizeof(wccp2_capability_info_header
));
907 ptr
+= sizeof(wccp2_capability_info_header
);
909 /* Add the forwarding method */
910 wccp2_here_i_am_header
.length
+= sizeof(wccp2_capability_element
);
912 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
914 wccp2_capability_element
.capability_type
= htons(WCCP2_CAPABILITY_FORWARDING_METHOD
);
916 wccp2_capability_element
.capability_length
= htons(sizeof(wccp2_capability_element
.capability_value
));
918 wccp2_capability_element
.capability_value
= htonl(Config
.Wccp2
.forwarding_method
);
920 xmemcpy(ptr
, &wccp2_capability_element
, sizeof(wccp2_capability_element
));
922 ptr
+= sizeof(wccp2_capability_element
);
924 /* Add the assignment method */
925 wccp2_here_i_am_header
.length
+= sizeof(wccp2_capability_element
);
927 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
929 wccp2_capability_element
.capability_type
= htons(WCCP2_CAPABILITY_ASSIGNMENT_METHOD
);
931 wccp2_capability_element
.capability_length
= htons(sizeof(wccp2_capability_element
.capability_value
));
933 wccp2_capability_element
.capability_value
= htonl(Config
.Wccp2
.assignment_method
);
935 xmemcpy(ptr
, &wccp2_capability_element
, sizeof(wccp2_capability_element
));
937 ptr
+= sizeof(wccp2_capability_element
);
939 /* Add the return method */
940 wccp2_here_i_am_header
.length
+= sizeof(wccp2_capability_element
);
942 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
944 wccp2_capability_element
.capability_type
= htons(WCCP2_CAPABILITY_RETURN_METHOD
);
946 wccp2_capability_element
.capability_length
= htons(sizeof(wccp2_capability_element
.capability_value
));
948 wccp2_capability_element
.capability_value
= htonl(Config
.Wccp2
.return_method
);
950 xmemcpy(ptr
, &wccp2_capability_element
, sizeof(wccp2_capability_element
));
952 ptr
+= sizeof(wccp2_capability_element
);
954 /* Finally, fix the total length to network order, and copy to the appropriate memory blob */
955 wccp2_here_i_am_header
.length
= htons(wccp2_here_i_am_header
.length
);
957 memcpy(&service_list_ptr
->wccp_packet
, &wccp2_here_i_am_header
, sizeof(wccp2_here_i_am_header
));
959 service_list_ptr
->wccp_packet_size
= ntohs(wccp2_here_i_am_header
.length
) + sizeof(wccp2_here_i_am_header
);
961 /* Add the event if everything initialised correctly */
962 debugs(80,3,"wccp2Init: scheduled 'HERE_I_AM' message to " << wccp2_numrouters
<< "routers.");
963 if (wccp2_numrouters
) {
964 if (!eventFind(wccp2HereIam
, NULL
)) {
965 eventAdd("wccp2HereIam", wccp2HereIam
, NULL
, 1, 1);
967 debugs(80,3,"wccp2Init: skip duplicate 'HERE_I_AM'.");
970 service_list_ptr
= service_list_ptr
->next
;
975 wccp2ConnectionOpen(void)
977 struct sockaddr_in router
, local
, null
;
978 socklen_t local_len
, router_len
;
980 struct wccp2_service_list_t
*service_list_ptr
;
982 struct wccp2_router_list_t
*router_list_ptr
;
984 debugs(80, 5, "wccp2ConnectionOpen: Called");
986 if (wccp2_numrouters
== 0 || !wccp2_service_list_head
) {
987 debugs(80, 2, "WCCPv2 Disabled.");
991 if ( !Config
.Wccp2
.address
.SetIPv4() ) {
992 debugs(80, 0, "WCCPv2 Disabled. " << Config
.Wccp2
.address
<< " is not an IPv4 address.");
996 Config
.Wccp2
.address
.SetPort(WCCP_PORT
);
997 theWccp2Connection
= comm_open_listener(SOCK_DGRAM
,
999 Config
.Wccp2
.address
,
1003 if (theWccp2Connection
< 0)
1004 fatal("Cannot open WCCP Port");
1006 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
1008 int i
= IP_PMTUDISC_DONT
;
1009 setsockopt(theWccp2Connection
, SOL_IP
, IP_MTU_DISCOVER
, &i
, sizeof i
);
1013 commSetSelect(theWccp2Connection
,
1019 debugs(80, 1, "Accepting WCCPv2 messages on port " << WCCP_PORT
<< ", FD " << theWccp2Connection
<< ".");
1020 debugs(80, 1, "Initialising all WCCPv2 lists");
1022 /* Initialise all routers on all services */
1023 memset(&null
, 0, sizeof(null
));
1025 null
.sin_family
= AF_UNSPEC
;
1027 service_list_ptr
= wccp2_service_list_head
;
1029 while (service_list_ptr
!= NULL
) {
1030 for (router_list_ptr
= &service_list_ptr
->router_list_head
; router_list_ptr
->next
!= NULL
; router_list_ptr
= router_list_ptr
->next
) {
1031 router_len
= sizeof(router
);
1032 memset(&router
, '\0', router_len
);
1033 router
.sin_family
= AF_INET
;
1034 router
.sin_port
= htons(WCCP_PORT
);
1035 router
.sin_addr
= router_list_ptr
->router_sendto_address
;
1037 if (connect(theWccp2Connection
, (struct sockaddr
*) &router
, router_len
))
1038 fatal("Unable to connect WCCP out socket");
1040 local_len
= sizeof(local
);
1042 memset(&local
, '\0', local_len
);
1044 if (getsockname(theWccp2Connection
, (struct sockaddr
*) &local
, &local_len
))
1045 fatal("Unable to getsockname on WCCP out socket");
1047 router_list_ptr
->local_ip
= local
.sin_addr
;
1049 /* Disconnect the sending socket. Note: FreeBSD returns error
1050 * but disconnects anyway so we have to just assume it worked
1052 if (wccp2_numrouters
> 1)
1053 connect(theWccp2Connection
, (struct sockaddr
*) &null
, router_len
);
1056 service_list_ptr
= service_list_ptr
->next
;
1059 wccp2_connected
= 1;
1063 wccp2ConnectionClose(void)
1066 struct wccp2_service_list_t
*service_list_ptr
;
1068 struct wccp2_service_list_t
*service_list_ptr_next
;
1070 struct wccp2_router_list_t
*router_list_ptr
;
1072 struct wccp2_router_list_t
*router_list_next
;
1074 struct wccp2_cache_list_t
*cache_list_ptr
;
1076 struct wccp2_cache_list_t
*cache_list_ptr_next
;
1078 if (wccp2_connected
== 0) {
1082 if (theWccp2Connection
> -1) {
1083 debugs(80, 1, "FD " << theWccp2Connection
<< " Closing WCCPv2 socket");
1084 comm_close(theWccp2Connection
);
1085 theWccp2Connection
= -1;
1088 /* for each router on each service send a packet */
1089 service_list_ptr
= wccp2_service_list_head
;
1091 while (service_list_ptr
!= NULL
) {
1092 for (router_list_ptr
= &service_list_ptr
->router_list_head
; router_list_ptr
!= NULL
; router_list_ptr
= router_list_next
) {
1093 for (cache_list_ptr
= &router_list_ptr
->cache_list_head
; cache_list_ptr
; cache_list_ptr
= cache_list_ptr_next
) {
1094 cache_list_ptr_next
= cache_list_ptr
->next
;
1096 if (cache_list_ptr
!= &router_list_ptr
->cache_list_head
) {
1097 xfree(cache_list_ptr
);
1100 memset(cache_list_ptr
, '\0', sizeof(struct wccp2_cache_list_t
));
1104 router_list_next
= router_list_ptr
->next
;
1106 if (router_list_ptr
!= &service_list_ptr
->router_list_head
) {
1107 xfree(router_list_ptr
);
1110 memset(router_list_ptr
, '\0', sizeof(struct wccp2_router_list_t
));
1114 service_list_ptr_next
= service_list_ptr
->next
;
1115 xfree(service_list_ptr
);
1116 service_list_ptr
= service_list_ptr_next
;
1119 wccp2_service_list_head
= NULL
;
1120 eventDelete(wccp2HereIam
, NULL
);
1121 eventDelete(wccp2AssignBuckets
, NULL
);
1122 eventDelete(wccp2HereIam
, NULL
);
1123 wccp2_connected
= 0;
1127 * Functions for handling the requests.
1131 * Accept the UDP packet
1134 wccp2HandleUdp(int sock
, void *not_used
)
1137 struct wccp2_service_list_t
*service_list_ptr
;
1139 struct wccp2_router_list_t
*router_list_ptr
;
1141 struct wccp2_cache_list_t
*cache_list_ptr
;
1143 struct wccp2_cache_list_t
*cache_list_ptr_next
;
1145 /* These structs form the parts of the packet */
1147 struct wccp2_item_header_t
*header
= NULL
;
1149 struct wccp2_security_none_t
*security_info
= NULL
;
1151 struct wccp2_service_info_t
*service_info
= NULL
;
1153 struct router_identity_info_t
*router_identity_info
= NULL
;
1155 struct router_view_t
*router_view_header
= NULL
;
1157 struct wccp2_cache_mask_identity_info_t
*cache_mask_identity
= NULL
;
1159 struct cache_mask_info_t
*cache_mask_info
= NULL
;
1161 struct wccp2_cache_identity_info_t
*cache_identity
= NULL
;
1163 struct wccp2_capability_info_header_t
*router_capability_header
= NULL
;
1165 struct wccp2_capability_element_t
*router_capability_element
;
1167 struct sockaddr_in from
;
1169 struct in_addr cache_address
;
1171 short int data_length
, offset
;
1176 debugs(80, 6, "wccp2HandleUdp: Called.");
1178 commSetSelect(sock
, COMM_SELECT_READ
, wccp2HandleUdp
, NULL
, 0);
1180 /* FIXME INET6 : drop conversion boundary */
1183 len
= comm_udp_recvfrom(sock
,
1188 /* FIXME INET6 : drop conversion boundary */
1189 from_tmp
.GetSockAddr(from
);
1194 if (ntohs(wccp2_i_see_you
.version
) != WCCP2_VERSION
)
1197 if (ntohl(wccp2_i_see_you
.type
) != WCCP2_I_SEE_YOU
)
1200 debugs(80, 3, "Incoming WCCPv2 I_SEE_YOU length " << ntohs(wccp2_i_see_you
.length
) << ".");
1202 /* Record the total data length */
1203 data_length
= ntohs(wccp2_i_see_you
.length
);
1207 if (data_length
> len
) {
1208 debugs(80, 1, "ERROR: Malformed WCCPv2 packet claiming it's bigger than received data");
1212 /* Go through the data structure */
1213 while (data_length
> offset
) {
1215 char *data
= wccp2_i_see_you
.data
;
1217 header
= (struct wccp2_item_header_t
*) &data
[offset
];
1219 switch (ntohs(header
->type
)) {
1221 case WCCP2_SECURITY_INFO
:
1223 if (security_info
!= NULL
) {
1224 debugs(80, 1, "Duplicate security definition");
1228 security_info
= (struct wccp2_security_none_t
*) &wccp2_i_see_you
.data
[offset
];
1231 case WCCP2_SERVICE_INFO
:
1233 if (service_info
!= NULL
) {
1234 debugs(80, 1, "Duplicate service_info definition");
1238 service_info
= (struct wccp2_service_info_t
*) &wccp2_i_see_you
.data
[offset
];
1241 case WCCP2_ROUTER_ID_INFO
:
1243 if (router_identity_info
!= NULL
) {
1244 debugs(80, 1, "Duplicate router_identity_info definition");
1248 router_identity_info
= (struct router_identity_info_t
*) &wccp2_i_see_you
.data
[offset
];
1251 case WCCP2_RTR_VIEW_INFO
:
1253 if (router_view_header
!= NULL
) {
1254 debugs(80, 1, "Duplicate router_view definition");
1258 router_view_header
= (struct router_view_t
*) &wccp2_i_see_you
.data
[offset
];
1261 case WCCP2_CAPABILITY_INFO
:
1263 if (router_capability_header
!= NULL
) {
1264 debugs(80, 1, "Duplicate router_capability definition");
1268 router_capability_header
= (struct wccp2_capability_info_header_t
*) &wccp2_i_see_you
.data
[offset
];
1271 /* Nothing to do for the types below */
1273 case WCCP2_ASSIGN_MAP
:
1277 debugs(80, 1, "Unknown record type in WCCPv2 Packet (" << ntohs(header
->type
) << ").");
1280 offset
+= sizeof(struct wccp2_item_header_t
);
1281 offset
+= ntohs(header
->length
);
1283 if (offset
> data_length
) {
1284 debugs(80, 1, "Error: WCCPv2 packet tried to tell us there is data beyond the end of the packet");
1289 if ((security_info
== NULL
) || (service_info
== NULL
) || (router_identity_info
== NULL
) || (router_view_header
== NULL
)) {
1290 debugs(80, 1, "Incomplete WCCPv2 Packet");
1294 debugs(80, 5, "Complete packet received");
1296 /* Check that the service in the packet is configured on this router */
1297 service_list_ptr
= wccp2_service_list_head
;
1299 while (service_list_ptr
!= NULL
) {
1300 if (service_info
->service_id
== service_list_ptr
->service_info
->service_id
) {
1304 service_list_ptr
= service_list_ptr
->next
;
1307 if (service_list_ptr
== NULL
) {
1308 debugs(80, 1, "WCCPv2 Unknown service received from router (" << service_info
->service_id
<< ")");
1312 if (ntohl(security_info
->security_option
) != ntohl(service_list_ptr
->security_info
->security_option
)) {
1313 debugs(80, 1, "Invalid security option in WCCPv2 Packet (" << ntohl(security_info
->security_option
) << " vs " << ntohl(service_list_ptr
->security_info
->security_option
) << ").");
1317 if (!wccp2_check_security(service_list_ptr
, (char *) security_info
, (char *) &wccp2_i_see_you
, len
)) {
1318 debugs(80, 1, "Received WCCPv2 Packet failed authentication");
1322 /* Check that the router address is configured on this router */
1323 for (router_list_ptr
= &service_list_ptr
->router_list_head
; router_list_ptr
->next
!= NULL
; router_list_ptr
= router_list_ptr
->next
) {
1324 if (router_list_ptr
->router_sendto_address
.s_addr
== from
.sin_addr
.s_addr
)
1328 if (router_list_ptr
->next
== NULL
) {
1329 debugs(80, 1, "WCCPv2 Packet received from unknown router");
1333 /* Set the router id */
1334 router_list_ptr
->info
->router_address
= router_identity_info
->router_id_element
.router_address
;
1336 /* Increment the received id in the packet */
1337 if (ntohl(router_list_ptr
->info
->received_id
) != ntohl(router_identity_info
->router_id_element
.received_id
)) {
1338 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
) << ".");
1339 router_list_ptr
->info
->received_id
= router_identity_info
->router_id_element
.received_id
;
1342 /* TODO: check return/forwarding methods */
1343 if (router_capability_header
== NULL
) {
1344 if ((Config
.Wccp2
.return_method
!= WCCP2_PACKET_RETURN_METHOD_GRE
) || (Config
.Wccp2
.forwarding_method
!= WCCP2_FORWARDING_METHOD_GRE
)) {
1345 debugs(80, 1, "wccp2HandleUdp: fatal error - A WCCP router does not support the forwarding method specified, only GRE supported");
1346 wccp2ConnectionClose();
1351 char *end
= ((char *) router_capability_header
) + sizeof(*router_capability_header
) + ntohs(router_capability_header
->capability_info_length
) - sizeof(struct wccp2_capability_info_header_t
);
1353 router_capability_element
= (struct wccp2_capability_element_t
*) (((char *) router_capability_header
) + sizeof(*router_capability_header
));
1355 while ((char *) router_capability_element
<= end
) {
1357 switch (ntohs(router_capability_element
->capability_type
)) {
1359 case WCCP2_CAPABILITY_FORWARDING_METHOD
:
1361 if (!(ntohl(router_capability_element
->capability_value
) & Config
.Wccp2
.forwarding_method
)) {
1362 debugs(80, 1, "wccp2HandleUdp: fatal error - A WCCP router has specified a different forwarding method " << ntohl(router_capability_element
->capability_value
) << ", expected " << Config
.Wccp2
.forwarding_method
);
1363 wccp2ConnectionClose();
1369 case WCCP2_CAPABILITY_ASSIGNMENT_METHOD
:
1371 if (!(ntohl(router_capability_element
->capability_value
) & Config
.Wccp2
.assignment_method
)) {
1372 debugs(80, 1, "wccp2HandleUdp: fatal error - A WCCP router has specified a different assignment method " << ntohl(router_capability_element
->capability_value
) << ", expected "<< Config
.Wccp2
.assignment_method
);
1373 wccp2ConnectionClose();
1379 case WCCP2_CAPABILITY_RETURN_METHOD
:
1381 if (!(ntohl(router_capability_element
->capability_value
) & Config
.Wccp2
.return_method
)) {
1382 debugs(80, 1, "wccp2HandleUdp: fatal error - A WCCP router has specified a different return method " << ntohl(router_capability_element
->capability_value
) << ", expected " << Config
.Wccp2
.return_method
);
1383 wccp2ConnectionClose();
1390 debugs(80, 1, "Unknown capability type in WCCPv2 Packet (" << ntohs(router_capability_element
->capability_type
) << ").");
1393 router_capability_element
= (struct wccp2_capability_element_t
*) (((char *) router_capability_element
) + sizeof(struct wccp2_item_header_t
) + ntohs(router_capability_element
->capability_length
));
1397 debugs(80, 5, "Cleaning out cache list");
1398 /* clean out the old cache list */
1400 for (cache_list_ptr
= &router_list_ptr
->cache_list_head
; cache_list_ptr
; cache_list_ptr
= cache_list_ptr_next
) {
1401 cache_list_ptr_next
= cache_list_ptr
->next
;
1403 if (cache_list_ptr
!= &router_list_ptr
->cache_list_head
) {
1404 xfree(cache_list_ptr
);
1408 router_list_ptr
->num_caches
= htonl(0);
1411 /* Check to see if we're the master cache and update the cache list */
1413 service_list_ptr
->lowest_ip
= 1;
1414 cache_list_ptr
= &router_list_ptr
->cache_list_head
;
1416 /* to find the list of caches, we start at the end of the router view header */
1418 ptr
= (char *) (router_view_header
) + sizeof(struct router_view_t
);
1420 /* Then we read the number of routers */
1421 memcpy(&tmp
, ptr
, sizeof(tmp
));
1423 /* skip the number plus all the ip's */
1425 ptr
+= sizeof(tmp
) + (ntohl(tmp
) * sizeof(struct in_addr
));
1427 /* Then read the number of caches */
1428 memcpy(&tmp
, ptr
, sizeof(tmp
));
1431 if (ntohl(tmp
) != 0) {
1432 /* search through the list of received-from ip addresses */
1434 for (num_caches
= 0; num_caches
< (int) ntohl(tmp
); num_caches
++) {
1435 /* Get a copy of the ip */
1436 memset(&cache_address
, 0, sizeof(cache_address
)); // Make GCC happy
1438 switch (Config
.Wccp2
.assignment_method
) {
1440 case WCCP2_ASSIGNMENT_METHOD_HASH
:
1442 cache_identity
= (struct wccp2_cache_identity_info_t
*) ptr
;
1444 ptr
+= sizeof(struct wccp2_cache_identity_info_t
);
1446 memcpy(&cache_address
, &cache_identity
->addr
, sizeof(struct in_addr
));
1448 cache_list_ptr
->weight
= ntohs(cache_identity
->weight
);
1451 case WCCP2_ASSIGNMENT_METHOD_MASK
:
1453 cache_mask_info
= (struct cache_mask_info_t
*) ptr
;
1455 /* The mask assignment has an undocumented variable length entry here */
1457 if (ntohl(cache_mask_info
->num1
) == 3) {
1459 cache_mask_identity
= (struct wccp2_cache_mask_identity_info_t
*) ptr
;
1461 ptr
+= sizeof(struct wccp2_cache_mask_identity_info_t
);
1463 memcpy(&cache_address
, &cache_mask_identity
->addr
, sizeof(struct in_addr
));
1466 ptr
+= sizeof(struct cache_mask_info_t
);
1468 memcpy(&cache_address
, &cache_mask_info
->addr
, sizeof(struct in_addr
));
1471 cache_list_ptr
->weight
= 0;
1475 fatalf("Unknown Wccp2 assignment method\n");
1478 /* Update the cache list */
1479 cache_list_ptr
->cache_ip
= cache_address
;
1481 cache_list_ptr
->next
= (wccp2_cache_list_t
*) xcalloc(1, sizeof(struct wccp2_cache_list_t
));
1483 cache_list_ptr
= cache_list_ptr
->next
;
1485 cache_list_ptr
->next
= NULL
;
1487 debugs (80, 5, "checking cache list: (" << std::hex
<< cache_address
.s_addr
<< ":" << router_list_ptr
->local_ip
.s_addr
<< ")");
1489 /* Check to see if it's the master, or us */
1491 if (cache_address
.s_addr
== router_list_ptr
->local_ip
.s_addr
) {
1495 if (cache_address
.s_addr
< router_list_ptr
->local_ip
.s_addr
) {
1496 service_list_ptr
->lowest_ip
= 0;
1500 debugs(80, 5, "Adding ourselves as the only cache");
1502 /* Update the cache list */
1503 cache_list_ptr
->cache_ip
= router_list_ptr
->local_ip
;
1505 cache_list_ptr
->next
= (wccp2_cache_list_t
*) xcalloc(1, sizeof(struct wccp2_cache_list_t
));
1506 cache_list_ptr
= cache_list_ptr
->next
;
1507 cache_list_ptr
->next
= NULL
;
1509 service_list_ptr
->lowest_ip
= 1;
1514 wccp2SortCacheList(&router_list_ptr
->cache_list_head
);
1516 router_list_ptr
->num_caches
= htonl(num_caches
);
1518 if ((found
== 1) && (service_list_ptr
->lowest_ip
== 1)) {
1519 if (ntohl(router_view_header
->change_number
) != router_list_ptr
->member_change
) {
1520 debugs(80, 4, "Change detected - queueing up new assignment");
1521 router_list_ptr
->member_change
= ntohl(router_view_header
->change_number
);
1522 eventDelete(wccp2AssignBuckets
, NULL
);
1523 eventAdd("wccp2AssignBuckets", wccp2AssignBuckets
, NULL
, 15.0, 1);
1525 debugs(80, 5, "Change not detected (" << ntohl(router_view_header
->change_number
) << " = " << router_list_ptr
->member_change
<< ")");
1528 eventDelete(wccp2AssignBuckets
, NULL
);
1529 debugs(80, 5, "I am not the lowest ip cache - not assigning buckets");
1534 wccp2HereIam(void *voidnotused
)
1537 struct wccp2_service_list_t
*service_list_ptr
;
1539 struct wccp2_router_list_t
*router_list_ptr
;
1541 struct wccp2_identity_info_t
*wccp2_identity_info_ptr
;
1543 struct wccp2_mask_identity_info_t
*wccp2_mask_identity_info_ptr
;
1547 debugs(80, 6, "wccp2HereIam: Called");
1549 if (wccp2_connected
== 0) {
1550 debugs(80, 1, "wccp2HereIam: wccp2 socket closed. Shutting down WCCP2");
1554 /* Wait if store dirs are rebuilding */
1555 if (StoreController::store_dirs_rebuilding
&& Config
.Wccp2
.rebuildwait
) {
1556 eventAdd("wccp2HereIam", wccp2HereIam
, NULL
, 1.0, 1);
1560 router
.SetPort(WCCP_PORT
);
1562 /* for each router on each service send a packet */
1563 service_list_ptr
= wccp2_service_list_head
;
1565 while (service_list_ptr
!= NULL
) {
1566 debugs(80, 5, "wccp2HereIam: sending to service id " << service_list_ptr
->info
.service_id
);
1568 for (router_list_ptr
= &service_list_ptr
->router_list_head
; router_list_ptr
->next
!= NULL
; router_list_ptr
= router_list_ptr
->next
) {
1569 router
= router_list_ptr
->router_sendto_address
;
1571 /* Set the cache id (ip) */
1573 switch (Config
.Wccp2
.assignment_method
) {
1575 case WCCP2_ASSIGNMENT_METHOD_HASH
:
1577 wccp2_identity_info_ptr
= (struct wccp2_identity_info_t
*) service_list_ptr
->wccp2_identity_info_ptr
;
1578 wccp2_identity_info_ptr
->cache_identity
.addr
= router_list_ptr
->local_ip
;
1581 case WCCP2_ASSIGNMENT_METHOD_MASK
:
1583 wccp2_mask_identity_info_ptr
= (struct wccp2_mask_identity_info_t
*) service_list_ptr
->wccp2_identity_info_ptr
;
1584 wccp2_mask_identity_info_ptr
->cache_identity
.addr
= router_list_ptr
->local_ip
;
1588 fatalf("Unknown Wccp2 assignment method\n");
1591 /* Security update, if needed */
1593 if (service_list_ptr
->wccp2_security_type
== WCCP2_MD5_SECURITY
) {
1594 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
);
1597 debugs(80, 3, "Sending HereIam packet size " << service_list_ptr
->wccp_packet_size
);
1598 /* Send the packet */
1600 if (wccp2_numrouters
> 1) {
1601 comm_udp_sendto(theWccp2Connection
,
1603 &service_list_ptr
->wccp_packet
,
1604 service_list_ptr
->wccp_packet_size
);
1606 send(theWccp2Connection
,
1607 &service_list_ptr
->wccp_packet
,
1608 service_list_ptr
->wccp_packet_size
,
1613 service_list_ptr
= service_list_ptr
->next
;
1616 eventAdd("wccp2HereIam", wccp2HereIam
, NULL
, 10.0, 1);
1620 wccp2AssignBuckets(void *voidnotused
)
1623 struct wccp2_service_list_t
*service_list_ptr
;
1625 struct wccp2_router_list_t
*router_list_ptr
;
1627 struct wccp2_cache_list_t
*cache_list_ptr
;
1628 char wccp_packet
[WCCP_RESPONSE_SIZE
];
1629 short int offset
, saved_offset
, assignment_offset
, alt_assignment_offset
;
1631 struct sockaddr_in router
;
1634 uint32_t service_flags
;
1635 u_short port
= WCCP_PORT
;
1637 /* Packet segments */
1639 struct wccp2_message_header_t
*main_header
;
1641 struct wccp2_security_md5_t
*security
= NULL
;
1642 /* service from service struct */
1644 struct wccp2_item_header_t
*assignment_header
;
1646 struct wccp2_item_header_t
*alt_assignment_type_header
= NULL
;
1648 struct assignment_key_t
*assignment_key
;
1649 /* number of routers */
1651 struct wccp2_router_assign_element_t
*router_assign
;
1652 /* number of caches */
1654 struct in_addr
*cache_address
;
1655 /* Alternative assignement mask/values */
1658 struct wccp2_mask_element_t
*mask_element
;
1660 struct wccp2_value_element_t
*value_element
;
1661 int valuecounter
, value
;
1664 assignment_offset
= alt_assignment_offset
= 0;
1666 router_len
= sizeof(router
);
1667 memset(&router
, '\0', router_len
);
1668 router
.sin_family
= AF_INET
;
1669 router
.sin_port
= htons(port
);
1671 /* Start main header - fill in length later */
1674 main_header
= (struct wccp2_message_header_t
*) &wccp_packet
[offset
];
1675 main_header
->type
= htonl(WCCP2_REDIRECT_ASSIGN
);
1676 main_header
->version
= htons(WCCP2_VERSION
);
1678 debugs(80, 2, "Running wccp2AssignBuckets");
1679 service_list_ptr
= wccp2_service_list_head
;
1681 while (service_list_ptr
!= NULL
) {
1682 /* If we're not the lowest, we don't need to worry */
1684 if (service_list_ptr
->lowest_ip
== 0) {
1686 service_list_ptr
= service_list_ptr
->next
;
1690 /* reset the offset */
1692 offset
= sizeof(struct wccp2_message_header_t
);
1694 /* build packet header from hereIam packet */
1696 /* XXX this should be made more generic! */
1697 /* XXX and I hate magic numbers! */
1698 switch (service_list_ptr
->wccp2_security_type
) {
1700 case WCCP2_NO_SECURITY
:
1702 security
= (struct wccp2_security_md5_t
*) &wccp_packet
[offset
];
1703 memcpy(security
, service_list_ptr
->security_info
, 8);
1707 case WCCP2_MD5_SECURITY
:
1709 security
= (struct wccp2_security_md5_t
*) &wccp_packet
[offset
];
1711 memcpy(security
, service_list_ptr
->security_info
, sizeof(struct wccp2_security_md5_t
));
1713 offset
+= sizeof(struct wccp2_security_md5_t
);
1717 fatalf("Unknown Wccp2 security type\n");
1722 memcpy(&wccp_packet
[offset
], service_list_ptr
->service_info
, sizeof(struct wccp2_service_info_t
));
1724 offset
+= sizeof(struct wccp2_service_info_t
);
1726 /* assignment header - fill in length later */
1728 assignment_header
= (struct wccp2_item_header_t
*) &wccp_packet
[offset
];
1730 switch (Config
.Wccp2
.assignment_method
) {
1732 case WCCP2_ASSIGNMENT_METHOD_HASH
:
1733 assignment_header
->type
= htons(WCCP2_REDIRECT_ASSIGNMENT
);
1735 offset
+= sizeof(struct wccp2_item_header_t
);
1736 assignment_offset
= offset
;
1739 case WCCP2_ASSIGNMENT_METHOD_MASK
:
1740 assignment_header
->type
= htons(WCCP2_ALT_ASSIGNMENT
);
1742 offset
+= sizeof(struct wccp2_item_header_t
);
1743 assignment_offset
= offset
;
1745 /* The alternative assignment has an extra header, fill in length later */
1747 alt_assignment_type_header
= (struct wccp2_item_header_t
*) &wccp_packet
[offset
];
1748 alt_assignment_type_header
->type
= htons(WCCP2_MASK_ASSIGNMENT
);
1750 offset
+= sizeof(struct wccp2_item_header_t
);
1751 alt_assignment_offset
= offset
;
1756 fatalf("Unknown Wccp2 assignment method\n");
1759 /* Assignment key - fill in master ip later */
1761 assignment_key
= (struct assignment_key_t
*) &wccp_packet
[offset
];
1763 assignment_key
->master_number
= htonl(++service_list_ptr
->change_num
);
1765 offset
+= sizeof(struct assignment_key_t
);
1767 /* Number of routers */
1768 xmemcpy(&wccp_packet
[offset
], &service_list_ptr
->num_routers
, sizeof(service_list_ptr
->num_routers
));
1770 offset
+= sizeof(service_list_ptr
->num_routers
);
1772 for (router_list_ptr
= &service_list_ptr
->router_list_head
; router_list_ptr
->next
!= NULL
; router_list_ptr
= router_list_ptr
->next
) {
1776 router_assign
= (struct wccp2_router_assign_element_t
*) &wccp_packet
[offset
];
1777 router_assign
->router_address
= router_list_ptr
->info
->router_address
;
1778 router_assign
->received_id
= router_list_ptr
->info
->received_id
;
1779 router_assign
->change_number
= htonl(router_list_ptr
->member_change
);
1781 offset
+= sizeof(struct wccp2_router_assign_element_t
);
1784 saved_offset
= offset
;
1786 for (router_list_ptr
= &service_list_ptr
->router_list_head
; router_list_ptr
->next
!= NULL
; router_list_ptr
= router_list_ptr
->next
) {
1787 unsigned long *weight
= (unsigned long *)xcalloc(sizeof(*weight
), ntohl(router_list_ptr
->num_caches
));
1788 unsigned long total_weight
= 0;
1789 int num_caches
= ntohl(router_list_ptr
->num_caches
);
1791 offset
= saved_offset
;
1793 switch (Config
.Wccp2
.assignment_method
) {
1795 case WCCP2_ASSIGNMENT_METHOD_HASH
:
1796 /* Number of caches */
1797 xmemcpy(&wccp_packet
[offset
], &router_list_ptr
->num_caches
, sizeof(router_list_ptr
->num_caches
));
1798 offset
+= sizeof(router_list_ptr
->num_caches
);
1803 for (cache
= 0, cache_list_ptr
= &router_list_ptr
->cache_list_head
; cache_list_ptr
->next
; cache_list_ptr
= cache_list_ptr
->next
, cache
++) {
1806 cache_address
= (struct in_addr
*) &wccp_packet
[offset
];
1808 xmemcpy(cache_address
, &cache_list_ptr
->cache_ip
, sizeof(struct in_addr
));
1809 total_weight
+= cache_list_ptr
->weight
<< 12;
1810 weight
[cache
] = cache_list_ptr
->weight
<< 12;
1812 offset
+= sizeof(struct in_addr
);
1817 buckets
= (char *) &wccp_packet
[offset
];
1819 memset(buckets
, '\0', WCCP_BUCKETS
);
1821 if (num_caches
!= 0) {
1822 if (total_weight
== 0) {
1823 for (bucket_counter
= 0; bucket_counter
< WCCP_BUCKETS
; bucket_counter
++) {
1824 buckets
[bucket_counter
] = (char) (bucket_counter
% num_caches
);
1827 unsigned long *assigned
= (unsigned long *)xcalloc(sizeof(*assigned
), num_caches
);
1828 unsigned long done
= 0;
1830 unsigned long per_bucket
= total_weight
/ WCCP_BUCKETS
;
1832 for (bucket_counter
= 0; bucket_counter
< WCCP_BUCKETS
; bucket_counter
++) {
1836 for (n
= num_caches
; n
; n
--) {
1839 if (cache
>= num_caches
)
1842 if (!weight
[cache
]) {
1847 if (assigned
[cache
] <= done
)
1851 buckets
[bucket_counter
] = (char) cache
;
1852 step
= per_bucket
* total_weight
/ weight
[cache
];
1853 assigned
[cache
] += step
;
1857 safe_free(assigned
);
1861 offset
+= (WCCP_BUCKETS
* sizeof(char));
1865 case WCCP2_ASSIGNMENT_METHOD_MASK
:
1866 num_maskval
= htonl(1);
1867 xmemcpy(&wccp_packet
[offset
], &num_maskval
, sizeof(int));
1868 offset
+= sizeof(int);
1870 mask_element
= (struct wccp2_mask_element_t
*) &wccp_packet
[offset
];
1871 service_flags
= ntohl(service_list_ptr
->service_info
->service_flags
);
1873 if ((service_flags
& WCCP2_SERVICE_SRC_IP_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_IP_ALT_HASH
)) {
1874 mask_element
->source_ip_mask
= htonl(0x00001741);
1875 mask_element
->dest_ip_mask
= 0;
1876 mask_element
->source_port_mask
= 0;
1877 mask_element
->dest_port_mask
= 0;
1878 } 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
)) {
1879 mask_element
->source_ip_mask
= 0;
1880 mask_element
->dest_ip_mask
= htonl(0x00001741);
1881 mask_element
->source_port_mask
= 0;
1882 mask_element
->dest_port_mask
= 0;
1883 } else if ((service_flags
& WCCP2_SERVICE_SRC_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_PORT_ALT_HASH
)) {
1884 mask_element
->source_ip_mask
= 0;
1885 mask_element
->dest_ip_mask
= 0;
1886 mask_element
->source_port_mask
= htons(0x1741);
1887 mask_element
->dest_port_mask
= 0;
1888 } else if ((service_flags
& WCCP2_SERVICE_DST_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_DST_PORT_ALT_HASH
)) {
1889 mask_element
->source_ip_mask
= 0;
1890 mask_element
->dest_ip_mask
= 0;
1891 mask_element
->source_port_mask
= 0;
1892 mask_element
->dest_port_mask
= htons(0x1741);
1894 fatalf("Unknown service hash method\n");
1897 mask_element
->number_values
= htonl(64);
1899 offset
+= sizeof(struct wccp2_mask_element_t
);
1901 cache_list_ptr
= &router_list_ptr
->cache_list_head
;
1904 for (valuecounter
= 0; valuecounter
< 64; valuecounter
++) {
1906 value_element
= (struct wccp2_value_element_t
*) &wccp_packet
[offset
];
1908 /* Update the value according the the "correct" formula */
1910 for (; (value
& 0x1741) != value
; value
++) {
1911 assert(value
<= 0x1741);
1914 if ((service_flags
& WCCP2_SERVICE_SRC_IP_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_IP_ALT_HASH
)) {
1915 value_element
->source_ip_value
= htonl(value
);
1916 value_element
->dest_ip_value
= 0;
1917 value_element
->source_port_value
= 0;
1918 value_element
->dest_port_value
= 0;
1919 } 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
)) {
1920 value_element
->source_ip_value
= 0;
1921 value_element
->dest_ip_value
= htonl(value
);
1922 value_element
->source_port_value
= 0;
1923 value_element
->dest_port_value
= 0;
1924 } else if ((service_flags
& WCCP2_SERVICE_SRC_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_PORT_ALT_HASH
)) {
1925 value_element
->source_ip_value
= 0;
1926 value_element
->dest_ip_value
= 0;
1927 value_element
->source_port_value
= htons(value
);
1928 value_element
->dest_port_value
= 0;
1929 } else if ((service_flags
& WCCP2_SERVICE_DST_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_DST_PORT_ALT_HASH
)) {
1930 value_element
->source_ip_value
= 0;
1931 value_element
->dest_ip_value
= 0;
1932 value_element
->source_port_value
= 0;
1933 value_element
->dest_port_value
= htons(value
);
1935 fatalf("Unknown service hash method\n");
1938 value_element
->cache_ip
= cache_list_ptr
->cache_ip
;
1940 offset
+= sizeof(struct wccp2_value_element_t
);
1943 /* Assign the next value to the next cache */
1945 if ((cache_list_ptr
->next
) && (cache_list_ptr
->next
->next
))
1946 cache_list_ptr
= cache_list_ptr
->next
;
1948 cache_list_ptr
= &router_list_ptr
->cache_list_head
;
1951 /* Fill in length */
1952 alt_assignment_type_header
->length
= htons(offset
- alt_assignment_offset
);
1957 fatalf("Unknown Wccp2 assignment method\n");
1960 /* Fill in length */
1962 assignment_header
->length
= htons(offset
- assignment_offset
);
1964 /* Fill in assignment key */
1965 assignment_key
->master_ip
= router_list_ptr
->local_ip
;
1969 main_header
->length
= htons(offset
- sizeof(struct wccp2_message_header_t
));
1971 /* set the destination address */
1972 router
.sin_addr
= router_list_ptr
->router_sendto_address
;
1974 /* Security update, if needed */
1976 if (service_list_ptr
->wccp2_security_type
== WCCP2_MD5_SECURITY
) {
1977 wccp2_update_md5_security(service_list_ptr
->wccp_password
, (char *) security
, wccp_packet
, offset
);
1980 if (ntohl(router_list_ptr
->num_caches
)) {
1983 if (wccp2_numrouters
> 1) {
1984 /* FIXME INET6 : drop temp conversion */
1985 IpAddress
tmp_rtr(router
);
1986 comm_udp_sendto(theWccp2Connection
,
1991 send(theWccp2Connection
,
1999 service_list_ptr
= service_list_ptr
->next
;
2005 * Configuration option parsing code
2009 * Parse wccp2_return_method and wccp2_forwarding_method options
2010 * they can be '1' aka 'gre' or '2' aka 'l2'
2011 * repesenting the integer numeric of the same.
2014 parse_wccp2_method(int *method
)
2018 /* Snarf the method */
2019 if ((t
= strtok(NULL
, w_space
)) == NULL
) {
2020 debugs(80, DBG_CRITICAL
, "wccp2_*_method: missing setting.");
2024 /* update configuration if its valid */
2025 if (strcmp(t
, "gre") == 0 || strcmp(t
, "1") == 0) {
2026 *method
= WCCP2_METHOD_GRE
;
2027 } else if (strcmp(t
, "l2") == 0 || strcmp(t
, "2") == 0) {
2028 *method
= WCCP2_METHOD_L2
;
2030 debugs(80, DBG_CRITICAL
, "wccp2_*_method: unknown setting, got " << t
);
2036 dump_wccp2_method(StoreEntry
* e
, const char *label
, int v
)
2039 case WCCP2_METHOD_GRE
:
2040 storeAppendPrintf(e
, "%s gre\n", label
);
2042 case WCCP2_METHOD_L2
:
2043 storeAppendPrintf(e
, "%s l2\n", label
);
2046 debugs(80, DBG_CRITICAL
, "FATAL: WCCPv2 configured method (" << v
<< ") is not valid.");
2052 free_wccp2_method(int *v
)
2056 * Parse wccp2_assignment_method option
2057 * they can be '1' aka 'hash' or '2' aka 'mask'
2058 * repesenting the integer numeric of the same.
2061 parse_wccp2_amethod(int *method
)
2065 /* Snarf the method */
2066 if ((t
= strtok(NULL
, w_space
)) == NULL
) {
2067 debugs(80, DBG_CRITICAL
, "wccp2_assignment_method: missing setting.");
2071 /* update configuration if its valid */
2072 if (strcmp(t
, "hash") == 0 || strcmp(t
, "1") == 0) {
2073 *method
= WCCP2_ASSIGNMENT_METHOD_HASH
;
2074 } else if (strcmp(t
, "mask") == 0 || strcmp(t
, "2") == 0) {
2075 *method
= WCCP2_ASSIGNMENT_METHOD_MASK
;
2077 debugs(80, DBG_CRITICAL
, "wccp2_assignment_method: unknown setting, got " << t
);
2083 dump_wccp2_amethod(StoreEntry
* e
, const char *label
, int v
)
2086 case WCCP2_ASSIGNMENT_METHOD_HASH
:
2087 storeAppendPrintf(e
, "%s hash\n", label
);
2089 case WCCP2_ASSIGNMENT_METHOD_MASK
:
2090 storeAppendPrintf(e
, "%s mask\n", label
);
2093 debugs(80, DBG_CRITICAL
, "FATAL: WCCPv2 configured " << label
<< " (" << v
<< ") is not valid.");
2099 free_wccp2_amethod(int *v
)
2105 * wccp2_service {standard|dynamic} {id} (password=password)
2108 parse_wccp2_service(void *v
)
2113 int security_type
= WCCP2_NO_SECURITY
;
2114 char wccp_password
[WCCP2_PASSWORD_LEN
+ 1];
2116 if (wccp2_connected
== 1) {
2117 debugs(80, 1, "WCCPv2: Somehow reparsing the configuration without having shut down WCCP! Try reloading squid again.");
2121 /* Snarf the type */
2122 if ((t
= strtok(NULL
, w_space
)) == NULL
) {
2123 debugs(80, 0, "wccp2ParseServiceInfo: missing service info type (standard|dynamic)");
2127 if (strcmp(t
, "standard") == 0) {
2128 service
= WCCP2_SERVICE_STANDARD
;
2129 } else if (strcmp(t
, "dynamic") == 0) {
2130 service
= WCCP2_SERVICE_DYNAMIC
;
2132 debugs(80, 0, "wccp2ParseServiceInfo: bad service info type (expected standard|dynamic, got " << t
<< ")");
2137 service_id
= GetInteger();
2139 if (service_id
< 0 || service_id
> 255) {
2140 debugs(80, 0, "wccp2ParseServiceInfo: service info id " << service_id
<< " is out of range (0..255)");
2144 memset(wccp_password
, 0, sizeof(wccp_password
));
2145 /* Handle password, if any */
2147 if ((t
= strtok(NULL
, w_space
)) != NULL
) {
2148 if (strncmp(t
, "password=", 9) == 0) {
2149 security_type
= WCCP2_MD5_SECURITY
;
2150 strncpy(wccp_password
, t
+ 9, WCCP2_PASSWORD_LEN
);
2154 /* Create a placeholder service record */
2155 wccp2_add_service_list(service
, service_id
, 0, 0, 0, empty_portlist
, security_type
, wccp_password
);
2159 dump_wccp2_service(StoreEntry
* e
, const char *label
, void *v
)
2162 struct wccp2_service_list_t
*srv
;
2163 srv
= wccp2_service_list_head
;
2165 while (srv
!= NULL
) {
2166 debugs(80, 3, "dump_wccp2_service: id " << srv
->info
.service_id
<< ", type " << srv
->info
.service
);
2167 storeAppendPrintf(e
, "%s %s %d", label
,
2168 (srv
->info
.service
== WCCP2_SERVICE_DYNAMIC
) ? "dynamic" : "standard",
2169 srv
->info
.service_id
);
2171 if (srv
->wccp2_security_type
== WCCP2_MD5_SECURITY
) {
2172 storeAppendPrintf(e
, " %s", srv
->wccp_password
);
2175 storeAppendPrintf(e
, "\n");
2182 free_wccp2_service(void *v
)
2186 check_null_wccp2_service(void *v
)
2188 return !wccp2_service_list_head
;
2194 * wccp2_service_info {id} stuff..
2198 * + flags=flag,flag,flag..
2199 * + proto=protocol (tcp|udp)
2200 * + ports=port,port,port (up to a max of 8)
2201 * + priority=priority (0->255)
2203 * The flags here are:
2204 * src_ip_hash, dst_ip_hash, source_port_hash, dst_port_hash, ports_defined,
2205 * ports_source, src_ip_alt_hash, dst_ip_alt_hash, src_port_alt_hash, dst_port_alt_hash
2208 parse_wccp2_service_flags(char *flags
)
2218 tmp
= xstrdup(flags
);
2221 flag
= strsep(&tmp2
, ",");
2224 if (strcmp(flag
, "src_ip_hash") == 0) {
2225 retflag
|= WCCP2_SERVICE_SRC_IP_HASH
;
2226 } else if (strcmp(flag
, "dst_ip_hash") == 0) {
2227 retflag
|= WCCP2_SERVICE_DST_IP_HASH
;
2228 } else if (strcmp(flag
, "source_port_hash") == 0) {
2229 retflag
|= WCCP2_SERVICE_SRC_PORT_HASH
;
2230 } else if (strcmp(flag
, "dst_port_hash") == 0) {
2231 retflag
|= WCCP2_SERVICE_DST_PORT_HASH
;
2232 } else if (strcmp(flag
, "ports_source") == 0) {
2233 retflag
|= WCCP2_SERVICE_PORTS_SOURCE
;
2234 } else if (strcmp(flag
, "src_ip_alt_hash") == 0) {
2235 retflag
|= WCCP2_SERVICE_SRC_IP_ALT_HASH
;
2236 } else if (strcmp(flag
, "dst_ip_alt_hash") == 0) {
2237 retflag
|= WCCP2_SERVICE_DST_IP_ALT_HASH
;
2238 } else if (strcmp(flag
, "src_port_alt_hash") == 0) {
2239 retflag
|= WCCP2_SERVICE_SRC_PORT_ALT_HASH
;
2240 } else if (strcmp(flag
, "dst_port_alt_hash") == 0) {
2241 retflag
|= WCCP2_SERVICE_DST_PORT_ALT_HASH
;
2243 fatalf("Unknown wccp2 service flag: %s\n", flag
);
2246 flag
= strsep(&tmp2
, ",");
2254 parse_wccp2_service_ports(char *options
, int portlist
[])
2258 char *tmp
, *tmp2
, *port
, *end
;
2264 tmp
= xstrdup(options
);
2267 port
= strsep(&tmp2
, ",");
2269 while (port
&& i
< WCCP2_NUMPORTS
) {
2270 p
= strtol(port
, &end
, 0);
2272 if (p
< 1 || p
> 65535) {
2273 fatalf("parse_wccp2_service_ports: port value '%s' isn't valid (1..65535)\n", port
);
2278 port
= strsep(&tmp2
, ",");
2282 fatalf("parse_wccp2_service_ports: too many ports (maximum: 8) in list '%s'\n", options
);
2289 parse_wccp2_service_info(void *v
)
2294 int portlist
[WCCP2_NUMPORTS
];
2295 int protocol
= -1; /* IPPROTO_TCP | IPPROTO_UDP */
2297 struct wccp2_service_list_t
*srv
;
2300 if (wccp2_connected
== 1) {
2301 debugs(80, 1, "WCCPv2: Somehow reparsing the configuration without having shut down WCCP! Try reloading squid again.");
2305 debugs(80, 5, "parse_wccp2_service_info: called");
2306 memset(portlist
, 0, sizeof(portlist
));
2307 /* First argument: id */
2308 service_id
= GetInteger();
2310 if (service_id
< 0 || service_id
> 255) {
2311 debugs(80, 1, "parse_wccp2_service_info: invalid service id " << service_id
<< " (must be between 0 .. 255)");
2315 /* Next: find the (hopefully!) existing service */
2316 srv
= wccp2_get_service_by_id(WCCP2_SERVICE_DYNAMIC
, service_id
);
2319 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
);
2322 /* Next: loop until we don't have any more tokens */
2323 while ((t
= strtok(NULL
, w_space
)) != NULL
) {
2324 if (strncmp(t
, "flags=", 6) == 0) {
2325 /* XXX eww, string pointer math */
2326 flags
= parse_wccp2_service_flags(t
+ 6);
2327 } else if (strncmp(t
, "ports=", 6) == 0) {
2328 parse_wccp2_service_ports(t
+ 6, portlist
);
2329 flags
|= WCCP2_SERVICE_PORTS_DEFINED
;
2330 } else if (strncmp(t
, "protocol=tcp", 12) == 0) {
2331 protocol
= IPPROTO_TCP
;
2332 } else if (strncmp(t
, "protocol=udp", 12) == 0) {
2333 protocol
= IPPROTO_UDP
;
2334 } else if (strncmp(t
, "protocol=", 9) == 0) {
2335 fatalf("parse_wccp2_service_info: id %d: unknown protocol (%s) - must be tcp or udp!\n", service_id
, t
);
2336 } else if (strncmp(t
, "priority=", 9) == 0) {
2337 priority
= strtol(t
+ 9, &end
, 0);
2339 if (priority
< 0 || priority
> 255) {
2340 fatalf("parse_wccp2_service_info: id %d: %s out of range (0..255)!\n", service_id
, t
);
2343 fatalf("parse_wccp2_service_info: id %d: unknown option '%s'\n", service_id
, t
);
2347 /* Check everything is set */
2348 if (priority
== -1) {
2349 fatalf("parse_wccp2_service_info: service %d: no priority defined (valid: 0..255)!\n", service_id
);
2352 if (protocol
== -1) {
2353 fatalf("parse_wccp2_service_info: service %d: no protocol defined (valid: tcp or udp)!\n", service_id
);
2356 if (!(flags
& WCCP2_SERVICE_PORTS_DEFINED
)) {
2357 fatalf("parse_wccp2_service_info: service %d: no ports defined!\n", service_id
);
2360 /* rightio! now we can update */
2361 wccp2_update_service(srv
, WCCP2_SERVICE_DYNAMIC
, service_id
, priority
,
2362 protocol
, flags
, portlist
);
2368 dump_wccp2_service_info(StoreEntry
* e
, const char *label
, void *v
)
2372 struct wccp2_service_list_t
*srv
;
2374 srv
= wccp2_service_list_head
;
2376 while (srv
!= NULL
) {
2377 debugs(80, 3, "dump_wccp2_service_info: id " << srv
->info
.service_id
<< " (type " << srv
->info
.service
<< ")");
2379 /* We don't need to spit out information for standard services */
2381 if (srv
->info
.service
== WCCP2_SERVICE_STANDARD
) {
2382 debugs(80, 3, "dump_wccp2_service_info: id " << srv
->info
.service_id
<< ": standard service, not dumping info");
2389 storeAppendPrintf(e
, "%s %d", label
, srv
->info
.service_id
);
2392 storeAppendPrintf(e
, " priority=%d", srv
->info
.service_priority
);
2395 flags
= ntohl(srv
->info
.service_flags
);
2399 storeAppendPrintf(e
, " flags=");
2401 if (flags
& WCCP2_SERVICE_SRC_IP_HASH
) {
2402 storeAppendPrintf(e
, "%ssrc_ip_hash", comma
? "," : "");
2406 if (flags
& WCCP2_SERVICE_DST_IP_HASH
) {
2407 storeAppendPrintf(e
, "%sdst_ip_hash", comma
? "," : "");
2411 if (flags
& WCCP2_SERVICE_SRC_PORT_HASH
) {
2412 storeAppendPrintf(e
, "%ssource_port_hash", comma
? "," : "");
2416 if (flags
& WCCP2_SERVICE_DST_PORT_HASH
) {
2417 storeAppendPrintf(e
, "%sdst_port_hash", comma
? "," : "");
2421 if (flags
& WCCP2_SERVICE_PORTS_DEFINED
) {
2422 storeAppendPrintf(e
, "%sports_defined", comma
? "," : "");
2426 if (flags
& WCCP2_SERVICE_PORTS_SOURCE
) {
2427 storeAppendPrintf(e
, "%sports_source", comma
? "," : "");
2431 if (flags
& WCCP2_SERVICE_SRC_IP_ALT_HASH
) {
2432 storeAppendPrintf(e
, "%ssrc_ip_alt_hash", comma
? "," : "");
2436 if (flags
& WCCP2_SERVICE_DST_IP_ALT_HASH
) {
2437 storeAppendPrintf(e
, "%ssrc_ip_alt_hash", comma
? "," : "");
2441 if (flags
& WCCP2_SERVICE_SRC_PORT_ALT_HASH
) {
2442 storeAppendPrintf(e
, "%ssrc_port_alt_hash", comma
? "," : "");
2446 if (flags
& WCCP2_SERVICE_DST_PORT_ALT_HASH
) {
2447 storeAppendPrintf(e
, "%sdst_port_alt_hash", comma
? "," : "");
2455 if (srv
->info
.port0
!= 0) {
2456 storeAppendPrintf(e
, "%s%d", comma
? "," : " ports=", ntohs(srv
->info
.port0
));
2460 if (srv
->info
.port1
!= 0) {
2461 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port1
));
2465 if (srv
->info
.port2
!= 0) {
2466 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port2
));
2470 if (srv
->info
.port3
!= 0) {
2471 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port3
));
2475 if (srv
->info
.port4
!= 0) {
2476 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port4
));
2480 if (srv
->info
.port5
!= 0) {
2481 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port5
));
2485 if (srv
->info
.port6
!= 0) {
2486 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port6
));
2490 if (srv
->info
.port7
!= 0) {
2491 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port7
));
2496 storeAppendPrintf(e
, " protocol=%s", (srv
->info
.service_protocol
== IPPROTO_TCP
) ? "tcp" : "udp");
2498 storeAppendPrintf(e
, "\n");
2504 /* Sort the cache list by doing a "selection sort" by IP address */
2506 wccp2SortCacheList(struct wccp2_cache_list_t
*head
)
2508 struct wccp2_cache_list_t tmp
;
2509 struct wccp2_cache_list_t
*this_item
;
2510 struct wccp2_cache_list_t
*find_item
;
2511 struct wccp2_cache_list_t
*next_lowest
;
2513 /* Go through each position in the list one at a time */
2514 for (this_item
= head
; this_item
->next
; this_item
= this_item
->next
) {
2515 /* Find the item with the lowest IP */
2516 next_lowest
= this_item
;
2518 for (find_item
= this_item
; find_item
->next
; find_item
= find_item
->next
) {
2519 if (find_item
->cache_ip
.s_addr
< next_lowest
->cache_ip
.s_addr
) {
2520 next_lowest
= find_item
;
2523 /* Swap if we need to */
2524 if (next_lowest
!= this_item
) {
2525 /* First make a copy of the current item */
2526 memcpy(&tmp
, this_item
, sizeof(struct wccp2_cache_list_t
));
2528 /* Next update the pointers to maintain the linked list */
2529 tmp
.next
= next_lowest
->next
;
2530 next_lowest
->next
= this_item
->next
;
2532 /* Finally copy the updated items to their correct location */
2533 memcpy(this_item
, next_lowest
, sizeof(struct wccp2_cache_list_t
));
2534 memcpy(next_lowest
, &tmp
, sizeof(struct wccp2_cache_list_t
));
2540 free_wccp2_service_info(void *v
)
2543 #endif /* USE_WCCPv2 */