xref: /netbsd/sys/nfs/nfs_bootstatic.c (revision 6550d01e)
1 /*	$NetBSD: nfs_bootstatic.c,v 1.8 2009/10/23 02:32:34 snj 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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: nfs_bootstatic.c,v 1.8 2009/10/23 02:32:34 snj Exp $");
32 
33 #ifdef _KERNEL_OPT
34 #include "opt_nfs_boot.h"
35 #endif
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/ioctl.h>
41 #include <sys/proc.h>
42 #include <sys/mount.h>
43 #include <sys/mbuf.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 
47 #include <net/if.h>
48 #include <net/if_types.h>
49 #include <net/if_media.h>
50 #include <net/route.h>
51 #include <net/if_ether.h>
52 
53 #include <netinet/in.h>
54 #include <netinet/if_inarp.h>
55 
56 #include <nfs/rpcv2.h>
57 
58 #include <nfs/nfsproto.h>
59 #include <nfs/nfs.h>
60 #include <nfs/nfsmount.h>
61 #include <nfs/nfsdiskless.h>
62 
63 int (*nfs_bootstatic_callback)(struct nfs_diskless *) = NULL;
64 
65 int
66 nfs_bootstatic(struct nfs_diskless *nd, struct lwp *lwp, int *_flags)
67 {
68 	struct ifnet *ifp = nd->nd_ifp;
69 	struct sockaddr_in *sin;
70 	int error, flags;
71 
72 	if (nfs_bootstatic_callback)
73 		flags = (*nfs_bootstatic_callback)(nd);
74 	else
75 		flags = 0;
76 
77 	if (flags & NFS_BOOT_NOSTATIC)
78 		return EOPNOTSUPP;
79 
80 	if (flags == 0) {
81 #ifdef NFS_BOOTSTATIC_MYIP
82 		if (!(*_flags & NFS_BOOT_HAS_MYIP)) {
83 			nd->nd_myip.s_addr = inet_addr(NFS_BOOTSTATIC_MYIP);
84 			flags |= NFS_BOOT_HAS_MYIP;
85 		}
86 #endif
87 #ifdef NFS_BOOTSTATIC_GWIP
88 		if (!(*_flags & NFS_BOOT_HAS_GWIP)) {
89 			nd->nd_gwip.s_addr = inet_addr(NFS_BOOTSTATIC_GWIP);
90 			flags |= NFS_BOOT_HAS_GWIP;
91 		}
92 #endif
93 #ifdef NFS_BOOTSTATIC_MASK
94 		if (!(*_flags & NFS_BOOT_HAS_MASK)) {
95 			nd->nd_mask.s_addr = inet_addr(NFS_BOOTSTATIC_MASK);
96 			flags |= NFS_BOOT_HAS_MASK;
97 		}
98 #endif
99 #ifdef NFS_BOOTSTATIC_SERVADDR
100 		if (!(*_flags & NFS_BOOT_HAS_SERVADDR)) {
101 			sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
102 			memset((void *)sin, 0, sizeof(*sin));
103 			sin->sin_len = sizeof(*sin);
104 			sin->sin_family = AF_INET;
105 			sin->sin_addr.s_addr = inet_addr(NFS_BOOTSTATIC_SERVADDR);
106 			flags |= NFS_BOOT_HAS_SERVADDR;
107 		}
108 #endif
109 #ifdef NFS_BOOTSTATIC_SERVER
110 		if (!(*_flags & NFS_BOOT_HAS_SERVER)) {
111 			strncpy(nd->nd_root.ndm_host, NFS_BOOTSTATIC_SERVER,
112 				MNAMELEN);
113 			flags |= NFS_BOOT_HAS_SERVER;
114 			if (strchr(nd->nd_root.ndm_host, ':') != NULL)
115 				flags |= NFS_BOOT_HAS_ROOTPATH;
116 		}
117 #endif
118 	}
119 
120 	*_flags |= flags;
121 
122 	if (!(*_flags & NFS_BOOT_HAS_SERVADDR) && (*_flags & NFS_BOOT_HAS_SERVER)) {
123 		char rootserver[MNAMELEN];
124 		char *sep;
125 
126 		sep = strchr(nd->nd_root.ndm_host, ':');
127 		strlcpy(rootserver, nd->nd_root.ndm_host,
128 			sep - nd->nd_root.ndm_host+1);
129 
130 		sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
131 		memset((void *)sin, 0, sizeof(*sin));
132 		sin->sin_len = sizeof(*sin);
133 		sin->sin_family = AF_INET;
134 		sin->sin_addr.s_addr = inet_addr(rootserver);
135 		flags |= NFS_BOOT_HAS_SERVADDR;
136 		*_flags |= NFS_BOOT_HAS_SERVADDR;
137 	}
138 
139 	if (flags & NFS_BOOT_HAS_MYIP)
140 		aprint_normal("nfs_boot: client_addr=%s\n",
141 		    inet_ntoa(nd->nd_myip));
142 
143 	if (flags & NFS_BOOT_HAS_GWIP)
144 		aprint_normal("nfs_boot: gateway=%s\n",
145 		    inet_ntoa(nd->nd_gwip));
146 
147 	if (flags & NFS_BOOT_HAS_MASK)
148 		aprint_normal("nfs_boot: netmask=%s\n",
149 		    inet_ntoa(nd->nd_mask));
150 
151 	if (flags & NFS_BOOT_HAS_SERVADDR) {
152 		sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
153 		aprint_normal("nfs_boot: server=%s\n",
154 		    inet_ntoa(sin->sin_addr));
155 	}
156 
157 	if (flags & NFS_BOOT_HAS_SERVER)
158 		aprint_normal("nfs_boot: root=%.*s\n", MNAMELEN,
159 		    nd->nd_root.ndm_host);
160 
161 	/*
162 	 * Do enough of ifconfig(8) so that the chosen interface
163 	 * can talk to the servers.
164 	 */
165 	error = nfs_boot_setaddress(ifp, lwp,
166 	    *_flags & NFS_BOOT_HAS_MYIP ? nd->nd_myip.s_addr : INADDR_ANY,
167 	    *_flags & NFS_BOOT_HAS_MASK ? nd->nd_mask.s_addr : INADDR_ANY,
168 	    INADDR_ANY);
169 	if (error) {
170 		aprint_error("nfs_boot: set ifaddr, error=%d\n", error);
171 		goto out;
172 	}
173 
174 	if ((*_flags & NFS_BOOT_ALLINFO) != NFS_BOOT_ALLINFO)
175 		return EADDRNOTAVAIL;
176 
177 	error = 0;
178 
179  out:
180 
181 	return error;
182 }
183