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