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