bootparam.c revision 1.5 1 /* $NetBSD: bootparam.c,v 1.5 1995/09/23 03:36:07 gwr 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 struct in_addr bp_server_addr; /* net order */
56 n_short bp_server_port; /* net order */
57
58 /*
59 * RPC definitions for bootparamd
60 */
61 #define BOOTPARAM_PROG 100026
62 #define BOOTPARAM_VERS 1
63 #define BOOTPARAM_WHOAMI 1
64 #define BOOTPARAM_GETFILE 2
65
66 /*
67 * Inet address in RPC messages
68 * (Note, really four ints, NOT chars. Blech.)
69 */
70 struct xdr_inaddr {
71 u_int32_t atype;
72 int32_t addr[4];
73 };
74
75 int xdr_inaddr_encode __P((char **p, struct in_addr ia));
76 int xdr_inaddr_decode __P((char **p, struct in_addr *ia));
77
78 int xdr_string_encode __P((char **p, char *str, int len));
79 int xdr_string_decode __P((char **p, char *str, int *len_p));
80
81
82 /*
83 * RPC: bootparam/whoami
84 * Given client IP address, get:
85 * client name (hostname)
86 * domain name (domainname)
87 * gateway address
88 *
89 * The hostname and domainname are set here for convenience.
90 *
91 * Note - bpsin is initialized to the broadcast address,
92 * and will be replaced with the bootparam server address
93 * after this call is complete. Have to use PMAP_PROC_CALL
94 * to make sure we get responses only from a servers that
95 * know about us (don't want to broadcast a getport call).
96 */
97 int
98 bp_whoami(sockfd)
99 int sockfd;
100 {
101 /* RPC structures for PMAPPROC_CALLIT */
102 struct args {
103 u_int32_t prog;
104 u_int32_t vers;
105 u_int32_t proc;
106 u_int32_t arglen;
107 struct xdr_inaddr xina;
108 } *args;
109 struct repl {
110 u_int32_t port;
111 u_int32_t encap_len;
112 /* encapsulated data here */
113 n_long capsule[64];
114 } *repl;
115 struct {
116 n_long h[RPC_HEADER_WORDS];
117 struct args d;
118 } sdata;
119 struct {
120 n_long h[RPC_HEADER_WORDS];
121 struct repl d;
122 } rdata;
123 char *send_tail, *recv_head;
124 struct iodesc *d;
125 int len, x;
126
127 #ifdef RPC_DEBUG
128 printf("bp_whoami: myip=%s\n", inet_ntoa(myip));
129 #endif
130
131 if (!(d = socktodesc(sockfd))) {
132 #ifdef RPC_DEBUG
133 printf("bp_whoami: bad socket. %d\n", sockfd);
134 #endif
135 return (-1);
136 }
137 args = &sdata.d;
138 repl = &rdata.d;
139
140 /*
141 * Build request args for PMAPPROC_CALLIT.
142 */
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;
148
149 /*
150 * append encapsulated data (client IP address)
151 */
152 if (xdr_inaddr_encode(&send_tail, myip))
153 return (-1);
154
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 */
159
160 len = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_CALLIT,
161 args, send_tail - (char*)args,
162 repl, sizeof(*repl));
163 if (len < 8) {
164 printf("bootparamd: 'whoami' call failed\n");
165 return (-1);
166 }
167
168 /* Save bootparam server address (from IP header). */
169 rpc_fromaddr(repl, &bp_server_addr, &bp_server_port);
170
171 /*
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.
175 */
176 bp_server_port = repl->port;
177
178 #ifdef RPC_DEBUG
179 printf("bp_whoami: server at %s:%d\n",
180 inet_ntoa(bp_server_addr), ntohs(bp_server_port));
181 #endif
182
183 /* We have just done a portmap call, so cache the portnum. */
184 rpc_pmap_putcache(bp_server_addr,
185 BOOTPARAM_PROG,
186 BOOTPARAM_VERS,
187 (int)ntohs(bp_server_port));
188
189 /*
190 * Parse the encapsulated results from bootparam/whoami
191 */
192 x = ntohl(repl->encap_len);
193 if (len < x) {
194 printf("bp_whoami: short reply, %d < %d\n", len, x);
195 return (-1);
196 }
197 recv_head = (char*) repl->capsule;
198
199 /* client name */
200 hostnamelen = MAXHOSTNAMELEN-1;
201 if (xdr_string_decode(&recv_head, hostname, &hostnamelen)) {
202 #ifdef RPC_DEBUG
203 printf("bp_whoami: bad hostname\n");
204 #endif
205 return (-1);
206 }
207
208 /* domain name */
209 domainnamelen = MAXHOSTNAMELEN-1;
210 if (xdr_string_decode(&recv_head, domainname, &domainnamelen)) {
211 #ifdef RPC_DEBUG
212 printf("bp_whoami: bad domainname\n");
213 #endif
214 return (-1);
215 }
216
217 /* gateway address */
218 if (xdr_inaddr_decode(&recv_head, &gateip)) {
219 #ifdef RPC_DEBUG
220 printf("bp_whoami: bad gateway\n");
221 #endif
222 return (-1);
223 }
224
225 /* success */
226 return(0);
227 }
228
229
230 /*
231 * RPC: bootparam/getfile
232 * Given client name and file "key", get:
233 * server name
234 * server IP address
235 * server pathname
236 */
237 int
238 bp_getfile(sockfd, key, serv_addr, pathname)
239 int sockfd;
240 char *key;
241 char *pathname;
242 struct in_addr *serv_addr;
243 {
244 struct {
245 n_long h[RPC_HEADER_WORDS];
246 n_long d[64];
247 } sdata;
248 struct {
249 n_long h[RPC_HEADER_WORDS];
250 n_long d[128];
251 } rdata;
252 char serv_name[FNAME_SIZE];
253 char *send_tail, *recv_head;
254 /* misc... */
255 struct iodesc *d;
256 int sn_len, path_len, rlen;
257
258 if (!(d = socktodesc(sockfd))) {
259 #ifdef RPC_DEBUG
260 printf("bp_getfile: bad socket. %d\n", sockfd);
261 #endif
262 return (-1);
263 }
264
265 send_tail = (char*) sdata.d;
266 recv_head = (char*) rdata.d;
267
268 /*
269 * Build request message.
270 */
271
272 /* client name (hostname) */
273 if (xdr_string_encode(&send_tail, hostname, hostnamelen)) {
274 #ifdef RPC_DEBUG
275 printf("bp_getfile: bad client\n");
276 #endif
277 return (-1);
278 }
279
280 /* key name (root or swap) */
281 if (xdr_string_encode(&send_tail, key, strlen(key))) {
282 #ifdef RPC_DEBUG
283 printf("bp_getfile: bad key\n");
284 #endif
285 return (-1);
286 }
287
288 /* RPC: bootparam/getfile */
289 d->myport = htons(--rpc_port);
290 d->destip = bp_server_addr;
291 /* rpc_call will set d->destport */
292
293 rlen = rpc_call(d,
294 BOOTPARAM_PROG, BOOTPARAM_VERS, BOOTPARAM_GETFILE,
295 sdata.d, send_tail - (char*)sdata.d,
296 rdata.d, sizeof(rdata.d));
297 if (rlen < 4) {
298 #ifdef RPC_DEBUG
299 printf("bp_getfile: short reply\n");
300 #endif
301 errno = EBADRPC;
302 return (-1);
303 }
304 recv_head = (char*) rdata.d;
305
306 /*
307 * Parse result message.
308 */
309
310 /* server name */
311 sn_len = FNAME_SIZE-1;
312 if (xdr_string_decode(&recv_head, serv_name, &sn_len)) {
313 #ifdef RPC_DEBUG
314 printf("bp_getfile: bad server name\n");
315 #endif
316 return (-1);
317 }
318
319 /* server IP address (mountd/NFS) */
320 if (xdr_inaddr_decode(&recv_head, serv_addr)) {
321 #ifdef RPC_DEBUG
322 printf("bp_getfile: bad server addr\n");
323 #endif
324 return (-1);
325 }
326
327 /* server pathname */
328 path_len = MAXPATHLEN-1;
329 if (xdr_string_decode(&recv_head, pathname, &path_len)) {
330 #ifdef RPC_DEBUG
331 printf("bp_getfile: bad server path\n");
332 #endif
333 return (-1);
334 }
335
336 /* success */
337 return(0);
338 }
339
340
341 /*
342 * eXternal Data Representation routines.
343 * (but with non-standard args...)
344 */
345
346
347 int
348 xdr_string_encode(pkt, str, len)
349 char **pkt;
350 char *str;
351 int len;
352 {
353 u_int32_t *lenp;
354 char *datap;
355 int padlen = (len + 3) & ~3; /* padded length */
356
357 /* The data will be int aligned. */
358 lenp = (u_int32_t*) *pkt;
359 *pkt += sizeof(*lenp);
360 *lenp = htonl(len);
361
362 datap = *pkt;
363 *pkt += padlen;
364 bcopy(str, datap, len);
365
366 return (0);
367 }
368
369 int
370 xdr_string_decode(pkt, str, len_p)
371 char **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 /* The data will be int aligned. */
381 lenp = (u_int32_t*) *pkt;
382 *pkt += sizeof(*lenp);
383 slen = ntohl(*lenp);
384 plen = (slen + 3) & ~3;
385
386 if (slen > *len_p)
387 slen = *len_p;
388 datap = *pkt;
389 *pkt += plen;
390 bcopy(datap, str, slen);
391
392 str[slen] = '\0';
393 *len_p = slen;
394
395 return (0);
396 }
397
398
399 int
400 xdr_inaddr_encode(pkt, ia)
401 char **pkt;
402 struct in_addr ia; /* network order */
403 {
404 struct xdr_inaddr *xi;
405 u_char *cp;
406 int32_t *ip;
407 union {
408 n_long l; /* network order */
409 u_char c[4];
410 } uia;
411
412 /* The data will be int aligned. */
413 xi = (struct xdr_inaddr *) *pkt;
414 *pkt += sizeof(*xi);
415 xi->atype = htonl(1);
416 uia.l = ia.s_addr;
417 cp = uia.c;
418 ip = xi->addr;
419 /*
420 * Note: the htonl() calls below DO NOT
421 * imply that uia.l is in host order.
422 * In fact this needs it in net order.
423 */
424 *ip++ = htonl((unsigned int)*cp++);
425 *ip++ = htonl((unsigned int)*cp++);
426 *ip++ = htonl((unsigned int)*cp++);
427 *ip++ = htonl((unsigned int)*cp++);
428
429 return (0);
430 }
431
432 int
433 xdr_inaddr_decode(pkt, ia)
434 char **pkt;
435 struct in_addr *ia; /* network order */
436 {
437 struct xdr_inaddr *xi;
438 u_char *cp;
439 int32_t *ip;
440 union {
441 n_long l; /* network order */
442 u_char c[4];
443 } uia;
444
445 /* The data will be int aligned. */
446 xi = (struct xdr_inaddr *) *pkt;
447 *pkt += sizeof(*xi);
448 if (xi->atype != htonl(1)) {
449 #ifdef RPC_DEBUG
450 printf("xdr_inaddr_decode: bad addrtype=%d\n",
451 ntohl(xi->atype));
452 #endif
453 return(-1);
454 }
455
456 cp = uia.c;
457 ip = xi->addr;
458 /*
459 * Note: the ntohl() calls below DO NOT
460 * imply that uia.l is in host order.
461 * In fact this needs it in net order.
462 */
463 *cp++ = ntohl(*ip++);
464 *cp++ = ntohl(*ip++);
465 *cp++ = ntohl(*ip++);
466 *cp++ = ntohl(*ip++);
467 ia->s_addr = uia.l;
468
469 return (0);
470 }
471