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