Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / tcpdump / print-arp.c
blobb8094cf4188e91b73f85b558e5d0a4c29b67c681
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 #include <sys/cdefs.h>
25 #ifndef lint
26 #if 0
27 static const char rcsid[] _U_ =
28 "@(#) Header: /tcpdump/master/tcpdump/print-arp.c,v 1.64 2004/04/30 16:42:14 mcr Exp (LBL)";
29 #else
30 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
31 #endif
32 #endif
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
38 #include <tcpdump-stdinc.h>
40 #include <stdio.h>
41 #include <string.h>
43 #include "netdissect.h"
44 #include "addrtoname.h"
45 #include "ether.h"
46 #include "ethertype.h"
47 #include "extract.h" /* must come after interface.h */
50 * Address Resolution Protocol.
52 * See RFC 826 for protocol description. ARP packets are variable
53 * in size; the arphdr structure defines the fixed-length portion.
54 * Protocol type values are the same as those for 10 Mb/s Ethernet.
55 * It is followed by the variable-sized fields ar_sha, arp_spa,
56 * arp_tha and arp_tpa in that order, according to the lengths
57 * specified. Field names used correspond to RFC 826.
59 struct arp_pkthdr {
60 u_short ar_hrd; /* format of hardware address */
61 #define ARPHRD_ETHER 1 /* ethernet hardware format */
62 #define ARPHRD_IEEE802 6 /* token-ring hardware format */
63 #define ARPHRD_ARCNET 7 /* arcnet hardware format */
64 #define ARPHRD_FRELAY 15 /* frame relay hardware format */
65 #define ARPHRD_STRIP 23 /* Ricochet Starmode Radio hardware format */
66 #define ARPHRD_IEEE1394 24 /* IEEE 1394 (FireWire) hardware format */
67 u_short ar_pro; /* format of protocol address */
68 u_char ar_hln; /* length of hardware address */
69 u_char ar_pln; /* length of protocol address */
70 u_short ar_op; /* one of: */
71 #define ARPOP_REQUEST 1 /* request to resolve address */
72 #define ARPOP_REPLY 2 /* response to previous request */
73 #define ARPOP_REVREQUEST 3 /* request protocol address given hardware */
74 #define ARPOP_REVREPLY 4 /* response giving protocol address */
75 #define ARPOP_INVREQUEST 8 /* request to identify peer */
76 #define ARPOP_INVREPLY 9 /* response identifying peer */
78 * The remaining fields are variable in size,
79 * according to the sizes above.
81 #ifdef COMMENT_ONLY
82 u_char ar_sha[]; /* sender hardware address */
83 u_char ar_spa[]; /* sender protocol address */
84 u_char ar_tha[]; /* target hardware address */
85 u_char ar_tpa[]; /* target protocol address */
86 #endif
87 #define ar_sha(ap) (((const u_char *)((ap)+1))+0)
88 #define ar_spa(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln)
89 #define ar_tha(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln+(ap)->ar_pln)
90 #define ar_tpa(ap) (((const u_char *)((ap)+1))+2*(ap)->ar_hln+(ap)->ar_pln)
93 #define ARP_HDRLEN 8
95 #define HRD(ap) EXTRACT_16BITS(&(ap)->ar_hrd)
96 #define HLN(ap) ((ap)->ar_hln)
97 #define PLN(ap) ((ap)->ar_pln)
98 #define OP(ap) EXTRACT_16BITS(&(ap)->ar_op)
99 #define PRO(ap) EXTRACT_16BITS(&(ap)->ar_pro)
100 #define SHA(ap) (ar_sha(ap))
101 #define SPA(ap) (ar_spa(ap))
102 #define THA(ap) (ar_tha(ap))
103 #define TPA(ap) (ar_tpa(ap))
106 * ATM Address Resolution Protocol.
108 * See RFC 2225 for protocol description. ATMARP packets are similar
109 * to ARP packets, except that there are no length fields for the
110 * protocol address - instead, there are type/length fields for
111 * the ATM number and subaddress - and the hardware addresses consist
112 * of an ATM number and an ATM subaddress.
114 struct atmarp_pkthdr {
115 u_short aar_hrd; /* format of hardware address */
116 #define ARPHRD_ATM2225 19 /* ATM (RFC 2225) */
117 u_short aar_pro; /* format of protocol address */
118 u_char aar_shtl; /* length of source ATM number */
119 u_char aar_sstl; /* length of source ATM subaddress */
120 #define ATMARP_IS_E164 0x40 /* bit in type/length for E.164 format */
121 #define ATMARP_LEN_MASK 0x3F /* length of {sub}address in type/length */
122 u_short aar_op; /* same as regular ARP */
123 #define ATMARPOP_NAK 10 /* NAK */
124 u_char aar_spln; /* length of source protocol address */
125 u_char aar_thtl; /* length of target ATM number */
126 u_char aar_tstl; /* length of target ATM subaddress */
127 u_char aar_tpln; /* length of target protocol address */
129 * The remaining fields are variable in size,
130 * according to the sizes above.
132 #ifdef COMMENT_ONLY
133 u_char aar_sha[]; /* source ATM number */
134 u_char aar_ssa[]; /* source ATM subaddress */
135 u_char aar_spa[]; /* sender protocol address */
136 u_char aar_tha[]; /* target ATM number */
137 u_char aar_tsa[]; /* target ATM subaddress */
138 u_char aar_tpa[]; /* target protocol address */
139 #endif
141 #define ATMHRD(ap) EXTRACT_16BITS(&(ap)->aar_hrd)
142 #define ATMSHLN(ap) ((ap)->aar_shtl & ATMARP_LEN_MASK)
143 #define ATMSSLN(ap) ((ap)->aar_sstl & ATMARP_LEN_MASK)
144 #define ATMSPLN(ap) ((ap)->aar_spln)
145 #define ATMOP(ap) EXTRACT_16BITS(&(ap)->aar_op)
146 #define ATMPRO(ap) EXTRACT_16BITS(&(ap)->aar_pro)
147 #define ATMTHLN(ap) ((ap)->aar_thtl & ATMARP_LEN_MASK)
148 #define ATMTSLN(ap) ((ap)->aar_tstl & ATMARP_LEN_MASK)
149 #define ATMTPLN(ap) ((ap)->aar_tpln)
150 #define aar_sha(ap) ((const u_char *)((ap)+1))
151 #define aar_ssa(ap) (aar_sha(ap) + ATMSHLN(ap))
152 #define aar_spa(ap) (aar_ssa(ap) + ATMSSLN(ap))
153 #define aar_tha(ap) (aar_spa(ap) + ATMSPLN(ap))
154 #define aar_tsa(ap) (aar_tha(ap) + ATMTHLN(ap))
155 #define aar_tpa(ap) (aar_tsa(ap) + ATMTSLN(ap))
158 #define ATMSHA(ap) (aar_sha(ap))
159 #define ATMSSA(ap) (aar_ssa(ap))
160 #define ATMSPA(ap) (aar_spa(ap))
161 #define ATMTHA(ap) (aar_tha(ap))
162 #define ATMTSA(ap) (aar_tsa(ap))
163 #define ATMTPA(ap) (aar_tpa(ap))
165 static u_char ezero[6];
167 static void
168 atmarp_addr_print(netdissect_options *ndo,
169 const u_char *ha, u_int ha_len, const u_char *srca,
170 u_int srca_len)
172 if (ha_len == 0)
173 ND_PRINT((ndo, "<No address>"));
174 else {
175 ND_PRINT((ndo, "%s", linkaddr_string(ha, ha_len)));
176 if (srca_len != 0)
177 ND_PRINT((ndo, ",%s",
178 linkaddr_string(srca, srca_len)));
182 static void
183 atmarp_print(netdissect_options *ndo,
184 const u_char *bp, u_int length, u_int caplen)
186 const struct atmarp_pkthdr *ap;
187 u_short pro, hrd, op;
189 ap = (const struct atmarp_pkthdr *)bp;
190 ND_TCHECK(*ap);
192 hrd = ATMHRD(ap);
193 pro = ATMPRO(ap);
194 op = ATMOP(ap);
196 if (!ND_TTEST2(*aar_tpa(ap), ATMTPLN(ap))) {
197 ND_PRINT((ndo, "truncated-atmarp"));
198 ND_DEFAULTPRINT((const u_char *)ap, length);
199 return;
202 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
203 ATMSPLN(ap) != 4 || ATMTPLN(ap) != 4) {
204 ND_PRINT((ndo, "atmarp-#%d for proto #%d (%d/%d) hardware #%d",
205 op, pro, ATMSPLN(ap), ATMTPLN(ap), hrd));
206 return;
208 if (pro == ETHERTYPE_TRAIL)
209 ND_PRINT((ndo, "trailer-"));
210 switch (op) {
212 case ARPOP_REQUEST:
213 ND_PRINT((ndo, "arp who-has %s", ipaddr_string(ATMTPA(ap))));
214 if (ATMTHLN(ap) != 0) {
215 ND_PRINT((ndo, " ("));
216 atmarp_addr_print(ndo, ATMTHA(ap), ATMTHLN(ap),
217 ATMTSA(ap), ATMTSLN(ap));
218 ND_PRINT((ndo, ")"));
220 ND_PRINT((ndo, " tell %s", ipaddr_string(ATMSPA(ap))));
221 break;
223 case ARPOP_REPLY:
224 ND_PRINT((ndo, "arp reply %s", ipaddr_string(ATMSPA(ap))));
225 ND_PRINT((ndo, " is-at "));
226 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHLN(ap), ATMSSA(ap),
227 ATMSSLN(ap));
228 break;
230 case ARPOP_INVREQUEST:
231 ND_PRINT((ndo, "invarp who-is "));
232 atmarp_addr_print(ndo, ATMTHA(ap), ATMTHLN(ap), ATMTSA(ap),
233 ATMTSLN(ap));
234 ND_PRINT((ndo, " tell "));
235 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHLN(ap), ATMSSA(ap),
236 ATMSSLN(ap));
237 break;
239 case ARPOP_INVREPLY:
240 ND_PRINT((ndo, "invarp reply "));
241 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHLN(ap), ATMSSA(ap),
242 ATMSSLN(ap));
243 ND_PRINT((ndo, " at %s", ipaddr_string(ATMSPA(ap))));
244 break;
246 case ATMARPOP_NAK:
247 ND_PRINT((ndo, "nak reply for %s",
248 ipaddr_string(ATMSPA(ap))));
249 break;
251 default:
252 ND_PRINT((ndo, "atmarp-#%d", op));
253 ND_DEFAULTPRINT((const u_char *)ap, caplen);
254 return;
256 return;
257 trunc:
258 ND_PRINT((ndo, "[|atmarp]"));
261 void
262 arp_print(netdissect_options *ndo,
263 const u_char *bp, u_int length, u_int caplen)
265 const struct arp_pkthdr *ap;
266 u_short pro, hrd, op;
268 ap = (const struct arp_pkthdr *)bp;
269 ND_TCHECK(*ap);
270 hrd = HRD(ap);
271 if (hrd == ARPHRD_ATM2225) {
272 atmarp_print(ndo, bp, length, caplen);
273 return;
275 pro = PRO(ap);
276 op = OP(ap);
278 if (!ND_TTEST2(*ar_tpa(ap), PLN(ap))) {
279 ND_PRINT((ndo, "truncated-arp"));
280 ND_DEFAULTPRINT((const u_char *)ap, length);
281 return;
284 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
285 PLN(ap) != 4 || HLN(ap) == 0) {
286 ND_PRINT((ndo, "arp-#%d for proto #%d (%d) hardware #%d (%d)",
287 op, pro, PLN(ap), hrd, HLN(ap)));
288 return;
290 if (pro == ETHERTYPE_TRAIL)
291 ND_PRINT((ndo, "trailer-"));
292 switch (op) {
294 case ARPOP_REQUEST:
295 ND_PRINT((ndo, "arp who-has %s", ipaddr_string(TPA(ap))));
296 if (memcmp((const char *)ezero, (const char *)THA(ap), HLN(ap)) != 0)
297 ND_PRINT((ndo, " (%s)",
298 linkaddr_string(THA(ap), HLN(ap))));
299 ND_PRINT((ndo, " tell %s", ipaddr_string(SPA(ap))));
300 break;
302 case ARPOP_REPLY:
303 ND_PRINT((ndo, "arp reply %s", ipaddr_string(SPA(ap))));
304 ND_PRINT((ndo, " is-at %s", linkaddr_string(SHA(ap), HLN(ap))));
305 break;
307 case ARPOP_REVREQUEST:
308 ND_PRINT((ndo, "rarp who-is %s tell %s",
309 linkaddr_string(THA(ap), HLN(ap)),
310 linkaddr_string(SHA(ap), HLN(ap))));
311 break;
313 case ARPOP_REVREPLY:
314 ND_PRINT((ndo, "rarp reply %s at %s",
315 linkaddr_string(THA(ap), HLN(ap)),
316 ipaddr_string(TPA(ap))));
317 break;
319 case ARPOP_INVREQUEST:
320 ND_PRINT((ndo, "invarp who-is %s tell %s",
321 linkaddr_string(THA(ap), HLN(ap)),
322 linkaddr_string(SHA(ap), HLN(ap))));
323 break;
325 case ARPOP_INVREPLY:
326 ND_PRINT((ndo,"invarp reply %s at %s",
327 linkaddr_string(THA(ap), HLN(ap)),
328 ipaddr_string(TPA(ap))));
329 break;
331 default:
332 ND_PRINT((ndo, "arp-#%d", op));
333 ND_DEFAULTPRINT((const u_char *)ap, caplen);
334 return;
336 if (hrd != ARPHRD_ETHER)
337 ND_PRINT((ndo, " hardware #%d", hrd));
338 return;
339 trunc:
340 ND_PRINT((ndo, "[|arp]"));
344 * Local Variables:
345 * c-style: bsd
346 * End: