2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 #include <sys/cdefs.h>
24 __RCSID("$NetBSD: print-arp.c,v 1.6 2015/03/31 21:59:35 christos Exp $");
27 #define NETDISSECT_REWORKED
32 #include <tcpdump-stdinc.h>
36 #include "interface.h"
37 #include "addrtoname.h"
39 #include "ethertype.h"
40 #include "extract.h" /* must come after interface.h */
42 static const char tstr
[] = "[|ARP]";
45 * Address Resolution Protocol.
47 * See RFC 826 for protocol description. ARP packets are variable
48 * in size; the arphdr structure defines the fixed-length portion.
49 * Protocol type values are the same as those for 10 Mb/s Ethernet.
50 * It is followed by the variable-sized fields ar_sha, arp_spa,
51 * arp_tha and arp_tpa in that order, according to the lengths
52 * specified. Field names used correspond to RFC 826.
55 u_short ar_hrd
; /* format of hardware address */
56 #define ARPHRD_ETHER 1 /* ethernet hardware format */
57 #define ARPHRD_IEEE802 6 /* token-ring hardware format */
58 #define ARPHRD_ARCNET 7 /* arcnet hardware format */
59 #define ARPHRD_FRELAY 15 /* frame relay hardware format */
60 #define ARPHRD_ATM2225 19 /* ATM (RFC 2225) */
61 #define ARPHRD_STRIP 23 /* Ricochet Starmode Radio hardware format */
62 #define ARPHRD_IEEE1394 24 /* IEEE 1394 (FireWire) hardware format */
63 u_short ar_pro
; /* format of protocol address */
64 u_char ar_hln
; /* length of hardware address */
65 u_char ar_pln
; /* length of protocol address */
66 u_short ar_op
; /* one of: */
67 #define ARPOP_REQUEST 1 /* request to resolve address */
68 #define ARPOP_REPLY 2 /* response to previous request */
69 #define ARPOP_REVREQUEST 3 /* request protocol address given hardware */
70 #define ARPOP_REVREPLY 4 /* response giving protocol address */
71 #define ARPOP_INVREQUEST 8 /* request to identify peer */
72 #define ARPOP_INVREPLY 9 /* response identifying peer */
73 #define ARPOP_NAK 10 /* NAK - only valif for ATM ARP */
76 * The remaining fields are variable in size,
77 * according to the sizes above.
80 u_char ar_sha
[]; /* sender hardware address */
81 u_char ar_spa
[]; /* sender protocol address */
82 u_char ar_tha
[]; /* target hardware address */
83 u_char ar_tpa
[]; /* target protocol address */
85 #define ar_sha(ap) (((const u_char *)((ap)+1))+0)
86 #define ar_spa(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln)
87 #define ar_tha(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln+(ap)->ar_pln)
88 #define ar_tpa(ap) (((const u_char *)((ap)+1))+2*(ap)->ar_hln+(ap)->ar_pln)
93 #define HRD(ap) EXTRACT_16BITS(&(ap)->ar_hrd)
94 #define HRD_LEN(ap) ((ap)->ar_hln)
95 #define PROTO_LEN(ap) ((ap)->ar_pln)
96 #define OP(ap) EXTRACT_16BITS(&(ap)->ar_op)
97 #define PRO(ap) EXTRACT_16BITS(&(ap)->ar_pro)
98 #define SHA(ap) (ar_sha(ap))
99 #define SPA(ap) (ar_spa(ap))
100 #define THA(ap) (ar_tha(ap))
101 #define TPA(ap) (ar_tpa(ap))
104 static const struct tok arpop_values
[] = {
105 { ARPOP_REQUEST
, "Request" },
106 { ARPOP_REPLY
, "Reply" },
107 { ARPOP_REVREQUEST
, "Reverse Request" },
108 { ARPOP_REVREPLY
, "Reverse Reply" },
109 { ARPOP_INVREQUEST
, "Inverse Request" },
110 { ARPOP_INVREPLY
, "Inverse Reply" },
111 { ARPOP_NAK
, "NACK Reply" },
115 static const struct tok arphrd_values
[] = {
116 { ARPHRD_ETHER
, "Ethernet" },
117 { ARPHRD_IEEE802
, "TokenRing" },
118 { ARPHRD_ARCNET
, "ArcNet" },
119 { ARPHRD_FRELAY
, "FrameRelay" },
120 { ARPHRD_STRIP
, "Strip" },
121 { ARPHRD_IEEE1394
, "IEEE 1394" },
122 { ARPHRD_ATM2225
, "ATM" },
127 * ATM Address Resolution Protocol.
129 * See RFC 2225 for protocol description. ATMARP packets are similar
130 * to ARP packets, except that there are no length fields for the
131 * protocol address - instead, there are type/length fields for
132 * the ATM number and subaddress - and the hardware addresses consist
133 * of an ATM number and an ATM subaddress.
135 struct atmarp_pkthdr
{
136 u_short aar_hrd
; /* format of hardware address */
137 u_short aar_pro
; /* format of protocol address */
138 u_char aar_shtl
; /* length of source ATM number */
139 u_char aar_sstl
; /* length of source ATM subaddress */
140 #define ATMARP_IS_E164 0x40 /* bit in type/length for E.164 format */
141 #define ATMARP_LEN_MASK 0x3F /* length of {sub}address in type/length */
142 u_short aar_op
; /* same as regular ARP */
143 u_char aar_spln
; /* length of source protocol address */
144 u_char aar_thtl
; /* length of target ATM number */
145 u_char aar_tstl
; /* length of target ATM subaddress */
146 u_char aar_tpln
; /* length of target protocol address */
148 * The remaining fields are variable in size,
149 * according to the sizes above.
152 u_char aar_sha
[]; /* source ATM number */
153 u_char aar_ssa
[]; /* source ATM subaddress */
154 u_char aar_spa
[]; /* sender protocol address */
155 u_char aar_tha
[]; /* target ATM number */
156 u_char aar_tsa
[]; /* target ATM subaddress */
157 u_char aar_tpa
[]; /* target protocol address */
160 #define ATMHRD(ap) EXTRACT_16BITS(&(ap)->aar_hrd)
161 #define ATMSHRD_LEN(ap) ((ap)->aar_shtl & ATMARP_LEN_MASK)
162 #define ATMSSLN(ap) ((ap)->aar_sstl & ATMARP_LEN_MASK)
163 #define ATMSPROTO_LEN(ap) ((ap)->aar_spln)
164 #define ATMOP(ap) EXTRACT_16BITS(&(ap)->aar_op)
165 #define ATMPRO(ap) EXTRACT_16BITS(&(ap)->aar_pro)
166 #define ATMTHRD_LEN(ap) ((ap)->aar_thtl & ATMARP_LEN_MASK)
167 #define ATMTSLN(ap) ((ap)->aar_tstl & ATMARP_LEN_MASK)
168 #define ATMTPROTO_LEN(ap) ((ap)->aar_tpln)
169 #define aar_sha(ap) ((const u_char *)((ap)+1))
170 #define aar_ssa(ap) (aar_sha(ap) + ATMSHRD_LEN(ap))
171 #define aar_spa(ap) (aar_ssa(ap) + ATMSSLN(ap))
172 #define aar_tha(ap) (aar_spa(ap) + ATMSPROTO_LEN(ap))
173 #define aar_tsa(ap) (aar_tha(ap) + ATMTHRD_LEN(ap))
174 #define aar_tpa(ap) (aar_tsa(ap) + ATMTSLN(ap))
177 #define ATMSHA(ap) (aar_sha(ap))
178 #define ATMSSA(ap) (aar_ssa(ap))
179 #define ATMSPA(ap) (aar_spa(ap))
180 #define ATMTHA(ap) (aar_tha(ap))
181 #define ATMTSA(ap) (aar_tsa(ap))
182 #define ATMTPA(ap) (aar_tpa(ap))
184 static u_char ezero
[6];
187 atmarp_addr_print(netdissect_options
*ndo
,
188 const u_char
*ha
, u_int ha_len
, const u_char
*srca
,
192 ND_PRINT((ndo
, "<No address>"));
194 ND_PRINT((ndo
, "%s", linkaddr_string(ndo
, ha
, LINKADDR_ATM
, ha_len
)));
196 ND_PRINT((ndo
, ",%s",
197 linkaddr_string(ndo
, srca
, LINKADDR_ATM
, srca_len
)));
202 atmarp_print(netdissect_options
*ndo
,
203 const u_char
*bp
, u_int length
, u_int caplen
)
205 const struct atmarp_pkthdr
*ap
;
206 u_short pro
, hrd
, op
;
208 ap
= (const struct atmarp_pkthdr
*)bp
;
215 if (!ND_TTEST2(*aar_tpa(ap
), ATMTPROTO_LEN(ap
))) {
216 ND_PRINT((ndo
, "%s", tstr
));
217 ND_DEFAULTPRINT((const u_char
*)ap
, length
);
221 if (!ndo
->ndo_eflag
) {
222 ND_PRINT((ndo
, "ARP, "));
225 if ((pro
!= ETHERTYPE_IP
&& pro
!= ETHERTYPE_TRAIL
) ||
226 ATMSPROTO_LEN(ap
) != 4 ||
227 ATMTPROTO_LEN(ap
) != 4 ||
229 ND_PRINT((ndo
, "%s, %s (len %u/%u)",
230 tok2str(arphrd_values
, "Unknown Hardware (%u)", hrd
),
231 tok2str(ethertype_values
, "Unknown Protocol (0x%04x)", pro
),
235 /* don't know know about the address formats */
236 if (!ndo
->ndo_vflag
) {
241 /* print operation */
242 ND_PRINT((ndo
, "%s%s ",
243 ndo
->ndo_vflag
? ", " : "",
244 tok2str(arpop_values
, "Unknown (%u)", op
)));
249 ND_PRINT((ndo
, "who-has %s", ipaddr_string(ndo
, ATMTPA(ap
))));
250 if (ATMTHRD_LEN(ap
) != 0) {
251 ND_PRINT((ndo
, " ("));
252 atmarp_addr_print(ndo
, ATMTHA(ap
), ATMTHRD_LEN(ap
),
253 ATMTSA(ap
), ATMTSLN(ap
));
254 ND_PRINT((ndo
, ")"));
256 ND_PRINT((ndo
, "tell %s", ipaddr_string(ndo
, ATMSPA(ap
))));
260 ND_PRINT((ndo
, "%s is-at ", ipaddr_string(ndo
, ATMSPA(ap
))));
261 atmarp_addr_print(ndo
, ATMSHA(ap
), ATMSHRD_LEN(ap
), ATMSSA(ap
),
265 case ARPOP_INVREQUEST
:
266 ND_PRINT((ndo
, "who-is "));
267 atmarp_addr_print(ndo
, ATMTHA(ap
), ATMTHRD_LEN(ap
), ATMTSA(ap
),
269 ND_PRINT((ndo
, " tell "));
270 atmarp_addr_print(ndo
, ATMSHA(ap
), ATMSHRD_LEN(ap
), ATMSSA(ap
),
275 atmarp_addr_print(ndo
, ATMSHA(ap
), ATMSHRD_LEN(ap
), ATMSSA(ap
),
277 ND_PRINT((ndo
, "at %s", ipaddr_string(ndo
, ATMSPA(ap
))));
281 ND_PRINT((ndo
, "for %s", ipaddr_string(ndo
, ATMSPA(ap
))));
285 ND_DEFAULTPRINT((const u_char
*)ap
, caplen
);
290 ND_PRINT((ndo
, ", length %u", length
));
294 ND_PRINT((ndo
, "%s", tstr
));
298 arp_print(netdissect_options
*ndo
,
299 const u_char
*bp
, u_int length
, u_int caplen
)
301 const struct arp_pkthdr
*ap
;
302 u_short pro
, hrd
, op
, linkaddr
;
304 ap
= (const struct arp_pkthdr
*)bp
;
312 /* if its ATM then call the ATM ARP printer
313 for Frame-relay ARP most of the fields
314 are similar to Ethernet so overload the Ethernet Printer
315 and set the linkaddr type for linkaddr_string(ndo, ) accordingly */
319 atmarp_print(ndo
, bp
, length
, caplen
);
322 linkaddr
= LINKADDR_FRELAY
;
325 linkaddr
= LINKADDR_ETHER
;
329 if (!ND_TTEST2(*ar_tpa(ap
), PROTO_LEN(ap
))) {
330 ND_PRINT((ndo
, "%s", tstr
));
331 ND_DEFAULTPRINT((const u_char
*)ap
, length
);
335 if (!ndo
->ndo_eflag
) {
336 ND_PRINT((ndo
, "ARP, "));
339 /* print hardware type/len and proto type/len */
340 if ((pro
!= ETHERTYPE_IP
&& pro
!= ETHERTYPE_TRAIL
) ||
341 PROTO_LEN(ap
) != 4 ||
344 ND_PRINT((ndo
, "%s (len %u), %s (len %u)",
345 tok2str(arphrd_values
, "Unknown Hardware (%u)", hrd
),
347 tok2str(ethertype_values
, "Unknown Protocol (0x%04x)", pro
),
350 /* don't know know about the address formats */
351 if (!ndo
->ndo_vflag
) {
356 /* print operation */
357 ND_PRINT((ndo
, "%s%s ",
358 ndo
->ndo_vflag
? ", " : "",
359 tok2str(arpop_values
, "Unknown (%u)", op
)));
364 ND_PRINT((ndo
, "who-has %s", ipaddr_string(ndo
, TPA(ap
))));
365 if (memcmp((const char *)ezero
, (const char *)THA(ap
), HRD_LEN(ap
)) != 0)
366 ND_PRINT((ndo
, " (%s)",
367 linkaddr_string(ndo
, THA(ap
), linkaddr
, HRD_LEN(ap
))));
368 ND_PRINT((ndo
, " tell %s", ipaddr_string(ndo
, SPA(ap
))));
372 ND_PRINT((ndo
, "%s is-at %s",
373 ipaddr_string(ndo
, SPA(ap
)),
374 linkaddr_string(ndo
, SHA(ap
), linkaddr
, HRD_LEN(ap
))));
377 case ARPOP_REVREQUEST
:
378 ND_PRINT((ndo
, "who-is %s tell %s",
379 linkaddr_string(ndo
, THA(ap
), linkaddr
, HRD_LEN(ap
)),
380 linkaddr_string(ndo
, SHA(ap
), linkaddr
, HRD_LEN(ap
))));
384 ND_PRINT((ndo
, "%s at %s",
385 linkaddr_string(ndo
, THA(ap
), linkaddr
, HRD_LEN(ap
)),
386 ipaddr_string(ndo
, TPA(ap
))));
389 case ARPOP_INVREQUEST
:
390 ND_PRINT((ndo
, "who-is %s tell %s",
391 linkaddr_string(ndo
, THA(ap
), linkaddr
, HRD_LEN(ap
)),
392 linkaddr_string(ndo
, SHA(ap
), linkaddr
, HRD_LEN(ap
))));
396 ND_PRINT((ndo
,"%s at %s",
397 linkaddr_string(ndo
, SHA(ap
), linkaddr
, HRD_LEN(ap
)),
398 ipaddr_string(ndo
, SPA(ap
))));
402 ND_DEFAULTPRINT((const u_char
*)ap
, caplen
);
407 ND_PRINT((ndo
, ", length %u", length
));
411 ND_PRINT((ndo
, "%s", tstr
));