Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / contrib / ipfilter / netinet / ip_raudio_pxy.c
1 /*
2  * $FreeBSD: src/sys/contrib/ipfilter/netinet/ip_raudio_pxy.c,v 1.7.2.3 2002/04/27 17:37:12 darrenr Exp $
3  * $DragonFly: src/sys/contrib/ipfilter/netinet/ip_raudio_pxy.c,v 1.2 2003/06/17 04:28:20 dillon Exp $
4  */
5 #if SOLARIS && defined(_KERNEL)
6 extern  kmutex_t        ipf_rw;
7 #endif
8
9 #define IPF_RAUDIO_PROXY
10
11
12 int ippr_raudio_init __P((void));
13 int ippr_raudio_new __P((fr_info_t *, ip_t *, ap_session_t *, nat_t *));
14 int ippr_raudio_in __P((fr_info_t *, ip_t *, ap_session_t *, nat_t *));
15 int ippr_raudio_out __P((fr_info_t *, ip_t *, ap_session_t *, nat_t *));
16
17 static  frentry_t       raudiofr;
18
19
20 /*
21  * Real Audio application proxy initialization.
22  */
23 int ippr_raudio_init()
24 {
25         bzero((char *)&raudiofr, sizeof(raudiofr));
26         raudiofr.fr_ref = 1;
27         raudiofr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
28         return 0;
29 }
30
31
32 /*
33  * Setup for a new proxy to handle Real Audio.
34  */
35 int ippr_raudio_new(fin, ip, aps, nat)
36 fr_info_t *fin;
37 ip_t *ip;
38 ap_session_t *aps;
39 nat_t *nat;
40 {
41         raudio_t *rap;
42
43
44         KMALLOCS(aps->aps_data, void *, sizeof(raudio_t));
45         if (aps->aps_data == NULL)
46                 return -1;
47
48         bzero(aps->aps_data, sizeof(raudio_t));
49         rap = aps->aps_data;
50         aps->aps_psiz = sizeof(raudio_t);
51         rap->rap_mode = RAP_M_TCP;      /* default is for TCP */
52         return 0;
53 }
54
55
56
57 int ippr_raudio_out(fin, ip, aps, nat)
58 fr_info_t *fin;
59 ip_t *ip;
60 ap_session_t *aps;
61 nat_t *nat;
62 {
63         raudio_t *rap = aps->aps_data;
64         unsigned char membuf[512 + 1], *s;
65         u_short id = 0;
66         int off, dlen;
67         tcphdr_t *tcp;
68         int len = 0;
69         mb_t *m;
70 #if     SOLARIS
71         mb_t *m1;
72 #endif
73
74         /*
75          * If we've already processed the start messages, then nothing left
76          * for the proxy to do.
77          */
78         if (rap->rap_eos == 1)
79                 return 0;
80
81         tcp = (tcphdr_t *)fin->fin_dp;
82         off = fin->fin_hlen + (tcp->th_off << 2);
83         bzero(membuf, sizeof(membuf));
84 #if     SOLARIS
85         m = fin->fin_qfm;
86
87         dlen = msgdsize(m) - off;
88         if (dlen <= 0)
89                 return 0;
90         dlen = MIN(sizeof(membuf), dlen);
91         copyout_mblk(m, off, dlen, (char *)membuf);
92 #else
93         m = *(mb_t **)fin->fin_mp;
94
95         dlen = mbufchainlen(m) - off;
96         if (dlen <= 0)
97                 return 0;
98         dlen = MIN(sizeof(membuf), dlen);
99         m_copydata(m, off, dlen, (char *)membuf);
100 #endif
101         /*
102          * In all the startup parsing, ensure that we don't go outside
103          * the packet buffer boundary.
104          */
105         /*
106          * Look for the start of connection "PNA" string if not seen yet.
107          */
108         if (rap->rap_seenpna == 0) {
109                 s = (u_char *)memstr("PNA", (char *)membuf, 3, dlen);
110                 if (s == NULL)
111                         return 0;
112                 s += 3;
113                 rap->rap_seenpna = 1;
114         } else
115                 s = membuf;
116
117         /*
118          * Directly after the PNA will be the version number of this
119          * connection.
120          */
121         if (rap->rap_seenpna == 1 && rap->rap_seenver == 0) {
122                 if ((s + 1) - membuf < dlen) {
123                         rap->rap_version = (*s << 8) | *(s + 1);
124                         s += 2;
125                         rap->rap_seenver = 1;
126                 } else
127                         return 0;
128         }
129
130         /*
131          * Now that we've been past the PNA and version number, we're into the
132          * startup messages block.  This ends when a message with an ID of 0.
133          */
134         while ((rap->rap_eos == 0) && ((s + 1) - membuf < dlen)) {
135                 if (rap->rap_gotid == 0) {
136                         id = (*s << 8) | *(s + 1);
137                         s += 2;
138                         rap->rap_gotid = 1;
139                         if (id == RA_ID_END) {
140                                 rap->rap_eos = 1;
141                                 break;
142                         }
143                 } else if (rap->rap_gotlen == 0) {
144                         len = (*s << 8) | *(s + 1);
145                         s += 2;
146                         rap->rap_gotlen = 1;
147                 }
148
149                 if (rap->rap_gotid == 1 && rap->rap_gotlen == 1) {
150                         if (id == RA_ID_UDP) {
151                                 rap->rap_mode &= ~RAP_M_TCP;
152                                 rap->rap_mode |= RAP_M_UDP;
153                                 rap->rap_plport = (*s << 8) | *(s + 1);
154                         } else if (id == RA_ID_ROBUST) {
155                                 rap->rap_mode |= RAP_M_ROBUST;
156                                 rap->rap_prport = (*s << 8) | *(s + 1);
157                         }
158                         s += len;
159                         rap->rap_gotlen = 0;
160                         rap->rap_gotid = 0;
161                 }
162         }
163         return 0;
164 }
165
166
167 int ippr_raudio_in(fin, ip, aps, nat)
168 fr_info_t *fin;
169 ip_t *ip;
170 ap_session_t *aps;
171 nat_t *nat;
172 {
173         unsigned char membuf[IPF_MAXPORTLEN + 1], *s;
174         tcphdr_t *tcp, tcph, *tcp2 = &tcph;
175         raudio_t *rap = aps->aps_data;
176         int off, dlen, slen, clen;
177         struct in_addr swa, swb;
178         int a1, a2, a3, a4;
179         u_short sp, dp;
180         fr_info_t fi;
181         tcp_seq seq;
182         nat_t *ipn;
183         u_char swp;
184         mb_t *m;
185 #if     SOLARIS
186         mb_t *m1;
187 #endif
188
189         /*
190          * Wait until we've seen the end of the start messages and even then
191          * only proceed further if we're using UDP.  If they want to use TCP
192          * then data is sent back on the same channel that is already open.
193          */
194         if (rap->rap_sdone != 0)
195                 return 0;
196
197         tcp = (tcphdr_t *)fin->fin_dp;
198         off = fin->fin_hlen + (tcp->th_off << 2);
199         m = *(mb_t **)fin->fin_mp;
200
201 #if     SOLARIS
202         m = fin->fin_qfm;
203
204         dlen = msgdsize(m) - off;
205         if (dlen <= 0)
206                 return 0;
207         bzero(membuf, sizeof(membuf));
208         clen = MIN(sizeof(membuf), dlen);
209         copyout_mblk(m, off, clen, (char *)membuf);
210 #else
211         dlen = mbufchainlen(m) - off;
212         if (dlen <= 0)
213                 return 0;
214         bzero(membuf, sizeof(membuf));
215         clen = MIN(sizeof(membuf), dlen);
216         m_copydata(m, off, clen, (char *)membuf);
217 #endif
218
219         seq = ntohl(tcp->th_seq);
220         /*
221          * Check to see if the data in this packet is of interest to us.
222          * We only care for the first 19 bytes coming back from the server.
223          */
224         if (rap->rap_sseq == 0) {
225                 s = (u_char *)memstr("PNA", (char *)membuf, 3, clen);
226                 if (s == NULL)
227                         return 0;
228                 a1 = s - membuf;
229                 dlen -= a1;
230                 a1 = 0;
231                 rap->rap_sseq = seq;
232                 a2 = MIN(dlen, sizeof(rap->rap_svr));
233         } else if (seq <= rap->rap_sseq + sizeof(rap->rap_svr)) {
234                 /*
235                  * seq # which is the start of data and from that the offset
236                  * into the buffer array.
237                  */
238                 a1 = seq - rap->rap_sseq;
239                 a2 = MIN(dlen, sizeof(rap->rap_svr));
240                 a2 -= a1;
241                 s = membuf;
242         } else
243                 return 0;
244
245         for (a3 = a1, a4 = a2; (a4 > 0) && (a3 < 19) && (a3 >= 0); a4--,a3++) {
246                 rap->rap_sbf |= (1 << a3);
247                 rap->rap_svr[a3] = *s++;
248         }
249
250         if ((rap->rap_sbf != 0x7ffff) || (!rap->rap_eos))       /* 19 bits */
251                 return 0;
252         rap->rap_sdone = 1;
253
254         s = (u_char *)rap->rap_svr + 11;
255         if (((*s << 8) | *(s + 1)) == RA_ID_ROBUST) {
256                 s += 2;
257                 rap->rap_srport = (*s << 8) | *(s + 1);
258         }
259
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_inip;
266         ip->ip_dst = nat->nat_oip;
267
268         bcopy((char *)fin, (char *)&fi, sizeof(fi));
269         bzero((char *)tcp2, sizeof(*tcp2));
270         tcp2->th_off = 5;
271         fi.fin_dp = (char *)tcp2;
272         fi.fin_fr = &raudiofr;
273         fi.fin_dlen = sizeof(*tcp2);
274         tcp2->th_win = htons(8192);
275         slen = ip->ip_len;
276         ip->ip_len = fin->fin_hlen + sizeof(*tcp);
277
278         if (((rap->rap_mode & RAP_M_UDP_ROBUST) == RAP_M_UDP_ROBUST) &&
279             (rap->rap_srport != 0)) {
280                 dp = rap->rap_srport;
281                 sp = rap->rap_prport;
282                 tcp2->th_sport = htons(sp);
283                 tcp2->th_dport = htons(dp);
284                 fi.fin_data[0] = dp;
285                 fi.fin_data[1] = sp;
286                 fi.fin_out = 0;
287                 ipn = nat_new(&fi, ip, nat->nat_ptr, NULL,
288                               IPN_UDP | (sp ? 0 : FI_W_SPORT), NAT_OUTBOUND);
289                 if (ipn != NULL) {
290                         ipn->nat_age = fr_defnatage;
291                         (void) fr_addstate(ip, &fi, NULL,
292                                            FI_IGNOREPKT|FI_NORULE|
293                                            (sp ? 0 : FI_W_SPORT));
294                 }
295         }
296
297         if ((rap->rap_mode & RAP_M_UDP) == RAP_M_UDP) {
298                 sp = rap->rap_plport;
299                 tcp2->th_sport = htons(sp);
300                 tcp2->th_dport = 0; /* XXX - don't specify remote port */
301                 fi.fin_data[0] = sp;
302                 fi.fin_data[1] = 0;
303                 fi.fin_out = 1;
304                 ipn = nat_new(&fi, ip, nat->nat_ptr, NULL, IPN_UDP|FI_W_DPORT,
305                               NAT_OUTBOUND);
306                 if (ipn != NULL) {
307                         ipn->nat_age = fr_defnatage;
308                         (void) fr_addstate(ip, &fi, NULL,
309                                            FI_W_DPORT|FI_IGNOREPKT|FI_NORULE);
310                 }
311         }
312
313         ip->ip_p = swp;
314         ip->ip_len = slen;
315         ip->ip_src = swa;
316         ip->ip_dst = swb;
317         return 0;
318 }