]> git.ipfire.org Git - thirdparty/squid.git/blame - src/wccp2.cc
Polish: update Ip::Address to follow Squid coding guidelines
[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"
27bc2077 41#include "compat/strsep.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
AJ
69#define WCCP2_NONE_SECURITY_LEN 0
70#define WCCP2_MD5_SECURITY_LEN 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{
09aabd84 576 uint8_t md5_digest[16];
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 */
604 /* XXX eventually we should be able to kill md5_digest 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
c3031d67 613 SquidMD5Final(md5_digest, &M);
0b0cfcf2 614
615 memcpy(ws->security_implementation, md5_digest, sizeof(md5_digest));
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;
09aabd84 630 uint8_t md5_digest[16], md5_challenge[16];
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 */
658 memcpy(md5_challenge, ws->security_implementation, 16);
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
c3031d67 668 SquidMD5Final(md5_digest, &M);
0b0cfcf2 669
670 return (memcmp(md5_digest, md5_challenge, 16) == 0);
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;
0b0cfcf2 1175
1176 len = comm_udp_recvfrom(sock,
1177 &wccp2_i_see_you,
1178 WCCP_RESPONSE_SIZE,
1179 0,
cc192b50 1180 from_tmp);
26ac0430 1181 /* FIXME INET6 : drop conversion boundary */
4dd643d5 1182 from_tmp.getSockAddr(from);
0b0cfcf2 1183
1184 if (len < 0)
1185 return;
1186
1187 if (ntohs(wccp2_i_see_you.version) != WCCP2_VERSION)
1188 return;
1189
1190 if (ntohl(wccp2_i_see_you.type) != WCCP2_I_SEE_YOU)
1191 return;
1192
bf8fe701 1193 debugs(80, 3, "Incoming WCCPv2 I_SEE_YOU length " << ntohs(wccp2_i_see_you.length) << ".");
0b0cfcf2 1194
1195 /* Record the total data length */
1196 data_length = ntohs(wccp2_i_see_you.length);
1197
1198 offset = 0;
1199
1200 if (data_length > len) {
e0236918 1201 debugs(80, DBG_IMPORTANT, "ERROR: Malformed WCCPv2 packet claiming it's bigger than received data");
0b0cfcf2 1202 return;
1203 }
1204
1205 /* Go through the data structure */
1206 while (data_length > offset) {
1207
af6a12ee 1208 char *data = wccp2_i_see_you.data;
c6ba4504
HN
1209
1210 header = (struct wccp2_item_header_t *) &data[offset];
0b0cfcf2 1211
1212 switch (ntohs(header->type)) {
1213
1214 case WCCP2_SECURITY_INFO:
1215
1216 if (security_info != NULL) {
e0236918 1217 debugs(80, DBG_IMPORTANT, "Duplicate security definition");
0b0cfcf2 1218 return;
1219 }
1220
1221 security_info = (struct wccp2_security_none_t *) &wccp2_i_see_you.data[offset];
1222 break;
1223
1224 case WCCP2_SERVICE_INFO:
1225
1226 if (service_info != NULL) {
e0236918 1227 debugs(80, DBG_IMPORTANT, "Duplicate service_info definition");
0b0cfcf2 1228 return;
1229 }
1230
1231 service_info = (struct wccp2_service_info_t *) &wccp2_i_see_you.data[offset];
1232 break;
1233
1234 case WCCP2_ROUTER_ID_INFO:
1235
1236 if (router_identity_info != NULL) {
e0236918 1237 debugs(80, DBG_IMPORTANT, "Duplicate router_identity_info definition");
0b0cfcf2 1238 return;
1239 }
1240
1241 router_identity_info = (struct router_identity_info_t *) &wccp2_i_see_you.data[offset];
1242 break;
1243
1244 case WCCP2_RTR_VIEW_INFO:
1245
1246 if (router_view_header != NULL) {
e0236918 1247 debugs(80, DBG_IMPORTANT, "Duplicate router_view definition");
0b0cfcf2 1248 return;
1249 }
1250
1251 router_view_header = (struct router_view_t *) &wccp2_i_see_you.data[offset];
1252 break;
1253
1254 case WCCP2_CAPABILITY_INFO:
1255
1256 if (router_capability_header != NULL) {
e0236918 1257 debugs(80, DBG_IMPORTANT, "Duplicate router_capability definition");
0b0cfcf2 1258 return;
1259 }
1260
1261 router_capability_header = (struct wccp2_capability_info_header_t *) &wccp2_i_see_you.data[offset];
c1f55201 1262 break;
0b0cfcf2 1263
b7d249f9 1264 /* Nothing to do for the types below */
1265
1266 case WCCP2_ASSIGN_MAP:
40228366 1267 case WCCP2_REDIRECT_ASSIGNMENT:
b7d249f9 1268 break;
1269
0b0cfcf2 1270 default:
e0236918 1271 debugs(80, DBG_IMPORTANT, "Unknown record type in WCCPv2 Packet (" << ntohs(header->type) << ").");
0b0cfcf2 1272 }
1273
1274 offset += sizeof(struct wccp2_item_header_t);
1275 offset += ntohs(header->length);
1276
1277 if (offset > data_length) {
e0236918 1278 debugs(80, DBG_IMPORTANT, "Error: WCCPv2 packet tried to tell us there is data beyond the end of the packet");
0b0cfcf2 1279 return;
1280 }
1281 }
1282
1283 if ((security_info == NULL) || (service_info == NULL) || (router_identity_info == NULL) || (router_view_header == NULL)) {
e0236918 1284 debugs(80, DBG_IMPORTANT, "Incomplete WCCPv2 Packet");
0b0cfcf2 1285 return;
1286 }
1287
bf8fe701 1288 debugs(80, 5, "Complete packet received");
0b0cfcf2 1289
1290 /* Check that the service in the packet is configured on this router */
1291 service_list_ptr = wccp2_service_list_head;
1292
1293 while (service_list_ptr != NULL) {
1294 if (service_info->service_id == service_list_ptr->service_info->service_id) {
1295 break;
1296 }
1297
1298 service_list_ptr = service_list_ptr->next;
1299 }
1300
1301 if (service_list_ptr == NULL) {
e0236918 1302 debugs(80, DBG_IMPORTANT, "WCCPv2 Unknown service received from router (" << service_info->service_id << ")");
0b0cfcf2 1303 return;
1304 }
1305
1306 if (ntohl(security_info->security_option) != ntohl(service_list_ptr->security_info->security_option)) {
e0236918 1307 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 1308 return;
1309 }
1310
1311 if (!wccp2_check_security(service_list_ptr, (char *) security_info, (char *) &wccp2_i_see_you, len)) {
e0236918 1312 debugs(80, DBG_IMPORTANT, "Received WCCPv2 Packet failed authentication");
0b0cfcf2 1313 return;
1314 }
1315
1316 /* Check that the router address is configured on this router */
1317 for (router_list_ptr = &service_list_ptr->router_list_head; router_list_ptr->next != NULL; router_list_ptr = router_list_ptr->next) {
1318 if (router_list_ptr->router_sendto_address.s_addr == from.sin_addr.s_addr)
1319 break;
1320 }
1321
1322 if (router_list_ptr->next == NULL) {
e0236918 1323 debugs(80, DBG_IMPORTANT, "WCCPv2 Packet received from unknown router");
0b0cfcf2 1324 return;
1325 }
1326
1327 /* Set the router id */
1328 router_list_ptr->info->router_address = router_identity_info->router_id_element.router_address;
1329
1330 /* Increment the received id in the packet */
1331 if (ntohl(router_list_ptr->info->received_id) != ntohl(router_identity_info->router_id_element.received_id)) {
6013702c 1332 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 1333 router_list_ptr->info->received_id = router_identity_info->router_id_element.received_id;
1334 }
1335
1336 /* TODO: check return/forwarding methods */
1337 if (router_capability_header == NULL) {
c1f55201 1338 if ((Config.Wccp2.return_method != WCCP2_PACKET_RETURN_METHOD_GRE) || (Config.Wccp2.forwarding_method != WCCP2_FORWARDING_METHOD_GRE)) {
e0236918 1339 debugs(80, DBG_IMPORTANT, "wccp2HandleUdp: fatal error - A WCCP router does not support the forwarding method specified, only GRE supported");
0b0cfcf2 1340 wccp2ConnectionClose();
1341 return;
1342 }
1343 } else {
0b0cfcf2 1344
c1f55201 1345 char *end = ((char *) router_capability_header) + sizeof(*router_capability_header) + ntohs(router_capability_header->capability_info_length) - sizeof(struct wccp2_capability_info_header_t);
1346
1347 router_capability_element = (struct wccp2_capability_element_t *) (((char *) router_capability_header) + sizeof(*router_capability_header));
0b0cfcf2 1348
c1f55201 1349 while ((char *) router_capability_element <= end) {
0b0cfcf2 1350
1351 switch (ntohs(router_capability_element->capability_type)) {
1352
1353 case WCCP2_CAPABILITY_FORWARDING_METHOD:
1354
c1f55201 1355 if (!(ntohl(router_capability_element->capability_value) & Config.Wccp2.forwarding_method)) {
e0236918 1356 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 1357 wccp2ConnectionClose();
1358 return;
1359 }
1360
1361 break;
1362
1363 case WCCP2_CAPABILITY_ASSIGNMENT_METHOD:
c1f55201 1364
b7d249f9 1365 if (!(ntohl(router_capability_element->capability_value) & Config.Wccp2.assignment_method)) {
e0236918 1366 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 1367 wccp2ConnectionClose();
1368 return;
1369 }
1370
0b0cfcf2 1371 break;
1372
1373 case WCCP2_CAPABILITY_RETURN_METHOD:
1374
c1f55201 1375 if (!(ntohl(router_capability_element->capability_value) & Config.Wccp2.return_method)) {
e0236918 1376 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 1377 wccp2ConnectionClose();
1378 return;
1379 }
1380
1381 break;
1382
43f3d687
AJ
1383 case 4:
1384 case 5:
1385 break; // ignore silently for now
1386
0b0cfcf2 1387 default:
e0236918 1388 debugs(80, DBG_IMPORTANT, "Unknown capability type in WCCPv2 Packet (" << ntohs(router_capability_element->capability_type) << ").");
0b0cfcf2 1389 }
c1f55201 1390
c898ced1 1391 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 1392 }
1393 }
1394
bf8fe701 1395 debugs(80, 5, "Cleaning out cache list");
0b0cfcf2 1396 /* clean out the old cache list */
1397
1398 for (cache_list_ptr = &router_list_ptr->cache_list_head; cache_list_ptr; cache_list_ptr = cache_list_ptr_next) {
1399 cache_list_ptr_next = cache_list_ptr->next;
1400
1401 if (cache_list_ptr != &router_list_ptr->cache_list_head) {
1402 xfree(cache_list_ptr);
1403 }
1404 }
1405
1406 router_list_ptr->num_caches = htonl(0);
1407 num_caches = 0;
1408
1409 /* Check to see if we're the master cache and update the cache list */
1410 found = 0;
1411 service_list_ptr->lowest_ip = 1;
1412 cache_list_ptr = &router_list_ptr->cache_list_head;
1413
1414 /* to find the list of caches, we start at the end of the router view header */
1415
1416 ptr = (char *) (router_view_header) + sizeof(struct router_view_t);
1417
1418 /* Then we read the number of routers */
1419 memcpy(&tmp, ptr, sizeof(tmp));
1420
1421 /* skip the number plus all the ip's */
1422
2adffc6f 1423 ptr += sizeof(tmp) + (ntohl(tmp) * sizeof(struct in_addr));
0b0cfcf2 1424
1425 /* Then read the number of caches */
1426 memcpy(&tmp, ptr, sizeof(tmp));
1427 ptr += sizeof(tmp);
1428
1429 if (ntohl(tmp) != 0) {
1430 /* search through the list of received-from ip addresses */
1431
14942edd 1432 for (num_caches = 0; num_caches < (int) ntohl(tmp); ++num_caches) {
0b0cfcf2 1433 /* Get a copy of the ip */
af6a12ee 1434 memset(&cache_address, 0, sizeof(cache_address)); // Make GCC happy
0b0cfcf2 1435
b7d249f9 1436 switch (Config.Wccp2.assignment_method) {
1437
1438 case WCCP2_ASSIGNMENT_METHOD_HASH:
1439
1440 cache_identity = (struct wccp2_cache_identity_info_t *) ptr;
1441
1442 ptr += sizeof(struct wccp2_cache_identity_info_t);
1443
2adffc6f 1444 memcpy(&cache_address, &cache_identity->addr, sizeof(struct in_addr));
b7d249f9 1445
1446 cache_list_ptr->weight = ntohs(cache_identity->weight);
1447 break;
1448
1449 case WCCP2_ASSIGNMENT_METHOD_MASK:
1450
1451 cache_mask_info = (struct cache_mask_info_t *) ptr;
1452
1453 /* The mask assignment has an undocumented variable length entry here */
1454
1455 if (ntohl(cache_mask_info->num1) == 3) {
1456
1457 cache_mask_identity = (struct wccp2_cache_mask_identity_info_t *) ptr;
1458
1459 ptr += sizeof(struct wccp2_cache_mask_identity_info_t);
0b0cfcf2 1460
2adffc6f 1461 memcpy(&cache_address, &cache_mask_identity->addr, sizeof(struct in_addr));
b7d249f9 1462 } else {
1463
1464 ptr += sizeof(struct cache_mask_info_t);
1465
2adffc6f 1466 memcpy(&cache_address, &cache_mask_info->addr, sizeof(struct in_addr));
b7d249f9 1467 }
0b0cfcf2 1468
b7d249f9 1469 cache_list_ptr->weight = 0;
1470 break;
1471
1472 default:
1473 fatalf("Unknown Wccp2 assignment method\n");
1474 }
0b0cfcf2 1475
1476 /* Update the cache list */
1477 cache_list_ptr->cache_ip = cache_address;
b7d249f9 1478
0b0cfcf2 1479 cache_list_ptr->next = (wccp2_cache_list_t*) xcalloc(1, sizeof(struct wccp2_cache_list_t));
b7d249f9 1480
0b0cfcf2 1481 cache_list_ptr = cache_list_ptr->next;
b7d249f9 1482
0b0cfcf2 1483 cache_list_ptr->next = NULL;
1484
bf8fe701 1485 debugs (80, 5, "checking cache list: (" << std::hex << cache_address.s_addr << ":" << router_list_ptr->local_ip.s_addr << ")");
b7d249f9 1486
0b0cfcf2 1487 /* Check to see if it's the master, or us */
1488
1489 if (cache_address.s_addr == router_list_ptr->local_ip.s_addr) {
1490 found = 1;
1491 }
1492
1493 if (cache_address.s_addr < router_list_ptr->local_ip.s_addr) {
1494 service_list_ptr->lowest_ip = 0;
1495 }
1496 }
1497 } else {
bf8fe701 1498 debugs(80, 5, "Adding ourselves as the only cache");
0b0cfcf2 1499
1500 /* Update the cache list */
1501 cache_list_ptr->cache_ip = router_list_ptr->local_ip;
1502
1503 cache_list_ptr->next = (wccp2_cache_list_t*) xcalloc(1, sizeof(struct wccp2_cache_list_t));
1504 cache_list_ptr = cache_list_ptr->next;
1505 cache_list_ptr->next = NULL;
1506
b7d249f9 1507 service_list_ptr->lowest_ip = 1;
0b0cfcf2 1508 found = 1;
1509 num_caches = 1;
1510 }
1511
264096e2 1512 wccp2SortCacheList(&router_list_ptr->cache_list_head);
1513
0b0cfcf2 1514 router_list_ptr->num_caches = htonl(num_caches);
1515
1516 if ((found == 1) && (service_list_ptr->lowest_ip == 1)) {
1517 if (ntohl(router_view_header->change_number) != router_list_ptr->member_change) {
bf8fe701 1518 debugs(80, 4, "Change detected - queueing up new assignment");
0b0cfcf2 1519 router_list_ptr->member_change = ntohl(router_view_header->change_number);
c1f55201 1520 eventDelete(wccp2AssignBuckets, NULL);
1521 eventAdd("wccp2AssignBuckets", wccp2AssignBuckets, NULL, 15.0, 1);
b7d249f9 1522 } else {
4a7a3d56 1523 debugs(80, 5, "Change not detected (" << ntohl(router_view_header->change_number) << " = " << router_list_ptr->member_change << ")");
0b0cfcf2 1524 }
1525 } else {
b7d249f9 1526 eventDelete(wccp2AssignBuckets, NULL);
bf8fe701 1527 debugs(80, 5, "I am not the lowest ip cache - not assigning buckets");
0b0cfcf2 1528 }
1529}
1530
1531static void
1532wccp2HereIam(void *voidnotused)
1533{
1534
1535 struct wccp2_service_list_t *service_list_ptr;
1536
1537 struct wccp2_router_list_t *router_list_ptr;
1538
b7d249f9 1539 struct wccp2_identity_info_t *wccp2_identity_info_ptr;
1540
1541 struct wccp2_mask_identity_info_t *wccp2_mask_identity_info_ptr;
1542
b7ac5457 1543 Ip::Address router;
0b0cfcf2 1544
bf8fe701 1545 debugs(80, 6, "wccp2HereIam: Called");
0b0cfcf2 1546
1547 if (wccp2_connected == 0) {
e0236918 1548 debugs(80, DBG_IMPORTANT, "wccp2HereIam: wccp2 socket closed. Shutting down WCCP2");
0b0cfcf2 1549 return;
1550 }
1551
bef81ea5 1552 /* Wait if store dirs are rebuilding */
1553 if (StoreController::store_dirs_rebuilding && Config.Wccp2.rebuildwait) {
f67332d3 1554 eventAdd("wccp2HereIam", wccp2HereIam, NULL, 1.0, 1);
1555 return;
1556 }
1557
4dd643d5 1558 router.port(WCCP_PORT);
0b0cfcf2 1559
1560 /* for each router on each service send a packet */
1561 service_list_ptr = wccp2_service_list_head;
1562
1563 while (service_list_ptr != NULL) {
bf8fe701 1564 debugs(80, 5, "wccp2HereIam: sending to service id " << service_list_ptr->info.service_id);
0b0cfcf2 1565
1566 for (router_list_ptr = &service_list_ptr->router_list_head; router_list_ptr->next != NULL; router_list_ptr = router_list_ptr->next) {
cc192b50 1567 router = router_list_ptr->router_sendto_address;
0b0cfcf2 1568
1569 /* Set the cache id (ip) */
b7d249f9 1570
1571 switch (Config.Wccp2.assignment_method) {
1572
1573 case WCCP2_ASSIGNMENT_METHOD_HASH:
1574
1575 wccp2_identity_info_ptr = (struct wccp2_identity_info_t *) service_list_ptr->wccp2_identity_info_ptr;
1576 wccp2_identity_info_ptr->cache_identity.addr = router_list_ptr->local_ip;
1577 break;
1578
1579 case WCCP2_ASSIGNMENT_METHOD_MASK:
1580
1581 wccp2_mask_identity_info_ptr = (struct wccp2_mask_identity_info_t *) service_list_ptr->wccp2_identity_info_ptr;
1582 wccp2_mask_identity_info_ptr->cache_identity.addr = router_list_ptr->local_ip;
1583 break;
1584
1585 default:
1586 fatalf("Unknown Wccp2 assignment method\n");
1587 }
1588
0b0cfcf2 1589 /* Security update, if needed */
1590
1591 if (service_list_ptr->wccp2_security_type == WCCP2_MD5_SECURITY) {
1592 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);
1593 }
1594
4a7a3d56 1595 debugs(80, 3, "Sending HereIam packet size " << service_list_ptr->wccp_packet_size);
0b0cfcf2 1596 /* Send the packet */
0b0cfcf2 1597
c1f55201 1598 if (wccp2_numrouters > 1) {
1599 comm_udp_sendto(theWccp2Connection,
cc192b50 1600 router,
c1f55201 1601 &service_list_ptr->wccp_packet,
1602 service_list_ptr->wccp_packet_size);
1603 } else {
182106f3 1604 errno = 0;
3af9db3f 1605 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 1606 debugs(80, 2, "ERROR: failed to send WCCPv2 HERE_I_AM packet to " << router << " : " << xstrerror());
c1f55201 1607 }
0b0cfcf2 1608 }
1609
1610 service_list_ptr = service_list_ptr->next;
1611 }
1612
f67332d3 1613 eventAdd("wccp2HereIam", wccp2HereIam, NULL, 10.0, 1);
0b0cfcf2 1614}
1615
1616static void
1617wccp2AssignBuckets(void *voidnotused)
1618{
1619
1620 struct wccp2_service_list_t *service_list_ptr;
1621
1622 struct wccp2_router_list_t *router_list_ptr;
1623
1624 struct wccp2_cache_list_t *cache_list_ptr;
1625 char wccp_packet[WCCP_RESPONSE_SIZE];
b7d249f9 1626 short int offset, saved_offset, assignment_offset, alt_assignment_offset;
0b0cfcf2 1627
1628 struct sockaddr_in router;
1629 int router_len;
1630 int bucket_counter;
b7d249f9 1631 uint32_t service_flags;
f45dd259 1632 unsigned short port = WCCP_PORT;
0b0cfcf2 1633
1634 /* Packet segments */
1635
c898ced1 1636 struct wccp2_message_header_t *main_header;
0b0cfcf2 1637
1638 struct wccp2_security_md5_t *security = NULL;
1639 /* service from service struct */
1640
1641 struct wccp2_item_header_t *assignment_header;
1642
b7d249f9 1643 struct wccp2_item_header_t *alt_assignment_type_header = NULL;
1644
0b0cfcf2 1645 struct assignment_key_t *assignment_key;
1646 /* number of routers */
1647
1648 struct wccp2_router_assign_element_t *router_assign;
1649 /* number of caches */
1650
2adffc6f 1651 struct in_addr *cache_address;
b7d249f9 1652 /* Alternative assignement mask/values */
1653 int num_maskval;
1654
1655 struct wccp2_mask_element_t *mask_element;
1656
1657 struct wccp2_value_element_t *value_element;
1658 int valuecounter, value;
0b0cfcf2 1659 char *buckets;
1660
b7d249f9 1661 assignment_offset = alt_assignment_offset = 0;
1662
0b0cfcf2 1663 router_len = sizeof(router);
1664 memset(&router, '\0', router_len);
1665 router.sin_family = AF_INET;
1666 router.sin_port = htons(port);
1667
1668 /* Start main header - fill in length later */
1669 offset = 0;
1670
c898ced1 1671 main_header = (struct wccp2_message_header_t *) &wccp_packet[offset];
0b0cfcf2 1672 main_header->type = htonl(WCCP2_REDIRECT_ASSIGN);
1673 main_header->version = htons(WCCP2_VERSION);
1674
bf8fe701 1675 debugs(80, 2, "Running wccp2AssignBuckets");
0b0cfcf2 1676 service_list_ptr = wccp2_service_list_head;
1677
1678 while (service_list_ptr != NULL) {
1679 /* If we're not the lowest, we don't need to worry */
1680
1681 if (service_list_ptr->lowest_ip == 0) {
1682 /* XXX eww */
1683 service_list_ptr = service_list_ptr->next;
1684 continue;
1685 }
1686
1687 /* reset the offset */
1688
c898ced1 1689 offset = sizeof(struct wccp2_message_header_t);
0b0cfcf2 1690
1691 /* build packet header from hereIam packet */
1692 /* Security info */
1693 /* XXX this should be made more generic! */
1694 /* XXX and I hate magic numbers! */
1695 switch (service_list_ptr->wccp2_security_type) {
1696
1697 case WCCP2_NO_SECURITY:
1698
1699 security = (struct wccp2_security_md5_t *) &wccp_packet[offset];
1700 memcpy(security, service_list_ptr->security_info, 8);
1701 offset += 8;
1702 break;
1703
1704 case WCCP2_MD5_SECURITY:
1705
1706 security = (struct wccp2_security_md5_t *) &wccp_packet[offset];
1707
1708 memcpy(security, service_list_ptr->security_info, sizeof(struct wccp2_security_md5_t));
1709
1710 offset += sizeof(struct wccp2_security_md5_t);
1711 break;
1712
1713 default:
1714 fatalf("Unknown Wccp2 security type\n");
1715 }
1716
1717 /* Service info */
1718
1719 memcpy(&wccp_packet[offset], service_list_ptr->service_info, sizeof(struct wccp2_service_info_t));
1720
1721 offset += sizeof(struct wccp2_service_info_t);
1722
1723 /* assignment header - fill in length later */
1724
1725 assignment_header = (struct wccp2_item_header_t *) &wccp_packet[offset];
1726
b7d249f9 1727 switch (Config.Wccp2.assignment_method) {
0b0cfcf2 1728
b7d249f9 1729 case WCCP2_ASSIGNMENT_METHOD_HASH:
1730 assignment_header->type = htons(WCCP2_REDIRECT_ASSIGNMENT);
1731
1732 offset += sizeof(struct wccp2_item_header_t);
1733 assignment_offset = offset;
1734 break;
1735
1736 case WCCP2_ASSIGNMENT_METHOD_MASK:
1737 assignment_header->type = htons(WCCP2_ALT_ASSIGNMENT);
1738
1739 offset += sizeof(struct wccp2_item_header_t);
1740 assignment_offset = offset;
1741
1742 /* The alternative assignment has an extra header, fill in length later */
1743
1744 alt_assignment_type_header = (struct wccp2_item_header_t *) &wccp_packet[offset];
1745 alt_assignment_type_header->type = htons(WCCP2_MASK_ASSIGNMENT);
1746
1747 offset += sizeof(struct wccp2_item_header_t);
1748 alt_assignment_offset = offset;
1749
1750 break;
1751
1752 default:
1753 fatalf("Unknown Wccp2 assignment method\n");
1754 }
0b0cfcf2 1755
1756 /* Assignment key - fill in master ip later */
1757
1758 assignment_key = (struct assignment_key_t *) &wccp_packet[offset];
1759
1760 assignment_key->master_number = htonl(++service_list_ptr->change_num);
1761
1762 offset += sizeof(struct assignment_key_t);
1763
1764 /* Number of routers */
41d00cd3 1765 memcpy(&wccp_packet[offset], &service_list_ptr->num_routers, sizeof(service_list_ptr->num_routers));
0b0cfcf2 1766
1767 offset += sizeof(service_list_ptr->num_routers);
1768
1769 for (router_list_ptr = &service_list_ptr->router_list_head; router_list_ptr->next != NULL; router_list_ptr = router_list_ptr->next) {
1770
1771 /* Add routers */
1772
1773 router_assign = (struct wccp2_router_assign_element_t *) &wccp_packet[offset];
1774 router_assign->router_address = router_list_ptr->info->router_address;
1775 router_assign->received_id = router_list_ptr->info->received_id;
1776 router_assign->change_number = htonl(router_list_ptr->member_change);
1777
1778 offset += sizeof(struct wccp2_router_assign_element_t);
1779 }
1780
1781 saved_offset = offset;
1782
1783 for (router_list_ptr = &service_list_ptr->router_list_head; router_list_ptr->next != NULL; router_list_ptr = router_list_ptr->next) {
b7d249f9 1784 unsigned long *weight = (unsigned long *)xcalloc(sizeof(*weight), ntohl(router_list_ptr->num_caches));
1785 unsigned long total_weight = 0;
1786 int num_caches = ntohl(router_list_ptr->num_caches);
f16fbc82 1787
0b0cfcf2 1788 offset = saved_offset;
1789
b7d249f9 1790 switch (Config.Wccp2.assignment_method) {
1791
1792 case WCCP2_ASSIGNMENT_METHOD_HASH:
1793 /* Number of caches */
41d00cd3 1794 memcpy(&wccp_packet[offset], &router_list_ptr->num_caches, sizeof(router_list_ptr->num_caches));
b7d249f9 1795 offset += sizeof(router_list_ptr->num_caches);
1796
1797 if (num_caches) {
1798 int cache;
1799
14942edd 1800 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 1801 /* add caches */
0b0cfcf2 1802
2adffc6f 1803 cache_address = (struct in_addr *) &wccp_packet[offset];
0b0cfcf2 1804
41d00cd3 1805 memcpy(cache_address, &cache_list_ptr->cache_ip, sizeof(struct in_addr));
b7d249f9 1806 total_weight += cache_list_ptr->weight << 12;
1807 weight[cache] = cache_list_ptr->weight << 12;
0b0cfcf2 1808
2adffc6f 1809 offset += sizeof(struct in_addr);
b7d249f9 1810 }
0b0cfcf2 1811 }
0b0cfcf2 1812
b7d249f9 1813 /* Add buckets */
1814 buckets = (char *) &wccp_packet[offset];
1815
1816 memset(buckets, '\0', WCCP_BUCKETS);
1817
1818 if (num_caches != 0) {
1819 if (total_weight == 0) {
14942edd 1820 for (bucket_counter = 0; bucket_counter < WCCP_BUCKETS; ++bucket_counter) {
b7d249f9 1821 buckets[bucket_counter] = (char) (bucket_counter % num_caches);
1822 }
1823 } else {
1824 unsigned long *assigned = (unsigned long *)xcalloc(sizeof(*assigned), num_caches);
1825 unsigned long done = 0;
1826 int cache = -1;
1827 unsigned long per_bucket = total_weight / WCCP_BUCKETS;
1828
14942edd 1829 for (bucket_counter = 0; bucket_counter < WCCP_BUCKETS; ++bucket_counter) {
b7d249f9 1830 int n;
1831 unsigned long step;
1832
5e263176 1833 for (n = num_caches; n; --n) {
14942edd 1834 ++cache;
b7d249f9 1835
1836 if (cache >= num_caches)
1837 cache = 0;
1838
1839 if (!weight[cache]) {
14942edd 1840 ++n;
b7d249f9 1841 continue;
1842 }
1843
1844 if (assigned[cache] <= done)
1845 break;
1846 }
1847
1848 buckets[bucket_counter] = (char) cache;
1849 step = per_bucket * total_weight / weight[cache];
1850 assigned[cache] += step;
1851 done += per_bucket;
1852 }
1853
1854 safe_free(assigned);
1855 }
1856 }
0b0cfcf2 1857
b7d249f9 1858 offset += (WCCP_BUCKETS * sizeof(char));
1859 safe_free(weight);
1860 break;
0b0cfcf2 1861
b7d249f9 1862 case WCCP2_ASSIGNMENT_METHOD_MASK:
1863 num_maskval = htonl(1);
41d00cd3 1864 memcpy(&wccp_packet[offset], &num_maskval, sizeof(int));
b7d249f9 1865 offset += sizeof(int);
1866
1867 mask_element = (struct wccp2_mask_element_t *) &wccp_packet[offset];
1868 service_flags = ntohl(service_list_ptr->service_info->service_flags);
1869
1870 if ((service_flags & WCCP2_SERVICE_SRC_IP_HASH) || (service_flags & WCCP2_SERVICE_SRC_IP_ALT_HASH)) {
1871 mask_element->source_ip_mask = htonl(0x00001741);
1872 mask_element->dest_ip_mask = 0;
1873 mask_element->source_port_mask = 0;
1874 mask_element->dest_port_mask = 0;
1875 } 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)) {
1876 mask_element->source_ip_mask = 0;
1877 mask_element->dest_ip_mask = htonl(0x00001741);
1878 mask_element->source_port_mask = 0;
1879 mask_element->dest_port_mask = 0;
1880 } else if ((service_flags & WCCP2_SERVICE_SRC_PORT_HASH) || (service_flags & WCCP2_SERVICE_SRC_PORT_ALT_HASH)) {
1881 mask_element->source_ip_mask = 0;
1882 mask_element->dest_ip_mask = 0;
1883 mask_element->source_port_mask = htons(0x1741);
1884 mask_element->dest_port_mask = 0;
1885 } else if ((service_flags & WCCP2_SERVICE_DST_PORT_HASH) || (service_flags & WCCP2_SERVICE_DST_PORT_ALT_HASH)) {
1886 mask_element->source_ip_mask = 0;
1887 mask_element->dest_ip_mask = 0;
1888 mask_element->source_port_mask = 0;
1889 mask_element->dest_port_mask = htons(0x1741);
1890 } else {
1891 fatalf("Unknown service hash method\n");
1892 }
1893
1894 mask_element->number_values = htonl(64);
1895
1896 offset += sizeof(struct wccp2_mask_element_t);
1897
1898 cache_list_ptr = &router_list_ptr->cache_list_head;
1899 value = 0;
1900
14942edd 1901 for (valuecounter = 0; valuecounter < 64; ++valuecounter) {
b7d249f9 1902
1903 value_element = (struct wccp2_value_element_t *) &wccp_packet[offset];
1904
25e26086 1905 /* Update the value according the the "correct" formula */
1906
14942edd 1907 for (; (value & 0x1741) != value; ++value) {
25e26086 1908 assert(value <= 0x1741);
1909 }
1910
b7d249f9 1911 if ((service_flags & WCCP2_SERVICE_SRC_IP_HASH) || (service_flags & WCCP2_SERVICE_SRC_IP_ALT_HASH)) {
1912 value_element->source_ip_value = htonl(value);
1913 value_element->dest_ip_value = 0;
1914 value_element->source_port_value = 0;
1915 value_element->dest_port_value = 0;
1916 } else if ((service_list_ptr->info.service == WCCP2_SERVICE_STANDARD) || (service_flags & WCCP2_SERVICE_DST_IP_HASH) || (service_flags & WCCP2_SERVICE_DST_IP_ALT_HASH)) {
1917 value_element->source_ip_value = 0;
1918 value_element->dest_ip_value = htonl(value);
1919 value_element->source_port_value = 0;
1920 value_element->dest_port_value = 0;
1921 } else if ((service_flags & WCCP2_SERVICE_SRC_PORT_HASH) || (service_flags & WCCP2_SERVICE_SRC_PORT_ALT_HASH)) {
1922 value_element->source_ip_value = 0;
1923 value_element->dest_ip_value = 0;
1924 value_element->source_port_value = htons(value);
1925 value_element->dest_port_value = 0;
1926 } else if ((service_flags & WCCP2_SERVICE_DST_PORT_HASH) || (service_flags & WCCP2_SERVICE_DST_PORT_ALT_HASH)) {
1927 value_element->source_ip_value = 0;
1928 value_element->dest_ip_value = 0;
1929 value_element->source_port_value = 0;
1930 value_element->dest_port_value = htons(value);
1931 } else {
1932 fatalf("Unknown service hash method\n");
1933 }
1934
1935 value_element->cache_ip = cache_list_ptr->cache_ip;
1936
1937 offset += sizeof(struct wccp2_value_element_t);
14942edd 1938 ++value;
b7d249f9 1939
1940 /* Assign the next value to the next cache */
25e26086 1941
b7d249f9 1942 if ((cache_list_ptr->next) && (cache_list_ptr->next->next))
1943 cache_list_ptr = cache_list_ptr->next;
1944 else
1945 cache_list_ptr = &router_list_ptr->cache_list_head;
1946 }
0b0cfcf2 1947
b7d249f9 1948 /* Fill in length */
1949 alt_assignment_type_header->length = htons(offset - alt_assignment_offset);
1950
1951 break;
1952
1953 default:
1954 fatalf("Unknown Wccp2 assignment method\n");
1955 }
1956
1957 /* Fill in length */
0b0cfcf2 1958
b7d249f9 1959 assignment_header->length = htons(offset - assignment_offset);
0b0cfcf2 1960
1961 /* Fill in assignment key */
1962 assignment_key->master_ip = router_list_ptr->local_ip;
1963
1964 /* finish length */
1965
c898ced1 1966 main_header->length = htons(offset - sizeof(struct wccp2_message_header_t));
0b0cfcf2 1967
1968 /* set the destination address */
1969 router.sin_addr = router_list_ptr->router_sendto_address;
1970
1971 /* Security update, if needed */
1972
1973 if (service_list_ptr->wccp2_security_type == WCCP2_MD5_SECURITY) {
1974 wccp2_update_md5_security(service_list_ptr->wccp_password, (char *) security, wccp_packet, offset);
1975 }
1976
1977 if (ntohl(router_list_ptr->num_caches)) {
1978 /* send packet */
c1f55201 1979
182106f3
AJ
1980 /* FIXME INET6 : drop temp conversion */
1981 Ip::Address tmp_rtr(router);
1982
c1f55201 1983 if (wccp2_numrouters > 1) {
1984 comm_udp_sendto(theWccp2Connection,
cc192b50 1985 tmp_rtr,
c1f55201 1986 &wccp_packet,
1987 offset);
1988 } else {
182106f3 1989 errno = 0;
3af9db3f 1990 if (send(theWccp2Connection, &wccp_packet, offset, 0) < static_cast<int>(offset))
182106f3 1991 debugs(80, 2, "ERROR: failed to send WCCPv2 HERE_I_AM packet to " << tmp_rtr << " : " << xstrerror());
c1f55201 1992 }
0b0cfcf2 1993 }
182106f3 1994 safe_free(weight);
0b0cfcf2 1995 }
1996
1997 service_list_ptr = service_list_ptr->next;
1998 }
1999}
2000
0b0cfcf2 2001/*
2002 * Configuration option parsing code
2003 */
2004
e313ab0a
AJ
2005/**
2006 * Parse wccp2_return_method and wccp2_forwarding_method options
72e2b394 2007 * they can be '1' aka 'gre' or '2' aka 'l2'
e313ab0a
AJ
2008 * repesenting the integer numeric of the same.
2009 */
2010void
2011parse_wccp2_method(int *method)
2012{
2013 char *t;
2014
2015 /* Snarf the method */
2016 if ((t = strtok(NULL, w_space)) == NULL) {
21ec1b94 2017 debugs(80, DBG_CRITICAL, "wccp2_*_method: missing setting.");
e313ab0a
AJ
2018 self_destruct();
2019 }
2020
2021 /* update configuration if its valid */
451c4786 2022 if (strcmp(t, "gre") == 0 || strcmp(t, "1") == 0) {
21ec1b94 2023 *method = WCCP2_METHOD_GRE;
451c4786 2024 } else if (strcmp(t, "l2") == 0 || strcmp(t, "2") == 0) {
21ec1b94 2025 *method = WCCP2_METHOD_L2;
e313ab0a 2026 } else {
21ec1b94 2027 debugs(80, DBG_CRITICAL, "wccp2_*_method: unknown setting, got " << t );
e313ab0a
AJ
2028 self_destruct();
2029 }
2030}
2031
2032void
2033dump_wccp2_method(StoreEntry * e, const char *label, int v)
2034{
26ac0430 2035 switch (v) {
451c4786
AJ
2036 case WCCP2_METHOD_GRE:
2037 storeAppendPrintf(e, "%s gre\n", label);
e313ab0a 2038 break;
451c4786
AJ
2039 case WCCP2_METHOD_L2:
2040 storeAppendPrintf(e, "%s l2\n", label);
e313ab0a
AJ
2041 break;
2042 default:
21ec1b94 2043 debugs(80, DBG_CRITICAL, "FATAL: WCCPv2 configured method (" << v << ") is not valid.");
e313ab0a
AJ
2044 self_destruct();
2045 }
2046}
2047
2048void
2049free_wccp2_method(int *v)
2050{ }
2051
451c4786
AJ
2052/**
2053 * Parse wccp2_assignment_method option
2054 * they can be '1' aka 'hash' or '2' aka 'mask'
2055 * repesenting the integer numeric of the same.
2056 */
2057void
72e2b394 2058parse_wccp2_amethod(int *method)
451c4786
AJ
2059{
2060 char *t;
2061
2062 /* Snarf the method */
2063 if ((t = strtok(NULL, w_space)) == NULL) {
2064 debugs(80, DBG_CRITICAL, "wccp2_assignment_method: missing setting.");
2065 self_destruct();
2066 }
2067
2068 /* update configuration if its valid */
2069 if (strcmp(t, "hash") == 0 || strcmp(t, "1") == 0) {
2070 *method = WCCP2_ASSIGNMENT_METHOD_HASH;
2071 } else if (strcmp(t, "mask") == 0 || strcmp(t, "2") == 0) {
2072 *method = WCCP2_ASSIGNMENT_METHOD_MASK;
2073 } else {
2074 debugs(80, DBG_CRITICAL, "wccp2_assignment_method: unknown setting, got " << t );
2075 self_destruct();
2076 }
2077}
2078
2079void
2080dump_wccp2_amethod(StoreEntry * e, const char *label, int v)
2081{
26ac0430 2082 switch (v) {
451c4786
AJ
2083 case WCCP2_ASSIGNMENT_METHOD_HASH:
2084 storeAppendPrintf(e, "%s hash\n", label);
2085 break;
2086 case WCCP2_ASSIGNMENT_METHOD_MASK:
2087 storeAppendPrintf(e, "%s mask\n", label);
2088 break;
2089 default:
2090 debugs(80, DBG_CRITICAL, "FATAL: WCCPv2 configured " << label << " (" << v << ") is not valid.");
2091 self_destruct();
2092 }
2093}
2094
2095void
2096free_wccp2_amethod(int *v)
2097{ }
2098
0b0cfcf2 2099/*
2100 * Format:
2101 *
2102 * wccp2_service {standard|dynamic} {id} (password=password)
2103 */
2104void
2105parse_wccp2_service(void *v)
2106{
2107 char *t;
2108 int service = 0;
2109 int service_id = 0;
2110 int security_type = WCCP2_NO_SECURITY;
2111 char wccp_password[WCCP2_PASSWORD_LEN + 1];
2112
2113 if (wccp2_connected == 1) {
e0236918 2114 debugs(80, DBG_IMPORTANT, "WCCPv2: Somehow reparsing the configuration without having shut down WCCP! Try reloading squid again.");
0b0cfcf2 2115 return;
2116 }
2117
2118 /* Snarf the type */
2119 if ((t = strtok(NULL, w_space)) == NULL) {
fa84c01d 2120 debugs(80, DBG_CRITICAL, "wccp2ParseServiceInfo: missing service info type (standard|dynamic)");
0b0cfcf2 2121 self_destruct();
2122 }
2123
2124 if (strcmp(t, "standard") == 0) {
2125 service = WCCP2_SERVICE_STANDARD;
2126 } else if (strcmp(t, "dynamic") == 0) {
2127 service = WCCP2_SERVICE_DYNAMIC;
2128 } else {
fa84c01d 2129 debugs(80, DBG_CRITICAL, "wccp2ParseServiceInfo: bad service info type (expected standard|dynamic, got " << t << ")");
0b0cfcf2 2130 self_destruct();
2131 }
2132
2133 /* Snarf the ID */
2134 service_id = GetInteger();
2135
2136 if (service_id < 0 || service_id > 255) {
30c48b1a 2137 debugs(80, DBG_CRITICAL, "ERROR: invalid WCCP service id " << service_id << " (must be between 0 .. 255)");
0b0cfcf2 2138 self_destruct();
2139 }
2140
2141 memset(wccp_password, 0, sizeof(wccp_password));
2142 /* Handle password, if any */
2143
2144 if ((t = strtok(NULL, w_space)) != NULL) {
2145 if (strncmp(t, "password=", 9) == 0) {
2146 security_type = WCCP2_MD5_SECURITY;
2147 strncpy(wccp_password, t + 9, WCCP2_PASSWORD_LEN);
2148 }
2149 }
2150
2151 /* Create a placeholder service record */
2152 wccp2_add_service_list(service, service_id, 0, 0, 0, empty_portlist, security_type, wccp_password);
2153}
2154
2155void
2156dump_wccp2_service(StoreEntry * e, const char *label, void *v)
2157{
2158
2159 struct wccp2_service_list_t *srv;
2160 srv = wccp2_service_list_head;
2161
2162 while (srv != NULL) {
bf8fe701 2163 debugs(80, 3, "dump_wccp2_service: id " << srv->info.service_id << ", type " << srv->info.service);
0b0cfcf2 2164 storeAppendPrintf(e, "%s %s %d", label,
2165 (srv->info.service == WCCP2_SERVICE_DYNAMIC) ? "dynamic" : "standard",
2166 srv->info.service_id);
2167
2168 if (srv->wccp2_security_type == WCCP2_MD5_SECURITY) {
2169 storeAppendPrintf(e, " %s", srv->wccp_password);
2170 }
2171
c208fd0e 2172 storeAppendPrintf(e, "\n");
2173
0b0cfcf2 2174 srv = srv->next;
2175 }
2176}
2177
2178void
2179free_wccp2_service(void *v)
2180{}
2181
2182int
2183check_null_wccp2_service(void *v)
2184{
2185 return !wccp2_service_list_head;
2186}
2187
2188/*
2189 * Format:
2190 *
2191 * wccp2_service_info {id} stuff..
2192 *
2193 * Where stuff is:
2194 *
2195 * + flags=flag,flag,flag..
2196 * + proto=protocol (tcp|udp)
2197 * + ports=port,port,port (up to a max of 8)
2198 * + priority=priority (0->255)
2199 *
2200 * The flags here are:
2201 * src_ip_hash, dst_ip_hash, source_port_hash, dst_port_hash, ports_defined,
2202 * ports_source, src_ip_alt_hash, dst_ip_alt_hash, src_port_alt_hash, dst_port_alt_hash
2203 */
2204static int
2205parse_wccp2_service_flags(char *flags)
2206{
2207 char *tmp, *tmp2;
2208 char *flag;
2209 int retflag = 0;
2210
2211 if (!flags) {
2212 return 0;
2213 }
2214
2215 tmp = xstrdup(flags);
2216 tmp2 = tmp;
2217
2218 flag = strsep(&tmp2, ",");
2219
2220 while (flag) {
2221 if (strcmp(flag, "src_ip_hash") == 0) {
2222 retflag |= WCCP2_SERVICE_SRC_IP_HASH;
2223 } else if (strcmp(flag, "dst_ip_hash") == 0) {
2224 retflag |= WCCP2_SERVICE_DST_IP_HASH;
2225 } else if (strcmp(flag, "source_port_hash") == 0) {
b7d249f9 2226 retflag |= WCCP2_SERVICE_SRC_PORT_HASH;
0b0cfcf2 2227 } else if (strcmp(flag, "dst_port_hash") == 0) {
2228 retflag |= WCCP2_SERVICE_DST_PORT_HASH;
2229 } else if (strcmp(flag, "ports_source") == 0) {
2230 retflag |= WCCP2_SERVICE_PORTS_SOURCE;
2231 } else if (strcmp(flag, "src_ip_alt_hash") == 0) {
2232 retflag |= WCCP2_SERVICE_SRC_IP_ALT_HASH;
2233 } else if (strcmp(flag, "dst_ip_alt_hash") == 0) {
2234 retflag |= WCCP2_SERVICE_DST_IP_ALT_HASH;
2235 } else if (strcmp(flag, "src_port_alt_hash") == 0) {
2236 retflag |= WCCP2_SERVICE_SRC_PORT_ALT_HASH;
2237 } else if (strcmp(flag, "dst_port_alt_hash") == 0) {
2238 retflag |= WCCP2_SERVICE_DST_PORT_ALT_HASH;
2239 } else {
2240 fatalf("Unknown wccp2 service flag: %s\n", flag);
2241 }
2242
2243 flag = strsep(&tmp2, ",");
2244 }
2245
2246 xfree(tmp);
2247 return retflag;
2248}
2249
2250static void
2251parse_wccp2_service_ports(char *options, int portlist[])
2252{
2253 int i = 0;
2254 int p;
54a063a2 2255 char *tmp, *tmp2, *port;
0b0cfcf2 2256
2257 if (!options) {
2258 return;
2259 }
2260
2261 tmp = xstrdup(options);
2262 tmp2 = tmp;
2263
2264 port = strsep(&tmp2, ",");
2265
2266 while (port && i < WCCP2_NUMPORTS) {
54a063a2 2267 p = xatoi(port);
0b0cfcf2 2268
2269 if (p < 1 || p > 65535) {
2270 fatalf("parse_wccp2_service_ports: port value '%s' isn't valid (1..65535)\n", port);
2271 }
2272
2273 portlist[i] = p;
14942edd 2274 ++i;
0b0cfcf2 2275 port = strsep(&tmp2, ",");
2276 }
2277
5355e54f 2278 if (i == WCCP2_NUMPORTS && port) {
0b0cfcf2 2279 fatalf("parse_wccp2_service_ports: too many ports (maximum: 8) in list '%s'\n", options);
2280 }
2281
2282 xfree(tmp);
2283}
2284
2285void
2286parse_wccp2_service_info(void *v)
2287{
2288 char *t, *end;
2289 int service_id = 0;
2290 int flags = 0;
2291 int portlist[WCCP2_NUMPORTS];
2292 int protocol = -1; /* IPPROTO_TCP | IPPROTO_UDP */
2293
2294 struct wccp2_service_list_t *srv;
2295 int priority = -1;
2296
2297 if (wccp2_connected == 1) {
e0236918 2298 debugs(80, DBG_IMPORTANT, "WCCPv2: Somehow reparsing the configuration without having shut down WCCP! Try reloading squid again.");
0b0cfcf2 2299 return;
2300 }
2301
bf8fe701 2302 debugs(80, 5, "parse_wccp2_service_info: called");
a302ad3c 2303 memset(portlist, 0, sizeof(portlist));
0b0cfcf2 2304 /* First argument: id */
2305 service_id = GetInteger();
2306
2307 if (service_id < 0 || service_id > 255) {
30c48b1a 2308 debugs(80, DBG_CRITICAL, "ERROR: invalid WCCP service id " << service_id << " (must be between 0 .. 255)");
0b0cfcf2 2309 self_destruct();
2310 }
2311
2312 /* Next: find the (hopefully!) existing service */
2313 srv = wccp2_get_service_by_id(WCCP2_SERVICE_DYNAMIC, service_id);
2314
2315 if (srv == NULL) {
2316 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);
2317 }
2318
2319 /* Next: loop until we don't have any more tokens */
2320 while ((t = strtok(NULL, w_space)) != NULL) {
2321 if (strncmp(t, "flags=", 6) == 0) {
2322 /* XXX eww, string pointer math */
2323 flags = parse_wccp2_service_flags(t + 6);
2324 } else if (strncmp(t, "ports=", 6) == 0) {
2325 parse_wccp2_service_ports(t + 6, portlist);
2326 flags |= WCCP2_SERVICE_PORTS_DEFINED;
2327 } else if (strncmp(t, "protocol=tcp", 12) == 0) {
2328 protocol = IPPROTO_TCP;
2329 } else if (strncmp(t, "protocol=udp", 12) == 0) {
2330 protocol = IPPROTO_UDP;
2331 } else if (strncmp(t, "protocol=", 9) == 0) {
2332 fatalf("parse_wccp2_service_info: id %d: unknown protocol (%s) - must be tcp or udp!\n", service_id, t);
2333 } else if (strncmp(t, "priority=", 9) == 0) {
2334 priority = strtol(t + 9, &end, 0);
2335
2336 if (priority < 0 || priority > 255) {
2337 fatalf("parse_wccp2_service_info: id %d: %s out of range (0..255)!\n", service_id, t);
2338 }
2339 } else {
2340 fatalf("parse_wccp2_service_info: id %d: unknown option '%s'\n", service_id, t);
2341 }
2342 }
2343
2344 /* Check everything is set */
2345 if (priority == -1) {
2346 fatalf("parse_wccp2_service_info: service %d: no priority defined (valid: 0..255)!\n", service_id);
2347 }
2348
2349 if (protocol == -1) {
2350 fatalf("parse_wccp2_service_info: service %d: no protocol defined (valid: tcp or udp)!\n", service_id);
2351 }
2352
2353 if (!(flags & WCCP2_SERVICE_PORTS_DEFINED)) {
2354 fatalf("parse_wccp2_service_info: service %d: no ports defined!\n", service_id);
2355 }
2356
2357 /* rightio! now we can update */
2358 wccp2_update_service(srv, WCCP2_SERVICE_DYNAMIC, service_id, priority,
2359 protocol, flags, portlist);
2360
2361 /* Done! */
2362}
2363
2364void
2365dump_wccp2_service_info(StoreEntry * e, const char *label, void *v)
2366{
2367 char comma;
2368
2369 struct wccp2_service_list_t *srv;
2370 int flags;
2371 srv = wccp2_service_list_head;
2372
2373 while (srv != NULL) {
bf8fe701 2374 debugs(80, 3, "dump_wccp2_service_info: id " << srv->info.service_id << " (type " << srv->info.service << ")");
0b0cfcf2 2375
2376 /* We don't need to spit out information for standard services */
2377
2378 if (srv->info.service == WCCP2_SERVICE_STANDARD) {
bf8fe701 2379 debugs(80, 3, "dump_wccp2_service_info: id " << srv->info.service_id << ": standard service, not dumping info");
2380
0b0cfcf2 2381 /* XXX eww */
2382 srv = srv->next;
2383 continue;
2384 }
2385
2386 storeAppendPrintf(e, "%s %d", label, srv->info.service_id);
2387
2388 /* priority */
2389 storeAppendPrintf(e, " priority=%d", srv->info.service_priority);
2390
2391 /* flags */
2392 flags = ntohl(srv->info.service_flags);
2393
2394 if (flags != 0) {
2395 comma = 0;
2396 storeAppendPrintf(e, " flags=");
2397
2398 if (flags & WCCP2_SERVICE_SRC_IP_HASH) {
2399 storeAppendPrintf(e, "%ssrc_ip_hash", comma ? "," : "");
2400 comma = 1;
2401 }
2402
2403 if (flags & WCCP2_SERVICE_DST_IP_HASH) {
2404 storeAppendPrintf(e, "%sdst_ip_hash", comma ? "," : "");
2405 comma = 1;
2406 }
2407
b7d249f9 2408 if (flags & WCCP2_SERVICE_SRC_PORT_HASH) {
0b0cfcf2 2409 storeAppendPrintf(e, "%ssource_port_hash", comma ? "," : "");
2410 comma = 1;
2411 }
2412
2413 if (flags & WCCP2_SERVICE_DST_PORT_HASH) {
2414 storeAppendPrintf(e, "%sdst_port_hash", comma ? "," : "");
2415 comma = 1;
2416 }
2417
2418 if (flags & WCCP2_SERVICE_PORTS_DEFINED) {
2419 storeAppendPrintf(e, "%sports_defined", comma ? "," : "");
2420 comma = 1;
2421 }
2422
2423 if (flags & WCCP2_SERVICE_PORTS_SOURCE) {
2424 storeAppendPrintf(e, "%sports_source", comma ? "," : "");
2425 comma = 1;
2426 }
2427
2428 if (flags & WCCP2_SERVICE_SRC_IP_ALT_HASH) {
2429 storeAppendPrintf(e, "%ssrc_ip_alt_hash", comma ? "," : "");
2430 comma = 1;
2431 }
2432
2433 if (flags & WCCP2_SERVICE_DST_IP_ALT_HASH) {
2434 storeAppendPrintf(e, "%ssrc_ip_alt_hash", comma ? "," : "");
2435 comma = 1;
2436 }
2437
2438 if (flags & WCCP2_SERVICE_SRC_PORT_ALT_HASH) {
2439 storeAppendPrintf(e, "%ssrc_port_alt_hash", comma ? "," : "");
2440 comma = 1;
2441 }
2442
2443 if (flags & WCCP2_SERVICE_DST_PORT_ALT_HASH) {
2444 storeAppendPrintf(e, "%sdst_port_alt_hash", comma ? "," : "");
2445 comma = 1;
2446 }
2447 }
2448
2449 /* ports */
2450 comma = 0;
2451
2452 if (srv->info.port0 != 0) {
2453 storeAppendPrintf(e, "%s%d", comma ? "," : " ports=", ntohs(srv->info.port0));
2454 comma = 1;
2455 }
2456
2457 if (srv->info.port1 != 0) {
2458 storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port1));
2459 comma = 1;
2460 }
2461
2462 if (srv->info.port2 != 0) {
2463 storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port2));
2464 comma = 1;
2465 }
2466
2467 if (srv->info.port3 != 0) {
2468 storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port3));
2469 comma = 1;
2470 }
2471
2472 if (srv->info.port4 != 0) {
2473 storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port4));
2474 comma = 1;
2475 }
2476
2477 if (srv->info.port5 != 0) {
2478 storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port5));
2479 comma = 1;
2480 }
2481
2482 if (srv->info.port6 != 0) {
2483 storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port6));
2484 comma = 1;
2485 }
2486
2487 if (srv->info.port7 != 0) {
2488 storeAppendPrintf(e, "%s%d", comma ? "," : "ports=", ntohs(srv->info.port7));
2489 comma = 1;
2490 }
2491
2492 /* protocol */
2493 storeAppendPrintf(e, " protocol=%s", (srv->info.service_protocol == IPPROTO_TCP) ? "tcp" : "udp");
2494
2495 storeAppendPrintf(e, "\n");
2496
2497 srv = srv->next;
2498 }
2499}
2500
264096e2 2501/* Sort the cache list by doing a "selection sort" by IP address */
2502static void
2503wccp2SortCacheList(struct wccp2_cache_list_t *head)
2504{
2505 struct wccp2_cache_list_t tmp;
2506 struct wccp2_cache_list_t *this_item;
2507 struct wccp2_cache_list_t *find_item;
2508 struct wccp2_cache_list_t *next_lowest;
2509
2510 /* Go through each position in the list one at a time */
2511 for (this_item = head; this_item->next; this_item = this_item->next) {
26ac0430
AJ
2512 /* Find the item with the lowest IP */
2513 next_lowest = this_item;
2514
2515 for (find_item = this_item; find_item->next; find_item = find_item->next) {
2516 if (find_item->cache_ip.s_addr < next_lowest->cache_ip.s_addr) {
2517 next_lowest = find_item;
2518 }
2519 }
2520 /* Swap if we need to */
2521 if (next_lowest != this_item) {
2522 /* First make a copy of the current item */
2523 memcpy(&tmp, this_item, sizeof(struct wccp2_cache_list_t));
2524
2525 /* Next update the pointers to maintain the linked list */
2526 tmp.next = next_lowest->next;
2527 next_lowest->next = this_item->next;
2528
2529 /* Finally copy the updated items to their correct location */
2530 memcpy(this_item, next_lowest, sizeof(struct wccp2_cache_list_t));
2531 memcpy(next_lowest, &tmp, sizeof(struct wccp2_cache_list_t));
2532 }
264096e2 2533 }
2534}
2535
0b0cfcf2 2536void
2537free_wccp2_service_info(void *v)
2538{}
2539
2540#endif /* USE_WCCPv2 */