]> The Tcpdump Group git mirrors - tcpdump/blob - print-pppoe.c
Get rid of unneeded includes of <net/if.h>.
[tcpdump] / print-pppoe.c
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.9 2000-09-28 06:43:06 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 struct mbuf;
36 struct rtentry;
37
38 #include <netinet/in.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 */
51 enum {
52 PPPOE_PADI = 0x09,
53 PPPOE_PADO = 0x07,
54 PPPOE_PADR = 0x19,
55 PPPOE_PADS = 0x65,
56 PPPOE_PADT = 0xa7
57 };
58
59 static 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 */
70 enum {
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
83 static 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
99 void
100 pppoe_print(register const u_char *bp, u_int length)
101 {
102 register const struct ether_header *eh;
103 register u_short pppoe_ver, pppoe_type, pppoe_code, pppoe_sessionid, pppoe_length;
104 const u_char *pppoe_packet, *pppoe_payload;
105
106 eh = (struct ether_header *)packetp;
107 pppoe_packet = packetp+sizeof(struct ether_header);
108 if (pppoe_packet > snapend) {
109 printf("[|pppoe]");
110 return;
111 }
112
113 pppoe_ver = (pppoe_packet[0]&0xF0)>>4;
114 pppoe_type = (pppoe_packet[0]&0x0F);
115 pppoe_code = (pppoe_packet[1]);
116 pppoe_sessionid = (EXTRACT_16BITS(pppoe_packet+2));
117 pppoe_length = (EXTRACT_16BITS(pppoe_packet+4));
118 pppoe_payload = pppoe_packet+6;
119
120 if (snapend < pppoe_payload) {
121 printf(" truncated PPPoE");
122 return;
123 }
124
125 if (pppoe_ver != 1) {
126 printf(" [ver %d]",pppoe_ver);
127 }
128 if (pppoe_type != 1) {
129 printf(" [type %d]",pppoe_type);
130 }
131
132 printf("PPPoE %s", tok2str(pppoecode2str, "PAD-%x", pppoe_code));
133 if (pppoe_code == PPPOE_PADI && pppoe_length > 1484-PPPOE_HDRLEN) {
134 printf(" [len %d!]",pppoe_length);
135 }
136 if (pppoe_sessionid) {
137 printf(" [ses 0x%x]",pppoe_sessionid);
138 }
139
140 if (pppoe_payload + pppoe_length < snapend) {
141 /*
142 printf(" [length %d (%d extra bytes)]", pppoe_length, snapend-pppoe_payload-pppoe_length);
143 {
144 const u_char *x = pppoe_payload+pppoe_length;
145 default_print(x, snapend - x);
146 }
147 */
148 snapend = pppoe_payload+pppoe_length;
149 }
150
151
152 if (pppoe_code) {
153 /* PPP session packets don't contain tags */
154 u_short tag_type = 0xffff, tag_len;
155 const u_char *p = pppoe_payload;
156
157 /* loop invariant:
158 p points to next tag,
159 tag_type is previous tag or 0xffff for first iteration
160 */
161 while (tag_type &&
162 p+4 < pppoe_payload + length &&
163 p+4 < snapend) {
164 tag_type = EXTRACT_16BITS(p);
165 tag_len = EXTRACT_16BITS(p+2);
166 p += 4;
167 /* p points to tag_value */
168
169 if (tag_len) {
170 int isascii = 1;
171 const u_char *v = p;
172
173 for (v=p; v<p+tag_len; v++)
174 if (*v >= 127 || *v < 32) {
175 isascii = 0;
176 break;
177 }
178
179 /* TODO print UTF8 decoded text */
180 if (isascii)
181 printf(" [%s \"%*.*s\"]",
182 tok2str(pppoetag2str, "TAG-0x%x", tag_type),
183 tag_len < 80 ? tag_len : 80,
184 tag_len < 80 ? tag_len : 80,
185 p
186 );
187 else
188 printf(" [%s UTF8]", tok2str(pppoetag2str, "TAG-0x%x", tag_type));
189 } else
190 printf(" [%s]", tok2str(pppoetag2str, "TAG-0x%x", tag_type));
191
192 p += tag_len;
193 /* p points to next tag */
194 }
195 } else {
196 #if 0
197 /* We now make use of ppp_print() instead, because it has more
198 comprehensive support for PPP. It also gives us a consistent
199 output with other protocols like L2TP. */
200 u_short ptype;
201 if (pppoe_payload[0] & 0x1) {
202 ptype = pppoe_payload[0];
203 pppoe_payload +=1;
204 pppoe_length -=1;
205 } else if (pppoe_payload[1] & 0x1) {
206 ptype = ntohs(*(u_short *)pppoe_payload);
207 pppoe_payload +=2;
208 pppoe_length -=2;
209 } else {
210 printf(" Invalid PPP protocol ID: %x %x", pppoe_payload[0],pppoe_payload[1]);
211 return;
212 }
213 printf(" ");
214 if (ptype == PPP_IP)
215 ip_print(pppoe_payload, pppoe_length);
216 else if (ptype == PPP_LCP)
217 lcp_print(pppoe_payload, pppoe_length);
218 else
219 printf("%s ", tok2str(ppptype2str, "proto-0x%x", ptype));
220 #endif
221 printf(" ");
222 ppp_print(pppoe_payload);
223 }
224 return;
225 }