1*541bccc9Skrw /* $OpenBSD: net.c,v 1.9 2023/06/01 17:24:56 krw Exp $ */
2284a978eSjason /* $NetBSD: net.c,v 1.1 2000/08/20 14:58:38 mrg Exp $ */
3284a978eSjason
4284a978eSjason /*
5284a978eSjason * Copyright (C) 1995 Wolfgang Solfrank.
6284a978eSjason * Copyright (C) 1995 TooLs GmbH.
7284a978eSjason * All rights reserved.
8284a978eSjason *
9284a978eSjason * Redistribution and use in source and binary forms, with or without
10284a978eSjason * modification, are permitted provided that the following conditions
11284a978eSjason * are met:
12284a978eSjason * 1. Redistributions of source code must retain the above copyright
13284a978eSjason * notice, this list of conditions and the following disclaimer.
14284a978eSjason * 2. Redistributions in binary form must reproduce the above copyright
15284a978eSjason * notice, this list of conditions and the following disclaimer in the
16284a978eSjason * documentation and/or other materials provided with the distribution.
17284a978eSjason * 3. All advertising materials mentioning features or use of this software
18284a978eSjason * must display the following acknowledgement:
19284a978eSjason * This product includes software developed by TooLs GmbH.
20284a978eSjason * 4. The name of TooLs GmbH may not be used to endorse or promote products
21284a978eSjason * derived from this software without specific prior written permission.
22284a978eSjason *
23284a978eSjason * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24284a978eSjason * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25284a978eSjason * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26284a978eSjason * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27284a978eSjason * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28284a978eSjason * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29284a978eSjason * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30284a978eSjason * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31284a978eSjason * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32284a978eSjason * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33284a978eSjason */
34284a978eSjason
35284a978eSjason /*
36284a978eSjason * This module implements a "raw device" interface suitable for
37284a978eSjason * use by the stand-alone I/O library NFS code. This interface
38284a978eSjason * does not support any "block" access, and exists only for the
39284a978eSjason * purpose of initializing the network interface, getting boot
40284a978eSjason * parameters, and performing the NFS mount.
41284a978eSjason *
42284a978eSjason * At open time, this does:
43284a978eSjason *
44284a978eSjason * find interface - netif_open()
45284a978eSjason * BOOTP - bootp()
46284a978eSjason * RPC/mountd - nfs_mount()
47284a978eSjason *
48284a978eSjason * The root file handle from mountd is saved in a global
49284a978eSjason * for use by the NFS open code (NFS/lookup).
50284a978eSjason *
51284a978eSjason * Note: this is based in part on sys/arch/sparc/stand/net.c
52284a978eSjason */
53284a978eSjason
54284a978eSjason #include <sys/param.h>
55284a978eSjason #include <sys/socket.h>
56284a978eSjason
57284a978eSjason #include <net/if.h>
58284a978eSjason #include <netinet/in.h>
59284a978eSjason
60284a978eSjason #include <lib/libsa/stand.h>
61284a978eSjason #include <lib/libsa/net.h>
62284a978eSjason #include <lib/libsa/netif.h>
636c45f45cSclaudio #include <lib/libsa/bootparam.h>
646c45f45cSclaudio #include <lib/libsa/bootp.h>
656c45f45cSclaudio #include <lib/libsa/nfs.h>
66284a978eSjason
67702dea91Sjsing #include "ofdev.h"
68284a978eSjason
696c45f45cSclaudio int net_mountroot_bootparams(void);
706c45f45cSclaudio int net_mountroot_bootp(void);
716c45f45cSclaudio int net_mountroot(void);
72284a978eSjason
73554f0ec8Sderaadt extern char rootpath[FNAME_SIZE];
74284a978eSjason
75284a978eSjason static int netdev_sock = -1;
76284a978eSjason static int open_count;
77284a978eSjason
78284a978eSjason /*
79284a978eSjason * Called by devopen after it sets f->f_dev to our devsw entry.
80284a978eSjason * This opens the low-level device and sets f->f_devdata.
81284a978eSjason */
82284a978eSjason int
net_open(struct of_dev * op)835d4d064fSjsing net_open(struct of_dev *op)
84284a978eSjason {
85284a978eSjason int error = 0;
86284a978eSjason
87284a978eSjason /*
88284a978eSjason * On first open, do netif open, mount, etc.
89284a978eSjason */
90284a978eSjason if (open_count == 0) {
91284a978eSjason /* Find network interface. */
92284a978eSjason if ((netdev_sock = netif_open(op)) < 0) {
93284a978eSjason error = errno;
94284a978eSjason goto bad;
95284a978eSjason }
96284a978eSjason if ((error = net_mountroot()) != 0)
97284a978eSjason goto bad;
98284a978eSjason }
99284a978eSjason open_count++;
100284a978eSjason bad:
101284a978eSjason if (netdev_sock >= 0 && open_count == 0) {
102284a978eSjason netif_close(netdev_sock);
103284a978eSjason netdev_sock = -1;
104284a978eSjason }
105284a978eSjason return error;
106284a978eSjason }
107284a978eSjason
1086c45f45cSclaudio void
net_close(struct of_dev * op)1095d4d064fSjsing net_close(struct of_dev *op)
110284a978eSjason {
111284a978eSjason /*
112284a978eSjason * On last close, do netif close, etc.
113284a978eSjason */
114284a978eSjason if (open_count > 0)
115284a978eSjason if (--open_count == 0) {
116284a978eSjason netif_close(netdev_sock);
117284a978eSjason netdev_sock = -1;
118284a978eSjason }
119284a978eSjason }
120284a978eSjason
121284a978eSjason int
net_mountroot_bootparams(void)1225d4d064fSjsing net_mountroot_bootparams(void)
123284a978eSjason {
124284a978eSjason /* Get our IP address. (rarp.c) */
125284a978eSjason if (rarp_getipaddress(netdev_sock) == -1)
126284a978eSjason return (errno);
127284a978eSjason
128284a978eSjason printf("Using BOOTPARAMS protocol: ");
129284a978eSjason printf("ip address: %s", inet_ntoa(myip));
130284a978eSjason
131284a978eSjason /* Get our hostname, server IP address. */
132284a978eSjason if (bp_whoami(netdev_sock))
133284a978eSjason return (errno);
134284a978eSjason
135284a978eSjason printf(", hostname: %s\n", hostname);
136284a978eSjason
137284a978eSjason /* Get the root pathname. */
138284a978eSjason if (bp_getfile(netdev_sock, "root", &rootip, rootpath))
139284a978eSjason return (errno);
140284a978eSjason
141284a978eSjason return (0);
142284a978eSjason }
143284a978eSjason
144284a978eSjason int
net_mountroot_bootp(void)1455d4d064fSjsing net_mountroot_bootp(void)
146284a978eSjason {
147284a978eSjason bootp(netdev_sock);
148284a978eSjason
149284a978eSjason if (myip.s_addr == 0)
150284a978eSjason return(ENOENT);
151284a978eSjason
152284a978eSjason printf("Using BOOTP protocol: ");
153284a978eSjason printf("ip address: %s", inet_ntoa(myip));
154284a978eSjason
155284a978eSjason if (hostname[0])
156284a978eSjason printf(", hostname: %s", hostname);
157284a978eSjason if (netmask)
158284a978eSjason printf(", netmask: %s", intoa(netmask));
159284a978eSjason if (gateip.s_addr)
160284a978eSjason printf(", gateway: %s", inet_ntoa(gateip));
161284a978eSjason printf("\n");
162284a978eSjason
163284a978eSjason return (0);
164284a978eSjason }
165284a978eSjason
166284a978eSjason int
net_mountroot(void)1675d4d064fSjsing net_mountroot(void)
168284a978eSjason {
169284a978eSjason int error;
170284a978eSjason
171284a978eSjason #ifdef DEBUG
172284a978eSjason printf("net_mountroot\n");
173284a978eSjason #endif
174284a978eSjason
175284a978eSjason /*
176284a978eSjason * Get info for NFS boot: our IP address, our hostname,
177284a978eSjason * server IP address, and our root path on the server.
178284a978eSjason * There are two ways to do this: The old, Sun way,
179284a978eSjason * and the more modern, BOOTP way. (RFC951, RFC1048)
180284a978eSjason */
181284a978eSjason
182284a978eSjason /* Historically, we've used BOOTPARAMS, so try that first */
183284a978eSjason error = net_mountroot_bootparams();
184284a978eSjason if (error != 0)
185284a978eSjason /* Next, try BOOTP */
186284a978eSjason error = net_mountroot_bootp();
187284a978eSjason if (error != 0)
188284a978eSjason return (error);
189284a978eSjason
190284a978eSjason printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
191284a978eSjason
192284a978eSjason /* Get the NFS file handle (mount). */
193284a978eSjason if (nfs_mount(netdev_sock, rootip, rootpath) != 0)
194284a978eSjason return (errno);
195284a978eSjason
196284a978eSjason return (0);
197284a978eSjason }
198