Upgrade to tcpdump-4.0.0.
[dragonfly.git] / contrib / tcpdump / print-arp.c
CommitLineData
c8cf0f94
PA
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
23static const char rcsid[] _U_ =
ea7b4bf5 24 "@(#) $Header: /tcpdump/master/tcpdump/print-arp.c,v 1.66 2006-03-03 22:53:21 hannes Exp $ (LBL)";
c8cf0f94
PA
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 "netdissect.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 */
ea7b4bf5
PA
52struct 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_ATM2225 19 /* ATM (RFC 2225) */
59#define ARPHRD_STRIP 23 /* Ricochet Starmode Radio hardware format */
60#define ARPHRD_IEEE1394 24 /* IEEE 1394 (FireWire) hardware format */
61 u_short ar_pro; /* format of protocol address */
62 u_char ar_hln; /* length of hardware address */
63 u_char ar_pln; /* length of protocol address */
64 u_short ar_op; /* one of: */
65#define ARPOP_REQUEST 1 /* request to resolve address */
66#define ARPOP_REPLY 2 /* response to previous request */
67#define ARPOP_REVREQUEST 3 /* request protocol address given hardware */
68#define ARPOP_REVREPLY 4 /* response giving protocol address */
69#define ARPOP_INVREQUEST 8 /* request to identify peer */
70#define ARPOP_INVREPLY 9 /* response identifying peer */
71#define ARPOP_NAK 10 /* NAK - only valif for ATM ARP */
72
c8cf0f94
PA
73/*
74 * The remaining fields are variable in size,
75 * according to the sizes above.
76 */
77#ifdef COMMENT_ONLY
78 u_char ar_sha[]; /* sender hardware address */
79 u_char ar_spa[]; /* sender protocol address */
80 u_char ar_tha[]; /* target hardware address */
81 u_char ar_tpa[]; /* target protocol address */
82#endif
83#define ar_sha(ap) (((const u_char *)((ap)+1))+0)
84#define ar_spa(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln)
85#define ar_tha(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln+(ap)->ar_pln)
86#define ar_tpa(ap) (((const u_char *)((ap)+1))+2*(ap)->ar_hln+(ap)->ar_pln)
87};
88
89#define ARP_HDRLEN 8
90
91#define HRD(ap) EXTRACT_16BITS(&(ap)->ar_hrd)
ea7b4bf5
PA
92#define HRD_LEN(ap) ((ap)->ar_hln)
93#define PROTO_LEN(ap) ((ap)->ar_pln)
c8cf0f94
PA
94#define OP(ap) EXTRACT_16BITS(&(ap)->ar_op)
95#define PRO(ap) EXTRACT_16BITS(&(ap)->ar_pro)
96#define SHA(ap) (ar_sha(ap))
97#define SPA(ap) (ar_spa(ap))
98#define THA(ap) (ar_tha(ap))
99#define TPA(ap) (ar_tpa(ap))
100
ea7b4bf5
PA
101
102struct tok arpop_values[] = {
103 { ARPOP_REQUEST, "Request" },
104 { ARPOP_REPLY, "Reply" },
105 { ARPOP_REVREQUEST, "Reverse Request" },
106 { ARPOP_REVREPLY, "Reverse Reply" },
107 { ARPOP_INVREQUEST, "Inverse Request" },
108 { ARPOP_INVREPLY, "Inverse Reply" },
109 { ARPOP_NAK, "NACK Reply" },
110 { 0, NULL }
111};
112
113struct tok arphrd_values[] = {
114 { ARPHRD_ETHER, "Ethernet" },
115 { ARPHRD_IEEE802, "TokenRing" },
116 { ARPHRD_ARCNET, "ArcNet" },
117 { ARPHRD_FRELAY, "FrameRelay" },
118 { ARPHRD_STRIP, "Strip" },
119 { ARPHRD_IEEE1394, "IEEE 1394" },
120 { ARPHRD_ATM2225, "ATM" },
121 { 0, NULL }
122};
123
c8cf0f94
PA
124/*
125 * ATM Address Resolution Protocol.
126 *
127 * See RFC 2225 for protocol description. ATMARP packets are similar
128 * to ARP packets, except that there are no length fields for the
129 * protocol address - instead, there are type/length fields for
130 * the ATM number and subaddress - and the hardware addresses consist
131 * of an ATM number and an ATM subaddress.
132 */
ea7b4bf5
PA
133struct atmarp_pkthdr {
134 u_short aar_hrd; /* format of hardware address */
135 u_short aar_pro; /* format of protocol address */
136 u_char aar_shtl; /* length of source ATM number */
137 u_char aar_sstl; /* length of source ATM subaddress */
138#define ATMARP_IS_E164 0x40 /* bit in type/length for E.164 format */
139#define ATMARP_LEN_MASK 0x3F /* length of {sub}address in type/length */
140 u_short aar_op; /* same as regular ARP */
141 u_char aar_spln; /* length of source protocol address */
142 u_char aar_thtl; /* length of target ATM number */
143 u_char aar_tstl; /* length of target ATM subaddress */
144 u_char aar_tpln; /* length of target protocol address */
c8cf0f94
PA
145/*
146 * The remaining fields are variable in size,
147 * according to the sizes above.
148 */
149#ifdef COMMENT_ONLY
150 u_char aar_sha[]; /* source ATM number */
151 u_char aar_ssa[]; /* source ATM subaddress */
152 u_char aar_spa[]; /* sender protocol address */
153 u_char aar_tha[]; /* target ATM number */
154 u_char aar_tsa[]; /* target ATM subaddress */
155 u_char aar_tpa[]; /* target protocol address */
156#endif
157
158#define ATMHRD(ap) EXTRACT_16BITS(&(ap)->aar_hrd)
ea7b4bf5 159#define ATMSHRD_LEN(ap) ((ap)->aar_shtl & ATMARP_LEN_MASK)
c8cf0f94 160#define ATMSSLN(ap) ((ap)->aar_sstl & ATMARP_LEN_MASK)
ea7b4bf5 161#define ATMSPROTO_LEN(ap) ((ap)->aar_spln)
c8cf0f94
PA
162#define ATMOP(ap) EXTRACT_16BITS(&(ap)->aar_op)
163#define ATMPRO(ap) EXTRACT_16BITS(&(ap)->aar_pro)
ea7b4bf5 164#define ATMTHRD_LEN(ap) ((ap)->aar_thtl & ATMARP_LEN_MASK)
c8cf0f94 165#define ATMTSLN(ap) ((ap)->aar_tstl & ATMARP_LEN_MASK)
ea7b4bf5 166#define ATMTPROTO_LEN(ap) ((ap)->aar_tpln)
c8cf0f94 167#define aar_sha(ap) ((const u_char *)((ap)+1))
ea7b4bf5 168#define aar_ssa(ap) (aar_sha(ap) + ATMSHRD_LEN(ap))
c8cf0f94 169#define aar_spa(ap) (aar_ssa(ap) + ATMSSLN(ap))
ea7b4bf5
PA
170#define aar_tha(ap) (aar_spa(ap) + ATMSPROTO_LEN(ap))
171#define aar_tsa(ap) (aar_tha(ap) + ATMTHRD_LEN(ap))
c8cf0f94
PA
172#define aar_tpa(ap) (aar_tsa(ap) + ATMTSLN(ap))
173};
174
175#define ATMSHA(ap) (aar_sha(ap))
176#define ATMSSA(ap) (aar_ssa(ap))
177#define ATMSPA(ap) (aar_spa(ap))
178#define ATMTHA(ap) (aar_tha(ap))
179#define ATMTSA(ap) (aar_tsa(ap))
180#define ATMTPA(ap) (aar_tpa(ap))
181
182static u_char ezero[6];
183
184static void
185atmarp_addr_print(netdissect_options *ndo,
186 const u_char *ha, u_int ha_len, const u_char *srca,
187 u_int srca_len)
188{
189 if (ha_len == 0)
190 ND_PRINT((ndo, "<No address>"));
191 else {
ea7b4bf5 192 ND_PRINT((ndo, "%s", linkaddr_string(ha, LINKADDR_ATM, ha_len)));
c8cf0f94
PA
193 if (srca_len != 0)
194 ND_PRINT((ndo, ",%s",
ea7b4bf5 195 linkaddr_string(srca, LINKADDR_ATM, srca_len)));
c8cf0f94
PA
196 }
197}
198
199static void
200atmarp_print(netdissect_options *ndo,
201 const u_char *bp, u_int length, u_int caplen)
202{
203 const struct atmarp_pkthdr *ap;
204 u_short pro, hrd, op;
205
206 ap = (const struct atmarp_pkthdr *)bp;
207 ND_TCHECK(*ap);
208
209 hrd = ATMHRD(ap);
210 pro = ATMPRO(ap);
211 op = ATMOP(ap);
212
ea7b4bf5
PA
213 if (!ND_TTEST2(*aar_tpa(ap), ATMTPROTO_LEN(ap))) {
214 ND_PRINT((ndo, "[|ARP]"));
c8cf0f94
PA
215 ND_DEFAULTPRINT((const u_char *)ap, length);
216 return;
217 }
218
ea7b4bf5
PA
219 if (!ndo->ndo_eflag) {
220 ND_PRINT((ndo, "ARP, "));
221 }
222
c8cf0f94 223 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
ea7b4bf5
PA
224 ATMSPROTO_LEN(ap) != 4 ||
225 ATMTPROTO_LEN(ap) != 4 ||
226 ndo->ndo_vflag) {
227 ND_PRINT((ndo, "%s, %s (len %u/%u)",
228 tok2str(arphrd_values, "Unknown Hardware (%u)", hrd),
229 tok2str(ethertype_values, "Unknown Protocol (0x%04x)", pro),
230 ATMSPROTO_LEN(ap),
231 ATMTPROTO_LEN(ap)));
232
233 /* don't know know about the address formats */
234 if (!ndo->ndo_vflag) {
235 goto out;
236 }
c8cf0f94 237 }
ea7b4bf5
PA
238
239 /* print operation */
240 printf("%s%s ",
241 ndo->ndo_vflag ? ", " : "",
242 tok2str(arpop_values, "Unknown (%u)", op));
243
c8cf0f94
PA
244 switch (op) {
245
246 case ARPOP_REQUEST:
ea7b4bf5
PA
247 ND_PRINT((ndo, "who-has %s", ipaddr_string(ATMTPA(ap))));
248 if (ATMTHRD_LEN(ap) != 0) {
c8cf0f94 249 ND_PRINT((ndo, " ("));
ea7b4bf5 250 atmarp_addr_print(ndo, ATMTHA(ap), ATMTHRD_LEN(ap),
c8cf0f94
PA
251 ATMTSA(ap), ATMTSLN(ap));
252 ND_PRINT((ndo, ")"));
253 }
ea7b4bf5 254 ND_PRINT((ndo, "tell %s", ipaddr_string(ATMSPA(ap))));
c8cf0f94
PA
255 break;
256
257 case ARPOP_REPLY:
ea7b4bf5
PA
258 ND_PRINT((ndo, "%s is-at ", ipaddr_string(ATMSPA(ap))));
259 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
260 ATMSSLN(ap));
c8cf0f94
PA
261 break;
262
263 case ARPOP_INVREQUEST:
ea7b4bf5
PA
264 ND_PRINT((ndo, "who-is "));
265 atmarp_addr_print(ndo, ATMTHA(ap), ATMTHRD_LEN(ap), ATMTSA(ap),
c8cf0f94
PA
266 ATMTSLN(ap));
267 ND_PRINT((ndo, " tell "));
ea7b4bf5 268 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
c8cf0f94
PA
269 ATMSSLN(ap));
270 break;
271
272 case ARPOP_INVREPLY:
ea7b4bf5 273 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
c8cf0f94 274 ATMSSLN(ap));
ea7b4bf5 275 ND_PRINT((ndo, "at %s", ipaddr_string(ATMSPA(ap))));
c8cf0f94
PA
276 break;
277
ea7b4bf5
PA
278 case ARPOP_NAK:
279 ND_PRINT((ndo, "for %s", ipaddr_string(ATMSPA(ap))));
c8cf0f94
PA
280 break;
281
282 default:
c8cf0f94
PA
283 ND_DEFAULTPRINT((const u_char *)ap, caplen);
284 return;
285 }
ea7b4bf5
PA
286
287 out:
288 ND_PRINT((ndo, ", length %u", length));
289 return;
290
c8cf0f94 291trunc:
ea7b4bf5 292 ND_PRINT((ndo, "[|ARP]"));
c8cf0f94
PA
293}
294
295void
296arp_print(netdissect_options *ndo,
297 const u_char *bp, u_int length, u_int caplen)
298{
299 const struct arp_pkthdr *ap;
ea7b4bf5 300 u_short pro, hrd, op, linkaddr;
c8cf0f94
PA
301
302 ap = (const struct arp_pkthdr *)bp;
303 ND_TCHECK(*ap);
ea7b4bf5 304
c8cf0f94 305 hrd = HRD(ap);
c8cf0f94
PA
306 pro = PRO(ap);
307 op = OP(ap);
308
ea7b4bf5
PA
309
310 /* if its ATM then call the ATM ARP printer
311 for Frame-relay ARP most of the fields
312 are similar to Ethernet so overload the Ethernet Printer
313 and set the linkaddr type for linkaddr_string() accordingly */
314
315 switch(hrd) {
316 case ARPHRD_ATM2225:
317 atmarp_print(ndo, bp, length, caplen);
318 return;
319 case ARPHRD_FRELAY:
320 linkaddr = LINKADDR_FRELAY;
321 default:
322 linkaddr = LINKADDR_ETHER;
323 break;
324 }
325
326 if (!ND_TTEST2(*ar_tpa(ap), PROTO_LEN(ap))) {
327 ND_PRINT((ndo, "[|ARP]"));
c8cf0f94
PA
328 ND_DEFAULTPRINT((const u_char *)ap, length);
329 return;
330 }
331
ea7b4bf5
PA
332 if (!ndo->ndo_eflag) {
333 ND_PRINT((ndo, "ARP, "));
334 }
335
336 /* print hardware type/len and proto type/len */
337 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
338 PROTO_LEN(ap) != 4 ||
339 HRD_LEN(ap) == 0 ||
340 ndo->ndo_vflag) {
341 ND_PRINT((ndo, "%s (len %u), %s (len %u)",
342 tok2str(arphrd_values, "Unknown Hardware (%u)", hrd),
343 HRD_LEN(ap),
344 tok2str(ethertype_values, "Unknown Protocol (0x%04x)", pro),
345 PROTO_LEN(ap)));
346
347 /* don't know know about the address formats */
348 if (!ndo->ndo_vflag) {
349 goto out;
350 }
c8cf0f94 351 }
ea7b4bf5
PA
352
353 /* print operation */
354 printf("%s%s ",
355 ndo->ndo_vflag ? ", " : "",
356 tok2str(arpop_values, "Unknown (%u)", op));
357
c8cf0f94
PA
358 switch (op) {
359
360 case ARPOP_REQUEST:
ea7b4bf5
PA
361 ND_PRINT((ndo, "who-has %s", ipaddr_string(TPA(ap))));
362 if (memcmp((const char *)ezero, (const char *)THA(ap), HRD_LEN(ap)) != 0)
c8cf0f94 363 ND_PRINT((ndo, " (%s)",
ea7b4bf5 364 linkaddr_string(THA(ap), linkaddr, HRD_LEN(ap))));
c8cf0f94
PA
365 ND_PRINT((ndo, " tell %s", ipaddr_string(SPA(ap))));
366 break;
367
368 case ARPOP_REPLY:
ea7b4bf5
PA
369 ND_PRINT((ndo, "%s is-at %s",
370 ipaddr_string(SPA(ap)),
371 linkaddr_string(SHA(ap), linkaddr, HRD_LEN(ap))));
c8cf0f94
PA
372 break;
373
374 case ARPOP_REVREQUEST:
ea7b4bf5
PA
375 ND_PRINT((ndo, "who-is %s tell %s",
376 linkaddr_string(THA(ap), linkaddr, HRD_LEN(ap)),
377 linkaddr_string(SHA(ap), linkaddr, HRD_LEN(ap))));
c8cf0f94
PA
378 break;
379
380 case ARPOP_REVREPLY:
ea7b4bf5
PA
381 ND_PRINT((ndo, "%s at %s",
382 linkaddr_string(THA(ap), linkaddr, HRD_LEN(ap)),
c8cf0f94
PA
383 ipaddr_string(TPA(ap))));
384 break;
385
386 case ARPOP_INVREQUEST:
ea7b4bf5
PA
387 ND_PRINT((ndo, "who-is %s tell %s",
388 linkaddr_string(THA(ap), linkaddr, HRD_LEN(ap)),
389 linkaddr_string(SHA(ap), linkaddr, HRD_LEN(ap))));
c8cf0f94
PA
390 break;
391
392 case ARPOP_INVREPLY:
ea7b4bf5
PA
393 ND_PRINT((ndo,"%s at %s",
394 linkaddr_string(THA(ap), linkaddr, HRD_LEN(ap)),
c8cf0f94
PA
395 ipaddr_string(TPA(ap))));
396 break;
397
398 default:
c8cf0f94
PA
399 ND_DEFAULTPRINT((const u_char *)ap, caplen);
400 return;
401 }
ea7b4bf5
PA
402
403 out:
404 ND_PRINT((ndo, ", length %u", length));
405
c8cf0f94
PA
406 return;
407trunc:
ea7b4bf5 408 ND_PRINT((ndo, "[|ARP]"));
c8cf0f94
PA
409}
410
411/*
412 * Local Variables:
413 * c-style: bsd
414 * End:
415 */
416