]> The Tcpdump Group git mirrors - tcpdump/blob - print-pppoe.c
remove trailing comma in enum
[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.5 2000-07-10 04:36:19 assar 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 #include <net/if.h>
38
39 #include <netinet/in.h>
40 #include <netinet/if_ether.h>
41
42 #include <stdio.h>
43 #include <string.h>
44
45 #include "interface.h"
46 #include "addrtoname.h"
47 #include "ppp.h"
48 #include "ethertype.h"
49 #include "extract.h" /* must come after interface.h */
50
51 /* Codes */
52 enum {
53 PPPOE_PADI = 0x09,
54 PPPOE_PADO = 0x07,
55 PPPOE_PADR = 0x19,
56 PPPOE_PADS = 0x65,
57 PPPOE_PADT = 0xa7
58 };
59
60 static struct tok pppoecode2str[] = {
61 { PPPOE_PADI, "PADI"},
62 { PPPOE_PADO, "PADO"},
63 { PPPOE_PADR, "PADR"},
64 { PPPOE_PADS, "PADS"},
65 { PPPOE_PADT, "PADT"},
66 { 0, ""}, /* PPP Data */
67 { 0, NULL }
68 };
69
70 /* Tags */
71 enum {
72 PPPOE_EOL = 0,
73 PPPOE_SERVICE_NAME = 0x0101,
74 PPPOE_AC_NAME = 0x0102,
75 PPPOE_HOST_UNIQ = 0x0103,
76 PPPOE_AC_COOKIE = 0x0104,
77 PPPOE_VENDOR = 0x0105,
78 PPPOE_RELAY_SID = 0x0110,
79 PPPOE_SERVICE_NAME_ERROR = 0x0201,
80 PPPOE_AC_SYSTEM_ERROR = 0x0202,
81 PPPOE_GENERIC_ERROR = 0x0203
82 };
83
84 static struct tok pppoetag2str[] = {
85 { PPPOE_EOL, "EOL"},
86 { PPPOE_SERVICE_NAME, "Service-Name" },
87 { PPPOE_AC_NAME, "AC-Name" },
88 { PPPOE_HOST_UNIQ, "Host-Uniq" },
89 { PPPOE_AC_COOKIE, "AC-Cookie" },
90 { PPPOE_VENDOR, "Vendor-Specific" },
91 { PPPOE_RELAY_SID, "Relay-Session-ID" },
92 { PPPOE_SERVICE_NAME_ERROR, "Service-Name-Error" },
93 { PPPOE_AC_SYSTEM_ERROR, "AC-System-Error" },
94 { PPPOE_GENERIC_ERROR, "Generic-Error" },
95 { 0, NULL}
96 };
97
98 #define PPPOE_HDRLEN 6
99
100 void
101 pppoe_print(register const u_char *bp, u_int length)
102 {
103 register const struct ether_header *eh;
104 register u_short pppoe_ver, pppoe_type, pppoe_code, pppoe_sessionid, pppoe_length;
105 const u_char *pppoe_packet, *pppoe_payload;
106
107 eh = (struct ether_header *)packetp;
108 pppoe_packet = packetp+sizeof(struct ether_header);
109 if (pppoe_packet > snapend) {
110 printf("[|pppoe]");
111 return;
112 }
113
114 pppoe_ver = (pppoe_packet[0]&0xF0)>>4;
115 pppoe_type = (pppoe_packet[0]&0x0F);
116 pppoe_code = (pppoe_packet[1]);
117 pppoe_sessionid = (EXTRACT_16BITS(pppoe_packet+2));
118 pppoe_length = (EXTRACT_16BITS(pppoe_packet+4));
119 pppoe_payload = pppoe_packet+6;
120
121 if (snapend < pppoe_payload) {
122 printf(" truncated PPPoE");
123 return;
124 }
125
126 if (pppoe_ver != 1) {
127 printf(" [ver %d]",pppoe_ver);
128 }
129 if (pppoe_type != 1) {
130 printf(" [type %d]",pppoe_type);
131 }
132
133 printf("PPPoE %s", tok2str(pppoecode2str, "PAD-%x", pppoe_code));
134 if (pppoe_code == PPPOE_PADI && pppoe_length > 1484-PPPOE_HDRLEN) {
135 printf(" [len %d!]",pppoe_length);
136 }
137 if (pppoe_sessionid) {
138 printf(" [ses 0x%x]",pppoe_sessionid);
139 }
140
141 if (pppoe_payload + pppoe_length < snapend) {
142 /*
143 printf(" [length %d (%d extra bytes)]", pppoe_length, snapend-pppoe_payload-pppoe_length);
144 {
145 const u_char *x = pppoe_payload+pppoe_length;
146 default_print(x, snapend - x);
147 }
148 */
149 snapend = pppoe_payload+pppoe_length;
150 }
151
152
153 if (pppoe_code) {
154 /* PPP session packets don't contain tags */
155 u_short tag_type = -1, tag_len;
156 const u_char *p = pppoe_payload;
157
158 /* loop invariant:
159 p points to next tag,
160 tag_type is previous tag or -1 for first iteration
161 */
162 while (tag_type &&
163 p+4 < pppoe_payload + length &&
164 p+4 < snapend) {
165 tag_type = EXTRACT_16BITS(p);
166 tag_len = EXTRACT_16BITS(p+2);
167 p += 4;
168 /* p points to tag_value */
169
170 if (tag_len) {
171 int isascii = 1;
172 const u_char *v = p;
173
174 for (v=p; v<p+tag_len; v++)
175 if (*v >= 127 || *v < 32) {
176 isascii = 0;
177 break;
178 }
179
180 /* TODO print UTF8 decoded text */
181 if (isascii)
182 printf(" [%s \"%*.*s\"]",
183 tok2str(pppoetag2str, "TAG-0x%x", tag_type),
184 tag_len < 80 ? tag_len : 80,
185 tag_len < 80 ? tag_len : 80,
186 p
187 );
188 else
189 printf(" [%s UTF8]", tok2str(pppoetag2str, "TAG-0x%x", tag_type));
190 } else
191 printf(" [%s]", tok2str(pppoetag2str, "TAG-0x%x", tag_type));
192
193 p += tag_len;
194 /* p points to next tag */
195 }
196 } else {
197 u_short ptype;
198 if (pppoe_payload[0] & 0x1) {
199 ptype = pppoe_payload[0];
200 pppoe_payload +=1;
201 pppoe_length -=1;
202 } else if (pppoe_payload[1] & 0x1) {
203 ptype = ntohs(*(u_short *)pppoe_payload);
204 pppoe_payload +=2;
205 pppoe_length -=2;
206 } else {
207 printf(" Invalid PPP protocol ID: %x %x", pppoe_payload[0],pppoe_payload[1]);
208 return;
209 }
210 printf(" ");
211 if (ptype == PPP_IP)
212 ip_print(pppoe_payload, pppoe_length);
213 else if (ptype == PPP_LCP)
214 lcp_print(pppoe_payload, pppoe_length);
215 else
216 printf("%s ", tok2str(ppptype2str, "proto-0x%x", ptype));
217 }
218 return;
219 }