xref: /freebsd/contrib/tcpdump/print-arp.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
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 char rcsid[] =
24     "@(#) $Header: print-arp.c,v 1.39 96/07/17 14:56:17 leres Exp $ (LBL)";
25 #endif
26 
27 #include <sys/param.h>
28 #include <sys/time.h>
29 #include <sys/socket.h>
30 
31 #if __STDC__
32 struct mbuf;
33 struct rtentry;
34 #endif
35 #include <net/if.h>
36 
37 #include <netinet/in.h>
38 #include <netinet/if_ether.h>
39 
40 #include <stdio.h>
41 #include <string.h>
42 
43 #include "interface.h"
44 #include "addrtoname.h"
45 #include "ethertype.h"
46 #include "extract.h"			/* must come after interface.h */
47 
48 /* Compatibility */
49 #ifndef REVARP_REQUEST
50 #define REVARP_REQUEST		3
51 #endif
52 #ifndef REVARP_REPLY
53 #define REVARP_REPLY		4
54 #endif
55 
56 static u_char ezero[6];
57 
58 void
59 arp_print(register const u_char *bp, u_int length, u_int caplen)
60 {
61 	register const struct ether_arp *ap;
62 	register const struct ether_header *eh;
63 	register u_short pro, hrd, op;
64 
65 	ap = (struct ether_arp *)bp;
66 	if ((u_char *)(ap + 1) > snapend) {
67 		printf("[|arp]");
68 		return;
69 	}
70 	if (length < sizeof(struct ether_arp)) {
71 		(void)printf("truncated-arp");
72 		default_print((u_char *)ap, length);
73 		return;
74 	}
75 
76 	pro = EXTRACT_16BITS(&ap->arp_pro);
77 	hrd = EXTRACT_16BITS(&ap->arp_hrd);
78 	op = EXTRACT_16BITS(&ap->arp_op);
79 
80 	if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
81 	    || ap->arp_hln != sizeof(SHA(ap))
82 	    || ap->arp_pln != sizeof(SPA(ap))) {
83 		(void)printf("arp-#%d for proto #%d (%d) hardware #%d (%d)",
84 				op, pro, ap->arp_pln,
85 				hrd, ap->arp_hln);
86 		return;
87 	}
88 	if (pro == ETHERTYPE_TRAIL)
89 		(void)printf("trailer-");
90 	eh = (struct ether_header *)packetp;
91 	switch (op) {
92 
93 	case ARPOP_REQUEST:
94 		(void)printf("arp who-has %s", ipaddr_string(TPA(ap)));
95 		if (memcmp((char *)ezero, (char *)THA(ap), 6) != 0)
96 			(void)printf(" (%s)", etheraddr_string(THA(ap)));
97 		(void)printf(" tell %s", ipaddr_string(SPA(ap)));
98 		if (memcmp((char *)ESRC(eh), (char *)SHA(ap), 6) != 0)
99 			(void)printf(" (%s)", etheraddr_string(SHA(ap)));
100 		break;
101 
102 	case ARPOP_REPLY:
103 		(void)printf("arp reply %s", ipaddr_string(SPA(ap)));
104 		if (memcmp((char *)ESRC(eh), (char *)SHA(ap), 6) != 0)
105 			(void)printf(" (%s)", etheraddr_string(SHA(ap)));
106 		(void)printf(" is-at %s", etheraddr_string(SHA(ap)));
107 		if (memcmp((char *)EDST(eh), (char *)THA(ap), 6) != 0)
108 			(void)printf(" (%s)", etheraddr_string(THA(ap)));
109 		break;
110 
111 	case REVARP_REQUEST:
112 		(void)printf("rarp who-is %s tell %s",
113 			etheraddr_string(THA(ap)),
114 			etheraddr_string(SHA(ap)));
115 		break;
116 
117 	case REVARP_REPLY:
118 		(void)printf("rarp reply %s at %s",
119 			etheraddr_string(THA(ap)),
120 			ipaddr_string(TPA(ap)));
121 		break;
122 
123 	default:
124 		(void)printf("arp-#%d", op);
125 		default_print((u_char *)ap, caplen);
126 		return;
127 	}
128 	if (hrd != ARPHRD_ETHER)
129 		printf(" hardware #%d", ap->arp_hrd);
130 }
131