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