From Richard Urwin a great enhancement to the color filter dialogue to
[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.25 2002/10/21 18:17:41 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 #include <glib.h>
30 #include <epan/packet.h>
31 #include <epan/strutil.h>
32 #include "etypes.h"
33
34 static int proto_pppoed = -1;
35
36 static gint ett_pppoed = -1;
37 static gint ett_pppoed_tags = -1;
38
39 static int proto_pppoes = -1;
40
41 static dissector_handle_t ppp_handle;
42
43 /* For lack of a better source, I made up the following defines. -jsj */
44
45 #define PPPOE_CODE_SESSION 0x00
46 #define PPPOE_CODE_PADO 0x7
47 #define PPPOE_CODE_PADI 0x9
48 #define PPPOE_CODE_PADR 0x19
49 #define PPPOE_CODE_PADS 0x65
50 #define PPPOE_CODE_PADT 0xa7
51
52 #define PPPOE_TAG_EOL 0x0000
53 #define PPPOE_TAG_SVC_NAME 0x0101
54 #define PPPOE_TAG_AC_NAME 0x0102
55 #define PPPOE_TAG_HOST_UNIQ 0x0103
56 #define PPPOE_TAG_AC_COOKIE 0x0104
57 #define PPPOE_TAG_VENDOR 0x0105
58 #define PPPOE_TAG_RELAY_ID 0x0110
59 #define PPPOE_TAG_SVC_ERR 0x0201
60 #define PPPOE_TAG_AC_ERR 0x0202
61 #define PPPOE_TAG_GENERIC_ERR 0x0203
62
63 static gchar *
64 pppoecode_to_str(guint8 codetype, const char *fmt) {
65         static const value_string code_vals[] = {
66                 {PPPOE_CODE_SESSION, "Session Data"                             },
67                 {PPPOE_CODE_PADO, "Active Discovery Offer (PADO)"               },
68                 {PPPOE_CODE_PADI, "Active Discovery Initiation (PADI)"          },
69                 {PPPOE_CODE_PADR, "Active Discovery Request (PADR)"             },
70                 {PPPOE_CODE_PADS, "Active Discovery Session-confirmation (PADS)"},
71                 {PPPOE_CODE_PADT, "Active Discovery Terminate (PADT)"           },
72                 {0,     NULL                                                        } };
73
74                 return val_to_str(codetype, code_vals, fmt);
75 }
76
77 static gchar *
78 pppoetag_to_str(guint16 tag_type, const char *fmt) {
79         static const value_string code_vals[] = {
80                 {PPPOE_TAG_EOL,        "End-Of-List"       },
81                 {PPPOE_TAG_SVC_NAME,   "Service-Name"      },
82                 {PPPOE_TAG_AC_NAME,    "AC-Name"           },
83                 {PPPOE_TAG_HOST_UNIQ,  "Host-Uniq"         },
84                 {PPPOE_TAG_AC_COOKIE,  "AC-Cookie"         },
85                 {PPPOE_TAG_VENDOR,     "Vendor-Specific"   },
86                 {PPPOE_TAG_RELAY_ID,   "Relay-Session-Id"  },
87                 {PPPOE_TAG_SVC_ERR,    "Service-Name-Error"},
88                 {PPPOE_TAG_AC_ERR,     "AC-System-Error"   },
89                 {PPPOE_TAG_GENERIC_ERR,"Generic-Error"     },
90                 {0,                    NULL                } };
91
92                 return val_to_str(tag_type, code_vals, fmt);
93 }
94
95
96 static void
97 dissect_pppoe_tags(tvbuff_t *tvb, int offset, proto_tree *tree, int payload_length) {
98
99         guint16 poe_tag;
100         guint16 poe_tag_length;
101         int tagstart;
102
103         proto_tree  *pppoe_tree;
104         proto_item  *ti;
105
106         /* Start Decoding Here. */
107
108         if (tree) {
109                 ti = proto_tree_add_text(tree, tvb,offset,payload_length,"PPPoE Tags");
110                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoed_tags);
111
112                 tagstart = offset;
113                 while(tagstart <= payload_length-2 ) {
114
115                         poe_tag = tvb_get_ntohs(tvb, tagstart);
116                         poe_tag_length = tvb_get_ntohs(tvb, tagstart + 2);
117
118                         proto_tree_add_text(pppoe_tree, tvb,tagstart,4,
119                                 "Tag: %s", pppoetag_to_str(poe_tag,"Unknown (0x%02x)"));
120
121                         switch(poe_tag) {
122                         case PPPOE_TAG_SVC_NAME:
123                         case PPPOE_TAG_AC_NAME:
124                         case PPPOE_TAG_SVC_ERR:
125                         case PPPOE_TAG_AC_ERR:
126                         case PPPOE_TAG_GENERIC_ERR:
127                                 /* tag value should be interpreted as a utf-8 unterminated string.*/
128                                 if(poe_tag_length > 0 ) {
129                                         /* really should do some limit checking here.  :( */
130                                         proto_tree_add_text(pppoe_tree, tvb,tagstart+4,poe_tag_length,
131                                                 "  String Data: %s",
132                                                 tvb_format_text(tvb, tagstart+4,poe_tag_length ));
133                                 }
134                                 break;
135                         default:
136                                 if(poe_tag_length > 0 ) {
137                                  proto_tree_add_text(pppoe_tree, tvb,tagstart+4,poe_tag_length,
138                                                 "  Binary Data: (%d bytes)", poe_tag_length );
139                                 }
140                         }
141
142                         if (poe_tag == PPPOE_TAG_EOL) break;
143
144                         tagstart += 4 + poe_tag_length;
145                 }
146         }
147 }
148
149 static void
150 dissect_pppoed(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
151         guint8 pppoe_ver_type;
152         guint8 pppoe_ver;
153         guint8 pppoe_type;
154         guint8  pppoe_code;
155         guint16 pppoe_session_id;
156         guint16 pppoe_length;
157
158         proto_tree  *pppoe_tree;
159         proto_item  *ti;
160
161         if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
162                 col_set_str(pinfo->cinfo,COL_PROTOCOL, "PPPoED");
163         }
164         if (check_col(pinfo->cinfo,COL_INFO)) {
165                 col_clear(pinfo->cinfo,COL_INFO);
166         }
167
168         /* Start Decoding Here. */
169         pppoe_ver_type = tvb_get_guint8(tvb, 0);
170         pppoe_ver = (pppoe_ver_type >> 4) & 0x0f;
171         pppoe_type = pppoe_ver_type & 0x0f;
172         pppoe_code = tvb_get_guint8(tvb, 1);
173
174         if (check_col(pinfo->cinfo,COL_INFO)) {
175                 col_add_fstr(pinfo->cinfo,COL_INFO,pppoecode_to_str(pppoe_code,"Unknown code (0x%02x)"));
176         }
177
178         pppoe_session_id = tvb_get_ntohs(tvb, 2);
179         pppoe_length = tvb_get_ntohs(tvb, 4);
180
181         if (tree) {
182                 ti = proto_tree_add_item(tree, proto_pppoed, tvb,0,
183                         pppoe_length+6, FALSE);
184                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoed);
185                 proto_tree_add_text(pppoe_tree, tvb,0,1,
186                         "Version: %u", pppoe_ver);
187                 proto_tree_add_text(pppoe_tree, tvb,0,1,
188                         "Type: %u", pppoe_type);
189                 proto_tree_add_text(pppoe_tree, tvb,1,1,
190                         "Code: %s", pppoecode_to_str(pppoe_code,"Unknown (0x%02x)"));
191                 proto_tree_add_text(pppoe_tree, tvb,2,2,
192                         "Session ID: %04x", pppoe_session_id);
193                 proto_tree_add_text(pppoe_tree, tvb,4,2,
194                         "Payload Length: %u", pppoe_length);
195         }
196         dissect_pppoe_tags(tvb,6,tree,6+pppoe_length);
197 }
198
199 void
200 proto_register_pppoed(void)
201 {
202         static gint *ett[] = {
203                 &ett_pppoed,
204                 &ett_pppoed_tags,
205         };
206
207         proto_pppoed = proto_register_protocol("PPP-over-Ethernet Discovery",
208             "PPPoED", "pppoed");
209
210         proto_register_subtree_array(ett, array_length(ett));
211 }
212
213 void
214 proto_reg_handoff_pppoed(void)
215 {
216         dissector_handle_t pppoed_handle;
217
218         pppoed_handle = create_dissector_handle(dissect_pppoed, proto_pppoed);
219         dissector_add("ethertype", ETHERTYPE_PPPOED, pppoed_handle);
220 }
221
222 static void
223 dissect_pppoes(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
224         guint8 pppoe_ver_type;
225         guint8 pppoe_ver;
226         guint8 pppoe_type;
227         guint8  pppoe_code;
228         guint16 pppoe_session_id;
229         guint16 pppoe_length;
230         gint length, reported_length;
231
232         proto_tree  *pppoe_tree;
233         proto_item  *ti;
234         tvbuff_t    *next_tvb;
235
236         if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
237                 col_set_str(pinfo->cinfo,COL_PROTOCOL, "PPPoES");
238         }
239         if (check_col(pinfo->cinfo,COL_INFO)) {
240                 col_clear(pinfo->cinfo,COL_INFO);
241         }
242
243         /* Start Decoding Here. */
244         pppoe_ver_type = tvb_get_guint8(tvb, 0);
245         pppoe_ver = (pppoe_ver_type >> 4) & 0x0f;
246         pppoe_type = pppoe_ver_type & 0x0f;
247         pppoe_code = tvb_get_guint8(tvb, 1);
248
249         if (check_col(pinfo->cinfo,COL_INFO)) {
250                 col_add_fstr(pinfo->cinfo,COL_INFO,
251                     pppoecode_to_str(pppoe_code,"Unknown code (0x%02x)"));
252         }
253
254         pppoe_session_id = tvb_get_ntohs(tvb, 2);
255         pppoe_length = tvb_get_ntohs(tvb, 4);
256
257         if (tree) {
258                 ti = proto_tree_add_item(tree, proto_pppoes, tvb,0,
259                         6, FALSE);
260                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoed);
261                 proto_tree_add_text(pppoe_tree, tvb,0,1,
262                         "Version: %u", pppoe_ver);
263                 proto_tree_add_text(pppoe_tree, tvb,0,1,
264                         "Type: %u", pppoe_type);
265                 proto_tree_add_text(pppoe_tree, tvb,1,1,
266                         "Code: %s", pppoecode_to_str(pppoe_code,"Unknown (0x%02x)"));
267                 proto_tree_add_text(pppoe_tree, tvb,2,2,
268                         "Session ID: %04x", pppoe_session_id);
269                 proto_tree_add_text(pppoe_tree, tvb,4,2,
270                         "Payload Length: %u", pppoe_length);
271         }
272         /* dissect_ppp is apparently done as a 'top level' dissector,
273          * so this doesn't work:
274          * dissect_ppp(pd,offset+6,pinfo->fd,tree);
275          * Im gonna try fudging it.
276          */
277         length = tvb_length_remaining(tvb, 6);
278         reported_length = tvb_reported_length_remaining(tvb, 6);
279         g_assert(length >= 0);
280         g_assert(reported_length >= 0);
281         if (length > reported_length)
282                 length = reported_length;
283         if ((guint)length > pppoe_length)
284                 length = pppoe_length;
285         if ((guint)reported_length > pppoe_length)
286                 reported_length = pppoe_length;
287         next_tvb = tvb_new_subset(tvb,6,length,reported_length);
288         call_dissector(ppp_handle,next_tvb,pinfo,tree);
289 }
290 void
291 proto_register_pppoes(void)
292 {
293         proto_pppoes = proto_register_protocol("PPP-over-Ethernet Session",
294             "PPPoES", "pppoes");
295 }
296
297 void
298 proto_reg_handoff_pppoes(void)
299 {
300         dissector_handle_t pppoes_handle;
301
302         pppoes_handle = create_dissector_handle(dissect_pppoes, proto_pppoes);
303         dissector_add("ethertype", ETHERTYPE_PPPOES, pppoes_handle);
304
305         /*
306          * Get a handle for the PPP dissector.
307          */
308         ppp_handle = find_dissector("ppp");
309 }