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

Cache object: c49673b6d780bb6ed15cf8ccb38f1f55


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