1 /* $NetBSD: rmpproto.c,v 1.11 1999/02/01 17:00:44 bouyer 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.11 1999/02/01 17:00:44 bouyer 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 dst1 = filepath; 334 dst2 = &rpl->r_brpl.rmp_flnm; 335 if (req->r_brq.rmp_flnmsize) 336 src = &req->r_brq.rmp_flnm; 337 else { 338 /* no file supplied, substitute the first one */ 339 src = filelist[0]; 340 req->r_brq.rmp_flnmsize = strlen(src); 341 } 342 for (i = 0; i < req->r_brq.rmp_flnmsize; i++) 343 *dst1++ = *dst2++ = *src++; 344 *dst1 = '\0'; 345 346 /* 347 * If we are booting HP-UX machines, their secondary loader will 348 * ask for files like "/hp-ux". As a security measure, we do not 349 * allow boot files to lay outside the boot directory (unless they 350 * are purposely link'd out. So, make `filename' become the path- 351 * stripped file name and spoof the client into thinking that it 352 * really got what it wanted. 353 */ 354 filename = (filename = strrchr(filepath,'/'))? ++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 struct rmp_packet *rpl, *req; 418 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 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 memmove((char *)&rconn->rmp.hp_hdr.daddr[0], 586 (char *)&rconn->rmp.hp_hdr.saddr[0], RMP_ADDRLEN); 587 #ifdef __FreeBSD__ 588 /* BPF (incorrectly) wants this in host order. */ 589 rconn->rmp.hp_hdr.len = rconn->rmplen - sizeof(struct hp_hdr); 590 #else 591 rconn->rmp.hp_hdr.len = htons(rconn->rmplen - sizeof(struct hp_hdr)); 592 #endif 593 594 /* 595 * Reverse 802.2/HP Extended Source & Destination Access Pts. 596 */ 597 rconn->rmp.hp_llc.dxsap = htons(HPEXT_SXSAP); 598 rconn->rmp.hp_llc.sxsap = htons(HPEXT_DXSAP); 599 600 /* 601 * Last time this connection was active. 602 */ 603 (void) gettimeofday(&rconn->tstamp, (struct timezone *)0); 604 605 if (DbgFp != NULL) /* display packet */ 606 DispPkt(rconn,DIR_SENT); 607 608 /* 609 * Send RMP packet to remote host. 610 */ 611 return(BpfWrite(rconn)); 612 } 613