]>
The Tcpdump Group git mirrors - tcpdump/blob - print-arp.c
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 /* \summary: Address Resolution Protocol (ARP) printer */
26 #include "netdissect-stdinc.h"
28 #define ND_LONGJMP_FROM_TCHECK
29 #include "netdissect.h"
30 #include "addrtoname.h"
31 #include "ethertype.h"
36 * Address Resolution Protocol.
38 * See RFC 826 for protocol description. ARP packets are variable
39 * in size; the arphdr structure defines the fixed-length portion.
40 * Protocol type values are the same as those for 10 Mb/s Ethernet.
41 * It is followed by the variable-sized fields ar_sha, arp_spa,
42 * arp_tha and arp_tpa in that order, according to the lengths
43 * specified. Field names used correspond to RFC 826.
46 nd_uint16_t ar_hrd
; /* format of hardware address */
47 #define ARPHRD_ETHER 1 /* ethernet hardware format */
48 #define ARPHRD_IEEE802 6 /* token-ring hardware format */
49 #define ARPHRD_ARCNET 7 /* arcnet hardware format */
50 #define ARPHRD_FRELAY 15 /* frame relay hardware format */
51 #define ARPHRD_ATM2225 19 /* ATM (RFC 2225) */
52 #define ARPHRD_STRIP 23 /* Ricochet Starmode Radio hardware format */
53 #define ARPHRD_IEEE1394 24 /* IEEE 1394 (FireWire) hardware format */
54 #define ARPHRD_INFINIBAND 32 /* InfiniBand RFC 4391 */
55 nd_uint16_t ar_pro
; /* format of protocol address */
56 nd_uint8_t ar_hln
; /* length of hardware address */
57 nd_uint8_t ar_pln
; /* length of protocol address */
58 nd_uint16_t ar_op
; /* one of: */
59 #define ARPOP_REQUEST 1 /* request to resolve address */
60 #define ARPOP_REPLY 2 /* response to previous request */
61 #define ARPOP_REVREQUEST 3 /* request protocol address given hardware */
62 #define ARPOP_REVREPLY 4 /* response giving protocol address */
63 #define ARPOP_INVREQUEST 8 /* request to identify peer */
64 #define ARPOP_INVREPLY 9 /* response identifying peer */
65 #define ARPOP_NAK 10 /* NAK - only valid for ATM ARP */
68 * The remaining fields are variable in size,
69 * according to the sizes above.
72 nd_byte ar_sha
[]; /* sender hardware address */
73 nd_byte ar_spa
[]; /* sender protocol address */
74 nd_byte ar_tha
[]; /* target hardware address */
75 nd_byte ar_tpa
[]; /* target protocol address */
77 #define ar_sha(ap) (((const u_char *)((ap)+1))+ 0)
78 #define ar_spa(ap) (((const u_char *)((ap)+1))+ GET_U_1((ap)->ar_hln))
79 #define ar_tha(ap) (((const u_char *)((ap)+1))+ GET_U_1((ap)->ar_hln)+GET_U_1((ap)->ar_pln))
80 #define ar_tpa(ap) (((const u_char *)((ap)+1))+2*GET_U_1((ap)->ar_hln)+GET_U_1((ap)->ar_pln))
85 #define HRD(ap) GET_BE_U_2((ap)->ar_hrd)
86 #define HRD_LEN(ap) GET_U_1((ap)->ar_hln)
87 #define PROTO_LEN(ap) GET_U_1((ap)->ar_pln)
88 #define OP(ap) GET_BE_U_2((ap)->ar_op)
89 #define PRO(ap) GET_BE_U_2((ap)->ar_pro)
90 #define SHA(ap) (ar_sha(ap))
91 #define SPA(ap) (ar_spa(ap))
92 #define THA(ap) (ar_tha(ap))
93 #define TPA(ap) (ar_tpa(ap))
96 static const struct tok arpop_values
[] = {
97 { ARPOP_REQUEST
, "Request" },
98 { ARPOP_REPLY
, "Reply" },
99 { ARPOP_REVREQUEST
, "Reverse Request" },
100 { ARPOP_REVREPLY
, "Reverse Reply" },
101 { ARPOP_INVREQUEST
, "Inverse Request" },
102 { ARPOP_INVREPLY
, "Inverse Reply" },
103 { ARPOP_NAK
, "NACK Reply" },
107 static const struct tok arphrd_values
[] = {
108 { ARPHRD_ETHER
, "Ethernet" },
109 { ARPHRD_IEEE802
, "TokenRing" },
110 { ARPHRD_ARCNET
, "ArcNet" },
111 { ARPHRD_FRELAY
, "FrameRelay" },
112 { ARPHRD_STRIP
, "Strip" },
113 { ARPHRD_IEEE1394
, "IEEE 1394" },
114 { ARPHRD_ATM2225
, "ATM" },
115 { ARPHRD_INFINIBAND
, "InfiniBand" },
120 * ATM Address Resolution Protocol.
122 * See RFC 2225 for protocol description. ATMARP packets are similar
123 * to ARP packets, except that there are no length fields for the
124 * protocol address - instead, there are type/length fields for
125 * the ATM number and subaddress - and the hardware addresses consist
126 * of an ATM number and an ATM subaddress.
128 struct atmarp_pkthdr
{
129 nd_uint16_t aar_hrd
; /* format of hardware address */
130 nd_uint16_t aar_pro
; /* format of protocol address */
131 nd_uint8_t aar_shtl
; /* length of source ATM number */
132 nd_uint8_t aar_sstl
; /* length of source ATM subaddress */
133 #define ATMARP_IS_E164 0x40 /* bit in type/length for E.164 format */
134 #define ATMARP_LEN_MASK 0x3F /* length of {sub}address in type/length */
135 nd_uint16_t aar_op
; /* same as regular ARP */
136 nd_uint8_t aar_spln
; /* length of source protocol address */
137 nd_uint8_t aar_thtl
; /* length of target ATM number */
138 nd_uint8_t aar_tstl
; /* length of target ATM subaddress */
139 nd_uint8_t aar_tpln
; /* length of target protocol address */
141 * The remaining fields are variable in size,
142 * according to the sizes above.
145 nd_byte aar_sha
[]; /* source ATM number */
146 nd_byte aar_ssa
[]; /* source ATM subaddress */
147 nd_byte aar_spa
[]; /* sender protocol address */
148 nd_byte aar_tha
[]; /* target ATM number */
149 nd_byte aar_tsa
[]; /* target ATM subaddress */
150 nd_byte aar_tpa
[]; /* target protocol address */
153 #define ATMHRD(ap) GET_BE_U_2((ap)->aar_hrd)
154 #define ATMSHRD_LEN(ap) (GET_U_1((ap)->aar_shtl) & ATMARP_LEN_MASK)
155 #define ATMSSLN(ap) (GET_U_1((ap)->aar_sstl) & ATMARP_LEN_MASK)
156 #define ATMSPROTO_LEN(ap) GET_U_1((ap)->aar_spln)
157 #define ATMOP(ap) GET_BE_U_2((ap)->aar_op)
158 #define ATMPRO(ap) GET_BE_U_2((ap)->aar_pro)
159 #define ATMTHRD_LEN(ap) (GET_U_1((ap)->aar_thtl) & ATMARP_LEN_MASK)
160 #define ATMTSLN(ap) (GET_U_1((ap)->aar_tstl) & ATMARP_LEN_MASK)
161 #define ATMTPROTO_LEN(ap) GET_U_1((ap)->aar_tpln)
162 #define aar_sha(ap) ((const u_char *)((ap)+1))
163 #define aar_ssa(ap) (aar_sha(ap) + ATMSHRD_LEN(ap))
164 #define aar_spa(ap) (aar_ssa(ap) + ATMSSLN(ap))
165 #define aar_tha(ap) (aar_spa(ap) + ATMSPROTO_LEN(ap))
166 #define aar_tsa(ap) (aar_tha(ap) + ATMTHRD_LEN(ap))
167 #define aar_tpa(ap) (aar_tsa(ap) + ATMTSLN(ap))
170 #define ATMSHA(ap) (aar_sha(ap))
171 #define ATMSSA(ap) (aar_ssa(ap))
172 #define ATMSPA(ap) (aar_spa(ap))
173 #define ATMTHA(ap) (aar_tha(ap))
174 #define ATMTSA(ap) (aar_tsa(ap))
175 #define ATMTPA(ap) (aar_tpa(ap))
178 isnonzero(netdissect_options
*ndo
, const u_char
*a
, size_t len
)
190 tpaddr_print_ip(netdissect_options
*ndo
,
191 const struct arp_pkthdr
*ap
, u_short pro
)
193 if (pro
!= ETHERTYPE_IP
&& pro
!= ETHERTYPE_TRAIL
)
194 ND_PRINT("<wrong proto type>");
195 else if (PROTO_LEN(ap
) != 4)
196 ND_PRINT("<wrong len>");
198 ND_PRINT("%s", GET_IPADDR_STRING(TPA(ap
)));
202 spaddr_print_ip(netdissect_options
*ndo
,
203 const struct arp_pkthdr
*ap
, u_short pro
)
205 if (pro
!= ETHERTYPE_IP
&& pro
!= ETHERTYPE_TRAIL
)
206 ND_PRINT("<wrong proto type>");
207 else if (PROTO_LEN(ap
) != 4)
208 ND_PRINT("<wrong len>");
210 ND_PRINT("%s", GET_IPADDR_STRING(SPA(ap
)));
214 atmarp_addr_print(netdissect_options
*ndo
,
215 const u_char
*ha
, u_int ha_len
, const u_char
*srca
,
219 ND_PRINT("<No address>");
221 ND_PRINT("%s", GET_LINKADDR_STRING(ha
, LINKADDR_ATM
, ha_len
));
224 GET_LINKADDR_STRING(srca
, LINKADDR_ATM
, srca_len
));
229 atmarp_tpaddr_print(netdissect_options
*ndo
,
230 const struct atmarp_pkthdr
*ap
, u_short pro
)
232 if (pro
!= ETHERTYPE_IP
&& pro
!= ETHERTYPE_TRAIL
)
233 ND_PRINT("<wrong proto type>");
234 else if (ATMTPROTO_LEN(ap
) != 4)
235 ND_PRINT("<wrong tplen>");
237 ND_PRINT("%s", GET_IPADDR_STRING(ATMTPA(ap
)));
241 atmarp_spaddr_print(netdissect_options
*ndo
,
242 const struct atmarp_pkthdr
*ap
, u_short pro
)
244 if (pro
!= ETHERTYPE_IP
&& pro
!= ETHERTYPE_TRAIL
)
245 ND_PRINT("<wrong proto type>");
246 else if (ATMSPROTO_LEN(ap
) != 4)
247 ND_PRINT("<wrong splen>");
249 ND_PRINT("%s", GET_IPADDR_STRING(ATMSPA(ap
)));
253 atmarp_print(netdissect_options
*ndo
,
254 const u_char
*bp
, u_int length
, u_int caplen
)
256 const struct atmarp_pkthdr
*ap
;
257 u_short pro
, hrd
, op
;
259 ap
= (const struct atmarp_pkthdr
*)bp
;
266 ND_TCHECK_LEN(ATMTPA(ap
), ATMTPROTO_LEN(ap
));
268 if (!ndo
->ndo_eflag
) {
272 if ((pro
!= ETHERTYPE_IP
&& pro
!= ETHERTYPE_TRAIL
) ||
273 ATMSPROTO_LEN(ap
) != 4 ||
274 ATMTPROTO_LEN(ap
) != 4 ||
276 ND_PRINT("%s, %s (len %u/%u)",
277 tok2str(arphrd_values
, "Unknown Hardware (%u)", hrd
),
278 tok2str(ethertype_values
, "Unknown Protocol (0x%04x)", pro
),
282 /* don't know about the address formats */
283 if (!ndo
->ndo_vflag
) {
288 /* print operation */
290 ndo
->ndo_vflag
? ", " : "",
291 tok2str(arpop_values
, "Unknown (%u)", op
));
296 ND_PRINT("who-has ");
297 atmarp_tpaddr_print(ndo
, ap
, pro
);
298 if (ATMTHRD_LEN(ap
) != 0) {
300 atmarp_addr_print(ndo
, ATMTHA(ap
), ATMTHRD_LEN(ap
),
301 ATMTSA(ap
), ATMTSLN(ap
));
305 atmarp_spaddr_print(ndo
, ap
, pro
);
309 atmarp_spaddr_print(ndo
, ap
, pro
);
311 atmarp_addr_print(ndo
, ATMSHA(ap
), ATMSHRD_LEN(ap
), ATMSSA(ap
),
315 case ARPOP_INVREQUEST
:
317 atmarp_addr_print(ndo
, ATMTHA(ap
), ATMTHRD_LEN(ap
), ATMTSA(ap
),
320 atmarp_addr_print(ndo
, ATMSHA(ap
), ATMSHRD_LEN(ap
), ATMSSA(ap
),
325 atmarp_addr_print(ndo
, ATMSHA(ap
), ATMSHRD_LEN(ap
), ATMSSA(ap
),
328 atmarp_spaddr_print(ndo
, ap
, pro
);
333 atmarp_spaddr_print(ndo
, ap
, pro
);
337 ND_DEFAULTPRINT((const u_char
*)ap
, caplen
);
342 ND_PRINT(", length %u", length
);
346 arp_print(netdissect_options
*ndo
,
347 const u_char
*bp
, u_int length
, u_int caplen
)
349 const struct arp_pkthdr
*ap
;
350 u_short pro
, hrd
, op
, linkaddr
;
352 ndo
->ndo_protocol
= "arp";
353 ap
= (const struct arp_pkthdr
*)bp
;
361 /* if its ATM then call the ATM ARP printer
362 for Frame-relay ARP most of the fields
363 are similar to Ethernet so overload the Ethernet Printer
364 and set the linkaddr type for GET_LINKADDR_STRING() accordingly */
368 atmarp_print(ndo
, bp
, length
, caplen
);
371 linkaddr
= LINKADDR_FRELAY
;
374 linkaddr
= LINKADDR_MAC48
;
378 ND_TCHECK_LEN(TPA(ap
), PROTO_LEN(ap
));
380 if (!ndo
->ndo_eflag
) {
384 /* print hardware type/len and proto type/len */
385 if ((pro
!= ETHERTYPE_IP
&& pro
!= ETHERTYPE_TRAIL
) ||
386 PROTO_LEN(ap
) != 4 ||
389 ND_PRINT("%s (len %u), %s (len %u)",
390 tok2str(arphrd_values
, "Unknown Hardware (%u)", hrd
),
392 tok2str(ethertype_values
, "Unknown Protocol (0x%04x)", pro
),
395 /* don't know about the address formats */
396 if (!ndo
->ndo_vflag
) {
401 /* print operation */
403 ndo
->ndo_vflag
? ", " : "",
404 tok2str(arpop_values
, "Unknown (%u)", op
));
409 ND_PRINT("who-has ");
410 tpaddr_print_ip(ndo
, ap
, pro
);
411 if (isnonzero(ndo
, (const u_char
*)THA(ap
), HRD_LEN(ap
)))
413 GET_LINKADDR_STRING(THA(ap
), linkaddr
, HRD_LEN(ap
)));
415 spaddr_print_ip(ndo
, ap
, pro
);
419 spaddr_print_ip(ndo
, ap
, pro
);
420 ND_PRINT(" is-at %s",
421 GET_LINKADDR_STRING(SHA(ap
), linkaddr
, HRD_LEN(ap
)));
424 case ARPOP_REVREQUEST
:
426 * XXX - GET_LINKADDR_STRING() may return a pointer to
427 * a static buffer, so we only have one call to it per
430 * This should be done in a cleaner fashion.
432 ND_PRINT("who-is %s",
433 GET_LINKADDR_STRING(THA(ap
), linkaddr
, HRD_LEN(ap
)));
435 GET_LINKADDR_STRING(SHA(ap
), linkaddr
, HRD_LEN(ap
)));
440 GET_LINKADDR_STRING(THA(ap
), linkaddr
, HRD_LEN(ap
)));
441 tpaddr_print_ip(ndo
, ap
, pro
);
444 case ARPOP_INVREQUEST
:
446 * XXX - GET_LINKADDR_STRING() may return a pointer to
447 * a static buffer, so we only have one call to it per
450 * This should be done in a cleaner fashion.
452 ND_PRINT("who-is %s",
453 GET_LINKADDR_STRING(THA(ap
), linkaddr
, HRD_LEN(ap
)));
455 GET_LINKADDR_STRING(SHA(ap
), linkaddr
, HRD_LEN(ap
)));
460 GET_LINKADDR_STRING(SHA(ap
), linkaddr
, HRD_LEN(ap
)));
461 spaddr_print_ip(ndo
, ap
, pro
);
465 ND_DEFAULTPRINT((const u_char
*)ap
, caplen
);
470 ND_PRINT(", length %u", length
);