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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2001 by Sun Microsystems, Inc.
* All rights reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sysmacros.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/pppoe.h>
#include "snoop.h"
/*
* These two macros extract the version and type fields respectively from
* the first byte of the PPPoE header.
*/
#define POE_VERS(x) (((x) >> 4) & 0x0f)
#define POE_TYPE(x) ((x) & 0x0f)
typedef void interpret_func_t(uint8_t *, uint16_t);
typedef struct taginfo {
char *tag_name;
uint16_t tag_type;
interpret_func_t *interpret_tagvalue;
} taginfo_t;
static char *pppoe_codetoname(int, boolean_t);
static taginfo_t *pppoe_gettaginfo(uint16_t);
static void print_hexdata(char *, uint8_t *, uint16_t);
static void print_utf8string(char *, char *, uint16_t);
static char *print_linetag(char *);
static interpret_func_t interpret_tags;
static interpret_func_t interpret_hexdata;
static interpret_func_t interpret_service;
static interpret_func_t interpret_access;
static interpret_func_t interpret_cookie;
static interpret_func_t interpret_vendor;
static interpret_func_t interpret_relay;
static interpret_func_t interpret_error;
static interpret_func_t interpret_hurl;
static interpret_func_t interpret_motm;
static interpret_func_t interpret_rteadd;
static taginfo_t taginfo_array[] = {
{ "End-Of-List", POETT_END, interpret_hexdata },
{ "Service-Name", POETT_SERVICE, interpret_service },
{ "AC-Name", POETT_ACCESS, interpret_access },
{ "Host-Uniq", POETT_UNIQ, interpret_hexdata },
{ "AC-Cookie", POETT_COOKIE, interpret_cookie },
{ "Vendor-Specific", POETT_VENDOR, interpret_vendor },
{ "Relay-Session-Id", POETT_RELAY, interpret_relay },
{ "Service-Name-Error", POETT_NAMERR, interpret_error },
{ "AC-System-Error", POETT_SYSERR, interpret_error },
{ "Generic-Error", POETT_GENERR, interpret_error },
{ "Multicast-Capable", POETT_MULTI, interpret_hexdata },
{ "Host-URL", POETT_HURL, interpret_hurl },
{ "Message-Of-The-Minute", POETT_MOTM, interpret_motm },
{ "IP-Route-Add", POETT_RTEADD, interpret_rteadd },
{ "Unknown TAG", 0, NULL }
};
int
interpret_pppoe(int flags, poep_t *poep, int len)
{
uint8_t code = poep->poep_code;
uint8_t *payload;
if (len < sizeof (poep_t))
return (len);
payload = (uint8_t *)poep + sizeof (poep_t);
if (flags & F_SUM) {
(void) sprintf(get_sum_line(), "PPPoE %s",
pppoe_codetoname(code, B_FALSE));
} else { /* flags & F_DTAIL */
show_header("PPPoE: ", "PPP Over Ethernet", len);
show_space();
(void) sprintf(get_line(0, 0),
"Version = %d", POE_VERS(poep->poep_version_type));
(void) sprintf(get_line(0, 0),
"Type = %d", POE_TYPE(poep->poep_version_type));
(void) sprintf(get_line(0, 0),
"Code = %d (%s)", code, pppoe_codetoname(code, B_TRUE));
(void) sprintf(get_line(0, 0),
"Session Id = %d", ntohs(poep->poep_session_id));
(void) sprintf(get_line(0, 0),
"Length = %d bytes", ntohs(poep->poep_length));
show_space();
len -= sizeof (poep_t);
len = MIN(len, ntohs(poep->poep_length));
if (poep->poep_code != 0 && poep->poep_length > 0) {
interpret_tags(payload, len);
}
}
if (poep->poep_code == 0) {
return (interpret_ppp(flags, payload, len));
}
return (len);
}
/*
* interpret_tags() prints PPPoE Discovery Stage TAGs in detail.
*/
static void
interpret_tags(uint8_t *payload, uint16_t length)
{
uint8_t *tagptr = payload;
uint16_t tag_length;
uint16_t tag_type;
uint8_t *tag_value;
taginfo_t *tinfo;
while (length >= POET_HDRLEN) {
tag_type = POET_GET_TYPE(tagptr);
tag_length = POET_GET_LENG(tagptr);
tinfo = pppoe_gettaginfo(tag_type);
show_header("PPPoE: ", tinfo->tag_name,
tag_length + POET_HDRLEN);
(void) sprintf(get_line(0, 0),
"Tag Type = %d", tag_type);
(void) sprintf(get_line(0, 0),
"Tag Length = %d bytes", tag_length);
length -= POET_HDRLEN;
if (tag_length > length) {
(void) sprintf(get_line(0, 0),
"Warning: Truncated Packet");
show_space();
break;
}
/*
* unknown tags or tags which should always have 0 length
* are not interpreted any further.
*/
tag_value = POET_DATA(tagptr);
if (tag_length != 0 && tinfo->interpret_tagvalue != NULL)
tinfo->interpret_tagvalue(tag_value, tag_length);
show_space();
length -= tag_length;
tagptr = POET_NEXT(tagptr);
}
}
static char *
pppoe_codetoname(int code, boolean_t verbose)
{
char *name;
switch (code) {
case POECODE_DATA:
name = "Session";
break;
case POECODE_PADO:
if (verbose)
name = "Active Discovery Offer";
else
name = "PADO";
break;
case POECODE_PADI:
if (verbose)
name = "Active Discovery Initiation";
else
name = "PADI";
break;
case POECODE_PADR:
if (verbose)
name = "Active Discovery Request";
else
name = "PADR";
break;
case POECODE_PADS:
if (verbose)
name = "Active Discovery Session-Confirmation";
else
name = "PADS";
break;
case POECODE_PADT:
if (verbose)
name = "Active Discovery Terminate";
else
name = "PADT";
break;
case POECODE_PADM:
if (verbose)
name = "Active Discovery Message";
else
name = "PADM";
break;
case POECODE_PADN:
if (verbose)
name = "Active Discovery Network";
else
name = "PADN";
break;
default:
name = "Unknown Code";
}
return (name);
}
static taginfo_t *
pppoe_gettaginfo(uint16_t type)
{
taginfo_t *taginfo_ptr = &taginfo_array[0];
int i = 0;
while (taginfo_ptr->tag_type != type &&
taginfo_ptr->interpret_tagvalue != NULL) {
taginfo_ptr = &taginfo_array[++i];
}
return (taginfo_ptr);
}
static void
interpret_hexdata(uint8_t *tag_value, uint16_t tag_length)
{
char *endofline;
endofline = print_linetag("Data = ");
print_hexdata(endofline, tag_value, tag_length);
}
static void
interpret_service(uint8_t *tag_value, uint16_t tag_length)
{
char *endofline;
endofline = print_linetag("Service Name = ");
print_utf8string(endofline, (char *)tag_value, tag_length);
}
static void
interpret_access(uint8_t *tag_value, uint16_t tag_length)
{
char *endofline;
endofline = print_linetag("AC Name = ");
print_utf8string(endofline, (char *)tag_value, tag_length);
}
static void
interpret_cookie(uint8_t *tag_value, uint16_t tag_length)
{
char *endofline;
endofline = print_linetag("Cookie = ");
print_hexdata(endofline, tag_value, tag_length);
}
static void
interpret_vendor(uint8_t *tag_value, uint16_t tag_length)
{
uint8_t *vendor_data;
uint32_t vendorid;
char *endofline;
vendorid = ntohl(*(uint32_t *)tag_value);
(void) sprintf(get_line(0, 0),
"Vendor ID = %d", vendorid);
if (tag_length > 4) {
vendor_data = tag_value + 4;
endofline = print_linetag("Vendor Data = ");
print_hexdata(endofline, vendor_data, tag_length - 4);
}
}
static void
interpret_relay(uint8_t *tag_value, uint16_t tag_length)
{
char *endofline;
endofline = print_linetag("ID = ");
print_hexdata(endofline, tag_value, tag_length);
}
static void
interpret_error(uint8_t *tag_value, uint16_t tag_length)
{
char *endofline;
endofline = print_linetag("Error = ");
print_utf8string(endofline, (char *)tag_value, tag_length);
}
static void
interpret_hurl(uint8_t *tag_value, uint16_t tag_length)
{
char *endofline;
endofline = print_linetag("URL = ");
print_utf8string(endofline, (char *)tag_value, tag_length);
}
static void
interpret_motm(uint8_t *tag_value, uint16_t tag_length)
{
char *endofline;
endofline = print_linetag("Message = ");
print_utf8string(endofline, (char *)tag_value, tag_length);
}
static void
interpret_rteadd(uint8_t *tag_value, uint16_t tag_length)
{
char dest[INET_ADDRSTRLEN];
char mask[INET_ADDRSTRLEN];
char gateway[INET_ADDRSTRLEN];
uint32_t metric;
if (tag_length == 16) {
(void) inet_ntop(AF_INET, tag_value, dest,
INET_ADDRSTRLEN);
(void) inet_ntop(AF_INET, &tag_value[4], mask,
INET_ADDRSTRLEN);
(void) inet_ntop(AF_INET, &tag_value[8], gateway,
INET_ADDRSTRLEN);
metric = ntohl(*(uint32_t *)&tag_value[12]);
sprintf(get_line(0, 0),
"Destination\tNetmask\tGateway\tMetric");
sprintf(get_line(0, 0),
"%s\t%s\t%s\t%d", dest, mask, gateway, metric);
}
}
static void
print_hexdata(char *line, uint8_t *data, uint16_t length)
{
uint16_t index = 0;
line += sprintf(line, "0x");
while (index < length) {
line += sprintf(line, "%02x", data[index++]);
}
}
static void
print_utf8string(char *firstline, char *string, uint16_t length)
{
(void) sprintf(firstline, "%.*s", length, string);
}
static char *
print_linetag(char *string)
{
char *line = get_line(0, 0);
return (line + sprintf(line, string));
}
|