etc/services - sync with NetBSD-8
[minix.git] / external / bsd / tcpdump / dist / print-arp.c
blobc0e3e550b79b769a99a92d11867b5c1b8b3119ae
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.
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.
22 #include <sys/cdefs.h>
23 #ifndef lint
24 __RCSID("$NetBSD: print-arp.c,v 1.6 2015/03/31 21:59:35 christos Exp $");
25 #endif
27 #define NETDISSECT_REWORKED
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
32 #include <tcpdump-stdinc.h>
34 #include <string.h>
36 #include "interface.h"
37 #include "addrtoname.h"
38 #include "ether.h"
39 #include "ethertype.h"
40 #include "extract.h" /* must come after interface.h */
42 static const char tstr[] = "[|ARP]";
45 * Address Resolution Protocol.
47 * See RFC 826 for protocol description. ARP packets are variable
48 * in size; the arphdr structure defines the fixed-length portion.
49 * Protocol type values are the same as those for 10 Mb/s Ethernet.
50 * It is followed by the variable-sized fields ar_sha, arp_spa,
51 * arp_tha and arp_tpa in that order, according to the lengths
52 * specified. Field names used correspond to RFC 826.
54 struct arp_pkthdr {
55 u_short ar_hrd; /* format of hardware address */
56 #define ARPHRD_ETHER 1 /* ethernet hardware format */
57 #define ARPHRD_IEEE802 6 /* token-ring hardware format */
58 #define ARPHRD_ARCNET 7 /* arcnet hardware format */
59 #define ARPHRD_FRELAY 15 /* frame relay hardware format */
60 #define ARPHRD_ATM2225 19 /* ATM (RFC 2225) */
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 #define ARPOP_NAK 10 /* NAK - only valif for ATM ARP */
76 * The remaining fields are variable in size,
77 * according to the sizes above.
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 #define ar_sha(ap) (((const u_char *)((ap)+1))+0)
86 #define ar_spa(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln)
87 #define ar_tha(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln+(ap)->ar_pln)
88 #define ar_tpa(ap) (((const u_char *)((ap)+1))+2*(ap)->ar_hln+(ap)->ar_pln)
91 #define ARP_HDRLEN 8
93 #define HRD(ap) EXTRACT_16BITS(&(ap)->ar_hrd)
94 #define HRD_LEN(ap) ((ap)->ar_hln)
95 #define PROTO_LEN(ap) ((ap)->ar_pln)
96 #define OP(ap) EXTRACT_16BITS(&(ap)->ar_op)
97 #define PRO(ap) EXTRACT_16BITS(&(ap)->ar_pro)
98 #define SHA(ap) (ar_sha(ap))
99 #define SPA(ap) (ar_spa(ap))
100 #define THA(ap) (ar_tha(ap))
101 #define TPA(ap) (ar_tpa(ap))
104 static const struct tok arpop_values[] = {
105 { ARPOP_REQUEST, "Request" },
106 { ARPOP_REPLY, "Reply" },
107 { ARPOP_REVREQUEST, "Reverse Request" },
108 { ARPOP_REVREPLY, "Reverse Reply" },
109 { ARPOP_INVREQUEST, "Inverse Request" },
110 { ARPOP_INVREPLY, "Inverse Reply" },
111 { ARPOP_NAK, "NACK Reply" },
112 { 0, NULL }
115 static const struct tok arphrd_values[] = {
116 { ARPHRD_ETHER, "Ethernet" },
117 { ARPHRD_IEEE802, "TokenRing" },
118 { ARPHRD_ARCNET, "ArcNet" },
119 { ARPHRD_FRELAY, "FrameRelay" },
120 { ARPHRD_STRIP, "Strip" },
121 { ARPHRD_IEEE1394, "IEEE 1394" },
122 { ARPHRD_ATM2225, "ATM" },
123 { 0, NULL }
127 * ATM Address Resolution Protocol.
129 * See RFC 2225 for protocol description. ATMARP packets are similar
130 * to ARP packets, except that there are no length fields for the
131 * protocol address - instead, there are type/length fields for
132 * the ATM number and subaddress - and the hardware addresses consist
133 * of an ATM number and an ATM subaddress.
135 struct atmarp_pkthdr {
136 u_short aar_hrd; /* format of hardware address */
137 u_short aar_pro; /* format of protocol address */
138 u_char aar_shtl; /* length of source ATM number */
139 u_char aar_sstl; /* length of source ATM subaddress */
140 #define ATMARP_IS_E164 0x40 /* bit in type/length for E.164 format */
141 #define ATMARP_LEN_MASK 0x3F /* length of {sub}address in type/length */
142 u_short aar_op; /* same as regular ARP */
143 u_char aar_spln; /* length of source protocol address */
144 u_char aar_thtl; /* length of target ATM number */
145 u_char aar_tstl; /* length of target ATM subaddress */
146 u_char aar_tpln; /* length of target protocol address */
148 * The remaining fields are variable in size,
149 * according to the sizes above.
151 #ifdef COMMENT_ONLY
152 u_char aar_sha[]; /* source ATM number */
153 u_char aar_ssa[]; /* source ATM subaddress */
154 u_char aar_spa[]; /* sender protocol address */
155 u_char aar_tha[]; /* target ATM number */
156 u_char aar_tsa[]; /* target ATM subaddress */
157 u_char aar_tpa[]; /* target protocol address */
158 #endif
160 #define ATMHRD(ap) EXTRACT_16BITS(&(ap)->aar_hrd)
161 #define ATMSHRD_LEN(ap) ((ap)->aar_shtl & ATMARP_LEN_MASK)
162 #define ATMSSLN(ap) ((ap)->aar_sstl & ATMARP_LEN_MASK)
163 #define ATMSPROTO_LEN(ap) ((ap)->aar_spln)
164 #define ATMOP(ap) EXTRACT_16BITS(&(ap)->aar_op)
165 #define ATMPRO(ap) EXTRACT_16BITS(&(ap)->aar_pro)
166 #define ATMTHRD_LEN(ap) ((ap)->aar_thtl & ATMARP_LEN_MASK)
167 #define ATMTSLN(ap) ((ap)->aar_tstl & ATMARP_LEN_MASK)
168 #define ATMTPROTO_LEN(ap) ((ap)->aar_tpln)
169 #define aar_sha(ap) ((const u_char *)((ap)+1))
170 #define aar_ssa(ap) (aar_sha(ap) + ATMSHRD_LEN(ap))
171 #define aar_spa(ap) (aar_ssa(ap) + ATMSSLN(ap))
172 #define aar_tha(ap) (aar_spa(ap) + ATMSPROTO_LEN(ap))
173 #define aar_tsa(ap) (aar_tha(ap) + ATMTHRD_LEN(ap))
174 #define aar_tpa(ap) (aar_tsa(ap) + ATMTSLN(ap))
177 #define ATMSHA(ap) (aar_sha(ap))
178 #define ATMSSA(ap) (aar_ssa(ap))
179 #define ATMSPA(ap) (aar_spa(ap))
180 #define ATMTHA(ap) (aar_tha(ap))
181 #define ATMTSA(ap) (aar_tsa(ap))
182 #define ATMTPA(ap) (aar_tpa(ap))
184 static u_char ezero[6];
186 static void
187 atmarp_addr_print(netdissect_options *ndo,
188 const u_char *ha, u_int ha_len, const u_char *srca,
189 u_int srca_len)
191 if (ha_len == 0)
192 ND_PRINT((ndo, "<No address>"));
193 else {
194 ND_PRINT((ndo, "%s", linkaddr_string(ndo, ha, LINKADDR_ATM, ha_len)));
195 if (srca_len != 0)
196 ND_PRINT((ndo, ",%s",
197 linkaddr_string(ndo, srca, LINKADDR_ATM, srca_len)));
201 static void
202 atmarp_print(netdissect_options *ndo,
203 const u_char *bp, u_int length, u_int caplen)
205 const struct atmarp_pkthdr *ap;
206 u_short pro, hrd, op;
208 ap = (const struct atmarp_pkthdr *)bp;
209 ND_TCHECK(*ap);
211 hrd = ATMHRD(ap);
212 pro = ATMPRO(ap);
213 op = ATMOP(ap);
215 if (!ND_TTEST2(*aar_tpa(ap), ATMTPROTO_LEN(ap))) {
216 ND_PRINT((ndo, "%s", tstr));
217 ND_DEFAULTPRINT((const u_char *)ap, length);
218 return;
221 if (!ndo->ndo_eflag) {
222 ND_PRINT((ndo, "ARP, "));
225 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
226 ATMSPROTO_LEN(ap) != 4 ||
227 ATMTPROTO_LEN(ap) != 4 ||
228 ndo->ndo_vflag) {
229 ND_PRINT((ndo, "%s, %s (len %u/%u)",
230 tok2str(arphrd_values, "Unknown Hardware (%u)", hrd),
231 tok2str(ethertype_values, "Unknown Protocol (0x%04x)", pro),
232 ATMSPROTO_LEN(ap),
233 ATMTPROTO_LEN(ap)));
235 /* don't know know about the address formats */
236 if (!ndo->ndo_vflag) {
237 goto out;
241 /* print operation */
242 ND_PRINT((ndo, "%s%s ",
243 ndo->ndo_vflag ? ", " : "",
244 tok2str(arpop_values, "Unknown (%u)", op)));
246 switch (op) {
248 case ARPOP_REQUEST:
249 ND_PRINT((ndo, "who-has %s", ipaddr_string(ndo, ATMTPA(ap))));
250 if (ATMTHRD_LEN(ap) != 0) {
251 ND_PRINT((ndo, " ("));
252 atmarp_addr_print(ndo, ATMTHA(ap), ATMTHRD_LEN(ap),
253 ATMTSA(ap), ATMTSLN(ap));
254 ND_PRINT((ndo, ")"));
256 ND_PRINT((ndo, "tell %s", ipaddr_string(ndo, ATMSPA(ap))));
257 break;
259 case ARPOP_REPLY:
260 ND_PRINT((ndo, "%s is-at ", ipaddr_string(ndo, ATMSPA(ap))));
261 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
262 ATMSSLN(ap));
263 break;
265 case ARPOP_INVREQUEST:
266 ND_PRINT((ndo, "who-is "));
267 atmarp_addr_print(ndo, ATMTHA(ap), ATMTHRD_LEN(ap), ATMTSA(ap),
268 ATMTSLN(ap));
269 ND_PRINT((ndo, " tell "));
270 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
271 ATMSSLN(ap));
272 break;
274 case ARPOP_INVREPLY:
275 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap),
276 ATMSSLN(ap));
277 ND_PRINT((ndo, "at %s", ipaddr_string(ndo, ATMSPA(ap))));
278 break;
280 case ARPOP_NAK:
281 ND_PRINT((ndo, "for %s", ipaddr_string(ndo, ATMSPA(ap))));
282 break;
284 default:
285 ND_DEFAULTPRINT((const u_char *)ap, caplen);
286 return;
289 out:
290 ND_PRINT((ndo, ", length %u", length));
291 return;
293 trunc:
294 ND_PRINT((ndo, "%s", tstr));
297 void
298 arp_print(netdissect_options *ndo,
299 const u_char *bp, u_int length, u_int caplen)
301 const struct arp_pkthdr *ap;
302 u_short pro, hrd, op, linkaddr;
304 ap = (const struct arp_pkthdr *)bp;
305 ND_TCHECK(*ap);
307 hrd = HRD(ap);
308 pro = PRO(ap);
309 op = OP(ap);
312 /* if its ATM then call the ATM ARP printer
313 for Frame-relay ARP most of the fields
314 are similar to Ethernet so overload the Ethernet Printer
315 and set the linkaddr type for linkaddr_string(ndo, ) accordingly */
317 switch(hrd) {
318 case ARPHRD_ATM2225:
319 atmarp_print(ndo, bp, length, caplen);
320 return;
321 case ARPHRD_FRELAY:
322 linkaddr = LINKADDR_FRELAY;
323 break;
324 default:
325 linkaddr = LINKADDR_ETHER;
326 break;
329 if (!ND_TTEST2(*ar_tpa(ap), PROTO_LEN(ap))) {
330 ND_PRINT((ndo, "%s", tstr));
331 ND_DEFAULTPRINT((const u_char *)ap, length);
332 return;
335 if (!ndo->ndo_eflag) {
336 ND_PRINT((ndo, "ARP, "));
339 /* print hardware type/len and proto type/len */
340 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
341 PROTO_LEN(ap) != 4 ||
342 HRD_LEN(ap) == 0 ||
343 ndo->ndo_vflag) {
344 ND_PRINT((ndo, "%s (len %u), %s (len %u)",
345 tok2str(arphrd_values, "Unknown Hardware (%u)", hrd),
346 HRD_LEN(ap),
347 tok2str(ethertype_values, "Unknown Protocol (0x%04x)", pro),
348 PROTO_LEN(ap)));
350 /* don't know know about the address formats */
351 if (!ndo->ndo_vflag) {
352 goto out;
356 /* print operation */
357 ND_PRINT((ndo, "%s%s ",
358 ndo->ndo_vflag ? ", " : "",
359 tok2str(arpop_values, "Unknown (%u)", op)));
361 switch (op) {
363 case ARPOP_REQUEST:
364 ND_PRINT((ndo, "who-has %s", ipaddr_string(ndo, TPA(ap))));
365 if (memcmp((const char *)ezero, (const char *)THA(ap), HRD_LEN(ap)) != 0)
366 ND_PRINT((ndo, " (%s)",
367 linkaddr_string(ndo, THA(ap), linkaddr, HRD_LEN(ap))));
368 ND_PRINT((ndo, " tell %s", ipaddr_string(ndo, SPA(ap))));
369 break;
371 case ARPOP_REPLY:
372 ND_PRINT((ndo, "%s is-at %s",
373 ipaddr_string(ndo, SPA(ap)),
374 linkaddr_string(ndo, SHA(ap), linkaddr, HRD_LEN(ap))));
375 break;
377 case ARPOP_REVREQUEST:
378 ND_PRINT((ndo, "who-is %s tell %s",
379 linkaddr_string(ndo, THA(ap), linkaddr, HRD_LEN(ap)),
380 linkaddr_string(ndo, SHA(ap), linkaddr, HRD_LEN(ap))));
381 break;
383 case ARPOP_REVREPLY:
384 ND_PRINT((ndo, "%s at %s",
385 linkaddr_string(ndo, THA(ap), linkaddr, HRD_LEN(ap)),
386 ipaddr_string(ndo, TPA(ap))));
387 break;
389 case ARPOP_INVREQUEST:
390 ND_PRINT((ndo, "who-is %s tell %s",
391 linkaddr_string(ndo, THA(ap), linkaddr, HRD_LEN(ap)),
392 linkaddr_string(ndo, SHA(ap), linkaddr, HRD_LEN(ap))));
393 break;
395 case ARPOP_INVREPLY:
396 ND_PRINT((ndo,"%s at %s",
397 linkaddr_string(ndo, SHA(ap), linkaddr, HRD_LEN(ap)),
398 ipaddr_string(ndo, SPA(ap))));
399 break;
401 default:
402 ND_DEFAULTPRINT((const u_char *)ap, caplen);
403 return;
406 out:
407 ND_PRINT((ndo, ", length %u", length));
409 return;
410 trunc:
411 ND_PRINT((ndo, "%s", tstr));
415 * Local Variables:
416 * c-style: bsd
417 * End: