1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
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@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. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * from: Utah Hdr: rmp_var.h 3.1 92/07/06
40 * Author: Jeff Forys, University of Utah CSS
41 */
42
43/*
44 *  Possible values for "rmp_type" fields.
45 */
46
47#define	RMP_BOOT_REQ	1	/* boot request packet */
48#define	RMP_BOOT_REPL	129	/* boot reply packet */
49#define	RMP_READ_REQ	2	/* read request packet */
50#define	RMP_READ_REPL	130	/* read reply packet */
51#define	RMP_BOOT_DONE	3	/* boot complete packet */
52
53/*
54 *  Useful constants.
55 */
56
57#define RMP_VERSION	2	/* protocol version */
58#define RMP_TIMEOUT	600	/* timeout connection after ten minutes */
59#define	RMP_PROBESID	0xffff	/* session ID for probes */
60#define	RMP_HOSTLEN	13	/* max length of server's name */
61#define	RMP_MACHLEN	20	/* length of machine type field */
62
63/*
64 *  RMP error codes
65 */
66
67#define	RMP_E_OKAY	0
68#define	RMP_E_EOF	2	/* read reply: returned end of file */
69#define	RMP_E_ABORT	3	/* abort operation */
70#define	RMP_E_BUSY	4	/* boot reply: server busy */
71#define	RMP_E_TIMEOUT	5	/* lengthen time out (not implemented) */
72#define	RMP_E_NOFILE	16	/* boot reply: file does not exist */
73#define RMP_E_OPENFILE	17	/* boot reply: file open failed */
74#define	RMP_E_NODFLT	18	/* boot reply: default file does not exist */
75#define RMP_E_OPENDFLT	19	/* boot reply: default file open failed */
76#define	RMP_E_BADSID	25	/* read reply: bad session ID */
77#define RMP_E_BADPACKET	27 	/* Bad packet detected */
78
79/*
80 *  RMPDATALEN is the maximum number of data octets that can be stuffed
81 *  into an RMP packet.  This excludes the 802.2 LLC w/HP extensions.
82 */
83#define RMPDATALEN	(RMP_MAX_PACKET - (sizeof(struct hp_hdr) + \
84			                   sizeof(struct hp_llc)))
85
86/*
87 *  Define sizes of packets we send.  Boot and Read replies are variable
88 *  in length depending on the length of `s'.
89 *
90 *  Also, define how much space `restofpkt' can take up for outgoing
91 *  Boot and Read replies.  Boot Request packets are effectively
92 *  limited to 255 bytes due to the preceding 1-byte length field.
93 */
94
95#define	RMPBOOTSIZE(s)	(sizeof(struct hp_hdr) + sizeof(struct hp_llc) + \
96			 sizeof(struct rmp_boot_repl) + s - sizeof(restofpkt))
97#define	RMPREADSIZE(s)	(sizeof(struct hp_hdr) + sizeof(struct hp_llc) + \
98			 sizeof(struct rmp_read_repl) + s - sizeof(restofpkt) \
99			 - sizeof(u_int8_t))
100#define	RMPDONESIZE	(sizeof(struct hp_hdr) + sizeof(struct hp_llc) + \
101			 sizeof(struct rmp_boot_done))
102#define	RMPBOOTDATA	255
103#define	RMPREADDATA	(RMPDATALEN - \
104			 (2*sizeof(u_int8_t)+sizeof(u_int16_t)+sizeof(u_word)))
105
106/*
107 * This protocol defines some field sizes as "rest of ethernet packet".
108 * There is no easy way to specify this in C, so we use a one character
109 * field to denote it, and index past it to the end of the packet.
110 */
111
112typedef char	restofpkt;
113
114/*
115 * Due to the RMP packet layout, we'll run into alignment problems
116 * on machines that can't access (or don't, by default, align) words
117 * on half-word boundaries.  If you know that your machine does not suffer
118 * from this problem, add it to the vax/tahoe/m68k #define below.
119 *
120 * The following macros are used to deal with this problem:
121 *	WORDZE(w)	Return True if u_word `w' is zero, False otherwise.
122 *	ZEROWORD(w)	Set u_word `w' to zero.
123 *	COPYWORD(w1,w2)	Copy u_word `w1' to `w2'.
124 *	GETWORD(w,i)	Copy u_word `w' into int `i'.
125 *	PUTWORD(i,w)	Copy int `i' into u_word `w'.
126 *
127 * N.B. Endianness is handled by use of ntohl/htonl
128 */
129#if defined(__vax__) || defined(__tahoe__) || defined(__m68k__)
130
131typedef	u_int32_t	u_word;
132
133#define	WORDZE(w)	((w) == 0)
134#define	ZEROWORD(w)	(w) = 0
135#define	COPYWORD(w1,w2)	(w2) = (w1)
136#define	GETWORD(w, i)	(i) = ntohl(w)
137#define	PUTWORD(i, w)	(w) = htonl(i)
138
139#else
140
141#define	_WORD_HIGHPART	0
142#define	_WORD_LOWPART	1
143
144typedef	struct _uword { u_int16_t val[2]; }	u_word;
145
146#define	WORDZE(w) \
147	((w.val[_WORD_HIGHPART] == 0) && (w.val[_WORD_LOWPART] == 0))
148#define	ZEROWORD(w) \
149	(w).val[_WORD_HIGHPART] = (w).val[_WORD_LOWPART] = 0
150#define	COPYWORD(w1, w2) \
151	{ (w2).val[_WORD_HIGHPART] = (w1).val[_WORD_HIGHPART]; \
152	  (w2).val[_WORD_LOWPART] = (w1).val[_WORD_LOWPART]; \
153	}
154#define	GETWORD(w, i) \
155	(i) = (((u_int32_t)ntohs((w).val[_WORD_HIGHPART])) << 16) | ntohs((w).val[_WORD_LOWPART])
156#define	PUTWORD(i, w) \
157	{ (w).val[_WORD_HIGHPART] = htons((u_int16_t) ((i >> 16) & 0xffff)); \
158	  (w).val[_WORD_LOWPART] = htons((u_int16_t) (i & 0xffff)); \
159	}
160
161#endif
162
163/*
164 * Packet structures.
165 */
166
167struct rmp_raw {		/* generic RMP packet */
168	u_int8_t  rmp_type;		/* packet type */
169	u_int8_t  rmp_rawdata[RMPDATALEN-1];
170};
171
172struct rmp_boot_req {		/* boot request */
173	u_int8_t  rmp_type;		/* packet type (RMP_BOOT_REQ) */
174	u_int8_t  rmp_retcode;		/* return code (0) */
175	u_word	  rmp_seqno;		/* sequence number (real time clock) */
176	u_int16_t rmp_session;		/* session id (normally 0) */
177	u_int16_t rmp_version;		/* protocol version (RMP_VERSION) */
178	char	  rmp_machtype[RMP_MACHLEN];	/* machine type */
179	u_int8_t  rmp_flnmsize;		/* length of rmp_flnm */
180	restofpkt rmp_flnm;		/* name of file to be read */
181};
182
183struct rmp_boot_repl {		/* boot reply */
184	u_int8_t  rmp_type;		/* packet type (RMP_BOOT_REPL) */
185	u_int8_t  rmp_retcode;		/* return code (normally 0) */
186	u_word	  rmp_seqno;		/* sequence number (from boot req) */
187	u_int16_t rmp_session;		/* session id (generated) */
188	u_int16_t rmp_version;		/* protocol version (RMP_VERSION) */
189	u_int8_t  rmp_flnmsize;		/* length of rmp_flnm */
190	restofpkt rmp_flnm;		/* name of file (from boot req) */
191};
192
193struct rmp_read_req {		/* read request */
194	u_int8_t  rmp_type;		/* packet type (RMP_READ_REQ) */
195	u_int8_t  rmp_retcode;		/* return code (0) */
196	u_word	  rmp_offset;		/* file relative byte offset */
197	u_int16_t rmp_session;		/* session id (from boot repl) */
198	u_int16_t rmp_size;		/* max no of bytes to send */
199};
200
201struct rmp_read_repl {		/* read reply */
202	u_int8_t  rmp_type;		/* packet type (RMP_READ_REPL) */
203	u_int8_t  rmp_retcode;		/* return code (normally 0) */
204	u_word	  rmp_offset;		/* byte offset (from read req) */
205	u_int16_t rmp_session;		/* session id (from read req) */
206	restofpkt rmp_data;		/* data (max size from read req) */
207	u_int8_t  rmp_unused;		/* padding to 16-bit boundary */
208};
209
210struct rmp_boot_done {		/* boot complete */
211	u_int8_t  rmp_type;		/* packet type (RMP_BOOT_DONE) */
212	u_int8_t  rmp_retcode;		/* return code (0) */
213	u_word	  rmp_unused;		/* not used (0) */
214	u_int16_t rmp_session;		/* session id (from read repl) */
215};
216
217struct rmp_packet {
218	struct hp_hdr hp_hdr;
219	struct hp_llc hp_llc;
220	union {
221		struct rmp_boot_req	rmp_brq;	/* boot request */
222		struct rmp_boot_repl	rmp_brpl;	/* boot reply */
223		struct rmp_read_req	rmp_rrq;	/* read request */
224		struct rmp_read_repl	rmp_rrpl;	/* read reply */
225		struct rmp_boot_done	rmp_done;	/* boot complete */
226		struct rmp_raw		rmp_raw;	/* raw data */
227	} rmp_proto;
228};
229
230/*
231 *  Make life easier...
232 */
233
234#define	r_type	rmp_proto.rmp_raw.rmp_type
235#define	r_data	rmp_proto.rmp_raw.rmp_rawdata
236#define	r_brq	rmp_proto.rmp_brq
237#define	r_brpl	rmp_proto.rmp_brpl
238#define	r_rrq	rmp_proto.rmp_rrq
239#define	r_rrpl	rmp_proto.rmp_rrpl
240#define	r_done	rmp_proto.rmp_done
241