Rename the routines that handle dissector tables with unsigned integer
[obnox/wireshark/wip.git] / epan / dissectors / packet-ancp.c
1 /* packet-ancp.c
2  *
3  * Dissector for ANCP - Access Node Control Protocol 
4  *
5  * More info on the protocol can be found on IETF: 
6  * http://tools.ietf.org/wg/ancp/
7  * http://tools.ietf.org/html/draft-ietf-ancp-protocol-09
8  *
9  * Copyright 2010, Aniruddha.A (anira@cisco.com)
10  *
11  * $Id$
12  *
13  * Wireshark - Network traffic analyzer
14  * By Gerald Combs <gerald@wireshark.org>
15  * Copyright 1998 Gerald Combs
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 along
28  * with this program; if not, write to the Free Software Foundation, Inc.,
29  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <epan/packet.h>
37 #include <epan/prefs.h>
38 #include <epan/dissectors/packet-tcp.h>
39 #include <epan/tap.h>
40 #include <epan/stats_tree.h>
41
42 #define ANCP_PORT 6068 /* The ANCP TCP port:draft-ietf-ancp-protocol-09.txt */
43
44 #define ANCP_MIN_HDR  4
45 #define ANCP_GSMP_ETHER_TYPE  0x880C
46 #define TECH_TYPE_DSL         0x5
47
48 #define ANCP_RESULT_MASK     0xF0 
49 #define ANCP_CODE_MASK       0x0FFF
50 #define ANCP_I_FLAG_MASK     0x80 
51 #define ANCP_SUBMSG_MASK     0x7FFF
52 #define ADJ_CODE_MASK        0x7F /* excluding MSB M-Flag */
53
54 #define ANCP_MTYPE_ADJ       10
55 #define ANCP_MTYPE_PORT_MGMT 32
56 #define ANCP_MTYPE_PORT_UP   80
57 #define ANCP_MTYPE_PORT_DN   81
58
59 /* Topology Discovery Extensions */
60 #define TLV_DSL_LINE_ATTRIBUTES         0x04
61 #define TLV_DSL_LINE_STATE              0x8F
62 #define TLV_DSL_TYPE                    0x91
63
64 /* Port Management Extensions */
65 #define TLV_PING_PARAMS                 0x07
66 #define TLV_PING_OPAQUE_DATA            0x08
67 #define TLV_PING_RES_STR                0x09
68
69 #define SKIPPADDING(_ofst, _len)         \
70     do {                                 \
71         if ((_len) % 4)                  \
72             _ofst += (4 - ((_len) % 4)); \
73     } while(0)
74
75 static int hf_ancp_len = -1;
76 static int hf_ancp_ver = -1;
77 static int hf_ancp_mtype = -1;
78 static int hf_ancp_timer = -1;
79 static int hf_ancp_adj_code = -1;
80 static int hf_ancp_sender_name = -1;
81 static int hf_ancp_receiver_name = -1;
82 static int hf_ancp_sender_port = -1;
83 static int hf_ancp_receiver_port = -1;
84 static int hf_ancp_p_info = -1;
85 static int hf_ancp_sender_instance = -1;
86 static int hf_ancp_p_id = -1;
87 static int hf_ancp_receiver_instance = -1;
88 static int hf_ancp_tech_type = -1;
89 static int hf_ancp_num_tlvs = -1;
90 static int hf_ancp_tot_len = -1;
91 static int hf_ancp_cap = -1;
92 static int hf_ancp_result = -1;
93 static int hf_ancp_code = -1;
94 static int hf_ancp_trans_id = -1;
95 static int hf_ancp_i_flag = -1;
96 static int hf_ancp_submsg_num = -1;
97 static int hf_ancp_port = -1;
98 static int hf_ancp_port_sess_num = -1;
99 static int hf_ancp_evt_seq_num = -1;
100 static int hf_ancp_label = -1;
101 static int hf_ancp_reserved = -1;
102 static int hf_ancp_blk_len = -1;
103 static int hf_ancp_num_ext_tlvs = -1;
104 static int hf_ancp_ext_tlv_type = -1;
105 static int hf_ancp_dsl_line_stlv_type = -1;
106 static int hf_ancp_dsl_line_stlv_value = -1;
107 static int hf_ancp_ext_tlv_value_str = -1;
108 static int hf_ancp_oam_opaque = -1;
109 static int hf_ancp_oam_loopb_cnt = -1;
110 static int hf_ancp_oam_timeout = -1;
111
112 static gint ett_ancp_len = -1;
113 static gint ett_ancp_ver = -1;
114 static gint ett_ancp_mtype = -1;
115 static gint ett_ancp_timer = -1;
116 static gint ett_ancp_adj_code = -1;
117 static gint ett_ancp_sender_name = -1;
118 static gint ett_ancp_receiver_name = -1;
119 static gint ett_ancp_sender_port = -1;
120 static gint ett_ancp_receiver_port = -1;
121 static gint ett_ancp_p_info = -1;
122 static gint ett_ancp_sender_instance = -1;
123 static gint ett_ancp_p_id = -1;
124 static gint ett_ancp_receiver_instance = -1;
125 static gint ett_ancp_tech_type = -1;
126 static gint ett_ancp_num_tlvs = -1;
127 static gint ett_ancp_tot_len = -1;
128 static gint ett_ancp_cap = -1;
129 static gint ett_ancp_result = -1;
130 static gint ett_ancp_code = -1;
131 static gint ett_ancp_trans_id = -1;
132 static gint ett_ancp_i_flag = -1;
133 static gint ett_ancp_submsg_num = -1;
134 static gint ett_ancp_port = -1;
135 static gint ett_ancp_port_sess_num= -1;
136 static gint ett_ancp_evt_seq_num = -1;
137 static gint ett_ancp_label = -1;
138 static gint ett_ancp_reserved = -1;
139 static gint ett_ancp_blk_len = -1;
140 static gint ett_ancp_num_ext_tlvs = -1;
141 static gint ett_ancp_ext_tlv_type = -1;
142 static gint ett_ancp_dsl_line_stlv_type = -1;
143 static gint ett_ancp_dsl_line_stlv_val = -1;
144 static gint ett_ancp_ext_tlv_value_str = -1;
145 static gint ett_ancp_oam_opaque = -1;
146 static gint ett_ancp_oam_loopb_cnt = -1;
147 static gint ett_ancp_oam_timeout = -1;
148
149 static int proto_ancp = -1;
150
151 /* ANCP stats - Tap interface */
152 static const guint8 *st_str_packets = "Total Packets";
153 static const guint8 *st_str_packet_types = "ANCP Packet Types";
154 static const guint8 *st_str_adj_pack_types = "ANCP Adjacency Packet Types";
155
156 static int st_node_packets = -1;
157 static int st_node_packet_types = -1;
158 static int st_node_adj_pack_types = -1;
159 static int ancp_tap = -1;
160
161 struct ancp_tap_t {
162     gint ancp_mtype;
163     gint ancp_adjcode; /* valid for ancp adjacency message only */
164 };
165
166 /* Value Strings */
167 static const value_string mtype_names[] = {
168     { 10, "Adjacency" },
169     { 32, "Port-Management" },
170     { 80, "Port-Up" },
171     { 81, "Port-Down" },
172     {  0,  NULL }
173 };
174
175 static const value_string adj_code_names[] = {
176     { 1, "Syn" },
177     { 2, "SynAck" },
178     { 3, "Ack" },
179     { 4, "Rstack" },
180     { 0,  NULL }
181 };
182
183 static const value_string captype_names[] = {
184     { 1, "Dynamic-Topology-Discovery" },
185     { 2, "Line-Configuration" },
186     { 3, "Transactional-Multicast" },
187     { 4, "OAM" },
188     { 0,  NULL }
189 };
190
191 static const value_string resulttype_names[] = {
192     { 0, "Ignore" },
193     { 1, "NAck" },
194     { 2, "AckAll" },
195     { 3, "Success" },
196     { 4, "Failure" },
197     { 0,  NULL }
198 };
199
200 static const value_string codetype_names[] = { /* For now, these are OAM codes*/
201     { 0x500, "Access-line-doesn't-exist" },
202     { 0x501, "Loopback-Test-Timeout" },
203     { 0x502, "Reserved" },
204     { 0x503, "DSL-line-status-showtime" },
205     { 0x504, "DSL-line-status-idle" },
206     { 0x505, "DSL-line-status-silent" },
207     { 0x506, "DSL-line-status-training" },
208     { 0x507, "DSL-line-integrity-error" },
209     { 0x508, "DSLAM resource-unavailable" },
210     { 0x509, "Invalid Test Parameter" },
211     { 0,  NULL }
212 };
213
214 static const value_string techtype_str[] = {
215     { 0x01, "PON" },
216     { 0x05, "DSL" },
217     { 0,  NULL }
218 };
219
220 static const value_string dsl_line_attrs[] = {
221     { 0x91,  "DSL-Type" },
222     { 0x81,  "Actual-Net-Data-Upstream" },
223     { 0x82,  "Actual-Net-Data-Rate-Downstream" },
224     { 0x83,  "Minimum-Net-Data-Rate-Upstream" },
225     { 0x84,  "Minimum-Net-Data-Rate-Downstream" },
226     { 0x85,  "Attainable-Net-Data-Rate-Upstream" },
227     { 0x86,  "Attainable-Net-Data-Rate-Downstream" },
228     { 0x87,  "Maximum-Net-Data-Rate-Upstream" },
229     { 0x88,  "Maximum-Net-Data-Rate-Downstream" },
230     { 0x89,  "Minimum-Net-Low-Power-Data-Rate-Upstream" },
231     { 0x8A,  "Minimum-Net-Low-Power-Data-Rate-Downstream" },
232     { 0x8B,  "Maximum-Interleaving-Delay-Upstream" },
233     { 0x8C,  "Actual-Interleaving-Delay-Upstream" },
234     { 0x8D,  "Maximum-Interleaving-Delay-Downstream" },
235     { 0x8E,  "Actual-Interleaving-Delay-Downstream" },
236     { 0x8F,  "DSL line state" },
237     { 0x90,  "Access Loop Encapsulation" },
238     { 0,  NULL }
239 };
240
241 static const value_string dsl_line_attr_units[] = {
242     { 0x91,  "" },
243     { 0x81,  "Kb/sec" },
244     { 0x82,  "Kb/sec" },
245     { 0x83,  "Kb/sec" },
246     { 0x84,  "Kb/sec" },
247     { 0x85,  "Kb/sec" },
248     { 0x86,  "Kb/sec" },
249     { 0x87,  "Kb/sec" },
250     { 0x88,  "Kb/sec" },
251     { 0x89,  "Kb/sec" },
252     { 0x8A,  "Kb/sec" },
253     { 0x8B,  "msec" },
254     { 0x8C,  "msec" },
255     { 0x8D,  "msec" },
256     { 0x8E,  "msec" },
257     { 0x8F,  "" },
258     { 0x90,  "" }, 
259     { 0,  NULL }
260 };
261
262 static const value_string dsl_line_type_names[] = {
263     { 1,  "ADSL1" },
264     { 2,  "ADSL2" },
265     { 3,  "ADSL2+" },
266     { 4,  "VDSL1" },
267     { 5,  "VDSL2" },
268     { 6,  "SDSL" },
269     { 0,  NULL }
270 };
271
272 static const value_string dsl_line_state_names[] = {
273     { 1,  "Showtime" },
274     { 2,  "Idle" },
275     { 3,  "Silent" },
276     { 0,  NULL }
277 };
278
279 static const value_string ext_tlv_types[] = {
280     { 0x01, "Access-Loop-Circuit-ID" },
281     { 0x02, "Access-Loop-Remote-ID" },
282     { 0x03, "Access-Aggregation-Circuit-ID-ASCII" },
283     { 0x04, "DSL Line Attributes" },
284     { 0x06, "Access-Aggregation-Circuit-ID-Binary" },
285     { 0x07, "OAM-Loopback-Test-Parameters" },
286     { 0x08, "Opaque-Data" },
287     { 0x09, "OAM-Loopback-Test-Response-String" },
288     { 0,  NULL }
289 };
290
291 static void
292 dissect_ancp_port_up_dn_mgmt(tvbuff_t *tvb, proto_tree *ancp_tree, gint offset)
293 {
294     proto_item *sti = NULL;
295     proto_tree *tlv_tree = NULL, *dsl_tree = NULL;
296     guint8  tech_type;
297     guint16 blk_len, tlen, ttype, stlvtype, stlvlen;
298     gint16  num_tlvs, num_stlvs;
299     gint    val;
300
301     sti = proto_tree_add_item(ancp_tree, hf_ancp_port, tvb, offset, 4, 
302             FALSE);
303     offset += 4;
304
305     sti = proto_tree_add_item(ancp_tree, hf_ancp_port_sess_num, tvb, offset, 4, 
306             FALSE);
307     offset += 4;
308
309     sti = proto_tree_add_item(ancp_tree, hf_ancp_evt_seq_num, tvb, offset, 4, 
310             FALSE);
311     offset += 4;
312
313     proto_tree_add_item(ancp_tree, hf_ancp_label, tvb, offset, 8, FALSE);
314     offset += 8;
315
316     /* Start of the Extension Block */
317     proto_tree_add_item(ancp_tree, hf_ancp_reserved, tvb, offset, 1, FALSE);
318     offset += 1;
319     /* 
320      * We have already displayed the message type in the common header dissect
321      * need not display this again here - skip it 
322      */
323     offset += 1; /* Message type in Ext Blk */
324
325     proto_tree_add_item(ancp_tree, hf_ancp_tech_type, tvb, offset, 1, FALSE);
326     tech_type = tvb_get_guint8(tvb, offset);
327     offset += 1;
328     
329     proto_tree_add_item(ancp_tree, hf_ancp_blk_len, tvb, offset, 1, FALSE);
330     offset += 1;
331
332     if (tech_type == TECH_TYPE_DSL) {
333         proto_tree_add_item(ancp_tree, hf_ancp_num_ext_tlvs, tvb, 
334                 offset, 2, FALSE);
335         num_tlvs = tvb_get_ntohs(tvb, offset);
336         offset += 2;
337
338         sti = proto_tree_add_item(ancp_tree, hf_ancp_len, tvb, 
339                 offset, 2, FALSE);
340         blk_len = tvb_get_ntohs(tvb, offset);
341         proto_item_append_text(sti, " (Extension Block)");
342         offset += 2;
343
344         /* Create a TLV sub tree */
345         tlv_tree = proto_item_add_subtree(sti, ett_ancp_len);
346
347         for( ;num_tlvs; num_tlvs--) {
348             sti = proto_tree_add_item(tlv_tree, hf_ancp_ext_tlv_type, tvb, 
349                     offset, 2, FALSE);
350             ttype = tvb_get_ntohs(tvb, offset);
351             offset += 2;
352
353             sti = proto_tree_add_item(tlv_tree, hf_ancp_len, tvb, 
354                     offset, 2, FALSE);
355             tlen = tvb_get_ntohs(tvb, offset);
356             offset += 2;
357
358             /* 
359              * Extension Block is common for event message and port
360              * management message, but the TLVs that can appear
361              * are different
362              */
363             switch (ttype) {
364                 case TLV_DSL_LINE_ATTRIBUTES: 
365                     /* Create a DSL Attribute SubTree */
366                     dsl_tree = proto_item_add_subtree(sti, 
367                             ett_ancp_ext_tlv_type);
368                     num_stlvs = tlen / 8; /* TODO - better way? */
369                     for ( ;num_stlvs; num_stlvs--) {
370                         sti = proto_tree_add_item(dsl_tree, 
371                                 hf_ancp_dsl_line_stlv_type, tvb, offset, 
372                                 2, FALSE);
373                         stlvtype = tvb_get_ntohs(tvb, offset);
374                         offset += 2;
375                         /* Skip sub-tlv-len display for now */
376                         stlvlen = tvb_get_ntohs(tvb, offset);
377                         offset += 2; /* Sub TLV Length */
378
379                         sti = proto_tree_add_item(dsl_tree, 
380                                 hf_ancp_dsl_line_stlv_value, tvb, offset, 
381                                 stlvlen, FALSE);
382                         val = tvb_get_ntohl(tvb, offset);
383                         offset += stlvlen; /* Except loop-encap, rest are 4B */
384
385                         switch (stlvtype) {
386                             case TLV_DSL_LINE_STATE:
387                                 proto_item_append_text(sti, " (%s)", 
388                                         val_to_str(val, dsl_line_state_names, 
389                                             "Unknown (0x%02x)"));
390                                 break;
391                             case TLV_DSL_TYPE:
392                                 proto_item_append_text(sti, " (%s)", 
393                                         val_to_str(val, dsl_line_type_names, 
394                                             "Unknown (0x%02x)"));
395                                 break;
396
397                             default:
398                                 /* Add Unit */
399                                 proto_item_append_text(sti, " %s", 
400                                         val_to_str(stlvtype, 
401                                             dsl_line_attr_units, 
402                                             "Unknown (0x%02x)"));
403                                 break;
404                         } 
405                         SKIPPADDING(offset, stlvlen);
406                     }
407                     break;
408                 case TLV_PING_OPAQUE_DATA:
409                     /* 2 32b values*/
410                     proto_tree_add_item(tlv_tree, hf_ancp_oam_opaque, 
411                             tvb, offset, 4, FALSE);
412                     offset += 4;
413                     proto_tree_add_item(tlv_tree, hf_ancp_oam_opaque,
414                             tvb, offset, 4, FALSE);
415                     offset += 4;
416                     break;
417                 case TLV_PING_PARAMS:
418                     /* Count (1B) Timeout (1B), 2B empty */
419                     proto_tree_add_item(tlv_tree, 
420                             hf_ancp_oam_loopb_cnt, tvb, offset, 1, FALSE);
421                     offset += 1;
422                     proto_tree_add_item(tlv_tree, 
423                             hf_ancp_oam_timeout, tvb, offset, 1, FALSE);
424                     offset += 1;
425                     /* Lets not bother about 2B until IETF WG figures out */
426                     offset += 2;    
427                     break;
428                 default: 
429                     /* Assume TLV value is string - covers ALCID, OAM resp */
430                     proto_tree_add_item(tlv_tree, hf_ancp_ext_tlv_value_str,
431                             tvb, offset, tlen, FALSE);
432                     offset += tlen;
433                     SKIPPADDING(offset, tlen);
434                     break;
435             } /* end switch {ttype} */
436         } /* end for {numtlvs} */
437     } /* end if {DSL} */ 
438 }
439
440 static void
441 dissect_ancp_adj_msg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ancp_tree, 
442                      gint offset, struct ancp_tap_t *ancp_info
443 )
444 {
445     proto_item *sti = NULL;
446     proto_tree *ancp_cap_tree = NULL;
447     guint8  byte, numcaps, adjcode;
448     guint16 tlv_len;
449
450     sti = proto_tree_add_item(ancp_tree, hf_ancp_timer, tvb, offset, 1,
451             FALSE);
452     offset += 1;
453     proto_item_append_text(sti, " msec");
454
455     sti = proto_tree_add_item(ancp_tree, hf_ancp_adj_code, tvb, offset, 1,
456             FALSE);
457     byte = tvb_get_guint8(tvb, offset);
458     offset += 1;
459     adjcode = byte & ADJ_CODE_MASK;
460     ancp_info->ancp_adjcode = adjcode; /* stats */
461     proto_item_append_text(sti, " (%s, M Flag %s)",
462             val_to_str(adjcode, adj_code_names, "Unknown (0x%02x)"),
463             byte >> 7 ? "Set" : "Unset");
464     col_append_fstr(pinfo->cinfo, COL_INFO, " (%s)",
465             val_to_str(adjcode, adj_code_names, "Unknown (0x%02x)"));
466
467     proto_tree_add_item(ancp_tree, hf_ancp_sender_name, tvb, offset, 6, FALSE);
468     offset += 6;
469
470     proto_tree_add_item(ancp_tree, hf_ancp_receiver_name, tvb,offset, 6, FALSE);
471     offset += 6;
472
473     proto_tree_add_item(ancp_tree, hf_ancp_sender_port, tvb, offset, 4, FALSE);
474     offset += 4;
475
476     proto_tree_add_item(ancp_tree, hf_ancp_receiver_port, tvb,offset, 4, FALSE);
477     offset += 4;
478
479     sti = proto_tree_add_item(ancp_tree, hf_ancp_p_info, tvb,
480             offset, 1, FALSE);
481     byte = tvb_get_guint8(tvb, offset);
482     offset += 1;
483     proto_item_append_text(sti, " (Type = %d, Flag = %d)",
484             byte >> 4, byte & 0x0F);
485
486     proto_tree_add_item(ancp_tree, hf_ancp_sender_instance, tvb, offset, 3, 
487             FALSE);
488     offset += 3;
489
490     proto_tree_add_item(ancp_tree, hf_ancp_p_id, tvb, offset, 1, FALSE);
491     offset += 1;
492
493     proto_tree_add_item(ancp_tree, hf_ancp_receiver_instance, tvb, offset, 3, 
494             FALSE);
495     offset += 3;
496
497     proto_tree_add_item(ancp_tree, hf_ancp_tech_type, tvb,
498             offset, 1, FALSE);
499     offset += 1;
500
501     sti = proto_tree_add_item(ancp_tree, hf_ancp_num_tlvs, tvb, offset, 1, 
502             FALSE);
503     numcaps = tvb_get_guint8(tvb, offset);
504     offset += 1;
505
506     /* Start the capability subtree */
507     ancp_cap_tree = proto_item_add_subtree(sti, ett_ancp_tot_len);
508
509     sti = proto_tree_add_item(ancp_cap_tree, hf_ancp_tot_len, tvb,
510             offset, 2, FALSE);
511     tlv_len = tvb_get_ntohs(tvb, offset);
512     offset += 2;
513
514     for ( ;numcaps; numcaps--) {
515         sti = proto_tree_add_item(ancp_cap_tree, hf_ancp_cap, tvb,
516                 offset, 2, FALSE);
517         offset += 2;
518
519         tlv_len = tvb_get_ntohs(tvb, offset);
520         offset += 2;
521         proto_item_append_text(sti, " (%d bytes)", tlv_len); 
522         /* TODO - if there are non boolean caps, validate before use */
523     }
524 }
525
526 static void 
527 ancp_stats_tree_init(stats_tree *st)
528 {
529     st_node_packets = stats_tree_create_node(st, st_str_packets, 0, TRUE);
530     st_node_packet_types = stats_tree_create_pivot(st, st_str_packet_types, 
531             st_node_packets);
532     st_node_adj_pack_types = stats_tree_create_node(st, st_str_adj_pack_types, 
533             st_node_packets, TRUE);
534 }
535
536 static int
537 ancp_stats_tree_packet(stats_tree* st, packet_info* pinfo _U_, 
538                        epan_dissect_t* edt _U_ , const void* p)
539 {
540     struct ancp_tap_t *pi = (struct ancp_tap_t *) p;
541
542     tick_stat_node(st, st_str_packets, 0, FALSE);
543     stats_tree_tick_pivot(st, st_node_packet_types,
544             val_to_str(pi->ancp_mtype, mtype_names, 
545                 "Unknown packet type (%d)"));
546     if (pi->ancp_mtype == ANCP_MTYPE_ADJ) 
547         stats_tree_tick_pivot(st, st_node_adj_pack_types,
548                 val_to_str(pi->ancp_adjcode, adj_code_names, 
549                     "Unknown Adjacency packet (%d)"));
550     return 1;
551 }
552
553 static void
554 dissect_ancp_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
555 {
556     gint   offset;
557     guint8 mtype;
558     struct ancp_tap_t *ancp_info;
559
560     offset = 0;
561     if (tvb_get_ntohs(tvb, offset) != ANCP_GSMP_ETHER_TYPE)
562         return;
563
564     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ANCP");
565
566     col_clear(pinfo->cinfo, COL_INFO);
567
568     ancp_info = ep_alloc(sizeof(struct ancp_tap_t));
569     ancp_info->ancp_mtype = 0;
570     ancp_info->ancp_adjcode = 0;
571
572     if (tree || have_tap_listener(ancp_tap)) { /* we are being asked for details */
573         proto_item *ti = NULL;
574         proto_item *sti = NULL;
575         proto_tree *ancp_tree = NULL;
576         guint8  byte;
577         guint16 len;
578
579         ti = proto_tree_add_item(tree, proto_ancp, tvb, 0, -1, FALSE);
580
581         ancp_tree = proto_item_add_subtree(ti, ett_ancp_len);
582         
583         offset = 2; /* skip ether type */
584
585         sti = proto_tree_add_item(ancp_tree, hf_ancp_len, tvb, offset, 2, 
586                 FALSE);
587         len = tvb_get_ntohs(tvb, offset);
588         offset += 2;
589         
590         sti = proto_tree_add_item(ancp_tree, hf_ancp_ver, tvb, offset, 1, 
591                 FALSE);
592         byte = tvb_get_guint8(tvb, offset);
593         offset += 1;
594         proto_item_append_text(sti, " (%d.%d)", byte >> 4, byte & 0x0F);
595
596         sti = proto_tree_add_item(ancp_tree, hf_ancp_mtype, tvb, offset, 1, 
597                 FALSE);
598         mtype = tvb_get_guint8(tvb, offset); /* ANCP message type */
599         ancp_info->ancp_mtype = mtype; /* stats */
600         offset += 1;
601
602         col_add_fstr(pinfo->cinfo, COL_INFO, "%s Message",
603                 val_to_str(mtype, mtype_names, "Unknown (0x%02x)"));
604
605         if (mtype != ANCP_MTYPE_ADJ) {
606             /* Dissect common header */
607             proto_tree_add_item(ancp_tree, hf_ancp_result, tvb, offset, 1, 
608                     FALSE); /* treat as 1B, but dont change offset */
609
610             proto_tree_add_item(ancp_tree, hf_ancp_code, tvb, offset, 2, 
611                     FALSE);
612             offset += 2;
613
614             proto_tree_add_item(ancp_tree, hf_ancp_p_id, tvb, offset,
615                     1, FALSE);
616             offset += 1;
617
618             proto_tree_add_item(ancp_tree, hf_ancp_trans_id, tvb,
619                     offset, 3, FALSE);
620             offset += 3;
621
622             proto_tree_add_item(ancp_tree, hf_ancp_i_flag, tvb, offset, 1, 
623                     FALSE); /* treat as 1B, but dont change offset */
624             
625             sti = proto_tree_add_item(ancp_tree, hf_ancp_submsg_num, tvb, 
626                     offset, 2, FALSE);
627             offset += 2;
628
629             /* 
630              * Lets not display the 'Length' field now, it is anyway same 
631              * as GSMP Length
632              * which we have already displayed at the start of the dissect
633              */
634             offset += 2; /* Length */
635         }
636       
637         switch(mtype) {
638             case ANCP_MTYPE_ADJ:
639                 dissect_ancp_adj_msg(tvb, pinfo, ancp_tree, offset, ancp_info);
640                 break;
641             case ANCP_MTYPE_PORT_DN:
642                 /* FALL THRU */ 
643             case ANCP_MTYPE_PORT_MGMT:
644                 /* FALL THRU */ 
645             case ANCP_MTYPE_PORT_UP:
646                 dissect_ancp_port_up_dn_mgmt(tvb, ancp_tree, offset);
647                 break;
648             default:
649                 proto_item_append_text(sti, " (Unknown Message %d)", mtype);
650                 break;
651         }
652     }
653     tap_queue_packet(ancp_tap, pinfo, ancp_info);
654 }
655
656 static guint 
657 get_ancp_msg_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
658 {
659     return (guint)tvb_get_ntohs(tvb, offset + 2) + 4; /* 2B len + 4B hdr */
660 }
661
662 static void
663 dissect_ancp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
664 {
665     tcp_dissect_pdus(tvb, pinfo, tree, TRUE, ANCP_MIN_HDR,
666             get_ancp_msg_len, dissect_ancp_message);
667 }
668
669 void
670 proto_register_ancp(void)
671 {
672     static hf_register_info hf[] = {
673         { &hf_ancp_len,
674             { "Length", "ancp.len",
675                 FT_UINT16, BASE_DEC,
676                 NULL, 0x0,
677                 NULL, HFILL }
678         },
679         { &hf_ancp_ver,
680             { "Version", "ancp.ver",
681                 FT_UINT8, BASE_HEX,
682                 NULL, 0x0,
683                 NULL, HFILL }
684         },
685         { &hf_ancp_mtype,
686             { "Message Type", "ancp.mtype",
687                 FT_UINT8, BASE_DEC,
688                 VALS(mtype_names), 0x0,
689                 NULL, HFILL }
690         },
691         { &hf_ancp_timer,
692             { "Timer", "ancp.timer",
693                 FT_UINT8, BASE_DEC,
694                 NULL, 0x0,
695                 NULL, HFILL }
696         },
697         { &hf_ancp_adj_code,
698             { "Code", "ancp.adjcode", /* this is diff from code */
699                 FT_UINT8, BASE_DEC,   /* for Adjacency msg only */
700                 NULL, ADJ_CODE_MASK,
701                 NULL, HFILL }
702         },
703         { &hf_ancp_sender_name,
704             { "Sender Name", "ancp.sender_name",
705                 FT_ETHER, BASE_NONE,
706                 NULL, 0x0,
707                 NULL, HFILL }
708         },
709         { &hf_ancp_receiver_name,
710             { "Receiver Name", "ancp.receiver_name",
711                 FT_ETHER, BASE_NONE,
712                 NULL, 0x0,
713                 NULL, HFILL }
714         },
715         { &hf_ancp_sender_port,
716             { "Sender Port", "ancp.sender_port",
717                 FT_UINT64, BASE_DEC,
718                 NULL, 0x0,
719                 NULL, HFILL }
720         },
721         { &hf_ancp_receiver_port,
722             { "Receiver Port", "ancp.receiver_port",
723                 FT_UINT64, BASE_DEC,
724                 NULL, 0x0,
725                 NULL, HFILL }
726         },
727         { &hf_ancp_p_info,
728             { "Partition Info", "ancp.partition_info",
729                 FT_UINT8, BASE_HEX,
730                 NULL, 0x0,
731                 NULL, HFILL }
732         },
733         { &hf_ancp_sender_instance,
734             { "Sender Instance", "ancp.sender_instance",
735                 FT_UINT24, BASE_DEC,
736                 NULL, 0x0,
737                 NULL, HFILL }
738         },
739         { &hf_ancp_p_id,
740             { "Partition ID", "ancp.partition_id",
741                 FT_UINT8, BASE_DEC,
742                 NULL, 0x0,
743                 NULL, HFILL }
744         },
745         { &hf_ancp_receiver_instance,
746             { "Receiver Instance", "ancp.receiver_instance",
747                 FT_UINT24, BASE_DEC,
748                 NULL, 0x0,
749                 NULL, HFILL }
750         },
751         { &hf_ancp_tech_type,
752             { "Tech Type", "ancp.tech_type",
753                 FT_UINT8, BASE_DEC,
754                 VALS(techtype_str), 0x0,
755                 NULL, HFILL }
756         },
757         { &hf_ancp_num_tlvs,
758             { "Num TLVs", "ancp.num_tlvs",
759                 FT_UINT8, BASE_DEC,
760                 NULL, 0x0,
761                 NULL, HFILL }
762         },
763         { &hf_ancp_tot_len,
764             { "Length", "ancp.tot_len", /* name just Len to reuse*/
765                 FT_UINT16, BASE_DEC,
766                 NULL, 0x0,
767                 NULL, HFILL }
768         },
769         { &hf_ancp_cap,
770             { "Capability", "ancp.capability",
771                 FT_UINT16, BASE_DEC,
772                 VALS(captype_names), 0x0,
773                 NULL, HFILL }
774         },
775         { &hf_ancp_result,
776             { "Result", "ancp.result",
777                 FT_UINT8, BASE_DEC,
778                 VALS(resulttype_names), ANCP_RESULT_MASK,
779                 NULL, HFILL }
780         },
781         { &hf_ancp_code,
782             { "Code", "ancp.code", 
783                 FT_UINT16, BASE_HEX,
784                 VALS(codetype_names), ANCP_CODE_MASK,
785                 NULL, HFILL }
786         },
787         { &hf_ancp_trans_id,
788             { "Transaction ID", "ancp.transaction_id",
789                 FT_UINT24, BASE_DEC,
790                 NULL, 0x0,
791                 NULL, HFILL }
792         },
793         { &hf_ancp_i_flag,
794             { "I Flag", "ancp.i_flag",
795                 FT_BOOLEAN, 8,
796                 TFS(&tfs_set_notset), ANCP_I_FLAG_MASK,
797                 NULL, HFILL }
798         },
799         { &hf_ancp_submsg_num,
800             { "SubMessage Number", "ancp.submessage_number",
801                 FT_UINT16, BASE_DEC,
802                 NULL, ANCP_SUBMSG_MASK,
803                 NULL, HFILL }
804         },
805         { &hf_ancp_port,
806             { "Port", "ancp.port",
807                 FT_UINT32, BASE_DEC,
808                 NULL, 0x0,
809                 NULL, HFILL }
810         },
811         { &hf_ancp_port_sess_num,
812             { "Port Session Number", "ancp.port_sess_num",
813                 FT_UINT32, BASE_DEC,
814                 NULL, 0x0,
815                 NULL, HFILL }
816         },
817         { &hf_ancp_evt_seq_num,
818             { "Event Sequence Number", "ancp.evt_seq_num",
819                 FT_UINT32, BASE_DEC,
820                 NULL, 0x0,
821                 NULL, HFILL }
822         },
823         { &hf_ancp_label,
824             { "Label", "ancp.label", /* Not used in proto */
825                 FT_UINT64, BASE_HEX,
826                 NULL, 0x0,
827                 NULL, HFILL }
828         },
829         { &hf_ancp_reserved,
830             { "Reserved", "ancp.reserved",
831                 FT_UINT8, BASE_DEC,
832                 NULL, 0x0,
833                 NULL, HFILL }
834         },
835         { &hf_ancp_blk_len,
836             { "Block Length", "ancp.blk_len",
837                 FT_UINT8, BASE_DEC,
838                 NULL, 0x0,
839                 NULL, HFILL }
840         },
841         { &hf_ancp_num_ext_tlvs,
842             { "Num TLVs", "ancp.ext_tlvs.count",
843                 FT_UINT16, BASE_DEC,
844                 NULL, 0x0,
845                 NULL, HFILL }
846         },
847         { &hf_ancp_ext_tlv_type,
848             { "TLV", "ancp.ext_tlv.type",
849                 FT_UINT16, BASE_DEC,
850                 VALS(ext_tlv_types), 0x0,
851                 NULL, HFILL }
852         },
853         { &hf_ancp_dsl_line_stlv_type,
854             { "Sub-TLV", "ancp.sub_tlv_type",
855                 FT_UINT16, BASE_HEX,
856                 VALS(dsl_line_attrs), 0x0,
857                 NULL, HFILL }
858         },
859         { &hf_ancp_dsl_line_stlv_value,
860             { "Value", "ancp.dsl_line_param",
861                 FT_UINT32, BASE_DEC,
862                 NULL, 0x0,
863                 NULL, HFILL }
864         },
865         { &hf_ancp_ext_tlv_value_str,
866             { "Value", "ancp.ext_tlv.value",
867                 FT_STRING, BASE_NONE,
868                 NULL, 0x0,
869                 NULL, HFILL }
870         },
871         { &hf_ancp_oam_opaque,
872             { "Opaque", "ancp.oam.opaque", /* There will be 2 such 32b vals */
873                 FT_UINT32, BASE_DEC,
874                 NULL, 0x0,
875                 NULL, HFILL }
876         },
877         { &hf_ancp_oam_loopb_cnt,
878             { "OAM Loopback Count", "ancp.oam.loopback_count",
879                 FT_UINT8, BASE_DEC,
880                 NULL, 0x0,
881                 NULL, HFILL }
882         },
883         { &hf_ancp_oam_timeout,
884             { "OAM Timeout", "ancp.oam.timeout",
885                 FT_UINT8, BASE_DEC,
886                 NULL, 0x0,
887                 NULL, HFILL }
888         },
889     };
890
891     /* Setup protocol subtree array */
892     static gint *ett[] = {
893         &ett_ancp_len,
894         &ett_ancp_ver,
895         &ett_ancp_mtype,
896         &ett_ancp_timer,
897         &ett_ancp_adj_code,
898         &ett_ancp_sender_name,
899         &ett_ancp_receiver_name,
900         &ett_ancp_sender_port,
901         &ett_ancp_receiver_port,
902         &ett_ancp_p_info,
903         &ett_ancp_sender_instance,
904         &ett_ancp_p_id,
905         &ett_ancp_receiver_instance,
906         &ett_ancp_tech_type,
907         &ett_ancp_num_tlvs,
908         &ett_ancp_tot_len,
909         &ett_ancp_cap,
910         &ett_ancp_result,
911         &ett_ancp_code,
912         &ett_ancp_trans_id,
913         &ett_ancp_i_flag,
914         &ett_ancp_submsg_num,
915         &ett_ancp_port,
916         &ett_ancp_port_sess_num,
917         &ett_ancp_evt_seq_num,
918         &ett_ancp_label,
919         &ett_ancp_reserved,
920         &ett_ancp_blk_len,
921         &ett_ancp_num_ext_tlvs,
922         &ett_ancp_ext_tlv_type,
923         &ett_ancp_dsl_line_stlv_type,
924         &ett_ancp_dsl_line_stlv_val,
925         &ett_ancp_ext_tlv_value_str,
926         &ett_ancp_oam_opaque,
927         &ett_ancp_oam_loopb_cnt,
928         &ett_ancp_oam_timeout,
929     };
930
931     proto_ancp = proto_register_protocol (
932             "Access Node Control Protocol",
933             "ANCP",
934             "ancp"
935             );
936
937     proto_register_field_array(proto_ancp, hf, array_length(hf));
938     proto_register_subtree_array(ett, array_length(ett));
939     ancp_tap = register_tap("ancp");
940 }
941  
942 void
943 proto_reg_handoff_ancp(void)
944 {
945     dissector_handle_t ancp_handle;
946
947     ancp_handle = create_dissector_handle(dissect_ancp, proto_ancp);
948     dissector_add_uint("tcp.port", ANCP_PORT, ancp_handle);
949     stats_tree_register("ancp", "ancp", "ANCP", 0,
950             ancp_stats_tree_packet, ancp_stats_tree_init, NULL);
951 }
952