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