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