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