dev_net.c revision 1.9 1 /* $NetBSD: dev_net.c,v 1.9 2009/03/14 15:36:08 dsl 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 and TFTP code. This interface
35 * does not support any "block" access, and exists only for the
36 * purpose of initializing the network interface and getting boot
37 * parameters.
38 *
39 * At open time, this does:
40 *
41 * find interface - netif_open()
42 * BOOTP for IP address - bootp()
43 */
44
45 #include <machine/stdarg.h>
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 "dev_net.h"
58
59 static int netdev_sock = -1;
60 static int netdev_opens;
61
62 static int net_getparams(int sock);
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 if (debug)
89 printf("net_open: netif_open() succeeded\n");
90 }
91 if (rootip.s_addr == 0) {
92 /* Get root IP address, and path, etc. */
93 error = net_getparams(netdev_sock);
94 if (error) {
95 /* getparams makes its own noise */
96 pxe_netif_close(netdev_sock);
97 netdev_sock = -1;
98 return (error);
99 }
100 }
101 }
102 netdev_opens++;
103 f->f_devdata = &netdev_sock;
104 return (error);
105 }
106
107 int
108 net_close(struct open_file *f)
109 {
110
111 #ifdef NETIF_DEBUG
112 if (debug)
113 printf("net_close: opens=%d\n", netdev_opens);
114 #endif
115
116 /* On last close, do netif close, etc. */
117 f->f_devdata = NULL;
118 /* Extra close call? */
119 if (netdev_opens <= 0)
120 return (0);
121 netdev_opens--;
122 /* Not last close? */
123 if (netdev_opens > 0)
124 return(0);
125 rootip.s_addr = 0;
126 if (netdev_sock >= 0) {
127 if (debug)
128 printf("net_close: calling netif_close()\n");
129 pxe_netif_close(netdev_sock);
130 //pxe_netif_shutdown(); /* XXX shouldn't be done here */
131 netdev_sock = -1;
132 }
133 return (0);
134 }
135
136 int
137 net_ioctl(struct open_file *f, u_long cmd, void *data)
138 {
139 return EIO;
140 }
141
142 int
143 net_strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf, size_t *rsize)
144 {
145 return EIO;
146 }
147
148
149 /*
150 * Get info for network boot: our IP address, our hostname,
151 * server IP address, and our root path on the server.
152 */
153 #ifdef SUPPORT_BOOTP
154 int bootp(int sock);
155 #endif
156
157 static int
158 net_getparams(int sock)
159 {
160
161 #ifdef SUPPORT_BOOTP
162 /*
163 * Try to get boot info using BOOTP. If we succeed, then
164 * the server IP address, gateway, and root path will all
165 * be initialized. If any remain uninitialized, we will
166 * use RARP and RPC/bootparam (the Sun way) to get them.
167 */
168 bootp(sock);
169 if (myip.s_addr != 0)
170 return (0);
171 if (debug)
172 printf("net_open: BOOTP failed\n");
173 #endif
174
175 return (EIO);
176 }
177