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