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