xref: /onnv-gate/usr/src/cmd/mdb/common/modules/arp/arp.c (revision 11042:2d6e217af1b4)
12546Scarlsonj /*
22546Scarlsonj  * CDDL HEADER START
32546Scarlsonj  *
42546Scarlsonj  * The contents of this file are subject to the terms of the
52546Scarlsonj  * Common Development and Distribution License (the "License").
62546Scarlsonj  * You may not use this file except in compliance with the License.
72546Scarlsonj  *
82546Scarlsonj  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92546Scarlsonj  * or http://www.opensolaris.org/os/licensing.
102546Scarlsonj  * See the License for the specific language governing permissions
112546Scarlsonj  * and limitations under the License.
122546Scarlsonj  *
132546Scarlsonj  * When distributing Covered Code, include this CDDL HEADER in each
142546Scarlsonj  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152546Scarlsonj  * If applicable, add the following below this CDDL HEADER, with the
162546Scarlsonj  * fields enclosed by brackets "[]" replaced with your own identifying
172546Scarlsonj  * information: Portions Copyright [yyyy] [name of copyright owner]
182546Scarlsonj  *
192546Scarlsonj  * CDDL HEADER END
202546Scarlsonj  */
212546Scarlsonj /*
22*11042SErik.Nordmark@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
232546Scarlsonj  * Use is subject to license terms.
242546Scarlsonj  */
252546Scarlsonj 
262546Scarlsonj #include <stdio.h>
272546Scarlsonj #include <sys/types.h>
282546Scarlsonj #include <sys/stropts.h>
292546Scarlsonj #include <sys/stream.h>
302546Scarlsonj #include <sys/dlpi.h>
312958Sdr146992 #include <sys/hook.h>
322958Sdr146992 #include <sys/hook_event.h>
332546Scarlsonj #include <inet/led.h>
342546Scarlsonj #include <inet/common.h>
352546Scarlsonj #include <inet/mi.h>
362546Scarlsonj #include <inet/arp.h>
372546Scarlsonj #include <inet/ip.h>
382546Scarlsonj #include <netinet/arp.h>
392546Scarlsonj 
402546Scarlsonj #include <mdb/mdb_modapi.h>
412546Scarlsonj #include <mdb/mdb_ks.h>
422546Scarlsonj 
432546Scarlsonj typedef struct {
442546Scarlsonj 	uint32_t	act_cmd;
452546Scarlsonj 	char		*act_name;
462546Scarlsonj 	char		*act_type;
472546Scarlsonj } arp_cmd_tbl;
482546Scarlsonj 
492546Scarlsonj /*
50*11042SErik.Nordmark@Sun.COM  * removed all the ace/arl related stuff. The only thing that remains
51*11042SErik.Nordmark@Sun.COM  * is code for dealing with ioctls and printing out arp header that
52*11042SErik.Nordmark@Sun.COM  * should probably be moved into the ip/mdb module.
533865Szf203873  */
542546Scarlsonj 
552546Scarlsonj /*
562546Scarlsonj  * Print an ARP hardware and protocol address pair; used when printing an ARP
572546Scarlsonj  * message.
582546Scarlsonj  */
592546Scarlsonj static void
print_arp(char field_id,const uchar_t * buf,const arh_t * arh,uint16_t ptype)602546Scarlsonj print_arp(char field_id, const uchar_t *buf, const arh_t *arh, uint16_t ptype)
612546Scarlsonj {
622546Scarlsonj 	char macstr[ARP_MAX_ADDR_LEN*3];
632546Scarlsonj 	in_addr_t inaddr;
642546Scarlsonj 
652546Scarlsonj 	if (arh->arh_hlen == 0)
662546Scarlsonj 		(void) strcpy(macstr, "(none)");
672546Scarlsonj 	else
682546Scarlsonj 		mdb_mac_addr(buf, arh->arh_hlen, macstr, sizeof (macstr));
692546Scarlsonj 	mdb_printf("%?s  ar$%cha %s\n", "", field_id, macstr);
702546Scarlsonj 	if (arh->arh_plen == 0) {
712546Scarlsonj 		mdb_printf("%?s  ar$%cpa (none)\n", "", field_id);
722546Scarlsonj 	} else if (ptype == IP_ARP_PROTO_TYPE) {
732546Scarlsonj 		mdb_printf("%?s  ar$%cpa (unknown)\n", "", field_id);
742546Scarlsonj 	} else if (arh->arh_plen == sizeof (in_addr_t)) {
752546Scarlsonj 		(void) memcpy(&inaddr, buf + arh->arh_hlen, sizeof (inaddr));
762546Scarlsonj 		mdb_printf("%?s  ar$%cpa %I\n", "", field_id, inaddr);
772546Scarlsonj 	} else {
782546Scarlsonj 		mdb_printf("%?s  ar$%cpa (malformed IP)\n", "", field_id);
792546Scarlsonj 	}
802546Scarlsonj }
812546Scarlsonj 
822546Scarlsonj /*
832546Scarlsonj  * Decode an ARP message and display it.
842546Scarlsonj  */
852546Scarlsonj /* ARGSUSED2 */
862546Scarlsonj static int
arphdr_cmd(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)872546Scarlsonj arphdr_cmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
882546Scarlsonj {
892546Scarlsonj 	struct {
902546Scarlsonj 		arh_t arh;
912546Scarlsonj 		uchar_t addrs[4 * ARP_MAX_ADDR_LEN];
922546Scarlsonj 	} arp;
932546Scarlsonj 	size_t blen;
942546Scarlsonj 	uint16_t htype, ptype, op;
952546Scarlsonj 	const char *cp;
962546Scarlsonj 
972546Scarlsonj 	if (!(flags & DCMD_ADDRSPEC)) {
982546Scarlsonj 		mdb_warn("address required to print ARP header\n");
992546Scarlsonj 		return (DCMD_ERR);
1002546Scarlsonj 	}
1012546Scarlsonj 
1022546Scarlsonj 	if (mdb_vread(&arp.arh, sizeof (arp.arh), addr) == -1) {
1032546Scarlsonj 		mdb_warn("unable to read ARP header at %p", addr);
1042546Scarlsonj 		return (DCMD_ERR);
1052546Scarlsonj 	}
1062546Scarlsonj 	mdb_nhconvert(&htype, arp.arh.arh_hardware, sizeof (htype));
1072546Scarlsonj 	mdb_nhconvert(&ptype, arp.arh.arh_proto, sizeof (ptype));
1082546Scarlsonj 	mdb_nhconvert(&op, arp.arh.arh_operation, sizeof (op));
1092546Scarlsonj 
1102546Scarlsonj 	switch (htype) {
1112546Scarlsonj 	case ARPHRD_ETHER:
1122546Scarlsonj 		cp = "Ether";
1132546Scarlsonj 		break;
1142546Scarlsonj 	case ARPHRD_IEEE802:
1152546Scarlsonj 		cp = "IEEE802";
1162546Scarlsonj 		break;
1172546Scarlsonj 	case ARPHRD_IB:
1182546Scarlsonj 		cp = "InfiniBand";
1192546Scarlsonj 		break;
1202546Scarlsonj 	default:
1212546Scarlsonj 		cp = "Unknown";
1222546Scarlsonj 		break;
1232546Scarlsonj 	}
1242546Scarlsonj 	mdb_printf("%?p: ar$hrd %x (%s)\n", addr, htype, cp);
1252546Scarlsonj 	mdb_printf("%?s  ar$pro %x (%s)\n", "", ptype,
1262546Scarlsonj 	    ptype == IP_ARP_PROTO_TYPE ? "IP" : "Unknown");
1272546Scarlsonj 
1282546Scarlsonj 	switch (op) {
1292546Scarlsonj 	case ARPOP_REQUEST:
1302546Scarlsonj 		cp = "ares_op$REQUEST";
1312546Scarlsonj 		break;
1322546Scarlsonj 	case ARPOP_REPLY:
1332546Scarlsonj 		cp = "ares_op$REPLY";
1342546Scarlsonj 		break;
1352546Scarlsonj 	case REVARP_REQUEST:
1362546Scarlsonj 		cp = "arev_op$REQUEST";
1372546Scarlsonj 		break;
1382546Scarlsonj 	case REVARP_REPLY:
1392546Scarlsonj 		cp = "arev_op$REPLY";
1402546Scarlsonj 		break;
1412546Scarlsonj 	default:
1422546Scarlsonj 		cp = "Unknown";
1432546Scarlsonj 		break;
1442546Scarlsonj 	}
1452546Scarlsonj 	mdb_printf("%?s  ar$op %d (%s)\n", "", op, cp);
1462546Scarlsonj 
1472546Scarlsonj 	/*
1482546Scarlsonj 	 * Note that we go to some length to attempt to print out the fixed
1492546Scarlsonj 	 * header data before trying to decode the variable-length data.  This
1502546Scarlsonj 	 * is done to maximize the amount of useful information shown when the
1512546Scarlsonj 	 * buffer is truncated or otherwise corrupt.
1522546Scarlsonj 	 */
1532546Scarlsonj 	blen = 2 * (arp.arh.arh_hlen + arp.arh.arh_plen);
1542546Scarlsonj 	if (mdb_vread(&arp.addrs, blen, addr + sizeof (arp.arh)) == -1) {
1552546Scarlsonj 		mdb_warn("unable to read ARP body at %p", addr);
1562546Scarlsonj 		return (DCMD_ERR);
1572546Scarlsonj 	}
1582546Scarlsonj 
1592546Scarlsonj 	print_arp('s', arp.addrs, &arp.arh, ptype);
1602546Scarlsonj 	print_arp('t', arp.addrs + arp.arh.arh_hlen + arp.arh.arh_plen,
1612546Scarlsonj 	    &arp.arh, ptype);
1622546Scarlsonj 	return (DCMD_OK);
1632546Scarlsonj }
1642546Scarlsonj 
1652546Scarlsonj static const mdb_dcmd_t dcmds[] = {
1662546Scarlsonj 	{ "arphdr", ":", "display an ARP header", arphdr_cmd, NULL },
1672546Scarlsonj 	{ NULL }
1682546Scarlsonj };
1692546Scarlsonj 
1702546Scarlsonj /* Note: ar_t walker is in genunix.c and net.c; generic MI walker */
1712546Scarlsonj static const mdb_walker_t walkers[] = {
1722546Scarlsonj 	{ NULL }
1732546Scarlsonj };
1742546Scarlsonj 
1752546Scarlsonj static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
1762546Scarlsonj 
1772546Scarlsonj const mdb_modinfo_t *
_mdb_init(void)1782546Scarlsonj _mdb_init(void)
1792546Scarlsonj {
1802546Scarlsonj 	return (&modinfo);
1812546Scarlsonj }
1822546Scarlsonj 
1832546Scarlsonj void
_mdb_fini(void)1842546Scarlsonj _mdb_fini(void)
1852546Scarlsonj {
1862546Scarlsonj }
187