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