Core integer types header file reorganization stage 1/2: Create and/or modify
[dragonfly.git] / sys / boot / common / dev_net.c
1 /*      
2  * $FreeBSD: src/sys/boot/common/dev_net.c,v 1.6.2.5 2001/03/04 04:44:42 obrien Exp $
3  * $DragonFly: src/sys/boot/common/dev_net.c,v 1.3 2003/11/09 02:22:33 dillon Exp $
4  * From: $NetBSD: dev_net.c,v 1.12 1997/12/10 20:38:37 gwr Exp $
5  */
6
7 /*-
8  * Copyright (c) 1997 The NetBSD Foundation, Inc.
9  * All rights reserved.
10  *
11  * This code is derived from software contributed to The NetBSD Foundation
12  * by Gordon W. Ross.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *        This product includes software developed by the NetBSD
25  *        Foundation, Inc. and its contributors.
26  * 4. Neither the name of The NetBSD Foundation nor the names of its
27  *    contributors may be used to endorse or promote products derived
28  *    from this software without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40  * POSSIBILITY OF SUCH DAMAGE.
41  */
42
43 /*
44  * This module implements a "raw device" interface suitable for
45  * use by the stand-alone I/O library NFS code.  This interface
46  * does not support any "block" access, and exists only for the
47  * purpose of initializing the network interface, getting boot
48  * parameters, and performing the NFS mount.
49  *
50  * At open time, this does:
51  *
52  * find interface      - netif_open()
53  * RARP for IP address - rarp_getipaddress()
54  * RPC/bootparams      - callrpc(d, RPC_BOOTPARAMS, ...)
55  * RPC/mountd          - nfs_mount(sock, ip, path)
56  *
57  * the root file handle from mountd is saved in a global
58  * for use by the NFS open code (NFS/lookup).
59  */
60
61 #include <machine/stdarg.h>
62 #include <sys/param.h>
63 #include <sys/socket.h>
64 #include <net/if.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67
68 #include <stand.h>
69 #include <string.h>
70 #include <net.h>
71 #include <netif.h>
72 #include <bootp.h>
73 #include <bootparam.h>
74
75 #include "dev_net.h"
76 #include "bootstrap.h"
77
78 int debug = 0;
79
80 static int netdev_sock = -1;
81 static int netdev_opens;
82
83 static int      net_init(void);
84 static int      net_open(struct open_file *, ...);
85 static int      net_close(struct open_file *);
86 static int      net_strategy();
87 static void     net_print(int);
88
89 static int net_getparams(int sock);
90
91 struct devsw netdev = {
92     "net", 
93     DEVT_NET, 
94     net_init,
95     net_strategy, 
96     net_open, 
97     net_close, 
98     noioctl,
99     net_print
100 };
101
102 int
103 net_init(void)
104 {
105     return 0;
106 }
107
108 /*
109  * Called by devopen after it sets f->f_dev to our devsw entry.
110  * This opens the low-level device and sets f->f_devdata.
111  * This is declared with variable arguments...
112  */
113 int
114 net_open(struct open_file *f, ...)
115 {
116     __va_list args;
117     char *devname;              /* Device part of file name (or NULL). */
118     int error = 0;
119
120     __va_start(args, f);
121     devname = __va_arg(args, char*);
122     __va_end(args);
123
124     /* On first open, do netif open, mount, etc. */
125     if (netdev_opens == 0) {
126         /* Find network interface. */
127         if (netdev_sock < 0) {
128             netdev_sock = netif_open(devname);
129             if (netdev_sock < 0) {
130                 printf("net_open: netif_open() failed\n");
131                 return (ENXIO);
132             }
133             if (debug)
134                 printf("net_open: netif_open() succeeded\n");
135         }
136         if (rootip.s_addr == 0) {
137             /* Get root IP address, and path, etc. */
138             error = net_getparams(netdev_sock);
139             if (error) {
140                                 /* getparams makes its own noise */
141                 netif_close(netdev_sock);
142                 netdev_sock = -1;
143                 return (error);
144             }
145         }
146         netdev_opens++;
147     }
148     netdev_opens++;
149     f->f_devdata = &netdev_sock;
150     return (error);
151 }
152
153 int
154 net_close(f)
155     struct open_file *f;
156 {
157
158 #ifdef  NETIF_DEBUG
159     if (debug)
160         printf("net_close: opens=%d\n", netdev_opens);
161 #endif
162
163     /* On last close, do netif close, etc. */
164     f->f_devdata = NULL;
165     /* Extra close call? */
166     if (netdev_opens <= 0)
167         return (0);
168     netdev_opens--;
169     /* Not last close? */
170     if (netdev_opens > 0)
171         return(0);
172     rootip.s_addr = 0;
173     if (netdev_sock >= 0) {
174         if (debug)
175             printf("net_close: calling netif_close()\n");
176         netif_close(netdev_sock);
177         netdev_sock = -1;
178     }
179     return (0);
180 }
181
182 int
183 net_strategy()
184 {
185     return EIO;
186 }
187
188 #define SUPPORT_BOOTP
189
190 /*
191  * Get info for NFS boot: our IP address, our hostname,
192  * server IP address, and our root path on the server.
193  * There are two ways to do this:  The old, Sun way,
194  * and the more modern, BOOTP way. (RFC951, RFC1048)
195  *
196  * The default is to use the Sun bootparams RPC
197  * (because that is what the kernel will do).
198  * MD code can make try_bootp initialied data,
199  * which will override this common definition.
200  */
201 #ifdef  SUPPORT_BOOTP
202 int try_bootp = 1;
203 #endif
204
205 extern n_long ip_convertaddr(char *p);
206
207 static int
208 net_getparams(sock)
209     int sock;
210 {
211     char buf[MAXHOSTNAMELEN];
212     char temp[FNAME_SIZE];
213     int i;
214     n_long smask;
215
216 #ifdef  SUPPORT_BOOTP
217     /*
218      * Try to get boot info using BOOTP.  If we succeed, then
219      * the server IP address, gateway, and root path will all
220      * be initialized.  If any remain uninitialized, we will
221      * use RARP and RPC/bootparam (the Sun way) to get them.
222      */
223     if (try_bootp)
224         bootp(sock, BOOTP_NONE);
225     if (myip.s_addr != 0)
226         goto exit;
227     if (debug)
228         printf("net_open: BOOTP failed, trying RARP/RPC...\n");
229 #endif
230
231     /*
232      * Use RARP to get our IP address.  This also sets our
233      * netmask to the "natural" default for our address.
234      */
235     if (rarp_getipaddress(sock)) {
236         printf("net_open: RARP failed\n");
237         return (EIO);
238     }
239     printf("net_open: client addr: %s\n", inet_ntoa(myip));
240
241     /* Get our hostname, server IP address, gateway. */
242     if (bp_whoami(sock)) {
243         printf("net_open: bootparam/whoami RPC failed\n");
244         return (EIO);
245     }
246     printf("net_open: client name: %s\n", hostname);
247
248     /*
249      * Ignore the gateway from whoami (unreliable).
250      * Use the "gateway" parameter instead.
251      */
252     smask = 0;
253     gateip.s_addr = 0;
254     if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
255         /* Got it!  Parse the netmask. */
256         smask = ip_convertaddr(buf);
257     }
258     if (smask) {
259         netmask = smask;
260         printf("net_open: subnet mask: %s\n", intoa(netmask));
261     }
262     if (gateip.s_addr)
263         printf("net_open: net gateway: %s\n", inet_ntoa(gateip));
264
265     /* Get the root server and pathname. */
266     if (bp_getfile(sock, "root", &rootip, rootpath)) {
267         printf("net_open: bootparam/getfile RPC failed\n");
268         return (EIO);
269     }
270  exit:
271     printf("net_open: server addr: %s\n", inet_ntoa(rootip));
272
273     /*  
274      * If present, strip the server's address off of the rootpath
275      * before passing it along.  This allows us to be compatible with
276      * the kernel's diskless (BOOTP_NFSROOT) booting conventions
277      */
278
279     for(i=0; i<FNAME_SIZE; i++)
280             if(rootpath[i] == ':')
281                     break;
282     if(i && i != FNAME_SIZE) {
283             i++;
284             bcopy(&rootpath[i], &temp[0], strlen(&rootpath[i])+1);
285             bcopy(&temp[0], &rootpath[0], strlen(&rootpath[i])+1);          
286     }
287     printf("net_open: server path: %s\n", rootpath);        
288     return (0);
289 }
290
291 static void
292 net_print(int verbose)
293 {
294     return;
295 }