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[] =
55 #include <sys/param.h>
57 #include <netinet/in.h>
68 ** ProcessPacket -- determine packet type and do what's required.
70 ** An RMP BOOT packet has been received. Look at the type field
71 ** and process Boot Requests, Read Requests, and Boot Complete
72 ** packets. Any other type will be dropped with a warning msg.
75 ** rconn - the new connection
76 ** client - list of files available to this host
82 ** - If this is a valid boot request, it will be added to
83 ** the linked list of outstanding requests (RmpConns).
84 ** - If this is a valid boot complete, its associated
85 ** entry in RmpConns will be deleted.
86 ** - Also, unless we run out of memory, a reply will be
87 ** sent to the host that sent the packet.
90 ProcessPacket(RMPCONN *rconn, CLIENT *client)
92 struct rmp_packet *rmp;
95 rmp = &rconn->rmp; /* cache pointer to RMP packet */
97 switch(rmp->r_type) { /* do what we came here to do */
98 case RMP_BOOT_REQ: /* boot request */
99 if ((rconnout = NewConn(rconn)) == NULL)
103 * If the Session ID is 0xffff, this is a "probe"
104 * packet and we do not want to add the connection
105 * to the linked list of active connections. There
106 * are two types of probe packets, if the Sequence
107 * Number is 0 they want to know our host name, o/w
108 * they want the name of the file associated with
109 * the number spec'd by the Sequence Number.
111 * If this is an actual boot request, open the file
112 * and send a reply. If SendBootRepl() does not
113 * return 0, add the connection to the linked list
114 * of active connections, otherwise delete it since
115 * an error was encountered.
117 if (ntohs(rmp->r_brq.rmp_session) == RMP_PROBESID) {
118 if (WORDZE(rmp->r_brq.rmp_seqno))
119 (void) SendServerID(rconnout);
121 (void) SendFileNo(rmp, rconnout,
122 client? client->files:
126 if (SendBootRepl(rmp, rconnout,
127 client? client->files: BootFiles))
134 case RMP_BOOT_REPL: /* boot reply (not valid) */
135 syslog(LOG_WARNING, "%s: sent a boot reply",
139 case RMP_READ_REQ: /* read request */
141 * Send a portion of the boot file.
143 (void) SendReadRepl(rconn);
146 case RMP_READ_REPL: /* read reply (not valid) */
147 syslog(LOG_WARNING, "%s: sent a read reply",
151 case RMP_BOOT_DONE: /* boot complete */
153 * Remove the entry from the linked list of active
156 (void) BootDone(rconn);
159 default: /* unknown RMP packet type */
160 syslog(LOG_WARNING, "%s: unknown packet type (%u)",
161 EnetStr(rconn), rmp->r_type);
166 ** SendServerID -- send our host name to who ever requested it.
169 ** rconn - the reply packet to be formatted.
172 ** 1 on success, 0 on failure.
178 SendServerID(RMPCONN *rconn)
180 struct rmp_packet *rpl;
184 rpl = &rconn->rmp; /* cache ptr to RMP packet */
187 * Set up assorted fields in reply packet.
189 rpl->r_brpl.rmp_type = RMP_BOOT_REPL;
190 rpl->r_brpl.rmp_retcode = RMP_E_OKAY;
191 ZEROWORD(rpl->r_brpl.rmp_seqno);
192 rpl->r_brpl.rmp_session = 0;
193 rpl->r_brpl.rmp_version = htons(RMP_VERSION);
195 size = &rpl->r_brpl.rmp_flnmsize; /* ptr to length of host name */
198 * Copy our host name into the reply packet incrementing the
199 * length as we go. Stop at RMP_HOSTLEN or the first dot.
202 dst = (char *) &rpl->r_brpl.rmp_flnm;
203 for (*size = 0; *size < RMP_HOSTLEN; (*size)++) {
204 if (*src == '.' || *src == '\0')
209 rconn->rmplen = RMPBOOTSIZE(*size); /* set packet length */
211 return(SendPacket(rconn)); /* send packet */
215 ** SendFileNo -- send the name of a bootable file to the requester.
218 ** req - RMP BOOT packet containing the request.
219 ** rconn - the reply packet to be formatted.
220 ** filelist - list of files available to the requester.
223 ** 1 on success, 0 on failure.
229 SendFileNo(struct rmp_packet *req, RMPCONN *rconn, char *filelist[])
231 struct rmp_packet *rpl;
236 GETWORD(req->r_brpl.rmp_seqno, i); /* SeqNo is really FileNo */
237 rpl = &rconn->rmp; /* cache ptr to RMP packet */
240 * Set up assorted fields in reply packet.
242 rpl->r_brpl.rmp_type = RMP_BOOT_REPL;
243 PUTWORD(i, rpl->r_brpl.rmp_seqno);
245 rpl->r_brpl.rmp_session = 0;
246 rpl->r_brpl.rmp_version = htons(RMP_VERSION);
248 size = &rpl->r_brpl.rmp_flnmsize; /* ptr to length of filename */
249 *size = 0; /* init length to zero */
252 * Copy the file name into the reply packet incrementing the
253 * length as we go. Stop at end of string or when RMPBOOTDATA
254 * characters have been copied. Also, set return code to
255 * indicate success or "no more files".
257 if (i < C_MAXFILE && filelist[i] != NULL) {
259 dst = (char *)&rpl->r_brpl.rmp_flnm;
260 for (; *src && *size < RMPBOOTDATA; (*size)++) {
265 rpl->r_brpl.rmp_retcode = RMP_E_OKAY;
267 rpl->r_brpl.rmp_retcode = RMP_E_NODFLT;
269 rconn->rmplen = RMPBOOTSIZE(*size); /* set packet length */
271 return(SendPacket(rconn)); /* send packet */
275 ** SendBootRepl -- open boot file and respond to boot request.
278 ** req - RMP BOOT packet containing the request.
279 ** rconn - the reply packet to be formatted.
280 ** filelist - list of files available to the requester.
283 ** 1 on success, 0 on failure.
289 SendBootRepl(struct rmp_packet *req, RMPCONN *rconn, char *filelist[])
292 char *filename, filepath[RMPBOOTDATA+1];
294 struct rmp_packet *rpl;
295 char *src, *dst1, *dst2;
299 * If another connection already exists, delete it since we
300 * are obviously starting again.
302 if ((oldconn = FindConn(rconn)) != NULL) {
303 syslog(LOG_WARNING, "%s: dropping existing connection",
308 rpl = &rconn->rmp; /* cache ptr to RMP packet */
311 * Set up assorted fields in reply packet.
313 rpl->r_brpl.rmp_type = RMP_BOOT_REPL;
314 COPYWORD(req->r_brq.rmp_seqno, rpl->r_brpl.rmp_seqno);
315 rpl->r_brpl.rmp_session = htons(GenSessID());
316 rpl->r_brpl.rmp_version = htons(RMP_VERSION);
317 rpl->r_brpl.rmp_flnmsize = req->r_brq.rmp_flnmsize;
320 * Copy file name to `filepath' string, and into reply packet.
322 src = &req->r_brq.rmp_flnm;
324 dst2 = &rpl->r_brpl.rmp_flnm;
325 for (i = 0; i < req->r_brq.rmp_flnmsize; i++)
326 *dst1++ = *dst2++ = *src++;
330 * If we are booting HP-UX machines, their secondary loader will
331 * ask for files like "/hp-ux". As a security measure, we do not
332 * allow boot files to lay outside the boot directory (unless they
333 * are purposely link'd out. So, make `filename' become the path-
334 * stripped file name and spoof the client into thinking that it
335 * really got what it wanted.
337 filename = (filename = strrchr(filepath,'/'))? ++filename: filepath;
340 * Check that this is a valid boot file name.
342 for (i = 0; i < C_MAXFILE && filelist[i] != NULL; i++)
343 if (STREQN(filename, filelist[i]))
347 * Invalid boot file name, set error and send reply packet.
349 rpl->r_brpl.rmp_retcode = RMP_E_NOFILE;
355 * This is a valid boot file. Open the file and save the file
356 * descriptor associated with this connection and set success
357 * indication. If the file couldnt be opened, set error:
358 * "no such file or dir" - RMP_E_NOFILE
359 * "file table overflow" - RMP_E_BUSY
360 * "too many open files" - RMP_E_BUSY
361 * anything else - RMP_E_OPENFILE
363 if ((rconn->bootfd = open(filename, O_RDONLY, 0600)) < 0) {
364 rpl->r_brpl.rmp_retcode = (errno == ENOENT)? RMP_E_NOFILE:
365 (errno == EMFILE || errno == ENFILE)? RMP_E_BUSY:
369 rpl->r_brpl.rmp_retcode = RMP_E_OKAY;
374 syslog(LOG_INFO, "%s: request to boot %s (%s)",
375 EnetStr(rconn), filename, retval? "granted": "denied");
377 rconn->rmplen = RMPBOOTSIZE(rpl->r_brpl.rmp_flnmsize);
379 return (retval & SendPacket(rconn));
383 ** SendReadRepl -- send a portion of the boot file to the requester.
386 ** rconn - the reply packet to be formatted.
389 ** 1 on success, 0 on failure.
395 SendReadRepl(RMPCONN *rconn)
399 struct rmp_packet *rpl, *req;
404 * Find the old connection. If one doesnt exist, create one only
405 * to return the error code.
407 if ((oldconn = FindConn(rconn)) == NULL) {
408 if ((oldconn = NewConn(rconn)) == NULL)
410 syslog(LOG_ERR, "SendReadRepl: no active connection (%s)",
415 req = &rconn->rmp; /* cache ptr to request packet */
416 rpl = &oldconn->rmp; /* cache ptr to reply packet */
418 if (madeconn) { /* no active connection above; abort */
419 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
425 * Make sure Session ID's match.
427 if (ntohs(req->r_rrq.rmp_session) !=
428 ((rpl->r_type == RMP_BOOT_REPL)? ntohs(rpl->r_brpl.rmp_session):
429 ntohs(rpl->r_rrpl.rmp_session))) {
430 syslog(LOG_ERR, "SendReadRepl: bad session id (%s)",
432 rpl->r_rrpl.rmp_retcode = RMP_E_BADSID;
438 * If the requester asks for more data than we can fit,
439 * silently clamp the request size down to RMPREADDATA.
441 * N.B. I do not know if this is "legal", however it seems
442 * to work. This is necessary for bpfwrite() on machines
443 * with MCLBYTES less than 1514.
445 if (ntohs(req->r_rrq.rmp_size) > RMPREADDATA)
446 req->r_rrq.rmp_size = htons(RMPREADDATA);
449 * Position read head on file according to info in request packet.
451 GETWORD(req->r_rrq.rmp_offset, size);
452 if (lseek(oldconn->bootfd, (off_t)size, SEEK_SET) < 0) {
453 syslog(LOG_ERR, "SendReadRepl: lseek: %m (%s)",
455 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
461 * Read data directly into reply packet.
463 if ((size = read(oldconn->bootfd, &rpl->r_rrpl.rmp_data,
464 (int) ntohs(req->r_rrq.rmp_size))) <= 0) {
466 syslog(LOG_ERR, "SendReadRepl: read: %m (%s)",
468 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
470 rpl->r_rrpl.rmp_retcode = RMP_E_EOF;
477 * Set success indication.
479 rpl->r_rrpl.rmp_retcode = RMP_E_OKAY;
483 * Set up assorted fields in reply packet.
485 rpl->r_rrpl.rmp_type = RMP_READ_REPL;
486 COPYWORD(req->r_rrq.rmp_offset, rpl->r_rrpl.rmp_offset);
487 rpl->r_rrpl.rmp_session = req->r_rrq.rmp_session;
489 oldconn->rmplen = RMPREADSIZE(size); /* set size of packet */
491 retval &= SendPacket(oldconn); /* send packet */
493 if (madeconn) /* clean up after ourself */
500 ** BootDone -- free up memory allocated for a connection.
503 ** rconn - incoming boot complete packet.
506 ** 1 on success, 0 on failure.
512 BootDone(RMPCONN *rconn)
515 struct rmp_packet *rpl;
518 * If we cant find the connection, ignore the request.
520 if ((oldconn = FindConn(rconn)) == NULL) {
521 syslog(LOG_ERR, "BootDone: no existing connection (%s)",
526 rpl = &oldconn->rmp; /* cache ptr to RMP packet */
529 * Make sure Session ID's match.
531 if (ntohs(rconn->rmp.r_rrq.rmp_session) !=
532 ((rpl->r_type == RMP_BOOT_REPL)? ntohs(rpl->r_brpl.rmp_session):
533 ntohs(rpl->r_rrpl.rmp_session))) {
534 syslog(LOG_ERR, "BootDone: bad session id (%s)",
539 RemoveConn(oldconn); /* remove connection */
541 syslog(LOG_INFO, "%s: boot complete", EnetStr(rconn));
547 ** SendPacket -- send an RMP packet to a remote host.
550 ** rconn - packet to be sent.
553 ** 1 on success, 0 on failure.
559 SendPacket(RMPCONN *rconn)
562 * Set Ethernet Destination address to Source (BPF and the enet
563 * driver will take care of getting our source address set).
565 memmove((char *)&rconn->rmp.hp_hdr.daddr[0],
566 (char *)&rconn->rmp.hp_hdr.saddr[0], RMP_ADDRLEN);
567 rconn->rmp.hp_hdr.len = htons(rconn->rmplen - sizeof(struct hp_hdr));
570 * Reverse 802.2/HP Extended Source & Destination Access Pts.
572 rconn->rmp.hp_llc.dxsap = htons(HPEXT_SXSAP);
573 rconn->rmp.hp_llc.sxsap = htons(HPEXT_DXSAP);
576 * Last time this connection was active.
578 (void) gettimeofday(&rconn->tstamp, (struct timezone *)0);
580 if (DbgFp != NULL) /* display packet */
581 DispPkt(rconn,DIR_SENT);
584 * Send RMP packet to remote host.
586 return(BpfWrite(rconn));