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