aboutsummaryrefslogtreecommitdiffstats
path: root/packet-pppoe.c
blob: abde716c6235d7ddf27abd304954ae10492fd327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* packet-arp.c
 * Routines for ARP packet disassembly
 *
 * $Id: packet-pppoe.c,v 1.3 1999/08/24 17:26:13 gram Exp $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@zing.org>
 * Copyright 1998 Gerald Combs
 *
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#include <glib.h>
#include "packet.h"

/* For lack of a better source, I made up the following defines. -jsj */

#define PPPOE_CODE_SESSION 0x00
#define PPPOE_CODE_PADO 0x7
#define PPPOE_CODE_PADI 0x9
#define PPPOE_CODE_PADR 0x19
#define PPPOE_CODE_PADS 0x65
#define PPPOE_CODE_PADT 0xa7

#define PPPOE_TAG_EOL 0x0000 
#define PPPOE_TAG_SVC_NAME 0x0101
#define PPPOE_TAG_AC_NAME 0x0102 
#define PPPOE_TAG_HOST_UNIQ 0x0103
#define PPPOE_TAG_AC_COOKIE 0x0104
#define PPPOE_TAG_VENDOR 0x0105 
#define PPPOE_TAG_RELAY_ID 0x0110 
#define PPPOE_TAG_SVC_ERR 0x0201 
#define PPPOE_TAG_AC_ERR 0x0202 
#define PPPOE_TAG_GENERIC_ERR 0x0203

gchar *
pppoecode_to_str(guint8 codetype, const char *fmt) {
	static const value_string code_vals[] = {
		{PPPOE_CODE_SESSION, "Session Data"                             },
		{PPPOE_CODE_PADO, "Active Discovery Offer (PADO)"               },
		{PPPOE_CODE_PADI, "Active Discovery Initiation (PADI)"          },
		{PPPOE_CODE_PADR, "Active Discovery Request (PADR)"             },
		{PPPOE_CODE_PADS, "Active Discovery Session-confirmation (PADS)"},
		{PPPOE_CODE_PADT, "Active Discovery Terminate (PADT)"           },
		{0,	NULL                                                        } };

		return val_to_str(codetype, code_vals, fmt);
}

gchar *
pppoetag_to_str(guint16 tag_type, const char *fmt) {
	static const value_string code_vals[] = {
		{PPPOE_TAG_EOL,        "End-Of-List"       },
		{PPPOE_TAG_SVC_NAME,   "Service-Name"      },
		{PPPOE_TAG_AC_NAME,    "AC-Name"           },
		{PPPOE_TAG_HOST_UNIQ,  "Host-Uniq"         },
		{PPPOE_TAG_AC_COOKIE,  "AC-Cookie"         },
		{PPPOE_TAG_VENDOR,     "Vendor-Specific"   },
		{PPPOE_TAG_RELAY_ID,   "Relay-Session-Id"  },
		{PPPOE_TAG_SVC_ERR,    "Service-Name-Error"},
		{PPPOE_TAG_AC_ERR,     "AC-System-Error"   },
		{PPPOE_TAG_GENERIC_ERR,"Generic-Error"     },
		{0,                    NULL                } };

		return val_to_str(tag_type, code_vals, fmt);
}


void
dissect_pppoe_tags(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, int payload_length) {

	guint16 poe_tag;
	guint16 poe_tag_length;
	int tagstart;

	proto_tree  *pppoe_tree;
	proto_item  *ti;

	/* Start Decoding Here. */

	if (tree) {
		ti = proto_tree_add_text(tree,offset,payload_length,"PPPoE Tags");
		pppoe_tree = proto_item_add_subtree(ti, ETT_PPPOED_TAGS);

		tagstart = offset;
		while(tagstart <= payload_length-2 ) {

			poe_tag = pntohs(&pd[tagstart]);
			poe_tag_length = pntohs(&pd[tagstart + 2]);

			proto_tree_add_text(pppoe_tree,tagstart,4,
				"Tag: %s", pppoetag_to_str(poe_tag,"Unknown (0x%02x)"));
			
			switch(poe_tag) {
			case PPPOE_TAG_SVC_NAME:
			case PPPOE_TAG_AC_NAME:
			case PPPOE_TAG_SVC_ERR:
			case PPPOE_TAG_AC_ERR:
			case PPPOE_TAG_GENERIC_ERR:
				/* tag value should be interpreted as a utf-8 unterminated string.*/
				if(poe_tag_length > 0 ) {
					/* really should do some limit checking here.  :( */
					proto_tree_add_text(pppoe_tree,tagstart+4,poe_tag_length,
						"  String Data: %s", format_text(&pd[tagstart+4],poe_tag_length ));
				}
				break;
			default:
				if(poe_tag_length > 0 ) {
				 proto_tree_add_text(pppoe_tree,tagstart+4,poe_tag_length,
						"  Binary Data: (%d bytes)", poe_tag_length );
				}
			}

			if (poe_tag == PPPOE_TAG_EOL) break;

			tagstart += 4 + poe_tag_length;
		}
	}
}

