Allow "-w" and/or "-R" to be specified either when doing a live capture
[obnox/wireshark/wip.git] / packet-pppoe.c
1 /* packet-arp.c
2  * Routines for ARP packet disassembly
3  *
4  * $Id: packet-pppoe.c,v 1.4 1999/11/16 11:42:48 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
37 static gint ett_pppoed = -1;
38 static gint ett_pppoed_tags = -1;
39
40 /* For lack of a better source, I made up the following defines. -jsj */
41
42 #define PPPOE_CODE_SESSION 0x00
43 #define PPPOE_CODE_PADO 0x7
44 #define PPPOE_CODE_PADI 0x9
45 #define PPPOE_CODE_PADR 0x19
46 #define PPPOE_CODE_PADS 0x65
47 #define PPPOE_CODE_PADT 0xa7
48
49 #define PPPOE_TAG_EOL 0x0000 
50 #define PPPOE_TAG_SVC_NAME 0x0101
51 #define PPPOE_TAG_AC_NAME 0x0102 
52 #define PPPOE_TAG_HOST_UNIQ 0x0103
53 #define PPPOE_TAG_AC_COOKIE 0x0104
54 #define PPPOE_TAG_VENDOR 0x0105 
55 #define PPPOE_TAG_RELAY_ID 0x0110 
56 #define PPPOE_TAG_SVC_ERR 0x0201 
57 #define PPPOE_TAG_AC_ERR 0x0202 
58 #define PPPOE_TAG_GENERIC_ERR 0x0203
59
60 gchar *
61 pppoecode_to_str(guint8 codetype, const char *fmt) {
62         static const value_string code_vals[] = {
63                 {PPPOE_CODE_SESSION, "Session Data"                             },
64                 {PPPOE_CODE_PADO, "Active Discovery Offer (PADO)"               },
65                 {PPPOE_CODE_PADI, "Active Discovery Initiation (PADI)"          },
66                 {PPPOE_CODE_PADR, "Active Discovery Request (PADR)"             },
67                 {PPPOE_CODE_PADS, "Active Discovery Session-confirmation (PADS)"},
68                 {PPPOE_CODE_PADT, "Active Discovery Terminate (PADT)"           },
69                 {0,     NULL                                                        } };
70
71                 return val_to_str(codetype, code_vals, fmt);
72 }
73
74 gchar *
75 pppoetag_to_str(guint16 tag_type, const char *fmt) {
76         static const value_string code_vals[] = {
77                 {PPPOE_TAG_EOL,        "End-Of-List"       },
78                 {PPPOE_TAG_SVC_NAME,   "Service-Name"      },
79                 {PPPOE_TAG_AC_NAME,    "AC-Name"           },
80                 {PPPOE_TAG_HOST_UNIQ,  "Host-Uniq"         },
81                 {PPPOE_TAG_AC_COOKIE,  "AC-Cookie"         },
82                 {PPPOE_TAG_VENDOR,     "Vendor-Specific"   },
83                 {PPPOE_TAG_RELAY_ID,   "Relay-Session-Id"  },
84                 {PPPOE_TAG_SVC_ERR,    "Service-Name-Error"},
85                 {PPPOE_TAG_AC_ERR,     "AC-System-Error"   },
86                 {PPPOE_TAG_GENERIC_ERR,"Generic-Error"     },
87                 {0,                    NULL                } };
88
89                 return val_to_str(tag_type, code_vals, fmt);
90 }
91
92
93 void
94 dissect_pppoe_tags(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, int payload_length) {
95
96         guint16 poe_tag;
97         guint16 poe_tag_length;
98         int tagstart;
99
100         proto_tree  *pppoe_tree;
101         proto_item  *ti;
102
103         /* Start Decoding Here. */
104
105         if (tree) {
106                 ti = proto_tree_add_text(tree,offset,payload_length,"PPPoE Tags");
107                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoed_tags);
108
109                 tagstart = offset;
110                 while(tagstart <= payload_length-2 ) {
111
112                         poe_tag = pntohs(&pd[tagstart]);
113                         poe_tag_length = pntohs(&pd[tagstart + 2]);
114
115                         proto_tree_add_text(pppoe_tree,tagstart,4,
116                                 "Tag: %s", pppoetag_to_str(poe_tag,"Unknown (0x%02x)"));
117                         
118                         switch(poe_tag) {
119                         case PPPOE_TAG_SVC_NAME:
120                         case PPPOE_TAG_AC_NAME:
121                         case PPPOE_TAG_SVC_ERR:
122                         case PPPOE_TAG_AC_ERR:
123                         case PPPOE_TAG_GENERIC_ERR:
124                                 /* tag value should be interpreted as a utf-8 unterminated string.*/
125                                 if(poe_tag_length > 0 ) {
126                                         /* really should do some limit checking here.  :( */
127                                         proto_tree_add_text(pppoe_tree,tagstart+4,poe_tag_length,
128                                                 "  String Data: %s", format_text(&pd[tagstart+4],poe_tag_length ));
129                                 }
130                                 break;
131                         default:
132                                 if(poe_tag_length > 0 ) {
133                                  proto_tree_add_text(pppoe_tree,tagstart+4,poe_tag_length,
134                                                 "  Binary Data: (%d bytes)", poe_tag_length );
135                                 }
136                         }
137
138                         if (poe_tag == PPPOE_TAG_EOL) break;
139
140                         tagstart += 4 + poe_tag_length;
141                 }
142         }
143 }
144
145 void
146 dissect_pppoed(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
147         guint8 pppoe_ver;
148         guint8 pppoe_type;
149         guint8  pppoe_code;
150         guint16 pppoe_session_id;
151         guint16 pppoe_length;
152
153         proto_tree  *pppoe_tree;
154         proto_item  *ti;
155
156         /* Start Decoding Here. */
157         pppoe_ver = (guint8) ((pd[offset] >> 4) & 0x0f);
158         pppoe_type = (guint8) (pd[offset] & 0x0f);
159         pppoe_code = (guint8) pd[offset + 1];
160         pppoe_session_id = pntohs(&pd[offset + 2]);
161         pppoe_length = pntohs(&pd[offset + 4]);
162
163         if (check_col(fd, COL_PROTOCOL)) {
164                 col_add_str(fd,COL_PROTOCOL, "PPPoED");
165         }
166
167         if (check_col(fd,COL_INFO)) {
168                 col_add_fstr(fd,COL_INFO,pppoecode_to_str(pppoe_code,"Unknown code (0x%02x)"));
169         }
170
171         if (tree) {
172                 ti = proto_tree_add_text(tree,offset,pppoe_length+6,"PPPoE Discovery");
173                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoed);
174                 proto_tree_add_text(pppoe_tree,offset,1,
175                         "Version: %d", pppoe_ver);
176                 proto_tree_add_text(pppoe_tree,offset,1,
177                         "Type: %d", pppoe_type);
178                 proto_tree_add_text(pppoe_tree,offset+1,1,
179                         "Code: %s", pppoecode_to_str(pppoe_code,"Unknown (0x%02x)"));
180                 proto_tree_add_text(pppoe_tree,offset+2,2,
181                         "Session ID: %04x", pppoe_session_id);
182                 proto_tree_add_text(pppoe_tree,offset+4,2,
183                         "Payload Length: %d", pppoe_length);
184         }
185         dissect_pppoe_tags(pd,offset+6,fd,tree,offset+6+pppoe_length);
186
187 }
188
189 void
190 dissect_pppoes(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
191         guint8 pppoe_ver;
192         guint8 pppoe_type;
193         guint8  pppoe_code;
194         guint16 pppoe_session_id;
195         guint16 pppoe_length;
196
197         proto_tree  *pppoe_tree;
198         proto_item  *ti;
199
200         /* Start Decoding Here. */
201         pppoe_ver = (guint8) ((pd[offset] >> 4) & 0x0f);
202         pppoe_type = (guint8) (pd[offset] & 0x0f);
203         pppoe_code = (guint8) pd[offset + 1];
204         pppoe_session_id = pntohs(&pd[offset + 2]);
205         pppoe_length = pntohs(&pd[offset + 4]);
206
207         if (check_col(fd, COL_PROTOCOL)) {
208                 col_add_str(fd,COL_PROTOCOL, "PPPoES");
209         }
210
211         if (check_col(fd,COL_INFO)) {
212                 col_add_fstr(fd,COL_INFO,pppoecode_to_str(pppoe_code,"Unknown code (0x%02x)"));
213         }
214
215         if (tree) {
216                 ti = proto_tree_add_text(tree,offset,pppoe_length+6,"PPPoE Session");
217                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoed);
218                 proto_tree_add_text(pppoe_tree,offset,1,
219                         "Version: %d", pppoe_ver);
220                 proto_tree_add_text(pppoe_tree,offset,1,
221                         "Type: %d", pppoe_type);
222                 proto_tree_add_text(pppoe_tree,offset+1,1,
223                         "Code: %s", pppoecode_to_str(pppoe_code,"Unknown (0x%02x)"));
224                 proto_tree_add_text(pppoe_tree,offset+2,2,
225                         "Session ID: %04x", pppoe_session_id);
226                 proto_tree_add_text(pppoe_tree,offset+4,2,
227                         "Payload Length: %d", pppoe_length);
228         }
229         /* dissect_ppp is apparently done as a 'top level' dissector,
230                 * so this doesn't work:  
231                 * dissect_ppp(pd,offset+6,fd,tree);
232                 * Im gonna try fudging it.
233                 */
234
235         dissect_payload_ppp(pd,offset+6,fd,tree);
236 }
237
238 void
239 proto_register_pppoed(void)
240 {
241         static gint *ett[] = {
242                 &ett_pppoed,
243                 &ett_pppoed_tags,
244         };
245
246         proto_register_subtree_array(ett, array_length(ett));
247 }
248