blob: 1624c5e50f3b26fbe03191141d9faec5a289e521 [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/*
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.
Elliott Hughes892a68b2015-10-19 14:43:53 -070020 *
21 * Original code by Greg Stark <gsstark@mit.edu>
The Android Open Source Project2949f582009-03-03 19:30:46 -080022 */
23
Elliott Hughes892a68b2015-10-19 14:43:53 -070024#define NETDISSECT_REWORKED
The Android Open Source Project2949f582009-03-03 19:30:46 -080025#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28
29#include <tcpdump-stdinc.h>
30
The Android Open Source Project2949f582009-03-03 19:30:46 -080031#include "interface.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080032#include "extract.h" /* must come after interface.h */
33
34/* Codes */
35enum {
36 PPPOE_PADI = 0x09,
37 PPPOE_PADO = 0x07,
38 PPPOE_PADR = 0x19,
39 PPPOE_PADS = 0x65,
40 PPPOE_PADT = 0xa7
41};
42
JP Abgrall53f17a92014-02-12 14:02:41 -080043static const struct tok pppoecode2str[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -080044 { PPPOE_PADI, "PADI" },
45 { PPPOE_PADO, "PADO" },
46 { PPPOE_PADR, "PADR" },
47 { PPPOE_PADS, "PADS" },
48 { PPPOE_PADT, "PADT" },
49 { 0, "" }, /* PPP Data */
50 { 0, NULL }
51};
52
53/* Tags */
54enum {
55 PPPOE_EOL = 0,
56 PPPOE_SERVICE_NAME = 0x0101,
57 PPPOE_AC_NAME = 0x0102,
58 PPPOE_HOST_UNIQ = 0x0103,
59 PPPOE_AC_COOKIE = 0x0104,
60 PPPOE_VENDOR = 0x0105,
61 PPPOE_RELAY_SID = 0x0110,
JP Abgrall53f17a92014-02-12 14:02:41 -080062 PPPOE_MAX_PAYLOAD = 0x0120,
The Android Open Source Project2949f582009-03-03 19:30:46 -080063 PPPOE_SERVICE_NAME_ERROR = 0x0201,
64 PPPOE_AC_SYSTEM_ERROR = 0x0202,
65 PPPOE_GENERIC_ERROR = 0x0203
66};
67
JP Abgrall53f17a92014-02-12 14:02:41 -080068static const struct tok pppoetag2str[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -080069 { PPPOE_EOL, "EOL" },
70 { PPPOE_SERVICE_NAME, "Service-Name" },
71 { PPPOE_AC_NAME, "AC-Name" },
72 { PPPOE_HOST_UNIQ, "Host-Uniq" },
73 { PPPOE_AC_COOKIE, "AC-Cookie" },
74 { PPPOE_VENDOR, "Vendor-Specific" },
75 { PPPOE_RELAY_SID, "Relay-Session-ID" },
JP Abgrall53f17a92014-02-12 14:02:41 -080076 { PPPOE_MAX_PAYLOAD, "PPP-Max-Payload" },
The Android Open Source Project2949f582009-03-03 19:30:46 -080077 { PPPOE_SERVICE_NAME_ERROR, "Service-Name-Error" },
78 { PPPOE_AC_SYSTEM_ERROR, "AC-System-Error" },
79 { PPPOE_GENERIC_ERROR, "Generic-Error" },
80 { 0, NULL }
81};
82
83#define PPPOE_HDRLEN 6
84#define MAXTAGPRINT 80
85
86u_int
Elliott Hughes892a68b2015-10-19 14:43:53 -070087pppoe_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, register const u_char *p)
The Android Open Source Project2949f582009-03-03 19:30:46 -080088{
Elliott Hughes892a68b2015-10-19 14:43:53 -070089 return (pppoe_print(ndo, p, h->len));
The Android Open Source Project2949f582009-03-03 19:30:46 -080090}
91
92u_int
Elliott Hughes892a68b2015-10-19 14:43:53 -070093pppoe_print(netdissect_options *ndo, register const u_char *bp, u_int length)
The Android Open Source Project2949f582009-03-03 19:30:46 -080094{
Elliott Hughes892a68b2015-10-19 14:43:53 -070095 uint16_t pppoe_ver, pppoe_type, pppoe_code, pppoe_sessionid;
The Android Open Source Project2949f582009-03-03 19:30:46 -080096 u_int pppoe_length;
97 const u_char *pppoe_packet, *pppoe_payload;
98
99 if (length < PPPOE_HDRLEN) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700100 ND_PRINT((ndo, "truncated-pppoe %u", length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800101 return (length);
102 }
103 length -= PPPOE_HDRLEN;
104 pppoe_packet = bp;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700105 ND_TCHECK2(*pppoe_packet, PPPOE_HDRLEN);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800106 pppoe_ver = (pppoe_packet[0] & 0xF0) >> 4;
107 pppoe_type = (pppoe_packet[0] & 0x0F);
108 pppoe_code = pppoe_packet[1];
109 pppoe_sessionid = EXTRACT_16BITS(pppoe_packet + 2);
110 pppoe_length = EXTRACT_16BITS(pppoe_packet + 4);
111 pppoe_payload = pppoe_packet + PPPOE_HDRLEN;
112
113 if (pppoe_ver != 1) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700114 ND_PRINT((ndo, " [ver %d]",pppoe_ver));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800115 }
116 if (pppoe_type != 1) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700117 ND_PRINT((ndo, " [type %d]",pppoe_type));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800118 }
119
Elliott Hughes892a68b2015-10-19 14:43:53 -0700120 ND_PRINT((ndo, "PPPoE %s", tok2str(pppoecode2str, "PAD-%x", pppoe_code)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800121 if (pppoe_code == PPPOE_PADI && pppoe_length > 1484 - PPPOE_HDRLEN) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700122 ND_PRINT((ndo, " [len %u!]",pppoe_length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800123 }
124 if (pppoe_length > length) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700125 ND_PRINT((ndo, " [len %u > %u!]", pppoe_length, length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800126 pppoe_length = length;
127 }
128 if (pppoe_sessionid) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700129 ND_PRINT((ndo, " [ses 0x%x]", pppoe_sessionid));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800130 }
131
132 if (pppoe_code) {
133 /* PPP session packets don't contain tags */
134 u_short tag_type = 0xffff, tag_len;
135 const u_char *p = pppoe_payload;
136
137 /*
138 * loop invariant:
139 * p points to current tag,
140 * tag_type is previous tag or 0xffff for first iteration
141 */
142 while (tag_type && p < pppoe_payload + pppoe_length) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700143 ND_TCHECK2(*p, 4);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800144 tag_type = EXTRACT_16BITS(p);
145 tag_len = EXTRACT_16BITS(p + 2);
146 p += 4;
147 /* p points to tag_value */
148
149 if (tag_len) {
150 unsigned isascii = 0, isgarbage = 0;
JP Abgrall53f17a92014-02-12 14:02:41 -0800151 const u_char *v;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800152 char tag_str[MAXTAGPRINT];
153 unsigned tag_str_len = 0;
154
155 /* TODO print UTF-8 decoded text */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700156 ND_TCHECK2(*p, tag_len);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800157 for (v = p; v < p + tag_len && tag_str_len < MAXTAGPRINT-1; v++)
158 if (*v >= 32 && *v < 127) {
159 tag_str[tag_str_len++] = *v;
160 isascii++;
161 } else {
162 tag_str[tag_str_len++] = '.';
163 isgarbage++;
164 }
165 tag_str[tag_str_len] = 0;
166
167 if (isascii > isgarbage) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700168 ND_PRINT((ndo, " [%s \"%*.*s\"]",
The Android Open Source Project2949f582009-03-03 19:30:46 -0800169 tok2str(pppoetag2str, "TAG-0x%x", tag_type),
170 (int)tag_str_len,
171 (int)tag_str_len,
Elliott Hughes892a68b2015-10-19 14:43:53 -0700172 tag_str));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800173 } else {
174 /* Print hex, not fast to abuse printf but this doesn't get used much */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700175 ND_PRINT((ndo, " [%s 0x", tok2str(pppoetag2str, "TAG-0x%x", tag_type)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800176 for (v=p; v<p+tag_len; v++) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700177 ND_PRINT((ndo, "%02X", *v));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800178 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700179 ND_PRINT((ndo, "]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800180 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700181
The Android Open Source Project2949f582009-03-03 19:30:46 -0800182
183 } else
Elliott Hughes892a68b2015-10-19 14:43:53 -0700184 ND_PRINT((ndo, " [%s]", tok2str(pppoetag2str,
185 "TAG-0x%x", tag_type)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800186
187 p += tag_len;
188 /* p points to next tag */
189 }
190 return (0);
191 } else {
192 /* PPPoE data */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700193 ND_PRINT((ndo, " "));
194 return (PPPOE_HDRLEN + ppp_print(ndo, pppoe_payload, pppoe_length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800195 }
196
197trunc:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700198 ND_PRINT((ndo, "[|pppoe]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800199 return (PPPOE_HDRLEN);
200}