net.c revision 1.3 1 1.3 christos /* $NetBSD: net.c,v 1.3 2004/06/30 15:43:57 christos 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.3 christos #include <lib/libsa/bootp.h>
63 1.3 christos #include <lib/libsa/nfs.h>
64 1.1 thorpej
65 1.1 thorpej #include <lib/libkern/libkern.h>
66 1.1 thorpej
67 1.3 christos #include "extern.h"
68 1.2 drochner #include "ofdev.h"
69 1.2 drochner #include "netif_of.h"
70 1.2 drochner
71 1.1 thorpej char rootpath[FNAME_SIZE];
72 1.1 thorpej
73 1.1 thorpej static int netdev_sock = -1;
74 1.1 thorpej static int open_count;
75 1.1 thorpej
76 1.1 thorpej /*
77 1.1 thorpej * Called by devopen after it sets f->f_dev to our devsw entry.
78 1.1 thorpej * This opens the low-level device and sets f->f_devdata.
79 1.1 thorpej */
80 1.1 thorpej int
81 1.3 christos net_open(struct of_dev *op)
82 1.1 thorpej {
83 1.1 thorpej int error = 0;
84 1.2 drochner
85 1.1 thorpej /*
86 1.1 thorpej * On first open, do netif open, mount, etc.
87 1.1 thorpej */
88 1.1 thorpej if (open_count == 0) {
89 1.1 thorpej /* Find network interface. */
90 1.2 drochner if ((netdev_sock = netif_of_open(op)) < 0) {
91 1.1 thorpej error = errno;
92 1.1 thorpej goto bad;
93 1.1 thorpej }
94 1.1 thorpej if ((error = net_mountroot()) != 0)
95 1.1 thorpej goto bad;
96 1.1 thorpej }
97 1.1 thorpej open_count++;
98 1.1 thorpej bad:
99 1.1 thorpej if (netdev_sock >= 0 && open_count == 0) {
100 1.2 drochner netif_of_close(netdev_sock);
101 1.1 thorpej netdev_sock = -1;
102 1.1 thorpej }
103 1.1 thorpej return error;
104 1.1 thorpej }
105 1.1 thorpej
106 1.1 thorpej int
107 1.3 christos net_close(struct of_dev *op)
108 1.1 thorpej {
109 1.1 thorpej /*
110 1.1 thorpej * On last close, do netif close, etc.
111 1.1 thorpej */
112 1.1 thorpej if (open_count > 0)
113 1.1 thorpej if (--open_count == 0) {
114 1.2 drochner netif_of_close(netdev_sock);
115 1.1 thorpej netdev_sock = -1;
116 1.1 thorpej }
117 1.3 christos return 0;
118 1.1 thorpej }
119 1.1 thorpej
120 1.1 thorpej int
121 1.3 christos net_mountroot(void)
122 1.1 thorpej {
123 1.1 thorpej
124 1.1 thorpej #ifdef DEBUG
125 1.1 thorpej printf("net_mountroot\n");
126 1.1 thorpej #endif
127 1.2 drochner
128 1.1 thorpej /*
129 1.1 thorpej * Get info for NFS boot: our IP address, out hostname,
130 1.1 thorpej * server IP address, and our root path on the server.
131 1.1 thorpej * We use BOOTP (RFC951, RFC1532) exclusively as mandated
132 1.1 thorpej * by PowerPC Reference Platform Specification I.4.2
133 1.1 thorpej */
134 1.2 drochner
135 1.1 thorpej bootp(netdev_sock);
136 1.2 drochner
137 1.1 thorpej if (myip.s_addr == 0)
138 1.1 thorpej return ETIMEDOUT;
139 1.2 drochner
140 1.1 thorpej printf("Using IP address: %s\n", inet_ntoa(myip));
141 1.2 drochner
142 1.1 thorpej #ifdef DEBUG
143 1.1 thorpej printf("myip: %s (%s)", hostname, inet_ntoa(myip));
144 1.1 thorpej if (gateip.s_addr)
145 1.1 thorpej printf(", gateip: %s", inet_ntoa(gateip));
146 1.1 thorpej if (netmask)
147 1.1 thorpej printf(", netmask: %s", intoa(netmask));
148 1.1 thorpej printf("\n");
149 1.1 thorpej #endif
150 1.1 thorpej printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
151 1.2 drochner
152 1.1 thorpej /*
153 1.1 thorpej * Get the NFS file handle (mount).
154 1.1 thorpej */
155 1.1 thorpej if (nfs_mount(netdev_sock, rootip, rootpath) < 0)
156 1.1 thorpej return errno;
157 1.1 thorpej return 0;
158 1.1 thorpej }
159