Home | History | Annotate | Line # | Download | only in libsa
bootparam.c revision 1.19
      1  1.19       snj /*	$NetBSD: bootparam.c,v 1.19 2009/10/21 23:12:10 snj 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  *
     16   1.1       gwr  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1       gwr  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1       gwr  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1       gwr  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1       gwr  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21   1.1       gwr  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22   1.1       gwr  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23   1.1       gwr  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24   1.1       gwr  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25   1.1       gwr  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1       gwr  */
     27   1.1       gwr 
     28   1.1       gwr /*
     29   1.1       gwr  * RPC/bootparams
     30   1.1       gwr  */
     31   1.1       gwr 
     32   1.1       gwr #include <sys/param.h>
     33   1.1       gwr #include <sys/socket.h>
     34   1.1       gwr 
     35   1.1       gwr #include <net/if.h>
     36   1.1       gwr 
     37   1.1       gwr #include <netinet/in.h>
     38   1.1       gwr #include <netinet/in_systm.h>
     39   1.1       gwr 
     40  1.13   thorpej #ifdef _STANDALONE
     41  1.13   thorpej #include <lib/libkern/libkern.h>
     42  1.13   thorpej #else
     43  1.13   thorpej #include <string.h>
     44  1.13   thorpej #endif
     45  1.13   thorpej 
     46   1.7       gwr #include "rpcv2.h"
     47   1.1       gwr 
     48   1.1       gwr #include "stand.h"
     49   1.1       gwr #include "net.h"
     50   1.1       gwr #include "rpc.h"
     51   1.1       gwr #include "bootparam.h"
     52   1.1       gwr 
     53   1.8  christos #ifdef DEBUG_RPC
     54   1.9  christos #define RPC_PRINTF(a)	printf a
     55   1.8  christos #else
     56   1.8  christos #define RPC_PRINTF(a)
     57   1.8  christos #endif
     58   1.8  christos 
     59   1.4        pk struct in_addr	bp_server_addr;	/* net order */
     60   1.4        pk n_short		bp_server_port;	/* net order */
     61  1.15  drochner 
     62  1.15  drochner int		hostnamelen;
     63  1.15  drochner char		domainname[FNAME_SIZE]; /* our DNS domain */
     64  1.15  drochner int		domainnamelen;
     65   1.1       gwr 
     66   1.1       gwr /*
     67   1.1       gwr  * RPC definitions for bootparamd
     68   1.1       gwr  */
     69   1.1       gwr #define	BOOTPARAM_PROG		100026
     70   1.1       gwr #define	BOOTPARAM_VERS		1
     71   1.1       gwr #define BOOTPARAM_WHOAMI	1
     72   1.1       gwr #define BOOTPARAM_GETFILE	2
     73   1.1       gwr 
     74   1.1       gwr /*
     75   1.1       gwr  * Inet address in RPC messages
     76   1.1       gwr  * (Note, really four ints, NOT chars.  Blech.)
     77   1.1       gwr  */
     78   1.1       gwr struct xdr_inaddr {
     79   1.1       gwr 	u_int32_t  atype;
     80   1.1       gwr 	int32_t	addr[4];
     81   1.1       gwr };
     82   1.1       gwr 
     83  1.18   tsutsui int xdr_inaddr_encode(char **, struct in_addr);
     84  1.18   tsutsui int xdr_inaddr_decode(char **, struct in_addr *);
     85   1.1       gwr 
     86  1.18   tsutsui int xdr_string_encode(char **, char *, int);
     87  1.18   tsutsui int xdr_string_decode(char **, char *, int *);
     88   1.1       gwr 
     89   1.1       gwr 
     90   1.1       gwr /*
     91   1.1       gwr  * RPC: bootparam/whoami
     92   1.1       gwr  * Given client IP address, get:
     93   1.1       gwr  *	client name	(hostname)
     94   1.1       gwr  *	domain name (domainname)
     95   1.1       gwr  *	gateway address
     96   1.1       gwr  *
     97   1.1       gwr  * The hostname and domainname are set here for convenience.
     98   1.1       gwr  *
     99   1.1       gwr  * Note - bpsin is initialized to the broadcast address,
    100   1.1       gwr  * and will be replaced with the bootparam server address
    101   1.1       gwr  * after this call is complete.  Have to use PMAP_PROC_CALL
    102   1.1       gwr  * to make sure we get responses only from a servers that
    103   1.1       gwr  * know about us (don't want to broadcast a getport call).
    104   1.1       gwr  */
    105   1.1       gwr int
    106  1.16     isaki bp_whoami(int sockfd)
    107   1.1       gwr {
    108   1.1       gwr 	/* RPC structures for PMAPPROC_CALLIT */
    109   1.1       gwr 	struct args {
    110   1.1       gwr 		u_int32_t prog;
    111   1.1       gwr 		u_int32_t vers;
    112   1.1       gwr 		u_int32_t proc;
    113   1.1       gwr 		u_int32_t arglen;
    114   1.1       gwr 		struct xdr_inaddr xina;
    115   1.1       gwr 	} *args;
    116   1.1       gwr 	struct repl {
    117   1.6       gwr 		u_int16_t _pad;
    118   1.6       gwr 		u_int16_t port;
    119   1.1       gwr 		u_int32_t encap_len;
    120   1.1       gwr 		/* encapsulated data here */
    121   1.1       gwr 		n_long  capsule[64];
    122   1.1       gwr 	} *repl;
    123   1.1       gwr 	struct {
    124   1.1       gwr 		n_long	h[RPC_HEADER_WORDS];
    125   1.1       gwr 		struct args d;
    126   1.1       gwr 	} sdata;
    127   1.1       gwr 	struct {
    128   1.1       gwr 		n_long	h[RPC_HEADER_WORDS];
    129   1.1       gwr 		struct repl d;
    130   1.1       gwr 	} rdata;
    131   1.5       gwr 	char *send_tail, *recv_head;
    132   1.1       gwr 	struct iodesc *d;
    133   1.1       gwr 	int len, x;
    134   1.1       gwr 
    135   1.8  christos 	RPC_PRINTF(("bp_whoami: myip=%s\n", inet_ntoa(myip)));
    136   1.1       gwr 
    137   1.1       gwr 	if (!(d = socktodesc(sockfd))) {
    138   1.8  christos 		RPC_PRINTF(("bp_whoami: bad socket. %d\n", sockfd));
    139  1.16     isaki 		return -1;
    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.16     isaki 	send_tail = (char *)&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.16     isaki 		return -1;
    158   1.1       gwr 
    159   1.1       gwr 	/* RPC: portmap/callit */
    160   1.1       gwr 	d->myport = htons(--rpc_port);
    161   1.4        pk 	d->destip.s_addr = 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.16     isaki 				  args, send_tail - (char *)args,
    166   1.1       gwr 				  repl, sizeof(*repl));
    167   1.1       gwr 	if (len < 8) {
    168   1.9  christos 		printf("bootparamd: 'whoami' call failed\n");
    169  1.16     isaki 		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.8  christos 	RPC_PRINTF(("bp_whoami: server at %s:%d\n",
    183   1.8  christos 	    inet_ntoa(bp_server_addr), ntohs(bp_server_port)));
    184   1.1       gwr 
    185   1.1       gwr 	/* We have just done a portmap call, so cache the portnum. */
    186   1.1       gwr 	rpc_pmap_putcache(bp_server_addr,
    187   1.1       gwr 			  BOOTPARAM_PROG,
    188   1.1       gwr 			  BOOTPARAM_VERS,
    189   1.5       gwr 			  (int)ntohs(bp_server_port));
    190   1.1       gwr 
    191   1.1       gwr 	/*
    192   1.1       gwr 	 * Parse the encapsulated results from bootparam/whoami
    193   1.1       gwr 	 */
    194   1.1       gwr 	x = ntohl(repl->encap_len);
    195   1.1       gwr 	if (len < x) {
    196   1.9  christos 		printf("bp_whoami: short reply, %d < %d\n", len, x);
    197  1.16     isaki 		return -1;
    198   1.1       gwr 	}
    199  1.16     isaki 	recv_head = (char *)repl->capsule;
    200   1.1       gwr 
    201   1.1       gwr 	/* client name */
    202   1.1       gwr 	hostnamelen = MAXHOSTNAMELEN-1;
    203   1.1       gwr 	if (xdr_string_decode(&recv_head, hostname, &hostnamelen)) {
    204   1.8  christos 		RPC_PRINTF(("bp_whoami: bad hostname\n"));
    205  1.16     isaki 		return -1;
    206   1.1       gwr 	}
    207   1.1       gwr 
    208   1.1       gwr 	/* domain name */
    209   1.1       gwr 	domainnamelen = MAXHOSTNAMELEN-1;
    210   1.1       gwr 	if (xdr_string_decode(&recv_head, domainname, &domainnamelen)) {
    211   1.8  christos 		RPC_PRINTF(("bp_whoami: bad domainname\n"));
    212  1.16     isaki 		return -1;
    213   1.1       gwr 	}
    214   1.1       gwr 
    215   1.1       gwr 	/* gateway address */
    216   1.1       gwr 	if (xdr_inaddr_decode(&recv_head, &gateip)) {
    217   1.8  christos 		RPC_PRINTF(("bp_whoami: bad gateway\n"));
    218  1.16     isaki 		return -1;
    219   1.1       gwr 	}
    220   1.1       gwr 
    221   1.1       gwr 	/* success */
    222  1.16     isaki 	return 0;
    223   1.1       gwr }
    224   1.1       gwr 
    225   1.1       gwr 
    226   1.1       gwr /*
    227   1.1       gwr  * RPC: bootparam/getfile
    228   1.1       gwr  * Given client name and file "key", get:
    229   1.1       gwr  *	server name
    230   1.1       gwr  *	server IP address
    231   1.1       gwr  *	server pathname
    232   1.1       gwr  */
    233   1.1       gwr int
    234  1.16     isaki bp_getfile(int sockfd, char *key, struct in_addr *serv_addr, char *pathname)
    235   1.1       gwr {
    236   1.1       gwr 	struct {
    237   1.1       gwr 		n_long	h[RPC_HEADER_WORDS];
    238   1.1       gwr 		n_long  d[64];
    239   1.1       gwr 	} sdata;
    240   1.1       gwr 	struct {
    241   1.1       gwr 		n_long	h[RPC_HEADER_WORDS];
    242   1.1       gwr 		n_long  d[128];
    243   1.1       gwr 	} rdata;
    244   1.1       gwr 	char serv_name[FNAME_SIZE];
    245   1.5       gwr 	char *send_tail, *recv_head;
    246   1.1       gwr 	/* misc... */
    247   1.1       gwr 	struct iodesc *d;
    248   1.1       gwr 	int sn_len, path_len, rlen;
    249   1.1       gwr 
    250   1.1       gwr 	if (!(d = socktodesc(sockfd))) {
    251   1.8  christos 		RPC_PRINTF(("bp_getfile: bad socket. %d\n", sockfd));
    252  1.16     isaki 		return -1;
    253   1.1       gwr 	}
    254   1.1       gwr 
    255  1.16     isaki 	send_tail = (char *)sdata.d;
    256  1.16     isaki 	recv_head = (char *)rdata.d;
    257   1.1       gwr 
    258   1.1       gwr 	/*
    259   1.1       gwr 	 * Build request message.
    260   1.1       gwr 	 */
    261   1.1       gwr 
    262   1.1       gwr 	/* client name (hostname) */
    263   1.1       gwr 	if (xdr_string_encode(&send_tail, hostname, hostnamelen)) {
    264   1.8  christos 		RPC_PRINTF(("bp_getfile: bad client\n"));
    265  1.16     isaki 		return -1;
    266   1.1       gwr 	}
    267   1.1       gwr 
    268   1.1       gwr 	/* key name (root or swap) */
    269   1.1       gwr 	if (xdr_string_encode(&send_tail, key, strlen(key))) {
    270   1.8  christos 		RPC_PRINTF(("bp_getfile: bad key\n"));
    271  1.16     isaki 		return -1;
    272   1.1       gwr 	}
    273   1.1       gwr 
    274   1.1       gwr 	/* RPC: bootparam/getfile */
    275   1.1       gwr 	d->myport = htons(--rpc_port);
    276  1.16     isaki 	d->destip = bp_server_addr;
    277   1.1       gwr 	/* rpc_call will set d->destport */
    278   1.1       gwr 
    279   1.1       gwr 	rlen = rpc_call(d,
    280   1.1       gwr 		BOOTPARAM_PROG, BOOTPARAM_VERS, BOOTPARAM_GETFILE,
    281  1.16     isaki 		sdata.d, send_tail - (char *)sdata.d,
    282   1.1       gwr 		rdata.d, sizeof(rdata.d));
    283   1.1       gwr 	if (rlen < 4) {
    284   1.8  christos 		RPC_PRINTF(("bp_getfile: short reply\n"));
    285   1.4        pk 		errno = EBADRPC;
    286  1.16     isaki 		return -1;
    287   1.1       gwr 	}
    288  1.16     isaki 	recv_head = (char *)rdata.d;
    289   1.1       gwr 
    290   1.1       gwr 	/*
    291   1.1       gwr 	 * Parse result message.
    292   1.1       gwr 	 */
    293   1.1       gwr 
    294   1.1       gwr 	/* server name */
    295   1.1       gwr 	sn_len = FNAME_SIZE-1;
    296   1.1       gwr 	if (xdr_string_decode(&recv_head, serv_name, &sn_len)) {
    297   1.8  christos 		RPC_PRINTF(("bp_getfile: bad server name\n"));
    298  1.16     isaki 		return -1;
    299   1.1       gwr 	}
    300   1.1       gwr 
    301   1.1       gwr 	/* server IP address (mountd/NFS) */
    302   1.1       gwr 	if (xdr_inaddr_decode(&recv_head, serv_addr)) {
    303   1.8  christos 		RPC_PRINTF(("bp_getfile: bad server addr\n"));
    304  1.16     isaki 		return -1;
    305   1.1       gwr 	}
    306   1.1       gwr 
    307   1.1       gwr 	/* server pathname */
    308  1.16     isaki 	path_len = MAXPATHLEN - 1;
    309   1.1       gwr 	if (xdr_string_decode(&recv_head, pathname, &path_len)) {
    310   1.8  christos 		RPC_PRINTF(("bp_getfile: bad server path\n"));
    311  1.16     isaki 		return -1;
    312   1.1       gwr 	}
    313   1.1       gwr 
    314   1.1       gwr 	/* success */
    315  1.16     isaki 	return 0;
    316   1.1       gwr }
    317   1.1       gwr 
    318   1.1       gwr 
    319   1.1       gwr /*
    320   1.1       gwr  * eXternal Data Representation routines.
    321   1.1       gwr  * (but with non-standard args...)
    322   1.1       gwr  */
    323   1.1       gwr 
    324   1.1       gwr 
    325   1.1       gwr int
    326  1.16     isaki xdr_string_encode(char **pkt, char *str, int len)
    327   1.1       gwr {
    328   1.1       gwr 	u_int32_t *lenp;
    329   1.1       gwr 	char *datap;
    330   1.1       gwr 	int padlen = (len + 3) & ~3;	/* padded length */
    331   1.1       gwr 
    332   1.5       gwr 	/* The data will be int aligned. */
    333  1.16     isaki 	lenp = (u_int32_t *)*pkt;
    334   1.5       gwr 	*pkt += sizeof(*lenp);
    335   1.1       gwr 	*lenp = htonl(len);
    336   1.1       gwr 
    337   1.1       gwr 	datap = *pkt;
    338   1.5       gwr 	*pkt += padlen;
    339  1.17  christos 	(void)memcpy(datap, str, len);
    340   1.1       gwr 
    341  1.16     isaki 	return 0;
    342   1.1       gwr }
    343   1.1       gwr 
    344  1.16     isaki /* len_p: bufsize - 1 */
    345   1.1       gwr int
    346  1.16     isaki xdr_string_decode(char **pkt, char *str, int *len_p)
    347   1.1       gwr {
    348   1.1       gwr 	u_int32_t *lenp;
    349   1.1       gwr 	char *datap;
    350   1.1       gwr 	int slen;	/* string length */
    351   1.1       gwr 	int plen;	/* padded length */
    352   1.1       gwr 
    353   1.5       gwr 	/* The data will be int aligned. */
    354  1.16     isaki 	lenp = (u_int32_t *)*pkt;
    355   1.5       gwr 	*pkt += sizeof(*lenp);
    356   1.1       gwr 	slen = ntohl(*lenp);
    357   1.1       gwr 	plen = (slen + 3) & ~3;
    358   1.1       gwr 
    359   1.1       gwr 	if (slen > *len_p)
    360   1.1       gwr 		slen = *len_p;
    361   1.1       gwr 	datap = *pkt;
    362   1.5       gwr 	*pkt += plen;
    363  1.17  christos 	(void)memcpy(str, datap, slen);
    364   1.1       gwr 
    365   1.1       gwr 	str[slen] = '\0';
    366   1.1       gwr 	*len_p = slen;
    367   1.1       gwr 
    368  1.16     isaki 	return 0;
    369   1.1       gwr }
    370   1.1       gwr 
    371   1.1       gwr 
    372  1.16     isaki /* ia: network order */
    373   1.1       gwr int
    374  1.16     isaki xdr_inaddr_encode(char **pkt, struct in_addr ia)
    375   1.1       gwr {
    376   1.1       gwr 	struct xdr_inaddr *xi;
    377   1.1       gwr 	u_char *cp;
    378   1.1       gwr 	int32_t *ip;
    379   1.1       gwr 	union {
    380   1.5       gwr 		n_long l;	/* network order */
    381   1.1       gwr 		u_char c[4];
    382   1.1       gwr 	} uia;
    383   1.1       gwr 
    384   1.5       gwr 	/* The data will be int aligned. */
    385  1.16     isaki 	xi = (struct xdr_inaddr *)*pkt;
    386   1.5       gwr 	*pkt += sizeof(*xi);
    387   1.1       gwr 	xi->atype = htonl(1);
    388   1.4        pk 	uia.l = ia.s_addr;
    389   1.1       gwr 	cp = uia.c;
    390   1.1       gwr 	ip = xi->addr;
    391   1.5       gwr 	/*
    392   1.5       gwr 	 * Note: the htonl() calls below DO NOT
    393   1.5       gwr 	 * imply that uia.l is in host order.
    394   1.5       gwr 	 * In fact this needs it in net order.
    395   1.5       gwr 	 */
    396   1.4        pk 	*ip++ = htonl((unsigned int)*cp++);
    397   1.4        pk 	*ip++ = htonl((unsigned int)*cp++);
    398   1.4        pk 	*ip++ = htonl((unsigned int)*cp++);
    399   1.4        pk 	*ip++ = htonl((unsigned int)*cp++);
    400   1.1       gwr 
    401  1.16     isaki 	return 0;
    402   1.1       gwr }
    403   1.1       gwr 
    404  1.16     isaki /* ia: network order */
    405   1.1       gwr int
    406  1.16     isaki xdr_inaddr_decode(char **pkt, struct in_addr *ia)
    407   1.1       gwr {
    408   1.1       gwr 	struct xdr_inaddr *xi;
    409   1.1       gwr 	u_char *cp;
    410   1.1       gwr 	int32_t *ip;
    411   1.1       gwr 	union {
    412   1.5       gwr 		n_long l;	/* network order */
    413   1.1       gwr 		u_char c[4];
    414   1.1       gwr 	} uia;
    415   1.1       gwr 
    416   1.5       gwr 	/* The data will be int aligned. */
    417  1.16     isaki 	xi = (struct xdr_inaddr *)*pkt;
    418   1.5       gwr 	*pkt += sizeof(*xi);
    419   1.1       gwr 	if (xi->atype != htonl(1)) {
    420   1.8  christos 		RPC_PRINTF(("xdr_inaddr_decode: bad addrtype=%d\n",
    421   1.8  christos 		    ntohl(xi->atype)));
    422  1.16     isaki 		return -1;
    423   1.1       gwr 	}
    424   1.1       gwr 
    425   1.1       gwr 	cp = uia.c;
    426   1.1       gwr 	ip = xi->addr;
    427   1.5       gwr 	/*
    428   1.5       gwr 	 * Note: the ntohl() calls below DO NOT
    429   1.5       gwr 	 * imply that uia.l is in host order.
    430   1.5       gwr 	 * In fact this needs it in net order.
    431   1.5       gwr 	 */
    432   1.4        pk 	*cp++ = ntohl(*ip++);
    433   1.4        pk 	*cp++ = ntohl(*ip++);
    434   1.4        pk 	*cp++ = ntohl(*ip++);
    435   1.4        pk 	*cp++ = ntohl(*ip++);
    436   1.4        pk 	ia->s_addr = uia.l;
    437   1.1       gwr 
    438  1.16     isaki 	return 0;
    439   1.1       gwr }
    440