1 /* $NetBSD: rarp.c,v 1.16 1997/07/07 15:52:52 drochner 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. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #include <sys/param.h> 38 #include <sys/socket.h> 39 #include <net/if.h> 40 #include <netinet/in.h> 41 #include <netinet/if_ether.h> 42 43 #include <netinet/in_systm.h> 44 45 #include <string.h> 46 47 #include "stand.h" 48 #include "net.h" 49 #include "netif.h" 50 51 52 static ssize_t rarpsend(struct iodesc *, void *, size_t); 53 static ssize_t rarprecv(struct iodesc *, void **, void **, time_t, void *); 54 55 /* 56 * Ethernet (Reverse) Address Resolution Protocol (see RFC 903, and 826). 57 */ 58 int 59 rarp_getipaddress(int sock) 60 { 61 struct iodesc *d; 62 struct ether_arp *ap; 63 void *pkt; 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 72 #ifdef RARP_DEBUG 73 if (debug) 74 printf("rarp: socket=%d\n", sock); 75 #endif 76 if (!(d = socktodesc(sock))) { 77 printf("rarp: bad socket. %d\n", sock); 78 return (-1); 79 } 80 #ifdef RARP_DEBUG 81 if (debug) 82 printf("rarp: d=%lx\n", (long)d); 83 #endif 84 85 bzero((char*)&wbuf.data, sizeof(wbuf.data)); 86 ap = &wbuf.data.arp; 87 ap->arp_hrd = htons(ARPHRD_ETHER); 88 ap->arp_pro = htons(ETHERTYPE_IP); 89 ap->arp_hln = sizeof(ap->arp_sha); /* hardware address length */ 90 ap->arp_pln = sizeof(ap->arp_spa); /* protocol address length */ 91 ap->arp_op = htons(ARPOP_REVREQUEST); 92 bcopy(d->myea, ap->arp_sha, 6); 93 bcopy(d->myea, ap->arp_tha, 6); 94 pkt = NULL; 95 96 if (sendrecv(d, 97 rarpsend, &wbuf.data, sizeof(wbuf.data), 98 rarprecv, &pkt, (void *)&ap, NULL) < 0) { 99 printf("No response for RARP request\n"); 100 return (-1); 101 } 102 103 bcopy(ap->arp_tpa, (char *)&myip, sizeof(myip)); 104 #if 0 105 /* XXX - Can NOT assume this is our root server! */ 106 bcopy(ap->arp_spa, (char *)&rootip, sizeof(rootip)); 107 #endif 108 free(pkt); 109 110 /* Compute our "natural" netmask. */ 111 if (IN_CLASSA(myip.s_addr)) 112 netmask = IN_CLASSA_NET; 113 else if (IN_CLASSB(myip.s_addr)) 114 netmask = IN_CLASSB_NET; 115 else 116 netmask = IN_CLASSC_NET; 117 118 d->myip = myip; 119 return (0); 120 } 121 122 /* 123 * Broadcast a RARP request (i.e. who knows who I am) 124 */ 125 static ssize_t 126 rarpsend(struct iodesc *d, void *pkt, size_t len) 127 { 128 129 #ifdef RARP_DEBUG 130 if (debug) 131 printf("rarpsend: called\n"); 132 #endif 133 134 return (sendether(d, pkt, len, bcea, ETHERTYPE_REVARP)); 135 } 136 137 /* 138 * Returns 0 if this is the packet we're waiting for 139 * else -1 (and errno == 0) 140 */ 141 static ssize_t 142 rarprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft, 143 void *extra) 144 { 145 ssize_t n; 146 struct ether_arp *ap; 147 void *ptr = NULL; 148 uint16_t etype; /* host order */ 149 150 #ifdef RARP_DEBUG 151 if (debug) 152 printf("rarprecv: "); 153 #endif 154 155 n = readether(d, &ptr, (void **)&ap, tleft, &etype); 156 errno = 0; /* XXX */ 157 if (n == -1 || n < sizeof(struct ether_arp)) { 158 #ifdef RARP_DEBUG 159 if (debug) 160 printf("bad len=%zd\n", n); 161 #endif 162 free(ptr); 163 return (-1); 164 } 165 166 if (etype != ETHERTYPE_REVARP) { 167 #ifdef RARP_DEBUG 168 if (debug) 169 printf("bad type=0x%x\n", etype); 170 #endif 171 free(ptr); 172 return (-1); 173 } 174 175 if (ap->arp_hrd != htons(ARPHRD_ETHER) || 176 ap->arp_pro != htons(ETHERTYPE_IP) || 177 ap->arp_hln != sizeof(ap->arp_sha) || 178 ap->arp_pln != sizeof(ap->arp_spa) ) 179 { 180 #ifdef RARP_DEBUG 181 if (debug) 182 printf("bad hrd/pro/hln/pln\n"); 183 #endif 184 free(ptr); 185 return (-1); 186 } 187 188 if (ap->arp_op != htons(ARPOP_REVREPLY)) { 189 #ifdef RARP_DEBUG 190 if (debug) 191 printf("bad op=0x%x\n", ntohs(ap->arp_op)); 192 #endif 193 free(ptr); 194 return (-1); 195 } 196 197 /* Is the reply for our Ethernet address? */ 198 if (bcmp(ap->arp_tha, d->myea, 6)) { 199 #ifdef RARP_DEBUG 200 if (debug) 201 printf("unwanted address\n"); 202 #endif 203 free(ptr); 204 return (-1); 205 } 206 207 /* We have our answer. */ 208 #ifdef RARP_DEBUG 209 if (debug) 210 printf("got it\n"); 211 #endif 212 *pkt = ptr; 213 *payload = ap; 214 return (n); 215 } 216