6ed77b671fd3046bcbf9f6e5fbe09c0cdd82d397
[obnox/wireshark/wip.git] / epan / dissectors / 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$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  *
26  * References:
27  *
28  * http://www.protocols.com/pbook/frame.htm
29  * http://www.mplsforum.org/frame/Approved/FRF.3/FRF.3.2.pdf
30  * ITU Recommendations Q.922 and Q.933
31  * RFC-1490
32  * RFC-2427
33  * Cisco encapsulation
34  * http://www.trillium.com/assets/legacyframe/white_paper/8771019.pdf
35  */
36
37 #ifdef HAVE_CONFIG_H
38 # include "config.h"
39 #endif
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <ctype.h>
44
45 #include <string.h>
46 #include <glib.h>
47 #include <epan/packet.h>
48 #include <epan/prefs.h>
49 #include "packet-llc.h"
50 #include "packet-chdlc.h"
51 #include "packet-eth.h"
52 #include "packet-ip.h"
53 #include "packet-ipv6.h"
54 #include "packet-ppp.h"
55 #include "packet-fr.h"
56 #include <epan/xdlc.h>
57 #include <epan/etypes.h>
58 #include <epan/oui.h>
59 #include <epan/nlpid.h>
60 #include <epan/greproto.h>
61
62 /*
63  * Bits in the address field.
64  */
65 #define FRELAY_EA               0x01    /* Address field extension bit */
66
67 #define FRELAY_UPPER_DLCI       0xFC    /* Upper DLCI */
68 #define FRELAY_CR               0x02    /* Command/response bit in first octet */
69
70 #define FRELAY_SECOND_DLCI      0xF0    /* DLCI bits in FECN/BECN/DE octet */
71 #define FRELAY_FECN             0x08    /* Forward Explicit Congestion Notification */
72 #define FRELAY_BECN             0x04    /* Backward Explicit Congestion Notification */
73 #define FRELAY_DE               0x02    /* Discard Eligibility */
74
75 #define FRELAY_THIRD_DLCI       0xFE    /* DLCI bits in third octet, if any */
76
77 #define FRELAY_LOWER_DLCI       0xFC    /* Lower DLCI */
78 #define FRELAY_DC               0x02    /* DLCI or DL-CORE control indicator in last octet */
79
80 #define FROM_DCE        0x80            /* for direction setting */
81
82 static gint proto_fr    = -1;
83 static gint ett_fr      = -1;
84 static gint ett_fr_address = -1;
85 static gint ett_fr_control = -1;
86 static gint hf_fr_ea    = -1;
87 static gint hf_fr_upper_dlci = -1;
88 static gint hf_fr_cr    = -1;
89 static gint hf_fr_second_dlci = -1;
90 static gint hf_fr_fecn  = -1;
91 static gint hf_fr_becn  = -1;
92 static gint hf_fr_de    = -1;
93 static gint hf_fr_third_dlci = -1;
94 static gint hf_fr_dlcore_control = -1;
95 static gint hf_fr_lower_dlci = -1;
96 static gint hf_fr_dc    = -1;
97 static gint hf_fr_dlci  = -1;
98 static gint hf_fr_control = -1;
99 static gint hf_fr_n_r = -1;
100 static gint hf_fr_n_s = -1;
101 static gint hf_fr_p = -1;
102 static gint hf_fr_p_ext = -1;
103 static gint hf_fr_f = -1;
104 static gint hf_fr_f_ext = -1;
105 static gint hf_fr_s_ftype = -1;
106 static gint hf_fr_u_modifier_cmd = -1;
107 static gint hf_fr_u_modifier_resp = -1;
108 static gint hf_fr_ftype_i = -1;
109 static gint hf_fr_ftype_s_u = -1;
110 static gint hf_fr_ftype_s_u_ext = -1;
111 static gint hf_fr_nlpid = -1;
112 static gint hf_fr_oui   = -1;
113 static gint hf_fr_pid   = -1;
114 static gint hf_fr_snaptype = -1;
115 static gint hf_fr_chdlctype = -1;
116
117 static dissector_handle_t eth_withfcs_handle;
118 static dissector_handle_t gprs_ns_handle;
119 static dissector_handle_t data_handle;
120
121 static dissector_table_t osinl_subdissector_table;
122
123 /*
124  * Encapsulation type.
125  * XXX - this should be per-DLCI as well.
126  */
127 #define FRF_3_2         0       /* FRF 3.2 or Cisco HDLC */
128 #define GPRS_NS         1       /* GPRS Network Services (3GPP TS 08.16) */
129 #define RAW_ETHER       2       /* Raw Ethernet */
130
131 static gint fr_encap = FRF_3_2;
132
133 static const true_false_string cmd_string = {
134                 "Command",
135                 "Response"
136         };
137 static const true_false_string ctrl_string = {
138                 "DLCI Address",
139                 "Control"
140         };
141 static const true_false_string ea_string = {
142                 "Last Octet",
143                 "More Follows"
144         };
145
146 /*
147  * This isn't the same as "nlpid_vals[]"; 0x08 is Q.933, not Q.931,
148  * and 0x09 is LMI, not Q.2931, and we assume that it's an initial
149  * protocol identifier, so 0x01 is T.70, not X.29.
150  */
151 static const value_string fr_nlpid_vals[] = {
152         { NLPID_NULL,            "NULL" },
153         { NLPID_IPI_T_70,        "T.70" },      /* XXX - IPI, or SPI? */
154         { NLPID_X_633,           "X.633" },
155         { NLPID_Q_931,           "Q.933" },
156         { NLPID_LMI,             "LMI" },
157         { NLPID_Q_2119,          "Q.2119" },
158         { NLPID_SNAP,            "SNAP" },
159         { NLPID_ISO8473_CLNP,    "CLNP" },
160         { NLPID_ISO9542_ESIS,    "ESIS" },
161         { NLPID_ISO10589_ISIS,   "ISIS" },
162         { NLPID_ISO10747_IDRP,   "IDRP" },
163         { NLPID_ISO9542X25_ESIS, "ESIS (X.25)" },
164         { NLPID_ISO10030,        "ISO 10030" },
165         { NLPID_ISO11577,        "ISO 11577" },
166         { NLPID_COMPRESSED,      "Data compression protocol" },
167         { NLPID_IP,              "IP" },
168         { NLPID_IP6,             "IPv6" },
169         { NLPID_PPP,             "PPP" },
170         { 0,                     NULL },
171 };
172
173 static dissector_table_t fr_subdissector_table;
174 static dissector_table_t fr_osinl_subdissector_table;
175
176 static void dissect_fr_nlpid(tvbuff_t *tvb, int offset, packet_info *pinfo,
177                              proto_tree *tree, proto_item *ti,
178                              proto_tree *fr_tree, guint8 fr_ctrl);
179 static void dissect_lapf(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
180 static void dissect_fr_xid(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
181
182 /* Used only for U frames */
183 static const xdlc_cf_items fr_cf_items = {
184         NULL,
185         NULL,
186         &hf_fr_p,
187         &hf_fr_f,
188         NULL,
189         &hf_fr_u_modifier_cmd,
190         &hf_fr_u_modifier_resp,
191         NULL,
192         &hf_fr_ftype_s_u
193 };
194
195 /* Used only for I and S frames */
196 static const xdlc_cf_items fr_cf_items_ext = {
197         &hf_fr_n_r,
198         &hf_fr_n_s,
199         &hf_fr_p_ext,
200         &hf_fr_f_ext,
201         &hf_fr_s_ftype,
202         NULL,
203         NULL,
204         &hf_fr_ftype_i,
205         &hf_fr_ftype_s_u_ext
206 };
207
208 void
209 capture_fr(const guchar *pd, int offset, int len, packet_counts *ld)
210 {
211   guint8  fr_octet;
212   guint32 addr;
213   guint8  fr_ctrl;
214   guint8  fr_nlpid;
215
216   /*
217    * OK, fetch the address field - keep going until we get an EA bit.
218    */
219   if (!BYTES_ARE_IN_FRAME(offset, len, 1)) {
220     ld->other++;
221     return;
222   }
223   fr_octet = pd[offset];
224   if (fr_octet & FRELAY_EA) {
225     /*
226      * Bogus!  There should be at least 2 octets.
227      * XXX - is this FRF.12 frame relay fragmentation?  If so, can
228      * we handle that?
229      */
230     ld->other++;
231     return;
232   }
233   /*
234    * The first octet contains the upper 6 bits of the DLCI, as well
235    * as the C/R bit.
236    */
237   addr = (fr_octet & FRELAY_UPPER_DLCI) >> 2;
238   offset++;
239
240   /*
241    * The second octet contains 4 more bits of DLCI, as well as FECN,
242    * BECN, and DE.
243    */
244   if (!BYTES_ARE_IN_FRAME(offset, len, 1)) {
245     ld->other++;
246     return;
247   }
248   fr_octet = pd[offset];
249   addr = (addr << 4) | ((fr_octet & FRELAY_SECOND_DLCI) >> 4);
250   offset++;
251
252   if (!(fr_octet & FRELAY_EA)) {
253     /*
254      * We have 3 or more address octets.
255      *
256      * The third octet contains 7 more bits of DLCI if EA isn't set,
257      * and lower DLCI or DL-CORE control plus the DLCI or DL-CORE
258      * control indicator flag if EA is set.
259      */
260     if (!BYTES_ARE_IN_FRAME(offset, len, 1)) {
261       ld->other++;
262       return;
263     }
264     fr_octet = pd[offset];
265     if (!(fr_octet & FRELAY_EA)) {
266       /*
267        * 7 more bits of DLCI.
268        */
269       addr = (addr << 7) | ((fr_octet & FRELAY_THIRD_DLCI) >> 1);
270       offset++;
271       if (!BYTES_ARE_IN_FRAME(offset, len, 1)) {
272         ld->other++;
273         return;
274       }
275       fr_octet = pd[offset];
276       while (!(fr_octet & FRELAY_EA)) {
277         /*
278          * Bogus!  More than 4 octets of address.
279          */
280         offset++;
281         if (!BYTES_ARE_IN_FRAME(offset, len, 1)) {
282           ld->other++;
283           return;
284         }
285         fr_octet = pd[offset];
286       }
287     }
288
289     /*
290      * Last octet - contains lower DLCI or DL-CORE control, DLCI or
291      * DL-CORE control indicator flag.
292      */
293     if (fr_octet & FRELAY_DC) {
294       /*
295        * DL-CORE.
296        */
297     } else {
298       /*
299        * Last 6 bits of DLCI.
300        */
301       addr = (addr << 6) | ((fr_octet & FRELAY_LOWER_DLCI) >> 2);
302     }
303   }
304
305   switch (fr_encap) {
306
307   case FRF_3_2:
308     if (!BYTES_ARE_IN_FRAME(offset, len, 1)) {
309       ld->other++;
310       return;
311     }
312     fr_ctrl = pd[offset];
313     if (fr_ctrl == XDLC_U) {
314       offset++;
315
316       /*
317        * XXX - treat DLCI 0 specially?  On DLCI 0, an NLPID of 0x08
318        * means Q.933, but on other circuits it could be the "for
319        * protocols which do not have an NLPID assigned or do not
320        * have a SNAP encapsulation" stuff from RFC 2427.
321        */
322       if (!BYTES_ARE_IN_FRAME(offset, len, 1)) {
323         ld->other++;
324         return;
325       }
326       fr_nlpid = pd[offset];
327       if (fr_nlpid == 0) {
328         offset++;
329         if (!BYTES_ARE_IN_FRAME(offset, len, 1)) {
330           ld->other++;
331           return;
332         }
333         fr_nlpid = pd[offset];
334       }
335       offset++;
336       switch (fr_nlpid) {
337
338       case NLPID_IP:
339         capture_ip(pd, offset, len, ld);
340         break;
341
342       case NLPID_IP6:
343         capture_ipv6(pd, offset, len, ld);
344         break;
345
346       case NLPID_PPP:
347         capture_ppp_hdlc(pd, offset, len, ld);
348         break;
349
350       case NLPID_SNAP:
351         capture_snap(pd, offset, len, ld);
352         break;
353
354       default:
355         ld->other++;
356         break;
357       }
358     } else {
359       if (addr == 0) {
360         /*
361          * This must be some sort of LAPF on DLCI 0 for SVC
362          * because DLCI 0 is reserved for LMI and SVC signaling
363          * encapsulated in LAPF, and LMI is transmitted in
364          * unnumbered information (03), so this must be LAPF
365          * (guessing).
366          *
367          * XXX - but what is it?  Is Q.933 carried inside UI
368          * frames or other types of frames or both?
369          */
370         ld->other++;
371         return;
372       }
373       if (fr_ctrl == (XDLC_U|XDLC_XID)) {
374         /*
375          * XID.
376          */
377         ld->other++;
378         return;
379       }
380
381       /*
382        * If the data does not start with unnumbered information (03) and
383        * the DLCI# is not 0, then there may be Cisco Frame Relay encapsulation.
384        */
385       capture_chdlc(pd, offset, len, ld);
386     }
387     break;
388
389   case GPRS_NS:
390     ld->other++;
391     break;
392
393   case RAW_ETHER:
394     if (addr != 0)
395       capture_eth(pd, offset, len, ld);
396     else
397       ld->other++;
398     break;
399   }
400 }
401
402 static void
403 dissect_fr_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
404                   gboolean has_direction)
405 {
406   int offset = 0;
407   proto_item *ti = NULL;
408   proto_tree *fr_tree = NULL;
409   proto_item *octet_item = NULL;
410   proto_tree *octet_tree = NULL;
411   guint8 fr_octet;
412   int is_response = FALSE;
413   guint32 addr;
414   guint8  fr_ctrl;
415   guint16 fr_type;
416   tvbuff_t *next_tvb;
417
418   if (check_col(pinfo->cinfo, COL_PROTOCOL))
419       col_set_str(pinfo->cinfo, COL_PROTOCOL, "FR");
420   if (check_col(pinfo->cinfo, COL_INFO))
421       col_clear(pinfo->cinfo, COL_INFO);
422
423   if (has_direction) {
424     if (pinfo->pseudo_header->x25.flags & FROM_DCE) {
425       if (check_col(pinfo->cinfo, COL_RES_DL_DST))
426         col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DTE");
427       if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
428         col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DCE");
429     } else {
430       if (check_col(pinfo->cinfo, COL_RES_DL_DST))
431         col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DCE");
432       if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
433         col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DTE");
434     }
435   }
436
437   /*
438    * OK, fetch the address field - keep going until we get an EA bit.
439    */
440   fr_octet = tvb_get_guint8(tvb, offset);
441   if (tree) {
442       ti = proto_tree_add_protocol_format(tree, proto_fr, tvb, 0, -1, "Frame Relay");
443       fr_tree = proto_item_add_subtree(ti, ett_fr);
444   }
445   if (fr_octet & FRELAY_EA) {
446     /*
447      * Bogus!  There should be at least 2 octets.
448      * XXX - is this FRF.12 frame relay fragmentation?  If so, we
449      * should dissect it as such, if possible.
450      */
451     addr = 0;
452     if (tree) {
453       proto_tree_add_text(fr_tree, tvb, offset, 1,
454                           "Bogus 1-octet address field");
455       offset++;
456     }
457   } else {
458     /*
459      * The first octet contains the upper 6 bits of the DLCI, as well
460      * as the C/R bit.
461      */
462     addr = (fr_octet & FRELAY_UPPER_DLCI) >> 2;
463     is_response = (fr_octet & FRELAY_CR);
464     if (tree) {
465       octet_item = proto_tree_add_text(fr_tree, tvb, offset, 1,
466                                        "First address octet: 0x%02x", fr_octet);
467       octet_tree = proto_item_add_subtree(octet_item, ett_fr_address);
468       proto_tree_add_uint(octet_tree, hf_fr_upper_dlci, tvb, offset, 1, fr_octet);
469       proto_tree_add_boolean(octet_tree, hf_fr_cr, tvb, offset, 1, fr_octet);
470       proto_tree_add_boolean(octet_tree, hf_fr_ea, tvb, offset, 1, fr_octet);
471     }
472     offset++;
473
474     /*
475      * The second octet contains 4 more bits of DLCI, as well as FECN,
476      * BECN, and DE.
477      */
478     fr_octet = tvb_get_guint8(tvb, offset);
479     addr = (addr << 4) | ((fr_octet & FRELAY_SECOND_DLCI) >> 4);
480     if (tree) {
481       octet_item = proto_tree_add_text(fr_tree, tvb, offset, 1,
482                                        "Second address octet: 0x%02x",
483                                        fr_octet);
484       octet_tree = proto_item_add_subtree(octet_item, ett_fr_address);
485       proto_tree_add_uint(octet_tree, hf_fr_second_dlci, tvb, offset, 1, fr_octet);
486       proto_tree_add_boolean(octet_tree, hf_fr_fecn, tvb, 0, offset, fr_octet);
487       proto_tree_add_boolean(octet_tree, hf_fr_becn, tvb, 0, offset, fr_octet);
488       proto_tree_add_boolean(octet_tree, hf_fr_de, tvb, 0, offset, fr_octet);
489       proto_tree_add_boolean(octet_tree, hf_fr_ea, tvb, offset, 1, fr_octet);
490     }
491     offset++;
492
493     if (!(fr_octet & FRELAY_EA)) {
494       /*
495        * We have 3 or more address octets.
496        *
497        * The third octet contains 7 more bits of DLCI if EA isn't set,
498        * and lower DLCI or DL-CORE control plus the DLCI or DL-CORE
499        * control indicator flag if EA is set.
500        */
501       fr_octet = tvb_get_guint8(tvb, offset);
502       if (!(fr_octet & FRELAY_EA)) {
503         /*
504          * 7 more bits of DLCI.
505          */
506         addr = (addr << 7) | ((fr_octet & FRELAY_THIRD_DLCI) >> 1);
507         if (tree) {
508           octet_item = proto_tree_add_text(fr_tree, tvb, offset, 1,
509                                            "Third address octet: 0x%02x",
510                                            fr_octet);
511           octet_tree = proto_item_add_subtree(octet_item, ett_fr_address);
512           proto_tree_add_uint(octet_tree, hf_fr_third_dlci, tvb, offset, 1, fr_octet);
513           proto_tree_add_boolean(octet_tree, hf_fr_ea, tvb, offset, 1, fr_octet);
514         }
515         offset++;
516         fr_octet = tvb_get_guint8(tvb, offset);
517         while (!(fr_octet & FRELAY_EA)) {
518           /*
519            * Bogus!  More than 4 octets of address.
520            */
521           if (tree) {
522             proto_tree_add_text(fr_tree, tvb, offset, 1,
523                                 "Bogus extra address octet");
524           }
525           offset++;
526           fr_octet = tvb_get_guint8(tvb, offset);
527         }
528       }
529       if (tree) {
530         octet_item = proto_tree_add_text(fr_tree, tvb, offset, 1,
531                                          "Final address octet: 0x%02x",
532                                          fr_octet);
533         octet_tree = proto_item_add_subtree(octet_item, ett_fr_address);
534       } 
535
536       /*
537        * Last octet - contains lower DLCI or DL-CORE control, DLCI or
538        * DL-CORE control indicator flag.
539        */
540       if (fr_octet & FRELAY_DC) {
541         /*
542          * DL-CORE.
543          */
544         proto_tree_add_uint(octet_tree, hf_fr_dlcore_control, tvb, offset, 1, fr_octet);
545       } else {
546         /*
547          * Last 6 bits of DLCI.
548          */
549         addr = (addr << 6) | ((fr_octet & FRELAY_LOWER_DLCI) >> 2);
550         proto_tree_add_uint(octet_tree, hf_fr_lower_dlci, tvb, offset, 1, fr_octet);
551       }
552       proto_tree_add_boolean(octet_tree, hf_fr_dc, tvb, offset, 1, fr_octet);
553       proto_tree_add_boolean(octet_tree, hf_fr_ea, tvb, offset, 1, fr_octet);
554       
555       offset++;
556     }
557   }
558   if (tree) {
559     /* Put the full DLCI into the protocol tree. */
560     proto_tree_add_uint(fr_tree, hf_fr_dlci, tvb, 0, offset, addr);
561   }
562
563   pinfo->ctype = CT_DLCI;
564   pinfo->circuit_id = addr;
565
566   /* Add DLCI to a collumn */
567   if ( check_col(pinfo->cinfo, COL_FR_DLCI)) {
568       col_add_fstr(pinfo->cinfo, COL_FR_DLCI, "%u", addr);
569   }
570   
571   if (check_col(pinfo->cinfo, COL_INFO))
572       col_add_fstr(pinfo->cinfo, COL_INFO, "DLCI %u", addr);
573
574   switch (fr_encap) {
575
576   case FRF_3_2:
577     fr_ctrl = tvb_get_guint8(tvb, offset);
578     if (fr_ctrl == XDLC_U) {
579       dissect_xdlc_control(tvb, offset, pinfo, fr_tree, hf_fr_control,
580                            ett_fr_control, &fr_cf_items, &fr_cf_items_ext,
581                            NULL, NULL, is_response, TRUE, TRUE);
582       offset++;
583
584       /*
585        * XXX - treat DLCI 0 specially?  On DLCI 0, an NLPID of 0x08
586        * means Q.933, but on other circuits it could be the "for
587        * protocols which do not have an NLPID assigned or do not
588        * have a SNAP encapsulation" stuff from RFC 2427.
589        */
590       dissect_fr_nlpid(tvb, offset, pinfo, tree, ti, fr_tree, fr_ctrl);
591     } else {
592       if (addr == 0) {
593                 /*
594                  * This must be some sort of LAPF on DLCI 0 for SVC
595                  * because DLCI 0 is reserved for LMI and SVC signaling
596                  * encapsulated in LAPF, and LMI is transmitted in
597                  * unnumbered information (03), so this must be LAPF
598                  * (guessing).
599                  *
600                  * XXX - but what is it?  Is Q.933 carried inside UI
601                  * frames or other types of frames or both?
602                  */
603                 dissect_xdlc_control(tvb, offset, pinfo, fr_tree,
604                                      hf_fr_control, ett_fr_control,
605                                      &fr_cf_items, &fr_cf_items_ext,
606                                      NULL, NULL, is_response, TRUE, TRUE);
607                 dissect_lapf(tvb_new_subset(tvb,offset,-1,-1),pinfo,tree);
608                 return;
609       }
610       if (fr_ctrl == (XDLC_U|XDLC_XID)) {
611                 dissect_xdlc_control(tvb, offset, pinfo, fr_tree,
612                                      hf_fr_control, ett_fr_control,
613                                      &fr_cf_items, &fr_cf_items_ext,
614                                      NULL, NULL, is_response, TRUE, TRUE);
615                 dissect_fr_xid(tvb_new_subset(tvb,offset,-1,-1),pinfo,tree);
616                 return;
617       }
618
619       /*
620        * If the data does not start with unnumbered information (03) and
621        * the DLCI# is not 0, then there may be Cisco Frame Relay encapsulation.
622        */
623       fr_type  = tvb_get_ntohs(tvb, offset);
624       if (ti != NULL) {
625                 /* Include the Cisco HDLC type in the top-level protocol
626                    tree item. */
627                 proto_item_set_end(ti, tvb, offset+2);
628       }
629       chdlctype(fr_type, tvb, offset+2, pinfo, tree, fr_tree, hf_fr_chdlctype);
630     }
631     break;
632
633   case GPRS_NS:
634     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
635     if (addr != 0)
636       call_dissector(gprs_ns_handle, next_tvb, pinfo, tree);
637     else
638       dissect_lapf(next_tvb, pinfo, tree);
639     break;
640
641   case RAW_ETHER:
642     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
643     if (addr != 0)
644       call_dissector(eth_withfcs_handle, next_tvb, pinfo, tree);
645     else
646       dissect_lapf(next_tvb, pinfo, tree);
647     break;
648   }
649 }
650
651 static void
652 dissect_fr(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
653 {
654   dissect_fr_common(tvb, pinfo, tree, FALSE);
655 }
656
657 static void
658 dissect_fr_phdr(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
659 {
660   dissect_fr_common(tvb, pinfo, tree, TRUE);
661 }
662
663 static void dissect_fr_uncompressed(tvbuff_t *tvb, packet_info *pinfo,
664                                     proto_tree *tree)
665 {
666   proto_item *ti = NULL;
667   proto_tree *fr_tree = NULL;
668
669   if (check_col(pinfo->cinfo, COL_PROTOCOL))
670       col_set_str(pinfo->cinfo, COL_PROTOCOL, "FR");
671   if (check_col(pinfo->cinfo, COL_INFO))
672       col_clear(pinfo->cinfo, COL_INFO);
673
674   if (tree) {
675       ti = proto_tree_add_protocol_format(tree, proto_fr, tvb, 0, -1, "Frame Relay");
676       fr_tree = proto_item_add_subtree(ti, ett_fr);
677   }
678   dissect_fr_nlpid(tvb, 0, pinfo, tree, ti, fr_tree, XDLC_U);
679 }
680
681 static void dissect_fr_nlpid(tvbuff_t *tvb, int offset, packet_info *pinfo,
682                              proto_tree *tree, proto_item *ti,
683                              proto_tree *fr_tree, guint8 fr_ctrl)
684 {
685   guint8  fr_nlpid;
686   tvbuff_t *next_tvb;
687
688   /*
689    * Tentatively set the Frame Relay item not to include the NLPID,
690    * as OSI network layer protocols consider it to be part of
691    * the OSI PDU.
692    */
693   proto_item_set_end(ti, tvb, offset);
694   fr_nlpid = tvb_get_guint8 (tvb,offset);
695   if (fr_nlpid == 0) {
696         if (tree)
697                 proto_tree_add_text(fr_tree, tvb, offset, 1, "Padding");
698         offset++;
699         if (ti != NULL) {
700                 /* Include the padding in the top-level protocol tree item. */
701                 proto_item_set_end(ti, tvb, offset);
702         }
703         fr_nlpid=tvb_get_guint8( tvb,offset);
704   }
705
706   /*
707    * OSI network layer protocols consider the NLPID to be part
708    * of the frame, so we'll pass it as part of the payload and,
709    * if the protocol is one of those, add it as a hidden item here.
710    * We check both the generic OSI NLPID dissector table and
711    * the Frame Relay OSI NLPID dissector table - the latter is for
712    * NLPID's such as 0x08, which is Q.933 in Frame Relay but
713    * other protocols (e.g., Q.931) on other network layers.
714    *
715    * "OSI network layer protocols" includes Q.933.
716    *
717    * XXX - note that an NLPID of 0x08 for Q.933 could either be a
718    * Q.933 signaling message or a message for a protocol
719    * identified by a 2-octet layer 2 protocol type and a
720    * 2-octet layer 3 protocol type, those protocol type
721    * octets having the values from octets 6, 6a, 7, and 7a
722    * of a Q.931 low layer compatibility information element
723    * (section 4.5.19 of Q.931; Q.933 says they have the values
724    * from a Q.933 low layer compatibility information element,
725    * but Q.933 low layer compatibility information elements
726    * don't have protocol values in them).
727    *
728    * Assuming that, as Q.933 seems to imply, that Q.933 messages
729    * look just like Q.931 messages except where it explicitly
730    * says they differ, then the octet after the NLPID would,
731    * in a Q.933 message, have its upper 4 bits zero (that's
732    * the length of the call reference value, in Q.931, and
733    * is limited to 15 or fewer octets).  As appears to be the case,
734    * octet 6 of a Q.931 low layer compatibility element has the
735    * 0x40 bit set, so you can distinguish between a Q.933
736    * message and an encapsulated packet by checking whether
737    * the upper 4 bits of the octet after the NLPID are zero.
738    *
739    * Either that, or it's Q.933 iff the DLCI is 0.
740    */
741   next_tvb = tvb_new_subset(tvb,offset,-1,-1);
742   if (dissector_try_port(osinl_subdissector_table, fr_nlpid, next_tvb,
743                          pinfo, tree) ||
744       dissector_try_port(fr_osinl_subdissector_table, fr_nlpid, next_tvb,
745                          pinfo, tree)) {
746         /*
747          * Yes, we got a match.  Add the NLPID as a hidden item,
748          * so you can, at least, filter on it.
749          */
750         if (tree) {
751                 proto_item *hidden_item;
752                 hidden_item = proto_tree_add_uint(fr_tree, hf_fr_nlpid,
753                                     tvb, offset, 1, fr_nlpid );
754                 PROTO_ITEM_SET_HIDDEN(hidden_item);
755         }
756         return;
757   }
758
759   /*
760    * All other protocols don't.
761    *
762    * XXX - what about Cisco/Gang-of-Four LMI?  Is the 0x09 considered
763    * to be part of the LMI PDU?
764    */
765   if (tree)
766         proto_tree_add_uint(fr_tree, hf_fr_nlpid, tvb, offset, 1, fr_nlpid );
767   offset++;
768
769   switch (fr_nlpid) {
770
771   case NLPID_SNAP:
772         if (ti != NULL) {
773                 /* Include the NLPID and SNAP header in the top-level
774                    protocol tree item. */
775                 proto_item_set_end(ti, tvb, offset+5);
776         }
777         dissect_snap(tvb, offset, pinfo, tree, fr_tree, fr_ctrl,
778               hf_fr_oui, hf_fr_snaptype, hf_fr_pid, 0);
779         return;
780
781   default:
782         if (ti != NULL) {
783                 /* Include the NLPID in the top-level protocol tree item. */
784                 proto_item_set_end(ti, tvb, offset);
785         }
786         next_tvb = tvb_new_subset(tvb,offset,-1,-1);
787         if (!dissector_try_port(fr_subdissector_table,fr_nlpid,
788                                 next_tvb, pinfo, tree))
789                 call_dissector(data_handle,next_tvb, pinfo, tree);
790         break;
791   }
792 }
793
794 static void dissect_lapf(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
795 {
796         proto_tree_add_text(tree, tvb, 0, 0, "Frame relay lapf not yet implemented");
797         call_dissector(data_handle,tvb_new_subset(tvb,0,-1,-1),pinfo,tree);
798 }
799 static void dissect_fr_xid(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
800 {
801         proto_tree_add_text(tree, tvb, 0, 0, "Frame relay xid not yet implemented");
802         call_dissector(data_handle,tvb_new_subset(tvb,0,-1,-1),pinfo,tree);
803 }
804
805 /* Register the protocol with Wireshark */
806 void proto_register_fr(void)
807 {
808   static hf_register_info hf[] = {
809         { &hf_fr_ea, {
810            "EA", "fr.ea", FT_BOOLEAN, 8, TFS(&ea_string),
811             FRELAY_EA, "Extended Address", HFILL }},
812         { &hf_fr_upper_dlci, {
813            "Upper DLCI", "fr.upper_dlci", FT_UINT8, BASE_HEX,
814             NULL, FRELAY_UPPER_DLCI, "Upper bits of DLCI", HFILL }},
815         { &hf_fr_cr, {
816            "CR", "fr.cr", FT_BOOLEAN, 8, TFS(&cmd_string),
817             FRELAY_CR, "Command/Response", HFILL }},
818         { &hf_fr_second_dlci, {
819            "Second DLCI", "fr.second_dlci", FT_UINT8, BASE_HEX,
820             NULL, FRELAY_SECOND_DLCI, "Bits below upper bits of DLCI", HFILL }},
821         { &hf_fr_fecn, {
822            "FECN", "fr.fecn", FT_BOOLEAN, 8,
823             NULL, FRELAY_FECN, "Forward Explicit Congestion Notification", HFILL }},
824         { &hf_fr_becn, {
825            "BECN", "fr.becn", FT_BOOLEAN, 8,
826             NULL, FRELAY_BECN, "Backward Explicit Congestion Notification", HFILL }},
827         { &hf_fr_de, {
828            "DE", "fr.de", FT_BOOLEAN, 8,
829             NULL, FRELAY_DE, "Discard Eligibility", HFILL }},
830         { &hf_fr_third_dlci, {
831            "Third DLCI", "fr.third_dlci", FT_UINT8, BASE_HEX,
832             NULL, FRELAY_THIRD_DLCI, "Additional bits of DLCI", HFILL }},
833         { &hf_fr_dlcore_control, {
834            "DL-CORE Control", "fr.dlcore_control", FT_UINT8, BASE_HEX,
835             NULL, FRELAY_LOWER_DLCI, "DL-Core control bits", HFILL }},
836         { &hf_fr_lower_dlci, {
837            "Lower DLCI", "fr.lower_dlci", FT_UINT8, BASE_HEX,
838             NULL, FRELAY_LOWER_DLCI, "Lower bits of DLCI", HFILL }},
839         { &hf_fr_dc, {
840            "DC", "fr.dc", FT_BOOLEAN, 16, TFS(&ctrl_string),
841             FRELAY_CR, "Address/Control", HFILL }},
842         { &hf_fr_dlci, {
843            "DLCI", "fr.dlci", FT_UINT32, BASE_DEC,
844             NULL, 0x0, "Data-Link Connection Identifier", HFILL }},
845         { &hf_fr_control, {
846           "Control Field", "fr.control", FT_UINT8, BASE_HEX,
847           NULL, 0x0, "Control field", HFILL }},
848         { &hf_fr_n_r, {
849           "N(R)", "fr.control.n_r", FT_UINT16, BASE_DEC,
850           NULL, XDLC_N_R_EXT_MASK, "", HFILL }},
851         { &hf_fr_n_s, {
852           "N(S)", "fr.control.n_s", FT_UINT16, BASE_DEC,
853           NULL, XDLC_N_S_EXT_MASK, "", HFILL }},
854         { &hf_fr_p, {
855           "Poll", "fr.control.p", FT_BOOLEAN, 8,
856           TFS(&flags_set_truth), XDLC_P_F, "", HFILL }},
857         { &hf_fr_p_ext, {
858           "Poll", "fr.control.p", FT_BOOLEAN, 16,
859           TFS(&flags_set_truth), XDLC_P_F_EXT, "", HFILL }},
860         { &hf_fr_f, {
861           "Final", "fr.control.f", FT_BOOLEAN, 8,
862           TFS(&flags_set_truth), XDLC_P_F, "", HFILL }},
863         { &hf_fr_f_ext, {
864           "Final", "fr.control.f", FT_BOOLEAN, 16,
865           TFS(&flags_set_truth), XDLC_P_F_EXT, "", HFILL }},
866         { &hf_fr_s_ftype, {
867           "Supervisory frame type", "fr.control.s_ftype", FT_UINT16, BASE_HEX,
868           VALS(stype_vals), XDLC_S_FTYPE_MASK, "", HFILL }},
869         { &hf_fr_u_modifier_cmd, {
870           "Command", "fr.control.u_modifier_cmd", FT_UINT8, BASE_HEX,
871           VALS(modifier_vals_cmd), XDLC_U_MODIFIER_MASK, "", HFILL }},
872         { &hf_fr_u_modifier_resp, {
873           "Response", "fr.control.u_modifier_resp", FT_UINT8, BASE_HEX,
874             VALS(modifier_vals_resp), XDLC_U_MODIFIER_MASK, "", HFILL }},
875         { &hf_fr_ftype_i, {
876           "Frame type", "fr.control.ftype", FT_UINT16, BASE_HEX,
877           VALS(ftype_vals), XDLC_I_MASK, "", HFILL }},
878         { &hf_fr_ftype_s_u, {
879           "Frame type", "fr.control.ftype", FT_UINT8, BASE_HEX,
880           VALS(ftype_vals), XDLC_S_U_MASK, "", HFILL }},
881         { &hf_fr_ftype_s_u_ext, {
882           "Frame type", "fr.control.ftype", FT_UINT16, BASE_HEX,
883           VALS(ftype_vals), XDLC_S_U_MASK, "", HFILL }},
884         { &hf_fr_nlpid, {
885            "NLPID", "fr.nlpid", FT_UINT8, BASE_HEX,
886             VALS(fr_nlpid_vals), 0x0, "Frame Relay Encapsulated Protocol NLPID", HFILL }},
887         { &hf_fr_oui, {
888            "Organization Code", "fr.snap.oui", FT_UINT24, BASE_HEX,
889            VALS(oui_vals), 0x0, "", HFILL }},
890         { &hf_fr_pid, {
891            "Protocol ID", "fr.snap.pid", FT_UINT16, BASE_HEX,
892            NULL, 0x0, "", HFILL }},
893         { &hf_fr_snaptype, {
894            "Type", "fr.snaptype", FT_UINT16, BASE_HEX,
895             VALS(etype_vals), 0x0, "Frame Relay SNAP Encapsulated Protocol", HFILL }},
896         { &hf_fr_chdlctype, {
897            "Type", "fr.chdlctype", FT_UINT16, BASE_HEX,
898             VALS(chdlc_vals), 0x0, "Frame Relay Cisco HDLC Encapsulated Protocol", HFILL }},
899   };
900
901   /* Setup protocol subtree array */
902   static gint *ett[] = {
903     &ett_fr,
904     &ett_fr_address,
905     &ett_fr_control,
906   };
907   static enum_val_t fr_encap_options[] = {
908     { "frf-3.2", "FRF 3.2/Cisco HDLC", FRF_3_2 },
909     { "gprs-ns", "GPRS Network Service", GPRS_NS },
910     { "ethernet", "Raw Ethernet", RAW_ETHER },
911     { NULL, NULL, 0 },
912   };
913   module_t *frencap_module;
914
915   proto_fr = proto_register_protocol("Frame Relay", "FR", "fr");
916   proto_register_field_array(proto_fr, hf, array_length(hf));
917   proto_register_subtree_array(ett, array_length(ett));
918
919   fr_subdissector_table = register_dissector_table("fr.ietf",
920         "Frame Relay NLPID", FT_UINT8, BASE_HEX);
921   fr_osinl_subdissector_table = register_dissector_table("fr.osinl",
922         "Frame Relay OSI NLPID", FT_UINT8, BASE_HEX);
923
924   register_dissector("fr_uncompressed", dissect_fr_uncompressed, proto_fr);
925   register_dissector("fr", dissect_fr, proto_fr);
926
927   frencap_module = prefs_register_protocol(proto_fr, NULL);
928   /*
929    * XXX - this should really be per-circuit - I've seen at least one
930    * capture where different DLCIs have different encapsulations - but
931    * we don't yet have any support for per-circuit encapsulations.
932    *
933    * Even with that, though, we might want a default encapsulation,
934    * so that people dealing with GPRS can make gprs-ns the default.
935    */
936   prefs_register_enum_preference(frencap_module, "encap", "Encapsulation",
937                                  "Encapsulation", &fr_encap,
938                                  fr_encap_options, FALSE);
939 }
940
941 void proto_reg_handoff_fr(void)
942 {
943   dissector_handle_t fr_handle, fr_phdr_handle;
944
945   fr_handle = create_dissector_handle(dissect_fr, proto_fr);
946   dissector_add("gre.proto", ETHERTYPE_RAW_FR, fr_handle);
947   dissector_add("wtap_encap", WTAP_ENCAP_FRELAY, fr_handle);
948
949   fr_phdr_handle = create_dissector_handle(dissect_fr_phdr, proto_fr);
950   dissector_add("wtap_encap", WTAP_ENCAP_FRELAY_WITH_PHDR, fr_phdr_handle);
951
952   eth_withfcs_handle = find_dissector("eth_withfcs");
953   gprs_ns_handle = find_dissector("gprs_ns");
954   data_handle = find_dissector("data");
955
956   osinl_subdissector_table = find_dissector_table("osinl");
957 }