1 /* $NetBSD: nfs_bootstatic.c,v 1.6 2008/10/27 10:58:22 cegger Exp $ */
2
3 /*
4 *
5 * Copyright (c) 2004 Christian Limpach.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Christian Limpach.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: nfs_bootstatic.c,v 1.6 2008/10/27 10:58:22 cegger Exp $");
37
38 #include "opt_nfs_boot.h"
39 #include "opt_inet.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/ioctl.h>
45 #include <sys/proc.h>
46 #include <sys/mount.h>
47 #include <sys/mbuf.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50
51 #include <net/if.h>
52 #include <net/if_types.h>
53 #include <net/if_media.h>
54 #include <net/route.h>
55 #include <net/if_ether.h>
56
57 #include <netinet/in.h>
58 #include <netinet/if_inarp.h>
59
60 #include <nfs/rpcv2.h>
61
62 #include <nfs/nfsproto.h>
63 #include <nfs/nfs.h>
64 #include <nfs/nfsmount.h>
65 #include <nfs/nfsdiskless.h>
66
67 int (*nfs_bootstatic_callback)(struct nfs_diskless *) = NULL;
68
69 int
70 nfs_bootstatic(struct nfs_diskless *nd, struct lwp *lwp, int *_flags)
71 {
72 struct ifnet *ifp = nd->nd_ifp;
73 struct sockaddr_in *sin;
74 int error, flags;
75
76 if (nfs_bootstatic_callback)
77 flags = (*nfs_bootstatic_callback)(nd);
78 else
79 flags = 0;
80
81 if (flags & NFS_BOOT_NOSTATIC)
82 return EOPNOTSUPP;
83
84 if (flags == 0) {
85 #ifdef NFS_BOOTSTATIC_MYIP
86 if (!(*_flags & NFS_BOOT_HAS_MYIP)) {
87 nd->nd_myip.s_addr = inet_addr(NFS_BOOTSTATIC_MYIP);
88 flags |= NFS_BOOT_HAS_MYIP;
89 }
90 #endif
91 #ifdef NFS_BOOTSTATIC_GWIP
92 if (!(*_flags & NFS_BOOT_HAS_GWIP)) {
93 nd->nd_gwip.s_addr = inet_addr(NFS_BOOTSTATIC_GWIP);
94 flags |= NFS_BOOT_HAS_GWIP;
95 }
96 #endif
97 #ifdef NFS_BOOTSTATIC_MASK
98 if (!(*_flags & NFS_BOOT_HAS_MASK)) {
99 nd->nd_mask.s_addr = inet_addr(NFS_BOOTSTATIC_MASK);
100 flags |= NFS_BOOT_HAS_MASK;
101 }
102 #endif
103 #ifdef NFS_BOOTSTATIC_SERVADDR
104 if (!(*_flags & NFS_BOOT_HAS_SERVADDR)) {
105 sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
106 memset((void *)sin, 0, sizeof(*sin));
107 sin->sin_len = sizeof(*sin);
108 sin->sin_family = AF_INET;
109 sin->sin_addr.s_addr = inet_addr(NFS_BOOTSTATIC_SERVADDR);
110 flags |= NFS_BOOT_HAS_SERVADDR;
111 }
112 #endif
113 #ifdef NFS_BOOTSTATIC_SERVER
114 if (!(*_flags & NFS_BOOT_HAS_SERVER)) {
115 strncpy(nd->nd_root.ndm_host, NFS_BOOTSTATIC_SERVER,
116 MNAMELEN);
117 flags |= NFS_BOOT_HAS_SERVER;
118 if (strchr(nd->nd_root.ndm_host, ':') != NULL)
119 flags |= NFS_BOOT_HAS_ROOTPATH;
120 }
121 #endif
122 }
123
124 *_flags |= flags;
125
126 if (!(*_flags & NFS_BOOT_HAS_SERVADDR) && (*_flags & NFS_BOOT_HAS_SERVER)) {
127 char rootserver[MNAMELEN];
128 char *sep;
129
130 sep = strchr(nd->nd_root.ndm_host, ':');
131 strlcpy(rootserver, nd->nd_root.ndm_host,
132 sep - nd->nd_root.ndm_host+1);
133
134 sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
135 memset((void *)sin, 0, sizeof(*sin));
136 sin->sin_len = sizeof(*sin);
137 sin->sin_family = AF_INET;
138 sin->sin_addr.s_addr = inet_addr(rootserver);
139 flags |= NFS_BOOT_HAS_SERVADDR;
140 *_flags |= NFS_BOOT_HAS_SERVADDR;
141 }
142
143 if (flags & NFS_BOOT_HAS_MYIP)
144 aprint_normal("nfs_boot: client_addr=%s\n",
145 inet_ntoa(nd->nd_myip));
146
147 if (flags & NFS_BOOT_HAS_GWIP)
148 aprint_normal("nfs_boot: gateway=%s\n",
149 inet_ntoa(nd->nd_gwip));
150
151 if (flags & NFS_BOOT_HAS_MASK)
152 aprint_normal("nfs_boot: netmask=%s\n",
153 inet_ntoa(nd->nd_mask));
154
155 if (flags & NFS_BOOT_HAS_SERVADDR) {
156 sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
157 aprint_normal("nfs_boot: server=%s\n",
158 inet_ntoa(sin->sin_addr));
159 }
160
161 if (flags & NFS_BOOT_HAS_SERVER)
162 aprint_normal("nfs_boot: root=%.*s\n", MNAMELEN,
163 nd->nd_root.ndm_host);
164
165 /*
166 * Do enough of ifconfig(8) so that the chosen interface
167 * can talk to the servers.
168 */
169 error = nfs_boot_setaddress(ifp, lwp,
170 *_flags & NFS_BOOT_HAS_MYIP ? nd->nd_myip.s_addr : INADDR_ANY,
171 *_flags & NFS_BOOT_HAS_MASK ? nd->nd_mask.s_addr : INADDR_ANY,
172 INADDR_ANY);
173 if (error) {
174 aprint_error("nfs_boot: set ifaddr, error=%d\n", error);
175 goto out;
176 }
177
178 if ((*_flags & NFS_BOOT_ALLINFO) != NFS_BOOT_ALLINFO)
179 return EADDRNOTAVAIL;
180
181 error = 0;
182
183 out:
184
185 return error;
186 }
Cache object: 1dcdd89a65400182dec789dfaf6abe92
|