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/nfs/bootp_subr.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: 7a68c6d6897688cec2b433e5706b0d76


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