4 * Copyright (C) 1998-2003 by Darren Reed
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * $Id: ip_rcmd_pxy.c,v 1.41.2.7 2006/07/14 06:12:18 darrenr Exp $
10 * Simple RCMD transparent proxy for in-kernel use. For use with the NAT
15 #define IPF_RCMD_PROXY
18 int ippr_rcmd_init __P((void));
19 void ippr_rcmd_fini __P((void));
20 int ippr_rcmd_new __P((fr_info_t *, ap_session_t *, nat_t *));
21 int ippr_rcmd_out __P((fr_info_t *, ap_session_t *, nat_t *));
22 int ippr_rcmd_in __P((fr_info_t *, ap_session_t *, nat_t *));
23 u_short ipf_rcmd_atoi __P((char *));
24 int ippr_rcmd_portmsg __P((fr_info_t *, ap_session_t *, nat_t *));
26 static frentry_t rcmdfr;
28 int rcmd_proxy_init = 0;
32 * RCMD application proxy initialization.
36 bzero((char *)&rcmdfr, sizeof(rcmdfr));
38 rcmdfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
39 MUTEX_INIT(&rcmdfr.fr_lock, "RCMD proxy rule lock");
48 if (rcmd_proxy_init == 1) {
49 MUTEX_DESTROY(&rcmdfr.fr_lock);
56 * Setup for a new RCMD proxy.
58 int ippr_rcmd_new(fin, aps, nat)
63 tcphdr_t *tcp = (tcphdr_t *)fin->fin_dp;
68 aps->aps_psiz = sizeof(u_32_t);
69 KMALLOCS(aps->aps_data, u_32_t *, sizeof(u_32_t));
70 if (aps->aps_data == NULL) {
71 #ifdef IP_RCMD_PROXY_DEBUG
72 printf("ippr_rcmd_new:KMALLOCS(%d) failed\n", sizeof(u_32_t));
76 *(u_32_t *)aps->aps_data = 0;
77 aps->aps_sport = tcp->th_sport;
78 aps->aps_dport = tcp->th_dport;
84 * ipf_rcmd_atoi - implement a simple version of atoi
86 u_short ipf_rcmd_atoi(ptr)
89 register char *s = ptr, c;
90 register u_short i = 0;
92 while (((c = *s++) != '\0') && ISDIGIT(c)) {
100 int ippr_rcmd_portmsg(fin, aps, nat)
105 tcphdr_t *tcp, tcph, *tcp2 = &tcph;
106 struct in_addr swip, swip2;
107 int off, dlen, nflags;
115 tcp = (tcphdr_t *)fin->fin_dp;
117 if (tcp->th_flags & TH_SYN) {
118 *(u_32_t *)aps->aps_data = htonl(ntohl(tcp->th_seq) + 1);
122 if ((*(u_32_t *)aps->aps_data != 0) &&
123 (tcp->th_seq != *(u_32_t *)aps->aps_data))
128 off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff;
131 dlen = fin->fin_plen - off;
133 dlen = MSGDSIZE(m) - off;
138 bzero(portbuf, sizeof(portbuf));
139 COPYDATA(m, off, MIN(sizeof(portbuf), dlen), portbuf);
141 portbuf[sizeof(portbuf) - 1] = '\0';
143 sp = ipf_rcmd_atoi(s);
145 #ifdef IP_RCMD_PROXY_DEBUG
146 printf("ippr_rcmd_portmsg:sp == 0 dlen %d [%s]\n",
153 * Add skeleton NAT entry for connection which will come back the
156 bcopy((char *)fin, (char *)&fi, sizeof(fi));
159 fi.fin_flx |= FI_IGNORE;
162 if (nat->nat_dir == NAT_OUTBOUND)
163 nat2 = nat_outlookup(&fi, NAT_SEARCH|IPN_TCP, nat->nat_p,
164 nat->nat_inip, nat->nat_oip);
166 nat2 = nat_inlookup(&fi, NAT_SEARCH|IPN_TCP, nat->nat_p,
167 nat->nat_inip, nat->nat_oip);
172 ip->ip_len = fin->fin_hlen + sizeof(*tcp);
173 bzero((char *)tcp2, sizeof(*tcp2));
174 tcp2->th_win = htons(8192);
175 tcp2->th_sport = htons(sp);
176 tcp2->th_dport = 0; /* XXX - don't specify remote port */
178 tcp2->th_flags = TH_SYN;
179 fi.fin_dp = (char *)tcp2;
181 fi.fin_dlen = sizeof(*tcp2);
182 fi.fin_plen = fi.fin_hlen + sizeof(*tcp2);
183 fi.fin_flx &= FI_LOWTTL|FI_FRAG|FI_TCPUDP|FI_OPTIONS|FI_IGNORE;
184 nflags = NAT_SLAVE|IPN_TCP|SI_W_DPORT;
189 if (nat->nat_dir == NAT_OUTBOUND) {
190 fi.fin_fi.fi_saddr = nat->nat_inip.s_addr;
191 ip->ip_src = nat->nat_inip;
193 fi.fin_fi.fi_saddr = nat->nat_oip.s_addr;
194 ip->ip_src = nat->nat_oip;
195 nflags |= NAT_NOTRULEPORT;
198 nat2 = nat_new(&fi, nat->nat_ptr, NULL, nflags, nat->nat_dir);
201 (void) nat_proto(&fi, nat2, IPN_TCP);
202 nat_update(&fi, nat2, nat2->nat_ptr);
204 if (nat->nat_dir == NAT_INBOUND) {
205 fi.fin_fi.fi_daddr = nat->nat_inip.s_addr;
206 ip->ip_dst = nat->nat_inip;
208 (void) fr_addstate(&fi, NULL, SI_W_DPORT);
209 if (fi.fin_state != NULL)
210 fr_statederef((ipstate_t **)&fi.fin_state);
220 int ippr_rcmd_out(fin, aps, nat)
225 if (nat->nat_dir == NAT_OUTBOUND)
226 return ippr_rcmd_portmsg(fin, aps, nat);
231 int ippr_rcmd_in(fin, aps, nat)
236 if (nat->nat_dir == NAT_INBOUND)
237 return ippr_rcmd_portmsg(fin, aps, nat);