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