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