4 * Copyright (C) 2012 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * $Id: ip_raudio_pxy.c,v 1.40.2.4 2006/07/14 06:12:17 darrenr Exp $
11 #define IPF_RAUDIO_PROXY
14 void ipf_p_raudio_main_load __P((void));
15 void ipf_p_raudio_main_unload __P((void));
16 int ipf_p_raudio_new __P((void *, fr_info_t *, ap_session_t *, nat_t *));
17 int ipf_p_raudio_in __P((void *, fr_info_t *, ap_session_t *, nat_t *));
18 int ipf_p_raudio_out __P((void *, fr_info_t *, ap_session_t *, nat_t *));
20 static frentry_t raudiofr;
22 int raudio_proxy_init = 0;
26 * Real Audio application proxy initialization.
29 ipf_p_raudio_main_load()
31 bzero((char *)&raudiofr, sizeof(raudiofr));
33 raudiofr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
34 MUTEX_INIT(&raudiofr.fr_lock, "Real Audio proxy rule lock");
35 raudio_proxy_init = 1;
40 ipf_p_raudio_main_unload()
42 if (raudio_proxy_init == 1) {
43 MUTEX_DESTROY(&raudiofr.fr_lock);
44 raudio_proxy_init = 0;
50 * Setup for a new proxy to handle Real Audio.
53 ipf_p_raudio_new(arg, fin, aps, nat)
66 KMALLOCS(aps->aps_data, void *, sizeof(raudio_t));
67 if (aps->aps_data == NULL)
70 bzero(aps->aps_data, sizeof(raudio_t));
72 aps->aps_psiz = sizeof(raudio_t);
73 rap->rap_mode = RAP_M_TCP; /* default is for TCP */
80 ipf_p_raudio_out(arg, fin, aps, nat)
86 raudio_t *rap = aps->aps_data;
87 unsigned char membuf[512 + 1], *s;
97 * If we've already processed the start messages, then nothing left
98 * for the proxy to do.
100 if (rap->rap_eos == 1)
104 tcp = (tcphdr_t *)fin->fin_dp;
105 off = (char *)tcp - (char *)fin->fin_ip;
106 off += (TCP_OFF(tcp) << 2) + fin->fin_ipoff;
109 dlen = fin->fin_plen - off;
111 dlen = MSGDSIZE(m) - off;
116 if (dlen > sizeof(membuf))
117 dlen = sizeof(membuf);
119 bzero((char *)membuf, sizeof(membuf));
120 COPYDATA(m, off, dlen, (char *)membuf);
122 * In all the startup parsing, ensure that we don't go outside
123 * the packet buffer boundary.
126 * Look for the start of connection "PNA" string if not seen yet.
128 if (rap->rap_seenpna == 0) {
129 s = (u_char *)memstr("PNA", (char *)membuf, 3, dlen);
133 rap->rap_seenpna = 1;
138 * Directly after the PNA will be the version number of this
141 if (rap->rap_seenpna == 1 && rap->rap_seenver == 0) {
142 if ((s + 1) - membuf < dlen) {
143 rap->rap_version = (*s << 8) | *(s + 1);
145 rap->rap_seenver = 1;
151 * Now that we've been past the PNA and version number, we're into the
152 * startup messages block. This ends when a message with an ID of 0.
154 while ((rap->rap_eos == 0) && ((s + 1) - membuf < dlen)) {
155 if (rap->rap_gotid == 0) {
156 id = (*s << 8) | *(s + 1);
159 if (id == RA_ID_END) {
163 } else if (rap->rap_gotlen == 0) {
164 len = (*s << 8) | *(s + 1);
169 if (rap->rap_gotid == 1 && rap->rap_gotlen == 1) {
170 if (id == RA_ID_UDP) {
171 rap->rap_mode &= ~RAP_M_TCP;
172 rap->rap_mode |= RAP_M_UDP;
173 rap->rap_plport = (*s << 8) | *(s + 1);
174 } else if (id == RA_ID_ROBUST) {
175 rap->rap_mode |= RAP_M_ROBUST;
176 rap->rap_prport = (*s << 8) | *(s + 1);
188 ipf_p_raudio_in(arg, fin, aps, nat)
194 unsigned char membuf[IPF_MAXPORTLEN + 1], *s;
195 tcphdr_t *tcp, tcph, *tcp2 = &tcph;
196 raudio_t *rap = aps->aps_data;
197 ipf_main_softc_t *softc;
198 ipf_nat_softc_t *softn;
199 struct in_addr swa, swb;
210 softc = fin->fin_main_soft;
211 softn = softc->ipf_nat_soft;
213 * Wait until we've seen the end of the start messages and even then
214 * only proceed further if we're using UDP. If they want to use TCP
215 * then data is sent back on the same channel that is already open.
217 if (rap->rap_sdone != 0)
221 tcp = (tcphdr_t *)fin->fin_dp;
222 off = (char *)tcp - (char *)fin->fin_ip;
223 off += (TCP_OFF(tcp) << 2) + fin->fin_ipoff;
226 dlen = fin->fin_plen - off;
228 dlen = MSGDSIZE(m) - off;
233 if (dlen > sizeof(membuf))
234 dlen = sizeof(membuf);
236 bzero((char *)membuf, sizeof(membuf));
237 COPYDATA(m, off, dlen, (char *)membuf);
239 seq = ntohl(tcp->th_seq);
241 * Check to see if the data in this packet is of interest to us.
242 * We only care for the first 19 bytes coming back from the server.
244 if (rap->rap_sseq == 0) {
245 s = (u_char *)memstr("PNA", (char *)membuf, 3, dlen);
252 a2 = MIN(dlen, sizeof(rap->rap_svr));
253 } else if (seq <= rap->rap_sseq + sizeof(rap->rap_svr)) {
255 * seq # which is the start of data and from that the offset
256 * into the buffer array.
258 a1 = seq - rap->rap_sseq;
259 a2 = MIN(dlen, sizeof(rap->rap_svr));
265 for (a3 = a1, a4 = a2; (a4 > 0) && (a3 < 19) && (a3 >= 0); a4--,a3++) {
266 rap->rap_sbf |= (1 << a3);
267 rap->rap_svr[a3] = *s++;
270 if ((rap->rap_sbf != 0x7ffff) || (!rap->rap_eos)) /* 19 bits */
274 s = (u_char *)rap->rap_svr + 11;
275 if (((*s << 8) | *(s + 1)) == RA_ID_ROBUST) {
277 rap->rap_srport = (*s << 8) | *(s + 1);
285 ip->ip_p = IPPROTO_UDP;
286 ip->ip_src = nat->nat_ndstip;
287 ip->ip_dst = nat->nat_odstip;
289 bcopy((char *)fin, (char *)&fi, sizeof(fi));
290 bzero((char *)tcp2, sizeof(*tcp2));
292 fi.fin_flx |= FI_IGNORE;
293 fi.fin_dp = (char *)tcp2;
294 fi.fin_fr = &raudiofr;
295 fi.fin_dlen = sizeof(*tcp2);
296 fi.fin_plen = fi.fin_hlen + sizeof(*tcp2);
297 tcp2->th_win = htons(8192);
299 ip->ip_len = htons(fin->fin_hlen + sizeof(*tcp));
301 if (((rap->rap_mode & RAP_M_UDP_ROBUST) == RAP_M_UDP_ROBUST) &&
302 (rap->rap_srport != 0)) {
303 dp = rap->rap_srport;
304 sp = rap->rap_prport;
305 tcp2->th_sport = htons(sp);
306 tcp2->th_dport = htons(dp);
310 MUTEX_ENTER(&softn->ipf_nat_new);
311 nat2 = ipf_nat_add(&fi, nat->nat_ptr, NULL,
312 NAT_SLAVE|IPN_UDP | (sp ? 0 : SI_W_SPORT),
314 MUTEX_EXIT(&softn->ipf_nat_new);
316 (void) ipf_nat_proto(&fi, nat2, IPN_UDP);
317 MUTEX_ENTER(&nat2->nat_lock);
318 ipf_nat_update(&fi, nat2);
319 MUTEX_EXIT(&nat2->nat_lock);
321 (void) ipf_state_add(softc, &fi, NULL,
322 (sp ? 0 : SI_W_SPORT));
326 if ((rap->rap_mode & RAP_M_UDP) == RAP_M_UDP) {
327 sp = rap->rap_plport;
328 tcp2->th_sport = htons(sp);
329 tcp2->th_dport = 0; /* XXX - don't specify remote port */
333 MUTEX_ENTER(&softn->ipf_nat_new);
334 nat2 = ipf_nat_add(&fi, nat->nat_ptr, NULL,
335 NAT_SLAVE|IPN_UDP|SI_W_DPORT,
337 MUTEX_EXIT(&softn->ipf_nat_new);
339 (void) ipf_nat_proto(&fi, nat2, IPN_UDP);
340 MUTEX_ENTER(&nat2->nat_lock);
341 ipf_nat_update(&fi, nat2);
342 MUTEX_EXIT(&nat2->nat_lock);
344 (void) ipf_state_add(softc, &fi, NULL, SI_W_DPORT);