1 /* $OpenBSD: bootp.c,v 1.15 2014/11/19 19:58:40 miod Exp $ */ 2 /* $NetBSD: bootp.c,v 1.10 1996/10/13 02:28:59 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: bootp.c,v 1.4 93/09/11 03:13:51 leres Exp (LBL) 41 */ 42 43 #include <sys/types.h> 44 #include <sys/socket.h> 45 #include <netinet/in.h> 46 47 #include "stand.h" 48 #include "net.h" 49 #include "netif.h" 50 #include "bootp.h" 51 52 static u_int32_t nmask, smask; 53 54 static time_t bot; 55 56 static char vm_rfc1048[4] = VM_RFC1048; 57 static char vm_cmu[4] = VM_CMU; 58 59 /* Local forwards */ 60 static ssize_t bootpsend(struct iodesc *, void *, size_t); 61 static ssize_t bootprecv(struct iodesc *, void *, size_t, time_t); 62 static void vend_cmu(const u_char *); 63 static void vend_rfc1048(const u_char *, u_int); 64 65 /* Fetch required bootp information */ 66 void 67 bootp(int sock) 68 { 69 struct iodesc *d; 70 struct bootp *bp; 71 struct { 72 struct packet_header header; 73 struct bootp wbootp; 74 } wbuf; 75 struct { 76 struct packet_header header; 77 struct bootp rbootp; 78 } rbuf; 79 80 #ifdef BOOTP_DEBUG 81 if (debug) 82 printf("bootp: socket=%d\n", sock); 83 #endif 84 if (!bot) 85 bot = getsecs(); 86 87 if (!(d = socktodesc(sock))) { 88 printf("bootp: bad socket. %d\n", sock); 89 return; 90 } 91 #ifdef BOOTP_DEBUG 92 if (debug) 93 printf("bootp: d=%x\n", (u_int)d); 94 #endif 95 96 bp = &wbuf.wbootp; 97 bzero(bp, sizeof(*bp)); 98 99 bp->bp_op = BOOTREQUEST; 100 bp->bp_htype = HTYPE_ETHERNET; /* 10Mb Ethernet (48 bits) */ 101 bp->bp_hlen = 6; 102 bp->bp_xid = htonl(d->xid); 103 MACPY(d->myea, bp->bp_chaddr); 104 bzero(bp->bp_file, sizeof(bp->bp_file)); 105 bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)); 106 107 d->myip = myip; 108 d->myport = htons(IPPORT_BOOTPC); 109 d->destip.s_addr = INADDR_BROADCAST; 110 d->destport = htons(IPPORT_BOOTPS); 111 112 (void)sendrecv(d, 113 bootpsend, bp, sizeof(*bp), 114 bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp)); 115 116 /* Bump xid so next request will be unique. */ 117 ++d->xid; 118 } 119 120 /* Transmit a bootp request */ 121 static ssize_t 122 bootpsend(struct iodesc *d, void *pkt, size_t len) 123 { 124 struct bootp *bp; 125 126 #ifdef BOOTP_DEBUG 127 if (debug) 128 printf("bootpsend: d=%x called.\n", (u_int)d); 129 #endif 130 131 bp = pkt; 132 bp->bp_secs = htons((u_short)(getsecs() - bot)); 133 134 #ifdef BOOTP_DEBUG 135 if (debug) 136 printf("bootpsend: calling sendudp\n"); 137 #endif 138 139 return (sendudp(d, pkt, len)); 140 } 141 142 /* Returns 0 if this is the packet we're waiting for else -1 (and errno == 0) */ 143 static ssize_t 144 bootprecv(struct iodesc *d, void *pkt, size_t len, time_t tleft) 145 { 146 ssize_t n; 147 struct bootp *bp; 148 149 #ifdef BOOTP_DEBUG 150 if (debug) 151 printf("bootprecv: called\n"); 152 #endif 153 154 n = readudp(d, pkt, len, tleft); 155 if (n < 0 || (size_t)n < sizeof(struct bootp)) 156 goto bad; 157 158 bp = (struct bootp *)pkt; 159 160 #ifdef BOOTP_DEBUG 161 if (debug) 162 printf("bootprecv: checked. bp = 0x%x, n = %d\n", 163 (unsigned)bp, n); 164 #endif 165 if (bp->bp_xid != htonl(d->xid)) { 166 #ifdef BOOTP_DEBUG 167 if (debug) { 168 printf("bootprecv: expected xid 0x%lx, got 0x%lx\n", 169 d->xid, ntohl(bp->bp_xid)); 170 } 171 #endif 172 goto bad; 173 } 174 175 #ifdef BOOTP_DEBUG 176 if (debug) 177 printf("bootprecv: got one!\n"); 178 #endif 179 180 /* Pick up our ip address (and natural netmask) */ 181 myip = d->myip = bp->bp_yiaddr; 182 #ifdef BOOTP_DEBUG 183 if (debug) 184 printf("our ip address is %s\n", inet_ntoa(d->myip)); 185 #endif 186 if (IN_CLASSA(d->myip.s_addr)) 187 nmask = IN_CLASSA_NET; 188 else if (IN_CLASSB(d->myip.s_addr)) 189 nmask = IN_CLASSB_NET; 190 else 191 nmask = IN_CLASSC_NET; 192 #ifdef BOOTP_DEBUG 193 if (debug) 194 printf("'native netmask' is %s\n", intoa(nmask)); 195 #endif 196 197 /* Pick up root or swap server address and file spec. */ 198 if (bp->bp_siaddr.s_addr != 0) 199 rootip = bp->bp_siaddr; 200 if (bp->bp_file[0] != '\0') { 201 strncpy(bootfile, (char *)bp->bp_file, sizeof(bootfile)); 202 bootfile[sizeof(bootfile) - 1] = '\0'; 203 } 204 205 /* Suck out vendor info */ 206 if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0) 207 vend_cmu(bp->bp_vend); 208 else if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) 209 vend_rfc1048(bp->bp_vend, sizeof(bp->bp_vend)); 210 else 211 printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend); 212 213 /* Check subnet mask against net mask; toss if bogus */ 214 if ((nmask & smask) != nmask) { 215 #ifdef BOOTP_DEBUG 216 if (debug) 217 printf("subnet mask (%s) bad\n", intoa(smask)); 218 #endif 219 smask = 0; 220 } 221 222 /* Get subnet (or natural net) mask */ 223 netmask = nmask; 224 if (smask) 225 netmask = smask; 226 #ifdef BOOTP_DEBUG 227 if (debug) 228 printf("mask: %s\n", intoa(netmask)); 229 #endif 230 231 /* We need a gateway if root or swap is on a different net */ 232 if (!SAMENET(d->myip, rootip, netmask)) { 233 #ifdef BOOTP_DEBUG 234 if (debug) 235 printf("need gateway for root ip\n"); 236 #endif 237 } 238 239 if (!SAMENET(d->myip, swapip, netmask)) { 240 #ifdef BOOTP_DEBUG 241 if (debug) 242 printf("need gateway for swap ip\n"); 243 #endif 244 } 245 246 /* Toss gateway if on a different net */ 247 if (!SAMENET(d->myip, gateip, netmask)) { 248 #ifdef BOOTP_DEBUG 249 if (debug) 250 printf("gateway ip (%s) bad\n", inet_ntoa(gateip)); 251 #endif 252 gateip.s_addr = 0; 253 } 254 255 return (n); 256 257 bad: 258 errno = 0; 259 return (-1); 260 } 261 262 static void 263 vend_cmu(const u_char *cp) 264 { 265 const struct cmu_vend *vp; 266 267 #ifdef BOOTP_DEBUG 268 if (debug) 269 printf("vend_cmu bootp info.\n"); 270 #endif 271 vp = (const struct cmu_vend *)cp; 272 273 if (vp->v_smask.s_addr != 0) 274 smask = vp->v_smask.s_addr; 275 if (vp->v_dgate.s_addr != 0) 276 gateip = vp->v_dgate; 277 } 278 279 static void 280 vend_rfc1048(const u_char *cp, u_int len) 281 { 282 const u_char *ep; 283 int size; 284 u_char tag; 285 286 #ifdef BOOTP_DEBUG 287 if (debug) 288 printf("vend_rfc1048 bootp info. len=%d\n", len); 289 #endif 290 ep = cp + len; 291 292 /* Step over magic cookie */ 293 cp += sizeof(int); 294 295 while (cp < ep) { 296 tag = *cp++; 297 size = *cp++; 298 if (tag == TAG_END) 299 break; 300 301 if (tag == TAG_SUBNET_MASK) 302 bcopy(cp, &smask, sizeof(smask)); 303 if (tag == TAG_GATEWAY) 304 bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr)); 305 if (tag == TAG_SWAPSERVER) 306 bcopy(cp, &swapip.s_addr, sizeof(swapip.s_addr)); 307 if (tag == TAG_DOMAIN_SERVER) 308 bcopy(cp, &nameip.s_addr, sizeof(nameip.s_addr)); 309 if (tag == TAG_ROOTPATH) { 310 strncpy(rootpath, (char *)cp, sizeof(rootpath)); 311 rootpath[size] = '\0'; 312 } 313 if (tag == TAG_HOSTNAME) { 314 strncpy(hostname, (char *)cp, sizeof(hostname)); 315 hostname[size] = '\0'; 316 } 317 if (tag == TAG_DOMAINNAME) { 318 strncpy(domainname, (char *)cp, sizeof(domainname)); 319 domainname[size] = '\0'; 320 } 321 cp += size; 322 } 323 } 324