Obscure more email addresses.
[obnox/wireshark/wip.git] / packet-l2tp.c
1 /* packet-l2tp.c
2  * Routines for Layer Two Tunnelling Protocol (L2TP) (RFC 2661) packet
3  * disassembly
4  * John Thomes <john@ensemblecom.com>
5  *
6  * Minor changes by: (2000-01-10)
7  * Laurent Cazalet <laurent.cazalet@mailclub.net>
8  * Thomas Parvais <thomas.parvais@advalvas.be>
9  *
10  * $Id: packet-l2tp.c,v 1.37 2003/01/14 18:57:07 guy Exp $
11  *
12  * Ethereal - Network traffic analyzer
13  * By Gerald Combs <gerald@ethereal.com>
14  * Copyright 1998 Gerald Combs
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29  */
30
31 static int proto_l2tp = -1;
32 static int hf_l2tp_type = -1;
33 static int hf_l2tp_length_bit = -1;
34 static int hf_l2tp_seq_bit = -1;
35 static int hf_l2tp_offset_bit = -1;
36 static int hf_l2tp_priority = -1;
37 static int hf_l2tp_version = -1;
38 static int hf_l2tp_length = -1;
39 static int hf_l2tp_tunnel = -1;
40 static int hf_l2tp_session = -1;
41 static int hf_l2tp_Ns = -1;
42 static int hf_l2tp_Nr = -1;
43 static int hf_l2tp_offset = -1;
44 static int hf_l2tp_avp_mandatory = -1;
45 static int hf_l2tp_avp_hidden = -1;
46 static int hf_l2tp_avp_length = -1;
47 static int hf_l2tp_avp_vendor_id = -1;
48 static int hf_l2tp_avp_type = -1;
49 static int hf_l2tp_tie_breaker = -1;
50
51 #ifdef HAVE_CONFIG_H
52 #include "config.h"
53 #endif
54
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <ctype.h>
59 #include <glib.h>
60 #include <epan/packet.h>
61 #include <epan/resolv.h>
62
63 #define UDP_PORT_L2TP   1701
64
65 #define CONTROL_BIT(msg_info) (msg_info & 0x8000)   /* Type bit control = 1 data = 0 */
66 #define LENGTH_BIT(msg_info) (msg_info & 0x4000)    /* Length bit = 1  */
67 #define RESERVE_BITS(msg_info) (msg_info &0x37F8)   /* Reserved bit - usused */
68 #define SEQUENCE_BIT(msg_info) (msg_info & 0x0800)  /* SEQUENCE bit = 1 Ns and Nr fields */
69 #define OFFSET_BIT(msg_info) (msg_info & 0x0200)    /* Offset */
70 #define PRIORITY_BIT(msg_info) (msg_info & 0x0100)  /* Priority */
71 #define L2TP_VERSION(msg_info) (msg_info & 0x000f)  /* Version of l2tp */
72 #define MANDATORY_BIT(msg_info) (msg_info & 0x8000) /* Mandatory = 1 */
73 #define HIDDEN_BIT(msg_info) (msg_info & 0x4000)    /* Hidden = 1 */
74 #define AVP_LENGTH(msg_info) (msg_info & 0x03ff)    /* AVP Length */
75 #define FRAMING_SYNC(msg_info)  (msg_info & 0x0001) /* SYNC Framing Type */
76 #define FRAMING_ASYNC(msg_info) (msg_info & 0x0002) /* ASYNC Framing Type */
77 #define BEARER_DIGITAL(msg_info) (msg_info & 0x0001) /* Digital Bearer Type */
78 #define BEARER_ANALOG(msg_info) (msg_info & 0x0002) /* Analog Bearer Type */
79
80 static gint ett_l2tp = -1;
81 static gint ett_l2tp_ctrl = -1;
82 static gint ett_l2tp_avp = -1;
83 static gint ett_l2tp_lcp = -1;
84
85 #define AVP_SCCRQ      1
86 #define AVP_SCCRP      2
87 #define AVP_SCCCN      3
88 #define AVP_StopCCN    4
89 #define AVP_Reserved   5
90 #define AVP_HELLO      6
91 #define AVP_OCRQ       7
92 #define AVP_OCRP       8
93 #define AVP_ORCRP      9
94 #define AVP_ICRQ      10
95 #define AVP_ICRP      11
96 #define AVP_ICCN      12
97 #define AVP_Reserved1 13
98 #define AVP_CDN       14
99
100
101 #define NUM_CONTROL_CALL_TYPES  16
102 static const char *calltypestr[NUM_CONTROL_CALL_TYPES+1] = {
103   "Unknown Call Type           ",
104   "Start_Control_Request       ",
105   "Start_Control_Reply         ",
106   "Start_Control_Connected     ",
107   "Stop_Control_Notification   ",
108   "Reserved                    ",
109   "Hello                       ",
110   "Outgoing_Call_Request       ",
111   "Outgoing_Call_Reply         ",
112   "Outgoing_Call_Connected     ",
113   "Incoming_Call_Request       ",
114   "Incoming_Call_Reply         ",
115   "Incoming_Call_Connected     ",
116   "Reserved                    ",
117   "Call_Disconnect_Notification",
118   "WAN_Error_Notify            ",
119   "Set_Link_Info               ",
120 };
121
122 static const char *calltype_short_str[NUM_CONTROL_CALL_TYPES+1] = {
123   "Unknown ",
124   "SCCRQ   ",
125   "SCCRP   ",
126   "SCCCN   ",
127   "StopCCN ",
128   "Reserved",
129   "Hello   ",
130   "OCRQ    ",
131   "OCRP    ",
132   "OCCN    ",
133   "ICRQ    ",
134   "ICRP    ",
135   "ICCN    ",
136   "Reserved",
137   "CDN     ",
138   "WEN     ",
139   "SLI     ",
140 };
141
142
143 static const char *control_msg  = "Control Message";
144 static const char *data_msg     = "Data    Message";
145 static const value_string l2tp_type_vals[] = {
146         { 0, "Data Message" },
147         { 1, "Control Message" },
148         { 0, NULL },
149 };
150
151 static const value_string cause_code_direction_vals[] = {
152         { 0, "global error" },
153         { 1, "at peer" },
154         { 2, "at local" },
155         { 0, NULL },
156 };
157
158 static const true_false_string l2tp_length_bit_truth =
159         { "Length field is present", "Length field is not present" };
160
161 static const true_false_string l2tp_seq_bit_truth =
162         { "Ns and Nr fields are present", "Ns and Nr fields are not present" };
163
164 static const true_false_string l2tp_offset_bit_truth =
165         { "Offset Size field is present", "Offset size field is not present" };
166
167 static const true_false_string l2tp_priority_truth =
168         { "This data message has priority", "No priority" };
169
170 static const value_string authen_type_vals[] = {
171   { 0, "Reserved" },
172   { 1, "Textual username and password" },
173   { 2, "PPP CHAP" },
174   { 3, "PPP PAP" },
175   { 4, "No Authentication" },
176   { 5, "Microsoft CHAP Version 1" },
177   { 0, NULL }
178 };
179
180 #define  CONTROL_MESSAGE  0
181 #define  RESULT_ERROR_CODE 1
182 #define  PROTOCOL_VERSION  2
183 #define  FRAMING_CAPABILITIES 3
184 #define  BEARER_CAPABILITIES 4
185 #define  TIE_BREAKER 5
186 #define  FIRMWARE_REVISION 6
187 #define  HOST_NAME 7
188 #define  VENDOR_NAME 8
189 #define  ASSIGNED_TUNNEL_ID 9
190 #define  RECEIVE_WINDOW_SIZE 10
191 #define  CHALLENGE 11
192 #define  CAUSE_CODE 12
193 #define  CHALLENGE_RESPONSE 13
194 #define  ASSIGNED_SESSION 14
195 #define  CALL_SERIAL_NUMBER 15
196 #define  MINIMUM_BPS 16
197 #define  MAXIMUM_BPS 17
198 #define  BEARER_TYPE 18
199 #define  FRAMING_TYPE 19
200 #define  CALLED_NUMBER 21
201 #define  CALLING_NUMBER 22
202 #define  SUB_ADDRESS 23
203 #define  TX_CONNECT_SPEED 24
204 #define  PHYSICAL_CHANNEL 25
205 #define  INITIAL_RECEIVED_LCP_CONFREQ 26
206 #define  LAST_SENT_LCP_CONFREQ 27
207 #define  LAST_RECEIVED_LCP_CONFREQ 28
208 #define  PROXY_AUTHEN_TYPE 29
209 #define  PROXY_AUTHEN_NAME 30
210 #define  PROXY_AUTHEN_CHALLENGE 31
211 #define  PROXY_AUTHEN_ID 32
212 #define  PROXY_AUTHEN_RESPONSE 33
213 #define  CALL_STATUS_AVPS 34
214 #define  ACCM 35
215 #define  RANDOM_VECTOR 36
216 #define  PRIVATE_GROUP_ID 37
217 #define  RX_CONNECT_SPEED 38
218 #define  SEQUENCING_REQUIRED 39
219 #define  PPP_DISCONNECT_CAUSE_CODE 46   /* RFC 3145 */
220
221 #define NUM_AVP_TYPES  40
222 static const value_string avp_type_vals[] = {
223   { CONTROL_MESSAGE,           "Control Message" },
224   { RESULT_ERROR_CODE,         "Result-Error Code" },
225   { PROTOCOL_VERSION,          "Protocol Version" },
226   { FRAMING_CAPABILITIES,      "Framing Capabilities" },
227   { BEARER_CAPABILITIES,       "Bearer Capabilities" },
228   { TIE_BREAKER,               "Tie Breaker" },
229   { FIRMWARE_REVISION,         "Firmware Revision" },
230   { HOST_NAME,                 "Host Name" },
231   { VENDOR_NAME,               "Vendor Name" },
232   { ASSIGNED_TUNNEL_ID,        "Assigned Tunnel ID" },
233   { RECEIVE_WINDOW_SIZE,       "Receive Window Size" },
234   { CHALLENGE,                 "Challenge" },
235   { CAUSE_CODE,                "Cause Code" },
236   { CHALLENGE_RESPONSE,        "Challenge Response" },
237   { ASSIGNED_SESSION,          "Assigned Session" },
238   { CALL_SERIAL_NUMBER,        "Call Serial Number" },
239   { MINIMUM_BPS,               "Minimum BPS" },
240   { MAXIMUM_BPS,               "Maximum BPS" },
241   { BEARER_TYPE,               "Bearer Type" },
242   { FRAMING_TYPE,              "Framing Type" },
243   { CALLED_NUMBER,             "Called Number" },
244   { CALLING_NUMBER,            "Calling Number" },
245   { SUB_ADDRESS,               "Sub-Address" },
246   { TX_CONNECT_SPEED,          "Connect Speed" },
247   { PHYSICAL_CHANNEL,          "Physical Channel" },
248   { INITIAL_RECEIVED_LCP_CONFREQ, "Initial Received LCP CONFREQ" },
249   { LAST_SENT_LCP_CONFREQ,     "Last Sent LCP CONFREQ" },
250   { LAST_RECEIVED_LCP_CONFREQ, "Last Received LCP CONFREQ" },
251   { PROXY_AUTHEN_TYPE,         "Proxy Authen Type" },
252   { PROXY_AUTHEN_NAME,         "Proxy Authen Name" },
253   { PROXY_AUTHEN_CHALLENGE,    "Proxy Authen Challenge" },
254   { PROXY_AUTHEN_ID,           "Proxy Authen ID" },
255   { PROXY_AUTHEN_RESPONSE,     "Proxy Authen Response" },
256   { CALL_STATUS_AVPS,          "Call status AVPs" },
257   { ACCM,                      "ACCM" },
258   { RANDOM_VECTOR,             "Random Vector" },
259   { PRIVATE_GROUP_ID,          "Private group ID" },
260   { RX_CONNECT_SPEED,          "RxConnect Speed" },
261   { SEQUENCING_REQUIRED,       "Sequencing Required" },
262   { PPP_DISCONNECT_CAUSE_CODE, "PPP Disconnect Cause Code" },
263   { 0,                         NULL }
264 };
265
266 /*
267  * These are SMI Network Management Private Enterprise Codes for
268  * organizations; see
269  *
270  *      http://www.isi.edu/in-notes/iana/assignments/enterprise-numbers
271  *
272  * for a list.
273  */
274 #define VENDOR_IETF 0
275 #define VENDOR_ACC 5
276 #define VENDOR_CISCO 9
277 #define VENDOR_SHIVA 166
278 #define VENDOR_LIVINGSTON 307
279 #define VENDOR_3COM 429
280 #define VENDOR_ASCEND 529
281 #define VENDOR_BAY 1584
282 #define VENDOR_REDBACK 2352
283 #define VENDOR_JUNIPER 2636
284 #define VENDOR_COSINE 3085
285 #define VENDOR_UNISPHERE 4874
286
287 static const value_string avp_vendor_id_vals[] =
288 {{VENDOR_IETF,"IETF"},
289 {VENDOR_ACC,"ACC"},
290 {VENDOR_CISCO,"Cisco"},
291 {VENDOR_SHIVA,"Shiva"},
292 {VENDOR_LIVINGSTON,"Livingston"},
293 {VENDOR_3COM,"3Com"},
294 {VENDOR_ASCEND,"Ascend"},
295 {VENDOR_BAY,"Bay Networks"},
296 {VENDOR_REDBACK,"Redback"},
297 {VENDOR_JUNIPER,"Juniper Networks"},
298 {VENDOR_COSINE,"CoSine Communications"},
299 {VENDOR_UNISPHERE,"Unisphere Networks"},
300 {0,NULL}};
301
302 static gchar textbuffer[200];
303
304 static dissector_handle_t ppp_hdlc_handle;
305 static dissector_handle_t ppp_lcp_options_handle;
306
307 static void
308 dissect_l2tp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
309 {
310   proto_tree *l2tp_tree=NULL, *l2tp_avp_tree, *l2tp_lcp_avp_tree, *ctrl_tree;
311   proto_item *l2tp_item = NULL, *ti, *tf, *te;
312   int rhcode;
313   int index = 0;
314   int tmp_index;
315   guint16 length = 0;           /* Length field */
316   guint16 tid;                  /* Tunnel ID */
317   guint16 cid;                  /* Call ID */
318   guint16 offset_size;          /* Offset size */
319   guint16 ver_len_hidden;
320   guint16 avp_vendor_id;
321   guint16 avp_type;
322   guint16 msg_type;
323   guint16 avp_len;
324   guint16 result_code;
325   guint16 error_code;
326   guint32 bits;
327   guint16 firmware_rev;
328   guint16       control;
329   tvbuff_t      *next_tvb;
330
331   if (check_col(pinfo->cinfo, COL_PROTOCOL))    /* build output for closed L2tp frame displayed  */
332         col_set_str(pinfo->cinfo, COL_PROTOCOL, "L2TP");
333   if (check_col(pinfo->cinfo, COL_INFO))
334         col_clear(pinfo->cinfo, COL_INFO);
335
336   control = tvb_get_ntohs(tvb, 0);
337
338   if (L2TP_VERSION(control) != 2) {
339           if (check_col(pinfo->cinfo, COL_INFO)) {
340                 col_add_fstr(pinfo->cinfo, COL_INFO, "L2TP Version %u", L2TP_VERSION(control) );
341           }
342           return;
343   }
344
345   rhcode= 10;
346
347   if (LENGTH_BIT(control)) {            /* length field included ? */
348       index += 2;                       /* skip ahead */
349       length = tvb_get_ntohs(tvb, index);
350   }
351
352   /* collect the tunnel id & call id */
353   index += 2;
354   tid = tvb_get_ntohs(tvb, index);
355   index += 2;
356   cid = tvb_get_ntohs(tvb, index);
357
358   if (check_col(pinfo->cinfo, COL_INFO)) {
359         if (CONTROL_BIT(control)) {
360             /* CONTROL MESSAGE */
361             tmp_index = index;
362
363               if ((LENGTH_BIT(control))&&(length==12))                  /* ZLB Message */
364                   sprintf(textbuffer,"%s - ZLB      (tunnel id=%d, session id=%d)",
365                           control_msg , tid ,cid);
366               else
367               {
368                 if (SEQUENCE_BIT(control)) {
369                     tmp_index += 4;
370                 }
371
372                 tmp_index+=4;
373
374                 avp_type = tvb_get_ntohs(tvb, (tmp_index+=2));
375
376                 if (avp_type == CONTROL_MESSAGE)
377                 {
378                     /* We print message type */
379                     msg_type = tvb_get_ntohs(tvb, (tmp_index+=2));
380                     sprintf(textbuffer,"%s - %s (tunnel id=%d, session id=%d)",
381                             control_msg ,
382                             ((NUM_CONTROL_CALL_TYPES + 1 ) > msg_type) ?
383                             calltype_short_str[msg_type] : "Unknown",
384                             tid ,cid);
385                 }
386                 else
387                 {
388                     /*
389                      * This is not a control message.
390                      * We never pass here except in case of bad l2tp packet!
391                      */
392                     sprintf(textbuffer,"%s (tunnel id=%d, session id=%d)",
393                             control_msg ,  tid ,cid);
394
395                 }
396               }
397         }
398         else {
399             /* DATA Message */
400                sprintf(textbuffer,"%s            (tunnel id=%d, session id=%d)",
401                        data_msg, tid ,cid);
402         }
403         col_add_fstr(pinfo->cinfo,COL_INFO,textbuffer);
404   }
405
406   if (LENGTH_BIT(control)) {
407         /*
408          * Set the length of this tvbuff to be no longer than the length
409          * in the header.
410          *
411          * XXX - complain if that length is longer than the length of
412          * the tvbuff?  Have "set_actual_length()" return a Boolean
413          * and have its callers check the result?
414          */
415         set_actual_length(tvb, length);
416   }
417
418   if (tree) {
419         l2tp_item = proto_tree_add_item(tree,proto_l2tp, tvb, 0, -1, FALSE);
420         l2tp_tree = proto_item_add_subtree(l2tp_item, ett_l2tp);
421
422         ti = proto_tree_add_text(l2tp_tree, tvb, 0, 2,
423                         "Packet Type: %s Tunnel Id=%d Session Id=%d",
424                         (CONTROL_BIT(control) ? control_msg : data_msg), tid, cid);
425
426         ctrl_tree = proto_item_add_subtree(ti, ett_l2tp_ctrl);
427         proto_tree_add_uint(ctrl_tree, hf_l2tp_type, tvb, 0, 2, control);
428         proto_tree_add_boolean(ctrl_tree, hf_l2tp_length_bit, tvb, 0, 2, control);
429         proto_tree_add_boolean(ctrl_tree, hf_l2tp_seq_bit, tvb, 0, 2, control);
430         proto_tree_add_boolean(ctrl_tree, hf_l2tp_offset_bit, tvb, 0, 2, control);
431         proto_tree_add_boolean(ctrl_tree, hf_l2tp_priority, tvb, 0, 2, control);
432         proto_tree_add_uint(ctrl_tree, hf_l2tp_version, tvb, 0, 2, control);
433   }
434   index = 2;
435   if (LENGTH_BIT(control)) {
436           if (tree) {
437                 proto_tree_add_item(l2tp_tree, hf_l2tp_length, tvb, index, 2, FALSE);
438           }
439         index += 2;
440   }
441
442   if (tree) {
443         proto_tree_add_item(l2tp_tree, hf_l2tp_tunnel, tvb, index, 2, FALSE);
444   }
445   index += 2;
446   if (tree) {
447         proto_tree_add_item(l2tp_tree, hf_l2tp_session, tvb, index, 2, FALSE);
448   }
449   index += 2;
450
451   if (SEQUENCE_BIT(control)) {
452           if (tree) {
453                 proto_tree_add_item(l2tp_tree, hf_l2tp_Ns, tvb, index, 2, FALSE);
454           }
455           index += 2;
456           if (tree) {
457                 proto_tree_add_item(l2tp_tree, hf_l2tp_Nr, tvb, index, 2, FALSE);
458           }
459           index += 2;
460   }
461   if (OFFSET_BIT(control)) {
462         offset_size = tvb_get_ntohs(tvb, index);
463         if (tree) {
464                 proto_tree_add_uint(l2tp_tree, hf_l2tp_offset, tvb, index, 2,
465                     offset_size);
466         }
467         index += 2;
468         if (offset_size != 0) {
469                 if (tree) {
470                         proto_tree_add_text(l2tp_tree, tvb, index, offset_size, "Offset Padding");
471                 }
472                 index += offset_size;
473         }
474   }
475   if (tree && (LENGTH_BIT(control))&&(length==12)) {
476             proto_tree_add_text(l2tp_tree, tvb, 0, 0, "Zero Length Bit message");
477   }
478
479   if (!CONTROL_BIT(control)) {  /* Data Messages so we are done */
480         if (tree)
481                 proto_item_set_len(l2tp_item, index);
482         /* If we have data, signified by having a length bit, dissect it */
483         if (tvb_offset_exists(tvb, index)) {
484                 next_tvb = tvb_new_subset(tvb, index, -1, -1);
485                 call_dissector(ppp_hdlc_handle, next_tvb, pinfo, tree);
486         }
487         return;
488   }
489
490   if (tree) {
491         if (!LENGTH_BIT(control)) {
492                 return;
493         }
494         while (index < length ) {    /* Process AVP's */
495                 ver_len_hidden  = tvb_get_ntohs(tvb, index);
496                 avp_len         = AVP_LENGTH(ver_len_hidden);
497                 avp_vendor_id   = tvb_get_ntohs(tvb, index + 2);
498                 avp_type        = tvb_get_ntohs(tvb, index + 4);
499
500                 if (avp_vendor_id == VENDOR_IETF) {
501                         tf =  proto_tree_add_text(l2tp_tree, tvb, index,
502                         avp_len, "%s AVP",
503                         val_to_str(avp_type, avp_type_vals, "Unknown (%u)"));
504                 } else {         /* Vendor-Specific AVP */
505                         tf =  proto_tree_add_text(l2tp_tree, tvb, index,
506                         avp_len, "Vendor %s AVP",
507                         val_to_str(avp_vendor_id, avp_vendor_id_vals, "Unknown (%u)"));
508                 }
509
510                 l2tp_avp_tree = proto_item_add_subtree(tf,  ett_l2tp_avp);
511
512                 proto_tree_add_boolean_format(l2tp_avp_tree,hf_l2tp_avp_mandatory, tvb, index, 1,
513                                            rhcode, "Mandatory: %s",
514                                            (MANDATORY_BIT(ver_len_hidden)) ? "True" : "False" );
515                 proto_tree_add_boolean_format(l2tp_avp_tree,hf_l2tp_avp_hidden, tvb, index, 1,
516                                            rhcode, "Hidden: %s",
517                                            (HIDDEN_BIT(ver_len_hidden)) ? "True" : "False" );
518                 proto_tree_add_uint_format(l2tp_avp_tree,hf_l2tp_avp_length, tvb, index, 2,
519                                            rhcode, "Length: %u", avp_len);
520                 if (HIDDEN_BIT(ver_len_hidden)) { /* don't try do display hidden */
521                         index += avp_len;
522                         continue;
523                 }
524
525                 if (avp_len == 0) {
526                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 0,
527                           "AVP length must not be zero");
528                         return;
529                 }
530                 index += 2;
531                 avp_len -= 2;
532
533                 proto_tree_add_item(l2tp_avp_tree, hf_l2tp_avp_vendor_id,
534                     tvb, index, 2, FALSE);
535                 index += 2;
536                 avp_len -= 2;
537
538                 if (avp_vendor_id != VENDOR_IETF) {
539                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 2,
540                                             "Type: %u", avp_type);
541                         index += 2;
542                         avp_len -= 2;
543
544                         /* For the time being, we don't decode any Vendor-
545                            specific AVP. */
546                         proto_tree_add_text(l2tp_avp_tree, tvb, index,
547                                             avp_len, "Vendor-Specific AVP");
548
549                         index += avp_len;
550                         continue;
551                 }
552
553                 proto_tree_add_uint(l2tp_avp_tree, hf_l2tp_avp_type,
554                     tvb, index, 2, avp_type);
555                 index += 2;
556                 avp_len -= 2;
557
558                 switch (avp_type) {
559
560                 case CONTROL_MESSAGE:
561                         msg_type = tvb_get_ntohs(tvb, index);
562                         proto_tree_add_text(l2tp_avp_tree,tvb, index, 2,
563                           "Control Message Type: (%u) %s", msg_type,
564                           ((NUM_CONTROL_CALL_TYPES + 1 ) > msg_type) ?
565                           calltypestr[msg_type] : "Unknown");
566                         break;
567
568                 case RESULT_ERROR_CODE:
569                         if (avp_len < 2)
570                                 break;
571                         result_code = tvb_get_ntohs(tvb, index);
572                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 2,
573                           "Result code: %u",  result_code);
574                         index += 2;
575                         avp_len -= 2;
576
577                         if (avp_len < 2)
578                                 break;
579                         error_code = tvb_get_ntohs(tvb, index);
580                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 2,
581                           "Error code: %u", error_code);
582                         index += 2;
583                         avp_len -= 2;
584
585                         if (avp_len == 0)
586                                 break;
587                         proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
588                           "Error Message: %.*s", avp_len,
589                           tvb_get_ptr(tvb, index, avp_len));
590                         break;
591
592                 case PROTOCOL_VERSION:
593                         if (avp_len < 1)
594                                 break;
595                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 1,
596                           "Version: %u", tvb_get_guint8(tvb, index));
597                         index += 1;
598                         avp_len -= 1;
599
600                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 1,
601                           "Revision: %u", tvb_get_guint8(tvb, index));
602                         break;
603
604                 case FRAMING_CAPABILITIES:
605                         bits = tvb_get_ntohl(tvb, index);
606                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
607                           "Async Framing Supported: %s",
608                           (FRAMING_ASYNC(bits)) ? "True" : "False");
609                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
610                           "Sync Framing Supported: %s",
611                           (FRAMING_SYNC(bits)) ? "True" : "False");
612                         break;
613
614                 case BEARER_CAPABILITIES:
615                         bits = tvb_get_ntohl(tvb, index);
616                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
617                           "Analog Access Supported: %s",
618                           (BEARER_ANALOG(bits)) ? "True" : "False");
619                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
620                           "Digital Access Supported: %s",
621                           (BEARER_DIGITAL(bits)) ? "True" : "False");
622                         break;
623
624                 case TIE_BREAKER:
625                         proto_tree_add_item(l2tp_avp_tree, hf_l2tp_tie_breaker, tvb, index, 8, FALSE);
626                         break;
627
628                 case FIRMWARE_REVISION:
629                         firmware_rev = tvb_get_ntohs(tvb, index);
630                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 2,
631                           "Firmware Revision: %d 0x%x", firmware_rev,firmware_rev );
632                         break;
633
634                 case HOST_NAME:
635                         proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
636                           "Host Name: %.*s", avp_len,
637                           tvb_get_ptr(tvb, index, avp_len));
638                         break;
639
640                 case VENDOR_NAME:
641                         proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
642                           "Vendor Name: %.*s", avp_len,
643                           tvb_get_ptr(tvb, index, avp_len));
644                         break;
645
646                 case ASSIGNED_TUNNEL_ID:
647                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 2,
648                           "Tunnel ID: %u", tvb_get_ntohs(tvb, index));
649                         break;
650
651                 case RECEIVE_WINDOW_SIZE:
652                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 2,
653                           "Receive Window Size: %u",
654                           tvb_get_ntohs(tvb, index));
655                         break;
656
657                 case CHALLENGE:
658                         proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
659                           "CHAP Challenge: %s",
660                           tvb_bytes_to_str(tvb, index, avp_len));
661                         break;
662
663                 case CAUSE_CODE:
664                         /*
665                          * XXX - export stuff from the Q.931 dissector
666                          * to dissect the cause code and cause message,
667                          * and use it.
668                          */
669                         if (avp_len < 2)
670                                 break;
671                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 2,
672                           "Cause Code: %u",
673                           tvb_get_ntohs(tvb, index));
674                         index += 2;
675                         avp_len -= 2;
676
677                         if (avp_len < 1)
678                                 break;
679                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 1,
680                           "Cause Msg: %u",
681                           tvb_get_guint8(tvb, index));
682                         index += 1;
683                         avp_len -= 1;
684
685                         if (avp_len == 0)
686                                 break;
687                         proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
688                           "Advisory Msg: %.*s", avp_len,
689                           tvb_get_ptr(tvb, index, avp_len));
690                         break;
691
692                 case CHALLENGE_RESPONSE:
693                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 16,
694                           "CHAP Challenge Response: %s",
695                           tvb_bytes_to_str(tvb, index, 16));
696                         break;
697
698                 case ASSIGNED_SESSION:
699                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 2,
700                           "Assigned Session: %u",
701                           tvb_get_ntohs(tvb, index));
702                         break;
703
704                 case CALL_SERIAL_NUMBER:
705                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
706                           "Call Serial Number: %u",
707                           tvb_get_ntohl(tvb, index));
708                         break;
709
710                 case MINIMUM_BPS:
711                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
712                           "Minimum BPS: %u",
713                           tvb_get_ntohl(tvb, index));
714                         break;
715
716                 case MAXIMUM_BPS:
717                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
718                           "Maximum BPS: %u",
719                           tvb_get_ntohl(tvb, index));
720                         break;
721
722                 case BEARER_TYPE:
723                         bits = tvb_get_ntohl(tvb, index);
724                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
725                           "Analog Bearer Type: %s",
726                           (BEARER_ANALOG(bits)) ? "True" : "False");
727                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
728                           "Digital Bearer Type: %s",
729                           (BEARER_DIGITAL(bits)) ? "True" : "False");
730                         break;
731
732                 case FRAMING_TYPE:
733                         bits = tvb_get_ntohl(tvb, index);
734                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
735                           "Async Framing Type: %s",
736                           (FRAMING_ASYNC(bits)) ? "True" : "False");
737                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
738                           "Sync Framing Type: %s",
739                           (FRAMING_SYNC(bits)) ? "True" : "False");
740                         break;
741
742                 case CALLED_NUMBER:
743                         if (avp_len == 0)
744                                 break;
745                         proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
746                           "Called Number: %.*s", avp_len,
747                           tvb_get_ptr(tvb, index, avp_len));
748                         break;
749
750                 case CALLING_NUMBER:
751                         if (avp_len == 0)
752                                 break;
753                         proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
754                           "Calling Number: %.*s", avp_len,
755                           tvb_get_ptr(tvb, index, avp_len));
756                         break;
757
758                 case SUB_ADDRESS:
759                         if (avp_len == 0)
760                                 break;
761                         proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
762                           "Sub-Address: %.*s", avp_len,
763                           tvb_get_ptr(tvb, index, avp_len));
764                         break;
765
766                 case TX_CONNECT_SPEED:
767                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
768                           "Connect Speed: %u",
769                           tvb_get_ntohl(tvb, index));
770                         break;
771
772                 case PHYSICAL_CHANNEL:
773                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
774                           "Physical Channel: %u",
775                           tvb_get_ntohl(tvb, index));
776                         break;
777
778                 case INITIAL_RECEIVED_LCP_CONFREQ:
779                         te = proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
780                                "Initial Received LCP CONFREQ: %s",
781                                tvb_bytes_to_str(tvb, index, avp_len));
782                         l2tp_lcp_avp_tree = proto_item_add_subtree(te, ett_l2tp_lcp);
783                         next_tvb = tvb_new_subset(tvb, index, avp_len, avp_len);
784                         call_dissector(ppp_lcp_options_handle, next_tvb, pinfo, l2tp_lcp_avp_tree );
785                         break;
786
787                 case LAST_SENT_LCP_CONFREQ:
788                         te = proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
789                                 "Last Sent LCP CONFREQ: %s",
790                                 tvb_bytes_to_str(tvb, index, avp_len));
791                         l2tp_lcp_avp_tree = proto_item_add_subtree(te, ett_l2tp_lcp);
792                         next_tvb = tvb_new_subset(tvb, index, avp_len, avp_len);
793                         call_dissector(ppp_lcp_options_handle, next_tvb, pinfo, l2tp_lcp_avp_tree );
794                         break;
795
796                 case LAST_RECEIVED_LCP_CONFREQ:
797                         te = proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
798                                "Last Received LCP CONFREQ: %s",
799                                tvb_bytes_to_str(tvb, index, avp_len));
800                         l2tp_lcp_avp_tree = proto_item_add_subtree(te, ett_l2tp_lcp);
801                         next_tvb = tvb_new_subset(tvb, index, avp_len, avp_len);
802                         call_dissector(ppp_lcp_options_handle, next_tvb, pinfo, l2tp_lcp_avp_tree );
803                         break;
804
805                 case PROXY_AUTHEN_TYPE:
806                         msg_type = tvb_get_ntohs(tvb, index);
807                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 2,
808                           "Proxy Authen Type: %s",
809                           val_to_str(msg_type, authen_type_vals, "Unknown (%u)"));
810                         break;
811
812                 case PROXY_AUTHEN_NAME:
813                         if (avp_len == 0)
814                                 break;
815                         proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
816                           "Proxy Authen Name: %.*s", avp_len,
817                           tvb_get_ptr(tvb, index, avp_len));
818                         break;
819
820                 case PROXY_AUTHEN_CHALLENGE:
821                         proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
822                           "Proxy Authen Challenge: %s",
823                           tvb_bytes_to_str(tvb, index, avp_len));
824                         break;
825
826                 case PROXY_AUTHEN_ID:
827                         proto_tree_add_text(l2tp_avp_tree, tvb, index + 1, 1,
828                           "Proxy Authen ID: %u",
829                           tvb_get_guint8(tvb, index + 1));
830                         break;
831
832                 case PROXY_AUTHEN_RESPONSE:
833                         proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
834                           "Proxy Authen Response: %s",
835                           tvb_bytes_to_str(tvb, index, avp_len));
836                         break;
837
838                 case CALL_STATUS_AVPS:
839                         if (avp_len < 2)
840                                 break;
841                         index += 2;
842                         avp_len -= 2;
843
844                         if (avp_len < 4)
845                                 break;
846                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
847                           "CRC Errors: %u", tvb_get_ntohl(tvb, index));
848                         index += 4;
849                         avp_len -= 4;
850
851                         if (avp_len < 4)
852                                 break;
853                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
854                           "Framing Errors: %u", tvb_get_ntohl(tvb, index));
855                         index += 4;
856                         avp_len -= 4;
857
858                         if (avp_len < 4)
859                                 break;
860                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
861                           "Hardware Overruns: %u", tvb_get_ntohl(tvb, index));
862                         index += 4;
863                         avp_len -= 4;
864
865                         if (avp_len < 4)
866                                 break;
867                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
868                           "Buffer Overruns: %u", tvb_get_ntohl(tvb, index));
869                         index += 4;
870                         avp_len -= 4;
871
872                         if (avp_len < 4)
873                                 break;
874                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
875                           "Time-out Errors: %u", tvb_get_ntohl(tvb, index));
876                         index += 4;
877                         avp_len -= 4;
878
879                         if (avp_len < 4)
880                                 break;
881                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
882                           "Alignment Errors: %u", tvb_get_ntohl(tvb, index));
883                         index += 4;
884                         avp_len -= 4;
885                         break;
886
887                 case ACCM:
888                         if (avp_len < 2)
889                                 break;
890                         index += 2;
891                         avp_len -= 2;
892
893                         if (avp_len < 4)
894                                 break;
895                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
896                           "Send ACCM: %u", tvb_get_ntohl(tvb, index));
897                         index += 4;
898                         avp_len -= 4;
899
900                         if (avp_len < 4)
901                                 break;
902                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
903                           "Receive ACCM: %u", tvb_get_ntohl(tvb, index));
904                         index += 4;
905                         avp_len -= 4;
906                         break;
907
908                 case RANDOM_VECTOR:
909                         proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
910                           "Random Vector: %s",
911                           tvb_bytes_to_str(tvb, index, avp_len));
912                         break;
913
914                 case PRIVATE_GROUP_ID:
915                         proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
916                           "Private Group ID: %s",
917                           tvb_bytes_to_str(tvb, index, avp_len));
918                         break;
919
920                 case RX_CONNECT_SPEED:
921                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 4,
922                           "Rx Connect Speed: %u",
923                           tvb_get_ntohl(tvb, index));
924                         break;
925
926                 case PPP_DISCONNECT_CAUSE_CODE:
927                         if (avp_len < 2)
928                                 break;
929                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 2,
930                           "Disconnect Code: %u",
931                           tvb_get_ntohs(tvb, index));
932                         index += 2;
933                         avp_len -= 2;
934
935                         if (avp_len < 2)
936                                 break;
937                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 2,
938                           "Control Protocol Number: %u",
939                           tvb_get_ntohs(tvb, index));
940                         index += 2;
941                         avp_len -= 2;
942
943                         if (avp_len < 1)
944                                 break;
945                         proto_tree_add_text(l2tp_avp_tree, tvb, index, 1,
946                           "Direction: %s",
947                           val_to_str(tvb_get_guint8(tvb, index),
948                                      cause_code_direction_vals,
949                                      "Reserved (%u)"));
950                         index += 1;
951                         avp_len -= 1;
952
953                         if (avp_len == 0)
954                                 break;
955                         proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
956                           "Message: %.*s", avp_len,
957                           tvb_get_ptr(tvb, index, avp_len));
958                         break;
959
960                 default:
961                         proto_tree_add_text(l2tp_avp_tree, tvb, index, avp_len,
962                           "Unknown AVP");
963                         break;
964                 }
965
966                 /* printf("Avp Decode avp_len= %d index= %d length= %d %x\n ",avp_len,
967                    index,length,length); */
968
969                 index += avp_len;
970         }
971
972   }
973 }
974
975 /* registration with the filtering engine */
976 void
977 proto_register_l2tp(void)
978 {
979         static hf_register_info hf[] = {
980                 { &hf_l2tp_type,
981                 { "Type", "l2tp.type", FT_UINT16, BASE_DEC, VALS(l2tp_type_vals), 0x8000,
982                         "Type bit", HFILL }},
983
984                 { &hf_l2tp_length_bit,
985                 { "Length Bit", "l2tp.length_bit", FT_BOOLEAN, 16, TFS(&l2tp_length_bit_truth), 0x4000,
986                         "Length bit", HFILL }},
987
988                 { &hf_l2tp_seq_bit,
989                 { "Sequence Bit", "l2tp.seq_bit", FT_BOOLEAN, 16, TFS(&l2tp_seq_bit_truth), 0x0800,
990                         "Sequence bit", HFILL }},
991
992                 { &hf_l2tp_offset_bit,
993                 { "Offset bit", "l2tp.offset_bit", FT_BOOLEAN, 16, TFS(&l2tp_offset_bit_truth), 0x0200,
994                         "Offset bit", HFILL }},
995
996                 { &hf_l2tp_priority,
997                 { "Priority", "l2tp.priority", FT_BOOLEAN, 16, TFS(&l2tp_priority_truth), 0x0100,
998                         "Priority bit", HFILL }},
999
1000                 { &hf_l2tp_version,
1001                 { "Version", "l2tp.version", FT_UINT16, BASE_DEC, NULL, 0x000f,
1002                         "Version", HFILL }},
1003
1004                 { &hf_l2tp_length,
1005                 { "Length","l2tp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
1006                         "", HFILL }},
1007
1008                 { &hf_l2tp_tunnel,
1009                 { "Tunnel ID","l2tp.tunnel", FT_UINT16, BASE_DEC, NULL, 0x0, /* Probably should be FT_BYTES */
1010                         "Tunnel ID", HFILL }},
1011
1012                 { &hf_l2tp_session,
1013                 { "Session ID","l2tp.session", FT_UINT16, BASE_DEC, NULL, 0x0, /* Probably should be FT_BYTES */
1014                         "Session ID", HFILL }},
1015
1016                 { &hf_l2tp_Ns,
1017                 { "Ns","l2tp.Ns", FT_UINT16, BASE_DEC, NULL, 0x0,
1018                         "", HFILL }},
1019
1020                 { &hf_l2tp_Nr,
1021                 { "Nr","l2tp.Nr", FT_UINT16, BASE_DEC, NULL, 0x0,
1022                         "", HFILL }},
1023
1024                 { &hf_l2tp_offset,
1025                 { "Offset","l2tp.offset", FT_UINT16, BASE_DEC, NULL, 0x0,
1026                         "Number of octest past the L2TP header at which the"
1027                                 "payload data starts.", HFILL }},
1028
1029                 { &hf_l2tp_avp_mandatory,
1030                 { "Mandatory", "l2tp.avp.mandatory", FT_BOOLEAN, BASE_NONE, NULL, 0,
1031                         "Mandatory AVP", HFILL }},
1032
1033                 { &hf_l2tp_avp_hidden,
1034                 { "Hidden", "l2tp.avp.hidden", FT_BOOLEAN, BASE_NONE, NULL, 0,
1035                         "Hidden AVP", HFILL }},
1036
1037                 { &hf_l2tp_avp_length,
1038                 { "Length", "l2tp.avp.length", FT_UINT16, BASE_DEC, NULL, 0,
1039                         "AVP Length", HFILL }},
1040
1041                 { &hf_l2tp_avp_vendor_id,
1042                 { "Vendor ID", "l2tp.avp.vendor_id", FT_UINT16, BASE_DEC, VALS(avp_vendor_id_vals), 0,
1043                         "AVP Vendor ID", HFILL }},
1044
1045                 { &hf_l2tp_avp_type,
1046                 { "Type", "l2tp.avp.type", FT_UINT16, BASE_DEC, VALS(avp_type_vals), 0,
1047                         "AVP Type", HFILL }},
1048
1049                 { &hf_l2tp_tie_breaker,
1050                 { "Tie Breaker", "l2tp.tie_breaker", FT_UINT64, BASE_HEX, NULL, 0,
1051                         "Tie Breaker", HFILL }},
1052
1053         };
1054
1055         static gint *ett[] = {
1056                 &ett_l2tp,
1057                 &ett_l2tp_ctrl,
1058                 &ett_l2tp_avp,
1059                 &ett_l2tp_lcp,
1060         };
1061
1062         proto_l2tp = proto_register_protocol(
1063                 "Layer 2 Tunneling Protocol", "L2TP", "l2tp");
1064         proto_register_field_array(proto_l2tp, hf, array_length(hf));
1065         proto_register_subtree_array(ett, array_length(ett));
1066 }
1067
1068 void
1069 proto_reg_handoff_l2tp(void)
1070 {
1071         dissector_handle_t l2tp_handle;
1072
1073         l2tp_handle = create_dissector_handle(dissect_l2tp, proto_l2tp);
1074         dissector_add("udp.port", UDP_PORT_L2TP, l2tp_handle);
1075
1076         /*
1077          * Get a handle for the PPP-in-HDLC-like-framing dissector.
1078          */
1079         ppp_hdlc_handle = find_dissector("ppp_hdlc");
1080         ppp_lcp_options_handle = find_dissector("ppp_lcp_options");
1081
1082 }