source: rtems/c/src/libnetworking/nfs/bootp_subr.c @ ff0f694d

4.104.114.84.95
Last change on this file since ff0f694d was ff0f694d, checked in by Joel Sherrill <joel.sherrill@…>, on 08/20/98 at 21:47:37

Fixed many warnings.

  • Property mode set to 100644
File size: 23.8 KB
Line 
1/*      $Id$    */
2
3/*
4 * Copyright (c) 1995 Gordon Ross, Adam Glass
5 * Copyright (c) 1992 Regents of the University of California.
6 * All rights reserved.
7 *
8 * This software was developed by the Computer Systems Engineering group
9 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10 * contributed to Berkeley.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 *    must display the following acknowledgement:
22 *      This product includes software developed by the University of
23 *      California, Lawrence Berkeley Laboratory and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 *    may be used to endorse or promote products derived from this software
26 *    without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * based on:
41 *      nfs/krpc_subr.c
42 *      $NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $
43 */
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/conf.h>
49#include <sys/sockio.h>
50#include <sys/proc.h>
51#include <sys/mount.h>
52#include <sys/mbuf.h>
53#include <sys/reboot.h>
54#include <sys/socket.h>
55#include <sys/socketvar.h>
56
57#include <net/if.h>
58#include <net/route.h>
59
60#include <netinet/in.h>
61#include <net/if_types.h>
62#include <net/if_dl.h>
63#include <netinet/if_ether.h>
64
65#include <nfs/rpcv2.h>
66#include <nfs/nfsproto.h>
67#include <nfs/nfs.h>
68#include <nfs/nfsdiskless.h>
69#include <nfs/krpc.h>
70#include <nfs/xdr_subs.h>
71
72#define BOOTP_MIN_LEN           300     /* Minimum size of bootp udp packet */
73
74/*
75 * What is the longest we will wait before re-sending a request?
76 * Note this is also the frequency of "RPC timeout" messages.
77 * The re-send loop count sup linearly to this maximum, so the
78 * first complaint will happen after (1+2+3+4+5)=15 seconds.
79 */
80#define MAX_RESEND_DELAY 5      /* seconds */
81
82/* Definitions from RFC951 */
83struct bootp_packet {
84  u_int8_t op;
85  u_int8_t htype;
86  u_int8_t hlen;
87  u_int8_t hops;
88  u_int32_t xid;
89  u_int16_t secs;
90  u_int16_t flags;
91  struct in_addr ciaddr;
92  struct in_addr yiaddr;
93  struct in_addr siaddr;
94  struct in_addr giaddr;
95  unsigned char chaddr[16];
96  char sname[64];
97  char file[128];
98  unsigned char vend[256];
99};
100
101#define IPPORT_BOOTPC 68
102#define IPPORT_BOOTPS 67
103
104extern int nfs_diskless_valid;
105extern struct nfsv3_diskless nfsv3_diskless;
106
107/* mountd RPC */
108#if !defined(__rtems__)
109static int md_mount __P((struct sockaddr_in *mdsin, char *path,
110        u_char *fhp, int *fhsizep, struct nfs_args *args,struct proc *procp));
111static int md_lookup_swap __P((struct sockaddr_in *mdsin,char *path,
112                               u_char *fhp, int *fhsizep,
113                               struct nfs_args *args,
114                               struct proc *procp));
115static int setfs __P((struct sockaddr_in *addr, char *path, char *p));
116#endif
117static int getdec __P((char **ptr));
118static char *substr __P((char *a,char *b));
119#if !defined(__rtems__)
120static void mountopts __P((struct nfs_args *args, char *p));
121static int xdr_opaque_decode __P((struct mbuf **ptr,u_char *buf,
122                                  int len));
123static int xdr_int_decode __P((struct mbuf **ptr,int *iptr));
124#endif
125static void printip __P((char *prefix,struct in_addr addr));
126
127#ifdef BOOTP_DEBUG
128void bootpboot_p_sa(struct sockaddr *sa,struct sockaddr *ma);
129void bootpboot_p_ma(struct sockaddr *ma);
130void bootpboot_p_rtentry(struct rtentry *rt);
131void bootpboot_p_tree(struct radix_node *rn);
132void bootpboot_p_rtlist(void);
133void bootpboot_p_iflist(void);
134#endif
135
136int  bootpc_call(struct bootp_packet *call,
137                 struct bootp_packet *reply,
138                 struct proc *procp);
139
140int bootpc_fakeup_interface(struct ifreq *ireq,struct socket *so,
141                        struct proc *procp);
142
143int
144bootpc_adjust_interface(struct ifreq *ireq,struct socket *so,
145                        struct sockaddr_in *myaddr,
146                        struct sockaddr_in *netmask,
147                        struct sockaddr_in *gw,
148                        struct proc *procp);
149
150void bootpc_init(void);
151
152#ifdef BOOTP_DEBUG
153void bootpboot_p_sa(sa,ma)
154     struct sockaddr *sa;
155     struct sockaddr *ma;
156{
157  if (!sa) {
158    printf("(sockaddr *) <null>");
159    return;
160  }
161  switch (sa->sa_family) {
162  case AF_INET:
163    {
164      struct sockaddr_in *sin = (struct sockaddr_in *) sa;
165      printf("inet %x",ntohl(sin->sin_addr.s_addr));
166      if (ma) {
167        struct sockaddr_in *sin = (struct sockaddr_in *) ma;
168        printf(" mask %x",ntohl(sin->sin_addr.s_addr));
169      }
170    }
171  break;
172  case AF_LINK:
173    {
174      struct sockaddr_dl *sli = (struct sockaddr_dl *) sa;
175      int i;
176      printf("link %.*s ",sli->sdl_nlen,sli->sdl_data);
177      for (i=0;i<sli->sdl_alen;i++) {
178        if (i>0)
179          printf(":");
180        printf("%x",(unsigned char) sli->sdl_data[i+sli->sdl_nlen]);
181      }
182    }
183  break;
184  default:
185    printf("af%d",sa->sa_family);
186  }
187}
188
189void bootpboot_p_ma(ma)
190     struct sockaddr *ma;
191{
192  if (!ma) {
193    printf("<null>");
194    return;
195  }
196  printf("%x",*(int*)ma);
197}
198
199void bootpboot_p_rtentry(rt)
200     struct rtentry *rt;
201{
202  bootpboot_p_sa(rt_key(rt),rt_mask(rt));
203  printf(" ");
204  bootpboot_p_ma(rt->rt_genmask);
205  printf(" ");
206  bootpboot_p_sa(rt->rt_gateway,NULL);
207  printf(" ");
208  printf("flags %x",(unsigned short) rt->rt_flags);
209  printf(" %d",rt->rt_rmx.rmx_expire);
210  printf(" %s%d\n",rt->rt_ifp->if_name,rt->rt_ifp->if_unit);
211}
212void  bootpboot_p_tree(rn)
213     struct radix_node *rn;
214{
215  while (rn) {
216    if (rn->rn_b < 0) {
217      if (rn->rn_flags & RNF_ROOT) {
218      } else {
219        bootpboot_p_rtentry((struct rtentry *) rn);
220      }
221      rn = rn->rn_dupedkey;
222    } else {
223      bootpboot_p_tree(rn->rn_l);
224      bootpboot_p_tree(rn->rn_r);
225      return;
226    }
227   
228  }
229}
230
231void bootpboot_p_rtlist(void)
232{
233  printf("Routing table:\n");
234  bootpboot_p_tree(rt_tables[AF_INET]->rnh_treetop);
235}
236
237void bootpboot_p_iflist(void)
238{
239  struct ifnet *ifp;
240  struct ifaddr *ifa;
241  printf("Interface list:\n");
242  for (ifp = TAILQ_FIRST(&ifnet); ifp != 0; ifp = TAILQ_NEXT(ifp,if_link))
243    {
244      for (ifa = TAILQ_FIRST(&ifp->if_addrhead) ;ifa;
245           ifa=TAILQ_NEXT(ifa,ifa_link))
246        if (ifa->ifa_addr->sa_family == AF_INET ) {
247          printf("%s%d flags %x, addr %x, bcast %x, net %x\n",
248                 ifp->if_name,ifp->if_unit,
249                 (unsigned short) ifp->if_flags,
250                 ntohl(((struct sockaddr_in *) ifa->ifa_addr)->sin_addr.s_addr),
251                 ntohl(((struct sockaddr_in *) ifa->ifa_dstaddr)->sin_addr.s_addr),
252                 ntohl(((struct sockaddr_in *) ifa->ifa_netmask)->sin_addr.s_addr)
253                 );
254        }
255    }
256}
257#endif
258
259int
260bootpc_call(call,reply,procp)
261     struct bootp_packet *call;
262     struct bootp_packet *reply;        /* output */
263     struct proc *procp;
264{
265        struct socket *so;
266        struct sockaddr_in *sin;
267        struct mbuf *m, *nam;
268        struct uio auio;
269        struct iovec aio;
270        int error, rcvflg, timo, secs, len;
271
272        /* Free at end if not null. */
273        nam = NULL;
274
275        /*
276         * Create socket and set its recieve timeout.
277         */
278        if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0,procp)))
279                goto out;
280
281        m = m_get(M_WAIT, MT_SOOPTS);
282        if (m == NULL) {
283                error = ENOBUFS;
284                goto out;
285        } else {
286                struct timeval *tv;
287                tv = mtod(m, struct timeval *);
288                m->m_len = sizeof(*tv);
289                tv->tv_sec = 1;
290                tv->tv_usec = 0;
291                if ((error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m)))
292                        goto out;
293        }
294
295        /*
296         * Enable broadcast.
297         */
298        {
299                int *on;
300                m = m_get(M_WAIT, MT_SOOPTS);
301                if (m == NULL) {
302                        error = ENOBUFS;
303                        goto out;
304                }
305                on = mtod(m, int *);
306                m->m_len = sizeof(*on);
307                *on = 1;
308                if ((error = sosetopt(so, SOL_SOCKET, SO_BROADCAST, m)))
309                        goto out;
310        }
311
312        /*
313         * Bind the local endpoint to a bootp client port.
314         */
315        m = m_getclr(M_WAIT, MT_SONAME);
316        sin = mtod(m, struct sockaddr_in *);
317        sin->sin_len = m->m_len = sizeof(*sin);
318        sin->sin_family = AF_INET;
319        sin->sin_addr.s_addr = INADDR_ANY;
320        sin->sin_port = htons(IPPORT_BOOTPC);
321        error = sobind(so, m);
322        m_freem(m);
323        if (error) {
324                printf("bind failed\n");
325                goto out;
326        }
327
328        /*
329         * Setup socket address for the server.
330         */
331        nam = m_get(M_WAIT, MT_SONAME);
332        if (nam == NULL) {
333                error = ENOBUFS;
334                goto out;
335        }
336        sin = mtod(nam, struct sockaddr_in *);
337        sin-> sin_len = sizeof(*sin);
338        sin-> sin_family = AF_INET;
339        sin->sin_addr.s_addr = INADDR_BROADCAST;
340        sin->sin_port = htons(IPPORT_BOOTPS);
341
342        nam->m_len = sizeof(*sin);
343
344        /*
345         * Send it, repeatedly, until a reply is received,
346         * but delay each re-send by an increasing amount.
347         * If the delay hits the maximum, start complaining.
348         */
349        timo = 0;
350        for (;;) {
351                /* Send BOOTP request (or re-send). */
352               
353                aio.iov_base = (caddr_t) call;
354                aio.iov_len = sizeof(*call);
355               
356                auio.uio_iov = &aio;
357                auio.uio_iovcnt = 1;
358                auio.uio_segflg = UIO_SYSSPACE;
359                auio.uio_rw = UIO_WRITE;
360                auio.uio_offset = 0;
361                auio.uio_resid = sizeof(*call);
362                auio.uio_procp = procp;
363
364                error = sosend(so, nam, &auio, NULL, NULL, 0);
365                if (error) {
366                        printf("bootpc_call: sosend: %d\n", error);
367                        goto out;
368                }
369
370                /* Determine new timeout. */
371                if (timo < MAX_RESEND_DELAY)
372                        timo++;
373                else
374                        printf("BOOTP timeout for server 0x%x\n",
375                               (int)ntohl(sin->sin_addr.s_addr));
376
377                /*
378                 * Wait for up to timo seconds for a reply.
379                 * The socket receive timeout was set to 1 second.
380                 */
381                secs = timo;
382                while (secs > 0) {
383                        aio.iov_base = (caddr_t) reply;
384                        aio.iov_len = sizeof(*reply);
385
386                        auio.uio_iov = &aio;
387                        auio.uio_iovcnt = 1;
388                        auio.uio_segflg = UIO_SYSSPACE;
389                        auio.uio_rw = UIO_READ;
390                        auio.uio_offset = 0;
391                        auio.uio_resid = sizeof(*reply);
392                        auio.uio_procp = procp;
393                       
394                        rcvflg = 0;
395                        error = soreceive(so, NULL, &auio, NULL, NULL, &rcvflg);
396                        if (error == EWOULDBLOCK) {
397                                secs--;
398                                call->secs=htons(ntohs(call->secs)+1);
399                                continue;
400                        }
401                        if (error)
402                                goto out;
403                        len = sizeof(*reply) - auio.uio_resid;
404
405                        /* Do we have the required number of bytes ? */
406                        if (len < BOOTP_MIN_LEN)
407                                continue;
408
409                        /* Is it the right reply? */
410                        if (reply->op != 2)
411                          continue;
412
413                        if (reply->xid != call->xid)
414                                continue;
415
416                        if (reply->hlen != call->hlen)
417                          continue;
418
419                        if (bcmp(reply->chaddr,call->chaddr,call->hlen))
420                          continue;
421
422                        goto gotreply;  /* break two levels */
423
424                } /* while secs */
425        } /* forever send/receive */
426
427        error = ETIMEDOUT;
428        goto out;
429
430 gotreply:
431 out:
432        if (nam) m_freem(nam);
433        soclose(so);
434        return error;
435}
436
437int
438bootpc_fakeup_interface(struct ifreq *ireq,struct socket *so,
439                        struct proc *procp)
440{
441  struct sockaddr_in *sin;
442  int error;
443  struct sockaddr_in dst;
444  struct sockaddr_in gw;
445  struct sockaddr_in mask;
446
447  /*
448   * Bring up the interface.
449   *
450   * Get the old interface flags and or IFF_UP into them; if
451   * IFF_UP set blindly, interface selection can be clobbered.
452   */
453  error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, procp);
454  if (error)
455    panic("bootpc_fakeup_interface: GIFFLAGS, error=%d", error);
456  ireq->ifr_flags |= IFF_UP;
457  error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, procp);
458  if (error)
459    panic("bootpc_fakeup_interface: SIFFLAGS, error=%d", error);
460
461  /*
462   * Do enough of ifconfig(8) so that the chosen interface
463   * can talk to the servers.  (just set the address)
464   */
465 
466  /* addr is 0.0.0.0 */
467 
468  sin = (struct sockaddr_in *)&ireq->ifr_addr;
469  bzero((caddr_t)sin, sizeof(*sin));
470  sin->sin_len = sizeof(*sin);
471  sin->sin_family = AF_INET;
472  sin->sin_addr.s_addr = INADDR_ANY;
473  error = ifioctl(so, SIOCSIFADDR, (caddr_t)ireq, procp);
474  if (error)
475    panic("bootpc_fakeup_interface: set if addr, error=%d", error);
476 
477  /* netmask is 0.0.0.0 */
478 
479  sin = (struct sockaddr_in *)&ireq->ifr_addr;
480  bzero((caddr_t)sin, sizeof(*sin));
481  sin->sin_len = sizeof(*sin);
482  sin->sin_family = AF_INET;
483  sin->sin_addr.s_addr = INADDR_ANY;
484  error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, procp);
485  if (error)
486    panic("bootpc_fakeup_interface: set if net addr, error=%d", error);
487 
488  /* Broadcast is 255.255.255.255 */
489 
490  sin = (struct sockaddr_in *)&ireq->ifr_addr;
491  bzero((caddr_t)sin, sizeof(*sin));
492  sin->sin_len = sizeof(*sin);
493  sin->sin_family = AF_INET;
494  sin->sin_addr.s_addr = INADDR_BROADCAST;
495  error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t)ireq, procp);
496  if (error)
497    panic("bootpc_fakeup_interface: set if broadcast addr, error=%d", error);
498 
499  /* Add default route to 0.0.0.0 so we can send data */
500 
501  bzero((caddr_t) &dst, sizeof(dst));
502  dst.sin_len=sizeof(dst);
503  dst.sin_family=AF_INET;
504  dst.sin_addr.s_addr = htonl(0);
505 
506  bzero((caddr_t) &gw, sizeof(gw));
507  gw.sin_len=sizeof(gw);
508  gw.sin_family=AF_INET;
509  gw.sin_addr.s_addr = htonl(0x0);
510 
511  bzero((caddr_t) &mask, sizeof(mask));
512  mask.sin_len=sizeof(mask);
513  mask.sin_family=AF_INET;
514  mask.sin_addr.s_addr = htonl(0);
515 
516  error = rtrequest(RTM_ADD,
517                    (struct sockaddr *) &dst,
518                    (struct sockaddr *) &gw,
519                    (struct sockaddr *) &mask,
520                    RTF_UP | RTF_STATIC
521                    , NULL);
522  if (error)
523    printf("bootpc_fakeup_interface: add default route, error=%d\n", error);
524  return error;
525}
526
527int
528bootpc_adjust_interface(struct ifreq *ireq,struct socket *so,
529                        struct sockaddr_in *myaddr,
530                        struct sockaddr_in *netmask,
531                        struct sockaddr_in *gw,
532                        struct proc *procp)
533{
534  int error;
535  struct sockaddr_in oldgw;
536  struct sockaddr_in olddst;
537  struct sockaddr_in oldmask;
538  struct sockaddr_in *sin;
539
540  /* Remove old default route to 0.0.0.0 */
541 
542  bzero((caddr_t) &olddst, sizeof(olddst));
543  olddst.sin_len=sizeof(olddst);
544  olddst.sin_family=AF_INET;
545  olddst.sin_addr.s_addr = INADDR_ANY;
546 
547  bzero((caddr_t) &oldgw, sizeof(oldgw));
548  oldgw.sin_len=sizeof(oldgw);
549  oldgw.sin_family=AF_INET;
550  oldgw.sin_addr.s_addr = INADDR_ANY;
551 
552  bzero((caddr_t) &oldmask, sizeof(oldmask));
553  oldmask.sin_len=sizeof(oldmask);
554  oldmask.sin_family=AF_INET;
555  oldmask.sin_addr.s_addr = INADDR_ANY;
556 
557  error = rtrequest(RTM_DELETE,
558                    (struct sockaddr *) &olddst,
559                    (struct sockaddr *) &oldgw,
560                    (struct sockaddr *) &oldmask,
561                    (RTF_UP | RTF_STATIC), NULL);
562  if (error) {
563    printf("nfs_boot: del default route, error=%d\n", error);
564    return error;
565  }
566
567  /*
568   * Do enough of ifconfig(8) so that the chosen interface
569   * can talk to the servers.  (just set the address)
570   */
571  bcopy(netmask,&ireq->ifr_addr,sizeof(*netmask));
572  error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, procp);
573  if (error)
574    panic("nfs_boot: set if netmask, error=%d", error);
575
576  /* Broadcast is with host part of IP address all 1's */
577 
578  sin = (struct sockaddr_in *)&ireq->ifr_addr;
579  bzero((caddr_t)sin, sizeof(*sin));
580  sin->sin_len = sizeof(*sin);
581  sin->sin_family = AF_INET;
582  sin->sin_addr.s_addr = myaddr->sin_addr.s_addr | ~ netmask->sin_addr.s_addr;
583  error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t)ireq, procp);
584  if (error)
585    panic("bootpc_call: set if broadcast addr, error=%d", error);
586 
587  bcopy(myaddr,&ireq->ifr_addr,sizeof(*myaddr));
588  error = ifioctl(so, SIOCSIFADDR, (caddr_t)ireq, procp);
589  if (error)
590    panic("nfs_boot: set if addr, error=%d", error);
591
592  /* Add new default route */
593
594  error = rtrequest(RTM_ADD,
595                    (struct sockaddr *) &olddst,
596                    (struct sockaddr *) gw,
597                    (struct sockaddr *) &oldmask,
598                    (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
599  if (error) {
600    printf("nfs_boot: add net route, error=%d\n", error);
601    return error;
602  }
603
604  return 0;
605}
606
607#if !defined(__rtems__)
608static int setfs(addr, path, p)
609        struct sockaddr_in *addr;
610        char *path;
611        char *p;
612{
613        unsigned ip = 0;
614        int val;
615
616        if (((val = getdec(&p)) < 0) || (val > 255)) return(0);
617        ip = val << 24;
618        if (*p != '.') return(0);
619        p++;
620        if (((val = getdec(&p)) < 0) || (val > 255)) return(0);
621        ip |= (val << 16);
622        if (*p != '.') return(0);
623        p++;
624        if (((val = getdec(&p)) < 0) || (val > 255)) return(0);
625        ip |= (val << 8);
626        if (*p != '.') return(0);
627        p++;
628        if (((val = getdec(&p)) < 0) || (val > 255)) return(0);
629        ip |= val;
630        if (*p != ':') return(0);
631        p++;
632
633        addr->sin_addr.s_addr = htonl(ip);
634        addr->sin_len = sizeof(struct sockaddr_in);
635        addr->sin_family = AF_INET;
636
637        strncpy(path,p,MNAMELEN-1);
638        return(1);
639}
640#endif
641
642static int getdec(ptr)
643        char **ptr;
644{
645        char *p = *ptr;
646        int ret=0;
647        if ((*p < '0') || (*p > '9')) return(-1);
648        while ((*p >= '0') && (*p <= '9')) {
649                ret = ret*10 + (*p - '0');
650                p++;
651        }
652        *ptr = p;
653        return(ret);
654}
655
656static char *substr(a,b)
657        char *a,*b;
658{
659        char *loc1;
660        char *loc2;
661
662        while (*a != '\0') {
663                loc1 = a;
664                loc2 = b;
665                while (*loc1 == *loc2++) {
666                        if (*loc1 == '\0') return (0);
667                        loc1++;
668                        if (*loc2 == '\0') return (loc1);
669                }
670        a++;
671        }
672        return (0);
673}
674
675static void printip(char *prefix,struct in_addr addr)
676{
677  unsigned int ip;
678
679  ip = ntohl(addr.s_addr);
680
681  printf("%s is %d.%d.%d.%d\n",prefix,
682         ip >> 24, (ip >> 16) & 255 ,(ip >> 8) & 255 ,ip & 255 );
683}
684
685void
686bootpc_init(void)
687{
688  struct bootp_packet call;
689  struct bootp_packet reply;
690  static u_int32_t xid = ~0xFF;
691 
692  struct ifreq ireq;
693  struct ifnet *ifp;
694  struct socket *so;
695  int error;
696  int code,ncode,len;
697  int j;
698  char *p;
699  unsigned int ip;
700
701  struct sockaddr_in myaddr;
702  struct sockaddr_in netmask;
703  struct sockaddr_in gw;
704  int gotgw=0;
705  int gotnetmask=0;
706#if !defined(__rtems__)
707  int gotrootpath=0;
708  int gotswappath=0;
709#endif
710  char lookup_path[24];
711
712#define EALEN 6
713#if !defined(__rtems__)
714  unsigned char ea[EALEN];
715#endif
716  struct ifaddr *ifa;
717  struct sockaddr_dl *sdl = NULL;
718  char *delim;
719  struct proc *procp = NULL;
720
721  /*
722   * If already filled in, don't touch it here
723   */
724  if (nfs_diskless_valid)
725    return;
726
727  /*
728   * Bump time if 0.
729  if (!time.tv_sec)
730    time.tv_sec++;
731   */
732
733  /*
734   * Find a network interface.
735   */
736  for (ifp = ifnet; ifp != 0; ifp = ifp->if_next)
737    if ((ifp->if_flags &
738      (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
739        break;
740  if (ifp == NULL)
741    panic("bootpc_init: no suitable interface");
742  bzero(&ireq,sizeof(ireq));
743  sprintf(ireq.ifr_name, "%s%d", ifp->if_name,ifp->if_unit);
744  printf("bootpc_init: using network interface '%s'\n",
745         ireq.ifr_name);
746
747  if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0,procp)) != 0)
748    panic("nfs_boot: socreate, error=%d", error);
749         
750  bootpc_fakeup_interface(&ireq,so,procp);
751
752  printf("Bootpc testing starting\n");
753 
754  /* Get HW address */
755
756  for (ifa = ifp->if_addrlist;ifa; ifa = ifa->ifa_next)
757    if (ifa->ifa_addr->sa_family == AF_LINK &&
758        (sdl = ((struct sockaddr_dl *) ifa->ifa_addr)) &&
759        sdl->sdl_type == IFT_ETHER)
760      break;
761 
762  if (!sdl)
763    panic("bootpc: Unable to find HW address");
764  if (sdl->sdl_alen != EALEN )
765    panic("bootpc: HW address len is %d, expected value is %d",
766          sdl->sdl_alen,EALEN);
767
768  printf("bootpc hw address is ");
769  delim="";
770  for (j=0;j<sdl->sdl_alen;j++) {
771    printf("%s%x",delim,((unsigned char *)LLADDR(sdl))[j]);
772    delim=":";
773  }
774  printf("\n");
775
776#if 0
777  bootpboot_p_iflist();
778  bootpboot_p_rtlist();
779#endif
780 
781  bzero((caddr_t) &call, sizeof(call));
782
783  /* bootpc part */
784  call.op = 1;                  /* BOOTREQUEST */
785  call.htype= 1;                /* 10mb ethernet */
786  call.hlen=sdl->sdl_alen;      /* Hardware address length */
787  call.hops=0; 
788  xid++;
789  call.xid = txdr_unsigned(xid);
790  bcopy(LLADDR(sdl),&call.chaddr,sdl->sdl_alen);
791 
792  call.vend[0]=99;
793  call.vend[1]=130;
794  call.vend[2]=83;
795  call.vend[3]=99;
796  call.vend[4]=255;
797 
798  call.secs = 0;
799  call.flags = htons(0x8000); /* We need an broadcast answer */
800 
801  error = bootpc_call(&call,&reply,procp);
802 
803  if (error)
804    panic("BOOTP call failed -- error %d", error);
805 
806  bzero(&myaddr,sizeof(myaddr));
807  bzero(&netmask,sizeof(netmask));
808  bzero(&gw,sizeof(gw));
809
810  myaddr.sin_len = sizeof(myaddr);
811  myaddr.sin_family = AF_INET;
812
813  netmask.sin_len = sizeof(netmask);
814  netmask.sin_family = AF_INET;
815
816  gw.sin_len = sizeof(gw);
817  gw.sin_family= AF_INET;
818
819  rtems_bsdnet_bootp_server_address = reply.siaddr;
820  rtems_bsdnet_log_host_address = reply.siaddr;
821
822  myaddr.sin_addr = reply.yiaddr;
823
824  ip = ntohl(myaddr.sin_addr.s_addr);
825  sprintf(lookup_path,"swap.%d.%d.%d.%d",
826          ip >> 24, (ip >> 16) & 255 ,(ip >> 8) & 255 ,ip & 255 );
827
828  printip("My ip address",myaddr.sin_addr);
829
830  printip("Server ip address",reply.siaddr);
831
832  gw.sin_addr = reply.giaddr;
833  printip("Gateway ip address",reply.giaddr);
834
835  if (reply.sname[0])
836    printf("Server name is %s\n",reply.sname);
837  if (reply.file[0])
838    printf("boot file is %s\n",reply.file);
839  rtems_bsdnet_bootp_boot_file_name = strdup (reply.file);
840  if (reply.vend[0]==99 && reply.vend[1]==130 &&
841      reply.vend[2]==83 && reply.vend[3]==99) {
842    j=4;
843    ncode = reply.vend[j];
844    while (j<sizeof(reply.vend)) {
845      code = reply.vend[j] = ncode;
846      if (code==255)
847        break;
848      if (code==0) {
849        j++;
850        continue;
851      }
852      len = reply.vend[j+1];
853      j+=2;
854      if (len+j>=sizeof(reply.vend)) {
855        printf("Truncated field");
856        break;
857      }
858      ncode = reply.vend[j+len];
859      reply.vend[j+len]='\0';
860      p = &reply.vend[j];
861      switch (code) {
862      case 1:
863        if (len!=4)
864          panic("bootpc: subnet mask len is %d",len);
865        bcopy(&reply.vend[j],&netmask.sin_addr,4);
866        gotnetmask=1;
867        printip("Subnet mask",netmask.sin_addr);
868        break;
869      case 6:
870        /* Domain Name servers */
871        if (len % 4)
872          panic("bootpc: DNS Len is %d",len);
873        {
874        int dlen = 0;
875        while ((dlen < len) &&
876          (rtems_bsdnet_nameserver_count < sizeof rtems_bsdnet_config.name_server /
877                                  sizeof rtems_bsdnet_config.name_server[0])) {
878            bcopy(&reply.vend[j+dlen],
879                        &rtems_bsdnet_nameserver[rtems_bsdnet_nameserver_count],
880                        4);
881            printip("Domain Name Server",
882                        rtems_bsdnet_nameserver[rtems_bsdnet_nameserver_count]);
883            rtems_bsdnet_nameserver_count++;
884            dlen += 4;
885          }
886        }
887        break;
888      case 16:  /* Swap server IP address. unused */
889      case 2:
890        /* Time offset */
891        break;
892      case 3:
893        /* Routers */
894        if (len % 4)
895          panic("bootpc: Router Len is %d",len);
896        if (len > 0) {
897          bcopy(&reply.vend[j],&gw.sin_addr,4);
898          printip("Router",gw.sin_addr);
899          gotgw=1;
900        }
901        break;
902      case 7:
903        /* Log servers */
904        if (len % 4)
905          panic("bootpc: Log server len is %d",len);
906        if (len > 0) {
907          bcopy(&reply.vend[j],&rtems_bsdnet_log_host_address,4);
908          printip("Log server",rtems_bsdnet_log_host_address);
909        }
910        break;
911      case 12:
912        if (len>=MAXHOSTNAMELEN)
913          panic("bootpc: hostname  >=%d bytes",MAXHOSTNAMELEN);
914        if (sethostname (&reply.vend[j], len) < 0)
915          panic("Can't set host name");
916        printf("Hostname is %.*s\n",len,&reply.vend[j]);
917        break;
918      case 15:
919        /* Domain name */
920        rtems_bsdnet_domain_name = strdup (&reply.vend[j]);
921        if (rtems_bsdnet_domain_name)
922                printf("Domain name is %s\n", rtems_bsdnet_domain_name);
923        break;
924      default:
925        printf("Ignoring field type %d\n",code);
926      }
927      j+=len;
928    }
929  }
930
931  if (!gotnetmask) {
932    if (IN_CLASSA(ntohl(myaddr.sin_addr.s_addr)))
933      netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
934    else if (IN_CLASSB(ntohl(myaddr.sin_addr.s_addr)))
935      netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
936    else
937      netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
938  }
939  if (!gotgw) {
940    /* Use proxyarp */
941    gw.sin_addr.s_addr = myaddr.sin_addr.s_addr;
942  }
943 
944#if 0
945  bootpboot_p_iflist();
946  bootpboot_p_rtlist();
947#endif
948  error = bootpc_adjust_interface(&ireq,so,
949                                  &myaddr,&netmask,&gw,procp);
950 
951  soclose(so);
952
953#if 0
954  bootpboot_p_iflist();
955  bootpboot_p_rtlist();
956#endif
957
958
959#if 0
960    myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
961#endif
962
963#if 0
964  bootpboot_p_iflist();
965  bootpboot_p_rtlist();
966#endif
967  return;
968}
Note: See TracBrowser for help on using the repository browser.