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