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