xref: /freebsd/stand/libsa/rarp.c (revision 61ba55bcf70f2340f9c943c9571113b3fd8eda69)
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 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netinet/if_ether.h>
44 
45 #include <netinet/in_systm.h>
46 
47 #include <string.h>
48 
49 #include "stand.h"
50 #include "net.h"
51 #include "netif.h"
52 
53 
54 static ssize_t rarpsend(struct iodesc *, void *, size_t);
55 static ssize_t rarprecv(struct iodesc *, void **, void **, time_t, void *);
56 
57 /*
58  * Ethernet (Reverse) Address Resolution Protocol (see RFC 903, and 826).
59  */
60 int
61 rarp_getipaddress(int sock)
62 {
63 	struct iodesc *d;
64 	struct ether_arp *ap;
65 	void *pkt;
66 	struct {
67 		u_char header[ETHER_SIZE];
68 		struct {
69 			struct ether_arp arp;
70 			u_char pad[18]; 	/* 60 - sizeof(arp) */
71 		} data;
72 	} wbuf;
73 
74 #ifdef RARP_DEBUG
75  	if (debug)
76 		printf("rarp: socket=%d\n", sock);
77 #endif
78 	if (!(d = socktodesc(sock))) {
79 		printf("rarp: bad socket. %d\n", sock);
80 		return (-1);
81 	}
82 #ifdef RARP_DEBUG
83  	if (debug)
84 		printf("rarp: d=%lx\n", (long)d);
85 #endif
86 
87 	bzero((char*)&wbuf.data, sizeof(wbuf.data));
88 	ap = &wbuf.data.arp;
89 	ap->arp_hrd = htons(ARPHRD_ETHER);
90 	ap->arp_pro = htons(ETHERTYPE_IP);
91 	ap->arp_hln = sizeof(ap->arp_sha); /* hardware address length */
92 	ap->arp_pln = sizeof(ap->arp_spa); /* protocol address length */
93 	ap->arp_op = htons(ARPOP_REVREQUEST);
94 	bcopy(d->myea, ap->arp_sha, 6);
95 	bcopy(d->myea, ap->arp_tha, 6);
96 	pkt = NULL;
97 
98 	if (sendrecv(d,
99 	    rarpsend, &wbuf.data, sizeof(wbuf.data),
100 	    rarprecv, &pkt, (void *)&ap, NULL) < 0) {
101 		printf("No response for RARP request\n");
102 		return (-1);
103 	}
104 
105 	bcopy(ap->arp_tpa, (char *)&myip, sizeof(myip));
106 #if 0
107 	/* XXX - Can NOT assume this is our root server! */
108 	bcopy(ap->arp_spa, (char *)&rootip, sizeof(rootip));
109 #endif
110 	free(pkt);
111 
112 	/* Compute our "natural" netmask. */
113 	if (IN_CLASSA(myip.s_addr))
114 		netmask = IN_CLASSA_NET;
115 	else if (IN_CLASSB(myip.s_addr))
116 		netmask = IN_CLASSB_NET;
117 	else
118 		netmask = IN_CLASSC_NET;
119 
120 	d->myip = myip;
121 	return (0);
122 }
123 
124 /*
125  * Broadcast a RARP request (i.e. who knows who I am)
126  */
127 static ssize_t
128 rarpsend(struct iodesc *d, void *pkt, size_t len)
129 {
130 
131 #ifdef RARP_DEBUG
132  	if (debug)
133 		printf("rarpsend: called\n");
134 #endif
135 
136 	return (sendether(d, pkt, len, bcea, ETHERTYPE_REVARP));
137 }
138 
139 /*
140  * Returns 0 if this is the packet we're waiting for
141  * else -1 (and errno == 0)
142  */
143 static ssize_t
144 rarprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft,
145     void *extra)
146 {
147 	ssize_t n;
148 	struct ether_arp *ap;
149 	void *ptr = NULL;
150 	uint16_t etype;		/* host order */
151 
152 #ifdef RARP_DEBUG
153  	if (debug)
154 		printf("rarprecv: ");
155 #endif
156 
157 	n = readether(d, &ptr, (void **)&ap, tleft, &etype);
158 	errno = 0;	/* XXX */
159 	if (n == -1 || n < sizeof(struct ether_arp)) {
160 #ifdef RARP_DEBUG
161 		if (debug)
162 			printf("bad len=%zd\n", n);
163 #endif
164 		free(ptr);
165 		return (-1);
166 	}
167 
168 	if (etype != ETHERTYPE_REVARP) {
169 #ifdef RARP_DEBUG
170 		if (debug)
171 			printf("bad type=0x%x\n", etype);
172 #endif
173 		free(ptr);
174 		return (-1);
175 	}
176 
177 	if (ap->arp_hrd != htons(ARPHRD_ETHER) ||
178 	    ap->arp_pro != htons(ETHERTYPE_IP) ||
179 	    ap->arp_hln != sizeof(ap->arp_sha) ||
180 	    ap->arp_pln != sizeof(ap->arp_spa) )
181 	{
182 #ifdef RARP_DEBUG
183 		if (debug)
184 			printf("bad hrd/pro/hln/pln\n");
185 #endif
186 		free(ptr);
187 		return (-1);
188 	}
189 
190 	if (ap->arp_op != htons(ARPOP_REVREPLY)) {
191 #ifdef RARP_DEBUG
192 		if (debug)
193 			printf("bad op=0x%x\n", ntohs(ap->arp_op));
194 #endif
195 		free(ptr);
196 		return (-1);
197 	}
198 
199 	/* Is the reply for our Ethernet address? */
200 	if (bcmp(ap->arp_tha, d->myea, 6)) {
201 #ifdef RARP_DEBUG
202 		if (debug)
203 			printf("unwanted address\n");
204 #endif
205 		free(ptr);
206 		return (-1);
207 	}
208 
209 	/* We have our answer. */
210 #ifdef RARP_DEBUG
211  	if (debug)
212 		printf("got it\n");
213 #endif
214 	*pkt = ptr;
215 	*payload = ap;
216 	return (n);
217 }
218