summaryrefslogtreecommitdiff
path: root/usr.sbin/tcpdump/print-nhrp.c
blob: f8b8bd620e4ca4ef747a671cf4ab3ffe6f6d5467 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*	$OpenBSD: print-nhrp.c,v 1.1 2020/04/15 20:19:25 remi Exp $ */

/*
 * Copyright (c) 2020 Remi Locherer <remi@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * RFC 2332 NBMA Next Hop Resolution Protocol (NHRP)
 */

#include <sys/time.h>
#include <sys/uio.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>

#include <net/ethertypes.h>

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "addrtoname.h"
#include "afnum.h"
#include "interface.h"
#include "extract.h"

#define NHRP_VER_RFC2332		1

#define NHRP_PKG_RESOLUTION_REQUEST	1
#define NHRP_PKG_RESOLUTION_REPLY	2
#define NHRP_PKG_REGISTRATION_REQUEST	3
#define NHRP_PKG_REGISTRATION_REPLY	4
#define NHRP_PKG_PURGE_REQUEST		5
#define NHRP_PKG_PURGE_REPLY		6
#define NHRP_PKG_ERROR_INDICATION	7


struct nhrp_header {
	/* fixed header part */
	u_int16_t	afn;		/* link layer address */
	u_int16_t	pro_type;	/* protocol type (short form) */
	u_int8_t	pro_snap[5];	/* protocol type (long form) */
	u_int8_t	hopcnt;		/* hop count */
	u_int16_t	pktsz;		/* length of the NHRP packet (octets) */
	u_int16_t	chksum;		/* IP checksum over the entier packet */
	u_int16_t	extoff;		/* extension offset */
	u_int8_t	op_version;	/* version of address mapping and
					   management protocol */
	u_int8_t	op_type;	/* NHRP packet type */
	u_int8_t	shtl;		/* type and length of src NBMA addr */
	u_int8_t	sstl;		/* type and length of src NBMA
					   subaddress */
	/* mandatory header part */
	u_int8_t	spl;		/* src proto len */
	u_int8_t	dpl;		/* dst proto len */
	u_int16_t	flags;		/* flags */
        union {
		u_int32_t	id;	/* request id */
		struct {		/* error code */
			u_int16_t	code;
			u_int16_t	offset;
		} err;
	} u;
};

struct nhrp_cie {
	/* client information entrie */
	u_int8_t	code;
	u_int8_t	plen;
	u_int16_t	unused;
	u_int16_t	mtu;
	u_int16_t	htime;
	u_int8_t	cli_addr_tl;
	u_int8_t	cli_saddr_tl;
	u_int8_t	cli_proto_tl;
	u_int8_t	pref;
};

static const u_char *	nhrp_print_cie(const u_char *, u_int16_t, u_int16_t);


