net.c revision 1.2
1/*	$NetBSD: net.c,v 1.2 1997/07/22 17:41:05 drochner Exp $	*/
2
3/*
4 * Copyright (C) 1995 Wolfgang Solfrank.
5 * Copyright (C) 1995 TooLs GmbH.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/*
35 * This module implements a "raw device" interface suitable for
36 * use by the stand-alone I/O library NFS code.  This interface
37 * does not support any "block" access, and exists only for the
38 * purpose of initializing the network interface, getting boot
39 * parameters, and performing the NFS mount.
40 *
41 * At open time, this does:
42 *
43 * find interface	- netif_open()
44 * BOOTP		- bootp()
45 * RPC/mountd		- nfs_mount()
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 * Note: this is based in part on sys/arch/sparc/stand/net.c
51 */
52
53#include <sys/param.h>
54#include <sys/socket.h>
55#include <net/if.h>
56#include <netinet/in.h>
57#include <netinet/in_systm.h>
58
59#include <lib/libsa/stand.h>
60#include <lib/libsa/net.h>
61#include <lib/libsa/netif.h>
62
63char	rootpath[FNAME_SIZE];
64
65static	int netdev_sock = -1;
66static	int open_count;
67
68/*
69 * Called by devopen after it sets f->f_dev to our devsw entry.
70 * This opens the low-level device and sets f->f_devdata.
71 */
72int
73net_open(op)
74	struct of_dev *op;
75{
76	int error = 0;
77
78	/*
79	 * On first open, do netif open, mount, etc.
80	 */
81	if (open_count == 0) {
82		/* Find network interface. */
83		if ((netdev_sock = netif_open(op)) < 0) {
84			error = errno;
85			goto bad;
86		}
87		if ((error = net_mountroot()) != 0)
88			goto bad;
89	}
90	open_count++;
91bad:
92	if (netdev_sock >= 0 && open_count == 0) {
93		netif_close(netdev_sock);
94		netdev_sock = -1;
95	}
96	return error;
97}
98
99int
100net_close(op)
101	struct of_dev *op;
102{
103	/*
104	 * On last close, do netif close, etc.
105	 */
106	if (open_count > 0)
107		if (--open_count == 0) {
108			netif_close(netdev_sock);
109			netdev_sock = -1;
110		}
111}
112
113int
114net_mountroot()
115{
116
117#ifdef	DEBUG
118	printf("net_mountroot\n");
119#endif
120
121	/*
122	 * Get info for NFS boot: our IP address, out hostname,
123	 * server IP address, and our root path on the server.
124	 * We use BOOTP (RFC951, RFC1532) exclusively as mandated
125	 * by PowerPC Reference Platform Specification I.4.2
126	 */
127
128	bootp(netdev_sock);
129
130	if (myip.s_addr == 0)
131		return ETIMEDOUT;
132
133	printf("Using IP address: %s\n", inet_ntoa(myip));
134
135#ifdef	DEBUG
136	printf("myip: %s (%s)", hostname, inet_ntoa(myip));
137	if (gateip.s_addr)
138		printf(", gateip: %s", inet_ntoa(gateip));
139	if (netmask)
140		printf(", netmask: %s", intoa(netmask));
141	printf("\n");
142#endif
143	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
144
145	/*
146	 * Get the NFS file handle (mount).
147	 */
148	if (nfs_mount(netdev_sock, rootip, rootpath) < 0)
149		return errno;
150	return 0;
151}
152