rarp.c revision 1.19 1 /* $NetBSD: rarp.c,v 1.19 1999/11/13 21:17:57 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1992 Regents of the University of California.
5 * All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Lawrence Berkeley Laboratory and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp (LBL)
40 */
41 #include <sys/param.h>
42 #include <sys/socket.h>
43 #include <net/if.h>
44 #include <net/if_ether.h>
45 #include <netinet/in.h>
46
47 #include <netinet/in_systm.h>
48
49 #ifdef _STANDALONE
50 #include <lib/libkern/libkern.h>
51 #else
52 #include <string.h>
53 #endif
54
55 #include "stand.h"
56 #include "net.h"
57 #include "netif.h"
58
59
60 /*
61 * Ethernet Address Resolution Protocol.
62 *
63 * See RFC 826 for protocol description. Structure below is adapted
64 * to resolving internet addresses. Field names used correspond to
65 * RFC 826.
66 */
67 struct ether_arp {
68 struct arphdr ea_hdr; /* fixed-size header */
69 u_int8_t arp_sha[ETHER_ADDR_LEN]; /* sender hardware address */
70 u_int8_t arp_spa[4]; /* sender protocol address */
71 u_int8_t arp_tha[ETHER_ADDR_LEN]; /* target hardware address */
72 u_int8_t arp_tpa[4]; /* target protocol address */
73 };
74 #define arp_hrd ea_hdr.ar_hrd
75 #define arp_pro ea_hdr.ar_pro
76 #define arp_hln ea_hdr.ar_hln
77 #define arp_pln ea_hdr.ar_pln
78 #define arp_op ea_hdr.ar_op
79
80 static ssize_t rarpsend __P((struct iodesc *, void *, size_t));
81 static ssize_t rarprecv __P((struct iodesc *, void *, size_t, time_t));
82
83 /*
84 * Ethernet (Reverse) Address Resolution Protocol (see RFC 903, and 826).
85 */
86 int
87 rarp_getipaddress(sock)
88 int sock;
89 {
90 struct iodesc *d;
91 register struct ether_arp *ap;
92 struct {
93 u_char header[ETHER_SIZE];
94 struct {
95 struct ether_arp arp;
96 u_char pad[18]; /* 60 - sizeof(arp) */
97 } data;
98 } wbuf;
99 struct {
100 u_char header[ETHER_SIZE];
101 struct {
102 struct ether_arp arp;
103 u_char pad[24]; /* extra space */
104 } data;
105 } rbuf;
106
107 #ifdef RARP_DEBUG
108 if (debug)
109 printf("rarp: socket=%d\n", sock);
110 #endif
111 if (!(d = socktodesc(sock))) {
112 printf("rarp: bad socket. %d\n", sock);
113 return (-1);
114 }
115 #ifdef RARP_DEBUG
116 if (debug)
117 printf("rarp: d=%lx\n", (u_long)d);
118 #endif
119
120 bzero((char*)&wbuf.data, sizeof(wbuf.data));
121 ap = &wbuf.data.arp;
122 ap->arp_hrd = htons(ARPHRD_ETHER);
123 ap->arp_pro = htons(ETHERTYPE_IP);
124 ap->arp_hln = sizeof(ap->arp_sha); /* hardware address length */
125 ap->arp_pln = sizeof(ap->arp_spa); /* protocol address length */
126 ap->arp_op = htons(ARPOP_REVREQUEST);
127 bcopy(d->myea, ap->arp_sha, 6);
128 bcopy(d->myea, ap->arp_tha, 6);
129
130 if (sendrecv(d,
131 rarpsend, &wbuf.data, sizeof(wbuf.data),
132 rarprecv, &rbuf.data, sizeof(rbuf.data)) < 0)
133 {
134 printf("No response for RARP request\n");
135 return (-1);
136 }
137
138 ap = &rbuf.data.arp;
139 bcopy(ap->arp_tpa, (char *)&myip, sizeof(myip));
140 #if 0
141 /* XXX - Can NOT assume this is our root server! */
142 bcopy(ap->arp_spa, (char *)&rootip, sizeof(rootip));
143 #endif
144
145 /* Compute our "natural" netmask. */
146 if (IN_CLASSA(myip.s_addr))
147 netmask = IN_CLASSA_NET;
148 else if (IN_CLASSB(myip.s_addr))
149 netmask = IN_CLASSB_NET;
150 else
151 netmask = IN_CLASSC_NET;
152
153 d->myip = myip;
154 return (0);
155 }
156
157 /*
158 * Broadcast a RARP request (i.e. who knows who I am)
159 */
160 static ssize_t
161 rarpsend(d, pkt, len)
162 register struct iodesc *d;
163 register void *pkt;
164 register size_t len;
165 {
166
167 #ifdef RARP_DEBUG
168 if (debug)
169 printf("rarpsend: called\n");
170 #endif
171
172 return (sendether(d, pkt, len, bcea, ETHERTYPE_REVARP));
173 }
174
175 /*
176 * Returns 0 if this is the packet we're waiting for
177 * else -1 (and errno == 0)
178 */
179 static ssize_t
180 rarprecv(d, pkt, len, tleft)
181 register struct iodesc *d;
182 register void *pkt;
183 register size_t len;
184 time_t tleft;
185 {
186 register ssize_t n;
187 register struct ether_arp *ap;
188 u_int16_t etype; /* host order */
189
190 #ifdef RARP_DEBUG
191 if (debug)
192 printf("rarprecv: ");
193 #endif
194
195 n = readether(d, pkt, len, tleft, &etype);
196 errno = 0; /* XXX */
197 if (n == -1 || n < sizeof(struct ether_arp)) {
198 #ifdef RARP_DEBUG
199 if (debug)
200 printf("bad len=%d\n", n);
201 #endif
202 return (-1);
203 }
204
205 if (etype != ETHERTYPE_REVARP) {
206 #ifdef RARP_DEBUG
207 if (debug)
208 printf("bad type=0x%x\n", etype);
209 #endif
210 return (-1);
211 }
212
213 ap = (struct ether_arp *)pkt;
214 if (ap->arp_hrd != htons(ARPHRD_ETHER) ||
215 ap->arp_pro != htons(ETHERTYPE_IP) ||
216 ap->arp_hln != sizeof(ap->arp_sha) ||
217 ap->arp_pln != sizeof(ap->arp_spa) )
218 {
219 #ifdef RARP_DEBUG
220 if (debug)
221 printf("bad hrd/pro/hln/pln\n");
222 #endif
223 return (-1);
224 }
225
226 if (ap->arp_op != htons(ARPOP_REVREPLY)) {
227 #ifdef RARP_DEBUG
228 if (debug)
229 printf("bad op=0x%x\n", ntohs(ap->arp_op));
230 #endif
231 return (-1);
232 }
233
234 /* Is the reply for our Ethernet address? */
235 if (bcmp(ap->arp_tha, d->myea, 6)) {
236 #ifdef RARP_DEBUG
237 if (debug)
238 printf("unwanted address\n");
239 #endif
240 return (-1);
241 }
242
243 /* We have our answer. */
244 #ifdef RARP_DEBUG
245 if (debug)
246 printf("got it\n");
247 #endif
248 return (n);
249 }
250