Always call the Ethernet and Token Ring dissectors through a dissector
[metze/wireshark/wip.git] / packet-atm.c
1 /* packet-atm.c
2  * Routines for ATM packet disassembly
3  *
4  * $Id: packet-atm.c,v 1.32 2001/01/21 20:16:01 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
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 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #include <stdio.h>
35 #include <glib.h>
36 #include "packet.h"
37 #include "oui.h"
38 #include "resolv.h"
39
40 #include "packet-snmp.h"
41 #include "packet-sscop.h"
42
43 static int proto_atm = -1;
44 static int hf_atm_vpi = -1;
45 static int hf_atm_vci = -1;
46 static int proto_atm_lane = -1;
47 static int proto_ilmi = -1;
48
49 static gint ett_atm = -1;
50 static gint ett_atm_lane = -1;
51 static gint ett_atm_lane_lc_lan_dest = -1;
52 static gint ett_atm_lane_lc_lan_dest_rd = -1;
53 static gint ett_atm_lane_lc_flags = -1;
54 static gint ett_atm_lane_lc_tlv = -1;
55 static gint ett_ilmi = -1;
56
57 static dissector_handle_t eth_handle;
58 static dissector_handle_t tr_handle;
59 static dissector_handle_t llc_handle;
60
61 /*
62  * See
63  *
64  *      http://www.atmforum.org/atmforum/specs/approved.html
65  *
66  * for a number of ATM Forum specifications, e.g. the LAN Emulation
67  * over ATM 1.0 spec, whence I got most of this.
68  */
69
70 /* LE Control opcodes */
71 #define LE_CONFIGURE_REQUEST    0x0001
72 #define LE_CONFIGURE_RESPONSE   0x0101
73 #define LE_JOIN_REQUEST         0x0002
74 #define LE_JOIN_RESPONSE        0x0102
75 #define READY_QUERY             0x0003
76 #define READY_IND               0x0103
77 #define LE_REGISTER_REQUEST     0x0004
78 #define LE_REGISTER_RESPONSE    0x0104
79 #define LE_UNREGISTER_REQUEST   0x0005
80 #define LE_UNREGISTER_RESPONSE  0x0105
81 #define LE_ARP_REQUEST          0x0006
82 #define LE_ARP_RESPONSE         0x0106
83 #define LE_FLUSH_REQUEST        0x0007
84 #define LE_FLUSH_RESPONSE       0x0107
85 #define LE_NARP_REQUEST         0x0008
86 #define LE_TOPOLOGY_REQUEST     0x0009
87
88 static const value_string le_control_opcode_vals[] = {
89         { LE_CONFIGURE_REQUEST,   "LE_CONFIGURE_REQUEST" },
90         { LE_CONFIGURE_RESPONSE,  "LE_CONFIGURE_RESPONSE" },
91         { LE_JOIN_REQUEST,        "LE_JOIN_REQUEST" },
92         { LE_JOIN_RESPONSE,       "LE_JOIN_RESPONSE" },
93         { READY_QUERY,            "READY_QUERY" },
94         { READY_IND,              "READY_IND" },
95         { LE_REGISTER_REQUEST,    "LE_REGISTER_REQUEST" },
96         { LE_REGISTER_RESPONSE,   "LE_REGISTER_RESPONSE" },
97         { LE_UNREGISTER_REQUEST,  "LE_UNREGISTER_REQUEST" },
98         { LE_UNREGISTER_RESPONSE, "LE_UNREGISTER_RESPONSE" },
99         { LE_ARP_REQUEST,         "LE_ARP_REQUEST" },
100         { LE_ARP_RESPONSE,        "LE_ARP_RESPONSE" },
101         { LE_FLUSH_REQUEST,       "LE_FLUSH_REQUEST" },
102         { LE_FLUSH_RESPONSE,      "LE_FLUSH_RESPONSE" },
103         { LE_NARP_REQUEST,        "LE_NARP_REQUEST" },
104         { LE_TOPOLOGY_REQUEST,    "LE_TOPOLOGY_REQUEST" },
105         { 0,                      NULL }
106 };
107
108 /* LE Control statuses */
109 static const value_string le_control_status_vals[] = {
110         { 0,  "Success" },
111         { 1,  "Version not supported" },
112         { 2,  "Invalid request parameters" },
113         { 4,  "Duplicate LAN destination registration" },
114         { 5,  "Duplicate ATM address" },
115         { 6,  "Insufficient resources to grant request" },
116         { 7,  "Access denied" },
117         { 8,  "Invalid REQUESTOR-ID" },
118         { 9,  "Invalid LAN destination" },
119         { 10, "Invalid ATM address" },
120         { 20, "No configuraton" },
121         { 21, "LE_CONFIGURE error" },
122         { 22, "Insufficient information" },
123         { 0,  NULL }
124 };
125
126 /* LE Control LAN destination tags */
127 #define TAG_NOT_PRESENT         0x0000
128 #define TAG_MAC_ADDRESS         0x0001
129 #define TAG_ROUTE_DESCRIPTOR    0x0002
130
131 static const value_string le_control_landest_tag_vals[] = {
132         { TAG_NOT_PRESENT,       "Not present" },
133         { TAG_MAC_ADDRESS,       "MAC address" },
134         { TAG_ROUTE_DESCRIPTOR,  "Route descriptor" },
135         { 0,                     NULL }
136 };
137
138 /* LE Control LAN types */
139 #define LANT_UNSPEC     0x00
140 #define LANT_802_3      0x01
141 #define LANT_802_5      0x02
142
143 static const value_string le_control_lan_type_vals[] = {
144         { LANT_UNSPEC, "Unspecified" },
145         { LANT_802_3,  "Ethernet/802.3" },
146         { LANT_802_5,  "802.5" },
147         { 0,           NULL }
148 };
149
150 static void
151 dissect_le_client(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
152 {
153   proto_item *ti;
154   proto_tree *lane_tree;
155
156   if (tree) {
157     ti = proto_tree_add_protocol_format(tree, proto_atm_lane, tvb, 0, 2, "ATM LANE");
158     lane_tree = proto_item_add_subtree(ti, ett_atm_lane);
159
160     proto_tree_add_text(lane_tree, tvb, 0, 2, "LE Client: 0x%04X",
161                         tvb_get_ntohs(tvb, 0));
162   }
163 }
164
165 static void
166 dissect_lan_destination(tvbuff_t *tvb, int offset, const char *type, proto_tree *tree) 
167 {
168   proto_item *td;
169   proto_tree *dest_tree;
170   guint16 tag;
171   proto_item *trd;
172   proto_tree *rd_tree;
173   guint16 route_descriptor;
174
175   td = proto_tree_add_text(tree, tvb, offset, 8, "%s LAN destination",
176                         type);
177   dest_tree = proto_item_add_subtree(td, ett_atm_lane_lc_lan_dest);
178   tag = tvb_get_ntohs(tvb, offset);
179   proto_tree_add_text(dest_tree, tvb, offset, 2, "Tag: %s",
180         val_to_str(tag, le_control_landest_tag_vals,
181                                 "Unknown (0x%04X)"));
182   offset += 2;
183
184   switch (tag) {
185
186   case TAG_MAC_ADDRESS:
187     proto_tree_add_text(dest_tree, tvb, offset, 6, "MAC address: %s",
188                         ether_to_str(tvb_get_ptr(tvb, offset, 6)));
189     break;
190
191   case TAG_ROUTE_DESCRIPTOR:
192     offset += 4;
193     route_descriptor = tvb_get_ntohs(tvb, offset);
194     trd = proto_tree_add_text(dest_tree, tvb, offset, 2, "Route descriptor: 0x%02X",
195                         route_descriptor);
196     rd_tree = proto_item_add_subtree(td, ett_atm_lane_lc_lan_dest_rd);
197     proto_tree_add_text(rd_tree, tvb, offset, 2,
198             decode_numeric_bitfield(route_descriptor, 0xFFF0, 2*8,
199                         "LAN ID = %u"));
200     proto_tree_add_text(rd_tree, tvb, offset, 2,
201             decode_numeric_bitfield(route_descriptor, 0x000F, 2*8,
202                         "Bridge number = %u"));
203     break;
204   }
205 }
206
207 /*
208  * TLV values in LE Control frames.
209  */
210 #define TLV_TYPE(oui, ident)            (((oui) << 8) | (ident))
211
212 #define LE_CONTROL_TIMEOUT              TLV_TYPE(OUI_ATM_FORUM, 0x01)
213 #define LE_MAX_UNK_FRAME_COUNT          TLV_TYPE(OUI_ATM_FORUM, 0x02)
214 #define LE_MAX_UNK_FRAME_TIME           TLV_TYPE(OUI_ATM_FORUM, 0x03)
215 #define LE_VCC_TIMEOUT_PERIOD           TLV_TYPE(OUI_ATM_FORUM, 0x04)
216 #define LE_MAX_RETRY_COUNT              TLV_TYPE(OUI_ATM_FORUM, 0x05)
217 #define LE_AGING_TIME                   TLV_TYPE(OUI_ATM_FORUM, 0x06)
218 #define LE_FORWARD_DELAY_TIME           TLV_TYPE(OUI_ATM_FORUM, 0x07)
219 #define LE_EXPECTED_ARP_RESPONSE_TIME   TLV_TYPE(OUI_ATM_FORUM, 0x08)
220 #define LE_FLUSH_TIMEOUT                TLV_TYPE(OUI_ATM_FORUM, 0x09)
221 #define LE_PATH_SWITCHING_DELAY         TLV_TYPE(OUI_ATM_FORUM, 0x0A)
222 #define LE_LOCAL_SEGMENT_ID             TLV_TYPE(OUI_ATM_FORUM, 0x0B)
223 #define LE_MCAST_SEND_VCC_TYPE          TLV_TYPE(OUI_ATM_FORUM, 0x0C)
224 #define LE_MCAST_SEND_VCC_AVGRATE       TLV_TYPE(OUI_ATM_FORUM, 0x0D)
225 #define LE_MCAST_SEND_VCC_PEAKRATE      TLV_TYPE(OUI_ATM_FORUM, 0x0E)
226 #define LE_CONN_COMPLETION_TIMER        TLV_TYPE(OUI_ATM_FORUM, 0x0F)
227
228 static const value_string le_tlv_type_vals[] = {
229         { LE_CONTROL_TIMEOUT,           "Control Time-out" },
230         { LE_MAX_UNK_FRAME_COUNT,       "Maximum Unknown Frame Count" },
231         { LE_MAX_UNK_FRAME_TIME,        "Maximum Unknown Frame Time" },
232         { LE_VCC_TIMEOUT_PERIOD,        "VCC Time-out" },
233         { LE_MAX_RETRY_COUNT,           "Maximum Retry Count" },
234         { LE_AGING_TIME,                "Aging Time" },
235         { LE_FORWARD_DELAY_TIME,        "Forwarding Delay Time" },
236         { LE_EXPECTED_ARP_RESPONSE_TIME, "Expected LE_ARP Response Time" },
237         { LE_FLUSH_TIMEOUT,             "Flush Time-out" },
238         { LE_PATH_SWITCHING_DELAY,      "Path Switching Delay" },
239         { LE_LOCAL_SEGMENT_ID,          "Local Segment ID" },
240         { LE_MCAST_SEND_VCC_TYPE,       "Mcast Send VCC Type" },
241         { LE_MCAST_SEND_VCC_AVGRATE,    "Mcast Send VCC AvgRate" },
242         { LE_MCAST_SEND_VCC_PEAKRATE,   "Mcast Send VCC PeakRate" },
243         { LE_CONN_COMPLETION_TIMER,     "Connection Completion Timer" },
244         { 0,                            NULL },
245 };
246
247 static void
248 dissect_le_control(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
249 {
250   proto_item *ti;
251   proto_tree *lane_tree;
252   int offset = 0;
253   proto_item *tf;
254   proto_tree *flags_tree;
255   proto_item *ttlv;
256   proto_tree *tlv_tree;
257   guint16 opcode;
258   guint16 flags;
259   guint8 num_tlvs;
260   guint32 tlv_type;
261   guint8 tlv_length;
262
263   if (check_col(pinfo->fd, COL_INFO))
264     col_set_str(pinfo->fd, COL_INFO, "LE Control");
265
266   if (tree) {
267     ti = proto_tree_add_protocol_format(tree, proto_atm_lane, tvb, offset, 108, "ATM LANE");
268     lane_tree = proto_item_add_subtree(ti, ett_atm_lane);
269
270     proto_tree_add_text(lane_tree, tvb, offset, 2, "Marker: 0x%04X",
271                         tvb_get_ntohs(tvb, offset));
272     offset += 2;
273
274     proto_tree_add_text(lane_tree, tvb, offset, 1, "Protocol: 0x%02X",
275                         tvb_get_guint8(tvb, offset));
276     offset += 1;
277
278     proto_tree_add_text(lane_tree, tvb, offset, 1, "Version: 0x%02X",
279                         tvb_get_guint8(tvb, offset));
280     offset += 1;
281
282     opcode = tvb_get_ntohs(tvb, offset);
283     proto_tree_add_text(lane_tree, tvb, offset, 2, "Opcode: %s",
284         val_to_str(opcode, le_control_opcode_vals,
285                                 "Unknown (0x%04X)"));
286     offset += 2;
287
288     if (opcode == READY_QUERY || opcode == READY_IND) {
289       /* There's nothing more in this packet. */
290       return;
291     }
292
293     if (opcode & 0x0100) {
294       /* Response; decode status. */
295       proto_tree_add_text(lane_tree, tvb, offset, 2, "Status: %s",
296         val_to_str(tvb_get_ntohs(tvb, offset), le_control_status_vals,
297                                 "Unknown (0x%04X)"));
298     }
299     offset += 2;
300
301     proto_tree_add_text(lane_tree, tvb, offset, 4, "Transaction ID: 0x%08X",
302                         tvb_get_ntohl(tvb, offset));
303     offset += 4;
304
305     proto_tree_add_text(lane_tree, tvb, offset, 2, "Requester LECID: 0x%04X",
306                         tvb_get_ntohs(tvb, offset));
307     offset += 2;
308
309     flags = tvb_get_ntohs(tvb, offset);
310     tf = proto_tree_add_text(lane_tree, tvb, offset, 2, "Flags: 0x%04X",
311                         flags);
312     flags_tree = proto_item_add_subtree(tf, ett_atm_lane_lc_flags);
313     proto_tree_add_text(flags_tree, tvb, offset, 2, "%s",
314         decode_boolean_bitfield(flags, 0x0001, 8*2,
315                                 "Remote address", "Local address"));
316     proto_tree_add_text(flags_tree, tvb, offset, 2, "%s",
317         decode_boolean_bitfield(flags, 0x0080, 8*2,
318                                 "Proxy", "Not proxy"));
319     proto_tree_add_text(flags_tree, tvb, offset, 2, "%s",
320         decode_boolean_bitfield(flags, 0x0100, 8*2,
321                                 "Topology change", "No topology change"));
322     offset += 2;
323
324     dissect_lan_destination(tvb, offset, "Source", lane_tree);
325     offset += 8;
326
327     dissect_lan_destination(tvb, offset, "Target", lane_tree);
328     offset += 8;
329
330     proto_tree_add_text(lane_tree, tvb, offset, 20, "Source ATM Address: %s",
331                         tvb_bytes_to_str(tvb, offset, 20));
332     offset += 20;
333
334     proto_tree_add_text(lane_tree, tvb, offset, 1, "LAN type: %s",
335         val_to_str(tvb_get_guint8(tvb, offset), le_control_lan_type_vals,
336                                 "Unknown (0x%02X)"));
337     offset += 1;
338
339     proto_tree_add_text(lane_tree, tvb, offset, 1, "Maximum frame size: %u",
340                         tvb_get_guint8(tvb, offset));
341     offset += 1;
342
343     num_tlvs = tvb_get_guint8(tvb, offset);
344     proto_tree_add_text(lane_tree, tvb, offset, 1, "Number of TLVs: %u",
345                         num_tlvs);
346     offset += 1;
347
348     proto_tree_add_text(lane_tree, tvb, offset, 1, "ELAN name size: %u",
349                         tvb_get_guint8(tvb, offset));
350     offset += 1;
351
352     proto_tree_add_text(lane_tree, tvb, offset, 20, "Target ATM Address: %s",
353                         tvb_bytes_to_str(tvb, offset, 20));
354     offset += 20;
355
356     proto_tree_add_text(lane_tree, tvb, offset, 32, "ELAN name: %s",
357                         tvb_bytes_to_str(tvb, offset, 32));
358     offset += 32;
359
360     while (num_tlvs != 0) {
361       tlv_type = tvb_get_ntohl(tvb, offset);
362       tlv_length = tvb_get_guint8(tvb, offset+4);
363       ttlv = proto_tree_add_text(lane_tree, tvb, offset, 5+tlv_length, "TLV type: %s",
364         val_to_str(tlv_type, le_tlv_type_vals, "Unknown (0x%08x)"));
365       tlv_tree = proto_item_add_subtree(ttlv, ett_atm_lane_lc_tlv);
366       proto_tree_add_text(tlv_tree, tvb, offset, 4, "TLV Type: %s",
367         val_to_str(tlv_type, le_tlv_type_vals, "Unknown (0x%08x)"));
368       proto_tree_add_text(tlv_tree, tvb, offset+4, 1, "TLV Length: %u", tlv_length);
369       offset += 5+tlv_length;
370       num_tlvs--;
371     }
372   }
373 }
374
375 static void
376 dissect_lane(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) 
377 {
378   tvbuff_t      *next_tvb;
379   tvbuff_t      *next_tvb_le_client;
380
381   CHECK_DISPLAY_AS_DATA(proto_atm_lane, tvb, pinfo, tree);
382
383   pinfo->current_proto = "ATM LANE";
384
385   if (check_col(pinfo->fd, COL_PROTOCOL))
386     col_set_str(pinfo->fd, COL_PROTOCOL, "ATM LANE");
387   if (check_col(pinfo->fd, COL_INFO))
388     col_set_str(pinfo->fd, COL_INFO, "ATM LANE");
389
390   /* Is it LE Control, 802.3, 802.5, or "none of the above"? */
391   switch (pinfo->pseudo_header->ngsniffer_atm.AppHLType) {
392
393   case AHLT_LANE_LE_CTRL:
394     dissect_le_control(tvb, pinfo, tree);
395     break;
396
397   case AHLT_LANE_802_3:
398   case AHLT_LANE_802_3_MC:
399     dissect_le_client(tvb, pinfo, tree);
400
401     /* Dissect as Ethernet */
402     next_tvb_le_client  = tvb_new_subset(tvb, 2, -1, -1);
403     call_dissector(eth_handle, next_tvb_le_client, pinfo, tree);
404     break;
405
406   case AHLT_LANE_802_5:
407   case AHLT_LANE_802_5_MC:
408     dissect_le_client(tvb, pinfo, tree);
409
410     /* Dissect as Token-Ring */
411     next_tvb_le_client  = tvb_new_subset(tvb, 2, -1, -1);
412     call_dissector(tr_handle, next_tvb_le_client, pinfo, tree);
413     break;
414
415   default:
416     /* Dump it as raw data. */
417     next_tvb            = tvb_new_subset(tvb, 0, -1, -1);
418     dissect_data(next_tvb, 0, pinfo, tree);
419     break;
420   }
421 }
422
423 /* AAL types */
424 static const value_string aal_vals[] = {
425         { ATT_AAL_UNKNOWN,    "Unknown AAL" },
426         { ATT_AAL1,           "AAL1" },
427         { ATT_AAL3_4,         "AAL3/4" },
428         { ATT_AAL5,           "AAL5" },
429         { ATT_AAL_USER,       "User AAL" },
430         { ATT_AAL_SIGNALLING, "Signalling AAL" },
431         { ATT_OAMCELL,        "OAM cell" },
432         { 0,                  NULL }
433 };
434
435 /* AAL5 higher-level traffic types */
436 static const value_string aal5_hltype_vals[] = {
437         { ATT_HL_UNKNOWN, "Unknown traffic type" },
438         { ATT_HL_LLCMX,   "LLC multiplexed" },
439         { ATT_HL_VCMX,    "VC multiplexed" },
440         { ATT_HL_LANE,    "LANE" },
441         { ATT_HL_ILMI,    "ILMI" },
442         { ATT_HL_FRMR,    "Frame Relay" },
443         { ATT_HL_SPANS,   "FORE SPANS" },
444         { ATT_HL_IPSILON, "Ipsilon" },
445         { 0,              NULL }
446 };
447
448 /* Traffic subtypes for VC multiplexed traffic */
449 static const value_string vcmx_type_vals[] = {
450         { AHLT_UNKNOWN,        "Unknown VC multiplexed traffic type" },
451         { AHLT_VCMX_802_3_FCS, "802.3 FCS" },
452         { AHLT_VCMX_802_4_FCS, "802.4 FCS" },
453         { AHLT_VCMX_802_5_FCS, "802.5 FCS" },
454         { AHLT_VCMX_FDDI_FCS,  "FDDI FCS" },
455         { AHLT_VCMX_802_6_FCS, "802.6 FCS" },
456         { AHLT_VCMX_802_3,     "802.3" },
457         { AHLT_VCMX_802_4,     "802.4" },
458         { AHLT_VCMX_802_5,     "802.5" },
459         { AHLT_VCMX_FDDI,      "FDDI" },
460         { AHLT_VCMX_802_6,     "802.6" },
461         { AHLT_VCMX_FRAGMENTS, "Fragments" },
462         { AHLT_VCMX_BPDU,      "BPDU" },
463         { 0,                   NULL }
464 };
465
466 /* Traffic subtypes for LANE traffic */
467 static const value_string lane_type_vals[] = {
468         { AHLT_UNKNOWN,       "Unknown LANE traffic type" },
469         { AHLT_LANE_LE_CTRL,  "LE Control" },
470         { AHLT_LANE_802_3,    "802.3" },
471         { AHLT_LANE_802_5,    "802.5" },
472         { AHLT_LANE_802_3_MC, "802.3 multicast" },
473         { AHLT_LANE_802_5_MC, "802.5 multicast" },
474         { 0,                  NULL }
475 };
476
477 /* Traffic subtypes for Ipsilon traffic */
478 static const value_string ipsilon_type_vals[] = {
479         { AHLT_UNKNOWN,     "Unknown Ipsilon traffic type" },
480         { AHLT_IPSILON_FT0, "Flow type 0" },
481         { AHLT_IPSILON_FT1, "Flow type 1" },
482         { AHLT_IPSILON_FT2, "Flow type 2" },
483         { 0,                NULL }
484 };
485
486 /*
487  * We don't know what kind of traffic this is; try to guess.
488  * We at least know it's AAL5....
489  */
490 static void
491 atm_guess_content(tvbuff_t *tvb, packet_info *pinfo)
492 {
493         guint8 byte0, byte1, byte2;
494
495         if (pinfo->pseudo_header->ngsniffer_atm.Vpi == 0) {
496                 /*
497                  * Traffic on some PVCs with a VPI of 0 and certain
498                  * VCIs is of particular types.
499                  */
500                 switch (pinfo->pseudo_header->ngsniffer_atm.Vci) {
501
502                 case 5:
503                         /*
504                          * Signalling AAL.
505                          */
506                         pinfo->pseudo_header->ngsniffer_atm.AppTrafType =
507                             ATT_AAL_SIGNALLING;
508                         return;
509
510                 case 16:
511                         /*
512                          * ILMI.
513                          */
514                         pinfo->pseudo_header->ngsniffer_atm.AppTrafType |=
515                             ATT_HL_ILMI;
516                         return;
517                 }
518         }
519
520         /*
521          * OK, we can't tell what it is based on the VPI/VCI; try
522          * guessing based on the contents.
523          */
524         byte0 = tvb_get_guint8(tvb, 0);
525         byte1 = tvb_get_guint8(tvb, 1);
526         byte2 = tvb_get_guint8(tvb, 2);
527         if (byte0 == 0xaa && byte1 == 0xaa && byte2 == 0x03) {
528                 /*
529                  * Looks like a SNAP header; assume it's LLC multiplexed
530                  * RFC 1483 traffic.
531                  */
532                 pinfo->pseudo_header->ngsniffer_atm.AppTrafType |= ATT_HL_LLCMX;
533         } else {
534                 /*
535                  * Assume it's LANE.
536                  */
537                 pinfo->pseudo_header->ngsniffer_atm.AppTrafType |= ATT_HL_LANE;
538                 if (byte0 == 0xff && byte1 == 0x00) {
539                         /*
540                          * Looks like LE Control traffic.
541                          */
542                         pinfo->pseudo_header->ngsniffer_atm.AppHLType =
543                             AHLT_LANE_LE_CTRL;
544                 } else {
545                         /*
546                          * XXX - Ethernet, or Token Ring?
547                          * Assume Ethernet for now; if we see earlier
548                          * LANE traffic, we may be able to figure out
549                          * the traffic type from that, but there may
550                          * still be situations where the user has to
551                          * tell us.
552                          */
553                         pinfo->pseudo_header->ngsniffer_atm.AppHLType =
554                             AHLT_LANE_802_3;
555                 }
556         }
557 }
558
559 static void
560 dissect_atm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
561 {
562   proto_tree   *atm_tree;
563   proto_item   *ti;
564   guint         aal_type;
565   guint         hl_type;
566   const guint8 *pd;
567   int           offset;
568
569   CHECK_DISPLAY_AS_DATA(proto_atm, tvb, pinfo, tree);
570
571   pinfo->current_proto = "ATM";
572
573   aal_type = pinfo->pseudo_header->ngsniffer_atm.AppTrafType & ATT_AALTYPE;
574   hl_type = pinfo->pseudo_header->ngsniffer_atm.AppTrafType & ATT_HLTYPE;
575   if (aal_type == ATT_AAL5) {
576     if (hl_type == ATT_HL_UNKNOWN ||
577         pinfo->pseudo_header->ngsniffer_atm.AppHLType == AHLT_UNKNOWN) {
578       /*
579        * The joys of a connection-oriented link layer; the type of
580        * traffic may be implied by the connection on which it's
581        * traveling, rather than being specified in the packet itself.
582        *
583        * For this packet, the program that captured the packet didn't
584        * save the type of traffic, presumably because it didn't know
585        * the traffic type (either it didn't see the connection setup
586        * and wasn't running on one of the endpoints, and wasn't later
587        * told, e.g. by the human running it, what type of traffic was
588        * on that circuit, or was running on one of the endpoints but
589        * was using, to capture the packets, a mechanism that either
590        * doesn't have access to data saying what's going over the
591        * connection or doesn't bother providing that information).
592        *
593        * For now, we try to guess the traffic type based on the VPI/VCI
594        * or the packet header; later, we should provide a mechanism
595        * by which the user can specify what sort of traffic is on a
596        * particular circuit.
597        */
598       atm_guess_content(tvb, pinfo);
599
600       /*
601        * OK, now get the AAL type and high-layer type again.
602        */
603       aal_type = pinfo->pseudo_header->ngsniffer_atm.AppTrafType & ATT_AALTYPE;
604       hl_type = pinfo->pseudo_header->ngsniffer_atm.AppTrafType & ATT_HLTYPE;
605     }
606   }
607
608   if (check_col(pinfo->fd, COL_PROTOCOL))
609     col_set_str(pinfo->fd, COL_PROTOCOL, "ATM");
610
611   switch (pinfo->pseudo_header->ngsniffer_atm.channel) {
612
613   case 0:
614     /* Traffic from DCE to DTE. */
615     if (check_col(pinfo->fd, COL_RES_DL_DST))
616       col_set_str(pinfo->fd, COL_RES_DL_DST, "DTE");
617     if (check_col(pinfo->fd, COL_RES_DL_SRC))
618       col_set_str(pinfo->fd, COL_RES_DL_SRC, "DCE");
619     break;
620
621   case 1:
622     /* Traffic from DTE to DCE. */
623     if (check_col(pinfo->fd, COL_RES_DL_DST))
624       col_set_str(pinfo->fd, COL_RES_DL_DST, "DCE");
625     if (check_col(pinfo->fd, COL_RES_DL_SRC))
626       col_set_str(pinfo->fd, COL_RES_DL_SRC, "DTE");
627     break;
628   }
629
630   if (check_col(pinfo->fd, COL_INFO)) {
631     if (aal_type == ATT_AAL5) {
632       col_add_fstr(pinfo->fd, COL_INFO, "AAL5 %s",
633                 val_to_str(hl_type, aal5_hltype_vals,
634                                 "Unknown traffic type (%x)"));
635     } else {
636       col_add_str(pinfo->fd, COL_INFO,
637                 val_to_str(aal_type, aal_vals, "Unknown AAL (%x)"));
638     }
639   }
640
641   if (tree) {
642     ti = proto_tree_add_protocol_format(tree, proto_atm, tvb, 0, 0, "ATM");
643     atm_tree = proto_item_add_subtree(ti, ett_atm);
644
645     proto_tree_add_text(atm_tree, tvb, 0, 0, "AAL: %s",
646         val_to_str(aal_type, aal_vals, "Unknown AAL (%x)"));
647     if (aal_type == ATT_AAL5) {
648       proto_tree_add_text(atm_tree, tvb, 0, 0, "Traffic type: %s",
649         val_to_str(hl_type, aal5_hltype_vals, "Unknown AAL5 traffic type (%x)"));
650       switch (hl_type) {
651
652       case ATT_HL_LLCMX:
653         proto_tree_add_text(atm_tree, tvb, 0, 0, "LLC multiplexed traffic");
654         break;
655
656       case ATT_HL_VCMX:
657         proto_tree_add_text(atm_tree, tvb, 0, 0, "VC multiplexed traffic type: %s",
658                 val_to_str(pinfo->pseudo_header->ngsniffer_atm.AppHLType,
659                         vcmx_type_vals, "Unknown VCMX traffic type (%x)"));
660         break;
661
662       case ATT_HL_LANE:
663         proto_tree_add_text(atm_tree, tvb, 0, 0, "LANE traffic type: %s",
664                 val_to_str(pinfo->pseudo_header->ngsniffer_atm.AppHLType,
665                         lane_type_vals, "Unknown LANE traffic type (%x)"));
666         break;
667
668       case ATT_HL_IPSILON:
669         proto_tree_add_text(atm_tree, tvb, 0, 0, "Ipsilon traffic type: %s",
670                 val_to_str(pinfo->pseudo_header->ngsniffer_atm.AppHLType,
671                         ipsilon_type_vals, "Unknown Ipsilon traffic type (%x)"));
672         break;
673       }
674     }
675     proto_tree_add_uint(atm_tree, hf_atm_vpi, tvb, 0, 0,
676                 pinfo->pseudo_header->ngsniffer_atm.Vpi);
677     proto_tree_add_uint(atm_tree, hf_atm_vci, tvb, 0, 0,
678                 pinfo->pseudo_header->ngsniffer_atm.Vci);
679     switch (pinfo->pseudo_header->ngsniffer_atm.channel) {
680
681     case 0:
682       /* Traffic from DCE to DTE. */
683       proto_tree_add_text(atm_tree, tvb, 0, 0, "Channel: DCE->DTE");
684       break;
685
686     case 1:
687       /* Traffic from DTE to DCE. */
688       proto_tree_add_text(atm_tree, tvb, 0, 0, "Channel: DTE->DCE");
689       break;
690
691     default:
692       /* Sniffers shouldn't provide anything other than 0 or 1. */
693       proto_tree_add_text(atm_tree, tvb, 0, 0, "Channel: %u",
694                 pinfo->pseudo_header->ngsniffer_atm.channel);
695       break;
696     }
697     if (pinfo->pseudo_header->ngsniffer_atm.cells != 0) {
698       /*
699        * If the cell count is 0, assume it means we don't know how
700        * many cells it was.
701        *
702        * XXX - also, if this is AAL5 traffic, assume it means we don't
703        * know what was in the AAL5 trailer.  We may, however, find
704        * some capture program that can give us the AAL5 trailer
705        * information but not the cell count, in which case we need
706        * some other way of indicating whether we have the AAL5 trailer
707        * information.
708        */
709       proto_tree_add_text(atm_tree, tvb, 0, 0, "Cells: %u",
710                 pinfo->pseudo_header->ngsniffer_atm.cells);
711       if (aal_type == ATT_AAL5) {
712         proto_tree_add_text(atm_tree, tvb, 0, 0, "AAL5 U2U: %u",
713                 pinfo->pseudo_header->ngsniffer_atm.aal5t_u2u);
714         proto_tree_add_text(atm_tree, tvb, 0, 0, "AAL5 len: %u",
715                 pinfo->pseudo_header->ngsniffer_atm.aal5t_len);
716         proto_tree_add_text(atm_tree, tvb, 0, 0, "AAL5 checksum: 0x%08X",
717                 pinfo->pseudo_header->ngsniffer_atm.aal5t_chksum);
718       }
719     }
720   }
721
722   switch (aal_type) {
723
724   case ATT_AAL_SIGNALLING:
725     dissect_sscop(tvb, pinfo, tree);
726     break;
727
728   case ATT_AAL5:
729     switch (hl_type) {
730
731     case ATT_HL_LLCMX:
732       /* Dissect as WTAP_ENCAP_ATM_RFC1483 */
733       /* The ATM iptrace capture that we have shows LLC at this point,
734        * so that's what I'm calling */
735       call_dissector(llc_handle, tvb, pinfo, tree);
736       break;
737
738     case ATT_HL_LANE:
739       dissect_lane(tvb, pinfo, tree);
740       break;
741
742     case ATT_HL_ILMI:
743       CHECK_DISPLAY_AS_DATA(proto_ilmi, tvb, pinfo, tree);
744       tvb_compat(tvb, &pd, &offset);
745       dissect_snmp_pdu(pd, offset, pinfo->fd, tree, "ILMI", proto_ilmi, ett_ilmi);
746       break;
747
748     default:
749       if (tree) {
750         /* Dump it as raw data. */
751         dissect_data(tvb, 0, pinfo, tree);
752         break;
753       }
754     }
755     break;
756
757   default:
758     if (tree) {
759       /* Dump it as raw data.  (Is this a single cell?) */
760       dissect_data(tvb, 0, pinfo, tree);
761     }
762     break;
763   }
764 }
765
766 void
767 proto_register_atm(void)
768 {
769         static hf_register_info hf[] = {
770                 { &hf_atm_vpi,
771                 { "VPI",                "atm.vpi", FT_UINT8, BASE_DEC, NULL, 0x0,
772                         "" }},
773
774                 { &hf_atm_vci,
775                 { "VCI",                "atm.vci", FT_UINT16, BASE_DEC, NULL, 0x0,
776                         "" }},
777         };
778         static gint *ett[] = {
779                 &ett_atm,
780                 &ett_ilmi,
781                 &ett_atm_lane,
782                 &ett_atm_lane_lc_lan_dest,
783                 &ett_atm_lane_lc_lan_dest_rd,
784                 &ett_atm_lane_lc_flags,
785                 &ett_atm_lane_lc_tlv,
786         };
787         proto_atm = proto_register_protocol("ATM", "ATM", "atm");
788         proto_register_field_array(proto_atm, hf, array_length(hf));
789         proto_ilmi = proto_register_protocol("ILMI", "ILMI", "ilmi");
790         proto_atm_lane = proto_register_protocol("ATM LAN Emulation",
791             "ATM LANE", "lane");
792         proto_register_subtree_array(ett, array_length(ett));
793 }
794
795 void
796 proto_reg_handoff_atm(void)
797 {
798         /*
799          * Get handles for the Ethernet, Token Ring, and LLC dissectors.
800          */
801         eth_handle = find_dissector("eth");
802         tr_handle = find_dissector("tr");
803         llc_handle = find_dissector("llc");
804
805         dissector_add("wtap_encap", WTAP_ENCAP_ATM_SNIFFER, dissect_atm,
806             proto_atm);
807 }