xref: /freebsd/contrib/tcpdump/print-pppoe.c (revision ae83180158c4c937f170e31eff311b18c0286a93)
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 
22 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-pppoe.c,v 1.15 2001/07/05 18:54:17 guy Exp $ (LBL)";
25 #endif
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/socket.h>
34 
35 #include <netinet/in.h>
36 
37 #include <stdio.h>
38 #include <string.h>
39 
40 #include "interface.h"
41 #include "addrtoname.h"
42 #include "ppp.h"
43 #include "ethertype.h"
44 #include "ether.h"
45 #include "extract.h"			/* must come after interface.h */
46 
47 /* Codes */
48 enum {
49 	PPPOE_PADI = 0x09,
50 	PPPOE_PADO = 0x07,
51 	PPPOE_PADR = 0x19,
52 	PPPOE_PADS = 0x65,
53 	PPPOE_PADT = 0xa7
54 };
55 
56 static struct tok pppoecode2str[] = {
57 	{ PPPOE_PADI, "PADI" },
58 	{ PPPOE_PADO, "PADO" },
59 	{ PPPOE_PADR, "PADR" },
60 	{ PPPOE_PADS, "PADS" },
61 	{ PPPOE_PADT, "PADT" },
62 	{ 0, "" }, /* PPP Data */
63 	{ 0, NULL }
64 };
65 
66 /* Tags */
67 enum {
68 	PPPOE_EOL = 0,
69 	PPPOE_SERVICE_NAME = 0x0101,
70 	PPPOE_AC_NAME = 0x0102,
71 	PPPOE_HOST_UNIQ = 0x0103,
72 	PPPOE_AC_COOKIE = 0x0104,
73 	PPPOE_VENDOR = 0x0105,
74 	PPPOE_RELAY_SID = 0x0110,
75 	PPPOE_SERVICE_NAME_ERROR = 0x0201,
76 	PPPOE_AC_SYSTEM_ERROR = 0x0202,
77 	PPPOE_GENERIC_ERROR = 0x0203
78 };
79 
80 static struct tok pppoetag2str[] = {
81 	{ PPPOE_EOL, "EOL" },
82 	{ PPPOE_SERVICE_NAME, "Service-Name" },
83 	{ PPPOE_AC_NAME, "AC-Name" },
84 	{ PPPOE_HOST_UNIQ, "Host-Uniq" },
85 	{ PPPOE_AC_COOKIE, "AC-Cookie" },
86 	{ PPPOE_VENDOR, "Vendor-Specific" },
87 	{ PPPOE_RELAY_SID, "Relay-Session-ID" },
88 	{ PPPOE_SERVICE_NAME_ERROR, "Service-Name-Error" },
89 	{ PPPOE_AC_SYSTEM_ERROR, "AC-System-Error" },
90 	{ PPPOE_GENERIC_ERROR, "Generic-Error" },
91 	{ 0, NULL }
92 };
93 
94 #define PPPOE_HDRLEN 6
95 
96 void
97 pppoe_if_print(u_char *user, const struct pcap_pkthdr *h,
98 	     register const u_char *p)
99 {
100 	register u_int length = h->len;
101 	register u_int caplen = h->caplen;
102 
103 	++infodelay;
104 	ts_print(&h->ts);
105 
106 	/*
107 	 * Some printers want to get back at the link level addresses,
108 	 * and/or check that they're not walking off the end of the packet.
109 	 * Rather than pass them all the way down, we set these globals.
110 	 */
111 	packetp = p;
112 	snapend = p + caplen;
113 
114 	pppoe_print(p, length);
115 	putchar('\n');
116 	--infodelay;
117 	if (infoprint)
118 		info(0);
119 }
120 
121 void
122 pppoe_print(register const u_char *bp, u_int length)
123 {
124 	u_short pppoe_ver, pppoe_type, pppoe_code, pppoe_sessionid, pppoe_length;
125 	const u_char *pppoe_packet, *pppoe_payload;
126 
127 	pppoe_packet = bp;
128 	if (pppoe_packet > snapend) {
129 		printf("[|pppoe]");
130 		return;
131 	}
132 
133 	pppoe_ver  = (pppoe_packet[0] & 0xF0) >> 4;
134 	pppoe_type  = (pppoe_packet[0] & 0x0F);
135 	pppoe_code = pppoe_packet[1];
136 	pppoe_sessionid = EXTRACT_16BITS(pppoe_packet + 2);
137 	pppoe_length    = EXTRACT_16BITS(pppoe_packet + 4);
138 	pppoe_payload = pppoe_packet + PPPOE_HDRLEN;
139 
140 	if (snapend < pppoe_payload) {
141 		printf(" truncated PPPoE");
142 		return;
143 	}
144 
145 	if (pppoe_ver != 1) {
146 		printf(" [ver %d]",pppoe_ver);
147 	}
148 	if (pppoe_type != 1) {
149 		printf(" [type %d]",pppoe_type);
150 	}
151 
152 	printf("PPPoE %s", tok2str(pppoecode2str, "PAD-%x", pppoe_code));
153 	if (pppoe_code == PPPOE_PADI && pppoe_length > 1484 - PPPOE_HDRLEN) {
154 		printf(" [len %d!]",pppoe_length);
155 	}
156 	if (pppoe_sessionid) {
157 		printf(" [ses 0x%x]", pppoe_sessionid);
158 	}
159 
160 	if (pppoe_payload + pppoe_length < snapend) {
161 #if 0
162 		const u_char *x = pppoe_payload + pppoe_length;
163 		printf(" [length %d (%d extra bytes)]",
164 		    pppoe_length, snapend - pppoe_payload - pppoe_length);
165 		default_print(x, snapend - x);
166 #endif
167 		snapend = pppoe_payload+pppoe_length;
168 	}
169 
170 	if (pppoe_code) {
171 		/* PPP session packets don't contain tags */
172 		u_short tag_type = 0xffff, tag_len;
173 		const u_char *p = pppoe_payload;
174 
175 		/*
176 		 * loop invariant:
177 		 * p points to next tag,
178 		 * tag_type is previous tag or 0xffff for first iteration
179 		 */
180 		while (tag_type && p + 4 < pppoe_payload + length &&
181 		    p + 4 < snapend) {
182 			tag_type = EXTRACT_16BITS(p);
183 			tag_len = EXTRACT_16BITS(p + 2);
184 			p += 4;
185 			/* p points to tag_value */
186 
187 			if (tag_len) {
188 				int isascii = 1;
189 				const u_char *v = p;
190 				u_short l;
191 
192 				for (v = p; v < p + tag_len; v++)
193 					if (*v >= 127 || *v < 32) {
194 						isascii = 0;
195 						break;
196 					}
197 
198 				/* TODO print UTF8 decoded text */
199 				if (isascii) {
200 					l = (tag_len < 80 ? tag_len : 80);
201 					printf(" [%s \"%*.*s\"]",
202 					    tok2str(pppoetag2str, "TAG-0x%x", tag_type),
203 					    l, l, p);
204 				} else
205 					printf(" [%s UTF8]",
206 					    tok2str(pppoetag2str, "TAG-0x%x", tag_type));
207 			} else
208 				printf(" [%s]", tok2str(pppoetag2str,
209 				    "TAG-0x%x", tag_type));
210 
211 			p += tag_len;
212 			/* p points to next tag */
213 		}
214 	} else {
215 		printf(" ");
216 		ppp_print(pppoe_payload, pppoe_length);
217 	}
218 	return;
219 }
220