1 /* $NetBSD: bootparam.c,v 1.11 1997/06/26 19:11:32 drochner Exp $ */
4 * Copyright (c) 1995 Gordon W. Ross
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 * 4. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Gordon W. Ross
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/param.h>
38 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
53 #include "bootparam.h"
56 #define RPC_PRINTF(a) printf a
61 struct in_addr bp_server_addr; /* net order */
62 n_short bp_server_port; /* net order */
65 * RPC definitions for bootparamd
67 #define BOOTPARAM_PROG 100026
68 #define BOOTPARAM_VERS 1
69 #define BOOTPARAM_WHOAMI 1
70 #define BOOTPARAM_GETFILE 2
73 * Inet address in RPC messages
74 * (Note, really four ints, NOT chars. Blech.)
81 int xdr_inaddr_encode(char **p, struct in_addr ia);
82 int xdr_inaddr_decode(char **p, struct in_addr *ia);
84 int xdr_string_encode(char **p, char *str, int len);
85 int xdr_string_decode(char **p, char *str, int *len_p);
89 * RPC: bootparam/whoami
90 * Given client IP address, get:
91 * client name (hostname)
92 * domain name (domainname)
95 * The hostname and domainname are set here for convenience.
97 * Note - bpsin is initialized to the broadcast address,
98 * and will be replaced with the bootparam server address
99 * after this call is complete. Have to use PMAP_PROC_CALL
100 * to make sure we get responses only from a servers that
101 * know about us (don't want to broadcast a getport call).
107 /* RPC structures for PMAPPROC_CALLIT */
113 struct xdr_inaddr xina;
119 /* encapsulated data here */
123 n_long h[RPC_HEADER_WORDS];
127 n_long h[RPC_HEADER_WORDS];
130 char *send_tail, *recv_head;
134 RPC_PRINTF(("bp_whoami: myip=%s\n", inet_ntoa(myip)));
136 if (!(d = socktodesc(sockfd))) {
137 RPC_PRINTF(("bp_whoami: bad socket. %d\n", sockfd));
144 * Build request args for PMAPPROC_CALLIT.
146 args->prog = htonl(BOOTPARAM_PROG);
147 args->vers = htonl(BOOTPARAM_VERS);
148 args->proc = htonl(BOOTPARAM_WHOAMI);
149 args->arglen = htonl(sizeof(struct xdr_inaddr));
150 send_tail = (char*) &args->xina;
153 * append encapsulated data (client IP address)
155 if (xdr_inaddr_encode(&send_tail, myip))
158 /* RPC: portmap/callit */
159 d->myport = htons(--rpc_port);
160 d->destip.s_addr = INADDR_BROADCAST; /* XXX: subnet bcast? */
161 /* rpc_call will set d->destport */
163 len = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_CALLIT,
164 args, send_tail - (char*)args,
165 repl, sizeof(*repl));
167 printf("bootparamd: 'whoami' call failed\n");
171 /* Save bootparam server address (from IP header). */
172 rpc_fromaddr(repl, &bp_server_addr, &bp_server_port);
175 * Note that bp_server_port is now 111 due to the
176 * indirect call (using PMAPPROC_CALLIT), so get the
177 * actual port number from the reply data.
179 bp_server_port = repl->port;
181 RPC_PRINTF(("bp_whoami: server at %s:%d\n",
182 inet_ntoa(bp_server_addr), ntohs(bp_server_port)));
184 /* We have just done a portmap call, so cache the portnum. */
185 rpc_pmap_putcache(bp_server_addr,
188 (int)ntohs(bp_server_port));
191 * Parse the encapsulated results from bootparam/whoami
193 x = ntohl(repl->encap_len);
195 printf("bp_whoami: short reply, %d < %d\n", len, x);
198 recv_head = (char*) repl->capsule;
201 hostnamelen = MAXHOSTNAMELEN-1;
202 if (xdr_string_decode(&recv_head, hostname, &hostnamelen)) {
203 RPC_PRINTF(("bp_whoami: bad hostname\n"));
208 domainnamelen = MAXHOSTNAMELEN-1;
209 if (xdr_string_decode(&recv_head, domainname, &domainnamelen)) {
210 RPC_PRINTF(("bp_whoami: bad domainname\n"));
214 /* gateway address */
215 if (xdr_inaddr_decode(&recv_head, &gateip)) {
216 RPC_PRINTF(("bp_whoami: bad gateway\n"));
226 * RPC: bootparam/getfile
227 * Given client name and file "key", get:
233 bp_getfile(sockfd, key, serv_addr, pathname)
237 struct in_addr *serv_addr;
240 n_long h[RPC_HEADER_WORDS];
244 n_long h[RPC_HEADER_WORDS];
247 char serv_name[FNAME_SIZE];
248 char *send_tail, *recv_head;
251 int sn_len, path_len, rlen;
253 if (!(d = socktodesc(sockfd))) {
254 RPC_PRINTF(("bp_getfile: bad socket. %d\n", sockfd));
258 send_tail = (char*) sdata.d;
259 recv_head = (char*) rdata.d;
262 * Build request message.
265 /* client name (hostname) */
266 if (xdr_string_encode(&send_tail, hostname, hostnamelen)) {
267 RPC_PRINTF(("bp_getfile: bad client\n"));
271 /* key name (root or swap) */
272 if (xdr_string_encode(&send_tail, key, strlen(key))) {
273 RPC_PRINTF(("bp_getfile: bad key\n"));
277 /* RPC: bootparam/getfile */
278 d->myport = htons(--rpc_port);
279 d->destip = bp_server_addr;
280 /* rpc_call will set d->destport */
283 BOOTPARAM_PROG, BOOTPARAM_VERS, BOOTPARAM_GETFILE,
284 sdata.d, send_tail - (char*)sdata.d,
285 rdata.d, sizeof(rdata.d));
287 RPC_PRINTF(("bp_getfile: short reply\n"));
291 recv_head = (char*) rdata.d;
294 * Parse result message.
298 sn_len = FNAME_SIZE-1;
299 if (xdr_string_decode(&recv_head, serv_name, &sn_len)) {
300 RPC_PRINTF(("bp_getfile: bad server name\n"));
304 /* server IP address (mountd/NFS) */
305 if (xdr_inaddr_decode(&recv_head, serv_addr)) {
306 RPC_PRINTF(("bp_getfile: bad server addr\n"));
310 /* server pathname */
311 path_len = MAXPATHLEN-1;
312 if (xdr_string_decode(&recv_head, pathname, &path_len)) {
313 RPC_PRINTF(("bp_getfile: bad server path\n"));
323 * eXternal Data Representation routines.
324 * (but with non-standard args...)
329 xdr_string_encode(pkt, str, len)
336 int padlen = (len + 3) & ~3; /* padded length */
338 /* The data will be int aligned. */
339 lenp = (u_int32_t*) *pkt;
340 *pkt += sizeof(*lenp);
345 bcopy(str, datap, len);
351 xdr_string_decode(pkt, str, len_p)
354 int *len_p; /* bufsize - 1 */
358 int slen; /* string length */
359 int plen; /* padded length */
361 /* The data will be int aligned. */
362 lenp = (u_int32_t*) *pkt;
363 *pkt += sizeof(*lenp);
365 plen = (slen + 3) & ~3;
371 bcopy(datap, str, slen);
381 xdr_inaddr_encode(pkt, ia)
383 struct in_addr ia; /* network order */
385 struct xdr_inaddr *xi;
389 n_long l; /* network order */
393 /* The data will be int aligned. */
394 xi = (struct xdr_inaddr *) *pkt;
396 xi->atype = htonl(1);
401 * Note: the htonl() calls below DO NOT
402 * imply that uia.l is in host order.
403 * In fact this needs it in net order.
405 *ip++ = htonl((unsigned int)*cp++);
406 *ip++ = htonl((unsigned int)*cp++);
407 *ip++ = htonl((unsigned int)*cp++);
408 *ip++ = htonl((unsigned int)*cp++);
414 xdr_inaddr_decode(pkt, ia)
416 struct in_addr *ia; /* network order */
418 struct xdr_inaddr *xi;
422 n_long l; /* network order */
426 /* The data will be int aligned. */
427 xi = (struct xdr_inaddr *) *pkt;
429 if (xi->atype != htonl(1)) {
430 RPC_PRINTF(("xdr_inaddr_decode: bad addrtype=%d\n",
438 * Note: the ntohl() calls below DO NOT
439 * imply that uia.l is in host order.
440 * In fact this needs it in net order.
442 *cp++ = ntohl(*ip++);
443 *cp++ = ntohl(*ip++);
444 *cp++ = ntohl(*ip++);
445 *cp++ = ntohl(*ip++);