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