]> The Tcpdump Group git mirrors - tcpdump/blob - print-arp.c
Clean up whitespaces/indentation
[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 /* \summary: Address Resolution Protocol (ARP) printer */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "netdissect-stdinc.h"
29
30 #include <string.h>
31
32 #include "netdissect.h"
33 #include "addrtoname.h"
34 #include "ethertype.h"
35 #include "extract.h"
36
37
38 /*
39 * Address Resolution Protocol.
40 *
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.
47 */
48 struct arp_pkthdr {
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 nd_uint16_t ar_pro; /* format of protocol address */
58 nd_uint8_t ar_hln; /* length of hardware address */
59 nd_uint8_t ar_pln; /* length of protocol address */
60 nd_uint16_t ar_op; /* one of: */
61 #define ARPOP_REQUEST 1 /* request to resolve address */
62 #define ARPOP_REPLY 2 /* response to previous request */
63 #define ARPOP_REVREQUEST 3 /* request protocol address given hardware */
64 #define ARPOP_REVREPLY 4 /* response giving protocol address */
65 #define ARPOP_INVREQUEST 8 /* request to identify peer */
66 #define ARPOP_INVREPLY 9 /* response identifying peer */
67 #define ARPOP_NAK 10 /* NAK - only valif for ATM ARP */
68
69 /*
70 * The remaining fields are variable in size,
71 * according to the sizes above.
72 */
73 #ifdef COMMENT_ONLY
74 nd_byte ar_sha[]; /* sender hardware address */
75 nd_byte ar_spa[]; /* sender protocol address */
76 nd_byte ar_tha[]; /* target hardware address */
77 nd_byte ar_tpa[]; /* target protocol address */
78 #endif
79 #define ar_sha(ap) (((const u_char *)((ap)+1))+ 0)
80 #define ar_spa(ap) (((const u_char *)((ap)+1))+ EXTRACT_U_1((ap)->ar_hln))
81 #define ar_tha(ap) (((const u_char *)((ap)+1))+ EXTRACT_U_1((ap)->ar_hln)+EXTRACT_U_1((ap)->ar_pln))
82 #define ar_tpa(ap) (((const u_char *)((ap)+1))+2*EXTRACT_U_1((ap)->ar_hln)+EXTRACT_U_1((ap)->ar_pln))
83 };
84
85 #define ARP_HDRLEN 8
86
87 #define HRD(ap) EXTRACT_BE_U_2((ap)->ar_hrd)
88 #define HRD_LEN(ap) EXTRACT_U_1((ap)->ar_hln)
89 #define PROTO_LEN(ap) EXTRACT_U_1((ap)->ar_pln)
90 #define OP(ap) EXTRACT_BE_U_2((ap)->ar_op)
91 #define PRO(ap) EXTRACT_BE_U_2((ap)->ar_pro)
92 #define SHA(ap) (ar_sha(ap))
93 #define SPA(ap) (ar_spa(ap))
94 #define THA(ap) (ar_tha(ap))
95 #define TPA(ap) (ar_tpa(ap))
96
97
98 static const struct tok arpop_values[] = {
99 { ARPOP_REQUEST, "Request" },
100 { ARPOP_REPLY, "Reply" },
101 { ARPOP_REVREQUEST, "Reverse Request" },
102 { ARPOP_REVREPLY, "Reverse Reply" },
103 { ARPOP_INVREQUEST, "Inverse Request" },
104 { ARPOP_INVREPLY, "Inverse Reply" },
105 { ARPOP_NAK, "NACK Reply" },
106 { 0, NULL }
107 };
108
109 static const struct tok arphrd_values[] = {
110 { ARPHRD_ETHER, "Ethernet" },
111 { ARPHRD_IEEE802, "TokenRing" },
112 { ARPHRD_ARCNET, "ArcNet" },
113 { ARPHRD_FRELAY, "FrameRelay" },
114 { ARPHRD_STRIP, "Strip" },
115 { ARPHRD_IEEE1394, "IEEE 1394" },
116 { ARPHRD_ATM2225, "ATM" },
117 { 0, NULL }
118 };
119
120 /*
121 * ATM Address Resolution Protocol.
122 *
123 * See RFC 2225 for protocol description. ATMARP packets are similar
124 * to ARP packets, except that there are no length fields for the
125 * protocol address - instead, there are type/length fields for
126 * the ATM number and subaddress - and the hardware addresses consist
127 * of an ATM number and an ATM subaddress.
128 */
129 struct atmarp_pkthdr {
130 nd_uint16_t aar_hrd; /* format of hardware address */
131 nd_uint16_t aar_pro; /* format of protocol address */
132 nd_uint8_t aar_shtl; /* length of source ATM number */
133 nd_uint8_t aar_sstl; /* length of source ATM subaddress */
134 #define ATMARP_IS_E164 0x40 /* bit in type/length for E.164 format */
135 #define ATMARP_LEN_MASK 0x3F /* length of {sub}address in type/length */
136 nd_uint16_t aar_op; /* same as regular ARP */
137 nd_uint8_t aar_spln; /* length of source protocol address */
138 nd_uint8_t aar_thtl; /* length of target ATM number */
139 nd_uint8_t aar_tstl; /* length of target ATM subaddress */
140 nd_uint8_t aar_tpln; /* length of target protocol address */
141 /*
142 * The remaining fields are variable in size,
143 * according to the sizes above.
144 */
145 #ifdef COMMENT_ONLY
146 nd_byte aar_sha[]; /* source ATM number */
147 nd_byte aar_ssa[]; /* source ATM subaddress */
148 nd_byte aar_spa[]; /* sender protocol address */
149 nd_byte aar_tha[]; /* target ATM number */
150 nd_byte aar_tsa[]; /* target ATM subaddress */
151 nd_byte aar_tpa[]; /* target protocol address */
152 #endif
153
154 #define ATMHRD(ap) EXTRACT_BE_U_2((ap)->aar_hrd)
155 #define ATMSHRD_LEN(ap) (EXTRACT_U_1((ap)->aar_shtl) & ATMARP_LEN_MASK)
156 #define ATMSSLN(ap) (EXTRACT_U_1((ap)->aar_sstl) & ATMARP_LEN_MASK)
157 #define ATMSPROTO_LEN(ap) EXTRACT_U_1((ap)->aar_spln)
158 #define ATMOP(ap) EXTRACT_BE_U_2((ap)->aar_op)
159 #define ATMPRO(ap) EXTRACT_BE_U_2((ap)->aar_pro)
160 #define ATMTHRD_LEN(ap) (EXTRACT_U_1((ap)->aar_thtl) & ATMARP_LEN_MASK)
161 #define ATMTSLN(ap) (EXTRACT_U_1((ap)->aar_tstl) & ATMARP_LEN_MASK)
162 #define ATMTPROTO_LEN(ap) EXTRACT_U_1((ap)->aar_tpln)
163 #define aar_sha(ap) ((const u_char *)((ap)+1))
164 #define aar_ssa(ap) (aar_sha(ap) + ATMSHRD_LEN(ap))
165 #define aar_spa(ap) (aar_ssa(ap) + ATMSSLN(ap))
166 #define aar_tha(ap) (aar_spa(ap) + ATMSPROTO_LEN(ap))
167 #define aar_tsa(ap) (aar_tha(ap) + ATMTHRD_LEN(ap))
168 #define aar_tpa(ap) (aar_tsa(ap) + ATMTSLN(ap))
169 };
170
171 #define ATMSHA(ap) (aar_sha(ap))
172 #define ATMSSA(ap) (aar_ssa(ap))
173 #define ATMSPA(ap) (aar_spa(ap))
174 #define ATMTHA(ap) (aar_tha(ap))
175 #define ATMTSA(ap) (aar_tsa(ap))
176 #define ATMTPA(ap) (aar_tpa(ap))
177
178 static int
179 isnonzero(const u_char *a, size_t len)
180 {
181 while (len > 0) {
182 if (EXTRACT_U_1(a) != 0)
183 return (1);
184 a++;
185 len--;
186 }
187 return (0);
188 }
189
190 static void
191 tpaddr_print_ip(netdissect_options *ndo,
192 const struct arp_pkthdr *ap, u_short pro)
193 {
194 if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
195 ND_PRINT("<wrong proto type>");
196 else if (PROTO_LEN(ap) != 4)
197 ND_PRINT("<wrong len>");
198 else
199 ND_PRINT("%s", ipaddr_string(ndo, TPA(ap)));
200 }
201
202 static void
203 spaddr_print_ip(netdissect_options *ndo,
204 const struct arp_pkthdr *ap, u_short pro)
205 {
206 if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
207 ND_PRINT("<wrong proto type>");
208 else if (PROTO_LEN(ap) != 4)
209 ND_PRINT("<wrong len>");
210 else
211 ND_PRINT("%s", ipaddr_string(ndo, SPA(ap)));
212 }
213
214 static void
215 atmarp_addr_print(netdissect_options *ndo,
216 const u_char *ha, u_int ha_len, const u_char *srca,
217 u_int srca_len)
218 {
219 if (ha_len == 0)
220 ND_PRINT("<No address>");
221 else {
222 ND_PRINT("%s", linkaddr_string(ndo, ha, LINKADDR_ATM, ha_len));
223 if (srca_len != 0)
224 ND_PRINT(",%s",
225 linkaddr_string(ndo, srca, LINKADDR_ATM, srca_len));
226 }
227 }
228
229 static void
230 atmarp_tpaddr_print(netdissect_options *ndo,
231 const struct atmarp_pkthdr *ap, u_short pro)
232 {
233 if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
234 ND_PRINT("<wrong proto type>");
235 else if (ATMTPROTO_LEN(ap) != 4)
236 ND_PRINT("<wrong tplen>");
237 else
238 ND_PRINT("%s", ipaddr_string(ndo, ATMTPA(ap)));
239 }
240
241 static void
242 atmarp_spaddr_print(netdissect_options *ndo,
243 const struct atmarp_pkthdr *ap, u_short pro)
244 {
245 if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
246 ND_PRINT("<wrong proto type>");
247 else if (ATMSPROTO_LEN(ap) != 4)
248 ND_PRINT("<wrong splen>");
249 else
250 ND_PRINT("%s", ipaddr_string(ndo, ATMSPA(ap)));
251 }
252
253 static void
254 atmarp_print(netdissect_options *ndo,
255 const u_char *bp, u_int length, u_int caplen)
256 {
257 const struct atmarp_pkthdr *ap;
258 u_short pro, hrd, op;
259
260 ap = (const struct atmarp_pkthdr *)bp;
261 ND_TCHECK_SIZE(ap);
262
263 hrd = ATMHRD(ap);
264 pro = ATMPRO(ap);
265 op = ATMOP(ap);
266
267 if (!ND_TTEST_LEN(aar_tpa(ap), ATMTPROTO_LEN(ap))) {
268 nd_print_trunc(ndo);
269 ND_DEFAULTPRINT((const u_char *)ap, length);
270 return;
271 }
272
273 if (!ndo->ndo_eflag) {
274 ND_PRINT("ARP, ");
275 }
276
277 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
278 ATMSPROTO_LEN(ap) != 4 ||
279 ATMTPROTO_LEN(ap) != 4 ||
280 ndo->ndo_vflag) {
281 ND_PRINT("%s, %s (len %u/%u)",
282 tok2str(arphrd_values, "Unknown Hardware (%u)", hrd),
283 tok2str(ethertype_values, "Unknown Protocol (0x%04x)", pro),
284 ATMSPROTO_LEN(ap),
285 ATMTPROTO_LEN(ap));
286
287 /* don't know know about the address formats */
288 if (!ndo->ndo_vflag) {
289 goto out;
290 }
291 }
292
293 /* print operation */
294 ND_PRINT("%s%s ",
295 ndo->ndo_vflag ? ", " : "",
296 tok2str(arpop_values, "Unknown (%u)", op));
297
298 switch (op) {
299
300 case ARPOP_REQUEST:
301 ND_PRINT("who-has ");
302 atmarp_tpaddr_print(ndo, ap, pro);
303 if (ATMTHRD_LEN(ap) != 0) {
304 ND_PRINT(" (");
305 atmarp_addr_print(ndo, ATMTHA(ap), ATMTHRD_LEN(ap),
306 ATMTSA(ap), ATMTSLN(ap));
307 ND_PRINT(")");
308 }
309 ND_PRINT(" tell ");
310 atmarp_spaddr_print(ndo, ap, pro);
311 break;
312
313 case ARPOP_REPLY:
314 atmarp_spaddr_print(ndo, ap, pro);
315 ND_PRINT(" is-at ");
316 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
317 ATMSSLN(ap));
318 break;
319
320 case ARPOP_INVREQUEST:
321 ND_PRINT("who-is ");
322 atmarp_addr_print(ndo, ATMTHA(ap), ATMTHRD_LEN(ap), ATMTSA(ap),
323 ATMTSLN(ap));
324 ND_PRINT(" tell ");
325 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
326 ATMSSLN(ap));
327 break;
328
329 case ARPOP_INVREPLY:
330 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
331 ATMSSLN(ap));
332 ND_PRINT("at ");
333 atmarp_spaddr_print(ndo, ap, pro);
334 break;
335
336 case ARPOP_NAK:
337 ND_PRINT("for ");
338 atmarp_spaddr_print(ndo, ap, pro);
339 break;
340
341 default:
342 ND_DEFAULTPRINT((const u_char *)ap, caplen);
343 return;
344 }
345
346 out:
347 ND_PRINT(", length %u", length);
348 return;
349
350 trunc:
351 nd_print_trunc(ndo);
352 }
353
354 void
355 arp_print(netdissect_options *ndo,
356 const u_char *bp, u_int length, u_int caplen)
357 {
358 const struct arp_pkthdr *ap;
359 u_short pro, hrd, op, linkaddr;
360
361 ndo->ndo_protocol = "arp";
362 ap = (const struct arp_pkthdr *)bp;
363 ND_TCHECK_SIZE(ap);
364
365 hrd = HRD(ap);
366 pro = PRO(ap);
367 op = OP(ap);
368
369
370 /* if its ATM then call the ATM ARP printer
371 for Frame-relay ARP most of the fields
372 are similar to Ethernet so overload the Ethernet Printer
373 and set the linkaddr type for linkaddr_string(ndo, ) accordingly */
374
375 switch(hrd) {
376 case ARPHRD_ATM2225:
377 atmarp_print(ndo, bp, length, caplen);
378 return;
379 case ARPHRD_FRELAY:
380 linkaddr = LINKADDR_FRELAY;
381 break;
382 default:
383 linkaddr = LINKADDR_ETHER;
384 break;
385 }
386
387 if (!ND_TTEST_LEN(TPA(ap), PROTO_LEN(ap))) {
388 nd_print_trunc(ndo);
389 ND_DEFAULTPRINT((const u_char *)ap, length);
390 return;
391 }
392
393 if (!ndo->ndo_eflag) {
394 ND_PRINT("ARP, ");
395 }
396
397 /* print hardware type/len and proto type/len */
398 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
399 PROTO_LEN(ap) != 4 ||
400 HRD_LEN(ap) == 0 ||
401 ndo->ndo_vflag) {
402 ND_PRINT("%s (len %u), %s (len %u)",
403 tok2str(arphrd_values, "Unknown Hardware (%u)", hrd),
404 HRD_LEN(ap),
405 tok2str(ethertype_values, "Unknown Protocol (0x%04x)", pro),
406 PROTO_LEN(ap));
407
408 /* don't know know about the address formats */
409 if (!ndo->ndo_vflag) {
410 goto out;
411 }
412 }
413
414 /* print operation */
415 ND_PRINT("%s%s ",
416 ndo->ndo_vflag ? ", " : "",
417 tok2str(arpop_values, "Unknown (%u)", op));
418
419 switch (op) {
420
421 case ARPOP_REQUEST:
422 ND_PRINT("who-has ");
423 tpaddr_print_ip(ndo, ap, pro);
424 if (isnonzero((const u_char *)THA(ap), HRD_LEN(ap)))
425 ND_PRINT(" (%s)",
426 linkaddr_string(ndo, THA(ap), linkaddr, HRD_LEN(ap)));
427 ND_PRINT(" tell ");
428 spaddr_print_ip(ndo, ap, pro);
429 break;
430
431 case ARPOP_REPLY:
432 spaddr_print_ip(ndo, ap, pro);
433 ND_PRINT(" is-at %s",
434 linkaddr_string(ndo, SHA(ap), linkaddr, HRD_LEN(ap)));
435 break;
436
437 case ARPOP_REVREQUEST:
438 ND_PRINT("who-is %s tell %s",
439 linkaddr_string(ndo, THA(ap), linkaddr, HRD_LEN(ap)),
440 linkaddr_string(ndo, SHA(ap), linkaddr, HRD_LEN(ap)));
441 break;
442
443 case ARPOP_REVREPLY:
444 ND_PRINT("%s at ",
445 linkaddr_string(ndo, THA(ap), linkaddr, HRD_LEN(ap)));
446 tpaddr_print_ip(ndo, ap, pro);
447 break;
448
449 case ARPOP_INVREQUEST:
450 ND_PRINT("who-is %s tell %s",
451 linkaddr_string(ndo, THA(ap), linkaddr, HRD_LEN(ap)),
452 linkaddr_string(ndo, SHA(ap), linkaddr, HRD_LEN(ap)));
453 break;
454
455 case ARPOP_INVREPLY:
456 ND_PRINT("%s at ",
457 linkaddr_string(ndo, SHA(ap), linkaddr, HRD_LEN(ap)));
458 spaddr_print_ip(ndo, ap, pro);
459 break;
460
461 default:
462 ND_DEFAULTPRINT((const u_char *)ap, caplen);
463 return;
464 }
465
466 out:
467 ND_PRINT(", length %u", length);
468
469 return;
470 trunc:
471 nd_print_trunc(ndo);
472 }