xref: /dflybsd-src/contrib/tcpdump/print-arp.c (revision 59c07fbdf8168fa08c76c515186d561b5a92690c)
141c99275SPeter Avalos /*
241c99275SPeter Avalos  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
341c99275SPeter Avalos  *	The Regents of the University of California.  All rights reserved.
441c99275SPeter Avalos  *
541c99275SPeter Avalos  * Redistribution and use in source and binary forms, with or without
641c99275SPeter Avalos  * modification, are permitted provided that: (1) source code distributions
741c99275SPeter Avalos  * retain the above copyright notice and this paragraph in its entirety, (2)
841c99275SPeter Avalos  * distributions including binary code include the above copyright notice and
941c99275SPeter Avalos  * this paragraph in its entirety in the documentation or other materials
1041c99275SPeter Avalos  * provided with the distribution, and (3) all advertising materials mentioning
1141c99275SPeter Avalos  * features or use of this software display the following acknowledgement:
1241c99275SPeter Avalos  * ``This product includes software developed by the University of California,
1341c99275SPeter Avalos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1441c99275SPeter Avalos  * the University nor the names of its contributors may be used to endorse
1541c99275SPeter Avalos  * or promote products derived from this software without specific prior
1641c99275SPeter Avalos  * written permission.
1741c99275SPeter Avalos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1841c99275SPeter Avalos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1941c99275SPeter Avalos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2041c99275SPeter Avalos  */
2141c99275SPeter Avalos 
22411677aeSAaron LI /* \summary: Address Resolution Protocol (ARP) printer */
2341c99275SPeter Avalos 
2441c99275SPeter Avalos #ifdef HAVE_CONFIG_H
25*ed775ee7SAntonio Huete Jimenez #include <config.h>
2641c99275SPeter Avalos #endif
2741c99275SPeter Avalos 
28*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
2941c99275SPeter Avalos 
30*ed775ee7SAntonio Huete Jimenez #define ND_LONGJMP_FROM_TCHECK
3141c99275SPeter Avalos #include "netdissect.h"
3241c99275SPeter Avalos #include "addrtoname.h"
3341c99275SPeter Avalos #include "ethertype.h"
34411677aeSAaron LI #include "extract.h"
35411677aeSAaron LI 
3641c99275SPeter Avalos 
3741c99275SPeter Avalos /*
3841c99275SPeter Avalos  * Address Resolution Protocol.
3941c99275SPeter Avalos  *
4041c99275SPeter Avalos  * See RFC 826 for protocol description.  ARP packets are variable
4141c99275SPeter Avalos  * in size; the arphdr structure defines the fixed-length portion.
4241c99275SPeter Avalos  * Protocol type values are the same as those for 10 Mb/s Ethernet.
4341c99275SPeter Avalos  * It is followed by the variable-sized fields ar_sha, arp_spa,
4441c99275SPeter Avalos  * arp_tha and arp_tpa in that order, according to the lengths
4541c99275SPeter Avalos  * specified.  Field names used correspond to RFC 826.
4641c99275SPeter Avalos  */
4741c99275SPeter Avalos struct  arp_pkthdr {
48*ed775ee7SAntonio Huete Jimenez         nd_uint16_t ar_hrd;     /* format of hardware address */
4941c99275SPeter Avalos #define ARPHRD_ETHER    1       /* ethernet hardware format */
5041c99275SPeter Avalos #define ARPHRD_IEEE802  6       /* token-ring hardware format */
5141c99275SPeter Avalos #define ARPHRD_ARCNET   7       /* arcnet hardware format */
5241c99275SPeter Avalos #define ARPHRD_FRELAY   15      /* frame relay hardware format */
53ea7b4bf5SPeter Avalos #define ARPHRD_ATM2225  19      /* ATM (RFC 2225) */
5441c99275SPeter Avalos #define ARPHRD_STRIP    23      /* Ricochet Starmode Radio hardware format */
5541c99275SPeter Avalos #define ARPHRD_IEEE1394 24      /* IEEE 1394 (FireWire) hardware format */
56*ed775ee7SAntonio Huete Jimenez #define ARPHRD_INFINIBAND 32    /* InfiniBand RFC 4391 */
57*ed775ee7SAntonio Huete Jimenez         nd_uint16_t ar_pro;     /* format of protocol address */
58*ed775ee7SAntonio Huete Jimenez         nd_uint8_t  ar_hln;     /* length of hardware address */
59*ed775ee7SAntonio Huete Jimenez         nd_uint8_t  ar_pln;     /* length of protocol address */
60*ed775ee7SAntonio Huete Jimenez         nd_uint16_t ar_op;      /* one of: */
6141c99275SPeter Avalos #define ARPOP_REQUEST   1       /* request to resolve address */
6241c99275SPeter Avalos #define ARPOP_REPLY     2       /* response to previous request */
6341c99275SPeter Avalos #define ARPOP_REVREQUEST 3      /* request protocol address given hardware */
6441c99275SPeter Avalos #define ARPOP_REVREPLY  4       /* response giving protocol address */
6541c99275SPeter Avalos #define ARPOP_INVREQUEST 8      /* request to identify peer */
6641c99275SPeter Avalos #define ARPOP_INVREPLY  9       /* response identifying peer */
67ea7b4bf5SPeter Avalos #define ARPOP_NAK       10      /* NAK - only valif for ATM ARP */
68ea7b4bf5SPeter Avalos 
6941c99275SPeter Avalos /*
7041c99275SPeter Avalos  * The remaining fields are variable in size,
7141c99275SPeter Avalos  * according to the sizes above.
7241c99275SPeter Avalos  */
7341c99275SPeter Avalos #ifdef COMMENT_ONLY
74*ed775ee7SAntonio Huete Jimenez 	nd_byte		ar_sha[];	/* sender hardware address */
75*ed775ee7SAntonio Huete Jimenez 	nd_byte		ar_spa[];	/* sender protocol address */
76*ed775ee7SAntonio Huete Jimenez 	nd_byte		ar_tha[];	/* target hardware address */
77*ed775ee7SAntonio Huete Jimenez 	nd_byte		ar_tpa[];	/* target protocol address */
7841c99275SPeter Avalos #endif
7941c99275SPeter Avalos #define ar_sha(ap)	(((const u_char *)((ap)+1))+  0)
80*ed775ee7SAntonio Huete Jimenez #define ar_spa(ap)	(((const u_char *)((ap)+1))+  GET_U_1((ap)->ar_hln))
81*ed775ee7SAntonio Huete Jimenez #define ar_tha(ap)	(((const u_char *)((ap)+1))+  GET_U_1((ap)->ar_hln)+GET_U_1((ap)->ar_pln))
82*ed775ee7SAntonio Huete Jimenez #define ar_tpa(ap)	(((const u_char *)((ap)+1))+2*GET_U_1((ap)->ar_hln)+GET_U_1((ap)->ar_pln))
8341c99275SPeter Avalos };
8441c99275SPeter Avalos 
8541c99275SPeter Avalos #define ARP_HDRLEN	8
8641c99275SPeter Avalos 
87*ed775ee7SAntonio Huete Jimenez #define HRD(ap) GET_BE_U_2((ap)->ar_hrd)
88*ed775ee7SAntonio Huete Jimenez #define HRD_LEN(ap) GET_U_1((ap)->ar_hln)
89*ed775ee7SAntonio Huete Jimenez #define PROTO_LEN(ap) GET_U_1((ap)->ar_pln)
90*ed775ee7SAntonio Huete Jimenez #define OP(ap)  GET_BE_U_2((ap)->ar_op)
91*ed775ee7SAntonio Huete Jimenez #define PRO(ap) GET_BE_U_2((ap)->ar_pro)
9241c99275SPeter Avalos #define SHA(ap) (ar_sha(ap))
9341c99275SPeter Avalos #define SPA(ap) (ar_spa(ap))
9441c99275SPeter Avalos #define THA(ap) (ar_tha(ap))
9541c99275SPeter Avalos #define TPA(ap) (ar_tpa(ap))
9641c99275SPeter Avalos 
97ea7b4bf5SPeter Avalos 
98411677aeSAaron LI static const struct tok arpop_values[] = {
99ea7b4bf5SPeter Avalos     { ARPOP_REQUEST, "Request" },
100ea7b4bf5SPeter Avalos     { ARPOP_REPLY, "Reply" },
101ea7b4bf5SPeter Avalos     { ARPOP_REVREQUEST, "Reverse Request" },
102ea7b4bf5SPeter Avalos     { ARPOP_REVREPLY, "Reverse Reply" },
103ea7b4bf5SPeter Avalos     { ARPOP_INVREQUEST, "Inverse Request" },
104ea7b4bf5SPeter Avalos     { ARPOP_INVREPLY, "Inverse Reply" },
105ea7b4bf5SPeter Avalos     { ARPOP_NAK, "NACK Reply" },
106ea7b4bf5SPeter Avalos     { 0, NULL }
107ea7b4bf5SPeter Avalos };
108ea7b4bf5SPeter Avalos 
109411677aeSAaron LI static const struct tok arphrd_values[] = {
110ea7b4bf5SPeter Avalos     { ARPHRD_ETHER, "Ethernet" },
111ea7b4bf5SPeter Avalos     { ARPHRD_IEEE802, "TokenRing" },
112ea7b4bf5SPeter Avalos     { ARPHRD_ARCNET, "ArcNet" },
113ea7b4bf5SPeter Avalos     { ARPHRD_FRELAY, "FrameRelay" },
114ea7b4bf5SPeter Avalos     { ARPHRD_STRIP, "Strip" },
115ea7b4bf5SPeter Avalos     { ARPHRD_IEEE1394, "IEEE 1394" },
116ea7b4bf5SPeter Avalos     { ARPHRD_ATM2225, "ATM" },
117*ed775ee7SAntonio Huete Jimenez     { ARPHRD_INFINIBAND, "InfiniBand" },
118ea7b4bf5SPeter Avalos     { 0, NULL }
119ea7b4bf5SPeter Avalos };
120ea7b4bf5SPeter Avalos 
12141c99275SPeter Avalos /*
12241c99275SPeter Avalos  * ATM Address Resolution Protocol.
12341c99275SPeter Avalos  *
12441c99275SPeter Avalos  * See RFC 2225 for protocol description.  ATMARP packets are similar
12541c99275SPeter Avalos  * to ARP packets, except that there are no length fields for the
12641c99275SPeter Avalos  * protocol address - instead, there are type/length fields for
12741c99275SPeter Avalos  * the ATM number and subaddress - and the hardware addresses consist
12841c99275SPeter Avalos  * of an ATM number and an ATM subaddress.
12941c99275SPeter Avalos  */
13041c99275SPeter Avalos struct  atmarp_pkthdr {
131*ed775ee7SAntonio Huete Jimenez         nd_uint16_t aar_hrd;    /* format of hardware address */
132*ed775ee7SAntonio Huete Jimenez         nd_uint16_t aar_pro;    /* format of protocol address */
133*ed775ee7SAntonio Huete Jimenez         nd_uint8_t  aar_shtl;   /* length of source ATM number */
134*ed775ee7SAntonio Huete Jimenez         nd_uint8_t  aar_sstl;   /* length of source ATM subaddress */
13541c99275SPeter Avalos #define ATMARP_IS_E164  0x40    /* bit in type/length for E.164 format */
13641c99275SPeter Avalos #define ATMARP_LEN_MASK 0x3F    /* length of {sub}address in type/length */
137*ed775ee7SAntonio Huete Jimenez         nd_uint16_t aar_op;     /* same as regular ARP */
138*ed775ee7SAntonio Huete Jimenez         nd_uint8_t  aar_spln;   /* length of source protocol address */
139*ed775ee7SAntonio Huete Jimenez         nd_uint8_t  aar_thtl;   /* length of target ATM number */
140*ed775ee7SAntonio Huete Jimenez         nd_uint8_t  aar_tstl;   /* length of target ATM subaddress */
141*ed775ee7SAntonio Huete Jimenez         nd_uint8_t  aar_tpln;   /* length of target protocol address */
14241c99275SPeter Avalos /*
14341c99275SPeter Avalos  * The remaining fields are variable in size,
14441c99275SPeter Avalos  * according to the sizes above.
14541c99275SPeter Avalos  */
14641c99275SPeter Avalos #ifdef COMMENT_ONLY
147*ed775ee7SAntonio Huete Jimenez 	nd_byte		aar_sha[];	/* source ATM number */
148*ed775ee7SAntonio Huete Jimenez 	nd_byte		aar_ssa[];	/* source ATM subaddress */
149*ed775ee7SAntonio Huete Jimenez 	nd_byte		aar_spa[];	/* sender protocol address */
150*ed775ee7SAntonio Huete Jimenez 	nd_byte		aar_tha[];	/* target ATM number */
151*ed775ee7SAntonio Huete Jimenez 	nd_byte		aar_tsa[];	/* target ATM subaddress */
152*ed775ee7SAntonio Huete Jimenez 	nd_byte		aar_tpa[];	/* target protocol address */
15341c99275SPeter Avalos #endif
15441c99275SPeter Avalos 
155*ed775ee7SAntonio Huete Jimenez #define ATMHRD(ap)  GET_BE_U_2((ap)->aar_hrd)
156*ed775ee7SAntonio Huete Jimenez #define ATMSHRD_LEN(ap) (GET_U_1((ap)->aar_shtl) & ATMARP_LEN_MASK)
157*ed775ee7SAntonio Huete Jimenez #define ATMSSLN(ap) (GET_U_1((ap)->aar_sstl) & ATMARP_LEN_MASK)
158*ed775ee7SAntonio Huete Jimenez #define ATMSPROTO_LEN(ap) GET_U_1((ap)->aar_spln)
159*ed775ee7SAntonio Huete Jimenez #define ATMOP(ap)   GET_BE_U_2((ap)->aar_op)
160*ed775ee7SAntonio Huete Jimenez #define ATMPRO(ap)  GET_BE_U_2((ap)->aar_pro)
161*ed775ee7SAntonio Huete Jimenez #define ATMTHRD_LEN(ap) (GET_U_1((ap)->aar_thtl) & ATMARP_LEN_MASK)
162*ed775ee7SAntonio Huete Jimenez #define ATMTSLN(ap) (GET_U_1((ap)->aar_tstl) & ATMARP_LEN_MASK)
163*ed775ee7SAntonio Huete Jimenez #define ATMTPROTO_LEN(ap) GET_U_1((ap)->aar_tpln)
16441c99275SPeter Avalos #define aar_sha(ap)	((const u_char *)((ap)+1))
165ea7b4bf5SPeter Avalos #define aar_ssa(ap)	(aar_sha(ap) + ATMSHRD_LEN(ap))
16641c99275SPeter Avalos #define aar_spa(ap)	(aar_ssa(ap) + ATMSSLN(ap))
167ea7b4bf5SPeter Avalos #define aar_tha(ap)	(aar_spa(ap) + ATMSPROTO_LEN(ap))
168ea7b4bf5SPeter Avalos #define aar_tsa(ap)	(aar_tha(ap) + ATMTHRD_LEN(ap))
16941c99275SPeter Avalos #define aar_tpa(ap)	(aar_tsa(ap) + ATMTSLN(ap))
17041c99275SPeter Avalos };
17141c99275SPeter Avalos 
17241c99275SPeter Avalos #define ATMSHA(ap) (aar_sha(ap))
17341c99275SPeter Avalos #define ATMSSA(ap) (aar_ssa(ap))
17441c99275SPeter Avalos #define ATMSPA(ap) (aar_spa(ap))
17541c99275SPeter Avalos #define ATMTHA(ap) (aar_tha(ap))
17641c99275SPeter Avalos #define ATMTSA(ap) (aar_tsa(ap))
17741c99275SPeter Avalos #define ATMTPA(ap) (aar_tpa(ap))
17841c99275SPeter Avalos 
179411677aeSAaron LI static int
isnonzero(netdissect_options * ndo,const u_char * a,size_t len)180*ed775ee7SAntonio Huete Jimenez isnonzero(netdissect_options *ndo, const u_char *a, size_t len)
181411677aeSAaron LI {
182411677aeSAaron LI 	while (len > 0) {
183*ed775ee7SAntonio Huete Jimenez 		if (GET_U_1(a) != 0)
184411677aeSAaron LI 			return (1);
185411677aeSAaron LI 		a++;
186411677aeSAaron LI 		len--;
187411677aeSAaron LI 	}
188411677aeSAaron LI 	return (0);
189411677aeSAaron LI }
190411677aeSAaron LI 
191411677aeSAaron LI static void
tpaddr_print_ip(netdissect_options * ndo,const struct arp_pkthdr * ap,u_short pro)192411677aeSAaron LI tpaddr_print_ip(netdissect_options *ndo,
193411677aeSAaron LI 	        const struct arp_pkthdr *ap, u_short pro)
194411677aeSAaron LI {
195411677aeSAaron LI 	if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
196*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("<wrong proto type>");
197411677aeSAaron LI 	else if (PROTO_LEN(ap) != 4)
198*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("<wrong len>");
199411677aeSAaron LI 	else
200*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("%s", GET_IPADDR_STRING(TPA(ap)));
201411677aeSAaron LI }
202411677aeSAaron LI 
203411677aeSAaron LI static void
spaddr_print_ip(netdissect_options * ndo,const struct arp_pkthdr * ap,u_short pro)204411677aeSAaron LI spaddr_print_ip(netdissect_options *ndo,
205411677aeSAaron LI 	        const struct arp_pkthdr *ap, u_short pro)
206411677aeSAaron LI {
207411677aeSAaron LI 	if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
208*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("<wrong proto type>");
209411677aeSAaron LI 	else if (PROTO_LEN(ap) != 4)
210*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("<wrong len>");
211411677aeSAaron LI 	else
212*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("%s", GET_IPADDR_STRING(SPA(ap)));
213411677aeSAaron LI }
21441c99275SPeter Avalos 
21541c99275SPeter Avalos static void
atmarp_addr_print(netdissect_options * ndo,const u_char * ha,u_int ha_len,const u_char * srca,u_int srca_len)21641c99275SPeter Avalos atmarp_addr_print(netdissect_options *ndo,
21741c99275SPeter Avalos 		  const u_char *ha, u_int ha_len, const u_char *srca,
21841c99275SPeter Avalos     u_int srca_len)
21941c99275SPeter Avalos {
22041c99275SPeter Avalos 	if (ha_len == 0)
221*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("<No address>");
22241c99275SPeter Avalos 	else {
223*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("%s", GET_LINKADDR_STRING(ha, LINKADDR_ATM, ha_len));
22441c99275SPeter Avalos 		if (srca_len != 0)
225*ed775ee7SAntonio Huete Jimenez 			ND_PRINT(",%s",
226*ed775ee7SAntonio Huete Jimenez 				  GET_LINKADDR_STRING(srca, LINKADDR_ATM, srca_len));
22741c99275SPeter Avalos 	}
22841c99275SPeter Avalos }
22941c99275SPeter Avalos 
23041c99275SPeter Avalos static void
atmarp_tpaddr_print(netdissect_options * ndo,const struct atmarp_pkthdr * ap,u_short pro)231411677aeSAaron LI atmarp_tpaddr_print(netdissect_options *ndo,
232411677aeSAaron LI 		    const struct atmarp_pkthdr *ap, u_short pro)
233411677aeSAaron LI {
234411677aeSAaron LI 	if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
235*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("<wrong proto type>");
236411677aeSAaron LI 	else if (ATMTPROTO_LEN(ap) != 4)
237*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("<wrong tplen>");
238411677aeSAaron LI 	else
239*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("%s", GET_IPADDR_STRING(ATMTPA(ap)));
240411677aeSAaron LI }
241411677aeSAaron LI 
242411677aeSAaron LI static void
atmarp_spaddr_print(netdissect_options * ndo,const struct atmarp_pkthdr * ap,u_short pro)243411677aeSAaron LI atmarp_spaddr_print(netdissect_options *ndo,
244411677aeSAaron LI 		    const struct atmarp_pkthdr *ap, u_short pro)
245411677aeSAaron LI {
246411677aeSAaron LI 	if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
247*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("<wrong proto type>");
248411677aeSAaron LI 	else if (ATMSPROTO_LEN(ap) != 4)
249*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("<wrong splen>");
250411677aeSAaron LI 	else
251*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("%s", GET_IPADDR_STRING(ATMSPA(ap)));
252411677aeSAaron LI }
253411677aeSAaron LI 
254411677aeSAaron LI static void
atmarp_print(netdissect_options * ndo,const u_char * bp,u_int length,u_int caplen)25541c99275SPeter Avalos atmarp_print(netdissect_options *ndo,
25641c99275SPeter Avalos 	     const u_char *bp, u_int length, u_int caplen)
25741c99275SPeter Avalos {
25841c99275SPeter Avalos 	const struct atmarp_pkthdr *ap;
25941c99275SPeter Avalos 	u_short pro, hrd, op;
26041c99275SPeter Avalos 
26141c99275SPeter Avalos 	ap = (const struct atmarp_pkthdr *)bp;
262*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_SIZE(ap);
26341c99275SPeter Avalos 
26441c99275SPeter Avalos 	hrd = ATMHRD(ap);
26541c99275SPeter Avalos 	pro = ATMPRO(ap);
26641c99275SPeter Avalos 	op = ATMOP(ap);
26741c99275SPeter Avalos 
268*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_LEN(ATMTPA(ap), ATMTPROTO_LEN(ap));
26941c99275SPeter Avalos 
270ea7b4bf5SPeter Avalos         if (!ndo->ndo_eflag) {
271*ed775ee7SAntonio Huete Jimenez             ND_PRINT("ARP, ");
27241c99275SPeter Avalos         }
273ea7b4bf5SPeter Avalos 
274ea7b4bf5SPeter Avalos 	if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
275ea7b4bf5SPeter Avalos 	    ATMSPROTO_LEN(ap) != 4 ||
276ea7b4bf5SPeter Avalos             ATMTPROTO_LEN(ap) != 4 ||
277ea7b4bf5SPeter Avalos             ndo->ndo_vflag) {
278*ed775ee7SAntonio Huete Jimenez                 ND_PRINT("%s, %s (len %u/%u)",
279ea7b4bf5SPeter Avalos                           tok2str(arphrd_values, "Unknown Hardware (%u)", hrd),
280ea7b4bf5SPeter Avalos                           tok2str(ethertype_values, "Unknown Protocol (0x%04x)", pro),
281ea7b4bf5SPeter Avalos                           ATMSPROTO_LEN(ap),
282*ed775ee7SAntonio Huete Jimenez                           ATMTPROTO_LEN(ap));
283ea7b4bf5SPeter Avalos 
284*ed775ee7SAntonio Huete Jimenez                 /* don't know about the address formats */
285ea7b4bf5SPeter Avalos                 if (!ndo->ndo_vflag) {
286ea7b4bf5SPeter Avalos                     goto out;
287ea7b4bf5SPeter Avalos                 }
288ea7b4bf5SPeter Avalos 	}
289ea7b4bf5SPeter Avalos 
290ea7b4bf5SPeter Avalos         /* print operation */
291*ed775ee7SAntonio Huete Jimenez         ND_PRINT("%s%s ",
292ea7b4bf5SPeter Avalos                ndo->ndo_vflag ? ", " : "",
293*ed775ee7SAntonio Huete Jimenez                tok2str(arpop_values, "Unknown (%u)", op));
294ea7b4bf5SPeter Avalos 
29541c99275SPeter Avalos 	switch (op) {
29641c99275SPeter Avalos 
29741c99275SPeter Avalos 	case ARPOP_REQUEST:
298*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("who-has ");
299411677aeSAaron LI 		atmarp_tpaddr_print(ndo, ap, pro);
300ea7b4bf5SPeter Avalos 		if (ATMTHRD_LEN(ap) != 0) {
301*ed775ee7SAntonio Huete Jimenez 			ND_PRINT(" (");
302ea7b4bf5SPeter Avalos 			atmarp_addr_print(ndo, ATMTHA(ap), ATMTHRD_LEN(ap),
30341c99275SPeter Avalos 			    ATMTSA(ap), ATMTSLN(ap));
304*ed775ee7SAntonio Huete Jimenez 			ND_PRINT(")");
30541c99275SPeter Avalos 		}
306*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" tell ");
307411677aeSAaron LI 		atmarp_spaddr_print(ndo, ap, pro);
30841c99275SPeter Avalos 		break;
30941c99275SPeter Avalos 
31041c99275SPeter Avalos 	case ARPOP_REPLY:
311411677aeSAaron LI 		atmarp_spaddr_print(ndo, ap, pro);
312*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" is-at ");
313ea7b4bf5SPeter Avalos 		atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
31441c99275SPeter Avalos                                   ATMSSLN(ap));
31541c99275SPeter Avalos 		break;
31641c99275SPeter Avalos 
31741c99275SPeter Avalos 	case ARPOP_INVREQUEST:
318*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("who-is ");
319ea7b4bf5SPeter Avalos 		atmarp_addr_print(ndo, ATMTHA(ap), ATMTHRD_LEN(ap), ATMTSA(ap),
32041c99275SPeter Avalos 		    ATMTSLN(ap));
321*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" tell ");
322ea7b4bf5SPeter Avalos 		atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
32341c99275SPeter Avalos 		    ATMSSLN(ap));
32441c99275SPeter Avalos 		break;
32541c99275SPeter Avalos 
32641c99275SPeter Avalos 	case ARPOP_INVREPLY:
327ea7b4bf5SPeter Avalos 		atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
32841c99275SPeter Avalos 		    ATMSSLN(ap));
329*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("at ");
330411677aeSAaron LI 		atmarp_spaddr_print(ndo, ap, pro);
33141c99275SPeter Avalos 		break;
33241c99275SPeter Avalos 
333ea7b4bf5SPeter Avalos 	case ARPOP_NAK:
334*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("for ");
335411677aeSAaron LI 		atmarp_spaddr_print(ndo, ap, pro);
33641c99275SPeter Avalos 		break;
33741c99275SPeter Avalos 
33841c99275SPeter Avalos 	default:
33941c99275SPeter Avalos 		ND_DEFAULTPRINT((const u_char *)ap, caplen);
34041c99275SPeter Avalos 		return;
34141c99275SPeter Avalos 	}
342ea7b4bf5SPeter Avalos 
343ea7b4bf5SPeter Avalos  out:
344*ed775ee7SAntonio Huete Jimenez         ND_PRINT(", length %u", length);
34541c99275SPeter Avalos }
34641c99275SPeter Avalos 
34741c99275SPeter Avalos void
arp_print(netdissect_options * ndo,const u_char * bp,u_int length,u_int caplen)34841c99275SPeter Avalos arp_print(netdissect_options *ndo,
34941c99275SPeter Avalos 	  const u_char *bp, u_int length, u_int caplen)
35041c99275SPeter Avalos {
35141c99275SPeter Avalos 	const struct arp_pkthdr *ap;
352ea7b4bf5SPeter Avalos 	u_short pro, hrd, op, linkaddr;
35341c99275SPeter Avalos 
354*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "arp";
35541c99275SPeter Avalos 	ap = (const struct arp_pkthdr *)bp;
356*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_SIZE(ap);
357ea7b4bf5SPeter Avalos 
35841c99275SPeter Avalos 	hrd = HRD(ap);
35941c99275SPeter Avalos 	pro = PRO(ap);
36041c99275SPeter Avalos 	op = OP(ap);
36141c99275SPeter Avalos 
362ea7b4bf5SPeter Avalos 
363ea7b4bf5SPeter Avalos         /* if its ATM then call the ATM ARP printer
364ea7b4bf5SPeter Avalos            for Frame-relay ARP most of the fields
365ea7b4bf5SPeter Avalos            are similar to Ethernet so overload the Ethernet Printer
366*ed775ee7SAntonio Huete Jimenez            and set the linkaddr type for GET_LINKADDR_STRING() accordingly */
367ea7b4bf5SPeter Avalos 
368ea7b4bf5SPeter Avalos         switch(hrd) {
369ea7b4bf5SPeter Avalos         case ARPHRD_ATM2225:
370ea7b4bf5SPeter Avalos             atmarp_print(ndo, bp, length, caplen);
371ea7b4bf5SPeter Avalos             return;
372ea7b4bf5SPeter Avalos         case ARPHRD_FRELAY:
373ea7b4bf5SPeter Avalos             linkaddr = LINKADDR_FRELAY;
37427bfbee1SPeter Avalos             break;
375ea7b4bf5SPeter Avalos         default:
376ea7b4bf5SPeter Avalos             linkaddr = LINKADDR_ETHER;
377ea7b4bf5SPeter Avalos             break;
378ea7b4bf5SPeter Avalos 	}
379ea7b4bf5SPeter Avalos 
380*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_LEN(TPA(ap), PROTO_LEN(ap));
38141c99275SPeter Avalos 
382ea7b4bf5SPeter Avalos         if (!ndo->ndo_eflag) {
383*ed775ee7SAntonio Huete Jimenez             ND_PRINT("ARP, ");
38441c99275SPeter Avalos         }
385ea7b4bf5SPeter Avalos 
386ea7b4bf5SPeter Avalos         /* print hardware type/len and proto type/len */
387ea7b4bf5SPeter Avalos         if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
388ea7b4bf5SPeter Avalos 	    PROTO_LEN(ap) != 4 ||
389ea7b4bf5SPeter Avalos             HRD_LEN(ap) == 0 ||
390ea7b4bf5SPeter Avalos             ndo->ndo_vflag) {
391*ed775ee7SAntonio Huete Jimenez             ND_PRINT("%s (len %u), %s (len %u)",
392ea7b4bf5SPeter Avalos                       tok2str(arphrd_values, "Unknown Hardware (%u)", hrd),
393ea7b4bf5SPeter Avalos                       HRD_LEN(ap),
394ea7b4bf5SPeter Avalos                       tok2str(ethertype_values, "Unknown Protocol (0x%04x)", pro),
395*ed775ee7SAntonio Huete Jimenez                       PROTO_LEN(ap));
396ea7b4bf5SPeter Avalos 
397*ed775ee7SAntonio Huete Jimenez             /* don't know about the address formats */
398ea7b4bf5SPeter Avalos             if (!ndo->ndo_vflag) {
399ea7b4bf5SPeter Avalos                 goto out;
400ea7b4bf5SPeter Avalos             }
401ea7b4bf5SPeter Avalos 	}
402ea7b4bf5SPeter Avalos 
403ea7b4bf5SPeter Avalos         /* print operation */
404*ed775ee7SAntonio Huete Jimenez         ND_PRINT("%s%s ",
405ea7b4bf5SPeter Avalos                ndo->ndo_vflag ? ", " : "",
406*ed775ee7SAntonio Huete Jimenez                tok2str(arpop_values, "Unknown (%u)", op));
407ea7b4bf5SPeter Avalos 
40841c99275SPeter Avalos 	switch (op) {
40941c99275SPeter Avalos 
41041c99275SPeter Avalos 	case ARPOP_REQUEST:
411*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("who-has ");
412411677aeSAaron LI 		tpaddr_print_ip(ndo, ap, pro);
413*ed775ee7SAntonio Huete Jimenez 		if (isnonzero(ndo, (const u_char *)THA(ap), HRD_LEN(ap)))
414*ed775ee7SAntonio Huete Jimenez 			ND_PRINT(" (%s)",
415*ed775ee7SAntonio Huete Jimenez 				  GET_LINKADDR_STRING(THA(ap), linkaddr, HRD_LEN(ap)));
416*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" tell ");
417411677aeSAaron LI 		spaddr_print_ip(ndo, ap, pro);
41841c99275SPeter Avalos 		break;
41941c99275SPeter Avalos 
42041c99275SPeter Avalos 	case ARPOP_REPLY:
421411677aeSAaron LI 		spaddr_print_ip(ndo, ap, pro);
422*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" is-at %s",
423*ed775ee7SAntonio Huete Jimenez                           GET_LINKADDR_STRING(SHA(ap), linkaddr, HRD_LEN(ap)));
42441c99275SPeter Avalos 		break;
42541c99275SPeter Avalos 
42641c99275SPeter Avalos 	case ARPOP_REVREQUEST:
427*ed775ee7SAntonio Huete Jimenez 		/*
428*ed775ee7SAntonio Huete Jimenez 		 * XXX - GET_LINKADDR_STRING() may return a pointer to
429*ed775ee7SAntonio Huete Jimenez 		 * a static buffer, so we only have one call to it per
430*ed775ee7SAntonio Huete Jimenez 		 * ND_PRINT() call.
431*ed775ee7SAntonio Huete Jimenez 		 *
432*ed775ee7SAntonio Huete Jimenez 		 * This should be done in a cleaner fashion.
433*ed775ee7SAntonio Huete Jimenez 		 */
434*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("who-is %s",
435*ed775ee7SAntonio Huete Jimenez 			  GET_LINKADDR_STRING(THA(ap), linkaddr, HRD_LEN(ap)));
436*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" tell %s",
437*ed775ee7SAntonio Huete Jimenez 			  GET_LINKADDR_STRING(SHA(ap), linkaddr, HRD_LEN(ap)));
43841c99275SPeter Avalos 		break;
43941c99275SPeter Avalos 
44041c99275SPeter Avalos 	case ARPOP_REVREPLY:
441*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("%s at ",
442*ed775ee7SAntonio Huete Jimenez 			  GET_LINKADDR_STRING(THA(ap), linkaddr, HRD_LEN(ap)));
443411677aeSAaron LI 		tpaddr_print_ip(ndo, ap, pro);
44441c99275SPeter Avalos 		break;
44541c99275SPeter Avalos 
44641c99275SPeter Avalos 	case ARPOP_INVREQUEST:
447*ed775ee7SAntonio Huete Jimenez 		/*
448*ed775ee7SAntonio Huete Jimenez 		 * XXX - GET_LINKADDR_STRING() may return a pointer to
449*ed775ee7SAntonio Huete Jimenez 		 * a static buffer, so we only have one call to it per
450*ed775ee7SAntonio Huete Jimenez 		 * ND_PRINT() call.
451*ed775ee7SAntonio Huete Jimenez 		 *
452*ed775ee7SAntonio Huete Jimenez 		 * This should be done in a cleaner fashion.
453*ed775ee7SAntonio Huete Jimenez 		 */
454*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("who-is %s",
455*ed775ee7SAntonio Huete Jimenez 			  GET_LINKADDR_STRING(THA(ap), linkaddr, HRD_LEN(ap)));
456*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" tell %s",
457*ed775ee7SAntonio Huete Jimenez 			  GET_LINKADDR_STRING(SHA(ap), linkaddr, HRD_LEN(ap)));
45841c99275SPeter Avalos 		break;
45941c99275SPeter Avalos 
46041c99275SPeter Avalos 	case ARPOP_INVREPLY:
461*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("%s at ",
462*ed775ee7SAntonio Huete Jimenez 			  GET_LINKADDR_STRING(SHA(ap), linkaddr, HRD_LEN(ap)));
463411677aeSAaron LI 		spaddr_print_ip(ndo, ap, pro);
46441c99275SPeter Avalos 		break;
46541c99275SPeter Avalos 
46641c99275SPeter Avalos 	default:
46741c99275SPeter Avalos 		ND_DEFAULTPRINT((const u_char *)ap, caplen);
46841c99275SPeter Avalos 		return;
46941c99275SPeter Avalos 	}
470ea7b4bf5SPeter Avalos 
471ea7b4bf5SPeter Avalos  out:
472*ed775ee7SAntonio Huete Jimenez         ND_PRINT(", length %u", length);
47341c99275SPeter Avalos }
474