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