1 /* 2 * Copyright (c) 1995 Gordon Ross, Adam Glass 3 * Copyright (c) 1992 Regents of the University of California. 4 * All rights reserved. 5 * 6 * This software was developed by the Computer Systems Engineering group 7 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 8 * contributed to Berkeley. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Lawrence Berkeley Laboratory and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * nfs/krpc_subr.c 39 * $NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $ 40 * $FreeBSD: src/sys/nfs/bootp_subr.c,v 1.20.2.9 2003/04/24 16:51:08 ambrisko Exp $ 41 * $DragonFly: src/sys/vfs/nfs/nfs_mountrpc.c,v 1.2 2006/12/23 00:41:29 swildner Exp $ 42 */ 43 /* 44 * Procedures used by NFS_ROOT and BOOTP to do an NFS mount rpc to obtain 45 * the nfs root file handle for a NFS-based root mount point. This module 46 * is not used by normal operating code because the 'mount' command has a 47 * far more sophisticated implementation. 48 */ 49 #include "opt_bootp.h" 50 #include "opt_nfsroot.h" 51 52 #if defined(BOOTP) || defined(NFS_ROOT) 53 54 #include <sys/param.h> 55 #include <sys/systm.h> 56 #include <sys/kernel.h> 57 #include <sys/sockio.h> 58 #include <sys/proc.h> 59 #include <sys/malloc.h> 60 #include <sys/mount.h> 61 #include <sys/mbuf.h> 62 #include <sys/socket.h> 63 #include <sys/socketvar.h> 64 #include <sys/sysctl.h> 65 #include <sys/uio.h> 66 67 #include <net/if.h> 68 #include <net/route.h> 69 70 #include <netinet/in.h> 71 #include <net/if_types.h> 72 #include <net/if_dl.h> 73 74 #include "rpcv2.h" 75 #include "nfsproto.h" 76 #include "nfs.h" 77 #include "nfsdiskless.h" 78 #include "krpc.h" 79 #include "xdr_subs.h" 80 #include "nfsmountrpc.h" 81 82 /* 83 * What is the longest we will wait before re-sending a request? 84 * Note this is also the frequency of "RPC timeout" messages. 85 * The re-send loop count sup linearly to this maximum, so the 86 * first complaint will happen after (1+2+3+4+5)=15 seconds. 87 */ 88 89 static int getdec(char **ptr); 90 static char *substr(char *a,char *b); 91 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len); 92 static int xdr_int_decode(struct mbuf **ptr, int *iptr); 93 94 void 95 nfs_mountopts(struct nfs_args *args, char *p) 96 { 97 char *tmp; 98 99 args->version = NFS_ARGSVERSION; 100 args->rsize = 8192; 101 args->wsize = 8192; 102 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT; 103 args->sotype = SOCK_STREAM; 104 if (p == NULL) 105 return; 106 if ((tmp = (char *)substr(p, "rsize="))) 107 args->rsize = getdec(&tmp); 108 if ((tmp = (char *)substr(p, "wsize="))) 109 args->wsize = getdec(&tmp); 110 if ((tmp = (char *)substr(p, "intr"))) 111 args->flags |= NFSMNT_INT; 112 if ((tmp = (char *)substr(p, "soft"))) 113 args->flags |= NFSMNT_SOFT; 114 if ((tmp = (char *)substr(p, "noconn"))) 115 args->flags |= NFSMNT_NOCONN; 116 if ((tmp = (char *)substr(p, "udp"))) 117 args->sotype = SOCK_DGRAM; 118 } 119 120 /* 121 * RPC: mountd/mount 122 * Given a server pathname, get an NFS file handle. 123 * Also, sets sin->sin_port to the NFS service port. 124 */ 125 int 126 md_mount(struct sockaddr_in *mdsin, /* mountd server address */ 127 char *path, 128 u_char *fhp, 129 int *fhsizep, 130 struct nfs_args *args, 131 struct thread *td) 132 { 133 struct mbuf *m; 134 int error; 135 int authunixok; 136 int authcount; 137 int authver; 138 139 /* First try NFS v3 */ 140 /* Get port number for MOUNTD. */ 141 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3, 142 &mdsin->sin_port, td); 143 if (error == 0) { 144 m = xdr_string_encode(path, strlen(path)); 145 146 /* Do RPC to mountd. */ 147 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3, 148 RPCMNT_MOUNT, &m, NULL, td); 149 } 150 if (error == 0) { 151 args->flags |= NFSMNT_NFSV3; 152 } else { 153 /* Fallback to NFS v2 */ 154 155 /* Get port number for MOUNTD. */ 156 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1, 157 &mdsin->sin_port, td); 158 if (error != 0) 159 return error; 160 161 m = xdr_string_encode(path, strlen(path)); 162 163 /* Do RPC to mountd. */ 164 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1, 165 RPCMNT_MOUNT, &m, NULL, td); 166 if (error != 0) 167 return error; /* message already freed */ 168 args->flags &= ~NFSMNT_NFSV3; 169 } 170 171 if (xdr_int_decode(&m, &error) != 0 || error != 0) 172 goto bad; 173 174 if ((args->flags & NFSMNT_NFSV3) != 0) { 175 if (xdr_int_decode(&m, fhsizep) != 0 || 176 *fhsizep > NFSX_V3FHMAX || 177 *fhsizep <= 0) 178 goto bad; 179 } else 180 *fhsizep = NFSX_V2FH; 181 182 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0) 183 goto bad; 184 185 if (args->flags & NFSMNT_NFSV3) { 186 if (xdr_int_decode(&m, &authcount) != 0) 187 goto bad; 188 authunixok = 0; 189 if (authcount < 0 || authcount > 100) 190 goto bad; 191 while (authcount > 0) { 192 if (xdr_int_decode(&m, &authver) != 0) 193 goto bad; 194 if (authver == RPCAUTH_UNIX) 195 authunixok = 1; 196 authcount--; 197 } 198 if (authunixok == 0) 199 goto bad; 200 } 201 202 /* Set port number for NFS use. */ 203 error = krpc_portmap(mdsin, NFS_PROG, 204 (args->flags & 205 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2, 206 &mdsin->sin_port, td); 207 208 goto out; 209 210 bad: 211 error = EBADRPC; 212 213 out: 214 m_freem(m); 215 return error; 216 } 217 218 int 219 md_lookup_swap(struct sockaddr_in *mdsin, /* mountd server address */ 220 char *path, 221 u_char *fhp, 222 int *fhsizep, 223 struct nfs_args *args, 224 struct thread *td) 225 { 226 struct mbuf *m; 227 int error; 228 int size = -1; 229 int attribs_present; 230 int status; 231 union { 232 u_int32_t v2[17]; 233 u_int32_t v3[21]; 234 } fattribs; 235 236 m = m_get(MB_WAIT,MT_DATA); 237 if (m == NULL) 238 return ENOBUFS; 239 240 if ((args->flags & NFSMNT_NFSV3) != 0) { 241 *mtod(m, u_int32_t *) = txdr_unsigned(*fhsizep); 242 bcopy(fhp, mtod(m, u_char *) + sizeof(u_int32_t), *fhsizep); 243 m->m_len = *fhsizep + sizeof(u_int32_t); 244 } else { 245 bcopy(fhp, mtod(m, u_char *), NFSX_V2FH); 246 m->m_len = NFSX_V2FH; 247 } 248 249 m->m_next = xdr_string_encode(path, strlen(path)); 250 if (m->m_next == NULL) { 251 error = ENOBUFS; 252 goto out; 253 } 254 255 /* Do RPC to nfsd. */ 256 if ((args->flags & NFSMNT_NFSV3) != 0) 257 error = krpc_call(mdsin, NFS_PROG, NFS_VER3, 258 NFSPROC_LOOKUP, &m, NULL, td); 259 else 260 error = krpc_call(mdsin, NFS_PROG, NFS_VER2, 261 NFSV2PROC_LOOKUP, &m, NULL, td); 262 if (error != 0) 263 return error; /* message already freed */ 264 265 if (xdr_int_decode(&m, &status) != 0) 266 goto bad; 267 if (status != 0) { 268 error = ENOENT; 269 goto out; 270 } 271 272 if ((args->flags & NFSMNT_NFSV3) != 0) { 273 if (xdr_int_decode(&m, fhsizep) != 0 || 274 *fhsizep > NFSX_V3FHMAX || 275 *fhsizep <= 0) 276 goto bad; 277 } else 278 *fhsizep = NFSX_V2FH; 279 280 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0) 281 goto bad; 282 283 if ((args->flags & NFSMNT_NFSV3) != 0) { 284 if (xdr_int_decode(&m, &attribs_present) != 0) 285 goto bad; 286 if (attribs_present != 0) { 287 if (xdr_opaque_decode(&m, (u_char *) &fattribs.v3, 288 sizeof(u_int32_t) * 21) != 0) 289 goto bad; 290 size = fxdr_unsigned(u_int32_t, fattribs.v3[6]); 291 } 292 } else { 293 if (xdr_opaque_decode(&m,(u_char *) &fattribs.v2, 294 sizeof(u_int32_t) * 17) != 0) 295 goto bad; 296 size = fxdr_unsigned(u_int32_t, fattribs.v2[5]); 297 } 298 299 if (nfsv3_diskless.swap_nblks == 0 && size != -1) { 300 nfsv3_diskless.swap_nblks = size / 1024; 301 kprintf("md_lookup_swap: Swap size is %d KB\n", 302 nfsv3_diskless.swap_nblks); 303 } 304 305 goto out; 306 307 bad: 308 error = EBADRPC; 309 310 out: 311 m_freem(m); 312 return error; 313 } 314 315 int 316 setfs(struct sockaddr_in *addr, char *path, char *p) 317 { 318 unsigned int ip; 319 int val; 320 321 ip = 0; 322 if (((val = getdec(&p)) < 0) || (val > 255)) 323 return 0; 324 ip = val << 24; 325 if (*p != '.') 326 return 0; 327 p++; 328 if (((val = getdec(&p)) < 0) || (val > 255)) 329 return 0; 330 ip |= (val << 16); 331 if (*p != '.') 332 return 0; 333 p++; 334 if (((val = getdec(&p)) < 0) || (val > 255)) 335 return 0; 336 ip |= (val << 8); 337 if (*p != '.') 338 return 0; 339 p++; 340 if (((val = getdec(&p)) < 0) || (val > 255)) 341 return 0; 342 ip |= val; 343 if (*p != ':') 344 return 0; 345 p++; 346 347 addr->sin_addr.s_addr = htonl(ip); 348 addr->sin_len = sizeof(struct sockaddr_in); 349 addr->sin_family = AF_INET; 350 351 strncpy(path, p, MNAMELEN - 1); 352 return 1; 353 } 354 355 static int 356 getdec(char **ptr) 357 { 358 char *p; 359 int ret; 360 361 p = *ptr; 362 ret = 0; 363 if ((*p < '0') || (*p > '9')) 364 return -1; 365 while ((*p >= '0') && (*p <= '9')) { 366 ret = ret * 10 + (*p - '0'); 367 p++; 368 } 369 *ptr = p; 370 return ret; 371 } 372 373 static char * 374 substr(char *a, char *b) 375 { 376 char *loc1; 377 char *loc2; 378 379 while (*a != '\0') { 380 loc1 = a; 381 loc2 = b; 382 while (*loc1 == *loc2++) { 383 if (*loc1 == '\0') 384 return 0; 385 loc1++; 386 if (*loc2 == '\0') 387 return loc1; 388 } 389 a++; 390 } 391 return 0; 392 } 393 394 static int 395 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len) 396 { 397 struct mbuf *m; 398 int alignedlen; 399 400 m = *mptr; 401 alignedlen = ( len + 3 ) & ~3; 402 403 if (m->m_len < alignedlen) { 404 m = m_pullup(m, alignedlen); 405 if (m == NULL) { 406 *mptr = NULL; 407 return EBADRPC; 408 } 409 } 410 bcopy(mtod(m, u_char *), buf, len); 411 m_adj(m, alignedlen); 412 *mptr = m; 413 return 0; 414 } 415 416 static int 417 xdr_int_decode(struct mbuf **mptr, int *iptr) 418 { 419 u_int32_t i; 420 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0) 421 return EBADRPC; 422 *iptr = fxdr_unsigned(u_int32_t, i); 423 return 0; 424 } 425 426 #endif /* BOOTP && NFS_ROOT */ 427 428