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