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/5.0/sys/nfsclient/bootp_subr.c 102052 2002-08-18 07:05:00Z sobomax $");
   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 <nfs/rpcv2.h>
   69 #include <nfs/nfsproto.h>
   70 #include <nfsclient/nfs.h>
   71 #include <nfsclient/nfsdiskless.h>
   72 #include <nfsclient/krpc.h>
   73 #include <nfs/xdr_subs.h>
   74 
   75 
   76 #define BOOTP_MIN_LEN           300     /* Minimum size of bootp udp packet */
   77 
   78 #ifndef BOOTP_SETTLE_DELAY
   79 #define BOOTP_SETTLE_DELAY 3
   80 #endif
   81 
   82 /*
   83  * What is the longest we will wait before re-sending a request?
   84  * Note this is also the frequency of "RPC timeout" messages.
   85  * The re-send loop count sup linearly to this maximum, so the
   86  * first complaint will happen after (1+2+3+4+5)=15 seconds.
   87  */
   88 #define MAX_RESEND_DELAY 5      /* seconds */
   89 
   90 /* Definitions from RFC951 */
   91 struct bootp_packet {
   92         u_int8_t op;
   93         u_int8_t htype;
   94         u_int8_t hlen;
   95         u_int8_t hops;
   96         u_int32_t xid;
   97         u_int16_t secs;
   98         u_int16_t flags;
   99         struct in_addr ciaddr;
  100         struct in_addr yiaddr;
  101         struct in_addr siaddr;
  102         struct in_addr giaddr;
  103         unsigned char chaddr[16];
  104         char sname[64];
  105         char file[128];
  106         unsigned char vend[1222];
  107 };
  108 
  109 struct bootpc_ifcontext {
  110         struct bootpc_ifcontext *next;
  111         struct bootp_packet call;
  112         struct bootp_packet reply;
  113         int replylen;
  114         int overload;
  115         struct socket *so;
  116         struct ifreq ireq;
  117         struct ifnet *ifp;
  118         struct sockaddr_dl *sdl;
  119         struct sockaddr_in myaddr;
  120         struct sockaddr_in netmask;
  121         struct sockaddr_in gw;
  122         struct sockaddr_in broadcast;   /* Different for each interface */
  123         int gotgw;
  124         int gotnetmask;
  125         int gotrootpath;
  126         int outstanding;
  127         int sentmsg;
  128         u_int32_t xid;
  129         enum {
  130                 IF_BOOTP_UNRESOLVED,
  131                 IF_BOOTP_RESOLVED,
  132                 IF_BOOTP_FAILED,
  133                 IF_DHCP_UNRESOLVED,
  134                 IF_DHCP_OFFERED,
  135                 IF_DHCP_RESOLVED,
  136                 IF_DHCP_FAILED,
  137         } state;
  138         int dhcpquerytype;              /* dhcp type sent */
  139         struct in_addr dhcpserver;
  140         int gotdhcpserver;
  141 };
  142 
  143 #define TAG_MAXLEN 1024
  144 struct bootpc_tagcontext {
  145         char buf[TAG_MAXLEN + 1];
  146         int overload;
  147         int badopt;
  148         int badtag;
  149         int foundopt;
  150         int taglen;
  151 };
  152 
  153 struct bootpc_globalcontext {
  154         struct bootpc_ifcontext *interfaces;
  155         struct bootpc_ifcontext *lastinterface;
  156         u_int32_t xid;
  157         int gotrootpath;
  158         int gotswappath;
  159         int gotgw;
  160         int ifnum;
  161         int secs;
  162         int starttime;
  163         struct bootp_packet reply;
  164         int replylen;
  165         struct bootpc_ifcontext *setswapfs;
  166         struct bootpc_ifcontext *setrootfs;
  167         struct bootpc_ifcontext *sethostname;
  168         char lookup_path[24];
  169         struct bootpc_tagcontext tmptag;
  170         struct bootpc_tagcontext tag;
  171 };
  172 
  173 #define IPPORT_BOOTPC 68
  174 #define IPPORT_BOOTPS 67
  175 
  176 #define BOOTP_REQUEST 1
  177 #define BOOTP_REPLY 2
  178 
  179 /* Common tags */
  180 #define TAG_PAD           0  /* Pad option, implicit length 1 */
  181 #define TAG_SUBNETMASK    1  /* RFC 950 subnet mask */
  182 #define TAG_ROUTERS       3  /* Routers (in order of preference) */
  183 #define TAG_HOSTNAME     12  /* Client host name */
  184 #define TAG_ROOT         17  /* Root path */
  185 
  186 /* DHCP specific tags */
  187 #define TAG_OVERLOAD     52  /* Option Overload */
  188 #define TAG_MAXMSGSIZE   57  /* Maximum DHCP Message Size */
  189 
  190 #define TAG_END         255  /* End Option (i.e. no more options) */
  191 
  192 /* Overload values */
  193 #define OVERLOAD_FILE     1
  194 #define OVERLOAD_SNAME    2
  195 
  196 /* Site specific tags: */
  197 #define TAG_SWAP        128
  198 #define TAG_SWAPSIZE    129
  199 #define TAG_ROOTOPTS    130
  200 #define TAG_SWAPOPTS    131
  201 #define TAG_COOKIE      134     /* ascii info for userland, via sysctl */
  202 
  203 #define TAG_DHCP_MSGTYPE 53
  204 #define TAG_DHCP_REQ_ADDR 50
  205 #define TAG_DHCP_SERVERID 54
  206 #define TAG_DHCP_LEASETIME 51
  207 
  208 #define TAG_VENDOR_INDENTIFIER 60
  209 
  210 #define DHCP_NOMSG    0
  211 #define DHCP_DISCOVER 1
  212 #define DHCP_OFFER    2
  213 #define DHCP_REQUEST  3
  214 #define DHCP_ACK      5
  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      md_lookup_swap(struct sockaddr_in *mdsin, char *path,
  224                     u_char *fhp, int *fhsizep, struct nfs_args *args,
  225                     struct thread *td);
  226 static int      setfs(struct sockaddr_in *addr, char *path, char *p);
  227 static int      getdec(char **ptr);
  228 static char     *substr(char *a, char *b);
  229 static void     mountopts(struct nfs_args *args, char *p);
  230 static int      xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
  231 static int      xdr_int_decode(struct mbuf **ptr, int *iptr);
  232 static void     print_in_addr(struct in_addr addr);
  233 static void     print_sin_addr(struct sockaddr_in *addr);
  234 static void     clear_sinaddr(struct sockaddr_in *sin);
  235 static struct bootpc_ifcontext *allocifctx(struct bootpc_globalcontext *gctx);
  236 static void     bootpc_compose_query(struct bootpc_ifcontext *ifctx,
  237                     struct bootpc_globalcontext *gctx, struct thread *td);
  238 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
  239                     struct bootp_packet *bp, int len, int tag);
  240 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
  241                     unsigned char *start, int len, int tag);
  242 
  243 #ifdef BOOTP_DEBUG
  244 void bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma);
  245 void bootpboot_p_ma(struct sockaddr *ma);
  246 void bootpboot_p_rtentry(struct rtentry *rt);
  247 void bootpboot_p_tree(struct radix_node *rn);
  248 void bootpboot_p_rtlist(void);
  249 void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
  250 void bootpboot_p_iflist(void);
  251 #endif
  252 
  253 static int      bootpc_call(struct bootpc_globalcontext *gctx,
  254                     struct thread *td);
  255 
  256 static int      bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
  257                     struct bootpc_globalcontext *gctx, struct thread *td);
  258 
  259 static int      bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
  260                     struct bootpc_globalcontext *gctx, struct thread *td);
  261 
  262 static void     bootpc_decode_reply(struct nfsv3_diskless *nd,
  263                     struct bootpc_ifcontext *ifctx,
  264                     struct bootpc_globalcontext *gctx);
  265 
  266 static int      bootpc_received(struct bootpc_globalcontext *gctx,
  267                     struct bootpc_ifcontext *ifctx);
  268 
  269 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
  270 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
  271 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
  272 
  273 /*
  274  * In order to have multiple active interfaces with address 0.0.0.0
  275  * and be able to send data to a selected interface, we perform
  276  * some tricks:
  277  *
  278  *  - The 'broadcast' address is different for each interface.
  279  *
  280  *  - We temporarily add routing pointing 255.255.255.255 to the
  281  *    selected interface broadcast address, thus the packet sent
  282  *    goes to that interface.
  283  */
  284 
  285 #ifdef BOOTP_DEBUG
  286 void
  287 bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
  288 {
  289 
  290         if (sa == NULL) {
  291                 printf("(sockaddr *) <null>");
  292                 return;
  293         }
  294         switch (sa->sa_family) {
  295         case AF_INET:
  296         {
  297                 struct sockaddr_in *sin;
  298 
  299                 sin = (struct sockaddr_in *) sa;
  300                 printf("inet ");
  301                 print_sin_addr(sin);
  302                 if (ma != NULL) {
  303                         sin = (struct sockaddr_in *) ma;
  304                         printf(" mask ");
  305                         print_sin_addr(sin);
  306                 }
  307         }
  308         break;
  309         case AF_LINK:
  310         {
  311                 struct sockaddr_dl *sli;
  312                 int i;
  313 
  314                 sli = (struct sockaddr_dl *) sa;
  315                 printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
  316                 for (i = 0; i < sli->sdl_alen; i++) {
  317                         if (i > 0)
  318                                 printf(":");
  319                         printf("%x", ((unsigned char *) LLADDR(sli))[i]);
  320                 }
  321         }
  322         break;
  323         default:
  324                 printf("af%d", sa->sa_family);
  325         }
  326 }
  327 
  328 void
  329 bootpboot_p_ma(struct sockaddr *ma)
  330 {
  331 
  332         if (ma == NULL) {
  333                 printf("<null>");
  334                 return;
  335         }
  336         printf("%x", *(int *)ma);
  337 }
  338 
  339 void
  340 bootpboot_p_rtentry(struct rtentry *rt)
  341 {
  342 
  343         bootpboot_p_sa(rt_key(rt), rt_mask(rt));
  344         printf(" ");
  345         bootpboot_p_ma(rt->rt_genmask);
  346         printf(" ");
  347         bootpboot_p_sa(rt->rt_gateway, NULL);
  348         printf(" ");
  349         printf("flags %x", (unsigned short) rt->rt_flags);
  350         printf(" %d", (int) rt->rt_rmx.rmx_expire);
  351         printf(" %s%d\n", rt->rt_ifp->if_name, rt->rt_ifp->if_unit);
  352 }
  353 
  354 void
  355 bootpboot_p_tree(struct radix_node *rn)
  356 {
  357 
  358         while (rn != NULL) {
  359                 if (rn->rn_bit < 0) {
  360                         if ((rn->rn_flags & RNF_ROOT) != 0) {
  361                         } else {
  362                                 bootpboot_p_rtentry((struct rtentry *) rn);
  363                         }
  364                         rn = rn->rn_dupedkey;
  365                 } else {
  366                         bootpboot_p_tree(rn->rn_left);
  367                         bootpboot_p_tree(rn->rn_right);
  368                         return;
  369                 }
  370         }
  371 }
  372 
  373 void
  374 bootpboot_p_rtlist(void)
  375 {
  376 
  377         printf("Routing table:\n");
  378         bootpboot_p_tree(rt_tables[AF_INET]->rnh_treetop);
  379 }
  380 
  381 void
  382 bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
  383 {
  384 
  385         printf("%s%d flags %x, addr ",
  386                ifp->if_name,
  387                ifp->if_unit,
  388                ifp->if_flags);
  389         print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
  390         printf(", broadcast ");
  391         print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
  392         printf(", netmask ");
  393         print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
  394         printf("\n");
  395 }
  396 
  397 void
  398 bootpboot_p_iflist(void)
  399 {
  400         struct ifnet *ifp;
  401         struct ifaddr *ifa;
  402 
  403         printf("Interface list:\n");
  404         for (ifp = TAILQ_FIRST(&ifnet);
  405              ifp != NULL;
  406              ifp = TAILQ_NEXT(ifp, if_link)) {
  407                 for (ifa = TAILQ_FIRST(&ifp->if_addrhead);
  408                      ifa != NULL;
  409                      ifa = TAILQ_NEXT(ifa, ifa_link))
  410                         if (ifa->ifa_addr->sa_family == AF_INET)
  411                                 bootpboot_p_if(ifp, ifa);
  412         }
  413 }
  414 #endif /* defined(BOOTP_DEBUG) */
  415 
  416 static void
  417 clear_sinaddr(struct sockaddr_in *sin)
  418 {
  419 
  420         bzero(sin, sizeof(*sin));
  421         sin->sin_len = sizeof(*sin);
  422         sin->sin_family = AF_INET;
  423         sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
  424         sin->sin_port = 0;
  425 }
  426 
  427 static struct bootpc_ifcontext *
  428 allocifctx(struct bootpc_globalcontext *gctx)
  429 {
  430         struct bootpc_ifcontext *ifctx;
  431         ifctx = (struct bootpc_ifcontext *) malloc(sizeof(*ifctx),
  432                                                    M_TEMP, M_WAITOK);
  433         if (ifctx == NULL)
  434                 panic("Failed to allocate bootp interface context structure");
  435 
  436         bzero(ifctx, sizeof(*ifctx));
  437         ifctx->xid = gctx->xid;
  438 #ifdef BOOTP_NO_DHCP
  439         ifctx->state = IF_BOOTP_UNRESOLVED;
  440 #else
  441         ifctx->state = IF_DHCP_UNRESOLVED;
  442 #endif
  443         gctx->xid += 0x100;
  444         return ifctx;
  445 }
  446 
  447 static __inline int
  448 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
  449 {
  450 
  451         if (ifctx->state == IF_BOOTP_RESOLVED ||
  452             ifctx->state == IF_DHCP_RESOLVED)
  453                 return 1;
  454         return 0;
  455 }
  456 
  457 static __inline int
  458 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
  459 {
  460 
  461         if (ifctx->state == IF_BOOTP_UNRESOLVED ||
  462             ifctx->state == IF_DHCP_UNRESOLVED)
  463                 return 1;
  464         return 0;
  465 }
  466 
  467 static __inline int
  468 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
  469 {
  470 
  471         if (ifctx->state == IF_BOOTP_FAILED ||
  472             ifctx->state == IF_DHCP_FAILED)
  473                 return 1;
  474         return 0;
  475 }
  476 
  477 static int
  478 bootpc_received(struct bootpc_globalcontext *gctx,
  479     struct bootpc_ifcontext *ifctx)
  480 {
  481         unsigned char dhcpreplytype;
  482         char *p;
  483 
  484         /*
  485          * Need timeout for fallback to less
  486          * desirable alternative.
  487          */
  488 
  489         /* This call used for the side effect (badopt flag) */
  490         (void) bootpc_tag(&gctx->tmptag, &gctx->reply,
  491                           gctx->replylen,
  492                           TAG_END);
  493 
  494         /* If packet is invalid, ignore it */
  495         if (gctx->tmptag.badopt != 0)
  496                 return 0;
  497 
  498         p = bootpc_tag(&gctx->tmptag, &gctx->reply,
  499                        gctx->replylen, TAG_DHCP_MSGTYPE);
  500         if (p != NULL)
  501                 dhcpreplytype = *p;
  502         else
  503                 dhcpreplytype = DHCP_NOMSG;
  504 
  505         switch (ifctx->dhcpquerytype) {
  506         case DHCP_DISCOVER:
  507                 if (dhcpreplytype != DHCP_OFFER         /* Normal DHCP offer */
  508 #ifndef BOOTP_FORCE_DHCP
  509                     && dhcpreplytype != DHCP_NOMSG      /* Fallback to BOOTP */
  510 #endif
  511                         )
  512                         return 0;
  513                 break;
  514         case DHCP_REQUEST:
  515                 if (dhcpreplytype != DHCP_ACK)
  516                         return 0;
  517         case DHCP_NOMSG:
  518                 break;
  519         }
  520 
  521         /* Ignore packet unless it gives us a root tag we didn't have */
  522 
  523         if ((ifctx->state == IF_BOOTP_RESOLVED ||
  524              (ifctx->dhcpquerytype == DHCP_DISCOVER &&
  525               (ifctx->state == IF_DHCP_OFFERED ||
  526                ifctx->state == IF_DHCP_RESOLVED))) &&
  527             (bootpc_tag(&gctx->tmptag, &ifctx->reply,
  528                         ifctx->replylen,
  529                         TAG_ROOT) != NULL ||
  530              bootpc_tag(&gctx->tmptag, &gctx->reply,
  531                         gctx->replylen,
  532                         TAG_ROOT) == NULL))
  533                 return 0;
  534 
  535         bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
  536         ifctx->replylen = gctx->replylen;
  537 
  538         /* XXX: Only reset if 'perfect' response */
  539         if (ifctx->state == IF_BOOTP_UNRESOLVED)
  540                 ifctx->state = IF_BOOTP_RESOLVED;
  541         else if (ifctx->state == IF_DHCP_UNRESOLVED &&
  542                  ifctx->dhcpquerytype == DHCP_DISCOVER) {
  543                 if (dhcpreplytype == DHCP_OFFER)
  544                         ifctx->state = IF_DHCP_OFFERED;
  545                 else
  546                         ifctx->state = IF_BOOTP_RESOLVED;       /* Fallback */
  547         } else if (ifctx->state == IF_DHCP_OFFERED &&
  548                    ifctx->dhcpquerytype == DHCP_REQUEST)
  549                 ifctx->state = IF_DHCP_RESOLVED;
  550 
  551 
  552         if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
  553             ifctx->state != IF_BOOTP_RESOLVED) {
  554                 p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
  555                                ifctx->replylen, TAG_DHCP_SERVERID);
  556                 if (p != NULL && gctx->tmptag.taglen == 4) {
  557                         memcpy(&ifctx->dhcpserver, p, 4);
  558                         ifctx->gotdhcpserver = 1;
  559                 } else
  560                         ifctx->gotdhcpserver = 0;
  561                 return 1;
  562         }
  563 
  564         ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
  565                                          ifctx->replylen,
  566                                          TAG_ROOT) != NULL);
  567         ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
  568                                    ifctx->replylen,
  569                                    TAG_ROUTERS) != NULL);
  570         ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
  571                                         ifctx->replylen,
  572                                         TAG_SUBNETMASK) != NULL);
  573         return 1;
  574 }
  575 
  576 static int
  577 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
  578 {
  579         struct socket *so;
  580         struct sockaddr_in *sin, dst;
  581         struct uio auio;
  582         struct sockopt sopt;
  583         struct iovec aio;
  584         int error, on, rcvflg, timo, len;
  585         time_t atimo;
  586         time_t rtimo;
  587         struct timeval tv;
  588         struct bootpc_ifcontext *ifctx;
  589         int outstanding;
  590         int gotrootpath;
  591         int retry;
  592         const char *s;
  593 
  594         /*
  595          * Create socket and set its recieve timeout.
  596          */
  597         error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td->td_ucred, td);
  598         if (error != 0)
  599                 goto out;
  600 
  601         tv.tv_sec = 1;
  602         tv.tv_usec = 0;
  603         bzero(&sopt, sizeof(sopt));
  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                         tsleep(&error, PZERO + 8, "bootpw", 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         return error;
  969 }
  970 
  971 static int
  972 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
  973     struct bootpc_globalcontext *gctx, struct thread *td)
  974 {
  975         struct sockaddr_in *sin;
  976         int error;
  977         struct ifreq *ireq;
  978         struct socket *so;
  979         struct ifaddr *ifa;
  980         struct sockaddr_dl *sdl;
  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         for (ifa = TAILQ_FIRST(&ifctx->ifp->if_addrhead);
 1045              ifa != NULL;
 1046              ifa = TAILQ_NEXT(ifa, ifa_link))
 1047                 if (ifa->ifa_addr->sa_family == AF_LINK &&
 1048                     (sdl = ((struct sockaddr_dl *) ifa->ifa_addr)) != NULL &&
 1049                     sdl->sdl_type == IFT_ETHER)
 1050                         break;
 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 {
 1158         unsigned int ip;
 1159         int val;
 1160 
 1161         ip = 0;
 1162         if (((val = getdec(&p)) < 0) || (val > 255))
 1163                 return 0;
 1164         ip = val << 24;
 1165         if (*p != '.')
 1166                 return 0;
 1167         p++;
 1168         if (((val = getdec(&p)) < 0) || (val > 255))
 1169                 return 0;
 1170         ip |= (val << 16);
 1171         if (*p != '.')
 1172                 return 0;
 1173         p++;
 1174         if (((val = getdec(&p)) < 0) || (val > 255))
 1175                 return 0;
 1176         ip |= (val << 8);
 1177         if (*p != '.')
 1178                 return 0;
 1179         p++;
 1180         if (((val = getdec(&p)) < 0) || (val > 255))
 1181                 return 0;
 1182         ip |= val;
 1183         if (*p != ':')
 1184                 return 0;
 1185         p++;
 1186 
 1187         addr->sin_addr.s_addr = htonl(ip);
 1188         addr->sin_len = sizeof(struct sockaddr_in);
 1189         addr->sin_family = AF_INET;
 1190 
 1191         strncpy(path, p, MNAMELEN - 1);
 1192         return 1;
 1193 }
 1194 
 1195 static int
 1196 getdec(char **ptr)
 1197 {
 1198         char *p;
 1199         int ret;
 1200 
 1201         p = *ptr;
 1202         ret = 0;
 1203         if ((*p < '') || (*p > '9'))
 1204                 return -1;
 1205         while ((*p >= '') && (*p <= '9')) {
 1206                 ret = ret * 10 + (*p - '');
 1207                 p++;
 1208         }
 1209         *ptr = p;
 1210         return ret;
 1211 }
 1212 
 1213 static char *
 1214 substr(char *a, char *b)
 1215 {
 1216         char *loc1;
 1217         char *loc2;
 1218 
 1219         while (*a != '\0') {
 1220                 loc1 = a;
 1221                 loc2 = b;
 1222                 while (*loc1 == *loc2++) {
 1223                         if (*loc1 == '\0')
 1224                                 return 0;
 1225                         loc1++;
 1226                         if (*loc2 == '\0')
 1227                                 return loc1;
 1228                 }
 1229                 a++;
 1230         }
 1231         return 0;
 1232 }
 1233 
 1234 static void
 1235 mountopts(struct nfs_args *args, char *p)
 1236 {
 1237         char *tmp;
 1238 
 1239         args->version = NFS_ARGSVERSION;
 1240         args->rsize = 8192;
 1241         args->wsize = 8192;
 1242         args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
 1243         args->sotype = SOCK_DGRAM;
 1244         if (p == NULL)
 1245                 return;
 1246         if ((tmp = (char *)substr(p, "rsize=")))
 1247                 args->rsize = getdec(&tmp);
 1248         if ((tmp = (char *)substr(p, "wsize=")))
 1249                 args->wsize = getdec(&tmp);
 1250         if ((tmp = (char *)substr(p, "intr")))
 1251                 args->flags |= NFSMNT_INT;
 1252         if ((tmp = (char *)substr(p, "soft")))
 1253                 args->flags |= NFSMNT_SOFT;
 1254         if ((tmp = (char *)substr(p, "noconn")))
 1255                 args->flags |= NFSMNT_NOCONN;
 1256         if ((tmp = (char *)substr(p, "tcp")))
 1257                 args->sotype = SOCK_STREAM;
 1258 }
 1259 
 1260 static int
 1261 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
 1262 {
 1263         struct mbuf *m;
 1264         int alignedlen;
 1265 
 1266         m = *mptr;
 1267         alignedlen = ( len + 3 ) & ~3;
 1268 
 1269         if (m->m_len < alignedlen) {
 1270                 m = m_pullup(m, alignedlen);
 1271                 if (m == NULL) {
 1272                         *mptr = NULL;
 1273                         return EBADRPC;
 1274                 }
 1275         }
 1276         bcopy(mtod(m, u_char *), buf, len);
 1277         m_adj(m, alignedlen);
 1278         *mptr = m;
 1279         return 0;
 1280 }
 1281 
 1282 static int
 1283 xdr_int_decode(struct mbuf **mptr, int *iptr)
 1284 {
 1285         u_int32_t i;
 1286 
 1287         if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
 1288                 return EBADRPC;
 1289         *iptr = fxdr_unsigned(u_int32_t, i);
 1290         return 0;
 1291 }
 1292 
 1293 static void
 1294 print_sin_addr(struct sockaddr_in *sin)
 1295 {
 1296 
 1297         print_in_addr(sin->sin_addr);
 1298 }
 1299 
 1300 static void
 1301 print_in_addr(struct in_addr addr)
 1302 {
 1303         unsigned int ip;
 1304 
 1305         ip = ntohl(addr.s_addr);
 1306         printf("%d.%d.%d.%d",
 1307                ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
 1308 }
 1309 
 1310 static void
 1311 bootpc_compose_query(struct bootpc_ifcontext *ifctx,
 1312     struct bootpc_globalcontext *gctx, struct thread *td)
 1313 {
 1314         unsigned char *vendp;
 1315         unsigned char vendor_client[64];
 1316         uint32_t leasetime;
 1317         uint8_t vendor_client_len;
 1318 
 1319         ifctx->gotrootpath = 0;
 1320 
 1321         bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
 1322 
 1323         /* bootpc part */
 1324         ifctx->call.op = BOOTP_REQUEST;         /* BOOTREQUEST */
 1325         ifctx->call.htype = 1;                  /* 10mb ethernet */
 1326         ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
 1327         ifctx->call.hops = 0;
 1328         if (bootpc_ifctx_isunresolved(ifctx) != 0)
 1329                 ifctx->xid++;
 1330         ifctx->call.xid = txdr_unsigned(ifctx->xid);
 1331         bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
 1332 
 1333         vendp = ifctx->call.vend;
 1334         *vendp++ = 99;          /* RFC1048 cookie */
 1335         *vendp++ = 130;
 1336         *vendp++ = 83;
 1337         *vendp++ = 99;
 1338         *vendp++ = TAG_MAXMSGSIZE;
 1339         *vendp++ = 2;
 1340         *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
 1341         *vendp++ = sizeof(struct bootp_packet) & 255;
 1342 
 1343         snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
 1344                 ostype, MACHINE, osrelease);
 1345         vendor_client_len = strlen(vendor_client);
 1346         *vendp++ = TAG_VENDOR_INDENTIFIER;
 1347         *vendp++ = vendor_client_len;
 1348         memcpy(vendp, vendor_client, vendor_client_len);
 1349         vendp += vendor_client_len;;
 1350         ifctx->dhcpquerytype = DHCP_NOMSG;
 1351         switch (ifctx->state) {
 1352         case IF_DHCP_UNRESOLVED:
 1353                 *vendp++ = TAG_DHCP_MSGTYPE;
 1354                 *vendp++ = 1;
 1355                 *vendp++ = DHCP_DISCOVER;
 1356                 ifctx->dhcpquerytype = DHCP_DISCOVER;
 1357                 ifctx->gotdhcpserver = 0;
 1358                 break;
 1359         case IF_DHCP_OFFERED:
 1360                 *vendp++ = TAG_DHCP_MSGTYPE;
 1361                 *vendp++ = 1;
 1362                 *vendp++ = DHCP_REQUEST;
 1363                 ifctx->dhcpquerytype = DHCP_REQUEST;
 1364                 *vendp++ = TAG_DHCP_REQ_ADDR;
 1365                 *vendp++ = 4;
 1366                 memcpy(vendp, &ifctx->reply.yiaddr, 4);
 1367                 vendp += 4;
 1368                 if (ifctx->gotdhcpserver != 0) {
 1369                         *vendp++ = TAG_DHCP_SERVERID;
 1370                         *vendp++ = 4;
 1371                         memcpy(vendp, &ifctx->dhcpserver, 4);
 1372                         vendp += 4;
 1373                 }
 1374                 *vendp++ = TAG_DHCP_LEASETIME;
 1375                 *vendp++ = 4;
 1376                 leasetime = htonl(300);
 1377                 memcpy(vendp, &leasetime, 4);
 1378                 vendp += 4;
 1379         default:
 1380                 ;
 1381         }
 1382         *vendp = TAG_END;
 1383 
 1384         ifctx->call.secs = 0;
 1385         ifctx->call.flags = htons(0x8000); /* We need an broadcast answer */
 1386 }
 1387 
 1388 static int
 1389 bootpc_hascookie(struct bootp_packet *bp)
 1390 {
 1391 
 1392         return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
 1393                 bp->vend[2] == 83 && bp->vend[3] == 99);
 1394 }
 1395 
 1396 static void
 1397 bootpc_tag_helper(struct bootpc_tagcontext *tctx,
 1398     unsigned char *start, int len, int tag)
 1399 {
 1400         unsigned char *j;
 1401         unsigned char *ej;
 1402         unsigned char code;
 1403 
 1404         if (tctx->badtag != 0 || tctx->badopt != 0)
 1405                 return;
 1406 
 1407         j = start;
 1408         ej = j + len;
 1409 
 1410         while (j < ej) {
 1411                 code = *j++;
 1412                 if (code == TAG_PAD)
 1413                         continue;
 1414                 if (code == TAG_END)
 1415                         return;
 1416                 if (j >= ej || j + *j + 1 > ej) {
 1417                         tctx->badopt = 1;
 1418                         return;
 1419                 }
 1420                 len = *j++;
 1421                 if (code == tag) {
 1422                         if (tctx->taglen + len > TAG_MAXLEN) {
 1423                                 tctx->badtag = 1;
 1424                                 return;
 1425                         }
 1426                         tctx->foundopt = 1;
 1427                         if (len > 0)
 1428                                 memcpy(tctx->buf + tctx->taglen,
 1429                                        j, len);
 1430                         tctx->taglen += len;
 1431                 }
 1432                 if (code == TAG_OVERLOAD)
 1433                         tctx->overload = *j;
 1434 
 1435                 j += len;
 1436         }
 1437 }
 1438 
 1439 static unsigned char *
 1440 bootpc_tag(struct bootpc_tagcontext *tctx,
 1441     struct bootp_packet *bp, int len, int tag)
 1442 {
 1443         unsigned char *j;
 1444         unsigned char *ej;
 1445 
 1446         tctx->overload = 0;
 1447         tctx->badopt = 0;
 1448         tctx->badtag = 0;
 1449         tctx->foundopt = 0;
 1450         tctx->taglen = 0;
 1451 
 1452         if (bootpc_hascookie(bp) == 0)
 1453                 return NULL;
 1454 
 1455         j = &bp->vend[4];
 1456         ej = (unsigned char *) bp + len;
 1457 
 1458         bootpc_tag_helper(tctx, &bp->vend[4],
 1459                           (unsigned char *) bp + len - &bp->vend[4], tag);
 1460 
 1461         if ((tctx->overload & OVERLOAD_FILE) != 0)
 1462                 bootpc_tag_helper(tctx,
 1463                                   (unsigned char *) bp->file,
 1464                                   sizeof(bp->file),
 1465                                   tag);
 1466         if ((tctx->overload & OVERLOAD_SNAME) != 0)
 1467                 bootpc_tag_helper(tctx,
 1468                                   (unsigned char *) bp->sname,
 1469                                   sizeof(bp->sname),
 1470                                   tag);
 1471 
 1472         if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
 1473                 return NULL;
 1474         tctx->buf[tctx->taglen] = '\0';
 1475         return tctx->buf;
 1476 }
 1477 
 1478 static void
 1479 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
 1480     struct bootpc_globalcontext *gctx)
 1481 {
 1482         char *p;
 1483         unsigned int ip;
 1484 
 1485         ifctx->gotgw = 0;
 1486         ifctx->gotnetmask = 0;
 1487 
 1488         clear_sinaddr(&ifctx->myaddr);
 1489         clear_sinaddr(&ifctx->netmask);
 1490         clear_sinaddr(&ifctx->gw);
 1491 
 1492         ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
 1493 
 1494         ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
 1495         snprintf(gctx->lookup_path, sizeof(gctx->lookup_path),
 1496                  "swap.%d.%d.%d.%d",
 1497                  ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
 1498 
 1499         printf("%s at ", ifctx->ireq.ifr_name);
 1500         print_sin_addr(&ifctx->myaddr);
 1501         printf(" server ");
 1502         print_in_addr(ifctx->reply.siaddr);
 1503 
 1504         ifctx->gw.sin_addr = ifctx->reply.giaddr;
 1505         if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
 1506                 printf(" via gateway ");
 1507                 print_in_addr(ifctx->reply.giaddr);
 1508         }
 1509 
 1510         /* This call used for the side effect (overload flag) */
 1511         (void) bootpc_tag(&gctx->tmptag,
 1512                           &ifctx->reply, ifctx->replylen, TAG_END);
 1513 
 1514         if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
 1515                 if (ifctx->reply.sname[0] != '\0')
 1516                         printf(" server name %s", ifctx->reply.sname);
 1517         if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
 1518                 if (ifctx->reply.file[0] != '\0')
 1519                         printf(" boot file %s", ifctx->reply.file);
 1520 
 1521         printf("\n");
 1522 
 1523         p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
 1524                        TAG_SUBNETMASK);
 1525         if (p != NULL) {
 1526                 if (gctx->tag.taglen != 4)
 1527                         panic("bootpc: subnet mask len is %d",
 1528                               gctx->tag.taglen);
 1529                 bcopy(p, &ifctx->netmask.sin_addr, 4);
 1530                 ifctx->gotnetmask = 1;
 1531                 printf("subnet mask ");
 1532                 print_sin_addr(&ifctx->netmask);
 1533                 printf(" ");
 1534         }
 1535 
 1536         p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
 1537                        TAG_ROUTERS);
 1538         if (p != NULL) {
 1539                 /* Routers */
 1540                 if (gctx->tag.taglen % 4)
 1541                         panic("bootpc: Router Len is %d", gctx->tag.taglen);
 1542                 if (gctx->tag.taglen > 0) {
 1543                         bcopy(p, &ifctx->gw.sin_addr, 4);
 1544                         printf("router ");
 1545                         print_sin_addr(&ifctx->gw);
 1546                         printf(" ");
 1547                         ifctx->gotgw = 1;
 1548                         gctx->gotgw = 1;
 1549                 }
 1550         }
 1551 
 1552         p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
 1553                        TAG_ROOT);
 1554         if (p != NULL) {
 1555                 if (gctx->setrootfs != NULL) {
 1556                         printf("rootfs %s (ignored) ", p);
 1557                 } else  if (setfs(&nd->root_saddr,
 1558                                   nd->root_hostnam, p)) {
 1559                         printf("rootfs %s ", p);
 1560                         gctx->gotrootpath = 1;
 1561                         ifctx->gotrootpath = 1;
 1562                         gctx->setrootfs = ifctx;
 1563 
 1564                         p = bootpc_tag(&gctx->tag, &ifctx->reply,
 1565                                        ifctx->replylen,
 1566                                        TAG_ROOTOPTS);
 1567                         if (p != NULL) {
 1568                                 mountopts(&nd->root_args, p);
 1569                                 printf("rootopts %s ", p);
 1570                         }
 1571                 } else
 1572                         panic("Failed to set rootfs to %s", p);
 1573         }
 1574 
 1575         p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
 1576                        TAG_SWAP);
 1577         if (p != NULL) {
 1578                 if (gctx->setswapfs != NULL) {
 1579                         printf("swapfs %s (ignored) ", p);
 1580                 } else  if (setfs(&nd->swap_saddr,
 1581                                   nd->swap_hostnam, p)) {
 1582                         gctx->gotswappath = 1;
 1583                         gctx->setswapfs = ifctx;
 1584                         printf("swapfs %s ", p);
 1585 
 1586                         p = bootpc_tag(&gctx->tag, &ifctx->reply,
 1587                                        ifctx->replylen,
 1588                                        TAG_SWAPOPTS);
 1589                         if (p != NULL) {
 1590                                 /* swap mount options */
 1591                                 mountopts(&nd->swap_args, p);
 1592                                 printf("swapopts %s ", p);
 1593                         }
 1594 
 1595                         p = bootpc_tag(&gctx->tag, &ifctx->reply,
 1596                                        ifctx->replylen,
 1597                                        TAG_SWAPSIZE);
 1598                         if (p != NULL) {
 1599                                 int swaplen;
 1600                                 if (gctx->tag.taglen != 4)
 1601                                         panic("bootpc: "
 1602                                               "Expected 4 bytes for swaplen, "
 1603                                               "not %d bytes",
 1604                                               gctx->tag.taglen);
 1605                                 bcopy(p, &swaplen, 4);
 1606                                 nd->swap_nblks = ntohl(swaplen);
 1607                                 printf("swapsize %d KB ",
 1608                                        nd->swap_nblks);
 1609                         }
 1610                 } else
 1611                         panic("Failed to set swapfs to %s", p);
 1612         }
 1613 
 1614         p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
 1615                        TAG_HOSTNAME);
 1616         if (p != NULL) {
 1617                 if (gctx->tag.taglen >= MAXHOSTNAMELEN)
 1618                         panic("bootpc: hostname >= %d bytes",
 1619                               MAXHOSTNAMELEN);
 1620                 if (gctx->sethostname != NULL) {
 1621                         printf("hostname %s (ignored) ", p);
 1622                 } else {
 1623                         strcpy(nd->my_hostnam, p);
 1624                         strcpy(hostname, p);
 1625                         printf("hostname %s ", hostname);
 1626                         gctx->sethostname = ifctx;
 1627                 }
 1628         }
 1629         p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
 1630                         TAG_COOKIE);
 1631         if (p != NULL) {        /* store in a sysctl variable */
 1632                 int i, l = sizeof(bootp_cookie) - 1;
 1633                 for (i = 0; i < l && p[i] != '\0'; i++)
 1634                         bootp_cookie[i] = p[i];
 1635                 p[i] = '\0';
 1636         }
 1637 
 1638 
 1639         printf("\n");
 1640 
 1641         if (ifctx->gotnetmask == 0) {
 1642                 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
 1643                         ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
 1644                 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
 1645                         ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
 1646                 else
 1647                         ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
 1648         }
 1649         if (ifctx->gotgw == 0) {
 1650                 /* Use proxyarp */
 1651                 ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
 1652         }
 1653 }
 1654 
 1655 void
 1656 bootpc_init(void)
 1657 {
 1658         struct bootpc_ifcontext *ifctx, *nctx;  /* Interface BOOTP contexts */
 1659         struct bootpc_globalcontext *gctx;      /* Global BOOTP context */
 1660         struct ifnet *ifp;
 1661         int error;
 1662         struct nfsv3_diskless *nd;
 1663         struct thread *td;
 1664 
 1665         nd = &nfsv3_diskless;
 1666         td = curthread;
 1667 
 1668         /*
 1669          * If already filled in, don't touch it here
 1670          */
 1671         if (nfs_diskless_valid != 0)
 1672                 return;
 1673 
 1674         gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK);
 1675         if (gctx == NULL)
 1676                 panic("Failed to allocate bootp global context structure");
 1677 
 1678         bzero(gctx, sizeof(*gctx));
 1679         gctx->xid = ~0xFFFF;
 1680         gctx->starttime = time_second;
 1681 
 1682         ifctx = allocifctx(gctx);
 1683 
 1684         /*
 1685          * Find a network interface.
 1686          */
 1687 #ifdef BOOTP_WIRED_TO
 1688         printf("bootpc_init: wired to interface '%s'\n",
 1689                __XSTRING(BOOTP_WIRED_TO));
 1690 #endif
 1691         bzero(&ifctx->ireq, sizeof(ifctx->ireq));
 1692         for (ifp = TAILQ_FIRST(&ifnet);
 1693              ifp != NULL;
 1694              ifp = TAILQ_NEXT(ifp, if_link)) {
 1695                 snprintf(ifctx->ireq.ifr_name, sizeof(ifctx->ireq.ifr_name),
 1696                          "%s%d", ifp->if_name, ifp->if_unit);
 1697 #ifdef BOOTP_WIRED_TO
 1698                 if (strcmp(ifctx->ireq.ifr_name,
 1699                            __XSTRING(BOOTP_WIRED_TO)) != 0)
 1700                         continue;
 1701 #else
 1702                 if ((ifp->if_flags &
 1703                      (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
 1704                     IFF_BROADCAST)
 1705                         continue;
 1706 #endif
 1707                 if (gctx->interfaces != NULL)
 1708                         gctx->lastinterface->next = ifctx;
 1709                 else
 1710                         gctx->interfaces = ifctx;
 1711                 ifctx->ifp = ifp;
 1712                 gctx->lastinterface = ifctx;
 1713                 ifctx = allocifctx(gctx);
 1714         }
 1715         free(ifctx, M_TEMP);
 1716 
 1717         if (gctx->interfaces == NULL) {
 1718 #ifdef BOOTP_WIRED_TO
 1719                 panic("bootpc_init: Could not find interface specified "
 1720                       "by BOOTP_WIRED_TO: "
 1721                       __XSTRING(BOOTP_WIRED_TO));
 1722 #else
 1723                 panic("bootpc_init: no suitable interface");
 1724 #endif
 1725         }
 1726 
 1727         gctx->gotrootpath = 0;
 1728         gctx->gotswappath = 0;
 1729         gctx->gotgw = 0;
 1730 
 1731         for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
 1732                 bootpc_fakeup_interface(ifctx, gctx, td);
 1733 
 1734         for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
 1735                 bootpc_compose_query(ifctx, gctx, td);
 1736 
 1737         ifctx = gctx->interfaces;
 1738         error = bootpc_call(gctx, td);
 1739 
 1740         if (error != 0) {
 1741 #ifdef BOOTP_NFSROOT
 1742                 panic("BOOTP call failed");
 1743 #else
 1744                 printf("BOOTP call failed\n");
 1745 #endif
 1746         }
 1747 
 1748         mountopts(&nd->root_args, NULL);
 1749 
 1750         mountopts(&nd->swap_args, NULL);
 1751 
 1752         for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
 1753                 if (bootpc_ifctx_isresolved(ifctx) != 0)
 1754                         bootpc_decode_reply(nd, ifctx, gctx);
 1755 
 1756         if (gctx->gotswappath == 0)
 1757                 nd->swap_nblks = 0;
 1758 #ifdef BOOTP_NFSROOT
 1759         if (gctx->gotrootpath == 0)
 1760                 panic("bootpc: No root path offered");
 1761 #endif
 1762 
 1763         for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
 1764                 bootpc_adjust_interface(ifctx, gctx, td);
 1765 
 1766                 soclose(ifctx->so);
 1767         }
 1768 
 1769         for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
 1770                 if (ifctx->gotrootpath != 0)
 1771                         break;
 1772         if (ifctx == NULL) {
 1773                 for (ifctx = gctx->interfaces;
 1774                      ifctx != NULL;
 1775                      ifctx = ifctx->next)
 1776                         if (bootpc_ifctx_isresolved(ifctx) != 0)
 1777                                 break;
 1778         }
 1779         if (ifctx == NULL)
 1780                 goto out;
 1781 
 1782         if (gctx->gotrootpath != 0) {
 1783 
 1784                 error = md_mount(&nd->root_saddr, nd->root_hostnam,
 1785                                  nd->root_fh, &nd->root_fhsize,
 1786                                  &nd->root_args, td);
 1787                 if (error != 0)
 1788                         panic("nfs_boot: mountd root, error=%d", error);
 1789 
 1790                 if (gctx->gotswappath != 0) {
 1791 
 1792                         error = md_mount(&nd->swap_saddr,
 1793                                          nd->swap_hostnam,
 1794                                          nd->swap_fh, &nd->swap_fhsize,
 1795                                          &nd->swap_args, td);
 1796                         if (error != 0)
 1797                                 panic("nfs_boot: mountd swap, error=%d",
 1798                                       error);
 1799 
 1800                         error = md_lookup_swap(&nd->swap_saddr,
 1801                                                gctx->lookup_path,
 1802                                                nd->swap_fh, &nd->swap_fhsize,
 1803                                                &nd->swap_args, td);
 1804                         if (error != 0)
 1805                                 panic("nfs_boot: lookup swap, error=%d",
 1806                                       error);
 1807                 }
 1808                 nfs_diskless_valid = 3;
 1809         }
 1810 
 1811         strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
 1812         bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
 1813         bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
 1814         ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
 1815                 ifctx->myaddr.sin_addr.s_addr |
 1816                 ~ ifctx->netmask.sin_addr.s_addr;
 1817         bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
 1818 
 1819 out:
 1820         for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
 1821                 nctx = ifctx->next;
 1822                 free(ifctx, M_TEMP);
 1823         }
 1824         free(gctx, M_TEMP);
 1825 }
 1826 
 1827 /*
 1828  * RPC: mountd/mount
 1829  * Given a server pathname, get an NFS file handle.
 1830  * Also, sets sin->sin_port to the NFS service port.
 1831  */
 1832 static int
 1833 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
 1834     struct nfs_args *args, struct thread *td)
 1835 {
 1836         struct mbuf *m;
 1837         int error;
 1838         int authunixok;
 1839         int authcount;
 1840         int authver;
 1841 
 1842 #ifdef BOOTP_NFSV3
 1843         /* First try NFS v3 */
 1844         /* Get port number for MOUNTD. */
 1845         error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
 1846                              &mdsin->sin_port, td);
 1847         if (error == 0) {
 1848                 m = xdr_string_encode(path, strlen(path));
 1849 
 1850                 /* Do RPC to mountd. */
 1851                 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
 1852                                   RPCMNT_MOUNT, &m, NULL, td);
 1853         }
 1854         if (error == 0) {
 1855                 args->flags |= NFSMNT_NFSV3;
 1856         } else {
 1857 #endif
 1858                 /* Fallback to NFS v2 */
 1859 
 1860                 /* Get port number for MOUNTD. */
 1861                 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
 1862                                      &mdsin->sin_port, td);
 1863                 if (error != 0)
 1864                         return error;
 1865 
 1866                 m = xdr_string_encode(path, strlen(path));
 1867 
 1868                 /* Do RPC to mountd. */
 1869                 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
 1870                                   RPCMNT_MOUNT, &m, NULL, td);
 1871                 if (error != 0)
 1872                         return error;   /* message already freed */
 1873 
 1874 #ifdef BOOTP_NFSV3
 1875         }
 1876 #endif
 1877 
 1878         if (xdr_int_decode(&m, &error) != 0 || error != 0)
 1879                 goto bad;
 1880 
 1881         if ((args->flags & NFSMNT_NFSV3) != 0) {
 1882                 if (xdr_int_decode(&m, fhsizep) != 0 ||
 1883                     *fhsizep > NFSX_V3FHMAX ||
 1884                     *fhsizep <= 0)
 1885                         goto bad;
 1886         } else
 1887                 *fhsizep = NFSX_V2FH;
 1888 
 1889         if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
 1890                 goto bad;
 1891 
 1892         if (args->flags & NFSMNT_NFSV3) {
 1893                 if (xdr_int_decode(&m, &authcount) != 0)
 1894                         goto bad;
 1895                 authunixok = 0;
 1896                 if (authcount < 0 || authcount > 100)
 1897                         goto bad;
 1898                 while (authcount > 0) {
 1899                         if (xdr_int_decode(&m, &authver) != 0)
 1900                                 goto bad;
 1901                         if (authver == RPCAUTH_UNIX)
 1902                                 authunixok = 1;
 1903                         authcount--;
 1904                 }
 1905                 if (authunixok == 0)
 1906                         goto bad;
 1907         }
 1908 
 1909         /* Set port number for NFS use. */
 1910         error = krpc_portmap(mdsin, NFS_PROG,
 1911                              (args->flags &
 1912                               NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
 1913                              &mdsin->sin_port, td);
 1914 
 1915         goto out;
 1916 
 1917 bad:
 1918         error = EBADRPC;
 1919 
 1920 out:
 1921         m_freem(m);
 1922         return error;
 1923 }
 1924 
 1925 static int
 1926 md_lookup_swap(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
 1927     struct nfs_args *args, struct thread *td)
 1928 {
 1929         struct mbuf *m;
 1930         int error;
 1931         int size = -1;
 1932         int attribs_present;
 1933         int status;
 1934         union {
 1935                 u_int32_t v2[17];
 1936                 u_int32_t v3[21];
 1937         } fattribs;
 1938 
 1939         m = m_get(M_TRYWAIT, MT_DATA);
 1940         if (m == NULL)
 1941                 return ENOBUFS;
 1942 
 1943         if ((args->flags & NFSMNT_NFSV3) != 0) {
 1944                 *mtod(m, u_int32_t *) = txdr_unsigned(*fhsizep);
 1945                 bcopy(fhp, mtod(m, u_char *) + sizeof(u_int32_t), *fhsizep);
 1946                 m->m_len = *fhsizep + sizeof(u_int32_t);
 1947         } else {
 1948                 bcopy(fhp, mtod(m, u_char *), NFSX_V2FH);
 1949                 m->m_len = NFSX_V2FH;
 1950         }
 1951 
 1952         m->m_next = xdr_string_encode(path, strlen(path));
 1953         if (m->m_next == NULL) {
 1954                 error = ENOBUFS;
 1955                 goto out;
 1956         }
 1957 
 1958         /* Do RPC to nfsd. */
 1959         if ((args->flags & NFSMNT_NFSV3) != 0)
 1960                 error = krpc_call(mdsin, NFS_PROG, NFS_VER3,
 1961                                   NFSPROC_LOOKUP, &m, NULL, td);
 1962         else
 1963                 error = krpc_call(mdsin, NFS_PROG, NFS_VER2,
 1964                                   NFSV2PROC_LOOKUP, &m, NULL, td);
 1965         if (error != 0)
 1966                 return error;   /* message already freed */
 1967 
 1968         if (xdr_int_decode(&m, &status) != 0)
 1969                 goto bad;
 1970         if (status != 0) {
 1971                 error = ENOENT;
 1972                 goto out;
 1973         }
 1974 
 1975         if ((args->flags & NFSMNT_NFSV3) != 0) {
 1976                 if (xdr_int_decode(&m, fhsizep) != 0 ||
 1977                     *fhsizep > NFSX_V3FHMAX ||
 1978                     *fhsizep <= 0)
 1979                         goto bad;
 1980         } else
 1981                 *fhsizep = NFSX_V2FH;
 1982 
 1983         if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
 1984                 goto bad;
 1985 
 1986         if ((args->flags & NFSMNT_NFSV3) != 0) {
 1987                 if (xdr_int_decode(&m, &attribs_present) != 0)
 1988                         goto bad;
 1989                 if (attribs_present != 0) {
 1990                         if (xdr_opaque_decode(&m, (u_char *) &fattribs.v3,
 1991                                               sizeof(u_int32_t) * 21) != 0)
 1992                                 goto bad;
 1993                         size = fxdr_unsigned(u_int32_t, fattribs.v3[6]);
 1994                 }
 1995         } else {
 1996                 if (xdr_opaque_decode(&m,(u_char *) &fattribs.v2,
 1997                                       sizeof(u_int32_t) * 17) != 0)
 1998                         goto bad;
 1999                 size = fxdr_unsigned(u_int32_t, fattribs.v2[5]);
 2000         }
 2001 
 2002         if (nfsv3_diskless.swap_nblks == 0 && size != -1) {
 2003                 nfsv3_diskless.swap_nblks = size / 1024;
 2004                 printf("md_lookup_swap: Swap size is %d KB\n",
 2005                        nfsv3_diskless.swap_nblks);
 2006         }
 2007 
 2008         goto out;
 2009 
 2010 bad:
 2011         error = EBADRPC;
 2012 
 2013 out:
 2014         m_freem(m);
 2015         return error;
 2016 }

Cache object: a7ac2876ea15e98216aff6643d32d7ab


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