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