1/*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * Original code by Greg Stark <gsstark@mit.edu>
22 */
23
24#include <sys/cdefs.h>
25#ifndef lint
26#if 0
27static const char rcsid[] _U_ =
28"@(#) Header: /tcpdump/master/tcpdump/print-pppoe.c,v 1.31 2005-04-26 19:48:38 guy Exp (LBL)";
29#else
30__RCSID("$NetBSD: print-pppoe.c,v 1.2 2010/12/05 05:11:30 christos Exp $");
31#endif
32#endif
33
34#ifdef HAVE_CONFIG_H
35#include "config.h"
36#endif
37
38#include <tcpdump-stdinc.h>
39
40#include <stdio.h>
41#include <string.h>
42
43#include "interface.h"
44#include "addrtoname.h"
45#include "ppp.h"
46#include "ethertype.h"
47#include "ether.h"
48#include "extract.h"			/* must come after interface.h */
49
50/* Codes */
51enum {
52	PPPOE_PADI = 0x09,
53	PPPOE_PADO = 0x07,
54	PPPOE_PADR = 0x19,
55	PPPOE_PADS = 0x65,
56	PPPOE_PADT = 0xa7
57};
58
59static struct tok pppoecode2str[] = {
60	{ PPPOE_PADI, "PADI" },
61	{ PPPOE_PADO, "PADO" },
62	{ PPPOE_PADR, "PADR" },
63	{ PPPOE_PADS, "PADS" },
64	{ PPPOE_PADT, "PADT" },
65	{ 0, "" }, /* PPP Data */
66	{ 0, NULL }
67};
68
69/* Tags */
70enum {
71	PPPOE_EOL = 0,
72	PPPOE_SERVICE_NAME = 0x0101,
73	PPPOE_AC_NAME = 0x0102,
74	PPPOE_HOST_UNIQ = 0x0103,
75	PPPOE_AC_COOKIE = 0x0104,
76	PPPOE_VENDOR = 0x0105,
77	PPPOE_RELAY_SID = 0x0110,
78	PPPOE_SERVICE_NAME_ERROR = 0x0201,
79	PPPOE_AC_SYSTEM_ERROR = 0x0202,
80	PPPOE_GENERIC_ERROR = 0x0203
81};
82
83static struct tok pppoetag2str[] = {
84	{ PPPOE_EOL, "EOL" },
85	{ PPPOE_SERVICE_NAME, "Service-Name" },
86	{ PPPOE_AC_NAME, "AC-Name" },
87	{ PPPOE_HOST_UNIQ, "Host-Uniq" },
88	{ PPPOE_AC_COOKIE, "AC-Cookie" },
89	{ PPPOE_VENDOR, "Vendor-Specific" },
90	{ PPPOE_RELAY_SID, "Relay-Session-ID" },
91	{ PPPOE_SERVICE_NAME_ERROR, "Service-Name-Error" },
92	{ PPPOE_AC_SYSTEM_ERROR, "AC-System-Error" },
93	{ PPPOE_GENERIC_ERROR, "Generic-Error" },
94	{ 0, NULL }
95};
96
97#define PPPOE_HDRLEN 6
98#define MAXTAGPRINT 80
99
100u_int
101pppoe_if_print(const struct pcap_pkthdr *h, register const u_char *p)
102{
103	return (pppoe_print(p, h->len));
104}
105
106u_int
107pppoe_print(register const u_char *bp, u_int length)
108{
109	u_int16_t pppoe_ver, pppoe_type, pppoe_code, pppoe_sessionid;
110	u_int pppoe_length;
111	const u_char *pppoe_packet, *pppoe_payload;
112
113	if (length < PPPOE_HDRLEN) {
114		(void)printf("truncated-pppoe %u", length);
115		return (length);
116	}
117	length -= PPPOE_HDRLEN;
118	pppoe_packet = bp;
119	TCHECK2(*pppoe_packet, PPPOE_HDRLEN);
120	pppoe_ver  = (pppoe_packet[0] & 0xF0) >> 4;
121	pppoe_type  = (pppoe_packet[0] & 0x0F);
122	pppoe_code = pppoe_packet[1];
123	pppoe_sessionid = EXTRACT_16BITS(pppoe_packet + 2);
124	pppoe_length    = EXTRACT_16BITS(pppoe_packet + 4);
125	pppoe_payload = pppoe_packet + PPPOE_HDRLEN;
126
127	if (pppoe_ver != 1) {
128		printf(" [ver %d]",pppoe_ver);
129	}
130	if (pppoe_type != 1) {
131		printf(" [type %d]",pppoe_type);
132	}
133
134	printf("PPPoE %s", tok2str(pppoecode2str, "PAD-%x", pppoe_code));
135	if (pppoe_code == PPPOE_PADI && pppoe_length > 1484 - PPPOE_HDRLEN) {
136		printf(" [len %u!]",pppoe_length);
137	}
138	if (pppoe_length > length) {
139		printf(" [len %u > %u!]", pppoe_length, length);
140		pppoe_length = length;
141	}
142	if (pppoe_sessionid) {
143		printf(" [ses 0x%x]", pppoe_sessionid);
144	}
145
146	if (pppoe_code) {
147		/* PPP session packets don't contain tags */
148		u_short tag_type = 0xffff, tag_len;
149		const u_char *p = pppoe_payload;
150
151		/*
152		 * loop invariant:
153		 * p points to current tag,
154		 * tag_type is previous tag or 0xffff for first iteration
155		 */
156		while (tag_type && p < pppoe_payload + pppoe_length) {
157			TCHECK2(*p, 4);
158			tag_type = EXTRACT_16BITS(p);
159			tag_len = EXTRACT_16BITS(p + 2);
160			p += 4;
161			/* p points to tag_value */
162
163			if (tag_len) {
164				unsigned isascii = 0, isgarbage = 0;
165				const u_char *v = p;
166				char tag_str[MAXTAGPRINT];
167				unsigned tag_str_len = 0;
168
169				/* TODO print UTF-8 decoded text */
170				TCHECK2(*p, tag_len);
171				for (v = p; v < p + tag_len && tag_str_len < MAXTAGPRINT-1; v++)
172					if (*v >= 32 && *v < 127) {
173						tag_str[tag_str_len++] = *v;
174						isascii++;
175					} else {
176						tag_str[tag_str_len++] = '.';
177						isgarbage++;
178					}
179				tag_str[tag_str_len] = 0;
180
181				if (isascii > isgarbage) {
182					printf(" [%s \"%*.*s\"]",
183					       tok2str(pppoetag2str, "TAG-0x%x", tag_type),
184					       (int)tag_str_len,
185					       (int)tag_str_len,
186					       tag_str);
187				} else {
188					/* Print hex, not fast to abuse printf but this doesn't get used much */
189					printf(" [%s 0x", tok2str(pppoetag2str, "TAG-0x%x", tag_type));
190					for (v=p; v<p+tag_len; v++) {
191						printf("%02X", *v);
192					}
193					printf("]");
194				}
195
196
197			} else
198				printf(" [%s]", tok2str(pppoetag2str,
199				    "TAG-0x%x", tag_type));
200
201			p += tag_len;
202			/* p points to next tag */
203		}
204		return (0);
205	} else {
206		/* PPPoE data */
207		printf(" ");
208		return (PPPOE_HDRLEN + ppp_print(pppoe_payload, pppoe_length));
209	}
210
211trunc:
212	printf("[|pppoe]");
213	return (PPPOE_HDRLEN);
214}
215