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