2 * Copyright (C) 2002-2003 by Ryan Beasley <ryanb@goddamnbastard.org>
4 * See the IPFILTER.LICENCE file for details on licencing.
8 * This is an in-kernel application proxy for Sun's RPCBIND (nee portmap)
9 * protocol as defined in RFC1833. It is far from complete, mostly
10 * lacking in less-likely corner cases, but it's definitely functional.
13 * rdr <int> <e_ip>/32 port <e_p> -> <i_ip> port <i_p> udp proxy rpcbu
15 * If the host running IP Filter is the same as the RPC server, it's
16 * perfectly legal for both the internal and external addresses and ports
19 * When triggered by appropriate IP NAT rules, this proxy works by
20 * examining data contained in received packets. Requests and replies are
21 * modified, NAT and state table entries created, etc., as necessary.
26 * o Must implement locking to protect proxy session data.
27 * o Fragmentation isn't supported.
28 * o Only supports UDP.
29 * o Doesn't support multiple RPC records in a single request.
30 * o Errors should be more fine-grained. (e.g., malloc failure vs.
31 * illegal RPCB request / reply)
32 * o Even with the limit on the total amount of recorded transactions,
33 * should there be a timeout on transaction removal?
34 * o There is a potential collision between cloning, wildcard NAT and
35 * state entries. There should be an appr_getport routine for
37 * o The enclosed hack of STREAMS support is pretty sick and most likely
40 * $Id: ip_rpcb_pxy.c,v 2.25.2.7 2007/06/04 09:16:31 darrenr Exp $
43 #define IPF_RPCB_PROXY
48 int ippr_rpcb_init __P((void));
49 void ippr_rpcb_fini __P((void));
50 int ippr_rpcb_new __P((fr_info_t *, ap_session_t *, nat_t *));
51 void ippr_rpcb_del __P((ap_session_t *));
52 int ippr_rpcb_in __P((fr_info_t *, ap_session_t *, nat_t *));
53 int ippr_rpcb_out __P((fr_info_t *, ap_session_t *, nat_t *));
55 static void ippr_rpcb_flush __P((rpcb_session_t *));
56 static int ippr_rpcb_decodereq __P((fr_info_t *, nat_t *,
57 rpcb_session_t *, rpc_msg_t *));
58 static int ippr_rpcb_skipauth __P((rpc_msg_t *, xdr_auth_t *, u_32_t **));
59 static int ippr_rpcb_insert __P((rpcb_session_t *, rpcb_xact_t *));
60 static int ippr_rpcb_xdrrpcb __P((rpc_msg_t *, u_32_t *, rpcb_args_t *));
61 static int ippr_rpcb_getuaddr __P((rpc_msg_t *, xdr_uaddr_t *,
63 static u_int ippr_rpcb_atoi __P((char *));
64 static int ippr_rpcb_modreq __P((fr_info_t *, nat_t *, rpc_msg_t *,
66 static int ippr_rpcb_decoderep __P((fr_info_t *, nat_t *,
67 rpcb_session_t *, rpc_msg_t *, rpcb_xact_t **));
68 static rpcb_xact_t * ippr_rpcb_lookup __P((rpcb_session_t *, u_32_t));
69 static void ippr_rpcb_deref __P((rpcb_session_t *, rpcb_xact_t *));
70 static int ippr_rpcb_getproto __P((rpc_msg_t *, xdr_proto_t *,
72 static int ippr_rpcb_getnat __P((fr_info_t *, nat_t *, u_int, u_int));
73 static int ippr_rpcb_modv3 __P((fr_info_t *, nat_t *, rpc_msg_t *,
75 static int ippr_rpcb_modv4 __P((fr_info_t *, nat_t *, rpc_msg_t *,
77 static void ippr_rpcb_fixlen __P((fr_info_t *, int));
82 static frentry_t rpcbfr; /* Skeleton rule for reference by entities
83 this proxy creates. */
84 static int rpcbcnt; /* Upper bound of allocated RPCB sessions. */
85 /* XXX rpcbcnt still requires locking. */
87 int rpcb_proxy_init = 0;
91 * Since rpc_msg contains only pointers, one should use this macro as a
92 * handy way to get to the goods. (In case you're wondering about the name,
93 * this started as BYTEREF -> BREF -> B.)
95 #define B(r) (u_32_t)ntohl(*(r))
101 /* -------------------------------------------------------------------- */
102 /* Function: ippr_rpcb_init */
103 /* Returns: int - 0 == success */
104 /* Parameters: (void) */
106 /* Initialize the filter rule entry and session limiter. */
107 /* -------------------------------------------------------------------- */
113 bzero((char *)&rpcbfr, sizeof(rpcbfr));
115 rpcbfr.fr_flags = FR_PASS|FR_QUICK|FR_KEEPSTATE;
116 MUTEX_INIT(&rpcbfr.fr_lock, "ipf Sun RPCB proxy rule lock");
122 /* -------------------------------------------------------------------- */
123 /* Function: ippr_rpcb_fini */
125 /* Parameters: (void) */
127 /* Destroy rpcbfr's mutex to avoid a lock leak. */
128 /* -------------------------------------------------------------------- */
132 if (rpcb_proxy_init == 1) {
133 MUTEX_DESTROY(&rpcbfr.fr_lock);
138 /* -------------------------------------------------------------------- */
139 /* Function: ippr_rpcb_new */
140 /* Returns: int - -1 == failure, 0 == success */
141 /* Parameters: fin(I) - pointer to packet information */
142 /* aps(I) - pointer to proxy session structure */
143 /* nat(I) - pointer to NAT session structure */
145 /* Allocate resources for per-session proxy structures. */
146 /* -------------------------------------------------------------------- */
148 ippr_rpcb_new(fin, aps, nat)
155 fin = fin; /* LINT */
156 nat = nat; /* LINT */
158 KMALLOC(rs, rpcb_session_t *);
162 bzero((char *)rs, sizeof(*rs));
163 MUTEX_INIT(&rs->rs_rxlock, "ipf Sun RPCB proxy session lock");
170 /* -------------------------------------------------------------------- */
171 /* Function: ippr_rpcb_del */
173 /* Parameters: aps(I) - pointer to proxy session structure */
175 /* Free up a session's list of RPCB requests. */
176 /* -------------------------------------------------------------------- */
182 rs = (rpcb_session_t *)aps->aps_data;
184 MUTEX_ENTER(&rs->rs_rxlock);
186 MUTEX_EXIT(&rs->rs_rxlock);
187 MUTEX_DESTROY(&rs->rs_rxlock);
190 /* -------------------------------------------------------------------- */
191 /* Function: ippr_rpcb_in */
192 /* Returns: int - APR_ERR(1) == drop the packet, */
193 /* APR_ERR(2) == kill the proxy session, */
194 /* else change in packet length (in bytes) */
195 /* Parameters: fin(I) - pointer to packet information */
196 /* ip(I) - pointer to packet header */
197 /* aps(I) - pointer to proxy session structure */
198 /* nat(I) - pointer to NAT session structure */
200 /* Given a presumed RPCB request, perform some minor tests and pass off */
201 /* for decoding. Also pass packet off for a rewrite if necessary. */
202 /* -------------------------------------------------------------------- */
204 ippr_rpcb_in(fin, aps, nat)
209 rpc_msg_t rpcmsg, *rm;
215 /* Disallow fragmented or illegally short packets. */
216 if ((fin->fin_flx & (FI_FRAG|FI_SHORT)) != 0)
219 /* Perform basic variable initialization. */
220 rs = (rpcb_session_t *)aps->aps_data;
223 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
224 off += sizeof(udphdr_t) + fin->fin_ipoff;
225 dlen = fin->fin_dlen - sizeof(udphdr_t);
227 /* Disallow packets outside legal range for supported requests. */
228 if ((dlen < RPCB_REQMIN) || (dlen > RPCB_REQMAX))
231 /* Copy packet over to convenience buffer. */
233 bzero((char *)rm, sizeof(*rm));
234 COPYDATA(m, off, dlen, (caddr_t)&rm->rm_msgbuf);
235 rm->rm_buflen = dlen;
237 /* Send off to decode request. */
238 rv = ippr_rpcb_decodereq(fin, nat, rs, rm);
249 rv = ippr_rpcb_modreq(fin, nat, rm, m, off);
252 /*CONSTANTCONDITION*/
253 IPF_PANIC(1, ("illegal rv %d (ippr_rpcb_req)", rv));
259 /* -------------------------------------------------------------------- */
260 /* Function: ippr_rpcb_out */
261 /* Returns: int - APR_ERR(1) == drop the packet, */
262 /* APR_ERR(2) == kill the proxy session, */
263 /* else change in packet length (in bytes) */
264 /* Parameters: fin(I) - pointer to packet information */
265 /* ip(I) - pointer to packet header */
266 /* aps(I) - pointer to proxy session structure */
267 /* nat(I) - pointer to NAT session structure */
269 /* Given a presumed RPCB reply, perform some minor tests and pass off */
270 /* for decoding. If the message indicates a successful request with */
271 /* valid addressing information, create NAT and state structures to */
272 /* allow direct communication between RPC client and server. */
273 /* -------------------------------------------------------------------- */
275 ippr_rpcb_out(fin, aps, nat)
280 rpc_msg_t rpcmsg, *rm;
287 /* Disallow fragmented or illegally short packets. */
288 if ((fin->fin_flx & (FI_FRAG|FI_SHORT)) != 0)
291 /* Perform basic variable initialization. */
292 rs = (rpcb_session_t *)aps->aps_data;
296 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
297 off += sizeof(udphdr_t) + fin->fin_ipoff;
298 dlen = fin->fin_dlen - sizeof(udphdr_t);
301 /* Disallow packets outside legal range for supported requests. */
302 if ((dlen < RPCB_REPMIN) || (dlen > RPCB_REPMAX))
305 /* Copy packet over to convenience buffer. */
307 bzero((char *)rm, sizeof(*rm));
308 COPYDATA(m, off, dlen, (caddr_t)&rm->rm_msgbuf);
309 rm->rm_buflen = dlen;
311 rx = NULL; /* XXX gcc */
313 /* Send off to decode reply. */
314 rv = ippr_rpcb_decoderep(fin, nat, rs, rm, &rx);
318 case -1: /* Bad packet */
320 MUTEX_ENTER(&rs->rs_rxlock);
321 ippr_rpcb_deref(rs, rx);
322 MUTEX_EXIT(&rs->rs_rxlock);
327 case 0: /* Negative reply / request rejected */
329 case 1: /* Positive reply */
331 * With the IP address embedded in a GETADDR(LIST) reply,
332 * we'll need to rewrite the packet in the very possible
333 * event that the internal & external addresses aren't the
334 * same. (i.e., this box is either a router or rpcbind
335 * only listens on loopback.)
337 if (nat->nat_inip.s_addr != nat->nat_outip.s_addr) {
338 if (rx->rx_type == RPCB_RES_STRING)
339 diff = ippr_rpcb_modv3(fin, nat, rm, m, off);
340 else if (rx->rx_type == RPCB_RES_LIST)
341 diff = ippr_rpcb_modv4(fin, nat, rm, m, off);
345 /*CONSTANTCONDITION*/
346 IPF_PANIC(1, ("illegal rv %d (ippr_rpcb_decoderep)", rv));
350 MUTEX_ENTER(&rs->rs_rxlock);
351 /* XXX Gross hack - I'm overloading the reference
352 * counter to deal with both threads and retransmitted
353 * requests. One deref signals that this thread is
354 * finished with rx, and the other signals that we've
355 * processed its reply.
357 ippr_rpcb_deref(rs, rx);
358 ippr_rpcb_deref(rs, rx);
359 MUTEX_EXIT(&rs->rs_rxlock);
366 * Private support subroutines
369 /* -------------------------------------------------------------------- */
370 /* Function: ippr_rpcb_flush */
372 /* Parameters: rs(I) - pointer to RPCB session structure */
374 /* Simply flushes the list of outstanding transactions, if any. */
375 /* -------------------------------------------------------------------- */
380 rpcb_xact_t *r1, *r2;
393 /* -------------------------------------------------------------------- */
394 /* Function: ippr_rpcb_decodereq */
395 /* Returns: int - -1 == bad request or critical failure, */
396 /* 0 == request successfully decoded, */
397 /* 1 == request successfully decoded; requires */
398 /* address rewrite/modification */
399 /* Parameters: fin(I) - pointer to packet information */
400 /* nat(I) - pointer to NAT session structure */
401 /* rs(I) - pointer to RPCB session structure */
402 /* rm(I) - pointer to RPC message structure */
404 /* Take a presumed RPCB request, decode it, and store the results in */
405 /* the transaction list. If the internal target address needs to be */
406 /* modified, store its location in ptr. */
407 /* WARNING: It's the responsibility of the caller to make sure there */
408 /* is enough room in rs_buf for the basic RPC message "preamble". */
409 /* -------------------------------------------------------------------- */
411 ippr_rpcb_decodereq(fin, nat, rs, rm)
423 p = (u_32_t *)rm->rm_msgbuf;
426 bzero((char *)&rx, sizeof(rx));
430 rx.rx_xid = B(p++); /* Record this message's XID. */
432 /* Parse out and test the RPC header. */
433 if ((B(p++) != RPCB_CALL) ||
434 (B(p++) != RPCB_MSG_VERSION) ||
435 (B(p++) != RPCB_PROG))
438 /* Record the RPCB version and procedure. */
442 /* Bypass RPC authentication stuff. */
443 if (ippr_rpcb_skipauth(rm, &rc->rc_authcred, &p) != 0)
445 if (ippr_rpcb_skipauth(rm, &rc->rc_authverf, &p) != 0)
448 /* Compare RPCB version and procedure numbers. */
449 switch(B(rc->rc_vers))
452 /* This proxy only supports PMAP_GETPORT. */
453 if (B(rc->rc_proc) != RPCB_GETPORT)
456 /* Portmap requests contain four 4 byte parameters. */
457 if (RPCB_BUF_EQ(rm, p, 16) == 0)
460 p += 2; /* Skip requested program and version numbers. */
462 /* Sanity check the requested protocol. */
464 if (!(xdr == IPPROTO_UDP || xdr == IPPROTO_TCP))
467 rx.rx_type = RPCB_RES_PMAP;
472 /* GETADDRLIST is exclusive to v4; GETADDR for v3 & v4 */
473 switch(B(rc->rc_proc))
476 rx.rx_type = RPCB_RES_STRING;
477 rx.rx_proto = (u_int)fin->fin_p;
479 case RPCB_GETADDRLIST:
480 if (B(rc->rc_vers) != 4)
482 rx.rx_type = RPCB_RES_LIST;
488 ra = &rc->rc_rpcbargs;
490 /* Decode the 'struct rpcb' request. */
491 if (ippr_rpcb_xdrrpcb(rm, p, ra) != 0)
494 /* Are the target address & port valid? */
495 if ((ra->ra_maddr.xu_ip != nat->nat_outip.s_addr) ||
496 (ra->ra_maddr.xu_port != nat->nat_outport))
499 /* Do we need to rewrite this packet? */
500 if ((nat->nat_outip.s_addr != nat->nat_inip.s_addr) ||
501 (nat->nat_outport != nat->nat_inport))
508 MUTEX_ENTER(&rs->rs_rxlock);
509 if (ippr_rpcb_insert(rs, &rx) != 0) {
510 MUTEX_EXIT(&rs->rs_rxlock);
513 MUTEX_EXIT(&rs->rs_rxlock);
518 /* -------------------------------------------------------------------- */
519 /* Function: ippr_rpcb_skipauth */
520 /* Returns: int -- -1 == illegal auth parameters (lengths) */
521 /* 0 == valid parameters, pointer advanced */
522 /* Parameters: rm(I) - pointer to RPC message structure */
523 /* auth(I) - pointer to RPC auth structure */
524 /* buf(IO) - pointer to location within convenience buffer */
526 /* Record auth data length & location of auth data, then advance past */
528 /* -------------------------------------------------------------------- */
530 ippr_rpcb_skipauth(rm, auth, buf)
539 /* Make sure we have enough space for expected fixed auth parms. */
540 if (RPCB_BUF_GEQ(rm, p, 8) == 0)
543 p++; /* We don't care about auth_flavor. */
545 auth->xa_string.xs_len = p;
546 xdr = B(p++); /* Length of auth_data */
548 /* Test for absurdity / illegality of auth_data length. */
549 if ((XDRALIGN(xdr) < xdr) || (RPCB_BUF_GEQ(rm, p, XDRALIGN(xdr)) == 0))
552 auth->xa_string.xs_str = (char *)p;
554 p += XDRALIGN(xdr); /* Advance our location. */
561 /* -------------------------------------------------------------------- */
562 /* Function: ippr_rpcb_insert */
563 /* Returns: int -- -1 == list insertion failed, */
564 /* 0 == item successfully added */
565 /* Parameters: rs(I) - pointer to RPCB session structure */
566 /* rx(I) - pointer to RPCB transaction structure */
567 /* -------------------------------------------------------------------- */
569 ippr_rpcb_insert(rs, rx)
575 rxp = ippr_rpcb_lookup(rs, rx->rx_xid);
581 if (rpcbcnt == RPCB_MAXREQS)
584 KMALLOC(rxp, rpcb_xact_t *);
588 bcopy((char *)rx, (char *)rxp, sizeof(*rx));
590 if (rs->rs_rxlist != NULL)
591 rs->rs_rxlist->rx_pnext = &rxp->rx_next;
593 rxp->rx_pnext = &rs->rs_rxlist;
594 rxp->rx_next = rs->rs_rxlist;
604 /* -------------------------------------------------------------------- */
605 /* Function: ippr_rpcb_xdrrpcb */
606 /* Returns: int -- -1 == failure to properly decode the request */
607 /* 0 == rpcb successfully decoded */
608 /* Parameters: rs(I) - pointer to RPCB session structure */
609 /* p(I) - pointer to location within session buffer */
610 /* rpcb(O) - pointer to rpcb (xdr type) structure */
612 /* Decode a XDR encoded rpcb structure and record its contents in rpcb */
613 /* within only the context of TCP/UDP over IP networks. */
614 /* -------------------------------------------------------------------- */
616 ippr_rpcb_xdrrpcb(rm, p, ra)
621 if (!RPCB_BUF_GEQ(rm, p, 20))
624 /* Bypass target program & version. */
627 /* Decode r_netid. Must be "tcp" or "udp". */
628 if (ippr_rpcb_getproto(rm, &ra->ra_netid, &p) != 0)
631 /* Decode r_maddr. */
632 if (ippr_rpcb_getuaddr(rm, &ra->ra_maddr, &p) != 0)
635 /* Advance to r_owner and make sure it's empty. */
636 if (!RPCB_BUF_EQ(rm, p, 4) || (B(p) != 0))
642 /* -------------------------------------------------------------------- */
643 /* Function: ippr_rpcb_getuaddr */
644 /* Returns: int -- -1 == illegal string, */
645 /* 0 == string parsed; contents recorded */
646 /* Parameters: rm(I) - pointer to RPC message structure */
647 /* xu(I) - pointer to universal address structure */
648 /* p(IO) - pointer to location within message buffer */
650 /* Decode the IP address / port at p and record them in xu. */
651 /* -------------------------------------------------------------------- */
653 ippr_rpcb_getuaddr(rm, xu, p)
658 char *c, *i, *b, *pp;
662 /* Test for string length. */
663 if (!RPCB_BUF_GEQ(rm, *p, 4))
666 xu->xu_xslen = (*p)++;
667 xu->xu_xsstr = (char *)*p;
671 if (l < 11 || l > 23 || !RPCB_BUF_GEQ(rm, *p, XDRALIGN(l)))
675 *(char **)p += XDRALIGN(l);
677 /* Copy string to local buffer & terminate C style */
678 bcopy(xu->xu_xsstr, uastr, l);
681 i = (char *)&xu->xu_ip;
682 pp = (char *)&xu->xu_port;
685 * Expected format: a.b.c.d.e.f where [a-d] correspond to bytes of
686 * an IP address and [ef] are the bytes of a L4 port.
688 if (!(ISDIGIT(uastr[0]) && ISDIGIT(uastr[l-1])))
691 for (c = &uastr[1], d = 0, dd = 0; c < &uastr[l-1]; c++) {
700 /* Check for ASCII byte. */
702 t = ippr_rpcb_atoi(b);
706 /* Aim b at beginning of the next byte. */
709 /* Switch off IP addr vs port parsing. */
713 pp[d++ - 4] = t & 0xff;
720 if (d != 5) /* String must contain exactly 5 periods. */
723 /* Handle the last byte (port low byte) */
724 t = ippr_rpcb_atoi(b);
727 pp[d - 4] = t & 0xff;
732 /* -------------------------------------------------------------------- */
733 /* Function: ippr_rpcb_atoi (XXX should be generic for all proxies) */
734 /* Returns: int -- integer representation of supplied string */
735 /* Parameters: ptr(I) - input string */
737 /* Simple version of atoi(3) ripped from ip_rcmd_pxy.c. */
738 /* -------------------------------------------------------------------- */
743 register char *s = ptr, c;
744 register u_int i = 0;
746 while (((c = *s++) != '\0') && ISDIGIT(c)) {
753 /* -------------------------------------------------------------------- */
754 /* Function: ippr_rpcb_modreq */
755 /* Returns: int -- change in datagram length */
756 /* APR_ERR(2) - critical failure */
757 /* Parameters: fin(I) - pointer to packet information */
758 /* nat(I) - pointer to NAT session */
759 /* rm(I) - pointer to RPC message structure */
760 /* m(I) - pointer to mbuf chain */
761 /* off(I) - current offset within mbuf chain */
763 /* When external and internal addresses differ, we rewrite the former */
764 /* with the latter. (This is exclusive to protocol versions 3 & 4). */
765 /* -------------------------------------------------------------------- */
767 ippr_rpcb_modreq(fin, nat, rm, m, off)
774 u_int len, xlen, pos, bogo;
781 ra = &rm->rm_call.rc_rpcbargs;
782 i = (char *)&nat->nat_inip.s_addr;
783 p = (char *)&nat->nat_inport;
785 /* Form new string. */
786 bzero(uaddr, sizeof(uaddr)); /* Just in case we need padding. */
787 #if defined(SNPRINTF) && defined(_KERNEL)
788 SNPRINTF(uaddr, sizeof(uaddr),
790 (void) sprintf(uaddr,
792 "%u.%u.%u.%u.%u.%u", i[0] & 0xff, i[1] & 0xff,
793 i[2] & 0xff, i[3] & 0xff, p[0] & 0xff, p[1] & 0xff);
795 xlen = XDRALIGN(len);
797 /* Determine mbuf offset to start writing to. */
798 pos = (char *)ra->ra_maddr.xu_xslen - rm->rm_msgbuf;
801 /* Write new string length. */
803 COPYBACK(m, off, 4, (caddr_t)&bogo);
806 /* Write new string. */
807 COPYBACK(m, off, xlen, uaddr);
810 /* Write in zero r_owner. */
812 COPYBACK(m, off, 4, (caddr_t)&bogo);
814 /* Determine difference in data lengths. */
815 diff = xlen - XDRALIGN(B(ra->ra_maddr.xu_xslen));
818 * If our new string has a different length, make necessary
823 udp->uh_ulen = htons(ntohs(udp->uh_ulen) + diff);
824 fin->fin_ip->ip_len += diff;
825 fin->fin_dlen += diff;
826 fin->fin_plen += diff;
827 /* XXX Storage lengths. */
833 /* -------------------------------------------------------------------- */
834 /* Function: ippr_rpcb_decoderep */
835 /* Returns: int - -1 == bad request or critical failure, */
836 /* 0 == valid, negative reply */
837 /* 1 == vaddlid, positive reply; needs no changes */
838 /* Parameters: fin(I) - pointer to packet information */
839 /* nat(I) - pointer to NAT session structure */
840 /* rs(I) - pointer to RPCB session structure */
841 /* rm(I) - pointer to RPC message structure */
842 /* rxp(O) - pointer to RPCB transaction structure */
844 /* Take a presumed RPCB reply, extract the XID, search for the original */
845 /* request information, and determine whether the request was accepted */
846 /* or rejected. With a valid accepted reply, go ahead and create NAT */
847 /* and state entries, and finish up by rewriting the packet as */
850 /* WARNING: It's the responsibility of the caller to make sure there */
851 /* is enough room in rs_buf for the basic RPC message "preamble". */
852 /* -------------------------------------------------------------------- */
854 ippr_rpcb_decoderep(fin, nat, rs, rm, rxp)
868 p = (u_32_t *)rm->rm_msgbuf;
870 bzero((char *)&rx, sizeof(rx));
874 xdr = B(p++); /* Record this message's XID. */
877 MUTEX_ENTER(&rs->rs_rxlock);
878 if ((rx = ippr_rpcb_lookup(rs, xdr)) == NULL) {
879 MUTEX_EXIT(&rs->rs_rxlock);
882 ++rx->rx_ref; /* per thread reference */
883 MUTEX_EXIT(&rs->rs_rxlock);
887 /* Test call vs reply */
888 if (B(p++) != RPCB_REPLY)
891 /* Test reply_stat */
894 case RPCB_MSG_DENIED:
896 case RPCB_MSG_ACCEPTED:
902 /* Bypass RPC authentication stuff. */
903 if (ippr_rpcb_skipauth(rm, &rr->rr_authverf, &p) != 0)
906 /* Test accept status */
907 if (!RPCB_BUF_GEQ(rm, p, 4))
912 /* Parse out the expected reply */
916 /* There must be only one 4 byte argument. */
917 if (!RPCB_BUF_EQ(rm, p, 4))
923 /* Reply w/ a 0 port indicates service isn't registered */
927 /* Is the value sane? */
931 /* Create NAT & state table entries. */
932 if (ippr_rpcb_getnat(fin, nat, rx->rx_proto, (u_int)xdr) != 0)
935 case RPCB_RES_STRING:
936 /* Expecting a XDR string; need 4 bytes for length */
937 if (!RPCB_BUF_GEQ(rm, p, 4))
940 rr->rr_v3.xu_str.xs_len = p++;
941 rr->rr_v3.xu_str.xs_str = (char *)p;
943 xdr = B(rr->rr_v3.xu_xslen);
945 /* A null string indicates an unregistered service */
946 if ((xdr == 0) && RPCB_BUF_EQ(rm, p, 0))
949 /* Decode the target IP address / port. */
950 if (ippr_rpcb_getuaddr(rm, &rr->rr_v3, &p) != 0)
953 /* Validate the IP address and port contained. */
954 if (nat->nat_inip.s_addr != rr->rr_v3.xu_ip)
957 /* Create NAT & state table entries. */
958 if (ippr_rpcb_getnat(fin, nat, rx->rx_proto,
959 (u_int)rr->rr_v3.xu_port) != 0)
963 if (!RPCB_BUF_GEQ(rm, p, 4))
965 /* rpcb_entry_list_ptr */
982 re = &rl->rl_entries[rl->rl_cnt];
983 if (ippr_rpcb_getuaddr(rm, &re->re_maddr, &p) != 0)
985 if (ippr_rpcb_getproto(rm, &re->re_netid, &p) != 0)
987 /* re_semantics & re_pfamily length */
988 if (!RPCB_BUF_GEQ(rm, p, 12))
990 p++; /* Skipping re_semantics. */
992 if ((xdr != 4) || strncmp((char *)p, "inet", 4))
995 if (ippr_rpcb_getproto(rm, &re->re_proto, &p) != 0)
997 if (!RPCB_BUF_GEQ(rm, p, 4))
1000 if (B(re->re_more) > 1) /* 0,1 only legal values */
1004 if (B(re->re_more) == 0)
1006 /* Replies in max out at 2; TCP and/or UDP */
1012 for(rl->rl_cnt = 0; rl->rl_cnt < cnt; rl->rl_cnt++) {
1013 re = &rl->rl_entries[rl->rl_cnt];
1014 rv = ippr_rpcb_getnat(fin, nat,
1015 re->re_proto.xp_proto,
1016 (u_int)re->re_maddr.xu_port);
1022 /*CONSTANTCONDITION*/
1023 IPF_PANIC(1, ("illegal rx_type %d", rx->rx_type));
1029 /* -------------------------------------------------------------------- */
1030 /* Function: ippr_rpcb_lookup */
1031 /* Returns: rpcb_xact_t * - NULL == no matching record, */
1032 /* else pointer to relevant entry */
1033 /* Parameters: rs(I) - pointer to RPCB session */
1034 /* xid(I) - XID to look for */
1035 /* -------------------------------------------------------------------- */
1036 static rpcb_xact_t *
1037 ippr_rpcb_lookup(rs, xid)
1043 if (rs->rs_rxlist == NULL)
1046 for (rx = rs->rs_rxlist; rx != NULL; rx = rx->rx_next)
1047 if (rx->rx_xid == xid)
1053 /* -------------------------------------------------------------------- */
1054 /* Function: ippr_rpcb_deref */
1055 /* Returns: (void) */
1056 /* Parameters: rs(I) - pointer to RPCB session */
1057 /* rx(I) - pointer to RPC transaction struct to remove */
1058 /* force(I) - indicates to delete entry regardless of */
1059 /* reference count */
1060 /* Locking: rs->rs_rxlock must be held write only */
1062 /* Free the RPCB transaction record rx from the chain of entries. */
1063 /* -------------------------------------------------------------------- */
1065 ippr_rpcb_deref(rs, rx)
1074 if (--rx->rx_ref != 0)
1077 if (rx->rx_next != NULL)
1078 rx->rx_next->rx_pnext = rx->rx_pnext;
1080 *rx->rx_pnext = rx->rx_next;
1087 /* -------------------------------------------------------------------- */
1088 /* Function: ippr_rpcb_getproto */
1089 /* Returns: int - -1 == illegal protocol/netid, */
1090 /* 0 == legal protocol/netid */
1091 /* Parameters: rm(I) - pointer to RPC message structure */
1092 /* xp(I) - pointer to netid structure */
1093 /* p(IO) - pointer to location within packet buffer */
1095 /* Decode netid/proto stored at p and record its numeric value. */
1096 /* -------------------------------------------------------------------- */
1098 ippr_rpcb_getproto(rm, xp, p)
1105 /* Must have 4 bytes for length & 4 bytes for "tcp" or "udp". */
1106 if (!RPCB_BUF_GEQ(rm, p, 8))
1109 xp->xp_xslen = (*p)++;
1110 xp->xp_xsstr = (char *)*p;
1112 /* Test the string length. */
1113 len = B(xp->xp_xslen);
1117 /* Test the actual string & record the protocol accordingly. */
1118 if (!strncmp((char *)xp->xp_xsstr, "tcp\0", 4))
1119 xp->xp_proto = IPPROTO_TCP;
1120 else if (!strncmp((char *)xp->xp_xsstr, "udp\0", 4))
1121 xp->xp_proto = IPPROTO_UDP;
1126 /* Advance past the string. */
1132 /* -------------------------------------------------------------------- */
1133 /* Function: ippr_rpcb_getnat */
1134 /* Returns: int -- -1 == failed to create table entries, */
1136 /* Parameters: fin(I) - pointer to packet information */
1137 /* nat(I) - pointer to NAT table entry */
1138 /* proto(I) - transport protocol for new entries */
1139 /* port(I) - new port to use w/ wildcard table entries */
1141 /* Create state and NAT entries to handle an anticipated connection */
1142 /* attempt between RPC client and server. */
1143 /* -------------------------------------------------------------------- */
1145 ippr_rpcb_getnat(fin, nat, proto, port)
1151 ipnat_t *ipn, ipnat;
1160 /* Generate dummy fr_info */
1161 bcopy((char *)fin, (char *)&fi, sizeof(fi));
1162 fi.fin_state = NULL;
1165 fi.fin_src = fin->fin_dst;
1166 fi.fin_dst = nat->nat_outip;
1169 fi.fin_dport = port & 0xffff;
1170 fi.fin_flx |= FI_IGNORE;
1172 bzero((char *)&tcp, sizeof(tcp));
1173 tcp.th_dport = htons(port);
1175 if (proto == IPPROTO_TCP) {
1176 tcp.th_win = htons(8192);
1177 TCP_OFF_A(&tcp, sizeof(tcphdr_t) >> 2);
1178 fi.fin_dlen = sizeof(tcphdr_t);
1179 tcp.th_flags = TH_SYN;
1182 fi.fin_dlen = sizeof(udphdr_t);
1186 nflags |= SI_W_SPORT|NAT_SEARCH;
1188 fi.fin_plen = fi.fin_hlen + fi.fin_dlen;
1191 * Search for existing NAT & state entries. Pay close attention to
1192 * mutexes / locks grabbed from lookup routines, as not doing so could
1193 * lead to bad things.
1195 * If successful, fr_stlookup returns with ipf_state locked. We have
1196 * no use for this lock, so simply unlock it if necessary.
1198 is = fr_stlookup(&fi, &tcp, NULL);
1200 RWLOCK_EXIT(&ipf_state);
1203 RWLOCK_EXIT(&ipf_nat);
1205 WRITE_ENTER(&ipf_nat);
1206 natl = nat_inlookup(&fi, nflags, proto, fi.fin_src, fi.fin_dst);
1208 if ((natl != NULL) && (is != NULL)) {
1209 MUTEX_DOWNGRADE(&ipf_nat);
1213 /* Slightly modify the following structures for actual use in creating
1214 * NAT and/or state entries. We're primarily concerned with stripping
1215 * flags that may be detrimental to the creation process or simply
1216 * shouldn't be associated with a table entry.
1218 fi.fin_fr = &rpcbfr;
1219 fi.fin_flx &= ~FI_IGNORE;
1220 nflags &= ~NAT_SEARCH;
1223 /* XXX Since we're just copying the original ipn contents
1224 * back, would we be better off just sending a pointer to
1225 * the 'temp' copy off to nat_new instead?
1227 /* Generate template/bogus NAT rule. */
1228 bcopy((char *)ipn, (char *)&ipnat, sizeof(ipnat));
1229 ipn->in_flags = nflags & IPN_TCPUDP;
1232 ipn->in_pmin = htons(fi.fin_dport);
1233 ipn->in_pmax = htons(fi.fin_dport);
1234 ipn->in_pnext = htons(fi.fin_dport);
1237 if (ipn->in_flags & IPN_FILTER) {
1241 *ipn->in_plabel = '\0';
1243 /* Create NAT entry. return NULL if this fails. */
1244 natl = nat_new(&fi, ipn, NULL, nflags|SI_CLONE|NAT_SLAVE,
1247 bcopy((char *)&ipnat, (char *)ipn, sizeof(ipnat));
1250 MUTEX_DOWNGRADE(&ipf_nat);
1255 (void) nat_proto(&fi, natl, nflags);
1256 nat_update(&fi, natl, natl->nat_ptr);
1258 MUTEX_DOWNGRADE(&ipf_nat);
1261 /* Create state entry. Return NULL if this fails. */
1262 fi.fin_dst = nat->nat_inip;
1263 fi.fin_nat = (void *)natl;
1264 fi.fin_flx |= FI_NATED;
1265 fi.fin_flx &= ~FI_STATE;
1266 nflags &= NAT_TCPUDP;
1267 nflags |= SI_W_SPORT|SI_CLONE;
1269 is = fr_addstate(&fi, NULL, nflags);
1272 * XXX nat_delete is private to ip_nat.c. Should
1273 * check w/ Darren about this one.
1275 * nat_delete(natl, NL_EXPIRE);
1279 if (fi.fin_state != NULL)
1280 fr_statederef((ipstate_t **)&fi.fin_state);
1286 /* -------------------------------------------------------------------- */
1287 /* Function: ippr_rpcb_modv3 */
1288 /* Returns: int -- change in packet length */
1289 /* Parameters: fin(I) - pointer to packet information */
1290 /* nat(I) - pointer to NAT session */
1291 /* rm(I) - pointer to RPC message structure */
1292 /* m(I) - pointer to mbuf chain */
1293 /* off(I) - offset within mbuf chain */
1295 /* Write a new universal address string to this packet, adjusting */
1296 /* lengths as necessary. */
1297 /* -------------------------------------------------------------------- */
1299 ippr_rpcb_modv3(fin, nat, rm, m, off)
1306 u_int len, xlen, pos, bogo;
1313 i = (char *)&nat->nat_outip.s_addr;
1314 p = (char *)&rr->rr_v3.xu_port;
1316 /* Form new string. */
1317 bzero(uaddr, sizeof(uaddr)); /* Just in case we need padding. */
1318 #if defined(SNPRINTF) && defined(_KERNEL)
1319 SNPRINTF(uaddr, sizeof(uaddr),
1321 (void) sprintf(uaddr,
1323 "%u.%u.%u.%u.%u.%u", i[0] & 0xff, i[1] & 0xff,
1324 i[2] & 0xff, i[3] & 0xff, p[0] & 0xff, p[1] & 0xff);
1325 len = strlen(uaddr);
1326 xlen = XDRALIGN(len);
1328 /* Determine mbuf offset to write to. */
1329 pos = (char *)rr->rr_v3.xu_xslen - rm->rm_msgbuf;
1332 /* Write new string length. */
1334 COPYBACK(m, off, 4, (caddr_t)&bogo);
1337 /* Write new string. */
1338 COPYBACK(m, off, xlen, uaddr);
1340 /* Determine difference in data lengths. */
1341 diff = xlen - XDRALIGN(B(rr->rr_v3.xu_xslen));
1344 * If our new string has a different length, make necessary
1348 ippr_rpcb_fixlen(fin, diff);
1353 /* -------------------------------------------------------------------- */
1354 /* Function: ippr_rpcb_modv4 */
1355 /* Returns: int -- change in packet length */
1356 /* Parameters: fin(I) - pointer to packet information */
1357 /* nat(I) - pointer to NAT session */
1358 /* rm(I) - pointer to RPC message structure */
1359 /* m(I) - pointer to mbuf chain */
1360 /* off(I) - offset within mbuf chain */
1362 /* Write new rpcb_entry list, adjusting lengths as necessary. */
1363 /* -------------------------------------------------------------------- */
1365 ippr_rpcb_modv4(fin, nat, rm, m, off)
1372 u_int len, xlen, pos, bogo;
1384 i = (char *)&nat->nat_outip.s_addr;
1386 /* Determine mbuf offset to write to. */
1387 re = &rl->rl_entries[0];
1388 pos = (char *)re->re_maddr.xu_xslen - rm->rm_msgbuf;
1391 for (cnt = 0; cnt < rl->rl_cnt; cnt++) {
1392 re = &rl->rl_entries[cnt];
1393 p = (char *)&re->re_maddr.xu_port;
1395 /* Form new string. */
1396 bzero(uaddr, sizeof(uaddr)); /* Just in case we need
1398 #if defined(SNPRINTF) && defined(_KERNEL)
1399 SNPRINTF(uaddr, sizeof(uaddr),
1401 (void) sprintf(uaddr,
1403 "%u.%u.%u.%u.%u.%u", i[0] & 0xff,
1404 i[1] & 0xff, i[2] & 0xff, i[3] & 0xff,
1405 p[0] & 0xff, p[1] & 0xff);
1406 len = strlen(uaddr);
1407 xlen = XDRALIGN(len);
1409 /* Write new string length. */
1411 COPYBACK(m, off, 4, (caddr_t)&bogo);
1414 /* Write new string. */
1415 COPYBACK(m, off, xlen, uaddr);
1418 /* Record any change in length. */
1419 diff += xlen - XDRALIGN(B(re->re_maddr.xu_xslen));
1421 /* If the length changed, copy back the rest of this entry. */
1422 len = ((char *)re->re_more + 4) -
1423 (char *)re->re_netid.xp_xslen;
1425 COPYBACK(m, off, len, (caddr_t)re->re_netid.xp_xslen);
1431 * If our new string has a different length, make necessary
1435 ippr_rpcb_fixlen(fin, diff);
1441 /* -------------------------------------------------------------------- */
1442 /* Function: ippr_rpcb_fixlen */
1443 /* Returns: (void) */
1444 /* Parameters: fin(I) - pointer to packet information */
1445 /* len(I) - change in packet length */
1447 /* Adjust various packet related lengths held in structure and packet */
1448 /* header fields. */
1449 /* -------------------------------------------------------------------- */
1451 ippr_rpcb_fixlen(fin, len)
1458 udp->uh_ulen = htons(ntohs(udp->uh_ulen) + len);
1459 fin->fin_ip->ip_len += len;
1460 fin->fin_dlen += len;
1461 fin->fin_plen += len;