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