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. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * from: @(#)rmpproto.c 8.1 (Berkeley) 6/4/93
43 * From: Utah Hdr: rmpproto.c 3.1 92/07/06
44 * Author: Jeff Forys, University of Utah CSS
49 static const char sccsid[] = "@(#)rmpproto.c 8.1 (Berkeley) 6/4/93";
51 static const char rcsid[] =
52 "$FreeBSD: src/libexec/rbootd/rmpproto.c,v 1.6.2.1 2001/02/18 02:54:11 kris Exp $";
55 #include <sys/param.h>
67 ** ProcessPacket -- determine packet type and do what's required.
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.
74 ** rconn - the new connection
75 ** client - list of files available to this host
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.
89 ProcessPacket(rconn, client)
93 struct rmp_packet *rmp;
96 rmp = &rconn->rmp; /* cache pointer to RMP packet */
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)
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.
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.
118 if (ntohs(rmp->r_brq.rmp_session) == RMP_PROBESID) {
119 if (WORDZE(rmp->r_brq.rmp_seqno))
120 (void) SendServerID(rconnout);
122 (void) SendFileNo(rmp, rconnout,
123 client? client->files:
127 if (SendBootRepl(rmp, rconnout,
128 client? client->files: BootFiles))
135 case RMP_BOOT_REPL: /* boot reply (not valid) */
136 syslog(LOG_WARNING, "%s: sent a boot reply",
140 case RMP_READ_REQ: /* read request */
142 * Send a portion of the boot file.
144 (void) SendReadRepl(rconn);
147 case RMP_READ_REPL: /* read reply (not valid) */
148 syslog(LOG_WARNING, "%s: sent a read reply",
152 case RMP_BOOT_DONE: /* boot complete */
154 * Remove the entry from the linked list of active
157 (void) BootDone(rconn);
160 default: /* unknown RMP packet type */
161 syslog(LOG_WARNING, "%s: unknown packet type (%u)",
162 EnetStr(rconn), rmp->r_type);
167 ** SendServerID -- send our host name to who ever requested it.
170 ** rconn - the reply packet to be formatted.
173 ** 1 on success, 0 on failure.
182 struct rmp_packet *rpl;
186 rpl = &rconn->rmp; /* cache ptr to RMP packet */
189 * Set up assorted fields in reply packet.
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);
197 size = &rpl->r_brpl.rmp_flnmsize; /* ptr to length of host name */
200 * Copy our host name into the reply packet incrementing the
201 * length as we go. Stop at RMP_HOSTLEN or the first dot.
204 dst = (char *) &rpl->r_brpl.rmp_flnm;
205 for (*size = 0; *size < RMP_HOSTLEN; (*size)++) {
206 if (*src == '.' || *src == '\0')
211 rconn->rmplen = RMPBOOTSIZE(*size); /* set packet length */
213 return(SendPacket(rconn)); /* send packet */
217 ** SendFileNo -- send the name of a bootable file to the requester.
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.
225 ** 1 on success, 0 on failure.
231 SendFileNo(req, rconn, filelist)
232 struct rmp_packet *req;
236 struct rmp_packet *rpl;
241 GETWORD(req->r_brpl.rmp_seqno, i); /* SeqNo is really FileNo */
242 rpl = &rconn->rmp; /* cache ptr to RMP packet */
245 * Set up assorted fields in reply packet.
247 rpl->r_brpl.rmp_type = RMP_BOOT_REPL;
248 PUTWORD(i, rpl->r_brpl.rmp_seqno);
250 rpl->r_brpl.rmp_session = 0;
251 rpl->r_brpl.rmp_version = htons(RMP_VERSION);
253 size = &rpl->r_brpl.rmp_flnmsize; /* ptr to length of filename */
254 *size = 0; /* init length to zero */
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".
262 if (i < C_MAXFILE && filelist[i] != NULL) {
264 dst = (char *)&rpl->r_brpl.rmp_flnm;
265 for (; *src && *size < RMPBOOTDATA; (*size)++) {
270 rpl->r_brpl.rmp_retcode = RMP_E_OKAY;
272 rpl->r_brpl.rmp_retcode = RMP_E_NODFLT;
274 rconn->rmplen = RMPBOOTSIZE(*size); /* set packet length */
276 return(SendPacket(rconn)); /* send packet */
280 ** SendBootRepl -- open boot file and respond to boot request.
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.
288 ** 1 on success, 0 on failure.
294 SendBootRepl(req, rconn, filelist)
295 struct rmp_packet *req;
300 char *filename, filepath[RMPBOOTDATA+1];
302 struct rmp_packet *rpl;
303 char *src, *dst1, *dst2;
307 * If another connection already exists, delete it since we
308 * are obviously starting again.
310 if ((oldconn = FindConn(rconn)) != NULL) {
311 syslog(LOG_WARNING, "%s: dropping existing connection",
316 rpl = &rconn->rmp; /* cache ptr to RMP packet */
319 * Set up assorted fields in reply packet.
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;
328 * Copy file name to `filepath' string, and into reply packet.
330 src = &req->r_brq.rmp_flnm;
332 dst2 = &rpl->r_brpl.rmp_flnm;
333 for (i = 0; i < req->r_brq.rmp_flnmsize; i++)
334 *dst1++ = *dst2++ = *src++;
338 * If we are booting HP-UX machines, their secondary loader will
339 * ask for files like "/hp-ux". As a security measure, we do not
340 * allow boot files to lay outside the boot directory (unless they
341 * are purposely link'd out. So, make `filename' become the path-
342 * stripped file name and spoof the client into thinking that it
343 * really got what it wanted.
345 filename = (filename = strrchr(filepath,'/'))? ++filename: filepath;
348 * Check that this is a valid boot file name.
350 for (i = 0; i < C_MAXFILE && filelist[i] != NULL; i++)
351 if (STREQN(filename, filelist[i]))
355 * Invalid boot file name, set error and send reply packet.
357 rpl->r_brpl.rmp_retcode = RMP_E_NOFILE;
363 * This is a valid boot file. Open the file and save the file
364 * descriptor associated with this connection and set success
365 * indication. If the file couldnt be opened, set error:
366 * "no such file or dir" - RMP_E_NOFILE
367 * "file table overflow" - RMP_E_BUSY
368 * "too many open files" - RMP_E_BUSY
369 * anything else - RMP_E_OPENFILE
371 if ((rconn->bootfd = open(filename, O_RDONLY, 0600)) < 0) {
372 rpl->r_brpl.rmp_retcode = (errno == ENOENT)? RMP_E_NOFILE:
373 (errno == EMFILE || errno == ENFILE)? RMP_E_BUSY:
377 rpl->r_brpl.rmp_retcode = RMP_E_OKAY;
382 syslog(LOG_INFO, "%s: request to boot %s (%s)",
383 EnetStr(rconn), filename, retval? "granted": "denied");
385 rconn->rmplen = RMPBOOTSIZE(rpl->r_brpl.rmp_flnmsize);
387 return (retval & SendPacket(rconn));
391 ** SendReadRepl -- send a portion of the boot file to the requester.
394 ** rconn - the reply packet to be formatted.
397 ** 1 on success, 0 on failure.
408 struct rmp_packet *rpl, *req;
413 * Find the old connection. If one doesnt exist, create one only
414 * to return the error code.
416 if ((oldconn = FindConn(rconn)) == NULL) {
417 if ((oldconn = NewConn(rconn)) == NULL)
419 syslog(LOG_ERR, "SendReadRepl: no active connection (%s)",
424 req = &rconn->rmp; /* cache ptr to request packet */
425 rpl = &oldconn->rmp; /* cache ptr to reply packet */
427 if (madeconn) { /* no active connection above; abort */
428 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
434 * Make sure Session ID's match.
436 if (ntohs(req->r_rrq.rmp_session) !=
437 ((rpl->r_type == RMP_BOOT_REPL)? ntohs(rpl->r_brpl.rmp_session):
438 ntohs(rpl->r_rrpl.rmp_session))) {
439 syslog(LOG_ERR, "SendReadRepl: bad session id (%s)",
441 rpl->r_rrpl.rmp_retcode = RMP_E_BADSID;
447 * If the requester asks for more data than we can fit,
448 * silently clamp the request size down to RMPREADDATA.
450 * N.B. I do not know if this is "legal", however it seems
451 * to work. This is necessary for bpfwrite() on machines
452 * with MCLBYTES less than 1514.
454 if (ntohs(req->r_rrq.rmp_size) > RMPREADDATA)
455 req->r_rrq.rmp_size = htons(RMPREADDATA);
458 * Position read head on file according to info in request packet.
460 GETWORD(req->r_rrq.rmp_offset, size);
461 if (lseek(oldconn->bootfd, (off_t)size, SEEK_SET) < 0) {
462 syslog(LOG_ERR, "SendReadRepl: lseek: %m (%s)",
464 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
470 * Read data directly into reply packet.
472 if ((size = read(oldconn->bootfd, &rpl->r_rrpl.rmp_data,
473 (int) ntohs(req->r_rrq.rmp_size))) <= 0) {
475 syslog(LOG_ERR, "SendReadRepl: read: %m (%s)",
477 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
479 rpl->r_rrpl.rmp_retcode = RMP_E_EOF;
486 * Set success indication.
488 rpl->r_rrpl.rmp_retcode = RMP_E_OKAY;
492 * Set up assorted fields in reply packet.
494 rpl->r_rrpl.rmp_type = RMP_READ_REPL;
495 COPYWORD(req->r_rrq.rmp_offset, rpl->r_rrpl.rmp_offset);
496 rpl->r_rrpl.rmp_session = req->r_rrq.rmp_session;
498 oldconn->rmplen = RMPREADSIZE(size); /* set size of packet */
500 retval &= SendPacket(oldconn); /* send packet */
502 if (madeconn) /* clean up after ourself */
509 ** BootDone -- free up memory allocated for a connection.
512 ** rconn - incoming boot complete packet.
515 ** 1 on success, 0 on failure.
525 struct rmp_packet *rpl;
528 * If we cant find the connection, ignore the request.
530 if ((oldconn = FindConn(rconn)) == NULL) {
531 syslog(LOG_ERR, "BootDone: no existing connection (%s)",
536 rpl = &oldconn->rmp; /* cache ptr to RMP packet */
539 * Make sure Session ID's match.
541 if (ntohs(rconn->rmp.r_rrq.rmp_session) !=
542 ((rpl->r_type == RMP_BOOT_REPL)? ntohs(rpl->r_brpl.rmp_session):
543 ntohs(rpl->r_rrpl.rmp_session))) {
544 syslog(LOG_ERR, "BootDone: bad session id (%s)",
549 RemoveConn(oldconn); /* remove connection */
551 syslog(LOG_INFO, "%s: boot complete", EnetStr(rconn));
557 ** SendPacket -- send an RMP packet to a remote host.
560 ** rconn - packet to be sent.
563 ** 1 on success, 0 on failure.
573 * Set Ethernet Destination address to Source (BPF and the enet
574 * driver will take care of getting our source address set).
576 memmove((char *)&rconn->rmp.hp_hdr.daddr[0],
577 (char *)&rconn->rmp.hp_hdr.saddr[0], RMP_ADDRLEN);
578 rconn->rmp.hp_hdr.len = htons(rconn->rmplen - sizeof(struct hp_hdr));
581 * Reverse 802.2/HP Extended Source & Destination Access Pts.
583 rconn->rmp.hp_llc.dxsap = htons(HPEXT_SXSAP);
584 rconn->rmp.hp_llc.sxsap = htons(HPEXT_DXSAP);
587 * Last time this connection was active.
589 (void) gettimeofday(&rconn->tstamp, (struct timezone *)0);
591 if (DbgFp != NULL) /* display packet */
592 DispPkt(rconn,DIR_SENT);
595 * Send RMP packet to remote host.
597 return(BpfWrite(rconn));