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.
33 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
54 #include "bootparam.h"
57 #define RPC_PRINTF(a) printf a
62 struct in_addr bp_server_addr; /* net order */
63 n_short bp_server_port; /* net order */
66 * RPC definitions for bootparamd
68 #define BOOTPARAM_PROG 100026
69 #define BOOTPARAM_VERS 1
70 #define BOOTPARAM_WHOAMI 1
71 #define BOOTPARAM_GETFILE 2
74 * Inet address in RPC messages
75 * (Note, really four ints, NOT chars. Blech.)
82 int xdr_inaddr_encode(char **p, struct in_addr ia);
83 int xdr_inaddr_decode(char **p, struct in_addr *ia);
85 int xdr_string_encode(char **p, char *str, int len);
86 int xdr_string_decode(char **p, char *str, int *len_p);
90 * RPC: bootparam/whoami
91 * Given client IP address, get:
92 * client name (hostname)
93 * domain name (domainname)
96 * The hostname and domainname are set here for convenience.
98 * Note - bpsin is initialized to the broadcast address,
99 * and will be replaced with the bootparam server address
100 * after this call is complete. Have to use PMAP_PROC_CALL
101 * to make sure we get responses only from a servers that
102 * know about us (don't want to broadcast a getport call).
105 bp_whoami(int sockfd)
107 /* RPC structures for PMAPPROC_CALLIT */
113 struct xdr_inaddr xina;
119 /* encapsulated data here */
123 n_long h[RPC_HEADER_WORDS];
126 char *send_tail, *recv_head;
131 RPC_PRINTF(("bp_whoami: myip=%s\n", inet_ntoa(myip)));
134 if (!(d = socktodesc(sockfd))) {
135 RPC_PRINTF(("bp_whoami: bad socket. %d\n", sockfd));
141 * Build request args for PMAPPROC_CALLIT.
143 args->prog = htonl(BOOTPARAM_PROG);
144 args->vers = htonl(BOOTPARAM_VERS);
145 args->proc = htonl(BOOTPARAM_WHOAMI);
146 args->arglen = htonl(sizeof(struct xdr_inaddr));
147 send_tail = (char*) &args->xina;
150 * append encapsulated data (client IP address)
152 if (xdr_inaddr_encode(&send_tail, myip))
155 /* RPC: portmap/callit */
156 d->myport = htons(--rpc_port);
157 d->destip.s_addr = INADDR_BROADCAST; /* XXX: subnet bcast? */
158 /* rpc_call will set d->destport */
161 len = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_CALLIT,
162 args, send_tail - (char*)args, (void **)&repl, &pkt);
164 printf("bootparamd: 'whoami' call failed\n");
168 /* Save bootparam server address (from IP header). */
169 rpc_fromaddr(repl, &bp_server_addr, &bp_server_port);
172 * Note that bp_server_port is now 111 due to the
173 * indirect call (using PMAPPROC_CALLIT), so get the
174 * actual port number from the reply data.
176 bp_server_port = repl->port;
178 RPC_PRINTF(("bp_whoami: server at %s:%d\n",
179 inet_ntoa(bp_server_addr), ntohs(bp_server_port)));
181 /* We have just done a portmap call, so cache the portnum. */
182 rpc_pmap_putcache(bp_server_addr,
185 (int)ntohs(bp_server_port));
188 * Parse the encapsulated results from bootparam/whoami
190 x = ntohl(repl->encap_len);
192 printf("bp_whoami: short reply, %d < %d\n", len, x);
195 recv_head = (char*) repl->capsule;
198 hostnamelen = MAXHOSTNAMELEN-1;
199 if (xdr_string_decode(&recv_head, hostname, &hostnamelen)) {
200 RPC_PRINTF(("bp_whoami: bad hostname\n"));
205 domainnamelen = MAXHOSTNAMELEN-1;
206 if (xdr_string_decode(&recv_head, domainname, &domainnamelen)) {
207 RPC_PRINTF(("bp_whoami: bad domainname\n"));
211 /* gateway address */
212 if (xdr_inaddr_decode(&recv_head, &gateip)) {
213 RPC_PRINTF(("bp_whoami: bad gateway\n"));
226 * RPC: bootparam/getfile
227 * Given client name and file "key", get:
233 bp_getfile(int sockfd, char *key, struct in_addr *serv_addr, char *pathname)
236 n_long h[RPC_HEADER_WORDS];
240 char serv_name[FNAME_SIZE];
241 char *rdata, *send_tail;
244 int rc = -1, sn_len, path_len, rlen;
246 if (!(d = socktodesc(sockfd))) {
247 RPC_PRINTF(("bp_getfile: bad socket. %d\n", sockfd));
251 send_tail = (char*) sdata.d;
254 * Build request message.
257 /* client name (hostname) */
258 if (xdr_string_encode(&send_tail, hostname, hostnamelen)) {
259 RPC_PRINTF(("bp_getfile: bad client\n"));
263 /* key name (root or swap) */
264 if (xdr_string_encode(&send_tail, key, strlen(key))) {
265 RPC_PRINTF(("bp_getfile: bad key\n"));
269 /* RPC: bootparam/getfile */
270 d->myport = htons(--rpc_port);
271 d->destip = bp_server_addr;
272 /* rpc_call will set d->destport */
275 BOOTPARAM_PROG, BOOTPARAM_VERS, BOOTPARAM_GETFILE,
276 sdata.d, send_tail - (char*)sdata.d,
277 (void **)&rdata, &pkt);
279 RPC_PRINTF(("bp_getfile: short reply\n"));
285 * Parse result message.
289 sn_len = FNAME_SIZE-1;
290 if (xdr_string_decode(&rdata, serv_name, &sn_len)) {
291 RPC_PRINTF(("bp_getfile: bad server name\n"));
295 /* server IP address (mountd/NFS) */
296 if (xdr_inaddr_decode(&rdata, serv_addr)) {
297 RPC_PRINTF(("bp_getfile: bad server addr\n"));
301 /* server pathname */
302 path_len = MAXPATHLEN-1;
303 if (xdr_string_decode(&rdata, pathname, &path_len)) {
304 RPC_PRINTF(("bp_getfile: bad server path\n"));
317 * eXternal Data Representation routines.
318 * (but with non-standard args...)
323 xdr_string_encode(char **pkt, char *str, int len)
327 int padlen = (len + 3) & ~3; /* padded length */
329 /* The data will be int aligned. */
330 lenp = (uint32_t *) *pkt;
331 *pkt += sizeof(*lenp);
336 bcopy(str, datap, len);
342 xdr_string_decode(char **pkt, char *str, int *len_p)
346 int slen; /* string length */
347 int plen; /* padded length */
349 /* The data will be int aligned. */
350 lenp = (uint32_t *) *pkt;
351 *pkt += sizeof(*lenp);
353 plen = (slen + 3) & ~3;
359 bcopy(datap, str, slen);
369 xdr_inaddr_encode(char **pkt, struct in_addr ia)
371 struct xdr_inaddr *xi;
375 n_long l; /* network order */
379 /* The data will be int aligned. */
380 xi = (struct xdr_inaddr *) *pkt;
382 xi->atype = htonl(1);
387 * Note: the htonl() calls below DO NOT
388 * imply that uia.l is in host order.
389 * In fact this needs it in net order.
391 *ip++ = htonl((unsigned int)*cp++);
392 *ip++ = htonl((unsigned int)*cp++);
393 *ip++ = htonl((unsigned int)*cp++);
394 *ip++ = htonl((unsigned int)*cp++);
400 xdr_inaddr_decode(char **pkt, struct in_addr *ia)
402 struct xdr_inaddr *xi;
406 n_long l; /* network order */
410 /* The data will be int aligned. */
411 xi = (struct xdr_inaddr *) *pkt;
413 if (xi->atype != htonl(1)) {
414 RPC_PRINTF(("xdr_inaddr_decode: bad addrtype=%d\n",
422 * Note: the ntohl() calls below DO NOT
423 * imply that uia.l is in host order.
424 * In fact this needs it in net order.
426 *cp++ = ntohl(*ip++);
427 *cp++ = ntohl(*ip++);
428 *cp++ = ntohl(*ip++);
429 *cp++ = ntohl(*ip++);