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