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