xref: /openbsd-src/usr.sbin/tcpdump/print-arp.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 /*	$OpenBSD: print-arp.c,v 1.16 2020/01/24 22:46:36 procter 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 struct mbuf;
28 struct rtentry;
29 #include <net/if.h>
30 
31 #include <netinet/in.h>
32 #include <netinet/if_ether.h>
33 
34 #include <stdio.h>
35 #include <string.h>
36 
37 #include "interface.h"
38 #include "addrtoname.h"
39 #include "ethertype.h"
40 #include "extract.h"			/* must come after interface.h */
41 
42 /* Compatibility */
43 #ifndef REVARP_REQUEST
44 #define REVARP_REQUEST		3
45 #endif
46 #ifndef REVARP_REPLY
47 #define REVARP_REPLY		4
48 #endif
49 
50 static u_char ezero[6];
51 
52 void
53 arp_print(const u_char *bp, u_int length, u_int caplen)
54 {
55 	const struct ether_arp *ap;
56 	const struct ether_header *eh;
57 	u_short pro, hrd, op;
58 
59 	ap = (struct ether_arp *)bp;
60 	if ((u_char *)(ap + 1) > snapend) {
61 		printf("[|arp]");
62 		return;
63 	}
64 	if (length < sizeof(struct ether_arp)) {
65 		printf("truncated-arp");
66 		default_print((u_char *)ap, length);
67 		return;
68 	}
69 
70 	pro = EXTRACT_16BITS(&ap->arp_pro);
71 	hrd = EXTRACT_16BITS(&ap->arp_hrd);
72 	op = EXTRACT_16BITS(&ap->arp_op);
73 
74 	if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
75 	    || ap->arp_hln != sizeof(SHA(ap))
76 	    || ap->arp_pln != sizeof(SPA(ap))) {
77 		printf("arp-#%d for proto #%d (%d) hardware #%d (%d)",
78 		    op, pro, ap->arp_pln, hrd, ap->arp_hln);
79 		return;
80 	}
81 	if (pro == ETHERTYPE_TRAIL)
82 		printf("trailer-");
83 	eh = (struct ether_header *)packetp;
84 	switch (op) {
85 
86 	case ARPOP_REQUEST:
87 		printf("arp who-has %s", ipaddr_string(TPA(ap)));
88 		if (memcmp((char *)ezero, (char *)THA(ap), 6) != 0)
89 			printf(" (%s)", etheraddr_string(THA(ap)));
90 		printf(" tell %s", ipaddr_string(SPA(ap)));
91 		if (memcmp((char *)ESRC(eh), (char *)SHA(ap), 6) != 0)
92 			printf(" (%s)", etheraddr_string(SHA(ap)));
93 		break;
94 
95 	case ARPOP_REPLY:
96 		printf("arp reply %s", ipaddr_string(SPA(ap)));
97 		if (memcmp((char *)ESRC(eh), (char *)SHA(ap), 6) != 0)
98 			printf(" (%s)", etheraddr_string(SHA(ap)));
99 		printf(" is-at %s", etheraddr_string(SHA(ap)));
100 		if (memcmp((char *)EDST(eh), (char *)THA(ap), 6) != 0)
101 			printf(" (%s)", etheraddr_string(THA(ap)));
102 		break;
103 
104 	case REVARP_REQUEST:
105 		printf("rarp who-is %s tell %s",
106 		    etheraddr_string(THA(ap)),
107 		    etheraddr_string(SHA(ap)));
108 		break;
109 
110 	case REVARP_REPLY:
111 		printf("rarp reply %s at %s",
112 		    etheraddr_string(THA(ap)),
113 		    ipaddr_string(TPA(ap)));
114 		break;
115 
116 	default:
117 		printf("arp-#%d", op);
118 		default_print((u_char *)ap, caplen);
119 		return;
120 	}
121 	if (hrd != ARPHRD_ETHER)
122 		printf(" hardware #%d", hrd);
123 }
124