1*426b943dSderaadt /* $OpenBSD: print-arp.c,v 1.17 2021/12/01 18:28:45 deraadt Exp $ */
2cf4e9b47Sho
3df930be7Sderaadt /*
45205045aSbrad * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5df930be7Sderaadt * The Regents of the University of California. All rights reserved.
6df930be7Sderaadt *
7df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
8df930be7Sderaadt * modification, are permitted provided that: (1) source code distributions
9df930be7Sderaadt * retain the above copyright notice and this paragraph in its entirety, (2)
10df930be7Sderaadt * distributions including binary code include the above copyright notice and
11df930be7Sderaadt * this paragraph in its entirety in the documentation or other materials
12df930be7Sderaadt * provided with the distribution, and (3) all advertising materials mentioning
13df930be7Sderaadt * features or use of this software display the following acknowledgement:
14df930be7Sderaadt * ``This product includes software developed by the University of California,
15df930be7Sderaadt * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16df930be7Sderaadt * the University nor the names of its contributors may be used to endorse
17df930be7Sderaadt * or promote products derived from this software without specific prior
18df930be7Sderaadt * written permission.
19df930be7Sderaadt * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20df930be7Sderaadt * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21df930be7Sderaadt * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22df930be7Sderaadt */
23df930be7Sderaadt
24df930be7Sderaadt #include <sys/time.h>
25df930be7Sderaadt #include <sys/socket.h>
26df930be7Sderaadt
27df930be7Sderaadt #include <net/if.h>
28df930be7Sderaadt
29df930be7Sderaadt #include <netinet/in.h>
30df930be7Sderaadt #include <netinet/if_ether.h>
31df930be7Sderaadt
32df930be7Sderaadt #include <stdio.h>
33c15d59edSmickey #include <string.h>
34df930be7Sderaadt
35df930be7Sderaadt #include "interface.h"
36df930be7Sderaadt #include "addrtoname.h"
37dc709136Sbitblt #include "ethertype.h"
38c15d59edSmickey #include "extract.h" /* must come after interface.h */
39c15d59edSmickey
40dc709136Sbitblt /* Compatibility */
41dc709136Sbitblt #ifndef REVARP_REQUEST
42dc709136Sbitblt #define REVARP_REQUEST 3
43dc709136Sbitblt #endif
44dc709136Sbitblt #ifndef REVARP_REPLY
45dc709136Sbitblt #define REVARP_REPLY 4
46c15d59edSmickey #endif
47df930be7Sderaadt
48df930be7Sderaadt static u_char ezero[6];
49df930be7Sderaadt
50df930be7Sderaadt void
arp_print(const u_char * bp,u_int length,u_int caplen)510d5f83f3Smmcc arp_print(const u_char *bp, u_int length, u_int caplen)
52df930be7Sderaadt {
530d5f83f3Smmcc const struct ether_arp *ap;
540d5f83f3Smmcc const struct ether_header *eh;
550d5f83f3Smmcc u_short pro, hrd, op;
56df930be7Sderaadt
57df930be7Sderaadt ap = (struct ether_arp *)bp;
58df930be7Sderaadt if ((u_char *)(ap + 1) > snapend) {
59df930be7Sderaadt printf("[|arp]");
60df930be7Sderaadt return;
61df930be7Sderaadt }
62df930be7Sderaadt if (length < sizeof(struct ether_arp)) {
63f96bb33fSprocter printf("truncated-arp");
64df930be7Sderaadt default_print((u_char *)ap, length);
65df930be7Sderaadt return;
66df930be7Sderaadt }
67c15d59edSmickey
68dc709136Sbitblt pro = EXTRACT_16BITS(&ap->arp_pro);
69dc709136Sbitblt hrd = EXTRACT_16BITS(&ap->arp_hrd);
70dc709136Sbitblt op = EXTRACT_16BITS(&ap->arp_op);
71df930be7Sderaadt
72df930be7Sderaadt if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
73df930be7Sderaadt || ap->arp_hln != sizeof(SHA(ap))
74df930be7Sderaadt || ap->arp_pln != sizeof(SPA(ap))) {
75f96bb33fSprocter printf("arp-#%d for proto #%d (%d) hardware #%d (%d)",
76f96bb33fSprocter op, pro, ap->arp_pln, hrd, ap->arp_hln);
77df930be7Sderaadt return;
78df930be7Sderaadt }
79df930be7Sderaadt if (pro == ETHERTYPE_TRAIL)
80f96bb33fSprocter printf("trailer-");
81df930be7Sderaadt eh = (struct ether_header *)packetp;
82df930be7Sderaadt switch (op) {
83df930be7Sderaadt
84df930be7Sderaadt case ARPOP_REQUEST:
85f96bb33fSprocter printf("arp who-has %s", ipaddr_string(TPA(ap)));
86c15d59edSmickey if (memcmp((char *)ezero, (char *)THA(ap), 6) != 0)
87f96bb33fSprocter printf(" (%s)", etheraddr_string(THA(ap)));
88f96bb33fSprocter printf(" tell %s", ipaddr_string(SPA(ap)));
89c15d59edSmickey if (memcmp((char *)ESRC(eh), (char *)SHA(ap), 6) != 0)
90f96bb33fSprocter printf(" (%s)", etheraddr_string(SHA(ap)));
91df930be7Sderaadt break;
92df930be7Sderaadt
93df930be7Sderaadt case ARPOP_REPLY:
94f96bb33fSprocter printf("arp reply %s", ipaddr_string(SPA(ap)));
95c15d59edSmickey if (memcmp((char *)ESRC(eh), (char *)SHA(ap), 6) != 0)
96f96bb33fSprocter printf(" (%s)", etheraddr_string(SHA(ap)));
97f96bb33fSprocter printf(" is-at %s", etheraddr_string(SHA(ap)));
98c15d59edSmickey if (memcmp((char *)EDST(eh), (char *)THA(ap), 6) != 0)
99f96bb33fSprocter printf(" (%s)", etheraddr_string(THA(ap)));
100df930be7Sderaadt break;
101df930be7Sderaadt
102df930be7Sderaadt case REVARP_REQUEST:
103f96bb33fSprocter printf("rarp who-is %s tell %s",
104df930be7Sderaadt etheraddr_string(THA(ap)),
105df930be7Sderaadt etheraddr_string(SHA(ap)));
106df930be7Sderaadt break;
107df930be7Sderaadt
108df930be7Sderaadt case REVARP_REPLY:
109f96bb33fSprocter printf("rarp reply %s at %s",
110df930be7Sderaadt etheraddr_string(THA(ap)),
111df930be7Sderaadt ipaddr_string(TPA(ap)));
112df930be7Sderaadt break;
113df930be7Sderaadt
114df930be7Sderaadt default:
115f96bb33fSprocter printf("arp-#%d", op);
116df930be7Sderaadt default_print((u_char *)ap, caplen);
117df930be7Sderaadt return;
118df930be7Sderaadt }
119df930be7Sderaadt if (hrd != ARPHRD_ETHER)
120dc709136Sbitblt printf(" hardware #%d", hrd);
121df930be7Sderaadt }
122