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>
34 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
56 #include "bootparam.h"
59 #define RPC_PRINTF(a) printf a
64 struct in_addr bp_server_addr
; /* net order */
65 n_short bp_server_port
; /* net order */
68 * RPC definitions for bootparamd
70 #define BOOTPARAM_PROG 100026
71 #define BOOTPARAM_VERS 1
72 #define BOOTPARAM_WHOAMI 1
73 #define BOOTPARAM_GETFILE 2
76 * Inet address in RPC messages
77 * (Note, really four ints, NOT chars. Blech.)
84 int xdr_inaddr_encode(char **p
, struct in_addr ia
);
85 int xdr_inaddr_decode(char **p
, struct in_addr
*ia
);
87 int xdr_string_encode(char **p
, char *str
, int len
);
88 int xdr_string_decode(char **p
, char *str
, int *len_p
);
92 * RPC: bootparam/whoami
93 * Given client IP address, get:
94 * client name (hostname)
95 * domain name (domainname)
98 * The hostname and domainname are set here for convenience.
100 * Note - bpsin is initialized to the broadcast address,
101 * and will be replaced with the bootparam server address
102 * after this call is complete. Have to use PMAP_PROC_CALL
103 * to make sure we get responses only from a servers that
104 * know about us (don't want to broadcast a getport call).
110 /* RPC structures for PMAPPROC_CALLIT */
116 struct xdr_inaddr xina
;
122 /* encapsulated data here */
126 n_long h
[RPC_HEADER_WORDS
];
130 n_long h
[RPC_HEADER_WORDS
];
133 char *send_tail
, *recv_head
;
137 RPC_PRINTF(("bp_whoami: myip=%s\n", inet_ntoa(myip
)));
139 if (!(d
= socktodesc(sockfd
))) {
140 RPC_PRINTF(("bp_whoami: bad socket. %d\n", sockfd
));
147 * Build request args for PMAPPROC_CALLIT.
149 args
->prog
= htonl(BOOTPARAM_PROG
);
150 args
->vers
= htonl(BOOTPARAM_VERS
);
151 args
->proc
= htonl(BOOTPARAM_WHOAMI
);
152 args
->arglen
= htonl(sizeof(struct xdr_inaddr
));
153 send_tail
= (char*) &args
->xina
;
156 * append encapsulated data (client IP address)
158 if (xdr_inaddr_encode(&send_tail
, myip
))
161 /* RPC: portmap/callit */
162 d
->myport
= htons(--rpc_port
);
163 d
->destip
.s_addr
= INADDR_BROADCAST
; /* XXX: subnet bcast? */
164 /* rpc_call will set d->destport */
166 len
= rpc_call(d
, PMAPPROG
, PMAPVERS
, PMAPPROC_CALLIT
,
167 args
, send_tail
- (char*)args
,
168 repl
, sizeof(*repl
));
170 printf("bootparamd: 'whoami' call failed\n");
174 /* Save bootparam server address (from IP header). */
175 rpc_fromaddr(repl
, &bp_server_addr
, &bp_server_port
);
178 * Note that bp_server_port is now 111 due to the
179 * indirect call (using PMAPPROC_CALLIT), so get the
180 * actual port number from the reply data.
182 bp_server_port
= repl
->port
;
184 RPC_PRINTF(("bp_whoami: server at %s:%d\n",
185 inet_ntoa(bp_server_addr
), ntohs(bp_server_port
)));
187 /* We have just done a portmap call, so cache the portnum. */
188 rpc_pmap_putcache(bp_server_addr
,
191 (int)ntohs(bp_server_port
));
194 * Parse the encapsulated results from bootparam/whoami
196 x
= ntohl(repl
->encap_len
);
198 printf("bp_whoami: short reply, %d < %d\n", len
, x
);
201 recv_head
= (char*) repl
->capsule
;
204 hostnamelen
= MAXHOSTNAMELEN
-1;
205 if (xdr_string_decode(&recv_head
, hostname
, &hostnamelen
)) {
206 RPC_PRINTF(("bp_whoami: bad hostname\n"));
211 domainnamelen
= MAXHOSTNAMELEN
-1;
212 if (xdr_string_decode(&recv_head
, domainname
, &domainnamelen
)) {
213 RPC_PRINTF(("bp_whoami: bad domainname\n"));
217 /* gateway address */
218 if (xdr_inaddr_decode(&recv_head
, &gateip
)) {
219 RPC_PRINTF(("bp_whoami: bad gateway\n"));
229 * RPC: bootparam/getfile
230 * Given client name and file "key", get:
236 bp_getfile(sockfd
, key
, serv_addr
, pathname
)
240 struct in_addr
*serv_addr
;
243 n_long h
[RPC_HEADER_WORDS
];
247 n_long h
[RPC_HEADER_WORDS
];
250 char serv_name
[FNAME_SIZE
];
251 char *send_tail
, *recv_head
;
254 int sn_len
, path_len
, rlen
;
256 if (!(d
= socktodesc(sockfd
))) {
257 RPC_PRINTF(("bp_getfile: bad socket. %d\n", sockfd
));
261 send_tail
= (char*) sdata
.d
;
262 recv_head
= (char*) rdata
.d
;
265 * Build request message.
268 /* client name (hostname) */
269 if (xdr_string_encode(&send_tail
, hostname
, hostnamelen
)) {
270 RPC_PRINTF(("bp_getfile: bad client\n"));
274 /* key name (root or swap) */
275 if (xdr_string_encode(&send_tail
, key
, strlen(key
))) {
276 RPC_PRINTF(("bp_getfile: bad key\n"));
280 /* RPC: bootparam/getfile */
281 d
->myport
= htons(--rpc_port
);
282 d
->destip
= bp_server_addr
;
283 /* rpc_call will set d->destport */
286 BOOTPARAM_PROG
, BOOTPARAM_VERS
, BOOTPARAM_GETFILE
,
287 sdata
.d
, send_tail
- (char*)sdata
.d
,
288 rdata
.d
, sizeof(rdata
.d
));
290 RPC_PRINTF(("bp_getfile: short reply\n"));
294 recv_head
= (char*) rdata
.d
;
297 * Parse result message.
301 sn_len
= FNAME_SIZE
-1;
302 if (xdr_string_decode(&recv_head
, serv_name
, &sn_len
)) {
303 RPC_PRINTF(("bp_getfile: bad server name\n"));
307 /* server IP address (mountd/NFS) */
308 if (xdr_inaddr_decode(&recv_head
, serv_addr
)) {
309 RPC_PRINTF(("bp_getfile: bad server addr\n"));
313 /* server pathname */
314 path_len
= MAXPATHLEN
-1;
315 if (xdr_string_decode(&recv_head
, pathname
, &path_len
)) {
316 RPC_PRINTF(("bp_getfile: bad server path\n"));
326 * eXternal Data Representation routines.
327 * (but with non-standard args...)
332 xdr_string_encode(pkt
, str
, len
)
339 int padlen
= (len
+ 3) & ~3; /* padded length */
341 /* The data will be int aligned. */
342 lenp
= (u_int32_t
*) *pkt
;
343 *pkt
+= sizeof(*lenp
);
348 bcopy(str
, datap
, len
);
354 xdr_string_decode(pkt
, str
, len_p
)
357 int *len_p
; /* bufsize - 1 */
361 int slen
; /* string length */
362 int plen
; /* padded length */
364 /* The data will be int aligned. */
365 lenp
= (u_int32_t
*) *pkt
;
366 *pkt
+= sizeof(*lenp
);
368 plen
= (slen
+ 3) & ~3;
374 bcopy(datap
, str
, slen
);
384 xdr_inaddr_encode(pkt
, ia
)
386 struct in_addr ia
; /* network order */
388 struct xdr_inaddr
*xi
;
392 n_long l
; /* network order */
396 /* The data will be int aligned. */
397 xi
= (struct xdr_inaddr
*) *pkt
;
399 xi
->atype
= htonl(1);
404 * Note: the htonl() calls below DO NOT
405 * imply that uia.l is in host order.
406 * In fact this needs it in net order.
408 *ip
++ = htonl((unsigned int)*cp
++);
409 *ip
++ = htonl((unsigned int)*cp
++);
410 *ip
++ = htonl((unsigned int)*cp
++);
411 *ip
++ = htonl((unsigned int)*cp
++);
417 xdr_inaddr_decode(pkt
, ia
)
419 struct in_addr
*ia
; /* network order */
421 struct xdr_inaddr
*xi
;
425 n_long l
; /* network order */
429 /* The data will be int aligned. */
430 xi
= (struct xdr_inaddr
*) *pkt
;
432 if (xi
->atype
!= htonl(1)) {
433 RPC_PRINTF(("xdr_inaddr_decode: bad addrtype=%d\n",
441 * Note: the ntohl() calls below DO NOT
442 * imply that uia.l is in host order.
443 * In fact this needs it in net order.
445 *cp
++ = ntohl(*ip
++);
446 *cp
++ = ntohl(*ip
++);
447 *cp
++ = ntohl(*ip
++);
448 *cp
++ = ntohl(*ip
++);