summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_arp.c
blob: c41507c20e8f7d1b4eefe4cd2bdea377e08dd59b (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/types.h>
#include <sys/errno.h>
#include <setjmp.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#include <netdb.h>
#include <net/if_types.h>

#include "snoop.h"

extern char *dlc_header;
extern jmp_buf xdr_err;

static char *printip(unsigned char *);
static char *addrtoname_align(unsigned char *);

static char unarp_addr[] = "Unknown";
char *opname[] = {
	"",
	"ARP Request",
	"ARP Reply",
	"REVARP Request",
	"REVARP Reply",
};

void
interpret_arp(int flags, struct arphdr *ap, int alen)
{
	char *line;
	extern char *src_name, *dst_name;
	unsigned char *sip, *tip, *sha, *tha;
	char *smacbuf = NULL, *dmacbuf = NULL;
	int maclen;
	ushort_t arpop;
	boolean_t is_ip = B_FALSE;

	/*
	 * Check that at least the generic ARP header was received.
	 */
	if (sizeof (struct arphdr) > alen)
		goto short_packet;

	arpop = ntohs(ap->ar_op);
	maclen = ap->ar_hln;
	if (ntohs(ap->ar_pro) == ETHERTYPE_IP)
		is_ip = B_TRUE;

	sha = (unsigned char *)(ap + 1);
	sip = sha + maclen;
	tha = sip + ap->ar_pln;
	tip = tha + maclen;

	/*
	 * Check that the protocol/hardware addresses were received.
	 */
	if ((tip + ap->ar_pln) > ((unsigned char *)ap + alen))
		goto short_packet;

	if (maclen == 0) {
		smacbuf = dmacbuf = unarp_addr;
	} else {
		if (((flags & F_DTAIL) && is_ip) || (arpop == ARPOP_REPLY)) {
			smacbuf = _link_ntoa(sha, NULL, maclen, IFT_OTHER);
			if (smacbuf == NULL)
				pr_err("Warning: malloc failure");
		}

		if (((flags & F_DTAIL) && is_ip) || (arpop ==
		    REVARP_REQUEST) || (arpop == REVARP_REPLY)) {
			dmacbuf = _link_ntoa(tha, NULL, maclen, IFT_OTHER);
			if (dmacbuf == NULL)
				pr_err("Warning: malloc failure");
		}
	}

	src_name = addrtoname_align(sip);

	if (flags & F_SUM) {

		line = get_sum_line();

		switch (arpop) {
		case ARPOP_REQUEST:
			(void) snprintf(line, MAXLINE, "ARP C Who is %s ?",
			    printip(tip));
			break;
		case ARPOP_REPLY:
			(void) snprintf(line, MAXLINE, "ARP R %s is %s",
			    printip(sip), smacbuf);
			dst_name = addrtoname_align(tip);
			break;
		case REVARP_REQUEST:
			(void) snprintf(line, MAXLINE, "RARP C Who is %s ?",
			    dmacbuf);
			break;
		case REVARP_REPLY:
			(void) snprintf(line, MAXLINE, "RARP R %s is %s",
			    dmacbuf, printip(tip));
			dst_name = addrtoname_align(tip);
			break;
		}
	}

	if (flags & F_DTAIL) {
		show_header("ARP:  ", "ARP/RARP Frame", alen);
		show_space();
		(void) snprintf(get_line(0, 0), get_line_remain(),
		    "Hardware type = %d (%s)", ntohs(ap->ar_hrd),
		    arp_htype(ntohs(ap->ar_hrd)));
		(void) snprintf(get_line(0, 0), get_line_remain(),
		    "Protocol type = %04x (%s)", ntohs(ap->ar_pro),
		    print_ethertype(ntohs(ap->ar_pro)));
		(void) snprintf(get_line(0, 0), get_line_remain(),
		    "Length of hardware address = %d bytes", ap->ar_hln);
		(void) snprintf(get_line(0, 0), get_line_remain(),
		    "Length of protocol address = %d bytes", ap->ar_pln);
		(void) snprintf(get_line(0, 0), get_line_remain(),
		    "Opcode %d (%s)", arpop,
		    (arpop > REVARP_REPLY) ? opname[0] : opname[arpop]);

		if (is_ip) {
			(void) snprintf(get_line(0, 0), get_line_remain(),
			    "Sender's hardware address = %s", smacbuf);
			(void) snprintf(get_line(0, 0), get_line_remain(),
			    "Sender's protocol address = %s",
			    printip(sip));
			(void) snprintf(get_line(0, 0), get_line_remain(),
			    "Target hardware address = %s",
			    arpop == ARPOP_REQUEST ? "?" : dmacbuf);
			(void) snprintf(get_line(0, 0), get_line_remain(),
			    "Target protocol address = %s",
			    arpop == REVARP_REQUEST ? "?" :
			    printip(tip));
		}
		show_trailer();
	}

	if (maclen != 0) {
		free(smacbuf);
		free(dmacbuf);
	}
	return;

short_packet:
	if (flags & F_SUM) {
		(void) snprintf(get_sum_line(), MAXLINE,
		    "ARP (short packet)");
	} else if (flags & F_DTAIL) {
		show_header("ARP:  ", "ARP/RARP Frame", alen);
		show_space();
		(void) snprintf(get_line(0, 0), get_line_remain(),
		    "ARP (short packet)");
	}
}

char *
printip(unsigned char *p)
{
	static char buff[MAXHOSTNAMELEN + 32];
	char *ap, *np;
	struct in_addr a;

	memcpy(&a, p, 4);
	ap = (char *)inet_ntoa(a);
	np = (char *)addrtoname(AF_INET, &a);
	(void) snprintf(buff, MAXHOSTNAMELEN, "%s, %s", ap, np);
	return (buff);
}

char *
addrtoname_align(unsigned char *p)
{
	struct in_addr a;

	memcpy(&a, p, 4);
	return ((char *)addrtoname(AF_INET, &a));
}

/*
 * These numbers are assigned by the IANA.  See the arp-parameters registry.
 * Only those values that are used within Solaris have #defines.
 */
const char *
arp_htype(int t)
{
	switch (t) {
	case ARPHRD_ETHER:
		return ("Ethernet (10Mb)");
	case 2:
		return ("Experimental Ethernet (3MB)");
	case 3:
		return ("Amateur Radio AX.25");
	case 4:
		return ("Proteon ProNET Token Ring");
	case 5:
		return ("Chaos");
	case ARPHRD_IEEE802:
		return ("IEEE 802");
	case 7:
		return ("ARCNET");
	case 8:
		return ("Hyperchannel");
	case 9:
		return ("Lanstar");
	case 10:
		return ("Autonet");
	case 11:
		return ("LocalTalk");
	case 12:
		return ("LocalNet");
	case 13:
		return ("Ultra Link");
	case 14:
		return ("SMDS");
	case ARPHRD_FRAME:
		return ("Frame Relay");
	case ARPHRD_ATM:
		return ("ATM");
	case ARPHRD_HDLC:
		return ("HDLC");
	case ARPHRD_FC:
		return ("Fibre Channel");
	case ARPHRD_IPATM:
		return ("IP-ATM");
	case ARPHRD_TUNNEL:
		return ("Tunnel");
	case ARPHRD_IB:
		return ("IPIB");
	};
	return ("UNKNOWN");
}