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 int rpcbcnt; /* Upper bound of allocated RPCB sessions. */
84 /* XXX rpcbcnt still requires locking. */
86 static int rpcb_proxy_init = 0;
90 * Since rpc_msg contains only pointers, one should use this macro as a
91 * handy way to get to the goods. (In case you're wondering about the name,
92 * this started as BYTEREF -> BREF -> B.)
94 #define B(r) (u_32_t)ntohl(*(r))
100 /* -------------------------------------------------------------------- */
101 /* Function: ipf_p_rpcb_main_load */
103 /* Parameters: (void) */
105 /* Initialize the filter rule entry and session limiter. */
106 /* -------------------------------------------------------------------- */
108 ipf_p_rpcb_main_load()
112 bzero((char *)&rpcbfr, sizeof(rpcbfr));
114 rpcbfr.fr_flags = FR_PASS|FR_QUICK|FR_KEEPSTATE;
115 MUTEX_INIT(&rpcbfr.fr_lock, "ipf Sun RPCB proxy rule lock");
119 /* -------------------------------------------------------------------- */
120 /* Function: ipf_p_rpcb_main_unload */
122 /* Parameters: (void) */
124 /* Destroy rpcbfr's mutex to avoid a lock leak. */
125 /* -------------------------------------------------------------------- */
127 ipf_p_rpcb_main_unload()
129 if (rpcb_proxy_init == 1) {
130 MUTEX_DESTROY(&rpcbfr.fr_lock);
135 /* -------------------------------------------------------------------- */
136 /* Function: ipf_p_rpcb_new */
137 /* Returns: int - -1 == failure, 0 == success */
138 /* Parameters: fin(I) - pointer to packet information */
139 /* aps(I) - pointer to proxy session structure */
140 /* nat(I) - pointer to NAT session structure */
142 /* Allocate resources for per-session proxy structures. */
143 /* -------------------------------------------------------------------- */
145 ipf_p_rpcb_new(arg, fin, aps, nat)
153 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: ipf_p_rpcb_del */
173 /* Parameters: aps(I) - pointer to proxy session structure */
175 /* Free up a session's list of RPCB requests. */
176 /* -------------------------------------------------------------------- */
178 ipf_p_rpcb_del(softc, aps)
179 ipf_main_softc_t *softc;
183 rs = (rpcb_session_t *)aps->aps_data;
185 MUTEX_ENTER(&rs->rs_rxlock);
186 ipf_p_rpcb_flush(rs);
187 MUTEX_EXIT(&rs->rs_rxlock);
188 MUTEX_DESTROY(&rs->rs_rxlock);
191 /* -------------------------------------------------------------------- */
192 /* Function: ipf_p_rpcb_in */
193 /* Returns: int - APR_ERR(1) == drop the packet, */
194 /* APR_ERR(2) == kill the proxy session, */
195 /* else change in packet length (in bytes) */
196 /* Parameters: fin(I) - pointer to packet information */
197 /* ip(I) - pointer to packet header */
198 /* aps(I) - pointer to proxy session structure */
199 /* nat(I) - pointer to NAT session structure */
201 /* Given a presumed RPCB request, perform some minor tests and pass off */
202 /* for decoding. Also pass packet off for a rewrite if necessary. */
203 /* -------------------------------------------------------------------- */
205 ipf_p_rpcb_in(arg, fin, aps, nat)
211 rpc_msg_t rpcmsg, *rm;
217 /* Disallow fragmented or illegally short packets. */
218 if ((fin->fin_flx & (FI_FRAG|FI_SHORT)) != 0)
221 /* Perform basic variable initialization. */
222 rs = (rpcb_session_t *)aps->aps_data;
225 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
226 off += sizeof(udphdr_t) + fin->fin_ipoff;
227 dlen = fin->fin_dlen - sizeof(udphdr_t);
229 /* Disallow packets outside legal range for supported requests. */
230 if ((dlen < RPCB_REQMIN) || (dlen > RPCB_REQMAX))
233 /* Copy packet over to convenience buffer. */
235 bzero((char *)rm, sizeof(*rm));
236 COPYDATA(m, off, dlen, (caddr_t)&rm->rm_msgbuf);
237 rm->rm_buflen = dlen;
239 /* Send off to decode request. */
240 rv = ipf_p_rpcb_decodereq(fin, nat, rs, rm);
251 rv = ipf_p_rpcb_modreq(fin, nat, rm, m, off);
254 /*CONSTANTCONDITION*/
255 IPF_PANIC(1, ("illegal rv %d (ipf_p_rpcb_req)", rv));
261 /* -------------------------------------------------------------------- */
262 /* Function: ipf_p_rpcb_out */
263 /* Returns: int - APR_ERR(1) == drop the packet, */
264 /* APR_ERR(2) == kill the proxy session, */
265 /* else change in packet length (in bytes) */
266 /* Parameters: fin(I) - pointer to packet information */
267 /* ip(I) - pointer to packet header */
268 /* aps(I) - pointer to proxy session structure */
269 /* nat(I) - pointer to NAT session structure */
271 /* Given a presumed RPCB reply, perform some minor tests and pass off */
272 /* for decoding. If the message indicates a successful request with */
273 /* valid addressing information, create NAT and state structures to */
274 /* allow direct communication between RPC client and server. */
275 /* -------------------------------------------------------------------- */
277 ipf_p_rpcb_out(arg, fin, aps, nat)
283 rpc_msg_t rpcmsg, *rm;
290 /* Disallow fragmented or illegally short packets. */
291 if ((fin->fin_flx & (FI_FRAG|FI_SHORT)) != 0)
294 /* Perform basic variable initialization. */
295 rs = (rpcb_session_t *)aps->aps_data;
299 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
300 off += sizeof(udphdr_t) + fin->fin_ipoff;
301 dlen = fin->fin_dlen - sizeof(udphdr_t);
304 /* Disallow packets outside legal range for supported requests. */
305 if ((dlen < RPCB_REPMIN) || (dlen > RPCB_REPMAX))
308 /* Copy packet over to convenience buffer. */
310 bzero((char *)rm, sizeof(*rm));
311 COPYDATA(m, off, dlen, (caddr_t)&rm->rm_msgbuf);
312 rm->rm_buflen = dlen;
314 rx = NULL; /* XXX gcc */
316 /* Send off to decode reply. */
317 rv = ipf_p_rpcb_decoderep(fin, nat, rs, rm, &rx);
321 case -1: /* Bad packet */
323 MUTEX_ENTER(&rs->rs_rxlock);
324 ipf_p_rpcb_deref(rs, rx);
325 MUTEX_EXIT(&rs->rs_rxlock);
330 case 0: /* Negative reply / request rejected */
332 case 1: /* Positive reply */
334 * With the IP address embedded in a GETADDR(LIST) reply,
335 * we'll need to rewrite the packet in the very possible
336 * event that the internal & external addresses aren't the
337 * same. (i.e., this box is either a router or rpcbind
338 * only listens on loopback.)
340 if (nat->nat_odstaddr != nat->nat_ndstaddr) {
341 if (rx->rx_type == RPCB_RES_STRING)
342 diff = ipf_p_rpcb_modv3(fin, nat, rm, m, off);
343 else if (rx->rx_type == RPCB_RES_LIST)
344 diff = ipf_p_rpcb_modv4(fin, nat, rm, m, off);
348 /*CONSTANTCONDITION*/
349 IPF_PANIC(1, ("illegal rv %d (ipf_p_rpcb_decoderep)", rv));
353 MUTEX_ENTER(&rs->rs_rxlock);
354 /* XXX Gross hack - I'm overloading the reference
355 * counter to deal with both threads and retransmitted
356 * requests. One deref signals that this thread is
357 * finished with rx, and the other signals that we've
358 * processed its reply.
360 ipf_p_rpcb_deref(rs, rx);
361 ipf_p_rpcb_deref(rs, rx);
362 MUTEX_EXIT(&rs->rs_rxlock);
369 * Private support subroutines
372 /* -------------------------------------------------------------------- */
373 /* Function: ipf_p_rpcb_flush */
375 /* Parameters: rs(I) - pointer to RPCB session structure */
377 /* Simply flushes the list of outstanding transactions, if any. */
378 /* -------------------------------------------------------------------- */
383 rpcb_xact_t *r1, *r2;
396 /* -------------------------------------------------------------------- */
397 /* Function: ipf_p_rpcb_decodereq */
398 /* Returns: int - -1 == bad request or critical failure, */
399 /* 0 == request successfully decoded, */
400 /* 1 == request successfully decoded; requires */
401 /* address rewrite/modification */
402 /* Parameters: fin(I) - pointer to packet information */
403 /* nat(I) - pointer to NAT session structure */
404 /* rs(I) - pointer to RPCB session structure */
405 /* rm(I) - pointer to RPC message structure */
407 /* Take a presumed RPCB request, decode it, and store the results in */
408 /* the transaction list. If the internal target address needs to be */
409 /* modified, store its location in ptr. */
410 /* WARNING: It's the responsibility of the caller to make sure there */
411 /* is enough room in rs_buf for the basic RPC message "preamble". */
412 /* -------------------------------------------------------------------- */
414 ipf_p_rpcb_decodereq(fin, nat, rs, rm)
426 p = (u_32_t *)rm->rm_msgbuf;
429 bzero((char *)&rx, sizeof(rx));
433 rx.rx_xid = B(p++); /* Record this message's XID. */
435 /* Parse out and test the RPC header. */
436 if ((B(p++) != RPCB_CALL) ||
437 (B(p++) != RPCB_MSG_VERSION) ||
438 (B(p++) != RPCB_PROG))
441 /* Record the RPCB version and procedure. */
445 /* Bypass RPC authentication stuff. */
446 if (ipf_p_rpcb_skipauth(rm, &rc->rc_authcred, &p) != 0)
448 if (ipf_p_rpcb_skipauth(rm, &rc->rc_authverf, &p) != 0)
451 /* Compare RPCB version and procedure numbers. */
452 switch(B(rc->rc_vers))
455 /* This proxy only supports PMAP_GETPORT. */
456 if (B(rc->rc_proc) != RPCB_GETPORT)
459 /* Portmap requests contain four 4 byte parameters. */
460 if (RPCB_BUF_EQ(rm, p, 16) == 0)
463 p += 2; /* Skip requested program and version numbers. */
465 /* Sanity check the requested protocol. */
467 if (!(xdr == IPPROTO_UDP || xdr == IPPROTO_TCP))
470 rx.rx_type = RPCB_RES_PMAP;
475 /* GETADDRLIST is exclusive to v4; GETADDR for v3 & v4 */
476 switch(B(rc->rc_proc))
479 rx.rx_type = RPCB_RES_STRING;
480 rx.rx_proto = (u_int)fin->fin_p;
482 case RPCB_GETADDRLIST:
483 if (B(rc->rc_vers) != 4)
485 rx.rx_type = RPCB_RES_LIST;
491 ra = &rc->rc_rpcbargs;
493 /* Decode the 'struct rpcb' request. */
494 if (ipf_p_rpcb_xdrrpcb(rm, p, ra) != 0)
497 /* Are the target address & port valid? */
498 if ((ra->ra_maddr.xu_ip != nat->nat_ndstaddr) ||
499 (ra->ra_maddr.xu_port != nat->nat_ndport))
502 /* Do we need to rewrite this packet? */
503 if ((nat->nat_ndstaddr != nat->nat_odstaddr) ||
504 (nat->nat_ndport != nat->nat_odport))
511 MUTEX_ENTER(&rs->rs_rxlock);
512 if (ipf_p_rpcb_insert(rs, &rx) != 0) {
513 MUTEX_EXIT(&rs->rs_rxlock);
516 MUTEX_EXIT(&rs->rs_rxlock);
521 /* -------------------------------------------------------------------- */
522 /* Function: ipf_p_rpcb_skipauth */
523 /* Returns: int -- -1 == illegal auth parameters (lengths) */
524 /* 0 == valid parameters, pointer advanced */
525 /* Parameters: rm(I) - pointer to RPC message structure */
526 /* auth(I) - pointer to RPC auth structure */
527 /* buf(IO) - pointer to location within convenience buffer */
529 /* Record auth data length & location of auth data, then advance past */
531 /* -------------------------------------------------------------------- */
533 ipf_p_rpcb_skipauth(rm, auth, buf)
542 /* Make sure we have enough space for expected fixed auth parms. */
543 if (RPCB_BUF_GEQ(rm, p, 8) == 0)
546 p++; /* We don't care about auth_flavor. */
548 auth->xa_string.xs_len = p;
549 xdr = B(p++); /* Length of auth_data */
551 /* Test for absurdity / illegality of auth_data length. */
552 if ((XDRALIGN(xdr) < xdr) || (RPCB_BUF_GEQ(rm, p, XDRALIGN(xdr)) == 0))
555 auth->xa_string.xs_str = (char *)p;
557 p += XDRALIGN(xdr); /* Advance our location. */
564 /* -------------------------------------------------------------------- */
565 /* Function: ipf_p_rpcb_insert */
566 /* Returns: int -- -1 == list insertion failed, */
567 /* 0 == item successfully added */
568 /* Parameters: rs(I) - pointer to RPCB session structure */
569 /* rx(I) - pointer to RPCB transaction structure */
570 /* -------------------------------------------------------------------- */
572 ipf_p_rpcb_insert(rs, rx)
578 rxp = ipf_p_rpcb_lookup(rs, rx->rx_xid);
584 if (rpcbcnt == RPCB_MAXREQS)
587 KMALLOC(rxp, rpcb_xact_t *);
591 bcopy((char *)rx, (char *)rxp, sizeof(*rx));
593 if (rs->rs_rxlist != NULL)
594 rs->rs_rxlist->rx_pnext = &rxp->rx_next;
596 rxp->rx_pnext = &rs->rs_rxlist;
597 rxp->rx_next = rs->rs_rxlist;
607 /* -------------------------------------------------------------------- */
608 /* Function: ipf_p_rpcb_xdrrpcb */
609 /* Returns: int -- -1 == failure to properly decode the request */
610 /* 0 == rpcb successfully decoded */
611 /* Parameters: rs(I) - pointer to RPCB session structure */
612 /* p(I) - pointer to location within session buffer */
613 /* rpcb(O) - pointer to rpcb (xdr type) structure */
615 /* Decode a XDR encoded rpcb structure and record its contents in rpcb */
616 /* within only the context of TCP/UDP over IP networks. */
617 /* -------------------------------------------------------------------- */
619 ipf_p_rpcb_xdrrpcb(rm, p, ra)
624 if (!RPCB_BUF_GEQ(rm, p, 20))
627 /* Bypass target program & version. */
630 /* Decode r_netid. Must be "tcp" or "udp". */
631 if (ipf_p_rpcb_getproto(rm, &ra->ra_netid, &p) != 0)
634 /* Decode r_maddr. */
635 if (ipf_p_rpcb_getuaddr(rm, &ra->ra_maddr, &p) != 0)
638 /* Advance to r_owner and make sure it's empty. */
639 if (!RPCB_BUF_EQ(rm, p, 4) || (B(p) != 0))
645 /* -------------------------------------------------------------------- */
646 /* Function: ipf_p_rpcb_getuaddr */
647 /* Returns: int -- -1 == illegal string, */
648 /* 0 == string parsed; contents recorded */
649 /* Parameters: rm(I) - pointer to RPC message structure */
650 /* xu(I) - pointer to universal address structure */
651 /* p(IO) - pointer to location within message buffer */
653 /* Decode the IP address / port at p and record them in xu. */
654 /* -------------------------------------------------------------------- */
656 ipf_p_rpcb_getuaddr(rm, xu, p)
661 char *c, *i, *b, *pp;
665 /* Test for string length. */
666 if (!RPCB_BUF_GEQ(rm, *p, 4))
669 xu->xu_xslen = (*p)++;
670 xu->xu_xsstr = (char *)*p;
674 if (l < 11 || l > 23 || !RPCB_BUF_GEQ(rm, *p, XDRALIGN(l)))
678 *(char **)p += XDRALIGN(l);
680 /* Copy string to local buffer & terminate C style */
681 bcopy(xu->xu_xsstr, uastr, l);
684 i = (char *)&xu->xu_ip;
685 pp = (char *)&xu->xu_port;
688 * Expected format: a.b.c.d.e.f where [a-d] correspond to bytes of
689 * an IP address and [ef] are the bytes of a L4 port.
691 if (!(ISDIGIT(uastr[0]) && ISDIGIT(uastr[l-1])))
694 for (c = &uastr[1], d = 0, dd = 0; c < &uastr[l-1]; c++) {
703 /* Check for ASCII byte. */
705 t = ipf_p_rpcb_atoi(b);
709 /* Aim b at beginning of the next byte. */
712 /* Switch off IP addr vs port parsing. */
716 pp[d++ - 4] = t & 0xff;
723 if (d != 5) /* String must contain exactly 5 periods. */
726 /* Handle the last byte (port low byte) */
727 t = ipf_p_rpcb_atoi(b);
730 pp[d - 4] = t & 0xff;
735 /* -------------------------------------------------------------------- */
736 /* Function: ipf_p_rpcb_atoi (XXX should be generic for all proxies) */
737 /* Returns: int -- integer representation of supplied string */
738 /* Parameters: ptr(I) - input string */
740 /* Simple version of atoi(3) ripped from ip_rcmd_pxy.c. */
741 /* -------------------------------------------------------------------- */
746 register char *s = ptr, c;
747 register u_int i = 0;
749 while (((c = *s++) != '\0') && ISDIGIT(c)) {
756 /* -------------------------------------------------------------------- */
757 /* Function: ipf_p_rpcb_modreq */
758 /* Returns: int -- change in datagram length */
759 /* APR_ERR(2) - critical failure */
760 /* Parameters: fin(I) - pointer to packet information */
761 /* nat(I) - pointer to NAT session */
762 /* rm(I) - pointer to RPC message structure */
763 /* m(I) - pointer to mbuf chain */
764 /* off(I) - current offset within mbuf chain */
766 /* When external and internal addresses differ, we rewrite the former */
767 /* with the latter. (This is exclusive to protocol versions 3 & 4). */
768 /* -------------------------------------------------------------------- */
770 ipf_p_rpcb_modreq(fin, nat, rm, m, off)
777 u_int len, xlen, pos, bogo;
784 ra = &rm->rm_call.rc_rpcbargs;
785 i = (char *)&nat->nat_odstaddr;
786 p = (char *)&nat->nat_odport;
788 /* Form new string. */
789 bzero(uaddr, sizeof(uaddr)); /* Just in case we need padding. */
790 #if defined(SNPRINTF) && defined(_KERNEL)
791 SNPRINTF(uaddr, sizeof(uaddr),
793 (void) sprintf(uaddr,
795 "%u.%u.%u.%u.%u.%u", i[0] & 0xff, i[1] & 0xff,
796 i[2] & 0xff, i[3] & 0xff, p[0] & 0xff, p[1] & 0xff);
798 xlen = XDRALIGN(len);
800 /* Determine mbuf offset to start writing to. */
801 pos = (char *)ra->ra_maddr.xu_xslen - rm->rm_msgbuf;
804 /* Write new string length. */
806 COPYBACK(m, off, 4, (caddr_t)&bogo);
809 /* Write new string. */
810 COPYBACK(m, off, xlen, uaddr);
813 /* Write in zero r_owner. */
815 COPYBACK(m, off, 4, (caddr_t)&bogo);
817 /* Determine difference in data lengths. */
818 diff = xlen - XDRALIGN(B(ra->ra_maddr.xu_xslen));
821 * If our new string has a different length, make necessary
826 udp->uh_ulen = htons(ntohs(udp->uh_ulen) + diff);
827 fin->fin_plen += diff;
828 fin->fin_ip->ip_len = htons(fin->fin_plen);
829 fin->fin_dlen += diff;
830 /* XXX Storage lengths. */
836 /* -------------------------------------------------------------------- */
837 /* Function: ipf_p_rpcb_decoderep */
838 /* Returns: int - -1 == bad request or critical failure, */
839 /* 0 == valid, negative reply */
840 /* 1 == vaddlid, positive reply; needs no changes */
841 /* Parameters: fin(I) - pointer to packet information */
842 /* nat(I) - pointer to NAT session structure */
843 /* rs(I) - pointer to RPCB session structure */
844 /* rm(I) - pointer to RPC message structure */
845 /* rxp(O) - pointer to RPCB transaction structure */
847 /* Take a presumed RPCB reply, extract the XID, search for the original */
848 /* request information, and determine whether the request was accepted */
849 /* or rejected. With a valid accepted reply, go ahead and create NAT */
850 /* and state entries, and finish up by rewriting the packet as */
853 /* WARNING: It's the responsibility of the caller to make sure there */
854 /* is enough room in rs_buf for the basic RPC message "preamble". */
855 /* -------------------------------------------------------------------- */
857 ipf_p_rpcb_decoderep(fin, nat, rs, rm, rxp)
871 p = (u_32_t *)rm->rm_msgbuf;
873 bzero((char *)&rx, sizeof(rx));
877 xdr = B(p++); /* Record this message's XID. */
880 MUTEX_ENTER(&rs->rs_rxlock);
881 if ((rx = ipf_p_rpcb_lookup(rs, xdr)) == NULL) {
882 MUTEX_EXIT(&rs->rs_rxlock);
885 ++rx->rx_ref; /* per thread reference */
886 MUTEX_EXIT(&rs->rs_rxlock);
890 /* Test call vs reply */
891 if (B(p++) != RPCB_REPLY)
894 /* Test reply_stat */
897 case RPCB_MSG_DENIED:
899 case RPCB_MSG_ACCEPTED:
905 /* Bypass RPC authentication stuff. */
906 if (ipf_p_rpcb_skipauth(rm, &rr->rr_authverf, &p) != 0)
909 /* Test accept status */
910 if (!RPCB_BUF_GEQ(rm, p, 4))
915 /* Parse out the expected reply */
919 /* There must be only one 4 byte argument. */
920 if (!RPCB_BUF_EQ(rm, p, 4))
926 /* Reply w/ a 0 port indicates service isn't registered */
930 /* Is the value sane? */
934 /* Create NAT & state table entries. */
935 if (ipf_p_rpcb_getnat(fin, nat, rx->rx_proto, (u_int)xdr) != 0)
938 case RPCB_RES_STRING:
939 /* Expecting a XDR string; need 4 bytes for length */
940 if (!RPCB_BUF_GEQ(rm, p, 4))
943 rr->rr_v3.xu_str.xs_len = p++;
944 rr->rr_v3.xu_str.xs_str = (char *)p;
946 xdr = B(rr->rr_v3.xu_xslen);
948 /* A null string indicates an unregistered service */
949 if ((xdr == 0) && RPCB_BUF_EQ(rm, p, 0))
952 /* Decode the target IP address / port. */
953 if (ipf_p_rpcb_getuaddr(rm, &rr->rr_v3, &p) != 0)
956 /* Validate the IP address and port contained. */
957 if (nat->nat_odstaddr != rr->rr_v3.xu_ip)
960 /* Create NAT & state table entries. */
961 if (ipf_p_rpcb_getnat(fin, nat, rx->rx_proto,
962 (u_int)rr->rr_v3.xu_port) != 0)
966 if (!RPCB_BUF_GEQ(rm, p, 4))
968 /* rpcb_entry_list_ptr */
985 re = &rl->rl_entries[rl->rl_cnt];
986 if (ipf_p_rpcb_getuaddr(rm, &re->re_maddr, &p) != 0)
988 if (ipf_p_rpcb_getproto(rm, &re->re_netid, &p) != 0)
990 /* re_semantics & re_pfamily length */
991 if (!RPCB_BUF_GEQ(rm, p, 12))
993 p++; /* Skipping re_semantics. */
995 if ((xdr != 4) || strncmp((char *)p, "inet", 4))
998 if (ipf_p_rpcb_getproto(rm, &re->re_proto, &p) != 0)
1000 if (!RPCB_BUF_GEQ(rm, p, 4))
1003 if (B(re->re_more) > 1) /* 0,1 only legal values */
1007 if (B(re->re_more) == 0)
1009 /* Replies in max out at 2; TCP and/or UDP */
1015 for(rl->rl_cnt = 0; rl->rl_cnt < cnt; rl->rl_cnt++) {
1016 re = &rl->rl_entries[rl->rl_cnt];
1017 rv = ipf_p_rpcb_getnat(fin, nat,
1018 re->re_proto.xp_proto,
1019 (u_int)re->re_maddr.xu_port);
1025 /*CONSTANTCONDITION*/
1026 IPF_PANIC(1, ("illegal rx_type %d", rx->rx_type));
1032 /* -------------------------------------------------------------------- */
1033 /* Function: ipf_p_rpcb_lookup */
1034 /* Returns: rpcb_xact_t * - NULL == no matching record, */
1035 /* else pointer to relevant entry */
1036 /* Parameters: rs(I) - pointer to RPCB session */
1037 /* xid(I) - XID to look for */
1038 /* -------------------------------------------------------------------- */
1039 static rpcb_xact_t *
1040 ipf_p_rpcb_lookup(rs, xid)
1046 if (rs->rs_rxlist == NULL)
1049 for (rx = rs->rs_rxlist; rx != NULL; rx = rx->rx_next)
1050 if (rx->rx_xid == xid)
1056 /* -------------------------------------------------------------------- */
1057 /* Function: ipf_p_rpcb_deref */
1058 /* Returns: (void) */
1059 /* Parameters: rs(I) - pointer to RPCB session */
1060 /* rx(I) - pointer to RPC transaction struct to remove */
1061 /* force(I) - indicates to delete entry regardless of */
1062 /* reference count */
1063 /* Locking: rs->rs_rxlock must be held write only */
1065 /* Free the RPCB transaction record rx from the chain of entries. */
1066 /* -------------------------------------------------------------------- */
1068 ipf_p_rpcb_deref(rs, rx)
1077 if (--rx->rx_ref != 0)
1080 if (rx->rx_next != NULL)
1081 rx->rx_next->rx_pnext = rx->rx_pnext;
1083 *rx->rx_pnext = rx->rx_next;
1090 /* -------------------------------------------------------------------- */
1091 /* Function: ipf_p_rpcb_getproto */
1092 /* Returns: int - -1 == illegal protocol/netid, */
1093 /* 0 == legal protocol/netid */
1094 /* Parameters: rm(I) - pointer to RPC message structure */
1095 /* xp(I) - pointer to netid structure */
1096 /* p(IO) - pointer to location within packet buffer */
1098 /* Decode netid/proto stored at p and record its numeric value. */
1099 /* -------------------------------------------------------------------- */
1101 ipf_p_rpcb_getproto(rm, xp, p)
1108 /* Must have 4 bytes for length & 4 bytes for "tcp" or "udp". */
1109 if (!RPCB_BUF_GEQ(rm, p, 8))
1112 xp->xp_xslen = (*p)++;
1113 xp->xp_xsstr = (char *)*p;
1115 /* Test the string length. */
1116 len = B(xp->xp_xslen);
1120 /* Test the actual string & record the protocol accordingly. */
1121 if (!strncmp((char *)xp->xp_xsstr, "tcp\0", 4))
1122 xp->xp_proto = IPPROTO_TCP;
1123 else if (!strncmp((char *)xp->xp_xsstr, "udp\0", 4))
1124 xp->xp_proto = IPPROTO_UDP;
1129 /* Advance past the string. */
1135 /* -------------------------------------------------------------------- */
1136 /* Function: ipf_p_rpcb_getnat */
1137 /* Returns: int -- -1 == failed to create table entries, */
1139 /* Parameters: fin(I) - pointer to packet information */
1140 /* nat(I) - pointer to NAT table entry */
1141 /* proto(I) - transport protocol for new entries */
1142 /* port(I) - new port to use w/ wildcard table entries */
1144 /* Create state and NAT entries to handle an anticipated connection */
1145 /* attempt between RPC client and server. */
1146 /* -------------------------------------------------------------------- */
1148 ipf_p_rpcb_getnat(fin, nat, proto, port)
1154 ipf_main_softc_t *softc = fin->fin_main_soft;
1155 ipnat_t *ipn, ipnat;
1164 /* Generate dummy fr_info */
1165 bcopy((char *)fin, (char *)&fi, sizeof(fi));
1169 fi.fin_dport = port & 0xffff;
1170 fi.fin_flx |= FI_IGNORE;
1171 fi.fin_saddr = nat->nat_osrcaddr;
1172 fi.fin_daddr = nat->nat_odstaddr;
1174 bzero((char *)&tcp, sizeof(tcp));
1175 tcp.th_dport = htons(port);
1177 if (proto == IPPROTO_TCP) {
1178 tcp.th_win = htons(8192);
1179 TCP_OFF_A(&tcp, sizeof(tcphdr_t) >> 2);
1180 fi.fin_dlen = sizeof(tcphdr_t);
1181 tcp.th_flags = TH_SYN;
1184 fi.fin_dlen = sizeof(udphdr_t);
1188 nflags |= SI_W_SPORT|NAT_SEARCH;
1190 fi.fin_plen = fi.fin_hlen + fi.fin_dlen;
1193 * Search for existing NAT & state entries. Pay close attention to
1194 * mutexes / locks grabbed from lookup routines, as not doing so could
1195 * lead to bad things.
1197 * If successful, fr_stlookup returns with ipf_state locked. We have
1198 * no use for this lock, so simply unlock it if necessary.
1200 is = ipf_state_lookup(&fi, &tcp, NULL);
1202 RWLOCK_EXIT(&softc->ipf_state);
1205 RWLOCK_EXIT(&softc->ipf_nat);
1207 WRITE_ENTER(&softc->ipf_nat);
1208 natl = ipf_nat_inlookup(&fi, nflags, proto, fi.fin_src, fi.fin_dst);
1210 if ((natl != NULL) && (is != NULL)) {
1211 MUTEX_DOWNGRADE(&softc->ipf_nat);
1215 /* Slightly modify the following structures for actual use in creating
1216 * NAT and/or state entries. We're primarily concerned with stripping
1217 * flags that may be detrimental to the creation process or simply
1218 * shouldn't be associated with a table entry.
1220 fi.fin_fr = &rpcbfr;
1221 fi.fin_flx &= ~FI_IGNORE;
1222 nflags &= ~NAT_SEARCH;
1226 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1229 /* XXX Since we're just copying the original ipn contents
1230 * back, would we be better off just sending a pointer to
1231 * the 'temp' copy off to nat_new instead?
1233 /* Generate template/bogus NAT rule. */
1234 bcopy((char *)ipn, (char *)&ipnat, sizeof(ipnat));
1235 ipn->in_flags = nflags & IPN_TCPUDP;
1237 ipn->in_pr[0] = proto;
1238 ipn->in_pr[1] = proto;
1239 ipn->in_dpmin = fi.fin_dport;
1240 ipn->in_dpmax = fi.fin_dport;
1241 ipn->in_dpnext = fi.fin_dport;
1244 if (ipn->in_flags & IPN_FILTER) {
1248 ipn->in_plabel = -1;
1250 /* Create NAT entry. return NULL if this fails. */
1251 MUTEX_ENTER(&softn->ipf_nat_new);
1252 natl = ipf_nat_add(&fi, ipn, NULL, nflags|SI_CLONE|NAT_SLAVE,
1254 MUTEX_EXIT(&softn->ipf_nat_new);
1256 bcopy((char *)&ipnat, (char *)ipn, sizeof(ipnat));
1259 MUTEX_DOWNGRADE(&softc->ipf_nat);
1263 natl->nat_ptr = ipn;
1264 fi.fin_saddr = natl->nat_nsrcaddr;
1265 fi.fin_daddr = natl->nat_ndstaddr;
1267 (void) ipf_nat_proto(&fi, natl, nflags);
1268 MUTEX_ENTER(&natl->nat_lock);
1269 ipf_nat_update(&fi, natl);
1270 MUTEX_EXIT(&natl->nat_lock);
1272 MUTEX_DOWNGRADE(&softc->ipf_nat);
1275 /* Create state entry. Return NULL if this fails. */
1276 fi.fin_flx |= FI_NATED;
1277 fi.fin_flx &= ~FI_STATE;
1278 nflags &= NAT_TCPUDP;
1279 nflags |= SI_W_SPORT|SI_CLONE;
1281 if (ipf_state_add(softc, &fi, NULL, nflags) != 0) {
1283 * XXX nat_delete is private to ip_nat.c. Should
1284 * check w/ Darren about this one.
1286 * nat_delete(natl, NL_EXPIRE);
1295 /* -------------------------------------------------------------------- */
1296 /* Function: ipf_p_rpcb_modv3 */
1297 /* Returns: int -- change in packet length */
1298 /* Parameters: fin(I) - pointer to packet information */
1299 /* nat(I) - pointer to NAT session */
1300 /* rm(I) - pointer to RPC message structure */
1301 /* m(I) - pointer to mbuf chain */
1302 /* off(I) - offset within mbuf chain */
1304 /* Write a new universal address string to this packet, adjusting */
1305 /* lengths as necessary. */
1306 /* -------------------------------------------------------------------- */
1308 ipf_p_rpcb_modv3(fin, nat, rm, m, off)
1315 u_int len, xlen, pos, bogo;
1322 i = (char *)&nat->nat_ndstaddr;
1323 p = (char *)&rr->rr_v3.xu_port;
1325 /* Form new string. */
1326 bzero(uaddr, sizeof(uaddr)); /* Just in case we need padding. */
1327 #if defined(SNPRINTF) && defined(_KERNEL)
1328 SNPRINTF(uaddr, sizeof(uaddr),
1330 (void) sprintf(uaddr,
1332 "%u.%u.%u.%u.%u.%u", i[0] & 0xff, i[1] & 0xff,
1333 i[2] & 0xff, i[3] & 0xff, p[0] & 0xff, p[1] & 0xff);
1334 len = strlen(uaddr);
1335 xlen = XDRALIGN(len);
1337 /* Determine mbuf offset to write to. */
1338 pos = (char *)rr->rr_v3.xu_xslen - rm->rm_msgbuf;
1341 /* Write new string length. */
1343 COPYBACK(m, off, 4, (caddr_t)&bogo);
1346 /* Write new string. */
1347 COPYBACK(m, off, xlen, uaddr);
1349 /* Determine difference in data lengths. */
1350 diff = xlen - XDRALIGN(B(rr->rr_v3.xu_xslen));
1353 * If our new string has a different length, make necessary
1357 ipf_p_rpcb_fixlen(fin, diff);
1362 /* -------------------------------------------------------------------- */
1363 /* Function: ipf_p_rpcb_modv4 */
1364 /* Returns: int -- change in packet length */
1365 /* Parameters: fin(I) - pointer to packet information */
1366 /* nat(I) - pointer to NAT session */
1367 /* rm(I) - pointer to RPC message structure */
1368 /* m(I) - pointer to mbuf chain */
1369 /* off(I) - offset within mbuf chain */
1371 /* Write new rpcb_entry list, adjusting lengths as necessary. */
1372 /* -------------------------------------------------------------------- */
1374 ipf_p_rpcb_modv4(fin, nat, rm, m, off)
1381 u_int len, xlen, pos, bogo;
1393 i = (char *)&nat->nat_ndstaddr;
1395 /* Determine mbuf offset to write to. */
1396 re = &rl->rl_entries[0];
1397 pos = (char *)re->re_maddr.xu_xslen - rm->rm_msgbuf;
1400 for (cnt = 0; cnt < rl->rl_cnt; cnt++) {
1401 re = &rl->rl_entries[cnt];
1402 p = (char *)&re->re_maddr.xu_port;
1404 /* Form new string. */
1405 bzero(uaddr, sizeof(uaddr)); /* Just in case we need
1407 #if defined(SNPRINTF) && defined(_KERNEL)
1408 SNPRINTF(uaddr, sizeof(uaddr),
1410 (void) sprintf(uaddr,
1412 "%u.%u.%u.%u.%u.%u", i[0] & 0xff,
1413 i[1] & 0xff, i[2] & 0xff, i[3] & 0xff,
1414 p[0] & 0xff, p[1] & 0xff);
1415 len = strlen(uaddr);
1416 xlen = XDRALIGN(len);
1418 /* Write new string length. */
1420 COPYBACK(m, off, 4, (caddr_t)&bogo);
1423 /* Write new string. */
1424 COPYBACK(m, off, xlen, uaddr);
1427 /* Record any change in length. */
1428 diff += xlen - XDRALIGN(B(re->re_maddr.xu_xslen));
1430 /* If the length changed, copy back the rest of this entry. */
1431 len = ((char *)re->re_more + 4) -
1432 (char *)re->re_netid.xp_xslen;
1434 COPYBACK(m, off, len, (caddr_t)re->re_netid.xp_xslen);
1440 * If our new string has a different length, make necessary
1444 ipf_p_rpcb_fixlen(fin, diff);
1450 /* -------------------------------------------------------------------- */
1451 /* Function: ipf_p_rpcb_fixlen */
1452 /* Returns: (void) */
1453 /* Parameters: fin(I) - pointer to packet information */
1454 /* len(I) - change in packet length */
1456 /* Adjust various packet related lengths held in structure and packet */
1457 /* header fields. */
1458 /* -------------------------------------------------------------------- */
1460 ipf_p_rpcb_fixlen(fin, len)
1467 udp->uh_ulen = htons(ntohs(udp->uh_ulen) + len);
1468 fin->fin_plen += len;
1469 fin->fin_ip->ip_len = htons(fin->fin_plen);
1470 fin->fin_dlen += len;