/*
* Copyright (C) 2002-2003 by Ryan Beasley <ryanb@goddamnbastard.org>
*
* See the IPFILTER.LICENCE file for details on licencing.
*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Overview:
* This is an in-kernel application proxy for Sun's RPCBIND (nee portmap)
* protocol as defined in RFC1833. It is far from complete, mostly
* lacking in less-likely corner cases, but it's definitely functional.
*
* Invocation:
* rdr <int> <e_ip>/32 port <e_p> -> <i_ip> port <i_p> udp proxy rpcbu
*
* If the host running IP Filter is the same as the RPC server, it's
* perfectly legal for both the internal and external addresses and ports
* to match.
*
* When triggered by appropriate IP NAT rules, this proxy works by
* examining data contained in received packets. Requests and replies are
* modified, NAT and state table entries created, etc., as necessary.
*/
/*
* TODO / NOTES
*
* o Must implement locking to protect proxy session data.
* o Fragmentation isn't supported.
* o Only supports UDP.
* o Doesn't support multiple RPC records in a single request.
* o Errors should be more fine-grained. (e.g., malloc failure vs.
* illegal RPCB request / reply)
* o Even with the limit on the total amount of recorded transactions,
* should there be a timeout on transaction removal?
* o There is a potential collision between cloning, wildcard NAT and
* state entries. There should be an appr_getport routine for
* to avoid this.
* o The enclosed hack of STREAMS support is pretty sick and most likely
* broken.
*
* $Id: ip_rpcb_pxy.c,v 2.25.2.3 2005/02/04 10:22:56 darrenr Exp $
*/
#define IPF_RPCB_PROXY
typedef struct ifs_rpcbpxy {
this proxy creates. */
/* XXX rpcbcnt still requires locking. */
int rpcb_proxy_init;
/*
* Function prototypes
*/
ifs_rpcbpxy_t *));
u_32_t **));
ifs_rpcbpxy_t *));
u_32_t **));
/*
* Since rpc_msg contains only pointers, one should use this macro as a
* handy way to get to the goods. (In case you're wondering about the name,
* this started as BYTEREF -> BREF -> B.)
*/
/*
* Public subroutines
*/
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_init */
/* Returns: int - 0 == success */
/* Parameters: (void) */
/* */
/* Initialize the filter rule entry and session limiter. */
/* -------------------------------------------------------------------- */
/*ARGSUSED*/
int
void **private;
{
return -1;
return(0);
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_fini */
/* Returns: void */
/* Parameters: (void) */
/* */
/* Destroy rpcbfr's mutex to avoid a lock leak. */
/* -------------------------------------------------------------------- */
/*ARGSUSED*/
void
void **private;
{
ifsrpcb->rpcb_proxy_init = 0;
}
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_new */
/* Returns: int - -1 == failure, 0 == success */
/* Parameters: fin(I) - pointer to packet information */
/* aps(I) - pointer to proxy session structure */
/* nat(I) - pointer to NAT session structure */
/* */
/* Allocate resources for per-session proxy structures. */
/* -------------------------------------------------------------------- */
/*ARGSUSED*/
int
void *private;
{
return(-1);
return(0);
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_del */
/* Returns: void */
/* Parameters: aps(I) - pointer to proxy session structure */
/* */
/* Free up a session's list of RPCB requests. */
/* -------------------------------------------------------------------- */
/*ARGSUSED*/
void
void *private;
{
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_in */
/* Returns: int - APR_ERR(1) == drop the packet, */
/* APR_ERR(2) == kill the proxy session, */
/* else change in packet length (in bytes) */
/* Parameters: fin(I) - pointer to packet information */
/* ip(I) - pointer to packet header */
/* aps(I) - pointer to proxy session structure */
/* nat(I) - pointer to NAT session structure */
/* */
/* Given a presumed RPCB request, perform some minor tests and pass off */
/* for decoding. Also pass packet off for a rewrite if necessary. */
/* -------------------------------------------------------------------- */
int
void *private;
{
mb_t *m;
int rv;
/* Disallow fragmented or illegally short packets. */
return(APR_ERR(1));
/* Perform basic variable initialization. */
/* Disallow packets outside legal range for supported requests. */
return(APR_ERR(1));
/* Copy packet over to convenience buffer. */
/* Send off to decode request. */
switch(rv)
{
case -1:
return(APR_ERR(1));
case 0:
break;
case 1:
break;
default:
/*CONSTANTCONDITION*/
}
return(rv);
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_out */
/* Returns: int - APR_ERR(1) == drop the packet, */
/* APR_ERR(2) == kill the proxy session, */
/* else change in packet length (in bytes) */
/* Parameters: fin(I) - pointer to packet information */
/* ip(I) - pointer to packet header */
/* aps(I) - pointer to proxy session structure */
/* nat(I) - pointer to NAT session structure */
/* */
/* Given a presumed RPCB reply, perform some minor tests and pass off */
/* for decoding. If the message indicates a successful request with */
/* valid addressing information, create NAT and state structures to */
/* allow direct communication between RPC client and server. */
/* -------------------------------------------------------------------- */
int
void *private;
{
mb_t *m;
/* Disallow fragmented or illegally short packets. */
return(APR_ERR(1));
/* Perform basic variable initialization. */
diff = 0;
/* Disallow packets outside legal range for supported requests. */
return(APR_ERR(1));
/* Copy packet over to convenience buffer. */
/* Send off to decode reply. */
switch(rv)
{
case -1: /* Bad packet */
}
return(APR_ERR(1));
case 0: /* Negative reply / request rejected */
break;
case 1: /* Positive reply */
/*
* With the IP address embedded in a GETADDR(LIST) reply,
* we'll need to rewrite the packet in the very possible
* event that the internal & external addresses aren't the
* same. (i.e., this box is either a router or rpcbind
* only listens on loopback.)
*/
}
break;
default:
/*CONSTANTCONDITION*/
}
/* XXX Gross hack - I'm overloading the reference
* counter to deal with both threads and retransmitted
* requests. One deref signals that this thread is
* finished with rx, and the other signals that we've
* processed its reply.
*/
}
return(diff);
}
/*
* Private support subroutines
*/
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_flush */
/* Returns: void */
/* Parameters: rs(I) - pointer to RPCB session structure */
/* */
/* Simply flushes the list of outstanding transactions, if any. */
/* -------------------------------------------------------------------- */
static void
{
return;
}
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_decodereq */
/* Returns: int - -1 == bad request or critical failure, */
/* 0 == request successfully decoded, */
/* 1 == request successfully decoded; requires */
/* address rewrite/modification */
/* Parameters: fin(I) - pointer to packet information */
/* nat(I) - pointer to NAT session structure */
/* rs(I) - pointer to RPCB session structure */
/* rm(I) - pointer to RPC message structure */
/* */
/* Take a presumed RPCB request, decode it, and store the results in */
/* the transaction list. If the internal target address needs to be */
/* modified, store its location in ptr. */
/* WARNING: It's the responsibility of the caller to make sure there */
/* is enough room in rs_buf for the basic RPC message "preamble". */
/* -------------------------------------------------------------------- */
static int
{
int mod;
mod = 0;
/* Parse out and test the RPC header. */
if ((B(p++) != RPCB_CALL) ||
(B(p++) != RPCB_MSG_VERSION) ||
(B(p++) != RPCB_PROG))
return(-1);
/* Record the RPCB version and procedure. */
/* Bypass RPC authentication stuff. */
return(-1);
return(-1);
/* Compare RPCB version and procedure numbers. */
{
case 2:
/* This proxy only supports PMAP_GETPORT. */
return(-1);
/* Portmap requests contain four 4 byte parameters. */
return(-1);
p += 2; /* Skip requested program and version numbers. */
/* Sanity check the requested protocol. */
xdr = B(p);
return(-1);
break;
case 3:
case 4:
/* GETADDRLIST is exclusive to v4; GETADDR for v3 & v4 */
{
case RPCB_GETADDR:
break;
case RPCB_GETADDRLIST:
return(-1);
break;
default:
return(-1);
}
/* Decode the 'struct rpcb' request. */
return(-1);
/* Are the target address & port valid? */
return(-1);
/* Do we need to rewrite this packet? */
mod = 1;
break;
default:
return(-1);
}
return(-1);
}
return(mod);
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_skipauth */
/* Returns: int -- -1 == illegal auth parameters (lengths) */
/* 0 == valid parameters, pointer advanced */
/* Parameters: rm(I) - pointer to RPC message structure */
/* auth(I) - pointer to RPC auth structure */
/* buf(IO) - pointer to location within convenience buffer */
/* */
/* Record auth data length & location of auth data, then advance past */
/* it. */
/* -------------------------------------------------------------------- */
static int
{
p = *buf;
/* Make sure we have enough space for expected fixed auth parms. */
return(-1);
p++; /* We don't care about auth_flavor. */
xdr = B(p++); /* Length of auth_data */
/* Test for absurdity / illegality of auth_data length. */
return(-1);
return(0);
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_insert */
/* Returns: int -- -1 == list insertion failed, */
/* 0 == item successfully added */
/* Parameters: rs(I) - pointer to RPCB session structure */
/* rx(I) - pointer to RPCB transaction structure */
/* -------------------------------------------------------------------- */
static int
{
return(0);
}
return(-1);
return(-1);
return(0);
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_xdrrpcb */
/* Returns: int -- -1 == failure to properly decode the request */
/* 0 == rpcb successfully decoded */
/* Parameters: rs(I) - pointer to RPCB session structure */
/* p(I) - pointer to location within session buffer */
/* rpcb(O) - pointer to rpcb (xdr type) structure */
/* */
/* Decode a XDR encoded rpcb structure and record its contents in rpcb */
/* -------------------------------------------------------------------- */
static int
u_32_t *p;
{
return(-1);
/* Bypass target program & version. */
p += 2;
/* Decode r_netid. Must be "tcp" or "udp". */
return(-1);
/* Decode r_maddr. */
return(-1);
/* Advance to r_owner and make sure it's empty. */
return(-1);
return(0);
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_getuaddr */
/* Returns: int -- -1 == illegal string, */
/* 0 == string parsed; contents recorded */
/* Parameters: rm(I) - pointer to RPC message structure */
/* xu(I) - pointer to universal address structure */
/* p(IO) - pointer to location within message buffer */
/* */
/* Decode the IP address / port at p and record them in xu. */
/* -------------------------------------------------------------------- */
static int
u_32_t **p;
{
char *c, *i, *b, *pp;
/* Test for string length. */
return(-1);
/* Length check */
return(-1);
/* Advance p */
*(char **)p += XDRALIGN(l);
/* Copy string to local buffer & terminate C style */
uastr[l] = '\0';
/*
* Expected format: a.b.c.d.e.f where [a-d] correspond to bytes of
* an IP address and [ef] are the bytes of a L4 port.
*/
return(-1);
b = uastr;
if (ISDIGIT(*c)) {
dd = 0;
continue;
}
if (*c == '.') {
if (dd != 0)
return(-1);
/* Check for ASCII byte. */
*c = '\0';
t = ippr_rpcb_atoi(b);
if (t > 255)
return(-1);
/* Aim b at beginning of the next byte. */
b = c + 1;
/* Switch off IP addr vs port parsing. */
if (d < 4)
i[d++] = t & 0xff;
else
dd = 1;
continue;
}
return(-1);
}
if (d != 5) /* String must contain exactly 5 periods. */
return(-1);
/* Handle the last byte (port low byte) */
t = ippr_rpcb_atoi(b);
if (t > 255)
return(-1);
return(0);
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_atoi (XXX should be generic for all proxies) */
/* Returns: int -- integer representation of supplied string */
/* Parameters: ptr(I) - input string */
/* */
/* Simple version of atoi(3) ripped from ip_rcmd_pxy.c. */
/* -------------------------------------------------------------------- */
static u_int
char *ptr;
{
register char *s = ptr, c;
register u_int i = 0;
while (((c = *s++) != '\0') && ISDIGIT(c)) {
i *= 10;
i += c - '0';
}
return i;
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_modreq */
/* Returns: int -- change in datagram length */
/* APR_ERR(2) - critical failure */
/* Parameters: fin(I) - pointer to packet information */
/* nat(I) - pointer to NAT session */
/* rm(I) - pointer to RPC message structure */
/* m(I) - pointer to mbuf chain */
/* off(I) - current offset within mbuf chain */
/* */
/* When external and internal addresses differ, we rewrite the former */
/* with the latter. (This is exclusive to protocol versions 3 & 4). */
/* -------------------------------------------------------------------- */
static int
mb_t *m;
{
char *i, *p;
int diff;
p = (char *)&nat->nat_inport;
/* Form new string. */
#else
#endif
"%u.%u.%u.%u.%u.%u", i[0] & 0xff, i[1] & 0xff,
i[2] & 0xff, i[3] & 0xff, p[0] & 0xff, p[1] & 0xff);
/* Determine mbuf offset to start writing to. */
/* Write new string length. */
off += 4;
/* Write new string. */
/* Write in zero r_owner. */
bogo = 0;
/* Determine difference in data lengths. */
/*
* If our new string has a different length, make necessary
* adjustments.
*/
if (diff != 0) {
/* XXX Storage lengths. */
}
return(diff);
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_decoderep */
/* Returns: int - -1 == bad request or critical failure, */
/* 0 == valid, negative reply */
/* 1 == vaddlid, positive reply; needs no changes */
/* Parameters: fin(I) - pointer to packet information */
/* nat(I) - pointer to NAT session structure */
/* rs(I) - pointer to RPCB session structure */
/* rm(I) - pointer to RPC message structure */
/* rxp(O) - pointer to RPCB transaction structure */
/* */
/* Take a presumed RPCB reply, extract the XID, search for the original */
/* request information, and determine whether the request was accepted */
/* or rejected. With a valid accepted reply, go ahead and create NAT */
/* and state entries, and finish up by rewriting the packet as */
/* required. */
/* */
/* WARNING: It's the responsibility of the caller to make sure there */
/* is enough room in rs_buf for the basic RPC message "preamble". */
/* -------------------------------------------------------------------- */
static int
rpcb_xact_t **rxp;
{
xdr = B(p++); /* Record this message's XID. */
/* Lookup XID */
return(-1);
}
/* Test call vs reply */
if (B(p++) != RPCB_REPLY)
return(-1);
/* Test reply_stat */
switch(B(p++))
{
case RPCB_MSG_DENIED:
return(0);
case RPCB_MSG_ACCEPTED:
break;
default:
return(-1);
}
/* Bypass RPC authentication stuff. */
return(-1);
/* Test accept status */
return(-1);
if (B(p++) != 0)
return(0);
/* Parse out the expected reply */
{
case RPCB_RES_PMAP:
/* There must be only one 4 byte argument. */
return(-1);
/* Reply w/ a 0 port indicates service isn't registered */
if (xdr == 0)
return(0);
/* Is the value sane? */
if (xdr > 65535)
return(-1);
/* Create NAT & state table entries. */
return(-1);
break;
case RPCB_RES_STRING:
/* Expecting a XDR string; need 4 bytes for length */
return(-1);
/* A null string indicates an unregistered service */
return(0);
/* Decode the target IP address / port. */
return(-1);
/* Validate the IP address and port contained. */
return(-1);
/* Create NAT & state table entries. */
return(-1);
break;
case RPCB_RES_LIST:
return(-1);
/* rpcb_entry_list_ptr */
switch(B(p))
{
case 0:
return(0);
case 1:
break;
default:
return(-1);
}
cnt = 0;
for(;;) {
return(-1);
return(-1);
/* re_semantics & re_pfamily length */
return(-1);
p++; /* Skipping re_semantics. */
xdr = B(p++);
return(-1);
p++;
return(-1);
return(-1);
return(-1);
++cnt;
break;
if (cnt > 2)
return(-1);
p++;
}
ifsrpcb);
if (rv != 0)
return(-1);
}
break;
default:
/*CONSTANTCONDITION*/
}
return(1);
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_lookup */
/* Returns: rpcb_xact_t * - NULL == no matching record, */
/* else pointer to relevant entry */
/* Parameters: rs(I) - pointer to RPCB session */
/* xid(I) - XID to look for */
/* -------------------------------------------------------------------- */
static rpcb_xact_t *
{
return(NULL);
break;
return(rx);
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_deref */
/* Returns: (void) */
/* Parameters: rs(I) - pointer to RPCB session */
/* rx(I) - pointer to RPC transaction struct to remove */
/* force(I) - indicates to delete entry regardless of */
/* reference count */
/* Locking: rs->rs_rxlock must be held write only */
/* */
/* Free the RPCB transaction record rx from the chain of entries. */
/* -------------------------------------------------------------------- */
static void
{
return;
return;
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_getproto */
/* Parameters: rm(I) - pointer to RPC message structure */
/* xp(I) - pointer to netid structure */
/* p(IO) - pointer to location within packet buffer */
/* */
/* -------------------------------------------------------------------- */
static int
u_32_t **p;
{
/* Must have 4 bytes for length & 4 bytes for "tcp" or "udp". */
return(-1);
/* Test the string length. */
if (len != 3)
return(-1);
/* Test the actual string & record the protocol accordingly. */
else {
return(-1);
}
/* Advance past the string. */
(*p)++;
return(0);
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_getnat */
/* Returns: int -- -1 == failed to create table entries, */
/* 0 == success */
/* Parameters: fin(I) - pointer to packet information */
/* nat(I) - pointer to NAT table entry */
/* proto(I) - transport protocol for new entries */
/* port(I) - new port to use w/ wildcard table entries */
/* */
/* Create state and NAT entries to handle an anticipated connection */
/* attempt between RPC client and server. */
/* -------------------------------------------------------------------- */
static int
{
int nflags;
/* Generate dummy fr_info */
if (proto == IPPROTO_TCP) {
} else {
}
/*
* Search for existing NAT & state entries. Pay close attention to
* mutexes / locks grabbed from lookup routines, as not doing so could
* lead to bad things.
*
* If successful, fr_stlookup returns with ipf_state locked. We have
* no use for this lock, so simply unlock it if necessary.
*/
return(0);
}
/* Slightly modify the following structures for actual use in creating
* flags that may be detrimental to the creation process or simply
* shouldn't be associated with a table entry.
*/
nflags &= ~NAT_SEARCH;
/* XXX Since we're just copying the original ipn contents
* back, would we be better off just sending a pointer to
* the 'temp' copy off to nat_new instead?
*/
}
/* Create NAT entry. return NULL if this fails. */
return(-1);
}
}
/* Create state entry. Return NULL if this fails. */
nflags &= NAT_TCPUDP;
/*
* XXX nat_delete is private to ip_nat.c. Should
* check w/ Darren about this one.
*
* nat_delete(natl, NL_EXPIRE, ifs);
*/
return(-1);
}
}
return(0);
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_modv3 */
/* Returns: int -- change in packet length */
/* Parameters: fin(I) - pointer to packet information */
/* nat(I) - pointer to NAT session */
/* rm(I) - pointer to RPC message structure */
/* m(I) - pointer to mbuf chain */
/* off(I) - offset within mbuf chain */
/* */
/* Write a new universal address string to this packet, adjusting */
/* lengths as necessary. */
/* -------------------------------------------------------------------- */
static int
mb_t *m;
{
char *i, *p;
int diff;
/* Form new string. */
#else
#endif
"%u.%u.%u.%u.%u.%u", i[0] & 0xff, i[1] & 0xff,
i[2] & 0xff, i[3] & 0xff, p[0] & 0xff, p[1] & 0xff);
/* Determine mbuf offset to write to. */
/* Write new string length. */
off += 4;
/* Write new string. */
/* Determine difference in data lengths. */
/*
* If our new string has a different length, make necessary
* adjustments.
*/
if (diff != 0)
return(diff);
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_modv4 */
/* Returns: int -- change in packet length */
/* Parameters: fin(I) - pointer to packet information */
/* nat(I) - pointer to NAT session */
/* rm(I) - pointer to RPC message structure */
/* m(I) - pointer to mbuf chain */
/* off(I) - offset within mbuf chain */
/* */
/* Write new rpcb_entry list, adjusting lengths as necessary. */
/* -------------------------------------------------------------------- */
static int
mb_t *m;
{
char *i, *p;
diff = 0;
/* Determine mbuf offset to write to. */
/* Form new string. */
padding. */
#else
#endif
"%u.%u.%u.%u.%u.%u", i[0] & 0xff,
i[1] & 0xff, i[2] & 0xff, i[3] & 0xff,
p[0] & 0xff, p[1] & 0xff);
/* Write new string length. */
off += 4;
/* Write new string. */
/* Record any change in length. */
/* If the length changed, copy back the rest of this entry. */
if (diff != 0) {
}
}
/*
* If our new string has a different length, make necessary
* adjustments.
*/
if (diff != 0)
return(diff);
}
/* -------------------------------------------------------------------- */
/* Function: ippr_rpcb_fixlen */
/* Returns: (void) */
/* Parameters: fin(I) - pointer to packet information */
/* len(I) - change in packet length */
/* */
/* Adjust various packet related lengths held in structure and packet */
/* header fields. */
/* -------------------------------------------------------------------- */
static void
int len;
{
}
#undef B