]> The Tcpdump Group git mirrors - tcpdump/blob - print-arp.c
Added support for Win32, based on WinPcap.
[tcpdump] / print-arp.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
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
16 * written permission.
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.
20 */
21
22 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-arp.c,v 1.55 2002-08-01 08:53:00 risso Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <tcpdump-stdinc.h>
32
33 #include <stdio.h>
34 #include <string.h>
35
36 #include "interface.h"
37 #include "addrtoname.h"
38 #include "ether.h"
39 #include "ethertype.h"
40 #include "extract.h" /* must come after interface.h */
41
42 /*
43 * Address Resolution Protocol.
44 *
45 * See RFC 826 for protocol description. ARP packets are variable
46 * in size; the arphdr structure defines the fixed-length portion.
47 * Protocol type values are the same as those for 10 Mb/s Ethernet.
48 * It is followed by the variable-sized fields ar_sha, arp_spa,
49 * arp_tha and arp_tpa in that order, according to the lengths
50 * specified. Field names used correspond to RFC 826.
51 */
52 struct arp_pkthdr {
53 u_short ar_hrd; /* format of hardware address */
54 #define ARPHRD_ETHER 1 /* ethernet hardware format */
55 #define ARPHRD_IEEE802 6 /* token-ring hardware format */
56 #define ARPHRD_ARCNET 7 /* arcnet hardware format */
57 #define ARPHRD_FRELAY 15 /* frame relay hardware format */
58 #define ARPHRD_STRIP 23 /* Ricochet Starmode Radio hardware format */
59 #define ARPHRD_IEEE1394 24 /* IEEE 1394 (FireWire) hardware format */
60 u_short ar_pro; /* format of protocol address */
61 u_char ar_hln; /* length of hardware address */
62 u_char ar_pln; /* length of protocol address */
63 u_short ar_op; /* one of: */
64 #define ARPOP_REQUEST 1 /* request to resolve address */
65 #define ARPOP_REPLY 2 /* response to previous request */
66 #define ARPOP_REVREQUEST 3 /* request protocol address given hardware */
67 #define ARPOP_REVREPLY 4 /* response giving protocol address */
68 #define ARPOP_INVREQUEST 8 /* request to identify peer */
69 #define ARPOP_INVREPLY 9 /* response identifying peer */
70 /*
71 * The remaining fields are variable in size,
72 * according to the sizes above.
73 */
74 #ifdef COMMENT_ONLY
75 u_char ar_sha[]; /* sender hardware address */
76 u_char ar_spa[]; /* sender protocol address */
77 u_char ar_tha[]; /* target hardware address */
78 u_char ar_tpa[]; /* target protocol address */
79 #endif
80 #define ar_sha(ap) (((const u_char *)((ap)+1))+0)
81 #define ar_spa(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln)
82 #define ar_tha(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln+(ap)->ar_pln)
83 #define ar_tpa(ap) (((const u_char *)((ap)+1))+2*(ap)->ar_hln+(ap)->ar_pln)
84 };
85
86 #define ARP_HDRLEN 8
87
88 #define HRD(ap) ((ap)->ar_hrd)
89 #define HLN(ap) ((ap)->ar_hln)
90 #define PLN(ap) ((ap)->ar_pln)
91 #define OP(ap) ((ap)->ar_op)
92 #define PRO(ap) ((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))
97
98 /*
99 * ATM Address Resolution Protocol.
100 *
101 * See RFC 2225 for protocol description. ARP packets are variable
102 * in size; the arphdr structure defines the fixed-length portion.
103 * Protocol type values are the same as those for 10 Mb/s Ethernet.
104 * It is followed by the variable-sized fields ar_sha, arp_spa,
105 * arp_tha and arp_tpa in that order, according to the lengths
106 * specified. Field names used correspond to RFC 826.
107 */
108 struct atmarp_pkthdr {
109 u_short ar_hrd; /* format of hardware address */
110 #define ARPHRD_ATM2225 19 /* ATM (RFC 2225) */
111 u_short ar_pro; /* format of protocol address */
112 u_char ar_shtl; /* length of hardware address */
113 u_char ar_sstl; /* length of hardware address */
114 u_short ar_op; /* same as regular ARP */
115 u_char ar_spln;
116 u_char ar_thtl;
117 u_char ar_tstl;
118 u_char ar_tpln;
119 /*
120 * The remaining fields are variable in size,
121 * according to the sizes above.
122 */
123 #ifdef COMMENT_ONLY
124 u_char ar_sha[]; /* sender hardware address */
125 u_char ar_spa[]; /* sender protocol address */
126 u_char ar_tha[]; /* target hardware address */
127 u_char ar_tpa[]; /* target protocol address */
128 #endif
129 #define ar_sha(ap) (((const u_char *)((ap)+1))+0)
130 #define ar_spa(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln)
131 #define ar_tha(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln+(ap)->ar_pln)
132 #define ar_tpa(ap) (((const u_char *)((ap)+1))+2*(ap)->ar_hln+(ap)->ar_pln)
133 };
134
135 static u_char ezero[6];
136
137 static void
138 atmarp_print(const u_char *bp, u_int length, u_int caplen)
139 {
140 const struct arp_pkthdr *ap;
141 u_short pro, hrd, op;
142
143 ap = (const struct arp_pkthdr *)bp;
144 TCHECK(*ap);
145 if ((const u_char *)(ar_tpa(ap) + PLN(ap)) > snapend) {
146 (void)printf("truncated-arp");
147 default_print((const u_char *)ap, length);
148 return;
149 }
150
151 hrd = EXTRACT_16BITS(&HRD(ap));
152 if (hrd == ARPHRD_ATM2225)
153 atmarp_print(bp, length, caplen);
154 pro = EXTRACT_16BITS(&PRO(ap));
155 op = EXTRACT_16BITS(&OP(ap));
156
157 if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) {
158 (void)printf("arp-#%d for proto #%d (%d) hardware #%d (%d)",
159 op, pro, PLN(ap), hrd, HLN(ap));
160 return;
161 }
162 if (pro == ETHERTYPE_TRAIL)
163 (void)printf("trailer-");
164 switch (op) {
165
166 case ARPOP_REQUEST:
167 (void)printf("arp who-has %s", ipaddr_string(TPA(ap)));
168 if (memcmp((const char *)ezero, (const char *)THA(ap), HLN(ap)) != 0)
169 (void)printf(" (%s)",
170 linkaddr_string(THA(ap), HLN(ap)));
171 (void)printf(" tell %s", ipaddr_string(SPA(ap)));
172 break;
173
174 case ARPOP_REPLY:
175 (void)printf("arp reply %s", ipaddr_string(SPA(ap)));
176 (void)printf(" is-at %s", linkaddr_string(SHA(ap), HLN(ap)));
177 break;
178
179 case ARPOP_REVREQUEST:
180 (void)printf("rarp who-is %s tell %s",
181 linkaddr_string(THA(ap), HLN(ap)),
182 linkaddr_string(SHA(ap), HLN(ap)));
183 break;
184
185 case ARPOP_REVREPLY:
186 (void)printf("rarp reply %s at %s",
187 linkaddr_string(THA(ap), HLN(ap)),
188 ipaddr_string(TPA(ap)));
189 break;
190
191 case ARPOP_INVREQUEST:
192 (void)printf("invarp who-is %s tell %s",
193 linkaddr_string(THA(ap), HLN(ap)),
194 linkaddr_string(SHA(ap), HLN(ap)));
195 break;
196
197 case ARPOP_INVREPLY:
198 (void)printf("invarp reply %s at %s",
199 linkaddr_string(THA(ap), HLN(ap)),
200 ipaddr_string(TPA(ap)));
201 break;
202
203 default:
204 (void)printf("arp-#%d", op);
205 default_print((const u_char *)ap, caplen);
206 return;
207 }
208 if (hrd != ARPHRD_ETHER)
209 printf(" hardware #%d", hrd);
210 return;
211 trunc:
212 (void)printf("[|arp]");
213 }
214
215 void
216 arp_print(const u_char *bp, u_int length, u_int caplen)
217 {
218 const struct arp_pkthdr *ap;
219 u_short pro, hrd, op;
220
221 ap = (const struct arp_pkthdr *)bp;
222 TCHECK(*ap);
223 if ((const u_char *)(ar_tpa(ap) + PLN(ap)) > snapend) {
224 (void)printf("truncated-arp");
225 default_print((const u_char *)ap, length);
226 return;
227 }
228
229 hrd = EXTRACT_16BITS(&HRD(ap));
230 if (hrd == ARPHRD_ATM2225)
231 atmarp_print(bp, length, caplen);
232 pro = EXTRACT_16BITS(&PRO(ap));
233 op = EXTRACT_16BITS(&OP(ap));
234
235 if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) {
236 (void)printf("arp-#%d for proto #%d (%d) hardware #%d (%d)",
237 op, pro, PLN(ap), hrd, HLN(ap));
238 return;
239 }
240 if (pro == ETHERTYPE_TRAIL)
241 (void)printf("trailer-");
242 switch (op) {
243
244 case ARPOP_REQUEST:
245 (void)printf("arp who-has %s", ipaddr_string(TPA(ap)));
246 if (memcmp((const char *)ezero, (const char *)THA(ap), HLN(ap)) != 0)
247 (void)printf(" (%s)",
248 linkaddr_string(THA(ap), HLN(ap)));
249 (void)printf(" tell %s", ipaddr_string(SPA(ap)));
250 break;
251
252 case ARPOP_REPLY:
253 (void)printf("arp reply %s", ipaddr_string(SPA(ap)));
254 (void)printf(" is-at %s", linkaddr_string(SHA(ap), HLN(ap)));
255 break;
256
257 case ARPOP_REVREQUEST:
258 (void)printf("rarp who-is %s tell %s",
259 linkaddr_string(THA(ap), HLN(ap)),
260 linkaddr_string(SHA(ap), HLN(ap)));
261 break;
262
263 case ARPOP_REVREPLY:
264 (void)printf("rarp reply %s at %s",
265 linkaddr_string(THA(ap), HLN(ap)),
266 ipaddr_string(TPA(ap)));
267 break;
268
269 case ARPOP_INVREQUEST:
270 (void)printf("invarp who-is %s tell %s",
271 linkaddr_string(THA(ap), HLN(ap)),
272 linkaddr_string(SHA(ap), HLN(ap)));
273 break;
274
275 case ARPOP_INVREPLY:
276 (void)printf("invarp reply %s at %s",
277 linkaddr_string(THA(ap), HLN(ap)),
278 ipaddr_string(TPA(ap)));
279 break;
280
281 default:
282 (void)printf("arp-#%d", op);
283 default_print((const u_char *)ap, caplen);
284 return;
285 }
286 if (hrd != ARPHRD_ETHER)
287 printf(" hardware #%d", hrd);
288 return;
289 trunc:
290 (void)printf("[|arp]");
291 }