void
nhrp_print(const u_char *p, u_int length)
{
	struct nhrp_header	*hdr;
	const u_char		*nhrpext, *nhrpend;

	printf("NHRP: ");

	if ((snapend - p) < sizeof(*hdr))
		goto trunc;

	hdr = (struct nhrp_header *)p;

	if (hdr->op_version != NHRP_VER_RFC2332) {
		printf("unknown-version-%02x", hdr->op_version);
		return;

	}

	nhrpext = p + EXTRACT_16BITS(&hdr->extoff);
	nhrpend = p + EXTRACT_16BITS(&hdr->pktsz);

	switch (hdr->op_type) {
	case NHRP_PKG_RESOLUTION_REQUEST:
		printf("res request, ");
		break;
	case NHRP_PKG_RESOLUTION_REPLY:
		printf("res reply, ");
		break;
	case NHRP_PKG_REGISTRATION_REQUEST:
		printf("reg request, ");
		break;
	case NHRP_PKG_REGISTRATION_REPLY:
		printf("reg reply, ");
		break;
	case NHRP_PKG_PURGE_REQUEST:
		printf("purge request, ");
		break;
	case NHRP_PKG_PURGE_REPLY:
		printf("purge reply, ");
		break;
	case NHRP_PKG_ERROR_INDICATION:
		printf("error %u", hdr->u.err.code);
		return;
	default:
		printf("unknown-op-type-%04x, ", hdr->op_type);
		break;
	}

	printf("id %u", EXTRACT_32BITS(&hdr->u.id));

	if (vflag) {
		printf(", hopcnt %u", hdr->hopcnt);

		/* most significat bit must be 0 */
		if (hdr->shtl & 0x80)
			printf(" (shtl bit 7 set)");

		/* check 2nd most significant bit */
		if (hdr->shtl & 0x40)
			printf(" (nbma E.154)");
	}

	p += sizeof(*hdr);
	if ((snapend - p) < hdr->shtl)
		goto trunc;

	if (hdr->shtl) {
		switch (EXTRACT_16BITS(&hdr->afn)) {
		case AFNUM_INET:
			printf(", src nbma %s", ipaddr_string(p));
			break;
		case AFNUM_INET6:
			printf(", src nbma %s", ip6addr_string(p));
			break;
		case AFNUM_802:
			printf(", src nbma %s", etheraddr_string(p));
			break;
		default:
			printf(", unknown-nbma-addr-family-%04x",
			    EXTRACT_16BITS(&hdr->afn));
			break;
		}
	}

	p += hdr->shtl;
	if ((snapend - p) < (hdr->spl + hdr->dpl))
		goto trunc;

	switch (EXTRACT_16BITS(&hdr->pro_type)) {
	case ETHERTYPE_IP:
		printf(", %s -> %s",
		    ipaddr_string(p),
		    ipaddr_string(p + hdr->spl));
		break;
	case ETHERTYPE_IPV6:
		printf(", %s -> %s",
		    ip6addr_string(p),
		    ip6addr_string(p + hdr->spl));
		break;
	default:
		printf(", proto type %04x",
		    EXTRACT_16BITS(&hdr->pro_type));
		break;
	}

	p += hdr->spl + hdr->dpl;

	do {
		p = nhrp_print_cie(p, hdr->afn, hdr->pro_type);
		if (p == 0)
			goto trunc;
	} while ((hdr->extoff && (p < nhrpext)) ||
		    ((!hdr->extoff && (p < nhrpend))));

	return;

trunc:
	printf(" [|nhrp]");

}

static const u_char *
nhrp_print_cie(const u_char *data, u_int16_t afn, u_int16_t pro_type)
{
	struct nhrp_cie		*cie;
	int			 family, type;

	if ((snapend - data) < sizeof(*cie))
		return (0);

	cie = (struct nhrp_cie *)data;

	family = EXTRACT_16BITS(&afn);
	type = EXTRACT_16BITS(&pro_type);

	printf(" (code %d", cie->code);
	if (vflag)
		printf(", pl %d, mtu %d, htime %d, pref %d", cie->plen,
		    EXTRACT_16BITS(&cie->mtu), EXTRACT_16BITS(&cie->htime),
		    cie->pref);

	/* check 2nd most significant bit */
	if (cie->cli_addr_tl & 0x40)
		printf(", nbma E.154");

	data += sizeof(*cie);
	if ((snapend - data) < cie->cli_addr_tl)
		return (0);

	if (cie->cli_addr_tl) {
		switch (family) {
		case AFNUM_INET:
			printf(", nbma %s", ipaddr_string(data));
			break;
		case AFNUM_INET6:
			printf(", nbma %s", ip6addr_string(data));
			break;
		case AFNUM_802:
			printf(", nbma %s", etheraddr_string(data));
			break;
		default:
			printf(", unknown-nbma-addr-family-%04x", family);
			break;
		}
	}
	if (cie->cli_saddr_tl)
		printf(", unknown-nbma-saddr-family");

	data += cie->cli_addr_tl + cie->cli_saddr_tl;
	if ((snapend - data) < cie->cli_proto_tl)
		return (0);

	if (cie->cli_proto_tl) {
		switch (type) {
		case ETHERTYPE_IP:
			printf(", proto %s", ipaddr_string(data));
			break;
		case ETHERTYPE_IPV6:
			printf(", proto %s", ip6addr_string(data));
			break;
		default:
			printf(", unknown-proto-family-%04x", type);
			break;
		}
	}

	printf(")");

	return (data + cie->cli_proto_tl);
}