"col_format_to_pref_str()" is used only in "prefs.c", and knows about
[obnox/wireshark/wip.git] / packet-fr.c
1 /* packet-fr.c
2  * Routines for Frame Relay  dissection
3  *
4  * Copyright 2001, Paul Ionescu <paul@acorp.ro>
5  *
6  * $Id: packet-fr.c,v 1.18 2001/06/18 02:17:46 guy Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@zing.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  *
27  *
28  * References:
29  *
30  * http://www.protocols.com/pbook/frame.htm
31  * http://www.frforum.com/5000/Approved/FRF.3/FRF.3.2.pdf
32  * ITU Recommendation Q.933
33  * RFC-1490
34  * RFC-2427
35  * Cisco encapsulation
36  * http://www.trillium.com/whats-new/wp_frmrly.html
37  *
38  */
39
40 #ifdef HAVE_CONFIG_H
41 # include "config.h"
42 #endif
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <ctype.h>
47
48 #include <string.h>
49 #include <glib.h>
50 #include "packet.h"
51 #include "packet-fr.h"
52 #include "packet-osi.h"
53 #include "packet-llc.h"
54 #include "packet-chdlc.h"
55 #include "xdlc.h"
56 #include "etypes.h"
57 #include "oui.h"
58 #include "nlpid.h"
59 #include "greproto.h"
60 #include "conversation.h"
61
62 /*
63  * Bits in the address field.
64  */
65 #define FRELAY_DLCI     0xfcf0          /* 2 byte DLCI Address */
66 #define FRELAY_CR       0x0200          /* Command/Response bit */
67 #define FRELAY_EA       0x0001          /* Address Extension bit */
68 #define FRELAY_FECN     0x0008          /* Forward Explicit Congestion Notification */
69 #define FRELAY_BECN     0x0004          /* Backward Explicit Congestion Notification */
70 #define FRELAY_DE       0x0002          /* Discard Eligibility */
71 #define FRELAY_DC       0x0002          /* Control bits */
72
73 /*
74  * Extract the DLCI from the address field.
75  */
76 #define EXTRACT_DLCI(addr)      ((((addr)&0xfc00) >> 6) | (((addr)&0xf0) >> 4))
77
78 #define FROM_DCE        0x80            /* for direction setting */
79
80 static gint proto_fr    = -1;
81 static gint ett_fr      = -1;
82 static gint hf_fr_dlci  = -1;
83 static gint hf_fr_cr    = -1;
84 static gint hf_fr_becn  = -1;
85 static gint hf_fr_fecn  = -1;
86 static gint hf_fr_de    = -1;
87 static gint hf_fr_ea    = -1;
88 static gint hf_fr_dc    = -1;
89 static gint hf_fr_nlpid = -1;
90 static gint hf_fr_oui   = -1;
91 static gint hf_fr_pid   = -1;
92 static gint hf_fr_snaptype = -1;
93 static gint hf_fr_chdlctype = -1;
94
95 static const true_false_string cmd_string = {
96                 "Command",
97                 "Response"
98         };
99 static const true_false_string ctrl_string = {
100                 "DLCI Address",
101                 "Control"
102         };
103 static const true_false_string ea_string = {
104                 "Last Octet",
105                 "More Follows"
106         };
107
108 /*
109  * This isn't the same as "nlpid_vals[]"; 0x08 is Q.933, not Q.931,
110  * and 0x09 is LMI, not Q.2931.
111  */
112 static const value_string fr_nlpid_vals[] = {
113         { NLPID_NULL,            "NULL" },
114         { NLPID_T_70,            "T.70" },
115         { NLPID_X_633,           "X.633" },
116         { NLPID_Q_931,           "Q.933" },
117         { NLPID_LMI,             "LMI" },
118         { NLPID_Q_2119,          "Q.2119" },
119         { NLPID_SNAP,            "SNAP" },
120         { NLPID_ISO8473_CLNP,    "CLNP" },
121         { NLPID_ISO9542_ESIS,    "ESIS" },
122         { NLPID_ISO10589_ISIS,   "ISIS" },
123         { NLPID_ISO10747_IDRP,   "IDRP" },
124         { NLPID_ISO9542X25_ESIS, "ESIS (X.25)" },
125         { NLPID_ISO10030,        "ISO 10030" },
126         { NLPID_ISO11577,        "ISO 11577" },
127         { NLPID_COMPRESSED,      "Data compression protocol" },
128         { NLPID_IP,              "IP" },
129         { NLPID_PPP,             "PPP" },
130         { 0,                     NULL },
131 };
132
133 dissector_table_t fr_subdissector_table;
134
135 static void dissect_fr_nlpid(tvbuff_t *tvb, int offset, packet_info *pinfo,
136                              proto_tree *tree, proto_item *ti,
137                              proto_tree *fr_tree, guint8 fr_ctrl);
138 static void dissect_lapf(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
139 static void dissect_fr_xid(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
140
141 static void dissect_fr(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
142 {
143   proto_item *ti = NULL;
144   proto_tree *fr_tree = NULL;
145   guint16 fr_header,fr_type,offset=2; /* default header length of FR is 2 bytes */
146   guint16 address;
147   char    buf[32];
148   guint8  fr_ctrl;
149
150   pinfo->current_proto = "Frame Relay";
151   if (check_col(pinfo->fd, COL_PROTOCOL)) 
152       col_set_str(pinfo->fd, COL_PROTOCOL, "FR");
153
154   if (pinfo->pseudo_header->x25.flags & FROM_DCE) {
155         if(check_col(pinfo->fd, COL_RES_DL_DST))
156             col_set_str(pinfo->fd, COL_RES_DL_DST, "DTE");
157         if(check_col(pinfo->fd, COL_RES_DL_SRC))
158             col_set_str(pinfo->fd, COL_RES_DL_SRC, "DCE");
159     }
160     else {
161         if(check_col(pinfo->fd, COL_RES_DL_DST))
162             col_set_str(pinfo->fd, COL_RES_DL_DST, "DCE");
163         if(check_col(pinfo->fd, COL_RES_DL_SRC))
164             col_set_str(pinfo->fd, COL_RES_DL_SRC, "DTE");
165     }
166
167 /*XXX We should check the EA bits and use that to generate the address. */
168
169   fr_header = tvb_get_ntohs(tvb, 0);
170   fr_ctrl = tvb_get_guint8( tvb, 2);
171   address = EXTRACT_DLCI(fr_header);
172
173   if (check_col(pinfo->fd, COL_INFO)) 
174       col_add_fstr(pinfo->fd, COL_INFO, "DLCI %u", address);
175
176   if (tree) {
177       ti = proto_tree_add_protocol_format(tree, proto_fr, tvb, 0, 3, "Frame Relay");
178       fr_tree = proto_item_add_subtree(ti, ett_fr);
179
180       decode_bitfield_value(buf, fr_header, FRELAY_DLCI, 16);
181       proto_tree_add_uint_format(fr_tree, hf_fr_dlci, tvb, 0, 2, address,
182         "%sDLCI: %u", buf, address);
183       proto_tree_add_boolean(fr_tree, hf_fr_cr,   tvb, 0, offset, fr_header);
184       proto_tree_add_boolean(fr_tree, hf_fr_fecn, tvb, 0, offset, fr_header);
185       proto_tree_add_boolean(fr_tree, hf_fr_becn, tvb, 0, offset, fr_header);
186       proto_tree_add_boolean(fr_tree, hf_fr_de,   tvb, 0, offset, fr_header);
187       proto_tree_add_boolean(fr_tree, hf_fr_ea,   tvb, 0, offset, fr_header);
188   }
189
190   if (fr_ctrl == XDLC_U) {
191       if (tree) {
192                 proto_tree_add_text(fr_tree, tvb, offset, 0, "------- Q.933 Encapsulation -------");
193                 /*
194                  * XXX - if we're going to show this as Unnumbered
195                  * Information, should we just hand it to
196                  * "dissect_xdlc_control()"?
197                  */
198                 proto_tree_add_text(fr_tree, tvb, offset, 1, "Unnumbered Information");
199       }
200       offset++;
201
202       SET_ADDRESS(&pinfo->dl_src, AT_DLCI, 2, (guint8*)&address);
203
204       dissect_fr_nlpid(tvb, offset, pinfo, tree, ti, fr_tree, fr_ctrl);
205   } else {
206       if (address == 0) {
207                 /* this must be some sort of lapf on DLCI 0 for SVC */
208                 /* because DLCI 0 is rezerved for LMI and  SVC signaling encaplulated in lapf */
209                 /* and LMI is transmitted in unnumbered information (03) */
210                 /* so this must be lapf (guessing) */
211                 dissect_lapf(tvb_new_subset(tvb,offset,-1,-1),pinfo,tree);
212                 return;
213       }
214       if (fr_ctrl == (XDLC_U|XDLC_XID)) {
215                 dissect_fr_xid(tvb_new_subset(tvb,offset,-1,-1),pinfo,tree);
216                 return;
217       }
218
219       /*
220        * If the data does not start with unnumbered information (03) and
221        * the DLCI# is not 0, then there may be Cisco Frame Relay encapsulation.
222        */
223       proto_tree_add_text(fr_tree, tvb, offset, 0, "------- Cisco Encapsulation -------");
224       fr_type  = tvb_get_ntohs(tvb, offset);
225       if (ti != NULL) {
226                 /* Include the Cisco HDLC type in the top-level protocol
227                    tree item. */
228                 proto_item_set_len(ti, offset+2);
229       }
230       chdlctype(fr_type, tvb, offset+2, pinfo, tree, fr_tree, hf_fr_chdlctype);
231   }
232 }
233
234
235 void dissect_fr_uncompressed(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
236 {
237   proto_item *ti = NULL;
238   proto_tree *fr_tree = NULL;
239
240   CHECK_DISPLAY_AS_DATA(proto_fr, tvb, pinfo, tree);
241
242   pinfo->current_proto = "Frame Relay";
243   if (check_col(pinfo->fd, COL_PROTOCOL)) 
244       col_set_str(pinfo->fd, COL_PROTOCOL, "FR");
245   if (check_col(pinfo->fd, COL_INFO)) 
246       col_clear(pinfo->fd, COL_INFO);
247
248   if (tree) {
249       ti = proto_tree_add_protocol_format(tree, proto_fr, tvb, 0, 4, "Frame Relay");
250       fr_tree = proto_item_add_subtree(ti, ett_fr);
251   }
252   dissect_fr_nlpid(tvb, 0, pinfo, tree, ti, fr_tree, XDLC_U);
253 }
254
255 static void dissect_fr_nlpid(tvbuff_t *tvb, int offset, packet_info *pinfo,
256                              proto_tree *tree, proto_item *ti,
257                              proto_tree *fr_tree, guint8 fr_ctrl)
258 {
259   guint8  fr_nlpid;
260   tvbuff_t *next_tvb;
261
262   fr_nlpid = tvb_get_guint8 (tvb,offset);
263   if (fr_nlpid == 0) {
264         if (tree)
265                 proto_tree_add_text(fr_tree, tvb, offset, 1, "Padding");
266         offset++;
267         if (ti != NULL) {
268                 /* Include the padding in the top-level protocol tree item. */
269                 proto_item_set_len(ti, offset);
270         }
271         fr_nlpid=tvb_get_guint8( tvb,offset);
272   }
273
274   /*
275    * OSI network layer protocols consider the NLPID to be part
276    * of the frame, so we'll pass it as part of the payload and,
277    * if the protocol is one of those, add it as a hidden item here.
278    */
279   next_tvb = tvb_new_subset(tvb,offset,-1,-1);
280   if (dissector_try_port(osinl_subdissector_table, fr_nlpid, next_tvb,
281                          pinfo, tree)) {
282         /*
283          * Yes, we got a match.  Add the NLPID as a hidden item,
284          * so you can, at least, filter on it.
285          */
286         if (tree)
287                 proto_tree_add_uint_hidden(fr_tree, hf_fr_nlpid,
288                     tvb, offset, 1, fr_nlpid );
289         return;
290   }
291
292   /*
293    * All other protocols don't.
294    *
295    * XXX - not true for Q.933 and LMI, but we don't yet have a
296    * Q.933 dissector (it'd be similar to the Q.931 dissector,
297    * but I don't think it'd be identical, although it's less
298    * different than is the Q.2931 dissector), and the LMI
299    * dissector doesn't yet put the protocol discriminator
300    * (NLPID) into the tree.
301    *
302    * Note that an NLPID of 0x08 for Q.933 could either be a
303    * Q.933 signaling message or a message for a protocol
304    * identified by a 2-octet layer 2 protocol type and a
305    * 2-octet layer 3 protocol type, those protocol type
306    * octets having the values from octets 6, 6a, 7, and 7a
307    * of a Q.931 low layer compatibility information element
308    * (section 4.5.19 of Q.931; Q.933 says they have the values
309    * from a Q.933 low layer compatibility information element,
310    * but Q.933 low layer compatibility information elements
311    * don't have protocol values in them).
312    *
313    * Assuming that, as Q.933 seems to imply, that Q.933 messages
314    * look just like Q.931 messages except where it explicitly
315    * says they differ, then the octet after the NLPID would,
316    * in a Q.933 message, have its upper 4 bits zero (that's
317    * the length of the call reference value, in Q.931, and
318    * is limited to 15 or fewer octets).  As appears to be the case,
319    * octet 6 of a Q.931 low layer compatibility element has the
320    * 0x40 bit set, so you can distinguish between a Q.933
321    * message and an encapsulated packet by checking whether
322    * the upper 4 bits of the octet after the NLPID are zero.
323    *
324    * To handle this, we'd handle Q.933's NLPID specially, which
325    * we'd want to do anyway, so that we give it a tvbuff that
326    * includes the NLPID.
327    */
328   if (tree)
329         proto_tree_add_uint(fr_tree, hf_fr_nlpid, tvb, offset, 1, fr_nlpid );
330   offset++;
331
332   switch (fr_nlpid) {
333
334   case NLPID_SNAP:
335         if (ti != NULL) {
336                 /* Include the NLPID and SNAP header in the top-level
337                    protocol tree item. */
338                 proto_item_set_len(ti, offset+5);
339         }
340         dissect_snap(tvb, offset, pinfo, tree, fr_tree, fr_ctrl,
341               hf_fr_oui, hf_fr_snaptype, hf_fr_pid, 0);
342         return;
343
344   default:
345         if (ti != NULL) {
346                 /* Include the NLPID in the top-level protocol tree item. */
347                 proto_item_set_len(ti, offset);
348         }
349         next_tvb = tvb_new_subset(tvb,offset,-1,-1);
350         if (!dissector_try_port(fr_subdissector_table,fr_nlpid,
351                                 next_tvb, pinfo, tree))
352                 dissect_data(next_tvb, 0, pinfo, tree);
353         break;
354   }
355 }
356
357 static void dissect_lapf(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
358 {
359         proto_tree_add_text(tree, tvb, 0, 0, "Frame relay lapf not yet implemented");
360         dissect_data(tvb_new_subset(tvb,0,-1,-1),0,pinfo,tree);
361 }
362 static void dissect_fr_xid(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
363 {
364         proto_tree_add_text(tree, tvb, 0, 0, "Frame relay xid not yet implemented");
365         dissect_data(tvb_new_subset(tvb,0,-1,-1),0,pinfo,tree);
366 }
367  
368 /* Register the protocol with Ethereal */
369 void proto_register_fr(void)
370 {                 
371   static hf_register_info hf[] = {
372
373         { &hf_fr_dlci, { 
374            "DLCI", "fr.dlci", FT_UINT16, BASE_DEC, 
375             NULL, FRELAY_DLCI, "Data-Link Connection Identifier", HFILL }},
376         { &hf_fr_cr, { 
377            "CR", "fr.cr", FT_BOOLEAN, 16, TFS(&cmd_string),
378             FRELAY_CR, "Command/Response", HFILL }},
379         { &hf_fr_dc, { 
380            "DC", "fr.dc", FT_BOOLEAN, 16, TFS(&ctrl_string),
381             FRELAY_CR, "Address/Control", HFILL }},
382
383         { &hf_fr_fecn, { 
384            "FECN", "fr.fecn", FT_BOOLEAN, 16, 
385             NULL, FRELAY_FECN, "Forward Explicit Congestion Notification", HFILL }},
386         { &hf_fr_becn, { 
387            "BECN", "fr.becn", FT_BOOLEAN, 16, 
388             NULL, FRELAY_BECN, "Backward Explicit Congestion Notification", HFILL }},
389         { &hf_fr_de, { 
390            "DE", "fr.de", FT_BOOLEAN, 16, 
391             NULL, FRELAY_DE, "Discard Eligibility", HFILL }},
392         { &hf_fr_ea, { 
393            "EA", "fr.ea", FT_BOOLEAN, 16, TFS(&ea_string),
394             FRELAY_EA, "Extended Address", HFILL }},
395         { &hf_fr_nlpid, { 
396            "NLPID", "fr.nlpid", FT_UINT8, BASE_HEX, 
397             VALS(fr_nlpid_vals), 0x0, "FrameRelay Encapsulated Protocol NLPID", HFILL }},
398         { &hf_fr_oui, {
399            "Organization Code", "fr.snap.oui", FT_UINT24, BASE_HEX, 
400            VALS(oui_vals), 0x0, "", HFILL }},
401         { &hf_fr_pid, {
402            "Protocol ID", "fr.snap.pid", FT_UINT16, BASE_HEX, 
403            NULL, 0x0, "", HFILL }},
404         { &hf_fr_snaptype, { 
405            "Type", "fr.snaptype", FT_UINT16, BASE_HEX, 
406             VALS(etype_vals), 0x0, "FrameRelay SNAP Encapsulated Protocol", HFILL }},
407         { &hf_fr_chdlctype, { 
408            "Type", "fr.chdlctype", FT_UINT16, BASE_HEX, 
409             VALS(chdlc_vals), 0x0, "FrameRelay Cisco HDLC Encapsulated Protocol", HFILL }},
410   };
411
412
413   /* Setup protocol subtree array */
414   static gint *ett[] = {
415     &ett_fr,
416   };
417
418   proto_fr = proto_register_protocol("Frame Relay", "FR", "fr");
419   proto_register_field_array(proto_fr, hf, array_length(hf));
420   proto_register_subtree_array(ett, array_length(ett));
421
422   fr_subdissector_table = register_dissector_table("fr.ietf");
423 }
424
425 void proto_reg_handoff_fr(void)
426 {
427   dissector_add("wtap_encap", WTAP_ENCAP_FRELAY, dissect_fr, proto_fr);
428   dissector_add("gre.proto", GRE_FR, dissect_fr, proto_fr);
429 }