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