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