xref: /illumos-gate/usr/src/boot/libsa/rarp.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*	$NetBSD: rarp.c,v 1.16 1997/07/07 15:52:52 drochner 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. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp  (LBL)
36  */
37 
38 #include <sys/cdefs.h>
39 
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <net/if.h>
43 #include <netinet/in.h>
44 #include <netinet/if_ether.h>
45 
46 #include <netinet/in_systm.h>
47 
48 #include <string.h>
49 
50 #include "stand.h"
51 #include "net.h"
52 #include "netif.h"
53 
54 
55 static ssize_t rarpsend(struct iodesc *, void *, size_t);
56 static ssize_t rarprecv(struct iodesc *, void **, void **, time_t, void *);
57 
58 /*
59  * Ethernet (Reverse) Address Resolution Protocol (see RFC 903, and 826).
60  */
61 int
62 rarp_getipaddress(int sock)
63 {
64 	struct iodesc *d;
65 	struct ether_arp *ap;
66 	void *pkt;
67 	struct {
68 		u_char header[ETHER_SIZE];
69 		struct {
70 			struct ether_arp arp;
71 			u_char pad[18]; 	/* 60 - sizeof(arp) */
72 		} data;
73 	} wbuf;
74 
75 #ifdef RARP_DEBUG
76  	if (debug)
77 		printf("rarp: socket=%d\n", sock);
78 #endif
79 	if (!(d = socktodesc(sock))) {
80 		printf("rarp: bad socket. %d\n", sock);
81 		return (-1);
82 	}
83 #ifdef RARP_DEBUG
84  	if (debug)
85 		printf("rarp: d=%x\n", (u_int)d);
86 #endif
87 
88 	bzero((char*)&wbuf.data, sizeof(wbuf.data));
89 	ap = &wbuf.data.arp;
90 	ap->arp_hrd = htons(ARPHRD_ETHER);
91 	ap->arp_pro = htons(ETHERTYPE_IP);
92 	ap->arp_hln = sizeof(ap->arp_sha); /* hardware address length */
93 	ap->arp_pln = sizeof(ap->arp_spa); /* protocol address length */
94 	ap->arp_op = htons(ARPOP_REVREQUEST);
95 	bcopy(d->myea, ap->arp_sha, 6);
96 	bcopy(d->myea, ap->arp_tha, 6);
97 	pkt = NULL;
98 
99 	if (sendrecv(d,
100 	    rarpsend, &wbuf.data, sizeof(wbuf.data),
101 	    rarprecv, &pkt, (void *)&ap, NULL) < 0) {
102 		printf("No response for RARP request\n");
103 		return (-1);
104 	}
105 
106 	bcopy(ap->arp_tpa, (char *)&myip, sizeof(myip));
107 #if 0
108 	/* XXX - Can NOT assume this is our root server! */
109 	bcopy(ap->arp_spa, (char *)&rootip, sizeof(rootip));
110 #endif
111 	free(pkt);
112 
113 	/* Compute our "natural" netmask. */
114 	if (IN_CLASSA(myip.s_addr))
115 		netmask = IN_CLASSA_NET;
116 	else if (IN_CLASSB(myip.s_addr))
117 		netmask = IN_CLASSB_NET;
118 	else
119 		netmask = IN_CLASSC_NET;
120 
121 	d->myip = myip;
122 	return (0);
123 }
124 
125 /*
126  * Broadcast a RARP request (i.e. who knows who I am)
127  */
128 static ssize_t
129 rarpsend(struct iodesc *d, void *pkt, size_t len)
130 {
131 
132 #ifdef RARP_DEBUG
133  	if (debug)
134 		printf("rarpsend: called\n");
135 #endif
136 
137 	return (sendether(d, pkt, len, bcea, ETHERTYPE_REVARP));
138 }
139 
140 /*
141  * Returns 0 if this is the packet we're waiting for
142  * else -1 (and errno == 0)
143  */
144 static ssize_t
145 rarprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft,
146     void *extra __unused)
147 {
148 	ssize_t n;
149 	struct ether_arp *ap;
150 	void *ptr = NULL;
151 	uint16_t etype;		/* host order */
152 
153 #ifdef RARP_DEBUG
154  	if (debug)
155 		printf("rarprecv: ");
156 #endif
157 
158 	n = readether(d, &ptr, (void **)&ap, tleft, &etype);
159 	errno = 0;	/* XXX */
160 	if (n == -1 || n < sizeof(struct ether_arp)) {
161 #ifdef RARP_DEBUG
162 		if (debug)
163 			printf("bad len=%d\n", n);
164 #endif
165 		free(ptr);
166 		return (-1);
167 	}
168 
169 	if (etype != ETHERTYPE_REVARP) {
170 #ifdef RARP_DEBUG
171 		if (debug)
172 			printf("bad type=0x%x\n", etype);
173 #endif
174 		free(ptr);
175 		return (-1);
176 	}
177 
178 	if (ap->arp_hrd != htons(ARPHRD_ETHER) ||
179 	    ap->arp_pro != htons(ETHERTYPE_IP) ||
180 	    ap->arp_hln != sizeof(ap->arp_sha) ||
181 	    ap->arp_pln != sizeof(ap->arp_spa) )
182 	{
183 #ifdef RARP_DEBUG
184 		if (debug)
185 			printf("bad hrd/pro/hln/pln\n");
186 #endif
187 		free(ptr);
188 		return (-1);
189 	}
190 
191 	if (ap->arp_op != htons(ARPOP_REVREPLY)) {
192 #ifdef RARP_DEBUG
193 		if (debug)
194 			printf("bad op=0x%x\n", ntohs(ap->arp_op));
195 #endif
196 		free(ptr);
197 		return (-1);
198 	}
199 
200 	/* Is the reply for our Ethernet address? */
201 	if (bcmp(ap->arp_tha, d->myea, 6)) {
202 #ifdef RARP_DEBUG
203 		if (debug)
204 			printf("unwanted address\n");
205 #endif
206 		free(ptr);
207 		return (-1);
208 	}
209 
210 	/* We have our answer. */
211 #ifdef RARP_DEBUG
212  	if (debug)
213 		printf("got it\n");
214 #endif
215 	*pkt = ptr;
216 	*payload = ap;
217 	return (n);
218 }
219