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