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