xref: /titanic_52/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_arp.c (revision d04ccbb3f3163ae5962a8b7465d9796bff6ca434)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*d04ccbb3Scarlsonj  * Common Development and Distribution License (the "License").
6*d04ccbb3Scarlsonj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*d04ccbb3Scarlsonj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/errno.h>
307c478bd9Sstevel@tonic-gate #include <setjmp.h>
317c478bd9Sstevel@tonic-gate #include <sys/socket.h>
327c478bd9Sstevel@tonic-gate #include <net/if.h>
337c478bd9Sstevel@tonic-gate #include <net/if_arp.h>
347c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
357c478bd9Sstevel@tonic-gate #include <netinet/in.h>
367c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
377c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
387c478bd9Sstevel@tonic-gate #include <netdb.h>
397c478bd9Sstevel@tonic-gate #include <net/if_types.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include "snoop.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate extern char *dlc_header;
447c478bd9Sstevel@tonic-gate extern jmp_buf xdr_err;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static char *printip(unsigned char *);
477c478bd9Sstevel@tonic-gate static char *addrtoname_align(unsigned char *);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static char unarp_addr[] = "Unknown";
507c478bd9Sstevel@tonic-gate char *opname[] = {
517c478bd9Sstevel@tonic-gate 	"",
527c478bd9Sstevel@tonic-gate 	"ARP Request",
537c478bd9Sstevel@tonic-gate 	"ARP Reply",
547c478bd9Sstevel@tonic-gate 	"REVARP Request",
557c478bd9Sstevel@tonic-gate 	"REVARP Reply",
567c478bd9Sstevel@tonic-gate };
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate void
597c478bd9Sstevel@tonic-gate interpret_arp(int flags, struct arphdr *ap, int alen)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	char *line;
627c478bd9Sstevel@tonic-gate 	extern char *src_name, *dst_name;
637c478bd9Sstevel@tonic-gate 	unsigned char *sip, *tip, *sha, *tha;
647c478bd9Sstevel@tonic-gate 	char *smacbuf = NULL, *dmacbuf = NULL;
657c478bd9Sstevel@tonic-gate 	int maclen;
667c478bd9Sstevel@tonic-gate 	ushort_t arpop;
677c478bd9Sstevel@tonic-gate 	boolean_t is_ip = B_FALSE;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	/*
707c478bd9Sstevel@tonic-gate 	 * Check that at least the generic ARP header was received.
717c478bd9Sstevel@tonic-gate 	 */
727c478bd9Sstevel@tonic-gate 	if (sizeof (struct arphdr) > alen)
737c478bd9Sstevel@tonic-gate 		goto short_packet;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	arpop = ntohs(ap->ar_op);
767c478bd9Sstevel@tonic-gate 	maclen = ap->ar_hln;
777c478bd9Sstevel@tonic-gate 	if (ntohs(ap->ar_pro) == ETHERTYPE_IP)
787c478bd9Sstevel@tonic-gate 		is_ip = B_TRUE;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	sha = (unsigned char *)(ap + 1);
817c478bd9Sstevel@tonic-gate 	sip = sha + maclen;
827c478bd9Sstevel@tonic-gate 	tha = sip + ap->ar_pln;
837c478bd9Sstevel@tonic-gate 	tip = tha + maclen;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	/*
867c478bd9Sstevel@tonic-gate 	 * Check that the protocol/hardware addresses were received.
877c478bd9Sstevel@tonic-gate 	 */
887c478bd9Sstevel@tonic-gate 	if ((tip + ap->ar_pln) > ((unsigned char *)ap + alen))
897c478bd9Sstevel@tonic-gate 		goto short_packet;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if (maclen == 0) {
927c478bd9Sstevel@tonic-gate 		smacbuf = dmacbuf = unarp_addr;
937c478bd9Sstevel@tonic-gate 	} else {
947c478bd9Sstevel@tonic-gate 		if (((flags & F_DTAIL) && is_ip) || (arpop == ARPOP_REPLY)) {
957c478bd9Sstevel@tonic-gate 			smacbuf = _link_ntoa(sha, NULL, maclen, IFT_OTHER);
967c478bd9Sstevel@tonic-gate 			if (smacbuf == NULL)
977c478bd9Sstevel@tonic-gate 				pr_err("Warning: malloc failure");
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 		if (((flags & F_DTAIL) && is_ip) || (arpop ==
1017c478bd9Sstevel@tonic-gate 		    REVARP_REQUEST) || (arpop == REVARP_REPLY)) {
1027c478bd9Sstevel@tonic-gate 			dmacbuf = _link_ntoa(tha, NULL, maclen, IFT_OTHER);
1037c478bd9Sstevel@tonic-gate 			if (dmacbuf == NULL)
1047c478bd9Sstevel@tonic-gate 				pr_err("Warning: malloc failure");
1057c478bd9Sstevel@tonic-gate 		}
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	src_name = addrtoname_align(sip);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 		line = get_sum_line();
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 		switch (arpop) {
1157c478bd9Sstevel@tonic-gate 		case ARPOP_REQUEST:
1167c478bd9Sstevel@tonic-gate 			(void) snprintf(line, MAXLINE, "ARP C Who is %s ?",
1177c478bd9Sstevel@tonic-gate 			    printip(tip));
1187c478bd9Sstevel@tonic-gate 			break;
1197c478bd9Sstevel@tonic-gate 		case ARPOP_REPLY:
1207c478bd9Sstevel@tonic-gate 			(void) snprintf(line, MAXLINE, "ARP R %s is %s",
1217c478bd9Sstevel@tonic-gate 			    printip(sip), smacbuf);
1227c478bd9Sstevel@tonic-gate 			dst_name = addrtoname_align(tip);
1237c478bd9Sstevel@tonic-gate 			break;
1247c478bd9Sstevel@tonic-gate 		case REVARP_REQUEST:
1257c478bd9Sstevel@tonic-gate 			(void) snprintf(line, MAXLINE, "RARP C Who is %s ?",
1267c478bd9Sstevel@tonic-gate 			    dmacbuf);
1277c478bd9Sstevel@tonic-gate 			break;
1287c478bd9Sstevel@tonic-gate 		case REVARP_REPLY:
1297c478bd9Sstevel@tonic-gate 			(void) snprintf(line, MAXLINE, "RARP R %s is %s",
1307c478bd9Sstevel@tonic-gate 			    dmacbuf, printip(tip));
1317c478bd9Sstevel@tonic-gate 			dst_name = addrtoname_align(tip);
1327c478bd9Sstevel@tonic-gate 			break;
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	if (flags & F_DTAIL) {
1377c478bd9Sstevel@tonic-gate 		show_header("ARP:  ", "ARP/RARP Frame", alen);
1387c478bd9Sstevel@tonic-gate 		show_space();
1397c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
140*d04ccbb3Scarlsonj 		    "Hardware type = %d (%s)", ntohs(ap->ar_hrd),
141*d04ccbb3Scarlsonj 		    arp_htype(ntohs(ap->ar_hrd)));
1427c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
1437c478bd9Sstevel@tonic-gate 		    "Protocol type = %04x (%s)", ntohs(ap->ar_pro),
1447c478bd9Sstevel@tonic-gate 		    print_ethertype(ntohs(ap->ar_pro)));
1457c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
1467c478bd9Sstevel@tonic-gate 		    "Length of hardware address = %d bytes", ap->ar_hln);
1477c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
1487c478bd9Sstevel@tonic-gate 		    "Length of protocol address = %d bytes", ap->ar_pln);
1497c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
1507c478bd9Sstevel@tonic-gate 		    "Opcode %d (%s)", arpop,
1517c478bd9Sstevel@tonic-gate 		    (arpop > REVARP_REPLY) ? opname[0] : opname[arpop]);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		if (is_ip) {
1547c478bd9Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
1557c478bd9Sstevel@tonic-gate 			    "Sender's hardware address = %s", smacbuf);
1567c478bd9Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
1577c478bd9Sstevel@tonic-gate 			    "Sender's protocol address = %s",
1587c478bd9Sstevel@tonic-gate 			    printip(sip));
1597c478bd9Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
1607c478bd9Sstevel@tonic-gate 			    "Target hardware address = %s",
1617c478bd9Sstevel@tonic-gate 			    arpop == ARPOP_REQUEST ? "?" : dmacbuf);
1627c478bd9Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
1637c478bd9Sstevel@tonic-gate 			    "Target protocol address = %s",
1647c478bd9Sstevel@tonic-gate 			    arpop == REVARP_REQUEST ? "?" :
1657c478bd9Sstevel@tonic-gate 			    printip(tip));
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 		show_trailer();
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (maclen != 0) {
1717c478bd9Sstevel@tonic-gate 		free(smacbuf);
1727c478bd9Sstevel@tonic-gate 		free(dmacbuf);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	return;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate short_packet:
1777c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
1787c478bd9Sstevel@tonic-gate 		(void) snprintf(get_sum_line(), MAXLINE,
1797c478bd9Sstevel@tonic-gate 		    "ARP (short packet)");
1807c478bd9Sstevel@tonic-gate 	} else if (flags & F_DTAIL) {
1817c478bd9Sstevel@tonic-gate 		show_header("ARP:  ", "ARP/RARP Frame", alen);
1827c478bd9Sstevel@tonic-gate 		show_space();
1837c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
1847c478bd9Sstevel@tonic-gate 		    "ARP (short packet)");
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate char *
1897c478bd9Sstevel@tonic-gate printip(unsigned char *p)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	static char buff[MAXHOSTNAMELEN + 32];
1927c478bd9Sstevel@tonic-gate 	char *ap, *np;
1937c478bd9Sstevel@tonic-gate 	struct in_addr a;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	memcpy(&a, p, 4);
1967c478bd9Sstevel@tonic-gate 	ap = (char *)inet_ntoa(a);
1977c478bd9Sstevel@tonic-gate 	np = (char *)addrtoname(AF_INET, &a);
1987c478bd9Sstevel@tonic-gate 	(void) snprintf(buff, MAXHOSTNAMELEN, "%s, %s", ap, np);
1997c478bd9Sstevel@tonic-gate 	return (buff);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate char *
2037c478bd9Sstevel@tonic-gate addrtoname_align(unsigned char *p)
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate 	struct in_addr a;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	memcpy(&a, p, 4);
2087c478bd9Sstevel@tonic-gate 	return ((char *)addrtoname(AF_INET, &a));
2097c478bd9Sstevel@tonic-gate }
210*d04ccbb3Scarlsonj 
211*d04ccbb3Scarlsonj /*
212*d04ccbb3Scarlsonj  * These numbers are assigned by the IANA.  See the arp-parameters registry.
213*d04ccbb3Scarlsonj  * Only those values that are used within Solaris have #defines.
214*d04ccbb3Scarlsonj  */
215*d04ccbb3Scarlsonj const char *
216*d04ccbb3Scarlsonj arp_htype(int t)
217*d04ccbb3Scarlsonj {
218*d04ccbb3Scarlsonj 	switch (t) {
219*d04ccbb3Scarlsonj 	case ARPHRD_ETHER:
220*d04ccbb3Scarlsonj 		return ("Ethernet (10Mb)");
221*d04ccbb3Scarlsonj 	case 2:
222*d04ccbb3Scarlsonj 		return ("Experimental Ethernet (3MB)");
223*d04ccbb3Scarlsonj 	case 3:
224*d04ccbb3Scarlsonj 		return ("Amateur Radio AX.25");
225*d04ccbb3Scarlsonj 	case 4:
226*d04ccbb3Scarlsonj 		return ("Proteon ProNET Token Ring");
227*d04ccbb3Scarlsonj 	case 5:
228*d04ccbb3Scarlsonj 		return ("Chaos");
229*d04ccbb3Scarlsonj 	case ARPHRD_IEEE802:
230*d04ccbb3Scarlsonj 		return ("IEEE 802");
231*d04ccbb3Scarlsonj 	case 7:
232*d04ccbb3Scarlsonj 		return ("ARCNET");
233*d04ccbb3Scarlsonj 	case 8:
234*d04ccbb3Scarlsonj 		return ("Hyperchannel");
235*d04ccbb3Scarlsonj 	case 9:
236*d04ccbb3Scarlsonj 		return ("Lanstar");
237*d04ccbb3Scarlsonj 	case 10:
238*d04ccbb3Scarlsonj 		return ("Autonet");
239*d04ccbb3Scarlsonj 	case 11:
240*d04ccbb3Scarlsonj 		return ("LocalTalk");
241*d04ccbb3Scarlsonj 	case 12:
242*d04ccbb3Scarlsonj 		return ("LocalNet");
243*d04ccbb3Scarlsonj 	case 13:
244*d04ccbb3Scarlsonj 		return ("Ultra Link");
245*d04ccbb3Scarlsonj 	case 14:
246*d04ccbb3Scarlsonj 		return ("SMDS");
247*d04ccbb3Scarlsonj 	case ARPHRD_FRAME:
248*d04ccbb3Scarlsonj 		return ("Frame Relay");
249*d04ccbb3Scarlsonj 	case ARPHRD_ATM:
250*d04ccbb3Scarlsonj 		return ("ATM");
251*d04ccbb3Scarlsonj 	case ARPHRD_HDLC:
252*d04ccbb3Scarlsonj 		return ("HDLC");
253*d04ccbb3Scarlsonj 	case ARPHRD_FC:
254*d04ccbb3Scarlsonj 		return ("Fibre Channel");
255*d04ccbb3Scarlsonj 	case ARPHRD_IPATM:
256*d04ccbb3Scarlsonj 		return ("IP-ATM");
257*d04ccbb3Scarlsonj 	case ARPHRD_TUNNEL:
258*d04ccbb3Scarlsonj 		return ("Tunnel");
259*d04ccbb3Scarlsonj 	case ARPHRD_IB:
260*d04ccbb3Scarlsonj 		return ("IPIB");
261*d04ccbb3Scarlsonj 	};
262*d04ccbb3Scarlsonj 	return ("UNKNOWN");
263*d04ccbb3Scarlsonj }
264