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