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