blob: 36002280716b9eb30ce415d52b2fa703d382a356 [file] [log] [blame] [raw]
Darren Reedda0443e2006-06-15 16:17:17 +00001/*
2 * Copyright 2001, QNX Software Systems Ltd. All Rights Reserved
3 *
4 * This source code has been published by QNX Software Systems Ltd. (QSSL).
5 * However, any use, reproduction, modification, distribution or transfer of
6 * this software, or any software which includes or is based upon any of this
7 * code, is only permitted under the terms of the QNX Open Community License
8 * version 1.0 (see licensing.qnx.com for details) or as otherwise expressly
9 * authorized by a written license agreement from QSSL. For more information,
10 * please email licensing@qnx.com.
11 *
12 * For more details, see QNX_OCL.txt provided with this distribution.
13 */
14
15/*
16 * Simple H.323 proxy
17 *
18 * by xtang@canada.com
19 * ported to ipfilter 3.4.20 by Michael Grant mg-ipf@grant.org
20 */
21
22#if __FreeBSD_version >= 220000 && defined(_KERNEL)
23# include <sys/fcntl.h>
24# include <sys/filio.h>
25#else
Darren Reedd4718fc2006-06-15 17:00:40 +000026# ifndef linux
27# include <sys/ioctl.h>
28# endif
Darren Reedda0443e2006-06-15 16:17:17 +000029#endif
30
31#define IPF_H323_PROXY
32
33int ippr_h323_init __P((void));
34void ippr_h323_fini __P((void));
35int ippr_h323_new __P((fr_info_t *, ap_session_t *, nat_t *));
36void ippr_h323_del __P((ap_session_t *));
37int ippr_h323_out __P((fr_info_t *, ap_session_t *, nat_t *));
38int ippr_h323_in __P((fr_info_t *, ap_session_t *, nat_t *));
39
40int ippr_h245_new __P((fr_info_t *, ap_session_t *, nat_t *));
41int ippr_h245_out __P((fr_info_t *, ap_session_t *, nat_t *));
42int ippr_h245_in __P((fr_info_t *, ap_session_t *, nat_t *));
43
44static frentry_t h323_fr;
45
46int h323_proxy_init = 0;
47
48static int find_port __P((int, caddr_t, int datlen, int *, u_short *));
49
50
Darren Reedc4af1f32007-08-20 10:15:33 +000051static int
52find_port(ipaddr, data, datlen, off, port)
53 int ipaddr;
54 caddr_t data;
55 int datlen, *off;
56 unsigned short *port;
Darren Reedda0443e2006-06-15 16:17:17 +000057{
58 u_32_t addr, netaddr;
59 u_char *dp;
60 int offset;
61
62 if (datlen < 6)
63 return -1;
Martti Kuparinenb4c6ea22007-10-25 09:26:49 +000064
Darren Reedda0443e2006-06-15 16:17:17 +000065 *port = 0;
66 offset = *off;
67 dp = (u_char *)data;
68 netaddr = ntohl(ipaddr);
69
70 for (offset = 0; offset <= datlen - 6; offset++, dp++) {
71 addr = (dp[0] << 24) | (dp[1] << 16) | (dp[2] << 8) | dp[3];
72 if (netaddr == addr)
73 {
74 *port = (*(dp + 4) << 8) | *(dp + 5);
75 break;
76 }
77 }
78 *off = offset;
79 return (offset > datlen - 6) ? -1 : 0;
80}
81
82/*
83 * Initialize local structures.
84 */
Darren Reedc4af1f32007-08-20 10:15:33 +000085int
86ippr_h323_init()
Darren Reedda0443e2006-06-15 16:17:17 +000087{
88 bzero((char *)&h323_fr, sizeof(h323_fr));
89 h323_fr.fr_ref = 1;
90 h323_fr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
91 MUTEX_INIT(&h323_fr.fr_lock, "H323 proxy rule lock");
92 h323_proxy_init = 1;
93
94 return 0;
95}
96
97
Darren Reedc4af1f32007-08-20 10:15:33 +000098void
99ippr_h323_fini()
Darren Reedda0443e2006-06-15 16:17:17 +0000100{
101 if (h323_proxy_init == 1) {
102 MUTEX_DESTROY(&h323_fr.fr_lock);
103 h323_proxy_init = 0;
104 }
105}
106
107
Darren Reedc4af1f32007-08-20 10:15:33 +0000108int
109ippr_h323_new(fin, aps, nat)
110 fr_info_t *fin;
111 ap_session_t *aps;
112 nat_t *nat;
Darren Reedda0443e2006-06-15 16:17:17 +0000113{
114 fin = fin; /* LINT */
115 nat = nat; /* LINT */
116
117 aps->aps_data = NULL;
118 aps->aps_psiz = 0;
119
120 return 0;
121}
122
123
Darren Reedc4af1f32007-08-20 10:15:33 +0000124void
125ippr_h323_del(aps)
126 ap_session_t *aps;
Darren Reedda0443e2006-06-15 16:17:17 +0000127{
128 int i;
129 ipnat_t *ipn;
Martti Kuparinenb4c6ea22007-10-25 09:26:49 +0000130
Darren Reedda0443e2006-06-15 16:17:17 +0000131 if (aps->aps_data) {
132 for (i = 0, ipn = aps->aps_data;
133 i < (aps->aps_psiz / sizeof(ipnat_t));
134 i++, ipn = (ipnat_t *)((char *)ipn + sizeof(*ipn)))
135 {
136 /*
137 * Check the comment in ippr_h323_in() function,
138 * just above fr_nat_ioctl() call.
139 * We are lucky here because this function is not
140 * called with ipf_nat locked.
141 */
Darren Reedc4af1f32007-08-20 10:15:33 +0000142 if (ipf_nat_ioctl((caddr_t)ipn, SIOCRMNAT, NAT_SYSSPACE|
143 NAT_LOCKHELD|FWRITE, 0, NULL) == -1) {
Darren Reedda0443e2006-06-15 16:17:17 +0000144 /*EMPTY*/;
145 /* log the error */
146 }
147 }
148 KFREES(aps->aps_data, aps->aps_psiz);
149 /* avoid double free */
150 aps->aps_data = NULL;
151 aps->aps_psiz = 0;
152 }
153 return;
154}
155
156
Darren Reedc4af1f32007-08-20 10:15:33 +0000157int
158ippr_h323_in(fin, aps, nat)
159 fr_info_t *fin;
160 ap_session_t *aps;
161 nat_t *nat;
Darren Reedda0443e2006-06-15 16:17:17 +0000162{
163 int ipaddr, off, datlen;
164 unsigned short port;
165 caddr_t data;
166 tcphdr_t *tcp;
167 ip_t *ip;
168
169 ip = fin->fin_ip;
170 tcp = (tcphdr_t *)fin->fin_dp;
171 ipaddr = ip->ip_src.s_addr;
Martti Kuparinenb4c6ea22007-10-25 09:26:49 +0000172
Darren Reedda0443e2006-06-15 16:17:17 +0000173 data = (caddr_t)tcp + (TCP_OFF(tcp) << 2);
174 datlen = fin->fin_dlen - (TCP_OFF(tcp) << 2);
175 if (find_port(ipaddr, data, datlen, &off, &port) == 0) {
176 ipnat_t *ipn;
177 char *newarray;
178
179 /* setup a nat rule to set a h245 proxy on tcp-port "port"
180 * it's like:
181 * map <if> <inter_ip>/<mask> -> <gate_ip>/<mask> proxy port <port> <port>/tcp
182 */
183 KMALLOCS(newarray, char *, aps->aps_psiz + sizeof(*ipn));
184 if (newarray == NULL) {
185 return -1;
186 }
187 ipn = (ipnat_t *)&newarray[aps->aps_psiz];
188 bcopy((caddr_t)nat->nat_ptr, (caddr_t)ipn, sizeof(ipnat_t));
189 (void) strncpy(ipn->in_plabel, "h245", APR_LABELLEN);
Martti Kuparinenb4c6ea22007-10-25 09:26:49 +0000190
Darren Reedc4af1f32007-08-20 10:15:33 +0000191 ipn->in_osrcip = nat->nat_osrcip;
192 ipn->in_osrcmsk = 0xffffffff;
193 ipn->in_odstip = nat->nat_odstip;
194 ipn->in_odstmsk = 0xffffffff;
195 ipn->in_odport = htons(port);
Darren Reedda0443e2006-06-15 16:17:17 +0000196 /*
197 * we got a problem here. we need to call fr_nat_ioctl() to add
198 * the h245 proxy rule, but since we already hold (READ locked)
199 * the nat table rwlock (ipf_nat), if we go into fr_nat_ioctl(),
200 * it will try to WRITE lock it. This will causing dead lock
201 * on RTP.
202 *
203 * The quick & dirty solution here is release the read lock,
204 * call fr_nat_ioctl() and re-lock it.
205 * A (maybe better) solution is do a UPGRADE(), and instead
206 * of calling fr_nat_ioctl(), we add the nat rule ourself.
207 */
208 RWLOCK_EXIT(&ipf_nat);
Darren Reedc4af1f32007-08-20 10:15:33 +0000209 if (ipf_nat_ioctl((caddr_t)ipn, SIOCADNAT,
210 NAT_SYSSPACE|FWRITE, 0, NULL) == -1) {
Darren Reedda0443e2006-06-15 16:17:17 +0000211 READ_ENTER(&ipf_nat);
212 return -1;
213 }
214 READ_ENTER(&ipf_nat);
215 if (aps->aps_data != NULL && aps->aps_psiz > 0) {
216 bcopy(aps->aps_data, newarray, aps->aps_psiz);
217 KFREES(aps->aps_data, aps->aps_psiz);
218 }
219 aps->aps_data = newarray;
220 aps->aps_psiz += sizeof(*ipn);
221 }
222 return 0;
223}
224
225
Darren Reedc4af1f32007-08-20 10:15:33 +0000226int
227ippr_h245_new(fin, aps, nat)
228 fr_info_t *fin;
229 ap_session_t *aps;
230 nat_t *nat;
Darren Reedda0443e2006-06-15 16:17:17 +0000231{
232 fin = fin; /* LINT */
233 nat = nat; /* LINT */
234
235 aps->aps_data = NULL;
236 aps->aps_psiz = 0;
237 return 0;
238}
239
240
Darren Reedc4af1f32007-08-20 10:15:33 +0000241int
242ippr_h245_out(fin, aps, nat)
243 fr_info_t *fin;
244 ap_session_t *aps;
245 nat_t *nat;
Darren Reedda0443e2006-06-15 16:17:17 +0000246{
247 int ipaddr, off, datlen;
248 tcphdr_t *tcp;
249 caddr_t data;
250 u_short port;
251 ip_t *ip;
252
253 aps = aps; /* LINT */
254
255 ip = fin->fin_ip;
256 tcp = (tcphdr_t *)fin->fin_dp;
Darren Reedc4af1f32007-08-20 10:15:33 +0000257 ipaddr = nat->nat_osrcaddr;
Darren Reedda0443e2006-06-15 16:17:17 +0000258 data = (caddr_t)tcp + (TCP_OFF(tcp) << 2);
Darren Reedd4718fc2006-06-15 17:00:40 +0000259 datlen = fin->fin_plen - fin->fin_hlen - (TCP_OFF(tcp) << 2);
Darren Reedda0443e2006-06-15 16:17:17 +0000260 if (find_port(ipaddr, data, datlen, &off, &port) == 0) {
261 fr_info_t fi;
262 nat_t *nat2;
263
264/* port = htons(port); */
Darren Reedc4af1f32007-08-20 10:15:33 +0000265 nat2 = ipf_nat_outlookup(fin, IPN_UDP, IPPROTO_UDP,
266 ip->ip_src, ip->ip_dst);
Darren Reedda0443e2006-06-15 16:17:17 +0000267 if (nat2 == NULL) {
268 struct ip newip;
269 struct udphdr udp;
Martti Kuparinenb4c6ea22007-10-25 09:26:49 +0000270
Darren Reedda0443e2006-06-15 16:17:17 +0000271 bcopy((caddr_t)ip, (caddr_t)&newip, sizeof(newip));
Darren Reedd4718fc2006-06-15 17:00:40 +0000272 newip.ip_len = htons(fin->fin_hlen + sizeof(udp));
Darren Reedda0443e2006-06-15 16:17:17 +0000273 newip.ip_p = IPPROTO_UDP;
Darren Reedc4af1f32007-08-20 10:15:33 +0000274 newip.ip_src = nat->nat_osrcip;
Martti Kuparinenb4c6ea22007-10-25 09:26:49 +0000275
Darren Reedda0443e2006-06-15 16:17:17 +0000276 bzero((char *)&udp, sizeof(udp));
277 udp.uh_sport = port;
Martti Kuparinenb4c6ea22007-10-25 09:26:49 +0000278
Darren Reedda0443e2006-06-15 16:17:17 +0000279 bcopy((caddr_t)fin, (caddr_t)&fi, sizeof(fi));
Darren Reedc4af1f32007-08-20 10:15:33 +0000280 fi.fin_state = NULL;
281 fi.fin_nat = NULL;
Darren Reedda0443e2006-06-15 16:17:17 +0000282 fi.fin_fi.fi_p = IPPROTO_UDP;
283 fi.fin_data[0] = port;
284 fi.fin_data[1] = 0;
285 fi.fin_dp = (char *)&udp;
286
Darren Reedc4af1f32007-08-20 10:15:33 +0000287 nat2 = ipf_nat_add(&fi, nat->nat_ptr, NULL,
Darren Reedda0443e2006-06-15 16:17:17 +0000288 NAT_SLAVE|IPN_UDP|SI_W_DPORT,
289 NAT_OUTBOUND);
290 if (nat2 != NULL) {
Darren Reedc4af1f32007-08-20 10:15:33 +0000291 (void) ipf_nat_proto(&fi, nat2, IPN_UDP);
292 ipf_nat_update(&fi, nat2, nat2->nat_ptr);
Darren Reedda0443e2006-06-15 16:17:17 +0000293
294 nat2->nat_ptr->in_hits++;
295#ifdef IPFILTER_LOG
Darren Reedc4af1f32007-08-20 10:15:33 +0000296 ipf_nat_log(nat2, (u_int)(nat->nat_ptr->in_redir));
Darren Reedda0443e2006-06-15 16:17:17 +0000297#endif
298 bcopy((caddr_t)&ip->ip_src.s_addr,
299 data + off, 4);
Darren Reedc4af1f32007-08-20 10:15:33 +0000300 bcopy((caddr_t)&nat2->nat_osport,
Darren Reedda0443e2006-06-15 16:17:17 +0000301 data + off + 4, 2);
302 }
303 }
304 }
305 return 0;
306}