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