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