nfs_bootdhcp.c revision 1.1.2.3 1 1.1.2.3 thorpej /* $NetBSD: nfs_bootdhcp.c,v 1.1.2.3 1997/10/14 15:58:22 thorpej Exp $ */
2 1.1.2.2 thorpej
3 1.1.2.2 thorpej /*-
4 1.1.2.2 thorpej * Copyright (c) 1995, 1997 The NetBSD Foundation, Inc.
5 1.1.2.2 thorpej * All rights reserved.
6 1.1.2.2 thorpej *
7 1.1.2.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.2 thorpej * by Adam Glass and Gordon W. Ross.
9 1.1.2.2 thorpej *
10 1.1.2.2 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1.2.2 thorpej * modification, are permitted provided that the following conditions
12 1.1.2.2 thorpej * are met:
13 1.1.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1.2.2 thorpej * documentation and/or other materials provided with the distribution.
18 1.1.2.2 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.1.2.2 thorpej * must display the following acknowledgement:
20 1.1.2.2 thorpej * This product includes software developed by the NetBSD
21 1.1.2.2 thorpej * Foundation, Inc. and its contributors.
22 1.1.2.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.2.2 thorpej * contributors may be used to endorse or promote products derived
24 1.1.2.2 thorpej * from this software without specific prior written permission.
25 1.1.2.2 thorpej *
26 1.1.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.2.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.2.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.2.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.2.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.2.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.2.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.2.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.2.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.2.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.2.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1.2.2 thorpej */
38 1.1.2.2 thorpej
39 1.1.2.2 thorpej /*
40 1.1.2.2 thorpej * Support for NFS diskless booting with BOOTP (RFC951, RFC1048)
41 1.1.2.2 thorpej *
42 1.1.2.2 thorpej * History:
43 1.1.2.2 thorpej *
44 1.1.2.2 thorpej * Tor Egge developed the initial version of this code based on
45 1.1.2.2 thorpej * the Sun RPC/bootparam sources nfs_boot.c and krpc_subr.c and
46 1.1.2.2 thorpej * submitted that work to NetBSD as bugreport "kern/2351" on
47 1.1.2.2 thorpej * 29 Apr 1996.
48 1.1.2.2 thorpej *
49 1.1.2.2 thorpej * Gordon Ross reorganized Tor's version into this form and
50 1.1.2.2 thorpej * integrated it into the NetBSD sources during Aug 1997.
51 1.1.2.2 thorpej */
52 1.1.2.2 thorpej
53 1.1.2.2 thorpej #include <sys/param.h>
54 1.1.2.2 thorpej #include <sys/systm.h>
55 1.1.2.2 thorpej #include <sys/kernel.h>
56 1.1.2.2 thorpej #include <sys/conf.h>
57 1.1.2.2 thorpej #include <sys/device.h>
58 1.1.2.2 thorpej #include <sys/ioctl.h>
59 1.1.2.2 thorpej #include <sys/proc.h>
60 1.1.2.2 thorpej #include <sys/mount.h>
61 1.1.2.2 thorpej #include <sys/mbuf.h>
62 1.1.2.2 thorpej #include <sys/reboot.h>
63 1.1.2.2 thorpej #include <sys/socket.h>
64 1.1.2.2 thorpej #include <sys/socketvar.h>
65 1.1.2.2 thorpej
66 1.1.2.2 thorpej #include <net/if.h>
67 1.1.2.2 thorpej #include <net/if_arp.h> /* ARPHRD_ETHER, etc. */
68 1.1.2.2 thorpej #include <net/if_dl.h>
69 1.1.2.2 thorpej #include <net/if_ether.h>
70 1.1.2.2 thorpej #include <net/route.h>
71 1.1.2.2 thorpej
72 1.1.2.2 thorpej #include <netinet/in.h>
73 1.1.2.2 thorpej #include <netinet/if_inarp.h>
74 1.1.2.2 thorpej
75 1.1.2.2 thorpej #include <nfs/nfsproto.h>
76 1.1.2.2 thorpej /* #include <nfs/nfs.h> */
77 1.1.2.2 thorpej #include <nfs/nfsdiskless.h>
78 1.1.2.2 thorpej
79 1.1.2.2 thorpej /*
80 1.1.2.2 thorpej * There are two implementations of NFS diskless boot.
81 1.1.2.2 thorpej * This implementation uses BOOTP (RFC951, RFC1048), and
82 1.1.2.2 thorpej * the other uses Sun RPC/bootparams (nfs_bootparam.c).
83 1.1.2.2 thorpej *
84 1.1.2.2 thorpej * This method gets everything it needs with one BOOTP
85 1.1.2.2 thorpej * request and reply. Note that this actually uses only
86 1.1.2.2 thorpej * the old BOOTP functionality subset of DHCP. It is not
87 1.1.2.2 thorpej * clear that DHCP provides any advantage over BOOTP for
88 1.1.2.2 thorpej * diskless boot. DHCP allows the server to assign an IP
89 1.1.2.2 thorpej * address without any a-priori knowledge of the client,
90 1.1.2.2 thorpej * but we require that the server has a-priori knowledge
91 1.1.2.2 thorpej * of the client so it can export our (unique) NFS root.
92 1.1.2.2 thorpej * Given that the server needs a-priori knowledge about
93 1.1.2.2 thorpej * the client anyway, it might as well assign a fixed IP
94 1.1.2.2 thorpej * address for the client and support BOOTP.
95 1.1.2.2 thorpej *
96 1.1.2.2 thorpej * On the other hand, disk-FULL clients may use DHCP, but
97 1.1.2.2 thorpej * in that case the DHCP client should be user-mode code,
98 1.1.2.2 thorpej * and has no bearing on the code below. -gwr
99 1.1.2.2 thorpej */
100 1.1.2.2 thorpej
101 1.1.2.2 thorpej /* Begin stuff from bootp.h */
102 1.1.2.2 thorpej /* Definitions from RFC951 */
103 1.1.2.2 thorpej #define BP_CHADDR_LEN 16
104 1.1.2.2 thorpej #define BP_SNAME_LEN 64
105 1.1.2.2 thorpej #define BP_FILE_LEN 128
106 1.1.2.2 thorpej #define BP_VEND_LEN 64
107 1.1.2.2 thorpej struct bootp {
108 1.1.2.2 thorpej u_int8_t bp_op; /* packet opcode type */
109 1.1.2.2 thorpej u_int8_t bp_htype; /* hardware addr type */
110 1.1.2.2 thorpej u_int8_t bp_hlen; /* hardware addr length */
111 1.1.2.2 thorpej u_int8_t bp_hops; /* gateway hops */
112 1.1.2.2 thorpej u_int32_t bp_xid; /* transaction ID */
113 1.1.2.2 thorpej u_int16_t bp_secs; /* seconds since boot began */
114 1.1.2.2 thorpej u_int16_t bp_flags; /* RFC1532 broadcast, etc. */
115 1.1.2.2 thorpej struct in_addr bp_ciaddr; /* client IP address */
116 1.1.2.2 thorpej struct in_addr bp_yiaddr; /* 'your' IP address */
117 1.1.2.2 thorpej struct in_addr bp_siaddr; /* server IP address */
118 1.1.2.2 thorpej struct in_addr bp_giaddr; /* gateway IP address */
119 1.1.2.2 thorpej u_int8_t bp_chaddr[BP_CHADDR_LEN]; /* client hardware address */
120 1.1.2.2 thorpej char bp_sname[BP_SNAME_LEN]; /* server host name */
121 1.1.2.2 thorpej char bp_file[BP_FILE_LEN]; /* boot file name */
122 1.1.2.2 thorpej u_int8_t bp_vend[BP_VEND_LEN]; /* RFC1048 options */
123 1.1.2.2 thorpej /*
124 1.1.2.2 thorpej * Note that BOOTP packets are allowed to be longer
125 1.1.2.2 thorpej * (see RFC 1532 sect. 2.1) and common practice is to
126 1.1.2.2 thorpej * allow the option data in bp_vend to extend into the
127 1.1.2.2 thorpej * additional space provided in longer packets.
128 1.1.2.2 thorpej */
129 1.1.2.2 thorpej };
130 1.1.2.2 thorpej
131 1.1.2.2 thorpej #define IPPORT_BOOTPS 67
132 1.1.2.2 thorpej #define IPPORT_BOOTPC 68
133 1.1.2.2 thorpej
134 1.1.2.2 thorpej #define BOOTREQUEST 1
135 1.1.2.2 thorpej #define BOOTREPLY 2
136 1.1.2.2 thorpej
137 1.1.2.2 thorpej /*
138 1.1.2.2 thorpej * Is this available from the sockaddr_dl somehow?
139 1.1.2.2 thorpej * Perhaps (struct arphdr)->ar_hrd = ARPHRD_ETHER?
140 1.1.2.2 thorpej * The interface has ->if_type but not the ARP fmt.
141 1.1.2.2 thorpej */
142 1.1.2.2 thorpej #define HTYPE_ETHERNET 1
143 1.1.2.2 thorpej
144 1.1.2.2 thorpej /*
145 1.1.2.2 thorpej * Vendor magic cookie (v_magic) for RFC1048
146 1.1.2.2 thorpej */
147 1.1.2.2 thorpej static const u_int8_t vm_rfc1048[4] = { 99, 130, 83, 99 };
148 1.1.2.2 thorpej
149 1.1.2.2 thorpej /*
150 1.1.2.2 thorpej * Tag values used to specify what information is being supplied in
151 1.1.2.2 thorpej * the vendor (options) data area of the packet.
152 1.1.2.2 thorpej */
153 1.1.2.2 thorpej /* RFC 1048 */
154 1.1.2.2 thorpej #define TAG_END ((unsigned char) 255)
155 1.1.2.2 thorpej #define TAG_PAD ((unsigned char) 0)
156 1.1.2.2 thorpej #define TAG_SUBNET_MASK ((unsigned char) 1)
157 1.1.2.2 thorpej #define TAG_TIME_OFFSET ((unsigned char) 2)
158 1.1.2.2 thorpej #define TAG_GATEWAY ((unsigned char) 3)
159 1.1.2.2 thorpej #define TAG_TIME_SERVER ((unsigned char) 4)
160 1.1.2.2 thorpej #define TAG_NAME_SERVER ((unsigned char) 5)
161 1.1.2.2 thorpej #define TAG_DOMAIN_SERVER ((unsigned char) 6)
162 1.1.2.2 thorpej #define TAG_LOG_SERVER ((unsigned char) 7)
163 1.1.2.2 thorpej #define TAG_COOKIE_SERVER ((unsigned char) 8)
164 1.1.2.2 thorpej #define TAG_LPR_SERVER ((unsigned char) 9)
165 1.1.2.2 thorpej #define TAG_IMPRESS_SERVER ((unsigned char) 10)
166 1.1.2.2 thorpej #define TAG_RLP_SERVER ((unsigned char) 11)
167 1.1.2.2 thorpej #define TAG_HOST_NAME ((unsigned char) 12)
168 1.1.2.2 thorpej #define TAG_BOOT_SIZE ((unsigned char) 13)
169 1.1.2.2 thorpej /* RFC 1395 */
170 1.1.2.2 thorpej #define TAG_DUMP_FILE ((unsigned char) 14)
171 1.1.2.2 thorpej #define TAG_DOMAIN_NAME ((unsigned char) 15)
172 1.1.2.2 thorpej #define TAG_SWAP_SERVER ((unsigned char) 16)
173 1.1.2.2 thorpej #define TAG_ROOT_PATH ((unsigned char) 17)
174 1.1.2.2 thorpej /* End of stuff from bootp.h */
175 1.1.2.2 thorpej
176 1.1.2.3 thorpej #ifdef NFS_BOOT_DHCP
177 1.1.2.3 thorpej #define TAG_REQ_ADDR ((unsigned char) 50)
178 1.1.2.3 thorpej #define TAG_LEASETIME ((unsigned char) 51)
179 1.1.2.3 thorpej #define TAG_OVERLOAD ((unsigned char) 52)
180 1.1.2.3 thorpej #define TAG_DHCP_MSGTYPE ((unsigned char) 53)
181 1.1.2.3 thorpej #define TAG_SERVERID ((unsigned char) 54)
182 1.1.2.3 thorpej #define TAG_PARAM_REQ ((unsigned char) 55)
183 1.1.2.3 thorpej #define TAG_MSG ((unsigned char) 56)
184 1.1.2.3 thorpej #define TAG_MAXSIZE ((unsigned char) 57)
185 1.1.2.3 thorpej #define TAG_T1 ((unsigned char) 58)
186 1.1.2.3 thorpej #define TAG_T2 ((unsigned char) 59)
187 1.1.2.3 thorpej #define TAG_CLASSID ((unsigned char) 60)
188 1.1.2.3 thorpej #define TAG_CLIENTID ((unsigned char) 61)
189 1.1.2.3 thorpej #endif
190 1.1.2.3 thorpej
191 1.1.2.3 thorpej #ifdef NFS_BOOT_DHCP
192 1.1.2.3 thorpej #define DHCPDISCOVER 1
193 1.1.2.3 thorpej #define DHCPOFFER 2
194 1.1.2.3 thorpej #define DHCPREQUEST 3
195 1.1.2.3 thorpej #define DHCPDECLINE 4
196 1.1.2.3 thorpej #define DHCPACK 5
197 1.1.2.3 thorpej #define DHCPNAK 6
198 1.1.2.3 thorpej #define DHCPRELEASE 7
199 1.1.2.3 thorpej #endif
200 1.1.2.2 thorpej
201 1.1.2.3 thorpej #ifdef NFS_BOOT_DHCP
202 1.1.2.3 thorpej #define BOOTP_SIZE_MAX (sizeof(struct bootp)+312-64)
203 1.1.2.3 thorpej #else
204 1.1.2.2 thorpej /*
205 1.1.2.2 thorpej * The "extended" size is somewhat arbitrary, but is
206 1.1.2.2 thorpej * constrained by the maximum message size specified
207 1.1.2.2 thorpej * by RFC1533 (567 total). This value increases the
208 1.1.2.2 thorpej * space for options from 64 bytes to 256 bytes.
209 1.1.2.2 thorpej */
210 1.1.2.3 thorpej #define BOOTP_SIZE_MAX (sizeof(struct bootp)+256-64)
211 1.1.2.3 thorpej #endif
212 1.1.2.2 thorpej #define BOOTP_SIZE_MIN (sizeof(struct bootp))
213 1.1.2.2 thorpej
214 1.1.2.2 thorpej /* Convenience macro */
215 1.1.2.2 thorpej #define INTOHL(ina) ((u_int32_t)ntohl((ina).s_addr))
216 1.1.2.2 thorpej
217 1.1.2.2 thorpej static int bootpc_call __P((struct socket *, struct ifnet *,
218 1.1.2.2 thorpej struct nfs_diskless *, struct proc *));
219 1.1.2.3 thorpej static void bootp_extract __P((struct bootp *, int, struct nfs_diskless *));
220 1.1.2.2 thorpej
221 1.1.2.2 thorpej /* #define DEBUG XXX */
222 1.1.2.2 thorpej
223 1.1.2.2 thorpej #ifdef DEBUG
224 1.1.2.2 thorpej #define DPRINT(s) printf("nfs_boot: %s\n", s)
225 1.1.2.2 thorpej #else
226 1.1.2.2 thorpej #define DPRINT(s) (void)0
227 1.1.2.2 thorpej #endif
228 1.1.2.2 thorpej
229 1.1.2.2 thorpej
230 1.1.2.2 thorpej /*
231 1.1.2.2 thorpej * Get our boot parameters using BOOTP.
232 1.1.2.2 thorpej */
233 1.1.2.2 thorpej int
234 1.1.2.2 thorpej nfs_bootdhcp(ifp, nd, procp)
235 1.1.2.2 thorpej struct ifnet *ifp;
236 1.1.2.2 thorpej struct nfs_diskless *nd;
237 1.1.2.2 thorpej struct proc *procp;
238 1.1.2.2 thorpej {
239 1.1.2.2 thorpej struct ifaliasreq iareq;
240 1.1.2.2 thorpej struct socket *so;
241 1.1.2.2 thorpej struct sockaddr_in *sin;
242 1.1.2.2 thorpej int error;
243 1.1.2.2 thorpej
244 1.1.2.2 thorpej /*
245 1.1.2.2 thorpej * Get a socket to use for various things in here.
246 1.1.2.2 thorpej * After this, use "goto out" to cleanup and return.
247 1.1.2.2 thorpej */
248 1.1.2.2 thorpej error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
249 1.1.2.2 thorpej if (error) {
250 1.1.2.2 thorpej printf("nfs_boot: socreate, error=%d\n", error);
251 1.1.2.2 thorpej return (error);
252 1.1.2.2 thorpej }
253 1.1.2.2 thorpej
254 1.1.2.2 thorpej /*
255 1.1.2.2 thorpej * Do enough of ifconfig(8) so that the chosen interface
256 1.1.2.2 thorpej * can talk to the servers. Use address zero for now.
257 1.1.2.2 thorpej */
258 1.1.2.2 thorpej bzero(&iareq, sizeof(iareq));
259 1.1.2.2 thorpej bcopy(ifp->if_xname, iareq.ifra_name, IFNAMSIZ);
260 1.1.2.2 thorpej /* Set the I/F address */
261 1.1.2.2 thorpej sin = (struct sockaddr_in *)&iareq.ifra_addr;
262 1.1.2.2 thorpej sin->sin_len = sizeof(*sin);
263 1.1.2.2 thorpej sin->sin_family = AF_INET;
264 1.1.2.2 thorpej sin->sin_addr.s_addr = INADDR_ANY;
265 1.1.2.2 thorpej /* Leave subnetmask unspecified (len=0) */
266 1.1.2.2 thorpej /* Set the broadcast addr. */
267 1.1.2.2 thorpej sin = (struct sockaddr_in *)&iareq.ifra_broadaddr;
268 1.1.2.2 thorpej sin->sin_len = sizeof(*sin);
269 1.1.2.2 thorpej sin->sin_family = AF_INET;
270 1.1.2.2 thorpej sin->sin_addr.s_addr = INADDR_BROADCAST;
271 1.1.2.2 thorpej error = ifioctl(so, SIOCAIFADDR, (caddr_t)&iareq, procp);
272 1.1.2.2 thorpej if (error) {
273 1.1.2.2 thorpej printf("nfs_boot: set ifaddr zero, error=%d\n", error);
274 1.1.2.2 thorpej goto out;
275 1.1.2.2 thorpej }
276 1.1.2.2 thorpej
277 1.1.2.2 thorpej /* This function call does the real send/recv work. */
278 1.1.2.2 thorpej error = bootpc_call(so, ifp, nd, procp);
279 1.1.2.2 thorpej /* Get rid of the temporary (zero) IP address. */
280 1.1.2.3 thorpej /*
281 1.1.2.3 thorpej * XXX SIOCDIFADDR takes a "struct ifreq", which is
282 1.1.2.3 thorpej * an exact subset of "struct ifaliasreq".
283 1.1.2.3 thorpej */
284 1.1.2.2 thorpej (void) ifioctl(so, SIOCDIFADDR, (caddr_t)&iareq, procp);
285 1.1.2.2 thorpej /* NOW we can test the error from bootpc_call. */
286 1.1.2.2 thorpej if (error)
287 1.1.2.2 thorpej goto out;
288 1.1.2.2 thorpej
289 1.1.2.2 thorpej /*
290 1.1.2.2 thorpej * Do ifconfig with our real IP address and mask.
291 1.1.2.2 thorpej */
292 1.1.2.2 thorpej /* I/F address */
293 1.1.2.2 thorpej sin = (struct sockaddr_in *)&iareq.ifra_addr;
294 1.1.2.2 thorpej sin->sin_addr = nd->nd_myip;
295 1.1.2.2 thorpej /* subnetmask */
296 1.1.2.2 thorpej if (nd->nd_mask.s_addr) {
297 1.1.2.2 thorpej sin = (struct sockaddr_in *)&iareq.ifra_mask;
298 1.1.2.2 thorpej sin->sin_len = sizeof(*sin);
299 1.1.2.2 thorpej sin->sin_family = AF_INET;
300 1.1.2.2 thorpej sin->sin_addr = nd->nd_mask;
301 1.1.2.2 thorpej }
302 1.1.2.2 thorpej /* Let ifioctl() default the broadcast address. */
303 1.1.2.2 thorpej sin = (struct sockaddr_in *)&iareq.ifra_broadaddr;
304 1.1.2.2 thorpej sin->sin_len = 0;
305 1.1.2.2 thorpej sin->sin_family = 0;
306 1.1.2.2 thorpej sin->sin_addr.s_addr = 0;
307 1.1.2.2 thorpej error = ifioctl(so, SIOCAIFADDR, (caddr_t)&iareq, procp);
308 1.1.2.2 thorpej if (error) {
309 1.1.2.2 thorpej printf("nfs_boot: set ifaddr real, error=%d\n", error);
310 1.1.2.2 thorpej goto out;
311 1.1.2.2 thorpej }
312 1.1.2.2 thorpej
313 1.1.2.2 thorpej out:
314 1.1.2.2 thorpej soclose(so);
315 1.1.2.2 thorpej return (error);
316 1.1.2.2 thorpej }
317 1.1.2.2 thorpej
318 1.1.2.3 thorpej struct bootpcontext {
319 1.1.2.3 thorpej int xid;
320 1.1.2.3 thorpej u_char *haddr;
321 1.1.2.3 thorpej u_char halen;
322 1.1.2.3 thorpej struct bootp *replybuf;
323 1.1.2.3 thorpej int replylen;
324 1.1.2.3 thorpej #ifdef NFS_BOOT_DHCP
325 1.1.2.3 thorpej char expected_dhcpmsgtype, dhcp_ok;
326 1.1.2.3 thorpej struct in_addr dhcp_serverip;
327 1.1.2.3 thorpej #endif
328 1.1.2.3 thorpej };
329 1.1.2.3 thorpej
330 1.1.2.3 thorpej static int bootpset __P((struct mbuf*, void*, int));
331 1.1.2.3 thorpej static int bootpcheck __P((struct mbuf*, void*));
332 1.1.2.3 thorpej
333 1.1.2.3 thorpej static int bootpset(m, context, waited)
334 1.1.2.3 thorpej struct mbuf *m;
335 1.1.2.3 thorpej void *context;
336 1.1.2.3 thorpej int waited;
337 1.1.2.3 thorpej {
338 1.1.2.3 thorpej struct bootp *bootp;
339 1.1.2.3 thorpej
340 1.1.2.3 thorpej /* we know it's contigous (in 1 mbuf cluster) */
341 1.1.2.3 thorpej bootp = mtod(m, struct bootp*);
342 1.1.2.3 thorpej
343 1.1.2.3 thorpej bootp->bp_secs = htons(waited);
344 1.1.2.3 thorpej
345 1.1.2.3 thorpej return(0);
346 1.1.2.3 thorpej }
347 1.1.2.3 thorpej
348 1.1.2.3 thorpej static int bootpcheck(m, context)
349 1.1.2.3 thorpej struct mbuf *m;
350 1.1.2.3 thorpej void *context;
351 1.1.2.3 thorpej {
352 1.1.2.3 thorpej struct bootp *bootp;
353 1.1.2.3 thorpej struct bootpcontext *bpc = context;
354 1.1.2.3 thorpej u_int tag, len;
355 1.1.2.3 thorpej u_char *p, *limit;
356 1.1.2.3 thorpej
357 1.1.2.3 thorpej /*
358 1.1.2.3 thorpej * Is this a valid reply?
359 1.1.2.3 thorpej */
360 1.1.2.3 thorpej if (m->m_pkthdr.len < BOOTP_SIZE_MIN) {
361 1.1.2.3 thorpej DPRINT("short packet");
362 1.1.2.3 thorpej return(-1);
363 1.1.2.3 thorpej }
364 1.1.2.3 thorpej if (m->m_pkthdr.len > BOOTP_SIZE_MAX) {
365 1.1.2.3 thorpej DPRINT("long packet");
366 1.1.2.3 thorpej return(-1);
367 1.1.2.3 thorpej }
368 1.1.2.3 thorpej
369 1.1.2.3 thorpej /*
370 1.1.2.3 thorpej * don't make first checks more expensive than necessary
371 1.1.2.3 thorpej */
372 1.1.2.3 thorpej #define ofs(what, elem) ((int)&(((what *)0)->elem))
373 1.1.2.3 thorpej if (m->m_len < ofs(struct bootp, bp_secs)) {
374 1.1.2.3 thorpej m = m_pullup(m, ofs(struct bootp, bp_secs));
375 1.1.2.3 thorpej if (m == NULL)
376 1.1.2.3 thorpej return(-1);
377 1.1.2.3 thorpej }
378 1.1.2.3 thorpej #undef ofs
379 1.1.2.3 thorpej bootp = mtod(m, struct bootp*);
380 1.1.2.3 thorpej
381 1.1.2.3 thorpej if (bootp->bp_op != BOOTREPLY) {
382 1.1.2.3 thorpej DPRINT("not reply");
383 1.1.2.3 thorpej return(-1);
384 1.1.2.3 thorpej }
385 1.1.2.3 thorpej if (bootp->bp_hlen != bpc->halen) {
386 1.1.2.3 thorpej DPRINT("bad hwa_len");
387 1.1.2.3 thorpej return(-1);
388 1.1.2.3 thorpej }
389 1.1.2.3 thorpej if (bcmp(bootp->bp_chaddr, bpc->haddr, bpc->halen)) {
390 1.1.2.3 thorpej DPRINT("wrong hwaddr");
391 1.1.2.3 thorpej return(-1);
392 1.1.2.3 thorpej }
393 1.1.2.3 thorpej if (bootp->bp_xid != bpc->xid) {
394 1.1.2.3 thorpej DPRINT("wrong xid");
395 1.1.2.3 thorpej return(-1);
396 1.1.2.3 thorpej }
397 1.1.2.3 thorpej
398 1.1.2.3 thorpej /*
399 1.1.2.3 thorpej * OK, it's worth to look deeper.
400 1.1.2.3 thorpej * We copy the mbuf into a flat buffer here because
401 1.1.2.3 thorpej * m_pullup() is a bit limited for this purpose
402 1.1.2.3 thorpej * (doesn't allocate a cluster if necessary).
403 1.1.2.3 thorpej */
404 1.1.2.3 thorpej bpc->replylen = m->m_pkthdr.len;
405 1.1.2.3 thorpej m_copydata(m, 0, bpc->replylen, (caddr_t)bpc->replybuf);
406 1.1.2.3 thorpej bootp = bpc->replybuf;
407 1.1.2.3 thorpej
408 1.1.2.3 thorpej /*
409 1.1.2.3 thorpej * Check the vendor data.
410 1.1.2.3 thorpej */
411 1.1.2.3 thorpej if (bcmp(bootp->bp_vend, vm_rfc1048, 4)) {
412 1.1.2.3 thorpej printf("nfs_boot: reply missing options\n");
413 1.1.2.3 thorpej return(-1);
414 1.1.2.3 thorpej }
415 1.1.2.3 thorpej p = &bootp->bp_vend[4];
416 1.1.2.3 thorpej limit = ((char*)bootp) + bpc->replylen;
417 1.1.2.3 thorpej while (p < limit) {
418 1.1.2.3 thorpej tag = *p++;
419 1.1.2.3 thorpej if (tag == TAG_END)
420 1.1.2.3 thorpej break;
421 1.1.2.3 thorpej if (tag == TAG_PAD)
422 1.1.2.3 thorpej continue;
423 1.1.2.3 thorpej len = *p++;
424 1.1.2.3 thorpej if ((p + len) > limit) {
425 1.1.2.3 thorpej printf("nfs_boot: option %d too long\n", tag);
426 1.1.2.3 thorpej return(-1);
427 1.1.2.3 thorpej }
428 1.1.2.3 thorpej switch (tag) {
429 1.1.2.3 thorpej #ifdef NFS_BOOT_DHCP
430 1.1.2.3 thorpej case TAG_DHCP_MSGTYPE:
431 1.1.2.3 thorpej if (*p != bpc->expected_dhcpmsgtype)
432 1.1.2.3 thorpej return(-1);
433 1.1.2.3 thorpej bpc->dhcp_ok = 1;
434 1.1.2.3 thorpej break;
435 1.1.2.3 thorpej case TAG_SERVERID:
436 1.1.2.3 thorpej bcopy(p, &bpc->dhcp_serverip.s_addr,
437 1.1.2.3 thorpej sizeof(bpc->dhcp_serverip.s_addr));
438 1.1.2.3 thorpej break;
439 1.1.2.3 thorpej #endif
440 1.1.2.3 thorpej default:
441 1.1.2.3 thorpej break;
442 1.1.2.3 thorpej }
443 1.1.2.3 thorpej p += len;
444 1.1.2.3 thorpej }
445 1.1.2.3 thorpej return(0);
446 1.1.2.3 thorpej }
447 1.1.2.3 thorpej
448 1.1.2.2 thorpej static int
449 1.1.2.2 thorpej bootpc_call(so, ifp, nd, procp)
450 1.1.2.2 thorpej struct socket *so;
451 1.1.2.2 thorpej struct ifnet *ifp;
452 1.1.2.2 thorpej struct nfs_diskless *nd;
453 1.1.2.2 thorpej struct proc *procp;
454 1.1.2.2 thorpej {
455 1.1.2.2 thorpej static u_int32_t xid = ~0xFF;
456 1.1.2.3 thorpej struct bootp *bootp; /* request */
457 1.1.2.2 thorpej struct mbuf *m, *nam;
458 1.1.2.2 thorpej struct sockaddr_in *sin;
459 1.1.2.3 thorpej int error;
460 1.1.2.2 thorpej u_char *haddr;
461 1.1.2.2 thorpej u_char hafmt, halen;
462 1.1.2.3 thorpej struct bootpcontext bpc;
463 1.1.2.2 thorpej
464 1.1.2.2 thorpej /*
465 1.1.2.2 thorpej * Initialize to NULL anything that will hold an allocation,
466 1.1.2.2 thorpej * and free each at the end if not null.
467 1.1.2.2 thorpej */
468 1.1.2.3 thorpej bpc.replybuf = NULL;
469 1.1.2.2 thorpej m = nam = NULL;
470 1.1.2.2 thorpej
471 1.1.2.2 thorpej /* Record our H/W (Ethernet) address. */
472 1.1.2.2 thorpej { struct sockaddr_dl *sdl = ifp->if_sadl;
473 1.1.2.2 thorpej hafmt = HTYPE_ETHERNET; /* XXX: sdl->sdl_type? */
474 1.1.2.2 thorpej halen = sdl->sdl_alen;
475 1.1.2.2 thorpej haddr = (unsigned char *)LLADDR(sdl);
476 1.1.2.2 thorpej }
477 1.1.2.2 thorpej
478 1.1.2.2 thorpej /*
479 1.1.2.2 thorpej * Skip the route table when sending on this socket.
480 1.1.2.2 thorpej * If this is not done, ip_output finds the loopback
481 1.1.2.2 thorpej * interface (why?) and then fails because broadcast
482 1.1.2.2 thorpej * is not supported on that interface...
483 1.1.2.2 thorpej */
484 1.1.2.2 thorpej { int32_t *opt;
485 1.1.2.2 thorpej m = m_get(M_WAIT, MT_SOOPTS);
486 1.1.2.2 thorpej opt = mtod(m, int32_t *);
487 1.1.2.2 thorpej m->m_len = sizeof(*opt);
488 1.1.2.2 thorpej *opt = 1;
489 1.1.2.2 thorpej error = sosetopt(so, SOL_SOCKET, SO_DONTROUTE, m);
490 1.1.2.2 thorpej m = NULL; /* was consumed */
491 1.1.2.2 thorpej }
492 1.1.2.2 thorpej if (error) {
493 1.1.2.2 thorpej DPRINT("SO_DONTROUTE");
494 1.1.2.2 thorpej goto out;
495 1.1.2.2 thorpej }
496 1.1.2.2 thorpej
497 1.1.2.2 thorpej /* Enable broadcast. */
498 1.1.2.3 thorpej if ((error = nfs_boot_enbroadcast(so))) {
499 1.1.2.2 thorpej DPRINT("SO_BROADCAST");
500 1.1.2.2 thorpej goto out;
501 1.1.2.2 thorpej }
502 1.1.2.2 thorpej
503 1.1.2.2 thorpej /* Set the receive timeout for the socket. */
504 1.1.2.3 thorpej if ((error = nfs_boot_setrecvtimo(so))) {
505 1.1.2.2 thorpej DPRINT("SO_RCVTIMEO");
506 1.1.2.2 thorpej goto out;
507 1.1.2.2 thorpej }
508 1.1.2.2 thorpej
509 1.1.2.2 thorpej /*
510 1.1.2.2 thorpej * Bind the local endpoint to a bootp client port.
511 1.1.2.2 thorpej */
512 1.1.2.3 thorpej if ((error = nfs_boot_sobind_ipport(so, IPPORT_BOOTPC))) {
513 1.1.2.2 thorpej DPRINT("bind failed\n");
514 1.1.2.2 thorpej goto out;
515 1.1.2.2 thorpej }
516 1.1.2.2 thorpej
517 1.1.2.2 thorpej /*
518 1.1.2.2 thorpej * Setup socket address for the server.
519 1.1.2.2 thorpej */
520 1.1.2.2 thorpej nam = m_get(M_WAIT, MT_SONAME);
521 1.1.2.2 thorpej sin = mtod(nam, struct sockaddr_in *);
522 1.1.2.2 thorpej sin->sin_len = nam->m_len = sizeof(*sin);
523 1.1.2.2 thorpej sin->sin_family = AF_INET;
524 1.1.2.2 thorpej sin->sin_addr.s_addr = INADDR_BROADCAST;
525 1.1.2.2 thorpej sin->sin_port = htons(IPPORT_BOOTPS);
526 1.1.2.2 thorpej
527 1.1.2.2 thorpej /*
528 1.1.2.3 thorpej * Allocate buffer used for request
529 1.1.2.2 thorpej */
530 1.1.2.3 thorpej m = m_gethdr(M_WAIT, MT_DATA);
531 1.1.2.3 thorpej MCLGET(m, M_WAIT);
532 1.1.2.3 thorpej bootp = mtod(m, struct bootp*);
533 1.1.2.3 thorpej m->m_pkthdr.len = m->m_len = BOOTP_SIZE_MAX;
534 1.1.2.3 thorpej m->m_pkthdr.rcvif = NULL;
535 1.1.2.2 thorpej
536 1.1.2.2 thorpej /*
537 1.1.2.3 thorpej * Build the BOOTP reqest message.
538 1.1.2.2 thorpej * Note: xid is host order! (opaque to server)
539 1.1.2.2 thorpej */
540 1.1.2.2 thorpej bzero((caddr_t)bootp, BOOTP_SIZE_MAX);
541 1.1.2.2 thorpej bootp->bp_op = BOOTREQUEST;
542 1.1.2.2 thorpej bootp->bp_htype = hafmt;
543 1.1.2.2 thorpej bootp->bp_hlen = halen; /* Hardware address length */
544 1.1.2.2 thorpej bootp->bp_xid = ++xid;
545 1.1.2.2 thorpej bcopy(haddr, bootp->bp_chaddr, halen);
546 1.1.2.2 thorpej /* Fill-in the vendor data. */
547 1.1.2.2 thorpej bcopy(vm_rfc1048, bootp->bp_vend, 4);
548 1.1.2.3 thorpej #ifdef NFS_BOOT_DHCP
549 1.1.2.3 thorpej bootp->bp_vend[4] = TAG_DHCP_MSGTYPE;
550 1.1.2.3 thorpej bootp->bp_vend[5] = 1;
551 1.1.2.3 thorpej bootp->bp_vend[6] = DHCPDISCOVER;
552 1.1.2.3 thorpej bootp->bp_vend[7] = TAG_END;
553 1.1.2.3 thorpej #else
554 1.1.2.2 thorpej bootp->bp_vend[4] = TAG_END;
555 1.1.2.3 thorpej #endif
556 1.1.2.2 thorpej
557 1.1.2.3 thorpej bpc.xid = xid;
558 1.1.2.3 thorpej bpc.haddr = haddr;
559 1.1.2.3 thorpej bpc.halen = halen;
560 1.1.2.3 thorpej bpc.replybuf = malloc(BOOTP_SIZE_MAX, M_DEVBUF, M_WAITOK);
561 1.1.2.3 thorpej if (bpc.replybuf == NULL)
562 1.1.2.3 thorpej panic("nfs_boot: malloc reply buf");
563 1.1.2.3 thorpej #ifdef NFS_BOOT_DHCP
564 1.1.2.3 thorpej bpc.expected_dhcpmsgtype = DHCPOFFER;
565 1.1.2.3 thorpej bpc.dhcp_ok = 0;
566 1.1.2.3 thorpej #endif
567 1.1.2.3 thorpej
568 1.1.2.3 thorpej error = nfs_boot_sendrecv(so, nam, bootpset, m,
569 1.1.2.3 thorpej bootpcheck, 0, 0, &bpc);
570 1.1.2.3 thorpej if (error)
571 1.1.2.2 thorpej goto out;
572 1.1.2.3 thorpej
573 1.1.2.3 thorpej #ifdef NFS_BOOT_DHCP
574 1.1.2.3 thorpej if (bpc.dhcp_ok) {
575 1.1.2.3 thorpej u_int32_t leasetime;
576 1.1.2.3 thorpej bootp->bp_vend[6] = DHCPREQUEST;
577 1.1.2.3 thorpej bootp->bp_vend[7] = TAG_REQ_ADDR;
578 1.1.2.3 thorpej bootp->bp_vend[8] = 4;
579 1.1.2.3 thorpej bcopy(&bpc.replybuf->bp_yiaddr, &bootp->bp_vend[9], 4);
580 1.1.2.3 thorpej bootp->bp_vend[13] = TAG_SERVERID;
581 1.1.2.3 thorpej bootp->bp_vend[14] = 4;
582 1.1.2.3 thorpej bcopy(&bpc.dhcp_serverip.s_addr, &bootp->bp_vend[15], 4);
583 1.1.2.3 thorpej bootp->bp_vend[19] = TAG_LEASETIME;
584 1.1.2.3 thorpej bootp->bp_vend[20] = 4;
585 1.1.2.3 thorpej leasetime = htonl(300);
586 1.1.2.3 thorpej bcopy(&leasetime, &bootp->bp_vend[21], 4);
587 1.1.2.3 thorpej bootp->bp_vend[25] = TAG_END;
588 1.1.2.3 thorpej
589 1.1.2.3 thorpej bpc.expected_dhcpmsgtype = DHCPACK;
590 1.1.2.3 thorpej
591 1.1.2.3 thorpej error = nfs_boot_sendrecv(so, nam, bootpset, m,
592 1.1.2.3 thorpej bootpcheck, 0, 0, &bpc);
593 1.1.2.3 thorpej if (error)
594 1.1.2.3 thorpej goto out;
595 1.1.2.2 thorpej }
596 1.1.2.3 thorpej #endif
597 1.1.2.2 thorpej
598 1.1.2.2 thorpej /*
599 1.1.2.3 thorpej * bootpcheck() has copied the receive mbuf into
600 1.1.2.3 thorpej * the buffer at bpc.replybuf.
601 1.1.2.2 thorpej */
602 1.1.2.3 thorpej bootp_extract(bpc.replybuf, bpc.replylen, nd);
603 1.1.2.2 thorpej
604 1.1.2.3 thorpej out:
605 1.1.2.3 thorpej if (bpc.replybuf)
606 1.1.2.3 thorpej free(bpc.replybuf, M_DEVBUF);
607 1.1.2.3 thorpej if (m)
608 1.1.2.3 thorpej m_freem(m);
609 1.1.2.3 thorpej if (nam)
610 1.1.2.3 thorpej m_freem(nam);
611 1.1.2.3 thorpej return (error);
612 1.1.2.3 thorpej }
613 1.1.2.3 thorpej
614 1.1.2.3 thorpej static void bootp_extract(bootp, replylen, nd)
615 1.1.2.3 thorpej struct bootp *bootp;
616 1.1.2.3 thorpej int replylen;
617 1.1.2.3 thorpej struct nfs_diskless *nd;
618 1.1.2.3 thorpej {
619 1.1.2.3 thorpej struct sockaddr_in *sin;
620 1.1.2.3 thorpej struct in_addr netmask;
621 1.1.2.3 thorpej struct in_addr gateway;
622 1.1.2.3 thorpej struct in_addr rootserver;
623 1.1.2.3 thorpej char *myname; /* my hostname */
624 1.1.2.3 thorpej char *mydomain; /* my domainname */
625 1.1.2.3 thorpej char *rootpath;
626 1.1.2.3 thorpej int mynamelen;
627 1.1.2.3 thorpej int mydomainlen;
628 1.1.2.3 thorpej int rootpathlen;
629 1.1.2.3 thorpej u_int tag, len;
630 1.1.2.3 thorpej u_char *p, *limit;
631 1.1.2.2 thorpej
632 1.1.2.3 thorpej /* Default these to "unspecified". */
633 1.1.2.3 thorpej netmask.s_addr = 0;
634 1.1.2.3 thorpej gateway.s_addr = 0;
635 1.1.2.3 thorpej mydomain = myname = rootpath = NULL;
636 1.1.2.3 thorpej mydomainlen = mynamelen = rootpathlen = 0;
637 1.1.2.3 thorpej /* default root server to bootp next-server */
638 1.1.2.3 thorpej rootserver = bootp->bp_siaddr;
639 1.1.2.3 thorpej
640 1.1.2.3 thorpej p = &bootp->bp_vend[4];
641 1.1.2.3 thorpej limit = ((char*)bootp) + replylen;
642 1.1.2.3 thorpej while (p < limit) {
643 1.1.2.3 thorpej tag = *p++;
644 1.1.2.3 thorpej if (tag == TAG_END)
645 1.1.2.3 thorpej break;
646 1.1.2.3 thorpej if (tag == TAG_PAD)
647 1.1.2.2 thorpej continue;
648 1.1.2.3 thorpej len = *p++;
649 1.1.2.3 thorpej if ((p + len) > limit) {
650 1.1.2.3 thorpej printf("nfs_boot: option %d too long\n", tag);
651 1.1.2.3 thorpej break;
652 1.1.2.3 thorpej }
653 1.1.2.3 thorpej switch (tag) {
654 1.1.2.3 thorpej case TAG_SUBNET_MASK:
655 1.1.2.3 thorpej bcopy(p, &netmask, 4);
656 1.1.2.3 thorpej break;
657 1.1.2.3 thorpej case TAG_GATEWAY:
658 1.1.2.3 thorpej /* Routers */
659 1.1.2.3 thorpej bcopy(p, &gateway, 4);
660 1.1.2.3 thorpej break;
661 1.1.2.3 thorpej case TAG_HOST_NAME:
662 1.1.2.3 thorpej if (len >= sizeof(hostname)) {
663 1.1.2.3 thorpej printf("nfs_boot: host name >=%d bytes",
664 1.1.2.3 thorpej sizeof(hostname));
665 1.1.2.2 thorpej break;
666 1.1.2.2 thorpej }
667 1.1.2.3 thorpej myname = p;
668 1.1.2.3 thorpej mynamelen = len;
669 1.1.2.3 thorpej break;
670 1.1.2.3 thorpej case TAG_DOMAIN_NAME:
671 1.1.2.3 thorpej if (len >= sizeof(domainname)) {
672 1.1.2.3 thorpej printf("nfs_boot: domain name >=%d bytes",
673 1.1.2.3 thorpej sizeof(domainname));
674 1.1.2.2 thorpej break;
675 1.1.2.3 thorpej }
676 1.1.2.3 thorpej mydomain = p;
677 1.1.2.3 thorpej mydomainlen = len;
678 1.1.2.3 thorpej break;
679 1.1.2.3 thorpej case TAG_ROOT_PATH:
680 1.1.2.3 thorpej /* Leave some room for the server name. */
681 1.1.2.3 thorpej if (len >= (MNAMELEN-10)) {
682 1.1.2.3 thorpej printf("nfs_boot: rootpath >=%d bytes",
683 1.1.2.3 thorpej (MNAMELEN-10));
684 1.1.2.2 thorpej break;
685 1.1.2.2 thorpej }
686 1.1.2.3 thorpej rootpath = p;
687 1.1.2.3 thorpej rootpathlen = len;
688 1.1.2.3 thorpej break;
689 1.1.2.3 thorpej case TAG_SWAP_SERVER:
690 1.1.2.3 thorpej /* override NFS server address */
691 1.1.2.3 thorpej bcopy(p, &rootserver, 4);
692 1.1.2.3 thorpej break;
693 1.1.2.3 thorpej default:
694 1.1.2.3 thorpej break;
695 1.1.2.2 thorpej }
696 1.1.2.3 thorpej p += len;
697 1.1.2.2 thorpej }
698 1.1.2.2 thorpej
699 1.1.2.2 thorpej /*
700 1.1.2.2 thorpej * Store and print network config info.
701 1.1.2.2 thorpej */
702 1.1.2.2 thorpej if (myname) {
703 1.1.2.2 thorpej myname[mynamelen] = '\0';
704 1.1.2.2 thorpej strncpy(hostname, myname, sizeof(hostname));
705 1.1.2.2 thorpej hostnamelen = mynamelen;
706 1.1.2.2 thorpej printf("nfs_boot: my_name=%s\n", hostname);
707 1.1.2.2 thorpej }
708 1.1.2.2 thorpej if (mydomain) {
709 1.1.2.2 thorpej mydomain[mydomainlen] = '\0';
710 1.1.2.2 thorpej strncpy(domainname, mydomain, sizeof(domainname));
711 1.1.2.2 thorpej domainnamelen = mydomainlen;
712 1.1.2.2 thorpej printf("nfs_boot: my_domain=%s\n", domainname);
713 1.1.2.2 thorpej }
714 1.1.2.2 thorpej nd->nd_myip = bootp->bp_yiaddr;
715 1.1.2.2 thorpej if (nd->nd_myip.s_addr)
716 1.1.2.2 thorpej printf("nfs_boot: my_addr=0x%x\n", INTOHL(nd->nd_myip));
717 1.1.2.2 thorpej nd->nd_mask = netmask;
718 1.1.2.2 thorpej if (nd->nd_mask.s_addr)
719 1.1.2.2 thorpej printf("nfs_boot: my_mask=0x%x\n", INTOHL(nd->nd_mask));
720 1.1.2.2 thorpej nd->nd_gwip = gateway;
721 1.1.2.2 thorpej if (nd->nd_gwip.s_addr)
722 1.1.2.2 thorpej printf("nfs_boot: gateway=0x%x\n", INTOHL(nd->nd_gwip));
723 1.1.2.2 thorpej
724 1.1.2.2 thorpej /*
725 1.1.2.2 thorpej * Store the information about our NFS root mount.
726 1.1.2.2 thorpej * The caller will print it, so be silent here.
727 1.1.2.2 thorpej */
728 1.1.2.2 thorpej {
729 1.1.2.2 thorpej struct nfs_dlmount *ndm = &nd->nd_root;
730 1.1.2.2 thorpej
731 1.1.2.2 thorpej /* Server IP address. */
732 1.1.2.2 thorpej sin = (struct sockaddr_in *) &ndm->ndm_saddr;
733 1.1.2.2 thorpej bzero((caddr_t)sin, sizeof(*sin));
734 1.1.2.2 thorpej sin->sin_len = sizeof(*sin);
735 1.1.2.2 thorpej sin->sin_family = AF_INET;
736 1.1.2.3 thorpej sin->sin_addr = rootserver;
737 1.1.2.2 thorpej /* Server name. */
738 1.1.2.3 thorpej if (!bcmp(&rootserver, &bootp->bp_siaddr,
739 1.1.2.3 thorpej sizeof(struct in_addr))) {
740 1.1.2.3 thorpej /* standard root server, we have the name */
741 1.1.2.3 thorpej strncpy(ndm->ndm_host, bootp->bp_sname, BP_SNAME_LEN-1);
742 1.1.2.3 thorpej } else {
743 1.1.2.2 thorpej /* Show the server IP address numerically. */
744 1.1.2.2 thorpej sprintf(ndm->ndm_host, "0x%8x",
745 1.1.2.3 thorpej INTOHL(rootserver));
746 1.1.2.2 thorpej }
747 1.1.2.3 thorpej len = strlen(ndm->ndm_host);
748 1.1.2.3 thorpej if (rootpath &&
749 1.1.2.3 thorpej len + 1 + rootpathlen + 1 <= sizeof(ndm->ndm_host)) {
750 1.1.2.3 thorpej ndm->ndm_host[len++] = ':';
751 1.1.2.3 thorpej strncpy(ndm->ndm_host + len,
752 1.1.2.3 thorpej rootpath, rootpathlen);
753 1.1.2.3 thorpej ndm->ndm_host[len + rootpathlen] = '\0';
754 1.1.2.3 thorpej } /* else: upper layer will handle error */
755 1.1.2.2 thorpej }
756 1.1.2.2 thorpej }
757