Deleted Added
sdiff udiff text old ( 130886 ) new ( 145522 )
full compact
1/*
2 * $Id: ip_rcmd_pxy.c,v 1.4.2.7 2003/04/26 05:59:39 darrenr Exp $
3 */
4/*
5 * Simple RCMD transparent proxy for in-kernel use. For use with the NAT
6 * code.
7 * $FreeBSD: head/sys/contrib/ipfilter/netinet/ip_rcmd_pxy.c 130886 2004-06-21 22:46:36Z darrenr $
8 */
9#if SOLARIS && defined(_KERNEL)
10extern kmutex_t ipf_rw;
11#endif
12
13#define isdigit(x) ((x) >= '0' && (x) <= '9')
14
15#define IPF_RCMD_PROXY
16
17
18int ippr_rcmd_init __P((void));
19int ippr_rcmd_new __P((fr_info_t *, ip_t *, ap_session_t *, nat_t *));
20int ippr_rcmd_out __P((fr_info_t *, ip_t *, ap_session_t *, nat_t *));
21u_short ipf_rcmd_atoi __P((char *));
22int ippr_rcmd_portmsg __P((fr_info_t *, ip_t *, ap_session_t *, nat_t *));
23
24static frentry_t rcmdfr;
25
26
27/*
28 * RCMD application proxy initialization.
29 */
30int ippr_rcmd_init()
31{
32 bzero((char *)&rcmdfr, sizeof(rcmdfr));
33 rcmdfr.fr_ref = 1;
34 rcmdfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
35 return 0;
36}
37
38
39/*
40 * Setup for a new RCMD proxy.
41 */
42int ippr_rcmd_new(fin, ip, aps, nat)
43fr_info_t *fin;
44ip_t *ip;
45ap_session_t *aps;
46nat_t *nat;
47{
48 tcphdr_t *tcp = (tcphdr_t *)fin->fin_dp;
49
50 aps->aps_psiz = sizeof(u_32_t);
51 KMALLOCS(aps->aps_data, u_32_t *, sizeof(u_32_t));
52 if (aps->aps_data == NULL)
53 return -1;
54 *(u_32_t *)aps->aps_data = 0;
55 aps->aps_sport = tcp->th_sport;
56 aps->aps_dport = tcp->th_dport;
57 return 0;
58}
59
60
61/*
62 * ipf_rcmd_atoi - implement a simple version of atoi
63 */
64u_short ipf_rcmd_atoi(ptr)
65char *ptr;
66{
67 register char *s = ptr, c;
68 register u_short i = 0;
69
70 while ((c = *s++) && isdigit(c)) {
71 i *= 10;
72 i += c - '0';
73 }
74 return i;
75}
76
77
78int ippr_rcmd_portmsg(fin, ip, aps, nat)
79fr_info_t *fin;
80ip_t *ip;
81ap_session_t *aps;
82nat_t *nat;
83{
84 char portbuf[8], *s;
85 struct in_addr swip;
86 int off, dlen;
87 tcphdr_t *tcp, tcph, *tcp2 = &tcph;
88 fr_info_t fi;
89 u_short sp;
90 nat_t *ipn;
91 mb_t *m;
92
93 tcp = (tcphdr_t *)fin->fin_dp;
94
95 if (tcp->th_flags & TH_SYN) {
96 *(u_32_t *)aps->aps_data = htonl(ntohl(tcp->th_seq) + 1);
97 return 0;
98 }
99
100 if ((*(u_32_t *)aps->aps_data != 0) &&
101 (tcp->th_seq != *(u_32_t *)aps->aps_data))
102 return 0;
103
104 off = fin->fin_hlen + (tcp->th_off << 2);
105
106#if SOLARIS
107 m = fin->fin_qfm;
108
109 dlen = msgdsize(m) - off;
110 bzero(portbuf, sizeof(portbuf));
111 copyout_mblk(m, off, MIN(sizeof(portbuf), dlen), portbuf);
112#else
113 m = *(mb_t **)fin->fin_mp;
114 dlen = mbufchainlen(m) - off;
115 bzero(portbuf, sizeof(portbuf));
116 m_copydata(m, off, MIN(sizeof(portbuf), dlen), portbuf);
117#endif
118
119 portbuf[sizeof(portbuf) - 1] = '\0';
120 s = portbuf;
121 sp = ipf_rcmd_atoi(s);
122 if (!sp)
123 return 0;
124
125 /*
126 * Add skeleton NAT entry for connection which will come back the
127 * other way.
128 */
129 bcopy((char *)fin, (char *)&fi, sizeof(fi));
130 fi.fin_data[0] = sp;
131 fi.fin_data[1] = fin->fin_data[1];
132 ipn = nat_outlookup(&fi, IPN_TCP, nat->nat_p, nat->nat_inip,
133 ip->ip_dst, 0);
134 if (ipn == NULL) {
135 int slen;
136
137 slen = ip->ip_len;
138 ip->ip_len = fin->fin_hlen + sizeof(*tcp);
139 bzero((char *)tcp2, sizeof(*tcp2));
140 tcp2->th_win = htons(8192);
141 tcp2->th_sport = htons(sp);
142 tcp2->th_dport = 0; /* XXX - don't specify remote port */
143 tcp2->th_off = 5;
144 tcp2->th_flags = TH_SYN;
145 fi.fin_data[1] = 0;
146 fi.fin_dp = (char *)tcp2;
147 fi.fin_dlen = sizeof(*tcp2);
148 swip = ip->ip_src;
149 ip->ip_src = nat->nat_inip;
150 ipn = nat_new(&fi, ip, nat->nat_ptr, NULL, IPN_TCP|FI_W_DPORT,
151 NAT_OUTBOUND);
152 if (ipn != NULL) {
153 ipn->nat_age = fr_defnatage;
154 fi.fin_fr = &rcmdfr;
155 (void) fr_addstate(ip, &fi, NULL,
156 FI_W_DPORT|FI_IGNOREPKT);
157 }
158 ip->ip_len = slen;
159 ip->ip_src = swip;
160 }
161 return 0;
162}
163
164
165int ippr_rcmd_out(fin, ip, aps, nat)
166fr_info_t *fin;
167ip_t *ip;
168ap_session_t *aps;
169nat_t *nat;
170{
171 return ippr_rcmd_portmsg(fin, ip, aps, nat);
172}