xref: /netbsd-src/sys/nfs/nfs_bootstatic.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: nfs_bootstatic.c,v 1.7 2008/11/19 18:36:09 ad 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.7 2008/11/19 18:36:09 ad Exp $");
37 
38 #ifdef _KERNEL_OPT
39 #include "opt_nfs_boot.h"
40 #endif
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/ioctl.h>
46 #include <sys/proc.h>
47 #include <sys/mount.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 
52 #include <net/if.h>
53 #include <net/if_types.h>
54 #include <net/if_media.h>
55 #include <net/route.h>
56 #include <net/if_ether.h>
57 
58 #include <netinet/in.h>
59 #include <netinet/if_inarp.h>
60 
61 #include <nfs/rpcv2.h>
62 
63 #include <nfs/nfsproto.h>
64 #include <nfs/nfs.h>
65 #include <nfs/nfsmount.h>
66 #include <nfs/nfsdiskless.h>
67 
68 int (*nfs_bootstatic_callback)(struct nfs_diskless *) = NULL;
69 
70 int
71 nfs_bootstatic(struct nfs_diskless *nd, struct lwp *lwp, int *_flags)
72 {
73 	struct ifnet *ifp = nd->nd_ifp;
74 	struct sockaddr_in *sin;
75 	int error, flags;
76 
77 	if (nfs_bootstatic_callback)
78 		flags = (*nfs_bootstatic_callback)(nd);
79 	else
80 		flags = 0;
81 
82 	if (flags & NFS_BOOT_NOSTATIC)
83 		return EOPNOTSUPP;
84 
85 	if (flags == 0) {
86 #ifdef NFS_BOOTSTATIC_MYIP
87 		if (!(*_flags & NFS_BOOT_HAS_MYIP)) {
88 			nd->nd_myip.s_addr = inet_addr(NFS_BOOTSTATIC_MYIP);
89 			flags |= NFS_BOOT_HAS_MYIP;
90 		}
91 #endif
92 #ifdef NFS_BOOTSTATIC_GWIP
93 		if (!(*_flags & NFS_BOOT_HAS_GWIP)) {
94 			nd->nd_gwip.s_addr = inet_addr(NFS_BOOTSTATIC_GWIP);
95 			flags |= NFS_BOOT_HAS_GWIP;
96 		}
97 #endif
98 #ifdef NFS_BOOTSTATIC_MASK
99 		if (!(*_flags & NFS_BOOT_HAS_MASK)) {
100 			nd->nd_mask.s_addr = inet_addr(NFS_BOOTSTATIC_MASK);
101 			flags |= NFS_BOOT_HAS_MASK;
102 		}
103 #endif
104 #ifdef NFS_BOOTSTATIC_SERVADDR
105 		if (!(*_flags & NFS_BOOT_HAS_SERVADDR)) {
106 			sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
107 			memset((void *)sin, 0, sizeof(*sin));
108 			sin->sin_len = sizeof(*sin);
109 			sin->sin_family = AF_INET;
110 			sin->sin_addr.s_addr = inet_addr(NFS_BOOTSTATIC_SERVADDR);
111 			flags |= NFS_BOOT_HAS_SERVADDR;
112 		}
113 #endif
114 #ifdef NFS_BOOTSTATIC_SERVER
115 		if (!(*_flags & NFS_BOOT_HAS_SERVER)) {
116 			strncpy(nd->nd_root.ndm_host, NFS_BOOTSTATIC_SERVER,
117 				MNAMELEN);
118 			flags |= NFS_BOOT_HAS_SERVER;
119 			if (strchr(nd->nd_root.ndm_host, ':') != NULL)
120 				flags |= NFS_BOOT_HAS_ROOTPATH;
121 		}
122 #endif
123 	}
124 
125 	*_flags |= flags;
126 
127 	if (!(*_flags & NFS_BOOT_HAS_SERVADDR) && (*_flags & NFS_BOOT_HAS_SERVER)) {
128 		char rootserver[MNAMELEN];
129 		char *sep;
130 
131 		sep = strchr(nd->nd_root.ndm_host, ':');
132 		strlcpy(rootserver, nd->nd_root.ndm_host,
133 			sep - nd->nd_root.ndm_host+1);
134 
135 		sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
136 		memset((void *)sin, 0, sizeof(*sin));
137 		sin->sin_len = sizeof(*sin);
138 		sin->sin_family = AF_INET;
139 		sin->sin_addr.s_addr = inet_addr(rootserver);
140 		flags |= NFS_BOOT_HAS_SERVADDR;
141 		*_flags |= NFS_BOOT_HAS_SERVADDR;
142 	}
143 
144 	if (flags & NFS_BOOT_HAS_MYIP)
145 		aprint_normal("nfs_boot: client_addr=%s\n",
146 		    inet_ntoa(nd->nd_myip));
147 
148 	if (flags & NFS_BOOT_HAS_GWIP)
149 		aprint_normal("nfs_boot: gateway=%s\n",
150 		    inet_ntoa(nd->nd_gwip));
151 
152 	if (flags & NFS_BOOT_HAS_MASK)
153 		aprint_normal("nfs_boot: netmask=%s\n",
154 		    inet_ntoa(nd->nd_mask));
155 
156 	if (flags & NFS_BOOT_HAS_SERVADDR) {
157 		sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
158 		aprint_normal("nfs_boot: server=%s\n",
159 		    inet_ntoa(sin->sin_addr));
160 	}
161 
162 	if (flags & NFS_BOOT_HAS_SERVER)
163 		aprint_normal("nfs_boot: root=%.*s\n", MNAMELEN,
164 		    nd->nd_root.ndm_host);
165 
166 	/*
167 	 * Do enough of ifconfig(8) so that the chosen interface
168 	 * can talk to the servers.
169 	 */
170 	error = nfs_boot_setaddress(ifp, lwp,
171 	    *_flags & NFS_BOOT_HAS_MYIP ? nd->nd_myip.s_addr : INADDR_ANY,
172 	    *_flags & NFS_BOOT_HAS_MASK ? nd->nd_mask.s_addr : INADDR_ANY,
173 	    INADDR_ANY);
174 	if (error) {
175 		aprint_error("nfs_boot: set ifaddr, error=%d\n", error);
176 		goto out;
177 	}
178 
179 	if ((*_flags & NFS_BOOT_ALLINFO) != NFS_BOOT_ALLINFO)
180 		return EADDRNOTAVAIL;
181 
182 	error = 0;
183 
184  out:
185 
186 	return error;
187 }
188