1
2 /*
3 * Copyright (C) 2012 by Darren Reed.
4 *
5 * See the IPFILTER.LICENCE file for details on licencing.
6 *
7 * $Id: ip_raudio_pxy.c,v 1.40.2.4 2006/07/14 06:12:17 darrenr Exp $
8 */
9
10 #define IPF_RAUDIO_PROXY
11
12
13 void ipf_p_raudio_main_load(void);
14 void ipf_p_raudio_main_unload(void);
15 int ipf_p_raudio_new(void *, fr_info_t *, ap_session_t *, nat_t *);
16 int ipf_p_raudio_in(void *, fr_info_t *, ap_session_t *, nat_t *);
17 int ipf_p_raudio_out(void *, fr_info_t *, ap_session_t *, nat_t *);
18
19 static frentry_t raudiofr;
20
21 int raudio_proxy_init = 0;
22
23
24 /*
25 * Real Audio application proxy initialization.
26 */
27 void
ipf_p_raudio_main_load(void)28 ipf_p_raudio_main_load(void)
29 {
30 bzero((char *)&raudiofr, sizeof(raudiofr));
31 raudiofr.fr_ref = 1;
32 raudiofr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
33 MUTEX_INIT(&raudiofr.fr_lock, "Real Audio proxy rule lock");
34 raudio_proxy_init = 1;
35 }
36
37
38 void
ipf_p_raudio_main_unload(void)39 ipf_p_raudio_main_unload(void)
40 {
41 if (raudio_proxy_init == 1) {
42 MUTEX_DESTROY(&raudiofr.fr_lock);
43 raudio_proxy_init = 0;
44 }
45 }
46
47
48 /*
49 * Setup for a new proxy to handle Real Audio.
50 */
51 int
ipf_p_raudio_new(void * arg,fr_info_t * fin,ap_session_t * aps,nat_t * nat __unused)52 ipf_p_raudio_new(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat __unused)
53 {
54 raudio_t *rap;
55
56 if (fin->fin_v != 4)
57 return (-1);
58
59 KMALLOCS(aps->aps_data, void *, sizeof(raudio_t));
60 if (aps->aps_data == NULL)
61 return (-1);
62
63 bzero(aps->aps_data, sizeof(raudio_t));
64 rap = aps->aps_data;
65 aps->aps_psiz = sizeof(raudio_t);
66 rap->rap_mode = RAP_M_TCP; /* default is for TCP */
67 return (0);
68 }
69
70
71
72 int
ipf_p_raudio_out(void * arg,fr_info_t * fin,ap_session_t * aps,nat_t * nat __unused)73 ipf_p_raudio_out(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat __unused)
74 {
75 raudio_t *rap = aps->aps_data;
76 unsigned char membuf[512 + 1], *s;
77 u_short id = 0;
78 tcphdr_t *tcp;
79 int off, dlen;
80 int len = 0;
81 mb_t *m;
82
83 /*
84 * If we've already processed the start messages, then nothing left
85 * for the proxy to do.
86 */
87 if (rap->rap_eos == 1)
88 return (0);
89
90 m = fin->fin_m;
91 tcp = (tcphdr_t *)fin->fin_dp;
92 off = (char *)tcp - (char *)fin->fin_ip;
93 off += (TCP_OFF(tcp) << 2) + fin->fin_ipoff;
94
95 dlen = MSGDSIZE(m) - off;
96 if (dlen <= 0)
97 return (0);
98
99 if (dlen > sizeof(membuf))
100 dlen = sizeof(membuf);
101
102 bzero((char *)membuf, sizeof(membuf));
103 COPYDATA(m, off, dlen, (char *)membuf);
104 /*
105 * In all the startup parsing, ensure that we don't go outside
106 * the packet buffer boundary.
107 */
108 /*
109 * Look for the start of connection "PNA" string if not seen yet.
110 */
111 if (rap->rap_seenpna == 0) {
112 s = (u_char *)memstr("PNA", (char *)membuf, 3, dlen);
113 if (s == NULL)
114 return (0);
115 s += 3;
116 rap->rap_seenpna = 1;
117 } else
118 s = membuf;
119
120 /*
121 * Directly after the PNA will be the version number of this
122 * connection.
123 */
124 if (rap->rap_seenpna == 1 && rap->rap_seenver == 0) {
125 if ((s + 1) - membuf < dlen) {
126 rap->rap_version = (*s << 8) | *(s + 1);
127 s += 2;
128 rap->rap_seenver = 1;
129 } else
130 return (0);
131 }
132
133 /*
134 * Now that we've been past the PNA and version number, we're into the
135 * startup messages block. This ends when a message with an ID of 0.
136 */
137 while ((rap->rap_eos == 0) && ((s + 1) - membuf < dlen)) {
138 if (rap->rap_gotid == 0) {
139 id = (*s << 8) | *(s + 1);
140 s += 2;
141 rap->rap_gotid = 1;
142 if (id == RA_ID_END) {
143 rap->rap_eos = 1;
144 break;
145 }
146 } else if (rap->rap_gotlen == 0) {
147 len = (*s << 8) | *(s + 1);
148 s += 2;
149 rap->rap_gotlen = 1;
150 }
151
152 if (rap->rap_gotid == 1 && rap->rap_gotlen == 1) {
153 if (id == RA_ID_UDP) {
154 rap->rap_mode &= ~RAP_M_TCP;
155 rap->rap_mode |= RAP_M_UDP;
156 rap->rap_plport = (*s << 8) | *(s + 1);
157 } else if (id == RA_ID_ROBUST) {
158 rap->rap_mode |= RAP_M_ROBUST;
159 rap->rap_prport = (*s << 8) | *(s + 1);
160 }
161 s += len;
162 rap->rap_gotlen = 0;
163 rap->rap_gotid = 0;
164 }
165 }
166 return (0);
167 }
168
169
170 int
ipf_p_raudio_in(void * arg,fr_info_t * fin,ap_session_t * aps,nat_t * nat)171 ipf_p_raudio_in(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
172 {
173 unsigned char membuf[IPF_MAXPORTLEN + 1], *s;
174 tcphdr_t *tcp, tcph, *tcp2 = &tcph;
175 raudio_t *rap = aps->aps_data;
176 ipf_main_softc_t *softc;
177 ipf_nat_softc_t *softn;
178 struct in_addr swa, swb;
179 int off, dlen, slen;
180 int a1, a2, a3, a4;
181 u_short sp, dp;
182 fr_info_t fi;
183 tcp_seq seq;
184 nat_t *nat2;
185 u_char swp;
186 ip_t *ip;
187 mb_t *m;
188
189 softc = fin->fin_main_soft;
190 softn = softc->ipf_nat_soft;
191 /*
192 * Wait until we've seen the end of the start messages and even then
193 * only proceed further if we're using UDP. If they want to use TCP
194 * then data is sent back on the same channel that is already open.
195 */
196 if (rap->rap_sdone != 0)
197 return (0);
198
199 m = fin->fin_m;
200 tcp = (tcphdr_t *)fin->fin_dp;
201 off = (char *)tcp - (char *)fin->fin_ip;
202 off += (TCP_OFF(tcp) << 2) + fin->fin_ipoff;
203
204 dlen = MSGDSIZE(m) - off;
205 if (dlen <= 0)
206 return (0);
207
208 if (dlen > sizeof(membuf))
209 dlen = sizeof(membuf);
210
211 bzero((char *)membuf, sizeof(membuf));
212 COPYDATA(m, off, dlen, (char *)membuf);
213
214 seq = ntohl(tcp->th_seq);
215 /*
216 * Check to see if the data in this packet is of interest to us.
217 * We only care for the first 19 bytes coming back from the server.
218 */
219 if (rap->rap_sseq == 0) {
220 s = (u_char *)memstr("PNA", (char *)membuf, 3, dlen);
221 if (s == NULL)
222 return (0);
223 a1 = s - membuf;
224 dlen -= a1;
225 a1 = 0;
226 rap->rap_sseq = seq;
227 a2 = MIN(dlen, sizeof(rap->rap_svr));
228 } else if (seq <= rap->rap_sseq + sizeof(rap->rap_svr)) {
229 /*
230 * seq # which is the start of data and from that the offset
231 * into the buffer array.
232 */
233 a1 = seq - rap->rap_sseq;
234 a2 = MIN(dlen, sizeof(rap->rap_svr));
235 a2 -= a1;
236 s = membuf;
237 } else
238 return (0);
239
240 for (a3 = a1, a4 = a2; (a4 > 0) && (a3 < 19) && (a3 >= 0); a4--,a3++) {
241 rap->rap_sbf |= (1 << a3);
242 rap->rap_svr[a3] = *s++;
243 }
244
245 if ((rap->rap_sbf != 0x7ffff) || (!rap->rap_eos)) /* 19 bits */
246 return (0);
247 rap->rap_sdone = 1;
248
249 s = (u_char *)rap->rap_svr + 11;
250 if (((*s << 8) | *(s + 1)) == RA_ID_ROBUST) {
251 s += 2;
252 rap->rap_srport = (*s << 8) | *(s + 1);
253 }
254
255 ip = fin->fin_ip;
256 swp = ip->ip_p;
257 swa = ip->ip_src;
258 swb = ip->ip_dst;
259
260 ip->ip_p = IPPROTO_UDP;
261 ip->ip_src = nat->nat_ndstip;
262 ip->ip_dst = nat->nat_odstip;
263
264 bcopy((char *)fin, (char *)&fi, sizeof(fi));
265 bzero((char *)tcp2, sizeof(*tcp2));
266 TCP_OFF_A(tcp2, 5);
267 fi.fin_flx |= FI_IGNORE;
268 fi.fin_dp = (char *)tcp2;
269 fi.fin_fr = &raudiofr;
270 fi.fin_dlen = sizeof(*tcp2);
271 fi.fin_plen = fi.fin_hlen + sizeof(*tcp2);
272 tcp2->th_win = htons(8192);
273 slen = ip->ip_len;
274 ip->ip_len = htons(fin->fin_hlen + sizeof(*tcp));
275
276 if (((rap->rap_mode & RAP_M_UDP_ROBUST) == RAP_M_UDP_ROBUST) &&
277 (rap->rap_srport != 0)) {
278 dp = rap->rap_srport;
279 sp = rap->rap_prport;
280 tcp2->th_sport = htons(sp);
281 tcp2->th_dport = htons(dp);
282 fi.fin_data[0] = dp;
283 fi.fin_data[1] = sp;
284 fi.fin_out = 0;
285 MUTEX_ENTER(&softn->ipf_nat_new);
286 nat2 = ipf_nat_add(&fi, nat->nat_ptr, NULL,
287 NAT_SLAVE|IPN_UDP | (sp ? 0 : SI_W_SPORT),
288 NAT_OUTBOUND);
289 MUTEX_EXIT(&softn->ipf_nat_new);
290 if (nat2 != NULL) {
291 (void) ipf_nat_proto(&fi, nat2, IPN_UDP);
292 MUTEX_ENTER(&nat2->nat_lock);
293 ipf_nat_update(&fi, nat2);
294 MUTEX_EXIT(&nat2->nat_lock);
295
296 (void) ipf_state_add(softc, &fi, NULL,
297 (sp ? 0 : SI_W_SPORT));
298 }
299 }
300
301 if ((rap->rap_mode & RAP_M_UDP) == RAP_M_UDP) {
302 sp = rap->rap_plport;
303 tcp2->th_sport = htons(sp);
304 tcp2->th_dport = 0; /* XXX - don't specify remote port */
305 fi.fin_data[0] = sp;
306 fi.fin_data[1] = 0;
307 fi.fin_out = 1;
308 MUTEX_ENTER(&softn->ipf_nat_new);
309 nat2 = ipf_nat_add(&fi, nat->nat_ptr, NULL,
310 NAT_SLAVE|IPN_UDP|SI_W_DPORT,
311 NAT_OUTBOUND);
312 MUTEX_EXIT(&softn->ipf_nat_new);
313 if (nat2 != NULL) {
314 (void) ipf_nat_proto(&fi, nat2, IPN_UDP);
315 MUTEX_ENTER(&nat2->nat_lock);
316 ipf_nat_update(&fi, nat2);
317 MUTEX_EXIT(&nat2->nat_lock);
318
319 (void) ipf_state_add(softc, &fi, NULL, SI_W_DPORT);
320 }
321 }
322
323 ip->ip_p = swp;
324 ip->ip_len = slen;
325 ip->ip_src = swa;
326 ip->ip_dst = swb;
327 return (0);
328 }
329