Home | History | Annotate | Line # | Download | only in libsa
rarp.c revision 1.13.6.1
      1  1.13.6.1        is /*	$NetBSD: rarp.c,v 1.13.6.1 1997/03/07 21:36:10 is Exp $	*/
      2       1.4       cgd 
      3       1.3    brezak /*
      4       1.3    brezak  * Copyright (c) 1992 Regents of the University of California.
      5       1.3    brezak  * All rights reserved.
      6       1.3    brezak  *
      7       1.3    brezak  * This software was developed by the Computer Systems Engineering group
      8       1.3    brezak  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9       1.3    brezak  * contributed to Berkeley.
     10       1.3    brezak  *
     11       1.3    brezak  * Redistribution and use in source and binary forms, with or without
     12       1.3    brezak  * modification, are permitted provided that the following conditions
     13       1.3    brezak  * are met:
     14       1.3    brezak  * 1. Redistributions of source code must retain the above copyright
     15       1.3    brezak  *    notice, this list of conditions and the following disclaimer.
     16       1.3    brezak  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.3    brezak  *    notice, this list of conditions and the following disclaimer in the
     18       1.3    brezak  *    documentation and/or other materials provided with the distribution.
     19       1.3    brezak  * 3. All advertising materials mentioning features or use of this software
     20       1.3    brezak  *    must display the following acknowledgement:
     21       1.3    brezak  *	This product includes software developed by the University of
     22       1.3    brezak  *	California, Lawrence Berkeley Laboratory and its contributors.
     23       1.3    brezak  * 4. Neither the name of the University nor the names of its contributors
     24       1.3    brezak  *    may be used to endorse or promote products derived from this software
     25       1.3    brezak  *    without specific prior written permission.
     26       1.3    brezak  *
     27       1.3    brezak  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28       1.3    brezak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29       1.3    brezak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30       1.3    brezak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31       1.3    brezak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32       1.3    brezak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33       1.3    brezak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34       1.3    brezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35       1.3    brezak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36       1.3    brezak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37       1.3    brezak  * SUCH DAMAGE.
     38       1.3    brezak  *
     39       1.4       cgd  * @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp  (LBL)
     40       1.3    brezak  */
     41       1.1    brezak #include <sys/param.h>
     42       1.1    brezak #include <sys/socket.h>
     43       1.1    brezak #include <net/if.h>
     44       1.1    brezak #include <netinet/in.h>
     45       1.1    brezak 
     46       1.1    brezak #include <netinet/in_systm.h>
     47       1.1    brezak 
     48       1.1    brezak #include <string.h>
     49       1.1    brezak 
     50  1.13.6.1        is #include "if_ether.h"
     51       1.1    brezak #include "stand.h"
     52       1.1    brezak #include "net.h"
     53       1.1    brezak #include "netif.h"
     54       1.1    brezak 
     55       1.8        pk static ssize_t rarpsend __P((struct iodesc *, void *, size_t));
     56       1.8        pk static ssize_t rarprecv __P((struct iodesc *, void *, size_t, time_t));
     57       1.1    brezak 
     58       1.1    brezak /*
     59       1.1    brezak  * Ethernet (Reverse) Address Resolution Protocol (see RFC 903, and 826).
     60       1.1    brezak  */
     61       1.9        pk int
     62       1.1    brezak rarp_getipaddress(sock)
     63       1.1    brezak 	int sock;
     64       1.1    brezak {
     65       1.1    brezak 	struct iodesc *d;
     66       1.1    brezak 	register struct ether_arp *ap;
     67       1.1    brezak 	struct {
     68       1.6       gwr 		u_char header[ETHER_SIZE];
     69       1.6       gwr 		struct {
     70       1.6       gwr 			struct ether_arp arp;
     71       1.6       gwr 			u_char pad[18]; 	/* 60 - sizeof(arp) */
     72       1.6       gwr 		} data;
     73       1.1    brezak 	} wbuf;
     74       1.5   mycroft 	struct {
     75       1.6       gwr 		u_char header[ETHER_SIZE];
     76       1.6       gwr 		struct {
     77       1.6       gwr 			struct ether_arp arp;
     78       1.6       gwr 			u_char pad[24]; 	/* extra space */
     79       1.6       gwr 		} data;
     80       1.1    brezak 	} rbuf;
     81       1.1    brezak 
     82       1.1    brezak #ifdef RARP_DEBUG
     83       1.1    brezak  	if (debug)
     84      1.13  christos 		printf("rarp: socket=%d\n", sock);
     85       1.1    brezak #endif
     86       1.1    brezak 	if (!(d = socktodesc(sock))) {
     87      1.13  christos 		printf("rarp: bad socket. %d\n", sock);
     88       1.9        pk 		return (-1);
     89       1.1    brezak 	}
     90       1.1    brezak #ifdef RARP_DEBUG
     91       1.1    brezak  	if (debug)
     92      1.13  christos 		printf("rarp: d=%x\n", (u_int)d);
     93       1.1    brezak #endif
     94       1.5   mycroft 
     95       1.6       gwr 	bzero((char*)&wbuf.data, sizeof(wbuf.data));
     96       1.6       gwr 	ap = &wbuf.data.arp;
     97       1.1    brezak 	ap->arp_hrd = htons(ARPHRD_ETHER);
     98       1.1    brezak 	ap->arp_pro = htons(ETHERTYPE_IP);
     99       1.1    brezak 	ap->arp_hln = sizeof(ap->arp_sha); /* hardware address length */
    100       1.1    brezak 	ap->arp_pln = sizeof(ap->arp_spa); /* protocol address length */
    101       1.6       gwr 	ap->arp_op = htons(ARPOP_REVREQUEST);
    102       1.1    brezak 	bcopy(d->myea, ap->arp_sha, 6);
    103       1.1    brezak 	bcopy(d->myea, ap->arp_tha, 6);
    104       1.1    brezak 
    105       1.1    brezak 	if (sendrecv(d,
    106       1.6       gwr 	    rarpsend, &wbuf.data, sizeof(wbuf.data),
    107      1.10       gwr 	    rarprecv, &rbuf.data, sizeof(rbuf.data)) < 0)
    108      1.10       gwr 	{
    109      1.13  christos 		printf("No response for RARP request\n");
    110       1.9        pk 		return (-1);
    111       1.1    brezak 	}
    112       1.1    brezak 
    113      1.10       gwr 	ap = &rbuf.data.arp;
    114      1.10       gwr 	bcopy(ap->arp_tpa, (char *)&myip, sizeof(myip));
    115      1.10       gwr #if 0
    116      1.10       gwr 	/* XXX - Can NOT assume this is our root server! */
    117      1.10       gwr 	bcopy(ap->arp_spa, (char *)&rootip, sizeof(rootip));
    118      1.10       gwr #endif
    119      1.10       gwr 
    120      1.10       gwr 	/* Compute our "natural" netmask. */
    121      1.10       gwr 	if (IN_CLASSA(myip.s_addr))
    122      1.10       gwr 		netmask = IN_CLASSA_NET;
    123      1.10       gwr 	else if (IN_CLASSB(myip.s_addr))
    124      1.10       gwr 		netmask = IN_CLASSB_NET;
    125      1.10       gwr 	else
    126      1.10       gwr 		netmask = IN_CLASSC_NET;
    127      1.10       gwr 
    128      1.10       gwr 	d->myip = myip;
    129       1.9        pk 	return (0);
    130       1.1    brezak }
    131       1.1    brezak 
    132       1.1    brezak /*
    133       1.1    brezak  * Broadcast a RARP request (i.e. who knows who I am)
    134       1.1    brezak  */
    135       1.8        pk static ssize_t
    136       1.1    brezak rarpsend(d, pkt, len)
    137       1.1    brezak 	register struct iodesc *d;
    138       1.1    brezak 	register void *pkt;
    139       1.5   mycroft 	register size_t len;
    140       1.1    brezak {
    141       1.5   mycroft 
    142       1.1    brezak #ifdef RARP_DEBUG
    143       1.1    brezak  	if (debug)
    144      1.13  christos 		printf("rarpsend: called\n");
    145       1.1    brezak #endif
    146       1.5   mycroft 
    147       1.1    brezak 	return (sendether(d, pkt, len, bcea, ETHERTYPE_REVARP));
    148       1.1    brezak }
    149       1.1    brezak 
    150       1.1    brezak /*
    151      1.10       gwr  * Returns 0 if this is the packet we're waiting for
    152      1.10       gwr  * else -1 (and errno == 0)
    153       1.1    brezak  */
    154       1.8        pk static ssize_t
    155       1.5   mycroft rarprecv(d, pkt, len, tleft)
    156       1.1    brezak 	register struct iodesc *d;
    157       1.1    brezak 	register void *pkt;
    158       1.5   mycroft 	register size_t len;
    159       1.5   mycroft 	time_t tleft;
    160       1.1    brezak {
    161       1.8        pk 	register ssize_t n;
    162       1.1    brezak 	register struct ether_arp *ap;
    163       1.7   thorpej 	u_int16_t etype;	/* host order */
    164       1.1    brezak 
    165       1.1    brezak #ifdef RARP_DEBUG
    166       1.1    brezak  	if (debug)
    167      1.13  christos 		printf("rarprecv: ");
    168       1.1    brezak #endif
    169       1.1    brezak 
    170       1.8        pk 	n = readether(d, pkt, len, tleft, &etype);
    171      1.10       gwr 	errno = 0;	/* XXX */
    172       1.8        pk 	if (n == -1 || n < sizeof(struct ether_arp)) {
    173       1.6       gwr #ifdef RARP_DEBUG
    174       1.6       gwr 		if (debug)
    175      1.13  christos 			printf("bad len=%d\n", n);
    176       1.6       gwr #endif
    177      1.10       gwr 		return (-1);
    178       1.6       gwr 	}
    179       1.5   mycroft 
    180       1.7   thorpej 	if (etype != ETHERTYPE_REVARP) {
    181       1.6       gwr #ifdef RARP_DEBUG
    182       1.6       gwr 		if (debug)
    183      1.13  christos 			printf("bad type=0x%x\n", etype);
    184       1.6       gwr #endif
    185      1.10       gwr 		return (-1);
    186       1.6       gwr 	}
    187       1.1    brezak 
    188       1.5   mycroft 	ap = (struct ether_arp *)pkt;
    189      1.10       gwr 	if (ap->arp_hrd != htons(ARPHRD_ETHER) ||
    190      1.10       gwr 	    ap->arp_pro != htons(ETHERTYPE_IP) ||
    191      1.10       gwr 	    ap->arp_hln != sizeof(ap->arp_sha) ||
    192      1.10       gwr 	    ap->arp_pln != sizeof(ap->arp_spa) )
    193      1.10       gwr 	{
    194       1.6       gwr #ifdef RARP_DEBUG
    195       1.6       gwr 		if (debug)
    196      1.13  christos 			printf("bad hrd/pro/hln/pln\n");
    197       1.6       gwr #endif
    198      1.10       gwr 		return (-1);
    199       1.6       gwr 	}
    200       1.6       gwr 
    201      1.10       gwr 	if (ap->arp_op != htons(ARPOP_REVREPLY)) {
    202       1.6       gwr #ifdef RARP_DEBUG
    203       1.6       gwr 		if (debug)
    204      1.13  christos 			printf("bad op=0x%x\n", ntohs(ap->arp_op));
    205       1.6       gwr #endif
    206      1.10       gwr 		return (-1);
    207       1.6       gwr 	}
    208       1.6       gwr 
    209      1.10       gwr 	/* Is the reply for our Ethernet address? */
    210       1.6       gwr 	if (bcmp(ap->arp_tha, d->myea, 6)) {
    211       1.6       gwr #ifdef RARP_DEBUG
    212       1.6       gwr 		if (debug)
    213      1.13  christos 			printf("unwanted address\n");
    214       1.6       gwr #endif
    215      1.10       gwr 		return (-1);
    216       1.6       gwr 	}
    217       1.1    brezak 
    218      1.10       gwr 	/* We have our answer. */
    219       1.6       gwr #ifdef RARP_DEBUG
    220      1.10       gwr  	if (debug)
    221      1.13  christos 		printf("got it\n");
    222       1.6       gwr #endif
    223       1.8        pk 	return (n);
    224       1.1    brezak }
    225