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
;
205 //#define WCCP2_HASH_ASSIGNMENT_DATA 0x0
207 /* 5.7.2 Hash Assignment Data Element */
208 char buckets
[32]; /* Draft indicates 8x 32-bit buckets but it's just a mask so doesn't matter how we define. */
213 /** \interface WCCPv2_Protocol
214 * Sect 5.6.4 Web-Cache Identity Info Component
216 struct wccp2_identity_info_t
{
217 uint16_t cache_identity_type
;
218 uint16_t cache_identity_length
;
220 struct wccp2_cache_identity_info_t cache_identity
;
223 static struct wccp2_identity_info_t wccp2_identity_info
;
225 /** \interface WCCPv2_Protocol
226 * Sect 5.7.7 Mask Element
228 struct wccp2_mask_element_t
{
229 uint32_t source_ip_mask
;
230 uint32_t dest_ip_mask
;
231 uint16_t source_port_mask
;
232 uint16_t dest_port_mask
;
233 uint32_t number_values
;
237 /** \interface WCCPv2_Protocol
238 * Sect 5.7.2 Web-Cache Identity Element
240 struct wccp2_cache_mask_identity_info_t
{
242 uint16_t hash_revision
;
244 #define WCCP2_MASK_ASSIGNMENT_DATA (0x1 <<14)
246 /* Sect 5.7.2 Mask Assignment Data Element
248 * NP: draft specifies a variable-length set of keys here.
249 * the following fields only matche the special case Squid sends outbound (single-cache).
251 uint32_t mask_element_count
;
253 /* Sect 5.7.6 Mask/Value Set Element */
254 /* special case: single mask element. no values. */
255 struct wccp2_mask_element_t mask
;
257 /* Sect 5.7.2 Mask Assignment Data Element */
262 /** \interface WCCPv2_Protocol
263 * Sect 5.6.4 Web-Cache Identity Info Component
265 struct wccp2_mask_identity_info_t
{
266 uint16_t cache_identity_type
;
267 uint16_t cache_identity_length
;
269 struct wccp2_cache_mask_identity_info_t cache_identity
;
272 static struct wccp2_mask_identity_info_t wccp2_mask_identity_info
;
274 /** \interface WCCPv2_Protocol
275 * Sect 5.6.5 Router View Info Component
276 * Sect 5.6.6 Web Cache View Info Component
278 * first three fields. (shared by both view components)
280 struct wccp2_cache_view_header_t
{
281 uint16_t cache_view_type
;
282 uint16_t cache_view_length
;
283 uint32_t cache_view_version
;
286 static struct wccp2_cache_view_header_t wccp2_cache_view_header
;
288 /// \interface WCCPv2_Protocol
289 /* NP: special-case 5.6.5 or 5.6.6 * View Info when no routers or caches are advertised? */
290 struct wccp2_cache_view_info_t
{
291 uint32_t num_routers
;
295 static struct wccp2_cache_view_info_t wccp2_cache_view_info
;
297 /** \interface WCCPv2_Protocol
298 * Sect 5.7.1 Router ID Element
300 struct wccp2_router_id_element_t
{
301 struct in_addr router_address
;
302 uint32_t received_id
;
305 static struct wccp2_router_id_element_t wccp2_router_id_element
;
307 /** \interface WCCpv2_Protocol
308 * Sect 5.6.9 Capabilities Info Component
310 struct wccp2_capability_info_header_t
{
311 uint16_t capability_info_type
;
312 uint16_t capability_info_length
;
313 /* dynamic length capabilities list */
316 static struct wccp2_capability_info_header_t wccp2_capability_info_header
;
318 /** \interface WCCPv2_Protocol
319 * 5.7.5 Capability Element
321 struct wccp2_capability_element_t
{
322 uint16_t capability_type
;
323 uint16_t capability_length
;
324 uint32_t capability_value
;
326 static struct wccp2_capability_element_t wccp2_capability_element
;
328 /* capability types */
329 #define WCCP2_CAPABILITY_FORWARDING_METHOD 0x01
330 #define WCCP2_CAPABILITY_ASSIGNMENT_METHOD 0x02
331 #define WCCP2_CAPABILITY_RETURN_METHOD 0x03
333 /* capability values */
334 #define WCCP2_METHOD_GRE 0x00000001
335 #define WCCP2_METHOD_L2 0x00000002
336 /* when type=WCCP2_CAPABILITY_FORWARDING_METHOD */
337 #define WCCP2_FORWARDING_METHOD_GRE WCCP2_METHOD_GRE
338 #define WCCP2_FORWARDING_METHOD_L2 WCCP2_METHOD_L2
339 /* when type=WCCP2_CAPABILITY_ASSIGNMENT_METHOD */
340 #define WCCP2_ASSIGNMENT_METHOD_HASH 0x00000001
341 #define WCCP2_ASSIGNMENT_METHOD_MASK 0x00000002
342 /* when type=WCCP2_CAPABILITY_RETURN_METHOD */
343 #define WCCP2_PACKET_RETURN_METHOD_GRE WCCP2_METHOD_GRE
344 #define WCCP2_PACKET_RETURN_METHOD_L2 WCCP2_METHOD_L2
348 /** \interface WCCPv2_Protocol
349 * 5.7.8 Value Element
351 struct wccp2_value_element_t
{
352 uint32_t source_ip_value
;
353 uint32_t dest_ip_value
;
354 uint16_t source_port_value
;
355 uint16_t dest_port_value
;
357 struct in_addr cache_ip
;
360 /* RECEIVED PACKET STRUCTURE */
362 /** \interface WCCPv2_Protocol
363 * 5.2 'I See You' Message
365 struct wccp2_i_see_you_t
{
369 char data
[WCCP_RESPONSE_SIZE
];
372 static struct wccp2_i_see_you_t wccp2_i_see_you
;
374 /** \interface WCCPv2_Protocol
375 * 5.7.4 Router Assignment Element
377 struct wccp2_router_assign_element_t
{
378 struct in_addr router_address
;
379 uint32_t received_id
;
380 uint32_t change_number
;
383 /* Router identity struct */
385 /** \interface WCCPv2_Protocol
386 * 5.6.3 Router Identity Info Component (partial)
388 struct router_identity_info_t
{
390 struct wccp2_item_header_t header
;
392 struct wccp2_router_id_element_t router_id_element
;
394 struct in_addr router_address
;
395 uint32_t number_caches
;
396 /* dynamic list of cache IP addresses */
399 /* The received packet for a mask assignment is unusual */
401 /** \interface WCCPv2_Protocol
402 * Sect 5.7.7 Mask Element ???
403 * see code below. apparently the supposed IP address at position num1 can be equal to 3.
405 struct cache_mask_info_t
{
412 /** \interface WCCPv2_Protocol
413 * 5.7.3 Assignment Key Element
415 struct assignment_key_t
{
416 struct in_addr master_ip
;
417 uint32_t master_number
;
420 /** \interface WCCPv2_Protocol
421 * 5.6.5 Router View Info Component (first three fields)
423 struct router_view_t
{
424 struct wccp2_item_header_t header
;
425 uint32_t change_number
;
426 struct assignment_key_t assignment_key
;
427 /* dynamic lists of routers and caches elided */
430 /* Lists used to keep track of caches, routers and services */
432 /// \interface WCCPv2_Protocol
433 struct wccp2_cache_list_t
{
435 struct in_addr cache_ip
;
439 struct wccp2_cache_list_t
*next
;
442 /// \interface WCCPv2_Protocol
443 struct wccp2_router_list_t
{
445 struct wccp2_router_id_element_t
*info
;
447 struct in_addr local_ip
;
449 struct in_addr router_sendto_address
;
450 uint32_t member_change
;
453 struct wccp2_cache_list_t cache_list_head
;
455 struct wccp2_router_list_t
*next
;
458 static int wccp2_numrouters
;
460 /// \interface WCCPv2_Protocol
461 struct wccp2_service_list_t
{
463 struct wccp2_service_info_t info
;
464 uint32_t num_routers
;
466 struct wccp2_router_list_t router_list_head
;
470 char *wccp2_identity_info_ptr
;
472 struct wccp2_security_md5_t
*security_info
;
474 struct wccp2_service_info_t
*service_info
;
475 char wccp_packet
[WCCP_RESPONSE_SIZE
];
476 size_t wccp_packet_size
;
478 struct wccp2_service_list_t
*next
;
479 char wccp_password
[WCCP2_PASSWORD_LEN
+ 1]; /* hold the trailing C-string NUL */
480 uint32_t wccp2_security_type
;
483 static struct wccp2_service_list_t
*wccp2_service_list_head
= NULL
;
485 int empty_portlist
[WCCP2_NUMPORTS
] = {0, 0, 0, 0, 0, 0, 0, 0};
487 /* END WCCP V2 PROTOCL TYPES DEFINITION */
489 void wccp2_add_service_list(int service
, int service_id
, int service_priority
,
490 int service_proto
, int service_flags
, int ports
[], int security_type
, char *password
);
491 static void wccp2SortCacheList(struct wccp2_cache_list_t
*head
);
494 * The functions used during startup:
496 * wccp2ConnectionOpen
497 * wccp2ConnectionClose
501 wccp2InitServices(void)
503 debugs(80, 5, "wccp2InitServices: called");
507 wccp2_update_service(struct wccp2_service_list_t
*srv
, int service
,
508 int service_id
, int service_priority
, int service_proto
, int service_flags
,
511 /* XXX check what needs to be wrapped in htons()! */
512 srv
->info
.service
= service
;
513 srv
->info
.service_id
= service_id
;
514 srv
->info
.service_priority
= service_priority
;
515 srv
->info
.service_protocol
= service_proto
;
516 srv
->info
.service_flags
= htonl(service_flags
);
517 srv
->info
.port0
= htons(ports
[0]);
518 srv
->info
.port1
= htons(ports
[1]);
519 srv
->info
.port2
= htons(ports
[2]);
520 srv
->info
.port3
= htons(ports
[3]);
521 srv
->info
.port4
= htons(ports
[4]);
522 srv
->info
.port5
= htons(ports
[5]);
523 srv
->info
.port6
= htons(ports
[6]);
524 srv
->info
.port7
= htons(ports
[7]);
528 wccp2_add_service_list(int service
, int service_id
, int service_priority
,
529 int service_proto
, int service_flags
, int ports
[], int security_type
,
533 struct wccp2_service_list_t
*wccp2_service_list_ptr
;
535 wccp2_service_list_ptr
= (wccp2_service_list_t
*) xcalloc(1, sizeof(struct wccp2_service_list_t
));
537 debugs(80, 5, "wccp2_add_service_list: added service id " << service_id
);
539 /* XXX check what needs to be wrapped in htons()! */
540 wccp2_service_list_ptr
->info
.service_type
= htons(WCCP2_SERVICE_INFO
);
542 wccp2_service_list_ptr
->info
.service_length
= htons(sizeof(struct wccp2_service_info_t
) - 4);
543 wccp2_service_list_ptr
->change_num
= 0;
544 wccp2_update_service(wccp2_service_list_ptr
, service
, service_id
,
545 service_priority
, service_proto
, service_flags
, ports
);
546 wccp2_service_list_ptr
->wccp2_security_type
= security_type
;
547 memset(wccp2_service_list_ptr
->wccp_password
, 0, WCCP2_PASSWORD_LEN
+ 1);
548 strncpy(wccp2_service_list_ptr
->wccp_password
, password
, WCCP2_PASSWORD_LEN
);
549 /* add to linked list - XXX this should use the Squid dlink* routines! */
550 wccp2_service_list_ptr
->next
= wccp2_service_list_head
;
551 wccp2_service_list_head
= wccp2_service_list_ptr
;
554 static struct wccp2_service_list_t
*
555 wccp2_get_service_by_id(int service
, int service_id
) {
557 struct wccp2_service_list_t
*p
;
559 p
= wccp2_service_list_head
;
562 if (p
->info
.service
== service
&& p
->info
.service_id
== service_id
) {
573 * Update the md5 security header, if possible
575 * Returns: 1 if we set it, 0 if not (eg, no security section, or non-md5)
578 wccp2_update_md5_security(char *password
, char *ptr
, char *packet
, int len
)
580 u_int8_t md5_digest
[16];
581 char pwd
[WCCP2_PASSWORD_LEN
];
584 struct wccp2_security_md5_t
*ws
;
586 debugs(80, 5, "wccp2_update_md5_security: called");
588 /* The password field, for the MD5 hash, needs to be 8 bytes and NUL padded. */
589 memset(pwd
, 0, sizeof(pwd
));
590 strncpy(pwd
, password
, sizeof(pwd
));
592 ws
= (struct wccp2_security_md5_t
*) ptr
;
593 assert(ntohs(ws
->security_type
) == WCCP2_SECURITY_INFO
);
594 /* Its the security part */
596 if (ntohl(ws
->security_option
) != WCCP2_MD5_SECURITY
) {
597 debugs(80, 5, "wccp2_update_md5_security: this service ain't md5'ing, abort");
601 /* And now its the MD5 section! */
602 /* According to the draft, the MD5 security hash is the combination of
603 * the 8-octet password (padded w/ NUL bytes) and the entire WCCP packet,
604 * including the WCCP message header. The WCCP security implementation
605 * area should be zero'ed before calculating the MD5 hash.
607 /* XXX eventually we should be able to kill md5_digest and blit it directly in */
608 memset(ws
->security_implementation
, 0, sizeof(ws
->security_implementation
));
612 SquidMD5Update(&M
, pwd
, 8);
614 SquidMD5Update(&M
, packet
, len
);
616 SquidMD5Final(md5_digest
, &M
);
618 memcpy(ws
->security_implementation
, md5_digest
, sizeof(md5_digest
));
626 * Check the given WCCP2 packet against the given password.
630 wccp2_check_security(struct wccp2_service_list_t
*srv
, char *security
, char *packet
, int len
)
633 struct wccp2_security_md5_t
*ws
= (struct wccp2_security_md5_t
*) security
;
634 u_int8_t md5_digest
[16], md5_challenge
[16];
635 char pwd
[WCCP2_PASSWORD_LEN
];
638 /* Make sure the security type matches what we expect */
640 if (ntohl(ws
->security_option
) != srv
->wccp2_security_type
) {
641 debugs(80, 1, "wccp2_check_security: received packet has the wrong security option");
645 if (srv
->wccp2_security_type
== WCCP2_NO_SECURITY
) {
649 if (srv
->wccp2_security_type
!= WCCP2_MD5_SECURITY
) {
650 debugs(80, 1, "wccp2_check_security: invalid security option");
654 /* If execution makes it here then we have an MD5 security */
656 /* The password field, for the MD5 hash, needs to be 8 bytes and NUL padded. */
657 memset(pwd
, 0, sizeof(pwd
));
659 strncpy(pwd
, srv
->wccp_password
, sizeof(pwd
));
661 /* Take a copy of the challenge: we need to NUL it before comparing */
662 memcpy(md5_challenge
, ws
->security_implementation
, 16);
664 memset(ws
->security_implementation
, 0, sizeof(ws
->security_implementation
));
668 SquidMD5Update(&M
, pwd
, 8);
670 SquidMD5Update(&M
, packet
, len
);
672 SquidMD5Final(md5_digest
, &M
);
674 return (memcmp(md5_digest
, md5_challenge
, 16) == 0);
683 uint32_t service_flags
;
685 struct wccp2_service_list_t
*service_list_ptr
;
687 struct wccp2_router_list_t
*router_list_ptr
;
689 struct wccp2_security_md5_t wccp2_security_md5
;
691 debugs(80, 5, "wccp2Init: Called");
693 if (wccp2_connected
== 1)
696 wccp2_numrouters
= 0;
698 /* Calculate the number of routers configured in the config file */
699 for (s
= Config
.Wccp2
.router
; s
; s
= s
->next
) {
700 if (!s
->s
.IsAnyAddr()) {
701 /* Increment the counter */
706 if (wccp2_numrouters
== 0) {
710 /* Initialise the list of services */
713 service_list_ptr
= wccp2_service_list_head
;
715 while (service_list_ptr
!= NULL
) {
716 /* Set up our list pointers */
717 router_list_ptr
= &service_list_ptr
->router_list_head
;
719 /* start the wccp header */
720 wccp2_here_i_am_header
.type
= htonl(WCCP2_HERE_I_AM
);
721 wccp2_here_i_am_header
.version
= htons(WCCP2_VERSION
);
722 wccp2_here_i_am_header
.length
= 0;
723 ptr
= service_list_ptr
->wccp_packet
+ sizeof(wccp2_here_i_am_header
);
725 /* add the security section */
726 /* XXX this is ugly */
728 if (service_list_ptr
->wccp2_security_type
== WCCP2_MD5_SECURITY
) {
729 wccp2_security_md5
.security_option
= htonl(WCCP2_MD5_SECURITY
);
731 wccp2_security_md5
.security_length
= htons(sizeof(struct wccp2_security_md5_t
) - 4);
732 } else if (service_list_ptr
->wccp2_security_type
== WCCP2_NO_SECURITY
) {
733 wccp2_security_md5
.security_option
= htonl(WCCP2_NO_SECURITY
);
734 /* XXX I hate magic length numbers! */
735 wccp2_security_md5
.security_length
= htons(4);
737 fatalf("Bad WCCP2 security type\n");
740 wccp2_here_i_am_header
.length
+= ntohs(wccp2_security_md5
.security_length
) + 4;
741 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
742 wccp2_security_md5
.security_type
= htons(WCCP2_SECURITY_INFO
);
744 service_list_ptr
->security_info
= (struct wccp2_security_md5_t
*) ptr
;
746 if (service_list_ptr
->wccp2_security_type
== WCCP2_MD5_SECURITY
) {
748 xmemcpy(ptr
, &wccp2_security_md5
, sizeof(struct wccp2_security_md5_t
));
750 ptr
+= sizeof(struct wccp2_security_md5_t
);
752 /* assume NONE, and XXX I hate magic length numbers */
753 xmemcpy(ptr
, &wccp2_security_md5
, 8);
757 /* Add the service info section */
759 wccp2_here_i_am_header
.length
+= sizeof(struct wccp2_service_info_t
);
761 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
763 xmemcpy(ptr
, &service_list_ptr
->info
, sizeof(struct wccp2_service_info_t
));
765 service_list_ptr
->service_info
= (struct wccp2_service_info_t
*) ptr
;
767 ptr
+= sizeof(struct wccp2_service_info_t
);
769 /* Add the cache identity section */
771 switch (Config
.Wccp2
.assignment_method
) {
773 case WCCP2_ASSIGNMENT_METHOD_HASH
:
775 wccp2_here_i_am_header
.length
+= sizeof(struct wccp2_identity_info_t
);
776 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
777 wccp2_identity_info
.cache_identity_type
= htons(WCCP2_WC_ID_INFO
);
778 wccp2_identity_info
.cache_identity_length
= htons(sizeof(wccp2_identity_info
.cache_identity
));
779 memset(&wccp2_identity_info
.cache_identity
.addr
, '\0', sizeof(struct in_addr
));
780 memset(&wccp2_identity_info
.cache_identity
.hash_revision
, '\0', sizeof(wccp2_identity_info
.cache_identity
.hash_revision
));
781 memset(&wccp2_identity_info
.cache_identity
.bits
, '\0', sizeof(wccp2_identity_info
.cache_identity
.bits
));
782 memset(&wccp2_identity_info
.cache_identity
.buckets
, '\0', sizeof(wccp2_identity_info
.cache_identity
.buckets
));
783 wccp2_identity_info
.cache_identity
.weight
= htons(Config
.Wccp2
.weight
);
784 memset(&wccp2_identity_info
.cache_identity
.status
, '\0', sizeof(wccp2_identity_info
.cache_identity
.status
));
786 xmemcpy(ptr
, &wccp2_identity_info
, sizeof(struct wccp2_identity_info_t
));
787 service_list_ptr
->wccp2_identity_info_ptr
= ptr
;
789 ptr
+= sizeof(struct wccp2_identity_info_t
);
792 case WCCP2_ASSIGNMENT_METHOD_MASK
:
794 wccp2_here_i_am_header
.length
+= sizeof(struct wccp2_mask_identity_info_t
);
795 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
796 wccp2_mask_identity_info
.cache_identity_type
= htons(WCCP2_WC_ID_INFO
);
797 wccp2_mask_identity_info
.cache_identity_length
= htons(sizeof(wccp2_mask_identity_info
.cache_identity
));
798 memset(&wccp2_mask_identity_info
.cache_identity
.addr
, '\0', sizeof(struct in_addr
));
799 wccp2_mask_identity_info
.cache_identity
.bits
= WCCP2_MASK_ASSIGNMENT_DATA
; // num1 = htonl(2);
800 wccp2_mask_identity_info
.cache_identity
.mask_element_count
= htonl(1);
801 service_flags
= ntohl(service_list_ptr
->service_info
->service_flags
);
803 memset(&wccp2_mask_identity_info
.cache_identity
.mask
, 0, sizeof(struct wccp2_mask_element_t
));
805 if ((service_flags
& WCCP2_SERVICE_SRC_IP_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_IP_ALT_HASH
)) {
806 wccp2_mask_identity_info
.cache_identity
.mask
.source_ip_mask
= htonl(0x00001741);
807 } 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
)) {
808 wccp2_mask_identity_info
.cache_identity
.mask
.dest_ip_mask
= htonl(0x00001741);
809 } else if ((service_flags
& WCCP2_SERVICE_SRC_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_PORT_ALT_HASH
)) {
810 wccp2_mask_identity_info
.cache_identity
.mask
.source_port_mask
= htons(0x1741);
811 } else if ((service_flags
& WCCP2_SERVICE_DST_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_DST_PORT_ALT_HASH
)) {
812 wccp2_mask_identity_info
.cache_identity
.mask
.dest_port_mask
= htons(0x1741);
814 fatalf("Unknown service hash method\n");
817 wccp2_mask_identity_info
.cache_identity
.weight
= 0;
818 wccp2_mask_identity_info
.cache_identity
.status
= 0;
820 xmemcpy(ptr
, &wccp2_mask_identity_info
, sizeof(struct wccp2_mask_identity_info_t
));
821 service_list_ptr
->wccp2_identity_info_ptr
= ptr
;
823 ptr
+= sizeof(struct wccp2_mask_identity_info_t
);
827 fatalf("Unknown Wccp2 assignment method\n");
830 /* Add the cache view section */
831 wccp2_here_i_am_header
.length
+= sizeof(wccp2_cache_view_header
);
833 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
835 wccp2_cache_view_header
.cache_view_type
= htons(WCCP2_WC_VIEW_INFO
);
837 wccp2_cache_view_header
.cache_view_length
= htons(sizeof(wccp2_cache_view_header
) - 4 +
838 sizeof(wccp2_cache_view_info
) + (wccp2_numrouters
* sizeof(wccp2_router_id_element
)));
840 wccp2_cache_view_header
.cache_view_version
= htonl(1);
842 xmemcpy(ptr
, &wccp2_cache_view_header
, sizeof(wccp2_cache_view_header
));
844 ptr
+= sizeof(wccp2_cache_view_header
);
846 /* Add the number of routers to the packet */
847 wccp2_here_i_am_header
.length
+= sizeof(service_list_ptr
->num_routers
);
849 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
851 service_list_ptr
->num_routers
= htonl(wccp2_numrouters
);
853 xmemcpy(ptr
, &service_list_ptr
->num_routers
, sizeof(service_list_ptr
->num_routers
));
855 ptr
+= sizeof(service_list_ptr
->num_routers
);
857 /* Add each router. Keep this functionality here to make sure the received_id can be updated in the packet */
858 for (s
= Config
.Wccp2
.router
; s
; s
= s
->next
) {
859 if (!s
->s
.IsAnyAddr()) {
861 wccp2_here_i_am_header
.length
+= sizeof(struct wccp2_router_id_element_t
);
862 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
864 /* Add a pointer to the router list for this router */
866 router_list_ptr
->info
= (struct wccp2_router_id_element_t
*) ptr
;
867 s
->s
.GetInAddr(router_list_ptr
->info
->router_address
);
868 router_list_ptr
->info
->received_id
= htonl(0);
869 s
->s
.GetInAddr(router_list_ptr
->router_sendto_address
);
870 router_list_ptr
->member_change
= htonl(0);
872 /* Build the next struct */
874 router_list_ptr
->next
= (wccp2_router_list_t
*) xcalloc(1, sizeof(struct wccp2_router_list_t
));
876 /* update the pointer */
877 router_list_ptr
= router_list_ptr
->next
;
878 router_list_ptr
->next
= NULL
;
880 /* no need to copy memory - we've just set the values directly in the packet above */
882 ptr
+= sizeof(struct wccp2_router_id_element_t
);
886 /* Add the number of caches (0) */
887 wccp2_here_i_am_header
.length
+= sizeof(wccp2_cache_view_info
.num_caches
);
889 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
891 wccp2_cache_view_info
.num_caches
= htonl(0);
893 xmemcpy(ptr
, &wccp2_cache_view_info
.num_caches
, sizeof(wccp2_cache_view_info
.num_caches
));
895 ptr
+= sizeof(wccp2_cache_view_info
.num_caches
);
897 /* Add the extra capability header */
898 wccp2_here_i_am_header
.length
+= sizeof(wccp2_capability_info_header
);
900 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
902 wccp2_capability_info_header
.capability_info_type
= htons(WCCP2_CAPABILITY_INFO
);
904 wccp2_capability_info_header
.capability_info_length
= htons(3 * sizeof(wccp2_capability_element
));
906 xmemcpy(ptr
, &wccp2_capability_info_header
, sizeof(wccp2_capability_info_header
));
908 ptr
+= sizeof(wccp2_capability_info_header
);
910 /* Add the forwarding method */
911 wccp2_here_i_am_header
.length
+= sizeof(wccp2_capability_element
);
913 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
915 wccp2_capability_element
.capability_type
= htons(WCCP2_CAPABILITY_FORWARDING_METHOD
);
917 wccp2_capability_element
.capability_length
= htons(sizeof(wccp2_capability_element
.capability_value
));
919 wccp2_capability_element
.capability_value
= htonl(Config
.Wccp2
.forwarding_method
);
921 xmemcpy(ptr
, &wccp2_capability_element
, sizeof(wccp2_capability_element
));
923 ptr
+= sizeof(wccp2_capability_element
);
925 /* Add the assignment method */
926 wccp2_here_i_am_header
.length
+= sizeof(wccp2_capability_element
);
928 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
930 wccp2_capability_element
.capability_type
= htons(WCCP2_CAPABILITY_ASSIGNMENT_METHOD
);
932 wccp2_capability_element
.capability_length
= htons(sizeof(wccp2_capability_element
.capability_value
));
934 wccp2_capability_element
.capability_value
= htonl(Config
.Wccp2
.assignment_method
);
936 xmemcpy(ptr
, &wccp2_capability_element
, sizeof(wccp2_capability_element
));
938 ptr
+= sizeof(wccp2_capability_element
);
940 /* Add the return method */
941 wccp2_here_i_am_header
.length
+= sizeof(wccp2_capability_element
);
943 assert(wccp2_here_i_am_header
.length
<= WCCP_RESPONSE_SIZE
);
945 wccp2_capability_element
.capability_type
= htons(WCCP2_CAPABILITY_RETURN_METHOD
);
947 wccp2_capability_element
.capability_length
= htons(sizeof(wccp2_capability_element
.capability_value
));
949 wccp2_capability_element
.capability_value
= htonl(Config
.Wccp2
.return_method
);
951 xmemcpy(ptr
, &wccp2_capability_element
, sizeof(wccp2_capability_element
));
953 ptr
+= sizeof(wccp2_capability_element
);
955 /* Finally, fix the total length to network order, and copy to the appropriate memory blob */
956 wccp2_here_i_am_header
.length
= htons(wccp2_here_i_am_header
.length
);
958 memcpy(&service_list_ptr
->wccp_packet
, &wccp2_here_i_am_header
, sizeof(wccp2_here_i_am_header
));
960 service_list_ptr
->wccp_packet_size
= ntohs(wccp2_here_i_am_header
.length
) + sizeof(wccp2_here_i_am_header
);
962 /* Add the event if everything initialised correctly */
963 debugs(80,3,"wccp2Init: scheduled 'HERE_I_AM' message to " << wccp2_numrouters
<< "routers.");
964 if (wccp2_numrouters
) {
965 if (!eventFind(wccp2HereIam
, NULL
)) {
966 eventAdd("wccp2HereIam", wccp2HereIam
, NULL
, 1, 1);
968 debugs(80,3,"wccp2Init: skip duplicate 'HERE_I_AM'.");
971 service_list_ptr
= service_list_ptr
->next
;
976 wccp2ConnectionOpen(void)
978 struct sockaddr_in router
, local
, null
;
979 socklen_t local_len
, router_len
;
981 struct wccp2_service_list_t
*service_list_ptr
;
983 struct wccp2_router_list_t
*router_list_ptr
;
985 debugs(80, 5, "wccp2ConnectionOpen: Called");
987 if (wccp2_numrouters
== 0 || !wccp2_service_list_head
) {
988 debugs(80, 2, "WCCPv2 Disabled.");
992 if ( !Config
.Wccp2
.address
.SetIPv4() ) {
993 debugs(80, 0, "WCCPv2 Disabled. " << Config
.Wccp2
.address
<< " is not an IPv4 address.");
997 Config
.Wccp2
.address
.SetPort(WCCP_PORT
);
998 theWccp2Connection
= comm_open_listener(SOCK_DGRAM
,
1000 Config
.Wccp2
.address
,
1004 if (theWccp2Connection
< 0)
1005 fatal("Cannot open WCCP Port");
1007 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
1009 int i
= IP_PMTUDISC_DONT
;
1010 setsockopt(theWccp2Connection
, SOL_IP
, IP_MTU_DISCOVER
, &i
, sizeof i
);
1014 commSetSelect(theWccp2Connection
,
1020 debugs(80, 1, "Accepting WCCPv2 messages on port " << WCCP_PORT
<< ", FD " << theWccp2Connection
<< ".");
1021 debugs(80, 1, "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, 1, "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 commSetSelect(sock
, COMM_SELECT_READ
, wccp2HandleUdp
, NULL
, 0);
1181 /* FIXME INET6 : drop conversion boundary */
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, 1, "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, 1, "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, 1, "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, 1, "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, 1, "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, 1, "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
:
1278 debugs(80, 1, "Unknown record type in WCCPv2 Packet (" << ntohs(header
->type
) << ").");
1281 offset
+= sizeof(struct wccp2_item_header_t
);
1282 offset
+= ntohs(header
->length
);
1284 if (offset
> data_length
) {
1285 debugs(80, 1, "Error: WCCPv2 packet tried to tell us there is data beyond the end of the packet");
1290 if ((security_info
== NULL
) || (service_info
== NULL
) || (router_identity_info
== NULL
) || (router_view_header
== NULL
)) {
1291 debugs(80, 1, "Incomplete WCCPv2 Packet");
1295 debugs(80, 5, "Complete packet received");
1297 /* Check that the service in the packet is configured on this router */
1298 service_list_ptr
= wccp2_service_list_head
;
1300 while (service_list_ptr
!= NULL
) {
1301 if (service_info
->service_id
== service_list_ptr
->service_info
->service_id
) {
1305 service_list_ptr
= service_list_ptr
->next
;
1308 if (service_list_ptr
== NULL
) {
1309 debugs(80, 1, "WCCPv2 Unknown service received from router (" << service_info
->service_id
<< ")");
1313 if (ntohl(security_info
->security_option
) != ntohl(service_list_ptr
->security_info
->security_option
)) {
1314 debugs(80, 1, "Invalid security option in WCCPv2 Packet (" << ntohl(security_info
->security_option
) << " vs " << ntohl(service_list_ptr
->security_info
->security_option
) << ").");
1318 if (!wccp2_check_security(service_list_ptr
, (char *) security_info
, (char *) &wccp2_i_see_you
, len
)) {
1319 debugs(80, 1, "Received WCCPv2 Packet failed authentication");
1323 /* Check that the router address is configured on this router */
1324 for (router_list_ptr
= &service_list_ptr
->router_list_head
; router_list_ptr
->next
!= NULL
; router_list_ptr
= router_list_ptr
->next
) {
1325 if (router_list_ptr
->router_sendto_address
.s_addr
== from
.sin_addr
.s_addr
)
1329 if (router_list_ptr
->next
== NULL
) {
1330 debugs(80, 1, "WCCPv2 Packet received from unknown router");
1334 /* Set the router id */
1335 router_list_ptr
->info
->router_address
= router_identity_info
->router_id_element
.router_address
;
1337 /* Increment the received id in the packet */
1338 if (ntohl(router_list_ptr
->info
->received_id
) != ntohl(router_identity_info
->router_id_element
.received_id
)) {
1339 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
) << ".");
1340 router_list_ptr
->info
->received_id
= router_identity_info
->router_id_element
.received_id
;
1343 /* TODO: check return/forwarding methods */
1344 if (router_capability_header
== NULL
) {
1345 if ((Config
.Wccp2
.return_method
!= WCCP2_PACKET_RETURN_METHOD_GRE
) || (Config
.Wccp2
.forwarding_method
!= WCCP2_FORWARDING_METHOD_GRE
)) {
1346 debugs(80, 1, "wccp2HandleUdp: fatal error - A WCCP router does not support the forwarding method specified, only GRE supported");
1347 wccp2ConnectionClose();
1352 char *end
= ((char *) router_capability_header
) + sizeof(*router_capability_header
) + ntohs(router_capability_header
->capability_info_length
) - sizeof(struct wccp2_capability_info_header_t
);
1354 router_capability_element
= (struct wccp2_capability_element_t
*) (((char *) router_capability_header
) + sizeof(*router_capability_header
));
1356 while ((char *) router_capability_element
<= end
) {
1358 switch (ntohs(router_capability_element
->capability_type
)) {
1360 case WCCP2_CAPABILITY_FORWARDING_METHOD
:
1362 if (!(ntohl(router_capability_element
->capability_value
) & Config
.Wccp2
.forwarding_method
)) {
1363 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
);
1364 wccp2ConnectionClose();
1370 case WCCP2_CAPABILITY_ASSIGNMENT_METHOD
:
1372 if (!(ntohl(router_capability_element
->capability_value
) & Config
.Wccp2
.assignment_method
)) {
1373 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
);
1374 wccp2ConnectionClose();
1380 case WCCP2_CAPABILITY_RETURN_METHOD
:
1382 if (!(ntohl(router_capability_element
->capability_value
) & Config
.Wccp2
.return_method
)) {
1383 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
);
1384 wccp2ConnectionClose();
1391 debugs(80, 1, "Unknown capability type in WCCPv2 Packet (" << ntohs(router_capability_element
->capability_type
) << ").");
1394 router_capability_element
= (struct wccp2_capability_element_t
*) (((char *) router_capability_element
) + sizeof(struct wccp2_item_header_t
) + ntohs(router_capability_element
->capability_length
));
1398 debugs(80, 5, "Cleaning out cache list");
1399 /* clean out the old cache list */
1401 for (cache_list_ptr
= &router_list_ptr
->cache_list_head
; cache_list_ptr
; cache_list_ptr
= cache_list_ptr_next
) {
1402 cache_list_ptr_next
= cache_list_ptr
->next
;
1404 if (cache_list_ptr
!= &router_list_ptr
->cache_list_head
) {
1405 xfree(cache_list_ptr
);
1409 router_list_ptr
->num_caches
= htonl(0);
1412 /* Check to see if we're the master cache and update the cache list */
1414 service_list_ptr
->lowest_ip
= 1;
1415 cache_list_ptr
= &router_list_ptr
->cache_list_head
;
1417 /* to find the list of caches, we start at the end of the router view header */
1419 ptr
= (char *) (router_view_header
) + sizeof(struct router_view_t
);
1421 /* Then we read the number of routers */
1422 memcpy(&tmp
, ptr
, sizeof(tmp
));
1424 /* skip the number plus all the ip's */
1426 ptr
+= sizeof(tmp
) + (ntohl(tmp
) * sizeof(struct in_addr
));
1428 /* Then read the number of caches */
1429 memcpy(&tmp
, ptr
, sizeof(tmp
));
1432 if (ntohl(tmp
) != 0) {
1433 /* search through the list of received-from ip addresses */
1435 for (num_caches
= 0; num_caches
< (int) ntohl(tmp
); num_caches
++) {
1436 /* Get a copy of the ip */
1437 memset(&cache_address
, 0, sizeof(cache_address
)); // Make GCC happy
1439 switch (Config
.Wccp2
.assignment_method
) {
1441 case WCCP2_ASSIGNMENT_METHOD_HASH
:
1443 cache_identity
= (struct wccp2_cache_identity_info_t
*) ptr
;
1445 ptr
+= sizeof(struct wccp2_cache_identity_info_t
);
1447 memcpy(&cache_address
, &cache_identity
->addr
, sizeof(struct in_addr
));
1449 cache_list_ptr
->weight
= ntohs(cache_identity
->weight
);
1452 case WCCP2_ASSIGNMENT_METHOD_MASK
:
1454 cache_mask_info
= (struct cache_mask_info_t
*) ptr
;
1456 /* The mask assignment has an undocumented variable length entry here */
1458 if (ntohl(cache_mask_info
->num1
) == 3) {
1460 cache_mask_identity
= (struct wccp2_cache_mask_identity_info_t
*) ptr
;
1462 ptr
+= sizeof(struct wccp2_cache_mask_identity_info_t
);
1464 memcpy(&cache_address
, &cache_mask_identity
->addr
, sizeof(struct in_addr
));
1467 ptr
+= sizeof(struct cache_mask_info_t
);
1469 memcpy(&cache_address
, &cache_mask_info
->addr
, sizeof(struct in_addr
));
1472 cache_list_ptr
->weight
= 0;
1476 fatalf("Unknown Wccp2 assignment method\n");
1479 /* Update the cache list */
1480 cache_list_ptr
->cache_ip
= cache_address
;
1482 cache_list_ptr
->next
= (wccp2_cache_list_t
*) xcalloc(1, sizeof(struct wccp2_cache_list_t
));
1484 cache_list_ptr
= cache_list_ptr
->next
;
1486 cache_list_ptr
->next
= NULL
;
1488 debugs (80, 5, "checking cache list: (" << std::hex
<< cache_address
.s_addr
<< ":" << router_list_ptr
->local_ip
.s_addr
<< ")");
1490 /* Check to see if it's the master, or us */
1492 if (cache_address
.s_addr
== router_list_ptr
->local_ip
.s_addr
) {
1496 if (cache_address
.s_addr
< router_list_ptr
->local_ip
.s_addr
) {
1497 service_list_ptr
->lowest_ip
= 0;
1501 debugs(80, 5, "Adding ourselves as the only cache");
1503 /* Update the cache list */
1504 cache_list_ptr
->cache_ip
= router_list_ptr
->local_ip
;
1506 cache_list_ptr
->next
= (wccp2_cache_list_t
*) xcalloc(1, sizeof(struct wccp2_cache_list_t
));
1507 cache_list_ptr
= cache_list_ptr
->next
;
1508 cache_list_ptr
->next
= NULL
;
1510 service_list_ptr
->lowest_ip
= 1;
1515 wccp2SortCacheList(&router_list_ptr
->cache_list_head
);
1517 router_list_ptr
->num_caches
= htonl(num_caches
);
1519 if ((found
== 1) && (service_list_ptr
->lowest_ip
== 1)) {
1520 if (ntohl(router_view_header
->change_number
) != router_list_ptr
->member_change
) {
1521 debugs(80, 4, "Change detected - queueing up new assignment");
1522 router_list_ptr
->member_change
= ntohl(router_view_header
->change_number
);
1523 eventDelete(wccp2AssignBuckets
, NULL
);
1524 eventAdd("wccp2AssignBuckets", wccp2AssignBuckets
, NULL
, 15.0, 1);
1526 debugs(80, 5, "Change not detected (" << ntohl(router_view_header
->change_number
) << " = " << router_list_ptr
->member_change
<< ")");
1529 eventDelete(wccp2AssignBuckets
, NULL
);
1530 debugs(80, 5, "I am not the lowest ip cache - not assigning buckets");
1535 wccp2HereIam(void *voidnotused
)
1538 struct wccp2_service_list_t
*service_list_ptr
;
1540 struct wccp2_router_list_t
*router_list_ptr
;
1542 struct wccp2_identity_info_t
*wccp2_identity_info_ptr
;
1544 struct wccp2_mask_identity_info_t
*wccp2_mask_identity_info_ptr
;
1548 debugs(80, 6, "wccp2HereIam: Called");
1550 if (wccp2_connected
== 0) {
1551 debugs(80, 1, "wccp2HereIam: wccp2 socket closed. Shutting down WCCP2");
1555 /* Wait if store dirs are rebuilding */
1556 if (StoreController::store_dirs_rebuilding
&& Config
.Wccp2
.rebuildwait
) {
1557 eventAdd("wccp2HereIam", wccp2HereIam
, NULL
, 1.0, 1);
1561 router
.SetPort(WCCP_PORT
);
1563 /* for each router on each service send a packet */
1564 service_list_ptr
= wccp2_service_list_head
;
1566 while (service_list_ptr
!= NULL
) {
1567 debugs(80, 5, "wccp2HereIam: sending to service id " << service_list_ptr
->info
.service_id
);
1569 for (router_list_ptr
= &service_list_ptr
->router_list_head
; router_list_ptr
->next
!= NULL
; router_list_ptr
= router_list_ptr
->next
) {
1570 router
= router_list_ptr
->router_sendto_address
;
1572 /* Set the cache id (ip) */
1574 switch (Config
.Wccp2
.assignment_method
) {
1576 case WCCP2_ASSIGNMENT_METHOD_HASH
:
1578 wccp2_identity_info_ptr
= (struct wccp2_identity_info_t
*) service_list_ptr
->wccp2_identity_info_ptr
;
1579 wccp2_identity_info_ptr
->cache_identity
.addr
= router_list_ptr
->local_ip
;
1582 case WCCP2_ASSIGNMENT_METHOD_MASK
:
1584 wccp2_mask_identity_info_ptr
= (struct wccp2_mask_identity_info_t
*) service_list_ptr
->wccp2_identity_info_ptr
;
1585 wccp2_mask_identity_info_ptr
->cache_identity
.addr
= router_list_ptr
->local_ip
;
1589 fatalf("Unknown Wccp2 assignment method\n");
1592 /* Security update, if needed */
1594 if (service_list_ptr
->wccp2_security_type
== WCCP2_MD5_SECURITY
) {
1595 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
);
1598 debugs(80, 3, "Sending HereIam packet size " << service_list_ptr
->wccp_packet_size
);
1599 /* Send the packet */
1601 if (wccp2_numrouters
> 1) {
1602 comm_udp_sendto(theWccp2Connection
,
1604 &service_list_ptr
->wccp_packet
,
1605 service_list_ptr
->wccp_packet_size
);
1607 send(theWccp2Connection
,
1608 &service_list_ptr
->wccp_packet
,
1609 service_list_ptr
->wccp_packet_size
,
1614 service_list_ptr
= service_list_ptr
->next
;
1617 eventAdd("wccp2HereIam", wccp2HereIam
, NULL
, 10.0, 1);
1621 wccp2AssignBuckets(void *voidnotused
)
1624 struct wccp2_service_list_t
*service_list_ptr
;
1626 struct wccp2_router_list_t
*router_list_ptr
;
1628 struct wccp2_cache_list_t
*cache_list_ptr
;
1629 char wccp_packet
[WCCP_RESPONSE_SIZE
];
1630 short int offset
, saved_offset
, assignment_offset
, alt_assignment_offset
;
1632 struct sockaddr_in router
;
1635 uint32_t service_flags
;
1636 u_short port
= WCCP_PORT
;
1638 /* Packet segments */
1640 struct wccp2_message_header_t
*main_header
;
1642 struct wccp2_security_md5_t
*security
= NULL
;
1643 /* service from service struct */
1645 struct wccp2_item_header_t
*assignment_header
;
1647 struct wccp2_item_header_t
*alt_assignment_type_header
= NULL
;
1649 struct assignment_key_t
*assignment_key
;
1650 /* number of routers */
1652 struct wccp2_router_assign_element_t
*router_assign
;
1653 /* number of caches */
1655 struct in_addr
*cache_address
;
1656 /* Alternative assignement mask/values */
1659 struct wccp2_mask_element_t
*mask_element
;
1661 struct wccp2_value_element_t
*value_element
;
1662 int valuecounter
, value
;
1665 assignment_offset
= alt_assignment_offset
= 0;
1667 router_len
= sizeof(router
);
1668 memset(&router
, '\0', router_len
);
1669 router
.sin_family
= AF_INET
;
1670 router
.sin_port
= htons(port
);
1672 /* Start main header - fill in length later */
1675 main_header
= (struct wccp2_message_header_t
*) &wccp_packet
[offset
];
1676 main_header
->type
= htonl(WCCP2_REDIRECT_ASSIGN
);
1677 main_header
->version
= htons(WCCP2_VERSION
);
1679 debugs(80, 2, "Running wccp2AssignBuckets");
1680 service_list_ptr
= wccp2_service_list_head
;
1682 while (service_list_ptr
!= NULL
) {
1683 /* If we're not the lowest, we don't need to worry */
1685 if (service_list_ptr
->lowest_ip
== 0) {
1687 service_list_ptr
= service_list_ptr
->next
;
1691 /* reset the offset */
1693 offset
= sizeof(struct wccp2_message_header_t
);
1695 /* build packet header from hereIam packet */
1697 /* XXX this should be made more generic! */
1698 /* XXX and I hate magic numbers! */
1699 switch (service_list_ptr
->wccp2_security_type
) {
1701 case WCCP2_NO_SECURITY
:
1703 security
= (struct wccp2_security_md5_t
*) &wccp_packet
[offset
];
1704 memcpy(security
, service_list_ptr
->security_info
, 8);
1708 case WCCP2_MD5_SECURITY
:
1710 security
= (struct wccp2_security_md5_t
*) &wccp_packet
[offset
];
1712 memcpy(security
, service_list_ptr
->security_info
, sizeof(struct wccp2_security_md5_t
));
1714 offset
+= sizeof(struct wccp2_security_md5_t
);
1718 fatalf("Unknown Wccp2 security type\n");
1723 memcpy(&wccp_packet
[offset
], service_list_ptr
->service_info
, sizeof(struct wccp2_service_info_t
));
1725 offset
+= sizeof(struct wccp2_service_info_t
);
1727 /* assignment header - fill in length later */
1729 assignment_header
= (struct wccp2_item_header_t
*) &wccp_packet
[offset
];
1731 switch (Config
.Wccp2
.assignment_method
) {
1733 case WCCP2_ASSIGNMENT_METHOD_HASH
:
1734 assignment_header
->type
= htons(WCCP2_REDIRECT_ASSIGNMENT
);
1736 offset
+= sizeof(struct wccp2_item_header_t
);
1737 assignment_offset
= offset
;
1740 case WCCP2_ASSIGNMENT_METHOD_MASK
:
1741 assignment_header
->type
= htons(WCCP2_ALT_ASSIGNMENT
);
1743 offset
+= sizeof(struct wccp2_item_header_t
);
1744 assignment_offset
= offset
;
1746 /* The alternative assignment has an extra header, fill in length later */
1748 alt_assignment_type_header
= (struct wccp2_item_header_t
*) &wccp_packet
[offset
];
1749 alt_assignment_type_header
->type
= htons(WCCP2_MASK_ASSIGNMENT
);
1751 offset
+= sizeof(struct wccp2_item_header_t
);
1752 alt_assignment_offset
= offset
;
1757 fatalf("Unknown Wccp2 assignment method\n");
1760 /* Assignment key - fill in master ip later */
1762 assignment_key
= (struct assignment_key_t
*) &wccp_packet
[offset
];
1764 assignment_key
->master_number
= htonl(++service_list_ptr
->change_num
);
1766 offset
+= sizeof(struct assignment_key_t
);
1768 /* Number of routers */
1769 xmemcpy(&wccp_packet
[offset
], &service_list_ptr
->num_routers
, sizeof(service_list_ptr
->num_routers
));
1771 offset
+= sizeof(service_list_ptr
->num_routers
);
1773 for (router_list_ptr
= &service_list_ptr
->router_list_head
; router_list_ptr
->next
!= NULL
; router_list_ptr
= router_list_ptr
->next
) {
1777 router_assign
= (struct wccp2_router_assign_element_t
*) &wccp_packet
[offset
];
1778 router_assign
->router_address
= router_list_ptr
->info
->router_address
;
1779 router_assign
->received_id
= router_list_ptr
->info
->received_id
;
1780 router_assign
->change_number
= htonl(router_list_ptr
->member_change
);
1782 offset
+= sizeof(struct wccp2_router_assign_element_t
);
1785 saved_offset
= offset
;
1787 for (router_list_ptr
= &service_list_ptr
->router_list_head
; router_list_ptr
->next
!= NULL
; router_list_ptr
= router_list_ptr
->next
) {
1788 unsigned long *weight
= (unsigned long *)xcalloc(sizeof(*weight
), ntohl(router_list_ptr
->num_caches
));
1789 unsigned long total_weight
= 0;
1790 int num_caches
= ntohl(router_list_ptr
->num_caches
);
1792 offset
= saved_offset
;
1794 switch (Config
.Wccp2
.assignment_method
) {
1796 case WCCP2_ASSIGNMENT_METHOD_HASH
:
1797 /* Number of caches */
1798 xmemcpy(&wccp_packet
[offset
], &router_list_ptr
->num_caches
, sizeof(router_list_ptr
->num_caches
));
1799 offset
+= sizeof(router_list_ptr
->num_caches
);
1804 for (cache
= 0, cache_list_ptr
= &router_list_ptr
->cache_list_head
; cache_list_ptr
->next
; cache_list_ptr
= cache_list_ptr
->next
, cache
++) {
1807 cache_address
= (struct in_addr
*) &wccp_packet
[offset
];
1809 xmemcpy(cache_address
, &cache_list_ptr
->cache_ip
, sizeof(struct in_addr
));
1810 total_weight
+= cache_list_ptr
->weight
<< 12;
1811 weight
[cache
] = cache_list_ptr
->weight
<< 12;
1813 offset
+= sizeof(struct in_addr
);
1818 buckets
= (char *) &wccp_packet
[offset
];
1820 memset(buckets
, '\0', WCCP_BUCKETS
);
1822 if (num_caches
!= 0) {
1823 if (total_weight
== 0) {
1824 for (bucket_counter
= 0; bucket_counter
< WCCP_BUCKETS
; bucket_counter
++) {
1825 buckets
[bucket_counter
] = (char) (bucket_counter
% num_caches
);
1828 unsigned long *assigned
= (unsigned long *)xcalloc(sizeof(*assigned
), num_caches
);
1829 unsigned long done
= 0;
1831 unsigned long per_bucket
= total_weight
/ WCCP_BUCKETS
;
1833 for (bucket_counter
= 0; bucket_counter
< WCCP_BUCKETS
; bucket_counter
++) {
1837 for (n
= num_caches
; n
; n
--) {
1840 if (cache
>= num_caches
)
1843 if (!weight
[cache
]) {
1848 if (assigned
[cache
] <= done
)
1852 buckets
[bucket_counter
] = (char) cache
;
1853 step
= per_bucket
* total_weight
/ weight
[cache
];
1854 assigned
[cache
] += step
;
1858 safe_free(assigned
);
1862 offset
+= (WCCP_BUCKETS
* sizeof(char));
1866 case WCCP2_ASSIGNMENT_METHOD_MASK
:
1867 num_maskval
= htonl(1);
1868 xmemcpy(&wccp_packet
[offset
], &num_maskval
, sizeof(int));
1869 offset
+= sizeof(int);
1871 mask_element
= (struct wccp2_mask_element_t
*) &wccp_packet
[offset
];
1872 service_flags
= ntohl(service_list_ptr
->service_info
->service_flags
);
1874 if ((service_flags
& WCCP2_SERVICE_SRC_IP_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_IP_ALT_HASH
)) {
1875 mask_element
->source_ip_mask
= htonl(0x00001741);
1876 mask_element
->dest_ip_mask
= 0;
1877 mask_element
->source_port_mask
= 0;
1878 mask_element
->dest_port_mask
= 0;
1879 } 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
)) {
1880 mask_element
->source_ip_mask
= 0;
1881 mask_element
->dest_ip_mask
= htonl(0x00001741);
1882 mask_element
->source_port_mask
= 0;
1883 mask_element
->dest_port_mask
= 0;
1884 } else if ((service_flags
& WCCP2_SERVICE_SRC_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_PORT_ALT_HASH
)) {
1885 mask_element
->source_ip_mask
= 0;
1886 mask_element
->dest_ip_mask
= 0;
1887 mask_element
->source_port_mask
= htons(0x1741);
1888 mask_element
->dest_port_mask
= 0;
1889 } else if ((service_flags
& WCCP2_SERVICE_DST_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_DST_PORT_ALT_HASH
)) {
1890 mask_element
->source_ip_mask
= 0;
1891 mask_element
->dest_ip_mask
= 0;
1892 mask_element
->source_port_mask
= 0;
1893 mask_element
->dest_port_mask
= htons(0x1741);
1895 fatalf("Unknown service hash method\n");
1898 mask_element
->number_values
= htonl(64);
1900 offset
+= sizeof(struct wccp2_mask_element_t
);
1902 cache_list_ptr
= &router_list_ptr
->cache_list_head
;
1905 for (valuecounter
= 0; valuecounter
< 64; valuecounter
++) {
1907 value_element
= (struct wccp2_value_element_t
*) &wccp_packet
[offset
];
1909 /* Update the value according the the "correct" formula */
1911 for (; (value
& 0x1741) != value
; value
++) {
1912 assert(value
<= 0x1741);
1915 if ((service_flags
& WCCP2_SERVICE_SRC_IP_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_IP_ALT_HASH
)) {
1916 value_element
->source_ip_value
= htonl(value
);
1917 value_element
->dest_ip_value
= 0;
1918 value_element
->source_port_value
= 0;
1919 value_element
->dest_port_value
= 0;
1920 } 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
)) {
1921 value_element
->source_ip_value
= 0;
1922 value_element
->dest_ip_value
= htonl(value
);
1923 value_element
->source_port_value
= 0;
1924 value_element
->dest_port_value
= 0;
1925 } else if ((service_flags
& WCCP2_SERVICE_SRC_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_SRC_PORT_ALT_HASH
)) {
1926 value_element
->source_ip_value
= 0;
1927 value_element
->dest_ip_value
= 0;
1928 value_element
->source_port_value
= htons(value
);
1929 value_element
->dest_port_value
= 0;
1930 } else if ((service_flags
& WCCP2_SERVICE_DST_PORT_HASH
) || (service_flags
& WCCP2_SERVICE_DST_PORT_ALT_HASH
)) {
1931 value_element
->source_ip_value
= 0;
1932 value_element
->dest_ip_value
= 0;
1933 value_element
->source_port_value
= 0;
1934 value_element
->dest_port_value
= htons(value
);
1936 fatalf("Unknown service hash method\n");
1939 value_element
->cache_ip
= cache_list_ptr
->cache_ip
;
1941 offset
+= sizeof(struct wccp2_value_element_t
);
1944 /* Assign the next value to the next cache */
1946 if ((cache_list_ptr
->next
) && (cache_list_ptr
->next
->next
))
1947 cache_list_ptr
= cache_list_ptr
->next
;
1949 cache_list_ptr
= &router_list_ptr
->cache_list_head
;
1952 /* Fill in length */
1953 alt_assignment_type_header
->length
= htons(offset
- alt_assignment_offset
);
1958 fatalf("Unknown Wccp2 assignment method\n");
1961 /* Fill in length */
1963 assignment_header
->length
= htons(offset
- assignment_offset
);
1965 /* Fill in assignment key */
1966 assignment_key
->master_ip
= router_list_ptr
->local_ip
;
1970 main_header
->length
= htons(offset
- sizeof(struct wccp2_message_header_t
));
1972 /* set the destination address */
1973 router
.sin_addr
= router_list_ptr
->router_sendto_address
;
1975 /* Security update, if needed */
1977 if (service_list_ptr
->wccp2_security_type
== WCCP2_MD5_SECURITY
) {
1978 wccp2_update_md5_security(service_list_ptr
->wccp_password
, (char *) security
, wccp_packet
, offset
);
1981 if (ntohl(router_list_ptr
->num_caches
)) {
1984 if (wccp2_numrouters
> 1) {
1985 /* FIXME INET6 : drop temp conversion */
1986 IpAddress
tmp_rtr(router
);
1987 comm_udp_sendto(theWccp2Connection
,
1992 send(theWccp2Connection
,
2000 service_list_ptr
= service_list_ptr
->next
;
2006 * Configuration option parsing code
2010 * Parse wccp2_return_method and wccp2_forwarding_method options
2011 * they can be '1' aka 'gre' or '2' aka 'l2'
2012 * repesenting the integer numeric of the same.
2015 parse_wccp2_method(int *method
)
2019 /* Snarf the method */
2020 if ((t
= strtok(NULL
, w_space
)) == NULL
) {
2021 debugs(80, DBG_CRITICAL
, "wccp2_*_method: missing setting.");
2025 /* update configuration if its valid */
2026 if (strcmp(t
, "gre") == 0 || strcmp(t
, "1") == 0) {
2027 *method
= WCCP2_METHOD_GRE
;
2028 } else if (strcmp(t
, "l2") == 0 || strcmp(t
, "2") == 0) {
2029 *method
= WCCP2_METHOD_L2
;
2031 debugs(80, DBG_CRITICAL
, "wccp2_*_method: unknown setting, got " << t
);
2037 dump_wccp2_method(StoreEntry
* e
, const char *label
, int v
)
2040 case WCCP2_METHOD_GRE
:
2041 storeAppendPrintf(e
, "%s gre\n", label
);
2043 case WCCP2_METHOD_L2
:
2044 storeAppendPrintf(e
, "%s l2\n", label
);
2047 debugs(80, DBG_CRITICAL
, "FATAL: WCCPv2 configured method (" << v
<< ") is not valid.");
2053 free_wccp2_method(int *v
)
2057 * Parse wccp2_assignment_method option
2058 * they can be '1' aka 'hash' or '2' aka 'mask'
2059 * repesenting the integer numeric of the same.
2062 parse_wccp2_amethod(int *method
)
2066 /* Snarf the method */
2067 if ((t
= strtok(NULL
, w_space
)) == NULL
) {
2068 debugs(80, DBG_CRITICAL
, "wccp2_assignment_method: missing setting.");
2072 /* update configuration if its valid */
2073 if (strcmp(t
, "hash") == 0 || strcmp(t
, "1") == 0) {
2074 *method
= WCCP2_ASSIGNMENT_METHOD_HASH
;
2075 } else if (strcmp(t
, "mask") == 0 || strcmp(t
, "2") == 0) {
2076 *method
= WCCP2_ASSIGNMENT_METHOD_MASK
;
2078 debugs(80, DBG_CRITICAL
, "wccp2_assignment_method: unknown setting, got " << t
);
2084 dump_wccp2_amethod(StoreEntry
* e
, const char *label
, int v
)
2087 case WCCP2_ASSIGNMENT_METHOD_HASH
:
2088 storeAppendPrintf(e
, "%s hash\n", label
);
2090 case WCCP2_ASSIGNMENT_METHOD_MASK
:
2091 storeAppendPrintf(e
, "%s mask\n", label
);
2094 debugs(80, DBG_CRITICAL
, "FATAL: WCCPv2 configured " << label
<< " (" << v
<< ") is not valid.");
2100 free_wccp2_amethod(int *v
)
2106 * wccp2_service {standard|dynamic} {id} (password=password)
2109 parse_wccp2_service(void *v
)
2114 int security_type
= WCCP2_NO_SECURITY
;
2115 char wccp_password
[WCCP2_PASSWORD_LEN
+ 1];
2117 if (wccp2_connected
== 1) {
2118 debugs(80, 1, "WCCPv2: Somehow reparsing the configuration without having shut down WCCP! Try reloading squid again.");
2122 /* Snarf the type */
2123 if ((t
= strtok(NULL
, w_space
)) == NULL
) {
2124 debugs(80, 0, "wccp2ParseServiceInfo: missing service info type (standard|dynamic)");
2128 if (strcmp(t
, "standard") == 0) {
2129 service
= WCCP2_SERVICE_STANDARD
;
2130 } else if (strcmp(t
, "dynamic") == 0) {
2131 service
= WCCP2_SERVICE_DYNAMIC
;
2133 debugs(80, 0, "wccp2ParseServiceInfo: bad service info type (expected standard|dynamic, got " << t
<< ")");
2138 service_id
= GetInteger();
2140 if (service_id
< 0 || service_id
> 255) {
2141 debugs(80, 0, "wccp2ParseServiceInfo: service info id " << service_id
<< " is out of range (0..255)");
2145 memset(wccp_password
, 0, sizeof(wccp_password
));
2146 /* Handle password, if any */
2148 if ((t
= strtok(NULL
, w_space
)) != NULL
) {
2149 if (strncmp(t
, "password=", 9) == 0) {
2150 security_type
= WCCP2_MD5_SECURITY
;
2151 strncpy(wccp_password
, t
+ 9, WCCP2_PASSWORD_LEN
);
2155 /* Create a placeholder service record */
2156 wccp2_add_service_list(service
, service_id
, 0, 0, 0, empty_portlist
, security_type
, wccp_password
);
2160 dump_wccp2_service(StoreEntry
* e
, const char *label
, void *v
)
2163 struct wccp2_service_list_t
*srv
;
2164 srv
= wccp2_service_list_head
;
2166 while (srv
!= NULL
) {
2167 debugs(80, 3, "dump_wccp2_service: id " << srv
->info
.service_id
<< ", type " << srv
->info
.service
);
2168 storeAppendPrintf(e
, "%s %s %d", label
,
2169 (srv
->info
.service
== WCCP2_SERVICE_DYNAMIC
) ? "dynamic" : "standard",
2170 srv
->info
.service_id
);
2172 if (srv
->wccp2_security_type
== WCCP2_MD5_SECURITY
) {
2173 storeAppendPrintf(e
, " %s", srv
->wccp_password
);
2176 storeAppendPrintf(e
, "\n");
2183 free_wccp2_service(void *v
)
2187 check_null_wccp2_service(void *v
)
2189 return !wccp2_service_list_head
;
2195 * wccp2_service_info {id} stuff..
2199 * + flags=flag,flag,flag..
2200 * + proto=protocol (tcp|udp)
2201 * + ports=port,port,port (up to a max of 8)
2202 * + priority=priority (0->255)
2204 * The flags here are:
2205 * src_ip_hash, dst_ip_hash, source_port_hash, dst_port_hash, ports_defined,
2206 * ports_source, src_ip_alt_hash, dst_ip_alt_hash, src_port_alt_hash, dst_port_alt_hash
2209 parse_wccp2_service_flags(char *flags
)
2219 tmp
= xstrdup(flags
);
2222 flag
= strsep(&tmp2
, ",");
2225 if (strcmp(flag
, "src_ip_hash") == 0) {
2226 retflag
|= WCCP2_SERVICE_SRC_IP_HASH
;
2227 } else if (strcmp(flag
, "dst_ip_hash") == 0) {
2228 retflag
|= WCCP2_SERVICE_DST_IP_HASH
;
2229 } else if (strcmp(flag
, "source_port_hash") == 0) {
2230 retflag
|= WCCP2_SERVICE_SRC_PORT_HASH
;
2231 } else if (strcmp(flag
, "dst_port_hash") == 0) {
2232 retflag
|= WCCP2_SERVICE_DST_PORT_HASH
;
2233 } else if (strcmp(flag
, "ports_source") == 0) {
2234 retflag
|= WCCP2_SERVICE_PORTS_SOURCE
;
2235 } else if (strcmp(flag
, "src_ip_alt_hash") == 0) {
2236 retflag
|= WCCP2_SERVICE_SRC_IP_ALT_HASH
;
2237 } else if (strcmp(flag
, "dst_ip_alt_hash") == 0) {
2238 retflag
|= WCCP2_SERVICE_DST_IP_ALT_HASH
;
2239 } else if (strcmp(flag
, "src_port_alt_hash") == 0) {
2240 retflag
|= WCCP2_SERVICE_SRC_PORT_ALT_HASH
;
2241 } else if (strcmp(flag
, "dst_port_alt_hash") == 0) {
2242 retflag
|= WCCP2_SERVICE_DST_PORT_ALT_HASH
;
2244 fatalf("Unknown wccp2 service flag: %s\n", flag
);
2247 flag
= strsep(&tmp2
, ",");
2255 parse_wccp2_service_ports(char *options
, int portlist
[])
2259 char *tmp
, *tmp2
, *port
, *end
;
2265 tmp
= xstrdup(options
);
2268 port
= strsep(&tmp2
, ",");
2270 while (port
&& i
< WCCP2_NUMPORTS
) {
2271 p
= strtol(port
, &end
, 0);
2273 if (p
< 1 || p
> 65535) {
2274 fatalf("parse_wccp2_service_ports: port value '%s' isn't valid (1..65535)\n", port
);
2279 port
= strsep(&tmp2
, ",");
2283 fatalf("parse_wccp2_service_ports: too many ports (maximum: 8) in list '%s'\n", options
);
2290 parse_wccp2_service_info(void *v
)
2295 int portlist
[WCCP2_NUMPORTS
];
2296 int protocol
= -1; /* IPPROTO_TCP | IPPROTO_UDP */
2298 struct wccp2_service_list_t
*srv
;
2301 if (wccp2_connected
== 1) {
2302 debugs(80, 1, "WCCPv2: Somehow reparsing the configuration without having shut down WCCP! Try reloading squid again.");
2306 debugs(80, 5, "parse_wccp2_service_info: called");
2307 memset(portlist
, 0, sizeof(portlist
));
2308 /* First argument: id */
2309 service_id
= GetInteger();
2311 if (service_id
< 0 || service_id
> 255) {
2312 debugs(80, 1, "parse_wccp2_service_info: invalid service id " << service_id
<< " (must be between 0 .. 255)");
2316 /* Next: find the (hopefully!) existing service */
2317 srv
= wccp2_get_service_by_id(WCCP2_SERVICE_DYNAMIC
, service_id
);
2320 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
);
2323 /* Next: loop until we don't have any more tokens */
2324 while ((t
= strtok(NULL
, w_space
)) != NULL
) {
2325 if (strncmp(t
, "flags=", 6) == 0) {
2326 /* XXX eww, string pointer math */
2327 flags
= parse_wccp2_service_flags(t
+ 6);
2328 } else if (strncmp(t
, "ports=", 6) == 0) {
2329 parse_wccp2_service_ports(t
+ 6, portlist
);
2330 flags
|= WCCP2_SERVICE_PORTS_DEFINED
;
2331 } else if (strncmp(t
, "protocol=tcp", 12) == 0) {
2332 protocol
= IPPROTO_TCP
;
2333 } else if (strncmp(t
, "protocol=udp", 12) == 0) {
2334 protocol
= IPPROTO_UDP
;
2335 } else if (strncmp(t
, "protocol=", 9) == 0) {
2336 fatalf("parse_wccp2_service_info: id %d: unknown protocol (%s) - must be tcp or udp!\n", service_id
, t
);
2337 } else if (strncmp(t
, "priority=", 9) == 0) {
2338 priority
= strtol(t
+ 9, &end
, 0);
2340 if (priority
< 0 || priority
> 255) {
2341 fatalf("parse_wccp2_service_info: id %d: %s out of range (0..255)!\n", service_id
, t
);
2344 fatalf("parse_wccp2_service_info: id %d: unknown option '%s'\n", service_id
, t
);
2348 /* Check everything is set */
2349 if (priority
== -1) {
2350 fatalf("parse_wccp2_service_info: service %d: no priority defined (valid: 0..255)!\n", service_id
);
2353 if (protocol
== -1) {
2354 fatalf("parse_wccp2_service_info: service %d: no protocol defined (valid: tcp or udp)!\n", service_id
);
2357 if (!(flags
& WCCP2_SERVICE_PORTS_DEFINED
)) {
2358 fatalf("parse_wccp2_service_info: service %d: no ports defined!\n", service_id
);
2361 /* rightio! now we can update */
2362 wccp2_update_service(srv
, WCCP2_SERVICE_DYNAMIC
, service_id
, priority
,
2363 protocol
, flags
, portlist
);
2369 dump_wccp2_service_info(StoreEntry
* e
, const char *label
, void *v
)
2373 struct wccp2_service_list_t
*srv
;
2375 srv
= wccp2_service_list_head
;
2377 while (srv
!= NULL
) {
2378 debugs(80, 3, "dump_wccp2_service_info: id " << srv
->info
.service_id
<< " (type " << srv
->info
.service
<< ")");
2380 /* We don't need to spit out information for standard services */
2382 if (srv
->info
.service
== WCCP2_SERVICE_STANDARD
) {
2383 debugs(80, 3, "dump_wccp2_service_info: id " << srv
->info
.service_id
<< ": standard service, not dumping info");
2390 storeAppendPrintf(e
, "%s %d", label
, srv
->info
.service_id
);
2393 storeAppendPrintf(e
, " priority=%d", srv
->info
.service_priority
);
2396 flags
= ntohl(srv
->info
.service_flags
);
2400 storeAppendPrintf(e
, " flags=");
2402 if (flags
& WCCP2_SERVICE_SRC_IP_HASH
) {
2403 storeAppendPrintf(e
, "%ssrc_ip_hash", comma
? "," : "");
2407 if (flags
& WCCP2_SERVICE_DST_IP_HASH
) {
2408 storeAppendPrintf(e
, "%sdst_ip_hash", comma
? "," : "");
2412 if (flags
& WCCP2_SERVICE_SRC_PORT_HASH
) {
2413 storeAppendPrintf(e
, "%ssource_port_hash", comma
? "," : "");
2417 if (flags
& WCCP2_SERVICE_DST_PORT_HASH
) {
2418 storeAppendPrintf(e
, "%sdst_port_hash", comma
? "," : "");
2422 if (flags
& WCCP2_SERVICE_PORTS_DEFINED
) {
2423 storeAppendPrintf(e
, "%sports_defined", comma
? "," : "");
2427 if (flags
& WCCP2_SERVICE_PORTS_SOURCE
) {
2428 storeAppendPrintf(e
, "%sports_source", comma
? "," : "");
2432 if (flags
& WCCP2_SERVICE_SRC_IP_ALT_HASH
) {
2433 storeAppendPrintf(e
, "%ssrc_ip_alt_hash", comma
? "," : "");
2437 if (flags
& WCCP2_SERVICE_DST_IP_ALT_HASH
) {
2438 storeAppendPrintf(e
, "%ssrc_ip_alt_hash", comma
? "," : "");
2442 if (flags
& WCCP2_SERVICE_SRC_PORT_ALT_HASH
) {
2443 storeAppendPrintf(e
, "%ssrc_port_alt_hash", comma
? "," : "");
2447 if (flags
& WCCP2_SERVICE_DST_PORT_ALT_HASH
) {
2448 storeAppendPrintf(e
, "%sdst_port_alt_hash", comma
? "," : "");
2456 if (srv
->info
.port0
!= 0) {
2457 storeAppendPrintf(e
, "%s%d", comma
? "," : " ports=", ntohs(srv
->info
.port0
));
2461 if (srv
->info
.port1
!= 0) {
2462 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port1
));
2466 if (srv
->info
.port2
!= 0) {
2467 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port2
));
2471 if (srv
->info
.port3
!= 0) {
2472 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port3
));
2476 if (srv
->info
.port4
!= 0) {
2477 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port4
));
2481 if (srv
->info
.port5
!= 0) {
2482 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port5
));
2486 if (srv
->info
.port6
!= 0) {
2487 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port6
));
2491 if (srv
->info
.port7
!= 0) {
2492 storeAppendPrintf(e
, "%s%d", comma
? "," : "ports=", ntohs(srv
->info
.port7
));
2497 storeAppendPrintf(e
, " protocol=%s", (srv
->info
.service_protocol
== IPPROTO_TCP
) ? "tcp" : "udp");
2499 storeAppendPrintf(e
, "\n");
2505 /* Sort the cache list by doing a "selection sort" by IP address */
2507 wccp2SortCacheList(struct wccp2_cache_list_t
*head
)
2509 struct wccp2_cache_list_t tmp
;
2510 struct wccp2_cache_list_t
*this_item
;
2511 struct wccp2_cache_list_t
*find_item
;
2512 struct wccp2_cache_list_t
*next_lowest
;
2514 /* Go through each position in the list one at a time */
2515 for (this_item
= head
; this_item
->next
; this_item
= this_item
->next
) {
2516 /* Find the item with the lowest IP */
2517 next_lowest
= this_item
;
2519 for (find_item
= this_item
; find_item
->next
; find_item
= find_item
->next
) {
2520 if (find_item
->cache_ip
.s_addr
< next_lowest
->cache_ip
.s_addr
) {
2521 next_lowest
= find_item
;
2524 /* Swap if we need to */
2525 if (next_lowest
!= this_item
) {
2526 /* First make a copy of the current item */
2527 memcpy(&tmp
, this_item
, sizeof(struct wccp2_cache_list_t
));
2529 /* Next update the pointers to maintain the linked list */
2530 tmp
.next
= next_lowest
->next
;
2531 next_lowest
->next
= this_item
->next
;
2533 /* Finally copy the updated items to their correct location */
2534 memcpy(this_item
, next_lowest
, sizeof(struct wccp2_cache_list_t
));
2535 memcpy(next_lowest
, &tmp
, sizeof(struct wccp2_cache_list_t
));
2541 free_wccp2_service_info(void *v
)
2544 #endif /* USE_WCCPv2 */