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