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