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