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