The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/nfsclient/bootp_subr.c

Version: -  FREEBSD  -  FREEBSD11  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: 1faca4754cb90667865ffbb1f042c91f


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.