Home | History | Annotate | Line # | Download | only in libsa
rarp.c revision 1.5
      1 /*	$NetBSD: rarp.c,v 1.5 1995/02/20 11:04:16 mycroft 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 size_t rarpsend __P((struct iodesc *, void *, size_t));
     56 static size_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[HEADER_SIZE];
     69 		struct ether_arp wrarp;
     70 	} wbuf;
     71 	struct {
     72 		u_char header[HEADER_SIZE];
     73 		struct ether_arp rrarp;
     74 	} rbuf;
     75 
     76 #ifdef RARP_DEBUG
     77  	if (debug)
     78 		printf("rarp: socket=%d\n", sock);
     79 #endif
     80 	if (!(d = socktodesc(sock))) {
     81 		printf("rarp: bad socket. %d\n", sock);
     82 		return(INADDR_ANY);
     83 	}
     84 #ifdef RARP_DEBUG
     85  	if (debug)
     86 		printf("rarp: d=%x\n", (u_int)d);
     87 #endif
     88 
     89 	ap = &wbuf.wrarp;
     90 	bzero(ap, sizeof(*ap));
     91 
     92 	ap->arp_hrd = htons(ARPHRD_ETHER);
     93 	ap->arp_pro = htons(ETHERTYPE_IP);
     94 	ap->arp_hln = sizeof(ap->arp_sha); /* hardware address length */
     95 	ap->arp_pln = sizeof(ap->arp_spa); /* protocol address length */
     96 	ap->arp_op = htons(ARPOP_REQUEST);
     97 	bcopy(d->myea, ap->arp_sha, 6);
     98 	bcopy(d->myea, ap->arp_tha, 6);
     99 
    100 	if (sendrecv(d,
    101 	    rarpsend, ap, sizeof(*ap),
    102 	    rarprecv, &rbuf.rrarp, sizeof(rbuf.rrarp)) < 0) {
    103 		printf("No response for RARP request\n");
    104 		return(INADDR_ANY);
    105 	}
    106 
    107 	return (myip);
    108 }
    109 
    110 /*
    111  * Broadcast a RARP request (i.e. who knows who I am)
    112  */
    113 static size_t
    114 rarpsend(d, pkt, len)
    115 	register struct iodesc *d;
    116 	register void *pkt;
    117 	register size_t len;
    118 {
    119 
    120 #ifdef RARP_DEBUG
    121  	if (debug)
    122 		printf("rarpsend: called\n");
    123 #endif
    124 
    125 	return (sendether(d, pkt, len, bcea, ETHERTYPE_REVARP));
    126 }
    127 
    128 /*
    129  * Called when packet containing RARP is received
    130  */
    131 static size_t
    132 rarprecv(d, pkt, len, tleft)
    133 	register struct iodesc *d;
    134 	register void *pkt;
    135 	register size_t len;
    136 	time_t tleft;
    137 {
    138 	register struct ether_header *eh;
    139 	register struct ether_arp *ap;
    140 
    141 #ifdef RARP_DEBUG
    142  	if (debug)
    143  		printf("rarprecv: called\n");
    144 #endif
    145 
    146 	len = readether(d, pkt, len, tleft);
    147 	if (len == -1 || len < sizeof(struct ether_arp))
    148 		goto bad;
    149 
    150 	eh = (struct ether_header *)pkt - 1;
    151 	if (ntohs(eh->ether_type) != ETHERTYPE_REVARP)
    152 		goto bad;
    153 
    154 	ap = (struct ether_arp *)pkt;
    155 	if (ntohs(ap->arp_op) != ARPOP_REPLY ||
    156 	    ntohs(ap->arp_pro) != ETHERTYPE_IP)
    157 		goto bad;
    158 
    159 	if (bcmp(ap->arp_tha, d->myea, 6))
    160 		goto bad;
    161 
    162 	bcopy(ap->arp_tpa, (char *)&myip, sizeof(myip));
    163 	bcopy(ap->arp_spa, (char *)&rootip, sizeof(rootip));
    164 
    165 	return (0);
    166 
    167 bad:
    168 	errno = 0;
    169 	return (-1);
    170 }
    171