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