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