1*6adfa96cSmrg /* $NetBSD: net.c,v 1.7 2021/04/12 03:55:40 mrg Exp $ */
2c594e4d0Stsubai
3c594e4d0Stsubai /*
4c594e4d0Stsubai * Copyright (c) 1995 Gordon W. Ross
5c594e4d0Stsubai * All rights reserved.
6c594e4d0Stsubai *
7c594e4d0Stsubai * Redistribution and use in source and binary forms, with or without
8c594e4d0Stsubai * modification, are permitted provided that the following conditions
9c594e4d0Stsubai * are met:
10c594e4d0Stsubai * 1. Redistributions of source code must retain the above copyright
11c594e4d0Stsubai * notice, this list of conditions and the following disclaimer.
12c594e4d0Stsubai * 2. Redistributions in binary form must reproduce the above copyright
13c594e4d0Stsubai * notice, this list of conditions and the following disclaimer in the
14c594e4d0Stsubai * documentation and/or other materials provided with the distribution.
15c594e4d0Stsubai *
16c594e4d0Stsubai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17c594e4d0Stsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18c594e4d0Stsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19c594e4d0Stsubai * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20c594e4d0Stsubai * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21c594e4d0Stsubai * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22c594e4d0Stsubai * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23c594e4d0Stsubai * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24c594e4d0Stsubai * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25c594e4d0Stsubai * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26c594e4d0Stsubai */
27c594e4d0Stsubai
28c594e4d0Stsubai /*
29c594e4d0Stsubai * This module implements a "raw device" interface suitable for
30c594e4d0Stsubai * use by the stand-alone I/O library NFS code. This interface
31c594e4d0Stsubai * does not support any "block" access, and exists only for the
32c594e4d0Stsubai * purpose of initializing the network interface, getting boot
33c594e4d0Stsubai * parameters, and performing the NFS mount.
34c594e4d0Stsubai *
35c594e4d0Stsubai * At open time, this does:
36c594e4d0Stsubai *
37c594e4d0Stsubai * find interface - netif_open()
38c594e4d0Stsubai * RARP for IP address - rarp_getipaddress()
39c594e4d0Stsubai * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
40c594e4d0Stsubai * RPC/mountd - nfs_mount(sock, ip, path)
41c594e4d0Stsubai *
42c594e4d0Stsubai * the root file handle from mountd is saved in a global
43c594e4d0Stsubai * for use by the NFS open code (NFS/lookup).
44c594e4d0Stsubai */
45c594e4d0Stsubai
46c594e4d0Stsubai #include <sys/param.h>
47c594e4d0Stsubai #include <sys/socket.h>
48c594e4d0Stsubai #include <net/if.h>
49c594e4d0Stsubai #include <netinet/in.h>
50c594e4d0Stsubai #include <netinet/in_systm.h>
51c594e4d0Stsubai
52c594e4d0Stsubai #include <lib/libsa/stand.h>
53c594e4d0Stsubai #include <lib/libsa/net.h>
54c594e4d0Stsubai #include <lib/libsa/bootparam.h>
558f53db05Stsutsui #include <lib/libsa/bootp.h>
56c594e4d0Stsubai #include <lib/libsa/nfs.h>
57c594e4d0Stsubai
58c594e4d0Stsubai #include <lib/libkern/libkern.h>
59c594e4d0Stsubai
60c594e4d0Stsubai #include <promdev.h>
61c594e4d0Stsubai
626e14bfebSdrochner #include "netif_news.h"
636e14bfebSdrochner
64c594e4d0Stsubai int netdev_sock = -1;
65c594e4d0Stsubai static int open_count;
66c594e4d0Stsubai
67c594e4d0Stsubai /*
68c594e4d0Stsubai * Called by devopen after it sets f->f_dev to our devsw entry.
69c594e4d0Stsubai * This opens the low-level device and sets f->f_devdata.
70c594e4d0Stsubai */
71c594e4d0Stsubai int
net_open(struct romdev * pd)72fd4afa03Stsutsui net_open(struct romdev *pd)
73c594e4d0Stsubai {
74c594e4d0Stsubai int error = 0;
75c594e4d0Stsubai
76c594e4d0Stsubai /* On first open, do netif open, mount, etc. */
77c594e4d0Stsubai if (open_count == 0) {
78c594e4d0Stsubai /* Find network interface. */
796e14bfebSdrochner if ((netdev_sock = netif_news_open(pd)) < 0) {
80c594e4d0Stsubai error = errno;
81c594e4d0Stsubai goto bad;
82c594e4d0Stsubai }
83c594e4d0Stsubai if ((error = net_mountroot()) != 0)
84c594e4d0Stsubai goto bad;
85c594e4d0Stsubai }
86c594e4d0Stsubai open_count++;
87c594e4d0Stsubai bad:
88fd4afa03Stsutsui return error;
89c594e4d0Stsubai }
90c594e4d0Stsubai
91c594e4d0Stsubai int
net_close(struct romdev * pd)92fd4afa03Stsutsui net_close(struct romdev *pd)
93c594e4d0Stsubai {
94c594e4d0Stsubai /* On last close, do netif close, etc. */
95c594e4d0Stsubai if (open_count <= 0)
96fd4afa03Stsutsui return 0;
97c594e4d0Stsubai
98c594e4d0Stsubai if (--open_count == 0)
996e14bfebSdrochner netif_news_close(netdev_sock);
100c594e4d0Stsubai
101fd4afa03Stsutsui return 0;
102c594e4d0Stsubai }
103c594e4d0Stsubai
104c594e4d0Stsubai int
net_mountroot(void)105fd4afa03Stsutsui net_mountroot(void)
106c594e4d0Stsubai {
107c594e4d0Stsubai
108c594e4d0Stsubai #ifdef DEBUG
109c594e4d0Stsubai printf("net_mountroot\n");
110c594e4d0Stsubai #endif
111c594e4d0Stsubai
112c594e4d0Stsubai /*
113c594e4d0Stsubai * Get info for NFS boot: our IP address, our hostname,
114c594e4d0Stsubai * server IP address, and our root path on the server.
115c594e4d0Stsubai * There are two ways to do this: The old, Sun way,
116c594e4d0Stsubai * and the more modern, BOOTP way. (RFC951, RFC1048)
117c594e4d0Stsubai */
118c594e4d0Stsubai
119c594e4d0Stsubai #ifdef SUN_BOOTPARAMS
120c594e4d0Stsubai /* Get boot info using RARP and Sun bootparams. */
121c594e4d0Stsubai
122c594e4d0Stsubai /* Get our IP address. (rarp.c) */
123c594e4d0Stsubai if (rarp_getipaddress(netdev_sock) == -1)
124c594e4d0Stsubai return (errno);
125c594e4d0Stsubai
126c594e4d0Stsubai printf("boot: client IP address: %s\n", inet_ntoa(myip));
127c594e4d0Stsubai
128c594e4d0Stsubai /* Get our hostname, server IP address. */
129c594e4d0Stsubai if (bp_whoami(netdev_sock))
130c594e4d0Stsubai return (errno);
131c594e4d0Stsubai
132c594e4d0Stsubai printf("boot: client name: %s\n", hostname);
133c594e4d0Stsubai
134c594e4d0Stsubai /* Get the root pathname. */
135c594e4d0Stsubai if (bp_getfile(netdev_sock, "root", &rootip, rootpath))
136c594e4d0Stsubai return (errno);
137c594e4d0Stsubai
138c594e4d0Stsubai #else
139c594e4d0Stsubai
140c594e4d0Stsubai /* Get boot info using BOOTP way. (RFC951, RFC1048) */
141c594e4d0Stsubai bootp(netdev_sock);
142c594e4d0Stsubai
143c594e4d0Stsubai printf("Using IP address: %s\n", inet_ntoa(myip));
144c594e4d0Stsubai
145c594e4d0Stsubai printf("myip: %s (%s)", hostname, inet_ntoa(myip));
1468f53db05Stsutsui if (gateip.s_addr)
147c594e4d0Stsubai printf(", gateip: %s", inet_ntoa(gateip));
148c594e4d0Stsubai if (netmask)
149c594e4d0Stsubai printf(", netmask: %s", intoa(netmask));
150c594e4d0Stsubai printf("\n");
151c594e4d0Stsubai
152c594e4d0Stsubai #endif
153c594e4d0Stsubai
154c594e4d0Stsubai printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
155c594e4d0Stsubai
156c594e4d0Stsubai /* Get the NFS file handle (mount). */
157c594e4d0Stsubai if (nfs_mount(netdev_sock, rootip, rootpath) != 0)
158c594e4d0Stsubai return (errno);
159c594e4d0Stsubai
160fd4afa03Stsutsui return 0;
161c594e4d0Stsubai }
162