1 /* $OpenBSD: pxe_net.c,v 1.4 2011/03/13 00:13:53 deraadt Exp $ */ 2 /* $NetBSD: dev_net.c,v 1.4 2003/03/12 13:15:08 drochner Exp $ */ 3 4 /*- 5 * Copyright (c) 1997 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Gordon W. Ross. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * This module implements a "raw device" interface suitable for 35 * use by the stand-alone I/O library NFS and TFTP code. This interface 36 * does not support any "block" access, and exists only for the 37 * purpose of initializing the network interface and getting boot 38 * parameters. 39 * 40 * At open time, this does: 41 * 42 * find interface - netif_open() 43 * BOOTP for IP address - bootp() 44 */ 45 46 #include <sys/param.h> 47 #include <sys/socket.h> 48 #include <net/if.h> 49 #include <netinet/in.h> 50 #include <netinet/in_systm.h> 51 52 #include <lib/libkern/libkern.h> 53 54 #include <lib/libsa/stand.h> 55 #include <lib/libsa/net.h> 56 #include "pxe_netif.h" 57 #include "pxe_net.h" 58 59 static int netdev_sock = -1; 60 static int netdev_opens; 61 62 int net_getparams(int); 63 64 /* 65 * Called by devopen after it sets f->f_dev to our devsw entry. 66 * This opens the low-level device and sets f->f_devdata. 67 * This is declared with variable arguments... 68 */ 69 int 70 net_open(struct open_file *f, ...) 71 { 72 int error = 0; 73 74 #ifdef NETIF_DEBUG 75 if (debug) 76 printf("net_open()\n"); 77 #endif 78 79 /* On first open, do netif open, mount, etc. */ 80 if (netdev_opens == 0) { 81 /* Find network interface. */ 82 if (netdev_sock < 0) { 83 netdev_sock = pxe_netif_open(); 84 if (netdev_sock < 0) { 85 printf("net_open: netif_open() failed\n"); 86 return ENXIO; 87 } 88 #ifdef NETIF_DEBUG 89 if (debug) 90 printf("net_open: netif_open() succeeded\n"); 91 #endif 92 } 93 #ifdef notyet 94 if (rootip.s_addr == 0) { 95 /* Get root IP address, and path, etc. */ 96 error = net_getparams(netdev_sock); 97 if (error) { 98 /* getparams makes its own noise */ 99 pxe_netif_close(netdev_sock); 100 netdev_sock = -1; 101 return error; 102 } 103 } 104 #endif 105 } 106 netdev_opens++; 107 f->f_devdata = &netdev_sock; 108 return error; 109 } 110 111 int 112 net_close(struct open_file *f) 113 { 114 #ifdef NETIF_DEBUG 115 if (debug) 116 printf("net_close: opens=%d\n", netdev_opens); 117 #endif 118 119 /* On last close, do netif close, etc. */ 120 f->f_devdata = NULL; 121 /* Extra close call? */ 122 if (netdev_opens <= 0) 123 return 0; 124 netdev_opens--; 125 /* Not last close? */ 126 if (netdev_opens > 0) 127 return 0; 128 rootip.s_addr = 0; 129 if (netdev_sock >= 0) { 130 #ifdef NETIF_DEBUG 131 if (debug) 132 printf("net_close: calling netif_close()\n"); 133 #endif 134 pxe_netif_close(netdev_sock); 135 netdev_sock = -1; 136 } 137 return 0; 138 } 139 140 int 141 net_ioctl(struct open_file *f, u_long cmd, void *data) 142 { 143 return EIO; 144 } 145 146 int 147 net_strategy(void *devdata, int rw, daddr32_t blk, size_t size, void *buf, 148 size_t *rsize) 149 { 150 return EIO; 151 } 152 153 154 /* 155 * Get info for network boot: our IP address, our hostname, 156 * server IP address, and our root path on the server. 157 */ 158 extern int bootp(int); 159 160 int 161 net_getparams(int sock) 162 { 163 /* 164 * Try to get boot info using BOOTP. If we succeed, then 165 * the server IP address, gateway, and root path will all 166 * be initialized. If any remain uninitialized, we will 167 * use RARP and RPC/bootparam (the Sun way) to get them. 168 */ 169 bootp(sock); 170 if (myip.s_addr != 0) 171 return 0; 172 if (debug) 173 printf("net_getparams: BOOTP failed\n"); 174 175 return EIO; 176 } 177