xref: /netbsd-src/external/bsd/tcpdump/dist/print-pppoe.c (revision cc576e1d8e4f4078fd4e81238abca9fca216f6ec)
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 __RCSID("$NetBSD: print-pppoe.c,v 1.6 2017/01/24 23:29:14 christos Exp $");
27 #endif
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <netdissect-stdinc.h>
34 
35 #include "netdissect.h"
36 #include "extract.h"
37 
38 /* Codes */
39 enum {
40 	PPPOE_PADI = 0x09,
41 	PPPOE_PADO = 0x07,
42 	PPPOE_PADR = 0x19,
43 	PPPOE_PADS = 0x65,
44 	PPPOE_PADT = 0xa7
45 };
46 
47 static const struct tok pppoecode2str[] = {
48 	{ PPPOE_PADI, "PADI" },
49 	{ PPPOE_PADO, "PADO" },
50 	{ PPPOE_PADR, "PADR" },
51 	{ PPPOE_PADS, "PADS" },
52 	{ PPPOE_PADT, "PADT" },
53 	{ 0, "" }, /* PPP Data */
54 	{ 0, NULL }
55 };
56 
57 /* Tags */
58 enum {
59 	PPPOE_EOL = 0,
60 	PPPOE_SERVICE_NAME = 0x0101,
61 	PPPOE_AC_NAME = 0x0102,
62 	PPPOE_HOST_UNIQ = 0x0103,
63 	PPPOE_AC_COOKIE = 0x0104,
64 	PPPOE_VENDOR = 0x0105,
65 	PPPOE_RELAY_SID = 0x0110,
66 	PPPOE_MAX_PAYLOAD = 0x0120,
67 	PPPOE_SERVICE_NAME_ERROR = 0x0201,
68 	PPPOE_AC_SYSTEM_ERROR = 0x0202,
69 	PPPOE_GENERIC_ERROR = 0x0203
70 };
71 
72 static const struct tok pppoetag2str[] = {
73 	{ PPPOE_EOL, "EOL" },
74 	{ PPPOE_SERVICE_NAME, "Service-Name" },
75 	{ PPPOE_AC_NAME, "AC-Name" },
76 	{ PPPOE_HOST_UNIQ, "Host-Uniq" },
77 	{ PPPOE_AC_COOKIE, "AC-Cookie" },
78 	{ PPPOE_VENDOR, "Vendor-Specific" },
79 	{ PPPOE_RELAY_SID, "Relay-Session-ID" },
80 	{ PPPOE_MAX_PAYLOAD, "PPP-Max-Payload" },
81 	{ PPPOE_SERVICE_NAME_ERROR, "Service-Name-Error" },
82 	{ PPPOE_AC_SYSTEM_ERROR, "AC-System-Error" },
83 	{ PPPOE_GENERIC_ERROR, "Generic-Error" },
84 	{ 0, NULL }
85 };
86 
87 #define PPPOE_HDRLEN 6
88 #define MAXTAGPRINT 80
89 
90 u_int
91 pppoe_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, register const u_char *p)
92 {
93 	return (pppoe_print(ndo, p, h->len));
94 }
95 
96 u_int
97 pppoe_print(netdissect_options *ndo, register const u_char *bp, u_int length)
98 {
99 	uint16_t pppoe_ver, pppoe_type, pppoe_code, pppoe_sessionid;
100 	u_int pppoe_length;
101 	const u_char *pppoe_packet, *pppoe_payload;
102 
103 	if (length < PPPOE_HDRLEN) {
104 		ND_PRINT((ndo, "truncated-pppoe %u", length));
105 		return (length);
106 	}
107 	length -= PPPOE_HDRLEN;
108 	pppoe_packet = bp;
109 	ND_TCHECK2(*pppoe_packet, PPPOE_HDRLEN);
110 	pppoe_ver  = (pppoe_packet[0] & 0xF0) >> 4;
111 	pppoe_type  = (pppoe_packet[0] & 0x0F);
112 	pppoe_code = pppoe_packet[1];
113 	pppoe_sessionid = EXTRACT_16BITS(pppoe_packet + 2);
114 	pppoe_length    = EXTRACT_16BITS(pppoe_packet + 4);
115 	pppoe_payload = pppoe_packet + PPPOE_HDRLEN;
116 
117 	if (pppoe_ver != 1) {
118 		ND_PRINT((ndo, " [ver %d]",pppoe_ver));
119 	}
120 	if (pppoe_type != 1) {
121 		ND_PRINT((ndo, " [type %d]",pppoe_type));
122 	}
123 
124 	ND_PRINT((ndo, "PPPoE %s", tok2str(pppoecode2str, "PAD-%x", pppoe_code)));
125 	if (pppoe_code == PPPOE_PADI && pppoe_length > 1484 - PPPOE_HDRLEN) {
126 		ND_PRINT((ndo, " [len %u!]",pppoe_length));
127 	}
128 	if (pppoe_length > length) {
129 		ND_PRINT((ndo, " [len %u > %u!]", pppoe_length, length));
130 		pppoe_length = length;
131 	}
132 	if (pppoe_sessionid) {
133 		ND_PRINT((ndo, " [ses 0x%x]", pppoe_sessionid));
134 	}
135 
136 	if (pppoe_code) {
137 		/* PPP session packets don't contain tags */
138 		u_short tag_type = 0xffff, tag_len;
139 		const u_char *p = pppoe_payload;
140 
141 		/*
142 		 * loop invariant:
143 		 * p points to current tag,
144 		 * tag_type is previous tag or 0xffff for first iteration
145 		 */
146 		while (tag_type && p < pppoe_payload + pppoe_length) {
147 			ND_TCHECK2(*p, 4);
148 			tag_type = EXTRACT_16BITS(p);
149 			tag_len = EXTRACT_16BITS(p + 2);
150 			p += 4;
151 			/* p points to tag_value */
152 
153 			if (tag_len) {
154 				unsigned ascii_count = 0, garbage_count = 0;
155 				const u_char *v;
156 				char tag_str[MAXTAGPRINT];
157 				unsigned tag_str_len = 0;
158 
159 				/* TODO print UTF-8 decoded text */
160 				ND_TCHECK2(*p, tag_len);
161 				for (v = p; v < p + tag_len && tag_str_len < MAXTAGPRINT-1; v++)
162 					if (*v >= 32 && *v < 127) {
163 						tag_str[tag_str_len++] = *v;
164 						ascii_count++;
165 					} else {
166 						tag_str[tag_str_len++] = '.';
167 						garbage_count++;
168 					}
169 				tag_str[tag_str_len] = 0;
170 
171 				if (ascii_count > garbage_count) {
172 					ND_PRINT((ndo, " [%s \"%*.*s\"]",
173 					       tok2str(pppoetag2str, "TAG-0x%x", tag_type),
174 					       (int)tag_str_len,
175 					       (int)tag_str_len,
176 					       tag_str));
177 				} else {
178 					/* Print hex, not fast to abuse printf but this doesn't get used much */
179 					ND_PRINT((ndo, " [%s 0x", tok2str(pppoetag2str, "TAG-0x%x", tag_type)));
180 					for (v=p; v<p+tag_len; v++) {
181 						ND_PRINT((ndo, "%02X", *v));
182 					}
183 					ND_PRINT((ndo, "]"));
184 				}
185 
186 
187 			} else
188 				ND_PRINT((ndo, " [%s]", tok2str(pppoetag2str,
189 				    "TAG-0x%x", tag_type)));
190 
191 			p += tag_len;
192 			/* p points to next tag */
193 		}
194 		return (0);
195 	} else {
196 		/* PPPoE data */
197 		ND_PRINT((ndo, " "));
198 		return (PPPOE_HDRLEN + ppp_print(ndo, pppoe_payload, pppoe_length));
199 	}
200 
201 trunc:
202 	ND_PRINT((ndo, "[|pppoe]"));
203 	return (PPPOE_HDRLEN);
204 }
205