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