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

Cache object: e64350ef0d8200d43b63d3e1ac6ac80a


[ 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.