From a4249dc0b100fe3873cdc67346616e7d96af88c0 Mon Sep 17 00:00:00 2001 From: patacongo Date: Tue, 6 Mar 2012 00:26:14 +0000 Subject: RPC update git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4456 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/fs/nfs/Make.defs | 2 +- nuttx/fs/nfs/rpc_idgen_sub.c | 192 +++++++++++++++++++++++++++++++++++++++++++ nuttx/fs/nfs/rpc_mbuf.h | 14 ++-- nuttx/fs/nfs/rpc_subr.c | 125 +++------------------------- nuttx/fs/nfs/rpc_types.h | 1 - 5 files changed, 211 insertions(+), 123 deletions(-) create mode 100644 nuttx/fs/nfs/rpc_idgen_sub.c diff --git a/nuttx/fs/nfs/Make.defs b/nuttx/fs/nfs/Make.defs index c0a19e8b2..f0431d289 100644 --- a/nuttx/fs/nfs/Make.defs +++ b/nuttx/fs/nfs/Make.defs @@ -42,7 +42,7 @@ CSRCS += # Files required for NFS RPC ASRCS += -CSRCS += rpc_subr.c +CSRCS += rpc_subr.c rpc_idgen_sub.c # Argument for dependency checking diff --git a/nuttx/fs/nfs/rpc_idgen_sub.c b/nuttx/fs/nfs/rpc_idgen_sub.c new file mode 100644 index 000000000..99b400783 --- /dev/null +++ b/nuttx/fs/nfs/rpc_idgen_sub.c @@ -0,0 +1,192 @@ +/**************************************************************************** + * fs/nfs/rpc_subr.c + * Non-repeating ID generation covering an almost maximal 32 bit range. + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2012 Jose Pablo Rojas Vargas. All rights reserved. + * Author: Jose Pablo Rojas Vargas + * + * Leveraged from OpenBSD: + * + * Copyright (c) 2008 Damien Miller + * Based on public domain SKIP32 by Greg Rose. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include + +#include"rpc_idgen.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const uint8_t ftable[256] = +{ + 0xa3, 0xd7, 0x09, 0x83, 0xf8, 0x48, 0xf6, 0xf4, + 0xb3, 0x21, 0x15, 0x78, 0x99, 0xb1, 0xaf, 0xf9, + 0xe7, 0x2d, 0x4d, 0x8a, 0xce, 0x4c, 0xca, 0x2e, + 0x52, 0x95, 0xd9, 0x1e, 0x4e, 0x38, 0x44, 0x28, + 0x0a, 0xdf, 0x02, 0xa0, 0x17, 0xf1, 0x60, 0x68, + 0x12, 0xb7, 0x7a, 0xc3, 0xe9, 0xfa, 0x3d, 0x53, + 0x96, 0x84, 0x6b, 0xba, 0xf2, 0x63, 0x9a, 0x19, + 0x7c, 0xae, 0xe5, 0xf5, 0xf7, 0x16, 0x6a, 0xa2, + 0x39, 0xb6, 0x7b, 0x0f, 0xc1, 0x93, 0x81, 0x1b, + 0xee, 0xb4, 0x1a, 0xea, 0xd0, 0x91, 0x2f, 0xb8, + 0x55, 0xb9, 0xda, 0x85, 0x3f, 0x41, 0xbf, 0xe0, + 0x5a, 0x58, 0x80, 0x5f, 0x66, 0x0b, 0xd8, 0x90, + 0x35, 0xd5, 0xc0, 0xa7, 0x33, 0x06, 0x65, 0x69, + 0x45, 0x00, 0x94, 0x56, 0x6d, 0x98, 0x9b, 0x76, + 0x97, 0xfc, 0xb2, 0xc2, 0xb0, 0xfe, 0xdb, 0x20, + 0xe1, 0xeb, 0xd6, 0xe4, 0xdd, 0x47, 0x4a, 0x1d, + 0x42, 0xed, 0x9e, 0x6e, 0x49, 0x3c, 0xcd, 0x43, + 0x27, 0xd2, 0x07, 0xd4, 0xde, 0xc7, 0x67, 0x18, + 0x89, 0xcb, 0x30, 0x1f, 0x8d, 0xc6, 0x8f, 0xaa, + 0xc8, 0x74, 0xdc, 0xc9, 0x5d, 0x5c, 0x31, 0xa4, + 0x70, 0x88, 0x61, 0x2c, 0x9f, 0x0d, 0x2b, 0x87, + 0x50, 0x82, 0x54, 0x64, 0x26, 0x7d, 0x03, 0x40, + 0x34, 0x4b, 0x1c, 0x73, 0xd1, 0xc4, 0xfd, 0x3b, + 0xcc, 0xfb, 0x7f, 0xab, 0xe6, 0x3e, 0x5b, 0xa5, + 0xad, 0x04, 0x23, 0x9c, 0x14, 0x51, 0x22, 0xf0, + 0x29, 0x79, 0x71, 0x7e, 0xff, 0x8c, 0x0e, 0xe2, + 0x0c, 0xef, 0xbc, 0x72, 0x75, 0x6f, 0x37, 0xa1, + 0xec, 0xd3, 0x8e, 0x62, 0x8b, 0x86, 0x10, 0xe8, + 0x08, 0x77, 0x11, 0xbe, 0x92, 0x4f, 0x24, 0xc5, + 0x32, 0x36, 0x9d, 0xcf, 0xf3, 0xa6, 0xbb, 0xac, + 0x5e, 0x6c, 0xa9, 0x13, 0x57, 0x25, 0xb5, 0xe3, + 0xbd, 0xa8, 0x3a, 0x01, 0x05, 0x59, 0x2a, 0x46 +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static uint16_t idgen32_g(uint8_t * key, int k, uint16_t w) +{ + uint8_t g1, g2, g3, g4, g5, g6; + unsigned int o = k * 4; + + g1 = (w >> 8) & 0xff; + g2 = w & 0xff; + + g3 = ftable[g2 ^ key[o++ & (IDGEN32_KEYLEN - 1)]] ^ g1; + g4 = ftable[g3 ^ key[o++ & (IDGEN32_KEYLEN - 1)]] ^ g2; + g5 = ftable[g4 ^ key[o++ & (IDGEN32_KEYLEN - 1)]] ^ g3; + g6 = ftable[g5 ^ key[o++ & (IDGEN32_KEYLEN - 1)]] ^ g4; + + return (g5 << 8) | g6; +} + +static uint32_t idgen32_permute(uint8_t key[IDGEN32_KEYLEN], uint32_t in) +{ + unsigned int i, r; + uint16_t wl, wr; + + wl = (in >> 16) & 0x7fff; + wr = in & 0xffff; + + /* Doubled up rounds, with an odd round at the end to swap */ + for (i = r = 0; i < IDGEN32_ROUNDS / 2; ++i) + { + wr ^= (idgen32_g(key, r, wl) ^ r); + r++; + wl ^= (idgen32_g(key, r, wr) ^ r) & 0x7fff; + r++; + } + wr ^= (idgen32_g(key, r, wl) ^ r); + + return (wl << 16) | wr; +} + +static void idgen32_rekey(struct idgen32_ctx *ctx) +{ + srand(time(NULL)); + ctx->id_counter = 0; + ctx->id_hibit ^= 0x80000000; + // ctx->id_offset = arc4random(); + ctx->id_offset = rand(); + // arc4random_buf(ctx->id_key, sizeof(ctx->id_key)); + ctx->id_key[0] = rand(); + ctx->id_key[1] = rand(); + ctx->id_key[2] = rand(); + ctx->id_key[3] = rand(); + ctx->id_key[4] = rand(); + ctx->id_key[5] = rand(); + ctx->id_key[6] = rand(); + ctx->id_key[7] = rand(); + ctx->id_key[8] = rand(); + ctx->id_key[9] = rand(); + ctx->id_key[10] = rand(); + ctx->id_key[11] = rand(); + ctx->id_key[12] = rand(); + ctx->id_key[13] = rand(); + ctx->id_key[14] = rand(); + ctx->id_key[15] = rand(); + ctx->id_key[16] = rand(); + ctx->id_key[17] = rand(); + ctx->id_key[18] = rand(); + ctx->id_key[19] = rand(); + ctx->id_key[20] = rand(); + ctx->id_key[21] = rand(); + ctx->id_key[22] = rand(); + ctx->id_key[23] = rand(); + ctx->id_key[24] = rand(); + ctx->id_key[25] = rand(); + ctx->id_key[26] = rand(); + ctx->id_key[27] = rand(); + ctx->id_key[28] = rand(); + ctx->id_key[29] = rand(); + ctx->id_key[30] = rand(); + ctx->id_key[31] = rand(); + ctx->id_rekey_time = IDGEN32_REKEY_TIME; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +void idgen32_init(struct idgen32_ctx *ctx) +{ + srand(time(NULL)); + memset(ctx, 0, sizeof(*ctx)); + ctx->id_hibit = rand() & 0x80000000; + idgen32_rekey(ctx); +} + +uint32_t idgen32(struct idgen32_ctx *ctx) +{ + uint32_t ret; + + /* Avoid emitting a zero ID as they often have special meaning */ + do + { + ret = idgen32_permute(ctx->id_key, ctx->id_offset + ctx->id_counter++); + + /* Rekey a little early to avoid "card counting" attack */ + if (ctx->id_counter > IDGEN32_REKEY_LIMIT || ctx->id_rekey_time > 0) + idgen32_rekey(ctx); + } + while (ret == 0); + + return ret | ctx->id_hibit; +} diff --git a/nuttx/fs/nfs/rpc_mbuf.h b/nuttx/fs/nfs/rpc_mbuf.h index d94d329fb..f820f8045 100644 --- a/nuttx/fs/nfs/rpc_mbuf.h +++ b/nuttx/fs/nfs/rpc_mbuf.h @@ -193,7 +193,7 @@ */ #define MEXTADD(m, buf, size, type, free, arg) do { \ - (m)->m_data = (m)->m_ext.ext_buf = (caddr_t)(buf); \ + (m)->m_data = (m)->m_ext.ext_buf = (char*)(buf); \ (m)->m_flags |= M_EXT; \ (m)->m_flags &= ~M_CLUSTER; \ (m)->m_ext.ext_size = (size); \ @@ -309,20 +309,20 @@ struct m_hdr struct pkthdr { - struct ifnet *rcvif; /* rcv interface */ - int len; /* Total packet length */ + struct ifnet *rcvif; /* rcv interface */ + int len; /* Total packet length */ }; /* Description of external storage mapped into mbuf, valid if M_EXT set */ struct mbuf_ext { - caddr_t ext_buf; /* start of buffer */ + char* ext_buf; /* start of buffer */ /* free routine if not the usual */ // void (*ext_free)(); // void *ext_arg; /* argument for ext_free */ // unsigned int ext_size; /* size of buffer, for ext_free */ - unsigned int ext_size; /* size of buffer, for ext_free */ + unsigned int ext_size; /* size of buffer, for ext_free */ }; struct mbuf @@ -423,13 +423,13 @@ void m_adj(struct mbuf *, int); int m_copyback(struct mbuf *, int, int, const void *, int); void m_freem(struct mbuf *); void m_reclaim(void *, int); -void m_copydata(struct mbuf *, int, int, caddr_t); +void m_copydata(struct mbuf *, int, int, char*); void m_cat(struct mbuf *, struct mbuf *); struct mbuf *m_devget(char *, int, int, struct ifnet *, void (*)(const void *, void *, size_t)); void m_zero(struct mbuf *); int m_apply(struct mbuf *, int, int, - int (*)(caddr_t, caddr_t, unsigned int), caddr_t); + int (*)(char*, char*, unsigned int), char*); int m_dup_pkthdr(struct mbuf *, struct mbuf *, int); /* Packet tag routines */ diff --git a/nuttx/fs/nfs/rpc_subr.c b/nuttx/fs/nfs/rpc_subr.c index 2cfc5a180..9785a9752 100644 --- a/nuttx/fs/nfs/rpc_subr.c +++ b/nuttx/fs/nfs/rpc_subr.c @@ -43,9 +43,6 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * partially based on: - * libnetboot/rpc.c - * ****************************************************************************/ /**************************************************************************** @@ -55,13 +52,11 @@ #include #include #include -#include #include #include #include #include "rpc_mbuf.h" -#include "rpc_types.h" #include "rpc_v2.h" #include "rpc.h" #include "xdr_subs.h" @@ -80,7 +75,6 @@ */ #define MAX_RESEND_DELAY 5 /* seconds */ -#define IP_PORTRANGE 19 /* int; range to choose for unspec port */ /**************************************************************************** * Private Types @@ -221,10 +215,7 @@ int krpc_portmap(struct sockaddr_in *sin, unsigned int prog, unsigned int vers, } /* Do a remote procedure call (RPC) and wait for its reply. - * If from_p is non-null, then we are doing broadcast, and - * the address from whence the response came is saved there. * data: input/output - * from_p: output */ int krpc_call(struct sockaddr_in *sa, unsigned int prog, unsigned int vers, @@ -232,13 +223,11 @@ int krpc_call(struct sockaddr_in *sa, unsigned int prog, unsigned int vers, { struct socket *so; struct sockaddr_in *sin; - struct mbuf *m, *nam, *mhead, *from; + struct sockaddr *addr; struct rpc_call *call; struct rpc_reply *reply; - struct uio auio; - int error, rcvflg, timo, secs, len; + int error, rcvflg, timo, secs; static uint32_t xid = 0; - int *ip; struct timeval *tv; /* Validate address family. @@ -265,36 +254,16 @@ int krpc_call(struct sockaddr_in *sa, unsigned int prog, unsigned int vers, goto out; } - /* Enable broadcast if necessary. */ - - if (from_p) - { - int32_t *on; - *on = 1; - - if ((error = psock_setsockopt(so, SOL_SOCKET, SO_BROADCAST, (const void *) on, sizeof (*on)))) - { - goto out; - } - } - /* Bind the local endpoint to a reserved port, * because some NFS servers refuse requests from * non-reserved (non-privileged) ports. */ - *ip = 2; - error = psock_setsockopt(so, IPPROTO_IP, IP_PORTRANGE, (const void *) ip, sizeof (*ip)); - if (error) - { - goto out; - } - - sin->sin_len = sizeof(struct sockaddr_in); sin->sin_family = AF_INET; - sin->sin_addr.s_addr = INADDR_ANY; sin->sin_port = htons(0); - error = psock_bind(so, m, &proc0); + sin->sin_addr.s_addr = INADDR_ANY; + + error = psock_bind(so, addr, sizeof(*addr)); if (error) { @@ -302,20 +271,13 @@ int krpc_call(struct sockaddr_in *sa, unsigned int prog, unsigned int vers, goto out; } - *ip = 0; - error = psock_setsockopt(so, IPPROTO_IP, IP_PORTRANGE, (const void *) ip, sizeof (*ip)); - if (error) - { - goto out; - } - /* Setup socket address for the server. */ - bcopy((caddr_t *) sa, (caddr_t *) sin, sa->sin_len); + memmove((char*)sin,(char*)sa, sizeof(*sa)); /* Prepend RPC message header. */ - bzero((caddr_t *) call, sizeof(*call)); + memset((char*) call, 0, sizeof(*call)); /* rpc_call part */ @@ -349,14 +311,13 @@ int krpc_call(struct sockaddr_in *sa, unsigned int prog, unsigned int vers, { /* Send RPC request (or re-send). */ - error = sosend(so, nam, NULL, m, NULL, 0); + error = psock_send(so, call, sizeof(*call), 0); if (error) { - printf("krpc_call: sosend: %d\n", error); + printf("krpc_call: psock_send: %d\n", error); goto out; } - m = NULL; /* Determine new timeout. */ @@ -377,23 +338,8 @@ int krpc_call(struct sockaddr_in *sa, unsigned int prog, unsigned int vers, secs = timo; while (secs > 0) { - if (from) - { - m_freem(from); - from = NULL; - } - - if (m) - { - m_freem(m); - m = NULL; - } - - auio.uio_resid = len = 1 << 16; - auio.uio_procp = NULL; rcvflg = 0; - - error = soreceive(so, &from, &auio, &m, NULL, &rcvflg, 0); + error = psock_recvfrom(so, reply, sizeof(*reply), rcvflg, NULL, 0); if (error == EWOULDBLOCK) { secs--; @@ -405,22 +351,7 @@ int krpc_call(struct sockaddr_in *sa, unsigned int prog, unsigned int vers, goto out; } - len -= auio.uio_resid; - - /* Does the reply contain at least a header? */ - - if (len < MIN_REPLY_HDR) - { - continue; - } - - if (m->m_len < MIN_REPLY_HDR) - { - continue; - } - - reply = mtod(m, struct rpc_reply *); - + /* Is it the right reply? */ if (reply->rp_direction != txdr_unsigned(RPC_REPLY)) @@ -450,46 +381,12 @@ int krpc_call(struct sockaddr_in *sa, unsigned int prog, unsigned int vers, printf("rpc denied, status=%d\n", error); continue; } - - goto gotreply; /* break two levels */ } } error = ETIMEDOUT; goto out; -gotreply: - - /* Get RPC reply header into first mbuf, - * get its length, then strip it off. - */ - - len = sizeof(*reply); - if (m->m_len < len) - { - m = m_pullup(m, len); - if (m == NULL) - { - error = ENOBUFS; - goto out; - } - } - - if (reply->rp_auth.authtype != 0) - { - len += fxdr_unsigned(uint32_t, reply->rp_auth.authlen); - len = (len + 3) & ~3; /* XXX? */ - } - - /* result */ - - *data = m; - if (from_p && error == 0) - { - *from_p = from; - from = NULL; - } - out: (void)psock_close(so); return error; diff --git a/nuttx/fs/nfs/rpc_types.h b/nuttx/fs/nfs/rpc_types.h index e1ec76e6a..df4b3dfe2 100644 --- a/nuttx/fs/nfs/rpc_types.h +++ b/nuttx/fs/nfs/rpc_types.h @@ -87,7 +87,6 @@ struct uio ssize_t uio_resid; /* remaining bytes to process */ enum uio_seg uio_segflg; /* address space */ enum uio_rw uio_rw; /* operation */ - struct thread *uio_procp; /* owner */ }; struct componentname -- cgit v1.2.3