• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.64 2004/04/30 16:42:14 mcr 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_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 /*
71  * The remaining fields are variable in size,
72  * according to the sizes above.
73  */
74 #ifdef COMMENT_ONLY
75 	u_char	ar_sha[];	/* sender hardware address */
76 	u_char	ar_spa[];	/* sender protocol address */
77 	u_char	ar_tha[];	/* target hardware address */
78 	u_char	ar_tpa[];	/* target protocol address */
79 #endif
80 #define ar_sha(ap)	(((const u_char *)((ap)+1))+0)
81 #define ar_spa(ap)	(((const u_char *)((ap)+1))+  (ap)->ar_hln)
82 #define ar_tha(ap)	(((const u_char *)((ap)+1))+  (ap)->ar_hln+(ap)->ar_pln)
83 #define ar_tpa(ap)	(((const u_char *)((ap)+1))+2*(ap)->ar_hln+(ap)->ar_pln)
84 };
85 
86 #define ARP_HDRLEN	8
87 
88 #define HRD(ap) EXTRACT_16BITS(&(ap)->ar_hrd)
89 #define HLN(ap) ((ap)->ar_hln)
90 #define PLN(ap) ((ap)->ar_pln)
91 #define OP(ap)  EXTRACT_16BITS(&(ap)->ar_op)
92 #define PRO(ap) EXTRACT_16BITS(&(ap)->ar_pro)
93 #define SHA(ap) (ar_sha(ap))
94 #define SPA(ap) (ar_spa(ap))
95 #define THA(ap) (ar_tha(ap))
96 #define TPA(ap) (ar_tpa(ap))
97 
98 /*
99  * ATM Address Resolution Protocol.
100  *
101  * See RFC 2225 for protocol description.  ATMARP packets are similar
102  * to ARP packets, except that there are no length fields for the
103  * protocol address - instead, there are type/length fields for
104  * the ATM number and subaddress - and the hardware addresses consist
105  * of an ATM number and an ATM subaddress.
106  */
107 struct	atmarp_pkthdr {
108 	u_short	aar_hrd;	/* format of hardware address */
109 #define ARPHRD_ATM2225	19	/* ATM (RFC 2225) */
110 	u_short	aar_pro;	/* format of protocol address */
111 	u_char	aar_shtl;	/* length of source ATM number */
112 	u_char	aar_sstl;	/* length of source ATM subaddress */
113 #define ATMARP_IS_E164	0x40	/* bit in type/length for E.164 format */
114 #define ATMARP_LEN_MASK	0x3F	/* length of {sub}address in type/length */
115 	u_short	aar_op;		/* same as regular ARP */
116 #define ATMARPOP_NAK	10	/* NAK */
117 	u_char	aar_spln;	/* length of source protocol address */
118 	u_char	aar_thtl;	/* length of target ATM number */
119 	u_char	aar_tstl;	/* length of target ATM subaddress */
120 	u_char	aar_tpln;	/* length of target protocol address */
121 /*
122  * The remaining fields are variable in size,
123  * according to the sizes above.
124  */
125 #ifdef COMMENT_ONLY
126 	u_char	aar_sha[];	/* source ATM number */
127 	u_char	aar_ssa[];	/* source ATM subaddress */
128 	u_char	aar_spa[];	/* sender protocol address */
129 	u_char	aar_tha[];	/* target ATM number */
130 	u_char	aar_tsa[];	/* target ATM subaddress */
131 	u_char	aar_tpa[];	/* target protocol address */
132 #endif
133 
134 #define ATMHRD(ap)  EXTRACT_16BITS(&(ap)->aar_hrd)
135 #define ATMSHLN(ap) ((ap)->aar_shtl & ATMARP_LEN_MASK)
136 #define ATMSSLN(ap) ((ap)->aar_sstl & ATMARP_LEN_MASK)
137 #define ATMSPLN(ap) ((ap)->aar_spln)
138 #define ATMOP(ap)   EXTRACT_16BITS(&(ap)->aar_op)
139 #define ATMPRO(ap)  EXTRACT_16BITS(&(ap)->aar_pro)
140 #define ATMTHLN(ap) ((ap)->aar_thtl & ATMARP_LEN_MASK)
141 #define ATMTSLN(ap) ((ap)->aar_tstl & ATMARP_LEN_MASK)
142 #define ATMTPLN(ap) ((ap)->aar_tpln)
143 #define aar_sha(ap)	((const u_char *)((ap)+1))
144 #define aar_ssa(ap)	(aar_sha(ap) + ATMSHLN(ap))
145 #define aar_spa(ap)	(aar_ssa(ap) + ATMSSLN(ap))
146 #define aar_tha(ap)	(aar_spa(ap) + ATMSPLN(ap))
147 #define aar_tsa(ap)	(aar_tha(ap) + ATMTHLN(ap))
148 #define aar_tpa(ap)	(aar_tsa(ap) + ATMTSLN(ap))
149 };
150 
151 #define ATMSHA(ap) (aar_sha(ap))
152 #define ATMSSA(ap) (aar_ssa(ap))
153 #define ATMSPA(ap) (aar_spa(ap))
154 #define ATMTHA(ap) (aar_tha(ap))
155 #define ATMTSA(ap) (aar_tsa(ap))
156 #define ATMTPA(ap) (aar_tpa(ap))
157 
158 static u_char ezero[6];
159 
160 static void
atmarp_addr_print(netdissect_options * ndo,const u_char * ha,u_int ha_len,const u_char * srca,u_int srca_len)161 atmarp_addr_print(netdissect_options *ndo,
162 		  const u_char *ha, u_int ha_len, const u_char *srca,
163     u_int srca_len)
164 {
165 	if (ha_len == 0)
166 		ND_PRINT((ndo, "<No address>"));
167 	else {
168 		ND_PRINT((ndo, "%s", linkaddr_string(ha, ha_len)));
169 		if (srca_len != 0)
170 			ND_PRINT((ndo, ",%s",
171 				  linkaddr_string(srca, srca_len)));
172 	}
173 }
174 
175 static void
atmarp_print(netdissect_options * ndo,const u_char * bp,u_int length,u_int caplen)176 atmarp_print(netdissect_options *ndo,
177 	     const u_char *bp, u_int length, u_int caplen)
178 {
179 	const struct atmarp_pkthdr *ap;
180 	u_short pro, hrd, op;
181 
182 	ap = (const struct atmarp_pkthdr *)bp;
183 	ND_TCHECK(*ap);
184 
185 	hrd = ATMHRD(ap);
186 	pro = ATMPRO(ap);
187 	op = ATMOP(ap);
188 
189 	if (!ND_TTEST2(*aar_tpa(ap), ATMTPLN(ap))) {
190 		ND_PRINT((ndo, "truncated-atmarp"));
191 		ND_DEFAULTPRINT((const u_char *)ap, length);
192 		return;
193 	}
194 
195 	if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
196 	    ATMSPLN(ap) != 4 || ATMTPLN(ap) != 4) {
197 		ND_PRINT((ndo, "atmarp-#%d for proto #%d (%d/%d) hardware #%d",
198 			  op, pro, ATMSPLN(ap), ATMTPLN(ap), hrd));
199 		return;
200 	}
201 	if (pro == ETHERTYPE_TRAIL)
202 		ND_PRINT((ndo, "trailer-"));
203 	switch (op) {
204 
205 	case ARPOP_REQUEST:
206 		ND_PRINT((ndo, "arp who-has %s", ipaddr_string(ATMTPA(ap))));
207 		if (ATMTHLN(ap) != 0) {
208 			ND_PRINT((ndo, " ("));
209 			atmarp_addr_print(ndo, ATMTHA(ap), ATMTHLN(ap),
210 			    ATMTSA(ap), ATMTSLN(ap));
211 			ND_PRINT((ndo, ")"));
212 		}
213 		ND_PRINT((ndo, " tell %s", ipaddr_string(ATMSPA(ap))));
214 		break;
215 
216 	case ARPOP_REPLY:
217 		ND_PRINT((ndo, "arp reply %s", ipaddr_string(ATMSPA(ap))));
218 		ND_PRINT((ndo, " is-at "));
219 		atmarp_addr_print(ndo, ATMSHA(ap), ATMSHLN(ap), ATMSSA(ap),
220 		    ATMSSLN(ap));
221 		break;
222 
223 	case ARPOP_INVREQUEST:
224 		ND_PRINT((ndo, "invarp who-is "));
225 		atmarp_addr_print(ndo, ATMTHA(ap), ATMTHLN(ap), ATMTSA(ap),
226 		    ATMTSLN(ap));
227 		ND_PRINT((ndo, " tell "));
228 		atmarp_addr_print(ndo, ATMSHA(ap), ATMSHLN(ap), ATMSSA(ap),
229 		    ATMSSLN(ap));
230 		break;
231 
232 	case ARPOP_INVREPLY:
233 		ND_PRINT((ndo, "invarp reply "));
234 		atmarp_addr_print(ndo, ATMSHA(ap), ATMSHLN(ap), ATMSSA(ap),
235 		    ATMSSLN(ap));
236 		ND_PRINT((ndo, " at %s", ipaddr_string(ATMSPA(ap))));
237 		break;
238 
239 	case ATMARPOP_NAK:
240 		ND_PRINT((ndo, "nak reply for %s",
241 			  ipaddr_string(ATMSPA(ap))));
242 		break;
243 
244 	default:
245 		ND_PRINT((ndo, "atmarp-#%d", op));
246 		ND_DEFAULTPRINT((const u_char *)ap, caplen);
247 		return;
248 	}
249 	return;
250 trunc:
251 	ND_PRINT((ndo, "[|atmarp]"));
252 }
253 
254 void
arp_print(netdissect_options * ndo,const u_char * bp,u_int length,u_int caplen)255 arp_print(netdissect_options *ndo,
256 	  const u_char *bp, u_int length, u_int caplen)
257 {
258 	const struct arp_pkthdr *ap;
259 	u_short pro, hrd, op;
260 
261 	ap = (const struct arp_pkthdr *)bp;
262 	ND_TCHECK(*ap);
263 	hrd = HRD(ap);
264 	if (hrd == ARPHRD_ATM2225) {
265 	        atmarp_print(ndo, bp, length, caplen);
266 		return;
267 	}
268 	pro = PRO(ap);
269 	op = OP(ap);
270 
271 	if (!ND_TTEST2(*ar_tpa(ap), PLN(ap))) {
272 		ND_PRINT((ndo, "truncated-arp"));
273 		ND_DEFAULTPRINT((const u_char *)ap, length);
274 		return;
275 	}
276 
277 	if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) ||
278 	    PLN(ap) != 4 || HLN(ap) == 0) {
279 		ND_PRINT((ndo, "arp-#%d for proto #%d (%d) hardware #%d (%d)",
280 			  op, pro, PLN(ap), hrd, HLN(ap)));
281 		return;
282 	}
283 	if (pro == ETHERTYPE_TRAIL)
284 		ND_PRINT((ndo, "trailer-"));
285 	switch (op) {
286 
287 	case ARPOP_REQUEST:
288 		ND_PRINT((ndo, "arp who-has %s", ipaddr_string(TPA(ap))));
289 		if (memcmp((const char *)ezero, (const char *)THA(ap), HLN(ap)) != 0)
290 			ND_PRINT((ndo, " (%s)",
291 				  linkaddr_string(THA(ap), HLN(ap))));
292 		ND_PRINT((ndo, " tell %s", ipaddr_string(SPA(ap))));
293 		break;
294 
295 	case ARPOP_REPLY:
296 		ND_PRINT((ndo, "arp reply %s", ipaddr_string(SPA(ap))));
297 		ND_PRINT((ndo, " is-at %s", linkaddr_string(SHA(ap), HLN(ap))));
298 		break;
299 
300 	case ARPOP_REVREQUEST:
301 		ND_PRINT((ndo, "rarp who-is %s tell %s",
302 			  linkaddr_string(THA(ap), HLN(ap)),
303 			  linkaddr_string(SHA(ap), HLN(ap))));
304 		break;
305 
306 	case ARPOP_REVREPLY:
307 		ND_PRINT((ndo, "rarp reply %s at %s",
308 			  linkaddr_string(THA(ap), HLN(ap)),
309 			  ipaddr_string(TPA(ap))));
310 		break;
311 
312 	case ARPOP_INVREQUEST:
313 		ND_PRINT((ndo, "invarp who-is %s tell %s",
314 			  linkaddr_string(THA(ap), HLN(ap)),
315 			  linkaddr_string(SHA(ap), HLN(ap))));
316 		break;
317 
318 	case ARPOP_INVREPLY:
319 		ND_PRINT((ndo,"invarp reply %s at %s",
320 			  linkaddr_string(THA(ap), HLN(ap)),
321 			  ipaddr_string(TPA(ap))));
322 		break;
323 
324 	default:
325 		ND_PRINT((ndo, "arp-#%d", op));
326 		ND_DEFAULTPRINT((const u_char *)ap, caplen);
327 		return;
328 	}
329 	if (hrd != ARPHRD_ETHER)
330 		ND_PRINT((ndo, " hardware #%d", hrd));
331 	return;
332 trunc:
333 	ND_PRINT((ndo, "[|arp]"));
334 }
335 
336 /*
337  * Local Variables:
338  * c-style: bsd
339  * End:
340  */
341 
342