]> The Tcpdump Group git mirrors - tcpdump/blob - print-arp.c
Some compilers may pad structures to a length that's a multiple of 2 or
[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.48 2000-10-09 03:24:25 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 #define ARP_HDRLEN 8
82
83 /*
84 * Ethernet Address Resolution Protocol.
85 *
86 * See RFC 826 for protocol description. Structure below is adapted
87 * to resolving internet addresses. Field names used correspond to
88 * RFC 826.
89 */
90 struct ether_arp {
91 struct arphdr ea_hdr; /* fixed-size header */
92 u_char arp_sha[6]; /* sender hardware address */
93 u_char arp_spa[4]; /* sender protocol address */
94 u_char arp_tha[6]; /* target hardware address */
95 u_char arp_tpa[4]; /* target protocol address */
96 };
97 #define arp_hrd ea_hdr.ar_hrd
98 #define arp_pro ea_hdr.ar_pro
99 #define arp_hln ea_hdr.ar_hln
100 #define arp_pln ea_hdr.ar_pln
101 #define arp_op ea_hdr.ar_op
102
103 #define ETHER_ARP_HDRLEN (ARP_HDRLEN + 6 + 4 + 6 + 4)
104
105 #define SHA(ap) ((ap)->arp_sha)
106 #define THA(ap) ((ap)->arp_tha)
107 #define SPA(ap) ((ap)->arp_spa)
108 #define TPA(ap) ((ap)->arp_tpa)
109
110 /* Compatibility */
111 #ifndef REVARP_REQUEST
112 #define REVARP_REQUEST 3
113 #endif
114 #ifndef REVARP_REPLY
115 #define REVARP_REPLY 4
116 #endif
117
118 static u_char ezero[6];
119
120 void
121 arp_print(register const u_char *bp, u_int length, u_int caplen)
122 {
123 register const struct ether_arp *ap;
124 register const struct ether_header *eh;
125 register u_short pro, hrd, op;
126
127 ap = (struct ether_arp *)bp;
128 if ((u_char *)(ap + 1) > snapend) {
129 printf("[|arp]");
130 return;
131 }
132 if (length < ETHER_ARP_HDRLEN) {
133 (void)printf("truncated-arp");
134 default_print((u_char *)ap, length);
135 return;
136 }
137
138 pro = EXTRACT_16BITS(&ap->arp_pro);
139 hrd = EXTRACT_16BITS(&ap->arp_hrd);
140 op = EXTRACT_16BITS(&ap->arp_op);
141
142 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
143 || ap->arp_hln != sizeof(SHA(ap))
144 || ap->arp_pln != sizeof(SPA(ap))) {
145 (void)printf("arp-#%d for proto #%d (%d) hardware #%d (%d)",
146 op, pro, ap->arp_pln,
147 hrd, ap->arp_hln);
148 return;
149 }
150 if (pro == ETHERTYPE_TRAIL)
151 (void)printf("trailer-");
152 eh = (struct ether_header *)packetp;
153 switch (op) {
154
155 case ARPOP_REQUEST:
156 (void)printf("arp who-has %s", ipaddr_string(TPA(ap)));
157 if (memcmp((char *)ezero, (char *)THA(ap), 6) != 0)
158 (void)printf(" (%s)", etheraddr_string(THA(ap)));
159 (void)printf(" tell %s", ipaddr_string(SPA(ap)));
160 if (memcmp((char *)ESRC(eh), (char *)SHA(ap), 6) != 0)
161 (void)printf(" (%s)", etheraddr_string(SHA(ap)));
162 break;
163
164 case ARPOP_REPLY:
165 (void)printf("arp reply %s", ipaddr_string(SPA(ap)));
166 if (memcmp((char *)ESRC(eh), (char *)SHA(ap), 6) != 0)
167 (void)printf(" (%s)", etheraddr_string(SHA(ap)));
168 (void)printf(" is-at %s", etheraddr_string(SHA(ap)));
169 if (memcmp((char *)EDST(eh), (char *)THA(ap), 6) != 0)
170 (void)printf(" (%s)", etheraddr_string(THA(ap)));
171 break;
172
173 case REVARP_REQUEST:
174 (void)printf("rarp who-is %s tell %s",
175 etheraddr_string(THA(ap)),
176 etheraddr_string(SHA(ap)));
177 break;
178
179 case REVARP_REPLY:
180 (void)printf("rarp reply %s at %s",
181 etheraddr_string(THA(ap)),
182 ipaddr_string(TPA(ap)));
183 break;
184
185 default:
186 (void)printf("arp-#%d", op);
187 default_print((u_char *)ap, caplen);
188 return;
189 }
190 if (hrd != ARPHRD_ETHER)
191 printf(" hardware #%d", hrd);
192 }