rmpproto.c revision 1.12 1 /* $NetBSD: rmpproto.c,v 1.12 2001/01/16 02:41:18 cgd 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 (at) 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.12 2001/01/16 02:41:18 cgd 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 if ((filename = strrchr(filepath,'/')) != NULL)
355 filename++;
356 else
357 filename = filepath;
358
359 /*
360 * Check that this is a valid boot file name.
361 */
362 for (i = 0; i < C_MAXFILE && filelist[i] != NULL; i++)
363 if (STREQN(filename, filelist[i]))
364 goto match;
365
366 /*
367 * Invalid boot file name, set error and send reply packet.
368 */
369 rpl->r_brpl.rmp_retcode = RMP_E_NOFILE;
370 retval = 0;
371 goto sendpkt;
372
373 match:
374 /*
375 * This is a valid boot file. Open the file and save the file
376 * descriptor associated with this connection and set success
377 * indication. If the file couldnt be opened, set error:
378 * "no such file or dir" - RMP_E_NOFILE
379 * "file table overflow" - RMP_E_BUSY
380 * "too many open files" - RMP_E_BUSY
381 * anything else - RMP_E_OPENFILE
382 */
383 if ((rconn->bootfd = open(filename, O_RDONLY, 0600)) < 0) {
384 rpl->r_brpl.rmp_retcode = (errno == ENOENT)? RMP_E_NOFILE:
385 (errno == EMFILE || errno == ENFILE)? RMP_E_BUSY:
386 RMP_E_OPENFILE;
387 retval = 0;
388 } else {
389 rpl->r_brpl.rmp_retcode = RMP_E_OKAY;
390 retval = 1;
391 }
392
393 sendpkt:
394 syslog(LOG_INFO, "%s: request to boot %s (%s)",
395 EnetStr(rconn), filename, retval? "granted": "denied");
396
397 rconn->rmplen = RMPBOOTSIZE(rpl->r_brpl.rmp_flnmsize);
398
399 return (retval & SendPacket(rconn));
400 }
401
402 /*
403 ** SendReadRepl -- send a portion of the boot file to the requester.
404 **
405 ** Parameters:
406 ** rconn - the reply packet to be formatted.
407 **
408 ** Returns:
409 ** 1 on success, 0 on failure.
410 **
411 ** Side Effects:
412 ** none.
413 */
414 int
415 SendReadRepl(rconn)
416 RMPCONN *rconn;
417 {
418 int retval = 0;
419 RMPCONN *oldconn;
420 struct rmp_packet *rpl, *req;
421 int size = 0;
422 int madeconn = 0;
423
424 /*
425 * Find the old connection. If one doesnt exist, create one only
426 * to return the error code.
427 */
428 if ((oldconn = FindConn(rconn)) == NULL) {
429 if ((oldconn = NewConn(rconn)) == NULL)
430 return(0);
431 syslog(LOG_ERR, "SendReadRepl: no active connection (%s)",
432 EnetStr(rconn));
433 madeconn++;
434 }
435
436 req = &rconn->rmp; /* cache ptr to request packet */
437 rpl = &oldconn->rmp; /* cache ptr to reply packet */
438
439 if (madeconn) { /* no active connection above; abort */
440 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
441 retval = 1;
442 goto sendpkt;
443 }
444
445 /*
446 * Make sure Session ID's match.
447 */
448 if (ntohs(req->r_rrq.rmp_session) !=
449 ((rpl->r_type == RMP_BOOT_REPL)? ntohs(rpl->r_brpl.rmp_session):
450 ntohs(rpl->r_rrpl.rmp_session))) {
451 syslog(LOG_ERR, "SendReadRepl: bad session id (%s)",
452 EnetStr(rconn));
453 rpl->r_rrpl.rmp_retcode = RMP_E_BADSID;
454 retval = 1;
455 goto sendpkt;
456 }
457
458 /*
459 * If the requester asks for more data than we can fit,
460 * silently clamp the request size down to RMPREADDATA.
461 *
462 * N.B. I do not know if this is "legal", however it seems
463 * to work. This is necessary for bpfwrite() on machines
464 * with MCLBYTES less than 1514.
465 */
466 if (ntohs(req->r_rrq.rmp_size) > RMPREADDATA)
467 req->r_rrq.rmp_size = htons(RMPREADDATA);
468
469 /*
470 * Position read head on file according to info in request packet.
471 */
472 GETWORD(req->r_rrq.rmp_offset, size);
473 if (lseek(oldconn->bootfd, (off_t)size, SEEK_SET) < 0) {
474 syslog(LOG_ERR, "SendReadRepl: lseek: %m (%s)",
475 EnetStr(rconn));
476 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
477 retval = 1;
478 goto sendpkt;
479 }
480
481 /*
482 * Read data directly into reply packet.
483 */
484 if ((size = read(oldconn->bootfd, &rpl->r_rrpl.rmp_data,
485 (int) ntohs(req->r_rrq.rmp_size))) <= 0) {
486 if (size < 0) {
487 syslog(LOG_ERR, "SendReadRepl: read: %m (%s)",
488 EnetStr(rconn));
489 rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
490 } else {
491 rpl->r_rrpl.rmp_retcode = RMP_E_EOF;
492 }
493 retval = 1;
494 goto sendpkt;
495 }
496
497 /*
498 * Set success indication.
499 */
500 rpl->r_rrpl.rmp_retcode = RMP_E_OKAY;
501
502 sendpkt:
503 /*
504 * Set up assorted fields in reply packet.
505 */
506 rpl->r_rrpl.rmp_type = RMP_READ_REPL;
507 COPYWORD(req->r_rrq.rmp_offset, rpl->r_rrpl.rmp_offset);
508 rpl->r_rrpl.rmp_session = req->r_rrq.rmp_session;
509
510 oldconn->rmplen = RMPREADSIZE(size); /* set size of packet */
511
512 retval &= SendPacket(oldconn); /* send packet */
513
514 if (madeconn) /* clean up after ourself */
515 FreeConn(oldconn);
516
517 return (retval);
518 }
519
520 /*
521 ** BootDone -- free up memory allocated for a connection.
522 **
523 ** Parameters:
524 ** rconn - incoming boot complete packet.
525 **
526 ** Returns:
527 ** 1 on success, 0 on failure.
528 **
529 ** Side Effects:
530 ** none.
531 */
532 int
533 BootDone(rconn)
534 RMPCONN *rconn;
535 {
536 RMPCONN *oldconn;
537 struct rmp_packet *rpl;
538
539 /*
540 * If we cant find the connection, ignore the request.
541 */
542 if ((oldconn = FindConn(rconn)) == NULL) {
543 syslog(LOG_ERR, "BootDone: no existing connection (%s)",
544 EnetStr(rconn));
545 return(0);
546 }
547
548 rpl = &oldconn->rmp; /* cache ptr to RMP packet */
549
550 /*
551 * Make sure Session ID's match.
552 */
553 if (ntohs(rconn->rmp.r_rrq.rmp_session) !=
554 ((rpl->r_type == RMP_BOOT_REPL)? ntohs(rpl->r_brpl.rmp_session):
555 ntohs(rpl->r_rrpl.rmp_session))) {
556 syslog(LOG_ERR, "BootDone: bad session id (%s)",
557 EnetStr(rconn));
558 return(0);
559 }
560
561 RemoveConn(oldconn); /* remove connection */
562
563 syslog(LOG_INFO, "%s: boot complete", EnetStr(rconn));
564
565 return(1);
566 }
567
568 /*
569 ** SendPacket -- send an RMP packet to a remote host.
570 **
571 ** Parameters:
572 ** rconn - packet to be sent.
573 **
574 ** Returns:
575 ** 1 on success, 0 on failure.
576 **
577 ** Side Effects:
578 ** none.
579 */
580 int
581 SendPacket(rconn)
582 RMPCONN *rconn;
583 {
584 /*
585 * Set Ethernet Destination address to Source (BPF and the enet
586 * driver will take care of getting our source address set).
587 */
588 memmove((char *)&rconn->rmp.hp_hdr.daddr[0],
589 (char *)&rconn->rmp.hp_hdr.saddr[0], RMP_ADDRLEN);
590 #ifdef __FreeBSD__
591 /* BPF (incorrectly) wants this in host order. */
592 rconn->rmp.hp_hdr.len = rconn->rmplen - sizeof(struct hp_hdr);
593 #else
594 rconn->rmp.hp_hdr.len = htons(rconn->rmplen - sizeof(struct hp_hdr));
595 #endif
596
597 /*
598 * Reverse 802.2/HP Extended Source & Destination Access Pts.
599 */
600 rconn->rmp.hp_llc.dxsap = htons(HPEXT_SXSAP);
601 rconn->rmp.hp_llc.sxsap = htons(HPEXT_DXSAP);
602
603 /*
604 * Last time this connection was active.
605 */
606 (void) gettimeofday(&rconn->tstamp, (struct timezone *)0);
607
608 if (DbgFp != NULL) /* display packet */
609 DispPkt(rconn,DIR_SENT);
610
611 /*
612 * Send RMP packet to remote host.
613 */
614 return(BpfWrite(rconn));
615 }
616