]> The Tcpdump Group git mirrors - tcpdump/blob - print-arp.c
Get rid of some includes that aren't needed; the include of <net/if.h>,
[tcpdump] / print-arp.c
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: /tcpdump/master/tcpdump/print-arp.c,v 1.47 2000-09-24 07:42:31 guy Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/socket.h>
34
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "interface.h"
39 #include "addrtoname.h"
40 #include "ether.h"
41 #include "ethertype.h"
42 #include "extract.h" /* must come after interface.h */
43
44 /*
45 * Address Resolution Protocol.
46 *
47 * See RFC 826 for protocol description. ARP packets are variable
48 * in size; the arphdr structure defines the fixed-length portion.
49 * Protocol type values are the same as those for 10 Mb/s Ethernet.
50 * It is followed by the variable-sized fields ar_sha, arp_spa,
51 * arp_tha and arp_tpa in that order, according to the lengths
52 * specified. Field names used correspond to RFC 826.
53 */
54 struct arphdr {
55 u_short ar_hrd; /* format of hardware address */
56 #define ARPHRD_ETHER 1 /* ethernet hardware format */
57 #define ARPHRD_IEEE802 6 /* token-ring hardware format */
58 #define ARPHRD_FRELAY 15 /* frame relay hardware format */
59 u_short ar_pro; /* format of protocol address */
60 u_char ar_hln; /* length of hardware address */
61 u_char ar_pln; /* length of protocol address */
62 u_short ar_op; /* one of: */
63 #define ARPOP_REQUEST 1 /* request to resolve address */
64 #define ARPOP_REPLY 2 /* response to previous request */
65 #define ARPOP_REVREQUEST 3 /* request protocol address given hardware */
66 #define ARPOP_REVREPLY 4 /* response giving protocol address */
67 #define ARPOP_INVREQUEST 8 /* request to identify peer */
68 #define ARPOP_INVREPLY 9 /* response identifying peer */
69 /*
70 * The remaining fields are variable in size,
71 * according to the sizes above.
72 */
73 #ifdef COMMENT_ONLY
74 u_char ar_sha[]; /* sender hardware address */
75 u_char ar_spa[]; /* sender protocol address */
76 u_char ar_tha[]; /* target hardware address */
77 u_char ar_tpa[]; /* target protocol address */
78 #endif
79 };
80
81 /*
82 * Ethernet Address Resolution Protocol.
83 *
84 * See RFC 826 for protocol description. Structure below is adapted
85 * to resolving internet addresses. Field names used correspond to
86 * RFC 826.
87 */
88 struct ether_arp {
89 struct arphdr ea_hdr; /* fixed-size header */
90 u_char arp_sha[6]; /* sender hardware address */
91 u_char arp_spa[4]; /* sender protocol address */
92 u_char arp_tha[6]; /* target hardware address */
93 u_char arp_tpa[4]; /* target protocol address */
94 };
95 #define arp_hrd ea_hdr.ar_hrd
96 #define arp_pro ea_hdr.ar_pro
97 #define arp_hln ea_hdr.ar_hln
98 #define arp_pln ea_hdr.ar_pln
99 #define arp_op ea_hdr.ar_op
100
101 #define SHA(ap) ((ap)->arp_sha)
102 #define THA(ap) ((ap)->arp_tha)
103 #define SPA(ap) ((ap)->arp_spa)
104 #define TPA(ap) ((ap)->arp_tpa)
105
106 /* Compatibility */
107 #ifndef REVARP_REQUEST
108 #define REVARP_REQUEST 3
109 #endif
110 #ifndef REVARP_REPLY
111 #define REVARP_REPLY 4
112 #endif
113
114 static u_char ezero[6];
115
116 void
117 arp_print(register const u_char *bp, u_int length, u_int caplen)
118 {
119 register const struct ether_arp *ap;
120 register const struct ether_header *eh;
121 register u_short pro, hrd, op;
122
123 ap = (struct ether_arp *)bp;
124 if ((u_char *)(ap + 1) > snapend) {
125 printf("[|arp]");
126 return;
127 }
128 if (length < sizeof(struct ether_arp)) {
129 (void)printf("truncated-arp");
130 default_print((u_char *)ap, length);
131 return;
132 }
133
134 pro = EXTRACT_16BITS(&ap->arp_pro);
135 hrd = EXTRACT_16BITS(&ap->arp_hrd);
136 op = EXTRACT_16BITS(&ap->arp_op);
137
138 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
139 || ap->arp_hln != sizeof(SHA(ap))
140 || ap->arp_pln != sizeof(SPA(ap))) {
141 (void)printf("arp-#%d for proto #%d (%d) hardware #%d (%d)",
142 op, pro, ap->arp_pln,
143 hrd, ap->arp_hln);
144 return;
145 }
146 if (pro == ETHERTYPE_TRAIL)
147 (void)printf("trailer-");
148 eh = (struct ether_header *)packetp;
149 switch (op) {
150
151 case ARPOP_REQUEST:
152 (void)printf("arp who-has %s", ipaddr_string(TPA(ap)));
153 if (memcmp((char *)ezero, (char *)THA(ap), 6) != 0)
154 (void)printf(" (%s)", etheraddr_string(THA(ap)));
155 (void)printf(" tell %s", ipaddr_string(SPA(ap)));
156 if (memcmp((char *)ESRC(eh), (char *)SHA(ap), 6) != 0)
157 (void)printf(" (%s)", etheraddr_string(SHA(ap)));
158 break;
159
160 case ARPOP_REPLY:
161 (void)printf("arp reply %s", ipaddr_string(SPA(ap)));
162 if (memcmp((char *)ESRC(eh), (char *)SHA(ap), 6) != 0)
163 (void)printf(" (%s)", etheraddr_string(SHA(ap)));
164 (void)printf(" is-at %s", etheraddr_string(SHA(ap)));
165 if (memcmp((char *)EDST(eh), (char *)THA(ap), 6) != 0)
166 (void)printf(" (%s)", etheraddr_string(THA(ap)));
167 break;
168
169 case REVARP_REQUEST:
170 (void)printf("rarp who-is %s tell %s",
171 etheraddr_string(THA(ap)),
172 etheraddr_string(SHA(ap)));
173 break;
174
175 case REVARP_REPLY:
176 (void)printf("rarp reply %s at %s",
177 etheraddr_string(THA(ap)),
178 ipaddr_string(TPA(ap)));
179 break;
180
181 default:
182 (void)printf("arp-#%d", op);
183 default_print((u_char *)ap, caplen);
184 return;
185 }
186 if (hrd != ARPHRD_ETHER)
187 printf(" hardware #%d", hrd);
188 }