]>
git.saurik.com Git - apple/network_cmds.git/blob - rbootd.tproj/rmpproto.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1988, 1992 The University of Utah and the Center
26 * for Software Science (CSS).
27 * Copyright (c) 1992, 1993
28 * The Regents of the University of California. All rights reserved.
30 * This code is derived from software contributed to Berkeley by
31 * the Center for Software Science of the University of Utah Computer
32 * Science Department. CSS requests users of this software to return
33 * to css-dist@cs.utah.edu any improvements that they make and grant
34 * CSS redistribution rights.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)rmpproto.c 8.1 (Berkeley) 6/4/93
66 * Utah $Hdr: rmpproto.c 3.1 92/07/06$
67 * Author: Jeff Forys, University of Utah CSS
71 static char sccsid
[] = "@(#)rmpproto.c 8.1 (Berkeley) 6/4/93";
74 #include <sys/param.h>
86 ** ProcessPacket -- determine packet type and do what's required.
88 ** An RMP BOOT packet has been received. Look at the type field
89 ** and process Boot Requests, Read Requests, and Boot Complete
90 ** packets. Any other type will be dropped with a warning msg.
93 ** rconn - the new connection
94 ** client - list of files available to this host
100 ** - If this is a valid boot request, it will be added to
101 ** the linked list of outstanding requests (RmpConns).
102 ** - If this is a valid boot complete, its associated
103 ** entry in RmpConns will be deleted.
104 ** - Also, unless we run out of memory, a reply will be
105 ** sent to the host that sent the packet.
108 ProcessPacket(rconn
, client
)
112 struct rmp_packet
*rmp
;
115 rmp
= &rconn
->rmp
; /* cache pointer to RMP packet */
117 switch(rmp
->r_type
) { /* do what we came here to do */
118 case RMP_BOOT_REQ
: /* boot request */
119 if ((rconnout
= NewConn(rconn
)) == NULL
)
123 * If the Session ID is 0xffff, this is a "probe"
124 * packet and we do not want to add the connection
125 * to the linked list of active connections. There
126 * are two types of probe packets, if the Sequence
127 * Number is 0 they want to know our host name, o/w
128 * they want the name of the file associated with
129 * the number spec'd by the Sequence Number.
131 * If this is an actual boot request, open the file
132 * and send a reply. If SendBootRepl() does not
133 * return 0, add the connection to the linked list
134 * of active connections, otherwise delete it since
135 * an error was encountered.
137 if (rmp
->r_brq
.rmp_session
== RMP_PROBESID
) {
138 if (WORDZE(rmp
->r_brq
.rmp_seqno
))
139 (void) SendServerID(rconnout
);
141 (void) SendFileNo(rmp
, rconnout
,
142 client
? client
->files
:
146 if (SendBootRepl(rmp
, rconnout
,
147 client
? client
->files
: BootFiles
))
154 case RMP_BOOT_REPL
: /* boot reply (not valid) */
155 syslog(LOG_WARNING
, "%s: sent a boot reply",
159 case RMP_READ_REQ
: /* read request */
161 * Send a portion of the boot file.
163 (void) SendReadRepl(rconn
);
166 case RMP_READ_REPL
: /* read reply (not valid) */
167 syslog(LOG_WARNING
, "%s: sent a read reply",
171 case RMP_BOOT_DONE
: /* boot complete */
173 * Remove the entry from the linked list of active
176 (void) BootDone(rconn
);
179 default: /* unknown RMP packet type */
180 syslog(LOG_WARNING
, "%s: unknown packet type (%u)",
181 EnetStr(rconn
), rmp
->r_type
);
186 ** SendServerID -- send our host name to who ever requested it.
189 ** rconn - the reply packet to be formatted.
192 ** 1 on success, 0 on failure.
201 register struct rmp_packet
*rpl
;
202 register char *src
, *dst
;
203 register u_char
*size
;
205 rpl
= &rconn
->rmp
; /* cache ptr to RMP packet */
208 * Set up assorted fields in reply packet.
210 rpl
->r_brpl
.rmp_type
= RMP_BOOT_REPL
;
211 rpl
->r_brpl
.rmp_retcode
= RMP_E_OKAY
;
212 ZEROWORD(rpl
->r_brpl
.rmp_seqno
);
213 rpl
->r_brpl
.rmp_session
= 0;
214 rpl
->r_brpl
.rmp_version
= RMP_VERSION
;
216 size
= &rpl
->r_brpl
.rmp_flnmsize
; /* ptr to length of host name */
219 * Copy our host name into the reply packet incrementing the
220 * length as we go. Stop at RMP_HOSTLEN or the first dot.
223 dst
= (char *) &rpl
->r_brpl
.rmp_flnm
;
224 for (*size
= 0; *size
< RMP_HOSTLEN
; (*size
)++) {
225 if (*src
== '.' || *src
== '\0')
230 rconn
->rmplen
= RMPBOOTSIZE(*size
); /* set packet length */
232 return(SendPacket(rconn
)); /* send packet */
236 ** SendFileNo -- send the name of a bootable file to the requester.
239 ** req - RMP BOOT packet containing the request.
240 ** rconn - the reply packet to be formatted.
241 ** filelist - list of files available to the requester.
244 ** 1 on success, 0 on failure.
250 SendFileNo(req
, rconn
, filelist
)
251 struct rmp_packet
*req
;
255 register struct rmp_packet
*rpl
;
256 register char *src
, *dst
;
257 register u_char
*size
, i
;
259 GETWORD(req
->r_brpl
.rmp_seqno
, i
); /* SeqNo is really FileNo */
260 rpl
= &rconn
->rmp
; /* cache ptr to RMP packet */
263 * Set up assorted fields in reply packet.
265 rpl
->r_brpl
.rmp_type
= RMP_BOOT_REPL
;
266 PUTWORD(i
, rpl
->r_brpl
.rmp_seqno
);
268 rpl
->r_brpl
.rmp_session
= 0;
269 rpl
->r_brpl
.rmp_version
= RMP_VERSION
;
271 size
= &rpl
->r_brpl
.rmp_flnmsize
; /* ptr to length of filename */
272 *size
= 0; /* init length to zero */
275 * Copy the file name into the reply packet incrementing the
276 * length as we go. Stop at end of string or when RMPBOOTDATA
277 * characters have been copied. Also, set return code to
278 * indicate success or "no more files".
280 if (i
< C_MAXFILE
&& filelist
[i
] != NULL
) {
282 dst
= (char *)&rpl
->r_brpl
.rmp_flnm
;
283 for (; *src
&& *size
< RMPBOOTDATA
; (*size
)++) {
288 rpl
->r_brpl
.rmp_retcode
= RMP_E_OKAY
;
290 rpl
->r_brpl
.rmp_retcode
= RMP_E_NODFLT
;
292 rconn
->rmplen
= RMPBOOTSIZE(*size
); /* set packet length */
294 return(SendPacket(rconn
)); /* send packet */
298 ** SendBootRepl -- open boot file and respond to boot request.
301 ** req - RMP BOOT packet containing the request.
302 ** rconn - the reply packet to be formatted.
303 ** filelist - list of files available to the requester.
306 ** 1 on success, 0 on failure.
312 SendBootRepl(req
, rconn
, filelist
)
313 struct rmp_packet
*req
;
318 char *filename
, filepath
[RMPBOOTDATA
+1];
320 register struct rmp_packet
*rpl
;
321 register char *src
, *dst1
, *dst2
;
325 * If another connection already exists, delete it since we
326 * are obviously starting again.
328 if ((oldconn
= FindConn(rconn
)) != NULL
) {
329 syslog(LOG_WARNING
, "%s: dropping existing connection",
334 rpl
= &rconn
->rmp
; /* cache ptr to RMP packet */
337 * Set up assorted fields in reply packet.
339 rpl
->r_brpl
.rmp_type
= RMP_BOOT_REPL
;
340 COPYWORD(req
->r_brq
.rmp_seqno
, rpl
->r_brpl
.rmp_seqno
);
341 rpl
->r_brpl
.rmp_session
= GenSessID();
342 rpl
->r_brpl
.rmp_version
= RMP_VERSION
;
343 rpl
->r_brpl
.rmp_flnmsize
= req
->r_brq
.rmp_flnmsize
;
346 * Copy file name to `filepath' string, and into reply packet.
348 src
= &req
->r_brq
.rmp_flnm
;
350 dst2
= &rpl
->r_brpl
.rmp_flnm
;
351 for (i
= 0; i
< req
->r_brq
.rmp_flnmsize
; i
++)
352 *dst1
++ = *dst2
++ = *src
++;
356 * If we are booting HP-UX machines, their secondary loader will
357 * ask for files like "/hp-ux". As a security measure, we do not
358 * allow boot files to lay outside the boot directory (unless they
359 * are purposely link'd out. So, make `filename' become the path-
360 * stripped file name and spoof the client into thinking that it
361 * really got what it wanted.
363 filename
= (filename
= rindex(filepath
,'/'))? ++filename
: filepath
;
366 * Check that this is a valid boot file name.
368 for (i
= 0; i
< C_MAXFILE
&& filelist
[i
] != NULL
; i
++)
369 if (STREQN(filename
, filelist
[i
]))
373 * Invalid boot file name, set error and send reply packet.
375 rpl
->r_brpl
.rmp_retcode
= RMP_E_NOFILE
;
381 * This is a valid boot file. Open the file and save the file
382 * descriptor associated with this connection and set success
383 * indication. If the file couldnt be opened, set error:
384 * "no such file or dir" - RMP_E_NOFILE
385 * "file table overflow" - RMP_E_BUSY
386 * "too many open files" - RMP_E_BUSY
387 * anything else - RMP_E_OPENFILE
389 if ((rconn
->bootfd
= open(filename
, O_RDONLY
, 0600)) < 0) {
390 rpl
->r_brpl
.rmp_retcode
= (errno
== ENOENT
)? RMP_E_NOFILE
:
391 (errno
== EMFILE
|| errno
== ENFILE
)? RMP_E_BUSY
:
395 rpl
->r_brpl
.rmp_retcode
= RMP_E_OKAY
;
400 syslog(LOG_INFO
, "%s: request to boot %s (%s)",
401 EnetStr(rconn
), filename
, retval
? "granted": "denied");
403 rconn
->rmplen
= RMPBOOTSIZE(rpl
->r_brpl
.rmp_flnmsize
);
405 return (retval
& SendPacket(rconn
));
409 ** SendReadRepl -- send a portion of the boot file to the requester.
412 ** rconn - the reply packet to be formatted.
415 ** 1 on success, 0 on failure.
426 register struct rmp_packet
*rpl
, *req
;
427 register int size
= 0;
431 * Find the old connection. If one doesnt exist, create one only
432 * to return the error code.
434 if ((oldconn
= FindConn(rconn
)) == NULL
) {
435 if ((oldconn
= NewConn(rconn
)) == NULL
)
437 syslog(LOG_ERR
, "SendReadRepl: no active connection (%s)",
442 req
= &rconn
->rmp
; /* cache ptr to request packet */
443 rpl
= &oldconn
->rmp
; /* cache ptr to reply packet */
445 if (madeconn
) { /* no active connection above; abort */
446 rpl
->r_rrpl
.rmp_retcode
= RMP_E_ABORT
;
452 * Make sure Session ID's match.
454 if (req
->r_rrq
.rmp_session
!=
455 ((rpl
->r_type
== RMP_BOOT_REPL
)? rpl
->r_brpl
.rmp_session
:
456 rpl
->r_rrpl
.rmp_session
)) {
457 syslog(LOG_ERR
, "SendReadRepl: bad session id (%s)",
459 rpl
->r_rrpl
.rmp_retcode
= RMP_E_BADSID
;
465 * If the requester asks for more data than we can fit,
466 * silently clamp the request size down to RMPREADDATA.
468 * N.B. I do not know if this is "legal", however it seems
469 * to work. This is necessary for bpfwrite() on machines
470 * with MCLBYTES less than 1514.
472 if (req
->r_rrq
.rmp_size
> RMPREADDATA
)
473 req
->r_rrq
.rmp_size
= RMPREADDATA
;
476 * Position read head on file according to info in request packet.
478 GETWORD(req
->r_rrq
.rmp_offset
, size
);
479 if (lseek(oldconn
->bootfd
, (off_t
)size
, L_SET
) < 0) {
480 syslog(LOG_ERR
, "SendReadRepl: lseek: %m (%s)",
482 rpl
->r_rrpl
.rmp_retcode
= RMP_E_ABORT
;
488 * Read data directly into reply packet.
490 if ((size
= read(oldconn
->bootfd
, &rpl
->r_rrpl
.rmp_data
,
491 (int) req
->r_rrq
.rmp_size
)) <= 0) {
493 syslog(LOG_ERR
, "SendReadRepl: read: %m (%s)",
495 rpl
->r_rrpl
.rmp_retcode
= RMP_E_ABORT
;
497 rpl
->r_rrpl
.rmp_retcode
= RMP_E_EOF
;
504 * Set success indication.
506 rpl
->r_rrpl
.rmp_retcode
= RMP_E_OKAY
;
510 * Set up assorted fields in reply packet.
512 rpl
->r_rrpl
.rmp_type
= RMP_READ_REPL
;
513 COPYWORD(req
->r_rrq
.rmp_offset
, rpl
->r_rrpl
.rmp_offset
);
514 rpl
->r_rrpl
.rmp_session
= req
->r_rrq
.rmp_session
;
516 oldconn
->rmplen
= RMPREADSIZE(size
); /* set size of packet */
518 retval
&= SendPacket(oldconn
); /* send packet */
520 if (madeconn
) /* clean up after ourself */
527 ** BootDone -- free up memory allocated for a connection.
530 ** rconn - incoming boot complete packet.
533 ** 1 on success, 0 on failure.
543 struct rmp_packet
*rpl
;
546 * If we cant find the connection, ignore the request.
548 if ((oldconn
= FindConn(rconn
)) == NULL
) {
549 syslog(LOG_ERR
, "BootDone: no existing connection (%s)",
554 rpl
= &oldconn
->rmp
; /* cache ptr to RMP packet */
557 * Make sure Session ID's match.
559 if (rconn
->rmp
.r_rrq
.rmp_session
!=
560 ((rpl
->r_type
== RMP_BOOT_REPL
)? rpl
->r_brpl
.rmp_session
:
561 rpl
->r_rrpl
.rmp_session
)) {
562 syslog(LOG_ERR
, "BootDone: bad session id (%s)",
567 RemoveConn(oldconn
); /* remove connection */
569 syslog(LOG_INFO
, "%s: boot complete", EnetStr(rconn
));
575 ** SendPacket -- send an RMP packet to a remote host.
578 ** rconn - packet to be sent.
581 ** 1 on success, 0 on failure.
588 register RMPCONN
*rconn
;
591 * Set Ethernet Destination address to Source (BPF and the enet
592 * driver will take care of getting our source address set).
594 bcopy((char *)&rconn
->rmp
.hp_hdr
.saddr
[0],
595 (char *)&rconn
->rmp
.hp_hdr
.daddr
[0], RMP_ADDRLEN
);
596 rconn
->rmp
.hp_hdr
.len
= rconn
->rmplen
- sizeof(struct hp_hdr
);
599 * Reverse 802.2/HP Extended Source & Destination Access Pts.
601 rconn
->rmp
.hp_llc
.dxsap
= HPEXT_SXSAP
;
602 rconn
->rmp
.hp_llc
.sxsap
= HPEXT_DXSAP
;
605 * Last time this connection was active.
607 (void) gettimeofday(&rconn
->tstamp
, (struct timezone
*)0);
609 if (DbgFp
!= NULL
) /* display packet */
610 DispPkt(rconn
,DIR_SENT
);
613 * Send RMP packet to remote host.
615 return(BpfWrite(rconn
));