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