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