void
dissect_pppoed(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
	guint8 pppoe_ver;
	guint8 pppoe_type;
	guint8	pppoe_code;
	guint16	pppoe_session_id;
	guint16	pppoe_length;

	proto_tree  *pppoe_tree;
	proto_item  *ti;

	/* Start Decoding Here. */
	pppoe_ver = (guint8) ((pd[offset] >> 4) & 0x0f);
	pppoe_type = (guint8) (pd[offset] & 0x0f);
	pppoe_code = (guint8) pd[offset + 1];
	pppoe_session_id = pntohs(&pd[offset + 2]);
	pppoe_length = pntohs(&pd[offset + 4]);

	if (check_col(fd, COL_PROTOCOL)) {
		col_add_str(fd,COL_PROTOCOL, "PPPoED");
	}

	if (check_col(fd,COL_INFO)) {
		col_add_fstr(fd,COL_INFO,pppoecode_to_str(pppoe_code,"Unknown code (0x%02x)"));
	}

	if (tree) {
		ti = proto_tree_add_text(tree,offset,pppoe_length+6,"PPPoE Discovery");
		pppoe_tree = proto_item_add_subtree(ti, ETT_PPPOED);
		proto_tree_add_text(pppoe_tree,offset,1,
			"Version: %d", pppoe_ver);
		proto_tree_add_text(pppoe_tree,offset,1,
			"Type: %d", pppoe_type);
		proto_tree_add_text(pppoe_tree,offset+1,1,
			"Code: %s", pppoecode_to_str(pppoe_code,"Unknown (0x%02x)"));
		proto_tree_add_text(pppoe_tree,offset+2,2,
			"Session ID: %04x", pppoe_session_id);
		proto_tree_add_text(pppoe_tree,offset+4,2,
			"Payload Length: %d", pppoe_length);
	}
	dissect_pppoe_tags(pd,offset+6,fd,tree,offset+6+pppoe_length);

}

void
dissect_pppoes(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
	guint8 pppoe_ver;
	guint8 pppoe_type;
	guint8	pppoe_code;
	guint16	pppoe_session_id;
	guint16	pppoe_length;

	proto_tree  *pppoe_tree;
	proto_item  *ti;

	/* Start Decoding Here. */
	pppoe_ver = (guint8) ((pd[offset] >> 4) & 0x0f);
	pppoe_type = (guint8) (pd[offset] & 0x0f);
	pppoe_code = (guint8) pd[offset + 1];
	pppoe_session_id = pntohs(&pd[offset + 2]);
	pppoe_length = pntohs(&pd[offset + 4]);

	if (check_col(fd, COL_PROTOCOL)) {
		col_add_str(fd,COL_PROTOCOL, "PPPoES");
	}

	if (check_col(fd,COL_INFO)) {
		col_add_fstr(fd,COL_INFO,pppoecode_to_str(pppoe_code,"Unknown code (0x%02x)"));
	}

	if (tree) {
		ti = proto_tree_add_text(tree,offset,pppoe_length+6,"PPPoE Session");
		pppoe_tree = proto_item_add_subtree(ti, ETT_PPPOED);
		proto_tree_add_text(pppoe_tree,offset,1,
			"Version: %d", pppoe_ver);
		proto_tree_add_text(pppoe_tree,offset,1,
			"Type: %d", pppoe_type);
		proto_tree_add_text(pppoe_tree,offset+1,1,
			"Code: %s", pppoecode_to_str(pppoe_code,"Unknown (0x%02x)"));
		proto_tree_add_text(pppoe_tree,offset+2,2,
			"Session ID: %04x", pppoe_session_id);
		proto_tree_add_text(pppoe_tree,offset+4,2,
			"Payload Length: %d", pppoe_length);
	}
	/* dissect_ppp is apparently done as a 'top level' dissector,
		* so this doesn't work:  
		* dissect_ppp(pd,offset+6,fd,tree);
		* Im gonna try fudging it.
		*/

	dissect_payload_ppp(pd,offset+6,fd,tree);
}