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 #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/errno.h>
287c478bd9Sstevel@tonic-gate #include <setjmp.h>
297c478bd9Sstevel@tonic-gate #include <sys/socket.h>
307c478bd9Sstevel@tonic-gate #include <net/if.h>
317c478bd9Sstevel@tonic-gate #include <net/if_arp.h>
327c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
337c478bd9Sstevel@tonic-gate #include <netinet/in.h>
347c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
357c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
367c478bd9Sstevel@tonic-gate #include <netdb.h>
377c478bd9Sstevel@tonic-gate #include <net/if_types.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "snoop.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate extern char *dlc_header;
427c478bd9Sstevel@tonic-gate extern jmp_buf xdr_err;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static char *printip(unsigned char *);
457c478bd9Sstevel@tonic-gate static char *addrtoname_align(unsigned char *);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static char unarp_addr[] = "Unknown";
487c478bd9Sstevel@tonic-gate char *opname[] = {
497c478bd9Sstevel@tonic-gate 	"",
507c478bd9Sstevel@tonic-gate 	"ARP Request",
517c478bd9Sstevel@tonic-gate 	"ARP Reply",
527c478bd9Sstevel@tonic-gate 	"REVARP Request",
537c478bd9Sstevel@tonic-gate 	"REVARP Reply",
547c478bd9Sstevel@tonic-gate };
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate void
interpret_arp(int flags,struct arphdr * ap,int alen)577c478bd9Sstevel@tonic-gate interpret_arp(int flags, struct arphdr *ap, int alen)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	char *line;
607c478bd9Sstevel@tonic-gate 	extern char *src_name, *dst_name;
617c478bd9Sstevel@tonic-gate 	unsigned char *sip, *tip, *sha, *tha;
627c478bd9Sstevel@tonic-gate 	char *smacbuf = NULL, *dmacbuf = NULL;
637c478bd9Sstevel@tonic-gate 	int maclen;
647c478bd9Sstevel@tonic-gate 	ushort_t arpop;
657c478bd9Sstevel@tonic-gate 	boolean_t is_ip = B_FALSE;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	/*
687c478bd9Sstevel@tonic-gate 	 * Check that at least the generic ARP header was received.
697c478bd9Sstevel@tonic-gate 	 */
707c478bd9Sstevel@tonic-gate 	if (sizeof (struct arphdr) > alen)
717c478bd9Sstevel@tonic-gate 		goto short_packet;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	arpop = ntohs(ap->ar_op);
747c478bd9Sstevel@tonic-gate 	maclen = ap->ar_hln;
757c478bd9Sstevel@tonic-gate 	if (ntohs(ap->ar_pro) == ETHERTYPE_IP)
767c478bd9Sstevel@tonic-gate 		is_ip = B_TRUE;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	sha = (unsigned char *)(ap + 1);
797c478bd9Sstevel@tonic-gate 	sip = sha + maclen;
807c478bd9Sstevel@tonic-gate 	tha = sip + ap->ar_pln;
817c478bd9Sstevel@tonic-gate 	tip = tha + maclen;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	/*
847c478bd9Sstevel@tonic-gate 	 * Check that the protocol/hardware addresses were received.
857c478bd9Sstevel@tonic-gate 	 */
867c478bd9Sstevel@tonic-gate 	if ((tip + ap->ar_pln) > ((unsigned char *)ap + alen))
877c478bd9Sstevel@tonic-gate 		goto short_packet;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (maclen == 0) {
907c478bd9Sstevel@tonic-gate 		smacbuf = dmacbuf = unarp_addr;
917c478bd9Sstevel@tonic-gate 	} else {
927c478bd9Sstevel@tonic-gate 		if (((flags & F_DTAIL) && is_ip) || (arpop == ARPOP_REPLY)) {
937c478bd9Sstevel@tonic-gate 			smacbuf = _link_ntoa(sha, NULL, maclen, IFT_OTHER);
947c478bd9Sstevel@tonic-gate 			if (smacbuf == NULL)
957c478bd9Sstevel@tonic-gate 				pr_err("Warning: malloc failure");
967c478bd9Sstevel@tonic-gate 		}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 		if (((flags & F_DTAIL) && is_ip) || (arpop ==
997c478bd9Sstevel@tonic-gate 		    REVARP_REQUEST) || (arpop == REVARP_REPLY)) {
1007c478bd9Sstevel@tonic-gate 			dmacbuf = _link_ntoa(tha, NULL, maclen, IFT_OTHER);
1017c478bd9Sstevel@tonic-gate 			if (dmacbuf == NULL)
1027c478bd9Sstevel@tonic-gate 				pr_err("Warning: malloc failure");
1037c478bd9Sstevel@tonic-gate 		}
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	src_name = addrtoname_align(sip);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 		line = get_sum_line();
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 		switch (arpop) {
1137c478bd9Sstevel@tonic-gate 		case ARPOP_REQUEST:
1147c478bd9Sstevel@tonic-gate 			(void) snprintf(line, MAXLINE, "ARP C Who is %s ?",
1157c478bd9Sstevel@tonic-gate 			    printip(tip));
1167c478bd9Sstevel@tonic-gate 			break;
1177c478bd9Sstevel@tonic-gate 		case ARPOP_REPLY:
1187c478bd9Sstevel@tonic-gate 			(void) snprintf(line, MAXLINE, "ARP R %s is %s",
1197c478bd9Sstevel@tonic-gate 			    printip(sip), smacbuf);
1207c478bd9Sstevel@tonic-gate 			dst_name = addrtoname_align(tip);
1217c478bd9Sstevel@tonic-gate 			break;
1227c478bd9Sstevel@tonic-gate 		case REVARP_REQUEST:
1237c478bd9Sstevel@tonic-gate 			(void) snprintf(line, MAXLINE, "RARP C Who is %s ?",
1247c478bd9Sstevel@tonic-gate 			    dmacbuf);
1257c478bd9Sstevel@tonic-gate 			break;
1267c478bd9Sstevel@tonic-gate 		case REVARP_REPLY:
1277c478bd9Sstevel@tonic-gate 			(void) snprintf(line, MAXLINE, "RARP R %s is %s",
1287c478bd9Sstevel@tonic-gate 			    dmacbuf, printip(tip));
1297c478bd9Sstevel@tonic-gate 			dst_name = addrtoname_align(tip);
1307c478bd9Sstevel@tonic-gate 			break;
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	if (flags & F_DTAIL) {
1357c478bd9Sstevel@tonic-gate 		show_header("ARP:  ", "ARP/RARP Frame", alen);
1367c478bd9Sstevel@tonic-gate 		show_space();
1377c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
138*d04ccbb3Scarlsonj 		    "Hardware type = %d (%s)", ntohs(ap->ar_hrd),
139*d04ccbb3Scarlsonj 		    arp_htype(ntohs(ap->ar_hrd)));
1407c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
1417c478bd9Sstevel@tonic-gate 		    "Protocol type = %04x (%s)", ntohs(ap->ar_pro),
1427c478bd9Sstevel@tonic-gate 		    print_ethertype(ntohs(ap->ar_pro)));
1437c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
1447c478bd9Sstevel@tonic-gate 		    "Length of hardware address = %d bytes", ap->ar_hln);
1457c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
1467c478bd9Sstevel@tonic-gate 		    "Length of protocol address = %d bytes", ap->ar_pln);
1477c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
1487c478bd9Sstevel@tonic-gate 		    "Opcode %d (%s)", arpop,
1497c478bd9Sstevel@tonic-gate 		    (arpop > REVARP_REPLY) ? opname[0] : opname[arpop]);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 		if (is_ip) {
1527c478bd9Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
1537c478bd9Sstevel@tonic-gate 			    "Sender's hardware address = %s", smacbuf);
1547c478bd9Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
1557c478bd9Sstevel@tonic-gate 			    "Sender's protocol address = %s",
1567c478bd9Sstevel@tonic-gate 			    printip(sip));
1577c478bd9Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
1587c478bd9Sstevel@tonic-gate 			    "Target hardware address = %s",
1597c478bd9Sstevel@tonic-gate 			    arpop == ARPOP_REQUEST ? "?" : dmacbuf);
1607c478bd9Sstevel@tonic-gate 			(void) snprintf(get_line(0, 0), get_line_remain(),
1617c478bd9Sstevel@tonic-gate 			    "Target protocol address = %s",
1627c478bd9Sstevel@tonic-gate 			    arpop == REVARP_REQUEST ? "?" :
1637c478bd9Sstevel@tonic-gate 			    printip(tip));
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 		show_trailer();
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if (maclen != 0) {
1697c478bd9Sstevel@tonic-gate 		free(smacbuf);
1707c478bd9Sstevel@tonic-gate 		free(dmacbuf);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 	return;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate short_packet:
1757c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
1767c478bd9Sstevel@tonic-gate 		(void) snprintf(get_sum_line(), MAXLINE,
1777c478bd9Sstevel@tonic-gate 		    "ARP (short packet)");
1787c478bd9Sstevel@tonic-gate 	} else if (flags & F_DTAIL) {
1797c478bd9Sstevel@tonic-gate 		show_header("ARP:  ", "ARP/RARP Frame", alen);
1807c478bd9Sstevel@tonic-gate 		show_space();
1817c478bd9Sstevel@tonic-gate 		(void) snprintf(get_line(0, 0), get_line_remain(),
1827c478bd9Sstevel@tonic-gate 		    "ARP (short packet)");
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate char *
printip(unsigned char * p)1877c478bd9Sstevel@tonic-gate printip(unsigned char *p)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	static char buff[MAXHOSTNAMELEN + 32];
1907c478bd9Sstevel@tonic-gate 	char *ap, *np;
1917c478bd9Sstevel@tonic-gate 	struct in_addr a;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	memcpy(&a, p, 4);
1947c478bd9Sstevel@tonic-gate 	ap = (char *)inet_ntoa(a);
1957c478bd9Sstevel@tonic-gate 	np = (char *)addrtoname(AF_INET, &a);
1967c478bd9Sstevel@tonic-gate 	(void) snprintf(buff, MAXHOSTNAMELEN, "%s, %s", ap, np);
1977c478bd9Sstevel@tonic-gate 	return (buff);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate char *
addrtoname_align(unsigned char * p)2017c478bd9Sstevel@tonic-gate addrtoname_align(unsigned char *p)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	struct in_addr a;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	memcpy(&a, p, 4);
2067c478bd9Sstevel@tonic-gate 	return ((char *)addrtoname(AF_INET, &a));
2077c478bd9Sstevel@tonic-gate }
208*d04ccbb3Scarlsonj 
209*d04ccbb3Scarlsonj /*
210*d04ccbb3Scarlsonj  * These numbers are assigned by the IANA.  See the arp-parameters registry.
211*d04ccbb3Scarlsonj  * Only those values that are used within Solaris have #defines.
212*d04ccbb3Scarlsonj  */
213*d04ccbb3Scarlsonj const char *
arp_htype(int t)214*d04ccbb3Scarlsonj arp_htype(int t)
215*d04ccbb3Scarlsonj {
216*d04ccbb3Scarlsonj 	switch (t) {
217*d04ccbb3Scarlsonj 	case ARPHRD_ETHER:
218*d04ccbb3Scarlsonj 		return ("Ethernet (10Mb)");
219*d04ccbb3Scarlsonj 	case 2:
220*d04ccbb3Scarlsonj 		return ("Experimental Ethernet (3MB)");
221*d04ccbb3Scarlsonj 	case 3:
222*d04ccbb3Scarlsonj 		return ("Amateur Radio AX.25");
223*d04ccbb3Scarlsonj 	case 4:
224*d04ccbb3Scarlsonj 		return ("Proteon ProNET Token Ring");
225*d04ccbb3Scarlsonj 	case 5:
226*d04ccbb3Scarlsonj 		return ("Chaos");
227*d04ccbb3Scarlsonj 	case ARPHRD_IEEE802:
228*d04ccbb3Scarlsonj 		return ("IEEE 802");
229*d04ccbb3Scarlsonj 	case 7:
230*d04ccbb3Scarlsonj 		return ("ARCNET");
231*d04ccbb3Scarlsonj 	case 8:
232*d04ccbb3Scarlsonj 		return ("Hyperchannel");
233*d04ccbb3Scarlsonj 	case 9:
234*d04ccbb3Scarlsonj 		return ("Lanstar");
235*d04ccbb3Scarlsonj 	case 10:
236*d04ccbb3Scarlsonj 		return ("Autonet");
237*d04ccbb3Scarlsonj 	case 11:
238*d04ccbb3Scarlsonj 		return ("LocalTalk");
239*d04ccbb3Scarlsonj 	case 12:
240*d04ccbb3Scarlsonj 		return ("LocalNet");
241*d04ccbb3Scarlsonj 	case 13:
242*d04ccbb3Scarlsonj 		return ("Ultra Link");
243*d04ccbb3Scarlsonj 	case 14:
244*d04ccbb3Scarlsonj 		return ("SMDS");
245*d04ccbb3Scarlsonj 	case ARPHRD_FRAME:
246*d04ccbb3Scarlsonj 		return ("Frame Relay");
247*d04ccbb3Scarlsonj 	case ARPHRD_ATM:
248*d04ccbb3Scarlsonj 		return ("ATM");
249*d04ccbb3Scarlsonj 	case ARPHRD_HDLC:
250*d04ccbb3Scarlsonj 		return ("HDLC");
251*d04ccbb3Scarlsonj 	case ARPHRD_FC:
252*d04ccbb3Scarlsonj 		return ("Fibre Channel");
253*d04ccbb3Scarlsonj 	case ARPHRD_IPATM:
254*d04ccbb3Scarlsonj 		return ("IP-ATM");
255*d04ccbb3Scarlsonj 	case ARPHRD_TUNNEL:
256*d04ccbb3Scarlsonj 		return ("Tunnel");
257*d04ccbb3Scarlsonj 	case ARPHRD_IB:
258*d04ccbb3Scarlsonj 		return ("IPIB");
259*d04ccbb3Scarlsonj 	};
260*d04ccbb3Scarlsonj 	return ("UNKNOWN");
261*d04ccbb3Scarlsonj }
262