blob: b5047a0d1075bb92e5eb2b58618fcc2107547fba [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/*
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_ =
JP Abgrall53f17a92014-02-12 14:02:41 -080024 "@(#) $Header: /tcpdump/master/tcpdump/print-arp.c,v 1.66 2006-03-03 22:53:21 hannes Exp $ (LBL)";
The Android Open Source Project2949f582009-03-03 19:30:46 -080025#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 */
JP Abgrall53f17a92014-02-12 14:02:41 -080052struct 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
The Android Open Source Project2949f582009-03-03 19:30:46 -080073/*
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)
JP Abgrall53f17a92014-02-12 14:02:41 -080092#define HRD_LEN(ap) ((ap)->ar_hln)
93#define PROTO_LEN(ap) ((ap)->ar_pln)
The Android Open Source Project2949f582009-03-03 19:30:46 -080094#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
JP Abgrall53f17a92014-02-12 14:02:41 -0800101
102static const struct 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
113static const struct 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
The Android Open Source Project2949f582009-03-03 19:30:46 -0800124/*
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 */
JP Abgrall53f17a92014-02-12 14:02:41 -0800133struct 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 */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800145/*
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)
JP Abgrall53f17a92014-02-12 14:02:41 -0800159#define ATMSHRD_LEN(ap) ((ap)->aar_shtl & ATMARP_LEN_MASK)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800160#define ATMSSLN(ap) ((ap)->aar_sstl & ATMARP_LEN_MASK)
JP Abgrall53f17a92014-02-12 14:02:41 -0800161#define ATMSPROTO_LEN(ap) ((ap)->aar_spln)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800162#define ATMOP(ap) EXTRACT_16BITS(&(ap)->aar_op)
163#define ATMPRO(ap) EXTRACT_16BITS(&(ap)->aar_pro)
JP Abgrall53f17a92014-02-12 14:02:41 -0800164#define ATMTHRD_LEN(ap) ((ap)->aar_thtl & ATMARP_LEN_MASK)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800165#define ATMTSLN(ap) ((ap)->aar_tstl & ATMARP_LEN_MASK)
JP Abgrall53f17a92014-02-12 14:02:41 -0800166#define ATMTPROTO_LEN(ap) ((ap)->aar_tpln)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800167#define aar_sha(ap) ((const u_char *)((ap)+1))
JP Abgrall53f17a92014-02-12 14:02:41 -0800168#define aar_ssa(ap) (aar_sha(ap) + ATMSHRD_LEN(ap))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800169#define aar_spa(ap) (aar_ssa(ap) + ATMSSLN(ap))
JP Abgrall53f17a92014-02-12 14:02:41 -0800170#define aar_tha(ap) (aar_spa(ap) + ATMSPROTO_LEN(ap))
171#define aar_tsa(ap) (aar_tha(ap) + ATMTHRD_LEN(ap))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800172#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 {
JP Abgrall53f17a92014-02-12 14:02:41 -0800192 ND_PRINT((ndo, "%s", linkaddr_string(ha, LINKADDR_ATM, ha_len)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800193 if (srca_len != 0)
194 ND_PRINT((ndo, ",%s",
JP Abgrall53f17a92014-02-12 14:02:41 -0800195 linkaddr_string(srca, LINKADDR_ATM, srca_len)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800196 }
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
JP Abgrall53f17a92014-02-12 14:02:41 -0800213 if (!ND_TTEST2(*aar_tpa(ap), ATMTPROTO_LEN(ap))) {
214 ND_PRINT((ndo, "[|ARP]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800215 ND_DEFAULTPRINT((const u_char *)ap, length);
216 return;
217 }
218
JP Abgrall53f17a92014-02-12 14:02:41 -0800219 if (!ndo->ndo_eflag) {
220 ND_PRINT((ndo, "ARP, "));
221 }
222
The Android Open Source Project2949f582009-03-03 19:30:46 -0800223 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
JP Abgrall53f17a92014-02-12 14:02:41 -0800224 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 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800237 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800238
239 /* print operation */
240 printf("%s%s ",
241 ndo->ndo_vflag ? ", " : "",
242 tok2str(arpop_values, "Unknown (%u)", op));
243
The Android Open Source Project2949f582009-03-03 19:30:46 -0800244 switch (op) {
245
246 case ARPOP_REQUEST:
JP Abgrall53f17a92014-02-12 14:02:41 -0800247 ND_PRINT((ndo, "who-has %s", ipaddr_string(ATMTPA(ap))));
248 if (ATMTHRD_LEN(ap) != 0) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800249 ND_PRINT((ndo, " ("));
JP Abgrall53f17a92014-02-12 14:02:41 -0800250 atmarp_addr_print(ndo, ATMTHA(ap), ATMTHRD_LEN(ap),
The Android Open Source Project2949f582009-03-03 19:30:46 -0800251 ATMTSA(ap), ATMTSLN(ap));
252 ND_PRINT((ndo, ")"));
253 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800254 ND_PRINT((ndo, "tell %s", ipaddr_string(ATMSPA(ap))));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800255 break;
256
257 case ARPOP_REPLY:
JP Abgrall53f17a92014-02-12 14:02:41 -0800258 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));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800261 break;
262
263 case ARPOP_INVREQUEST:
JP Abgrall53f17a92014-02-12 14:02:41 -0800264 ND_PRINT((ndo, "who-is "));
265 atmarp_addr_print(ndo, ATMTHA(ap), ATMTHRD_LEN(ap), ATMTSA(ap),
The Android Open Source Project2949f582009-03-03 19:30:46 -0800266 ATMTSLN(ap));
267 ND_PRINT((ndo, " tell "));
JP Abgrall53f17a92014-02-12 14:02:41 -0800268 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
The Android Open Source Project2949f582009-03-03 19:30:46 -0800269 ATMSSLN(ap));
270 break;
271
272 case ARPOP_INVREPLY:
JP Abgrall53f17a92014-02-12 14:02:41 -0800273 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
The Android Open Source Project2949f582009-03-03 19:30:46 -0800274 ATMSSLN(ap));
JP Abgrall53f17a92014-02-12 14:02:41 -0800275 ND_PRINT((ndo, "at %s", ipaddr_string(ATMSPA(ap))));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800276 break;
277
JP Abgrall53f17a92014-02-12 14:02:41 -0800278 case ARPOP_NAK:
279 ND_PRINT((ndo, "for %s", ipaddr_string(ATMSPA(ap))));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800280 break;
281
282 default:
The Android Open Source Project2949f582009-03-03 19:30:46 -0800283 ND_DEFAULTPRINT((const u_char *)ap, caplen);
284 return;
285 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800286
287 out:
288 ND_PRINT((ndo, ", length %u", length));
289 return;
290
The Android Open Source Project2949f582009-03-03 19:30:46 -0800291trunc:
JP Abgrall53f17a92014-02-12 14:02:41 -0800292 ND_PRINT((ndo, "[|ARP]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800293}
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;
JP Abgrall53f17a92014-02-12 14:02:41 -0800300 u_short pro, hrd, op, linkaddr;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800301
302 ap = (const struct arp_pkthdr *)bp;
303 ND_TCHECK(*ap);
JP Abgrall53f17a92014-02-12 14:02:41 -0800304
The Android Open Source Project2949f582009-03-03 19:30:46 -0800305 hrd = HRD(ap);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800306 pro = PRO(ap);
307 op = OP(ap);
308
JP Abgrall53f17a92014-02-12 14:02:41 -0800309
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 break;
322 default:
323 linkaddr = LINKADDR_ETHER;
324 break;
325 }
326
327 if (!ND_TTEST2(*ar_tpa(ap), PROTO_LEN(ap))) {
328 ND_PRINT((ndo, "[|ARP]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800329 ND_DEFAULTPRINT((const u_char *)ap, length);
330 return;
331 }
332
JP Abgrall53f17a92014-02-12 14:02:41 -0800333 if (!ndo->ndo_eflag) {
334 ND_PRINT((ndo, "ARP, "));
335 }
336
337 /* print hardware type/len and proto type/len */
338 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
339 PROTO_LEN(ap) != 4 ||
340 HRD_LEN(ap) == 0 ||
341 ndo->ndo_vflag) {
342 ND_PRINT((ndo, "%s (len %u), %s (len %u)",
343 tok2str(arphrd_values, "Unknown Hardware (%u)", hrd),
344 HRD_LEN(ap),
345 tok2str(ethertype_values, "Unknown Protocol (0x%04x)", pro),
346 PROTO_LEN(ap)));
347
348 /* don't know know about the address formats */
349 if (!ndo->ndo_vflag) {
350 goto out;
351 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800352 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800353
354 /* print operation */
355 printf("%s%s ",
356 ndo->ndo_vflag ? ", " : "",
357 tok2str(arpop_values, "Unknown (%u)", op));
358
The Android Open Source Project2949f582009-03-03 19:30:46 -0800359 switch (op) {
360
361 case ARPOP_REQUEST:
JP Abgrall53f17a92014-02-12 14:02:41 -0800362 ND_PRINT((ndo, "who-has %s", ipaddr_string(TPA(ap))));
363 if (memcmp((const char *)ezero, (const char *)THA(ap), HRD_LEN(ap)) != 0)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800364 ND_PRINT((ndo, " (%s)",
JP Abgrall53f17a92014-02-12 14:02:41 -0800365 linkaddr_string(THA(ap), linkaddr, HRD_LEN(ap))));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800366 ND_PRINT((ndo, " tell %s", ipaddr_string(SPA(ap))));
367 break;
368
369 case ARPOP_REPLY:
JP Abgrall53f17a92014-02-12 14:02:41 -0800370 ND_PRINT((ndo, "%s is-at %s",
371 ipaddr_string(SPA(ap)),
372 linkaddr_string(SHA(ap), linkaddr, HRD_LEN(ap))));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800373 break;
374
375 case ARPOP_REVREQUEST:
JP Abgrall53f17a92014-02-12 14:02:41 -0800376 ND_PRINT((ndo, "who-is %s tell %s",
377 linkaddr_string(THA(ap), linkaddr, HRD_LEN(ap)),
378 linkaddr_string(SHA(ap), linkaddr, HRD_LEN(ap))));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800379 break;
380
381 case ARPOP_REVREPLY:
JP Abgrall53f17a92014-02-12 14:02:41 -0800382 ND_PRINT((ndo, "%s at %s",
383 linkaddr_string(THA(ap), linkaddr, HRD_LEN(ap)),
The Android Open Source Project2949f582009-03-03 19:30:46 -0800384 ipaddr_string(TPA(ap))));
385 break;
386
387 case ARPOP_INVREQUEST:
JP Abgrall53f17a92014-02-12 14:02:41 -0800388 ND_PRINT((ndo, "who-is %s tell %s",
389 linkaddr_string(THA(ap), linkaddr, HRD_LEN(ap)),
390 linkaddr_string(SHA(ap), linkaddr, HRD_LEN(ap))));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800391 break;
392
393 case ARPOP_INVREPLY:
JP Abgrall53f17a92014-02-12 14:02:41 -0800394 ND_PRINT((ndo,"%s at %s",
395 linkaddr_string(THA(ap), linkaddr, HRD_LEN(ap)),
The Android Open Source Project2949f582009-03-03 19:30:46 -0800396 ipaddr_string(TPA(ap))));
397 break;
398
399 default:
The Android Open Source Project2949f582009-03-03 19:30:46 -0800400 ND_DEFAULTPRINT((const u_char *)ap, caplen);
401 return;
402 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800403
404 out:
405 ND_PRINT((ndo, ", length %u", length));
406
The Android Open Source Project2949f582009-03-03 19:30:46 -0800407 return;
408trunc:
JP Abgrall53f17a92014-02-12 14:02:41 -0800409 ND_PRINT((ndo, "[|ARP]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800410}
411
412/*
413 * Local Variables:
414 * c-style: bsd
415 * End:
416 */
417