2005-01-07 01:45:51 +00:00
|
|
|
/*-
|
1997-05-11 18:05:39 +00:00
|
|
|
* Copyright (c) 1995 Gordon Ross, Adam Glass
|
|
|
|
* Copyright (c) 1992 Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by the Computer Systems Engineering group
|
|
|
|
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
|
|
|
|
* contributed to Berkeley.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Lawrence Berkeley Laboratory and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* based on:
|
|
|
|
* nfs/krpc_subr.c
|
|
|
|
* $NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $
|
|
|
|
*/
|
|
|
|
|
2001-09-18 23:32:09 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2004-07-08 22:35:36 +00:00
|
|
|
#include "opt_bootp.h"
|
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2009-05-29 21:27:12 +00:00
|
|
|
#include <sys/jail.h>
|
1997-05-11 18:05:39 +00:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/sockio.h>
|
2000-10-29 01:19:32 +00:00
|
|
|
#include <sys/malloc.h>
|
1997-05-11 18:05:39 +00:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/mbuf.h>
|
2001-12-31 20:05:26 +00:00
|
|
|
#include <sys/proc.h>
|
1997-05-11 18:05:39 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
2002-03-13 09:23:11 +00:00
|
|
|
#include <sys/sysctl.h>
|
1998-03-28 10:33:27 +00:00
|
|
|
#include <sys/uio.h>
|
1997-05-11 18:05:39 +00:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/if_types.h>
|
|
|
|
#include <net/if_dl.h>
|
2008-12-02 21:37:28 +00:00
|
|
|
#include <net/vnet.h>
|
1997-05-11 18:05:39 +00:00
|
|
|
|
|
|
|
#include <nfs/nfsproto.h>
|
2001-09-18 23:32:09 +00:00
|
|
|
#include <nfsclient/nfs.h>
|
|
|
|
#include <nfsclient/nfsdiskless.h>
|
|
|
|
#include <nfsclient/krpc.h>
|
1997-05-11 18:05:39 +00:00
|
|
|
#include <nfs/xdr_subs.h>
|
|
|
|
|
|
|
|
|
1998-01-18 18:46:20 +00:00
|
|
|
#define BOOTP_MIN_LEN 300 /* Minimum size of bootp udp packet */
|
1997-05-11 18:05:39 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
#ifndef BOOTP_SETTLE_DELAY
|
|
|
|
#define BOOTP_SETTLE_DELAY 3
|
|
|
|
#endif
|
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
/*
|
|
|
|
* What is the longest we will wait before re-sending a request?
|
|
|
|
* Note this is also the frequency of "RPC timeout" messages.
|
|
|
|
* The re-send loop count sup linearly to this maximum, so the
|
|
|
|
* first complaint will happen after (1+2+3+4+5)=15 seconds.
|
|
|
|
*/
|
|
|
|
#define MAX_RESEND_DELAY 5 /* seconds */
|
|
|
|
|
|
|
|
/* Definitions from RFC951 */
|
|
|
|
struct bootp_packet {
|
2000-10-24 22:40:18 +00:00
|
|
|
u_int8_t op;
|
|
|
|
u_int8_t htype;
|
|
|
|
u_int8_t hlen;
|
|
|
|
u_int8_t hops;
|
|
|
|
u_int32_t xid;
|
|
|
|
u_int16_t secs;
|
|
|
|
u_int16_t flags;
|
|
|
|
struct in_addr ciaddr;
|
|
|
|
struct in_addr yiaddr;
|
|
|
|
struct in_addr siaddr;
|
|
|
|
struct in_addr giaddr;
|
|
|
|
unsigned char chaddr[16];
|
|
|
|
char sname[64];
|
|
|
|
char file[128];
|
2001-12-30 02:35:09 +00:00
|
|
|
unsigned char vend[1222];
|
1997-05-11 18:05:39 +00:00
|
|
|
};
|
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
struct bootpc_ifcontext {
|
|
|
|
struct bootpc_ifcontext *next;
|
|
|
|
struct bootp_packet call;
|
|
|
|
struct bootp_packet reply;
|
|
|
|
int replylen;
|
|
|
|
int overload;
|
|
|
|
struct socket *so;
|
|
|
|
struct ifreq ireq;
|
|
|
|
struct ifnet *ifp;
|
|
|
|
struct sockaddr_dl *sdl;
|
|
|
|
struct sockaddr_in myaddr;
|
|
|
|
struct sockaddr_in netmask;
|
|
|
|
struct sockaddr_in gw;
|
|
|
|
struct sockaddr_in broadcast; /* Different for each interface */
|
|
|
|
int gotgw;
|
|
|
|
int gotnetmask;
|
|
|
|
int gotrootpath;
|
|
|
|
int outstanding;
|
|
|
|
int sentmsg;
|
|
|
|
u_int32_t xid;
|
|
|
|
enum {
|
|
|
|
IF_BOOTP_UNRESOLVED,
|
|
|
|
IF_BOOTP_RESOLVED,
|
2001-02-02 02:35:40 +00:00
|
|
|
IF_BOOTP_FAILED,
|
|
|
|
IF_DHCP_UNRESOLVED,
|
2000-10-29 01:19:32 +00:00
|
|
|
IF_DHCP_OFFERED,
|
|
|
|
IF_DHCP_RESOLVED,
|
2001-02-02 02:35:40 +00:00
|
|
|
IF_DHCP_FAILED,
|
2000-10-29 01:19:32 +00:00
|
|
|
} state;
|
2001-02-02 02:35:40 +00:00
|
|
|
int dhcpquerytype; /* dhcp type sent */
|
|
|
|
struct in_addr dhcpserver;
|
|
|
|
int gotdhcpserver;
|
2000-10-29 01:19:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define TAG_MAXLEN 1024
|
|
|
|
struct bootpc_tagcontext {
|
|
|
|
char buf[TAG_MAXLEN + 1];
|
|
|
|
int overload;
|
|
|
|
int badopt;
|
|
|
|
int badtag;
|
|
|
|
int foundopt;
|
|
|
|
int taglen;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct bootpc_globalcontext {
|
|
|
|
struct bootpc_ifcontext *interfaces;
|
|
|
|
struct bootpc_ifcontext *lastinterface;
|
|
|
|
u_int32_t xid;
|
|
|
|
int gotrootpath;
|
|
|
|
int gotgw;
|
|
|
|
int ifnum;
|
|
|
|
int secs;
|
|
|
|
int starttime;
|
|
|
|
struct bootp_packet reply;
|
|
|
|
int replylen;
|
|
|
|
struct bootpc_ifcontext *setrootfs;
|
|
|
|
struct bootpc_ifcontext *sethostname;
|
|
|
|
struct bootpc_tagcontext tmptag;
|
|
|
|
struct bootpc_tagcontext tag;
|
|
|
|
};
|
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
#define IPPORT_BOOTPC 68
|
|
|
|
#define IPPORT_BOOTPS 67
|
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
#define BOOTP_REQUEST 1
|
|
|
|
#define BOOTP_REPLY 2
|
|
|
|
|
|
|
|
/* Common tags */
|
|
|
|
#define TAG_PAD 0 /* Pad option, implicit length 1 */
|
|
|
|
#define TAG_SUBNETMASK 1 /* RFC 950 subnet mask */
|
|
|
|
#define TAG_ROUTERS 3 /* Routers (in order of preference) */
|
|
|
|
#define TAG_HOSTNAME 12 /* Client host name */
|
|
|
|
#define TAG_ROOT 17 /* Root path */
|
|
|
|
|
|
|
|
/* DHCP specific tags */
|
|
|
|
#define TAG_OVERLOAD 52 /* Option Overload */
|
|
|
|
#define TAG_MAXMSGSIZE 57 /* Maximum DHCP Message Size */
|
|
|
|
|
|
|
|
#define TAG_END 255 /* End Option (i.e. no more options) */
|
|
|
|
|
|
|
|
/* Overload values */
|
|
|
|
#define OVERLOAD_FILE 1
|
|
|
|
#define OVERLOAD_SNAME 2
|
|
|
|
|
|
|
|
/* Site specific tags: */
|
|
|
|
#define TAG_ROOTOPTS 130
|
2002-03-13 09:23:11 +00:00
|
|
|
#define TAG_COOKIE 134 /* ascii info for userland, via sysctl */
|
2000-10-29 01:19:32 +00:00
|
|
|
|
2001-02-02 02:35:40 +00:00
|
|
|
#define TAG_DHCP_MSGTYPE 53
|
|
|
|
#define TAG_DHCP_REQ_ADDR 50
|
|
|
|
#define TAG_DHCP_SERVERID 54
|
|
|
|
#define TAG_DHCP_LEASETIME 51
|
|
|
|
|
2002-05-17 20:18:48 +00:00
|
|
|
#define TAG_VENDOR_INDENTIFIER 60
|
|
|
|
|
2001-02-02 02:35:40 +00:00
|
|
|
#define DHCP_NOMSG 0
|
|
|
|
#define DHCP_DISCOVER 1
|
|
|
|
#define DHCP_OFFER 2
|
|
|
|
#define DHCP_REQUEST 3
|
|
|
|
#define DHCP_ACK 5
|
|
|
|
|
2008-05-16 06:27:03 +00:00
|
|
|
/* NFS read/write block size */
|
|
|
|
#ifndef BOOTP_BLOCKSIZE
|
|
|
|
#define BOOTP_BLOCKSIZE 8192
|
|
|
|
#endif
|
|
|
|
|
2002-03-13 09:23:11 +00:00
|
|
|
static char bootp_cookie[128];
|
|
|
|
SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
|
|
|
|
bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
|
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
/* mountd RPC */
|
2001-09-18 23:32:09 +00:00
|
|
|
static int md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp,
|
|
|
|
int *fhsizep, struct nfs_args *args, struct thread *td);
|
2004-03-12 20:37:40 +00:00
|
|
|
static int setfs(struct sockaddr_in *addr, char *path, char *p,
|
|
|
|
const struct in_addr *siaddr);
|
2001-09-18 23:32:09 +00:00
|
|
|
static int getdec(char **ptr);
|
2004-03-12 20:37:40 +00:00
|
|
|
static int getip(char **ptr, struct in_addr *ip);
|
2001-09-18 23:32:09 +00:00
|
|
|
static void mountopts(struct nfs_args *args, char *p);
|
|
|
|
static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
|
|
|
|
static int xdr_int_decode(struct mbuf **ptr, int *iptr);
|
|
|
|
static void print_in_addr(struct in_addr addr);
|
|
|
|
static void print_sin_addr(struct sockaddr_in *addr);
|
|
|
|
static void clear_sinaddr(struct sockaddr_in *sin);
|
2003-08-07 21:27:17 +00:00
|
|
|
static void allocifctx(struct bootpc_globalcontext *gctx);
|
2001-09-18 23:32:09 +00:00
|
|
|
static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
|
|
|
|
struct bootpc_globalcontext *gctx, struct thread *td);
|
2000-10-29 01:19:32 +00:00
|
|
|
static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
|
2001-09-18 23:32:09 +00:00
|
|
|
struct bootp_packet *bp, int len, int tag);
|
2000-10-29 01:19:32 +00:00
|
|
|
static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
|
2001-09-18 23:32:09 +00:00
|
|
|
unsigned char *start, int len, int tag);
|
1997-05-11 18:05:39 +00:00
|
|
|
|
|
|
|
#ifdef BOOTP_DEBUG
|
2001-09-18 23:32:09 +00:00
|
|
|
void bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma);
|
1997-05-11 18:05:39 +00:00
|
|
|
void bootpboot_p_rtentry(struct rtentry *rt);
|
|
|
|
void bootpboot_p_tree(struct radix_node *rn);
|
|
|
|
void bootpboot_p_rtlist(void);
|
2000-10-29 01:19:32 +00:00
|
|
|
void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
|
1997-05-11 18:05:39 +00:00
|
|
|
void bootpboot_p_iflist(void);
|
|
|
|
#endif
|
|
|
|
|
2001-09-18 23:32:09 +00:00
|
|
|
static int bootpc_call(struct bootpc_globalcontext *gctx,
|
|
|
|
struct thread *td);
|
1997-05-11 18:05:39 +00:00
|
|
|
|
2001-09-18 23:32:09 +00:00
|
|
|
static int bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
|
|
|
|
struct bootpc_globalcontext *gctx, struct thread *td);
|
1997-05-11 18:05:39 +00:00
|
|
|
|
2001-09-18 23:32:09 +00:00
|
|
|
static int bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
|
|
|
|
struct bootpc_globalcontext *gctx, struct thread *td);
|
1997-05-11 18:05:39 +00:00
|
|
|
|
2001-09-18 23:32:09 +00:00
|
|
|
static void bootpc_decode_reply(struct nfsv3_diskless *nd,
|
|
|
|
struct bootpc_ifcontext *ifctx,
|
|
|
|
struct bootpc_globalcontext *gctx);
|
2000-10-29 01:19:32 +00:00
|
|
|
|
2001-09-18 23:32:09 +00:00
|
|
|
static int bootpc_received(struct bootpc_globalcontext *gctx,
|
|
|
|
struct bootpc_ifcontext *ifctx);
|
2000-10-29 01:19:32 +00:00
|
|
|
|
2001-02-02 02:35:40 +00:00
|
|
|
static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
|
|
|
|
static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
|
|
|
|
static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
|
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
/*
|
|
|
|
* In order to have multiple active interfaces with address 0.0.0.0
|
|
|
|
* and be able to send data to a selected interface, we perform
|
|
|
|
* some tricks:
|
2001-09-18 23:32:09 +00:00
|
|
|
*
|
2000-10-29 01:19:32 +00:00
|
|
|
* - The 'broadcast' address is different for each interface.
|
|
|
|
*
|
|
|
|
* - We temporarily add routing pointing 255.255.255.255 to the
|
|
|
|
* selected interface broadcast address, thus the packet sent
|
|
|
|
* goes to that interface.
|
|
|
|
*/
|
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
#ifdef BOOTP_DEBUG
|
2000-10-24 22:40:18 +00:00
|
|
|
void
|
2001-09-18 23:32:09 +00:00
|
|
|
bootpboot_p_sa(struct sockaddr *sa, struct sockaddr *ma)
|
1997-05-11 18:05:39 +00:00
|
|
|
{
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
if (sa == NULL) {
|
|
|
|
printf("(sockaddr *) <null>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (sa->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
struct sockaddr_in *sin;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
sin = (struct sockaddr_in *) sa;
|
2000-10-29 01:19:32 +00:00
|
|
|
printf("inet ");
|
|
|
|
print_sin_addr(sin);
|
2000-10-24 22:40:18 +00:00
|
|
|
if (ma != NULL) {
|
|
|
|
sin = (struct sockaddr_in *) ma;
|
2000-10-29 01:19:32 +00:00
|
|
|
printf(" mask ");
|
|
|
|
print_sin_addr(sin);
|
2000-10-24 22:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AF_LINK:
|
|
|
|
{
|
|
|
|
struct sockaddr_dl *sli;
|
|
|
|
int i;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
sli = (struct sockaddr_dl *) sa;
|
|
|
|
printf("link %.*s ", sli->sdl_nlen, sli->sdl_data);
|
|
|
|
for (i = 0; i < sli->sdl_alen; i++) {
|
|
|
|
if (i > 0)
|
|
|
|
printf(":");
|
|
|
|
printf("%x", ((unsigned char *) LLADDR(sli))[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("af%d", sa->sa_family);
|
|
|
|
}
|
1997-05-11 18:05:39 +00:00
|
|
|
}
|
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
void
|
|
|
|
bootpboot_p_rtentry(struct rtentry *rt)
|
1997-05-11 18:05:39 +00:00
|
|
|
{
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
bootpboot_p_sa(rt_key(rt), rt_mask(rt));
|
|
|
|
printf(" ");
|
|
|
|
bootpboot_p_sa(rt->rt_gateway, NULL);
|
|
|
|
printf(" ");
|
|
|
|
printf("flags %x", (unsigned short) rt->rt_flags);
|
|
|
|
printf(" %d", (int) rt->rt_rmx.rmx_expire);
|
2003-10-31 18:32:15 +00:00
|
|
|
printf(" %s\n", rt->rt_ifp->if_xname);
|
1997-05-11 18:05:39 +00:00
|
|
|
}
|
2000-10-24 22:40:18 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
bootpboot_p_tree(struct radix_node *rn)
|
1997-05-11 18:05:39 +00:00
|
|
|
{
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
while (rn != NULL) {
|
2000-10-24 23:33:01 +00:00
|
|
|
if (rn->rn_bit < 0) {
|
2000-10-24 22:40:18 +00:00
|
|
|
if ((rn->rn_flags & RNF_ROOT) != 0) {
|
|
|
|
} else {
|
|
|
|
bootpboot_p_rtentry((struct rtentry *) rn);
|
|
|
|
}
|
|
|
|
rn = rn->rn_dupedkey;
|
|
|
|
} else {
|
2000-10-24 23:33:01 +00:00
|
|
|
bootpboot_p_tree(rn->rn_left);
|
|
|
|
bootpboot_p_tree(rn->rn_right);
|
2000-10-24 22:40:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1997-05-11 18:05:39 +00:00
|
|
|
}
|
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
void
|
|
|
|
bootpboot_p_rtlist(void)
|
1997-05-11 18:05:39 +00:00
|
|
|
{
|
2009-06-01 15:49:42 +00:00
|
|
|
struct radix_node_head *rnh;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
printf("Routing table:\n");
|
2009-06-01 15:49:42 +00:00
|
|
|
rnh = rt_tables_get_rnh(0, AF_INET);
|
|
|
|
if (rnh == NULL)
|
|
|
|
return;
|
|
|
|
RADIX_NODE_HEAD_RLOCK(rnh); /* could sleep XXX */
|
|
|
|
bootpboot_p_tree(rnh->rnh_treetop);
|
|
|
|
RADIX_NODE_HEAD_RUNLOCK(rnh);
|
1997-05-11 18:05:39 +00:00
|
|
|
}
|
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
void
|
|
|
|
bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
|
|
|
|
{
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2003-10-31 18:32:15 +00:00
|
|
|
printf("%s flags %x, addr ",
|
|
|
|
ifp->if_xname, ifp->if_flags);
|
2000-10-29 01:19:32 +00:00
|
|
|
print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
|
|
|
|
printf(", broadcast ");
|
|
|
|
print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
|
|
|
|
printf(", netmask ");
|
|
|
|
print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
void
|
|
|
|
bootpboot_p_iflist(void)
|
1997-05-11 18:05:39 +00:00
|
|
|
{
|
2000-10-24 22:40:18 +00:00
|
|
|
struct ifnet *ifp;
|
|
|
|
struct ifaddr *ifa;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
printf("Interface list:\n");
|
2009-08-23 20:40:19 +00:00
|
|
|
IFNET_RLOCK();
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
for (ifp = TAILQ_FIRST(&V_ifnet);
|
2000-10-24 22:40:18 +00:00
|
|
|
ifp != NULL;
|
2000-10-29 01:19:32 +00:00
|
|
|
ifp = TAILQ_NEXT(ifp, if_link)) {
|
2000-10-24 22:40:18 +00:00
|
|
|
for (ifa = TAILQ_FIRST(&ifp->if_addrhead);
|
|
|
|
ifa != NULL;
|
2000-10-29 01:19:32 +00:00
|
|
|
ifa = TAILQ_NEXT(ifa, ifa_link))
|
|
|
|
if (ifa->ifa_addr->sa_family == AF_INET)
|
|
|
|
bootpboot_p_if(ifp, ifa);
|
1997-05-11 18:05:39 +00:00
|
|
|
}
|
2002-12-22 05:35:03 +00:00
|
|
|
IFNET_RUNLOCK();
|
1997-05-11 18:05:39 +00:00
|
|
|
}
|
2000-10-29 01:19:32 +00:00
|
|
|
#endif /* defined(BOOTP_DEBUG) */
|
|
|
|
|
|
|
|
static void
|
|
|
|
clear_sinaddr(struct sockaddr_in *sin)
|
|
|
|
{
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
bzero(sin, sizeof(*sin));
|
|
|
|
sin->sin_len = sizeof(*sin);
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
|
|
|
|
sin->sin_port = 0;
|
|
|
|
}
|
|
|
|
|
2003-08-07 21:27:17 +00:00
|
|
|
static void
|
2000-10-29 01:19:32 +00:00
|
|
|
allocifctx(struct bootpc_globalcontext *gctx)
|
|
|
|
{
|
|
|
|
struct bootpc_ifcontext *ifctx;
|
|
|
|
ifctx = (struct bootpc_ifcontext *) malloc(sizeof(*ifctx),
|
2003-08-07 21:27:17 +00:00
|
|
|
M_TEMP, M_WAITOK | M_ZERO);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (ifctx == NULL)
|
|
|
|
panic("Failed to allocate bootp interface context structure");
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
ifctx->xid = gctx->xid;
|
2001-02-02 02:35:40 +00:00
|
|
|
#ifdef BOOTP_NO_DHCP
|
|
|
|
ifctx->state = IF_BOOTP_UNRESOLVED;
|
|
|
|
#else
|
|
|
|
ifctx->state = IF_DHCP_UNRESOLVED;
|
|
|
|
#endif
|
2000-10-29 01:19:32 +00:00
|
|
|
gctx->xid += 0x100;
|
2003-08-07 21:27:17 +00:00
|
|
|
if (gctx->interfaces != NULL)
|
|
|
|
gctx->lastinterface->next = ifctx;
|
|
|
|
else
|
|
|
|
gctx->interfaces = ifctx;
|
|
|
|
gctx->lastinterface = ifctx;
|
2000-10-29 01:19:32 +00:00
|
|
|
}
|
|
|
|
|
2001-02-02 02:35:40 +00:00
|
|
|
static __inline int
|
|
|
|
bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
|
|
|
|
{
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2001-02-02 02:35:40 +00:00
|
|
|
if (ifctx->state == IF_BOOTP_RESOLVED ||
|
|
|
|
ifctx->state == IF_DHCP_RESOLVED)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline int
|
|
|
|
bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
|
|
|
|
{
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2001-02-02 02:35:40 +00:00
|
|
|
if (ifctx->state == IF_BOOTP_UNRESOLVED ||
|
|
|
|
ifctx->state == IF_DHCP_UNRESOLVED)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline int
|
|
|
|
bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
|
|
|
|
{
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2001-02-02 02:35:40 +00:00
|
|
|
if (ifctx->state == IF_BOOTP_FAILED ||
|
|
|
|
ifctx->state == IF_DHCP_FAILED)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
static int
|
|
|
|
bootpc_received(struct bootpc_globalcontext *gctx,
|
2001-09-18 23:32:09 +00:00
|
|
|
struct bootpc_ifcontext *ifctx)
|
2000-10-29 01:19:32 +00:00
|
|
|
{
|
2001-02-02 02:35:40 +00:00
|
|
|
unsigned char dhcpreplytype;
|
|
|
|
char *p;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
/*
|
|
|
|
* Need timeout for fallback to less
|
|
|
|
* desirable alternative.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This call used for the side effect (badopt flag) */
|
|
|
|
(void) bootpc_tag(&gctx->tmptag, &gctx->reply,
|
|
|
|
gctx->replylen,
|
|
|
|
TAG_END);
|
|
|
|
|
|
|
|
/* If packet is invalid, ignore it */
|
|
|
|
if (gctx->tmptag.badopt != 0)
|
|
|
|
return 0;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2001-02-02 02:35:40 +00:00
|
|
|
p = bootpc_tag(&gctx->tmptag, &gctx->reply,
|
|
|
|
gctx->replylen, TAG_DHCP_MSGTYPE);
|
|
|
|
if (p != NULL)
|
|
|
|
dhcpreplytype = *p;
|
|
|
|
else
|
|
|
|
dhcpreplytype = DHCP_NOMSG;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2001-02-02 02:35:40 +00:00
|
|
|
switch (ifctx->dhcpquerytype) {
|
|
|
|
case DHCP_DISCOVER:
|
|
|
|
if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
|
|
|
|
#ifndef BOOTP_FORCE_DHCP
|
|
|
|
&& dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case DHCP_REQUEST:
|
|
|
|
if (dhcpreplytype != DHCP_ACK)
|
|
|
|
return 0;
|
|
|
|
case DHCP_NOMSG:
|
2002-05-24 05:50:28 +00:00
|
|
|
break;
|
2001-02-02 02:35:40 +00:00
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
/* Ignore packet unless it gives us a root tag we didn't have */
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2001-02-02 02:35:40 +00:00
|
|
|
if ((ifctx->state == IF_BOOTP_RESOLVED ||
|
|
|
|
(ifctx->dhcpquerytype == DHCP_DISCOVER &&
|
|
|
|
(ifctx->state == IF_DHCP_OFFERED ||
|
|
|
|
ifctx->state == IF_DHCP_RESOLVED))) &&
|
2000-10-29 01:19:32 +00:00
|
|
|
(bootpc_tag(&gctx->tmptag, &ifctx->reply,
|
|
|
|
ifctx->replylen,
|
|
|
|
TAG_ROOT) != NULL ||
|
|
|
|
bootpc_tag(&gctx->tmptag, &gctx->reply,
|
|
|
|
gctx->replylen,
|
|
|
|
TAG_ROOT) == NULL))
|
|
|
|
return 0;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
|
|
|
bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
|
2000-10-29 01:19:32 +00:00
|
|
|
ifctx->replylen = gctx->replylen;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
/* XXX: Only reset if 'perfect' response */
|
2001-02-02 02:35:40 +00:00
|
|
|
if (ifctx->state == IF_BOOTP_UNRESOLVED)
|
|
|
|
ifctx->state = IF_BOOTP_RESOLVED;
|
|
|
|
else if (ifctx->state == IF_DHCP_UNRESOLVED &&
|
|
|
|
ifctx->dhcpquerytype == DHCP_DISCOVER) {
|
|
|
|
if (dhcpreplytype == DHCP_OFFER)
|
|
|
|
ifctx->state = IF_DHCP_OFFERED;
|
|
|
|
else
|
|
|
|
ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
|
|
|
|
} else if (ifctx->state == IF_DHCP_OFFERED &&
|
|
|
|
ifctx->dhcpquerytype == DHCP_REQUEST)
|
|
|
|
ifctx->state = IF_DHCP_RESOLVED;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
|
|
|
|
2001-02-02 02:35:40 +00:00
|
|
|
if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
|
|
|
|
ifctx->state != IF_BOOTP_RESOLVED) {
|
|
|
|
p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
|
|
|
|
ifctx->replylen, TAG_DHCP_SERVERID);
|
|
|
|
if (p != NULL && gctx->tmptag.taglen == 4) {
|
|
|
|
memcpy(&ifctx->dhcpserver, p, 4);
|
|
|
|
ifctx->gotdhcpserver = 1;
|
|
|
|
} else
|
|
|
|
ifctx->gotdhcpserver = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
|
|
|
|
ifctx->replylen,
|
|
|
|
TAG_ROOT) != NULL);
|
|
|
|
ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
|
|
|
|
ifctx->replylen,
|
|
|
|
TAG_ROUTERS) != NULL);
|
|
|
|
ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
|
|
|
|
ifctx->replylen,
|
|
|
|
TAG_SUBNETMASK) != NULL);
|
|
|
|
return 1;
|
|
|
|
}
|
1997-05-11 18:05:39 +00:00
|
|
|
|
1998-02-09 06:11:36 +00:00
|
|
|
static int
|
2001-09-18 23:32:09 +00:00
|
|
|
bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
|
1997-05-11 18:05:39 +00:00
|
|
|
{
|
|
|
|
struct socket *so;
|
2000-10-29 01:19:32 +00:00
|
|
|
struct sockaddr_in *sin, dst;
|
1997-05-11 18:05:39 +00:00
|
|
|
struct uio auio;
|
1998-08-23 03:07:17 +00:00
|
|
|
struct sockopt sopt;
|
1997-05-11 18:05:39 +00:00
|
|
|
struct iovec aio;
|
2000-10-29 01:19:32 +00:00
|
|
|
int error, on, rcvflg, timo, len;
|
|
|
|
time_t atimo;
|
|
|
|
time_t rtimo;
|
1998-08-23 03:07:17 +00:00
|
|
|
struct timeval tv;
|
2000-10-29 01:19:32 +00:00
|
|
|
struct bootpc_ifcontext *ifctx;
|
|
|
|
int outstanding;
|
|
|
|
int gotrootpath;
|
2001-02-02 02:35:40 +00:00
|
|
|
int retry;
|
|
|
|
const char *s;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2009-12-13 11:06:39 +00:00
|
|
|
CURVNET_SET(TD_TO_VNET(td));
|
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
/*
|
|
|
|
* Create socket and set its recieve timeout.
|
|
|
|
*/
|
2002-02-27 18:32:23 +00:00
|
|
|
error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td->td_ucred, td);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error != 0)
|
2003-06-09 03:44:34 +00:00
|
|
|
goto out0;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
1998-08-23 03:07:17 +00:00
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
2000-10-29 01:19:32 +00:00
|
|
|
bzero(&sopt, sizeof(sopt));
|
2003-10-04 17:41:59 +00:00
|
|
|
sopt.sopt_dir = SOPT_SET;
|
1998-08-23 03:07:17 +00:00
|
|
|
sopt.sopt_level = SOL_SOCKET;
|
|
|
|
sopt.sopt_name = SO_RCVTIMEO;
|
|
|
|
sopt.sopt_val = &tv;
|
|
|
|
sopt.sopt_valsize = sizeof tv;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
error = sosetopt(so, &sopt);
|
|
|
|
if (error != 0)
|
1997-05-11 18:05:39 +00:00
|
|
|
goto out;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
/*
|
|
|
|
* Enable broadcast.
|
|
|
|
*/
|
1998-08-23 03:07:17 +00:00
|
|
|
on = 1;
|
2000-10-29 01:19:32 +00:00
|
|
|
sopt.sopt_name = SO_BROADCAST;
|
1998-08-23 03:07:17 +00:00
|
|
|
sopt.sopt_val = &on;
|
|
|
|
sopt.sopt_valsize = sizeof on;
|
2000-10-29 01:19:32 +00:00
|
|
|
|
|
|
|
error = sosetopt(so, &sopt);
|
|
|
|
if (error != 0)
|
1998-08-23 03:07:17 +00:00
|
|
|
goto out;
|
1997-05-11 18:05:39 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
/*
|
|
|
|
* Disable routing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
on = 1;
|
|
|
|
sopt.sopt_name = SO_DONTROUTE;
|
|
|
|
sopt.sopt_val = &on;
|
|
|
|
sopt.sopt_valsize = sizeof on;
|
|
|
|
|
|
|
|
error = sosetopt(so, &sopt);
|
|
|
|
if (error != 0)
|
|
|
|
goto out;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
/*
|
|
|
|
* Bind the local endpoint to a bootp client port.
|
|
|
|
*/
|
2000-10-29 01:19:32 +00:00
|
|
|
sin = &dst;
|
|
|
|
clear_sinaddr(sin);
|
1997-05-11 18:05:39 +00:00
|
|
|
sin->sin_port = htons(IPPORT_BOOTPC);
|
2001-09-12 08:38:13 +00:00
|
|
|
error = sobind(so, (struct sockaddr *)sin, td);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error != 0) {
|
1997-05-11 18:05:39 +00:00
|
|
|
printf("bind failed\n");
|
|
|
|
goto out;
|
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
/*
|
|
|
|
* Setup socket address for the server.
|
|
|
|
*/
|
2000-10-29 01:19:32 +00:00
|
|
|
sin = &dst;
|
|
|
|
clear_sinaddr(sin);
|
1997-05-11 18:05:39 +00:00
|
|
|
sin->sin_addr.s_addr = INADDR_BROADCAST;
|
|
|
|
sin->sin_port = htons(IPPORT_BOOTPS);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
/*
|
|
|
|
* Send it, repeatedly, until a reply is received,
|
|
|
|
* but delay each re-send by an increasing amount.
|
|
|
|
* If the delay hits the maximum, start complaining.
|
|
|
|
*/
|
|
|
|
timo = 0;
|
2000-10-29 01:19:32 +00:00
|
|
|
rtimo = 0;
|
1997-05-11 18:05:39 +00:00
|
|
|
for (;;) {
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
outstanding = 0;
|
|
|
|
gotrootpath = 0;
|
|
|
|
|
|
|
|
for (ifctx = gctx->interfaces;
|
|
|
|
ifctx != NULL;
|
|
|
|
ifctx = ifctx->next) {
|
2001-02-02 02:35:40 +00:00
|
|
|
if (bootpc_ifctx_isresolved(ifctx) != 0 &&
|
2000-10-29 01:19:32 +00:00
|
|
|
bootpc_tag(&gctx->tmptag, &ifctx->reply,
|
|
|
|
ifctx->replylen,
|
|
|
|
TAG_ROOT) != NULL)
|
|
|
|
gotrootpath = 1;
|
1997-05-11 18:05:39 +00:00
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
for (ifctx = gctx->interfaces;
|
|
|
|
ifctx != NULL;
|
|
|
|
ifctx = ifctx->next) {
|
|
|
|
ifctx->outstanding = 0;
|
2001-02-02 02:35:40 +00:00
|
|
|
if (bootpc_ifctx_isresolved(ifctx) != 0 &&
|
2000-10-29 01:19:32 +00:00
|
|
|
gotrootpath != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2001-02-02 02:35:40 +00:00
|
|
|
if (bootpc_ifctx_isfailed(ifctx) != 0)
|
2000-10-29 01:19:32 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
outstanding++;
|
|
|
|
ifctx->outstanding = 1;
|
1997-05-11 18:05:39 +00:00
|
|
|
|
2001-02-02 02:35:40 +00:00
|
|
|
/* Proceed to next step in DHCP negotiation */
|
|
|
|
if ((ifctx->state == IF_DHCP_OFFERED &&
|
|
|
|
ifctx->dhcpquerytype != DHCP_REQUEST) ||
|
|
|
|
(ifctx->state == IF_DHCP_UNRESOLVED &&
|
|
|
|
ifctx->dhcpquerytype != DHCP_DISCOVER) ||
|
|
|
|
(ifctx->state == IF_BOOTP_UNRESOLVED &&
|
|
|
|
ifctx->dhcpquerytype != DHCP_NOMSG)) {
|
|
|
|
ifctx->sentmsg = 0;
|
2001-09-12 08:38:13 +00:00
|
|
|
bootpc_compose_query(ifctx, gctx, td);
|
2001-02-02 02:35:40 +00:00
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
/* Send BOOTP request (or re-send). */
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
if (ifctx->sentmsg == 0) {
|
2001-02-02 02:35:40 +00:00
|
|
|
switch(ifctx->dhcpquerytype) {
|
|
|
|
case DHCP_DISCOVER:
|
|
|
|
s = "DHCP Discover";
|
|
|
|
break;
|
|
|
|
case DHCP_REQUEST:
|
|
|
|
s = "DHCP Request";
|
|
|
|
break;
|
|
|
|
case DHCP_NOMSG:
|
|
|
|
default:
|
|
|
|
s = "BOOTP Query";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf("Sending %s packet from "
|
2000-10-29 01:19:32 +00:00
|
|
|
"interface %s (%*D)\n",
|
2001-02-02 02:35:40 +00:00
|
|
|
s,
|
2000-10-29 01:19:32 +00:00
|
|
|
ifctx->ireq.ifr_name,
|
|
|
|
ifctx->sdl->sdl_alen,
|
|
|
|
(unsigned char *) LLADDR(ifctx->sdl),
|
|
|
|
":");
|
|
|
|
ifctx->sentmsg = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
aio.iov_base = (caddr_t) &ifctx->call;
|
|
|
|
aio.iov_len = sizeof(ifctx->call);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
auio.uio_iov = &aio;
|
|
|
|
auio.uio_iovcnt = 1;
|
|
|
|
auio.uio_segflg = UIO_SYSSPACE;
|
|
|
|
auio.uio_rw = UIO_WRITE;
|
|
|
|
auio.uio_offset = 0;
|
|
|
|
auio.uio_resid = sizeof(ifctx->call);
|
2001-09-12 08:38:13 +00:00
|
|
|
auio.uio_td = td;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
/* Set netmask to 0.0.0.0 */
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
|
|
|
|
clear_sinaddr(sin);
|
|
|
|
error = ifioctl(ifctx->so, SIOCSIFNETMASK,
|
2001-09-12 08:38:13 +00:00
|
|
|
(caddr_t) &ifctx->ireq, td);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error != 0)
|
|
|
|
panic("bootpc_call:"
|
|
|
|
"set if netmask, error=%d",
|
|
|
|
error);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
error = sosend(so, (struct sockaddr *) &dst,
|
2001-09-12 08:38:13 +00:00
|
|
|
&auio, NULL, NULL, 0, td);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error != 0) {
|
|
|
|
printf("bootpc_call: sosend: %d state %08x\n",
|
2002-05-31 11:52:35 +00:00
|
|
|
error, (int) so->so_state);
|
2000-10-29 01:19:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: Is this needed ? */
|
2007-02-27 17:23:29 +00:00
|
|
|
pause("bootpw", hz/10);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
/* Set netmask to 255.0.0.0 */
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
sin = (struct sockaddr_in *) &ifctx->ireq.ifr_addr;
|
|
|
|
clear_sinaddr(sin);
|
|
|
|
sin->sin_addr.s_addr = htonl(0xff000000u);
|
|
|
|
error = ifioctl(ifctx->so, SIOCSIFNETMASK,
|
2001-09-12 08:38:13 +00:00
|
|
|
(caddr_t) &ifctx->ireq, td);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error != 0)
|
|
|
|
panic("bootpc_call:"
|
|
|
|
"set if netmask, error=%d",
|
|
|
|
error);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
if (outstanding == 0 &&
|
|
|
|
(rtimo == 0 || time_second >= rtimo)) {
|
|
|
|
error = 0;
|
|
|
|
goto gotreply;
|
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
/* Determine new timeout. */
|
|
|
|
if (timo < MAX_RESEND_DELAY)
|
|
|
|
timo++;
|
2000-10-29 01:19:32 +00:00
|
|
|
else {
|
2001-02-02 02:35:40 +00:00
|
|
|
printf("DHCP/BOOTP timeout for server ");
|
2000-10-29 01:19:32 +00:00
|
|
|
print_sin_addr(&dst);
|
|
|
|
printf("\n");
|
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
/*
|
|
|
|
* Wait for up to timo seconds for a reply.
|
|
|
|
* The socket receive timeout was set to 1 second.
|
|
|
|
*/
|
2000-10-29 01:19:32 +00:00
|
|
|
atimo = timo + time_second;
|
|
|
|
while (time_second < atimo) {
|
|
|
|
aio.iov_base = (caddr_t) &gctx->reply;
|
|
|
|
aio.iov_len = sizeof(gctx->reply);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
auio.uio_iov = &aio;
|
|
|
|
auio.uio_iovcnt = 1;
|
|
|
|
auio.uio_segflg = UIO_SYSSPACE;
|
|
|
|
auio.uio_rw = UIO_READ;
|
|
|
|
auio.uio_offset = 0;
|
2000-10-29 01:19:32 +00:00
|
|
|
auio.uio_resid = sizeof(gctx->reply);
|
2001-09-12 08:38:13 +00:00
|
|
|
auio.uio_td = td;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
rcvflg = 0;
|
2000-10-24 22:40:18 +00:00
|
|
|
error = soreceive(so, NULL, &auio,
|
|
|
|
NULL, NULL, &rcvflg);
|
2000-10-29 01:19:32 +00:00
|
|
|
gctx->secs = time_second - gctx->starttime;
|
|
|
|
for (ifctx = gctx->interfaces;
|
|
|
|
ifctx != NULL;
|
|
|
|
ifctx = ifctx->next) {
|
2001-02-02 02:35:40 +00:00
|
|
|
if (bootpc_ifctx_isresolved(ifctx) != 0 ||
|
|
|
|
bootpc_ifctx_isfailed(ifctx) != 0)
|
2000-10-29 01:19:32 +00:00
|
|
|
continue;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
ifctx->call.secs = htons(gctx->secs);
|
1997-05-11 18:05:39 +00:00
|
|
|
}
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error == EWOULDBLOCK)
|
|
|
|
continue;
|
|
|
|
if (error != 0)
|
1997-05-11 18:05:39 +00:00
|
|
|
goto out;
|
2000-10-29 01:19:32 +00:00
|
|
|
len = sizeof(gctx->reply) - auio.uio_resid;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
1998-01-18 18:46:20 +00:00
|
|
|
/* Do we have the required number of bytes ? */
|
|
|
|
if (len < BOOTP_MIN_LEN)
|
1997-05-11 18:05:39 +00:00
|
|
|
continue;
|
2000-10-29 01:19:32 +00:00
|
|
|
gctx->replylen = len;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
/* Is it a reply? */
|
|
|
|
if (gctx->reply.op != BOOTP_REPLY)
|
2000-10-24 22:40:18 +00:00
|
|
|
continue;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
/* Is this an answer to our query */
|
|
|
|
for (ifctx = gctx->interfaces;
|
|
|
|
ifctx != NULL;
|
|
|
|
ifctx = ifctx->next) {
|
|
|
|
if (gctx->reply.xid != ifctx->call.xid)
|
|
|
|
continue;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
/* Same HW address size ? */
|
|
|
|
if (gctx->reply.hlen != ifctx->call.hlen)
|
|
|
|
continue;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
/* Correct HW address ? */
|
|
|
|
if (bcmp(gctx->reply.chaddr,
|
|
|
|
ifctx->call.chaddr,
|
|
|
|
ifctx->call.hlen) != 0)
|
|
|
|
continue;
|
1997-05-11 18:05:39 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
break;
|
|
|
|
}
|
1997-05-11 18:05:39 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
if (ifctx != NULL) {
|
2001-02-02 02:35:40 +00:00
|
|
|
s = bootpc_tag(&gctx->tmptag,
|
|
|
|
&gctx->reply,
|
|
|
|
gctx->replylen,
|
|
|
|
TAG_DHCP_MSGTYPE);
|
|
|
|
if (s != NULL) {
|
|
|
|
switch (*s) {
|
|
|
|
case DHCP_OFFER:
|
|
|
|
s = "DHCP Offer";
|
|
|
|
break;
|
|
|
|
case DHCP_ACK:
|
|
|
|
s = "DHCP Ack";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s = "DHCP (unexpected)";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
s = "BOOTP Reply";
|
|
|
|
|
|
|
|
printf("Received %s packet"
|
2000-10-29 01:19:32 +00:00
|
|
|
" on %s from ",
|
2001-02-02 02:35:40 +00:00
|
|
|
s,
|
2000-10-29 01:19:32 +00:00
|
|
|
ifctx->ireq.ifr_name);
|
|
|
|
print_in_addr(gctx->reply.siaddr);
|
|
|
|
if (gctx->reply.giaddr.s_addr !=
|
|
|
|
htonl(INADDR_ANY)) {
|
|
|
|
printf(" via ");
|
|
|
|
print_in_addr(gctx->reply.giaddr);
|
|
|
|
}
|
|
|
|
if (bootpc_received(gctx, ifctx) != 0) {
|
|
|
|
printf(" (accepted)");
|
|
|
|
if (ifctx->outstanding) {
|
|
|
|
ifctx->outstanding = 0;
|
|
|
|
outstanding--;
|
|
|
|
}
|
|
|
|
/* Network settle delay */
|
|
|
|
if (outstanding == 0)
|
|
|
|
atimo = time_second +
|
|
|
|
BOOTP_SETTLE_DELAY;
|
|
|
|
} else
|
|
|
|
printf(" (ignored)");
|
|
|
|
if (ifctx->gotrootpath) {
|
|
|
|
gotrootpath = 1;
|
|
|
|
rtimo = time_second +
|
|
|
|
BOOTP_SETTLE_DELAY;
|
|
|
|
printf(" (got root path)");
|
|
|
|
} else
|
|
|
|
printf(" (no root path)");
|
|
|
|
printf("\n");
|
|
|
|
}
|
1997-05-11 18:05:39 +00:00
|
|
|
} /* while secs */
|
2000-10-29 01:19:32 +00:00
|
|
|
#ifdef BOOTP_TIMEOUT
|
|
|
|
if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
|
|
|
|
break;
|
|
|
|
#endif
|
2001-02-02 02:35:40 +00:00
|
|
|
/* Force a retry if halfway in DHCP negotiation */
|
|
|
|
retry = 0;
|
|
|
|
for (ifctx = gctx->interfaces; ifctx != NULL;
|
|
|
|
ifctx = ifctx->next) {
|
|
|
|
if (ifctx->state == IF_DHCP_OFFERED) {
|
|
|
|
if (ifctx->dhcpquerytype == DHCP_DISCOVER)
|
|
|
|
retry = 1;
|
|
|
|
else
|
|
|
|
ifctx->state = IF_DHCP_UNRESOLVED;
|
|
|
|
}
|
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2001-02-02 02:35:40 +00:00
|
|
|
if (retry != 0)
|
|
|
|
continue;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
if (gotrootpath != 0) {
|
|
|
|
gctx->gotrootpath = gotrootpath;
|
|
|
|
if (rtimo != 0 && time_second >= rtimo)
|
|
|
|
break;
|
|
|
|
}
|
1997-05-11 18:05:39 +00:00
|
|
|
} /* forever send/receive */
|
2001-09-18 23:32:09 +00:00
|
|
|
|
|
|
|
/*
|
2000-10-29 01:19:32 +00:00
|
|
|
* XXX: These are errors of varying seriousness being silently
|
|
|
|
* ignored
|
|
|
|
*/
|
1997-05-11 18:05:39 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
|
2001-02-02 02:35:40 +00:00
|
|
|
if (bootpc_ifctx_isresolved(ifctx) == 0) {
|
|
|
|
printf("%s timeout for interface %s\n",
|
|
|
|
ifctx->dhcpquerytype != DHCP_NOMSG ?
|
|
|
|
"DHCP" : "BOOTP",
|
2000-10-29 01:19:32 +00:00
|
|
|
ifctx->ireq.ifr_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gctx->gotrootpath != 0) {
|
|
|
|
#if 0
|
|
|
|
printf("Got a root path, ignoring remaining timeout\n");
|
|
|
|
#endif
|
|
|
|
error = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
2004-07-08 22:35:36 +00:00
|
|
|
#ifndef BOOTP_NFSROOT
|
|
|
|
for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
|
|
|
|
if (bootpc_ifctx_isresolved(ifctx) != 0) {
|
|
|
|
error = 0;
|
|
|
|
goto out;
|
2000-10-29 01:19:32 +00:00
|
|
|
}
|
|
|
|
}
|
2004-07-08 22:35:36 +00:00
|
|
|
#endif
|
1997-05-11 18:05:39 +00:00
|
|
|
error = ETIMEDOUT;
|
|
|
|
goto out;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
gotreply:
|
|
|
|
out:
|
1997-05-11 18:05:39 +00:00
|
|
|
soclose(so);
|
2003-06-09 03:44:34 +00:00
|
|
|
out0:
|
2009-12-13 11:06:39 +00:00
|
|
|
CURVNET_RESTORE();
|
1997-05-11 18:05:39 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
static int
|
2000-10-29 01:19:32 +00:00
|
|
|
bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
|
2001-09-18 23:32:09 +00:00
|
|
|
struct bootpc_globalcontext *gctx, struct thread *td)
|
1997-05-11 18:05:39 +00:00
|
|
|
{
|
2000-10-24 22:40:18 +00:00
|
|
|
struct sockaddr_in *sin;
|
|
|
|
int error;
|
2000-10-29 01:19:32 +00:00
|
|
|
struct ifreq *ireq;
|
|
|
|
struct socket *so;
|
|
|
|
struct ifaddr *ifa;
|
|
|
|
struct sockaddr_dl *sdl;
|
|
|
|
|
2009-12-13 11:06:39 +00:00
|
|
|
CURVNET_SET(TD_TO_VNET(td));
|
|
|
|
|
2002-02-27 18:32:23 +00:00
|
|
|
error = socreate(AF_INET, &ifctx->so, SOCK_DGRAM, 0, td->td_ucred, td);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error != 0)
|
|
|
|
panic("nfs_boot: socreate, error=%d", error);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
ireq = &ifctx->ireq;
|
|
|
|
so = ifctx->so;
|
2000-10-24 22:40:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Bring up the interface.
|
|
|
|
*
|
|
|
|
* Get the old interface flags and or IFF_UP into them; if
|
|
|
|
* IFF_UP set blindly, interface selection can be clobbered.
|
|
|
|
*/
|
2001-09-12 08:38:13 +00:00
|
|
|
error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, td);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error != 0)
|
2000-10-24 22:40:18 +00:00
|
|
|
panic("bootpc_fakeup_interface: GIFFLAGS, error=%d", error);
|
|
|
|
ireq->ifr_flags |= IFF_UP;
|
2001-09-12 08:38:13 +00:00
|
|
|
error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, td);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error != 0)
|
2000-10-24 22:40:18 +00:00
|
|
|
panic("bootpc_fakeup_interface: SIFFLAGS, error=%d", error);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
/*
|
|
|
|
* Do enough of ifconfig(8) so that the chosen interface
|
|
|
|
* can talk to the servers. (just set the address)
|
|
|
|
*/
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
/* addr is 0.0.0.0 */
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
sin = (struct sockaddr_in *) &ireq->ifr_addr;
|
|
|
|
clear_sinaddr(sin);
|
2001-09-12 08:38:13 +00:00
|
|
|
error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, td);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
|
|
|
|
panic("bootpc_fakeup_interface: "
|
|
|
|
"set if addr, error=%d", error);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
/* netmask is 255.0.0.0 */
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
sin = (struct sockaddr_in *) &ireq->ifr_addr;
|
|
|
|
clear_sinaddr(sin);
|
|
|
|
sin->sin_addr.s_addr = htonl(0xff000000u);
|
2001-09-12 08:38:13 +00:00
|
|
|
error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)ireq, td);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error != 0)
|
|
|
|
panic("bootpc_fakeup_interface: set if netmask, error=%d",
|
|
|
|
error);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
/* Broadcast is 255.255.255.255 */
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
sin = (struct sockaddr_in *)&ireq->ifr_addr;
|
2000-10-29 01:19:32 +00:00
|
|
|
clear_sinaddr(sin);
|
|
|
|
clear_sinaddr(&ifctx->broadcast);
|
|
|
|
sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
|
|
|
|
ifctx->broadcast.sin_addr.s_addr = sin->sin_addr.s_addr;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t)ireq, td);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error != 0)
|
|
|
|
panic("bootpc_fakeup_interface: "
|
|
|
|
"set if broadcast addr, error=%d",
|
|
|
|
error);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
/* Get HW address */
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
sdl = NULL;
|
2006-06-29 15:37:39 +00:00
|
|
|
TAILQ_FOREACH(ifa, &ifctx->ifp->if_addrhead, ifa_link)
|
2006-06-29 19:22:05 +00:00
|
|
|
if (ifa->ifa_addr->sa_family == AF_LINK) {
|
|
|
|
sdl = (struct sockaddr_dl *)ifa->ifa_addr;
|
|
|
|
if (sdl->sdl_type == IFT_ETHER)
|
|
|
|
break;
|
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
if (sdl == NULL)
|
|
|
|
panic("bootpc: Unable to find HW address for %s",
|
|
|
|
ifctx->ireq.ifr_name);
|
|
|
|
ifctx->sdl = sdl;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2009-12-13 11:06:39 +00:00
|
|
|
CURVNET_RESTORE();
|
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
return error;
|
1997-05-11 18:05:39 +00:00
|
|
|
}
|
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
static int
|
2000-10-29 01:19:32 +00:00
|
|
|
bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
|
2001-09-18 23:32:09 +00:00
|
|
|
struct bootpc_globalcontext *gctx, struct thread *td)
|
1997-05-11 18:05:39 +00:00
|
|
|
{
|
2000-10-24 22:40:18 +00:00
|
|
|
int error;
|
2000-10-29 01:19:32 +00:00
|
|
|
struct sockaddr_in defdst;
|
|
|
|
struct sockaddr_in defmask;
|
2000-10-24 22:40:18 +00:00
|
|
|
struct sockaddr_in *sin;
|
2000-10-29 01:19:32 +00:00
|
|
|
struct ifreq *ireq;
|
|
|
|
struct socket *so;
|
|
|
|
struct sockaddr_in *myaddr;
|
|
|
|
struct sockaddr_in *netmask;
|
|
|
|
struct sockaddr_in *gw;
|
1997-05-11 18:05:39 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
ireq = &ifctx->ireq;
|
|
|
|
so = ifctx->so;
|
|
|
|
myaddr = &ifctx->myaddr;
|
|
|
|
netmask = &ifctx->netmask;
|
|
|
|
gw = &ifctx->gw;
|
|
|
|
|
2001-02-02 02:35:40 +00:00
|
|
|
if (bootpc_ifctx_isresolved(ifctx) == 0) {
|
2000-10-29 01:19:32 +00:00
|
|
|
|
|
|
|
/* Shutdown interfaces where BOOTP failed */
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
|
2001-09-12 08:38:13 +00:00
|
|
|
error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)ireq, td);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error != 0)
|
|
|
|
panic("bootpc_adjust_interface: "
|
|
|
|
"SIOCGIFFLAGS, error=%d", error);
|
|
|
|
ireq->ifr_flags &= ~IFF_UP;
|
2001-09-12 08:38:13 +00:00
|
|
|
error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)ireq, td);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error != 0)
|
|
|
|
panic("bootpc_adjust_interface: "
|
|
|
|
"SIOCSIFFLAGS, error=%d", error);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
sin = (struct sockaddr_in *) &ireq->ifr_addr;
|
|
|
|
clear_sinaddr(sin);
|
2001-09-12 08:38:13 +00:00
|
|
|
error = ifioctl(so, SIOCDIFADDR, (caddr_t) ireq, td);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error != 0 && (error != EEXIST ||
|
|
|
|
ifctx == gctx->interfaces))
|
|
|
|
panic("bootpc_adjust_interface: "
|
|
|
|
"SIOCDIFADDR, error=%d", error);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
return 0;
|
2000-10-24 22:40:18 +00:00
|
|
|
}
|
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
printf("Adjusted interface %s\n", ifctx->ireq.ifr_name);
|
2000-10-24 22:40:18 +00:00
|
|
|
/*
|
|
|
|
* Do enough of ifconfig(8) so that the chosen interface
|
|
|
|
* can talk to the servers. (just set the address)
|
|
|
|
*/
|
|
|
|
bcopy(netmask, &ireq->ifr_addr, sizeof(*netmask));
|
2001-09-12 08:38:13 +00:00
|
|
|
error = ifioctl(so, SIOCSIFNETMASK, (caddr_t) ireq, td);
|
2000-10-24 22:40:18 +00:00
|
|
|
if (error != 0)
|
2000-10-29 01:19:32 +00:00
|
|
|
panic("bootpc_adjust_interface: "
|
|
|
|
"set if netmask, error=%d", error);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
/* Broadcast is with host part of IP address all 1's */
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
sin = (struct sockaddr_in *) &ireq->ifr_addr;
|
|
|
|
clear_sinaddr(sin);
|
2000-10-24 22:40:18 +00:00
|
|
|
sin->sin_addr.s_addr = myaddr->sin_addr.s_addr |
|
|
|
|
~ netmask->sin_addr.s_addr;
|
2001-09-12 08:38:13 +00:00
|
|
|
error = ifioctl(so, SIOCSIFBRDADDR, (caddr_t) ireq, td);
|
2000-10-24 22:40:18 +00:00
|
|
|
if (error != 0)
|
2000-10-29 01:19:32 +00:00
|
|
|
panic("bootpc_adjust_interface: "
|
|
|
|
"set if broadcast addr, error=%d", error);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
bcopy(myaddr, &ireq->ifr_addr, sizeof(*myaddr));
|
2001-09-12 08:38:13 +00:00
|
|
|
error = ifioctl(so, SIOCSIFADDR, (caddr_t) ireq, td);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error != 0 && (error != EEXIST || ifctx == gctx->interfaces))
|
|
|
|
panic("bootpc_adjust_interface: "
|
|
|
|
"set if addr, error=%d", error);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
/* Add new default route */
|
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
if (ifctx->gotgw != 0 || gctx->gotgw == 0) {
|
|
|
|
clear_sinaddr(&defdst);
|
|
|
|
clear_sinaddr(&defmask);
|
Add code to allow the system to handle multiple routing tables.
This particular implementation is designed to be fully backwards compatible
and to be MFC-able to 7.x (and 6.x)
Currently the only protocol that can make use of the multiple tables is IPv4
Similar functionality exists in OpenBSD and Linux.
From my notes:
-----
One thing where FreeBSD has been falling behind, and which by chance I
have some time to work on is "policy based routing", which allows
different
packet streams to be routed by more than just the destination address.
Constraints:
------------
I want to make some form of this available in the 6.x tree
(and by extension 7.x) , but FreeBSD in general needs it so I might as
well do it in -current and back port the portions I need.
One of the ways that this can be done is to have the ability to
instantiate multiple kernel routing tables (which I will now
refer to as "Forwarding Information Bases" or "FIBs" for political
correctness reasons). Which FIB a particular packet uses to make
the next hop decision can be decided by a number of mechanisms.
The policies these mechanisms implement are the "Policies" referred
to in "Policy based routing".
One of the constraints I have if I try to back port this work to
6.x is that it must be implemented as a EXTENSION to the existing
ABIs in 6.x so that third party applications do not need to be
recompiled in timespan of the branch.
This first version will not have some of the bells and whistles that
will come with later versions. It will, for example, be limited to 16
tables in the first commit.
Implementation method, Compatible version. (part 1)
-------------------------------
For this reason I have implemented a "sufficient subset" of a
multiple routing table solution in Perforce, and back-ported it
to 6.x. (also in Perforce though not always caught up with what I
have done in -current/P4). The subset allows a number of FIBs
to be defined at compile time (8 is sufficient for my purposes in 6.x)
and implements the changes needed to allow IPV4 to use them. I have not
done the changes for ipv6 simply because I do not need it, and I do not
have enough knowledge of ipv6 (e.g. neighbor discovery) needed to do it.
Other protocol families are left untouched and should there be
users with proprietary protocol families, they should continue to work
and be oblivious to the existence of the extra FIBs.
To understand how this is done, one must know that the current FIB
code starts everything off with a single dimensional array of
pointers to FIB head structures (One per protocol family), each of
which in turn points to the trie of routes available to that family.
The basic change in the ABI compatible version of the change is to
extent that array to be a 2 dimensional array, so that
instead of protocol family X looking at rt_tables[X] for the
table it needs, it looks at rt_tables[Y][X] when for all
protocol families except ipv4 Y is always 0.
Code that is unaware of the change always just sees the first row
of the table, which of course looks just like the one dimensional
array that existed before.
The entry points rtrequest(), rtalloc(), rtalloc1(), rtalloc_ign()
are all maintained, but refer only to the first row of the array,
so that existing callers in proprietary protocols can continue to
do the "right thing".
Some new entry points are added, for the exclusive use of ipv4 code
called in_rtrequest(), in_rtalloc(), in_rtalloc1() and in_rtalloc_ign(),
which have an extra argument which refers the code to the correct row.
In addition, there are some new entry points (currently called
rtalloc_fib() and friends) that check the Address family being
looked up and call either rtalloc() (and friends) if the protocol
is not IPv4 forcing the action to row 0 or to the appropriate row
if it IS IPv4 (and that info is available). These are for calling
from code that is not specific to any particular protocol. The way
these are implemented would change in the non ABI preserving code
to be added later.
One feature of the first version of the code is that for ipv4,
the interface routes show up automatically on all the FIBs, so
that no matter what FIB you select you always have the basic
direct attached hosts available to you. (rtinit() does this
automatically).
You CAN delete an interface route from one FIB should you want
to but by default it's there. ARP information is also available
in each FIB. It's assumed that the same machine would have the
same MAC address, regardless of which FIB you are using to get
to it.
This brings us as to how the correct FIB is selected for an outgoing
IPV4 packet.
Firstly, all packets have a FIB associated with them. if nothing
has been done to change it, it will be FIB 0. The FIB is changed
in the following ways.
Packets fall into one of a number of classes.
1/ locally generated packets, coming from a socket/PCB.
Such packets select a FIB from a number associated with the
socket/PCB. This in turn is inherited from the process,
but can be changed by a socket option. The process in turn
inherits it on fork. I have written a utility call setfib
that acts a bit like nice..
setfib -3 ping target.example.com # will use fib 3 for ping.
It is an obvious extension to make it a property of a jail
but I have not done so. It can be achieved by combining the setfib and
jail commands.
2/ packets received on an interface for forwarding.
By default these packets would use table 0,
(or possibly a number settable in a sysctl(not yet)).
but prior to routing the firewall can inspect them (see below).
(possibly in the future you may be able to associate a FIB
with packets received on an interface.. An ifconfig arg, but not yet.)
3/ packets inspected by a packet classifier, which can arbitrarily
associate a fib with it on a packet by packet basis.
A fib assigned to a packet by a packet classifier
(such as ipfw) would over-ride a fib associated by
a more default source. (such as cases 1 or 2).
4/ a tcp listen socket associated with a fib will generate
accept sockets that are associated with that same fib.
5/ Packets generated in response to some other packet (e.g. reset
or icmp packets). These should use the FIB associated with the
packet being reponded to.
6/ Packets generated during encapsulation.
gif, tun and other tunnel interfaces will encapsulate using the FIB
that was in effect withthe proces that set up the tunnel.
thus setfib 1 ifconfig gif0 [tunnel instructions]
will set the fib for the tunnel to use to be fib 1.
Routing messages would be associated with their
process, and thus select one FIB or another.
messages from the kernel would be associated with the fib they
refer to and would only be received by a routing socket associated
with that fib. (not yet implemented)
In addition Netstat has been edited to be able to cope with the
fact that the array is now 2 dimensional. (It looks in system
memory using libkvm (!)). Old versions of netstat see only the first FIB.
In addition two sysctls are added to give:
a) the number of FIBs compiled in (active)
b) the default FIB of the calling process.
Early testing experience:
-------------------------
Basically our (IronPort's) appliance does this functionality already
using ipfw fwd but that method has some drawbacks.
For example,
It can't fully simulate a routing table because it can't influence the
socket's choice of local address when a connect() is done.
Testing during the generating of these changes has been
remarkably smooth so far. Multiple tables have co-existed
with no notable side effects, and packets have been routes
accordingly.
ipfw has grown 2 new keywords:
setfib N ip from anay to any
count ip from any to any fib N
In pf there seems to be a requirement to be able to give symbolic names to the
fibs but I do not have that capacity. I am not sure if it is required.
SCTP has interestingly enough built in support for this, called VRFs
in Cisco parlance. it will be interesting to see how that handles it
when it suddenly actually does something.
Where to next:
--------------------
After committing the ABI compatible version and MFCing it, I'd
like to proceed in a forward direction in -current. this will
result in some roto-tilling in the routing code.
Firstly: the current code's idea of having a separate tree per
protocol family, all of the same format, and pointed to by the
1 dimensional array is a bit silly. Especially when one considers that
there is code that makes assumptions about every protocol having the
same internal structures there. Some protocols don't WANT that
sort of structure. (for example the whole idea of a netmask is foreign
to appletalk). This needs to be made opaque to the external code.
My suggested first change is to add routing method pointers to the
'domain' structure, along with information pointing the data.
instead of having an array of pointers to uniform structures,
there would be an array pointing to the 'domain' structures
for each protocol address domain (protocol family),
and the methods this reached would be called. The methods would have
an argument that gives FIB number, but the protocol would be free
to ignore it.
When the ABI can be changed it raises the possibilty of the
addition of a fib entry into the "struct route". Currently,
the structure contains the sockaddr of the desination, and the resulting
fib entry. To make this work fully, one could add a fib number
so that given an address and a fib, one can find the third element, the
fib entry.
Interaction with the ARP layer/ LL layer would need to be
revisited as well. Qing Li has been working on this already.
This work was sponsored by Ironport Systems/Cisco
Reviewed by: several including rwatson, bz and mlair (parts each)
Obtained from: Ironport systems/Cisco
2008-05-09 23:03:00 +00:00
|
|
|
/* XXX MRT just table 0 */
|
|
|
|
error = rtrequest_fib(RTM_ADD,
|
2000-10-29 01:19:32 +00:00
|
|
|
(struct sockaddr *) &defdst,
|
|
|
|
(struct sockaddr *) gw,
|
|
|
|
(struct sockaddr *) &defmask,
|
Add code to allow the system to handle multiple routing tables.
This particular implementation is designed to be fully backwards compatible
and to be MFC-able to 7.x (and 6.x)
Currently the only protocol that can make use of the multiple tables is IPv4
Similar functionality exists in OpenBSD and Linux.
From my notes:
-----
One thing where FreeBSD has been falling behind, and which by chance I
have some time to work on is "policy based routing", which allows
different
packet streams to be routed by more than just the destination address.
Constraints:
------------
I want to make some form of this available in the 6.x tree
(and by extension 7.x) , but FreeBSD in general needs it so I might as
well do it in -current and back port the portions I need.
One of the ways that this can be done is to have the ability to
instantiate multiple kernel routing tables (which I will now
refer to as "Forwarding Information Bases" or "FIBs" for political
correctness reasons). Which FIB a particular packet uses to make
the next hop decision can be decided by a number of mechanisms.
The policies these mechanisms implement are the "Policies" referred
to in "Policy based routing".
One of the constraints I have if I try to back port this work to
6.x is that it must be implemented as a EXTENSION to the existing
ABIs in 6.x so that third party applications do not need to be
recompiled in timespan of the branch.
This first version will not have some of the bells and whistles that
will come with later versions. It will, for example, be limited to 16
tables in the first commit.
Implementation method, Compatible version. (part 1)
-------------------------------
For this reason I have implemented a "sufficient subset" of a
multiple routing table solution in Perforce, and back-ported it
to 6.x. (also in Perforce though not always caught up with what I
have done in -current/P4). The subset allows a number of FIBs
to be defined at compile time (8 is sufficient for my purposes in 6.x)
and implements the changes needed to allow IPV4 to use them. I have not
done the changes for ipv6 simply because I do not need it, and I do not
have enough knowledge of ipv6 (e.g. neighbor discovery) needed to do it.
Other protocol families are left untouched and should there be
users with proprietary protocol families, they should continue to work
and be oblivious to the existence of the extra FIBs.
To understand how this is done, one must know that the current FIB
code starts everything off with a single dimensional array of
pointers to FIB head structures (One per protocol family), each of
which in turn points to the trie of routes available to that family.
The basic change in the ABI compatible version of the change is to
extent that array to be a 2 dimensional array, so that
instead of protocol family X looking at rt_tables[X] for the
table it needs, it looks at rt_tables[Y][X] when for all
protocol families except ipv4 Y is always 0.
Code that is unaware of the change always just sees the first row
of the table, which of course looks just like the one dimensional
array that existed before.
The entry points rtrequest(), rtalloc(), rtalloc1(), rtalloc_ign()
are all maintained, but refer only to the first row of the array,
so that existing callers in proprietary protocols can continue to
do the "right thing".
Some new entry points are added, for the exclusive use of ipv4 code
called in_rtrequest(), in_rtalloc(), in_rtalloc1() and in_rtalloc_ign(),
which have an extra argument which refers the code to the correct row.
In addition, there are some new entry points (currently called
rtalloc_fib() and friends) that check the Address family being
looked up and call either rtalloc() (and friends) if the protocol
is not IPv4 forcing the action to row 0 or to the appropriate row
if it IS IPv4 (and that info is available). These are for calling
from code that is not specific to any particular protocol. The way
these are implemented would change in the non ABI preserving code
to be added later.
One feature of the first version of the code is that for ipv4,
the interface routes show up automatically on all the FIBs, so
that no matter what FIB you select you always have the basic
direct attached hosts available to you. (rtinit() does this
automatically).
You CAN delete an interface route from one FIB should you want
to but by default it's there. ARP information is also available
in each FIB. It's assumed that the same machine would have the
same MAC address, regardless of which FIB you are using to get
to it.
This brings us as to how the correct FIB is selected for an outgoing
IPV4 packet.
Firstly, all packets have a FIB associated with them. if nothing
has been done to change it, it will be FIB 0. The FIB is changed
in the following ways.
Packets fall into one of a number of classes.
1/ locally generated packets, coming from a socket/PCB.
Such packets select a FIB from a number associated with the
socket/PCB. This in turn is inherited from the process,
but can be changed by a socket option. The process in turn
inherits it on fork. I have written a utility call setfib
that acts a bit like nice..
setfib -3 ping target.example.com # will use fib 3 for ping.
It is an obvious extension to make it a property of a jail
but I have not done so. It can be achieved by combining the setfib and
jail commands.
2/ packets received on an interface for forwarding.
By default these packets would use table 0,
(or possibly a number settable in a sysctl(not yet)).
but prior to routing the firewall can inspect them (see below).
(possibly in the future you may be able to associate a FIB
with packets received on an interface.. An ifconfig arg, but not yet.)
3/ packets inspected by a packet classifier, which can arbitrarily
associate a fib with it on a packet by packet basis.
A fib assigned to a packet by a packet classifier
(such as ipfw) would over-ride a fib associated by
a more default source. (such as cases 1 or 2).
4/ a tcp listen socket associated with a fib will generate
accept sockets that are associated with that same fib.
5/ Packets generated in response to some other packet (e.g. reset
or icmp packets). These should use the FIB associated with the
packet being reponded to.
6/ Packets generated during encapsulation.
gif, tun and other tunnel interfaces will encapsulate using the FIB
that was in effect withthe proces that set up the tunnel.
thus setfib 1 ifconfig gif0 [tunnel instructions]
will set the fib for the tunnel to use to be fib 1.
Routing messages would be associated with their
process, and thus select one FIB or another.
messages from the kernel would be associated with the fib they
refer to and would only be received by a routing socket associated
with that fib. (not yet implemented)
In addition Netstat has been edited to be able to cope with the
fact that the array is now 2 dimensional. (It looks in system
memory using libkvm (!)). Old versions of netstat see only the first FIB.
In addition two sysctls are added to give:
a) the number of FIBs compiled in (active)
b) the default FIB of the calling process.
Early testing experience:
-------------------------
Basically our (IronPort's) appliance does this functionality already
using ipfw fwd but that method has some drawbacks.
For example,
It can't fully simulate a routing table because it can't influence the
socket's choice of local address when a connect() is done.
Testing during the generating of these changes has been
remarkably smooth so far. Multiple tables have co-existed
with no notable side effects, and packets have been routes
accordingly.
ipfw has grown 2 new keywords:
setfib N ip from anay to any
count ip from any to any fib N
In pf there seems to be a requirement to be able to give symbolic names to the
fibs but I do not have that capacity. I am not sure if it is required.
SCTP has interestingly enough built in support for this, called VRFs
in Cisco parlance. it will be interesting to see how that handles it
when it suddenly actually does something.
Where to next:
--------------------
After committing the ABI compatible version and MFCing it, I'd
like to proceed in a forward direction in -current. this will
result in some roto-tilling in the routing code.
Firstly: the current code's idea of having a separate tree per
protocol family, all of the same format, and pointed to by the
1 dimensional array is a bit silly. Especially when one considers that
there is code that makes assumptions about every protocol having the
same internal structures there. Some protocols don't WANT that
sort of structure. (for example the whole idea of a netmask is foreign
to appletalk). This needs to be made opaque to the external code.
My suggested first change is to add routing method pointers to the
'domain' structure, along with information pointing the data.
instead of having an array of pointers to uniform structures,
there would be an array pointing to the 'domain' structures
for each protocol address domain (protocol family),
and the methods this reached would be called. The methods would have
an argument that gives FIB number, but the protocol would be free
to ignore it.
When the ABI can be changed it raises the possibilty of the
addition of a fib entry into the "struct route". Currently,
the structure contains the sockaddr of the desination, and the resulting
fib entry. To make this work fully, one could add a fib number
so that given an address and a fib, one can find the third element, the
fib entry.
Interaction with the ARP layer/ LL layer would need to be
revisited as well. Qing Li has been working on this already.
This work was sponsored by Ironport Systems/Cisco
Reviewed by: several including rwatson, bz and mlair (parts each)
Obtained from: Ironport systems/Cisco
2008-05-09 23:03:00 +00:00
|
|
|
(RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL, 0);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (error != 0) {
|
|
|
|
printf("bootpc_adjust_interface: "
|
|
|
|
"add net route, error=%d\n", error);
|
|
|
|
return error;
|
|
|
|
}
|
2000-10-24 22:40:18 +00:00
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
return 0;
|
1997-05-11 18:05:39 +00:00
|
|
|
}
|
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
static int
|
2004-03-12 20:37:40 +00:00
|
|
|
setfs(struct sockaddr_in *addr, char *path, char *p,
|
|
|
|
const struct in_addr *siaddr)
|
1997-05-11 18:05:39 +00:00
|
|
|
{
|
2004-03-12 20:37:40 +00:00
|
|
|
|
|
|
|
if (getip(&p, &addr->sin_addr) == 0) {
|
|
|
|
if (siaddr != NULL && *p == '/')
|
|
|
|
bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr));
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
if (*p != ':')
|
|
|
|
return 0;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr->sin_len = sizeof(struct sockaddr_in);
|
|
|
|
addr->sin_family = AF_INET;
|
|
|
|
|
|
|
|
strlcpy(path, p, MNAMELEN);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
getip(char **ptr, struct in_addr *addr)
|
|
|
|
{
|
|
|
|
char *p;
|
2000-10-24 22:40:18 +00:00
|
|
|
unsigned int ip;
|
1997-05-14 01:36:51 +00:00
|
|
|
int val;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2004-03-12 20:37:40 +00:00
|
|
|
p = *ptr;
|
2000-10-24 22:40:18 +00:00
|
|
|
ip = 0;
|
|
|
|
if (((val = getdec(&p)) < 0) || (val > 255))
|
|
|
|
return 0;
|
1997-05-14 01:36:51 +00:00
|
|
|
ip = val << 24;
|
2000-10-24 22:40:18 +00:00
|
|
|
if (*p != '.')
|
|
|
|
return 0;
|
1997-05-14 01:36:51 +00:00
|
|
|
p++;
|
2000-10-24 22:40:18 +00:00
|
|
|
if (((val = getdec(&p)) < 0) || (val > 255))
|
|
|
|
return 0;
|
1997-05-14 01:36:51 +00:00
|
|
|
ip |= (val << 16);
|
2000-10-24 22:40:18 +00:00
|
|
|
if (*p != '.')
|
|
|
|
return 0;
|
1997-05-14 01:36:51 +00:00
|
|
|
p++;
|
2000-10-24 22:40:18 +00:00
|
|
|
if (((val = getdec(&p)) < 0) || (val > 255))
|
|
|
|
return 0;
|
1997-05-14 01:36:51 +00:00
|
|
|
ip |= (val << 8);
|
2000-10-24 22:40:18 +00:00
|
|
|
if (*p != '.')
|
|
|
|
return 0;
|
1997-05-14 01:36:51 +00:00
|
|
|
p++;
|
2000-10-24 22:40:18 +00:00
|
|
|
if (((val = getdec(&p)) < 0) || (val > 255))
|
|
|
|
return 0;
|
1997-05-14 01:36:51 +00:00
|
|
|
ip |= val;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2004-03-12 20:37:40 +00:00
|
|
|
addr->s_addr = htonl(ip);
|
|
|
|
*ptr = p;
|
2000-10-24 22:40:18 +00:00
|
|
|
return 1;
|
1997-05-14 01:36:51 +00:00
|
|
|
}
|
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
static int
|
|
|
|
getdec(char **ptr)
|
1997-05-14 01:36:51 +00:00
|
|
|
{
|
2000-10-24 22:40:18 +00:00
|
|
|
char *p;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
p = *ptr;
|
|
|
|
ret = 0;
|
|
|
|
if ((*p < '0') || (*p > '9'))
|
|
|
|
return -1;
|
1997-05-14 01:36:51 +00:00
|
|
|
while ((*p >= '0') && (*p <= '9')) {
|
2000-10-24 22:40:18 +00:00
|
|
|
ret = ret * 10 + (*p - '0');
|
1997-05-14 01:36:51 +00:00
|
|
|
p++;
|
1997-05-11 18:05:39 +00:00
|
|
|
}
|
1997-05-14 01:36:51 +00:00
|
|
|
*ptr = p;
|
2000-10-24 22:40:18 +00:00
|
|
|
return ret;
|
1997-05-14 01:36:51 +00:00
|
|
|
}
|
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
static void
|
|
|
|
mountopts(struct nfs_args *args, char *p)
|
1997-05-14 01:36:51 +00:00
|
|
|
{
|
2000-10-29 01:19:32 +00:00
|
|
|
args->version = NFS_ARGSVERSION;
|
2008-05-16 06:27:03 +00:00
|
|
|
args->rsize = BOOTP_BLOCKSIZE;
|
|
|
|
args->wsize = BOOTP_BLOCKSIZE;
|
1997-05-14 01:36:51 +00:00
|
|
|
args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
|
|
|
|
args->sotype = SOCK_DGRAM;
|
2006-12-06 02:15:25 +00:00
|
|
|
if (p != NULL)
|
|
|
|
nfs_parse_options(p, args);
|
1997-05-11 18:05:39 +00:00
|
|
|
}
|
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
static int
|
|
|
|
xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
|
1997-06-12 14:08:20 +00:00
|
|
|
{
|
2000-10-24 22:40:18 +00:00
|
|
|
struct mbuf *m;
|
|
|
|
int alignedlen;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
m = *mptr;
|
|
|
|
alignedlen = ( len + 3 ) & ~3;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
if (m->m_len < alignedlen) {
|
|
|
|
m = m_pullup(m, alignedlen);
|
|
|
|
if (m == NULL) {
|
|
|
|
*mptr = NULL;
|
|
|
|
return EBADRPC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bcopy(mtod(m, u_char *), buf, len);
|
|
|
|
m_adj(m, alignedlen);
|
|
|
|
*mptr = m;
|
|
|
|
return 0;
|
1997-06-12 14:08:20 +00:00
|
|
|
}
|
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
static int
|
|
|
|
xdr_int_decode(struct mbuf **mptr, int *iptr)
|
1997-06-12 14:08:20 +00:00
|
|
|
{
|
2000-10-24 22:40:18 +00:00
|
|
|
u_int32_t i;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
|
|
|
|
return EBADRPC;
|
|
|
|
*iptr = fxdr_unsigned(u_int32_t, i);
|
|
|
|
return 0;
|
1997-06-12 14:08:20 +00:00
|
|
|
}
|
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
static void
|
2000-10-29 01:19:32 +00:00
|
|
|
print_sin_addr(struct sockaddr_in *sin)
|
1997-06-12 14:08:20 +00:00
|
|
|
{
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
print_in_addr(sin->sin_addr);
|
|
|
|
}
|
1997-06-12 14:08:20 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
static void
|
|
|
|
print_in_addr(struct in_addr addr)
|
|
|
|
{
|
|
|
|
unsigned int ip;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
ip = ntohl(addr.s_addr);
|
|
|
|
printf("%d.%d.%d.%d",
|
2000-10-24 22:40:18 +00:00
|
|
|
ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
|
1997-06-12 14:08:20 +00:00
|
|
|
}
|
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
static void
|
2001-09-18 23:32:09 +00:00
|
|
|
bootpc_compose_query(struct bootpc_ifcontext *ifctx,
|
|
|
|
struct bootpc_globalcontext *gctx, struct thread *td)
|
2000-10-29 01:19:32 +00:00
|
|
|
{
|
2001-02-02 02:35:40 +00:00
|
|
|
unsigned char *vendp;
|
2002-05-17 20:18:48 +00:00
|
|
|
unsigned char vendor_client[64];
|
2001-02-02 02:35:40 +00:00
|
|
|
uint32_t leasetime;
|
2002-05-17 20:18:48 +00:00
|
|
|
uint8_t vendor_client_len;
|
2001-02-02 02:35:40 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
ifctx->gotrootpath = 0;
|
2001-02-02 02:35:40 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
bzero((caddr_t) &ifctx->call, sizeof(ifctx->call));
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
/* bootpc part */
|
|
|
|
ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */
|
|
|
|
ifctx->call.htype = 1; /* 10mb ethernet */
|
|
|
|
ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */
|
|
|
|
ifctx->call.hops = 0;
|
2001-02-02 02:35:40 +00:00
|
|
|
if (bootpc_ifctx_isunresolved(ifctx) != 0)
|
|
|
|
ifctx->xid++;
|
2000-10-29 01:19:32 +00:00
|
|
|
ifctx->call.xid = txdr_unsigned(ifctx->xid);
|
|
|
|
bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2001-02-02 02:35:40 +00:00
|
|
|
vendp = ifctx->call.vend;
|
|
|
|
*vendp++ = 99; /* RFC1048 cookie */
|
|
|
|
*vendp++ = 130;
|
|
|
|
*vendp++ = 83;
|
|
|
|
*vendp++ = 99;
|
|
|
|
*vendp++ = TAG_MAXMSGSIZE;
|
|
|
|
*vendp++ = 2;
|
|
|
|
*vendp++ = (sizeof(struct bootp_packet) >> 8) & 255;
|
|
|
|
*vendp++ = sizeof(struct bootp_packet) & 255;
|
2002-05-17 20:18:48 +00:00
|
|
|
|
|
|
|
snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s",
|
|
|
|
ostype, MACHINE, osrelease);
|
|
|
|
vendor_client_len = strlen(vendor_client);
|
|
|
|
*vendp++ = TAG_VENDOR_INDENTIFIER;
|
|
|
|
*vendp++ = vendor_client_len;
|
|
|
|
memcpy(vendp, vendor_client, vendor_client_len);
|
|
|
|
vendp += vendor_client_len;;
|
2001-02-02 02:35:40 +00:00
|
|
|
ifctx->dhcpquerytype = DHCP_NOMSG;
|
|
|
|
switch (ifctx->state) {
|
|
|
|
case IF_DHCP_UNRESOLVED:
|
|
|
|
*vendp++ = TAG_DHCP_MSGTYPE;
|
|
|
|
*vendp++ = 1;
|
|
|
|
*vendp++ = DHCP_DISCOVER;
|
|
|
|
ifctx->dhcpquerytype = DHCP_DISCOVER;
|
|
|
|
ifctx->gotdhcpserver = 0;
|
|
|
|
break;
|
|
|
|
case IF_DHCP_OFFERED:
|
|
|
|
*vendp++ = TAG_DHCP_MSGTYPE;
|
|
|
|
*vendp++ = 1;
|
|
|
|
*vendp++ = DHCP_REQUEST;
|
|
|
|
ifctx->dhcpquerytype = DHCP_REQUEST;
|
|
|
|
*vendp++ = TAG_DHCP_REQ_ADDR;
|
|
|
|
*vendp++ = 4;
|
|
|
|
memcpy(vendp, &ifctx->reply.yiaddr, 4);
|
|
|
|
vendp += 4;
|
|
|
|
if (ifctx->gotdhcpserver != 0) {
|
|
|
|
*vendp++ = TAG_DHCP_SERVERID;
|
|
|
|
*vendp++ = 4;
|
|
|
|
memcpy(vendp, &ifctx->dhcpserver, 4);
|
|
|
|
vendp += 4;
|
|
|
|
}
|
|
|
|
*vendp++ = TAG_DHCP_LEASETIME;
|
|
|
|
*vendp++ = 4;
|
|
|
|
leasetime = htonl(300);
|
|
|
|
memcpy(vendp, &leasetime, 4);
|
|
|
|
vendp += 4;
|
2003-05-31 20:05:25 +00:00
|
|
|
break;
|
2001-02-02 02:35:40 +00:00
|
|
|
default:
|
2003-05-31 20:05:25 +00:00
|
|
|
break;
|
2001-02-02 02:35:40 +00:00
|
|
|
}
|
|
|
|
*vendp = TAG_END;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
ifctx->call.secs = 0;
|
2002-12-30 21:18:15 +00:00
|
|
|
ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */
|
2000-10-29 01:19:32 +00:00
|
|
|
}
|
2000-10-24 22:40:18 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
static int
|
|
|
|
bootpc_hascookie(struct bootp_packet *bp)
|
|
|
|
{
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
return (bp->vend[0] == 99 && bp->vend[1] == 130 &&
|
|
|
|
bp->vend[2] == 83 && bp->vend[3] == 99);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bootpc_tag_helper(struct bootpc_tagcontext *tctx,
|
2001-09-18 23:32:09 +00:00
|
|
|
unsigned char *start, int len, int tag)
|
2000-10-29 01:19:32 +00:00
|
|
|
{
|
|
|
|
unsigned char *j;
|
|
|
|
unsigned char *ej;
|
|
|
|
unsigned char code;
|
|
|
|
|
|
|
|
if (tctx->badtag != 0 || tctx->badopt != 0)
|
|
|
|
return;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
j = start;
|
|
|
|
ej = j + len;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
while (j < ej) {
|
|
|
|
code = *j++;
|
|
|
|
if (code == TAG_PAD)
|
|
|
|
continue;
|
|
|
|
if (code == TAG_END)
|
|
|
|
return;
|
|
|
|
if (j >= ej || j + *j + 1 > ej) {
|
|
|
|
tctx->badopt = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
len = *j++;
|
|
|
|
if (code == tag) {
|
|
|
|
if (tctx->taglen + len > TAG_MAXLEN) {
|
|
|
|
tctx->badtag = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tctx->foundopt = 1;
|
|
|
|
if (len > 0)
|
|
|
|
memcpy(tctx->buf + tctx->taglen,
|
|
|
|
j, len);
|
|
|
|
tctx->taglen += len;
|
|
|
|
}
|
|
|
|
if (code == TAG_OVERLOAD)
|
|
|
|
tctx->overload = *j;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
j += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char *
|
|
|
|
bootpc_tag(struct bootpc_tagcontext *tctx,
|
2001-09-18 23:32:09 +00:00
|
|
|
struct bootp_packet *bp, int len, int tag)
|
2000-10-29 01:19:32 +00:00
|
|
|
{
|
|
|
|
tctx->overload = 0;
|
|
|
|
tctx->badopt = 0;
|
|
|
|
tctx->badtag = 0;
|
|
|
|
tctx->foundopt = 0;
|
|
|
|
tctx->taglen = 0;
|
|
|
|
|
|
|
|
if (bootpc_hascookie(bp) == 0)
|
|
|
|
return NULL;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
bootpc_tag_helper(tctx, &bp->vend[4],
|
|
|
|
(unsigned char *) bp + len - &bp->vend[4], tag);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
if ((tctx->overload & OVERLOAD_FILE) != 0)
|
|
|
|
bootpc_tag_helper(tctx,
|
|
|
|
(unsigned char *) bp->file,
|
|
|
|
sizeof(bp->file),
|
|
|
|
tag);
|
|
|
|
if ((tctx->overload & OVERLOAD_SNAME) != 0)
|
|
|
|
bootpc_tag_helper(tctx,
|
|
|
|
(unsigned char *) bp->sname,
|
|
|
|
sizeof(bp->sname),
|
|
|
|
tag);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0)
|
|
|
|
return NULL;
|
|
|
|
tctx->buf[tctx->taglen] = '\0';
|
|
|
|
return tctx->buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-09-18 23:32:09 +00:00
|
|
|
bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx,
|
|
|
|
struct bootpc_globalcontext *gctx)
|
1997-05-11 18:05:39 +00:00
|
|
|
{
|
2000-10-24 22:40:18 +00:00
|
|
|
char *p;
|
|
|
|
unsigned int ip;
|
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
ifctx->gotgw = 0;
|
|
|
|
ifctx->gotnetmask = 0;
|
1997-05-11 18:05:39 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
clear_sinaddr(&ifctx->myaddr);
|
|
|
|
clear_sinaddr(&ifctx->netmask);
|
|
|
|
clear_sinaddr(&ifctx->gw);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
ifctx->myaddr.sin_addr = ifctx->reply.yiaddr;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
ip = ntohl(ifctx->myaddr.sin_addr.s_addr);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
printf("%s at ", ifctx->ireq.ifr_name);
|
|
|
|
print_sin_addr(&ifctx->myaddr);
|
|
|
|
printf(" server ");
|
|
|
|
print_in_addr(ifctx->reply.siaddr);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
ifctx->gw.sin_addr = ifctx->reply.giaddr;
|
|
|
|
if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) {
|
|
|
|
printf(" via gateway ");
|
|
|
|
print_in_addr(ifctx->reply.giaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This call used for the side effect (overload flag) */
|
|
|
|
(void) bootpc_tag(&gctx->tmptag,
|
|
|
|
&ifctx->reply, ifctx->replylen, TAG_END);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0)
|
|
|
|
if (ifctx->reply.sname[0] != '\0')
|
|
|
|
printf(" server name %s", ifctx->reply.sname);
|
|
|
|
if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0)
|
|
|
|
if (ifctx->reply.file[0] != '\0')
|
|
|
|
printf(" boot file %s", ifctx->reply.file);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
|
|
|
|
TAG_SUBNETMASK);
|
|
|
|
if (p != NULL) {
|
|
|
|
if (gctx->tag.taglen != 4)
|
|
|
|
panic("bootpc: subnet mask len is %d",
|
|
|
|
gctx->tag.taglen);
|
|
|
|
bcopy(p, &ifctx->netmask.sin_addr, 4);
|
|
|
|
ifctx->gotnetmask = 1;
|
|
|
|
printf("subnet mask ");
|
|
|
|
print_sin_addr(&ifctx->netmask);
|
|
|
|
printf(" ");
|
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
|
|
|
|
TAG_ROUTERS);
|
|
|
|
if (p != NULL) {
|
|
|
|
/* Routers */
|
|
|
|
if (gctx->tag.taglen % 4)
|
|
|
|
panic("bootpc: Router Len is %d", gctx->tag.taglen);
|
|
|
|
if (gctx->tag.taglen > 0) {
|
|
|
|
bcopy(p, &ifctx->gw.sin_addr, 4);
|
|
|
|
printf("router ");
|
|
|
|
print_sin_addr(&ifctx->gw);
|
|
|
|
printf(" ");
|
|
|
|
ifctx->gotgw = 1;
|
|
|
|
gctx->gotgw = 1;
|
|
|
|
}
|
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
|
|
|
|
TAG_ROOT);
|
|
|
|
if (p != NULL) {
|
|
|
|
if (gctx->setrootfs != NULL) {
|
|
|
|
printf("rootfs %s (ignored) ", p);
|
|
|
|
} else if (setfs(&nd->root_saddr,
|
2004-03-12 20:37:40 +00:00
|
|
|
nd->root_hostnam, p, &ifctx->reply.siaddr)) {
|
|
|
|
if (*p == '/') {
|
|
|
|
printf("root_server ");
|
|
|
|
print_sin_addr(&nd->root_saddr);
|
|
|
|
printf(" ");
|
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
printf("rootfs %s ", p);
|
2000-10-29 01:19:32 +00:00
|
|
|
gctx->gotrootpath = 1;
|
|
|
|
ifctx->gotrootpath = 1;
|
|
|
|
gctx->setrootfs = ifctx;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
p = bootpc_tag(&gctx->tag, &ifctx->reply,
|
|
|
|
ifctx->replylen,
|
|
|
|
TAG_ROOTOPTS);
|
|
|
|
if (p != NULL) {
|
|
|
|
mountopts(&nd->root_args, p);
|
|
|
|
printf("rootopts %s ", p);
|
|
|
|
}
|
|
|
|
} else
|
2001-09-18 23:32:09 +00:00
|
|
|
panic("Failed to set rootfs to %s", p);
|
2000-10-29 01:19:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
|
|
|
|
TAG_HOSTNAME);
|
|
|
|
if (p != NULL) {
|
|
|
|
if (gctx->tag.taglen >= MAXHOSTNAMELEN)
|
|
|
|
panic("bootpc: hostname >= %d bytes",
|
|
|
|
MAXHOSTNAMELEN);
|
|
|
|
if (gctx->sethostname != NULL) {
|
|
|
|
printf("hostname %s (ignored) ", p);
|
|
|
|
} else {
|
|
|
|
strcpy(nd->my_hostnam, p);
|
2009-05-29 21:27:12 +00:00
|
|
|
mtx_lock(&prison0.pr_mtx);
|
2009-06-13 15:39:12 +00:00
|
|
|
strcpy(prison0.pr_hostname, p);
|
2009-05-29 21:27:12 +00:00
|
|
|
mtx_unlock(&prison0.pr_mtx);
|
|
|
|
printf("hostname %s ", p);
|
2000-10-29 01:19:32 +00:00
|
|
|
gctx->sethostname = ifctx;
|
|
|
|
}
|
|
|
|
}
|
2002-03-13 09:23:11 +00:00
|
|
|
p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen,
|
|
|
|
TAG_COOKIE);
|
|
|
|
if (p != NULL) { /* store in a sysctl variable */
|
|
|
|
int i, l = sizeof(bootp_cookie) - 1;
|
|
|
|
for (i = 0; i < l && p[i] != '\0'; i++)
|
|
|
|
bootp_cookie[i] = p[i];
|
|
|
|
p[i] = '\0';
|
|
|
|
}
|
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
|
|
|
|
printf("\n");
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
if (ifctx->gotnetmask == 0) {
|
|
|
|
if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr)))
|
|
|
|
ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET);
|
|
|
|
else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr)))
|
|
|
|
ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET);
|
|
|
|
else
|
|
|
|
ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET);
|
|
|
|
}
|
|
|
|
if (ifctx->gotgw == 0) {
|
|
|
|
/* Use proxyarp */
|
|
|
|
ifctx->gw.sin_addr.s_addr = ifctx->myaddr.sin_addr.s_addr;
|
|
|
|
}
|
|
|
|
}
|
2000-10-24 22:40:18 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
void
|
|
|
|
bootpc_init(void)
|
|
|
|
{
|
|
|
|
struct bootpc_ifcontext *ifctx, *nctx; /* Interface BOOTP contexts */
|
|
|
|
struct bootpc_globalcontext *gctx; /* Global BOOTP context */
|
|
|
|
struct ifnet *ifp;
|
2004-07-08 22:35:36 +00:00
|
|
|
int error;
|
|
|
|
#ifndef BOOTP_WIRED_TO
|
2003-08-07 21:27:17 +00:00
|
|
|
int ifcnt;
|
2004-07-08 22:35:36 +00:00
|
|
|
#endif
|
2000-10-24 22:40:18 +00:00
|
|
|
struct nfsv3_diskless *nd;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td;
|
2000-10-24 22:40:18 +00:00
|
|
|
|
|
|
|
nd = &nfsv3_diskless;
|
2001-09-12 08:38:13 +00:00
|
|
|
td = curthread;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
/*
|
|
|
|
* If already filled in, don't touch it here
|
|
|
|
*/
|
|
|
|
if (nfs_diskless_valid != 0)
|
|
|
|
return;
|
|
|
|
|
2003-08-07 21:27:17 +00:00
|
|
|
gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO);
|
2000-10-29 01:19:32 +00:00
|
|
|
if (gctx == NULL)
|
|
|
|
panic("Failed to allocate bootp global context structure");
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
gctx->xid = ~0xFFFF;
|
|
|
|
gctx->starttime = time_second;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
/*
|
|
|
|
* Find a network interface.
|
|
|
|
*/
|
2004-07-08 22:35:36 +00:00
|
|
|
#ifdef BOOTP_WIRED_TO
|
|
|
|
printf("bootpc_init: wired to interface '%s'\n",
|
|
|
|
__XSTRING(BOOTP_WIRED_TO));
|
|
|
|
allocifctx(gctx);
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* Preallocate interface context storage, if another interface
|
|
|
|
* attaches and wins the race, it won't be eligible for bootp.
|
|
|
|
*/
|
|
|
|
IFNET_RLOCK();
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
for (ifp = TAILQ_FIRST(&V_ifnet), ifcnt = 0;
|
2004-07-08 22:35:36 +00:00
|
|
|
ifp != NULL;
|
|
|
|
ifp = TAILQ_NEXT(ifp, if_link)) {
|
|
|
|
if ((ifp->if_flags &
|
|
|
|
(IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
|
|
|
|
IFF_BROADCAST)
|
|
|
|
continue;
|
|
|
|
ifcnt++;
|
2004-07-08 13:40:33 +00:00
|
|
|
}
|
2004-07-08 22:35:36 +00:00
|
|
|
IFNET_RUNLOCK();
|
|
|
|
if (ifcnt == 0)
|
|
|
|
panic("bootpc_init: no eligible interfaces");
|
|
|
|
for (; ifcnt > 0; ifcnt--)
|
|
|
|
allocifctx(gctx);
|
|
|
|
#endif
|
2003-08-07 21:27:17 +00:00
|
|
|
|
|
|
|
IFNET_RLOCK();
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
for (ifp = TAILQ_FIRST(&V_ifnet), ifctx = gctx->interfaces;
|
2003-08-07 21:27:17 +00:00
|
|
|
ifp != NULL && ifctx != NULL;
|
2000-10-29 01:19:32 +00:00
|
|
|
ifp = TAILQ_NEXT(ifp, if_link)) {
|
2003-10-31 18:32:15 +00:00
|
|
|
strlcpy(ifctx->ireq.ifr_name, ifp->if_xname,
|
|
|
|
sizeof(ifctx->ireq.ifr_name));
|
2004-07-08 22:35:36 +00:00
|
|
|
#ifdef BOOTP_WIRED_TO
|
|
|
|
if (strcmp(ifctx->ireq.ifr_name,
|
|
|
|
__XSTRING(BOOTP_WIRED_TO)) != 0)
|
|
|
|
continue;
|
|
|
|
#else
|
|
|
|
if ((ifp->if_flags &
|
|
|
|
(IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) !=
|
2000-10-29 01:19:32 +00:00
|
|
|
IFF_BROADCAST)
|
|
|
|
continue;
|
2004-07-08 22:35:36 +00:00
|
|
|
#endif
|
2000-10-29 01:19:32 +00:00
|
|
|
ifctx->ifp = ifp;
|
2003-08-07 21:27:17 +00:00
|
|
|
ifctx = ifctx->next;
|
2000-10-24 22:40:18 +00:00
|
|
|
}
|
2002-12-22 05:35:03 +00:00
|
|
|
IFNET_RUNLOCK();
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2003-08-07 21:27:17 +00:00
|
|
|
if (gctx->interfaces == NULL || gctx->interfaces->ifp == NULL) {
|
2004-07-08 22:35:36 +00:00
|
|
|
#ifdef BOOTP_WIRED_TO
|
|
|
|
panic("bootpc_init: Could not find interface specified "
|
|
|
|
"by BOOTP_WIRED_TO: "
|
|
|
|
__XSTRING(BOOTP_WIRED_TO));
|
|
|
|
#else
|
|
|
|
panic("bootpc_init: no suitable interface");
|
|
|
|
#endif
|
2000-10-29 01:19:32 +00:00
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
|
2001-09-12 08:38:13 +00:00
|
|
|
bootpc_fakeup_interface(ifctx, gctx, td);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
|
2001-09-12 08:38:13 +00:00
|
|
|
bootpc_compose_query(ifctx, gctx, td);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
error = bootpc_call(gctx, td);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
if (error != 0) {
|
2004-07-08 22:35:36 +00:00
|
|
|
#ifdef BOOTP_NFSROOT
|
|
|
|
panic("BOOTP call failed");
|
|
|
|
#else
|
|
|
|
printf("BOOTP call failed\n");
|
|
|
|
#endif
|
1997-05-11 18:05:39 +00:00
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2004-07-28 21:54:57 +00:00
|
|
|
rootdevnames[0] = "nfs:";
|
2000-10-29 01:19:32 +00:00
|
|
|
mountopts(&nd->root_args, NULL);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
|
2001-02-02 02:35:40 +00:00
|
|
|
if (bootpc_ifctx_isresolved(ifctx) != 0)
|
2000-10-29 01:19:32 +00:00
|
|
|
bootpc_decode_reply(nd, ifctx, gctx);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2004-07-08 22:35:36 +00:00
|
|
|
#ifdef BOOTP_NFSROOT
|
|
|
|
if (gctx->gotrootpath == 0)
|
2000-10-24 22:40:18 +00:00
|
|
|
panic("bootpc: No root path offered");
|
2004-07-08 22:35:36 +00:00
|
|
|
#endif
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next) {
|
2001-09-12 08:38:13 +00:00
|
|
|
bootpc_adjust_interface(ifctx, gctx, td);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
soclose(ifctx->so);
|
2000-10-24 22:40:18 +00:00
|
|
|
}
|
1997-05-11 18:05:39 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = ifctx->next)
|
|
|
|
if (ifctx->gotrootpath != 0)
|
|
|
|
break;
|
|
|
|
if (ifctx == NULL) {
|
|
|
|
for (ifctx = gctx->interfaces;
|
|
|
|
ifctx != NULL;
|
|
|
|
ifctx = ifctx->next)
|
2001-02-02 02:35:40 +00:00
|
|
|
if (bootpc_ifctx_isresolved(ifctx) != 0)
|
2000-10-29 01:19:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ifctx == NULL)
|
|
|
|
goto out;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
if (gctx->gotrootpath != 0) {
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2005-04-26 20:45:29 +00:00
|
|
|
setenv("boot.netif.name", ifctx->ifp->if_xname);
|
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
error = md_mount(&nd->root_saddr, nd->root_hostnam,
|
|
|
|
nd->root_fh, &nd->root_fhsize,
|
2001-09-12 08:38:13 +00:00
|
|
|
&nd->root_args, td);
|
2000-10-24 22:40:18 +00:00
|
|
|
if (error != 0)
|
|
|
|
panic("nfs_boot: mountd root, error=%d", error);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
nfs_diskless_valid = 3;
|
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name);
|
|
|
|
bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr));
|
|
|
|
bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr));
|
2000-10-24 22:40:18 +00:00
|
|
|
((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
|
2000-10-29 01:19:32 +00:00
|
|
|
ifctx->myaddr.sin_addr.s_addr |
|
|
|
|
~ ifctx->netmask.sin_addr.s_addr;
|
|
|
|
bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask));
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-29 01:19:32 +00:00
|
|
|
out:
|
|
|
|
for (ifctx = gctx->interfaces; ifctx != NULL; ifctx = nctx) {
|
|
|
|
nctx = ifctx->next;
|
|
|
|
free(ifctx, M_TEMP);
|
|
|
|
}
|
|
|
|
free(gctx, M_TEMP);
|
1997-05-11 18:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RPC: mountd/mount
|
|
|
|
* Given a server pathname, get an NFS file handle.
|
|
|
|
* Also, sets sin->sin_port to the NFS service port.
|
|
|
|
*/
|
|
|
|
static int
|
2001-09-18 23:32:09 +00:00
|
|
|
md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep,
|
|
|
|
struct nfs_args *args, struct thread *td)
|
1997-05-11 18:05:39 +00:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
int error;
|
|
|
|
int authunixok;
|
|
|
|
int authcount;
|
|
|
|
int authver;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2009-06-30 19:03:27 +00:00
|
|
|
#define RPCPROG_MNT 100005
|
|
|
|
#define RPCMNT_VER1 1
|
|
|
|
#define RPCMNT_VER3 3
|
|
|
|
#define RPCMNT_MOUNT 1
|
|
|
|
#define AUTH_SYS 1 /* unix style (uid, gids) */
|
|
|
|
#define AUTH_UNIX AUTH_SYS
|
|
|
|
|
2006-12-06 02:15:25 +00:00
|
|
|
/* XXX honor v2/v3 flags in args->flags? */
|
2004-07-08 22:35:36 +00:00
|
|
|
#ifdef BOOTP_NFSV3
|
|
|
|
/* First try NFS v3 */
|
|
|
|
/* Get port number for MOUNTD. */
|
|
|
|
error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
|
|
|
|
&mdsin->sin_port, td);
|
|
|
|
if (error == 0) {
|
|
|
|
m = xdr_string_encode(path, strlen(path));
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2004-07-08 22:35:36 +00:00
|
|
|
/* Do RPC to mountd. */
|
|
|
|
error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
|
|
|
|
RPCMNT_MOUNT, &m, NULL, td);
|
|
|
|
}
|
|
|
|
if (error == 0) {
|
|
|
|
args->flags |= NFSMNT_NFSV3;
|
|
|
|
} else {
|
|
|
|
#endif
|
2000-10-24 22:40:18 +00:00
|
|
|
/* Fallback to NFS v2 */
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
/* Get port number for MOUNTD. */
|
|
|
|
error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
|
2001-09-12 08:38:13 +00:00
|
|
|
&mdsin->sin_port, td);
|
2000-10-24 22:40:18 +00:00
|
|
|
if (error != 0)
|
|
|
|
return error;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
m = xdr_string_encode(path, strlen(path));
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
/* Do RPC to mountd. */
|
|
|
|
error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
|
2001-09-12 08:38:13 +00:00
|
|
|
RPCMNT_MOUNT, &m, NULL, td);
|
2000-10-24 22:40:18 +00:00
|
|
|
if (error != 0)
|
|
|
|
return error; /* message already freed */
|
2004-07-08 22:35:36 +00:00
|
|
|
|
|
|
|
#ifdef BOOTP_NFSV3
|
1997-05-11 18:05:39 +00:00
|
|
|
}
|
2004-07-08 22:35:36 +00:00
|
|
|
#endif
|
1997-05-11 18:05:39 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
if (xdr_int_decode(&m, &error) != 0 || error != 0)
|
|
|
|
goto bad;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
if ((args->flags & NFSMNT_NFSV3) != 0) {
|
|
|
|
if (xdr_int_decode(&m, fhsizep) != 0 ||
|
|
|
|
*fhsizep > NFSX_V3FHMAX ||
|
|
|
|
*fhsizep <= 0)
|
|
|
|
goto bad;
|
|
|
|
} else
|
|
|
|
*fhsizep = NFSX_V2FH;
|
1997-05-11 18:05:39 +00:00
|
|
|
|
2000-10-24 22:40:18 +00:00
|
|
|
if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
|
|
|
|
goto bad;
|
1997-05-11 18:05:39 +00:00
|
|
|
|
|
|
|
if (args->flags & NFSMNT_NFSV3) {
|
2000-10-24 22:40:18 +00:00
|
|
|
if (xdr_int_decode(&m, &authcount) != 0)
|
|
|
|
goto bad;
|
|
|
|
authunixok = 0;
|
|
|
|
if (authcount < 0 || authcount > 100)
|
|
|
|
goto bad;
|
|
|
|
while (authcount > 0) {
|
|
|
|
if (xdr_int_decode(&m, &authver) != 0)
|
|
|
|
goto bad;
|
2009-06-30 19:03:27 +00:00
|
|
|
if (authver == AUTH_UNIX)
|
2000-10-24 22:40:18 +00:00
|
|
|
authunixok = 1;
|
|
|
|
authcount--;
|
|
|
|
}
|
|
|
|
if (authunixok == 0)
|
|
|
|
goto bad;
|
1997-05-11 18:05:39 +00:00
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
/* Set port number for NFS use. */
|
2000-10-24 22:40:18 +00:00
|
|
|
error = krpc_portmap(mdsin, NFS_PROG,
|
|
|
|
(args->flags &
|
|
|
|
NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
|
2001-09-12 08:38:13 +00:00
|
|
|
&mdsin->sin_port, td);
|
2001-09-18 23:32:09 +00:00
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
goto out;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
bad:
|
|
|
|
error = EBADRPC;
|
2001-09-18 23:32:09 +00:00
|
|
|
|
1997-05-11 18:05:39 +00:00
|
|
|
out:
|
|
|
|
m_freem(m);
|
|
|
|
return error;
|
|
|
|
}
|
2004-07-28 21:54:57 +00:00
|
|
|
|
|
|
|
SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL);
|