Home | History | Annotate | Line # | Download | only in common
bootp_subr.c revision 1.1.1.2
      1      1.1  dholland /*	$NetBSD: bootp_subr.c,v 1.1.1.2 2016/11/18 07:49:12 pgoyette Exp $	*/
      2      1.1  dholland /*-
      3      1.1  dholland  * Copyright (c) 1995 Gordon Ross, Adam Glass
      4      1.1  dholland  * Copyright (c) 1992 Regents of the University of California.
      5      1.1  dholland  * All rights reserved.
      6      1.1  dholland  *
      7      1.1  dholland  * This software was developed by the Computer Systems Engineering group
      8      1.1  dholland  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9      1.1  dholland  * contributed to Berkeley.
     10      1.1  dholland  *
     11      1.1  dholland  * Redistribution and use in source and binary forms, with or without
     12      1.1  dholland  * modification, are permitted provided that the following conditions
     13      1.1  dholland  * are met:
     14      1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     15      1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     16      1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     18      1.1  dholland  *    documentation and/or other materials provided with the distribution.
     19      1.1  dholland  * 3. All advertising materials mentioning features or use of this software
     20      1.1  dholland  *    must display the following acknowledgement:
     21      1.1  dholland  *	This product includes software developed by the University of
     22      1.1  dholland  *	California, Lawrence Berkeley Laboratory and its contributors.
     23      1.1  dholland  * 4. Neither the name of the University nor the names of its contributors
     24      1.1  dholland  *    may be used to endorse or promote products derived from this software
     25      1.1  dholland  *    without specific prior written permission.
     26      1.1  dholland  *
     27      1.1  dholland  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28      1.1  dholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29      1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30      1.1  dholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31      1.1  dholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32      1.1  dholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33      1.1  dholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34      1.1  dholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35      1.1  dholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36      1.1  dholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37      1.1  dholland  * SUCH DAMAGE.
     38      1.1  dholland  *
     39      1.1  dholland  * based on:
     40      1.1  dholland  *      nfs/krpc_subr.c
     41      1.1  dholland  *	NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp
     42      1.1  dholland  */
     43      1.1  dholland 
     44      1.1  dholland #include <sys/cdefs.h>
     45  1.1.1.2  pgoyette /* __FBSDID("FreeBSD: head/sys/nfs/bootp_subr.c 297326 2016-03-27 23:16:37Z ian "); */
     46      1.1  dholland __RCSID("$NetBSD: bootp_subr.c,v 1.1.1.2 2016/11/18 07:49:12 pgoyette Exp $");
     47      1.1  dholland 
     48      1.1  dholland #include "opt_bootp.h"
     49      1.1  dholland #include "opt_nfs.h"
     50      1.1  dholland #include "opt_rootdevname.h"
     51      1.1  dholland 
     52      1.1  dholland #include <sys/param.h>
     53      1.1  dholland #include <sys/systm.h>
     54  1.1.1.2  pgoyette #include <sys/endian.h>
     55      1.1  dholland #include <sys/jail.h>
     56      1.1  dholland #include <sys/kernel.h>
     57      1.1  dholland #include <sys/sockio.h>
     58      1.1  dholland #include <sys/malloc.h>
     59      1.1  dholland #include <sys/mount.h>
     60      1.1  dholland #include <sys/mbuf.h>
     61      1.1  dholland #include <sys/proc.h>
     62      1.1  dholland #include <sys/reboot.h>
     63      1.1  dholland #include <sys/socket.h>
     64      1.1  dholland #include <sys/socketvar.h>
     65      1.1  dholland #include <sys/sysctl.h>
     66      1.1  dholland #include <sys/uio.h>
     67      1.1  dholland 
     68      1.1  dholland #include <net/if.h>
     69  1.1.1.2  pgoyette #include <net/if_var.h>
     70      1.1  dholland #include <net/route.h>
     71  1.1.1.2  pgoyette #ifdef BOOTP_DEBUG
     72  1.1.1.2  pgoyette #include <net/route_var.h>
     73  1.1.1.2  pgoyette #endif
     74      1.1  dholland 
     75      1.1  dholland #include <netinet/in.h>
     76      1.1  dholland #include <netinet/in_var.h>
     77      1.1  dholland #include <net/if_types.h>
     78      1.1  dholland #include <net/if_dl.h>
     79      1.1  dholland #include <net/vnet.h>
     80      1.1  dholland 
     81      1.1  dholland #include <nfs/nfsproto.h>
     82      1.1  dholland #include <nfsclient/nfs.h>
     83      1.1  dholland #include <nfs/nfsdiskless.h>
     84      1.1  dholland #include <nfs/krpc.h>
     85      1.1  dholland #include <nfs/xdr_subs.h>
     86      1.1  dholland 
     87      1.1  dholland 
     88      1.1  dholland #define BOOTP_MIN_LEN		300	/* Minimum size of bootp udp packet */
     89      1.1  dholland 
     90      1.1  dholland #ifndef BOOTP_SETTLE_DELAY
     91      1.1  dholland #define BOOTP_SETTLE_DELAY 3
     92      1.1  dholland #endif
     93      1.1  dholland 
     94      1.1  dholland /*
     95      1.1  dholland  * Wait 10 seconds for interface appearance
     96      1.1  dholland  * USB ethernet adapters might require some time to pop up
     97      1.1  dholland  */
     98      1.1  dholland #ifndef	BOOTP_IFACE_WAIT_TIMEOUT
     99      1.1  dholland #define	BOOTP_IFACE_WAIT_TIMEOUT	10
    100      1.1  dholland #endif
    101      1.1  dholland 
    102      1.1  dholland /*
    103      1.1  dholland  * What is the longest we will wait before re-sending a request?
    104      1.1  dholland  * Note this is also the frequency of "RPC timeout" messages.
    105      1.1  dholland  * The re-send loop count sup linearly to this maximum, so the
    106      1.1  dholland  * first complaint will happen after (1+2+3+4+5)=15 seconds.
    107      1.1  dholland  */
    108      1.1  dholland #define	MAX_RESEND_DELAY 5	/* seconds */
    109      1.1  dholland 
    110      1.1  dholland /* Definitions from RFC951 */
    111      1.1  dholland struct bootp_packet {
    112      1.1  dholland 	u_int8_t op;
    113      1.1  dholland 	u_int8_t htype;
    114      1.1  dholland 	u_int8_t hlen;
    115      1.1  dholland 	u_int8_t hops;
    116      1.1  dholland 	u_int32_t xid;
    117      1.1  dholland 	u_int16_t secs;
    118      1.1  dholland 	u_int16_t flags;
    119      1.1  dholland 	struct in_addr ciaddr;
    120      1.1  dholland 	struct in_addr yiaddr;
    121      1.1  dholland 	struct in_addr siaddr;
    122      1.1  dholland 	struct in_addr giaddr;
    123      1.1  dholland 	unsigned char chaddr[16];
    124      1.1  dholland 	char sname[64];
    125      1.1  dholland 	char file[128];
    126      1.1  dholland 	unsigned char vend[1222];
    127      1.1  dholland };
    128      1.1  dholland 
    129      1.1  dholland struct bootpc_ifcontext {
    130      1.1  dholland 	STAILQ_ENTRY(bootpc_ifcontext) next;
    131      1.1  dholland 	struct bootp_packet call;
    132      1.1  dholland 	struct bootp_packet reply;
    133      1.1  dholland 	int replylen;
    134      1.1  dholland 	int overload;
    135      1.1  dholland 	union {
    136      1.1  dholland 		struct ifreq _ifreq;
    137      1.1  dholland 		struct in_aliasreq _in_alias_req;
    138      1.1  dholland 	} _req;
    139      1.1  dholland #define	ireq	_req._ifreq
    140      1.1  dholland #define	iareq	_req._in_alias_req
    141      1.1  dholland 	struct ifnet *ifp;
    142      1.1  dholland 	struct sockaddr_dl *sdl;
    143      1.1  dholland 	struct sockaddr_in myaddr;
    144      1.1  dholland 	struct sockaddr_in netmask;
    145      1.1  dholland 	struct sockaddr_in gw;
    146      1.1  dholland 	int gotgw;
    147      1.1  dholland 	int gotnetmask;
    148      1.1  dholland 	int gotrootpath;
    149      1.1  dholland 	int outstanding;
    150      1.1  dholland 	int sentmsg;
    151      1.1  dholland 	u_int32_t xid;
    152      1.1  dholland 	enum {
    153      1.1  dholland 		IF_BOOTP_UNRESOLVED,
    154      1.1  dholland 		IF_BOOTP_RESOLVED,
    155      1.1  dholland 		IF_BOOTP_FAILED,
    156      1.1  dholland 		IF_DHCP_UNRESOLVED,
    157      1.1  dholland 		IF_DHCP_OFFERED,
    158      1.1  dholland 		IF_DHCP_RESOLVED,
    159      1.1  dholland 		IF_DHCP_FAILED,
    160      1.1  dholland 	} state;
    161      1.1  dholland 	int dhcpquerytype;		/* dhcp type sent */
    162      1.1  dholland 	struct in_addr dhcpserver;
    163      1.1  dholland 	int gotdhcpserver;
    164  1.1.1.2  pgoyette 	uint16_t mtu;
    165      1.1  dholland };
    166      1.1  dholland 
    167      1.1  dholland #define TAG_MAXLEN 1024
    168      1.1  dholland struct bootpc_tagcontext {
    169      1.1  dholland 	char buf[TAG_MAXLEN + 1];
    170      1.1  dholland 	int overload;
    171      1.1  dholland 	int badopt;
    172      1.1  dholland 	int badtag;
    173      1.1  dholland 	int foundopt;
    174      1.1  dholland 	int taglen;
    175      1.1  dholland };
    176      1.1  dholland 
    177      1.1  dholland struct bootpc_globalcontext {
    178      1.1  dholland 	STAILQ_HEAD(, bootpc_ifcontext) interfaces;
    179      1.1  dholland 	u_int32_t xid;
    180      1.1  dholland 	int any_root_overrides;
    181      1.1  dholland 	int gotrootpath;
    182      1.1  dholland 	int gotgw;
    183      1.1  dholland 	int ifnum;
    184      1.1  dholland 	int secs;
    185      1.1  dholland 	int starttime;
    186      1.1  dholland 	struct bootp_packet reply;
    187      1.1  dholland 	int replylen;
    188      1.1  dholland 	struct bootpc_ifcontext *setrootfs;
    189      1.1  dholland 	struct bootpc_ifcontext *sethostname;
    190      1.1  dholland 	struct bootpc_tagcontext tmptag;
    191      1.1  dholland 	struct bootpc_tagcontext tag;
    192      1.1  dholland };
    193      1.1  dholland 
    194      1.1  dholland #define IPPORT_BOOTPC 68
    195      1.1  dholland #define IPPORT_BOOTPS 67
    196      1.1  dholland 
    197      1.1  dholland #define BOOTP_REQUEST 1
    198      1.1  dholland #define BOOTP_REPLY 2
    199      1.1  dholland 
    200      1.1  dholland /* Common tags */
    201      1.1  dholland #define TAG_PAD		  0  /* Pad option, implicit length 1 */
    202      1.1  dholland #define TAG_SUBNETMASK	  1  /* RFC 950 subnet mask */
    203      1.1  dholland #define TAG_ROUTERS	  3  /* Routers (in order of preference) */
    204      1.1  dholland #define TAG_HOSTNAME	 12  /* Client host name */
    205      1.1  dholland #define TAG_ROOT	 17  /* Root path */
    206  1.1.1.2  pgoyette #define TAG_INTF_MTU	 26  /* Interface MTU Size (RFC2132) */
    207      1.1  dholland 
    208      1.1  dholland /* DHCP specific tags */
    209      1.1  dholland #define TAG_OVERLOAD	 52  /* Option Overload */
    210      1.1  dholland #define TAG_MAXMSGSIZE   57  /* Maximum DHCP Message Size */
    211      1.1  dholland 
    212      1.1  dholland #define TAG_END		255  /* End Option (i.e. no more options) */
    213      1.1  dholland 
    214      1.1  dholland /* Overload values */
    215      1.1  dholland #define OVERLOAD_FILE     1
    216      1.1  dholland #define OVERLOAD_SNAME    2
    217      1.1  dholland 
    218      1.1  dholland /* Site specific tags: */
    219      1.1  dholland #define TAG_ROOTOPTS	130
    220      1.1  dholland #define TAG_COOKIE	134	/* ascii info for userland, via sysctl */
    221      1.1  dholland 
    222      1.1  dholland #define TAG_DHCP_MSGTYPE 53
    223      1.1  dholland #define TAG_DHCP_REQ_ADDR 50
    224      1.1  dholland #define TAG_DHCP_SERVERID 54
    225      1.1  dholland #define TAG_DHCP_LEASETIME 51
    226      1.1  dholland 
    227      1.1  dholland #define TAG_VENDOR_INDENTIFIER 60
    228      1.1  dholland 
    229      1.1  dholland #define DHCP_NOMSG    0
    230      1.1  dholland #define DHCP_DISCOVER 1
    231      1.1  dholland #define DHCP_OFFER    2
    232      1.1  dholland #define DHCP_REQUEST  3
    233      1.1  dholland #define DHCP_ACK      5
    234      1.1  dholland 
    235      1.1  dholland /* NFS read/write block size */
    236      1.1  dholland #ifndef BOOTP_BLOCKSIZE
    237      1.1  dholland #define	BOOTP_BLOCKSIZE	8192
    238      1.1  dholland #endif
    239      1.1  dholland 
    240      1.1  dholland static char bootp_cookie[128];
    241      1.1  dholland static struct socket *bootp_so;
    242      1.1  dholland SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
    243      1.1  dholland 	bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
    244      1.1  dholland 
    245      1.1  dholland /* mountd RPC */
    246      1.1  dholland static int	md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp,
    247      1.1  dholland 		    int *fhsizep, struct nfs_args *args, struct thread *td);
    248      1.1  dholland static int	setfs(struct sockaddr_in *addr, char *path, char *p,
    249      1.1  dholland 		    const struct in_addr *siaddr);
    250      1.1  dholland static int	getdec(char **ptr);
    251      1.1  dholland static int	getip(char **ptr, struct in_addr *ip);
    252      1.1  dholland static void	mountopts(struct nfs_args *args, char *p);
    253      1.1  dholland static int	xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
    254      1.1  dholland static int	xdr_int_decode(struct mbuf **ptr, int *iptr);
    255      1.1  dholland static void	print_in_addr(struct in_addr addr);
    256      1.1  dholland static void	print_sin_addr(struct sockaddr_in *addr);
    257      1.1  dholland static void	clear_sinaddr(struct sockaddr_in *sin);
    258      1.1  dholland static void	allocifctx(struct bootpc_globalcontext *gctx);
    259      1.1  dholland static void	bootpc_compose_query(struct bootpc_ifcontext *ifctx,
    260      1.1  dholland 		    struct thread *td);
    261      1.1  dholland static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
    262      1.1  dholland 		    struct bootp_packet *bp, int len, int tag);
    263      1.1  dholland static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
    264      1.1  dholland 		    unsigned char *start, int len, int tag);
    265      1.1  dholland 
    266      1.1  dholland #ifdef BOOTP_DEBUG
    267      1.1  dholland void bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma);
    268      1.1  dholland void bootpboot_p_rtentry(struct rtentry *rt);
    269      1.1  dholland void bootpboot_p_tree(struct radix_node *rn);
    270      1.1  dholland void bootpboot_p_rtlist(void);
    271      1.1  dholland void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
    272      1.1  dholland void bootpboot_p_iflist(void);
    273      1.1  dholland #endif
    274      1.1  dholland 
    275      1.1  dholland static int	bootpc_call(struct bootpc_globalcontext *gctx,
    276      1.1  dholland 		    struct thread *td);
    277      1.1  dholland 
    278      1.1  dholland static void	bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
    279      1.1  dholland 		    struct thread *td);
    280      1.1  dholland 
    281  1.1.1.2  pgoyette static void	bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
    282      1.1  dholland 		    struct bootpc_globalcontext *gctx, struct thread *td);
    283      1.1  dholland 
    284      1.1  dholland static void	bootpc_decode_reply(struct nfsv3_diskless *nd,
    285      1.1  dholland 		    struct bootpc_ifcontext *ifctx,
    286      1.1  dholland 		    struct bootpc_globalcontext *gctx);
    287      1.1  dholland 
    288      1.1  dholland static int	bootpc_received(struct bootpc_globalcontext *gctx,
    289      1.1  dholland 		    struct bootpc_ifcontext *ifctx);
    290      1.1  dholland 
    291      1.1  dholland static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
    292      1.1  dholland static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
    293      1.1  dholland static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
    294      1.1  dholland 
    295      1.1  dholland /*
    296      1.1  dholland  * In order to have multiple active interfaces with address 0.0.0.0
    297      1.1  dholland  * and be able to send data to a selected interface, we first set
    298      1.1  dholland  * mask to /8 on all interfaces, and temporarily set it to /0 when
    299      1.1  dholland  * doing sosend().
    300      1.1  dholland  */
    301      1.1  dholland 
    302      1.1  dholland #ifdef BOOTP_DEBUG
    303      1.1  dholland void
    304      1.1  dholland bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
    305      1.1  dholland {
    306      1.1  dholland 
    307      1.1  dholland 	if (sa == NULL) {
    308      1.1  dholland 		printf("(sockaddr *) <null>");
    309      1.1  dholland 		return;
    310      1.1  dholland 	}
    311      1.1  dholland 	switch (sa->sa_family) {
    312      1.1  dholland 	case AF_INET:
    313      1.1  dholland 	{
    314      1.1  dholland 		struct sockaddr_in *sin;
    315      1.1  dholland 
    316      1.1  dholland 		sin = (struct sockaddr_in *) sa;
    317      1.1  dholland 		printf("inet ");
    318      1.1  dholland 		print_sin_addr(sin);
    319      1.1  dholland 		if (ma != NULL) {
    320      1.1  dholland 			sin = (struct sockaddr_in *) ma;
    321      1.1  dholland 			printf(" mask ");
    322      1.1  dholland 			print_sin_addr(sin);
    323      1.1  dholland 		}
    324      1.1  dholland 	}
    325      1.1  dholland 	break;
    326      1.1  dholland 	case AF_LINK:
    327      1.1  dholland 	{
    328      1.1  dholland 		struct sockaddr_dl *sli;
    329      1.1  dholland 		int i;
    330      1.1  dholland 
    331      1.1  dholland 		sli = (struct sockaddr_dl *) sa;
    332      1.1  dholland 		printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
    333      1.1  dholland 		for (i = 0; i < sli->sdl_alen; i++) {
    334      1.1  dholland 			if (i > 0)
    335      1.1  dholland 				printf(":");
    336      1.1  dholland 			printf("%x", ((unsigned char *) LLADDR(sli))[i]);
    337      1.1  dholland 		}
    338      1.1  dholland 	}
    339      1.1  dholland 	break;
    340      1.1  dholland 	default:
    341      1.1  dholland 		printf("af%d", sa->sa_family);
    342      1.1  dholland 	}
    343      1.1  dholland }
    344      1.1  dholland 
    345      1.1  dholland void
    346      1.1  dholland bootpboot_p_rtentry(struct rtentry *rt)
    347      1.1  dholland {
    348      1.1  dholland 
    349      1.1  dholland 	bootpboot_p_sa(rt_key(rt), rt_mask(rt));
    350      1.1  dholland 	printf(" ");
    351      1.1  dholland 	bootpboot_p_sa(rt->rt_gateway, NULL);
    352      1.1  dholland 	printf(" ");
    353      1.1  dholland 	printf("flags %x", (unsigned short) rt->rt_flags);
    354  1.1.1.2  pgoyette 	printf(" %d", (int) rt->rt_expire);
    355      1.1  dholland 	printf(" %s\n", rt->rt_ifp->if_xname);
    356      1.1  dholland }
    357      1.1  dholland 
    358      1.1  dholland void
    359      1.1  dholland bootpboot_p_tree(struct radix_node *rn)
    360      1.1  dholland {
    361      1.1  dholland 
    362      1.1  dholland 	while (rn != NULL) {
    363      1.1  dholland 		if (rn->rn_bit < 0) {
    364      1.1  dholland 			if ((rn->rn_flags & RNF_ROOT) != 0) {
    365      1.1  dholland 			} else {
    366      1.1  dholland 				bootpboot_p_rtentry((struct rtentry *) rn);
    367      1.1  dholland 			}
    368      1.1  dholland 			rn = rn->rn_dupedkey;
    369      1.1  dholland 		} else {
    370      1.1  dholland 			bootpboot_p_tree(rn->rn_left);
    371      1.1  dholland 			bootpboot_p_tree(rn->rn_right);
    372      1.1  dholland 			return;
    373      1.1  dholland 		}
    374      1.1  dholland 	}
    375      1.1  dholland }
    376      1.1  dholland 
    377      1.1  dholland void
    378      1.1  dholland bootpboot_p_rtlist(void)
    379      1.1  dholland {
    380  1.1.1.2  pgoyette 	struct rib_head *rnh;
    381      1.1  dholland 
    382      1.1  dholland 	printf("Routing table:\n");
    383      1.1  dholland 	rnh = rt_tables_get_rnh(0, AF_INET);
    384      1.1  dholland 	if (rnh == NULL)
    385      1.1  dholland 		return;
    386  1.1.1.2  pgoyette 	RIB_RLOCK(rnh);	/* could sleep XXX */
    387      1.1  dholland 	bootpboot_p_tree(rnh->rnh_treetop);
    388  1.1.1.2  pgoyette 	RIB_RUNLOCK(rnh);
    389      1.1  dholland }
    390      1.1  dholland 
    391      1.1  dholland void
    392      1.1  dholland bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
    393      1.1  dholland {
    394      1.1  dholland 
    395      1.1  dholland 	printf("%s flags %x, addr ",
    396      1.1  dholland 	       ifp->if_xname, ifp->if_flags);
    397      1.1  dholland 	print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
    398      1.1  dholland 	printf(", broadcast ");
    399      1.1  dholland 	print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
    400      1.1  dholland 	printf(", netmask ");
    401      1.1  dholland 	print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
    402      1.1  dholland 	printf("\n");
    403      1.1  dholland }
    404      1.1  dholland 
    405      1.1  dholland void
    406      1.1  dholland bootpboot_p_iflist(void)
    407      1.1  dholland {
    408      1.1  dholland 	struct ifnet *ifp;
    409      1.1  dholland 	struct ifaddr *ifa;
    410      1.1  dholland 
    411      1.1  dholland 	printf("Interface list:\n");
    412      1.1  dholland 	IFNET_RLOCK();
    413      1.1  dholland 	for (ifp = TAILQ_FIRST(&V_ifnet);
    414      1.1  dholland 	     ifp != NULL;
    415      1.1  dholland 	     ifp = TAILQ_NEXT(ifp, if_link)) {
    416      1.1  dholland 		for (ifa = TAILQ_FIRST(&ifp->if_addrhead);
    417      1.1  dholland 		     ifa != NULL;
    418      1.1  dholland 		     ifa = TAILQ_NEXT(ifa, ifa_link))
    419      1.1  dholland 			if (ifa->ifa_addr->sa_family == AF_INET)
    420      1.1  dholland 				bootpboot_p_if(ifp, ifa);
    421      1.1  dholland 	}
    422      1.1  dholland 	IFNET_RUNLOCK();
    423      1.1  dholland }
    424      1.1  dholland #endif /* defined(BOOTP_DEBUG) */
    425      1.1  dholland 
    426      1.1  dholland static void
    427      1.1  dholland clear_sinaddr(struct sockaddr_in *sin)
    428      1.1  dholland {
    429      1.1  dholland 
    430      1.1  dholland 	bzero(sin, sizeof(*sin));
    431      1.1  dholland 	sin->sin_len = sizeof(*sin);
    432      1.1  dholland 	sin->sin_family = AF_INET;
    433      1.1  dholland 	sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
    434      1.1  dholland 	sin->sin_port = 0;
    435      1.1  dholland }
    436      1.1  dholland 
    437      1.1  dholland static void
    438      1.1  dholland allocifctx(struct bootpc_globalcontext *gctx)
    439      1.1  dholland {
    440      1.1  dholland 	struct bootpc_ifcontext *ifctx;
    441      1.1  dholland 
    442      1.1  dholland 	ifctx = malloc(sizeof(*ifctx), M_TEMP, M_WAITOK | M_ZERO);
    443      1.1  dholland 	ifctx->xid = gctx->xid;
    444      1.1  dholland #ifdef BOOTP_NO_DHCP
    445      1.1  dholland 	ifctx->state = IF_BOOTP_UNRESOLVED;
    446      1.1  dholland #else
    447      1.1  dholland 	ifctx->state = IF_DHCP_UNRESOLVED;
    448      1.1  dholland #endif
    449      1.1  dholland 	gctx->xid += 0x100;
    450      1.1  dholland 	STAILQ_INSERT_TAIL(&gctx->interfaces, ifctx, next);
    451      1.1  dholland }
    452      1.1  dholland 
    453      1.1  dholland static __inline int
    454      1.1  dholland bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
    455      1.1  dholland {
    456      1.1  dholland 
    457      1.1  dholland 	if (ifctx->state == IF_BOOTP_RESOLVED ||
    458      1.1  dholland 	    ifctx->state == IF_DHCP_RESOLVED)
    459      1.1  dholland 		return 1;
    460      1.1  dholland 	return 0;
    461      1.1  dholland }
    462      1.1  dholland 
    463      1.1  dholland static __inline int
    464      1.1  dholland bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
    465      1.1  dholland {
    466      1.1  dholland 
    467      1.1  dholland 	if (ifctx->state == IF_BOOTP_UNRESOLVED ||
    468      1.1  dholland 	    ifctx->state == IF_DHCP_UNRESOLVED)
    469      1.1  dholland 		return 1;
    470      1.1  dholland 	return 0;
    471      1.1  dholland }
    472      1.1  dholland 
    473      1.1  dholland static __inline int
    474      1.1  dholland bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
    475      1.1  dholland {
    476      1.1  dholland 
    477      1.1  dholland 	if (ifctx->state == IF_BOOTP_FAILED ||
    478      1.1  dholland 	    ifctx->state == IF_DHCP_FAILED)
    479      1.1  dholland 		return 1;
    480      1.1  dholland 	return 0;
    481      1.1  dholland }
    482      1.1  dholland 
    483      1.1  dholland static int
    484      1.1  dholland bootpc_received(struct bootpc_globalcontext *gctx,
    485      1.1  dholland     struct bootpc_ifcontext *ifctx)
    486      1.1  dholland {
    487      1.1  dholland 	unsigned char dhcpreplytype;
    488      1.1  dholland 	char *p;
    489      1.1  dholland 
    490      1.1  dholland 	/*
    491      1.1  dholland 	 * Need timeout for fallback to less
    492      1.1  dholland 	 * desirable alternative.
    493      1.1  dholland 	 */
    494      1.1  dholland 
    495      1.1  dholland 	/* This call used for the side effect (badopt flag) */
    496      1.1  dholland 	(void) bootpc_tag(&gctx->tmptag, &gctx->reply,
    497      1.1  dholland 			  gctx->replylen,
    498      1.1  dholland 			  TAG_END);
    499      1.1  dholland 
    500      1.1  dholland 	/* If packet is invalid, ignore it */
    501      1.1  dholland 	if (gctx->tmptag.badopt != 0)
    502      1.1  dholland 		return 0;
    503      1.1  dholland 
    504      1.1  dholland 	p = bootpc_tag(&gctx->tmptag, &gctx->reply,
    505      1.1  dholland 		       gctx->replylen, TAG_DHCP_MSGTYPE);
    506      1.1  dholland 	if (p != NULL)
    507      1.1  dholland 		dhcpreplytype = *p;
    508      1.1  dholland 	else
    509      1.1  dholland 		dhcpreplytype = DHCP_NOMSG;
    510      1.1  dholland 
    511      1.1  dholland 	switch (ifctx->dhcpquerytype) {
    512      1.1  dholland 	case DHCP_DISCOVER:
    513      1.1  dholland 		if (dhcpreplytype != DHCP_OFFER 	/* Normal DHCP offer */
    514      1.1  dholland #ifndef BOOTP_FORCE_DHCP
    515      1.1  dholland 		    && dhcpreplytype != DHCP_NOMSG	/* Fallback to BOOTP */
    516      1.1  dholland #endif
    517      1.1  dholland 			)
    518      1.1  dholland 			return 0;
    519      1.1  dholland 		break;
    520      1.1  dholland 	case DHCP_REQUEST:
    521      1.1  dholland 		if (dhcpreplytype != DHCP_ACK)
    522      1.1  dholland 			return 0;
    523      1.1  dholland 	case DHCP_NOMSG:
    524      1.1  dholland 		break;
    525      1.1  dholland 	}
    526      1.1  dholland 
    527      1.1  dholland 	/* Ignore packet unless it gives us a root tag we didn't have */
    528      1.1  dholland 
    529      1.1  dholland 	if ((ifctx->state == IF_BOOTP_RESOLVED ||
    530      1.1  dholland 	     (ifctx->dhcpquerytype == DHCP_DISCOVER &&
    531      1.1  dholland 	      (ifctx->state == IF_DHCP_OFFERED ||
    532      1.1  dholland 	       ifctx->state == IF_DHCP_RESOLVED))) &&
    533      1.1  dholland 	    (bootpc_tag(&gctx->tmptag, &ifctx->reply,
    534      1.1  dholland 			ifctx->replylen,
    535      1.1  dholland 			TAG_ROOT) != NULL ||
    536      1.1  dholland 	     bootpc_tag(&gctx->tmptag, &gctx->reply,
    537      1.1  dholland 			gctx->replylen,
    538      1.1  dholland 			TAG_ROOT) == NULL))
    539      1.1  dholland 		return 0;
    540      1.1  dholland 
    541      1.1  dholland 	bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
    542      1.1  dholland 	ifctx->replylen = gctx->replylen;
    543      1.1  dholland 
    544      1.1  dholland 	/* XXX: Only reset if 'perfect' response */
    545      1.1  dholland 	if (ifctx->state == IF_BOOTP_UNRESOLVED)
    546      1.1  dholland 		ifctx->state = IF_BOOTP_RESOLVED;
    547      1.1  dholland 	else if (ifctx->state == IF_DHCP_UNRESOLVED &&
    548      1.1  dholland 		 ifctx->dhcpquerytype == DHCP_DISCOVER) {
    549      1.1  dholland 		if (dhcpreplytype == DHCP_OFFER)
    550      1.1  dholland 			ifctx->state = IF_DHCP_OFFERED;
    551      1.1  dholland 		else
    552      1.1  dholland 			ifctx->state = IF_BOOTP_RESOLVED;	/* Fallback */
    553      1.1  dholland 	} else if (ifctx->state == IF_DHCP_OFFERED &&
    554      1.1  dholland 		   ifctx->dhcpquerytype == DHCP_REQUEST)
    555      1.1  dholland 		ifctx->state = IF_DHCP_RESOLVED;
    556      1.1  dholland 
    557      1.1  dholland 
    558      1.1  dholland 	if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
    559      1.1  dholland 	    ifctx->state != IF_BOOTP_RESOLVED) {
    560      1.1  dholland 		p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
    561      1.1  dholland 			       ifctx->replylen, TAG_DHCP_SERVERID);
    562      1.1  dholland 		if (p != NULL && gctx->tmptag.taglen == 4) {
    563      1.1  dholland 			memcpy(&ifctx->dhcpserver, p, 4);
    564      1.1  dholland 			ifctx->gotdhcpserver = 1;
    565      1.1  dholland 		} else
    566      1.1  dholland 			ifctx->gotdhcpserver = 0;
    567      1.1  dholland 		return 1;
    568      1.1  dholland 	}
    569      1.1  dholland 
    570      1.1  dholland 	ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
    571      1.1  dholland 					 ifctx->replylen,
    572      1.1  dholland 					 TAG_ROOT) != NULL);
    573      1.1  dholland 	ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
    574      1.1  dholland 				   ifctx->replylen,
    575      1.1  dholland 				   TAG_ROUTERS) != NULL);
    576      1.1  dholland 	ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
    577      1.1  dholland 					ifctx->replylen,
    578      1.1  dholland 					TAG_SUBNETMASK) != NULL);
    579      1.1  dholland 	return 1;
    580      1.1  dholland }
    581      1.1  dholland 
    582      1.1  dholland static int
    583      1.1  dholland bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
    584      1.1  dholland {
    585      1.1  dholland 	struct sockaddr_in *sin, dst;
    586      1.1  dholland 	struct uio auio;
    587      1.1  dholland 	struct sockopt sopt;
    588      1.1  dholland 	struct iovec aio;
    589      1.1  dholland 	int error, on, rcvflg, timo, len;
    590      1.1  dholland 	time_t atimo;
    591      1.1  dholland 	time_t rtimo;
    592      1.1  dholland 	struct timeval tv;
    593      1.1  dholland 	struct bootpc_ifcontext *ifctx;
    594      1.1  dholland 	int outstanding;
    595      1.1  dholland 	int gotrootpath;
    596      1.1  dholland 	int retry;
    597      1.1  dholland 	const char *s;
    598      1.1  dholland 
    599      1.1  dholland 	tv.tv_sec = 1;
    600      1.1  dholland 	tv.tv_usec = 0;
    601      1.1  dholland 	bzero(&sopt, sizeof(sopt));
    602      1.1  dholland 	sopt.sopt_dir = SOPT_SET;
    603      1.1  dholland 	sopt.sopt_level = SOL_SOCKET;
    604      1.1  dholland 	sopt.sopt_name = SO_RCVTIMEO;
    605      1.1  dholland 	sopt.sopt_val = &tv;
    606      1.1  dholland 	sopt.sopt_valsize = sizeof tv;
    607      1.1  dholland 
    608      1.1  dholland 	error = sosetopt(bootp_so, &sopt);
    609      1.1  dholland 	if (error != 0)
    610      1.1  dholland 		goto out;
    611      1.1  dholland 
    612      1.1  dholland 	/*
    613      1.1  dholland 	 * Enable broadcast.
    614      1.1  dholland 	 */
    615      1.1  dholland 	on = 1;
    616      1.1  dholland 	sopt.sopt_name = SO_BROADCAST;
    617      1.1  dholland 	sopt.sopt_val = &on;
    618      1.1  dholland 	sopt.sopt_valsize = sizeof on;
    619      1.1  dholland 
    620      1.1  dholland 	error = sosetopt(bootp_so, &sopt);
    621      1.1  dholland 	if (error != 0)
    622      1.1  dholland 		goto out;
    623      1.1  dholland 
    624      1.1  dholland 	/*
    625      1.1  dholland 	 * Disable routing.
    626      1.1  dholland 	 */
    627      1.1  dholland 
    628      1.1  dholland 	on = 1;
    629      1.1  dholland 	sopt.sopt_name = SO_DONTROUTE;
    630      1.1  dholland 	sopt.sopt_val = &on;
    631      1.1  dholland 	sopt.sopt_valsize = sizeof on;
    632      1.1  dholland 
    633      1.1  dholland 	error = sosetopt(bootp_so, &sopt);
    634      1.1  dholland 	if (error != 0)
    635      1.1  dholland 		goto out;
    636      1.1  dholland 
    637      1.1  dholland 	/*
    638      1.1  dholland 	 * Bind the local endpoint to a bootp client port.
    639      1.1  dholland 	 */
    640      1.1  dholland 	sin = &dst;
    641      1.1  dholland 	clear_sinaddr(sin);
    642      1.1  dholland 	sin->sin_port = htons(IPPORT_BOOTPC);
    643      1.1  dholland 	error = sobind(bootp_so, (struct sockaddr *)sin, td);
    644      1.1  dholland 	if (error != 0) {
    645      1.1  dholland 		printf("bind failed\n");
    646      1.1  dholland 		goto out;
    647      1.1  dholland 	}
    648      1.1  dholland 
    649      1.1  dholland 	/*
    650      1.1  dholland 	 * Setup socket address for the server.
    651      1.1  dholland 	 */
    652      1.1  dholland 	sin = &dst;
    653      1.1  dholland 	clear_sinaddr(sin);
    654      1.1  dholland 	sin->sin_addr.s_addr = INADDR_BROADCAST;
    655      1.1  dholland 	sin->sin_port = htons(IPPORT_BOOTPS);
    656      1.1  dholland 
    657      1.1  dholland 	/*
    658      1.1  dholland 	 * Send it, repeatedly, until a reply is received,
    659      1.1  dholland 	 * but delay each re-send by an increasing amount.
    660      1.1  dholland 	 * If the delay hits the maximum, start complaining.
    661      1.1  dholland 	 */
    662      1.1  dholland 	timo = 0;
    663      1.1  dholland 	rtimo = 0;
    664      1.1  dholland 	for (;;) {
    665      1.1  dholland 
    666      1.1  dholland 		outstanding = 0;
    667      1.1  dholland 		gotrootpath = 0;
    668      1.1  dholland 
    669      1.1  dholland 		STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
    670      1.1  dholland 			if (bootpc_ifctx_isresolved(ifctx) != 0 &&
    671      1.1  dholland 			    bootpc_tag(&gctx->tmptag, &ifctx->reply,
    672      1.1  dholland 				       ifctx->replylen,
    673      1.1  dholland 				       TAG_ROOT) != NULL)
    674      1.1  dholland 				gotrootpath = 1;
    675      1.1  dholland 		}
    676      1.1  dholland 
    677      1.1  dholland 		STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
    678      1.1  dholland 			struct in_aliasreq *ifra = &ifctx->iareq;
    679      1.1  dholland 			sin = (struct sockaddr_in *)&ifra->ifra_mask;
    680      1.1  dholland 
    681      1.1  dholland 			ifctx->outstanding = 0;
    682      1.1  dholland 			if (bootpc_ifctx_isresolved(ifctx)  != 0 &&
    683      1.1  dholland 			    gotrootpath != 0) {
    684      1.1  dholland 				continue;
    685      1.1  dholland 			}
    686      1.1  dholland 			if (bootpc_ifctx_isfailed(ifctx) != 0)
    687      1.1  dholland 				continue;
    688      1.1  dholland 
    689      1.1  dholland 			outstanding++;
    690      1.1  dholland 			ifctx->outstanding = 1;
    691      1.1  dholland 
    692      1.1  dholland 			/* Proceed to next step in DHCP negotiation */
    693      1.1  dholland 			if ((ifctx->state == IF_DHCP_OFFERED &&
    694      1.1  dholland 			     ifctx->dhcpquerytype != DHCP_REQUEST) ||
    695      1.1  dholland 			    (ifctx->state == IF_DHCP_UNRESOLVED &&
    696      1.1  dholland 			     ifctx->dhcpquerytype != DHCP_DISCOVER) ||
    697      1.1  dholland 			    (ifctx->state == IF_BOOTP_UNRESOLVED &&
    698      1.1  dholland 			     ifctx->dhcpquerytype != DHCP_NOMSG)) {
    699      1.1  dholland 				ifctx->sentmsg = 0;
    700      1.1  dholland 				bootpc_compose_query(ifctx, td);
    701      1.1  dholland 			}
    702      1.1  dholland 
    703      1.1  dholland 			/* Send BOOTP request (or re-send). */
    704      1.1  dholland 
    705      1.1  dholland 			if (ifctx->sentmsg == 0) {
    706      1.1  dholland 				switch(ifctx->dhcpquerytype) {
    707      1.1  dholland 				case DHCP_DISCOVER:
    708      1.1  dholland 					s = "DHCP Discover";
    709      1.1  dholland 					break;
    710      1.1  dholland 				case DHCP_REQUEST:
    711      1.1  dholland 					s = "DHCP Request";
    712      1.1  dholland 					break;
    713      1.1  dholland 				case DHCP_NOMSG:
    714      1.1  dholland 				default:
    715      1.1  dholland 					s = "BOOTP Query";
    716      1.1  dholland 					break;
    717      1.1  dholland 				}
    718      1.1  dholland 				printf("Sending %s packet from "
    719      1.1  dholland 				       "interface %s (%*D)\n",
    720      1.1  dholland 				       s,
    721      1.1  dholland 				       ifctx->ireq.ifr_name,
    722      1.1  dholland 				       ifctx->sdl->sdl_alen,
    723      1.1  dholland 				       (unsigned char *) LLADDR(ifctx->sdl),
    724      1.1  dholland 				       ":");
    725      1.1  dholland 				ifctx->sentmsg = 1;
    726      1.1  dholland 			}
    727      1.1  dholland 
    728      1.1  dholland 			aio.iov_base = (caddr_t) &ifctx->call;
    729      1.1  dholland 			aio.iov_len = sizeof(ifctx->call);
    730      1.1  dholland 
    731      1.1  dholland 			auio.uio_iov = &aio;
    732      1.1  dholland 			auio.uio_iovcnt = 1;
    733      1.1  dholland 			auio.uio_segflg = UIO_SYSSPACE;
    734      1.1  dholland 			auio.uio_rw = UIO_WRITE;
    735      1.1  dholland 			auio.uio_offset = 0;
    736      1.1  dholland 			auio.uio_resid = sizeof(ifctx->call);
    737      1.1  dholland 			auio.uio_td = td;
    738      1.1  dholland 
    739      1.1  dholland 			/* Set netmask to 0.0.0.0 */
    740      1.1  dholland 			clear_sinaddr(sin);
    741      1.1  dholland 			error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
    742      1.1  dholland 			    td);
    743      1.1  dholland 			if (error != 0)
    744      1.1  dholland 				panic("%s: SIOCAIFADDR, error=%d", __func__,
    745      1.1  dholland 				    error);
    746      1.1  dholland 
    747      1.1  dholland 			error = sosend(bootp_so, (struct sockaddr *) &dst,
    748      1.1  dholland 				       &auio, NULL, NULL, 0, td);
    749      1.1  dholland 			if (error != 0)
    750      1.1  dholland 				printf("%s: sosend: %d state %08x\n", __func__,
    751      1.1  dholland 				    error, (int )bootp_so->so_state);
    752      1.1  dholland 
    753      1.1  dholland 			/* Set netmask to 255.0.0.0 */
    754      1.1  dholland 			sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
    755      1.1  dholland 			error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
    756      1.1  dholland 			    td);
    757      1.1  dholland 			if (error != 0)
    758      1.1  dholland 				panic("%s: SIOCAIFADDR, error=%d", __func__,
    759      1.1  dholland 				    error);
    760      1.1  dholland 		}
    761      1.1  dholland 
    762      1.1  dholland 		if (outstanding == 0 &&
    763      1.1  dholland 		    (rtimo == 0 || time_second >= rtimo)) {
    764      1.1  dholland 			error = 0;
    765      1.1  dholland 			goto out;
    766      1.1  dholland 		}
    767      1.1  dholland 
    768      1.1  dholland 		/* Determine new timeout. */
    769      1.1  dholland 		if (timo < MAX_RESEND_DELAY)
    770      1.1  dholland 			timo++;
    771      1.1  dholland 		else {
    772      1.1  dholland 			printf("DHCP/BOOTP timeout for server ");
    773      1.1  dholland 			print_sin_addr(&dst);
    774      1.1  dholland 			printf("\n");
    775      1.1  dholland 		}
    776      1.1  dholland 
    777      1.1  dholland 		/*
    778      1.1  dholland 		 * Wait for up to timo seconds for a reply.
    779      1.1  dholland 		 * The socket receive timeout was set to 1 second.
    780      1.1  dholland 		 */
    781      1.1  dholland 		atimo = timo + time_second;
    782      1.1  dholland 		while (time_second < atimo) {
    783      1.1  dholland 			aio.iov_base = (caddr_t) &gctx->reply;
    784      1.1  dholland 			aio.iov_len = sizeof(gctx->reply);
    785      1.1  dholland 
    786      1.1  dholland 			auio.uio_iov = &aio;
    787      1.1  dholland 			auio.uio_iovcnt = 1;
    788      1.1  dholland 			auio.uio_segflg = UIO_SYSSPACE;
    789      1.1  dholland 			auio.uio_rw = UIO_READ;
    790      1.1  dholland 			auio.uio_offset = 0;
    791      1.1  dholland 			auio.uio_resid = sizeof(gctx->reply);
    792      1.1  dholland 			auio.uio_td = td;
    793      1.1  dholland 
    794      1.1  dholland 			rcvflg = 0;
    795      1.1  dholland 			error = soreceive(bootp_so, NULL, &auio,
    796      1.1  dholland 					  NULL, NULL, &rcvflg);
    797      1.1  dholland 			gctx->secs = time_second - gctx->starttime;
    798      1.1  dholland 			STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
    799      1.1  dholland 				if (bootpc_ifctx_isresolved(ifctx) != 0 ||
    800      1.1  dholland 				    bootpc_ifctx_isfailed(ifctx) != 0)
    801      1.1  dholland 					continue;
    802      1.1  dholland 
    803      1.1  dholland 				ifctx->call.secs = htons(gctx->secs);
    804      1.1  dholland 			}
    805      1.1  dholland 			if (error == EWOULDBLOCK)
    806      1.1  dholland 				continue;
    807      1.1  dholland 			if (error != 0)
    808      1.1  dholland 				goto out;
    809      1.1  dholland 			len = sizeof(gctx->reply) - auio.uio_resid;
    810      1.1  dholland 
    811      1.1  dholland 			/* Do we have the required number of bytes ? */
    812      1.1  dholland 			if (len < BOOTP_MIN_LEN)
    813      1.1  dholland 				continue;
    814      1.1  dholland 			gctx->replylen = len;
    815      1.1  dholland 
    816      1.1  dholland 			/* Is it a reply? */
    817      1.1  dholland 			if (gctx->reply.op != BOOTP_REPLY)
    818      1.1  dholland 				continue;
    819      1.1  dholland 
    820      1.1  dholland 			/* Is this an answer to our query */
    821      1.1  dholland 			STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
    822      1.1  dholland 				if (gctx->reply.xid != ifctx->call.xid)
    823      1.1  dholland 					continue;
    824      1.1  dholland 
    825      1.1  dholland 				/* Same HW address size ? */
    826      1.1  dholland 				if (gctx->reply.hlen != ifctx->call.hlen)
    827      1.1  dholland 					continue;
    828      1.1  dholland 
    829      1.1  dholland 				/* Correct HW address ? */
    830      1.1  dholland 				if (bcmp(gctx->reply.chaddr,
    831      1.1  dholland 					 ifctx->call.chaddr,
    832      1.1  dholland 					 ifctx->call.hlen) != 0)
    833      1.1  dholland 					continue;
    834      1.1  dholland 
    835      1.1  dholland 				break;
    836      1.1  dholland 			}
    837      1.1  dholland 
    838      1.1  dholland 			if (ifctx != NULL) {
    839      1.1  dholland 				s =  bootpc_tag(&gctx->tmptag,
    840      1.1  dholland 						&gctx->reply,
    841      1.1  dholland 						gctx->replylen,
    842      1.1  dholland 						TAG_DHCP_MSGTYPE);
    843      1.1  dholland 				if (s != NULL) {
    844      1.1  dholland 					switch (*s) {
    845      1.1  dholland 					case DHCP_OFFER:
    846      1.1  dholland 						s = "DHCP Offer";
    847      1.1  dholland 						break;
    848      1.1  dholland 					case DHCP_ACK:
    849      1.1  dholland 						s = "DHCP Ack";
    850      1.1  dholland 						break;
    851      1.1  dholland 					default:
    852      1.1  dholland 						s = "DHCP (unexpected)";
    853      1.1  dholland 						break;
    854      1.1  dholland 					}
    855      1.1  dholland 				} else
    856      1.1  dholland 					s = "BOOTP Reply";
    857      1.1  dholland 
    858      1.1  dholland 				printf("Received %s packet"
    859      1.1  dholland 				       " on %s from ",
    860      1.1  dholland 				       s,
    861      1.1  dholland 				       ifctx->ireq.ifr_name);
    862      1.1  dholland 				print_in_addr(gctx->reply.siaddr);
    863      1.1  dholland 				if (gctx->reply.giaddr.s_addr !=
    864      1.1  dholland 				    htonl(INADDR_ANY)) {
    865      1.1  dholland 					printf(" via ");
    866      1.1  dholland 					print_in_addr(gctx->reply.giaddr);
    867      1.1  dholland 				}
    868      1.1  dholland 				if (bootpc_received(gctx, ifctx) != 0) {
    869      1.1  dholland 					printf(" (accepted)");
    870      1.1  dholland 					if (ifctx->outstanding) {
    871      1.1  dholland 						ifctx->outstanding = 0;
    872      1.1  dholland 						outstanding--;
    873      1.1  dholland 					}
    874      1.1  dholland 					/* Network settle delay */
    875      1.1  dholland 					if (outstanding == 0)
    876      1.1  dholland 						atimo = time_second +
    877      1.1  dholland 							BOOTP_SETTLE_DELAY;
    878      1.1  dholland 				} else
    879      1.1  dholland 					printf(" (ignored)");
    880      1.1  dholland 				if (ifctx->gotrootpath ||
    881      1.1  dholland 				    gctx->any_root_overrides) {
    882      1.1  dholland 					gotrootpath = 1;
    883      1.1  dholland 					rtimo = time_second +
    884      1.1  dholland 						BOOTP_SETTLE_DELAY;
    885      1.1  dholland 					if (ifctx->gotrootpath)
    886      1.1  dholland 						printf(" (got root path)");
    887      1.1  dholland 				}
    888      1.1  dholland 				printf("\n");
    889      1.1  dholland 			}
    890      1.1  dholland 		} /* while secs */
    891      1.1  dholland #ifdef BOOTP_TIMEOUT
    892      1.1  dholland 		if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
    893      1.1  dholland 			break;
    894      1.1  dholland #endif
    895      1.1  dholland 		/* Force a retry if halfway in DHCP negotiation */
    896      1.1  dholland 		retry = 0;
    897      1.1  dholland 		STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
    898      1.1  dholland 			if (ifctx->state == IF_DHCP_OFFERED) {
    899      1.1  dholland 				if (ifctx->dhcpquerytype == DHCP_DISCOVER)
    900      1.1  dholland 					retry = 1;
    901      1.1  dholland 				else
    902      1.1  dholland 					ifctx->state = IF_DHCP_UNRESOLVED;
    903      1.1  dholland 			}
    904      1.1  dholland 
    905      1.1  dholland 		if (retry != 0)
    906      1.1  dholland 			continue;
    907      1.1  dholland 
    908      1.1  dholland 		if (gotrootpath != 0) {
    909      1.1  dholland 			gctx->gotrootpath = gotrootpath;
    910      1.1  dholland 			if (rtimo != 0 && time_second >= rtimo)
    911      1.1  dholland 				break;
    912      1.1  dholland 		}
    913      1.1  dholland 	} /* forever send/receive */
    914      1.1  dholland 
    915      1.1  dholland 	/*
    916      1.1  dholland 	 * XXX: These are errors of varying seriousness being silently
    917      1.1  dholland 	 * ignored
    918      1.1  dholland 	 */
    919      1.1  dholland 
    920      1.1  dholland 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
    921      1.1  dholland 		if (bootpc_ifctx_isresolved(ifctx) == 0) {
    922      1.1  dholland 			printf("%s timeout for interface %s\n",
    923      1.1  dholland 			       ifctx->dhcpquerytype != DHCP_NOMSG ?
    924      1.1  dholland 			       "DHCP" : "BOOTP",
    925      1.1  dholland 			       ifctx->ireq.ifr_name);
    926      1.1  dholland 		}
    927      1.1  dholland 
    928      1.1  dholland 	if (gctx->gotrootpath != 0) {
    929      1.1  dholland #if 0
    930      1.1  dholland 		printf("Got a root path, ignoring remaining timeout\n");
    931      1.1  dholland #endif
    932      1.1  dholland 		error = 0;
    933      1.1  dholland 		goto out;
    934      1.1  dholland 	}
    935      1.1  dholland #ifndef BOOTP_NFSROOT
    936      1.1  dholland 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
    937      1.1  dholland 		if (bootpc_ifctx_isresolved(ifctx) != 0) {
    938      1.1  dholland 			error = 0;
    939      1.1  dholland 			goto out;
    940      1.1  dholland 		}
    941      1.1  dholland #endif
    942      1.1  dholland 	error = ETIMEDOUT;
    943      1.1  dholland 
    944      1.1  dholland out:
    945      1.1  dholland 	return (error);
    946      1.1  dholland }
    947      1.1  dholland 
    948      1.1  dholland static void
    949      1.1  dholland bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
    950      1.1  dholland {
    951      1.1  dholland 	struct ifreq *ifr;
    952      1.1  dholland 	struct in_aliasreq *ifra;
    953      1.1  dholland 	struct sockaddr_in *sin;
    954      1.1  dholland 	int error;
    955      1.1  dholland 
    956      1.1  dholland 	ifr = &ifctx->ireq;
    957      1.1  dholland 	ifra = &ifctx->iareq;
    958      1.1  dholland 
    959      1.1  dholland 	/*
    960      1.1  dholland 	 * Bring up the interface.
    961      1.1  dholland 	 *
    962      1.1  dholland 	 * Get the old interface flags and or IFF_UP into them; if
    963      1.1  dholland 	 * IFF_UP set blindly, interface selection can be clobbered.
    964      1.1  dholland 	 */
    965      1.1  dholland 	error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
    966      1.1  dholland 	if (error != 0)
    967      1.1  dholland 		panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
    968      1.1  dholland 	ifr->ifr_flags |= IFF_UP;
    969      1.1  dholland 	error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
    970      1.1  dholland 	if (error != 0)
    971      1.1  dholland 		panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
    972      1.1  dholland 
    973      1.1  dholland 	/*
    974      1.1  dholland 	 * Do enough of ifconfig(8) so that the chosen interface
    975      1.1  dholland 	 * can talk to the servers. Set address to 0.0.0.0/8 and
    976      1.1  dholland 	 * broadcast address to local broadcast.
    977      1.1  dholland 	 */
    978      1.1  dholland 	sin = (struct sockaddr_in *)&ifra->ifra_addr;
    979      1.1  dholland 	clear_sinaddr(sin);
    980      1.1  dholland 	sin = (struct sockaddr_in *)&ifra->ifra_mask;
    981      1.1  dholland 	clear_sinaddr(sin);
    982      1.1  dholland 	sin->sin_addr.s_addr = htonl(IN_CLASSA_NET);
    983      1.1  dholland 	sin = (struct sockaddr_in *)&ifra->ifra_broadaddr;
    984      1.1  dholland 	clear_sinaddr(sin);
    985      1.1  dholland 	sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
    986      1.1  dholland 	error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
    987      1.1  dholland 	if (error != 0)
    988      1.1  dholland 		panic("%s: SIOCAIFADDR, error=%d", __func__, error);
    989      1.1  dholland }
    990      1.1  dholland 
    991      1.1  dholland static void
    992      1.1  dholland bootpc_shutdown_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
    993      1.1  dholland {
    994      1.1  dholland 	struct ifreq *ifr;
    995      1.1  dholland 	struct sockaddr_in *sin;
    996      1.1  dholland 	int error;
    997      1.1  dholland 
    998      1.1  dholland 	ifr = &ifctx->ireq;
    999      1.1  dholland 
   1000      1.1  dholland 	printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
   1001      1.1  dholland 	error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
   1002      1.1  dholland 	if (error != 0)
   1003      1.1  dholland 		panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
   1004      1.1  dholland 	ifr->ifr_flags &= ~IFF_UP;
   1005      1.1  dholland 	error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
   1006      1.1  dholland 	if (error != 0)
   1007      1.1  dholland 		panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
   1008      1.1  dholland 
   1009      1.1  dholland 	sin = (struct sockaddr_in *) &ifr->ifr_addr;
   1010      1.1  dholland 	clear_sinaddr(sin);
   1011      1.1  dholland 	error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
   1012      1.1  dholland 	if (error != 0)
   1013      1.1  dholland 		panic("%s: SIOCDIFADDR, error=%d", __func__, error);
   1014      1.1  dholland }
   1015      1.1  dholland 
   1016  1.1.1.2  pgoyette static void
   1017      1.1  dholland bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
   1018      1.1  dholland     struct bootpc_globalcontext *gctx, struct thread *td)
   1019      1.1  dholland {
   1020      1.1  dholland 	int error;
   1021      1.1  dholland 	struct sockaddr_in *sin;
   1022      1.1  dholland 	struct ifreq *ifr;
   1023      1.1  dholland 	struct in_aliasreq *ifra;
   1024      1.1  dholland 	struct sockaddr_in *myaddr;
   1025      1.1  dholland 	struct sockaddr_in *netmask;
   1026      1.1  dholland 
   1027      1.1  dholland 	ifr = &ifctx->ireq;
   1028      1.1  dholland 	ifra = &ifctx->iareq;
   1029      1.1  dholland 	myaddr = &ifctx->myaddr;
   1030      1.1  dholland 	netmask = &ifctx->netmask;
   1031      1.1  dholland 
   1032      1.1  dholland 	if (bootpc_ifctx_isresolved(ifctx) == 0) {
   1033      1.1  dholland 		/* Shutdown interfaces where BOOTP failed */
   1034      1.1  dholland 		bootpc_shutdown_interface(ifctx, td);
   1035  1.1.1.2  pgoyette 		return;
   1036  1.1.1.2  pgoyette 	}
   1037  1.1.1.2  pgoyette 
   1038  1.1.1.2  pgoyette 	printf("Adjusted interface %s", ifctx->ireq.ifr_name);
   1039  1.1.1.2  pgoyette 
   1040  1.1.1.2  pgoyette 	/* Do BOOTP interface options */
   1041  1.1.1.2  pgoyette 	if (ifctx->mtu != 0) {
   1042  1.1.1.2  pgoyette 		printf(" (MTU=%d%s)", ifctx->mtu,
   1043  1.1.1.2  pgoyette 		    (ifctx->mtu > 1514) ? "/JUMBO" : "");
   1044  1.1.1.2  pgoyette 		ifr->ifr_mtu = ifctx->mtu;
   1045  1.1.1.2  pgoyette 		error = ifioctl(bootp_so, SIOCSIFMTU, (caddr_t) ifr, td);
   1046  1.1.1.2  pgoyette 		if (error != 0)
   1047  1.1.1.2  pgoyette 			panic("%s: SIOCSIFMTU, error=%d", __func__, error);
   1048      1.1  dholland 	}
   1049  1.1.1.2  pgoyette 	printf("\n");
   1050      1.1  dholland 
   1051      1.1  dholland 	/*
   1052      1.1  dholland 	 * Do enough of ifconfig(8) so that the chosen interface
   1053      1.1  dholland 	 * can talk to the servers.  (just set the address)
   1054      1.1  dholland 	 */
   1055      1.1  dholland 	sin = (struct sockaddr_in *) &ifr->ifr_addr;
   1056      1.1  dholland 	clear_sinaddr(sin);
   1057      1.1  dholland 	error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
   1058      1.1  dholland 	if (error != 0)
   1059      1.1  dholland 		panic("%s: SIOCDIFADDR, error=%d", __func__, error);
   1060      1.1  dholland 
   1061      1.1  dholland 	bcopy(myaddr, &ifra->ifra_addr, sizeof(*myaddr));
   1062      1.1  dholland 	bcopy(netmask, &ifra->ifra_mask, sizeof(*netmask));
   1063      1.1  dholland 	clear_sinaddr(&ifra->ifra_broadaddr);
   1064      1.1  dholland 	ifra->ifra_broadaddr.sin_addr.s_addr = myaddr->sin_addr.s_addr |
   1065      1.1  dholland 	    ~netmask->sin_addr.s_addr;
   1066      1.1  dholland 
   1067      1.1  dholland 	error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
   1068      1.1  dholland 	if (error != 0)
   1069      1.1  dholland 		panic("%s: SIOCAIFADDR, error=%d", __func__, error);
   1070  1.1.1.2  pgoyette }
   1071      1.1  dholland 
   1072  1.1.1.2  pgoyette static void
   1073  1.1.1.2  pgoyette bootpc_add_default_route(struct bootpc_ifcontext *ifctx)
   1074  1.1.1.2  pgoyette {
   1075  1.1.1.2  pgoyette 	int error;
   1076  1.1.1.2  pgoyette 	struct sockaddr_in defdst;
   1077  1.1.1.2  pgoyette 	struct sockaddr_in defmask;
   1078      1.1  dholland 
   1079  1.1.1.2  pgoyette 	if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
   1080  1.1.1.2  pgoyette 		return;
   1081  1.1.1.2  pgoyette 
   1082  1.1.1.2  pgoyette 	clear_sinaddr(&defdst);
   1083  1.1.1.2  pgoyette 	clear_sinaddr(&defmask);
   1084  1.1.1.2  pgoyette 
   1085  1.1.1.2  pgoyette 	error = rtrequest_fib(RTM_ADD, (struct sockaddr *)&defdst,
   1086  1.1.1.2  pgoyette 	    (struct sockaddr *) &ifctx->gw, (struct sockaddr *)&defmask,
   1087  1.1.1.2  pgoyette 	    (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL, RT_DEFAULT_FIB);
   1088  1.1.1.2  pgoyette 	if (error != 0) {
   1089  1.1.1.2  pgoyette 		printf("%s: RTM_ADD, error=%d\n", __func__, error);
   1090      1.1  dholland 	}
   1091  1.1.1.2  pgoyette }
   1092  1.1.1.2  pgoyette 
   1093  1.1.1.2  pgoyette static void
   1094  1.1.1.2  pgoyette bootpc_remove_default_route(struct bootpc_ifcontext *ifctx)
   1095  1.1.1.2  pgoyette {
   1096  1.1.1.2  pgoyette 	int error;
   1097  1.1.1.2  pgoyette 	struct sockaddr_in defdst;
   1098  1.1.1.2  pgoyette 	struct sockaddr_in defmask;
   1099  1.1.1.2  pgoyette 
   1100  1.1.1.2  pgoyette 	if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
   1101  1.1.1.2  pgoyette 		return;
   1102      1.1  dholland 
   1103  1.1.1.2  pgoyette 	clear_sinaddr(&defdst);
   1104  1.1.1.2  pgoyette 	clear_sinaddr(&defmask);
   1105  1.1.1.2  pgoyette 
   1106  1.1.1.2  pgoyette 	error = rtrequest_fib(RTM_DELETE, (struct sockaddr *)&defdst,
   1107  1.1.1.2  pgoyette 	    (struct sockaddr *) &ifctx->gw, (struct sockaddr *)&defmask,
   1108  1.1.1.2  pgoyette 	    (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL, RT_DEFAULT_FIB);
   1109  1.1.1.2  pgoyette 	if (error != 0) {
   1110  1.1.1.2  pgoyette 		printf("%s: RTM_DELETE, error=%d\n", __func__, error);
   1111  1.1.1.2  pgoyette 	}
   1112      1.1  dholland }
   1113      1.1  dholland 
   1114      1.1  dholland static int
   1115      1.1  dholland setfs(struct sockaddr_in *addr, char *path, char *p,
   1116      1.1  dholland     const struct in_addr *siaddr)
   1117      1.1  dholland {
   1118      1.1  dholland 
   1119      1.1  dholland 	if (getip(&p, &addr->sin_addr) == 0) {
   1120      1.1  dholland 		if (siaddr != NULL && *p == '/')
   1121      1.1  dholland 			bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
   1122      1.1  dholland 		else
   1123      1.1  dholland 			return 0;
   1124      1.1  dholland 	} else {
   1125      1.1  dholland 		if (*p != ':')
   1126      1.1  dholland 			return 0;
   1127      1.1  dholland 		p++;
   1128      1.1  dholland 	}
   1129      1.1  dholland 
   1130      1.1  dholland 	addr->sin_len = sizeof(struct sockaddr_in);
   1131      1.1  dholland 	addr->sin_family = AF_INET;
   1132      1.1  dholland 
   1133      1.1  dholland 	strlcpy(path, p, MNAMELEN);
   1134      1.1  dholland 	return 1;
   1135      1.1  dholland }
   1136      1.1  dholland 
   1137      1.1  dholland static int
   1138      1.1  dholland getip(char **ptr, struct in_addr *addr)
   1139      1.1  dholland {
   1140      1.1  dholland 	char *p;
   1141      1.1  dholland 	unsigned int ip;
   1142      1.1  dholland 	int val;
   1143      1.1  dholland 
   1144      1.1  dholland 	p = *ptr;
   1145      1.1  dholland 	ip = 0;
   1146      1.1  dholland 	if (((val = getdec(&p)) < 0) || (val > 255))
   1147      1.1  dholland 		return 0;
   1148      1.1  dholland 	ip = val << 24;
   1149      1.1  dholland 	if (*p != '.')
   1150      1.1  dholland 		return 0;
   1151      1.1  dholland 	p++;
   1152      1.1  dholland 	if (((val = getdec(&p)) < 0) || (val > 255))
   1153      1.1  dholland 		return 0;
   1154      1.1  dholland 	ip |= (val << 16);
   1155      1.1  dholland 	if (*p != '.')
   1156      1.1  dholland 		return 0;
   1157      1.1  dholland 	p++;
   1158      1.1  dholland 	if (((val = getdec(&p)) < 0) || (val > 255))
   1159      1.1  dholland 		return 0;
   1160      1.1  dholland 	ip |= (val << 8);
   1161      1.1  dholland 	if (*p != '.')
   1162      1.1  dholland 		return 0;
   1163      1.1  dholland 	p++;
   1164      1.1  dholland 	if (((val = getdec(&p)) < 0) || (val > 255))
   1165      1.1  dholland 		return 0;
   1166      1.1  dholland 	ip |= val;
   1167      1.1  dholland 
   1168      1.1  dholland 	addr->s_addr = htonl(ip);
   1169      1.1  dholland 	*ptr = p;
   1170      1.1  dholland 	return 1;
   1171      1.1  dholland }
   1172      1.1  dholland 
   1173      1.1  dholland static int
   1174      1.1  dholland getdec(char **ptr)
   1175      1.1  dholland {
   1176      1.1  dholland 	char *p;
   1177      1.1  dholland 	int ret;
   1178      1.1  dholland 
   1179      1.1  dholland 	p = *ptr;
   1180      1.1  dholland 	ret = 0;
   1181      1.1  dholland 	if ((*p < '0') || (*p > '9'))
   1182      1.1  dholland 		return -1;
   1183      1.1  dholland 	while ((*p >= '0') && (*p <= '9')) {
   1184      1.1  dholland 		ret = ret * 10 + (*p - '0');
   1185      1.1  dholland 		p++;
   1186      1.1  dholland 	}
   1187      1.1  dholland 	*ptr = p;
   1188      1.1  dholland 	return ret;
   1189      1.1  dholland }
   1190      1.1  dholland 
   1191      1.1  dholland static void
   1192      1.1  dholland mountopts(struct nfs_args *args, char *p)
   1193      1.1  dholland {
   1194      1.1  dholland 	args->version = NFS_ARGSVERSION;
   1195      1.1  dholland 	args->rsize = BOOTP_BLOCKSIZE;
   1196      1.1  dholland 	args->wsize = BOOTP_BLOCKSIZE;
   1197      1.1  dholland 	args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
   1198      1.1  dholland 	args->sotype = SOCK_DGRAM;
   1199      1.1  dholland 	if (p != NULL)
   1200      1.1  dholland 		nfs_parse_options(p, args);
   1201      1.1  dholland }
   1202      1.1  dholland 
   1203      1.1  dholland static int
   1204      1.1  dholland xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
   1205      1.1  dholland {
   1206      1.1  dholland 	struct mbuf *m;
   1207      1.1  dholland 	int alignedlen;
   1208      1.1  dholland 
   1209      1.1  dholland 	m = *mptr;
   1210      1.1  dholland 	alignedlen = ( len + 3 ) & ~3;
   1211      1.1  dholland 
   1212      1.1  dholland 	if (m->m_len < alignedlen) {
   1213      1.1  dholland 		m = m_pullup(m, alignedlen);
   1214      1.1  dholland 		if (m == NULL) {
   1215      1.1  dholland 			*mptr = NULL;
   1216      1.1  dholland 			return EBADRPC;
   1217      1.1  dholland 		}
   1218      1.1  dholland 	}
   1219      1.1  dholland 	bcopy(mtod(m, u_char *), buf, len);
   1220      1.1  dholland 	m_adj(m, alignedlen);
   1221      1.1  dholland 	*mptr = m;
   1222      1.1  dholland 	return 0;
   1223      1.1  dholland }
   1224      1.1  dholland 
   1225      1.1  dholland static int
   1226      1.1  dholland xdr_int_decode(struct mbuf **mptr, int *iptr)
   1227      1.1  dholland {
   1228      1.1  dholland 	u_int32_t i;
   1229      1.1  dholland 
   1230      1.1  dholland 	if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
   1231      1.1  dholland 		return EBADRPC;
   1232      1.1  dholland 	*iptr = fxdr_unsigned(u_int32_t, i);
   1233      1.1  dholland 	return 0;
   1234      1.1  dholland }
   1235      1.1  dholland 
   1236      1.1  dholland static void
   1237      1.1  dholland print_sin_addr(struct sockaddr_in *sin)
   1238      1.1  dholland {
   1239      1.1  dholland 
   1240      1.1  dholland 	print_in_addr(sin->sin_addr);
   1241      1.1  dholland }
   1242      1.1  dholland 
   1243      1.1  dholland static void
   1244      1.1  dholland print_in_addr(struct in_addr addr)
   1245      1.1  dholland {
   1246      1.1  dholland 	unsigned int ip;
   1247      1.1  dholland 
   1248      1.1  dholland 	ip = ntohl(addr.s_addr);
   1249      1.1  dholland 	printf("%d.%d.%d.%d",
   1250      1.1  dholland 	       ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
   1251      1.1  dholland }
   1252      1.1  dholland 
   1253      1.1  dholland static void
   1254      1.1  dholland bootpc_compose_query(struct bootpc_ifcontext *ifctx, struct thread *td)
   1255      1.1  dholland {
   1256      1.1  dholland 	unsigned char *vendp;
   1257      1.1  dholland 	unsigned char vendor_client[64];
   1258      1.1  dholland 	uint32_t leasetime;
   1259      1.1  dholland 	uint8_t vendor_client_len;
   1260      1.1  dholland 
   1261      1.1  dholland 	ifctx->gotrootpath = 0;
   1262      1.1  dholland 
   1263      1.1  dholland 	bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
   1264      1.1  dholland 
   1265      1.1  dholland 	/* bootpc part */
   1266      1.1  dholland 	ifctx->call.op = BOOTP_REQUEST; 	/* BOOTREQUEST */
   1267      1.1  dholland 	ifctx->call.htype = 1;			/* 10mb ethernet */
   1268      1.1  dholland 	ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
   1269      1.1  dholland 	ifctx->call.hops = 0;
   1270      1.1  dholland 	if (bootpc_ifctx_isunresolved(ifctx) != 0)
   1271      1.1  dholland 		ifctx->xid++;
   1272      1.1  dholland 	ifctx->call.xid = txdr_unsigned(ifctx->xid);
   1273      1.1  dholland 	bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
   1274      1.1  dholland 
   1275      1.1  dholland 	vendp = ifctx->call.vend;
   1276      1.1  dholland 	*vendp++ = 99;		/* RFC1048 cookie */
   1277      1.1  dholland 	*vendp++ = 130;
   1278      1.1  dholland 	*vendp++ = 83;
   1279      1.1  dholland 	*vendp++ = 99;
   1280      1.1  dholland 	*vendp++ = TAG_MAXMSGSIZE;
   1281      1.1  dholland 	*vendp++ = 2;
   1282      1.1  dholland 	*vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
   1283      1.1  dholland 	*vendp++ = sizeof(struct bootp_packet) & 255;
   1284      1.1  dholland 
   1285      1.1  dholland 	snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
   1286      1.1  dholland 		ostype, MACHINE, osrelease);
   1287      1.1  dholland 	vendor_client_len = strlen(vendor_client);
   1288      1.1  dholland 	*vendp++ = TAG_VENDOR_INDENTIFIER;
   1289      1.1  dholland 	*vendp++ = vendor_client_len;
   1290      1.1  dholland 	memcpy(vendp, vendor_client, vendor_client_len);
   1291      1.1  dholland 	vendp += vendor_client_len;
   1292      1.1  dholland 	ifctx->dhcpquerytype = DHCP_NOMSG;
   1293      1.1  dholland 	switch (ifctx->state) {
   1294      1.1  dholland 	case IF_DHCP_UNRESOLVED:
   1295      1.1  dholland 		*vendp++ = TAG_DHCP_MSGTYPE;
   1296      1.1  dholland 		*vendp++ = 1;
   1297      1.1  dholland 		*vendp++ = DHCP_DISCOVER;
   1298      1.1  dholland 		ifctx->dhcpquerytype = DHCP_DISCOVER;
   1299      1.1  dholland 		ifctx->gotdhcpserver = 0;
   1300      1.1  dholland 		break;
   1301      1.1  dholland 	case IF_DHCP_OFFERED:
   1302      1.1  dholland 		*vendp++ = TAG_DHCP_MSGTYPE;
   1303      1.1  dholland 		*vendp++ = 1;
   1304      1.1  dholland 		*vendp++ = DHCP_REQUEST;
   1305      1.1  dholland 		ifctx->dhcpquerytype = DHCP_REQUEST;
   1306      1.1  dholland 		*vendp++ = TAG_DHCP_REQ_ADDR;
   1307      1.1  dholland 		*vendp++ = 4;
   1308      1.1  dholland 		memcpy(vendp, &ifctx->reply.yiaddr, 4);
   1309      1.1  dholland 		vendp += 4;
   1310      1.1  dholland 		if (ifctx->gotdhcpserver != 0) {
   1311      1.1  dholland 			*vendp++ = TAG_DHCP_SERVERID;
   1312      1.1  dholland 			*vendp++ = 4;
   1313      1.1  dholland 			memcpy(vendp, &ifctx->dhcpserver, 4);
   1314      1.1  dholland 			vendp += 4;
   1315      1.1  dholland 		}
   1316      1.1  dholland 		*vendp++ = TAG_DHCP_LEASETIME;
   1317      1.1  dholland 		*vendp++ = 4;
   1318      1.1  dholland 		leasetime = htonl(300);
   1319      1.1  dholland 		memcpy(vendp, &leasetime, 4);
   1320      1.1  dholland 		vendp += 4;
   1321      1.1  dholland 		break;
   1322      1.1  dholland 	default:
   1323      1.1  dholland 		break;
   1324      1.1  dholland 	}
   1325      1.1  dholland 	*vendp = TAG_END;
   1326      1.1  dholland 
   1327      1.1  dholland 	ifctx->call.secs = 0;
   1328      1.1  dholland 	ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
   1329      1.1  dholland }
   1330      1.1  dholland 
   1331      1.1  dholland static int
   1332      1.1  dholland bootpc_hascookie(struct bootp_packet *bp)
   1333      1.1  dholland {
   1334      1.1  dholland 
   1335      1.1  dholland 	return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
   1336      1.1  dholland 		bp->vend[2] == 83 && bp->vend[3] == 99);
   1337      1.1  dholland }
   1338      1.1  dholland 
   1339      1.1  dholland static void
   1340      1.1  dholland bootpc_tag_helper(struct bootpc_tagcontext *tctx,
   1341      1.1  dholland     unsigned char *start, int len, int tag)
   1342      1.1  dholland {
   1343      1.1  dholland 	unsigned char *j;
   1344      1.1  dholland 	unsigned char *ej;
   1345      1.1  dholland 	unsigned char code;
   1346      1.1  dholland 
   1347      1.1  dholland 	if (tctx->badtag != 0 || tctx->badopt != 0)
   1348      1.1  dholland 		return;
   1349      1.1  dholland 
   1350      1.1  dholland 	j = start;
   1351      1.1  dholland 	ej = j + len;
   1352      1.1  dholland 
   1353      1.1  dholland 	while (j < ej) {
   1354      1.1  dholland 		code = *j++;
   1355      1.1  dholland 		if (code == TAG_PAD)
   1356      1.1  dholland 			continue;
   1357      1.1  dholland 		if (code == TAG_END)
   1358      1.1  dholland 			return;
   1359      1.1  dholland 		if (j >= ej || j + *j + 1 > ej) {
   1360      1.1  dholland 			tctx->badopt = 1;
   1361      1.1  dholland 			return;
   1362      1.1  dholland 		}
   1363      1.1  dholland 		len = *j++;
   1364      1.1  dholland 		if (code == tag) {
   1365      1.1  dholland 			if (tctx->taglen + len > TAG_MAXLEN) {
   1366      1.1  dholland 				tctx->badtag = 1;
   1367      1.1  dholland 				return;
   1368      1.1  dholland 			}
   1369      1.1  dholland 			tctx->foundopt = 1;
   1370      1.1  dholland 			if (len > 0)
   1371      1.1  dholland 				memcpy(tctx->buf + tctx->taglen,
   1372      1.1  dholland 				       j, len);
   1373      1.1  dholland 			tctx->taglen += len;
   1374      1.1  dholland 		}
   1375      1.1  dholland 		if (code == TAG_OVERLOAD)
   1376      1.1  dholland 			tctx->overload = *j;
   1377      1.1  dholland 
   1378      1.1  dholland 		j += len;
   1379      1.1  dholland 	}
   1380      1.1  dholland }
   1381      1.1  dholland 
   1382      1.1  dholland static unsigned char *
   1383      1.1  dholland bootpc_tag(struct bootpc_tagcontext *tctx,
   1384      1.1  dholland     struct bootp_packet *bp, int len, int tag)
   1385      1.1  dholland {
   1386      1.1  dholland 	tctx->overload = 0;
   1387      1.1  dholland 	tctx->badopt = 0;
   1388      1.1  dholland 	tctx->badtag = 0;
   1389      1.1  dholland 	tctx->foundopt = 0;
   1390      1.1  dholland 	tctx->taglen = 0;
   1391      1.1  dholland 
   1392      1.1  dholland 	if (bootpc_hascookie(bp) == 0)
   1393      1.1  dholland 		return NULL;
   1394      1.1  dholland 
   1395      1.1  dholland 	bootpc_tag_helper(tctx, &bp->vend[4],
   1396      1.1  dholland 			  (unsigned char *) bp + len - &bp->vend[4], tag);
   1397      1.1  dholland 
   1398      1.1  dholland 	if ((tctx->overload & OVERLOAD_FILE) != 0)
   1399      1.1  dholland 		bootpc_tag_helper(tctx,
   1400      1.1  dholland 				  (unsigned char *) bp->file,
   1401      1.1  dholland 				  sizeof(bp->file),
   1402      1.1  dholland 				  tag);
   1403      1.1  dholland 	if ((tctx->overload & OVERLOAD_SNAME) != 0)
   1404      1.1  dholland 		bootpc_tag_helper(tctx,
   1405      1.1  dholland 				  (unsigned char *) bp->sname,
   1406      1.1  dholland 				  sizeof(bp->sname),
   1407      1.1  dholland 				  tag);
   1408      1.1  dholland 
   1409      1.1  dholland 	if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
   1410      1.1  dholland 		return NULL;
   1411      1.1  dholland 	tctx->buf[tctx->taglen] = '\0';
   1412      1.1  dholland 	return tctx->buf;
   1413      1.1  dholland }
   1414      1.1  dholland 
   1415      1.1  dholland static void
   1416      1.1  dholland bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
   1417      1.1  dholland     struct bootpc_globalcontext *gctx)
   1418      1.1  dholland {
   1419      1.1  dholland 	char *p, *s;
   1420      1.1  dholland 	unsigned int ip;
   1421      1.1  dholland 
   1422      1.1  dholland 	ifctx->gotgw = 0;
   1423      1.1  dholland 	ifctx->gotnetmask = 0;
   1424      1.1  dholland 
   1425      1.1  dholland 	clear_sinaddr(&ifctx->myaddr);
   1426      1.1  dholland 	clear_sinaddr(&ifctx->netmask);
   1427      1.1  dholland 	clear_sinaddr(&ifctx->gw);
   1428      1.1  dholland 
   1429      1.1  dholland 	ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
   1430      1.1  dholland 
   1431      1.1  dholland 	ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
   1432      1.1  dholland 
   1433      1.1  dholland 	printf("%s at ", ifctx->ireq.ifr_name);
   1434      1.1  dholland 	print_sin_addr(&ifctx->myaddr);
   1435      1.1  dholland 	printf(" server ");
   1436      1.1  dholland 	print_in_addr(ifctx->reply.siaddr);
   1437      1.1  dholland 
   1438      1.1  dholland 	ifctx->gw.sin_addr = ifctx->reply.giaddr;
   1439      1.1  dholland 	if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
   1440      1.1  dholland 		printf(" via gateway ");
   1441      1.1  dholland 		print_in_addr(ifctx->reply.giaddr);
   1442      1.1  dholland 	}
   1443      1.1  dholland 
   1444      1.1  dholland 	/* This call used for the side effect (overload flag) */
   1445      1.1  dholland 	(void) bootpc_tag(&gctx->tmptag,
   1446      1.1  dholland 			  &ifctx->reply, ifctx->replylen, TAG_END);
   1447      1.1  dholland 
   1448      1.1  dholland 	if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
   1449      1.1  dholland 		if (ifctx->reply.sname[0] != '\0')
   1450      1.1  dholland 			printf(" server name %s", ifctx->reply.sname);
   1451      1.1  dholland 	if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
   1452      1.1  dholland 		if (ifctx->reply.file[0] != '\0')
   1453      1.1  dholland 			printf(" boot file %s", ifctx->reply.file);
   1454      1.1  dholland 
   1455      1.1  dholland 	printf("\n");
   1456      1.1  dholland 
   1457      1.1  dholland 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
   1458      1.1  dholland 		       TAG_SUBNETMASK);
   1459      1.1  dholland 	if (p != NULL) {
   1460      1.1  dholland 		if (gctx->tag.taglen != 4)
   1461      1.1  dholland 			panic("bootpc: subnet mask len is %d",
   1462      1.1  dholland 			      gctx->tag.taglen);
   1463      1.1  dholland 		bcopy(p, &ifctx->netmask.sin_addr, 4);
   1464      1.1  dholland 		ifctx->gotnetmask = 1;
   1465      1.1  dholland 		printf("subnet mask ");
   1466      1.1  dholland 		print_sin_addr(&ifctx->netmask);
   1467      1.1  dholland 		printf(" ");
   1468      1.1  dholland 	}
   1469      1.1  dholland 
   1470      1.1  dholland 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
   1471      1.1  dholland 		       TAG_ROUTERS);
   1472      1.1  dholland 	if (p != NULL) {
   1473      1.1  dholland 		/* Routers */
   1474      1.1  dholland 		if (gctx->tag.taglen % 4)
   1475      1.1  dholland 			panic("bootpc: Router Len is %d", gctx->tag.taglen);
   1476      1.1  dholland 		if (gctx->tag.taglen > 0) {
   1477      1.1  dholland 			bcopy(p, &ifctx->gw.sin_addr, 4);
   1478      1.1  dholland 			printf("router ");
   1479      1.1  dholland 			print_sin_addr(&ifctx->gw);
   1480      1.1  dholland 			printf(" ");
   1481      1.1  dholland 			ifctx->gotgw = 1;
   1482      1.1  dholland 			gctx->gotgw = 1;
   1483      1.1  dholland 		}
   1484      1.1  dholland 	}
   1485      1.1  dholland 
   1486      1.1  dholland 	/*
   1487      1.1  dholland 	 * Choose a root filesystem.  If a value is forced in the environment
   1488      1.1  dholland 	 * and it contains "nfs:", use it unconditionally.  Otherwise, if the
   1489      1.1  dholland 	 * kernel is compiled with the ROOTDEVNAME option, then use it if:
   1490      1.1  dholland 	 *  - The server doesn't provide a pathname.
   1491      1.1  dholland 	 *  - The boothowto flags include RB_DFLTROOT (user said to override
   1492      1.1  dholland 	 *    the server value).
   1493      1.1  dholland 	 */
   1494      1.1  dholland 	p = NULL;
   1495  1.1.1.2  pgoyette 	if ((s = kern_getenv("vfs.root.mountfrom")) != NULL) {
   1496      1.1  dholland 		if ((p = strstr(s, "nfs:")) != NULL)
   1497      1.1  dholland 			p = strdup(p + 4, M_TEMP);
   1498      1.1  dholland 		freeenv(s);
   1499      1.1  dholland 	}
   1500      1.1  dholland 	if (p == NULL) {
   1501      1.1  dholland 		p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
   1502      1.1  dholland 		       TAG_ROOT);
   1503  1.1.1.2  pgoyette 		if (p != NULL)
   1504  1.1.1.2  pgoyette 			ifctx->gotrootpath = 1;
   1505      1.1  dholland 	}
   1506      1.1  dholland #ifdef ROOTDEVNAME
   1507      1.1  dholland 	if ((p == NULL || (boothowto & RB_DFLTROOT) != 0) &&
   1508      1.1  dholland 	    (p = strstr(ROOTDEVNAME, "nfs:")) != NULL) {
   1509      1.1  dholland 		p += 4;
   1510      1.1  dholland 	}
   1511      1.1  dholland #endif
   1512      1.1  dholland 	if (p != NULL) {
   1513      1.1  dholland 		if (gctx->setrootfs != NULL) {
   1514      1.1  dholland 			printf("rootfs %s (ignored) ", p);
   1515      1.1  dholland 		} else 	if (setfs(&nd->root_saddr,
   1516      1.1  dholland 				  nd->root_hostnam, p, &ifctx->reply.siaddr)) {
   1517      1.1  dholland 			if (*p == '/') {
   1518      1.1  dholland 				printf("root_server ");
   1519      1.1  dholland 				print_sin_addr(&nd->root_saddr);
   1520      1.1  dholland 				printf(" ");
   1521      1.1  dholland 			}
   1522      1.1  dholland 			printf("rootfs %s ", p);
   1523      1.1  dholland 			gctx->gotrootpath = 1;
   1524      1.1  dholland 			gctx->setrootfs = ifctx;
   1525      1.1  dholland 
   1526      1.1  dholland 			p = bootpc_tag(&gctx->tag, &ifctx->reply,
   1527      1.1  dholland 				       ifctx->replylen,
   1528      1.1  dholland 				       TAG_ROOTOPTS);
   1529      1.1  dholland 			if (p != NULL) {
   1530      1.1  dholland 				mountopts(&nd->root_args, p);
   1531      1.1  dholland 				printf("rootopts %s ", p);
   1532      1.1  dholland 			}
   1533      1.1  dholland 		} else
   1534      1.1  dholland 			panic("Failed to set rootfs to %s", p);
   1535      1.1  dholland 	}
   1536      1.1  dholland 
   1537      1.1  dholland 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
   1538      1.1  dholland 		       TAG_HOSTNAME);
   1539      1.1  dholland 	if (p != NULL) {
   1540      1.1  dholland 		if (gctx->tag.taglen >= MAXHOSTNAMELEN)
   1541      1.1  dholland 			panic("bootpc: hostname >= %d bytes",
   1542      1.1  dholland 			      MAXHOSTNAMELEN);
   1543      1.1  dholland 		if (gctx->sethostname != NULL) {
   1544      1.1  dholland 			printf("hostname %s (ignored) ", p);
   1545      1.1  dholland 		} else {
   1546      1.1  dholland 			strcpy(nd->my_hostnam, p);
   1547      1.1  dholland 			mtx_lock(&prison0.pr_mtx);
   1548      1.1  dholland 			strcpy(prison0.pr_hostname, p);
   1549      1.1  dholland 			mtx_unlock(&prison0.pr_mtx);
   1550      1.1  dholland 			printf("hostname %s ", p);
   1551      1.1  dholland 			gctx->sethostname = ifctx;
   1552      1.1  dholland 		}
   1553      1.1  dholland 	}
   1554      1.1  dholland 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
   1555      1.1  dholland 			TAG_COOKIE);
   1556      1.1  dholland 	if (p != NULL) {        /* store in a sysctl variable */
   1557      1.1  dholland 		int i, l = sizeof(bootp_cookie) - 1;
   1558      1.1  dholland 		for (i = 0; i < l && p[i] != '\0'; i++)
   1559      1.1  dholland 			bootp_cookie[i] = p[i];
   1560      1.1  dholland 		p[i] = '\0';
   1561      1.1  dholland 	}
   1562      1.1  dholland 
   1563  1.1.1.2  pgoyette 	p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
   1564  1.1.1.2  pgoyette 		       TAG_INTF_MTU);
   1565  1.1.1.2  pgoyette 	if (p != NULL) {
   1566  1.1.1.2  pgoyette 		ifctx->mtu = be16dec(p);
   1567  1.1.1.2  pgoyette 	}
   1568      1.1  dholland 
   1569      1.1  dholland 	printf("\n");
   1570      1.1  dholland 
   1571      1.1  dholland 	if (ifctx->gotnetmask == 0) {
   1572      1.1  dholland 		if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
   1573      1.1  dholland 			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
   1574      1.1  dholland 		else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
   1575      1.1  dholland 			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
   1576      1.1  dholland 		else
   1577      1.1  dholland 			ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
   1578      1.1  dholland 	}
   1579      1.1  dholland }
   1580      1.1  dholland 
   1581      1.1  dholland void
   1582      1.1  dholland bootpc_init(void)
   1583      1.1  dholland {
   1584      1.1  dholland 	struct bootpc_ifcontext *ifctx;		/* Interface BOOTP contexts */
   1585      1.1  dholland 	struct bootpc_globalcontext *gctx; 	/* Global BOOTP context */
   1586      1.1  dholland 	struct ifnet *ifp;
   1587      1.1  dholland 	struct sockaddr_dl *sdl;
   1588      1.1  dholland 	struct ifaddr *ifa;
   1589      1.1  dholland 	int error;
   1590      1.1  dholland #ifndef BOOTP_WIRED_TO
   1591      1.1  dholland 	int ifcnt;
   1592      1.1  dholland #endif
   1593      1.1  dholland 	struct nfsv3_diskless *nd;
   1594      1.1  dholland 	struct thread *td;
   1595      1.1  dholland 	int timeout;
   1596      1.1  dholland 	int delay;
   1597      1.1  dholland 
   1598      1.1  dholland 	timeout = BOOTP_IFACE_WAIT_TIMEOUT * hz;
   1599      1.1  dholland 	delay = hz / 10;
   1600      1.1  dholland 
   1601      1.1  dholland 	nd = &nfsv3_diskless;
   1602      1.1  dholland 	td = curthread;
   1603      1.1  dholland 
   1604      1.1  dholland 	/*
   1605      1.1  dholland 	 * If already filled in, don't touch it here
   1606      1.1  dholland 	 */
   1607      1.1  dholland 	if (nfs_diskless_valid != 0)
   1608      1.1  dholland 		return;
   1609      1.1  dholland 
   1610      1.1  dholland 	gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
   1611      1.1  dholland 	STAILQ_INIT(&gctx->interfaces);
   1612      1.1  dholland 	gctx->xid = ~0xFFFF;
   1613      1.1  dholland 	gctx->starttime = time_second;
   1614      1.1  dholland 
   1615      1.1  dholland 	/*
   1616      1.1  dholland 	 * If ROOTDEVNAME is defined or vfs.root.mountfrom is set then we have
   1617      1.1  dholland 	 * root-path overrides that can potentially let us boot even if we don't
   1618      1.1  dholland 	 * get a root path from the server, so we can treat that as a non-error.
   1619      1.1  dholland 	 */
   1620      1.1  dholland #ifdef ROOTDEVNAME
   1621      1.1  dholland 	gctx->any_root_overrides = 1;
   1622      1.1  dholland #else
   1623      1.1  dholland 	gctx->any_root_overrides = testenv("vfs.root.mountfrom");
   1624      1.1  dholland #endif
   1625      1.1  dholland 
   1626      1.1  dholland 	/*
   1627      1.1  dholland 	 * Find a network interface.
   1628      1.1  dholland 	 */
   1629      1.1  dholland 	CURVNET_SET(TD_TO_VNET(td));
   1630      1.1  dholland #ifdef BOOTP_WIRED_TO
   1631      1.1  dholland 	printf("%s: wired to interface '%s'\n", __func__,
   1632      1.1  dholland 	       __XSTRING(BOOTP_WIRED_TO));
   1633      1.1  dholland 	allocifctx(gctx);
   1634      1.1  dholland #else
   1635      1.1  dholland 	/*
   1636      1.1  dholland 	 * Preallocate interface context storage, if another interface
   1637      1.1  dholland 	 * attaches and wins the race, it won't be eligible for bootp.
   1638      1.1  dholland 	 */
   1639      1.1  dholland 	ifcnt = 0;
   1640      1.1  dholland 	IFNET_RLOCK();
   1641      1.1  dholland 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
   1642      1.1  dholland 		if ((ifp->if_flags &
   1643      1.1  dholland 		     (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
   1644      1.1  dholland 		    IFF_BROADCAST)
   1645      1.1  dholland 			continue;
   1646      1.1  dholland 		switch (ifp->if_alloctype) {
   1647      1.1  dholland 			case IFT_ETHER:
   1648      1.1  dholland 			case IFT_FDDI:
   1649      1.1  dholland 			case IFT_ISO88025:
   1650      1.1  dholland 				break;
   1651      1.1  dholland 			default:
   1652      1.1  dholland 				continue;
   1653      1.1  dholland 		}
   1654      1.1  dholland 		ifcnt++;
   1655      1.1  dholland 	}
   1656      1.1  dholland 	IFNET_RUNLOCK();
   1657      1.1  dholland 	if (ifcnt == 0)
   1658      1.1  dholland 		panic("%s: no eligible interfaces", __func__);
   1659      1.1  dholland 	for (; ifcnt > 0; ifcnt--)
   1660      1.1  dholland 		allocifctx(gctx);
   1661      1.1  dholland #endif
   1662      1.1  dholland 
   1663      1.1  dholland retry:
   1664      1.1  dholland 	ifctx = STAILQ_FIRST(&gctx->interfaces);
   1665      1.1  dholland 	IFNET_RLOCK();
   1666      1.1  dholland 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
   1667      1.1  dholland 		if (ifctx == NULL)
   1668      1.1  dholland 			break;
   1669      1.1  dholland #ifdef BOOTP_WIRED_TO
   1670      1.1  dholland 		if (strcmp(ifp->if_xname, __XSTRING(BOOTP_WIRED_TO)) != 0)
   1671      1.1  dholland 			continue;
   1672      1.1  dholland #else
   1673      1.1  dholland 		if ((ifp->if_flags &
   1674      1.1  dholland 		     (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
   1675      1.1  dholland 		    IFF_BROADCAST)
   1676      1.1  dholland 			continue;
   1677      1.1  dholland 		switch (ifp->if_alloctype) {
   1678      1.1  dholland 			case IFT_ETHER:
   1679      1.1  dholland 			case IFT_FDDI:
   1680      1.1  dholland 			case IFT_ISO88025:
   1681      1.1  dholland 				break;
   1682      1.1  dholland 			default:
   1683      1.1  dholland 				continue;
   1684      1.1  dholland 		}
   1685      1.1  dholland #endif
   1686      1.1  dholland 		strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
   1687      1.1  dholland 		    sizeof(ifctx->ireq.ifr_name));
   1688      1.1  dholland 		ifctx->ifp = ifp;
   1689      1.1  dholland 
   1690      1.1  dholland 		/* Get HW address */
   1691      1.1  dholland 		sdl = NULL;
   1692      1.1  dholland 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
   1693      1.1  dholland 			if (ifa->ifa_addr->sa_family == AF_LINK) {
   1694      1.1  dholland 				sdl = (struct sockaddr_dl *)ifa->ifa_addr;
   1695      1.1  dholland 				if (sdl->sdl_type == IFT_ETHER)
   1696      1.1  dholland 					break;
   1697      1.1  dholland 			}
   1698      1.1  dholland 		if (sdl == NULL)
   1699      1.1  dholland 			panic("bootpc: Unable to find HW address for %s",
   1700      1.1  dholland 			    ifctx->ireq.ifr_name);
   1701      1.1  dholland 		ifctx->sdl = sdl;
   1702      1.1  dholland 
   1703      1.1  dholland 		ifctx = STAILQ_NEXT(ifctx, next);
   1704      1.1  dholland 	}
   1705      1.1  dholland 	IFNET_RUNLOCK();
   1706      1.1  dholland 	CURVNET_RESTORE();
   1707      1.1  dholland 
   1708      1.1  dholland 	if (STAILQ_EMPTY(&gctx->interfaces) ||
   1709      1.1  dholland 	    STAILQ_FIRST(&gctx->interfaces)->ifp == NULL) {
   1710      1.1  dholland 		if (timeout > 0) {
   1711      1.1  dholland 			pause("bootpc", delay);
   1712      1.1  dholland 			timeout -= delay;
   1713      1.1  dholland 			goto retry;
   1714      1.1  dholland 		}
   1715      1.1  dholland #ifdef BOOTP_WIRED_TO
   1716      1.1  dholland 		panic("%s: Could not find interface specified "
   1717      1.1  dholland 		      "by BOOTP_WIRED_TO: "
   1718      1.1  dholland 		      __XSTRING(BOOTP_WIRED_TO), __func__);
   1719      1.1  dholland #else
   1720      1.1  dholland 		panic("%s: no suitable interface", __func__);
   1721      1.1  dholland #endif
   1722      1.1  dholland 	}
   1723      1.1  dholland 
   1724      1.1  dholland 	error = socreate(AF_INET, &bootp_so, SOCK_DGRAM, 0, td->td_ucred, td);
   1725      1.1  dholland 	if (error != 0)
   1726      1.1  dholland 		panic("%s: socreate, error=%d", __func__, error);
   1727      1.1  dholland 
   1728      1.1  dholland 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
   1729      1.1  dholland 		bootpc_fakeup_interface(ifctx, td);
   1730      1.1  dholland 
   1731      1.1  dholland 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
   1732      1.1  dholland 		bootpc_compose_query(ifctx, td);
   1733      1.1  dholland 
   1734      1.1  dholland 	error = bootpc_call(gctx, td);
   1735      1.1  dholland 	if (error != 0) {
   1736      1.1  dholland 		printf("BOOTP call failed\n");
   1737      1.1  dholland 	}
   1738      1.1  dholland 
   1739      1.1  dholland 	mountopts(&nd->root_args, NULL);
   1740      1.1  dholland 
   1741      1.1  dholland 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
   1742      1.1  dholland 		if (bootpc_ifctx_isresolved(ifctx) != 0)
   1743      1.1  dholland 			bootpc_decode_reply(nd, ifctx, gctx);
   1744      1.1  dholland 
   1745      1.1  dholland #ifdef BOOTP_NFSROOT
   1746      1.1  dholland 	if (gctx->gotrootpath == 0 && gctx->any_root_overrides == 0)
   1747      1.1  dholland 		panic("bootpc: No root path offered");
   1748      1.1  dholland #endif
   1749      1.1  dholland 
   1750      1.1  dholland 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
   1751      1.1  dholland 		bootpc_adjust_interface(ifctx, gctx, td);
   1752      1.1  dholland 
   1753      1.1  dholland 	soclose(bootp_so);
   1754      1.1  dholland 
   1755      1.1  dholland 	STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
   1756      1.1  dholland 		if (ifctx->gotrootpath != 0)
   1757      1.1  dholland 			break;
   1758      1.1  dholland 	if (ifctx == NULL) {
   1759      1.1  dholland 		STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
   1760      1.1  dholland 			if (bootpc_ifctx_isresolved(ifctx) != 0)
   1761      1.1  dholland 				break;
   1762      1.1  dholland 	}
   1763      1.1  dholland 	if (ifctx == NULL)
   1764      1.1  dholland 		goto out;
   1765      1.1  dholland 
   1766      1.1  dholland 	if (gctx->gotrootpath != 0) {
   1767      1.1  dholland 
   1768  1.1.1.2  pgoyette 		kern_setenv("boot.netif.name", ifctx->ifp->if_xname);
   1769      1.1  dholland 
   1770  1.1.1.2  pgoyette 		bootpc_add_default_route(ifctx);
   1771      1.1  dholland 		error = md_mount(&nd->root_saddr, nd->root_hostnam,
   1772      1.1  dholland 				 nd->root_fh, &nd->root_fhsize,
   1773      1.1  dholland 				 &nd->root_args, td);
   1774  1.1.1.2  pgoyette 		bootpc_remove_default_route(ifctx);
   1775      1.1  dholland 		if (error != 0) {
   1776      1.1  dholland 			if (gctx->any_root_overrides == 0)
   1777      1.1  dholland 				panic("nfs_boot: mount root, error=%d", error);
   1778      1.1  dholland 			else
   1779      1.1  dholland 				goto out;
   1780      1.1  dholland 		}
   1781      1.1  dholland 		rootdevnames[0] = "nfs:";
   1782      1.1  dholland 		nfs_diskless_valid = 3;
   1783      1.1  dholland 	}
   1784      1.1  dholland 
   1785      1.1  dholland 	strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
   1786      1.1  dholland 	bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
   1787      1.1  dholland 	bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
   1788      1.1  dholland 	((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
   1789      1.1  dholland 		ifctx->myaddr.sin_addr.s_addr |
   1790      1.1  dholland 		~ ifctx->netmask.sin_addr.s_addr;
   1791      1.1  dholland 	bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
   1792  1.1.1.2  pgoyette 	bcopy(&ifctx->gw, &nd->mygateway, sizeof(ifctx->gw));
   1793      1.1  dholland 
   1794      1.1  dholland out:
   1795      1.1  dholland 	while((ifctx = STAILQ_FIRST(&gctx->interfaces)) != NULL) {
   1796      1.1  dholland 		STAILQ_REMOVE_HEAD(&gctx->interfaces, next);
   1797      1.1  dholland 		free(ifctx, M_TEMP);
   1798      1.1  dholland 	}
   1799      1.1  dholland 	free(gctx, M_TEMP);
   1800      1.1  dholland }
   1801      1.1  dholland 
   1802      1.1  dholland /*
   1803      1.1  dholland  * RPC: mountd/mount
   1804      1.1  dholland  * Given a server pathname, get an NFS file handle.
   1805      1.1  dholland  * Also, sets sin->sin_port to the NFS service port.
   1806      1.1  dholland  */
   1807      1.1  dholland static int
   1808      1.1  dholland md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
   1809      1.1  dholland     struct nfs_args *args, struct thread *td)
   1810      1.1  dholland {
   1811      1.1  dholland 	struct mbuf *m;
   1812      1.1  dholland 	int error;
   1813      1.1  dholland 	int authunixok;
   1814      1.1  dholland 	int authcount;
   1815      1.1  dholland 	int authver;
   1816      1.1  dholland 
   1817      1.1  dholland #define	RPCPROG_MNT	100005
   1818      1.1  dholland #define	RPCMNT_VER1	1
   1819      1.1  dholland #define RPCMNT_VER3	3
   1820      1.1  dholland #define	RPCMNT_MOUNT	1
   1821      1.1  dholland #define	AUTH_SYS	1		/* unix style (uid, gids) */
   1822      1.1  dholland #define AUTH_UNIX	AUTH_SYS
   1823      1.1  dholland 
   1824      1.1  dholland 	/* XXX honor v2/v3 flags in args->flags? */
   1825      1.1  dholland #ifdef BOOTP_NFSV3
   1826      1.1  dholland 	/* First try NFS v3 */
   1827      1.1  dholland 	/* Get port number for MOUNTD. */
   1828      1.1  dholland 	error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
   1829      1.1  dholland 			     &mdsin->sin_port, td);
   1830      1.1  dholland 	if (error == 0) {
   1831      1.1  dholland 		m = xdr_string_encode(path, strlen(path));
   1832      1.1  dholland 
   1833      1.1  dholland 		/* Do RPC to mountd. */
   1834      1.1  dholland 		error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
   1835      1.1  dholland 				  RPCMNT_MOUNT, &m, NULL, td);
   1836      1.1  dholland 	}
   1837      1.1  dholland 	if (error == 0) {
   1838      1.1  dholland 		args->flags |= NFSMNT_NFSV3;
   1839      1.1  dholland 	} else {
   1840      1.1  dholland #endif
   1841      1.1  dholland 		/* Fallback to NFS v2 */
   1842      1.1  dholland 
   1843      1.1  dholland 		/* Get port number for MOUNTD. */
   1844      1.1  dholland 		error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
   1845      1.1  dholland 				     &mdsin->sin_port, td);
   1846      1.1  dholland 		if (error != 0)
   1847      1.1  dholland 			return error;
   1848      1.1  dholland 
   1849      1.1  dholland 		m = xdr_string_encode(path, strlen(path));
   1850      1.1  dholland 
   1851      1.1  dholland 		/* Do RPC to mountd. */
   1852      1.1  dholland 		error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
   1853      1.1  dholland 				  RPCMNT_MOUNT, &m, NULL, td);
   1854      1.1  dholland 		if (error != 0)
   1855      1.1  dholland 			return error;	/* message already freed */
   1856      1.1  dholland 
   1857      1.1  dholland #ifdef BOOTP_NFSV3
   1858      1.1  dholland 	}
   1859      1.1  dholland #endif
   1860      1.1  dholland 
   1861      1.1  dholland 	if (xdr_int_decode(&m, &error) != 0 || error != 0)
   1862      1.1  dholland 		goto bad;
   1863      1.1  dholland 
   1864      1.1  dholland 	if ((args->flags & NFSMNT_NFSV3) != 0) {
   1865      1.1  dholland 		if (xdr_int_decode(&m, fhsizep) != 0 ||
   1866      1.1  dholland 		    *fhsizep > NFSX_V3FHMAX ||
   1867      1.1  dholland 		    *fhsizep <= 0)
   1868      1.1  dholland 			goto bad;
   1869      1.1  dholland 	} else
   1870      1.1  dholland 		*fhsizep = NFSX_V2FH;
   1871      1.1  dholland 
   1872      1.1  dholland 	if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
   1873      1.1  dholland 		goto bad;
   1874      1.1  dholland 
   1875      1.1  dholland 	if (args->flags & NFSMNT_NFSV3) {
   1876      1.1  dholland 		if (xdr_int_decode(&m, &authcount) != 0)
   1877      1.1  dholland 			goto bad;
   1878      1.1  dholland 		authunixok = 0;
   1879      1.1  dholland 		if (authcount < 0 || authcount > 100)
   1880      1.1  dholland 			goto bad;
   1881      1.1  dholland 		while (authcount > 0) {
   1882      1.1  dholland 			if (xdr_int_decode(&m, &authver) != 0)
   1883      1.1  dholland 				goto bad;
   1884      1.1  dholland 			if (authver == AUTH_UNIX)
   1885      1.1  dholland 				authunixok = 1;
   1886      1.1  dholland 			authcount--;
   1887      1.1  dholland 		}
   1888      1.1  dholland 		if (authunixok == 0)
   1889      1.1  dholland 			goto bad;
   1890      1.1  dholland 	}
   1891      1.1  dholland 
   1892      1.1  dholland 	/* Set port number for NFS use. */
   1893      1.1  dholland 	error = krpc_portmap(mdsin, NFS_PROG,
   1894      1.1  dholland 			     (args->flags &
   1895      1.1  dholland 			      NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
   1896      1.1  dholland 			     &mdsin->sin_port, td);
   1897      1.1  dholland 
   1898      1.1  dholland 	goto out;
   1899      1.1  dholland 
   1900      1.1  dholland bad:
   1901      1.1  dholland 	error = EBADRPC;
   1902      1.1  dholland 
   1903      1.1  dholland out:
   1904      1.1  dholland 	m_freem(m);
   1905      1.1  dholland 	return error;
   1906      1.1  dholland }
   1907      1.1  dholland 
   1908      1.1  dholland SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);
   1909