xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_arp.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/errno.h>
28 #include <setjmp.h>
29 #include <sys/socket.h>
30 #include <net/if.h>
31 #include <net/if_arp.h>
32 #include <netinet/in_systm.h>
33 #include <netinet/in.h>
34 #include <netinet/ip.h>
35 #include <netinet/if_ether.h>
36 #include <netdb.h>
37 #include <net/if_types.h>
38 
39 #include "snoop.h"
40 
41 extern char *dlc_header;
42 extern jmp_buf xdr_err;
43 
44 static char *printip(unsigned char *);
45 static char *addrtoname_align(unsigned char *);
46 
47 static char unarp_addr[] = "Unknown";
48 char *opname[] = {
49 	"",
50 	"ARP Request",
51 	"ARP Reply",
52 	"REVARP Request",
53 	"REVARP Reply",
54 };
55 
56 void
interpret_arp(int flags,struct arphdr * ap,int alen)57 interpret_arp(int flags, struct arphdr *ap, int alen)
58 {
59 	char *line;
60 	extern char *src_name, *dst_name;
61 	unsigned char *sip, *tip, *sha, *tha;
62 	char *smacbuf = NULL, *dmacbuf = NULL;
63 	int maclen;
64 	ushort_t arpop;
65 	boolean_t is_ip = B_FALSE;
66 
67 	/*
68 	 * Check that at least the generic ARP header was received.
69 	 */
70 	if (sizeof (struct arphdr) > alen)
71 		goto short_packet;
72 
73 	arpop = ntohs(ap->ar_op);
74 	maclen = ap->ar_hln;
75 	if (ntohs(ap->ar_pro) == ETHERTYPE_IP)
76 		is_ip = B_TRUE;
77 
78 	sha = (unsigned char *)(ap + 1);
79 	sip = sha + maclen;
80 	tha = sip + ap->ar_pln;
81 	tip = tha + maclen;
82 
83 	/*
84 	 * Check that the protocol/hardware addresses were received.
85 	 */
86 	if ((tip + ap->ar_pln) > ((unsigned char *)ap + alen))
87 		goto short_packet;
88 
89 	if (maclen == 0) {
90 		smacbuf = dmacbuf = unarp_addr;
91 	} else {
92 		if (((flags & F_DTAIL) && is_ip) || (arpop == ARPOP_REPLY)) {
93 			smacbuf = _link_ntoa(sha, NULL, maclen, IFT_OTHER);
94 			if (smacbuf == NULL)
95 				pr_err("Warning: malloc failure");
96 		}
97 
98 		if (((flags & F_DTAIL) && is_ip) || (arpop ==
99 		    REVARP_REQUEST) || (arpop == REVARP_REPLY)) {
100 			dmacbuf = _link_ntoa(tha, NULL, maclen, IFT_OTHER);
101 			if (dmacbuf == NULL)
102 				pr_err("Warning: malloc failure");
103 		}
104 	}
105 
106 	src_name = addrtoname_align(sip);
107 
108 	if (flags & F_SUM) {
109 
110 		line = get_sum_line();
111 
112 		switch (arpop) {
113 		case ARPOP_REQUEST:
114 			(void) snprintf(line, MAXLINE, "ARP C Who is %s ?",
115 			    printip(tip));
116 			break;
117 		case ARPOP_REPLY:
118 			(void) snprintf(line, MAXLINE, "ARP R %s is %s",
119 			    printip(sip), smacbuf);
120 			dst_name = addrtoname_align(tip);
121 			break;
122 		case REVARP_REQUEST:
123 			(void) snprintf(line, MAXLINE, "RARP C Who is %s ?",
124 			    dmacbuf);
125 			break;
126 		case REVARP_REPLY:
127 			(void) snprintf(line, MAXLINE, "RARP R %s is %s",
128 			    dmacbuf, printip(tip));
129 			dst_name = addrtoname_align(tip);
130 			break;
131 		}
132 	}
133 
134 	if (flags & F_DTAIL) {
135 		show_header("ARP:  ", "ARP/RARP Frame", alen);
136 		show_space();
137 		(void) snprintf(get_line(0, 0), get_line_remain(),
138 		    "Hardware type = %d (%s)", ntohs(ap->ar_hrd),
139 		    arp_htype(ntohs(ap->ar_hrd)));
140 		(void) snprintf(get_line(0, 0), get_line_remain(),
141 		    "Protocol type = %04x (%s)", ntohs(ap->ar_pro),
142 		    print_ethertype(ntohs(ap->ar_pro)));
143 		(void) snprintf(get_line(0, 0), get_line_remain(),
144 		    "Length of hardware address = %d bytes", ap->ar_hln);
145 		(void) snprintf(get_line(0, 0), get_line_remain(),
146 		    "Length of protocol address = %d bytes", ap->ar_pln);
147 		(void) snprintf(get_line(0, 0), get_line_remain(),
148 		    "Opcode %d (%s)", arpop,
149 		    (arpop > REVARP_REPLY) ? opname[0] : opname[arpop]);
150 
151 		if (is_ip) {
152 			(void) snprintf(get_line(0, 0), get_line_remain(),
153 			    "Sender's hardware address = %s", smacbuf);
154 			(void) snprintf(get_line(0, 0), get_line_remain(),
155 			    "Sender's protocol address = %s",
156 			    printip(sip));
157 			(void) snprintf(get_line(0, 0), get_line_remain(),
158 			    "Target hardware address = %s",
159 			    arpop == ARPOP_REQUEST ? "?" : dmacbuf);
160 			(void) snprintf(get_line(0, 0), get_line_remain(),
161 			    "Target protocol address = %s",
162 			    arpop == REVARP_REQUEST ? "?" :
163 			    printip(tip));
164 		}
165 		show_trailer();
166 	}
167 
168 	if (maclen != 0) {
169 		free(smacbuf);
170 		free(dmacbuf);
171 	}
172 	return;
173 
174 short_packet:
175 	if (flags & F_SUM) {
176 		(void) snprintf(get_sum_line(), MAXLINE,
177 		    "ARP (short packet)");
178 	} else if (flags & F_DTAIL) {
179 		show_header("ARP:  ", "ARP/RARP Frame", alen);
180 		show_space();
181 		(void) snprintf(get_line(0, 0), get_line_remain(),
182 		    "ARP (short packet)");
183 	}
184 }
185 
186 char *
printip(unsigned char * p)187 printip(unsigned char *p)
188 {
189 	static char buff[MAXHOSTNAMELEN + 32];
190 	char *ap, *np;
191 	struct in_addr a;
192 
193 	memcpy(&a, p, 4);
194 	ap = (char *)inet_ntoa(a);
195 	np = (char *)addrtoname(AF_INET, &a);
196 	(void) snprintf(buff, MAXHOSTNAMELEN, "%s, %s", ap, np);
197 	return (buff);
198 }
199 
200 char *
addrtoname_align(unsigned char * p)201 addrtoname_align(unsigned char *p)
202 {
203 	struct in_addr a;
204 
205 	memcpy(&a, p, 4);
206 	return ((char *)addrtoname(AF_INET, &a));
207 }
208 
209 /*
210  * These numbers are assigned by the IANA.  See the arp-parameters registry.
211  * Only those values that are used within Solaris have #defines.
212  */
213 const char *
arp_htype(int t)214 arp_htype(int t)
215 {
216 	switch (t) {
217 	case ARPHRD_ETHER:
218 		return ("Ethernet (10Mb)");
219 	case 2:
220 		return ("Experimental Ethernet (3MB)");
221 	case 3:
222 		return ("Amateur Radio AX.25");
223 	case 4:
224 		return ("Proteon ProNET Token Ring");
225 	case 5:
226 		return ("Chaos");
227 	case ARPHRD_IEEE802:
228 		return ("IEEE 802");
229 	case 7:
230 		return ("ARCNET");
231 	case 8:
232 		return ("Hyperchannel");
233 	case 9:
234 		return ("Lanstar");
235 	case 10:
236 		return ("Autonet");
237 	case 11:
238 		return ("LocalTalk");
239 	case 12:
240 		return ("LocalNet");
241 	case 13:
242 		return ("Ultra Link");
243 	case 14:
244 		return ("SMDS");
245 	case ARPHRD_FRAME:
246 		return ("Frame Relay");
247 	case ARPHRD_ATM:
248 		return ("ATM");
249 	case ARPHRD_HDLC:
250 		return ("HDLC");
251 	case ARPHRD_FC:
252 		return ("Fibre Channel");
253 	case ARPHRD_IPATM:
254 		return ("IP-ATM");
255 	case ARPHRD_TUNNEL:
256 		return ("Tunnel");
257 	case ARPHRD_IB:
258 		return ("IPIB");
259 	};
260 	return ("UNKNOWN");
261 }
262