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