1199767f8SToomas Soome /* $NetBSD: rarp.c,v 1.16 1997/07/07 15:52:52 drochner Exp $ */
2199767f8SToomas Soome
3199767f8SToomas Soome /*
4199767f8SToomas Soome * Copyright (c) 1992 Regents of the University of California.
5199767f8SToomas Soome * All rights reserved.
6199767f8SToomas Soome *
7199767f8SToomas Soome * This software was developed by the Computer Systems Engineering group
8199767f8SToomas Soome * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9199767f8SToomas Soome * contributed to Berkeley.
10199767f8SToomas Soome *
11199767f8SToomas Soome * Redistribution and use in source and binary forms, with or without
12199767f8SToomas Soome * modification, are permitted provided that the following conditions
13199767f8SToomas Soome * are met:
14199767f8SToomas Soome * 1. Redistributions of source code must retain the above copyright
15199767f8SToomas Soome * notice, this list of conditions and the following disclaimer.
16199767f8SToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
17199767f8SToomas Soome * notice, this list of conditions and the following disclaimer in the
18199767f8SToomas Soome * documentation and/or other materials provided with the distribution.
191974da4bSToomas Soome * 3. Neither the name of the University nor the names of its contributors
20199767f8SToomas Soome * may be used to endorse or promote products derived from this software
21199767f8SToomas Soome * without specific prior written permission.
22199767f8SToomas Soome *
23199767f8SToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24199767f8SToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25199767f8SToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26199767f8SToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27199767f8SToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28199767f8SToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29199767f8SToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30199767f8SToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31199767f8SToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32199767f8SToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33199767f8SToomas Soome * SUCH DAMAGE.
34199767f8SToomas Soome *
35199767f8SToomas Soome * @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp (LBL)
36199767f8SToomas Soome */
37199767f8SToomas Soome
38199767f8SToomas Soome #include <sys/cdefs.h>
39199767f8SToomas Soome
40199767f8SToomas Soome #include <sys/param.h>
41199767f8SToomas Soome #include <sys/socket.h>
42199767f8SToomas Soome #include <net/if.h>
43199767f8SToomas Soome #include <netinet/in.h>
44199767f8SToomas Soome #include <netinet/if_ether.h>
45199767f8SToomas Soome
46199767f8SToomas Soome #include <netinet/in_systm.h>
47199767f8SToomas Soome
48199767f8SToomas Soome #include <string.h>
49199767f8SToomas Soome
50199767f8SToomas Soome #include "stand.h"
51199767f8SToomas Soome #include "net.h"
52199767f8SToomas Soome #include "netif.h"
53199767f8SToomas Soome
54199767f8SToomas Soome
55199767f8SToomas Soome static ssize_t rarpsend(struct iodesc *, void *, size_t);
56890c8671SToomas Soome static ssize_t rarprecv(struct iodesc *, void **, void **, time_t, void *);
57199767f8SToomas Soome
58199767f8SToomas Soome /*
59199767f8SToomas Soome * Ethernet (Reverse) Address Resolution Protocol (see RFC 903, and 826).
60199767f8SToomas Soome */
61199767f8SToomas Soome int
rarp_getipaddress(int sock)62859472daSToomas Soome rarp_getipaddress(int sock)
63199767f8SToomas Soome {
64199767f8SToomas Soome struct iodesc *d;
65199767f8SToomas Soome struct ether_arp *ap;
66859472daSToomas Soome void *pkt;
67199767f8SToomas Soome struct {
68199767f8SToomas Soome u_char header[ETHER_SIZE];
69199767f8SToomas Soome struct {
70199767f8SToomas Soome struct ether_arp arp;
71199767f8SToomas Soome u_char pad[18]; /* 60 - sizeof(arp) */
72199767f8SToomas Soome } data;
73199767f8SToomas Soome } wbuf;
74199767f8SToomas Soome
75199767f8SToomas Soome #ifdef RARP_DEBUG
76199767f8SToomas Soome if (debug)
77199767f8SToomas Soome printf("rarp: socket=%d\n", sock);
78199767f8SToomas Soome #endif
79199767f8SToomas Soome if (!(d = socktodesc(sock))) {
80199767f8SToomas Soome printf("rarp: bad socket. %d\n", sock);
81199767f8SToomas Soome return (-1);
82199767f8SToomas Soome }
83199767f8SToomas Soome #ifdef RARP_DEBUG
84199767f8SToomas Soome if (debug)
85199767f8SToomas Soome printf("rarp: d=%x\n", (u_int)d);
86199767f8SToomas Soome #endif
87199767f8SToomas Soome
88199767f8SToomas Soome bzero((char*)&wbuf.data, sizeof(wbuf.data));
89199767f8SToomas Soome ap = &wbuf.data.arp;
90199767f8SToomas Soome ap->arp_hrd = htons(ARPHRD_ETHER);
91199767f8SToomas Soome ap->arp_pro = htons(ETHERTYPE_IP);
92199767f8SToomas Soome ap->arp_hln = sizeof(ap->arp_sha); /* hardware address length */
93199767f8SToomas Soome ap->arp_pln = sizeof(ap->arp_spa); /* protocol address length */
94199767f8SToomas Soome ap->arp_op = htons(ARPOP_REVREQUEST);
95199767f8SToomas Soome bcopy(d->myea, ap->arp_sha, 6);
96199767f8SToomas Soome bcopy(d->myea, ap->arp_tha, 6);
97859472daSToomas Soome pkt = NULL;
98199767f8SToomas Soome
99199767f8SToomas Soome if (sendrecv(d,
100199767f8SToomas Soome rarpsend, &wbuf.data, sizeof(wbuf.data),
101890c8671SToomas Soome rarprecv, &pkt, (void *)&ap, NULL) < 0) {
102199767f8SToomas Soome printf("No response for RARP request\n");
103199767f8SToomas Soome return (-1);
104199767f8SToomas Soome }
105199767f8SToomas Soome
106199767f8SToomas Soome bcopy(ap->arp_tpa, (char *)&myip, sizeof(myip));
107199767f8SToomas Soome #if 0
108199767f8SToomas Soome /* XXX - Can NOT assume this is our root server! */
109199767f8SToomas Soome bcopy(ap->arp_spa, (char *)&rootip, sizeof(rootip));
110199767f8SToomas Soome #endif
111859472daSToomas Soome free(pkt);
112199767f8SToomas Soome
113199767f8SToomas Soome /* Compute our "natural" netmask. */
114199767f8SToomas Soome if (IN_CLASSA(myip.s_addr))
115199767f8SToomas Soome netmask = IN_CLASSA_NET;
116199767f8SToomas Soome else if (IN_CLASSB(myip.s_addr))
117199767f8SToomas Soome netmask = IN_CLASSB_NET;
118199767f8SToomas Soome else
119199767f8SToomas Soome netmask = IN_CLASSC_NET;
120199767f8SToomas Soome
121199767f8SToomas Soome d->myip = myip;
122199767f8SToomas Soome return (0);
123199767f8SToomas Soome }
124199767f8SToomas Soome
125199767f8SToomas Soome /*
126199767f8SToomas Soome * Broadcast a RARP request (i.e. who knows who I am)
127199767f8SToomas Soome */
128199767f8SToomas Soome static ssize_t
rarpsend(struct iodesc * d,void * pkt,size_t len)129859472daSToomas Soome rarpsend(struct iodesc *d, void *pkt, size_t len)
130199767f8SToomas Soome {
131199767f8SToomas Soome
132199767f8SToomas Soome #ifdef RARP_DEBUG
133199767f8SToomas Soome if (debug)
134199767f8SToomas Soome printf("rarpsend: called\n");
135199767f8SToomas Soome #endif
136199767f8SToomas Soome
137199767f8SToomas Soome return (sendether(d, pkt, len, bcea, ETHERTYPE_REVARP));
138199767f8SToomas Soome }
139199767f8SToomas Soome
140199767f8SToomas Soome /*
141199767f8SToomas Soome * Returns 0 if this is the packet we're waiting for
142199767f8SToomas Soome * else -1 (and errno == 0)
143199767f8SToomas Soome */
144199767f8SToomas Soome static ssize_t
rarprecv(struct iodesc * d,void ** pkt,void ** payload,time_t tleft,void * extra __unused)145890c8671SToomas Soome rarprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft,
1468eef2ab6SToomas Soome void *extra __unused)
147199767f8SToomas Soome {
148199767f8SToomas Soome ssize_t n;
149199767f8SToomas Soome struct ether_arp *ap;
150859472daSToomas Soome void *ptr = NULL;
151859472daSToomas Soome uint16_t etype; /* host order */
152199767f8SToomas Soome
153199767f8SToomas Soome #ifdef RARP_DEBUG
154199767f8SToomas Soome if (debug)
155199767f8SToomas Soome printf("rarprecv: ");
156199767f8SToomas Soome #endif
157199767f8SToomas Soome
158859472daSToomas Soome n = readether(d, &ptr, (void **)&ap, tleft, &etype);
159199767f8SToomas Soome errno = 0; /* XXX */
160199767f8SToomas Soome if (n == -1 || n < sizeof(struct ether_arp)) {
161199767f8SToomas Soome #ifdef RARP_DEBUG
162199767f8SToomas Soome if (debug)
163199767f8SToomas Soome printf("bad len=%d\n", n);
164199767f8SToomas Soome #endif
165859472daSToomas Soome free(ptr);
166199767f8SToomas Soome return (-1);
167199767f8SToomas Soome }
168199767f8SToomas Soome
169199767f8SToomas Soome if (etype != ETHERTYPE_REVARP) {
170199767f8SToomas Soome #ifdef RARP_DEBUG
171199767f8SToomas Soome if (debug)
172199767f8SToomas Soome printf("bad type=0x%x\n", etype);
173199767f8SToomas Soome #endif
174859472daSToomas Soome free(ptr);
175199767f8SToomas Soome return (-1);
176199767f8SToomas Soome }
177199767f8SToomas Soome
178199767f8SToomas Soome if (ap->arp_hrd != htons(ARPHRD_ETHER) ||
179199767f8SToomas Soome ap->arp_pro != htons(ETHERTYPE_IP) ||
180199767f8SToomas Soome ap->arp_hln != sizeof(ap->arp_sha) ||
181199767f8SToomas Soome ap->arp_pln != sizeof(ap->arp_spa) )
182199767f8SToomas Soome {
183199767f8SToomas Soome #ifdef RARP_DEBUG
184199767f8SToomas Soome if (debug)
185199767f8SToomas Soome printf("bad hrd/pro/hln/pln\n");
186199767f8SToomas Soome #endif
187859472daSToomas Soome free(ptr);
188199767f8SToomas Soome return (-1);
189199767f8SToomas Soome }
190199767f8SToomas Soome
191199767f8SToomas Soome if (ap->arp_op != htons(ARPOP_REVREPLY)) {
192199767f8SToomas Soome #ifdef RARP_DEBUG
193199767f8SToomas Soome if (debug)
194199767f8SToomas Soome printf("bad op=0x%x\n", ntohs(ap->arp_op));
195199767f8SToomas Soome #endif
196859472daSToomas Soome free(ptr);
197199767f8SToomas Soome return (-1);
198199767f8SToomas Soome }
199199767f8SToomas Soome
200199767f8SToomas Soome /* Is the reply for our Ethernet address? */
201199767f8SToomas Soome if (bcmp(ap->arp_tha, d->myea, 6)) {
202199767f8SToomas Soome #ifdef RARP_DEBUG
203199767f8SToomas Soome if (debug)
204199767f8SToomas Soome printf("unwanted address\n");
205199767f8SToomas Soome #endif
206859472daSToomas Soome free(ptr);
207199767f8SToomas Soome return (-1);
208199767f8SToomas Soome }
209199767f8SToomas Soome
210199767f8SToomas Soome /* We have our answer. */
211199767f8SToomas Soome #ifdef RARP_DEBUG
212199767f8SToomas Soome if (debug)
213199767f8SToomas Soome printf("got it\n");
214199767f8SToomas Soome #endif
215859472daSToomas Soome *pkt = ptr;
216859472daSToomas Soome *payload = ap;
217199767f8SToomas Soome return (n);
218199767f8SToomas Soome }
219