Show unknown AFP command codes in decimal in the summary line, as we
[obnox/wireshark/wip.git] / packet-pppoe.c
1 /* packet-pppoe.c
2  * Routines for PPP Over Ethernet (PPPoE) packet disassembly (RFC2516)
3  *
4  * $Id: packet-pppoe.c,v 1.21 2002/01/21 07:36:38 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  * 
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include <epan/strutil.h>
36 #include "etypes.h"
37
38 static int proto_pppoed = -1;
39
40 static gint ett_pppoed = -1;
41 static gint ett_pppoed_tags = -1;
42
43 static int proto_pppoes = -1;
44
45 static dissector_handle_t ppp_handle;
46
47 /* For lack of a better source, I made up the following defines. -jsj */
48
49 #define PPPOE_CODE_SESSION 0x00
50 #define PPPOE_CODE_PADO 0x7
51 #define PPPOE_CODE_PADI 0x9
52 #define PPPOE_CODE_PADR 0x19
53 #define PPPOE_CODE_PADS 0x65
54 #define PPPOE_CODE_PADT 0xa7
55
56 #define PPPOE_TAG_EOL 0x0000 
57 #define PPPOE_TAG_SVC_NAME 0x0101
58 #define PPPOE_TAG_AC_NAME 0x0102 
59 #define PPPOE_TAG_HOST_UNIQ 0x0103
60 #define PPPOE_TAG_AC_COOKIE 0x0104
61 #define PPPOE_TAG_VENDOR 0x0105 
62 #define PPPOE_TAG_RELAY_ID 0x0110 
63 #define PPPOE_TAG_SVC_ERR 0x0201 
64 #define PPPOE_TAG_AC_ERR 0x0202 
65 #define PPPOE_TAG_GENERIC_ERR 0x0203
66
67 static gchar *
68 pppoecode_to_str(guint8 codetype, const char *fmt) {
69         static const value_string code_vals[] = {
70                 {PPPOE_CODE_SESSION, "Session Data"                             },
71                 {PPPOE_CODE_PADO, "Active Discovery Offer (PADO)"               },
72                 {PPPOE_CODE_PADI, "Active Discovery Initiation (PADI)"          },
73                 {PPPOE_CODE_PADR, "Active Discovery Request (PADR)"             },
74                 {PPPOE_CODE_PADS, "Active Discovery Session-confirmation (PADS)"},
75                 {PPPOE_CODE_PADT, "Active Discovery Terminate (PADT)"           },
76                 {0,     NULL                                                        } };
77
78                 return val_to_str(codetype, code_vals, fmt);
79 }
80
81 static gchar *
82 pppoetag_to_str(guint16 tag_type, const char *fmt) {
83         static const value_string code_vals[] = {
84                 {PPPOE_TAG_EOL,        "End-Of-List"       },
85                 {PPPOE_TAG_SVC_NAME,   "Service-Name"      },
86                 {PPPOE_TAG_AC_NAME,    "AC-Name"           },
87                 {PPPOE_TAG_HOST_UNIQ,  "Host-Uniq"         },
88                 {PPPOE_TAG_AC_COOKIE,  "AC-Cookie"         },
89                 {PPPOE_TAG_VENDOR,     "Vendor-Specific"   },
90                 {PPPOE_TAG_RELAY_ID,   "Relay-Session-Id"  },
91                 {PPPOE_TAG_SVC_ERR,    "Service-Name-Error"},
92                 {PPPOE_TAG_AC_ERR,     "AC-System-Error"   },
93                 {PPPOE_TAG_GENERIC_ERR,"Generic-Error"     },
94                 {0,                    NULL                } };
95
96                 return val_to_str(tag_type, code_vals, fmt);
97 }
98
99
100 static void
101 dissect_pppoe_tags(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, int payload_length) {
102
103         guint16 poe_tag;
104         guint16 poe_tag_length;
105         int tagstart;
106
107         proto_tree  *pppoe_tree;
108         proto_item  *ti;
109
110         /* Start Decoding Here. */
111
112         if (tree) {
113                 ti = proto_tree_add_text(tree, tvb,offset,payload_length,"PPPoE Tags");
114                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoed_tags);
115
116                 tagstart = offset;
117                 while(tagstart <= payload_length-2 ) {
118
119                         poe_tag = tvb_get_ntohs(tvb, tagstart);
120                         poe_tag_length = tvb_get_ntohs(tvb, tagstart + 2);
121
122                         proto_tree_add_text(pppoe_tree, tvb,tagstart,4,
123                                 "Tag: %s", pppoetag_to_str(poe_tag,"Unknown (0x%02x)"));
124                         
125                         switch(poe_tag) {
126                         case PPPOE_TAG_SVC_NAME:
127                         case PPPOE_TAG_AC_NAME:
128                         case PPPOE_TAG_SVC_ERR:
129                         case PPPOE_TAG_AC_ERR:
130                         case PPPOE_TAG_GENERIC_ERR:
131                                 /* tag value should be interpreted as a utf-8 unterminated string.*/
132                                 if(poe_tag_length > 0 ) {
133                                         /* really should do some limit checking here.  :( */
134                                         proto_tree_add_text(pppoe_tree, tvb,tagstart+4,poe_tag_length,
135                                                 "  String Data: %s",
136                                                 tvb_format_text(tvb, tagstart+4,poe_tag_length ));
137                                 }
138                                 break;
139                         default:
140                                 if(poe_tag_length > 0 ) {
141                                  proto_tree_add_text(pppoe_tree, tvb,tagstart+4,poe_tag_length,
142                                                 "  Binary Data: (%d bytes)", poe_tag_length );
143                                 }
144                         }
145
146                         if (poe_tag == PPPOE_TAG_EOL) break;
147
148                         tagstart += 4 + poe_tag_length;
149                 }
150         }
151 }
152
153 static void
154 dissect_pppoed(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
155         guint8 pppoe_ver_type;
156         guint8 pppoe_ver;
157         guint8 pppoe_type;
158         guint8  pppoe_code;
159         guint16 pppoe_session_id;
160         guint16 pppoe_length;
161
162         proto_tree  *pppoe_tree;
163         proto_item  *ti;
164
165         if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
166                 col_set_str(pinfo->cinfo,COL_PROTOCOL, "PPPoED");
167         }
168         if (check_col(pinfo->cinfo,COL_INFO)) {
169                 col_clear(pinfo->cinfo,COL_INFO);
170         }
171
172         /* Start Decoding Here. */
173         pppoe_ver_type = tvb_get_guint8(tvb, 0);
174         pppoe_ver = (pppoe_ver_type >> 4) & 0x0f;
175         pppoe_type = pppoe_ver_type & 0x0f;
176         pppoe_code = tvb_get_guint8(tvb, 1);
177
178         if (check_col(pinfo->cinfo,COL_INFO)) {
179                 col_add_fstr(pinfo->cinfo,COL_INFO,pppoecode_to_str(pppoe_code,"Unknown code (0x%02x)"));
180         }
181
182         pppoe_session_id = tvb_get_ntohs(tvb, 2);
183         pppoe_length = tvb_get_ntohs(tvb, 4);
184
185         if (tree) {
186                 ti = proto_tree_add_item(tree, proto_pppoed, tvb,0,
187                         pppoe_length+6, FALSE);
188                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoed);
189                 proto_tree_add_text(pppoe_tree, tvb,0,1,
190                         "Version: %u", pppoe_ver);
191                 proto_tree_add_text(pppoe_tree, tvb,0,1,
192                         "Type: %u", pppoe_type);
193                 proto_tree_add_text(pppoe_tree, tvb,1,1,
194                         "Code: %s", pppoecode_to_str(pppoe_code,"Unknown (0x%02x)"));
195                 proto_tree_add_text(pppoe_tree, tvb,2,2,
196                         "Session ID: %04x", pppoe_session_id);
197                 proto_tree_add_text(pppoe_tree, tvb,4,2,
198                         "Payload Length: %u", pppoe_length);
199         }
200         dissect_pppoe_tags(tvb,6,pinfo,tree,6+pppoe_length);
201 }
202
203 void
204 proto_register_pppoed(void)
205 {
206         static gint *ett[] = {
207                 &ett_pppoed,
208                 &ett_pppoed_tags,
209         };
210
211         proto_pppoed = proto_register_protocol("PPP-over-Ethernet Discovery",
212             "PPPoED", "pppoed");
213
214         proto_register_subtree_array(ett, array_length(ett));
215 }
216
217 void
218 proto_reg_handoff_pppoed(void)
219 {
220         dissector_handle_t pppoed_handle;
221
222         pppoed_handle = create_dissector_handle(dissect_pppoed, proto_pppoed);
223         dissector_add("ethertype", ETHERTYPE_PPPOED, pppoed_handle);
224 }
225
226 static void
227 dissect_pppoes(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
228         guint8 pppoe_ver_type;
229         guint8 pppoe_ver;
230         guint8 pppoe_type;
231         guint8  pppoe_code;
232         guint16 pppoe_session_id;
233         guint16 pppoe_length;
234
235         proto_tree  *pppoe_tree;
236         proto_item  *ti;
237         tvbuff_t    *next_tvb;
238
239         if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
240                 col_set_str(pinfo->cinfo,COL_PROTOCOL, "PPPoES");
241         }
242         if (check_col(pinfo->cinfo,COL_INFO)) {
243                 col_clear(pinfo->cinfo,COL_INFO);
244         }
245
246         /* Start Decoding Here. */
247         pppoe_ver_type = tvb_get_guint8(tvb, 0);
248         pppoe_ver = (pppoe_ver_type >> 4) & 0x0f;
249         pppoe_type = pppoe_ver_type & 0x0f;
250         pppoe_code = tvb_get_guint8(tvb, 1);
251
252         if (check_col(pinfo->cinfo,COL_INFO)) {
253                 col_add_fstr(pinfo->cinfo,COL_INFO,
254                     pppoecode_to_str(pppoe_code,"Unknown code (0x%02x)"));
255         }
256
257         pppoe_session_id = tvb_get_ntohs(tvb, 2);
258         pppoe_length = tvb_get_ntohs(tvb, 4);
259
260         if (tree) {
261                 ti = proto_tree_add_item(tree, proto_pppoes, tvb,0,
262                         pppoe_length+6, FALSE);
263                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoed);
264                 proto_tree_add_text(pppoe_tree, tvb,0,1,
265                         "Version: %u", pppoe_ver);
266                 proto_tree_add_text(pppoe_tree, tvb,0,1,
267                         "Type: %u", pppoe_type);
268                 proto_tree_add_text(pppoe_tree, tvb,1,1,
269                         "Code: %s", pppoecode_to_str(pppoe_code,"Unknown (0x%02x)"));
270                 proto_tree_add_text(pppoe_tree, tvb,2,2,
271                         "Session ID: %04x", pppoe_session_id);
272                 proto_tree_add_text(pppoe_tree, tvb,4,2,
273                         "Payload Length: %u", pppoe_length);
274         }
275         /* dissect_ppp is apparently done as a 'top level' dissector,
276          * so this doesn't work:  
277          * dissect_ppp(pd,offset+6,pinfo->fd,tree);
278          * Im gonna try fudging it.
279          */
280         next_tvb = tvb_new_subset(tvb,6,-1,-1);
281         call_dissector(ppp_handle,next_tvb,pinfo,tree);
282 }
283 void
284 proto_register_pppoes(void)
285 {
286         proto_pppoes = proto_register_protocol("PPP-over-Ethernet Session",
287             "PPPoES", "pppoes");
288 }
289
290 void
291 proto_reg_handoff_pppoes(void)
292 {
293         dissector_handle_t pppoes_handle;
294
295         pppoes_handle = create_dissector_handle(dissect_pppoes, proto_pppoes);
296         dissector_add("ethertype", ETHERTYPE_PPPOES, pppoes_handle);
297
298         /*
299          * Get a handle for the PPP dissector.
300          */
301         ppp_handle = find_dissector("ppp");
302 }