Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/stand/common/dev_net.c
34677 views
1
/* $NetBSD: dev_net.c,v 1.23 2008/04/28 20:24:06 martin Exp $ */
2
3
/*-
4
* Copyright (c) 1997 The NetBSD Foundation, Inc.
5
* All rights reserved.
6
*
7
* This code is derived from software contributed to The NetBSD Foundation
8
* by Gordon W. Ross.
9
*
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
12
* are met:
13
* 1. Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in the
17
* documentation and/or other materials provided with the distribution.
18
*
19
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
* POSSIBILITY OF SUCH DAMAGE.
30
*/
31
32
/*-
33
* This module implements a "raw device" interface suitable for
34
* use by the stand-alone I/O library NFS code. This interface
35
* does not support any "block" access, and exists only for the
36
* purpose of initializing the network interface, getting boot
37
* parameters, and performing the NFS mount.
38
*
39
* At open time, this does:
40
*
41
* find interface - netif_open()
42
* RARP for IP address - rarp_getipaddress()
43
* RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
44
* RPC/mountd - nfs_mount(sock, ip, path)
45
*
46
* the root file handle from mountd is saved in a global
47
* for use by the NFS open code (NFS/lookup).
48
*/
49
50
#include <sys/param.h>
51
#include <sys/socket.h>
52
#include <sys/stdarg.h>
53
54
#include <net/if.h>
55
#include <netinet/in.h>
56
#include <netinet/in_systm.h>
57
58
#include <stand.h>
59
#include <stddef.h>
60
#include <string.h>
61
#include <net.h>
62
#include <netif.h>
63
#include <bootp.h>
64
#include <bootparam.h>
65
66
#include "dev_net.h"
67
#include "bootstrap.h"
68
69
#ifndef NETPROTO_DEFAULT
70
# define NETPROTO_DEFAULT NET_NFS
71
#endif
72
73
static char *netdev_name;
74
static int netdev_sock = -1;
75
static int netdev_opens;
76
77
static int net_init(void);
78
static int net_open(struct open_file *, ...);
79
static int net_close(struct open_file *);
80
static void net_cleanup(void);
81
static int net_strategy(void *, int, daddr_t, size_t, char *, size_t *);
82
static int net_print(int);
83
84
static int net_getparams(int sock);
85
86
struct devsw netdev = {
87
.dv_name = "net",
88
.dv_type = DEVT_NET,
89
.dv_init = net_init,
90
.dv_strategy = net_strategy,
91
.dv_open = net_open,
92
.dv_close = net_close,
93
.dv_ioctl = noioctl,
94
.dv_print = net_print,
95
.dv_cleanup = net_cleanup,
96
};
97
98
static struct uri_scheme {
99
const char *scheme;
100
int proto;
101
} uri_schemes[] = {
102
{ "tftp:/", NET_TFTP },
103
{ "nfs:/", NET_NFS },
104
};
105
106
static int
107
net_init(void)
108
{
109
110
return (0);
111
}
112
113
/*
114
* Called by devopen after it sets f->f_dev to our devsw entry.
115
* This opens the low-level device and sets dev->d_opendata.
116
* This is declared with variable arguments...
117
*/
118
static int
119
net_open(struct open_file *f, ...)
120
{
121
struct iodesc *d;
122
va_list args;
123
struct devdesc *dev;
124
const char *devname; /* Device part of file name (or NULL). */
125
int error = 0;
126
127
va_start(args, f);
128
dev = va_arg(args, struct devdesc *);
129
va_end(args);
130
131
devname = dev->d_dev->dv_name;
132
/* Before opening another interface, close the previous one first. */
133
if (netdev_sock >= 0 && strcmp(devname, netdev_name) != 0)
134
net_cleanup();
135
136
/* On first open, do netif open, mount, etc. */
137
if (netdev_opens == 0) {
138
/* Find network interface. */
139
if (netdev_sock < 0) {
140
netdev_sock = netif_open(dev);
141
if (netdev_sock < 0) {
142
printf("%s: netif_open() failed\n", __func__);
143
return (ENXIO);
144
}
145
netdev_name = strdup(devname);
146
DEBUG_PRINTF(1,("%s: netif_open() succeeded %#x\n",
147
__func__, rootip.s_addr));
148
}
149
/*
150
* If network params were not set by netif_open(), try to get
151
* them via bootp, rarp, etc.
152
*/
153
if (rootip.s_addr == 0) {
154
/* Get root IP address, and path, etc. */
155
error = net_getparams(netdev_sock);
156
if (error) {
157
/* getparams makes its own noise */
158
free(netdev_name);
159
netif_close(netdev_sock);
160
netdev_sock = -1;
161
return (error);
162
}
163
}
164
/*
165
* Set the variables required by the kernel's nfs_diskless
166
* mechanism. This is the minimum set of variables required to
167
* mount a root filesystem without needing to obtain additional
168
* info from bootp or other sources.
169
*/
170
d = socktodesc(netdev_sock);
171
setenv("boot.netif.hwaddr", ether_sprintf(d->myea), 1);
172
setenv("boot.netif.ip", inet_ntoa(myip), 1);
173
setenv("boot.netif.netmask", intoa(netmask), 1);
174
setenv("boot.netif.gateway", inet_ntoa(gateip), 1);
175
setenv("boot.netif.server", inet_ntoa(rootip), 1);
176
if (netproto == NET_TFTP) {
177
setenv("boot.tftproot.server", inet_ntoa(rootip), 1);
178
setenv("boot.tftproot.path", rootpath, 1);
179
} else if (netproto == NET_NFS) {
180
setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
181
setenv("boot.nfsroot.path", rootpath, 1);
182
}
183
if (intf_mtu != 0) {
184
char mtu[16];
185
snprintf(mtu, sizeof(mtu), "%u", intf_mtu);
186
setenv("boot.netif.mtu", mtu, 1);
187
}
188
189
DEBUG_PRINTF(1,("%s: netproto=%d\n", __func__, netproto));
190
}
191
netdev_opens++;
192
dev->d_opendata = &netdev_sock;
193
return (error);
194
}
195
196
static int
197
net_close(struct open_file *f)
198
{
199
struct devdesc *dev;
200
201
DEBUG_PRINTF(2,("%s: opens=%d\n", __func__, netdev_opens));
202
203
dev = f->f_devdata;
204
dev->d_opendata = NULL;
205
206
return (0);
207
}
208
209
static void
210
net_cleanup(void)
211
{
212
213
if (netdev_sock >= 0) {
214
DEBUG_PRINTF(1,("%s: calling netif_close()\n", __func__));
215
rootip.s_addr = 0;
216
free(netdev_name);
217
netif_close(netdev_sock);
218
netdev_sock = -1;
219
}
220
}
221
222
static int
223
net_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
224
size_t *rsize)
225
{
226
227
return (EIO);
228
}
229
230
#define SUPPORT_BOOTP
231
232
/*
233
* Get info for NFS boot: our IP address, our hostname,
234
* server IP address, and our root path on the server.
235
* There are two ways to do this: The old, Sun way,
236
* and the more modern, BOOTP way. (RFC951, RFC1048)
237
*
238
* The default is to use the Sun bootparams RPC
239
* (because that is what the kernel will do).
240
* MD code can make try_bootp initialied data,
241
* which will override this common definition.
242
*/
243
#ifdef SUPPORT_BOOTP
244
int try_bootp = 1;
245
#endif
246
247
extern n_long ip_convertaddr(char *p);
248
249
static int
250
net_getparams(int sock)
251
{
252
char buf[MAXHOSTNAMELEN];
253
n_long rootaddr, smask;
254
255
#ifdef SUPPORT_BOOTP
256
/*
257
* Try to get boot info using BOOTP. If we succeed, then
258
* the server IP address, gateway, and root path will all
259
* be initialized. If any remain uninitialized, we will
260
* use RARP and RPC/bootparam (the Sun way) to get them.
261
*/
262
if (try_bootp)
263
bootp(sock);
264
if (myip.s_addr != 0)
265
goto exit;
266
DEBUG_PRINTF(1,("%s: BOOTP failed, trying RARP/RPC...\n", __func__));
267
#endif
268
269
/*
270
* Use RARP to get our IP address. This also sets our
271
* netmask to the "natural" default for our address.
272
*/
273
if (rarp_getipaddress(sock)) {
274
printf("%s: RARP failed\n", __func__);
275
return (EIO);
276
}
277
printf("%s: client addr: %s\n", __func__, inet_ntoa(myip));
278
279
/* Get our hostname, server IP address, gateway. */
280
if (bp_whoami(sock)) {
281
printf("%s: bootparam/whoami RPC failed\n", __func__);
282
return (EIO);
283
}
284
DEBUG_PRINTF(1,("%s: client name: %s\n", __func__, hostname));
285
286
/*
287
* Ignore the gateway from whoami (unreliable).
288
* Use the "gateway" parameter instead.
289
*/
290
smask = 0;
291
gateip.s_addr = 0;
292
if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
293
/* Got it! Parse the netmask. */
294
smask = ip_convertaddr(buf);
295
}
296
if (smask) {
297
netmask = smask;
298
DEBUG_PRINTF(1,("%s: subnet mask: %s\n", __func__,
299
intoa(netmask)));
300
}
301
if (gateip.s_addr)
302
DEBUG_PRINTF(1,("%s: net gateway: %s\n", __func__,
303
inet_ntoa(gateip)));
304
305
/* Get the root server and pathname. */
306
if (bp_getfile(sock, "root", &rootip, rootpath)) {
307
printf("%s: bootparam/getfile RPC failed\n", __func__);
308
return (EIO);
309
}
310
exit:
311
if ((rootaddr = net_parse_rootpath()) != htonl(INADDR_NONE))
312
rootip.s_addr = rootaddr;
313
314
DEBUG_PRINTF(1,("%s: proto: %d\n", __func__, netproto));
315
DEBUG_PRINTF(1,("%s: server addr: %s\n", __func__, inet_ntoa(rootip)));
316
DEBUG_PRINTF(1,("%s: server port: %d\n", __func__, rootport));
317
DEBUG_PRINTF(1,("%s: server path: %s\n", __func__, rootpath));
318
319
return (0);
320
}
321
322
static int
323
net_print(int verbose)
324
{
325
struct netif_driver *drv;
326
int i, d, cnt;
327
int ret = 0;
328
329
if (netif_drivers[0] == NULL)
330
return (ret);
331
332
printf("%s devices:", netdev.dv_name);
333
if ((ret = pager_output("\n")) != 0)
334
return (ret);
335
336
cnt = 0;
337
for (d = 0; netif_drivers[d]; d++) {
338
drv = netif_drivers[d];
339
for (i = 0; i < drv->netif_nifs; i++) {
340
printf("\t%s%d:", netdev.dv_name, cnt++);
341
if (verbose) {
342
printf(" (%s%d)", drv->netif_bname,
343
drv->netif_ifs[i].dif_unit);
344
}
345
if ((ret = pager_output("\n")) != 0)
346
return (ret);
347
}
348
}
349
return (ret);
350
}
351
352
bool
353
is_tftp(void)
354
{
355
return (netproto == NET_TFTP);
356
}
357
358
/*
359
* Parses the rootpath if present
360
*
361
* The rootpath format can be in the form
362
* <scheme>://ip[:port]/path
363
* <scheme>:/path
364
*
365
* For compatibility with previous behaviour it also accepts as an NFS scheme
366
* ip:/path
367
* /path
368
*
369
* If an ip is set it returns it in network byte order.
370
* The default scheme defined in the global netproto, if not set it defaults to
371
* NFS.
372
* It leaves just the pathname in the global rootpath.
373
*/
374
uint32_t
375
net_parse_rootpath(void)
376
{
377
n_long addr = 0;
378
size_t i;
379
char ip[FNAME_SIZE];
380
char *ptr, *portp, *val;
381
382
netproto = NET_NONE;
383
384
for (i = 0; i < nitems(uri_schemes); i++) {
385
if (strncmp(rootpath, uri_schemes[i].scheme,
386
strlen(uri_schemes[i].scheme)) != 0)
387
continue;
388
389
netproto = uri_schemes[i].proto;
390
break;
391
}
392
ptr = rootpath;
393
/* Fallback for compatibility mode */
394
if (netproto == NET_NONE) {
395
netproto = NETPROTO_DEFAULT;
396
(void)strsep(&ptr, ":");
397
if (ptr != NULL) {
398
addr = inet_addr(rootpath);
399
DEBUG_PRINTF(1,("rootpath=%s addr=%#x\n",
400
rootpath, addr));
401
bcopy(ptr, rootpath, strlen(ptr) + 1);
402
}
403
} else {
404
ptr += strlen(uri_schemes[i].scheme);
405
if (*ptr == '/') {
406
/* we are in the form <scheme>://, we do expect an ip */
407
ptr++;
408
portp = val = strchr(ptr, ':');
409
if (val != NULL) {
410
val++;
411
rootport = strtol(val, NULL, 10);
412
}
413
val = strchr(ptr, '/');
414
if (val != NULL) {
415
if (portp == NULL)
416
portp = val;
417
snprintf(ip, sizeof(ip), "%.*s",
418
(int)(portp - ptr),
419
ptr);
420
addr = inet_addr(ip);
421
DEBUG_PRINTF(1,("ip=%s addr=%#x\n",
422
ip, addr));
423
bcopy(val, rootpath, strlen(val) + 1);
424
}
425
} else {
426
ptr--;
427
bcopy(ptr, rootpath, strlen(ptr) + 1);
428
}
429
}
430
if (addr == 0)
431
addr = htonl(INADDR_NONE);
432
return (addr);
433
}
434
435