2 * SPDX-License-Identifier: BSD-3-Clause
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.
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.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * from: @(#)rmpproto.c 8.1 (Berkeley) 6/4/93
41 * From: Utah Hdr: rmpproto.c 3.1 92/07/06
42 * Author: Jeff Forys, University of Utah CSS
47 static const char sccsid[] = "@(#)rmpproto.c 8.1 (Berkeley) 6/4/93";
49 static const char rcsid[] =
53 #include <sys/param.h>
55 #include <netinet/in.h>
66 ** ProcessPacket -- determine packet type and do what's required.
68 ** An RMP BOOT packet has been received. Look at the type field
69 ** and process Boot Requests, Read Requests, and Boot Complete
70 ** packets. Any other type will be dropped with a warning msg.
73 ** rconn - the new connection
74 ** client - list of files available to this host
80 ** - If this is a valid boot request, it will be added to
81 ** the linked list of outstanding requests (RmpConns).
82 ** - If this is a valid boot complete, its associated
83 ** entry in RmpConns will be deleted.
84 ** - Also, unless we run out of memory, a reply will be
85 ** sent to the host that sent the packet.
88 ProcessPacket(RMPCONN *rconn, CLIENT *client)
90 struct rmp_packet *rmp;
93 rmp = &rconn->rmp; /* cache pointer to RMP packet */
95 switch(rmp->r_type) { /* do what we came here to do */
96 case RMP_BOOT_REQ: /* boot request */
97 if ((rconnout = NewConn(rconn)) == NULL)
101 * If the Session ID is 0xffff, this is a "probe"
102 * packet and we do not want to add the connection
103 * to the linked list of active connections. There
104 * are two types of probe packets, if the Sequence
105 * Number is 0 they want to know our host name, o/w
106 * they want the name of the file associated with
107 * the number spec'd by the Sequence Number.
109 * If this is an actual boot request, open the file
110 * and send a reply. If SendBootRepl() does not
111 * return 0, add the connection to the linked list
112 * of active connections, otherwise delete it since
113 * an error was encountered.
115 if (ntohs(rmp->r_brq.rmp_session) == RMP_PROBESID) {
116 if (WORDZE(rmp->r_brq.rmp_seqno))
117 (void) SendServerID(rconnout);
119 (void) SendFileNo(rmp, rconnout,
120 client? client->files:
124 if (SendBootRepl(rmp, rconnout,
125 client? client->files: BootFiles))
132 case RMP_BOOT_REPL: /* boot reply (not valid) */
133 syslog(LOG_WARNING, "%s: sent a boot reply",
137 case RMP_READ_REQ: /* read request */
139 * Send a portion of the boot file.
141 (void) SendReadRepl(rconn);
144 case RMP_READ_REPL: /* read reply (not valid) */
145 syslog(LOG_WARNING, "%s: sent a read reply",
149 case RMP_BOOT_DONE: /* boot complete */
151 * Remove the entry from the linked list of active
154 (void) BootDone(rconn);
157 default: /* unknown RMP packet type */
158 syslog(LOG_WARNING, "%s: unknown packet type (%u)",
159 EnetStr(rconn), rmp->r_type);
164 ** SendServerID -- send our host name to who ever requested it.
167 ** rconn - the reply packet to be formatted.
170 ** 1 on success, 0 on failure.
176 SendServerID(RMPCONN *rconn)
178 struct rmp_packet *rpl;
182 rpl = &rconn->rmp; /* cache ptr to RMP packet */
185 * Set up assorted fields in reply packet.
187 rpl->r_brpl.rmp_type = RMP_BOOT_REPL;
188 rpl->r_brpl.rmp_retcode = RMP_E_OKAY;
189 ZEROWORD(rpl->r_brpl.rmp_seqno);
190 rpl->r_brpl.rmp_session = 0;
191 rpl->r_brpl.rmp_version = htons(RMP_VERSION);
193 size = &rpl->r_brpl.rmp_flnmsize; /* ptr to length of host name */
196 * Copy our host name into the reply packet incrementing the
197 * length as we go. Stop at RMP_HOSTLEN or the first dot.
200 dst = (char *) &rpl->r_brpl.rmp_flnm;
201 for (*size = 0; *size < RMP_HOSTLEN; (*size)++) {
202 if (*src == '.' || *src == '\0')
207 rconn->rmplen = RMPBOOTSIZE(*size); /* set packet length */
209 return(SendPacket(rconn)); /* send packet */
213 ** SendFileNo -- send the name of a bootable file to the requester.
216 ** req - RMP BOOT packet containing the request.
217 ** rconn - the reply packet to be formatted.
218 ** filelist - list of files available to the requester.
221 ** 1 on success, 0 on failure.
227 SendFileNo(struct rmp_packet *req, RMPCONN *rconn, char *filelist[])
229 struct rmp_packet *rpl;
234 GETWORD(req->r_brpl.rmp_seqno, i); /* SeqNo is really FileNo */
235 rpl = &rconn->rmp; /* cache ptr to RMP packet */
238 * Set up assorted fields in reply packet.
240 rpl->r_brpl.rmp_type = RMP_BOOT_REPL;
241 PUTWORD(i, rpl->r_brpl.rmp_seqno);
243 rpl->r_brpl.rmp_session = 0;
244 rpl->r_brpl.rmp_version = htons(RMP_VERSION);
246 size = &rpl->r_brpl.rmp_flnmsize; /* ptr to length of filename */
247 *size = 0; /* init length to zero */
250 * Copy the file name into the reply packet incrementing the
251 * length as we go. Stop at end of string or when RMPBOOTDATA
252 * characters have been copied. Also, set return code to
253 * indicate success or "no more files".
255 if (i < C_MAXFILE && filelist[i] != NULL) {
257 dst = (char *)&rpl->r_brpl.rmp_flnm;
258 for (; *src && *size < RMPBOOTDATA; (*size)++) {
263 rpl->r_brpl.rmp_retcode = RMP_E_OKAY;
265 rpl->r_brpl.rmp_retcode = RMP_E_NODFLT;
267 rconn->rmplen = RMPBOOTSIZE(*size); /* set packet length */
269 return(SendPacket(rconn)); /* send packet */
273 ** SendBootRepl -- open boot file and respond to boot request.
276 ** req - RMP BOOT packet containing the request.
277 ** rconn - the reply packet to be formatted.
278 ** filelist - list of files available to the requester.
281 ** 1 on success, 0 on failure.
287 SendBootRepl(struct rmp_packet *req, RMPCONN *rconn, char *filelist[])
290 char *filename, filepath[RMPBOOTDATA+1];
292 struct rmp_packet *rpl;
293 char *src, *dst1, *dst2;
297 * If another connection already exists, delete it since we
298 * are obviously starting again.
300 if ((oldconn = FindConn(rconn)) != NULL) {
301 syslog(LOG_WARNING, "%s: dropping existing connection",
306 rpl = &rconn->rmp; /* cache ptr to RMP packet */
309 * Set up assorted fields in reply packet.
311 rpl->r_brpl.rmp_type = RMP_BOOT_REPL;
312 COPYWORD(req->r_brq.rmp_seqno, rpl->r_brpl.rmp_seqno);
313 rpl->r_brpl.rmp_session = htons(GenSessID());
314 rpl->r_brpl.rmp_version = htons(RMP_VERSION);
315 rpl->r_brpl.rmp_flnmsize = req->r_brq.rmp_flnmsize;
318 * Copy file name to `filepath' string, and into reply packet.
320 src = &req->r_brq.rmp_flnm;
322 dst2 = &rpl->r_brpl.rmp_flnm;
323 for (i = 0; i < req->r_brq.rmp_flnmsize; i++)
324 *dst1++ = *dst2++ = *src++;
328 * If we are booting HP-UX machines, their secondary loader will
329 * ask for files like "/hp-ux". As a security measure, we do not
330 * allow boot files to lay outside the boot directory (unless they
331 * are purposely link'd out. So, make `filename' become the path-
332 * stripped file name and spoof the client into thinking that it
333 * really got what it wanted.
335 filename = strrchr(filepath,'/');
336 filename = filename? filename + 1: filepath;
339 * Check that this is a valid boot file name.
341 for (i = 0; i < C_MAXFILE && filelist[i] != NULL; i++)
342 if (STREQN(filename, filelist[i]))
346 * Invalid boot file name, set error and send reply packet.
348 rpl->r_brpl.rmp_retcode = RMP_E_NOFILE;
354 * This is a valid boot file. Open the file and save the file
355 * descriptor associated with this connection and set success
356 * indication. If the file couldnt be opened, set error:
357 * "no such file or dir" - RMP_E_NOFILE
358 * "file table overflow" - RMP_E_BUSY
359 * "too many open files" - RMP_E_BUSY
360 * anything else - RMP_E_OPENFILE
362 if ((rconn->bootfd = open(filename, O_RDONLY, 0600)) < 0) {
363 rpl->r_brpl.rmp_retcode = (errno == ENOENT)? RMP_E_NOFILE:
364 (errno == EMFILE || errno == ENFILE)? RMP_E_BUSY:
368 rpl->r_brpl.rmp_retcode = RMP_E_OKAY;
373 syslog(LOG_INFO, "%s: request to boot %s (%s)",
374 EnetStr(rconn), filename, retval? "granted": "denied");
376 rconn->rmplen = RMPBOOTSIZE(rpl->r_brpl.rmp_flnmsize);
378 return (retval & SendPacket(rconn));
382 ** SendReadRepl -- send a portion of the boot file to the requester.
385 ** rconn - the reply packet to be formatted.
388 ** 1 on success, 0 on failure.
394 SendReadRepl(RMPCONN *rconn)
398 struct rmp_packet *rpl, *req;
403 * Find the old connection. If one doesn't exist, create one only
404 * to return the error code.
406 if ((oldconn = FindConn(rconn)) == NULL) {
407 if ((oldconn = NewConn(rconn)) == NULL)
409 syslog(LOG_ERR, "SendReadRepl: no active connection (%s)",
414 req = &rconn->rmp; /* cache ptr to request packet */
415 rpl = &oldconn->rmp; /* cache ptr to reply packet */
417 if (madeconn) { /* no active connection above; abort */
418 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
424 * Make sure Session ID's match.
426 if (ntohs(req->r_rrq.rmp_session) !=
427 ((rpl->r_type == RMP_BOOT_REPL)? ntohs(rpl->r_brpl.rmp_session):
428 ntohs(rpl->r_rrpl.rmp_session))) {
429 syslog(LOG_ERR, "SendReadRepl: bad session id (%s)",
431 rpl->r_rrpl.rmp_retcode = RMP_E_BADSID;
437 * If the requester asks for more data than we can fit,
438 * silently clamp the request size down to RMPREADDATA.
440 * N.B. I do not know if this is "legal", however it seems
441 * to work. This is necessary for bpfwrite() on machines
442 * with MCLBYTES less than 1514.
444 if (ntohs(req->r_rrq.rmp_size) > RMPREADDATA)
445 req->r_rrq.rmp_size = htons(RMPREADDATA);
448 * Position read head on file according to info in request packet.
450 GETWORD(req->r_rrq.rmp_offset, size);
451 if (lseek(oldconn->bootfd, (off_t)size, SEEK_SET) < 0) {
452 syslog(LOG_ERR, "SendReadRepl: lseek: %m (%s)",
454 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
460 * Read data directly into reply packet.
462 if ((size = read(oldconn->bootfd, &rpl->r_rrpl.rmp_data,
463 (int) ntohs(req->r_rrq.rmp_size))) <= 0) {
465 syslog(LOG_ERR, "SendReadRepl: read: %m (%s)",
467 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
469 rpl->r_rrpl.rmp_retcode = RMP_E_EOF;
476 * Set success indication.
478 rpl->r_rrpl.rmp_retcode = RMP_E_OKAY;
482 * Set up assorted fields in reply packet.
484 rpl->r_rrpl.rmp_type = RMP_READ_REPL;
485 COPYWORD(req->r_rrq.rmp_offset, rpl->r_rrpl.rmp_offset);
486 rpl->r_rrpl.rmp_session = req->r_rrq.rmp_session;
488 oldconn->rmplen = RMPREADSIZE(size); /* set size of packet */
490 retval &= SendPacket(oldconn); /* send packet */
492 if (madeconn) /* clean up after ourself */
499 ** BootDone -- free up memory allocated for a connection.
502 ** rconn - incoming boot complete packet.
505 ** 1 on success, 0 on failure.
511 BootDone(RMPCONN *rconn)
514 struct rmp_packet *rpl;
517 * If we can't find the connection, ignore the request.
519 if ((oldconn = FindConn(rconn)) == NULL) {
520 syslog(LOG_ERR, "BootDone: no existing connection (%s)",
525 rpl = &oldconn->rmp; /* cache ptr to RMP packet */
528 * Make sure Session ID's match.
530 if (ntohs(rconn->rmp.r_rrq.rmp_session) !=
531 ((rpl->r_type == RMP_BOOT_REPL)? ntohs(rpl->r_brpl.rmp_session):
532 ntohs(rpl->r_rrpl.rmp_session))) {
533 syslog(LOG_ERR, "BootDone: bad session id (%s)",
538 RemoveConn(oldconn); /* remove connection */
540 syslog(LOG_INFO, "%s: boot complete", EnetStr(rconn));
546 ** SendPacket -- send an RMP packet to a remote host.
549 ** rconn - packet to be sent.
552 ** 1 on success, 0 on failure.
558 SendPacket(RMPCONN *rconn)
561 * Set Ethernet Destination address to Source (BPF and the enet
562 * driver will take care of getting our source address set).
564 memmove((char *)&rconn->rmp.hp_hdr.daddr[0],
565 (char *)&rconn->rmp.hp_hdr.saddr[0], RMP_ADDRLEN);
566 rconn->rmp.hp_hdr.len = htons(rconn->rmplen - sizeof(struct hp_hdr));
569 * Reverse 802.2/HP Extended Source & Destination Access Pts.
571 rconn->rmp.hp_llc.dxsap = htons(HPEXT_SXSAP);
572 rconn->rmp.hp_llc.sxsap = htons(HPEXT_DXSAP);
575 * Last time this connection was active.
577 (void)gettimeofday(&rconn->tstamp, NULL);
579 if (DbgFp != NULL) /* display packet */
580 DispPkt(rconn,DIR_SENT);
583 * Send RMP packet to remote host.
585 return(BpfWrite(rconn));