rarp.c revision 1.8 1 /* $NetBSD: rarp.c,v 1.8 1995/09/14 23:45:34 pk 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
46 #include <netinet/if_ether.h>
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 static ssize_t rarpsend __P((struct iodesc *, void *, size_t));
56 static ssize_t rarprecv __P((struct iodesc *, void *, size_t, time_t));
57
58 /*
59 * Ethernet (Reverse) Address Resolution Protocol (see RFC 903, and 826).
60 */
61 n_long
62 rarp_getipaddress(sock)
63 int sock;
64 {
65 struct iodesc *d;
66 register struct ether_arp *ap;
67 struct {
68 u_char header[ETHER_SIZE];
69 struct {
70 struct ether_arp arp;
71 u_char pad[18]; /* 60 - sizeof(arp) */
72 } data;
73 } wbuf;
74 struct {
75 u_char header[ETHER_SIZE];
76 struct {
77 struct ether_arp arp;
78 u_char pad[24]; /* extra space */
79 } data;
80 } rbuf;
81
82 #ifdef RARP_DEBUG
83 if (debug)
84 printf("rarp: socket=%d\n", sock);
85 #endif
86 if (!(d = socktodesc(sock))) {
87 printf("rarp: bad socket. %d\n", sock);
88 return(INADDR_ANY);
89 }
90 #ifdef RARP_DEBUG
91 if (debug)
92 printf("rarp: d=%x\n", (u_int)d);
93 #endif
94
95 bzero((char*)&wbuf.data, sizeof(wbuf.data));
96 ap = &wbuf.data.arp;
97 ap->arp_hrd = htons(ARPHRD_ETHER);
98 ap->arp_pro = htons(ETHERTYPE_IP);
99 ap->arp_hln = sizeof(ap->arp_sha); /* hardware address length */
100 ap->arp_pln = sizeof(ap->arp_spa); /* protocol address length */
101 ap->arp_op = htons(ARPOP_REVREQUEST);
102 bcopy(d->myea, ap->arp_sha, 6);
103 bcopy(d->myea, ap->arp_tha, 6);
104
105 if (sendrecv(d,
106 rarpsend, &wbuf.data, sizeof(wbuf.data),
107 rarprecv, &rbuf.data, sizeof(rbuf.data)) < 0) {
108 printf("No response for RARP request\n");
109 return(INADDR_ANY);
110 }
111
112 return (myip);
113 }
114
115 /*
116 * Broadcast a RARP request (i.e. who knows who I am)
117 */
118 static ssize_t
119 rarpsend(d, pkt, len)
120 register struct iodesc *d;
121 register void *pkt;
122 register size_t len;
123 {
124
125 #ifdef RARP_DEBUG
126 if (debug)
127 printf("rarpsend: called\n");
128 #endif
129
130 return (sendether(d, pkt, len, bcea, ETHERTYPE_REVARP));
131 }
132
133 /*
134 * Called when packet containing RARP is received
135 */
136 static ssize_t
137 rarprecv(d, pkt, len, tleft)
138 register struct iodesc *d;
139 register void *pkt;
140 register size_t len;
141 time_t tleft;
142 {
143 register ssize_t n;
144 register struct ether_arp *ap;
145 u_int16_t etype; /* host order */
146
147 #ifdef RARP_DEBUG
148 if (debug)
149 printf("rarprecv: ");
150 #endif
151
152 n = readether(d, pkt, len, tleft, &etype);
153 if (n == -1 || n < sizeof(struct ether_arp)) {
154 #ifdef RARP_DEBUG
155 if (debug)
156 printf("bad len=%d\n", n);
157 #endif
158 goto bad;
159 }
160
161 if (etype != ETHERTYPE_REVARP) {
162 #ifdef RARP_DEBUG
163 if (debug)
164 printf("bad type=0x%x\n", etype);
165 #endif
166 goto bad;
167 }
168
169 /* Verify that this is the right packet. */
170 ap = (struct ether_arp *)pkt;
171
172 if (ap->arp_op != htons(ARPOP_REVREPLY)) {
173 #ifdef RARP_DEBUG
174 if (debug)
175 printf("bad op=0x%x\n", ntohs(ap->arp_op));
176 #endif
177 goto bad;
178 }
179
180 if (ap->arp_pro != htons(ETHERTYPE_IP)) {
181 #ifdef RARP_DEBUG
182 if (debug)
183 printf("bad procol=0x%x\n", ntohs(ap->arp_pro));
184 #endif
185
186 goto bad;
187 }
188
189 if (bcmp(ap->arp_tha, d->myea, 6)) {
190 #ifdef RARP_DEBUG
191 if (debug)
192 printf("wrong ether address\n");
193 #endif
194 goto bad;
195 }
196
197 bcopy(ap->arp_tpa, (char *)&myip, sizeof(myip));
198 bcopy(ap->arp_spa, (char *)&rootip, sizeof(rootip));
199
200 d->myip = myip;
201 #ifdef RARP_DEBUG
202 if (debug)
203 printf("myip=0x%x\n", myip);
204 #endif
205
206 /* XXX - Compute our "natural" netmask? */
207 return (n);
208
209 bad:
210 errno = 0;
211 return (-1);
212 }
213