xref: /openbsd/usr.sbin/tcpdump/print-arp.c (revision f6aab3d8)
1 /*	$OpenBSD: print-arp.c,v 1.17 2021/12/01 18:28:45 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that: (1) source code distributions
9  * retain the above copyright notice and this paragraph in its entirety, (2)
10  * distributions including binary code include the above copyright notice and
11  * this paragraph in its entirety in the documentation or other materials
12  * provided with the distribution, and (3) all advertising materials mentioning
13  * features or use of this software display the following acknowledgement:
14  * ``This product includes software developed by the University of California,
15  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16  * the University nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior
18  * written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  */
23 
24 #include <sys/time.h>
25 #include <sys/socket.h>
26 
27 #include <net/if.h>
28 
29 #include <netinet/in.h>
30 #include <netinet/if_ether.h>
31 
32 #include <stdio.h>
33 #include <string.h>
34 
35 #include "interface.h"
36 #include "addrtoname.h"
37 #include "ethertype.h"
38 #include "extract.h"			/* must come after interface.h */
39 
40 /* Compatibility */
41 #ifndef REVARP_REQUEST
42 #define REVARP_REQUEST		3
43 #endif
44 #ifndef REVARP_REPLY
45 #define REVARP_REPLY		4
46 #endif
47 
48 static u_char ezero[6];
49 
50 void
51 arp_print(const u_char *bp, u_int length, u_int caplen)
52 {
53 	const struct ether_arp *ap;
54 	const struct ether_header *eh;
55 	u_short pro, hrd, op;
56 
57 	ap = (struct ether_arp *)bp;
58 	if ((u_char *)(ap + 1) > snapend) {
59 		printf("[|arp]");
60 		return;
61 	}
62 	if (length < sizeof(struct ether_arp)) {
63 		printf("truncated-arp");
64 		default_print((u_char *)ap, length);
65 		return;
66 	}
67 
68 	pro = EXTRACT_16BITS(&ap->arp_pro);
69 	hrd = EXTRACT_16BITS(&ap->arp_hrd);
70 	op = EXTRACT_16BITS(&ap->arp_op);
71 
72 	if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
73 	    || ap->arp_hln != sizeof(SHA(ap))
74 	    || ap->arp_pln != sizeof(SPA(ap))) {
75 		printf("arp-#%d for proto #%d (%d) hardware #%d (%d)",
76 		    op, pro, ap->arp_pln, hrd, ap->arp_hln);
77 		return;
78 	}
79 	if (pro == ETHERTYPE_TRAIL)
80 		printf("trailer-");
81 	eh = (struct ether_header *)packetp;
82 	switch (op) {
83 
84 	case ARPOP_REQUEST:
85 		printf("arp who-has %s", ipaddr_string(TPA(ap)));
86 		if (memcmp((char *)ezero, (char *)THA(ap), 6) != 0)
87 			printf(" (%s)", etheraddr_string(THA(ap)));
88 		printf(" tell %s", ipaddr_string(SPA(ap)));
89 		if (memcmp((char *)ESRC(eh), (char *)SHA(ap), 6) != 0)
90 			printf(" (%s)", etheraddr_string(SHA(ap)));
91 		break;
92 
93 	case ARPOP_REPLY:
94 		printf("arp reply %s", ipaddr_string(SPA(ap)));
95 		if (memcmp((char *)ESRC(eh), (char *)SHA(ap), 6) != 0)
96 			printf(" (%s)", etheraddr_string(SHA(ap)));
97 		printf(" is-at %s", etheraddr_string(SHA(ap)));
98 		if (memcmp((char *)EDST(eh), (char *)THA(ap), 6) != 0)
99 			printf(" (%s)", etheraddr_string(THA(ap)));
100 		break;
101 
102 	case REVARP_REQUEST:
103 		printf("rarp who-is %s tell %s",
104 		    etheraddr_string(THA(ap)),
105 		    etheraddr_string(SHA(ap)));
106 		break;
107 
108 	case REVARP_REPLY:
109 		printf("rarp reply %s at %s",
110 		    etheraddr_string(THA(ap)),
111 		    ipaddr_string(TPA(ap)));
112 		break;
113 
114 	default:
115 		printf("arp-#%d", op);
116 		default_print((u_char *)ap, caplen);
117 		return;
118 	}
119 	if (hrd != ARPHRD_ETHER)
120 		printf(" hardware #%d", hrd);
121 }
122