2 * Copyright (c) 1988, 1992 The University of Utah and the Center
3 * for Software Science (CSS).
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * the Center for Software Science of the University of Utah Computer
9 * Science Department. CSS requests users of this software to return
10 * to css-dist@cs.utah.edu any improvements that they make and grant
11 * CSS redistribution rights.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * from: @(#)rmpproto.c 8.1 (Berkeley) 6/4/93
39 * From: Utah Hdr: rmpproto.c 3.1 92/07/06
40 * Author: Jeff Forys, University of Utah CSS
42 * @(#)rmpproto.c 8.1 (Berkeley) 6/4/93
43 * $FreeBSD: src/libexec/rbootd/rmpproto.c,v 1.6.2.1 2001/02/18 02:54:11 kris Exp $
46 #include <sys/param.h>
58 ** ProcessPacket -- determine packet type and do what's required.
60 ** An RMP BOOT packet has been received. Look at the type field
61 ** and process Boot Requests, Read Requests, and Boot Complete
62 ** packets. Any other type will be dropped with a warning msg.
65 ** rconn - the new connection
66 ** client - list of files available to this host
72 ** - If this is a valid boot request, it will be added to
73 ** the linked list of outstanding requests (RmpConns).
74 ** - If this is a valid boot complete, its associated
75 ** entry in RmpConns will be deleted.
76 ** - Also, unless we run out of memory, a reply will be
77 ** sent to the host that sent the packet.
80 ProcessPacket(RMPCONN *rconn, CLIENT *client)
82 struct rmp_packet *rmp;
85 rmp = &rconn->rmp; /* cache pointer to RMP packet */
87 switch(rmp->r_type) { /* do what we came here to do */
88 case RMP_BOOT_REQ: /* boot request */
89 if ((rconnout = NewConn(rconn)) == NULL)
93 * If the Session ID is 0xffff, this is a "probe"
94 * packet and we do not want to add the connection
95 * to the linked list of active connections. There
96 * are two types of probe packets, if the Sequence
97 * Number is 0 they want to know our host name, o/w
98 * they want the name of the file associated with
99 * the number spec'd by the Sequence Number.
101 * If this is an actual boot request, open the file
102 * and send a reply. If SendBootRepl() does not
103 * return 0, add the connection to the linked list
104 * of active connections, otherwise delete it since
105 * an error was encountered.
107 if (ntohs(rmp->r_brq.rmp_session) == RMP_PROBESID) {
108 if (WORDZE(rmp->r_brq.rmp_seqno))
109 (void) SendServerID(rconnout);
111 (void) SendFileNo(rmp, rconnout,
112 client? client->files:
116 if (SendBootRepl(rmp, rconnout,
117 client? client->files: BootFiles))
124 case RMP_BOOT_REPL: /* boot reply (not valid) */
125 syslog(LOG_WARNING, "%s: sent a boot reply",
129 case RMP_READ_REQ: /* read request */
131 * Send a portion of the boot file.
133 (void) SendReadRepl(rconn);
136 case RMP_READ_REPL: /* read reply (not valid) */
137 syslog(LOG_WARNING, "%s: sent a read reply",
141 case RMP_BOOT_DONE: /* boot complete */
143 * Remove the entry from the linked list of active
146 (void) BootDone(rconn);
149 default: /* unknown RMP packet type */
150 syslog(LOG_WARNING, "%s: unknown packet type (%u)",
151 EnetStr(rconn), rmp->r_type);
156 ** SendServerID -- send our host name to who ever requested it.
159 ** rconn - the reply packet to be formatted.
162 ** 1 on success, 0 on failure.
168 SendServerID(RMPCONN *rconn)
170 struct rmp_packet *rpl;
174 rpl = &rconn->rmp; /* cache ptr to RMP packet */
177 * Set up assorted fields in reply packet.
179 rpl->r_brpl.rmp_type = RMP_BOOT_REPL;
180 rpl->r_brpl.rmp_retcode = RMP_E_OKAY;
181 ZEROWORD(rpl->r_brpl.rmp_seqno);
182 rpl->r_brpl.rmp_session = 0;
183 rpl->r_brpl.rmp_version = htons(RMP_VERSION);
185 size = &rpl->r_brpl.rmp_flnmsize; /* ptr to length of host name */
188 * Copy our host name into the reply packet incrementing the
189 * length as we go. Stop at RMP_HOSTLEN or the first dot.
192 dst = (char *) &rpl->r_brpl.rmp_flnm;
193 for (*size = 0; *size < RMP_HOSTLEN; (*size)++) {
194 if (*src == '.' || *src == '\0')
199 rconn->rmplen = RMPBOOTSIZE(*size); /* set packet length */
201 return(SendPacket(rconn)); /* send packet */
205 ** SendFileNo -- send the name of a bootable file to the requester.
208 ** req - RMP BOOT packet containing the request.
209 ** rconn - the reply packet to be formatted.
210 ** filelist - list of files available to the requester.
213 ** 1 on success, 0 on failure.
219 SendFileNo(struct rmp_packet *req, RMPCONN *rconn, char *filelist[])
221 struct rmp_packet *rpl;
226 GETWORD(req->r_brpl.rmp_seqno, i); /* SeqNo is really FileNo */
227 rpl = &rconn->rmp; /* cache ptr to RMP packet */
230 * Set up assorted fields in reply packet.
232 rpl->r_brpl.rmp_type = RMP_BOOT_REPL;
233 PUTWORD(i, rpl->r_brpl.rmp_seqno);
235 rpl->r_brpl.rmp_session = 0;
236 rpl->r_brpl.rmp_version = htons(RMP_VERSION);
238 size = &rpl->r_brpl.rmp_flnmsize; /* ptr to length of filename */
239 *size = 0; /* init length to zero */
242 * Copy the file name into the reply packet incrementing the
243 * length as we go. Stop at end of string or when RMPBOOTDATA
244 * characters have been copied. Also, set return code to
245 * indicate success or "no more files".
247 if (i < C_MAXFILE && filelist[i] != NULL) {
249 dst = (char *)&rpl->r_brpl.rmp_flnm;
250 for (; *src && *size < RMPBOOTDATA; (*size)++) {
255 rpl->r_brpl.rmp_retcode = RMP_E_OKAY;
257 rpl->r_brpl.rmp_retcode = RMP_E_NODFLT;
259 rconn->rmplen = RMPBOOTSIZE(*size); /* set packet length */
261 return(SendPacket(rconn)); /* send packet */
265 ** SendBootRepl -- open boot file and respond to boot request.
268 ** req - RMP BOOT packet containing the request.
269 ** rconn - the reply packet to be formatted.
270 ** filelist - list of files available to the requester.
273 ** 1 on success, 0 on failure.
279 SendBootRepl(struct rmp_packet *req, RMPCONN *rconn, char *filelist[])
282 char *filename, filepath[RMPBOOTDATA+1];
284 struct rmp_packet *rpl;
285 char *src, *dst1, *dst2;
289 * If another connection already exists, delete it since we
290 * are obviously starting again.
292 if ((oldconn = FindConn(rconn)) != NULL) {
293 syslog(LOG_WARNING, "%s: dropping existing connection",
298 rpl = &rconn->rmp; /* cache ptr to RMP packet */
301 * Set up assorted fields in reply packet.
303 rpl->r_brpl.rmp_type = RMP_BOOT_REPL;
304 COPYWORD(req->r_brq.rmp_seqno, rpl->r_brpl.rmp_seqno);
305 rpl->r_brpl.rmp_session = htons(GenSessID());
306 rpl->r_brpl.rmp_version = htons(RMP_VERSION);
307 rpl->r_brpl.rmp_flnmsize = req->r_brq.rmp_flnmsize;
310 * Copy file name to `filepath' string, and into reply packet.
312 src = &req->r_brq.rmp_flnm;
314 dst2 = &rpl->r_brpl.rmp_flnm;
315 for (i = 0; i < req->r_brq.rmp_flnmsize; i++)
316 *dst1++ = *dst2++ = *src++;
320 * If we are booting HP-UX machines, their secondary loader will
321 * ask for files like "/hp-ux". As a security measure, we do not
322 * allow boot files to lay outside the boot directory (unless they
323 * are purposely link'd out. So, make `filename' become the path-
324 * stripped file name and spoof the client into thinking that it
325 * really got what it wanted.
327 if ((filename = strrchr(filepath, '/')) != NULL)
333 * Check that this is a valid boot file name.
335 for (i = 0; i < C_MAXFILE && filelist[i] != NULL; i++)
336 if (STREQN(filename, filelist[i]))
340 * Invalid boot file name, set error and send reply packet.
342 rpl->r_brpl.rmp_retcode = RMP_E_NOFILE;
348 * This is a valid boot file. Open the file and save the file
349 * descriptor associated with this connection and set success
350 * indication. If the file couldnt be opened, set error:
351 * "no such file or dir" - RMP_E_NOFILE
352 * "file table overflow" - RMP_E_BUSY
353 * "too many open files" - RMP_E_BUSY
354 * anything else - RMP_E_OPENFILE
356 if ((rconn->bootfd = open(filename, O_RDONLY, 0600)) < 0) {
357 rpl->r_brpl.rmp_retcode = (errno == ENOENT)? RMP_E_NOFILE:
358 (errno == EMFILE || errno == ENFILE)? RMP_E_BUSY:
362 rpl->r_brpl.rmp_retcode = RMP_E_OKAY;
367 syslog(LOG_INFO, "%s: request to boot %s (%s)",
368 EnetStr(rconn), filename, retval? "granted": "denied");
370 rconn->rmplen = RMPBOOTSIZE(rpl->r_brpl.rmp_flnmsize);
372 return (retval & SendPacket(rconn));
376 ** SendReadRepl -- send a portion of the boot file to the requester.
379 ** rconn - the reply packet to be formatted.
382 ** 1 on success, 0 on failure.
388 SendReadRepl(RMPCONN *rconn)
392 struct rmp_packet *rpl, *req;
397 * Find the old connection. If one doesnt exist, create one only
398 * to return the error code.
400 if ((oldconn = FindConn(rconn)) == NULL) {
401 if ((oldconn = NewConn(rconn)) == NULL)
403 syslog(LOG_ERR, "SendReadRepl: no active connection (%s)",
408 req = &rconn->rmp; /* cache ptr to request packet */
409 rpl = &oldconn->rmp; /* cache ptr to reply packet */
411 if (madeconn) { /* no active connection above; abort */
412 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
418 * Make sure Session ID's match.
420 if (ntohs(req->r_rrq.rmp_session) !=
421 ((rpl->r_type == RMP_BOOT_REPL)? ntohs(rpl->r_brpl.rmp_session):
422 ntohs(rpl->r_rrpl.rmp_session))) {
423 syslog(LOG_ERR, "SendReadRepl: bad session id (%s)",
425 rpl->r_rrpl.rmp_retcode = RMP_E_BADSID;
431 * If the requester asks for more data than we can fit,
432 * silently clamp the request size down to RMPREADDATA.
434 * N.B. I do not know if this is "legal", however it seems
435 * to work. This is necessary for bpfwrite() on machines
436 * with MCLBYTES less than 1514.
438 if (ntohs(req->r_rrq.rmp_size) > RMPREADDATA)
439 req->r_rrq.rmp_size = htons(RMPREADDATA);
442 * Position read head on file according to info in request packet.
444 GETWORD(req->r_rrq.rmp_offset, size);
445 if (lseek(oldconn->bootfd, (off_t)size, SEEK_SET) < 0) {
446 syslog(LOG_ERR, "SendReadRepl: lseek: %m (%s)",
448 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
454 * Read data directly into reply packet.
456 if ((size = read(oldconn->bootfd, &rpl->r_rrpl.rmp_data,
457 (int) ntohs(req->r_rrq.rmp_size))) <= 0) {
459 syslog(LOG_ERR, "SendReadRepl: read: %m (%s)",
461 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
463 rpl->r_rrpl.rmp_retcode = RMP_E_EOF;
470 * Set success indication.
472 rpl->r_rrpl.rmp_retcode = RMP_E_OKAY;
476 * Set up assorted fields in reply packet.
478 rpl->r_rrpl.rmp_type = RMP_READ_REPL;
479 COPYWORD(req->r_rrq.rmp_offset, rpl->r_rrpl.rmp_offset);
480 rpl->r_rrpl.rmp_session = req->r_rrq.rmp_session;
482 oldconn->rmplen = RMPREADSIZE(size); /* set size of packet */
484 retval &= SendPacket(oldconn); /* send packet */
486 if (madeconn) /* clean up after ourself */
493 ** BootDone -- free up memory allocated for a connection.
496 ** rconn - incoming boot complete packet.
499 ** 1 on success, 0 on failure.
505 BootDone(RMPCONN *rconn)
508 struct rmp_packet *rpl;
511 * If we cant find the connection, ignore the request.
513 if ((oldconn = FindConn(rconn)) == NULL) {
514 syslog(LOG_ERR, "BootDone: no existing connection (%s)",
519 rpl = &oldconn->rmp; /* cache ptr to RMP packet */
522 * Make sure Session ID's match.
524 if (ntohs(rconn->rmp.r_rrq.rmp_session) !=
525 ((rpl->r_type == RMP_BOOT_REPL)? ntohs(rpl->r_brpl.rmp_session):
526 ntohs(rpl->r_rrpl.rmp_session))) {
527 syslog(LOG_ERR, "BootDone: bad session id (%s)",
532 RemoveConn(oldconn); /* remove connection */
534 syslog(LOG_INFO, "%s: boot complete", EnetStr(rconn));
540 ** SendPacket -- send an RMP packet to a remote host.
543 ** rconn - packet to be sent.
546 ** 1 on success, 0 on failure.
552 SendPacket(RMPCONN *rconn)
555 * Set Ethernet Destination address to Source (BPF and the enet
556 * driver will take care of getting our source address set).
558 memmove((char *)&rconn->rmp.hp_hdr.daddr[0],
559 (char *)&rconn->rmp.hp_hdr.saddr[0], RMP_ADDRLEN);
560 rconn->rmp.hp_hdr.len = htons(rconn->rmplen - sizeof(struct hp_hdr));
563 * Reverse 802.2/HP Extended Source & Destination Access Pts.
565 rconn->rmp.hp_llc.dxsap = htons(HPEXT_SXSAP);
566 rconn->rmp.hp_llc.sxsap = htons(HPEXT_DXSAP);
569 * Last time this connection was active.
571 (void) gettimeofday(&rconn->tstamp, NULL);
573 if (DbgFp != NULL) /* display packet */
574 DispPkt(rconn,DIR_SENT);
577 * Send RMP packet to remote host.
579 return(BpfWrite(rconn));