summaryrefslogtreecommitdiff
path: root/usr.sbin/pppoe/debug.c
blob: 5691a01ad7703f8a92fbe87cb5f2ef72c8a8ab2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*	$OpenBSD: debug.c,v 1.2 2003/06/04 04:46:13 jason Exp $	*/

/*
 * Copyright (c) 2000 Network Security Technologies, Inc. http://www.netsec.net
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/ppp_defs.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <string.h>
#include <ctype.h>

#include "pppoe.h"

void
debug_packet(u_int8_t *pkt, int len)
{
	struct ether_header eh;
	struct pppoe_header ph;

	if (option_verbose == 0)
		return;

	if (len < sizeof(eh)) {
		printf("short packet (%d)\n", len);
		return;
	}
	bcopy(pkt, &eh, sizeof(eh));
	pkt += sizeof(eh);
	len -= sizeof(eh);
	eh.ether_type = ntohs(eh.ether_type);

	if (eh.ether_type == ETHERTYPE_PPPOE && option_verbose < 2)
		return;

	printf("%s -> %s %04x[",
	    ether_ntoa((struct ether_addr *)eh.ether_shost),
	    ether_ntoa((struct ether_addr *)eh.ether_dhost),
	    eh.ether_type);
	switch (eh.ether_type) {
	case ETHERTYPE_PPPOEDISC:
		printf("discovery]");
		break;
	case ETHERTYPE_PPPOE:
		printf("session]");
		break;
	default:
		printf("unknown]\n");
		return;
	}
	
	if (len < sizeof(ph)) {
		printf("short packet (%d)\n", len);
		return;
	}
	bcopy(pkt, &ph, sizeof(ph));
	pkt += sizeof(ph);
	len -= sizeof(ph);
	ph.sessionid = ntohs(ph.sessionid);
	ph.len = ntohs(ph.len);

	if (ph.len <= len)
		len = ph.len;
	else {
		printf("missing (%d)\n", ph.len - len);
		return;
	}

	printf(": version %d, type %d, ",
	    PPPOE_VER(ph.vertype), PPPOE_TYPE(ph.vertype));
	switch (ph.code) {
	case PPPOE_CODE_SESSION:
		printf("session");
	case PPPOE_CODE_PADO:
		printf("PADO");
		break;
	case PPPOE_CODE_PADI:
		printf("PADI");
		break;
	case PPPOE_CODE_PADR:
		printf("PADR");
		break;
	case PPPOE_CODE_PADS:
		printf("PADS");
		break;
	case PPPOE_CODE_PADT:
		printf("PADT");
		break;
	default:
		printf("unknown");
		break;
	}
	printf(", len %d\n", len);

	while (len > 0 && eh.ether_type == ETHERTYPE_PPPOEDISC) {
		int i;
		u_int16_t ttype, tlen;

		if (len < sizeof(ttype)) {
			printf("|\n");
			return;
		}
		bcopy(pkt, &ttype, sizeof(ttype));
		len -= sizeof(ttype);
		pkt += sizeof(ttype);
		ttype = ntohs(ttype);
		printf("\ttag ");
		switch (ttype) {
		case PPPOE_TAG_END_OF_LIST:
			printf("end-of-list");
			break;
		case PPPOE_TAG_SERVICE_NAME:
			printf("service-name");
			break;
		case PPPOE_TAG_AC_NAME:
			printf("ac-name");
			break;
		case PPPOE_TAG_HOST_UNIQ:
			printf("host-uniq");
			break;
		case PPPOE_TAG_AC_COOKIE:
			printf("ac-cookie");
			break;
		case PPPOE_TAG_VENDOR_SPEC:
			printf("vendor-spec");
			break;
		case PPPOE_TAG_RELAY_SESSION:
			printf("relay-session");
			break;
		case PPPOE_TAG_SERVICE_NAME_ERROR:
			printf("service-name-error");
			break;
		case PPPOE_TAG_AC_SYSTEM_ERROR:
			printf("ac-system-error");
			break;
		case PPPOE_TAG_GENERIC_ERROR:
			printf("generic-error");
			break;
		default:
			printf("unknown %04x", ttype);
			break;
		}

		if (len < sizeof(tlen)) {
			printf("|\n");
			return;
		}
		bcopy(pkt, &tlen, sizeof(tlen));
		len -= sizeof(tlen);
		pkt += sizeof(tlen);
		tlen = ntohs(tlen);
		printf(", len %d", tlen);

		if (len < tlen) {
			printf("|\n");
			return;
		}
		for (i = 0; i < tlen; i++) {
			if (isprint(pkt[i]))
				printf("%c", pkt[i]);
			else
				printf("\\%03o", pkt[i]);
		}
		len -= tlen;
	}
}