]>
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 */
28 #include "netdissect-stdinc.h"
32 #include "netdissect.h"
33 #include "addrtoname.h"
34 #include "ethertype.h"
39 * Address Resolution Protocol.
41 * See RFC 826 for protocol description. ARP packets are variable
42 * in size; the arphdr structure defines the fixed-length portion.
43 * Protocol type values are the same as those for 10 Mb/s Ethernet.
44 * It is followed by the variable-sized fields ar_sha, arp_spa,
45 * arp_tha and arp_tpa in that order, according to the lengths
46 * specified. Field names used correspond to RFC 826.
49 nd_uint16_t ar_hrd
; /* format of hardware address */
50 #define ARPHRD_ETHER 1 /* ethernet hardware format */
51 #define ARPHRD_IEEE802 6 /* token-ring hardware format */
52 #define ARPHRD_ARCNET 7 /* arcnet hardware format */
53 #define ARPHRD_FRELAY 15 /* frame relay hardware format */
54 #define ARPHRD_ATM2225 19 /* ATM (RFC 2225) */
55 #define ARPHRD_STRIP 23 /* Ricochet Starmode Radio hardware format */
56 #define ARPHRD_IEEE1394 24 /* IEEE 1394 (FireWire) hardware format */
57 #define ARPHRD_INFINIBAND 32 /* InfiniBand RFC 4391 */
58 nd_uint16_t ar_pro
; /* format of protocol address */
59 nd_uint8_t ar_hln
; /* length of hardware address */
60 nd_uint8_t ar_pln
; /* length of protocol address */
61 nd_uint16_t ar_op
; /* one of: */
62 #define ARPOP_REQUEST 1 /* request to resolve address */
63 #define ARPOP_REPLY 2 /* response to previous request */
64 #define ARPOP_REVREQUEST 3 /* request protocol address given hardware */
65 #define ARPOP_REVREPLY 4 /* response giving protocol address */
66 #define ARPOP_INVREQUEST 8 /* request to identify peer */
67 #define ARPOP_INVREPLY 9 /* response identifying peer */
68 #define ARPOP_NAK 10 /* NAK - only valif for ATM ARP */
71 * The remaining fields are variable in size,
72 * according to the sizes above.
75 nd_byte ar_sha
[]; /* sender hardware address */
76 nd_byte ar_spa
[]; /* sender protocol address */
77 nd_byte ar_tha
[]; /* target hardware address */
78 nd_byte ar_tpa
[]; /* target protocol address */
80 #define ar_sha(ap) (((const u_char *)((ap)+1))+ 0)
81 #define ar_spa(ap) (((const u_char *)((ap)+1))+ GET_U_1((ap)->ar_hln))
82 #define ar_tha(ap) (((const u_char *)((ap)+1))+ GET_U_1((ap)->ar_hln)+GET_U_1((ap)->ar_pln))
83 #define ar_tpa(ap) (((const u_char *)((ap)+1))+2*GET_U_1((ap)->ar_hln)+GET_U_1((ap)->ar_pln))
88 #define HRD(ap) GET_BE_U_2((ap)->ar_hrd)
89 #define HRD_LEN(ap) GET_U_1((ap)->ar_hln)
90 #define PROTO_LEN(ap) GET_U_1((ap)->ar_pln)
91 #define OP(ap) GET_BE_U_2((ap)->ar_op)
92 #define PRO(ap) GET_BE_U_2((ap)->ar_pro)
93 #define SHA(ap) (ar_sha(ap))
94 #define SPA(ap) (ar_spa(ap))
95 #define THA(ap) (ar_tha(ap))
96 #define TPA(ap) (ar_tpa(ap))
99 static const struct tok arpop_values
[] = {
100 { ARPOP_REQUEST
, "Request" },
101 { ARPOP_REPLY
, "Reply" },
102 { ARPOP_REVREQUEST
, "Reverse Request" },
103 { ARPOP_REVREPLY
, "Reverse Reply" },
104 { ARPOP_INVREQUEST
, "Inverse Request" },
105 { ARPOP_INVREPLY
, "Inverse Reply" },
106 { ARPOP_NAK
, "NACK Reply" },
110 static const struct tok arphrd_values
[] = {
111 { ARPHRD_ETHER
, "Ethernet" },
112 { ARPHRD_IEEE802
, "TokenRing" },
113 { ARPHRD_ARCNET
, "ArcNet" },
114 { ARPHRD_FRELAY
, "FrameRelay" },
115 { ARPHRD_STRIP
, "Strip" },
116 { ARPHRD_IEEE1394
, "IEEE 1394" },
117 { ARPHRD_ATM2225
, "ATM" },
118 { ARPHRD_INFINIBAND
, "InfiniBand" },
123 * ATM Address Resolution Protocol.
125 * See RFC 2225 for protocol description. ATMARP packets are similar
126 * to ARP packets, except that there are no length fields for the
127 * protocol address - instead, there are type/length fields for
128 * the ATM number and subaddress - and the hardware addresses consist
129 * of an ATM number and an ATM subaddress.
131 struct atmarp_pkthdr
{
132 nd_uint16_t aar_hrd
; /* format of hardware address */
133 nd_uint16_t aar_pro
; /* format of protocol address */
134 nd_uint8_t aar_shtl
; /* length of source ATM number */
135 nd_uint8_t aar_sstl
; /* length of source ATM subaddress */
136 #define ATMARP_IS_E164 0x40 /* bit in type/length for E.164 format */
137 #define ATMARP_LEN_MASK 0x3F /* length of {sub}address in type/length */
138 nd_uint16_t aar_op
; /* same as regular ARP */
139 nd_uint8_t aar_spln
; /* length of source protocol address */
140 nd_uint8_t aar_thtl
; /* length of target ATM number */
141 nd_uint8_t aar_tstl
; /* length of target ATM subaddress */
142 nd_uint8_t aar_tpln
; /* length of target protocol address */
144 * The remaining fields are variable in size,
145 * according to the sizes above.
148 nd_byte aar_sha
[]; /* source ATM number */
149 nd_byte aar_ssa
[]; /* source ATM subaddress */
150 nd_byte aar_spa
[]; /* sender protocol address */
151 nd_byte aar_tha
[]; /* target ATM number */
152 nd_byte aar_tsa
[]; /* target ATM subaddress */
153 nd_byte aar_tpa
[]; /* target protocol address */
156 #define ATMHRD(ap) GET_BE_U_2((ap)->aar_hrd)
157 #define ATMSHRD_LEN(ap) (GET_U_1((ap)->aar_shtl) & ATMARP_LEN_MASK)
158 #define ATMSSLN(ap) (GET_U_1((ap)->aar_sstl) & ATMARP_LEN_MASK)
159 #define ATMSPROTO_LEN(ap) GET_U_1((ap)->aar_spln)
160 #define ATMOP(ap) GET_BE_U_2((ap)->aar_op)
161 #define ATMPRO(ap) GET_BE_U_2((ap)->aar_pro)
162 #define ATMTHRD_LEN(ap) (GET_U_1((ap)->aar_thtl) & ATMARP_LEN_MASK)
163 #define ATMTSLN(ap) (GET_U_1((ap)->aar_tstl) & ATMARP_LEN_MASK)
164 #define ATMTPROTO_LEN(ap) GET_U_1((ap)->aar_tpln)
165 #define aar_sha(ap) ((const u_char *)((ap)+1))
166 #define aar_ssa(ap) (aar_sha(ap) + ATMSHRD_LEN(ap))
167 #define aar_spa(ap) (aar_ssa(ap) + ATMSSLN(ap))
168 #define aar_tha(ap) (aar_spa(ap) + ATMSPROTO_LEN(ap))
169 #define aar_tsa(ap) (aar_tha(ap) + ATMTHRD_LEN(ap))
170 #define aar_tpa(ap) (aar_tsa(ap) + ATMTSLN(ap))
173 #define ATMSHA(ap) (aar_sha(ap))
174 #define ATMSSA(ap) (aar_ssa(ap))
175 #define ATMSPA(ap) (aar_spa(ap))
176 #define ATMTHA(ap) (aar_tha(ap))
177 #define ATMTSA(ap) (aar_tsa(ap))
178 #define ATMTPA(ap) (aar_tpa(ap))
181 isnonzero(netdissect_options
*ndo
, const u_char
*a
, size_t len
)
193 tpaddr_print_ip(netdissect_options
*ndo
,
194 const struct arp_pkthdr
*ap
, u_short pro
)
196 if (pro
!= ETHERTYPE_IP
&& pro
!= ETHERTYPE_TRAIL
)
197 ND_PRINT("<wrong proto type>");
198 else if (PROTO_LEN(ap
) != 4)
199 ND_PRINT("<wrong len>");
201 ND_PRINT("%s", GET_IPADDR_STRING(TPA(ap
)));
205 spaddr_print_ip(netdissect_options
*ndo
,
206 const struct arp_pkthdr
*ap
, u_short pro
)
208 if (pro
!= ETHERTYPE_IP
&& pro
!= ETHERTYPE_TRAIL
)
209 ND_PRINT("<wrong proto type>");
210 else if (PROTO_LEN(ap
) != 4)
211 ND_PRINT("<wrong len>");
213 ND_PRINT("%s", GET_IPADDR_STRING(SPA(ap
)));
217 atmarp_addr_print(netdissect_options
*ndo
,
218 const u_char
*ha
, u_int ha_len
, const u_char
*srca
,
222 ND_PRINT("<No address>");
224 ND_PRINT("%s", GET_LINKADDR_STRING(ha
, LINKADDR_ATM
, ha_len
));
227 GET_LINKADDR_STRING(srca
, LINKADDR_ATM
, srca_len
));
232 atmarp_tpaddr_print(netdissect_options
*ndo
,
233 const struct atmarp_pkthdr
*ap
, u_short pro
)
235 if (pro
!= ETHERTYPE_IP
&& pro
!= ETHERTYPE_TRAIL
)
236 ND_PRINT("<wrong proto type>");
237 else if (ATMTPROTO_LEN(ap
) != 4)
238 ND_PRINT("<wrong tplen>");
240 ND_PRINT("%s", GET_IPADDR_STRING(ATMTPA(ap
)));
244 atmarp_spaddr_print(netdissect_options
*ndo
,
245 const struct atmarp_pkthdr
*ap
, u_short pro
)
247 if (pro
!= ETHERTYPE_IP
&& pro
!= ETHERTYPE_TRAIL
)
248 ND_PRINT("<wrong proto type>");
249 else if (ATMSPROTO_LEN(ap
) != 4)
250 ND_PRINT("<wrong splen>");
252 ND_PRINT("%s", GET_IPADDR_STRING(ATMSPA(ap
)));
256 atmarp_print(netdissect_options
*ndo
,
257 const u_char
*bp
, u_int length
, u_int caplen
)
259 const struct atmarp_pkthdr
*ap
;
260 u_short pro
, hrd
, op
;
262 ap
= (const struct atmarp_pkthdr
*)bp
;
269 if (!ND_TTEST_LEN(aar_tpa(ap
), ATMTPROTO_LEN(ap
))) {
271 ND_DEFAULTPRINT((const u_char
*)ap
, length
);
275 if (!ndo
->ndo_eflag
) {
279 if ((pro
!= ETHERTYPE_IP
&& pro
!= ETHERTYPE_TRAIL
) ||
280 ATMSPROTO_LEN(ap
) != 4 ||
281 ATMTPROTO_LEN(ap
) != 4 ||
283 ND_PRINT("%s, %s (len %u/%u)",
284 tok2str(arphrd_values
, "Unknown Hardware (%u)", hrd
),
285 tok2str(ethertype_values
, "Unknown Protocol (0x%04x)", pro
),
289 /* don't know about the address formats */
290 if (!ndo
->ndo_vflag
) {
295 /* print operation */
297 ndo
->ndo_vflag
? ", " : "",
298 tok2str(arpop_values
, "Unknown (%u)", op
));
303 ND_PRINT("who-has ");
304 atmarp_tpaddr_print(ndo
, ap
, pro
);
305 if (ATMTHRD_LEN(ap
) != 0) {
307 atmarp_addr_print(ndo
, ATMTHA(ap
), ATMTHRD_LEN(ap
),
308 ATMTSA(ap
), ATMTSLN(ap
));
312 atmarp_spaddr_print(ndo
, ap
, pro
);
316 atmarp_spaddr_print(ndo
, ap
, pro
);
318 atmarp_addr_print(ndo
, ATMSHA(ap
), ATMSHRD_LEN(ap
), ATMSSA(ap
),
322 case ARPOP_INVREQUEST
:
324 atmarp_addr_print(ndo
, ATMTHA(ap
), ATMTHRD_LEN(ap
), ATMTSA(ap
),
327 atmarp_addr_print(ndo
, ATMSHA(ap
), ATMSHRD_LEN(ap
), ATMSSA(ap
),
332 atmarp_addr_print(ndo
, ATMSHA(ap
), ATMSHRD_LEN(ap
), ATMSSA(ap
),
335 atmarp_spaddr_print(ndo
, ap
, pro
);
340 atmarp_spaddr_print(ndo
, ap
, pro
);
344 ND_DEFAULTPRINT((const u_char
*)ap
, caplen
);
349 ND_PRINT(", length %u", length
);
357 arp_print(netdissect_options
*ndo
,
358 const u_char
*bp
, u_int length
, u_int caplen
)
360 const struct arp_pkthdr
*ap
;
361 u_short pro
, hrd
, op
, linkaddr
;
363 ndo
->ndo_protocol
= "arp";
364 ap
= (const struct arp_pkthdr
*)bp
;
372 /* if its ATM then call the ATM ARP printer
373 for Frame-relay ARP most of the fields
374 are similar to Ethernet so overload the Ethernet Printer
375 and set the linkaddr type for GET_LINKADDR_STRING() accordingly */
379 atmarp_print(ndo
, bp
, length
, caplen
);
382 linkaddr
= LINKADDR_FRELAY
;
385 linkaddr
= LINKADDR_ETHER
;
389 if (!ND_TTEST_LEN(TPA(ap
), PROTO_LEN(ap
))) {
391 ND_DEFAULTPRINT((const u_char
*)ap
, length
);
395 if (!ndo
->ndo_eflag
) {
399 /* print hardware type/len and proto type/len */
400 if ((pro
!= ETHERTYPE_IP
&& pro
!= ETHERTYPE_TRAIL
) ||
401 PROTO_LEN(ap
) != 4 ||
404 ND_PRINT("%s (len %u), %s (len %u)",
405 tok2str(arphrd_values
, "Unknown Hardware (%u)", hrd
),
407 tok2str(ethertype_values
, "Unknown Protocol (0x%04x)", pro
),
410 /* don't know about the address formats */
411 if (!ndo
->ndo_vflag
) {
416 /* print operation */
418 ndo
->ndo_vflag
? ", " : "",
419 tok2str(arpop_values
, "Unknown (%u)", op
));
424 ND_PRINT("who-has ");
425 tpaddr_print_ip(ndo
, ap
, pro
);
426 if (isnonzero(ndo
, (const u_char
*)THA(ap
), HRD_LEN(ap
)))
428 GET_LINKADDR_STRING(THA(ap
), linkaddr
, HRD_LEN(ap
)));
430 spaddr_print_ip(ndo
, ap
, pro
);
434 spaddr_print_ip(ndo
, ap
, pro
);
435 ND_PRINT(" is-at %s",
436 GET_LINKADDR_STRING(SHA(ap
), linkaddr
, HRD_LEN(ap
)));
439 case ARPOP_REVREQUEST
:
440 ND_PRINT("who-is %s tell %s",
441 GET_LINKADDR_STRING(THA(ap
), linkaddr
, HRD_LEN(ap
)),
442 GET_LINKADDR_STRING(SHA(ap
), linkaddr
, HRD_LEN(ap
)));
447 GET_LINKADDR_STRING(THA(ap
), linkaddr
, HRD_LEN(ap
)));
448 tpaddr_print_ip(ndo
, ap
, pro
);
451 case ARPOP_INVREQUEST
:
452 ND_PRINT("who-is %s tell %s",
453 GET_LINKADDR_STRING(THA(ap
), linkaddr
, HRD_LEN(ap
)),
454 GET_LINKADDR_STRING(SHA(ap
), linkaddr
, HRD_LEN(ap
)));
459 GET_LINKADDR_STRING(SHA(ap
), linkaddr
, HRD_LEN(ap
)));
460 spaddr_print_ip(ndo
, ap
, pro
);
464 ND_DEFAULTPRINT((const u_char
*)ap
, caplen
);
469 ND_PRINT(", length %u", length
);