10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*3431Scarlsonj * Common Development and Distribution License (the "License").
6*3431Scarlsonj * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*3431Scarlsonj * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/errno.h>
300Sstevel@tonic-gate #include <setjmp.h>
310Sstevel@tonic-gate #include <sys/socket.h>
320Sstevel@tonic-gate #include <net/if.h>
330Sstevel@tonic-gate #include <net/if_arp.h>
340Sstevel@tonic-gate #include <netinet/in_systm.h>
350Sstevel@tonic-gate #include <netinet/in.h>
360Sstevel@tonic-gate #include <netinet/ip.h>
370Sstevel@tonic-gate #include <netinet/if_ether.h>
380Sstevel@tonic-gate #include <netdb.h>
390Sstevel@tonic-gate #include <net/if_types.h>
400Sstevel@tonic-gate
410Sstevel@tonic-gate #include "snoop.h"
420Sstevel@tonic-gate
430Sstevel@tonic-gate extern char *dlc_header;
440Sstevel@tonic-gate extern jmp_buf xdr_err;
450Sstevel@tonic-gate
460Sstevel@tonic-gate static char *printip(unsigned char *);
470Sstevel@tonic-gate static char *addrtoname_align(unsigned char *);
480Sstevel@tonic-gate
490Sstevel@tonic-gate static char unarp_addr[] = "Unknown";
500Sstevel@tonic-gate char *opname[] = {
510Sstevel@tonic-gate "",
520Sstevel@tonic-gate "ARP Request",
530Sstevel@tonic-gate "ARP Reply",
540Sstevel@tonic-gate "REVARP Request",
550Sstevel@tonic-gate "REVARP Reply",
560Sstevel@tonic-gate };
570Sstevel@tonic-gate
580Sstevel@tonic-gate void
interpret_arp(int flags,struct arphdr * ap,int alen)590Sstevel@tonic-gate interpret_arp(int flags, struct arphdr *ap, int alen)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate char *line;
620Sstevel@tonic-gate extern char *src_name, *dst_name;
630Sstevel@tonic-gate unsigned char *sip, *tip, *sha, *tha;
640Sstevel@tonic-gate char *smacbuf = NULL, *dmacbuf = NULL;
650Sstevel@tonic-gate int maclen;
660Sstevel@tonic-gate ushort_t arpop;
670Sstevel@tonic-gate boolean_t is_ip = B_FALSE;
680Sstevel@tonic-gate
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate * Check that at least the generic ARP header was received.
710Sstevel@tonic-gate */
720Sstevel@tonic-gate if (sizeof (struct arphdr) > alen)
730Sstevel@tonic-gate goto short_packet;
740Sstevel@tonic-gate
750Sstevel@tonic-gate arpop = ntohs(ap->ar_op);
760Sstevel@tonic-gate maclen = ap->ar_hln;
770Sstevel@tonic-gate if (ntohs(ap->ar_pro) == ETHERTYPE_IP)
780Sstevel@tonic-gate is_ip = B_TRUE;
790Sstevel@tonic-gate
800Sstevel@tonic-gate sha = (unsigned char *)(ap + 1);
810Sstevel@tonic-gate sip = sha + maclen;
820Sstevel@tonic-gate tha = sip + ap->ar_pln;
830Sstevel@tonic-gate tip = tha + maclen;
840Sstevel@tonic-gate
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate * Check that the protocol/hardware addresses were received.
870Sstevel@tonic-gate */
880Sstevel@tonic-gate if ((tip + ap->ar_pln) > ((unsigned char *)ap + alen))
890Sstevel@tonic-gate goto short_packet;
900Sstevel@tonic-gate
910Sstevel@tonic-gate if (maclen == 0) {
920Sstevel@tonic-gate smacbuf = dmacbuf = unarp_addr;
930Sstevel@tonic-gate } else {
940Sstevel@tonic-gate if (((flags & F_DTAIL) && is_ip) || (arpop == ARPOP_REPLY)) {
950Sstevel@tonic-gate smacbuf = _link_ntoa(sha, NULL, maclen, IFT_OTHER);
960Sstevel@tonic-gate if (smacbuf == NULL)
970Sstevel@tonic-gate pr_err("Warning: malloc failure");
980Sstevel@tonic-gate }
990Sstevel@tonic-gate
1000Sstevel@tonic-gate if (((flags & F_DTAIL) && is_ip) || (arpop ==
1010Sstevel@tonic-gate REVARP_REQUEST) || (arpop == REVARP_REPLY)) {
1020Sstevel@tonic-gate dmacbuf = _link_ntoa(tha, NULL, maclen, IFT_OTHER);
1030Sstevel@tonic-gate if (dmacbuf == NULL)
1040Sstevel@tonic-gate pr_err("Warning: malloc failure");
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate src_name = addrtoname_align(sip);
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate if (flags & F_SUM) {
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate line = get_sum_line();
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate switch (arpop) {
1150Sstevel@tonic-gate case ARPOP_REQUEST:
1160Sstevel@tonic-gate (void) snprintf(line, MAXLINE, "ARP C Who is %s ?",
1170Sstevel@tonic-gate printip(tip));
1180Sstevel@tonic-gate break;
1190Sstevel@tonic-gate case ARPOP_REPLY:
1200Sstevel@tonic-gate (void) snprintf(line, MAXLINE, "ARP R %s is %s",
1210Sstevel@tonic-gate printip(sip), smacbuf);
1220Sstevel@tonic-gate dst_name = addrtoname_align(tip);
1230Sstevel@tonic-gate break;
1240Sstevel@tonic-gate case REVARP_REQUEST:
1250Sstevel@tonic-gate (void) snprintf(line, MAXLINE, "RARP C Who is %s ?",
1260Sstevel@tonic-gate dmacbuf);
1270Sstevel@tonic-gate break;
1280Sstevel@tonic-gate case REVARP_REPLY:
1290Sstevel@tonic-gate (void) snprintf(line, MAXLINE, "RARP R %s is %s",
1300Sstevel@tonic-gate dmacbuf, printip(tip));
1310Sstevel@tonic-gate dst_name = addrtoname_align(tip);
1320Sstevel@tonic-gate break;
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate if (flags & F_DTAIL) {
1370Sstevel@tonic-gate show_header("ARP: ", "ARP/RARP Frame", alen);
1380Sstevel@tonic-gate show_space();
1390Sstevel@tonic-gate (void) snprintf(get_line(0, 0), get_line_remain(),
140*3431Scarlsonj "Hardware type = %d (%s)", ntohs(ap->ar_hrd),
141*3431Scarlsonj arp_htype(ntohs(ap->ar_hrd)));
1420Sstevel@tonic-gate (void) snprintf(get_line(0, 0), get_line_remain(),
1430Sstevel@tonic-gate "Protocol type = %04x (%s)", ntohs(ap->ar_pro),
1440Sstevel@tonic-gate print_ethertype(ntohs(ap->ar_pro)));
1450Sstevel@tonic-gate (void) snprintf(get_line(0, 0), get_line_remain(),
1460Sstevel@tonic-gate "Length of hardware address = %d bytes", ap->ar_hln);
1470Sstevel@tonic-gate (void) snprintf(get_line(0, 0), get_line_remain(),
1480Sstevel@tonic-gate "Length of protocol address = %d bytes", ap->ar_pln);
1490Sstevel@tonic-gate (void) snprintf(get_line(0, 0), get_line_remain(),
1500Sstevel@tonic-gate "Opcode %d (%s)", arpop,
1510Sstevel@tonic-gate (arpop > REVARP_REPLY) ? opname[0] : opname[arpop]);
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate if (is_ip) {
1540Sstevel@tonic-gate (void) snprintf(get_line(0, 0), get_line_remain(),
1550Sstevel@tonic-gate "Sender's hardware address = %s", smacbuf);
1560Sstevel@tonic-gate (void) snprintf(get_line(0, 0), get_line_remain(),
1570Sstevel@tonic-gate "Sender's protocol address = %s",
1580Sstevel@tonic-gate printip(sip));
1590Sstevel@tonic-gate (void) snprintf(get_line(0, 0), get_line_remain(),
1600Sstevel@tonic-gate "Target hardware address = %s",
1610Sstevel@tonic-gate arpop == ARPOP_REQUEST ? "?" : dmacbuf);
1620Sstevel@tonic-gate (void) snprintf(get_line(0, 0), get_line_remain(),
1630Sstevel@tonic-gate "Target protocol address = %s",
1640Sstevel@tonic-gate arpop == REVARP_REQUEST ? "?" :
1650Sstevel@tonic-gate printip(tip));
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate show_trailer();
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate if (maclen != 0) {
1710Sstevel@tonic-gate free(smacbuf);
1720Sstevel@tonic-gate free(dmacbuf);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate return;
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate short_packet:
1770Sstevel@tonic-gate if (flags & F_SUM) {
1780Sstevel@tonic-gate (void) snprintf(get_sum_line(), MAXLINE,
1790Sstevel@tonic-gate "ARP (short packet)");
1800Sstevel@tonic-gate } else if (flags & F_DTAIL) {
1810Sstevel@tonic-gate show_header("ARP: ", "ARP/RARP Frame", alen);
1820Sstevel@tonic-gate show_space();
1830Sstevel@tonic-gate (void) snprintf(get_line(0, 0), get_line_remain(),
1840Sstevel@tonic-gate "ARP (short packet)");
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate char *
printip(unsigned char * p)1890Sstevel@tonic-gate printip(unsigned char *p)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate static char buff[MAXHOSTNAMELEN + 32];
1920Sstevel@tonic-gate char *ap, *np;
1930Sstevel@tonic-gate struct in_addr a;
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate memcpy(&a, p, 4);
1960Sstevel@tonic-gate ap = (char *)inet_ntoa(a);
1970Sstevel@tonic-gate np = (char *)addrtoname(AF_INET, &a);
1980Sstevel@tonic-gate (void) snprintf(buff, MAXHOSTNAMELEN, "%s, %s", ap, np);
1990Sstevel@tonic-gate return (buff);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate char *
addrtoname_align(unsigned char * p)2030Sstevel@tonic-gate addrtoname_align(unsigned char *p)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate struct in_addr a;
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate memcpy(&a, p, 4);
2080Sstevel@tonic-gate return ((char *)addrtoname(AF_INET, &a));
2090Sstevel@tonic-gate }
210*3431Scarlsonj
211*3431Scarlsonj /*
212*3431Scarlsonj * These numbers are assigned by the IANA. See the arp-parameters registry.
213*3431Scarlsonj * Only those values that are used within Solaris have #defines.
214*3431Scarlsonj */
215*3431Scarlsonj const char *
arp_htype(int t)216*3431Scarlsonj arp_htype(int t)
217*3431Scarlsonj {
218*3431Scarlsonj switch (t) {
219*3431Scarlsonj case ARPHRD_ETHER:
220*3431Scarlsonj return ("Ethernet (10Mb)");
221*3431Scarlsonj case 2:
222*3431Scarlsonj return ("Experimental Ethernet (3MB)");
223*3431Scarlsonj case 3:
224*3431Scarlsonj return ("Amateur Radio AX.25");
225*3431Scarlsonj case 4:
226*3431Scarlsonj return ("Proteon ProNET Token Ring");
227*3431Scarlsonj case 5:
228*3431Scarlsonj return ("Chaos");
229*3431Scarlsonj case ARPHRD_IEEE802:
230*3431Scarlsonj return ("IEEE 802");
231*3431Scarlsonj case 7:
232*3431Scarlsonj return ("ARCNET");
233*3431Scarlsonj case 8:
234*3431Scarlsonj return ("Hyperchannel");
235*3431Scarlsonj case 9:
236*3431Scarlsonj return ("Lanstar");
237*3431Scarlsonj case 10:
238*3431Scarlsonj return ("Autonet");
239*3431Scarlsonj case 11:
240*3431Scarlsonj return ("LocalTalk");
241*3431Scarlsonj case 12:
242*3431Scarlsonj return ("LocalNet");
243*3431Scarlsonj case 13:
244*3431Scarlsonj return ("Ultra Link");
245*3431Scarlsonj case 14:
246*3431Scarlsonj return ("SMDS");
247*3431Scarlsonj case ARPHRD_FRAME:
248*3431Scarlsonj return ("Frame Relay");
249*3431Scarlsonj case ARPHRD_ATM:
250*3431Scarlsonj return ("ATM");
251*3431Scarlsonj case ARPHRD_HDLC:
252*3431Scarlsonj return ("HDLC");
253*3431Scarlsonj case ARPHRD_FC:
254*3431Scarlsonj return ("Fibre Channel");
255*3431Scarlsonj case ARPHRD_IPATM:
256*3431Scarlsonj return ("IP-ATM");
257*3431Scarlsonj case ARPHRD_TUNNEL:
258*3431Scarlsonj return ("Tunnel");
259*3431Scarlsonj case ARPHRD_IB:
260*3431Scarlsonj return ("IPIB");
261*3431Scarlsonj };
262*3431Scarlsonj return ("UNKNOWN");
263*3431Scarlsonj }
264