bootparam.c revision 1.3 1 /* $NetBSD: bootparam.c,v 1.3 1995/09/17 00:49:38 pk Exp $ */
2
3 /*
4 * Copyright (c) 1995 Gordon W. Ross
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
20 *
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.
31 */
32
33 /*
34 * RPC/bootparams
35 */
36
37 #include <sys/param.h>
38 #include <sys/socket.h>
39
40 #include <net/if.h>
41
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44
45 #include <nfs/rpcv2.h>
46
47 #include <string.h>
48
49 #include "stand.h"
50 #include "net.h"
51 #include "netif.h"
52 #include "rpc.h"
53 #include "bootparam.h"
54
55 n_long bp_server_addr; /* net order */
56 n_short bp_server_port; /* net order */
57
58 int hostnamelen;
59 char hostname[FNAME_SIZE];
60
61 int domainnamelen;
62 char domainname[FNAME_SIZE];
63
64 /*
65 * RPC definitions for bootparamd
66 */
67 #define BOOTPARAM_PROG 100026
68 #define BOOTPARAM_VERS 1
69 #define BOOTPARAM_WHOAMI 1
70 #define BOOTPARAM_GETFILE 2
71
72 /*
73 * Inet address in RPC messages
74 * (Note, really four ints, NOT chars. Blech.)
75 */
76 struct xdr_inaddr {
77 u_int32_t atype;
78 int32_t addr[4];
79 };
80
81 int xdr_inaddr_encode __P((void **p, n_long ia));
82 int xdr_inaddr_decode __P((void **p, n_long *ia));
83
84 int xdr_string_encode __P((void **p, char *str, int len));
85 int xdr_string_decode __P((void **p, char *str, int *len_p));
86
87
88 /*
89 * RPC: bootparam/whoami
90 * Given client IP address, get:
91 * client name (hostname)
92 * domain name (domainname)
93 * gateway address
94 *
95 * The hostname and domainname are set here for convenience.
96 *
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).
102 */
103 int
104 bp_whoami(sockfd)
105 int sockfd;
106 {
107 /* RPC structures for PMAPPROC_CALLIT */
108 struct args {
109 u_int32_t prog;
110 u_int32_t vers;
111 u_int32_t proc;
112 u_int32_t arglen;
113 struct xdr_inaddr xina;
114 } *args;
115 struct repl {
116 u_int32_t port;
117 u_int32_t encap_len;
118 /* encapsulated data here */
119 n_long capsule[64];
120 } *repl;
121 struct {
122 n_long h[RPC_HEADER_WORDS];
123 struct args d;
124 } sdata;
125 struct {
126 n_long h[RPC_HEADER_WORDS];
127 struct repl d;
128 } rdata;
129 void *send_tail, *recv_head;
130 struct iodesc *d;
131 int len, x;
132
133 #ifdef RPC_DEBUG
134 printf("bp_whoami: myip=0x%x\n", myip);
135 #endif
136
137 if (!(d = socktodesc(sockfd))) {
138 printf("bp_whoami: bad socket. %d\n", sockfd);
139 return (EBADF);
140 }
141 args = &sdata.d;
142 repl = &rdata.d;
143
144 /*
145 * Build request args for PMAPPROC_CALLIT.
146 */
147 args->prog = htonl(BOOTPARAM_PROG);
148 args->vers = htonl(BOOTPARAM_VERS);
149 args->proc = htonl(BOOTPARAM_WHOAMI);
150 args->arglen = htonl(sizeof(struct xdr_inaddr));
151 send_tail = &args->xina;
152
153 /*
154 * append encapsulated data (client IP address)
155 */
156 if (xdr_inaddr_encode(&send_tail, myip))
157 return (-1);
158
159 /* RPC: portmap/callit */
160 d->myport = htons(--rpc_port);
161 d->destip = htonl(INADDR_BROADCAST); /* XXX: subnet bcast? */
162 /* rpc_call will set d->destport */
163
164 len = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_CALLIT,
165 args, (int)send_tail - (int)args,
166 repl, sizeof(*repl));
167 if (len < 8) {
168 printf("bootparamd: 'whoami' call failed\n");
169 return(-1);
170 }
171
172 /* Save bootparam server address (from IP header). */
173 rpc_fromaddr(repl, &bp_server_addr, &bp_server_port);
174
175 /*
176 * Note that bp_server_port is now 111 due to the
177 * indirect call (using PMAPPROC_CALLIT), so get the
178 * actual port number from the reply data.
179 */
180 bp_server_port = repl->port;
181
182 #ifdef RPC_DEBUG
183 printf("bp_whoami: server at %s:%d\n",
184 intoa(bp_server_addr), ntohs(bp_server_port));
185 #endif
186
187 /* We have just done a portmap call, so cache the portnum. */
188 rpc_pmap_putcache(bp_server_addr,
189 BOOTPARAM_PROG,
190 BOOTPARAM_VERS,
191 (int)bp_server_port);
192
193 /*
194 * Parse the encapsulated results from bootparam/whoami
195 */
196 x = ntohl(repl->encap_len);
197 if (len < x) {
198 printf("bp_whoami: short reply, %d < %d\n", len, x);
199 return(-1);
200 }
201 recv_head = repl->capsule;
202
203 /* client name */
204 hostnamelen = MAXHOSTNAMELEN-1;
205 if (xdr_string_decode(&recv_head, hostname, &hostnamelen)) {
206 #ifdef RPC_DEBUG
207 printf("bp_whoami: bad hostname\n");
208 #endif
209 return(-1);
210 }
211
212 /* domain name */
213 domainnamelen = MAXHOSTNAMELEN-1;
214 if (xdr_string_decode(&recv_head, domainname, &domainnamelen)) {
215 #ifdef RPC_DEBUG
216 printf("bp_whoami: bad domainname\n");
217 #endif
218 return(-1);
219 }
220
221 /* gateway address */
222 if (xdr_inaddr_decode(&recv_head, &gateip)) {
223 #ifdef RPC_DEBUG
224 printf("bp_whoami: bad gateway\n");
225 #endif
226 return(-1);
227 }
228
229 /* success */
230 return(0);
231 }
232
233
234 /*
235 * RPC: bootparam/getfile
236 * Given client name and file "key", get:
237 * server name
238 * server IP address
239 * server pathname
240 */
241 int
242 bp_getfile(sockfd, key, serv_addr, pathname)
243 int sockfd;
244 char *key;
245 char *pathname;
246 n_long *serv_addr;
247 {
248 struct {
249 n_long h[RPC_HEADER_WORDS];
250 n_long d[64];
251 } sdata;
252 struct {
253 n_long h[RPC_HEADER_WORDS];
254 n_long d[128];
255 } rdata;
256 char serv_name[FNAME_SIZE];
257 void *send_tail, *recv_head;
258 /* misc... */
259 struct iodesc *d;
260 int sn_len, path_len, rlen;
261
262 if (!(d = socktodesc(sockfd))) {
263 printf("bp_getfile: bad socket. %d\n", sockfd);
264 return (EBADF);
265 }
266
267 send_tail = sdata.d;
268 recv_head = rdata.d;
269
270 /*
271 * Build request message.
272 */
273
274 /* client name (hostname) */
275 if (xdr_string_encode(&send_tail, hostname, hostnamelen)) {
276 #ifdef RPC_DEBUG
277 printf("bp_getfile: bad client\n");
278 #endif
279 return (-1);
280 }
281
282 /* key name (root or swap) */
283 if (xdr_string_encode(&send_tail, key, strlen(key))) {
284 #ifdef RPC_DEBUG
285 printf("bp_getfile: bad key\n");
286 #endif
287 return (-1);
288 }
289
290 /* RPC: bootparam/getfile */
291 d->myport = htons(--rpc_port);
292 d->destip = bp_server_addr;
293 /* rpc_call will set d->destport */
294
295 rlen = rpc_call(d,
296 BOOTPARAM_PROG, BOOTPARAM_VERS, BOOTPARAM_GETFILE,
297 sdata.d, (int)send_tail - (int)sdata.d,
298 rdata.d, sizeof(rdata.d));
299 if (rlen < 4) {
300 #ifdef RPC_DEBUG
301 printf("bp_getfile: short reply\n");
302 #endif
303 return(-1);
304 }
305 recv_head = rdata.d;
306
307 /*
308 * Parse result message.
309 */
310
311 /* server name */
312 sn_len = FNAME_SIZE-1;
313 if (xdr_string_decode(&recv_head, serv_name, &sn_len)) {
314 #ifdef RPC_DEBUG
315 printf("bp_getfile: bad server name\n");
316 #endif
317 return(-1);
318 }
319
320 /* server IP address (mountd/NFS) */
321 if (xdr_inaddr_decode(&recv_head, serv_addr)) {
322 #ifdef RPC_DEBUG
323 printf("bp_getfile: bad server addr\n");
324 #endif
325 return(-1);
326 }
327
328 /* server pathname */
329 path_len = MAXPATHLEN-1;
330 if (xdr_string_decode(&recv_head, pathname, &path_len)) {
331 #ifdef RPC_DEBUG
332 printf("bp_getfile: bad server path\n");
333 #endif
334 return(-1);
335 }
336
337 /* success */
338 return(0);
339 }
340
341
342 /*
343 * eXternal Data Representation routines.
344 * (but with non-standard args...)
345 */
346
347
348 int
349 xdr_string_encode(pkt, str, len)
350 void **pkt;
351 char *str;
352 int len;
353 {
354 u_int32_t *lenp;
355 char *datap;
356 int padlen = (len + 3) & ~3; /* padded length */
357
358 lenp = *pkt;
359 *(char**)pkt += sizeof(*lenp);
360 *lenp = htonl(len);
361
362 datap = *pkt;
363 *(char**)pkt += padlen;
364 bcopy(str, datap, len);
365
366 return (0);
367 }
368
369 int
370 xdr_string_decode(pkt, str, len_p)
371 void **pkt;
372 char *str;
373 int *len_p; /* bufsize - 1 */
374 {
375 u_int32_t *lenp;
376 char *datap;
377 int slen; /* string length */
378 int plen; /* padded length */
379
380 lenp = *pkt;
381 *(char**)pkt += sizeof(*lenp);
382 slen = ntohl(*lenp);
383 plen = (slen + 3) & ~3;
384
385 if (slen > *len_p)
386 slen = *len_p;
387 datap = *pkt;
388 *(char**)pkt += plen;
389 bcopy(datap, str, slen);
390
391 str[slen] = '\0';
392 *len_p = slen;
393
394 return (0);
395 }
396
397
398 int
399 xdr_inaddr_encode(pkt, ia)
400 void **pkt;
401 n_long ia; /* host order */
402 {
403 struct xdr_inaddr *xi;
404 u_char *cp;
405 int32_t *ip;
406 union {
407 n_long l;
408 u_char c[4];
409 } uia;
410
411 xi = *pkt;
412 *(char**)pkt += sizeof(*xi);
413 xi->atype = htonl(1);
414 uia.l = htonl(ia);
415 cp = uia.c;
416 ip = xi->addr;
417 *ip++ = *cp++;
418 *ip++ = *cp++;
419 *ip++ = *cp++;
420 *ip++ = *cp++;
421
422 return (0);
423 }
424
425 int
426 xdr_inaddr_decode(pkt, ia)
427 void **pkt;
428 n_long *ia; /* host order */
429 {
430 struct xdr_inaddr *xi;
431 u_char *cp;
432 int32_t *ip;
433 union {
434 n_long l;
435 u_char c[4];
436 } uia;
437
438 xi = *pkt;
439 *(char**)pkt += sizeof(*xi);
440 if (xi->atype != htonl(1)) {
441 #ifdef RPC_DEBUG
442 printf("xdr_inaddr_decode: bad addrtype=%d\n",
443 ntohl(xi->atype));
444 #endif
445 return(-1);
446 }
447
448
449 cp = uia.c;
450 ip = xi->addr;
451 *cp++ = *ip++;
452 *cp++ = *ip++;
453 *cp++ = *ip++;
454 *cp++ = *ip++;
455 *ia = ntohl(uia.l);
456
457 return (0);
458 }
459