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