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