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 #include <sys/cdefs.h>
23 #ifndef lint
24 __RCSID("$NetBSD: print-arp.c,v 1.11 2024/09/02 16:15:30 christos Exp $");
25 #endif
26
27 /* \summary: Address Resolution Protocol (ARP) printer */
28
29 #include <config.h>
30
31 #include "netdissect-stdinc.h"
32
33 #define ND_LONGJMP_FROM_TCHECK
34 #include "netdissect.h"
35 #include "addrtoname.h"
36 #include "ethertype.h"
37 #include "extract.h"
38
39
40 /*
41 * Address Resolution Protocol.
42 *
43 * See RFC 826 for protocol description. ARP packets are variable
44 * in size; the arphdr structure defines the fixed-length portion.
45 * Protocol type values are the same as those for 10 Mb/s Ethernet.
46 * It is followed by the variable-sized fields ar_sha, arp_spa,
47 * arp_tha and arp_tpa in that order, according to the lengths
48 * specified. Field names used correspond to RFC 826.
49 */
50 struct arp_pkthdr {
51 nd_uint16_t ar_hrd; /* format of hardware address */
52 #define ARPHRD_ETHER 1 /* ethernet hardware format */
53 #define ARPHRD_IEEE802 6 /* token-ring hardware format */
54 #define ARPHRD_ARCNET 7 /* arcnet hardware format */
55 #define ARPHRD_FRELAY 15 /* frame relay hardware format */
56 #define ARPHRD_ATM2225 19 /* ATM (RFC 2225) */
57 #define ARPHRD_STRIP 23 /* Ricochet Starmode Radio hardware format */
58 #define ARPHRD_IEEE1394 24 /* IEEE 1394 (FireWire) hardware format */
59 #define ARPHRD_INFINIBAND 32 /* InfiniBand RFC 4391 */
60 nd_uint16_t ar_pro; /* format of protocol address */
61 nd_uint8_t ar_hln; /* length of hardware address */
62 nd_uint8_t ar_pln; /* length of protocol address */
63 nd_uint16_t 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 #define ARPOP_NAK 10 /* NAK - only valid for ATM ARP */
71
72 /*
73 * The remaining fields are variable in size,
74 * according to the sizes above.
75 */
76 #ifdef COMMENT_ONLY
77 nd_byte ar_sha[]; /* sender hardware address */
78 nd_byte ar_spa[]; /* sender protocol address */
79 nd_byte ar_tha[]; /* target hardware address */
80 nd_byte ar_tpa[]; /* target protocol address */
81 #endif
82 #define ar_sha(ap) (((const u_char *)((ap)+1))+ 0)
83 #define ar_spa(ap) (((const u_char *)((ap)+1))+ GET_U_1((ap)->ar_hln))
84 #define ar_tha(ap) (((const u_char *)((ap)+1))+ GET_U_1((ap)->ar_hln)+GET_U_1((ap)->ar_pln))
85 #define ar_tpa(ap) (((const u_char *)((ap)+1))+2*GET_U_1((ap)->ar_hln)+GET_U_1((ap)->ar_pln))
86 };
87
88 #define ARP_HDRLEN 8
89
90 #define HRD(ap) GET_BE_U_2((ap)->ar_hrd)
91 #define HRD_LEN(ap) GET_U_1((ap)->ar_hln)
92 #define PROTO_LEN(ap) GET_U_1((ap)->ar_pln)
93 #define OP(ap) GET_BE_U_2((ap)->ar_op)
94 #define PRO(ap) GET_BE_U_2((ap)->ar_pro)
95 #define SHA(ap) (ar_sha(ap))
96 #define SPA(ap) (ar_spa(ap))
97 #define THA(ap) (ar_tha(ap))
98 #define TPA(ap) (ar_tpa(ap))
99
100
101 static const struct tok arpop_values[] = {
102 { ARPOP_REQUEST, "Request" },
103 { ARPOP_REPLY, "Reply" },
104 { ARPOP_REVREQUEST, "Reverse Request" },
105 { ARPOP_REVREPLY, "Reverse Reply" },
106 { ARPOP_INVREQUEST, "Inverse Request" },
107 { ARPOP_INVREPLY, "Inverse Reply" },
108 { ARPOP_NAK, "NACK Reply" },
109 { 0, NULL }
110 };
111
112 static const struct tok arphrd_values[] = {
113 { ARPHRD_ETHER, "Ethernet" },
114 { ARPHRD_IEEE802, "TokenRing" },
115 { ARPHRD_ARCNET, "ArcNet" },
116 { ARPHRD_FRELAY, "FrameRelay" },
117 { ARPHRD_STRIP, "Strip" },
118 { ARPHRD_IEEE1394, "IEEE 1394" },
119 { ARPHRD_ATM2225, "ATM" },
120 { ARPHRD_INFINIBAND, "InfiniBand" },
121 { 0, NULL }
122 };
123
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 */
133 struct atmarp_pkthdr {
134 nd_uint16_t aar_hrd; /* format of hardware address */
135 nd_uint16_t aar_pro; /* format of protocol address */
136 nd_uint8_t aar_shtl; /* length of source ATM number */
137 nd_uint8_t 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 nd_uint16_t aar_op; /* same as regular ARP */
141 nd_uint8_t aar_spln; /* length of source protocol address */
142 nd_uint8_t aar_thtl; /* length of target ATM number */
143 nd_uint8_t aar_tstl; /* length of target ATM subaddress */
144 nd_uint8_t aar_tpln; /* length of target protocol address */
145 /*
146 * The remaining fields are variable in size,
147 * according to the sizes above.
148 */
149 #ifdef COMMENT_ONLY
150 nd_byte aar_sha[]; /* source ATM number */
151 nd_byte aar_ssa[]; /* source ATM subaddress */
152 nd_byte aar_spa[]; /* sender protocol address */
153 nd_byte aar_tha[]; /* target ATM number */
154 nd_byte aar_tsa[]; /* target ATM subaddress */
155 nd_byte aar_tpa[]; /* target protocol address */
156 #endif
157
158 #define ATMHRD(ap) GET_BE_U_2((ap)->aar_hrd)
159 #define ATMSHRD_LEN(ap) (GET_U_1((ap)->aar_shtl) & ATMARP_LEN_MASK)
160 #define ATMSSLN(ap) (GET_U_1((ap)->aar_sstl) & ATMARP_LEN_MASK)
161 #define ATMSPROTO_LEN(ap) GET_U_1((ap)->aar_spln)
162 #define ATMOP(ap) GET_BE_U_2((ap)->aar_op)
163 #define ATMPRO(ap) GET_BE_U_2((ap)->aar_pro)
164 #define ATMTHRD_LEN(ap) (GET_U_1((ap)->aar_thtl) & ATMARP_LEN_MASK)
165 #define ATMTSLN(ap) (GET_U_1((ap)->aar_tstl) & ATMARP_LEN_MASK)
166 #define ATMTPROTO_LEN(ap) GET_U_1((ap)->aar_tpln)
167 #define aar_sha(ap) ((const u_char *)((ap)+1))
168 #define aar_ssa(ap) (aar_sha(ap) + ATMSHRD_LEN(ap))
169 #define aar_spa(ap) (aar_ssa(ap) + ATMSSLN(ap))
170 #define aar_tha(ap) (aar_spa(ap) + ATMSPROTO_LEN(ap))
171 #define aar_tsa(ap) (aar_tha(ap) + ATMTHRD_LEN(ap))
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
182 static int
isnonzero(netdissect_options * ndo,const u_char * a,size_t len)183 isnonzero(netdissect_options *ndo, const u_char *a, size_t len)
184 {
185 while (len > 0) {
186 if (GET_U_1(a) != 0)
187 return (1);
188 a++;
189 len--;
190 }
191 return (0);
192 }
193
194 static void
tpaddr_print_ip(netdissect_options * ndo,const struct arp_pkthdr * ap,u_short pro)195 tpaddr_print_ip(netdissect_options *ndo,
196 const struct arp_pkthdr *ap, u_short pro)
197 {
198 if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
199 ND_PRINT("<wrong proto type>");
200 else if (PROTO_LEN(ap) != 4)
201 ND_PRINT("<wrong len>");
202 else
203 ND_PRINT("%s", GET_IPADDR_STRING(TPA(ap)));
204 }
205
206 static void
spaddr_print_ip(netdissect_options * ndo,const struct arp_pkthdr * ap,u_short pro)207 spaddr_print_ip(netdissect_options *ndo,
208 const struct arp_pkthdr *ap, u_short pro)
209 {
210 if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
211 ND_PRINT("<wrong proto type>");
212 else if (PROTO_LEN(ap) != 4)
213 ND_PRINT("<wrong len>");
214 else
215 ND_PRINT("%s", GET_IPADDR_STRING(SPA(ap)));
216 }
217
218 static void
atmarp_addr_print(netdissect_options * ndo,const u_char * ha,u_int ha_len,const u_char * srca,u_int srca_len)219 atmarp_addr_print(netdissect_options *ndo,
220 const u_char *ha, u_int ha_len, const u_char *srca,
221 u_int srca_len)
222 {
223 if (ha_len == 0)
224 ND_PRINT("<No address>");
225 else {
226 ND_PRINT("%s", GET_LINKADDR_STRING(ha, LINKADDR_ATM, ha_len));
227 if (srca_len != 0)
228 ND_PRINT(",%s",
229 GET_LINKADDR_STRING(srca, LINKADDR_ATM, srca_len));
230 }
231 }
232
233 static void
atmarp_tpaddr_print(netdissect_options * ndo,const struct atmarp_pkthdr * ap,u_short pro)234 atmarp_tpaddr_print(netdissect_options *ndo,
235 const struct atmarp_pkthdr *ap, u_short pro)
236 {
237 if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
238 ND_PRINT("<wrong proto type>");
239 else if (ATMTPROTO_LEN(ap) != 4)
240 ND_PRINT("<wrong tplen>");
241 else
242 ND_PRINT("%s", GET_IPADDR_STRING(ATMTPA(ap)));
243 }
244
245 static void
atmarp_spaddr_print(netdissect_options * ndo,const struct atmarp_pkthdr * ap,u_short pro)246 atmarp_spaddr_print(netdissect_options *ndo,
247 const struct atmarp_pkthdr *ap, u_short pro)
248 {
249 if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
250 ND_PRINT("<wrong proto type>");
251 else if (ATMSPROTO_LEN(ap) != 4)
252 ND_PRINT("<wrong splen>");
253 else
254 ND_PRINT("%s", GET_IPADDR_STRING(ATMSPA(ap)));
255 }
256
257 static void
atmarp_print(netdissect_options * ndo,const u_char * bp,u_int length,u_int caplen)258 atmarp_print(netdissect_options *ndo,
259 const u_char *bp, u_int length, u_int caplen)
260 {
261 const struct atmarp_pkthdr *ap;
262 u_short pro, hrd, op;
263
264 ap = (const struct atmarp_pkthdr *)bp;
265 ND_TCHECK_SIZE(ap);
266
267 hrd = ATMHRD(ap);
268 pro = ATMPRO(ap);
269 op = ATMOP(ap);
270
271 ND_TCHECK_LEN(ATMTPA(ap), ATMTPROTO_LEN(ap));
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 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 }
349
350 void
arp_print(netdissect_options * ndo,const u_char * bp,u_int length,u_int caplen)351 arp_print(netdissect_options *ndo,
352 const u_char *bp, u_int length, u_int caplen)
353 {
354 const struct arp_pkthdr *ap;
355 u_short pro, hrd, op, linkaddr;
356
357 ndo->ndo_protocol = "arp";
358 ap = (const struct arp_pkthdr *)bp;
359 ND_TCHECK_SIZE(ap);
360
361 hrd = HRD(ap);
362 pro = PRO(ap);
363 op = OP(ap);
364
365
366 /* if its ATM then call the ATM ARP printer
367 for Frame-relay ARP most of the fields
368 are similar to Ethernet so overload the Ethernet Printer
369 and set the linkaddr type for GET_LINKADDR_STRING() accordingly */
370
371 switch(hrd) {
372 case ARPHRD_ATM2225:
373 atmarp_print(ndo, bp, length, caplen);
374 return;
375 case ARPHRD_FRELAY:
376 linkaddr = LINKADDR_FRELAY;
377 break;
378 default:
379 linkaddr = LINKADDR_ETHER;
380 break;
381 }
382
383 ND_TCHECK_LEN(TPA(ap), PROTO_LEN(ap));
384
385 if (!ndo->ndo_eflag) {
386 ND_PRINT("ARP, ");
387 }
388
389 /* print hardware type/len and proto type/len */
390 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
391 PROTO_LEN(ap) != 4 ||
392 HRD_LEN(ap) == 0 ||
393 ndo->ndo_vflag) {
394 ND_PRINT("%s (len %u), %s (len %u)",
395 tok2str(arphrd_values, "Unknown Hardware (%u)", hrd),
396 HRD_LEN(ap),
397 tok2str(ethertype_values, "Unknown Protocol (0x%04x)", pro),
398 PROTO_LEN(ap));
399
400 /* don't know about the address formats */
401 if (!ndo->ndo_vflag) {
402 goto out;
403 }
404 }
405
406 /* print operation */
407 ND_PRINT("%s%s ",
408 ndo->ndo_vflag ? ", " : "",
409 tok2str(arpop_values, "Unknown (%u)", op));
410
411 switch (op) {
412
413 case ARPOP_REQUEST:
414 ND_PRINT("who-has ");
415 tpaddr_print_ip(ndo, ap, pro);
416 if (isnonzero(ndo, (const u_char *)THA(ap), HRD_LEN(ap)))
417 ND_PRINT(" (%s)",
418 GET_LINKADDR_STRING(THA(ap), linkaddr, HRD_LEN(ap)));
419 ND_PRINT(" tell ");
420 spaddr_print_ip(ndo, ap, pro);
421 break;
422
423 case ARPOP_REPLY:
424 spaddr_print_ip(ndo, ap, pro);
425 ND_PRINT(" is-at %s",
426 GET_LINKADDR_STRING(SHA(ap), linkaddr, HRD_LEN(ap)));
427 break;
428
429 case ARPOP_REVREQUEST:
430 /*
431 * XXX - GET_LINKADDR_STRING() may return a pointer to
432 * a static buffer, so we only have one call to it per
433 * ND_PRINT() call.
434 *
435 * This should be done in a cleaner fashion.
436 */
437 ND_PRINT("who-is %s",
438 GET_LINKADDR_STRING(THA(ap), linkaddr, HRD_LEN(ap)));
439 ND_PRINT(" tell %s",
440 GET_LINKADDR_STRING(SHA(ap), linkaddr, HRD_LEN(ap)));
441 break;
442
443 case ARPOP_REVREPLY:
444 ND_PRINT("%s at ",
445 GET_LINKADDR_STRING(THA(ap), linkaddr, HRD_LEN(ap)));
446 tpaddr_print_ip(ndo, ap, pro);
447 break;
448
449 case ARPOP_INVREQUEST:
450 /*
451 * XXX - GET_LINKADDR_STRING() may return a pointer to
452 * a static buffer, so we only have one call to it per
453 * ND_PRINT() call.
454 *
455 * This should be done in a cleaner fashion.
456 */
457 ND_PRINT("who-is %s",
458 GET_LINKADDR_STRING(THA(ap), linkaddr, HRD_LEN(ap)));
459 ND_PRINT(" tell %s",
460 GET_LINKADDR_STRING(SHA(ap), linkaddr, HRD_LEN(ap)));
461 break;
462
463 case ARPOP_INVREPLY:
464 ND_PRINT("%s at ",
465 GET_LINKADDR_STRING(SHA(ap), linkaddr, HRD_LEN(ap)));
466 spaddr_print_ip(ndo, ap, pro);
467 break;
468
469 default:
470 ND_DEFAULTPRINT((const u_char *)ap, caplen);
471 return;
472 }
473
474 out:
475 ND_PRINT(", length %u", length);
476 }
477