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