xref: /freebsd/contrib/tcpdump/print-arp.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
14edb46e9SPaul Traina /*
2699fc314SBill Fenner  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
34edb46e9SPaul Traina  *	The Regents of the University of California.  All rights reserved.
44edb46e9SPaul Traina  *
54edb46e9SPaul Traina  * Redistribution and use in source and binary forms, with or without
64edb46e9SPaul Traina  * modification, are permitted provided that: (1) source code distributions
74edb46e9SPaul Traina  * retain the above copyright notice and this paragraph in its entirety, (2)
84edb46e9SPaul Traina  * distributions including binary code include the above copyright notice and
94edb46e9SPaul Traina  * this paragraph in its entirety in the documentation or other materials
104edb46e9SPaul Traina  * provided with the distribution, and (3) all advertising materials mentioning
114edb46e9SPaul Traina  * features or use of this software display the following acknowledgement:
124edb46e9SPaul Traina  * ``This product includes software developed by the University of California,
134edb46e9SPaul Traina  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
144edb46e9SPaul Traina  * the University nor the names of its contributors may be used to endorse
154edb46e9SPaul Traina  * or promote products derived from this software without specific prior
164edb46e9SPaul Traina  * written permission.
174edb46e9SPaul Traina  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
184edb46e9SPaul Traina  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
194edb46e9SPaul Traina  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
204edb46e9SPaul Traina  */
214edb46e9SPaul Traina 
223340d773SGleb Smirnoff /* \summary: Address Resolution Protocol (ARP) printer */
233340d773SGleb Smirnoff 
24*ee67461eSJoseph Mingrone #include <config.h>
254edb46e9SPaul Traina 
26*ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
274edb46e9SPaul Traina 
28*ee67461eSJoseph Mingrone #define ND_LONGJMP_FROM_TCHECK
293340d773SGleb Smirnoff #include "netdissect.h"
304edb46e9SPaul Traina #include "addrtoname.h"
314edb46e9SPaul Traina #include "ethertype.h"
323340d773SGleb Smirnoff #include "extract.h"
334edb46e9SPaul Traina 
343c602fabSXin LI 
35943ee2b1SBill Fenner /*
36943ee2b1SBill Fenner  * Address Resolution Protocol.
37943ee2b1SBill Fenner  *
38943ee2b1SBill Fenner  * See RFC 826 for protocol description.  ARP packets are variable
39943ee2b1SBill Fenner  * in size; the arphdr structure defines the fixed-length portion.
40943ee2b1SBill Fenner  * Protocol type values are the same as those for 10 Mb/s Ethernet.
41943ee2b1SBill Fenner  * It is followed by the variable-sized fields ar_sha, arp_spa,
42943ee2b1SBill Fenner  * arp_tha and arp_tpa in that order, according to the lengths
43943ee2b1SBill Fenner  * specified.  Field names used correspond to RFC 826.
44943ee2b1SBill Fenner  */
450e0def19SBill Fenner struct  arp_pkthdr {
46*ee67461eSJoseph Mingrone         nd_uint16_t ar_hrd;     /* format of hardware address */
47943ee2b1SBill Fenner #define ARPHRD_ETHER    1       /* ethernet hardware format */
48943ee2b1SBill Fenner #define ARPHRD_IEEE802  6       /* token-ring hardware format */
49a1c2090eSBill Fenner #define ARPHRD_ARCNET   7       /* arcnet hardware format */
50943ee2b1SBill Fenner #define ARPHRD_FRELAY   15      /* frame relay hardware format */
51a5779b6eSRui Paulo #define ARPHRD_ATM2225  19      /* ATM (RFC 2225) */
52a1c2090eSBill Fenner #define ARPHRD_STRIP    23      /* Ricochet Starmode Radio hardware format */
53a1c2090eSBill Fenner #define ARPHRD_IEEE1394 24      /* IEEE 1394 (FireWire) hardware format */
54*ee67461eSJoseph Mingrone #define ARPHRD_INFINIBAND 32    /* InfiniBand RFC 4391 */
55*ee67461eSJoseph Mingrone         nd_uint16_t ar_pro;     /* format of protocol address */
56*ee67461eSJoseph Mingrone         nd_uint8_t  ar_hln;     /* length of hardware address */
57*ee67461eSJoseph Mingrone         nd_uint8_t  ar_pln;     /* length of protocol address */
58*ee67461eSJoseph Mingrone         nd_uint16_t ar_op;      /* one of: */
59943ee2b1SBill Fenner #define ARPOP_REQUEST   1       /* request to resolve address */
60943ee2b1SBill Fenner #define ARPOP_REPLY     2       /* response to previous request */
61943ee2b1SBill Fenner #define ARPOP_REVREQUEST 3      /* request protocol address given hardware */
62943ee2b1SBill Fenner #define ARPOP_REVREPLY  4       /* response giving protocol address */
63943ee2b1SBill Fenner #define ARPOP_INVREQUEST 8      /* request to identify peer */
64943ee2b1SBill Fenner #define ARPOP_INVREPLY  9       /* response identifying peer */
65*ee67461eSJoseph Mingrone #define ARPOP_NAK       10      /* NAK - only valid for ATM ARP */
66a5779b6eSRui Paulo 
67943ee2b1SBill Fenner /*
68943ee2b1SBill Fenner  * The remaining fields are variable in size,
69943ee2b1SBill Fenner  * according to the sizes above.
70943ee2b1SBill Fenner  */
71943ee2b1SBill Fenner #ifdef COMMENT_ONLY
72*ee67461eSJoseph Mingrone 	nd_byte		ar_sha[];	/* sender hardware address */
73*ee67461eSJoseph Mingrone 	nd_byte		ar_spa[];	/* sender protocol address */
74*ee67461eSJoseph Mingrone 	nd_byte		ar_tha[];	/* target hardware address */
75*ee67461eSJoseph Mingrone 	nd_byte		ar_tpa[];	/* target protocol address */
76943ee2b1SBill Fenner #endif
770e0def19SBill Fenner #define ar_sha(ap)	(((const u_char *)((ap)+1))+  0)
78*ee67461eSJoseph Mingrone #define ar_spa(ap)	(((const u_char *)((ap)+1))+  GET_U_1((ap)->ar_hln))
79*ee67461eSJoseph Mingrone #define ar_tha(ap)	(((const u_char *)((ap)+1))+  GET_U_1((ap)->ar_hln)+GET_U_1((ap)->ar_pln))
80*ee67461eSJoseph Mingrone #define ar_tpa(ap)	(((const u_char *)((ap)+1))+2*GET_U_1((ap)->ar_hln)+GET_U_1((ap)->ar_pln))
81943ee2b1SBill Fenner };
82943ee2b1SBill Fenner 
83943ee2b1SBill Fenner #define ARP_HDRLEN	8
84943ee2b1SBill Fenner 
85*ee67461eSJoseph Mingrone #define HRD(ap) GET_BE_U_2((ap)->ar_hrd)
86*ee67461eSJoseph Mingrone #define HRD_LEN(ap) GET_U_1((ap)->ar_hln)
87*ee67461eSJoseph Mingrone #define PROTO_LEN(ap) GET_U_1((ap)->ar_pln)
88*ee67461eSJoseph Mingrone #define OP(ap)  GET_BE_U_2((ap)->ar_op)
89*ee67461eSJoseph Mingrone #define PRO(ap) GET_BE_U_2((ap)->ar_pro)
90a1c2090eSBill Fenner #define SHA(ap) (ar_sha(ap))
91a1c2090eSBill Fenner #define SPA(ap) (ar_spa(ap))
92a1c2090eSBill Fenner #define THA(ap) (ar_tha(ap))
93a1c2090eSBill Fenner #define TPA(ap) (ar_tpa(ap))
944edb46e9SPaul Traina 
95a5779b6eSRui Paulo 
963c602fabSXin LI static const struct tok arpop_values[] = {
97a5779b6eSRui Paulo     { ARPOP_REQUEST, "Request" },
98a5779b6eSRui Paulo     { ARPOP_REPLY, "Reply" },
99a5779b6eSRui Paulo     { ARPOP_REVREQUEST, "Reverse Request" },
100a5779b6eSRui Paulo     { ARPOP_REVREPLY, "Reverse Reply" },
101a5779b6eSRui Paulo     { ARPOP_INVREQUEST, "Inverse Request" },
102a5779b6eSRui Paulo     { ARPOP_INVREPLY, "Inverse Reply" },
103a5779b6eSRui Paulo     { ARPOP_NAK, "NACK Reply" },
104a5779b6eSRui Paulo     { 0, NULL }
105a5779b6eSRui Paulo };
106a5779b6eSRui Paulo 
1073c602fabSXin LI static const struct tok arphrd_values[] = {
108a5779b6eSRui Paulo     { ARPHRD_ETHER, "Ethernet" },
109a5779b6eSRui Paulo     { ARPHRD_IEEE802, "TokenRing" },
110a5779b6eSRui Paulo     { ARPHRD_ARCNET, "ArcNet" },
111a5779b6eSRui Paulo     { ARPHRD_FRELAY, "FrameRelay" },
112a5779b6eSRui Paulo     { ARPHRD_STRIP, "Strip" },
113a5779b6eSRui Paulo     { ARPHRD_IEEE1394, "IEEE 1394" },
114a5779b6eSRui Paulo     { ARPHRD_ATM2225, "ATM" },
115*ee67461eSJoseph Mingrone     { ARPHRD_INFINIBAND, "InfiniBand" },
116a5779b6eSRui Paulo     { 0, NULL }
117a5779b6eSRui Paulo };
118a5779b6eSRui Paulo 
119cc391cceSBruce M Simpson /*
120cc391cceSBruce M Simpson  * ATM Address Resolution Protocol.
121cc391cceSBruce M Simpson  *
122cc391cceSBruce M Simpson  * See RFC 2225 for protocol description.  ATMARP packets are similar
123cc391cceSBruce M Simpson  * to ARP packets, except that there are no length fields for the
124cc391cceSBruce M Simpson  * protocol address - instead, there are type/length fields for
125cc391cceSBruce M Simpson  * the ATM number and subaddress - and the hardware addresses consist
126cc391cceSBruce M Simpson  * of an ATM number and an ATM subaddress.
127cc391cceSBruce M Simpson  */
128cc391cceSBruce M Simpson struct  atmarp_pkthdr {
129*ee67461eSJoseph Mingrone         nd_uint16_t aar_hrd;    /* format of hardware address */
130*ee67461eSJoseph Mingrone         nd_uint16_t aar_pro;    /* format of protocol address */
131*ee67461eSJoseph Mingrone         nd_uint8_t  aar_shtl;   /* length of source ATM number */
132*ee67461eSJoseph Mingrone         nd_uint8_t  aar_sstl;   /* length of source ATM subaddress */
133cc391cceSBruce M Simpson #define ATMARP_IS_E164  0x40    /* bit in type/length for E.164 format */
134cc391cceSBruce M Simpson #define ATMARP_LEN_MASK 0x3F    /* length of {sub}address in type/length */
135*ee67461eSJoseph Mingrone         nd_uint16_t aar_op;     /* same as regular ARP */
136*ee67461eSJoseph Mingrone         nd_uint8_t  aar_spln;   /* length of source protocol address */
137*ee67461eSJoseph Mingrone         nd_uint8_t  aar_thtl;   /* length of target ATM number */
138*ee67461eSJoseph Mingrone         nd_uint8_t  aar_tstl;   /* length of target ATM subaddress */
139*ee67461eSJoseph Mingrone         nd_uint8_t  aar_tpln;   /* length of target protocol address */
140cc391cceSBruce M Simpson /*
141cc391cceSBruce M Simpson  * The remaining fields are variable in size,
142cc391cceSBruce M Simpson  * according to the sizes above.
143cc391cceSBruce M Simpson  */
144cc391cceSBruce M Simpson #ifdef COMMENT_ONLY
145*ee67461eSJoseph Mingrone 	nd_byte		aar_sha[];	/* source ATM number */
146*ee67461eSJoseph Mingrone 	nd_byte		aar_ssa[];	/* source ATM subaddress */
147*ee67461eSJoseph Mingrone 	nd_byte		aar_spa[];	/* sender protocol address */
148*ee67461eSJoseph Mingrone 	nd_byte		aar_tha[];	/* target ATM number */
149*ee67461eSJoseph Mingrone 	nd_byte		aar_tsa[];	/* target ATM subaddress */
150*ee67461eSJoseph Mingrone 	nd_byte		aar_tpa[];	/* target protocol address */
151cc391cceSBruce M Simpson #endif
152cc391cceSBruce M Simpson 
153*ee67461eSJoseph Mingrone #define ATMHRD(ap)  GET_BE_U_2((ap)->aar_hrd)
154*ee67461eSJoseph Mingrone #define ATMSHRD_LEN(ap) (GET_U_1((ap)->aar_shtl) & ATMARP_LEN_MASK)
155*ee67461eSJoseph Mingrone #define ATMSSLN(ap) (GET_U_1((ap)->aar_sstl) & ATMARP_LEN_MASK)
156*ee67461eSJoseph Mingrone #define ATMSPROTO_LEN(ap) GET_U_1((ap)->aar_spln)
157*ee67461eSJoseph Mingrone #define ATMOP(ap)   GET_BE_U_2((ap)->aar_op)
158*ee67461eSJoseph Mingrone #define ATMPRO(ap)  GET_BE_U_2((ap)->aar_pro)
159*ee67461eSJoseph Mingrone #define ATMTHRD_LEN(ap) (GET_U_1((ap)->aar_thtl) & ATMARP_LEN_MASK)
160*ee67461eSJoseph Mingrone #define ATMTSLN(ap) (GET_U_1((ap)->aar_tstl) & ATMARP_LEN_MASK)
161*ee67461eSJoseph Mingrone #define ATMTPROTO_LEN(ap) GET_U_1((ap)->aar_tpln)
162cc391cceSBruce M Simpson #define aar_sha(ap)	((const u_char *)((ap)+1))
163a5779b6eSRui Paulo #define aar_ssa(ap)	(aar_sha(ap) + ATMSHRD_LEN(ap))
164cc391cceSBruce M Simpson #define aar_spa(ap)	(aar_ssa(ap) + ATMSSLN(ap))
165a5779b6eSRui Paulo #define aar_tha(ap)	(aar_spa(ap) + ATMSPROTO_LEN(ap))
166a5779b6eSRui Paulo #define aar_tsa(ap)	(aar_tha(ap) + ATMTHRD_LEN(ap))
167cc391cceSBruce M Simpson #define aar_tpa(ap)	(aar_tsa(ap) + ATMTSLN(ap))
168cc391cceSBruce M Simpson };
169cc391cceSBruce M Simpson 
170cc391cceSBruce M Simpson #define ATMSHA(ap) (aar_sha(ap))
171cc391cceSBruce M Simpson #define ATMSSA(ap) (aar_ssa(ap))
172cc391cceSBruce M Simpson #define ATMSPA(ap) (aar_spa(ap))
173cc391cceSBruce M Simpson #define ATMTHA(ap) (aar_tha(ap))
174cc391cceSBruce M Simpson #define ATMTSA(ap) (aar_tsa(ap))
175cc391cceSBruce M Simpson #define ATMTPA(ap) (aar_tpa(ap))
176cc391cceSBruce M Simpson 
1773340d773SGleb Smirnoff static int
isnonzero(netdissect_options * ndo,const u_char * a,size_t len)178*ee67461eSJoseph Mingrone isnonzero(netdissect_options *ndo, const u_char *a, size_t len)
1793340d773SGleb Smirnoff {
1803340d773SGleb Smirnoff 	while (len > 0) {
181*ee67461eSJoseph Mingrone 		if (GET_U_1(a) != 0)
1823340d773SGleb Smirnoff 			return (1);
1833340d773SGleb Smirnoff 		a++;
1843340d773SGleb Smirnoff 		len--;
1853340d773SGleb Smirnoff 	}
1863340d773SGleb Smirnoff 	return (0);
1873340d773SGleb Smirnoff }
1884edb46e9SPaul Traina 
189cc391cceSBruce M Simpson static void
tpaddr_print_ip(netdissect_options * ndo,const struct arp_pkthdr * ap,u_short pro)1900bff6a5aSEd Maste tpaddr_print_ip(netdissect_options *ndo,
1910bff6a5aSEd Maste 	        const struct arp_pkthdr *ap, u_short pro)
1920bff6a5aSEd Maste {
1930bff6a5aSEd Maste 	if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
194*ee67461eSJoseph Mingrone 		ND_PRINT("<wrong proto type>");
1950bff6a5aSEd Maste 	else if (PROTO_LEN(ap) != 4)
196*ee67461eSJoseph Mingrone 		ND_PRINT("<wrong len>");
1970bff6a5aSEd Maste 	else
198*ee67461eSJoseph Mingrone 		ND_PRINT("%s", GET_IPADDR_STRING(TPA(ap)));
1990bff6a5aSEd Maste }
2000bff6a5aSEd Maste 
2010bff6a5aSEd Maste static void
spaddr_print_ip(netdissect_options * ndo,const struct arp_pkthdr * ap,u_short pro)2020bff6a5aSEd Maste spaddr_print_ip(netdissect_options *ndo,
2030bff6a5aSEd Maste 	        const struct arp_pkthdr *ap, u_short pro)
2040bff6a5aSEd Maste {
2050bff6a5aSEd Maste 	if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
206*ee67461eSJoseph Mingrone 		ND_PRINT("<wrong proto type>");
2070bff6a5aSEd Maste 	else if (PROTO_LEN(ap) != 4)
208*ee67461eSJoseph Mingrone 		ND_PRINT("<wrong len>");
2090bff6a5aSEd Maste 	else
210*ee67461eSJoseph Mingrone 		ND_PRINT("%s", GET_IPADDR_STRING(SPA(ap)));
2110bff6a5aSEd Maste }
2120bff6a5aSEd Maste 
2130bff6a5aSEd Maste static void
atmarp_addr_print(netdissect_options * ndo,const u_char * ha,u_int ha_len,const u_char * srca,u_int srca_len)214c1ad1296SSam Leffler atmarp_addr_print(netdissect_options *ndo,
215c1ad1296SSam Leffler 		  const u_char *ha, u_int ha_len, const u_char *srca,
216cc391cceSBruce M Simpson     u_int srca_len)
217cc391cceSBruce M Simpson {
218cc391cceSBruce M Simpson 	if (ha_len == 0)
219*ee67461eSJoseph Mingrone 		ND_PRINT("<No address>");
220cc391cceSBruce M Simpson 	else {
221*ee67461eSJoseph Mingrone 		ND_PRINT("%s", GET_LINKADDR_STRING(ha, LINKADDR_ATM, ha_len));
222cc391cceSBruce M Simpson 		if (srca_len != 0)
223*ee67461eSJoseph Mingrone 			ND_PRINT(",%s",
224*ee67461eSJoseph Mingrone 				  GET_LINKADDR_STRING(srca, LINKADDR_ATM, srca_len));
225cc391cceSBruce M Simpson 	}
226cc391cceSBruce M Simpson }
227cc391cceSBruce M Simpson 
228cc391cceSBruce M Simpson static void
atmarp_tpaddr_print(netdissect_options * ndo,const struct atmarp_pkthdr * ap,u_short pro)2290bff6a5aSEd Maste atmarp_tpaddr_print(netdissect_options *ndo,
2300bff6a5aSEd Maste 		    const struct atmarp_pkthdr *ap, u_short pro)
2310bff6a5aSEd Maste {
2320bff6a5aSEd Maste 	if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
233*ee67461eSJoseph Mingrone 		ND_PRINT("<wrong proto type>");
2340bff6a5aSEd Maste 	else if (ATMTPROTO_LEN(ap) != 4)
235*ee67461eSJoseph Mingrone 		ND_PRINT("<wrong tplen>");
2360bff6a5aSEd Maste 	else
237*ee67461eSJoseph Mingrone 		ND_PRINT("%s", GET_IPADDR_STRING(ATMTPA(ap)));
2380bff6a5aSEd Maste }
2390bff6a5aSEd Maste 
2400bff6a5aSEd Maste static void
atmarp_spaddr_print(netdissect_options * ndo,const struct atmarp_pkthdr * ap,u_short pro)2410bff6a5aSEd Maste atmarp_spaddr_print(netdissect_options *ndo,
2420bff6a5aSEd Maste 		    const struct atmarp_pkthdr *ap, u_short pro)
2430bff6a5aSEd Maste {
2440bff6a5aSEd Maste 	if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
245*ee67461eSJoseph Mingrone 		ND_PRINT("<wrong proto type>");
2460bff6a5aSEd Maste 	else if (ATMSPROTO_LEN(ap) != 4)
247*ee67461eSJoseph Mingrone 		ND_PRINT("<wrong splen>");
2480bff6a5aSEd Maste 	else
249*ee67461eSJoseph Mingrone 		ND_PRINT("%s", GET_IPADDR_STRING(ATMSPA(ap)));
2500bff6a5aSEd Maste }
2510bff6a5aSEd Maste 
2520bff6a5aSEd Maste static void
atmarp_print(netdissect_options * ndo,const u_char * bp,u_int length,u_int caplen)253c1ad1296SSam Leffler atmarp_print(netdissect_options *ndo,
254c1ad1296SSam Leffler 	     const u_char *bp, u_int length, u_int caplen)
255cc391cceSBruce M Simpson {
256cc391cceSBruce M Simpson 	const struct atmarp_pkthdr *ap;
257cc391cceSBruce M Simpson 	u_short pro, hrd, op;
258cc391cceSBruce M Simpson 
259cc391cceSBruce M Simpson 	ap = (const struct atmarp_pkthdr *)bp;
260*ee67461eSJoseph Mingrone 	ND_TCHECK_SIZE(ap);
261cc391cceSBruce M Simpson 
262cc391cceSBruce M Simpson 	hrd = ATMHRD(ap);
263cc391cceSBruce M Simpson 	pro = ATMPRO(ap);
264cc391cceSBruce M Simpson 	op = ATMOP(ap);
265cc391cceSBruce M Simpson 
266*ee67461eSJoseph Mingrone 	ND_TCHECK_LEN(ATMTPA(ap), ATMTPROTO_LEN(ap));
267cc391cceSBruce M Simpson 
268a5779b6eSRui Paulo         if (!ndo->ndo_eflag) {
269*ee67461eSJoseph Mingrone             ND_PRINT("ARP, ");
270cc391cceSBruce M Simpson         }
271a5779b6eSRui Paulo 
272a5779b6eSRui Paulo 	if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
273a5779b6eSRui Paulo 	    ATMSPROTO_LEN(ap) != 4 ||
274a5779b6eSRui Paulo             ATMTPROTO_LEN(ap) != 4 ||
275a5779b6eSRui Paulo             ndo->ndo_vflag) {
276*ee67461eSJoseph Mingrone                 ND_PRINT("%s, %s (len %u/%u)",
277a5779b6eSRui Paulo                           tok2str(arphrd_values, "Unknown Hardware (%u)", hrd),
278a5779b6eSRui Paulo                           tok2str(ethertype_values, "Unknown Protocol (0x%04x)", pro),
279a5779b6eSRui Paulo                           ATMSPROTO_LEN(ap),
280*ee67461eSJoseph Mingrone                           ATMTPROTO_LEN(ap));
281a5779b6eSRui Paulo 
282*ee67461eSJoseph Mingrone                 /* don't know about the address formats */
283a5779b6eSRui Paulo                 if (!ndo->ndo_vflag) {
284a5779b6eSRui Paulo                     goto out;
285a5779b6eSRui Paulo                 }
286a5779b6eSRui Paulo 	}
287a5779b6eSRui Paulo 
288a5779b6eSRui Paulo         /* print operation */
289*ee67461eSJoseph Mingrone         ND_PRINT("%s%s ",
290a5779b6eSRui Paulo                ndo->ndo_vflag ? ", " : "",
291*ee67461eSJoseph Mingrone                tok2str(arpop_values, "Unknown (%u)", op));
292a5779b6eSRui Paulo 
293cc391cceSBruce M Simpson 	switch (op) {
294cc391cceSBruce M Simpson 
295cc391cceSBruce M Simpson 	case ARPOP_REQUEST:
296*ee67461eSJoseph Mingrone 		ND_PRINT("who-has ");
2970bff6a5aSEd Maste 		atmarp_tpaddr_print(ndo, ap, pro);
298a5779b6eSRui Paulo 		if (ATMTHRD_LEN(ap) != 0) {
299*ee67461eSJoseph Mingrone 			ND_PRINT(" (");
300a5779b6eSRui Paulo 			atmarp_addr_print(ndo, ATMTHA(ap), ATMTHRD_LEN(ap),
301cc391cceSBruce M Simpson 			    ATMTSA(ap), ATMTSLN(ap));
302*ee67461eSJoseph Mingrone 			ND_PRINT(")");
303cc391cceSBruce M Simpson 		}
304*ee67461eSJoseph Mingrone 		ND_PRINT(" tell ");
3050bff6a5aSEd Maste 		atmarp_spaddr_print(ndo, ap, pro);
306cc391cceSBruce M Simpson 		break;
307cc391cceSBruce M Simpson 
308cc391cceSBruce M Simpson 	case ARPOP_REPLY:
3090bff6a5aSEd Maste 		atmarp_spaddr_print(ndo, ap, pro);
310*ee67461eSJoseph Mingrone 		ND_PRINT(" is-at ");
311a5779b6eSRui Paulo 		atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
312cc391cceSBruce M Simpson                                   ATMSSLN(ap));
313cc391cceSBruce M Simpson 		break;
314cc391cceSBruce M Simpson 
315cc391cceSBruce M Simpson 	case ARPOP_INVREQUEST:
316*ee67461eSJoseph Mingrone 		ND_PRINT("who-is ");
317a5779b6eSRui Paulo 		atmarp_addr_print(ndo, ATMTHA(ap), ATMTHRD_LEN(ap), ATMTSA(ap),
318cc391cceSBruce M Simpson 		    ATMTSLN(ap));
319*ee67461eSJoseph Mingrone 		ND_PRINT(" tell ");
320a5779b6eSRui Paulo 		atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
321cc391cceSBruce M Simpson 		    ATMSSLN(ap));
322cc391cceSBruce M Simpson 		break;
323cc391cceSBruce M Simpson 
324cc391cceSBruce M Simpson 	case ARPOP_INVREPLY:
325a5779b6eSRui Paulo 		atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
326cc391cceSBruce M Simpson 		    ATMSSLN(ap));
327*ee67461eSJoseph Mingrone 		ND_PRINT("at ");
3280bff6a5aSEd Maste 		atmarp_spaddr_print(ndo, ap, pro);
329cc391cceSBruce M Simpson 		break;
330cc391cceSBruce M Simpson 
331a5779b6eSRui Paulo 	case ARPOP_NAK:
332*ee67461eSJoseph Mingrone 		ND_PRINT("for ");
3330bff6a5aSEd Maste 		atmarp_spaddr_print(ndo, ap, pro);
334cc391cceSBruce M Simpson 		break;
335cc391cceSBruce M Simpson 
336cc391cceSBruce M Simpson 	default:
337c1ad1296SSam Leffler 		ND_DEFAULTPRINT((const u_char *)ap, caplen);
338cc391cceSBruce M Simpson 		return;
339cc391cceSBruce M Simpson 	}
340a5779b6eSRui Paulo 
341a5779b6eSRui Paulo  out:
342*ee67461eSJoseph Mingrone         ND_PRINT(", length %u", length);
343cc391cceSBruce M Simpson }
344cc391cceSBruce M Simpson 
3454edb46e9SPaul Traina void
arp_print(netdissect_options * ndo,const u_char * bp,u_int length,u_int caplen)346c1ad1296SSam Leffler arp_print(netdissect_options *ndo,
347c1ad1296SSam Leffler 	  const u_char *bp, u_int length, u_int caplen)
3484edb46e9SPaul Traina {
3490e0def19SBill Fenner 	const struct arp_pkthdr *ap;
350a5779b6eSRui Paulo 	u_short pro, hrd, op, linkaddr;
3514edb46e9SPaul Traina 
352*ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "arp";
3530e0def19SBill Fenner 	ap = (const struct arp_pkthdr *)bp;
354*ee67461eSJoseph Mingrone 	ND_TCHECK_SIZE(ap);
355a5779b6eSRui Paulo 
356cc391cceSBruce M Simpson 	hrd = HRD(ap);
357cc391cceSBruce M Simpson 	pro = PRO(ap);
358cc391cceSBruce M Simpson 	op = OP(ap);
359cc391cceSBruce M Simpson 
360a5779b6eSRui Paulo 
361a5779b6eSRui Paulo         /* if its ATM then call the ATM ARP printer
362a5779b6eSRui Paulo            for Frame-relay ARP most of the fields
363a5779b6eSRui Paulo            are similar to Ethernet so overload the Ethernet Printer
364*ee67461eSJoseph Mingrone            and set the linkaddr type for GET_LINKADDR_STRING() accordingly */
365a5779b6eSRui Paulo 
366a5779b6eSRui Paulo         switch(hrd) {
367a5779b6eSRui Paulo         case ARPHRD_ATM2225:
368a5779b6eSRui Paulo             atmarp_print(ndo, bp, length, caplen);
369a5779b6eSRui Paulo             return;
370a5779b6eSRui Paulo         case ARPHRD_FRELAY:
371a5779b6eSRui Paulo             linkaddr = LINKADDR_FRELAY;
372cac3dcd5SXin LI             break;
373a5779b6eSRui Paulo         default:
374a5779b6eSRui Paulo             linkaddr = LINKADDR_ETHER;
375a5779b6eSRui Paulo             break;
376a5779b6eSRui Paulo 	}
377a5779b6eSRui Paulo 
378*ee67461eSJoseph Mingrone 	ND_TCHECK_LEN(TPA(ap), PROTO_LEN(ap));
3794edb46e9SPaul Traina 
380a5779b6eSRui Paulo         if (!ndo->ndo_eflag) {
381*ee67461eSJoseph Mingrone             ND_PRINT("ARP, ");
3824edb46e9SPaul Traina         }
383a5779b6eSRui Paulo 
384a5779b6eSRui Paulo         /* print hardware type/len and proto type/len */
385a5779b6eSRui Paulo         if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
386a5779b6eSRui Paulo 	    PROTO_LEN(ap) != 4 ||
387a5779b6eSRui Paulo             HRD_LEN(ap) == 0 ||
388a5779b6eSRui Paulo             ndo->ndo_vflag) {
389*ee67461eSJoseph Mingrone             ND_PRINT("%s (len %u), %s (len %u)",
390a5779b6eSRui Paulo                       tok2str(arphrd_values, "Unknown Hardware (%u)", hrd),
391a5779b6eSRui Paulo                       HRD_LEN(ap),
392a5779b6eSRui Paulo                       tok2str(ethertype_values, "Unknown Protocol (0x%04x)", pro),
393*ee67461eSJoseph Mingrone                       PROTO_LEN(ap));
394a5779b6eSRui Paulo 
395*ee67461eSJoseph Mingrone             /* don't know about the address formats */
396a5779b6eSRui Paulo             if (!ndo->ndo_vflag) {
397a5779b6eSRui Paulo                 goto out;
398a5779b6eSRui Paulo             }
399a5779b6eSRui Paulo 	}
400a5779b6eSRui Paulo 
401a5779b6eSRui Paulo         /* print operation */
402*ee67461eSJoseph Mingrone         ND_PRINT("%s%s ",
403a5779b6eSRui Paulo                ndo->ndo_vflag ? ", " : "",
404*ee67461eSJoseph Mingrone                tok2str(arpop_values, "Unknown (%u)", op));
405a5779b6eSRui Paulo 
4064edb46e9SPaul Traina 	switch (op) {
4074edb46e9SPaul Traina 
4084edb46e9SPaul Traina 	case ARPOP_REQUEST:
409*ee67461eSJoseph Mingrone 		ND_PRINT("who-has ");
4100bff6a5aSEd Maste 		tpaddr_print_ip(ndo, ap, pro);
411*ee67461eSJoseph Mingrone 		if (isnonzero(ndo, (const u_char *)THA(ap), HRD_LEN(ap)))
412*ee67461eSJoseph Mingrone 			ND_PRINT(" (%s)",
413*ee67461eSJoseph Mingrone 				  GET_LINKADDR_STRING(THA(ap), linkaddr, HRD_LEN(ap)));
414*ee67461eSJoseph Mingrone 		ND_PRINT(" tell ");
4150bff6a5aSEd Maste 		spaddr_print_ip(ndo, ap, pro);
4164edb46e9SPaul Traina 		break;
4174edb46e9SPaul Traina 
4184edb46e9SPaul Traina 	case ARPOP_REPLY:
4190bff6a5aSEd Maste 		spaddr_print_ip(ndo, ap, pro);
420*ee67461eSJoseph Mingrone 		ND_PRINT(" is-at %s",
421*ee67461eSJoseph Mingrone                           GET_LINKADDR_STRING(SHA(ap), linkaddr, HRD_LEN(ap)));
4224edb46e9SPaul Traina 		break;
4234edb46e9SPaul Traina 
424a1c2090eSBill Fenner 	case ARPOP_REVREQUEST:
425*ee67461eSJoseph Mingrone 		/*
426*ee67461eSJoseph Mingrone 		 * XXX - GET_LINKADDR_STRING() may return a pointer to
427*ee67461eSJoseph Mingrone 		 * a static buffer, so we only have one call to it per
428*ee67461eSJoseph Mingrone 		 * ND_PRINT() call.
429*ee67461eSJoseph Mingrone 		 *
430*ee67461eSJoseph Mingrone 		 * This should be done in a cleaner fashion.
431*ee67461eSJoseph Mingrone 		 */
432*ee67461eSJoseph Mingrone 		ND_PRINT("who-is %s",
433*ee67461eSJoseph Mingrone 			  GET_LINKADDR_STRING(THA(ap), linkaddr, HRD_LEN(ap)));
434*ee67461eSJoseph Mingrone 		ND_PRINT(" tell %s",
435*ee67461eSJoseph Mingrone 			  GET_LINKADDR_STRING(SHA(ap), linkaddr, HRD_LEN(ap)));
4364edb46e9SPaul Traina 		break;
4374edb46e9SPaul Traina 
438a1c2090eSBill Fenner 	case ARPOP_REVREPLY:
439*ee67461eSJoseph Mingrone 		ND_PRINT("%s at ",
440*ee67461eSJoseph Mingrone 			  GET_LINKADDR_STRING(THA(ap), linkaddr, HRD_LEN(ap)));
4410bff6a5aSEd Maste 		tpaddr_print_ip(ndo, ap, pro);
4424edb46e9SPaul Traina 		break;
4434edb46e9SPaul Traina 
444cc391cceSBruce M Simpson 	case ARPOP_INVREQUEST:
445*ee67461eSJoseph Mingrone 		/*
446*ee67461eSJoseph Mingrone 		 * XXX - GET_LINKADDR_STRING() may return a pointer to
447*ee67461eSJoseph Mingrone 		 * a static buffer, so we only have one call to it per
448*ee67461eSJoseph Mingrone 		 * ND_PRINT() call.
449*ee67461eSJoseph Mingrone 		 *
450*ee67461eSJoseph Mingrone 		 * This should be done in a cleaner fashion.
451*ee67461eSJoseph Mingrone 		 */
452*ee67461eSJoseph Mingrone 		ND_PRINT("who-is %s",
453*ee67461eSJoseph Mingrone 			  GET_LINKADDR_STRING(THA(ap), linkaddr, HRD_LEN(ap)));
454*ee67461eSJoseph Mingrone 		ND_PRINT(" tell %s",
455*ee67461eSJoseph Mingrone 			  GET_LINKADDR_STRING(SHA(ap), linkaddr, HRD_LEN(ap)));
456cc391cceSBruce M Simpson 		break;
457cc391cceSBruce M Simpson 
458cc391cceSBruce M Simpson 	case ARPOP_INVREPLY:
459*ee67461eSJoseph Mingrone 		ND_PRINT("%s at ",
460*ee67461eSJoseph Mingrone 			  GET_LINKADDR_STRING(SHA(ap), linkaddr, HRD_LEN(ap)));
4610bff6a5aSEd Maste 		spaddr_print_ip(ndo, ap, pro);
462cc391cceSBruce M Simpson 		break;
463cc391cceSBruce M Simpson 
4644edb46e9SPaul Traina 	default:
465c1ad1296SSam Leffler 		ND_DEFAULTPRINT((const u_char *)ap, caplen);
4664edb46e9SPaul Traina 		return;
4674edb46e9SPaul Traina 	}
468a5779b6eSRui Paulo 
469a5779b6eSRui Paulo  out:
470*ee67461eSJoseph Mingrone         ND_PRINT(", length %u", length);
4714edb46e9SPaul Traina }
472