1/* $FreeBSD$ */ 2 3/* 4 * Copyright (C) 1998-2003 by Darren Reed 5 * 6 * See the IPFILTER.LICENCE file for details on licencing. 7 * 8 * $Id: ip_rcmd_pxy.c,v 1.41.2.7 2006/07/14 06:12:18 darrenr Exp $ 9 * 10 * Simple RCMD transparent proxy for in-kernel use. For use with the NAT 11 * code. 12 * $FreeBSD$ 13 */ 14 15#define IPF_RCMD_PROXY 16 17 18int ippr_rcmd_init __P((void)); 19void ippr_rcmd_fini __P((void)); 20int ippr_rcmd_new __P((fr_info_t *, ap_session_t *, nat_t *)); 21int ippr_rcmd_out __P((fr_info_t *, ap_session_t *, nat_t *)); 22int ippr_rcmd_in __P((fr_info_t *, ap_session_t *, nat_t *)); 23u_short ipf_rcmd_atoi __P((char *)); 24int ippr_rcmd_portmsg __P((fr_info_t *, ap_session_t *, nat_t *)); 25 26static frentry_t rcmdfr; 27 28int rcmd_proxy_init = 0; 29 30 31/* 32 * RCMD application proxy initialization. 33 */ 34int ippr_rcmd_init() 35{ 36 bzero((char *)&rcmdfr, sizeof(rcmdfr)); 37 rcmdfr.fr_ref = 1; 38 rcmdfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE; 39 MUTEX_INIT(&rcmdfr.fr_lock, "RCMD proxy rule lock"); 40 rcmd_proxy_init = 1; 41 42 return 0; 43} 44 45 46void ippr_rcmd_fini() 47{ 48 if (rcmd_proxy_init == 1) { 49 MUTEX_DESTROY(&rcmdfr.fr_lock); 50 rcmd_proxy_init = 0; 51 } 52} 53 54 55/* 56 * Setup for a new RCMD proxy. 57 */ 58int ippr_rcmd_new(fin, aps, nat) 59fr_info_t *fin; 60ap_session_t *aps; 61nat_t *nat; 62{ 63 tcphdr_t *tcp = (tcphdr_t *)fin->fin_dp; 64 65 fin = fin; /* LINT */ 66 nat = nat; /* LINT */ 67 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)); 73#endif 74 return -1; 75 } 76 *(u_32_t *)aps->aps_data = 0; 77 aps->aps_sport = tcp->th_sport; 78 aps->aps_dport = tcp->th_dport; 79 return 0; 80} 81 82 83/* 84 * ipf_rcmd_atoi - implement a simple version of atoi 85 */ 86u_short ipf_rcmd_atoi(ptr) 87char *ptr; 88{ 89 register char *s = ptr, c; 90 register u_short i = 0; 91 92 while (((c = *s++) != '\0') && ISDIGIT(c)) { 93 i *= 10; 94 i += c - '0'; 95 } 96 return i; 97} 98 99 100int ippr_rcmd_portmsg(fin, aps, nat) 101fr_info_t *fin; 102ap_session_t *aps; 103nat_t *nat; 104{ 105 tcphdr_t *tcp, tcph, *tcp2 = &tcph; 106 struct in_addr swip, swip2; 107 int off, dlen, nflags; 108 char portbuf[8], *s; 109 fr_info_t fi; 110 u_short sp; 111 nat_t *nat2; 112 ip_t *ip; 113 mb_t *m; 114 115 tcp = (tcphdr_t *)fin->fin_dp; 116 117 if (tcp->th_flags & TH_SYN) { 118 *(u_32_t *)aps->aps_data = htonl(ntohl(tcp->th_seq) + 1); 119 return 0; 120 } 121 122 if ((*(u_32_t *)aps->aps_data != 0) && 123 (tcp->th_seq != *(u_32_t *)aps->aps_data)) 124 return 0; 125 126 m = fin->fin_m; 127 ip = fin->fin_ip; 128 off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff; 129 130#ifdef __sgi 131 dlen = fin->fin_plen - off; 132#else 133 dlen = MSGDSIZE(m) - off; 134#endif 135 if (dlen <= 0) 136 return 0; 137 138 bzero(portbuf, sizeof(portbuf)); 139 COPYDATA(m, off, MIN(sizeof(portbuf), dlen), portbuf); 140 141 portbuf[sizeof(portbuf) - 1] = '\0'; 142 s = portbuf; 143 sp = ipf_rcmd_atoi(s); 144 if (sp == 0) { 145#ifdef IP_RCMD_PROXY_DEBUG 146 printf("ippr_rcmd_portmsg:sp == 0 dlen %d [%s]\n", 147 dlen, portbuf); 148#endif 149 return 0; 150 } 151 152 /* 153 * Add skeleton NAT entry for connection which will come back the 154 * other way. 155 */ 156 bcopy((char *)fin, (char *)&fi, sizeof(fi)); 157 fi.fin_state = NULL; 158 fi.fin_nat = NULL; 159 fi.fin_flx |= FI_IGNORE; 160 fi.fin_data[0] = sp; 161 fi.fin_data[1] = 0; 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); 165 else 166 nat2 = nat_inlookup(&fi, NAT_SEARCH|IPN_TCP, nat->nat_p, 167 nat->nat_inip, nat->nat_oip); 168 if (nat2 == NULL) { 169 int slen; 170 171 slen = ip->ip_len; 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 */ 177 TCP_OFF_A(tcp2, 5); 178 tcp2->th_flags = TH_SYN; 179 fi.fin_dp = (char *)tcp2; 180 fi.fin_fr = &rcmdfr; 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; 185 186 swip = ip->ip_src; 187 swip2 = ip->ip_dst; 188 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; 192 } else { 193 fi.fin_fi.fi_saddr = nat->nat_oip.s_addr; 194 ip->ip_src = nat->nat_oip; 195 nflags |= NAT_NOTRULEPORT; 196 } 197 198 nat2 = nat_new(&fi, nat->nat_ptr, NULL, nflags, nat->nat_dir); 199 200 if (nat2 != NULL) { 201 (void) nat_proto(&fi, nat2, IPN_TCP); 202 nat_update(&fi, nat2, nat2->nat_ptr); 203 fi.fin_ifp = NULL; 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; 207 } 208 (void) fr_addstate(&fi, NULL, SI_W_DPORT); 209 if (fi.fin_state != NULL) 210 fr_statederef((ipstate_t **)&fi.fin_state); 211 } 212 ip->ip_len = slen; 213 ip->ip_src = swip; 214 ip->ip_dst = swip2; 215 } 216 return 0; 217} 218 219 220int ippr_rcmd_out(fin, aps, nat) 221fr_info_t *fin; 222ap_session_t *aps; 223nat_t *nat; 224{ 225 if (nat->nat_dir == NAT_OUTBOUND) 226 return ippr_rcmd_portmsg(fin, aps, nat); 227 return 0; 228} 229 230 231int ippr_rcmd_in(fin, aps, nat) 232fr_info_t *fin; 233ap_session_t *aps; 234nat_t *nat; 235{ 236 if (nat->nat_dir == NAT_INBOUND) 237 return ippr_rcmd_portmsg(fin, aps, nat); 238 return 0; 239} 240