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 static void net_setparams(int sock); 86 87 struct devsw netdev = { 88 .dv_name = "net", 89 .dv_type = DEVT_NET, 90 .dv_init = net_init, 91 .dv_strategy = net_strategy, 92 .dv_open = net_open, 93 .dv_close = net_close, 94 .dv_ioctl = noioctl, 95 .dv_print = net_print, 96 .dv_cleanup = net_cleanup, 97 }; 98 99 static struct uri_scheme { 100 const char *scheme; 101 int proto; 102 } uri_schemes[] = { 103 { "tftp:/", NET_TFTP }, 104 { "nfs:/", NET_NFS }, 105 }; 106 107 static int 108 net_init(void) 109 { 110 111 return (0); 112 } 113 114 /* 115 * Called by devopen after it sets f->f_dev to our devsw entry. 116 * This opens the low-level device and sets dev->d_opendata. 117 * This is declared with variable arguments... 118 */ 119 static int 120 net_open(struct open_file *f, ...) 121 { 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 net_setparams(netdev_sock); 165 } 166 netdev_opens++; 167 dev->d_opendata = &netdev_sock; 168 return (error); 169 } 170 171 /* 172 * Configure a network interface without opening a file on it. This is used 173 * by consumers which need DHCP configuration before switching to a firmware 174 * network protocol. 175 */ 176 int 177 net_configure(struct devdesc *dev) 178 { 179 int error, sock; 180 181 sock = netif_open(dev); 182 if (sock < 0) 183 return (ENXIO); 184 185 error = 0; 186 if (rootip.s_addr == 0) 187 error = net_getparams(sock); 188 if (error == 0) 189 net_setparams(sock); 190 netif_close(sock); 191 return (error); 192 } 193 194 static int 195 net_close(struct open_file *f) 196 { 197 struct devdesc *dev; 198 199 DEBUG_PRINTF(2,("%s: opens=%d\n", __func__, netdev_opens)); 200 201 dev = f->f_devdata; 202 dev->d_opendata = NULL; 203 204 return (0); 205 } 206 207 static void 208 net_cleanup(void) 209 { 210 211 if (netdev_sock >= 0) { 212 DEBUG_PRINTF(1,("%s: calling netif_close()\n", __func__)); 213 rootip.s_addr = 0; 214 free(netdev_name); 215 netif_close(netdev_sock); 216 netdev_sock = -1; 217 } 218 } 219 220 static int 221 net_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf, 222 size_t *rsize) 223 { 224 225 return (EIO); 226 } 227 228 #define SUPPORT_BOOTP 229 230 /* 231 * Get info for NFS boot: our IP address, our hostname, 232 * server IP address, and our root path on the server. 233 * There are two ways to do this: The old, Sun way, 234 * and the more modern, BOOTP way. (RFC951, RFC1048) 235 * 236 * The default is to use the Sun bootparams RPC 237 * (because that is what the kernel will do). 238 * MD code can make try_bootp initialied data, 239 * which will override this common definition. 240 */ 241 #ifdef SUPPORT_BOOTP 242 int try_bootp = 1; 243 #endif 244 245 extern n_long ip_convertaddr(char *p); 246 247 static int 248 net_getparams(int sock) 249 { 250 char buf[MAXHOSTNAMELEN]; 251 n_long rootaddr, smask; 252 253 #ifdef SUPPORT_BOOTP 254 /* 255 * Try to get boot info using BOOTP. If we succeed, then 256 * the server IP address, gateway, and root path will all 257 * be initialized. If any remain uninitialized, we will 258 * use RARP and RPC/bootparam (the Sun way) to get them. 259 */ 260 if (try_bootp) 261 bootp(sock); 262 if (myip.s_addr != 0) 263 goto exit; 264 DEBUG_PRINTF(1,("%s: BOOTP failed, trying RARP/RPC...\n", __func__)); 265 #endif 266 267 /* 268 * Use RARP to get our IP address. This also sets our 269 * netmask to the "natural" default for our address. 270 */ 271 if (rarp_getipaddress(sock)) { 272 printf("%s: RARP failed\n", __func__); 273 return (EIO); 274 } 275 printf("%s: client addr: %s\n", __func__, inet_ntoa(myip)); 276 277 /* Get our hostname, server IP address, gateway. */ 278 if (bp_whoami(sock)) { 279 printf("%s: bootparam/whoami RPC failed\n", __func__); 280 return (EIO); 281 } 282 DEBUG_PRINTF(1,("%s: client name: %s\n", __func__, hostname)); 283 284 /* 285 * Ignore the gateway from whoami (unreliable). 286 * Use the "gateway" parameter instead. 287 */ 288 smask = 0; 289 gateip.s_addr = 0; 290 if (bp_getfile(sock, "gateway", &gateip, buf) == 0) { 291 /* Got it! Parse the netmask. */ 292 smask = ip_convertaddr(buf); 293 } 294 if (smask) { 295 netmask = smask; 296 DEBUG_PRINTF(1,("%s: subnet mask: %s\n", __func__, 297 intoa(netmask))); 298 } 299 if (gateip.s_addr) 300 DEBUG_PRINTF(1,("%s: net gateway: %s\n", __func__, 301 inet_ntoa(gateip))); 302 303 /* Get the root server and pathname. */ 304 if (bp_getfile(sock, "root", &rootip, rootpath)) { 305 printf("%s: bootparam/getfile RPC failed\n", __func__); 306 return (EIO); 307 } 308 exit: 309 if ((rootaddr = net_parse_rootpath()) != htonl(INADDR_NONE)) 310 rootip.s_addr = rootaddr; 311 312 DEBUG_PRINTF(1,("%s: proto: %d\n", __func__, netproto)); 313 DEBUG_PRINTF(1,("%s: server addr: %s\n", __func__, inet_ntoa(rootip))); 314 DEBUG_PRINTF(1,("%s: server port: %d\n", __func__, rootport)); 315 DEBUG_PRINTF(1,("%s: server path: %s\n", __func__, rootpath)); 316 317 return (0); 318 } 319 320 static void 321 net_setparams(int sock) 322 { 323 struct iodesc *d; 324 325 /* 326 * Set the variables required by the kernel's nfs_diskless mechanism. 327 */ 328 d = socktodesc(sock); 329 setenv("boot.netif.hwaddr", ether_sprintf(d->myea), 1); 330 setenv("boot.netif.ip", inet_ntoa(myip), 1); 331 setenv("boot.netif.netmask", intoa(netmask), 1); 332 setenv("boot.netif.gateway", inet_ntoa(gateip), 1); 333 setenv("boot.netif.server", inet_ntoa(rootip), 1); 334 if (netproto == NET_TFTP) { 335 setenv("boot.tftproot.server", inet_ntoa(rootip), 1); 336 setenv("boot.tftproot.path", rootpath, 1); 337 } else if (netproto == NET_NFS) { 338 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1); 339 setenv("boot.nfsroot.path", rootpath, 1); 340 } 341 if (intf_mtu != 0) { 342 char mtu[16]; 343 344 snprintf(mtu, sizeof(mtu), "%u", intf_mtu); 345 setenv("boot.netif.mtu", mtu, 1); 346 } 347 348 DEBUG_PRINTF(1,("%s: netproto=%d\n", __func__, netproto)); 349 } 350 351 static int 352 net_print(int verbose) 353 { 354 struct netif_driver *drv; 355 int i, d, cnt; 356 int ret = 0; 357 358 if (netif_drivers[0] == NULL) 359 return (ret); 360 361 printf("%s devices:", netdev.dv_name); 362 if ((ret = pager_output("\n")) != 0) 363 return (ret); 364 365 cnt = 0; 366 for (d = 0; netif_drivers[d]; d++) { 367 drv = netif_drivers[d]; 368 for (i = 0; i < drv->netif_nifs; i++) { 369 printf("\t%s%d:", netdev.dv_name, cnt++); 370 if (verbose) { 371 printf(" (%s%d)", drv->netif_bname, 372 drv->netif_ifs[i].dif_unit); 373 } 374 if ((ret = pager_output("\n")) != 0) 375 return (ret); 376 } 377 } 378 return (ret); 379 } 380 381 bool 382 is_tftp(void) 383 { 384 return (netproto == NET_TFTP); 385 } 386 387 /* 388 * Parses the rootpath if present 389 * 390 * The rootpath format can be in the form 391 * <scheme>://ip[:port]/path 392 * <scheme>:/path 393 * 394 * For compatibility with previous behaviour it also accepts as an NFS scheme 395 * ip:/path 396 * /path 397 * 398 * If an ip is set it returns it in network byte order. 399 * The default scheme defined in the global netproto, if not set it defaults to 400 * NFS. 401 * It leaves just the pathname in the global rootpath. 402 */ 403 uint32_t 404 net_parse_rootpath(void) 405 { 406 n_long addr = 0; 407 size_t i; 408 char ip[FNAME_SIZE]; 409 char *ptr, *portp, *val; 410 411 netproto = NET_NONE; 412 413 for (i = 0; i < nitems(uri_schemes); i++) { 414 if (strncmp(rootpath, uri_schemes[i].scheme, 415 strlen(uri_schemes[i].scheme)) != 0) 416 continue; 417 418 netproto = uri_schemes[i].proto; 419 break; 420 } 421 ptr = rootpath; 422 /* Fallback for compatibility mode */ 423 if (netproto == NET_NONE) { 424 netproto = NETPROTO_DEFAULT; 425 (void)strsep(&ptr, ":"); 426 if (ptr != NULL) { 427 addr = inet_addr(rootpath); 428 DEBUG_PRINTF(1,("rootpath=%s addr=%#x\n", 429 rootpath, addr)); 430 bcopy(ptr, rootpath, strlen(ptr) + 1); 431 } 432 } else { 433 ptr += strlen(uri_schemes[i].scheme); 434 if (*ptr == '/') { 435 /* we are in the form <scheme>://, we do expect an ip */ 436 ptr++; 437 portp = val = strchr(ptr, ':'); 438 if (val != NULL) { 439 val++; 440 rootport = strtol(val, NULL, 10); 441 } 442 val = strchr(ptr, '/'); 443 if (val != NULL) { 444 if (portp == NULL) 445 portp = val; 446 snprintf(ip, sizeof(ip), "%.*s", 447 (int)(portp - ptr), 448 ptr); 449 addr = inet_addr(ip); 450 DEBUG_PRINTF(1,("ip=%s addr=%#x\n", 451 ip, addr)); 452 bcopy(val, rootpath, strlen(val) + 1); 453 } 454 } else { 455 ptr--; 456 bcopy(ptr, rootpath, strlen(ptr) + 1); 457 } 458 } 459 if (addr == 0) 460 addr = htonl(INADDR_NONE); 461 return (addr); 462 } 463