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