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