Date: Sun, 15 May 2011 00:11:00 +0000 (UTC) From: Rick Macklem <rmacklem@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org Subject: svn commit: r221933 - in stable/8/sys: amd64/amd64 conf fs/nfs fs/nfsclient i386/i386 modules/nfscl modules/nfsclient nfs nfsclient Message-ID: <201105150011.p4F0B0Ju020206@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: rmacklem Date: Sun May 15 00:11:00 2011 New Revision: 221933 URL: http://svn.freebsd.org/changeset/base/221933 Log: MFC: r221032,r221040,r221066 Move the files used for a diskless NFS root from sys/nfsclient to sys/nfs in preparation for them to be used by both NFS clients. Also, move the declaration of the three global data structures from sys/nfsclient/nfs_vfsops.c to sys/nfs/nfs_diskless.c so that they are defined when either client uses them. Also, make the changes to the experimental NFS client so that it uses the moved diskless NFS root files and fixes it so that it links for cases where "options NFS_ROOT" is not specified for the kernel config. Added: stable/8/sys/nfs/bootp_subr.c - copied unchanged from r221032, head/sys/nfs/bootp_subr.c stable/8/sys/nfs/krpc.h - copied unchanged from r221032, head/sys/nfs/krpc.h stable/8/sys/nfs/krpc_subr.c - copied unchanged from r221032, head/sys/nfs/krpc_subr.c stable/8/sys/nfs/nfs_diskless.c - copied unchanged from r221032, head/sys/nfs/nfs_diskless.c stable/8/sys/nfs/nfsdiskless.h - copied unchanged from r221032, head/sys/nfs/nfsdiskless.h Deleted: stable/8/sys/fs/nfsclient/nfsdiskless.h stable/8/sys/nfsclient/bootp_subr.c stable/8/sys/nfsclient/krpc.h stable/8/sys/nfsclient/krpc_subr.c stable/8/sys/nfsclient/nfs_diskless.c stable/8/sys/nfsclient/nfsdiskless.h Modified: stable/8/sys/amd64/amd64/genassym.c stable/8/sys/conf/files stable/8/sys/fs/nfs/nfs_var.h stable/8/sys/fs/nfsclient/nfs.h stable/8/sys/fs/nfsclient/nfs_clvfsops.c stable/8/sys/i386/i386/genassym.c stable/8/sys/modules/nfscl/Makefile stable/8/sys/modules/nfsclient/Makefile stable/8/sys/nfsclient/nfs_vfsops.c Directory Properties: stable/8/sys/ (props changed) stable/8/sys/amd64/include/xen/ (props changed) stable/8/sys/cddl/contrib/opensolaris/ (props changed) stable/8/sys/contrib/dev/acpica/ (props changed) stable/8/sys/contrib/pf/ (props changed) Modified: stable/8/sys/amd64/amd64/genassym.c ============================================================================== --- stable/8/sys/amd64/amd64/genassym.c Sat May 14 23:20:14 2011 (r221932) +++ stable/8/sys/amd64/amd64/genassym.c Sun May 15 00:11:00 2011 (r221933) @@ -65,7 +65,7 @@ __FBSDID("$FreeBSD$"); #include <netinet/in.h> #include <nfs/nfsproto.h> #include <nfsclient/nfs.h> -#include <nfsclient/nfsdiskless.h> +#include <nfs/nfsdiskless.h> #include <machine/apicreg.h> #include <machine/cpu.h> #include <machine/pcb.h> Modified: stable/8/sys/conf/files ============================================================================== --- stable/8/sys/conf/files Sat May 14 23:20:14 2011 (r221932) +++ stable/8/sys/conf/files Sun May 15 00:11:00 2011 (r221933) @@ -2673,12 +2673,12 @@ netsmb/smb_smb.c optional netsmb netsmb/smb_subr.c optional netsmb netsmb/smb_trantcp.c optional netsmb netsmb/smb_usr.c optional netsmb +nfs/bootp_subr.c optional bootp nfsclient | bootp nfscl +nfs/krpc_subr.c optional bootp nfsclient | bootp nfscl nfs/nfs_common.c optional nfsclient | nfsserver +nfs/nfs_diskless.c optional nfsclient nfs_root | nfscl nfs_root nfs/nfs_lock.c optional nfsclient | nfscl | nfslockd | nfsd -nfsclient/bootp_subr.c optional bootp nfsclient -nfsclient/krpc_subr.c optional bootp nfsclient nfsclient/nfs_bio.c optional nfsclient -nfsclient/nfs_diskless.c optional nfsclient nfs_root nfsclient/nfs_node.c optional nfsclient nfsclient/nfs_krpc.c optional nfsclient nfsclient/nfs_subs.c optional nfsclient Modified: stable/8/sys/fs/nfs/nfs_var.h ============================================================================== --- stable/8/sys/fs/nfs/nfs_var.h Sat May 14 23:20:14 2011 (r221932) +++ stable/8/sys/fs/nfs/nfs_var.h Sun May 15 00:11:00 2011 (r221933) @@ -41,7 +41,6 @@ struct ucred; struct nfscred; NFSPROC_T; struct buf; -struct nfs_diskless; struct sockaddr_in; struct nfs_dlmount; struct file; Modified: stable/8/sys/fs/nfsclient/nfs.h ============================================================================== --- stable/8/sys/fs/nfsclient/nfs.h Sat May 14 23:20:14 2011 (r221932) +++ stable/8/sys/fs/nfsclient/nfs.h Sun May 15 00:11:00 2011 (r221933) @@ -101,7 +101,6 @@ int ncl_fsinfo(struct nfsmount *, struct struct thread *); int ncl_init(struct vfsconf *); int ncl_uninit(struct vfsconf *); -int ncl_mountroot(struct mount *); void ncl_nfsiodnew(void); void ncl_nfsiodnew_tq(__unused void *, int); Modified: stable/8/sys/fs/nfsclient/nfs_clvfsops.c ============================================================================== --- stable/8/sys/fs/nfsclient/nfs_clvfsops.c Sat May 14 23:20:14 2011 (r221932) +++ stable/8/sys/fs/nfsclient/nfs_clvfsops.c Sun May 15 00:11:00 2011 (r221933) @@ -72,7 +72,7 @@ __FBSDID("$FreeBSD$"); #include <fs/nfsclient/nfsnode.h> #include <fs/nfsclient/nfsmount.h> #include <fs/nfsclient/nfs.h> -#include <fs/nfsclient/nfsdiskless.h> +#include <nfs/nfsdiskless.h> extern int nfscl_ticks; extern struct timeval nfsboottime; @@ -95,6 +95,7 @@ static int nfs_tprintf_delay = NFS_TPRIN SYSCTL_INT(_vfs_newnfs, NFS_TPRINTF_DELAY, downdelayinterval, CTLFLAG_RW, &nfs_tprintf_delay, 0, ""); +static int nfs_mountroot(struct mount *); static void nfs_sec_name(char *, int *); static void nfs_decode_args(struct mount *mp, struct nfsmount *nmp, struct nfs_args *argp, const char *, struct ucred *, @@ -134,23 +135,26 @@ VFS_SET(nfs_vfsops, newnfs, VFCF_NETWORK MODULE_VERSION(newnfs, 1); /* - * This structure must be filled in by a primary bootstrap or bootstrap - * server for a diskless/dataless machine. It is initialized below just - * to ensure that it is allocated to initialized data (.data not .bss). + * This structure is now defined in sys/nfs/nfs_diskless.c so that it + * can be shared by both NFS clients. It is declared here so that it + * will be defined for kernels built without NFS_ROOT, although it + * isn't used in that case. */ -struct nfs_diskless newnfs_diskless = { { { 0 } } }; -struct nfsv3_diskless newnfsv3_diskless = { { { 0 } } }; -int newnfs_diskless_valid = 0; +#if !defined(NFS_ROOT) && !defined(NFSCLIENT) +struct nfs_diskless nfs_diskless = { { { 0 } } }; +struct nfsv3_diskless nfsv3_diskless = { { { 0 } } }; +int nfs_diskless_valid = 0; +#endif SYSCTL_INT(_vfs_newnfs, OID_AUTO, diskless_valid, CTLFLAG_RD, - &newnfs_diskless_valid, 0, + &nfs_diskless_valid, 0, "Has the diskless struct been filled correctly"); SYSCTL_STRING(_vfs_newnfs, OID_AUTO, diskless_rootpath, CTLFLAG_RD, - newnfsv3_diskless.root_hostnam, 0, "Path to nfs root"); + nfsv3_diskless.root_hostnam, 0, "Path to nfs root"); SYSCTL_OPAQUE(_vfs_newnfs, OID_AUTO, diskless_rootaddr, CTLFLAG_RD, - &newnfsv3_diskless.root_saddr, sizeof newnfsv3_diskless.root_saddr, + &nfsv3_diskless.root_saddr, sizeof(nfsv3_diskless.root_saddr), "%Ssockaddr_in", "Diskless root nfs address"); @@ -228,29 +232,25 @@ static void nfs_convert_diskless(void) { - bcopy(&newnfs_diskless.myif, &newnfsv3_diskless.myif, - sizeof (struct ifaliasreq)); - bcopy(&newnfs_diskless.mygateway, &newnfsv3_diskless.mygateway, - sizeof (struct sockaddr_in)); - nfs_convert_oargs(&newnfsv3_diskless.root_args, - &newnfs_diskless.root_args); - if (newnfsv3_diskless.root_args.flags & NFSMNT_NFSV3) { - newnfsv3_diskless.root_fhsize = NFSX_MYFH; - bcopy(newnfs_diskless.root_fh, newnfsv3_diskless.root_fh, - NFSX_MYFH); + bcopy(&nfs_diskless.myif, &nfsv3_diskless.myif, + sizeof(struct ifaliasreq)); + bcopy(&nfs_diskless.mygateway, &nfsv3_diskless.mygateway, + sizeof(struct sockaddr_in)); + nfs_convert_oargs(&nfsv3_diskless.root_args,&nfs_diskless.root_args); + if (nfsv3_diskless.root_args.flags & NFSMNT_NFSV3) { + nfsv3_diskless.root_fhsize = NFSX_MYFH; + bcopy(nfs_diskless.root_fh, nfsv3_diskless.root_fh, NFSX_MYFH); } else { - newnfsv3_diskless.root_fhsize = NFSX_V2FH; - bcopy(newnfs_diskless.root_fh, newnfsv3_diskless.root_fh, - NFSX_V2FH); - } - bcopy(&newnfs_diskless.root_saddr,&newnfsv3_diskless.root_saddr, - sizeof(struct sockaddr_in)); - bcopy(newnfs_diskless.root_hostnam, newnfsv3_diskless.root_hostnam, - MNAMELEN); - newnfsv3_diskless.root_time = newnfs_diskless.root_time; - bcopy(newnfs_diskless.my_hostnam, newnfsv3_diskless.my_hostnam, - MAXHOSTNAMELEN); - newnfs_diskless_valid = 3; + nfsv3_diskless.root_fhsize = NFSX_V2FH; + bcopy(nfs_diskless.root_fh, nfsv3_diskless.root_fh, NFSX_V2FH); + } + bcopy(&nfs_diskless.root_saddr,&nfsv3_diskless.root_saddr, + sizeof(struct sockaddr_in)); + bcopy(nfs_diskless.root_hostnam, nfsv3_diskless.root_hostnam, MNAMELEN); + nfsv3_diskless.root_time = nfs_diskless.root_time; + bcopy(nfs_diskless.my_hostnam, nfsv3_diskless.my_hostnam, + MAXHOSTNAMELEN); + nfs_diskless_valid = 3; } /* @@ -356,12 +356,12 @@ ncl_fsinfo(struct nfsmount *nmp, struct /* * Mount a remote root fs via. nfs. This depends on the info in the - * newnfs_diskless structure that has been filled in properly by some primary + * nfs_diskless structure that has been filled in properly by some primary * bootstrap. * It goes something like this: * - do enough of "ifconfig" by calling ifioctl() so that the system * can talk to the server - * - If newnfs_diskless.mygateway is filled in, use that address as + * - If nfs_diskless.mygateway is filled in, use that address as * a default gateway. * - build the rootfs mount point and call mountnfs() to do the rest. * @@ -370,11 +370,11 @@ ncl_fsinfo(struct nfsmount *nmp, struct * nfs_mountroot() will be called once in the boot before any other NFS * client activity occurs. */ -int -ncl_mountroot(struct mount *mp) +static int +nfs_mountroot(struct mount *mp) { struct thread *td = curthread; - struct nfsv3_diskless *nd = &newnfsv3_diskless; + struct nfsv3_diskless *nd = &nfsv3_diskless; struct socket *so; struct vnode *vp; struct ifreq ir; @@ -389,9 +389,9 @@ ncl_mountroot(struct mount *mp) nfs_setup_diskless(); #endif - if (newnfs_diskless_valid == 0) + if (nfs_diskless_valid == 0) return (-1); - if (newnfs_diskless_valid == 1) + if (nfs_diskless_valid == 1) nfs_convert_diskless(); /* @@ -765,7 +765,7 @@ nfs_mount(struct mount *mp) td = curthread; if ((mp->mnt_flag & (MNT_ROOTFS | MNT_UPDATE)) == MNT_ROOTFS) { - error = ncl_mountroot(mp); + error = nfs_mountroot(mp); goto out; } Modified: stable/8/sys/i386/i386/genassym.c ============================================================================== --- stable/8/sys/i386/i386/genassym.c Sat May 14 23:20:14 2011 (r221932) +++ stable/8/sys/i386/i386/genassym.c Sun May 15 00:11:00 2011 (r221933) @@ -68,7 +68,7 @@ __FBSDID("$FreeBSD$"); #include <netinet/in.h> #include <nfs/nfsproto.h> #include <nfsclient/nfs.h> -#include <nfsclient/nfsdiskless.h> +#include <nfs/nfsdiskless.h> #ifdef DEV_APIC #include <machine/apicreg.h> #endif Modified: stable/8/sys/modules/nfscl/Makefile ============================================================================== --- stable/8/sys/modules/nfscl/Makefile Sat May 14 23:20:14 2011 (r221932) +++ stable/8/sys/modules/nfscl/Makefile Sun May 15 00:11:00 2011 (r221933) @@ -1,6 +1,6 @@ # $FreeBSD$ -.PATH: ${.CURDIR}/../../fs/nfsclient +.PATH: ${.CURDIR}/../../fs/nfsclient ${.CURDIR}/../../nfs KMOD= nfscl SRCS= vnode_if.h \ nfs_clrpcops.c \ @@ -22,4 +22,36 @@ SRCS= vnode_if.h \ opt_ufs.h \ opt_kgssapi.h +.if !defined(KERNBUILDDIR) +NFS_INET?= 1 # 0/1 - requires INET to be configured in kernel +NFS_INET6?= 1 # 0/1 - requires INET6 to be configured in kernel +NFS_ROOT?= 1 # 0/1 - requires NFS_ROOT to be configured in kernel + +.if ${NFS_INET} > 0 +opt_inet.h: + echo "#define INET 1" > ${.TARGET} +.endif + +.if ${NFS_INET6} > 0 +opt_inet6.h: + echo "#define INET6 1" > ${.TARGET} +.endif + +.if ${NFS_ROOT} > 0 +opt_nfsroot.h: + echo "#define NFS_ROOT 1" > ${.TARGET} +.endif +.else +OPT_NFS_ROOT!= cat ${KERNBUILDDIR}/opt_nfsroot.h +.if empty(OPT_NFS_ROOT) +NFS_ROOT= 0 +.else +NFS_ROOT= 1 +.endif +.endif + +.if ${NFS_ROOT} > 0 +SRCS+= nfs_diskless.c +.endif + .include <bsd.kmod.mk> Modified: stable/8/sys/modules/nfsclient/Makefile ============================================================================== --- stable/8/sys/modules/nfsclient/Makefile Sat May 14 23:20:14 2011 (r221932) +++ stable/8/sys/modules/nfsclient/Makefile Sun May 15 00:11:00 2011 (r221933) @@ -1,6 +1,6 @@ # $FreeBSD$ -.PATH: ${.CURDIR}/../../nfsclient ${.CURDIR}/../../rpc +.PATH: ${.CURDIR}/../../nfsclient ${.CURDIR}/../../nfs ${.CURDIR}/../../rpc KMOD= nfsclient SRCS= vnode_if.h \ Copied: stable/8/sys/nfs/bootp_subr.c (from r221032, head/sys/nfs/bootp_subr.c) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ stable/8/sys/nfs/bootp_subr.c Sun May 15 00:11:00 2011 (r221933, copy of r221032, head/sys/nfs/bootp_subr.c) @@ -0,0 +1,1867 @@ +/*- + * Copyright (c) 1995 Gordon Ross, Adam Glass + * Copyright (c) 1992 Regents of the University of California. + * All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Lawrence Berkeley Laboratory and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * based on: + * nfs/krpc_subr.c + * $NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $ + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include "opt_bootp.h" + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/jail.h> +#include <sys/kernel.h> +#include <sys/sockio.h> +#include <sys/malloc.h> +#include <sys/mount.h> +#include <sys/mbuf.h> +#include <sys/proc.h> +#include <sys/socket.h> +#include <sys/socketvar.h> +#include <sys/sysctl.h> +#include <sys/uio.h> + +#include <net/if.h> +#include <net/route.h> + +#include <netinet/in.h> +#include <net/if_types.h> +#include <net/if_dl.h> +#include <net/vnet.h> + +#include <nfs/nfsproto.h> +#include <nfsclient/nfs.h> +#include <nfs/nfsdiskless.h> +#include <nfs/krpc.h> +#include <nfs/xdr_subs.h> + + +#define BOOTP_MIN_LEN 300 /* Minimum size of bootp udp packet */ + +#ifndef BOOTP_SETTLE_DELAY +#define BOOTP_SETTLE_DELAY 3 +#endif + +/* + * What is the longest we will wait before re-sending a request? + * Note this is also the frequency of "RPC timeout" messages. + * The re-send loop count sup linearly to this maximum, so the + * first complaint will happen after (1+2+3+4+5)=15 seconds. + */ +#define MAX_RESEND_DELAY 5 /* seconds */ + +/* Definitions from RFC951 */ +struct bootp_packet { + u_int8_t op; + u_int8_t htype; + u_int8_t hlen; + u_int8_t hops; + u_int32_t xid; + u_int16_t secs; + u_int16_t flags; + struct in_addr ciaddr; + struct in_addr yiaddr; + struct in_addr siaddr; + struct in_addr giaddr; + unsigned char chaddr[16]; + char sname[64]; + char file[128]; + unsigned char vend[1222]; +}; + +struct bootpc_ifcontext { + struct bootpc_ifcontext *next; + struct bootp_packet call; + struct bootp_packet reply; + int replylen; + int overload; + struct socket *so; + struct ifreq ireq; + struct ifnet *ifp; + struct sockaddr_dl *sdl; + struct sockaddr_in myaddr; + struct sockaddr_in netmask; + struct sockaddr_in gw; + struct sockaddr_in broadcast; /* Different for each interface */ + int gotgw; + int gotnetmask; + int gotrootpath; + int outstanding; + int sentmsg; + u_int32_t xid; + enum { + IF_BOOTP_UNRESOLVED, + IF_BOOTP_RESOLVED, + IF_BOOTP_FAILED, + IF_DHCP_UNRESOLVED, + IF_DHCP_OFFERED, + IF_DHCP_RESOLVED, + IF_DHCP_FAILED, + } state; + int dhcpquerytype; /* dhcp type sent */ + struct in_addr dhcpserver; + int gotdhcpserver; +}; + +#define TAG_MAXLEN 1024 +struct bootpc_tagcontext { + char buf[TAG_MAXLEN + 1]; + int overload; + int badopt; + int badtag; + int foundopt; + int taglen; +}; + +struct bootpc_globalcontext { + struct bootpc_ifcontext *interfaces; + struct bootpc_ifcontext *lastinterface; + u_int32_t xid; + int gotrootpath; + int gotgw; + int ifnum; + int secs; + int starttime; + struct bootp_packet reply; + int replylen; + struct bootpc_ifcontext *setrootfs; + struct bootpc_ifcontext *sethostname; + struct bootpc_tagcontext tmptag; + struct bootpc_tagcontext tag; +}; + +#define IPPORT_BOOTPC 68 +#define IPPORT_BOOTPS 67 + +#define BOOTP_REQUEST 1 +#define BOOTP_REPLY 2 + +/* Common tags */ +#define TAG_PAD 0 /* Pad option, implicit length 1 */ +#define TAG_SUBNETMASK 1 /* RFC 950 subnet mask */ +#define TAG_ROUTERS 3 /* Routers (in order of preference) */ +#define TAG_HOSTNAME 12 /* Client host name */ +#define TAG_ROOT 17 /* Root path */ + +/* DHCP specific tags */ +#define TAG_OVERLOAD 52 /* Option Overload */ +#define TAG_MAXMSGSIZE 57 /* Maximum DHCP Message Size */ + +#define TAG_END 255 /* End Option (i.e. no more options) */ + +/* Overload values */ +#define OVERLOAD_FILE 1 +#define OVERLOAD_SNAME 2 + +/* Site specific tags: */ +#define TAG_ROOTOPTS 130 +#define TAG_COOKIE 134 /* ascii info for userland, via sysctl */ + +#define TAG_DHCP_MSGTYPE 53 +#define TAG_DHCP_REQ_ADDR 50 +#define TAG_DHCP_SERVERID 54 +#define TAG_DHCP_LEASETIME 51 + +#define TAG_VENDOR_INDENTIFIER 60 + +#define DHCP_NOMSG 0 +#define DHCP_DISCOVER 1 +#define DHCP_OFFER 2 +#define DHCP_REQUEST 3 +#define DHCP_ACK 5 + +/* NFS read/write block size */ +#ifndef BOOTP_BLOCKSIZE +#define BOOTP_BLOCKSIZE 8192 +#endif + +static char bootp_cookie[128]; +SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD, + bootp_cookie, 0, "Cookie (T134) supplied by bootp server"); + +/* mountd RPC */ +static int md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, + int *fhsizep, struct nfs_args *args, struct thread *td); +static int setfs(struct sockaddr_in *addr, char *path, char *p, + const struct in_addr *siaddr); +static int getdec(char **ptr); +static int getip(char **ptr, struct in_addr *ip); +static void mountopts(struct nfs_args *args, char *p); +static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len); +static int xdr_int_decode(struct mbuf **ptr, int *iptr); +static void print_in_addr(struct in_addr addr); +static void print_sin_addr(struct sockaddr_in *addr); +static void clear_sinaddr(struct sockaddr_in *sin); +static void allocifctx(struct bootpc_globalcontext *gctx); +static void bootpc_compose_query(struct bootpc_ifcontext *ifctx, + struct bootpc_globalcontext *gctx, struct thread *td); +static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx, + struct bootp_packet *bp, int len, int tag); +static void bootpc_tag_helper(struct bootpc_tagcontext *tctx, + unsigned char *start, int len, int tag); + +#ifdef BOOTP_DEBUG +void bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma); +void bootpboot_p_rtentry(struct rtentry *rt); +void bootpboot_p_tree(struct radix_node *rn); +void bootpboot_p_rtlist(void); +void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa); +void bootpboot_p_iflist(void); +#endif + +static int bootpc_call(struct bootpc_globalcontext *gctx, + struct thread *td); + +static int bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx, + struct bootpc_globalcontext *gctx, struct thread *td); + +static int bootpc_adjust_interface(struct bootpc_ifcontext *ifctx, + struct bootpc_globalcontext *gctx, struct thread *td); + +static void bootpc_decode_reply(struct nfsv3_diskless *nd, + struct bootpc_ifcontext *ifctx, + struct bootpc_globalcontext *gctx); + +static int bootpc_received(struct bootpc_globalcontext *gctx, + struct bootpc_ifcontext *ifctx); + +static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx); +static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx); +static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx); + +/* + * In order to have multiple active interfaces with address 0.0.0.0 + * and be able to send data to a selected interface, we perform + * some tricks: + * + * - The 'broadcast' address is different for each interface. + * + * - We temporarily add routing pointing 255.255.255.255 to the + * selected interface broadcast address, thus the packet sent + * goes to that interface. + */ + +#ifdef BOOTP_DEBUG +void +bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma) +{ + + if (sa == NULL) { + printf("(sockaddr *) <null>"); + return; + } + switch (sa->sa_family) { + case AF_INET: + { + struct sockaddr_in *sin; + + sin = (struct sockaddr_in *) sa; + printf("inet "); + print_sin_addr(sin); + if (ma != NULL) { + sin = (struct sockaddr_in *) ma; + printf(" mask "); + print_sin_addr(sin); + } + } + break; + case AF_LINK: + { + struct sockaddr_dl *sli; + int i; + + sli = (struct sockaddr_dl *) sa; + printf("link %.*s ", sli->sdl_nlen, sli->sdl_data); + for (i = 0; i < sli->sdl_alen; i++) { + if (i > 0) + printf(":"); + printf("%x", ((unsigned char *) LLADDR(sli))[i]); + } + } + break; + default: + printf("af%d", sa->sa_family); + } +} + +void +bootpboot_p_rtentry(struct rtentry *rt) +{ + + bootpboot_p_sa(rt_key(rt), rt_mask(rt)); + printf(" "); + bootpboot_p_sa(rt->rt_gateway, NULL); + printf(" "); + printf("flags %x", (unsigned short) rt->rt_flags); + printf(" %d", (int) rt->rt_rmx.rmx_expire); + printf(" %s\n", rt->rt_ifp->if_xname); +} + +void +bootpboot_p_tree(struct radix_node *rn) +{ + + while (rn != NULL) { + if (rn->rn_bit < 0) { + if ((rn->rn_flags & RNF_ROOT) != 0) { + } else { + bootpboot_p_rtentry((struct rtentry *) rn); + } + rn = rn->rn_dupedkey; + } else { + bootpboot_p_tree(rn->rn_left); + bootpboot_p_tree(rn->rn_right); + return; + } + } +} + +void +bootpboot_p_rtlist(void) +{ + struct radix_node_head *rnh; + + printf("Routing table:\n"); + rnh = rt_tables_get_rnh(0, AF_INET); + if (rnh == NULL) + return; + RADIX_NODE_HEAD_RLOCK(rnh); /* could sleep XXX */ + bootpboot_p_tree(rnh->rnh_treetop); + RADIX_NODE_HEAD_RUNLOCK(rnh); +} + +void +bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa) +{ + + printf("%s flags %x, addr ", + ifp->if_xname, ifp->if_flags); + print_sin_addr((struct sockaddr_in *) ifa->ifa_addr); + printf(", broadcast "); + print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr); + printf(", netmask "); + print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask); + printf("\n"); +} + +void +bootpboot_p_iflist(void) +{ + struct ifnet *ifp; + struct ifaddr *ifa; + + printf("Interface list:\n"); + IFNET_RLOCK(); + for (ifp = TAILQ_FIRST(&V_ifnet); + ifp != NULL; + ifp = TAILQ_NEXT(ifp, if_link)) { + for (ifa = TAILQ_FIRST(&ifp->if_addrhead); + ifa != NULL; + ifa = TAILQ_NEXT(ifa, ifa_link)) + if (ifa->ifa_addr->sa_family == AF_INET) + bootpboot_p_if(ifp, ifa); + } + IFNET_RUNLOCK(); +} +#endif /* defined(BOOTP_DEBUG) */ + +static void +clear_sinaddr(struct sockaddr_in *sin) +{ + + bzero(sin, sizeof(*sin)); + sin->sin_len = sizeof(*sin); + sin->sin_family = AF_INET; + sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */ + sin->sin_port = 0; +} + +static void +allocifctx(struct bootpc_globalcontext *gctx) +{ + struct bootpc_ifcontext *ifctx; + ifctx = (struct bootpc_ifcontext *) malloc(sizeof(*ifctx), + M_TEMP, M_WAITOK | M_ZERO); + if (ifctx == NULL) + panic("Failed to allocate bootp interface context structure"); + + ifctx->xid = gctx->xid; +#ifdef BOOTP_NO_DHCP + ifctx->state = IF_BOOTP_UNRESOLVED; +#else + ifctx->state = IF_DHCP_UNRESOLVED; +#endif + gctx->xid += 0x100; + if (gctx->interfaces != NULL) + gctx->lastinterface->next = ifctx; + else + gctx->interfaces = ifctx; + gctx->lastinterface = ifctx; +} + +static __inline int +bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx) +{ + + if (ifctx->state == IF_BOOTP_RESOLVED || + ifctx->state == IF_DHCP_RESOLVED) + return 1; + return 0; +} + +static __inline int +bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx) +{ + + if (ifctx->state == IF_BOOTP_UNRESOLVED || + ifctx->state == IF_DHCP_UNRESOLVED) + return 1; + return 0; +} + +static __inline int +bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx) +{ + + if (ifctx->state == IF_BOOTP_FAILED || + ifctx->state == IF_DHCP_FAILED) + return 1; + return 0; +} + +static int +bootpc_received(struct bootpc_globalcontext *gctx, + struct bootpc_ifcontext *ifctx) +{ + unsigned char dhcpreplytype; + char *p; + + /* + * Need timeout for fallback to less + * desirable alternative. + */ + + /* This call used for the side effect (badopt flag) */ + (void) bootpc_tag(&gctx->tmptag, &gctx->reply, + gctx->replylen, + TAG_END); + + /* If packet is invalid, ignore it */ + if (gctx->tmptag.badopt != 0) + return 0; + + p = bootpc_tag(&gctx->tmptag, &gctx->reply, + gctx->replylen, TAG_DHCP_MSGTYPE); + if (p != NULL) + dhcpreplytype = *p; + else + dhcpreplytype = DHCP_NOMSG; + + switch (ifctx->dhcpquerytype) { + case DHCP_DISCOVER: + if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */ +#ifndef BOOTP_FORCE_DHCP + && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */ +#endif + ) + return 0; + break; + case DHCP_REQUEST: + if (dhcpreplytype != DHCP_ACK) + return 0; + case DHCP_NOMSG: + break; + } + + /* Ignore packet unless it gives us a root tag we didn't have */ + + if ((ifctx->state == IF_BOOTP_RESOLVED || + (ifctx->dhcpquerytype == DHCP_DISCOVER && + (ifctx->state == IF_DHCP_OFFERED || + ifctx->state == IF_DHCP_RESOLVED))) && + (bootpc_tag(&gctx->tmptag, &ifctx->reply, + ifctx->replylen, + TAG_ROOT) != NULL || + bootpc_tag(&gctx->tmptag, &gctx->reply, + gctx->replylen, + TAG_ROOT) == NULL)) + return 0; + + bcopy(&gctx->reply, &ifctx->reply, gctx->replylen); + ifctx->replylen = gctx->replylen; + + /* XXX: Only reset if 'perfect' response */ + if (ifctx->state == IF_BOOTP_UNRESOLVED) + ifctx->state = IF_BOOTP_RESOLVED; + else if (ifctx->state == IF_DHCP_UNRESOLVED && + ifctx->dhcpquerytype == DHCP_DISCOVER) { + if (dhcpreplytype == DHCP_OFFER) + ifctx->state = IF_DHCP_OFFERED; + else + ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */ + } else if (ifctx->state == IF_DHCP_OFFERED && + ifctx->dhcpquerytype == DHCP_REQUEST) + ifctx->state = IF_DHCP_RESOLVED; + + + if (ifctx->dhcpquerytype == DHCP_DISCOVER && + ifctx->state != IF_BOOTP_RESOLVED) { + p = bootpc_tag(&gctx->tmptag, &ifctx->reply, + ifctx->replylen, TAG_DHCP_SERVERID); + if (p != NULL && gctx->tmptag.taglen == 4) { + memcpy(&ifctx->dhcpserver, p, 4); + ifctx->gotdhcpserver = 1; + } else + ifctx->gotdhcpserver = 0; + return 1; + } + + ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply, + ifctx->replylen, + TAG_ROOT) != NULL); + ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply, + ifctx->replylen, + TAG_ROUTERS) != NULL); + ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply, + ifctx->replylen, + TAG_SUBNETMASK) != NULL); + return 1; +} + +static int +bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td) +{ + struct socket *so; + struct sockaddr_in *sin, dst; + struct uio auio; + struct sockopt sopt; + struct iovec aio; + int error, on, rcvflg, timo, len; + time_t atimo; + time_t rtimo; + struct timeval tv; + struct bootpc_ifcontext *ifctx; + int outstanding; + int gotrootpath; + int retry; + const char *s; + + /* + * Create socket and set its recieve timeout. + */ + error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td->td_ucred, td); + if (error != 0) + goto out0; + + tv.tv_sec = 1; + tv.tv_usec = 0; + bzero(&sopt, sizeof(sopt)); + sopt.sopt_dir = SOPT_SET; + sopt.sopt_level = SOL_SOCKET; + sopt.sopt_name = SO_RCVTIMEO; + sopt.sopt_val = &tv; + sopt.sopt_valsize = sizeof tv; + + error = sosetopt(so, &sopt); + if (error != 0) + goto out; + + /* + * Enable broadcast. + */ + on = 1; + sopt.sopt_name = SO_BROADCAST; + sopt.sopt_val = &on; + sopt.sopt_valsize = sizeof on; + + error = sosetopt(so, &sopt); + if (error != 0) + goto out; + + /* + * Disable routing. + */ + + on = 1; + sopt.sopt_name = SO_DONTROUTE; + sopt.sopt_val = &on; + sopt.sopt_valsize = sizeof on; + + error = sosetopt(so, &sopt); + if (error != 0) + goto out; + + /* + * Bind the local endpoint to a bootp client port. + */ + sin = &dst; + clear_sinaddr(sin); + sin->sin_port = htons(IPPORT_BOOTPC); + error = sobind(so, (struct sockaddr *)sin, td); + if (error != 0) { + printf("bind failed\n"); + goto out; + } + + /* + * Setup socket address for the server. + */ + sin = &dst; + clear_sinaddr(sin); + sin->sin_addr.s_addr = INADDR_BROADCAST; + sin->sin_port = htons(IPPORT_BOOTPS); + + /* + * Send it, repeatedly, until a reply is received, + * but delay each re-send by an increasing amount. + * If the delay hits the maximum, start complaining. + */ + timo = 0; + rtimo = 0; + for (;;) { + + outstanding = 0; + gotrootpath = 0; + + for (ifctx = gctx->interfaces; + ifctx != NULL; + ifctx = ifctx->next) { + if (bootpc_ifctx_isresolved(ifctx) != 0 && + bootpc_tag(&gctx->tmptag, &ifctx->reply, + ifctx->replylen, + TAG_ROOT) != NULL) + gotrootpath = 1; + } + + for (ifctx = gctx->interfaces; + ifctx != NULL; + ifctx = ifctx->next) { + ifctx->outstanding = 0; + if (bootpc_ifctx_isresolved(ifctx) != 0 && + gotrootpath != 0) { + continue; + } + if (bootpc_ifctx_isfailed(ifctx) != 0) + continue; + + outstanding++; + ifctx->outstanding = 1; + + /* Proceed to next step in DHCP negotiation */ + if ((ifctx->state == IF_DHCP_OFFERED && + ifctx->dhcpquerytype != DHCP_REQUEST) || + (ifctx->state == IF_DHCP_UNRESOLVED && + ifctx->dhcpquerytype != DHCP_DISCOVER) || + (ifctx->state == IF_BOOTP_UNRESOLVED && + ifctx->dhcpquerytype != DHCP_NOMSG)) { + ifctx->sentmsg = 0; + bootpc_compose_query(ifctx, gctx, td); + } + + /* Send BOOTP request (or re-send). */ + + if (ifctx->sentmsg == 0) { + switch(ifctx->dhcpquerytype) { + case DHCP_DISCOVER: + s = "DHCP Discover"; + break; + case DHCP_REQUEST: + s = "DHCP Request"; + break; + case DHCP_NOMSG: + default: + s = "BOOTP Query"; + break; + } + printf("Sending %s packet from " + "interface %s (%*D)\n", + s, + ifctx->ireq.ifr_name, + ifctx->sdl->sdl_alen, + (unsigned char *) LLADDR(ifctx->sdl), + ":"); + ifctx->sentmsg = 1; + } + + aio.iov_base = (caddr_t) &ifctx->call; + aio.iov_len = sizeof(ifctx->call); + *** DIFF OUTPUT TRUNCATED AT 1000 LINES ***
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201105150011.p4F0B0Ju020206>