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