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