f771cbdc22a17a58482277688b30ec0876c2c615
[obnox/wireshark/wip.git] / epan / dissectors / packet-wccp.c
1 /* packet-wccp.c
2  * Routines for Web Cache Coordination Protocol dissection
3  * Jerry Talkington <jtalkington@users.sourceforge.net>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <string.h>
31 #include <glib.h>
32 #include <epan/packet.h>
33 #include <epan/strutil.h>
34 #include <epan/emem.h>
35 #include "packet-wccp.h"
36
37 static int proto_wccp = -1;
38 static int hf_wccp_message_type = -1;   /* the message type */
39 static int hf_wccp_version = -1;        /* protocol version */
40 static int hf_hash_revision = -1;       /* the version of the hash */
41 static int hf_change_num = -1;          /* change number */
42 static int hf_recvd_id = -1;
43 static int hf_cache_ip = -1;
44
45 static gint ett_wccp = -1;
46 static gint ett_cache_count = -1;
47 static gint ett_buckets = -1;
48 static gint ett_flags = -1;
49 static gint ett_cache_info = -1;
50 static gint ett_security_info = -1;
51 static gint ett_service_info = -1;
52 static gint ett_service_flags = -1;
53 static gint ett_router_identity_element = -1;
54 static gint ett_router_identity_info = -1;
55 static gint ett_wc_identity_element = -1;
56 static gint ett_wc_identity_info = -1;
57 static gint ett_router_view_info = -1;
58 static gint ett_wc_view_info = -1;
59 static gint ett_router_assignment_element = -1;
60 static gint ett_router_assignment_info = -1;
61 static gint ett_query_info = -1;
62 static gint ett_capabilities_info = -1;
63 static gint ett_capability_element = -1;
64 static gint ett_capability_forwarding_method = -1;
65 static gint ett_capability_assignment_method = -1;
66 static gint ett_capability_return_method = -1;
67 static gint ett_alt_assignment_info = -1;
68 static gint ett_mv_set_element = -1;
69 static gint ett_value_element = -1;
70 static gint ett_unknown_info = -1;
71
72 /*
73  * At
74  *
75  *      http://www.alternic.org/drafts/drafts-f-g/draft-forster-wrec-wccp-v1-00.html
76  *
77  * is a copy of the now-expired Internet-Draft for WCCP 1.0.
78  *
79  * At
80  *
81  *      http://tools.ietf.org/id/draft-wilson-wrec-wccp-v2-01.txt
82  *
83  * is an Internet-Draft for WCCP 2.0.
84  */
85
86 /* This is NOT IANA assigned */
87 #define UDP_PORT_WCCP   2048
88
89 #define WCCPv1                  4
90 #define WCCPv2                  0x0200
91 #define WCCP_HERE_I_AM          7
92 #define WCCP_I_SEE_YOU          8
93 #define WCCP_ASSIGN_BUCKET      9
94 #define WCCP2_HERE_I_AM         10
95 #define WCCP2_I_SEE_YOU         11
96 #define WCCP2_REDIRECT_ASSIGN   12
97 #define WCCP2_REMOVAL_QUERY     13
98
99 static const value_string wccp_type_vals[] = {
100     { WCCP_HERE_I_AM,        "1.0 Here I am" },
101     { WCCP_I_SEE_YOU,        "1.0 I see you" },
102     { WCCP_ASSIGN_BUCKET,    "1.0 Assign bucket" },
103     { WCCP2_HERE_I_AM,       "2.0 Here I am" },
104     { WCCP2_I_SEE_YOU,       "2.0 I see you" },
105     { WCCP2_REDIRECT_ASSIGN, "2.0 Redirect assign" },
106     { WCCP2_REMOVAL_QUERY,   "2.0 Removal query" },
107     { 0,                     NULL }
108 };
109
110 static const value_string wccp_version_val[] = {
111         { WCCPv1, "1"},
112         { WCCPv2, "2"},
113         { 0, NULL}
114 };
115
116 #define HASH_INFO_SIZE  (4*(1+8+1))
117
118 #define WCCP_U_FLAG     0x80000000
119
120 #define WCCP2_SECURITY_INFO             0
121 #define WCCP2_SERVICE_INFO              1
122 #define WCCP2_ROUTER_ID_INFO            2
123 #define WCCP2_WC_ID_INFO                3
124 #define WCCP2_RTR_VIEW_INFO             4
125 #define WCCP2_WC_VIEW_INFO              5
126 #define WCCP2_REDIRECT_ASSIGNMENT       6
127 #define WCCP2_QUERY_INFO                7
128 #define WCCP2_CAPABILITIES_INFO         8
129 #define WCCP2_ALT_ASSIGNMENT            13
130 #define WCCP2_ASSIGN_MAP                14
131 #define WCCP2_COMMAND_EXTENSION         15
132
133 static const value_string info_type_vals[] = {
134         { WCCP2_SECURITY_INFO,       "Security Info" },
135         { WCCP2_SERVICE_INFO,        "Service Info" },
136         { WCCP2_ROUTER_ID_INFO,      "Router Identity Info" },
137         { WCCP2_WC_ID_INFO,          "Web-Cache Identity Info" },
138         { WCCP2_RTR_VIEW_INFO,       "Router View Info" },
139         { WCCP2_WC_VIEW_INFO,        "Web-Cache View Info" },
140         { WCCP2_REDIRECT_ASSIGNMENT, "Assignment Info" },
141         { WCCP2_QUERY_INFO,          "Query Info" },
142         { WCCP2_CAPABILITIES_INFO,   "Capabilities Info" },
143         { WCCP2_ALT_ASSIGNMENT,      "Alternate Assignment" },
144         { WCCP2_ASSIGN_MAP,          "Assignment Map" },
145         { WCCP2_COMMAND_EXTENSION,   "Command Extension" },
146         { 0,                         NULL }
147 };
148
149 const value_string service_id_vals[] = {
150     { 0x00, "HTTP" },
151     { 0,    NULL }
152 };
153
154 typedef struct capability_flag {
155         guint32 value;
156         const char *short_name;
157         const char *long_name;
158 } capability_flag;
159
160 static void dissect_hash_data(tvbuff_t *tvb, int offset,
161     proto_tree *wccp_tree);
162 static void dissect_web_cache_list_entry(tvbuff_t *tvb, int offset,
163     int index, proto_tree *wccp_tree);
164 static int wccp_bucket_info(guint8 bucket_info, proto_tree *bucket_tree,
165     guint32 start, tvbuff_t *tvb, int offset);
166 static gchar *bucket_name(guint8 bucket);
167 static guint16 dissect_wccp2_header(tvbuff_t *tvb, int offset,
168     proto_tree *wccp_tree);
169 static void dissect_wccp2_info(tvbuff_t *tvb, int offset, guint16 length,
170     proto_tree *wccp_tree);
171 static gboolean dissect_wccp2_security_info(tvbuff_t *tvb, int offset,
172     int length, proto_tree *info_tree);
173 static gboolean dissect_wccp2_service_info(tvbuff_t *tvb, int offset,
174     int length, proto_tree *info_tree);
175 static gboolean dissect_wccp2_router_identity_info(tvbuff_t *tvb,
176     int offset, int length, proto_tree *info_tree);
177 static gboolean dissect_wccp2_wc_identity_info(tvbuff_t *tvb, int offset,
178     int length, proto_tree *info_tree);
179 static gboolean dissect_wccp2_router_view_info(tvbuff_t *tvb, int offset,
180     int length, proto_tree *info_tree);
181 static gboolean dissect_wccp2_wc_view_info(tvbuff_t *tvb, int offset,
182     int length, proto_tree *info_tree);
183 static gboolean dissect_wccp2_assignment_info(tvbuff_t *tvb, int offset,
184     int length, proto_tree *info_tree);
185 static gboolean dissect_wccp2_router_query_info(tvbuff_t *tvb, int offset,
186     int length, proto_tree *info_tree);
187 static gboolean dissect_wccp2_capability_info(tvbuff_t *tvb, int offset,
188     int length, proto_tree *info_tree);
189 static void dissect_32_bit_capability_flags(tvbuff_t *tvb, int curr_offset,
190     guint16 capability_val_len, gint ett, const capability_flag *flags,
191     proto_tree *element_tree);
192 static gboolean dissect_wccp2_alt_assignment_info(tvbuff_t *tvb, int offset,
193     int length, proto_tree *info_tree);
194
195 static int
196 dissect_wccp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
197 {
198         int offset = 0;
199         proto_tree *wccp_tree = NULL;
200         proto_item *wccp_tree_item;
201         guint32 wccp_message_type;
202         guint16 length;
203         guint32 cache_count;
204         guint32 ipaddr;
205         guint i;
206
207         wccp_message_type = tvb_get_ntohl(tvb, offset);
208
209         /* Check if this is really a WCCP message */
210         if (match_strval(wccp_message_type, wccp_type_vals) == NULL)
211                 return 0;
212
213         if(check_col(pinfo->cinfo, COL_PROTOCOL)) {
214                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "WCCP");
215         }
216         if(check_col(pinfo->cinfo, COL_INFO)) {
217                 col_clear(pinfo->cinfo, COL_INFO);
218         }
219
220
221         if(check_col(pinfo->cinfo, COL_INFO)) {
222                 col_add_str(pinfo->cinfo, COL_INFO, val_to_str(wccp_message_type,
223                     wccp_type_vals, "Unknown WCCP message (%u)"));
224         }
225
226         if(tree != NULL) {
227                 wccp_tree_item = proto_tree_add_item(tree, proto_wccp, tvb, offset,
228                     -1, FALSE);
229                 wccp_tree = proto_item_add_subtree(wccp_tree_item, ett_wccp);
230
231                 proto_tree_add_uint(wccp_tree, hf_wccp_message_type, tvb, offset,
232                     sizeof(wccp_message_type), wccp_message_type);
233                 offset += sizeof(wccp_message_type);
234
235                 switch (wccp_message_type) {
236
237                 case WCCP_HERE_I_AM:
238                         proto_tree_add_item(wccp_tree, hf_wccp_version, tvb,
239                             offset, 4, FALSE);
240                         offset += 4;
241                         dissect_hash_data(tvb, offset, wccp_tree);
242                         offset += HASH_INFO_SIZE;
243                         proto_tree_add_item(wccp_tree, hf_recvd_id, tvb, offset,
244                             4, FALSE);
245                         offset += 4;
246                         break;
247
248                 case WCCP_I_SEE_YOU:
249                         proto_tree_add_item(wccp_tree, hf_wccp_version, tvb,
250                             offset, 4, FALSE);
251                         offset += 4;
252                         proto_tree_add_item(wccp_tree, hf_change_num, tvb, offset,
253                             4, FALSE);
254                         offset += 4;
255                         proto_tree_add_item(wccp_tree, hf_recvd_id, tvb, offset,
256                             4, FALSE);
257                         offset += 4;
258                         cache_count = tvb_get_ntohl(tvb, offset);
259                         proto_tree_add_text(wccp_tree, tvb, offset, 4,
260                             "Number of Web Caches: %u", cache_count);
261                         offset += 4;
262                         for (i = 0; i < cache_count; i++) {
263                                 dissect_web_cache_list_entry(tvb, offset, i,
264                                     wccp_tree);
265                                 offset += 4 + HASH_INFO_SIZE;
266                         }
267                         break;
268
269                 case WCCP_ASSIGN_BUCKET:
270                         /*
271                          * This hasn't been tested, since I don't have any
272                          * traces with this in it.
273                          *
274                          * The V1 spec claims that this does, indeed,
275                          * have a Received ID field after the type,
276                          * rather than a Version field.
277                          */
278                         proto_tree_add_item(wccp_tree, hf_recvd_id, tvb, offset,
279                             4, FALSE);
280                         offset += 4;
281                         cache_count = tvb_get_ntohl(tvb, offset);
282                         proto_tree_add_text(wccp_tree, tvb, offset, 4,
283                             "Number of Web Caches: %u", cache_count);
284                         offset += 4;
285                         for (i = 0; i < cache_count; i++) {
286                                 ipaddr = tvb_get_ipv4(tvb, offset);
287                                 proto_tree_add_ipv4_format(wccp_tree,
288                                     hf_cache_ip, tvb, offset, 4,
289                                     ipaddr,
290                                     "Web Cache %d IP Address: %s", i,
291                                     ip_to_str((guint8 *)&ipaddr));
292                                 offset += 4;
293                         }
294                         for (i = 0; i < 256; i += 4) {
295                                 proto_tree_add_text(wccp_tree, tvb, offset, 4,
296                                     "Buckets %d - %d: %10s %10s %10s %10s",
297                                     i, i + 3,
298                                     bucket_name(tvb_get_guint8(tvb, offset)),
299                                     bucket_name(tvb_get_guint8(tvb, offset+1)),
300                                     bucket_name(tvb_get_guint8(tvb, offset+2)),
301                                     bucket_name(tvb_get_guint8(tvb, offset+3)));
302                                 offset += 4;
303                         }
304                         break;
305
306                 case WCCP2_HERE_I_AM:
307                 case WCCP2_I_SEE_YOU:
308                 case WCCP2_REMOVAL_QUERY:
309                 case WCCP2_REDIRECT_ASSIGN:
310                 default:        /* assume unknown packets are v2 */
311                         length = dissect_wccp2_header(tvb, offset, wccp_tree);
312                         offset += 4;
313                         dissect_wccp2_info(tvb, offset, length, wccp_tree);
314                         break;
315                 }
316         }
317
318         return(tvb_length(tvb));
319 }
320
321 static void
322 dissect_hash_data(tvbuff_t *tvb, int offset, proto_tree *wccp_tree)
323 {
324         proto_item *bucket_item;
325         proto_tree *bucket_tree;
326         proto_item *tf;
327         proto_tree *field_tree;
328         int i;
329         guint8 bucket_info;
330         int n;
331         guint32 flags;
332
333         proto_tree_add_item(wccp_tree, hf_hash_revision, tvb, offset, 4,
334             FALSE);
335         offset += 4;
336
337         bucket_item = proto_tree_add_text(wccp_tree, tvb, offset, 32,
338             "Hash information");
339         bucket_tree = proto_item_add_subtree(bucket_item, ett_buckets);
340
341         for (i = 0, n = 0; i < 32; i++) {
342                 bucket_info = tvb_get_guint8(tvb, offset);
343                 n = wccp_bucket_info(bucket_info, bucket_tree, n, tvb, offset);
344                 offset += 1;
345         }
346         flags = tvb_get_ntohl(tvb, offset);
347         tf = proto_tree_add_text(wccp_tree, tvb, offset, 4,
348             "Flags: 0x%08X (%s)", flags,
349             ((flags & WCCP_U_FLAG) ?
350               "Hash information is historical" :
351               "Hash information is current"));
352         field_tree = proto_item_add_subtree(tf, ett_flags);
353         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
354             decode_boolean_bitfield(flags, WCCP_U_FLAG,
355               sizeof (flags)*8,
356               "Hash information is historical",
357               "Hash information is current"));
358 }
359
360 static void
361 dissect_web_cache_list_entry(tvbuff_t *tvb, int offset, int index,
362     proto_tree *wccp_tree)
363 {
364         proto_item *tl;
365         proto_tree *list_entry_tree;
366
367         tl = proto_tree_add_text(wccp_tree, tvb, offset, 4 + HASH_INFO_SIZE,
368             "Web-Cache List Entry(%d)", index);
369         list_entry_tree = proto_item_add_subtree(tl, ett_cache_info);
370         proto_tree_add_item(list_entry_tree, hf_cache_ip, tvb, offset, 4,
371             FALSE);
372         dissect_hash_data(tvb, offset + 4, list_entry_tree);
373 }
374
375 /*
376  * wccp_bucket_info()
377  * takes an integer representing a "Hash Information" bitmap, and spits out
378  * the corresponding proto_tree entries, returning the next bucket number.
379  */
380 static int
381 wccp_bucket_info(guint8 bucket_info, proto_tree *bucket_tree, guint32 start,
382     tvbuff_t *tvb, int offset)
383 {
384         guint32 i;
385
386         for(i = 0; i < 8; i++) {
387                 proto_tree_add_text(bucket_tree, tvb, offset, sizeof(bucket_info), "Bucket %3d: %s", start, (bucket_info & 1<<i ? "Assigned" : "Not Assigned") );
388                 start++;
389         }
390         return(start);
391 }
392
393 static gchar *
394 bucket_name(guint8 bucket)
395 {
396         gchar *cur=NULL;
397
398         if (bucket == 0xff) {
399                 cur="Unassigned";
400         } else {
401                 cur=ep_alloc(10+1);
402                 g_snprintf(cur, 10+1, "%u", bucket);
403         }
404         return cur;
405 }
406
407 static guint16
408 dissect_wccp2_header(tvbuff_t *tvb, int offset, proto_tree *wccp_tree)
409 {
410         guint16 length;
411
412         proto_tree_add_item(wccp_tree, hf_wccp_version, tvb, offset, 2,
413             FALSE);
414         offset += 2;
415         length = tvb_get_ntohs(tvb, offset);
416         proto_tree_add_text(wccp_tree, tvb, offset, 2, "Length: %u",
417             length);
418         return length;
419 }
420
421 static void
422 dissect_wccp2_info(tvbuff_t *tvb, int offset, guint16 length,
423     proto_tree *wccp_tree)
424 {
425         guint16 type;
426         guint16 item_length;
427         proto_item *ti;
428         proto_tree *info_tree;
429         gint ett;
430         gboolean (*dissector)(tvbuff_t *, int, int, proto_tree *);
431
432         while (length != 0) {
433                 type = tvb_get_ntohs(tvb, offset);
434                 item_length = tvb_get_ntohs(tvb, offset+2);
435
436                 switch (type) {
437
438                 case WCCP2_SECURITY_INFO:
439                         ett = ett_security_info;
440                         dissector = dissect_wccp2_security_info;
441                         break;
442
443                 case WCCP2_SERVICE_INFO:
444                         ett = ett_service_info;
445                         dissector = dissect_wccp2_service_info;
446                         break;
447
448                 case WCCP2_ROUTER_ID_INFO:
449                         ett = ett_router_identity_info;
450                         dissector = dissect_wccp2_router_identity_info;
451                         break;
452
453                 case WCCP2_WC_ID_INFO:
454                         ett = ett_wc_identity_info;
455                         dissector = dissect_wccp2_wc_identity_info;
456                         break;
457
458                 case WCCP2_RTR_VIEW_INFO:
459                         ett = ett_router_view_info;
460                         dissector = dissect_wccp2_router_view_info;
461                         break;
462
463                 case WCCP2_WC_VIEW_INFO:
464                         ett = ett_wc_view_info;
465                         dissector = dissect_wccp2_wc_view_info;
466                         break;
467
468                 case WCCP2_REDIRECT_ASSIGNMENT:
469                         ett = ett_router_assignment_info;
470                         dissector = dissect_wccp2_assignment_info;
471                         break;
472
473                 case WCCP2_QUERY_INFO:
474                         ett = ett_query_info;
475                         dissector = dissect_wccp2_router_query_info;
476                         break;
477
478                 case WCCP2_CAPABILITIES_INFO:
479                         ett = ett_capabilities_info;
480                         dissector = dissect_wccp2_capability_info;
481                         break;
482
483                 case WCCP2_ALT_ASSIGNMENT:
484                         ett = ett_alt_assignment_info;
485                         dissector = dissect_wccp2_alt_assignment_info;
486                         break;
487
488                 case WCCP2_ASSIGN_MAP:
489                 case WCCP2_COMMAND_EXTENSION:
490                 default:
491                         ett = ett_unknown_info;
492                         dissector = NULL;
493                         break;
494                 }
495
496                 ti = proto_tree_add_text(wccp_tree, tvb, offset, item_length + 4, "%s",
497                     val_to_str(type, info_type_vals, "Unknown info type (%u)"));
498                 info_tree = proto_item_add_subtree(ti, ett);
499                 proto_tree_add_text(info_tree, tvb, offset, 2,
500                     "Type: %s",
501                     val_to_str(type, info_type_vals, "Unknown info type (%u)"));
502                 proto_tree_add_text(info_tree, tvb, offset+2, 2,
503                     "Length: %u", item_length);
504                 offset += 4;
505                 length -= 4;
506
507                 /*
508                  * XXX - pass in "length" and check for that as well.
509                  */
510                 if (dissector != NULL) {
511                         if (!(*dissector)(tvb, offset, item_length, info_tree))
512                                 return; /* ran out of data */
513                 } else {
514                         proto_tree_add_text(info_tree, tvb, offset, item_length,
515                             "Data: %u byte%s", item_length,
516                             plurality(item_length, "", "s"));
517                 }
518                 offset += item_length;
519                 length -= item_length;
520         }
521 }
522
523 #define SECURITY_INFO_LEN               4
524
525 #define WCCP2_NO_SECURITY               0
526 #define WCCP2_MD5_SECURITY              1
527
528 static gboolean
529 dissect_wccp2_security_info(tvbuff_t *tvb, int offset, int length,
530     proto_tree *info_tree)
531 {
532         guint32 security_option;
533
534         if (length < SECURITY_INFO_LEN) {
535                 proto_tree_add_text(info_tree, tvb, offset, 0,
536                     "Item length is %u, should be %u", length,
537                     SECURITY_INFO_LEN);
538                 return TRUE;
539         }
540
541         security_option = tvb_get_ntohl(tvb, offset);
542         switch (security_option) {
543
544         case WCCP2_NO_SECURITY:
545                 proto_tree_add_text(info_tree, tvb, offset, 4,
546                     "Security Option: None");
547                 break;
548
549         case WCCP2_MD5_SECURITY:
550                 proto_tree_add_text(info_tree, tvb, offset, 4,
551                     "Security Option: MD5");
552                 offset += 4;
553                 if (length > 4) {
554                         proto_tree_add_text(info_tree, tvb, offset,
555                             length - 4, "MD5 checksum: %s",
556                             tvb_bytes_to_str(tvb, offset, length - 4));
557                 }
558                 break;
559
560         default:
561                 proto_tree_add_text(info_tree, tvb, offset, 4,
562                     "Security Option: Unknown (%u)", security_option);
563                 break;
564         }
565         return TRUE;
566 }
567
568 #define SERVICE_INFO_LEN                (4+4+8*2)
569
570 #define WCCP2_SERVICE_STANDARD          0
571 #define WCCP2_SERVICE_DYNAMIC           1
572
573 /*
574  * Service flags.
575  */
576 #define WCCP2_SI_SRC_IP_HASH            0x0001
577 #define WCCP2_SI_DST_IP_HASH            0x0002
578 #define WCCP2_SI_SRC_PORT_HASH          0x0004
579 #define WCCP2_SI_DST_PORT_HASH          0x0008
580 #define WCCP2_SI_PORTS_DEFINED          0x0010
581 #define WCCP2_SI_PORTS_SOURCE           0x0020
582 #define WCCP2_SI_SRC_IP_ALT_HASH        0x0100
583 #define WCCP2_SI_DST_IP_ALT_HASH        0x0200
584 #define WCCP2_SI_SRC_PORT_ALT_HASH      0x0400
585 #define WCCP2_SI_DST_PORT_ALT_HASH      0x0800
586
587 static gboolean
588 dissect_wccp2_service_info(tvbuff_t *tvb, int offset, int length,
589     proto_tree *info_tree)
590 {
591         guint8 service_type;
592         guint32 flags;
593         proto_item *tf;
594         proto_tree *field_tree;
595         int i;
596
597         if (length != SERVICE_INFO_LEN) {
598                 proto_tree_add_text(info_tree, tvb, offset, 0,
599                     "Item length is %u, should be %u", length,
600                     SERVICE_INFO_LEN);
601                 return TRUE;
602         }
603
604         service_type = tvb_get_guint8(tvb, offset);
605         switch (service_type) {
606
607         case WCCP2_SERVICE_STANDARD:
608                 proto_tree_add_text(info_tree, tvb, offset, 1,
609                     "Service Type: Well-known service");
610                 proto_tree_add_text(info_tree, tvb, offset+1, 1,
611                     "Service ID: %s",
612                     val_to_str(tvb_get_guint8(tvb, offset+1), service_id_vals,
613                             "Unknown (0x%02X)"));
614
615                 break;
616
617         case WCCP2_SERVICE_DYNAMIC:
618                 proto_tree_add_text(info_tree, tvb, offset, 1,
619                     "Service Type: Dynamic service");
620                 proto_tree_add_text(info_tree, tvb, offset+1, 1,
621                     "Service ID: %s",
622                     val_to_str(tvb_get_guint8(tvb, offset+1), service_id_vals,
623                             "Unknown (0x%02X)"));
624                 proto_tree_add_text(info_tree, tvb, offset+2, 1,
625                     "Priority: %u", tvb_get_guint8(tvb, offset+2));
626                 /*
627                  * XXX - does "IP protocol identifier" mean this is a
628                  * protocol type of the sort you get in IP headers?
629                  * If so, we should get a table of those from the
630                  * IP dissector, and use that.
631                  */
632                 proto_tree_add_text(info_tree, tvb, offset+3, 1,
633                     "Protocol: %u", tvb_get_guint8(tvb, offset+3));     /* IP protocol identifier */
634                 break;
635
636         default:
637                 proto_tree_add_text(info_tree, tvb, offset, 1,
638                     "Service Type: Unknown (%u)", service_type);
639                 break;
640         }
641         offset += 4;
642
643         flags = tvb_get_ntohl(tvb, offset);
644         tf = proto_tree_add_text(info_tree, tvb, offset, 4,
645             "Flags: 0x%08X", flags);
646         field_tree = proto_item_add_subtree(tf, ett_service_flags);
647         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
648             decode_boolean_bitfield(flags, WCCP2_SI_SRC_IP_HASH,
649               sizeof (flags)*8,
650               "Use source IP address in primary hash",
651               "Don't use source IP address in primary hash"));
652         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
653             decode_boolean_bitfield(flags, WCCP2_SI_DST_IP_HASH,
654               sizeof (flags)*8,
655               "Use destination IP address in primary hash",
656               "Don't use destination IP address in primary hash"));
657         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
658             decode_boolean_bitfield(flags, WCCP2_SI_SRC_PORT_HASH,
659               sizeof (flags)*8,
660               "Use source port in primary hash",
661               "Don't use source port in primary hash"));
662         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
663             decode_boolean_bitfield(flags, WCCP2_SI_DST_PORT_HASH,
664               sizeof (flags)*8,
665               "Use destination port in primary hash",
666               "Don't use destination port in primary hash"));
667         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
668             decode_boolean_bitfield(flags, WCCP2_SI_PORTS_DEFINED,
669               sizeof (flags)*8,
670               "Ports defined",
671               "Ports not defined"));
672         if (flags & WCCP2_SI_PORTS_DEFINED) {
673                 proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
674                     decode_boolean_bitfield(flags, WCCP2_SI_PORTS_SOURCE,
675                       sizeof (flags)*8,
676                       "Ports refer to source port",
677                       "Ports refer to destination port"));
678         }
679         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
680             decode_boolean_bitfield(flags, WCCP2_SI_SRC_IP_ALT_HASH,
681               sizeof (flags)*8,
682               "Use source IP address in secondary hash",
683               "Don't use source IP address in secondary hash"));
684         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
685             decode_boolean_bitfield(flags, WCCP2_SI_DST_IP_ALT_HASH,
686               sizeof (flags)*8,
687               "Use destination IP address in secondary hash",
688               "Don't use destination IP address in secondary hash"));
689         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
690             decode_boolean_bitfield(flags, WCCP2_SI_SRC_PORT_ALT_HASH,
691               sizeof (flags)*8,
692               "Use source port in secondary hash",
693               "Don't use source port in secondary hash"));
694         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
695             decode_boolean_bitfield(flags, WCCP2_SI_DST_PORT_ALT_HASH,
696               sizeof (flags)*8,
697               "Use destination port in secondary hash",
698               "Don't use destination port in secondary hash"));
699         offset += 4;
700
701         if (flags & WCCP2_SI_PORTS_DEFINED) {
702                 for (i = 0; i < 8; i++) {
703                         proto_tree_add_text(info_tree, tvb, offset, 2,
704                             "Port %d: %u", i, tvb_get_ntohs(tvb, offset));
705                         offset += 2;
706                 }
707         }
708
709         return TRUE;
710 }
711
712 #define ROUTER_ID_INFO_MIN_LEN          (8+4+4)
713
714 static void
715 dissect_wccp2_router_identity_element(tvbuff_t *tvb, int offset,
716     proto_tree *tree)
717 {
718         proto_tree_add_text(tree, tvb, offset, 4,
719             "IP Address: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
720         proto_tree_add_text(tree, tvb, offset + 4, 4,
721             "Receive ID: %u", tvb_get_ntohl(tvb, offset + 4));
722 }
723
724 static gboolean
725 dissect_wccp2_router_identity_info(tvbuff_t *tvb, int offset, int length,
726     proto_tree *info_tree)
727 {
728         guint32 n_received_from;
729         guint i;
730         proto_item *te;
731         proto_tree *element_tree;
732
733         if (length < ROUTER_ID_INFO_MIN_LEN) {
734                 proto_tree_add_text(info_tree, tvb, offset, 0,
735                     "Item length is %u, should be >= %u", length,
736                     ROUTER_ID_INFO_MIN_LEN);
737                 return TRUE;
738         }
739
740         te = proto_tree_add_text(info_tree, tvb, offset, 8,
741             "Router Identity Element: IP address %s",
742             ip_to_str(tvb_get_ptr(tvb, offset, 4)));
743         element_tree = proto_item_add_subtree(te,
744             ett_router_identity_element);
745         dissect_wccp2_router_identity_element(tvb, offset, element_tree);
746         offset += 8;
747
748         proto_tree_add_text(info_tree, tvb, offset, 4,
749             "Sent To IP Address: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
750         offset += 4;
751
752         n_received_from = tvb_get_ntohl(tvb, offset);
753         proto_tree_add_text(info_tree, tvb, offset, 4,
754             "Number of Received From IP addresses: %u", n_received_from);
755         offset += 4;
756
757         for (i = 0; i < n_received_from; i++) {
758                 proto_tree_add_text(info_tree, tvb, offset, 4,
759                     "Received From IP Address %d: %s", i,
760                     ip_to_str(tvb_get_ptr(tvb, offset, 4)));
761                 offset += 4;
762         }
763
764         return TRUE;
765 }
766
767 #define WC_ID_INFO_LEN                  (4+4+8*4+4)
768
769 static gboolean
770 dissect_wccp2_web_cache_identity_element(tvbuff_t *tvb, int offset,
771     proto_tree *tree)
772 {
773         proto_item *bucket_item;
774         proto_tree *bucket_tree;
775         proto_item *tf;
776         proto_tree *field_tree;
777         guint16 flags;
778         int i;
779         guint8 bucket_info;
780         int n;
781
782         proto_tree_add_text(tree, tvb, offset, 4,
783             "Web-Cache IP Address: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
784         offset += 4;
785
786         proto_tree_add_text(tree, tvb, offset, 2,
787             "Hash Revision %u", tvb_get_ntohs(tvb, offset));
788         offset += 2;
789
790         flags = tvb_get_ntohs(tvb, offset);
791         tf = proto_tree_add_text(tree, tvb, offset, 2,
792             "Flags: 0x%04X (%s)", flags,
793             ((flags & 0x8000) ?
794               "Hash information is historical" :
795               "Hash information is current"));
796         field_tree = proto_item_add_subtree(tf, ett_flags);
797         proto_tree_add_text(field_tree, tvb, offset, 2, "%s",
798             decode_boolean_bitfield(flags, 0x8000,
799               sizeof (flags)*8,
800               "Hash information is historical",
801               "Hash information is current"));
802         offset += 2;
803
804         bucket_item = proto_tree_add_text(tree, tvb, offset, 8*4,
805             "Hash information");
806         bucket_tree = proto_item_add_subtree(bucket_item, ett_buckets);
807         for (i = 0, n = 0; i < 32; i++) {
808                 bucket_info = tvb_get_guint8(tvb, offset);
809                 n = wccp_bucket_info(bucket_info, bucket_tree, n, tvb, offset);
810                 offset += 1;
811         }
812
813         proto_tree_add_text(tree, tvb, offset, 2,
814             "Assignment Weight: %u", tvb_get_ntohs(tvb, offset));
815         offset += 2;
816
817         proto_tree_add_text(tree, tvb, offset, 2,
818             "Status: 0x%04X", tvb_get_ntohs(tvb, offset));
819
820         return TRUE;
821 }
822
823 static gboolean
824 dissect_wccp2_wc_identity_info(tvbuff_t *tvb, int offset, int length,
825     proto_tree *info_tree)
826 {
827         proto_item *te;
828         proto_tree *element_tree;
829
830         if (length != WC_ID_INFO_LEN) {
831                 proto_tree_add_text(info_tree, tvb, offset, 0,
832                     "Item length is %u, should be %u", length, WC_ID_INFO_LEN);
833                 return TRUE;
834         }
835
836         te = proto_tree_add_text(info_tree, tvb, offset, 4+2+2+32+2+2,
837             "Web-Cache Identity Element: IP address %s",
838             ip_to_str(tvb_get_ptr(tvb, offset, 4)));
839         element_tree = proto_item_add_subtree(te, ett_wc_identity_element);
840         if (!dissect_wccp2_web_cache_identity_element(tvb, offset,
841             element_tree))
842                 return FALSE;   /* ran out of data */
843
844         return TRUE;
845 }
846
847 #define ROUTER_VIEW_INFO_MIN_LEN        (4+8+4)
848
849 static void
850 dissect_wccp2_assignment_key(tvbuff_t *tvb, int offset,
851     proto_tree *info_tree)
852 {
853         proto_tree_add_text(info_tree, tvb, offset, 4,
854             "Assignment Key IP Address: %s",
855             ip_to_str(tvb_get_ptr(tvb, offset, 4)));
856         proto_tree_add_text(info_tree, tvb, offset + 4, 4,
857             "Assignment Key Change Number: %u", tvb_get_ntohl(tvb, offset + 4));
858 }
859
860 static gboolean
861 dissect_wccp2_router_view_info(tvbuff_t *tvb, int offset, int length,
862     proto_tree *info_tree)
863 {
864         guint32 n_routers;
865         guint32 n_web_caches;
866         guint i;
867         proto_item *te;
868         proto_tree *element_tree;
869
870         if (length < ROUTER_VIEW_INFO_MIN_LEN) {
871                 proto_tree_add_text(info_tree, tvb, offset, 0,
872                     "Item length is %u, should be >= %u", length,
873                     ROUTER_VIEW_INFO_MIN_LEN);
874                 return TRUE;
875         }
876
877         proto_tree_add_text(info_tree, tvb, offset, 4,
878             "Member Change Number: %u", tvb_get_ntohl(tvb, offset));
879         offset += 4;
880
881         dissect_wccp2_assignment_key(tvb, offset, info_tree);
882         offset += 8;
883
884         n_routers = tvb_get_ntohl(tvb, offset);
885         proto_tree_add_text(info_tree, tvb, offset, 4,
886             "Number of Routers: %u", n_routers);
887         offset += 4;
888
889         for (i = 0; i < n_routers; i++) {
890                 proto_tree_add_text(info_tree, tvb, offset, 4,
891                     "Router %d IP Address: %s", i,
892                     ip_to_str(tvb_get_ptr(tvb, offset, 4)));
893                 offset += 4;
894         }
895
896         n_web_caches = tvb_get_ntohl(tvb, offset);
897         proto_tree_add_text(info_tree, tvb, offset, 4,
898             "Number of Web Caches: %u", n_web_caches);
899         offset += 4;
900
901         for (i = 0; i < n_web_caches; i++) {
902                 te = proto_tree_add_text(info_tree, tvb, offset, WC_ID_INFO_LEN,
903                     "Web-Cache Identity Element %d: IP address %s", i,
904                     ip_to_str(tvb_get_ptr(tvb, offset, 4)));
905                 element_tree = proto_item_add_subtree(te,
906                     ett_wc_identity_element);
907                 if (!dissect_wccp2_web_cache_identity_element(tvb,
908                     offset, element_tree))
909                         return FALSE;   /* ran out of data */
910                 offset += WC_ID_INFO_LEN;
911         }
912
913         return TRUE;
914 }
915
916 #define WC_VIEW_INFO_MIN_LEN            (4+4)
917
918 static gboolean
919 dissect_wccp2_wc_view_info(tvbuff_t *tvb, int offset, int length,
920     proto_tree *info_tree)
921 {
922         guint32 n_routers;
923         guint32 n_web_caches;
924         guint i;
925         proto_item *te;
926         proto_tree *element_tree;
927
928         if (length < WC_VIEW_INFO_MIN_LEN) {
929                 proto_tree_add_text(info_tree, tvb, offset, 0,
930                     "Item length is %u, should be >= %u", length,
931                     WC_VIEW_INFO_MIN_LEN);
932                 return TRUE;
933         }
934
935         proto_tree_add_text(info_tree, tvb, offset, 4,
936             "Change Number: %u", tvb_get_ntohl(tvb, offset));
937         offset += 4;
938
939         n_routers = tvb_get_ntohl(tvb, offset);
940         proto_tree_add_text(info_tree, tvb, offset, 4,
941             "Number of Routers: %u", n_routers);
942         offset += 4;
943
944         for (i = 0; i < n_routers; i++) {
945                 te = proto_tree_add_text(info_tree, tvb, offset, 8,
946                     "Router %d Identity Element: IP address %s", i,
947                     ip_to_str(tvb_get_ptr(tvb, offset, 4)));
948                 element_tree = proto_item_add_subtree(te,
949                     ett_router_identity_element);
950                 dissect_wccp2_router_identity_element(tvb, offset, element_tree);
951                 offset += 8;
952         }
953
954         n_web_caches = tvb_get_ntohl(tvb, offset);
955         proto_tree_add_text(info_tree, tvb, offset, 4,
956             "Number of Web Caches: %u", n_web_caches);
957         offset += 4;
958
959         for (i = 0; i < n_web_caches; i++) {
960                 proto_tree_add_text(info_tree, tvb, offset, 4,
961                     "Web-Cache %d: IP address %s", i,
962                     ip_to_str(tvb_get_ptr(tvb, offset, 4)));
963                 offset += 4;
964         }
965
966         return TRUE;
967 }
968
969 #define ASSIGNMENT_INFO_MIN_LEN         (8+4)
970
971 static void
972 dissect_wccp2_router_assignment_element(tvbuff_t *tvb, int offset,
973     proto_tree *tree)
974 {
975         proto_tree_add_text(tree, tvb, offset, 4,
976             "IP Address: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
977         proto_tree_add_text(tree, tvb, offset + 4, 4,
978             "Receive ID: %u", tvb_get_ntohl(tvb, offset + 4));
979         proto_tree_add_text(tree, tvb, offset + 8, 4,
980             "Change Number: %u", tvb_get_ntohl(tvb, offset + 8));
981 }
982
983 static gchar *
984 assignment_bucket_name(guint8 bucket)
985 {
986         gchar *cur=NULL;
987
988         if (bucket == 0xff) {
989                 cur="Unassigned";
990         } else {
991                 cur=ep_alloc(10+1);
992                 g_snprintf(cur, 10+1, "%u%s", bucket >> 1,
993                     (bucket & 0x01) ? " (Alt)" : "");
994         }
995         return cur;
996 }
997
998 static gboolean
999 dissect_wccp2_assignment_info(tvbuff_t *tvb, int offset, int length,
1000     proto_tree *info_tree)
1001 {
1002         guint32 n_routers;
1003         guint32 n_web_caches;
1004         guint i;
1005         proto_item *te;
1006         proto_tree *element_tree;
1007
1008         if (length < ASSIGNMENT_INFO_MIN_LEN) {
1009                 proto_tree_add_text(info_tree, tvb, offset, 0,
1010                     "Item length is %u, should be >= %u", length,
1011                     ASSIGNMENT_INFO_MIN_LEN);
1012                 return TRUE;
1013         }
1014
1015         dissect_wccp2_assignment_key(tvb, offset, info_tree);
1016         offset += 8;
1017
1018         n_routers = tvb_get_ntohl(tvb, offset);
1019         proto_tree_add_text(info_tree, tvb, offset, 4,
1020             "Number of Routers: %u", n_routers);
1021         offset += 4;
1022
1023         for (i = 0; i < n_routers; i++) {
1024                 te = proto_tree_add_text(info_tree, tvb, offset, 4,
1025                     "Router %d Assignment Element: IP address %s", i,
1026                     ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1027                 element_tree = proto_item_add_subtree(te,
1028                     ett_router_assignment_element);
1029                 dissect_wccp2_router_assignment_element(tvb, offset,
1030                     element_tree);
1031                 offset += 12;
1032         }
1033
1034         n_web_caches = tvb_get_ntohl(tvb, offset);
1035         proto_tree_add_text(info_tree, tvb, offset, 4,
1036             "Number of Web Caches: %u", n_web_caches);
1037         offset += 4;
1038
1039         for (i = 0; i < n_web_caches; i++) {
1040                 proto_tree_add_text(info_tree, tvb, offset, 4,
1041                     "Web-Cache %d: IP address %s", i,
1042                     ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1043                 offset += 4;
1044         }
1045
1046         for (i = 0; i < 256; i += 4) {
1047                 proto_tree_add_text(info_tree, tvb, offset, 4,
1048                     "Buckets %d - %d: %10s %10s %10s %10s",
1049                     i, i + 3,
1050                     assignment_bucket_name(tvb_get_guint8(tvb, offset)),
1051                     assignment_bucket_name(tvb_get_guint8(tvb, offset+1)),
1052                     assignment_bucket_name(tvb_get_guint8(tvb, offset+2)),
1053                     assignment_bucket_name(tvb_get_guint8(tvb, offset+3)));
1054                 offset += 4;
1055         }
1056
1057         return TRUE;
1058 }
1059
1060 #define QUERY_INFO_LEN                  (4+4+4+4)
1061
1062 static gboolean
1063 dissect_wccp2_router_query_info(tvbuff_t *tvb, int offset, int length,
1064     proto_tree *info_tree)
1065 {
1066         if (length != QUERY_INFO_LEN) {
1067                 proto_tree_add_text(info_tree, tvb, offset, 0,
1068                     "Item length is %u, should be %u", length, QUERY_INFO_LEN);
1069                 return TRUE;
1070         }
1071
1072         proto_tree_add_text(info_tree, tvb, offset, 4,
1073             "Router IP Address: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1074         offset += 4;
1075
1076         proto_tree_add_text(info_tree, tvb, offset, 4,
1077             "Receive ID: %u", tvb_get_ntohl(tvb, offset));
1078         offset += 4;
1079
1080         proto_tree_add_text(info_tree, tvb, offset, 4,
1081             "Sent To IP Address: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1082         offset += 4;
1083
1084         proto_tree_add_text(info_tree, tvb, offset, 4,
1085             "Target IP Address: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1086
1087         return TRUE;
1088 }
1089
1090 #define WCCP2_FORWARDING_METHOD         0x01
1091 #define WCCP2_ASSIGNMENT_METHOD         0x02
1092 #define WCCP2_PACKET_RETURN_METHOD      0x03
1093
1094 static const value_string capability_type_vals[] = {
1095         { WCCP2_FORWARDING_METHOD,    "Forwarding Method" },
1096         { WCCP2_ASSIGNMENT_METHOD,    "Assignment Method" },
1097         { WCCP2_PACKET_RETURN_METHOD, "Return Method" },
1098         { 0,                          NULL }
1099 };
1100
1101 #define WCCP2_FORWARDING_METHOD_GRE     0x00000001
1102 #define WCCP2_FORWARDING_METHOD_L2      0x00000002
1103
1104 static const capability_flag forwarding_method_flags[] = {
1105         { WCCP2_FORWARDING_METHOD_GRE,
1106           "IP-GRE", "GRE-encapsulated" },
1107         { WCCP2_FORWARDING_METHOD_L2,
1108           "L2", "L2 rewrite" },
1109         { 0,
1110           NULL, NULL }
1111 };
1112
1113 #define WCCP2_ASSIGNMENT_METHOD_HASH    0x00000001
1114 #define WCCP2_ASSIGNMENT_METHOD_MASK    0x00000002
1115
1116 static const capability_flag assignment_method_flags[] = {
1117         { WCCP2_ASSIGNMENT_METHOD_HASH, "Hash", "Hash" },
1118         { WCCP2_ASSIGNMENT_METHOD_MASK, "Mask", "Mask" },
1119         { 0,                            NULL,   NULL }
1120 };
1121
1122 #define WCCP2_PACKET_RETURN_METHOD_GRE  0x00000001
1123 #define WCCP2_PACKET_RETURN_METHOD_L2   0x00000002
1124
1125 static const capability_flag packet_return_method_flags[] = {
1126         { WCCP2_PACKET_RETURN_METHOD_GRE,
1127           "IP-GRE", "GRE-encapsulated" },
1128         { WCCP2_PACKET_RETURN_METHOD_L2,
1129           "L2", "L2 rewrite" },
1130         { 0,
1131           NULL, NULL }
1132 };
1133
1134 static gboolean
1135 dissect_wccp2_capability_info(tvbuff_t *tvb, int offset, int length,
1136     proto_tree *info_tree)
1137 {
1138         guint16 capability_type;
1139         guint16 capability_val_len;
1140         int curr_offset;
1141         proto_item *te;
1142         proto_tree *element_tree;
1143
1144         for (curr_offset = offset; curr_offset < (length + offset);
1145             curr_offset += (capability_val_len + 4)) {
1146                 capability_type = tvb_get_ntohs(tvb, curr_offset);
1147                 capability_val_len = tvb_get_ntohs(tvb, curr_offset + 2);
1148                 te = proto_tree_add_text(info_tree, tvb, curr_offset,
1149                     capability_val_len + 4, "%s",
1150                     val_to_str(capability_type,
1151                       capability_type_vals, "Unknown Capability Element (0x%08X)"));
1152                 element_tree = proto_item_add_subtree(te,
1153                     ett_capability_element);
1154
1155                 proto_tree_add_text(element_tree, tvb, curr_offset, 2,
1156                     "Type: %s",
1157                     val_to_str(capability_type,
1158                       capability_type_vals, "Unknown (0x%08X)"));
1159
1160                 if (capability_val_len < 4) {
1161                         proto_tree_add_text(element_tree, tvb, curr_offset+2, 2,
1162                             "Value Length: %u (illegal, must be >= 4)",
1163                             capability_val_len);
1164                         break;
1165                 }
1166                 proto_tree_add_text(element_tree, tvb, curr_offset+2, 2,
1167                     "Value Length: %u", capability_val_len);
1168
1169                 switch (capability_type) {
1170
1171                 case WCCP2_FORWARDING_METHOD:
1172                         dissect_32_bit_capability_flags(tvb, curr_offset,
1173                             capability_val_len,
1174                             ett_capability_forwarding_method,
1175                             forwarding_method_flags, element_tree);
1176                         break;
1177
1178                 case WCCP2_ASSIGNMENT_METHOD:
1179                         dissect_32_bit_capability_flags(tvb, curr_offset,
1180                             capability_val_len,
1181                             ett_capability_assignment_method,
1182                             assignment_method_flags, element_tree);
1183                         break;
1184
1185                 case WCCP2_PACKET_RETURN_METHOD:
1186                         dissect_32_bit_capability_flags(tvb, curr_offset,
1187                             capability_val_len,
1188                             ett_capability_return_method,
1189                             packet_return_method_flags, element_tree);
1190                         break;
1191
1192                 default:
1193                         proto_tree_add_text(element_tree, tvb,
1194                             curr_offset+4, capability_val_len,
1195                             "Value: %s",
1196                             tvb_bytes_to_str(tvb, curr_offset+4,
1197                                              capability_val_len));
1198                         break;
1199                 }
1200
1201         }
1202         return TRUE;
1203 }
1204
1205 static void
1206 dissect_32_bit_capability_flags(tvbuff_t *tvb, int curr_offset,
1207     guint16 capability_val_len, gint ett, const capability_flag *flags,
1208     proto_tree *element_tree)
1209 {
1210         guint32 capability_val;
1211         proto_item *tm;
1212         proto_tree *method_tree;
1213         int i;
1214         char *flags_string;
1215         char *p;
1216         gint returned_length, str_index = 0;
1217         char *buf;
1218
1219         if (capability_val_len != 4) {
1220                 proto_tree_add_text(element_tree, tvb,
1221                     curr_offset+4, capability_val_len,
1222                     "Illegal length (must be 4)");
1223                 return;
1224         }
1225
1226         capability_val = tvb_get_ntohl(tvb, curr_offset + 4);
1227 #define FLAGS_STRING_LEN (128+1)
1228         flags_string=ep_alloc(FLAGS_STRING_LEN);
1229         flags_string[0] = 0;
1230         for (i = 0; flags[i].short_name != NULL; i++) {
1231                 if (capability_val & flags[i].value) {
1232                         if (str_index != 0) {
1233                                 returned_length = g_snprintf(&flags_string[str_index],
1234                                         FLAGS_STRING_LEN-str_index, ",");
1235                                 str_index += MIN(returned_length, FLAGS_STRING_LEN-str_index);
1236                         }
1237                         returned_length = g_snprintf(&flags_string[str_index],
1238                                 FLAGS_STRING_LEN-str_index, "%s", flags[i].short_name);
1239                         str_index += MIN(returned_length, FLAGS_STRING_LEN-str_index);
1240                 }
1241         }
1242         tm = proto_tree_add_text(element_tree, tvb, curr_offset+4, 4,
1243             "Value: 0x%08X (%s)", capability_val, flags_string);
1244
1245         method_tree = proto_item_add_subtree(tm, ett);
1246 #define BUF_SIZE 1024
1247         buf=ep_alloc(BUF_SIZE);
1248         buf[0]=0;
1249         for (i = 0; flags[i].long_name != NULL; i++) {
1250                 p = decode_bitfield_value(buf, capability_val,
1251                       flags[i].value, 32);
1252                 str_index = MIN((gint)(p-buf), BUF_SIZE);
1253                 returned_length = g_snprintf(&flags_string[str_index], BUF_SIZE-str_index,
1254                         "%s: %s", flags[i].long_name,
1255                                 (capability_val & flags[i].value)?
1256                                         "Supported":
1257                                         "Not supported"
1258                                 );
1259                 str_index += MIN(returned_length, BUF_SIZE-str_index);
1260                 proto_tree_add_text(method_tree, tvb, curr_offset+4, 4,
1261                     "%s", buf);
1262         }
1263 }
1264
1265 #define ALT_ASSIGNMENT_INFO_MIN_LEN    (4+4)
1266
1267 #define WCCP2_HASH_ASSIGNMENT_TYPE     0x0000
1268 #define WCCP2_MASK_ASSIGNMENT_TYPE     0x0001
1269
1270 static const value_string assignment_type_vals[] = {
1271         { WCCP2_HASH_ASSIGNMENT_TYPE, "Hash" },
1272         { WCCP2_MASK_ASSIGNMENT_TYPE, "Mask" },
1273         { 0,                          NULL }
1274 };
1275
1276 static void
1277 dissect_wccp2_value_element(tvbuff_t *tvb, int offset, int index, proto_tree *info_tree)
1278 {
1279         proto_item *tl;
1280         proto_tree *element_tree;
1281
1282         tl = proto_tree_add_text(info_tree, tvb, offset, 16, "Value Element(%u)", index);
1283         element_tree = proto_item_add_subtree(tl, ett_value_element);
1284
1285         proto_tree_add_text(element_tree, tvb, offset, 4, "Source Address value: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1286         offset += 4;
1287         proto_tree_add_text(element_tree, tvb, offset, 4, "Destination Address value: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1288         offset += 4;
1289         proto_tree_add_text(element_tree, tvb, offset, 2, "Source Port value: %u", tvb_get_ntohs(tvb, offset));
1290         offset += 2;
1291         proto_tree_add_text(element_tree, tvb, offset, 2, "Source Port value: %u", tvb_get_ntohs(tvb, offset));
1292         offset += 2;
1293         proto_tree_add_text(element_tree, tvb, offset, 4, "Web Cache Address: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1294 }
1295
1296 static guint
1297 dissect_wccp2_mask_value_set_element(tvbuff_t *tvb, int offset, int index, proto_tree *info_tree)
1298 {
1299         proto_item *tl;
1300         proto_tree *element_tree;
1301         guint num_of_val_elements;
1302         guint i;
1303
1304         tl = proto_tree_add_text(info_tree, tvb, offset, 0,
1305             "Mask/Value Set Element(%d)", index);
1306         element_tree = proto_item_add_subtree(tl, ett_mv_set_element);
1307
1308         proto_tree_add_text(element_tree, tvb, offset, 4, "Source Address Mask: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1309         offset += 4;
1310         proto_tree_add_text(element_tree, tvb, offset, 4, "Destination Address Mask: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1311         offset += 4;
1312         proto_tree_add_text(element_tree, tvb, offset, 2, "Source Port Mask: %04x", tvb_get_ntohs(tvb, offset));
1313         offset += 2;
1314         proto_tree_add_text(element_tree, tvb, offset, 2, "Destination Port Mask: %04x", tvb_get_ntohs(tvb, offset));
1315         offset += 2;
1316
1317         proto_tree_add_text(element_tree, tvb, offset, 4, "Number of Value Elements: %u", tvb_get_ntohl(tvb, offset));
1318         num_of_val_elements = tvb_get_ntohl(tvb, offset);
1319         offset += 4;
1320
1321         for (i = 0; i < num_of_val_elements; i++)
1322         {
1323                 dissect_wccp2_value_element(tvb, offset, i, element_tree);
1324                 offset += 16;
1325         }
1326
1327         proto_item_set_len(tl, 16+i*16);
1328
1329         return 16+index*16;
1330 }
1331
1332 static gboolean
1333 dissect_wccp2_alt_assignment_info(tvbuff_t *tvb, int offset, int length,
1334     proto_tree *info_tree)
1335 {
1336         guint16 assignment_type;
1337         guint16 assignment_length;
1338
1339         if (length < ALT_ASSIGNMENT_INFO_MIN_LEN) {
1340                 proto_tree_add_text(info_tree, tvb, offset, 0,
1341                     "Item length is %u, should be >= %u", length,
1342                     ALT_ASSIGNMENT_INFO_MIN_LEN);
1343                 return TRUE;
1344         }
1345
1346         assignment_type = tvb_get_ntohs(tvb, offset);
1347         proto_tree_add_text(info_tree, tvb, offset, 2,
1348             "Assignment type: %s", val_to_str(assignment_type,
1349             assignment_type_vals, "Unknown assignment type (%u)"));
1350         offset += 2;
1351
1352         assignment_length = tvb_get_ntohs(tvb, offset);
1353         proto_tree_add_text(info_tree, tvb, offset, 2,
1354             "Assignment length: %u", assignment_length);
1355         offset += 2;
1356
1357         switch (assignment_type) {
1358         case WCCP2_HASH_ASSIGNMENT_TYPE:
1359                 dissect_wccp2_assignment_info(tvb, offset, assignment_length, info_tree);
1360                 break;
1361
1362         case WCCP2_MASK_ASSIGNMENT_TYPE:
1363         {
1364                 guint num_of_rtr;
1365                 guint num_of_elem;
1366                 guint i;
1367
1368                 dissect_wccp2_assignment_key(tvb, offset, info_tree);
1369                 offset += 8;
1370
1371                 num_of_rtr = tvb_get_ntohl(tvb, offset);
1372                 proto_tree_add_text(info_tree, tvb, offset, 4, "Number of routers: %u", num_of_rtr);
1373                 offset += 4;
1374                 for (i = 0; i < num_of_rtr; i++)
1375                 {
1376                         dissect_wccp2_router_assignment_element(tvb, offset, info_tree);
1377                         offset += 12;
1378                 }
1379
1380                 num_of_elem = tvb_get_ntohl(tvb, offset);
1381                 proto_tree_add_text(info_tree, tvb, offset, 4, "Number of elements: %u", num_of_elem);
1382                 offset += 4;
1383                 for (i = 0; i < num_of_elem; i++)
1384                 {
1385                         offset += dissect_wccp2_mask_value_set_element(tvb, offset, i, info_tree);
1386                 }
1387                 break;
1388         }
1389
1390         default:
1391                 break;
1392         }
1393
1394         return TRUE;
1395 }
1396
1397 void
1398 proto_register_wccp(void)
1399 {
1400         static hf_register_info hf[] = {
1401                 { &hf_wccp_message_type,
1402                         { "WCCP Message Type", "wccp.message", FT_UINT32, BASE_DEC, VALS(wccp_type_vals), 0x0,
1403                                 "The WCCP message that was sent", HFILL }
1404                 },
1405                 { &hf_wccp_version,
1406                         { "WCCP Version", "wccp.version", FT_UINT32, BASE_HEX, VALS(wccp_version_val), 0x0,
1407                                 "The WCCP version", HFILL }
1408                 },
1409                 { &hf_hash_revision,
1410                         { "Hash Revision", "wccp.hash_revision", FT_UINT32, BASE_DEC, 0x0, 0x0,
1411                                 "The cache hash revision", HFILL }
1412                 },
1413                 { &hf_change_num,
1414                         { "Change Number", "wccp.change_num", FT_UINT32, BASE_DEC, 0x0, 0x0,
1415                                 "The Web-Cache list entry change number", HFILL }
1416                 },
1417                 { &hf_recvd_id,
1418                         { "Received ID", "wccp.recvd_id", FT_UINT32, BASE_DEC, 0x0, 0x0,
1419                                 "The number of I_SEE_YOU's that have been sent", HFILL }
1420                 },
1421                 { &hf_cache_ip,
1422                         { "Web Cache IP address", "wccp.cache_ip", FT_IPv4, BASE_NONE, NULL, 0x0,
1423                                 "The IP address of a Web cache", HFILL }
1424                 },
1425         };
1426         static gint *ett[] = {
1427                 &ett_wccp,
1428                 &ett_cache_count,
1429                 &ett_buckets,
1430                 &ett_flags,
1431                 &ett_cache_info,
1432                 &ett_security_info,
1433                 &ett_service_info,
1434                 &ett_service_flags,
1435                 &ett_router_identity_element,
1436                 &ett_router_identity_info,
1437                 &ett_wc_identity_element,
1438                 &ett_wc_identity_info,
1439                 &ett_router_view_info,
1440                 &ett_wc_view_info,
1441                 &ett_query_info,
1442                 &ett_router_assignment_element,
1443                 &ett_router_assignment_info,
1444                 &ett_capabilities_info,
1445                 &ett_capability_element,
1446                 &ett_capability_forwarding_method,
1447                 &ett_capability_assignment_method,
1448                 &ett_capability_return_method,
1449                 &ett_mv_set_element,
1450                 &ett_value_element,
1451                 &ett_alt_assignment_info,
1452                 &ett_unknown_info,
1453         };
1454
1455         proto_wccp = proto_register_protocol("Web Cache Coordination Protocol",
1456             "WCCP", "wccp");
1457         proto_register_field_array(proto_wccp, hf, array_length(hf));
1458         proto_register_subtree_array(ett, array_length(ett));
1459 }
1460
1461 void
1462 proto_reg_handoff_wccp(void)
1463 {
1464         dissector_handle_t wccp_handle;
1465
1466         wccp_handle = new_create_dissector_handle(dissect_wccp, proto_wccp);
1467         dissector_add("udp.port", UDP_PORT_WCCP, wccp_handle);
1468 }