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