1 /* $OpenBSD: rmpproto.c,v 1.6 2001/01/17 00:27:21 pjanzen Exp $ */ 2 /* $NetBSD: rmpproto.c,v 1.5.2.1 1995/11/14 08:45:44 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1988, 1992 The University of Utah and the Center 6 * for Software Science (CSS). 7 * Copyright (c) 1992, 1993 8 * The Regents of the University of California. All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * the Center for Software Science of the University of Utah Computer 12 * Science Department. CSS requests users of this software to return 13 * to css-dist@cs.utah.edu any improvements that they make and grant 14 * CSS redistribution rights. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. All advertising materials mentioning features or use of this software 25 * must display the following acknowledgement: 26 * This product includes software developed by the University of 27 * California, Berkeley and its contributors. 28 * 4. Neither the name of the University nor the names of its contributors 29 * may be used to endorse or promote products derived from this software 30 * without specific prior written permission. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42 * SUCH DAMAGE. 43 * 44 * from: @(#)rmpproto.c 8.1 (Berkeley) 6/4/93 45 * 46 * From: Utah Hdr: rmpproto.c 3.1 92/07/06 47 * Author: Jeff Forys, University of Utah CSS 48 */ 49 50 #ifndef lint 51 /*static char sccsid[] = "@(#)rmpproto.c 8.1 (Berkeley) 6/4/93";*/ 52 static char rcsid[] = "$OpenBSD: rmpproto.c,v 1.6 2001/01/17 00:27:21 pjanzen Exp $"; 53 #endif /* not lint */ 54 55 #include <sys/param.h> 56 #include <sys/time.h> 57 58 #include <errno.h> 59 #include <fcntl.h> 60 #include <stdio.h> 61 #include <string.h> 62 #include <syslog.h> 63 #include <unistd.h> 64 #include "defs.h" 65 66 /* 67 ** ProcessPacket -- determine packet type and do what's required. 68 ** 69 ** An RMP BOOT packet has been received. Look at the type field 70 ** and process Boot Requests, Read Requests, and Boot Complete 71 ** packets. Any other type will be dropped with a warning msg. 72 ** 73 ** Parameters: 74 ** rconn - the new connection 75 ** client - list of files available to this host 76 ** 77 ** Returns: 78 ** Nothing. 79 ** 80 ** Side Effects: 81 ** - If this is a valid boot request, it will be added to 82 ** the linked list of outstanding requests (RmpConns). 83 ** - If this is a valid boot complete, its associated 84 ** entry in RmpConns will be deleted. 85 ** - Also, unless we run out of memory, a reply will be 86 ** sent to the host that sent the packet. 87 */ 88 void 89 ProcessPacket(rconn, client) 90 RMPCONN *rconn; 91 CLIENT *client; 92 { 93 struct rmp_packet *rmp; 94 RMPCONN *rconnout; 95 96 rmp = &rconn->rmp; /* cache pointer to RMP packet */ 97 98 switch(rmp->r_type) { /* do what we came here to do */ 99 case RMP_BOOT_REQ: /* boot request */ 100 if ((rconnout = NewConn(rconn)) == NULL) 101 return; 102 103 /* 104 * If the Session ID is 0xffff, this is a "probe" 105 * packet and we do not want to add the connection 106 * to the linked list of active connections. There 107 * are two types of probe packets, if the Sequence 108 * Number is 0 they want to know our host name, o/w 109 * they want the name of the file associated with 110 * the number spec'd by the Sequence Number. 111 * 112 * If this is an actual boot request, open the file 113 * and send a reply. If SendBootRepl() does not 114 * return 0, add the connection to the linked list 115 * of active connections, otherwise delete it since 116 * an error was encountered. 117 */ 118 if (ntohs(rmp->r_brq.rmp_session) == RMP_PROBESID) { 119 if (WORDZE(rmp->r_brq.rmp_seqno)) 120 (void) SendServerID(rconnout); 121 else 122 (void) SendFileNo(rmp, rconnout, 123 client? client->files: 124 BootFiles); 125 FreeConn(rconnout); 126 } else { 127 if (SendBootRepl(rmp, rconnout, 128 client? client->files: BootFiles)) 129 AddConn(rconnout); 130 else 131 FreeConn(rconnout); 132 } 133 break; 134 135 case RMP_BOOT_REPL: /* boot reply (not valid) */ 136 syslog(LOG_WARNING, "%s: sent a boot reply", 137 EnetStr(rconn)); 138 break; 139 140 case RMP_READ_REQ: /* read request */ 141 /* 142 * Send a portion of the boot file. 143 */ 144 (void) SendReadRepl(rconn); 145 break; 146 147 case RMP_READ_REPL: /* read reply (not valid) */ 148 syslog(LOG_WARNING, "%s: sent a read reply", 149 EnetStr(rconn)); 150 break; 151 152 case RMP_BOOT_DONE: /* boot complete */ 153 /* 154 * Remove the entry from the linked list of active 155 * connections. 156 */ 157 (void) BootDone(rconn); 158 break; 159 160 default: /* unknown RMP packet type */ 161 syslog(LOG_WARNING, "%s: unknown packet type (%u)", 162 EnetStr(rconn), rmp->r_type); 163 } 164 } 165 166 /* 167 ** SendServerID -- send our host name to who ever requested it. 168 ** 169 ** Parameters: 170 ** rconn - the reply packet to be formatted. 171 ** 172 ** Returns: 173 ** 1 on success, 0 on failure. 174 ** 175 ** Side Effects: 176 ** none. 177 */ 178 int 179 SendServerID(rconn) 180 RMPCONN *rconn; 181 { 182 register struct rmp_packet *rpl; 183 register char *src, *dst; 184 register u_int8_t *size; 185 186 rpl = &rconn->rmp; /* cache ptr to RMP packet */ 187 188 /* 189 * Set up assorted fields in reply packet. 190 */ 191 rpl->r_brpl.rmp_type = RMP_BOOT_REPL; 192 rpl->r_brpl.rmp_retcode = RMP_E_OKAY; 193 ZEROWORD(rpl->r_brpl.rmp_seqno); 194 rpl->r_brpl.rmp_session = 0; 195 rpl->r_brpl.rmp_version = htons(RMP_VERSION); 196 197 size = &rpl->r_brpl.rmp_flnmsize; /* ptr to length of host name */ 198 199 /* 200 * Copy our host name into the reply packet incrementing the 201 * length as we go. Stop at RMP_HOSTLEN or the first dot. 202 */ 203 src = MyHost; 204 dst = (char *) &rpl->r_brpl.rmp_flnm; 205 for (*size = 0; *size < RMP_HOSTLEN; (*size)++) { 206 if (*src == '.' || *src == '\0') 207 break; 208 *dst++ = *src++; 209 } 210 211 rconn->rmplen = RMPBOOTSIZE(*size); /* set packet length */ 212 213 return(SendPacket(rconn)); /* send packet */ 214 } 215 216 /* 217 ** SendFileNo -- send the name of a bootable file to the requester. 218 ** 219 ** Parameters: 220 ** req - RMP BOOT packet containing the request. 221 ** rconn - the reply packet to be formatted. 222 ** filelist - list of files available to the requester. 223 ** 224 ** Returns: 225 ** 1 on success, 0 on failure. 226 ** 227 ** Side Effects: 228 ** none. 229 */ 230 int 231 SendFileNo(req, rconn, filelist) 232 struct rmp_packet *req; 233 RMPCONN *rconn; 234 char *filelist[]; 235 { 236 register struct rmp_packet *rpl; 237 register char *src, *dst; 238 register u_int8_t *size; 239 register int i; 240 241 GETWORD(req->r_brpl.rmp_seqno, i); /* SeqNo is really FileNo */ 242 rpl = &rconn->rmp; /* cache ptr to RMP packet */ 243 244 /* 245 * Set up assorted fields in reply packet. 246 */ 247 rpl->r_brpl.rmp_type = RMP_BOOT_REPL; 248 PUTWORD(i, rpl->r_brpl.rmp_seqno); 249 i--; 250 rpl->r_brpl.rmp_session = 0; 251 rpl->r_brpl.rmp_version = htons(RMP_VERSION); 252 253 size = &rpl->r_brpl.rmp_flnmsize; /* ptr to length of filename */ 254 *size = 0; /* init length to zero */ 255 256 /* 257 * Copy the file name into the reply packet incrementing the 258 * length as we go. Stop at end of string or when RMPBOOTDATA 259 * characters have been copied. Also, set return code to 260 * indicate success or "no more files". 261 */ 262 if (i < C_MAXFILE && filelist[i] != NULL) { 263 src = filelist[i]; 264 dst = (char *)&rpl->r_brpl.rmp_flnm; 265 for (; *src && *size < RMPBOOTDATA; (*size)++) { 266 if (*src == '\0') 267 break; 268 *dst++ = *src++; 269 } 270 rpl->r_brpl.rmp_retcode = RMP_E_OKAY; 271 } else 272 rpl->r_brpl.rmp_retcode = RMP_E_NODFLT; 273 274 rconn->rmplen = RMPBOOTSIZE(*size); /* set packet length */ 275 276 return(SendPacket(rconn)); /* send packet */ 277 } 278 279 /* 280 ** SendBootRepl -- open boot file and respond to boot request. 281 ** 282 ** Parameters: 283 ** req - RMP BOOT packet containing the request. 284 ** rconn - the reply packet to be formatted. 285 ** filelist - list of files available to the requester. 286 ** 287 ** Returns: 288 ** 1 on success, 0 on failure. 289 ** 290 ** Side Effects: 291 ** none. 292 */ 293 int 294 SendBootRepl(req, rconn, filelist) 295 struct rmp_packet *req; 296 RMPCONN *rconn; 297 char *filelist[]; 298 { 299 int retval; 300 char *filename, filepath[RMPBOOTDATA+1]; 301 RMPCONN *oldconn; 302 register struct rmp_packet *rpl; 303 register char *src, *dst1, *dst2; 304 register u_int8_t i; 305 306 /* 307 * If another connection already exists, delete it since we 308 * are obviously starting again. 309 */ 310 if ((oldconn = FindConn(rconn)) != NULL) { 311 syslog(LOG_WARNING, "%s: dropping existing connection", 312 EnetStr(oldconn)); 313 RemoveConn(oldconn); 314 } 315 316 rpl = &rconn->rmp; /* cache ptr to RMP packet */ 317 318 /* 319 * Set up assorted fields in reply packet. 320 */ 321 rpl->r_brpl.rmp_type = RMP_BOOT_REPL; 322 COPYWORD(req->r_brq.rmp_seqno, rpl->r_brpl.rmp_seqno); 323 rpl->r_brpl.rmp_session = htons(GenSessID()); 324 rpl->r_brpl.rmp_version = htons(RMP_VERSION); 325 rpl->r_brpl.rmp_flnmsize = req->r_brq.rmp_flnmsize; 326 327 /* 328 * Copy file name to `filepath' string, and into reply packet. 329 */ 330 dst1 = filepath; 331 dst2 = &rpl->r_brpl.rmp_flnm; 332 if (req->r_brq.rmp_flnmsize) 333 src = &req->r_brq.rmp_flnm; 334 else { 335 /* no file supplied, substitute the first one */ 336 src = filelist[0]; 337 req->r_brq.rmp_flnmsize = strlen(src); 338 } 339 for (i = 0; i < req->r_brq.rmp_flnmsize; i++) 340 *dst1++ = *dst2++ = *src++; 341 *dst1 = '\0'; 342 343 /* 344 * If we are booting HP-UX machines, their secondary loader will 345 * ask for files like "/hp-ux". As a security measure, we do not 346 * allow boot files to lay outside the boot directory (unless they 347 * are purposely link'd out. So, make `filename' become the path- 348 * stripped file name and spoof the client into thinking that it 349 * really got what it wanted. 350 */ 351 if ((filename = strrchr(filepath,'/')) != NULL) 352 filename++; 353 else 354 filename = filepath; 355 356 /* 357 * Check that this is a valid boot file name. 358 */ 359 for (i = 0; i < C_MAXFILE && filelist[i] != NULL; i++) 360 if (STREQN(filename, filelist[i])) 361 goto match; 362 363 /* 364 * Invalid boot file name, set error and send reply packet. 365 */ 366 rpl->r_brpl.rmp_retcode = RMP_E_NOFILE; 367 retval = 0; 368 goto sendpkt; 369 370 match: 371 /* 372 * This is a valid boot file. Open the file and save the file 373 * descriptor associated with this connection and set success 374 * indication. If the file couldnt be opened, set error: 375 * "no such file or dir" - RMP_E_NOFILE 376 * "file table overflow" - RMP_E_BUSY 377 * "too many open files" - RMP_E_BUSY 378 * anything else - RMP_E_OPENFILE 379 */ 380 if ((rconn->bootfd = open(filename, O_RDONLY, 0600)) < 0) { 381 rpl->r_brpl.rmp_retcode = (errno == ENOENT)? RMP_E_NOFILE: 382 (errno == EMFILE || errno == ENFILE)? RMP_E_BUSY: 383 RMP_E_OPENFILE; 384 retval = 0; 385 } else { 386 rpl->r_brpl.rmp_retcode = RMP_E_OKAY; 387 retval = 1; 388 } 389 390 sendpkt: 391 syslog(LOG_INFO, "%s: request to boot %s (%s)", 392 EnetStr(rconn), filename, retval? "granted": "denied"); 393 394 rconn->rmplen = RMPBOOTSIZE(rpl->r_brpl.rmp_flnmsize); 395 396 return (retval & SendPacket(rconn)); 397 } 398 399 /* 400 ** SendReadRepl -- send a portion of the boot file to the requester. 401 ** 402 ** Parameters: 403 ** rconn - the reply packet to be formatted. 404 ** 405 ** Returns: 406 ** 1 on success, 0 on failure. 407 ** 408 ** Side Effects: 409 ** none. 410 */ 411 int 412 SendReadRepl(rconn) 413 RMPCONN *rconn; 414 { 415 int retval = 0; 416 RMPCONN *oldconn; 417 register struct rmp_packet *rpl, *req; 418 register int size = 0; 419 int madeconn = 0; 420 421 /* 422 * Find the old connection. If one doesnt exist, create one only 423 * to return the error code. 424 */ 425 if ((oldconn = FindConn(rconn)) == NULL) { 426 if ((oldconn = NewConn(rconn)) == NULL) 427 return(0); 428 syslog(LOG_ERR, "SendReadRepl: no active connection (%s)", 429 EnetStr(rconn)); 430 madeconn++; 431 } 432 433 req = &rconn->rmp; /* cache ptr to request packet */ 434 rpl = &oldconn->rmp; /* cache ptr to reply packet */ 435 436 if (madeconn) { /* no active connection above; abort */ 437 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT; 438 retval = 1; 439 goto sendpkt; 440 } 441 442 /* 443 * Make sure Session ID's match. 444 */ 445 if (ntohs(req->r_rrq.rmp_session) != 446 ((rpl->r_type == RMP_BOOT_REPL)? ntohs(rpl->r_brpl.rmp_session): 447 ntohs(rpl->r_rrpl.rmp_session))) { 448 syslog(LOG_ERR, "SendReadRepl: bad session id (%s)", 449 EnetStr(rconn)); 450 rpl->r_rrpl.rmp_retcode = RMP_E_BADSID; 451 retval = 1; 452 goto sendpkt; 453 } 454 455 /* 456 * If the requester asks for more data than we can fit, 457 * silently clamp the request size down to RMPREADDATA. 458 * 459 * N.B. I do not know if this is "legal", however it seems 460 * to work. This is necessary for bpfwrite() on machines 461 * with MCLBYTES less than 1514. 462 */ 463 if (ntohs(req->r_rrq.rmp_size) > RMPREADDATA) 464 req->r_rrq.rmp_size = htons(RMPREADDATA); 465 466 /* 467 * Position read head on file according to info in request packet. 468 */ 469 GETWORD(req->r_rrq.rmp_offset, size); 470 if (lseek(oldconn->bootfd, (off_t)size, SEEK_SET) < 0) { 471 syslog(LOG_ERR, "SendReadRepl: lseek: %m (%s)", 472 EnetStr(rconn)); 473 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT; 474 retval = 1; 475 goto sendpkt; 476 } 477 478 /* 479 * Read data directly into reply packet. 480 */ 481 if ((size = read(oldconn->bootfd, &rpl->r_rrpl.rmp_data, 482 (int) ntohs(req->r_rrq.rmp_size))) <= 0) { 483 if (size < 0) { 484 syslog(LOG_ERR, "SendReadRepl: read: %m (%s)", 485 EnetStr(rconn)); 486 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT; 487 } else { 488 rpl->r_rrpl.rmp_retcode = RMP_E_EOF; 489 } 490 retval = 1; 491 goto sendpkt; 492 } 493 494 /* 495 * Set success indication. 496 */ 497 rpl->r_rrpl.rmp_retcode = RMP_E_OKAY; 498 499 sendpkt: 500 /* 501 * Set up assorted fields in reply packet. 502 */ 503 rpl->r_rrpl.rmp_type = RMP_READ_REPL; 504 COPYWORD(req->r_rrq.rmp_offset, rpl->r_rrpl.rmp_offset); 505 rpl->r_rrpl.rmp_session = req->r_rrq.rmp_session; 506 507 oldconn->rmplen = RMPREADSIZE(size); /* set size of packet */ 508 509 retval &= SendPacket(oldconn); /* send packet */ 510 511 if (madeconn) /* clean up after ourself */ 512 FreeConn(oldconn); 513 514 return (retval); 515 } 516 517 /* 518 ** BootDone -- free up memory allocated for a connection. 519 ** 520 ** Parameters: 521 ** rconn - incoming boot complete packet. 522 ** 523 ** Returns: 524 ** 1 on success, 0 on failure. 525 ** 526 ** Side Effects: 527 ** none. 528 */ 529 int 530 BootDone(rconn) 531 RMPCONN *rconn; 532 { 533 RMPCONN *oldconn; 534 struct rmp_packet *rpl; 535 536 /* 537 * If we cant find the connection, ignore the request. 538 */ 539 if ((oldconn = FindConn(rconn)) == NULL) { 540 syslog(LOG_ERR, "BootDone: no existing connection (%s)", 541 EnetStr(rconn)); 542 return(0); 543 } 544 545 rpl = &oldconn->rmp; /* cache ptr to RMP packet */ 546 547 /* 548 * Make sure Session ID's match. 549 */ 550 if (ntohs(rconn->rmp.r_rrq.rmp_session) != 551 ((rpl->r_type == RMP_BOOT_REPL)? ntohs(rpl->r_brpl.rmp_session): 552 ntohs(rpl->r_rrpl.rmp_session))) { 553 syslog(LOG_ERR, "BootDone: bad session id (%s)", 554 EnetStr(rconn)); 555 return(0); 556 } 557 558 RemoveConn(oldconn); /* remove connection */ 559 560 syslog(LOG_INFO, "%s: boot complete", EnetStr(rconn)); 561 562 return(1); 563 } 564 565 /* 566 ** SendPacket -- send an RMP packet to a remote host. 567 ** 568 ** Parameters: 569 ** rconn - packet to be sent. 570 ** 571 ** Returns: 572 ** 1 on success, 0 on failure. 573 ** 574 ** Side Effects: 575 ** none. 576 */ 577 int 578 SendPacket(rconn) 579 register RMPCONN *rconn; 580 { 581 /* 582 * Set Ethernet Destination address to Source (BPF and the enet 583 * driver will take care of getting our source address set). 584 */ 585 bcopy((char *)&rconn->rmp.hp_hdr.saddr[0], 586 (char *)&rconn->rmp.hp_hdr.daddr[0], RMP_ADDRLEN); 587 rconn->rmp.hp_hdr.len = htons(rconn->rmplen - sizeof(struct hp_hdr)); 588 589 /* 590 * Reverse 802.2/HP Extended Source & Destination Access Pts. 591 */ 592 rconn->rmp.hp_llc.dxsap = htons(HPEXT_SXSAP); 593 rconn->rmp.hp_llc.sxsap = htons(HPEXT_DXSAP); 594 595 /* 596 * Last time this connection was active. 597 */ 598 (void) gettimeofday(&rconn->tstamp, (struct timezone *)0); 599 600 if (DbgFp != NULL) /* display packet */ 601 DispPkt(rconn,DIR_SENT); 602 603 /* 604 * Send RMP packet to remote host. 605 */ 606 return(BpfWrite(rconn)); 607 } 608