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