1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 |
---|
5 | * The Regents of the University of California. All rights reserved. |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that: (1) source code distributions |
---|
9 | * retain the above copyright notice and this paragraph in its entirety, (2) |
---|
10 | * distributions including binary code include the above copyright notice and |
---|
11 | * this paragraph in its entirety in the documentation or other materials |
---|
12 | * provided with the distribution, and (3) all advertising materials mentioning |
---|
13 | * features or use of this software display the following acknowledgement: |
---|
14 | * ``This product includes software developed by the University of California, |
---|
15 | * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of |
---|
16 | * the University nor the names of its contributors may be used to endorse |
---|
17 | * or promote products derived from this software without specific prior |
---|
18 | * written permission. |
---|
19 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED |
---|
20 | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
---|
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
---|
22 | * |
---|
23 | * Original code by Greg Stark <gsstark@mit.edu> |
---|
24 | */ |
---|
25 | |
---|
26 | #ifndef lint |
---|
27 | static const char rcsid[] _U_ = |
---|
28 | "@(#) $Header: /tcpdump/master/tcpdump/print-pppoe.c,v 1.31 2005-04-26 19:48:38 guy Exp $ (LBL)"; |
---|
29 | #endif |
---|
30 | |
---|
31 | #ifdef HAVE_CONFIG_H |
---|
32 | #include "config.h" |
---|
33 | #endif |
---|
34 | |
---|
35 | #include <tcpdump-stdinc.h> |
---|
36 | |
---|
37 | #include <stdio.h> |
---|
38 | #include <string.h> |
---|
39 | |
---|
40 | #include "interface.h" |
---|
41 | #include "addrtoname.h" |
---|
42 | #include "ppp.h" |
---|
43 | #include "ethertype.h" |
---|
44 | #include "ether.h" |
---|
45 | #include "extract.h" /* must come after interface.h */ |
---|
46 | |
---|
47 | /* Codes */ |
---|
48 | enum { |
---|
49 | PPPOE_PADI = 0x09, |
---|
50 | PPPOE_PADO = 0x07, |
---|
51 | PPPOE_PADR = 0x19, |
---|
52 | PPPOE_PADS = 0x65, |
---|
53 | PPPOE_PADT = 0xa7 |
---|
54 | }; |
---|
55 | |
---|
56 | static struct tok pppoecode2str[] = { |
---|
57 | { PPPOE_PADI, "PADI" }, |
---|
58 | { PPPOE_PADO, "PADO" }, |
---|
59 | { PPPOE_PADR, "PADR" }, |
---|
60 | { PPPOE_PADS, "PADS" }, |
---|
61 | { PPPOE_PADT, "PADT" }, |
---|
62 | { 0, "" }, /* PPP Data */ |
---|
63 | { 0, NULL } |
---|
64 | }; |
---|
65 | |
---|
66 | /* Tags */ |
---|
67 | enum { |
---|
68 | PPPOE_EOL = 0, |
---|
69 | PPPOE_SERVICE_NAME = 0x0101, |
---|
70 | PPPOE_AC_NAME = 0x0102, |
---|
71 | PPPOE_HOST_UNIQ = 0x0103, |
---|
72 | PPPOE_AC_COOKIE = 0x0104, |
---|
73 | PPPOE_VENDOR = 0x0105, |
---|
74 | PPPOE_RELAY_SID = 0x0110, |
---|
75 | PPPOE_MAX_PAYLOAD = 0x0120, |
---|
76 | PPPOE_SERVICE_NAME_ERROR = 0x0201, |
---|
77 | PPPOE_AC_SYSTEM_ERROR = 0x0202, |
---|
78 | PPPOE_GENERIC_ERROR = 0x0203 |
---|
79 | }; |
---|
80 | |
---|
81 | static struct tok pppoetag2str[] = { |
---|
82 | { PPPOE_EOL, "EOL" }, |
---|
83 | { PPPOE_SERVICE_NAME, "Service-Name" }, |
---|
84 | { PPPOE_AC_NAME, "AC-Name" }, |
---|
85 | { PPPOE_HOST_UNIQ, "Host-Uniq" }, |
---|
86 | { PPPOE_AC_COOKIE, "AC-Cookie" }, |
---|
87 | { PPPOE_VENDOR, "Vendor-Specific" }, |
---|
88 | { PPPOE_RELAY_SID, "Relay-Session-ID" }, |
---|
89 | { PPPOE_MAX_PAYLOAD, "PPP-Max-Payload" }, |
---|
90 | { PPPOE_SERVICE_NAME_ERROR, "Service-Name-Error" }, |
---|
91 | { PPPOE_AC_SYSTEM_ERROR, "AC-System-Error" }, |
---|
92 | { PPPOE_GENERIC_ERROR, "Generic-Error" }, |
---|
93 | { 0, NULL } |
---|
94 | }; |
---|
95 | |
---|
96 | #define PPPOE_HDRLEN 6 |
---|
97 | #define MAXTAGPRINT 80 |
---|
98 | |
---|
99 | u_int |
---|
100 | pppoe_if_print(const struct pcap_pkthdr *h, register const u_char *p) |
---|
101 | { |
---|
102 | return (pppoe_print(p, h->len)); |
---|
103 | } |
---|
104 | |
---|
105 | u_int |
---|
106 | pppoe_print(register const u_char *bp, u_int length) |
---|
107 | { |
---|
108 | u_int16_t pppoe_ver, pppoe_type, pppoe_code, pppoe_sessionid; |
---|
109 | u_int pppoe_length; |
---|
110 | const u_char *pppoe_packet, *pppoe_payload; |
---|
111 | |
---|
112 | if (length < PPPOE_HDRLEN) { |
---|
113 | (void)printf("truncated-pppoe %u", length); |
---|
114 | return (length); |
---|
115 | } |
---|
116 | length -= PPPOE_HDRLEN; |
---|
117 | pppoe_packet = bp; |
---|
118 | TCHECK2(*pppoe_packet, PPPOE_HDRLEN); |
---|
119 | pppoe_ver = (pppoe_packet[0] & 0xF0) >> 4; |
---|
120 | pppoe_type = (pppoe_packet[0] & 0x0F); |
---|
121 | pppoe_code = pppoe_packet[1]; |
---|
122 | pppoe_sessionid = EXTRACT_16BITS(pppoe_packet + 2); |
---|
123 | pppoe_length = EXTRACT_16BITS(pppoe_packet + 4); |
---|
124 | pppoe_payload = pppoe_packet + PPPOE_HDRLEN; |
---|
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 %u!]",pppoe_length); |
---|
136 | } |
---|
137 | if (pppoe_length > length) { |
---|
138 | printf(" [len %u > %u!]", pppoe_length, length); |
---|
139 | pppoe_length = length; |
---|
140 | } |
---|
141 | if (pppoe_sessionid) { |
---|
142 | printf(" [ses 0x%x]", pppoe_sessionid); |
---|
143 | } |
---|
144 | |
---|
145 | if (pppoe_code) { |
---|
146 | /* PPP session packets don't contain tags */ |
---|
147 | u_short tag_type = 0xffff, tag_len; |
---|
148 | const u_char *p = pppoe_payload; |
---|
149 | |
---|
150 | /* |
---|
151 | * loop invariant: |
---|
152 | * p points to current tag, |
---|
153 | * tag_type is previous tag or 0xffff for first iteration |
---|
154 | */ |
---|
155 | while (tag_type && p < pppoe_payload + pppoe_length) { |
---|
156 | TCHECK2(*p, 4); |
---|
157 | tag_type = EXTRACT_16BITS(p); |
---|
158 | tag_len = EXTRACT_16BITS(p + 2); |
---|
159 | p += 4; |
---|
160 | /* p points to tag_value */ |
---|
161 | |
---|
162 | if (tag_len) { |
---|
163 | unsigned isascii = 0, isgarbage = 0; |
---|
164 | const u_char *v; |
---|
165 | char tag_str[MAXTAGPRINT]; |
---|
166 | unsigned tag_str_len = 0; |
---|
167 | |
---|
168 | /* TODO print UTF-8 decoded text */ |
---|
169 | TCHECK2(*p, tag_len); |
---|
170 | for (v = p; v < p + tag_len && tag_str_len < MAXTAGPRINT-1; v++) |
---|
171 | if (*v >= 32 && *v < 127) { |
---|
172 | tag_str[tag_str_len++] = *v; |
---|
173 | isascii++; |
---|
174 | } else { |
---|
175 | tag_str[tag_str_len++] = '.'; |
---|
176 | isgarbage++; |
---|
177 | } |
---|
178 | tag_str[tag_str_len] = 0; |
---|
179 | |
---|
180 | if (isascii > isgarbage) { |
---|
181 | printf(" [%s \"%*.*s\"]", |
---|
182 | tok2str(pppoetag2str, "TAG-0x%x", tag_type), |
---|
183 | (int)tag_str_len, |
---|
184 | (int)tag_str_len, |
---|
185 | tag_str); |
---|
186 | } else { |
---|
187 | /* Print hex, not fast to abuse printf but this doesn't get used much */ |
---|
188 | printf(" [%s 0x", tok2str(pppoetag2str, "TAG-0x%x", tag_type)); |
---|
189 | for (v=p; v<p+tag_len; v++) { |
---|
190 | printf("%02X", *v); |
---|
191 | } |
---|
192 | printf("]"); |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | } else |
---|
197 | printf(" [%s]", tok2str(pppoetag2str, |
---|
198 | "TAG-0x%x", tag_type)); |
---|
199 | |
---|
200 | p += tag_len; |
---|
201 | /* p points to next tag */ |
---|
202 | } |
---|
203 | return (0); |
---|
204 | } else { |
---|
205 | /* PPPoE data */ |
---|
206 | printf(" "); |
---|
207 | return (PPPOE_HDRLEN + ppp_print(pppoe_payload, pppoe_length)); |
---|
208 | } |
---|
209 | |
---|
210 | trunc: |
---|
211 | printf("[|pppoe]"); |
---|
212 | return (PPPOE_HDRLEN); |
---|
213 | } |
---|