]> The Tcpdump Group git mirrors - tcpdump/blob - print-arp.c
Remove obsolete comments
[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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <tcpdump-stdinc.h>
27
28 #include <string.h>
29
30 #include "netdissect.h"
31 #include "addrtoname.h"
32 #include "ether.h"
33 #include "ethertype.h"
34 #include "extract.h"
35
36 static const char tstr[] = "[|ARP]";
37
38 /*
39 * Address Resolution Protocol.
40 *
41 * See RFC 826 for protocol description. ARP packets are variable
42 * in size; the arphdr structure defines the fixed-length portion.
43 * Protocol type values are the same as those for 10 Mb/s Ethernet.
44 * It is followed by the variable-sized fields ar_sha, arp_spa,
45 * arp_tha and arp_tpa in that order, according to the lengths
46 * specified. Field names used correspond to RFC 826.
47 */
48 struct arp_pkthdr {
49 u_short ar_hrd; /* format of hardware address */
50 #define ARPHRD_ETHER 1 /* ethernet hardware format */
51 #define ARPHRD_IEEE802 6 /* token-ring hardware format */
52 #define ARPHRD_ARCNET 7 /* arcnet hardware format */
53 #define ARPHRD_FRELAY 15 /* frame relay hardware format */
54 #define ARPHRD_ATM2225 19 /* ATM (RFC 2225) */
55 #define ARPHRD_STRIP 23 /* Ricochet Starmode Radio hardware format */
56 #define ARPHRD_IEEE1394 24 /* IEEE 1394 (FireWire) hardware format */
57 u_short ar_pro; /* format of protocol address */
58 u_char ar_hln; /* length of hardware address */
59 u_char ar_pln; /* length of protocol address */
60 u_short ar_op; /* one of: */
61 #define ARPOP_REQUEST 1 /* request to resolve address */
62 #define ARPOP_REPLY 2 /* response to previous request */
63 #define ARPOP_REVREQUEST 3 /* request protocol address given hardware */
64 #define ARPOP_REVREPLY 4 /* response giving protocol address */
65 #define ARPOP_INVREQUEST 8 /* request to identify peer */
66 #define ARPOP_INVREPLY 9 /* response identifying peer */
67 #define ARPOP_NAK 10 /* NAK - only valif for ATM ARP */
68
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 #define ar_sha(ap) (((const u_char *)((ap)+1))+0)
80 #define ar_spa(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln)
81 #define ar_tha(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln+(ap)->ar_pln)
82 #define ar_tpa(ap) (((const u_char *)((ap)+1))+2*(ap)->ar_hln+(ap)->ar_pln)
83 };
84
85 #define ARP_HDRLEN 8
86
87 #define HRD(ap) EXTRACT_16BITS(&(ap)->ar_hrd)
88 #define HRD_LEN(ap) ((ap)->ar_hln)
89 #define PROTO_LEN(ap) ((ap)->ar_pln)
90 #define OP(ap) EXTRACT_16BITS(&(ap)->ar_op)
91 #define PRO(ap) EXTRACT_16BITS(&(ap)->ar_pro)
92 #define SHA(ap) (ar_sha(ap))
93 #define SPA(ap) (ar_spa(ap))
94 #define THA(ap) (ar_tha(ap))
95 #define TPA(ap) (ar_tpa(ap))
96
97
98 static const struct tok arpop_values[] = {
99 { ARPOP_REQUEST, "Request" },
100 { ARPOP_REPLY, "Reply" },
101 { ARPOP_REVREQUEST, "Reverse Request" },
102 { ARPOP_REVREPLY, "Reverse Reply" },
103 { ARPOP_INVREQUEST, "Inverse Request" },
104 { ARPOP_INVREPLY, "Inverse Reply" },
105 { ARPOP_NAK, "NACK Reply" },
106 { 0, NULL }
107 };
108
109 static const struct tok arphrd_values[] = {
110 { ARPHRD_ETHER, "Ethernet" },
111 { ARPHRD_IEEE802, "TokenRing" },
112 { ARPHRD_ARCNET, "ArcNet" },
113 { ARPHRD_FRELAY, "FrameRelay" },
114 { ARPHRD_STRIP, "Strip" },
115 { ARPHRD_IEEE1394, "IEEE 1394" },
116 { ARPHRD_ATM2225, "ATM" },
117 { 0, NULL }
118 };
119
120 /*
121 * ATM Address Resolution Protocol.
122 *
123 * See RFC 2225 for protocol description. ATMARP packets are similar
124 * to ARP packets, except that there are no length fields for the
125 * protocol address - instead, there are type/length fields for
126 * the ATM number and subaddress - and the hardware addresses consist
127 * of an ATM number and an ATM subaddress.
128 */
129 struct atmarp_pkthdr {
130 u_short aar_hrd; /* format of hardware address */
131 u_short aar_pro; /* format of protocol address */
132 u_char aar_shtl; /* length of source ATM number */
133 u_char aar_sstl; /* length of source ATM subaddress */
134 #define ATMARP_IS_E164 0x40 /* bit in type/length for E.164 format */
135 #define ATMARP_LEN_MASK 0x3F /* length of {sub}address in type/length */
136 u_short aar_op; /* same as regular ARP */
137 u_char aar_spln; /* length of source protocol address */
138 u_char aar_thtl; /* length of target ATM number */
139 u_char aar_tstl; /* length of target ATM subaddress */
140 u_char aar_tpln; /* length of target protocol address */
141 /*
142 * The remaining fields are variable in size,
143 * according to the sizes above.
144 */
145 #ifdef COMMENT_ONLY
146 u_char aar_sha[]; /* source ATM number */
147 u_char aar_ssa[]; /* source ATM subaddress */
148 u_char aar_spa[]; /* sender protocol address */
149 u_char aar_tha[]; /* target ATM number */
150 u_char aar_tsa[]; /* target ATM subaddress */
151 u_char aar_tpa[]; /* target protocol address */
152 #endif
153
154 #define ATMHRD(ap) EXTRACT_16BITS(&(ap)->aar_hrd)
155 #define ATMSHRD_LEN(ap) ((ap)->aar_shtl & ATMARP_LEN_MASK)
156 #define ATMSSLN(ap) ((ap)->aar_sstl & ATMARP_LEN_MASK)
157 #define ATMSPROTO_LEN(ap) ((ap)->aar_spln)
158 #define ATMOP(ap) EXTRACT_16BITS(&(ap)->aar_op)
159 #define ATMPRO(ap) EXTRACT_16BITS(&(ap)->aar_pro)
160 #define ATMTHRD_LEN(ap) ((ap)->aar_thtl & ATMARP_LEN_MASK)
161 #define ATMTSLN(ap) ((ap)->aar_tstl & ATMARP_LEN_MASK)
162 #define ATMTPROTO_LEN(ap) ((ap)->aar_tpln)
163 #define aar_sha(ap) ((const u_char *)((ap)+1))
164 #define aar_ssa(ap) (aar_sha(ap) + ATMSHRD_LEN(ap))
165 #define aar_spa(ap) (aar_ssa(ap) + ATMSSLN(ap))
166 #define aar_tha(ap) (aar_spa(ap) + ATMSPROTO_LEN(ap))
167 #define aar_tsa(ap) (aar_tha(ap) + ATMTHRD_LEN(ap))
168 #define aar_tpa(ap) (aar_tsa(ap) + ATMTSLN(ap))
169 };
170
171 #define ATMSHA(ap) (aar_sha(ap))
172 #define ATMSSA(ap) (aar_ssa(ap))
173 #define ATMSPA(ap) (aar_spa(ap))
174 #define ATMTHA(ap) (aar_tha(ap))
175 #define ATMTSA(ap) (aar_tsa(ap))
176 #define ATMTPA(ap) (aar_tpa(ap))
177
178 static u_char ezero[6];
179
180 static void
181 atmarp_addr_print(netdissect_options *ndo,
182 const u_char *ha, u_int ha_len, const u_char *srca,
183 u_int srca_len)
184 {
185 if (ha_len == 0)
186 ND_PRINT((ndo, "<No address>"));
187 else {
188 ND_PRINT((ndo, "%s", linkaddr_string(ndo, ha, LINKADDR_ATM, ha_len)));
189 if (srca_len != 0)
190 ND_PRINT((ndo, ",%s",
191 linkaddr_string(ndo, srca, LINKADDR_ATM, srca_len)));
192 }
193 }
194
195 static void
196 atmarp_print(netdissect_options *ndo,
197 const u_char *bp, u_int length, u_int caplen)
198 {
199 const struct atmarp_pkthdr *ap;
200 u_short pro, hrd, op;
201
202 ap = (const struct atmarp_pkthdr *)bp;
203 ND_TCHECK(*ap);
204
205 hrd = ATMHRD(ap);
206 pro = ATMPRO(ap);
207 op = ATMOP(ap);
208
209 if (!ND_TTEST2(*aar_tpa(ap), ATMTPROTO_LEN(ap))) {
210 ND_PRINT((ndo, "%s", tstr));
211 ND_DEFAULTPRINT((const u_char *)ap, length);
212 return;
213 }
214
215 if (!ndo->ndo_eflag) {
216 ND_PRINT((ndo, "ARP, "));
217 }
218
219 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
220 ATMSPROTO_LEN(ap) != 4 ||
221 ATMTPROTO_LEN(ap) != 4 ||
222 ndo->ndo_vflag) {
223 ND_PRINT((ndo, "%s, %s (len %u/%u)",
224 tok2str(arphrd_values, "Unknown Hardware (%u)", hrd),
225 tok2str(ethertype_values, "Unknown Protocol (0x%04x)", pro),
226 ATMSPROTO_LEN(ap),
227 ATMTPROTO_LEN(ap)));
228
229 /* don't know know about the address formats */
230 if (!ndo->ndo_vflag) {
231 goto out;
232 }
233 }
234
235 /* print operation */
236 ND_PRINT((ndo, "%s%s ",
237 ndo->ndo_vflag ? ", " : "",
238 tok2str(arpop_values, "Unknown (%u)", op)));
239
240 switch (op) {
241
242 case ARPOP_REQUEST:
243 ND_PRINT((ndo, "who-has %s", ipaddr_string(ndo, ATMTPA(ap))));
244 if (ATMTHRD_LEN(ap) != 0) {
245 ND_PRINT((ndo, " ("));
246 atmarp_addr_print(ndo, ATMTHA(ap), ATMTHRD_LEN(ap),
247 ATMTSA(ap), ATMTSLN(ap));
248 ND_PRINT((ndo, ")"));
249 }
250 ND_PRINT((ndo, "tell %s", ipaddr_string(ndo, ATMSPA(ap))));
251 break;
252
253 case ARPOP_REPLY:
254 ND_PRINT((ndo, "%s is-at ", ipaddr_string(ndo, ATMSPA(ap))));
255 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
256 ATMSSLN(ap));
257 break;
258
259 case ARPOP_INVREQUEST:
260 ND_PRINT((ndo, "who-is "));
261 atmarp_addr_print(ndo, ATMTHA(ap), ATMTHRD_LEN(ap), ATMTSA(ap),
262 ATMTSLN(ap));
263 ND_PRINT((ndo, " tell "));
264 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
265 ATMSSLN(ap));
266 break;
267
268 case ARPOP_INVREPLY:
269 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
270 ATMSSLN(ap));
271 ND_PRINT((ndo, "at %s", ipaddr_string(ndo, ATMSPA(ap))));
272 break;
273
274 case ARPOP_NAK:
275 ND_PRINT((ndo, "for %s", ipaddr_string(ndo, ATMSPA(ap))));
276 break;
277
278 default:
279 ND_DEFAULTPRINT((const u_char *)ap, caplen);
280 return;
281 }
282
283 out:
284 ND_PRINT((ndo, ", length %u", length));
285 return;
286
287 trunc:
288 ND_PRINT((ndo, "%s", tstr));
289 }
290
291 void
292 arp_print(netdissect_options *ndo,
293 const u_char *bp, u_int length, u_int caplen)
294 {
295 const struct arp_pkthdr *ap;
296 u_short pro, hrd, op, linkaddr;
297
298 ap = (const struct arp_pkthdr *)bp;
299 ND_TCHECK(*ap);
300
301 hrd = HRD(ap);
302 pro = PRO(ap);
303 op = OP(ap);
304
305
306 /* if its ATM then call the ATM ARP printer
307 for Frame-relay ARP most of the fields
308 are similar to Ethernet so overload the Ethernet Printer
309 and set the linkaddr type for linkaddr_string(ndo, ) accordingly */
310
311 switch(hrd) {
312 case ARPHRD_ATM2225:
313 atmarp_print(ndo, bp, length, caplen);
314 return;
315 case ARPHRD_FRELAY:
316 linkaddr = LINKADDR_FRELAY;
317 break;
318 default:
319 linkaddr = LINKADDR_ETHER;
320 break;
321 }
322
323 if (!ND_TTEST2(*ar_tpa(ap), PROTO_LEN(ap))) {
324 ND_PRINT((ndo, "%s", tstr));
325 ND_DEFAULTPRINT((const u_char *)ap, length);
326 return;
327 }
328
329 if (!ndo->ndo_eflag) {
330 ND_PRINT((ndo, "ARP, "));
331 }
332
333 /* print hardware type/len and proto type/len */
334 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
335 PROTO_LEN(ap) != 4 ||
336 HRD_LEN(ap) == 0 ||
337 ndo->ndo_vflag) {
338 ND_PRINT((ndo, "%s (len %u), %s (len %u)",
339 tok2str(arphrd_values, "Unknown Hardware (%u)", hrd),
340 HRD_LEN(ap),
341 tok2str(ethertype_values, "Unknown Protocol (0x%04x)", pro),
342 PROTO_LEN(ap)));
343
344 /* don't know know about the address formats */
345 if (!ndo->ndo_vflag) {
346 goto out;
347 }
348 }
349
350 /* print operation */
351 ND_PRINT((ndo, "%s%s ",
352 ndo->ndo_vflag ? ", " : "",
353 tok2str(arpop_values, "Unknown (%u)", op)));
354
355 switch (op) {
356
357 case ARPOP_REQUEST:
358 ND_PRINT((ndo, "who-has %s", ipaddr_string(ndo, TPA(ap))));
359 if (memcmp((const char *)ezero, (const char *)THA(ap), HRD_LEN(ap)) != 0)
360 ND_PRINT((ndo, " (%s)",
361 linkaddr_string(ndo, THA(ap), linkaddr, HRD_LEN(ap))));
362 ND_PRINT((ndo, " tell %s", ipaddr_string(ndo, SPA(ap))));
363 break;
364
365 case ARPOP_REPLY:
366 ND_PRINT((ndo, "%s is-at %s",
367 ipaddr_string(ndo, SPA(ap)),
368 linkaddr_string(ndo, SHA(ap), linkaddr, HRD_LEN(ap))));
369 break;
370
371 case ARPOP_REVREQUEST:
372 ND_PRINT((ndo, "who-is %s tell %s",
373 linkaddr_string(ndo, THA(ap), linkaddr, HRD_LEN(ap)),
374 linkaddr_string(ndo, SHA(ap), linkaddr, HRD_LEN(ap))));
375 break;
376
377 case ARPOP_REVREPLY:
378 ND_PRINT((ndo, "%s at %s",
379 linkaddr_string(ndo, THA(ap), linkaddr, HRD_LEN(ap)),
380 ipaddr_string(ndo, TPA(ap))));
381 break;
382
383 case ARPOP_INVREQUEST:
384 ND_PRINT((ndo, "who-is %s tell %s",
385 linkaddr_string(ndo, THA(ap), linkaddr, HRD_LEN(ap)),
386 linkaddr_string(ndo, SHA(ap), linkaddr, HRD_LEN(ap))));
387 break;
388
389 case ARPOP_INVREPLY:
390 ND_PRINT((ndo,"%s at %s",
391 linkaddr_string(ndo, SHA(ap), linkaddr, HRD_LEN(ap)),
392 ipaddr_string(ndo, SPA(ap))));
393 break;
394
395 default:
396 ND_DEFAULTPRINT((const u_char *)ap, caplen);
397 return;
398 }
399
400 out:
401 ND_PRINT((ndo, ", length %u", length));
402
403 return;
404 trunc:
405 ND_PRINT((ndo, "%s", tstr));
406 }
407
408 /*
409 * Local Variables:
410 * c-style: bsd
411 * End:
412 */
413