]> The Tcpdump Group git mirrors - tcpdump/blob - print-arp.c
Add SunATM support, based on code from Yen Yen Lim at North Dakota State
[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.54 2002-07-11 09:17:23 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 arp_pkthdr {
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_ARCNET 7 /* arcnet hardware format */
60 #define ARPHRD_FRELAY 15 /* frame relay hardware format */
61 #define ARPHRD_STRIP 23 /* Ricochet Starmode Radio hardware format */
62 #define ARPHRD_IEEE1394 24 /* IEEE 1394 (FireWire) hardware format */
63 u_short ar_pro; /* format of protocol address */
64 u_char ar_hln; /* length of hardware address */
65 u_char ar_pln; /* length of protocol address */
66 u_short ar_op; /* one of: */
67 #define ARPOP_REQUEST 1 /* request to resolve address */
68 #define ARPOP_REPLY 2 /* response to previous request */
69 #define ARPOP_REVREQUEST 3 /* request protocol address given hardware */
70 #define ARPOP_REVREPLY 4 /* response giving protocol address */
71 #define ARPOP_INVREQUEST 8 /* request to identify peer */
72 #define ARPOP_INVREPLY 9 /* response identifying peer */
73 /*
74 * The remaining fields are variable in size,
75 * according to the sizes above.
76 */
77 #ifdef COMMENT_ONLY
78 u_char ar_sha[]; /* sender hardware address */
79 u_char ar_spa[]; /* sender protocol address */
80 u_char ar_tha[]; /* target hardware address */
81 u_char ar_tpa[]; /* target protocol address */
82 #endif
83 #define ar_sha(ap) (((const u_char *)((ap)+1))+0)
84 #define ar_spa(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln)
85 #define ar_tha(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln+(ap)->ar_pln)
86 #define ar_tpa(ap) (((const u_char *)((ap)+1))+2*(ap)->ar_hln+(ap)->ar_pln)
87 };
88
89 #define ARP_HDRLEN 8
90
91 #define HRD(ap) ((ap)->ar_hrd)
92 #define HLN(ap) ((ap)->ar_hln)
93 #define PLN(ap) ((ap)->ar_pln)
94 #define OP(ap) ((ap)->ar_op)
95 #define PRO(ap) ((ap)->ar_pro)
96 #define SHA(ap) (ar_sha(ap))
97 #define SPA(ap) (ar_spa(ap))
98 #define THA(ap) (ar_tha(ap))
99 #define TPA(ap) (ar_tpa(ap))
100
101 /*
102 * ATM Address Resolution Protocol.
103 *
104 * See RFC 2225 for protocol description. ARP packets are variable
105 * in size; the arphdr structure defines the fixed-length portion.
106 * Protocol type values are the same as those for 10 Mb/s Ethernet.
107 * It is followed by the variable-sized fields ar_sha, arp_spa,
108 * arp_tha and arp_tpa in that order, according to the lengths
109 * specified. Field names used correspond to RFC 826.
110 */
111 struct atmarp_pkthdr {
112 u_short ar_hrd; /* format of hardware address */
113 #define ARPHRD_ATM2225 19 /* ATM (RFC 2225) */
114 u_short ar_pro; /* format of protocol address */
115 u_char ar_shtl; /* length of hardware address */
116 u_char ar_sstl; /* length of hardware address */
117 u_short ar_op; /* same as regular ARP */
118 u_char ar_spln;
119 u_char ar_thtl;
120 u_char ar_tstl;
121 u_char ar_tpln;
122 /*
123 * The remaining fields are variable in size,
124 * according to the sizes above.
125 */
126 #ifdef COMMENT_ONLY
127 u_char ar_sha[]; /* sender hardware address */
128 u_char ar_spa[]; /* sender protocol address */
129 u_char ar_tha[]; /* target hardware address */
130 u_char ar_tpa[]; /* target protocol address */
131 #endif
132 #define ar_sha(ap) (((const u_char *)((ap)+1))+0)
133 #define ar_spa(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln)
134 #define ar_tha(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln+(ap)->ar_pln)
135 #define ar_tpa(ap) (((const u_char *)((ap)+1))+2*(ap)->ar_hln+(ap)->ar_pln)
136 };
137
138 static u_char ezero[6];
139
140 static void
141 atmarp_print(const u_char *bp, u_int length, u_int caplen)
142 {
143 const struct arp_pkthdr *ap;
144 u_short pro, hrd, op;
145
146 ap = (const struct arp_pkthdr *)bp;
147 TCHECK(*ap);
148 if ((const u_char *)(ar_tpa(ap) + PLN(ap)) > snapend) {
149 (void)printf("truncated-arp");
150 default_print((const u_char *)ap, length);
151 return;
152 }
153
154 hrd = EXTRACT_16BITS(&HRD(ap));
155 if (hrd == ARPHRD_ATM2225)
156 atmarp_print(bp, length, caplen);
157 pro = EXTRACT_16BITS(&PRO(ap));
158 op = EXTRACT_16BITS(&OP(ap));
159
160 if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) {
161 (void)printf("arp-#%d for proto #%d (%d) hardware #%d (%d)",
162 op, pro, PLN(ap), hrd, HLN(ap));
163 return;
164 }
165 if (pro == ETHERTYPE_TRAIL)
166 (void)printf("trailer-");
167 switch (op) {
168
169 case ARPOP_REQUEST:
170 (void)printf("arp who-has %s", ipaddr_string(TPA(ap)));
171 if (memcmp((const char *)ezero, (const char *)THA(ap), HLN(ap)) != 0)
172 (void)printf(" (%s)",
173 linkaddr_string(THA(ap), HLN(ap)));
174 (void)printf(" tell %s", ipaddr_string(SPA(ap)));
175 break;
176
177 case ARPOP_REPLY:
178 (void)printf("arp reply %s", ipaddr_string(SPA(ap)));
179 (void)printf(" is-at %s", linkaddr_string(SHA(ap), HLN(ap)));
180 break;
181
182 case ARPOP_REVREQUEST:
183 (void)printf("rarp who-is %s tell %s",
184 linkaddr_string(THA(ap), HLN(ap)),
185 linkaddr_string(SHA(ap), HLN(ap)));
186 break;
187
188 case ARPOP_REVREPLY:
189 (void)printf("rarp reply %s at %s",
190 linkaddr_string(THA(ap), HLN(ap)),
191 ipaddr_string(TPA(ap)));
192 break;
193
194 case ARPOP_INVREQUEST:
195 (void)printf("invarp who-is %s tell %s",
196 linkaddr_string(THA(ap), HLN(ap)),
197 linkaddr_string(SHA(ap), HLN(ap)));
198 break;
199
200 case ARPOP_INVREPLY:
201 (void)printf("invarp reply %s at %s",
202 linkaddr_string(THA(ap), HLN(ap)),
203 ipaddr_string(TPA(ap)));
204 break;
205
206 default:
207 (void)printf("arp-#%d", op);
208 default_print((const u_char *)ap, caplen);
209 return;
210 }
211 if (hrd != ARPHRD_ETHER)
212 printf(" hardware #%d", hrd);
213 return;
214 trunc:
215 (void)printf("[|arp]");
216 }
217
218 void
219 arp_print(const u_char *bp, u_int length, u_int caplen)
220 {
221 const struct arp_pkthdr *ap;
222 u_short pro, hrd, op;
223
224 ap = (const struct arp_pkthdr *)bp;
225 TCHECK(*ap);
226 if ((const u_char *)(ar_tpa(ap) + PLN(ap)) > snapend) {
227 (void)printf("truncated-arp");
228 default_print((const u_char *)ap, length);
229 return;
230 }
231
232 hrd = EXTRACT_16BITS(&HRD(ap));
233 if (hrd == ARPHRD_ATM2225)
234 atmarp_print(bp, length, caplen);
235 pro = EXTRACT_16BITS(&PRO(ap));
236 op = EXTRACT_16BITS(&OP(ap));
237
238 if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) {
239 (void)printf("arp-#%d for proto #%d (%d) hardware #%d (%d)",
240 op, pro, PLN(ap), hrd, HLN(ap));
241 return;
242 }
243 if (pro == ETHERTYPE_TRAIL)
244 (void)printf("trailer-");
245 switch (op) {
246
247 case ARPOP_REQUEST:
248 (void)printf("arp who-has %s", ipaddr_string(TPA(ap)));
249 if (memcmp((const char *)ezero, (const char *)THA(ap), HLN(ap)) != 0)
250 (void)printf(" (%s)",
251 linkaddr_string(THA(ap), HLN(ap)));
252 (void)printf(" tell %s", ipaddr_string(SPA(ap)));
253 break;
254
255 case ARPOP_REPLY:
256 (void)printf("arp reply %s", ipaddr_string(SPA(ap)));
257 (void)printf(" is-at %s", linkaddr_string(SHA(ap), HLN(ap)));
258 break;
259
260 case ARPOP_REVREQUEST:
261 (void)printf("rarp who-is %s tell %s",
262 linkaddr_string(THA(ap), HLN(ap)),
263 linkaddr_string(SHA(ap), HLN(ap)));
264 break;
265
266 case ARPOP_REVREPLY:
267 (void)printf("rarp reply %s at %s",
268 linkaddr_string(THA(ap), HLN(ap)),
269 ipaddr_string(TPA(ap)));
270 break;
271
272 case ARPOP_INVREQUEST:
273 (void)printf("invarp who-is %s tell %s",
274 linkaddr_string(THA(ap), HLN(ap)),
275 linkaddr_string(SHA(ap), HLN(ap)));
276 break;
277
278 case ARPOP_INVREPLY:
279 (void)printf("invarp reply %s at %s",
280 linkaddr_string(THA(ap), HLN(ap)),
281 ipaddr_string(TPA(ap)));
282 break;
283
284 default:
285 (void)printf("arp-#%d", op);
286 default_print((const u_char *)ap, caplen);
287 return;
288 }
289 if (hrd != ARPHRD_ETHER)
290 printf(" hardware #%d", hrd);
291 return;
292 trunc:
293 (void)printf("[|arp]");
294 }