Explicitly request literal mode after .Xr.
[netbsd-mini2440.git] / dist / tcpdump / print-pppoe.c
blob68a34f11811c84ec3c162b0af9ad86b6edab696a
1 /* $NetBSD$ */
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.
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.
23 * Original code by Greg Stark <gsstark@mit.edu>
26 #include <sys/cdefs.h>
27 #ifndef lint
28 #if 0
29 static const char rcsid[] _U_ =
30 "@(#) Header: /tcpdump/master/tcpdump/print-pppoe.c,v 1.30.2.1 2005/04/26 19:48:56 guy Exp (LBL)";
31 #else
32 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
33 #endif
34 #endif
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
40 #include <tcpdump-stdinc.h>
42 #include <stdio.h>
43 #include <string.h>
45 #include "interface.h"
46 #include "addrtoname.h"
47 #include "ppp.h"
48 #include "ethertype.h"
49 #include "ether.h"
50 #include "extract.h" /* must come after interface.h */
52 /* Codes */
53 enum {
54 PPPOE_PADI = 0x09,
55 PPPOE_PADO = 0x07,
56 PPPOE_PADR = 0x19,
57 PPPOE_PADS = 0x65,
58 PPPOE_PADT = 0xa7
61 static struct tok pppoecode2str[] = {
62 { PPPOE_PADI, "PADI" },
63 { PPPOE_PADO, "PADO" },
64 { PPPOE_PADR, "PADR" },
65 { PPPOE_PADS, "PADS" },
66 { PPPOE_PADT, "PADT" },
67 { 0, "" }, /* PPP Data */
68 { 0, NULL }
71 /* Tags */
72 enum {
73 PPPOE_EOL = 0,
74 PPPOE_SERVICE_NAME = 0x0101,
75 PPPOE_AC_NAME = 0x0102,
76 PPPOE_HOST_UNIQ = 0x0103,
77 PPPOE_AC_COOKIE = 0x0104,
78 PPPOE_VENDOR = 0x0105,
79 PPPOE_RELAY_SID = 0x0110,
80 PPPOE_SERVICE_NAME_ERROR = 0x0201,
81 PPPOE_AC_SYSTEM_ERROR = 0x0202,
82 PPPOE_GENERIC_ERROR = 0x0203
85 static struct tok pppoetag2str[] = {
86 { PPPOE_EOL, "EOL" },
87 { PPPOE_SERVICE_NAME, "Service-Name" },
88 { PPPOE_AC_NAME, "AC-Name" },
89 { PPPOE_HOST_UNIQ, "Host-Uniq" },
90 { PPPOE_AC_COOKIE, "AC-Cookie" },
91 { PPPOE_VENDOR, "Vendor-Specific" },
92 { PPPOE_RELAY_SID, "Relay-Session-ID" },
93 { PPPOE_SERVICE_NAME_ERROR, "Service-Name-Error" },
94 { PPPOE_AC_SYSTEM_ERROR, "AC-System-Error" },
95 { PPPOE_GENERIC_ERROR, "Generic-Error" },
96 { 0, NULL }
99 #define PPPOE_HDRLEN 6
100 #define MAXTAGPRINT 80
102 u_int
103 pppoe_if_print(const struct pcap_pkthdr *h, register const u_char *p)
105 return (pppoe_print(p, h->len));
108 u_int
109 pppoe_print(register const u_char *bp, u_int length)
111 u_int16_t pppoe_ver, pppoe_type, pppoe_code, pppoe_sessionid;
112 u_int pppoe_length;
113 const u_char *pppoe_packet, *pppoe_payload;
115 if (length < PPPOE_HDRLEN) {
116 (void)printf("truncated-pppoe %u", length);
117 return (length);
119 length -= PPPOE_HDRLEN;
120 pppoe_packet = bp;
121 TCHECK2(*pppoe_packet, PPPOE_HDRLEN);
122 pppoe_ver = (pppoe_packet[0] & 0xF0) >> 4;
123 pppoe_type = (pppoe_packet[0] & 0x0F);
124 pppoe_code = pppoe_packet[1];
125 pppoe_sessionid = EXTRACT_16BITS(pppoe_packet + 2);
126 pppoe_length = EXTRACT_16BITS(pppoe_packet + 4);
127 pppoe_payload = pppoe_packet + PPPOE_HDRLEN;
129 if (pppoe_ver != 1) {
130 printf(" [ver %d]",pppoe_ver);
132 if (pppoe_type != 1) {
133 printf(" [type %d]",pppoe_type);
136 printf("PPPoE %s", tok2str(pppoecode2str, "PAD-%x", pppoe_code));
137 if (pppoe_code == PPPOE_PADI && pppoe_length > 1484 - PPPOE_HDRLEN) {
138 printf(" [len %u!]",pppoe_length);
140 if (pppoe_length > length) {
141 printf(" [len %u > %u!]", pppoe_length, length);
142 pppoe_length = length;
144 if (pppoe_sessionid) {
145 printf(" [ses 0x%x]", pppoe_sessionid);
148 if (pppoe_code) {
149 /* PPP session packets don't contain tags */
150 u_short tag_type = 0xffff, tag_len;
151 const u_char *p = pppoe_payload;
154 * loop invariant:
155 * p points to current tag,
156 * tag_type is previous tag or 0xffff for first iteration
158 while (tag_type && p < pppoe_payload + pppoe_length) {
159 TCHECK2(*p, 4);
160 tag_type = EXTRACT_16BITS(p);
161 tag_len = EXTRACT_16BITS(p + 2);
162 p += 4;
163 /* p points to tag_value */
165 if (tag_len) {
166 unsigned isascii = 0, isgarbage = 0;
167 const u_char *v = p;
168 char tag_str[MAXTAGPRINT];
169 unsigned tag_str_len = 0;
171 /* TODO print UTF-8 decoded text */
172 TCHECK2(*p, tag_len);
173 for (v = p; v < p + tag_len && tag_str_len < MAXTAGPRINT-1; v++)
174 if (*v >= 32 && *v < 127) {
175 tag_str[tag_str_len++] = *v;
176 isascii++;
177 } else {
178 tag_str[tag_str_len++] = '.';
179 isgarbage++;
181 tag_str[tag_str_len] = 0;
183 if (isascii > isgarbage) {
184 printf(" [%s \"%*.*s\"]",
185 tok2str(pppoetag2str, "TAG-0x%x", tag_type),
186 (int)tag_str_len,
187 (int)tag_str_len,
188 tag_str);
189 } else {
190 /* Print hex, not fast to abuse printf but this doesn't get used much */
191 printf(" [%s 0x", tok2str(pppoetag2str, "TAG-0x%x", tag_type));
192 for (v=p; v<p+tag_len; v++) {
193 printf("%02X", *v);
195 printf("]");
199 } else
200 printf(" [%s]", tok2str(pppoetag2str,
201 "TAG-0x%x", tag_type));
203 p += tag_len;
204 /* p points to next tag */
206 return (0);
207 } else {
208 /* PPPoE data */
209 printf(" ");
210 return (PPPOE_HDRLEN + ppp_print(pppoe_payload, pppoe_length));
213 trunc:
214 printf("[|pppoe]");
215 return (PPPOE_HDRLEN);