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