2 * Copyright (C) 2002-2012 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
42 #define IPF_RPCB_PROXY
47 void ipf_p_rpcb_main_load __P((void));
48 void ipf_p_rpcb_main_unload __P((void));
49 int ipf_p_rpcb_new __P((void *, fr_info_t *, ap_session_t *, nat_t *));
50 void ipf_p_rpcb_del __P((ipf_main_softc_t *, ap_session_t *));
51 int ipf_p_rpcb_in __P((void *, fr_info_t *, ap_session_t *, nat_t *));
52 int ipf_p_rpcb_out __P((void *, fr_info_t *, ap_session_t *, nat_t *));
54 static void ipf_p_rpcb_flush __P((rpcb_session_t *));
55 static int ipf_p_rpcb_decodereq __P((fr_info_t *, nat_t *,
56 rpcb_session_t *, rpc_msg_t *));
57 static int ipf_p_rpcb_skipauth __P((rpc_msg_t *, xdr_auth_t *, u_32_t **));
58 static int ipf_p_rpcb_insert __P((rpcb_session_t *, rpcb_xact_t *));
59 static int ipf_p_rpcb_xdrrpcb __P((rpc_msg_t *, u_32_t *, rpcb_args_t *));
60 static int ipf_p_rpcb_getuaddr __P((rpc_msg_t *, xdr_uaddr_t *,
62 static u_int ipf_p_rpcb_atoi __P((char *));
63 static int ipf_p_rpcb_modreq __P((fr_info_t *, nat_t *, rpc_msg_t *,
65 static int ipf_p_rpcb_decoderep __P((fr_info_t *, nat_t *,
66 rpcb_session_t *, rpc_msg_t *, rpcb_xact_t **));
67 static rpcb_xact_t * ipf_p_rpcb_lookup __P((rpcb_session_t *, u_32_t));
68 static void ipf_p_rpcb_deref __P((rpcb_session_t *, rpcb_xact_t *));
69 static int ipf_p_rpcb_getproto __P((rpc_msg_t *, xdr_proto_t *,
71 static int ipf_p_rpcb_getnat __P((fr_info_t *, nat_t *, u_int, u_int));
72 static int ipf_p_rpcb_modv3 __P((fr_info_t *, nat_t *, rpc_msg_t *,
74 static int ipf_p_rpcb_modv4 __P((fr_info_t *, nat_t *, rpc_msg_t *,
76 static void ipf_p_rpcb_fixlen __P((fr_info_t *, int));
81 static frentry_t rpcbfr; /* Skeleton rule for reference by entities
82 this proxy creates. */
83 static VNET_DEFINE(int, rpcbcnt);
84 #define V_rpcbcnt VNET(rpcbcnt)
85 /* Upper bound of allocated RPCB sessions. */
86 /* XXX rpcbcnt still requires locking. */
88 static int rpcb_proxy_init = 0;
92 * Since rpc_msg contains only pointers, one should use this macro as a
93 * handy way to get to the goods. (In case you're wondering about the name,
94 * this started as BYTEREF -> BREF -> B.)
96 #define B(r) (u_32_t)ntohl(*(r))
102 /* -------------------------------------------------------------------- */
103 /* Function: ipf_p_rpcb_main_load */
105 /* Parameters: (void) */
107 /* Initialize the filter rule entry and session limiter. */
108 /* -------------------------------------------------------------------- */
110 ipf_p_rpcb_main_load()
114 bzero((char *)&rpcbfr, sizeof(rpcbfr));
116 rpcbfr.fr_flags = FR_PASS|FR_QUICK|FR_KEEPSTATE;
117 MUTEX_INIT(&rpcbfr.fr_lock, "ipf Sun RPCB proxy rule lock");
121 /* -------------------------------------------------------------------- */
122 /* Function: ipf_p_rpcb_main_unload */
124 /* Parameters: (void) */
126 /* Destroy rpcbfr's mutex to avoid a lock leak. */
127 /* -------------------------------------------------------------------- */
129 ipf_p_rpcb_main_unload()
131 if (rpcb_proxy_init == 1) {
132 MUTEX_DESTROY(&rpcbfr.fr_lock);
137 /* -------------------------------------------------------------------- */
138 /* Function: ipf_p_rpcb_new */
139 /* Returns: int - -1 == failure, 0 == success */
140 /* Parameters: fin(I) - pointer to packet information */
141 /* aps(I) - pointer to proxy session structure */
142 /* nat(I) - pointer to NAT session structure */
144 /* Allocate resources for per-session proxy structures. */
145 /* -------------------------------------------------------------------- */
147 ipf_p_rpcb_new(arg, fin, aps, nat)
155 nat = nat; /* LINT */
160 KMALLOC(rs, rpcb_session_t *);
164 bzero((char *)rs, sizeof(*rs));
165 MUTEX_INIT(&rs->rs_rxlock, "ipf Sun RPCB proxy session lock");
172 /* -------------------------------------------------------------------- */
173 /* Function: ipf_p_rpcb_del */
175 /* Parameters: aps(I) - pointer to proxy session structure */
177 /* Free up a session's list of RPCB requests. */
178 /* -------------------------------------------------------------------- */
180 ipf_p_rpcb_del(softc, aps)
181 ipf_main_softc_t *softc;
185 rs = (rpcb_session_t *)aps->aps_data;
187 MUTEX_ENTER(&rs->rs_rxlock);
188 ipf_p_rpcb_flush(rs);
189 MUTEX_EXIT(&rs->rs_rxlock);
190 MUTEX_DESTROY(&rs->rs_rxlock);
193 /* -------------------------------------------------------------------- */
194 /* Function: ipf_p_rpcb_in */
195 /* Returns: int - APR_ERR(1) == drop the packet, */
196 /* APR_ERR(2) == kill the proxy session, */
197 /* else change in packet length (in bytes) */
198 /* Parameters: fin(I) - pointer to packet information */
199 /* ip(I) - pointer to packet header */
200 /* aps(I) - pointer to proxy session structure */
201 /* nat(I) - pointer to NAT session structure */
203 /* Given a presumed RPCB request, perform some minor tests and pass off */
204 /* for decoding. Also pass packet off for a rewrite if necessary. */
205 /* -------------------------------------------------------------------- */
207 ipf_p_rpcb_in(arg, fin, aps, nat)
213 rpc_msg_t rpcmsg, *rm;
219 /* Disallow fragmented or illegally short packets. */
220 if ((fin->fin_flx & (FI_FRAG|FI_SHORT)) != 0)
223 /* Perform basic variable initialization. */
224 rs = (rpcb_session_t *)aps->aps_data;
227 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
228 off += sizeof(udphdr_t) + fin->fin_ipoff;
229 dlen = fin->fin_dlen - sizeof(udphdr_t);
231 /* Disallow packets outside legal range for supported requests. */
232 if ((dlen < RPCB_REQMIN) || (dlen > RPCB_REQMAX))
235 /* Copy packet over to convenience buffer. */
237 bzero((char *)rm, sizeof(*rm));
238 COPYDATA(m, off, dlen, (caddr_t)&rm->rm_msgbuf);
239 rm->rm_buflen = dlen;
241 /* Send off to decode request. */
242 rv = ipf_p_rpcb_decodereq(fin, nat, rs, rm);
253 rv = ipf_p_rpcb_modreq(fin, nat, rm, m, off);
256 /*CONSTANTCONDITION*/
257 IPF_PANIC(1, ("illegal rv %d (ipf_p_rpcb_req)", rv));
263 /* -------------------------------------------------------------------- */
264 /* Function: ipf_p_rpcb_out */
265 /* Returns: int - APR_ERR(1) == drop the packet, */
266 /* APR_ERR(2) == kill the proxy session, */
267 /* else change in packet length (in bytes) */
268 /* Parameters: fin(I) - pointer to packet information */
269 /* ip(I) - pointer to packet header */
270 /* aps(I) - pointer to proxy session structure */
271 /* nat(I) - pointer to NAT session structure */
273 /* Given a presumed RPCB reply, perform some minor tests and pass off */
274 /* for decoding. If the message indicates a successful request with */
275 /* valid addressing information, create NAT and state structures to */
276 /* allow direct communication between RPC client and server. */
277 /* -------------------------------------------------------------------- */
279 ipf_p_rpcb_out(arg, fin, aps, nat)
285 rpc_msg_t rpcmsg, *rm;
292 /* Disallow fragmented or illegally short packets. */
293 if ((fin->fin_flx & (FI_FRAG|FI_SHORT)) != 0)
296 /* Perform basic variable initialization. */
297 rs = (rpcb_session_t *)aps->aps_data;
301 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
302 off += sizeof(udphdr_t) + fin->fin_ipoff;
303 dlen = fin->fin_dlen - sizeof(udphdr_t);
306 /* Disallow packets outside legal range for supported requests. */
307 if ((dlen < RPCB_REPMIN) || (dlen > RPCB_REPMAX))
310 /* Copy packet over to convenience buffer. */
312 bzero((char *)rm, sizeof(*rm));
313 COPYDATA(m, off, dlen, (caddr_t)&rm->rm_msgbuf);
314 rm->rm_buflen = dlen;
316 rx = NULL; /* XXX gcc */
318 /* Send off to decode reply. */
319 rv = ipf_p_rpcb_decoderep(fin, nat, rs, rm, &rx);
323 case -1: /* Bad packet */
325 MUTEX_ENTER(&rs->rs_rxlock);
326 ipf_p_rpcb_deref(rs, rx);
327 MUTEX_EXIT(&rs->rs_rxlock);
332 case 0: /* Negative reply / request rejected */
334 case 1: /* Positive reply */
336 * With the IP address embedded in a GETADDR(LIST) reply,
337 * we'll need to rewrite the packet in the very possible
338 * event that the internal & external addresses aren't the
339 * same. (i.e., this box is either a router or rpcbind
340 * only listens on loopback.)
342 if (nat->nat_odstaddr != nat->nat_ndstaddr) {
343 if (rx->rx_type == RPCB_RES_STRING)
344 diff = ipf_p_rpcb_modv3(fin, nat, rm, m, off);
345 else if (rx->rx_type == RPCB_RES_LIST)
346 diff = ipf_p_rpcb_modv4(fin, nat, rm, m, off);
350 /*CONSTANTCONDITION*/
351 IPF_PANIC(1, ("illegal rv %d (ipf_p_rpcb_decoderep)", rv));
355 MUTEX_ENTER(&rs->rs_rxlock);
356 /* XXX Gross hack - I'm overloading the reference
357 * counter to deal with both threads and retransmitted
358 * requests. One deref signals that this thread is
359 * finished with rx, and the other signals that we've
360 * processed its reply.
362 ipf_p_rpcb_deref(rs, rx);
363 ipf_p_rpcb_deref(rs, rx);
364 MUTEX_EXIT(&rs->rs_rxlock);
371 * Private support subroutines
374 /* -------------------------------------------------------------------- */
375 /* Function: ipf_p_rpcb_flush */
377 /* Parameters: rs(I) - pointer to RPCB session structure */
379 /* Simply flushes the list of outstanding transactions, if any. */
380 /* -------------------------------------------------------------------- */
385 rpcb_xact_t *r1, *r2;
398 /* -------------------------------------------------------------------- */
399 /* Function: ipf_p_rpcb_decodereq */
400 /* Returns: int - -1 == bad request or critical failure, */
401 /* 0 == request successfully decoded, */
402 /* 1 == request successfully decoded; requires */
403 /* address rewrite/modification */
404 /* Parameters: fin(I) - pointer to packet information */
405 /* nat(I) - pointer to NAT session structure */
406 /* rs(I) - pointer to RPCB session structure */
407 /* rm(I) - pointer to RPC message structure */
409 /* Take a presumed RPCB request, decode it, and store the results in */
410 /* the transaction list. If the internal target address needs to be */
411 /* modified, store its location in ptr. */
412 /* WARNING: It's the responsibility of the caller to make sure there */
413 /* is enough room in rs_buf for the basic RPC message "preamble". */
414 /* -------------------------------------------------------------------- */
416 ipf_p_rpcb_decodereq(fin, nat, rs, rm)
428 p = (u_32_t *)rm->rm_msgbuf;
431 bzero((char *)&rx, sizeof(rx));
435 rx.rx_xid = B(p++); /* Record this message's XID. */
437 /* Parse out and test the RPC header. */
438 if ((B(p++) != RPCB_CALL) ||
439 (B(p++) != RPCB_MSG_VERSION) ||
440 (B(p++) != RPCB_PROG))
443 /* Record the RPCB version and procedure. */
447 /* Bypass RPC authentication stuff. */
448 if (ipf_p_rpcb_skipauth(rm, &rc->rc_authcred, &p) != 0)
450 if (ipf_p_rpcb_skipauth(rm, &rc->rc_authverf, &p) != 0)
453 /* Compare RPCB version and procedure numbers. */
454 switch(B(rc->rc_vers))
457 /* This proxy only supports PMAP_GETPORT. */
458 if (B(rc->rc_proc) != RPCB_GETPORT)
461 /* Portmap requests contain four 4 byte parameters. */
462 if (RPCB_BUF_EQ(rm, p, 16) == 0)
465 p += 2; /* Skip requested program and version numbers. */
467 /* Sanity check the requested protocol. */
469 if (!(xdr == IPPROTO_UDP || xdr == IPPROTO_TCP))
472 rx.rx_type = RPCB_RES_PMAP;
477 /* GETADDRLIST is exclusive to v4; GETADDR for v3 & v4 */
478 switch(B(rc->rc_proc))
481 rx.rx_type = RPCB_RES_STRING;
482 rx.rx_proto = (u_int)fin->fin_p;
484 case RPCB_GETADDRLIST:
485 if (B(rc->rc_vers) != 4)
487 rx.rx_type = RPCB_RES_LIST;
493 ra = &rc->rc_rpcbargs;
495 /* Decode the 'struct rpcb' request. */
496 if (ipf_p_rpcb_xdrrpcb(rm, p, ra) != 0)
499 /* Are the target address & port valid? */
500 if ((ra->ra_maddr.xu_ip != nat->nat_ndstaddr) ||
501 (ra->ra_maddr.xu_port != nat->nat_ndport))
504 /* Do we need to rewrite this packet? */
505 if ((nat->nat_ndstaddr != nat->nat_odstaddr) ||
506 (nat->nat_ndport != nat->nat_odport))
513 MUTEX_ENTER(&rs->rs_rxlock);
514 if (ipf_p_rpcb_insert(rs, &rx) != 0) {
515 MUTEX_EXIT(&rs->rs_rxlock);
518 MUTEX_EXIT(&rs->rs_rxlock);
523 /* -------------------------------------------------------------------- */
524 /* Function: ipf_p_rpcb_skipauth */
525 /* Returns: int -- -1 == illegal auth parameters (lengths) */
526 /* 0 == valid parameters, pointer advanced */
527 /* Parameters: rm(I) - pointer to RPC message structure */
528 /* auth(I) - pointer to RPC auth structure */
529 /* buf(IO) - pointer to location within convenience buffer */
531 /* Record auth data length & location of auth data, then advance past */
533 /* -------------------------------------------------------------------- */
535 ipf_p_rpcb_skipauth(rm, auth, buf)
544 /* Make sure we have enough space for expected fixed auth parms. */
545 if (RPCB_BUF_GEQ(rm, p, 8) == 0)
548 p++; /* We don't care about auth_flavor. */
550 auth->xa_string.xs_len = p;
551 xdr = B(p++); /* Length of auth_data */
553 /* Test for absurdity / illegality of auth_data length. */
554 if ((XDRALIGN(xdr) < xdr) || (RPCB_BUF_GEQ(rm, p, XDRALIGN(xdr)) == 0))
557 auth->xa_string.xs_str = (char *)p;
559 p += XDRALIGN(xdr); /* Advance our location. */
566 /* -------------------------------------------------------------------- */
567 /* Function: ipf_p_rpcb_insert */
568 /* Returns: int -- -1 == list insertion failed, */
569 /* 0 == item successfully added */
570 /* Parameters: rs(I) - pointer to RPCB session structure */
571 /* rx(I) - pointer to RPCB transaction structure */
572 /* -------------------------------------------------------------------- */
574 ipf_p_rpcb_insert(rs, rx)
580 rxp = ipf_p_rpcb_lookup(rs, rx->rx_xid);
586 if (V_rpcbcnt == RPCB_MAXREQS)
589 KMALLOC(rxp, rpcb_xact_t *);
593 bcopy((char *)rx, (char *)rxp, sizeof(*rx));
595 if (rs->rs_rxlist != NULL)
596 rs->rs_rxlist->rx_pnext = &rxp->rx_next;
598 rxp->rx_pnext = &rs->rs_rxlist;
599 rxp->rx_next = rs->rs_rxlist;
609 /* -------------------------------------------------------------------- */
610 /* Function: ipf_p_rpcb_xdrrpcb */
611 /* Returns: int -- -1 == failure to properly decode the request */
612 /* 0 == rpcb successfully decoded */
613 /* Parameters: rs(I) - pointer to RPCB session structure */
614 /* p(I) - pointer to location within session buffer */
615 /* rpcb(O) - pointer to rpcb (xdr type) structure */
617 /* Decode a XDR encoded rpcb structure and record its contents in rpcb */
618 /* within only the context of TCP/UDP over IP networks. */
619 /* -------------------------------------------------------------------- */
621 ipf_p_rpcb_xdrrpcb(rm, p, ra)
626 if (!RPCB_BUF_GEQ(rm, p, 20))
629 /* Bypass target program & version. */
632 /* Decode r_netid. Must be "tcp" or "udp". */
633 if (ipf_p_rpcb_getproto(rm, &ra->ra_netid, &p) != 0)
636 /* Decode r_maddr. */
637 if (ipf_p_rpcb_getuaddr(rm, &ra->ra_maddr, &p) != 0)
640 /* Advance to r_owner and make sure it's empty. */
641 if (!RPCB_BUF_EQ(rm, p, 4) || (B(p) != 0))
647 /* -------------------------------------------------------------------- */
648 /* Function: ipf_p_rpcb_getuaddr */
649 /* Returns: int -- -1 == illegal string, */
650 /* 0 == string parsed; contents recorded */
651 /* Parameters: rm(I) - pointer to RPC message structure */
652 /* xu(I) - pointer to universal address structure */
653 /* p(IO) - pointer to location within message buffer */
655 /* Decode the IP address / port at p and record them in xu. */
656 /* -------------------------------------------------------------------- */
658 ipf_p_rpcb_getuaddr(rm, xu, p)
663 char *c, *i, *b, *pp;
667 /* Test for string length. */
668 if (!RPCB_BUF_GEQ(rm, *p, 4))
671 xu->xu_xslen = (*p)++;
672 xu->xu_xsstr = (char *)*p;
676 if (l < 11 || l > 23 || !RPCB_BUF_GEQ(rm, *p, XDRALIGN(l)))
680 *(char **)p += XDRALIGN(l);
682 /* Copy string to local buffer & terminate C style */
683 bcopy(xu->xu_xsstr, uastr, l);
686 i = (char *)&xu->xu_ip;
687 pp = (char *)&xu->xu_port;
690 * Expected format: a.b.c.d.e.f where [a-d] correspond to bytes of
691 * an IP address and [ef] are the bytes of a L4 port.
693 if (!(ISDIGIT(uastr[0]) && ISDIGIT(uastr[l-1])))
696 for (c = &uastr[1], d = 0, dd = 0; c < &uastr[l-1]; c++) {
705 /* Check for ASCII byte. */
707 t = ipf_p_rpcb_atoi(b);
711 /* Aim b at beginning of the next byte. */
714 /* Switch off IP addr vs port parsing. */
718 pp[d++ - 4] = t & 0xff;
725 if (d != 5) /* String must contain exactly 5 periods. */
728 /* Handle the last byte (port low byte) */
729 t = ipf_p_rpcb_atoi(b);
732 pp[d - 4] = t & 0xff;
737 /* -------------------------------------------------------------------- */
738 /* Function: ipf_p_rpcb_atoi (XXX should be generic for all proxies) */
739 /* Returns: int -- integer representation of supplied string */
740 /* Parameters: ptr(I) - input string */
742 /* Simple version of atoi(3) ripped from ip_rcmd_pxy.c. */
743 /* -------------------------------------------------------------------- */
748 register char *s = ptr, c;
749 register u_int i = 0;
751 while (((c = *s++) != '\0') && ISDIGIT(c)) {
758 /* -------------------------------------------------------------------- */
759 /* Function: ipf_p_rpcb_modreq */
760 /* Returns: int -- change in datagram length */
761 /* APR_ERR(2) - critical failure */
762 /* Parameters: fin(I) - pointer to packet information */
763 /* nat(I) - pointer to NAT session */
764 /* rm(I) - pointer to RPC message structure */
765 /* m(I) - pointer to mbuf chain */
766 /* off(I) - current offset within mbuf chain */
768 /* When external and internal addresses differ, we rewrite the former */
769 /* with the latter. (This is exclusive to protocol versions 3 & 4). */
770 /* -------------------------------------------------------------------- */
772 ipf_p_rpcb_modreq(fin, nat, rm, m, off)
779 u_int len, xlen, pos, bogo;
786 ra = &rm->rm_call.rc_rpcbargs;
787 i = (char *)&nat->nat_odstaddr;
788 p = (char *)&nat->nat_odport;
790 /* Form new string. */
791 bzero(uaddr, sizeof(uaddr)); /* Just in case we need padding. */
792 #if defined(SNPRINTF) && defined(_KERNEL)
793 SNPRINTF(uaddr, sizeof(uaddr),
795 (void) sprintf(uaddr,
797 "%u.%u.%u.%u.%u.%u", i[0] & 0xff, i[1] & 0xff,
798 i[2] & 0xff, i[3] & 0xff, p[0] & 0xff, p[1] & 0xff);
800 xlen = XDRALIGN(len);
802 /* Determine mbuf offset to start writing to. */
803 pos = (char *)ra->ra_maddr.xu_xslen - rm->rm_msgbuf;
806 /* Write new string length. */
808 COPYBACK(m, off, 4, (caddr_t)&bogo);
811 /* Write new string. */
812 COPYBACK(m, off, xlen, uaddr);
815 /* Write in zero r_owner. */
817 COPYBACK(m, off, 4, (caddr_t)&bogo);
819 /* Determine difference in data lengths. */
820 diff = xlen - XDRALIGN(B(ra->ra_maddr.xu_xslen));
823 * If our new string has a different length, make necessary
828 udp->uh_ulen = htons(ntohs(udp->uh_ulen) + diff);
829 fin->fin_plen += diff;
830 fin->fin_ip->ip_len = htons(fin->fin_plen);
831 fin->fin_dlen += diff;
832 /* XXX Storage lengths. */
838 /* -------------------------------------------------------------------- */
839 /* Function: ipf_p_rpcb_decoderep */
840 /* Returns: int - -1 == bad request or critical failure, */
841 /* 0 == valid, negative reply */
842 /* 1 == vaddlid, positive reply; needs no changes */
843 /* Parameters: fin(I) - pointer to packet information */
844 /* nat(I) - pointer to NAT session structure */
845 /* rs(I) - pointer to RPCB session structure */
846 /* rm(I) - pointer to RPC message structure */
847 /* rxp(O) - pointer to RPCB transaction structure */
849 /* Take a presumed RPCB reply, extract the XID, search for the original */
850 /* request information, and determine whether the request was accepted */
851 /* or rejected. With a valid accepted reply, go ahead and create NAT */
852 /* and state entries, and finish up by rewriting the packet as */
855 /* WARNING: It's the responsibility of the caller to make sure there */
856 /* is enough room in rs_buf for the basic RPC message "preamble". */
857 /* -------------------------------------------------------------------- */
859 ipf_p_rpcb_decoderep(fin, nat, rs, rm, rxp)
873 p = (u_32_t *)rm->rm_msgbuf;
875 bzero((char *)&rx, sizeof(rx));
879 xdr = B(p++); /* Record this message's XID. */
882 MUTEX_ENTER(&rs->rs_rxlock);
883 if ((rx = ipf_p_rpcb_lookup(rs, xdr)) == NULL) {
884 MUTEX_EXIT(&rs->rs_rxlock);
887 ++rx->rx_ref; /* per thread reference */
888 MUTEX_EXIT(&rs->rs_rxlock);
892 /* Test call vs reply */
893 if (B(p++) != RPCB_REPLY)
896 /* Test reply_stat */
899 case RPCB_MSG_DENIED:
901 case RPCB_MSG_ACCEPTED:
907 /* Bypass RPC authentication stuff. */
908 if (ipf_p_rpcb_skipauth(rm, &rr->rr_authverf, &p) != 0)
911 /* Test accept status */
912 if (!RPCB_BUF_GEQ(rm, p, 4))
917 /* Parse out the expected reply */
921 /* There must be only one 4 byte argument. */
922 if (!RPCB_BUF_EQ(rm, p, 4))
928 /* Reply w/ a 0 port indicates service isn't registered */
932 /* Is the value sane? */
936 /* Create NAT & state table entries. */
937 if (ipf_p_rpcb_getnat(fin, nat, rx->rx_proto, (u_int)xdr) != 0)
940 case RPCB_RES_STRING:
941 /* Expecting a XDR string; need 4 bytes for length */
942 if (!RPCB_BUF_GEQ(rm, p, 4))
945 rr->rr_v3.xu_str.xs_len = p++;
946 rr->rr_v3.xu_str.xs_str = (char *)p;
948 xdr = B(rr->rr_v3.xu_xslen);
950 /* A null string indicates an unregistered service */
951 if ((xdr == 0) && RPCB_BUF_EQ(rm, p, 0))
954 /* Decode the target IP address / port. */
955 if (ipf_p_rpcb_getuaddr(rm, &rr->rr_v3, &p) != 0)
958 /* Validate the IP address and port contained. */
959 if (nat->nat_odstaddr != rr->rr_v3.xu_ip)
962 /* Create NAT & state table entries. */
963 if (ipf_p_rpcb_getnat(fin, nat, rx->rx_proto,
964 (u_int)rr->rr_v3.xu_port) != 0)
968 if (!RPCB_BUF_GEQ(rm, p, 4))
970 /* rpcb_entry_list_ptr */
987 re = &rl->rl_entries[rl->rl_cnt];
988 if (ipf_p_rpcb_getuaddr(rm, &re->re_maddr, &p) != 0)
990 if (ipf_p_rpcb_getproto(rm, &re->re_netid, &p) != 0)
992 /* re_semantics & re_pfamily length */
993 if (!RPCB_BUF_GEQ(rm, p, 12))
995 p++; /* Skipping re_semantics. */
997 if ((xdr != 4) || strncmp((char *)p, "inet", 4))
1000 if (ipf_p_rpcb_getproto(rm, &re->re_proto, &p) != 0)
1002 if (!RPCB_BUF_GEQ(rm, p, 4))
1005 if (B(re->re_more) > 1) /* 0,1 only legal values */
1009 if (B(re->re_more) == 0)
1011 /* Replies in max out at 2; TCP and/or UDP */
1017 for(rl->rl_cnt = 0; rl->rl_cnt < cnt; rl->rl_cnt++) {
1018 re = &rl->rl_entries[rl->rl_cnt];
1019 rv = ipf_p_rpcb_getnat(fin, nat,
1020 re->re_proto.xp_proto,
1021 (u_int)re->re_maddr.xu_port);
1027 /*CONSTANTCONDITION*/
1028 IPF_PANIC(1, ("illegal rx_type %d", rx->rx_type));
1034 /* -------------------------------------------------------------------- */
1035 /* Function: ipf_p_rpcb_lookup */
1036 /* Returns: rpcb_xact_t * - NULL == no matching record, */
1037 /* else pointer to relevant entry */
1038 /* Parameters: rs(I) - pointer to RPCB session */
1039 /* xid(I) - XID to look for */
1040 /* -------------------------------------------------------------------- */
1041 static rpcb_xact_t *
1042 ipf_p_rpcb_lookup(rs, xid)
1048 if (rs->rs_rxlist == NULL)
1051 for (rx = rs->rs_rxlist; rx != NULL; rx = rx->rx_next)
1052 if (rx->rx_xid == xid)
1058 /* -------------------------------------------------------------------- */
1059 /* Function: ipf_p_rpcb_deref */
1060 /* Returns: (void) */
1061 /* Parameters: rs(I) - pointer to RPCB session */
1062 /* rx(I) - pointer to RPC transaction struct to remove */
1063 /* force(I) - indicates to delete entry regardless of */
1064 /* reference count */
1065 /* Locking: rs->rs_rxlock must be held write only */
1067 /* Free the RPCB transaction record rx from the chain of entries. */
1068 /* -------------------------------------------------------------------- */
1070 ipf_p_rpcb_deref(rs, rx)
1079 if (--rx->rx_ref != 0)
1082 if (rx->rx_next != NULL)
1083 rx->rx_next->rx_pnext = rx->rx_pnext;
1085 *rx->rx_pnext = rx->rx_next;
1092 /* -------------------------------------------------------------------- */
1093 /* Function: ipf_p_rpcb_getproto */
1094 /* Returns: int - -1 == illegal protocol/netid, */
1095 /* 0 == legal protocol/netid */
1096 /* Parameters: rm(I) - pointer to RPC message structure */
1097 /* xp(I) - pointer to netid structure */
1098 /* p(IO) - pointer to location within packet buffer */
1100 /* Decode netid/proto stored at p and record its numeric value. */
1101 /* -------------------------------------------------------------------- */
1103 ipf_p_rpcb_getproto(rm, xp, p)
1110 /* Must have 4 bytes for length & 4 bytes for "tcp" or "udp". */
1111 if (!RPCB_BUF_GEQ(rm, p, 8))
1114 xp->xp_xslen = (*p)++;
1115 xp->xp_xsstr = (char *)*p;
1117 /* Test the string length. */
1118 len = B(xp->xp_xslen);
1122 /* Test the actual string & record the protocol accordingly. */
1123 if (!strncmp((char *)xp->xp_xsstr, "tcp\0", 4))
1124 xp->xp_proto = IPPROTO_TCP;
1125 else if (!strncmp((char *)xp->xp_xsstr, "udp\0", 4))
1126 xp->xp_proto = IPPROTO_UDP;
1131 /* Advance past the string. */
1137 /* -------------------------------------------------------------------- */
1138 /* Function: ipf_p_rpcb_getnat */
1139 /* Returns: int -- -1 == failed to create table entries, */
1141 /* Parameters: fin(I) - pointer to packet information */
1142 /* nat(I) - pointer to NAT table entry */
1143 /* proto(I) - transport protocol for new entries */
1144 /* port(I) - new port to use w/ wildcard table entries */
1146 /* Create state and NAT entries to handle an anticipated connection */
1147 /* attempt between RPC client and server. */
1148 /* -------------------------------------------------------------------- */
1150 ipf_p_rpcb_getnat(fin, nat, proto, port)
1156 ipf_main_softc_t *softc = fin->fin_main_soft;
1157 ipnat_t *ipn, ipnat;
1166 /* Generate dummy fr_info */
1167 bcopy((char *)fin, (char *)&fi, sizeof(fi));
1171 fi.fin_dport = port & 0xffff;
1172 fi.fin_flx |= FI_IGNORE;
1173 fi.fin_saddr = nat->nat_osrcaddr;
1174 fi.fin_daddr = nat->nat_odstaddr;
1176 bzero((char *)&tcp, sizeof(tcp));
1177 tcp.th_dport = htons(port);
1179 if (proto == IPPROTO_TCP) {
1180 tcp.th_win = htons(8192);
1181 TCP_OFF_A(&tcp, sizeof(tcphdr_t) >> 2);
1182 fi.fin_dlen = sizeof(tcphdr_t);
1183 tcp.th_flags = TH_SYN;
1186 fi.fin_dlen = sizeof(udphdr_t);
1190 nflags |= SI_W_SPORT|NAT_SEARCH;
1192 fi.fin_plen = fi.fin_hlen + fi.fin_dlen;
1195 * Search for existing NAT & state entries. Pay close attention to
1196 * mutexes / locks grabbed from lookup routines, as not doing so could
1197 * lead to bad things.
1199 * If successful, fr_stlookup returns with ipf_state locked. We have
1200 * no use for this lock, so simply unlock it if necessary.
1202 is = ipf_state_lookup(&fi, &tcp, NULL);
1204 RWLOCK_EXIT(&softc->ipf_state);
1207 RWLOCK_EXIT(&softc->ipf_nat);
1209 WRITE_ENTER(&softc->ipf_nat);
1210 natl = ipf_nat_inlookup(&fi, nflags, proto, fi.fin_src, fi.fin_dst);
1212 if ((natl != NULL) && (is != NULL)) {
1213 MUTEX_DOWNGRADE(&softc->ipf_nat);
1217 /* Slightly modify the following structures for actual use in creating
1218 * NAT and/or state entries. We're primarily concerned with stripping
1219 * flags that may be detrimental to the creation process or simply
1220 * shouldn't be associated with a table entry.
1222 fi.fin_fr = &rpcbfr;
1223 fi.fin_flx &= ~FI_IGNORE;
1224 nflags &= ~NAT_SEARCH;
1228 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1231 /* XXX Since we're just copying the original ipn contents
1232 * back, would we be better off just sending a pointer to
1233 * the 'temp' copy off to nat_new instead?
1235 /* Generate template/bogus NAT rule. */
1236 bcopy((char *)ipn, (char *)&ipnat, sizeof(ipnat));
1237 ipn->in_flags = nflags & IPN_TCPUDP;
1239 ipn->in_pr[0] = proto;
1240 ipn->in_pr[1] = proto;
1241 ipn->in_dpmin = fi.fin_dport;
1242 ipn->in_dpmax = fi.fin_dport;
1243 ipn->in_dpnext = fi.fin_dport;
1246 if (ipn->in_flags & IPN_FILTER) {
1250 ipn->in_plabel = -1;
1252 /* Create NAT entry. return NULL if this fails. */
1253 MUTEX_ENTER(&softn->ipf_nat_new);
1254 natl = ipf_nat_add(&fi, ipn, NULL, nflags|SI_CLONE|NAT_SLAVE,
1256 MUTEX_EXIT(&softn->ipf_nat_new);
1258 bcopy((char *)&ipnat, (char *)ipn, sizeof(ipnat));
1261 MUTEX_DOWNGRADE(&softc->ipf_nat);
1265 natl->nat_ptr = ipn;
1266 fi.fin_saddr = natl->nat_nsrcaddr;
1267 fi.fin_daddr = natl->nat_ndstaddr;
1269 (void) ipf_nat_proto(&fi, natl, nflags);
1270 MUTEX_ENTER(&natl->nat_lock);
1271 ipf_nat_update(&fi, natl);
1272 MUTEX_EXIT(&natl->nat_lock);
1274 MUTEX_DOWNGRADE(&softc->ipf_nat);
1277 /* Create state entry. Return NULL if this fails. */
1278 fi.fin_flx |= FI_NATED;
1279 fi.fin_flx &= ~FI_STATE;
1280 nflags &= NAT_TCPUDP;
1281 nflags |= SI_W_SPORT|SI_CLONE;
1283 if (ipf_state_add(softc, &fi, NULL, nflags) != 0) {
1285 * XXX nat_delete is private to ip_nat.c. Should
1286 * check w/ Darren about this one.
1288 * nat_delete(natl, NL_EXPIRE);
1297 /* -------------------------------------------------------------------- */
1298 /* Function: ipf_p_rpcb_modv3 */
1299 /* Returns: int -- change in packet length */
1300 /* Parameters: fin(I) - pointer to packet information */
1301 /* nat(I) - pointer to NAT session */
1302 /* rm(I) - pointer to RPC message structure */
1303 /* m(I) - pointer to mbuf chain */
1304 /* off(I) - offset within mbuf chain */
1306 /* Write a new universal address string to this packet, adjusting */
1307 /* lengths as necessary. */
1308 /* -------------------------------------------------------------------- */
1310 ipf_p_rpcb_modv3(fin, nat, rm, m, off)
1317 u_int len, xlen, pos, bogo;
1324 i = (char *)&nat->nat_ndstaddr;
1325 p = (char *)&rr->rr_v3.xu_port;
1327 /* Form new string. */
1328 bzero(uaddr, sizeof(uaddr)); /* Just in case we need padding. */
1329 #if defined(SNPRINTF) && defined(_KERNEL)
1330 SNPRINTF(uaddr, sizeof(uaddr),
1332 (void) sprintf(uaddr,
1334 "%u.%u.%u.%u.%u.%u", i[0] & 0xff, i[1] & 0xff,
1335 i[2] & 0xff, i[3] & 0xff, p[0] & 0xff, p[1] & 0xff);
1336 len = strlen(uaddr);
1337 xlen = XDRALIGN(len);
1339 /* Determine mbuf offset to write to. */
1340 pos = (char *)rr->rr_v3.xu_xslen - rm->rm_msgbuf;
1343 /* Write new string length. */
1345 COPYBACK(m, off, 4, (caddr_t)&bogo);
1348 /* Write new string. */
1349 COPYBACK(m, off, xlen, uaddr);
1351 /* Determine difference in data lengths. */
1352 diff = xlen - XDRALIGN(B(rr->rr_v3.xu_xslen));
1355 * If our new string has a different length, make necessary
1359 ipf_p_rpcb_fixlen(fin, diff);
1364 /* -------------------------------------------------------------------- */
1365 /* Function: ipf_p_rpcb_modv4 */
1366 /* Returns: int -- change in packet length */
1367 /* Parameters: fin(I) - pointer to packet information */
1368 /* nat(I) - pointer to NAT session */
1369 /* rm(I) - pointer to RPC message structure */
1370 /* m(I) - pointer to mbuf chain */
1371 /* off(I) - offset within mbuf chain */
1373 /* Write new rpcb_entry list, adjusting lengths as necessary. */
1374 /* -------------------------------------------------------------------- */
1376 ipf_p_rpcb_modv4(fin, nat, rm, m, off)
1383 u_int len, xlen, pos, bogo;
1395 i = (char *)&nat->nat_ndstaddr;
1397 /* Determine mbuf offset to write to. */
1398 re = &rl->rl_entries[0];
1399 pos = (char *)re->re_maddr.xu_xslen - rm->rm_msgbuf;
1402 for (cnt = 0; cnt < rl->rl_cnt; cnt++) {
1403 re = &rl->rl_entries[cnt];
1404 p = (char *)&re->re_maddr.xu_port;
1406 /* Form new string. */
1407 bzero(uaddr, sizeof(uaddr)); /* Just in case we need
1409 #if defined(SNPRINTF) && defined(_KERNEL)
1410 SNPRINTF(uaddr, sizeof(uaddr),
1412 (void) sprintf(uaddr,
1414 "%u.%u.%u.%u.%u.%u", i[0] & 0xff,
1415 i[1] & 0xff, i[2] & 0xff, i[3] & 0xff,
1416 p[0] & 0xff, p[1] & 0xff);
1417 len = strlen(uaddr);
1418 xlen = XDRALIGN(len);
1420 /* Write new string length. */
1422 COPYBACK(m, off, 4, (caddr_t)&bogo);
1425 /* Write new string. */
1426 COPYBACK(m, off, xlen, uaddr);
1429 /* Record any change in length. */
1430 diff += xlen - XDRALIGN(B(re->re_maddr.xu_xslen));
1432 /* If the length changed, copy back the rest of this entry. */
1433 len = ((char *)re->re_more + 4) -
1434 (char *)re->re_netid.xp_xslen;
1436 COPYBACK(m, off, len, (caddr_t)re->re_netid.xp_xslen);
1442 * If our new string has a different length, make necessary
1446 ipf_p_rpcb_fixlen(fin, diff);
1452 /* -------------------------------------------------------------------- */
1453 /* Function: ipf_p_rpcb_fixlen */
1454 /* Returns: (void) */
1455 /* Parameters: fin(I) - pointer to packet information */
1456 /* len(I) - change in packet length */
1458 /* Adjust various packet related lengths held in structure and packet */
1459 /* header fields. */
1460 /* -------------------------------------------------------------------- */
1462 ipf_p_rpcb_fixlen(fin, len)
1469 udp->uh_ulen = htons(ntohs(udp->uh_ulen) + len);
1470 fin->fin_plen += len;
1471 fin->fin_ip->ip_len = htons(fin->fin_plen);
1472 fin->fin_dlen += len;