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