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