Example #1
0
/*
 * Get client name, gateway address, then
 * get root and swap server:pathname info.
 * RPCs: bootparam/whoami, bootparam/getfile
 *
 * Use the old broadcast address for the WHOAMI
 * call because we do not yet know our netmask.
 * The server address returned by the WHOAMI call
 * is used for all subsequent bootparam RPCs.
 */
int
nfs_bootparam(struct nfs_diskless *nd, struct lwp *lwp, int *flags)
{
	struct ifnet *ifp = nd->nd_ifp;
	struct in_addr my_ip, arps_ip, gw_ip;
	struct sockaddr_in bp_sin;
	struct sockaddr_in *sin;
#ifndef NFS_BOOTPARAM_NOGATEWAY
	struct nfs_dlmount *gw_ndm = 0;
	char *p;
	u_int32_t mask;
#endif
	int error;

	/*
	 * Bring up the interface. (just set the "up" flag)
	 */
	error = nfs_boot_ifupdown(ifp, lwp, 1);
	if (error) {
		printf("nfs_boot: SIFFLAGS, error=%d\n", error);
		return (error);
	}

	error = EADDRNOTAVAIL;
#if NARP > 0
	if (ifp->if_type == IFT_ETHER || ifp->if_type == IFT_FDDI) {
		/*
		 * Do RARP for the interface address.
		 */
		error = revarpwhoarewe(ifp, &arps_ip, &my_ip);
	}
#endif
	if (error) {
		printf("revarp failed, error=%d\n", error);
		goto out;
	}

	if (!(*flags & NFS_BOOT_HAS_MYIP)) {
		nd->nd_myip.s_addr = my_ip.s_addr;
		printf("nfs_boot: client_addr=%s", inet_ntoa(my_ip));
		printf(" (RARP from %s)\n", inet_ntoa(arps_ip));
		*flags |= NFS_BOOT_HAS_MYIP;
	}

	/*
	 * Do enough of ifconfig(8) so that the chosen interface
	 * can talk to the servers.  (just set the address)
	 */
	error = nfs_boot_setaddress(ifp, lwp, my_ip.s_addr,
				    INADDR_ANY, INADDR_ANY);
	if (error) {
		printf("nfs_boot: set ifaddr, error=%d\n", error);
		goto out;
	}

	/*
	 * Get client name and gateway address.
	 * RPC: bootparam/whoami
	 * Use the old broadcast address for the WHOAMI
	 * call because we do not yet know our netmask.
	 * The server address returned by the WHOAMI call
	 * is used for all subsequent booptaram RPCs.
	 */
	sin = &bp_sin;
	memset((void *)sin, 0, sizeof(*sin));
	sin->sin_len = sizeof(*sin);
	sin->sin_family = AF_INET;
	sin->sin_addr.s_addr = INADDR_BROADCAST;

	/* Do the RPC/bootparam/whoami. */
	error = bp_whoami(sin, &my_ip, &gw_ip, lwp);
	if (error) {
		printf("nfs_boot: bootparam whoami, error=%d\n", error);
		goto delout;
	}
	*flags |= NFS_BOOT_HAS_SERVADDR | NFS_BOOT_HAS_SERVER;
	printf("nfs_boot: server_addr=%s\n", inet_ntoa(sin->sin_addr));
	printf("nfs_boot: hostname=%s\n", hostname);

	/*
	 * Now fetch the server:pathname strings and server IP
	 * for root and swap.  Missing swap is not fatal.
	 */
	error = bp_getfile(sin, "root", &nd->nd_root, lwp);
	if (error) {
		printf("nfs_boot: bootparam get root: %d\n", error);
		goto delout;
	}
	*flags |= NFS_BOOT_HAS_ROOTPATH;

#ifndef NFS_BOOTPARAM_NOGATEWAY
	gw_ndm = kmem_alloc(sizeof(*gw_ndm), KM_SLEEP);
	memset((void *)gw_ndm, 0, sizeof(*gw_ndm));
	error = bp_getfile(sin, "gateway", gw_ndm, lwp);
	if (error) {
		/* No gateway supplied. No error, but try fallback. */
		error = 0;
		goto nogwrepl;
	}
	sin = (struct sockaddr_in *) &gw_ndm->ndm_saddr;
	if (sin->sin_addr.s_addr == 0)
		goto out;	/* no gateway */

	/* OK, we have a gateway! */
	printf("nfs_boot: gateway=%s\n", inet_ntoa(sin->sin_addr));
	/* Just save it.  Caller adds the route. */
	nd->nd_gwip = sin->sin_addr;
	*flags |= NFS_BOOT_HAS_GWIP;

	/* Look for a mask string after the colon. */
	p = strchr(gw_ndm->ndm_host, ':');
	if (p == 0)
		goto out;	/* no netmask */
	/* have pathname */
	p++;	/* skip ':' */
	mask = inet_addr(p);	/* libkern */
	if (mask == 0)
		goto out;	/* no netmask */

	/* Have a netmask too!  Save it; update the I/F. */
	nd->nd_mask.s_addr = mask;
	*flags |= NFS_BOOT_HAS_MASK;
	printf("nfs_boot: my_mask=%s\n", inet_ntoa(nd->nd_mask));
	(void)  nfs_boot_deladdress(ifp, lwp, my_ip.s_addr);
	error = nfs_boot_setaddress(ifp, lwp, my_ip.s_addr,
				    mask, INADDR_ANY);
	if (error) {
		printf("nfs_boot: set ifmask, error=%d\n", error);
		goto out;
	}
	goto gwok;
nogwrepl:
#endif
#ifdef NFS_BOOT_GATEWAY
	/*
	 * Note: we normally ignore the gateway address returned
	 * by the "bootparam/whoami" RPC above, because many old
	 * bootparam servers supply a bogus gateway value.
	 *
	 * These deficiencies in the bootparam RPC interface are
	 * circumvented by using the bootparam/getfile RPC.  The
	 * parameter "gateway" is requested, and if its returned,
	 * we use the "server" part of the reply as the gateway,
	 * and use the "pathname" part of the reply as the mask.
	 * (The mask comes to us as a string.)
	 */
	if (gw_ip.s_addr) {
		/* Our caller will add the route. */
		nd->nd_gwip = gw_ip;
		*flags |= NFS_BOOT_HAS_GWIP;
	}
#endif

delout:
	if (error)
		(void) nfs_boot_deladdress(ifp, lwp, my_ip.s_addr);
out:
	if (error) {
		(void) nfs_boot_ifupdown(ifp, lwp, 0);
		nfs_boot_flushrt(ifp);
	}
#ifndef NFS_BOOTPARAM_NOGATEWAY
gwok:
	if (gw_ndm)
		kmem_free(gw_ndm, sizeof(*gw_ndm));
#endif
	if ((*flags & NFS_BOOT_ALLINFO) != NFS_BOOT_ALLINFO)
		return error ? error : EADDRNOTAVAIL;

	return (error);
}
Example #2
0
int
nfs_bootstatic(struct nfs_diskless *nd, struct lwp *lwp, int *_flags)
{
	struct ifnet *ifp = nd->nd_ifp;
	struct sockaddr_in *sin;
	int error, flags;

	if (nfs_bootstatic_callback)
		flags = (*nfs_bootstatic_callback)(nd);
	else
		flags = 0;

	if (flags & NFS_BOOT_NOSTATIC)
		return EOPNOTSUPP;

	if (flags == 0) {
#ifdef NFS_BOOTSTATIC_MYIP
		if (!(*_flags & NFS_BOOT_HAS_MYIP)) {
			nd->nd_myip.s_addr = inet_addr(NFS_BOOTSTATIC_MYIP);
			flags |= NFS_BOOT_HAS_MYIP;
		}
#endif
#ifdef NFS_BOOTSTATIC_GWIP
		if (!(*_flags & NFS_BOOT_HAS_GWIP)) {
			nd->nd_gwip.s_addr = inet_addr(NFS_BOOTSTATIC_GWIP);
			flags |= NFS_BOOT_HAS_GWIP;
		}
#endif
#ifdef NFS_BOOTSTATIC_MASK
		if (!(*_flags & NFS_BOOT_HAS_MASK)) {
			nd->nd_mask.s_addr = inet_addr(NFS_BOOTSTATIC_MASK);
			flags |= NFS_BOOT_HAS_MASK;
		}
#endif
#ifdef NFS_BOOTSTATIC_SERVADDR
		if (!(*_flags & NFS_BOOT_HAS_SERVADDR)) {
			sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
			memset((void *)sin, 0, sizeof(*sin));
			sin->sin_len = sizeof(*sin);
			sin->sin_family = AF_INET;
			sin->sin_addr.s_addr = inet_addr(NFS_BOOTSTATIC_SERVADDR);
			flags |= NFS_BOOT_HAS_SERVADDR;
		}
#endif
#ifdef NFS_BOOTSTATIC_SERVER
		if (!(*_flags & NFS_BOOT_HAS_SERVER)) {
			strncpy(nd->nd_root.ndm_host, NFS_BOOTSTATIC_SERVER,
				MNAMELEN);
			flags |= NFS_BOOT_HAS_SERVER;
			if (strchr(nd->nd_root.ndm_host, ':') != NULL)
				flags |= NFS_BOOT_HAS_ROOTPATH;
		}
#endif
	}

	*_flags |= flags;

	if (!(*_flags & NFS_BOOT_HAS_SERVADDR) && (*_flags & NFS_BOOT_HAS_SERVER)) {
		char rootserver[MNAMELEN];
		char *sep;

		sep = strchr(nd->nd_root.ndm_host, ':');
		strlcpy(rootserver, nd->nd_root.ndm_host,
			sep - nd->nd_root.ndm_host+1);

		sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
		memset((void *)sin, 0, sizeof(*sin));
		sin->sin_len = sizeof(*sin);
		sin->sin_family = AF_INET;
		sin->sin_addr.s_addr = inet_addr(rootserver);
		flags |= NFS_BOOT_HAS_SERVADDR;
		*_flags |= NFS_BOOT_HAS_SERVADDR;
	}

	if (flags & NFS_BOOT_HAS_MYIP)
		aprint_normal("nfs_boot: client_addr=%s\n",
		    inet_ntoa(nd->nd_myip));

	if (flags & NFS_BOOT_HAS_GWIP)
		aprint_normal("nfs_boot: gateway=%s\n",
		    inet_ntoa(nd->nd_gwip));

	if (flags & NFS_BOOT_HAS_MASK)
		aprint_normal("nfs_boot: netmask=%s\n",
		    inet_ntoa(nd->nd_mask));

	if (flags & NFS_BOOT_HAS_SERVADDR) {
		sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
		aprint_normal("nfs_boot: server=%s\n",
		    inet_ntoa(sin->sin_addr));
	}

	if (flags & NFS_BOOT_HAS_SERVER)
		aprint_normal("nfs_boot: root=%.*s\n", MNAMELEN,
		    nd->nd_root.ndm_host);

	/*
	 * Do enough of ifconfig(8) so that the chosen interface
	 * can talk to the servers.
	 */
	error = nfs_boot_setaddress(ifp, lwp,
	    *_flags & NFS_BOOT_HAS_MYIP ? nd->nd_myip.s_addr : INADDR_ANY,
	    *_flags & NFS_BOOT_HAS_MASK ? nd->nd_mask.s_addr : INADDR_ANY,
	    INADDR_ANY);
	if (error) {
		aprint_error("nfs_boot: set ifaddr, error=%d\n", error);
		goto out;
	}

	if ((*_flags & NFS_BOOT_ALLINFO) != NFS_BOOT_ALLINFO)
		return EADDRNOTAVAIL;

	error = 0;

 out:

	return error;
}