rename functions that clash with reserved math procedures to avoid gcc3.4
[dragonfly.git] / sys / contrib / ipfilter / netinet / ip_proxy.c
1 /*
2  * Copyright (C) 1997-2002 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * @(#)$Id: ip_proxy.c,v 2.9.2.24 2002/08/28 12:45:51 darrenr Exp $
7  * $FreeBSD: src/sys/contrib/ipfilter/netinet/ip_proxy.c,v 1.11.2.5 2003/03/01 03:55:54 darrenr Exp $
8  * $DragonFly: src/sys/contrib/ipfilter/netinet/ip_proxy.c,v 1.7 2004/02/12 22:35:47 joerg Exp $
9  */
10 #if (defined(__DragonFly__) || defined(__FreeBSD__)) && defined(KERNEL) && !defined(_KERNEL)
11 # define        _KERNEL
12 #endif
13
14 #if defined(__sgi) && (IRIX > 602)
15 # include <sys/ptimers.h>
16 #endif
17 #include <sys/errno.h>
18 #include <sys/types.h>
19 #include <sys/param.h>
20 #include <sys/time.h>
21 #include <sys/file.h>
22 #if !defined(__DragonFly__) && !defined(__FreeBSD_version)  
23 # include <sys/ioctl.h>      
24 #endif
25 #include <sys/fcntl.h>
26 #if !defined(_KERNEL) && !defined(KERNEL)
27 # include <stdio.h>
28 # include <string.h>
29 # include <stdlib.h>
30 #endif
31 #ifndef linux
32 # include <sys/protosw.h>
33 #endif
34 #include <sys/socket.h>
35 #if defined(_KERNEL)
36 # if !defined(linux)
37 #  include <sys/systm.h>
38 # else
39 #  include <linux/string.h>
40 # endif
41 #endif
42 #if !defined(__SVR4) && !defined(__svr4__)
43 # ifndef linux
44 #  include <sys/mbuf.h>
45 # endif
46 #else
47 # include <sys/byteorder.h>
48 # ifdef _KERNEL
49 #  include <sys/dditypes.h>
50 # endif
51 # include <sys/stream.h>
52 # include <sys/kmem.h>
53 #endif
54 #if defined(__DragonFly__) || __FreeBSD__ > 2
55 # include <sys/queue.h>
56 #endif
57 #include <net/if.h>
58 #ifdef sun
59 # include <net/af.h>
60 #endif
61 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #ifndef linux
66 # include <netinet/ip_var.h>
67 #endif
68 #include <netinet/tcp.h>
69 #include <netinet/udp.h>
70 #include <netinet/ip_icmp.h>
71 #include "ip_compat.h"
72 #include <netinet/tcpip.h>
73 #include "ip_fil.h"
74 #include "ip_nat.h"
75 #include "ip_state.h"
76 #include "ip_proxy.h"
77 #if defined(__DragonFly__) || __FreeBSD_version >= 300000
78 # include <sys/malloc.h>
79 #endif
80
81 #if defined(_KERNEL) && (SOLARIS || defined(__sgi))
82 extern  KRWLOCK_T       ipf_nat, ipf_state;
83 #endif
84
85 static int appr_fixseqack (fr_info_t *, ip_t *, ap_session_t *, int );
86
87
88 #define PROXY_DEBUG 0
89
90 #define AP_SESS_SIZE    53
91
92 #include "ip_ftp_pxy.c"
93 #if defined(_KERNEL)
94 #include "ip_rcmd_pxy.c"
95 #include "ip_raudio_pxy.c"
96 #include "ip_netbios_pxy.c"
97 #include "ip_h323_pxy.c"
98 #endif
99 #include "ip_ipsec_pxy.c"
100
101 ap_session_t    *ap_sess_tab[AP_SESS_SIZE];
102 ap_session_t    *ap_sess_list = NULL;
103 aproxy_t        *ap_proxylist = NULL;
104 aproxy_t        ap_proxies[] = {
105 #ifdef  IPF_FTP_PROXY
106         { NULL, "ftp", (char)IPPROTO_TCP, 0, 0, ippr_ftp_init, NULL,
107           ippr_ftp_new, NULL, ippr_ftp_in, ippr_ftp_out, NULL },
108 #endif
109 #ifdef  IPF_RCMD_PROXY
110         { NULL, "rcmd", (char)IPPROTO_TCP, 0, 0, ippr_rcmd_init, NULL,
111           ippr_rcmd_new, NULL, NULL, ippr_rcmd_out, NULL },
112 #endif
113 #ifdef  IPF_RAUDIO_PROXY
114         { NULL, "raudio", (char)IPPROTO_TCP, 0, 0, ippr_raudio_init, NULL,
115           ippr_raudio_new, NULL, ippr_raudio_in, ippr_raudio_out, NULL },
116 #endif
117 #ifdef IPF_IPSEC_PROXY
118         { NULL, "ipsec", (char)IPPROTO_UDP, 0, 0, ippr_ipsec_init, NULL,
119           ippr_ipsec_new, ippr_ipsec_del, NULL, ippr_ipsec_out,
120           ippr_ipsec_match },
121 #endif
122 #ifdef  IPF_NETBIOS_PROXY
123         { NULL, "netbios", (char)IPPROTO_UDP, 0, 0, ippr_netbios_init, NULL,
124           NULL, NULL, NULL, ippr_netbios_out, NULL },
125 #endif
126 #ifdef  IPF_H323_PROXY
127     { NULL, "h323", (char)IPPROTO_TCP, 0, 0, ippr_h323_init, NULL,
128           ippr_h323_new, ippr_h323_del, ippr_h323_in, ippr_h323_out, NULL },
129     { NULL, "h245", (char)IPPROTO_TCP, 0, 0, ippr_h245_init, NULL,
130           ippr_h245_new, NULL, NULL, ippr_h245_out, NULL },
131 #endif   
132         { NULL, "", '\0', 0, 0, NULL, NULL, NULL }
133 };
134
135
136 /*
137  * Dynamically add a new kernel proxy.  Ensure that it is unique in the
138  * collection compiled in and dynamically added.
139  */
140 int appr_add(ap)
141 aproxy_t *ap;
142 {
143         aproxy_t *a;
144
145         for (a = ap_proxies; a->apr_p; a++)
146                 if ((a->apr_p == ap->apr_p) &&
147                     !strncmp(a->apr_label, ap->apr_label,
148                              sizeof(ap->apr_label)))
149                         return -1;
150
151         for (a = ap_proxylist; a && a->apr_p; a = a->apr_next)
152                 if ((a->apr_p == ap->apr_p) &&
153                     !strncmp(a->apr_label, ap->apr_label,
154                              sizeof(ap->apr_label)))
155                         return -1;
156         ap->apr_next = ap_proxylist;
157         ap_proxylist = ap;
158         return (*ap->apr_init)();
159 }
160
161
162 /*
163  * Delete a proxy that has been added dynamically from those available.
164  * If it is in use, return 1 (do not destroy NOW), not in use 0 or -1
165  * if it cannot be matched.
166  */
167 int appr_del(ap)
168 aproxy_t *ap;
169 {
170         aproxy_t *a, **app;
171
172         for (app = &ap_proxylist; (a = *app); app = &a->apr_next)
173                 if (a == ap) {
174                         a->apr_flags |= APR_DELETE;
175                         *app = a->apr_next;
176                         if (ap->apr_ref != 0)
177                                 return 1;
178                         return 0;
179                 }
180         return -1;
181 }
182
183
184 /*
185  * Return 1 if the packet is a good match against a proxy, else 0.
186  */
187 int appr_ok(ip, tcp, nat)
188 ip_t *ip;
189 tcphdr_t *tcp;
190 ipnat_t *nat;
191 {
192         aproxy_t *apr = nat->in_apr;
193         u_short dport = nat->in_dport;
194
195         if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
196             (ip->ip_p != apr->apr_p))
197                 return 0;
198         if (((tcp != NULL) && (tcp->th_dport != dport)) || (!tcp && dport))
199                 return 0;
200         return 1;
201 }
202
203
204 /*
205  * If a proxy has a match function, call that to do extended packet
206  * matching.
207  */
208 int appr_match(fin, nat)
209 fr_info_t *fin;
210 nat_t *nat;
211 {
212         aproxy_t *apr;
213         ipnat_t *ipn;
214
215         ipn = nat->nat_ptr;
216         if (ipn == NULL)
217                 return -1;
218         apr = ipn->in_apr;
219         if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
220             (nat->nat_aps == NULL))
221                 return -1;
222         if (apr->apr_match != NULL)
223                 if ((*apr->apr_match)(fin, nat->nat_aps, nat) != 0)
224                         return -1;
225         return 0;
226 }
227
228
229 /*
230  * Allocate a new application proxy structure and fill it in with the
231  * relevant details.  call the init function once complete, prior to
232  * returning.
233  */
234 int appr_new(fin, ip, nat)
235 fr_info_t *fin;
236 ip_t *ip;
237 nat_t *nat;
238 {
239         ap_session_t *aps;
240         aproxy_t *apr;
241
242         if ((nat->nat_ptr == NULL) || (nat->nat_aps != NULL))
243                 return -1;
244
245         apr = nat->nat_ptr->in_apr;
246
247         if (!apr || (apr->apr_flags & APR_DELETE) || (ip->ip_p != apr->apr_p))
248                 return -1;
249
250         KMALLOC(aps, ap_session_t *);
251         if (!aps)
252                 return -1;
253         bzero((char *)aps, sizeof(*aps));
254         aps->aps_p = ip->ip_p;
255         aps->aps_data = NULL;
256         aps->aps_apr = apr;
257         aps->aps_psiz = 0;
258         if (apr->apr_new != NULL)
259                 if ((*apr->apr_new)(fin, ip, aps, nat) == -1) {
260                         if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) {
261                                 KFREES(aps->aps_data, aps->aps_psiz);
262                         }
263                         KFREE(aps);
264                         return -1;
265                 }
266         aps->aps_nat = nat;
267         aps->aps_next = ap_sess_list;
268         ap_sess_list = aps;
269         nat->nat_aps = aps;
270
271         return 0;
272 }
273
274
275 /*
276  * check to see if a packet should be passed through an active proxy routine
277  * if one has been setup for it.
278  */
279 int appr_check(ip, fin, nat)
280 ip_t *ip;
281 fr_info_t *fin;
282 nat_t *nat;
283 {
284 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
285         mb_t *m = fin->fin_qfm;
286         int dosum = 1;
287 #endif
288         tcphdr_t *tcp = NULL;
289         ap_session_t *aps;
290         aproxy_t *apr;
291         u_32_t sum;
292         short rv;
293         int err;
294
295         aps = nat->nat_aps;
296         if ((aps != NULL) && (aps->aps_p == ip->ip_p)) {
297                 if (ip->ip_p == IPPROTO_TCP) {
298                         tcp = (tcphdr_t *)fin->fin_dp;
299                         /*
300                          * verify that the checksum is correct.  If not, then
301                          * don't do anything with this packet.
302                          */
303 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
304                         if (dohwcksum && (m->b_ick_flag == ICK_VALID)) {
305                                 sum = tcp->th_sum;
306                                 dosum = 0;
307                         }
308                         if (dosum)
309                                 sum = fr_tcpsum(fin->fin_qfm, ip, tcp);
310 #else
311                         sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp);
312 #endif
313                         if (sum != tcp->th_sum) {
314 #if PROXY_DEBUG || (!defined(_KERNEL) && !defined(KERNEL))
315                                 printf("proxy tcp checksum failure\n");
316 #endif
317                                 frstats[fin->fin_out].fr_tcpbad++;
318                                 return -1;
319                         }
320
321                         /*
322                          * Don't bother the proxy with these...or in fact,
323                          * should we free up proxy stuff when seen?
324                          */
325                         if ((tcp->th_flags & TH_RST) != 0)
326                                 return 0;
327                 }
328
329                 apr = aps->aps_apr;
330                 err = 0;
331                 if (fin->fin_out != 0) {
332                         if (apr->apr_outpkt != NULL)
333                                 err = (*apr->apr_outpkt)(fin, ip, aps, nat);
334                 } else {
335                         if (apr->apr_inpkt != NULL)
336                                 err = (*apr->apr_inpkt)(fin, ip, aps, nat);
337                 }
338
339                 rv = APR_EXIT(err);
340                 if (rv == 1) {
341 #if PROXY_DEBUG || (!defined(_KERNEL) && !defined(KERNEL))
342                         printf("proxy says bad packet received\n");
343 #endif
344                         return -1;
345                 }
346                 if (rv == 2) {
347 #if PROXY_DEBUG || (!defined(_KERNEL) && !defined(KERNEL))
348                         printf("proxy says free app proxy data\n");
349 #endif
350                         appr_free(apr);
351                         nat->nat_aps = NULL;
352                         return -1;
353                 }
354
355                 if (tcp != NULL) {
356                         err = appr_fixseqack(fin, ip, aps, APR_INC(err));
357 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
358                         if (dosum)
359                                 tcp->th_sum = fr_tcpsum(fin->fin_qfm, ip, tcp);
360 #else
361                         tcp->th_sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp);
362 #endif
363                 }
364                 aps->aps_bytes += ip->ip_len;
365                 aps->aps_pkts++;
366                 return 1;
367         }
368         return 0;
369 }
370
371
372 /*
373  * Search for an proxy by the protocol it is being used with and its name.
374  */
375 aproxy_t *appr_lookup(pr, name)
376 u_int pr;
377 char *name;
378 {
379         aproxy_t *ap;
380
381         for (ap = ap_proxies; ap->apr_p; ap++)
382                 if ((ap->apr_p == pr) &&
383                     !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
384                         ap->apr_ref++;
385                         return ap;
386                 }
387
388         for (ap = ap_proxylist; ap; ap = ap->apr_next)
389                 if ((ap->apr_p == pr) &&
390                     !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
391                         ap->apr_ref++;
392                         return ap;
393                 }
394         return NULL;
395 }
396
397
398 void appr_free(ap)
399 aproxy_t *ap;
400 {
401         ap->apr_ref--;
402 }
403
404
405 void aps_free(aps)
406 ap_session_t *aps;
407 {
408         ap_session_t *a, **ap;
409         aproxy_t *apr;
410
411         if (!aps)
412                 return;
413
414         for (ap = &ap_sess_list; (a = *ap); ap = &a->aps_next)
415                 if (a == aps) {
416                         *ap = a->aps_next;
417                         break;
418                 }
419
420         apr = aps->aps_apr;
421         if ((apr != NULL) && (apr->apr_del != NULL))
422                 (*apr->apr_del)(aps);
423
424         if ((aps->aps_data != NULL) && (aps->aps_psiz != 0))
425                 KFREES(aps->aps_data, aps->aps_psiz);
426         KFREE(aps);
427 }
428
429
430 /*
431  * returns 2 if ack or seq number in TCP header is changed, returns 0 otherwise
432  */
433 static int appr_fixseqack(fin, ip, aps, inc)
434 fr_info_t *fin;
435 ip_t *ip;
436 ap_session_t *aps;
437 int inc;
438 {
439         int sel, ch = 0, out, nlen;
440         u_32_t seq1, seq2;
441         tcphdr_t *tcp;
442         short inc2;
443
444         tcp = (tcphdr_t *)fin->fin_dp;
445         out = fin->fin_out;
446         /*
447          * ip_len has already been adjusted by 'inc'.
448          */
449         nlen = ip->ip_len;
450         nlen -= (ip->ip_hl << 2) + (tcp->th_off << 2);
451
452         inc2 = inc;
453         inc = (int)inc2;
454
455         if (out != 0) {
456                 seq1 = (u_32_t)ntohl(tcp->th_seq);
457                 sel = aps->aps_sel[out];
458
459                 /* switch to other set ? */
460                 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
461                     (seq1 > aps->aps_seqmin[!sel])) {
462 #if PROXY_DEBUG
463                         printf("proxy out switch set seq %d -> %d %x > %x\n",
464                                 sel, !sel, seq1, aps->aps_seqmin[!sel]);
465 #endif
466                         sel = aps->aps_sel[out] = !sel;
467                 }
468
469                 if (aps->aps_seqoff[sel]) {
470                         seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel];
471                         if (seq1 > seq2) {
472                                 seq2 = aps->aps_seqoff[sel];
473                                 seq1 += seq2;
474                                 tcp->th_seq = htonl(seq1);
475                                 ch = 1;
476                         }
477                 }
478
479                 if (inc && (seq1 > aps->aps_seqmin[!sel])) {
480                         aps->aps_seqmin[sel] = seq1 + nlen - 1;
481                         aps->aps_seqoff[sel] = aps->aps_seqoff[sel] + inc;
482 #if PROXY_DEBUG
483                         printf("proxy seq set %d at %x to %d + %d\n", sel,
484                                 aps->aps_seqmin[sel], aps->aps_seqoff[sel],
485                                 inc);
486 #endif
487                 }
488
489                 /***/
490
491                 seq1 = ntohl(tcp->th_ack);
492                 sel = aps->aps_sel[1 - out];
493
494                 /* switch to other set ? */
495                 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
496                     (seq1 > aps->aps_ackmin[!sel])) {
497 #if PROXY_DEBUG
498                         printf("proxy out switch set ack %d -> %d %x > %x\n",
499                                 sel, !sel, seq1, aps->aps_ackmin[!sel]);
500 #endif
501                         sel = aps->aps_sel[1 - out] = !sel;
502                 }
503
504                 if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) {
505                         seq2 = aps->aps_ackoff[sel];
506                         tcp->th_ack = htonl(seq1 - seq2);
507                         ch = 1;
508                 }
509         } else {
510                 seq1 = ntohl(tcp->th_seq);
511                 sel = aps->aps_sel[out];
512
513                 /* switch to other set ? */
514                 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
515                     (seq1 > aps->aps_ackmin[!sel])) {
516 #if PROXY_DEBUG
517                         printf("proxy in switch set ack %d -> %d %x > %x\n",
518                                 sel, !sel, seq1, aps->aps_ackmin[!sel]);
519 #endif
520                         sel = aps->aps_sel[out] = !sel;
521                 }
522
523                 if (aps->aps_ackoff[sel]) {
524                         seq2 = aps->aps_ackmin[sel] - aps->aps_ackoff[sel];
525                         if (seq1 > seq2) {
526                                 seq2 = aps->aps_ackoff[sel];
527                                 seq1 += seq2;
528                                 tcp->th_seq = htonl(seq1);
529                                 ch = 1;
530                         }
531                 }
532
533                 if (inc && (seq1 > aps->aps_ackmin[!sel])) {
534                         aps->aps_ackmin[!sel] = seq1 + nlen - 1;
535                         aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc;
536 #if PROXY_DEBUG
537                         printf("proxy ack set %d at %x to %d + %d\n", !sel,
538                                 aps->aps_seqmin[!sel], aps->aps_seqoff[sel],
539                                 inc);
540 #endif
541                 }
542
543                 /***/
544
545                 seq1 = ntohl(tcp->th_ack);
546                 sel = aps->aps_sel[1 - out];
547
548                 /* switch to other set ? */
549                 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
550                     (seq1 > aps->aps_seqmin[!sel])) {
551 #if PROXY_DEBUG
552                         printf("proxy in switch set seq %d -> %d %x > %x\n",
553                                 sel, !sel, seq1, aps->aps_seqmin[!sel]);
554 #endif
555                         sel = aps->aps_sel[1 - out] = !sel;
556                 }
557
558                 if (aps->aps_seqoff[sel] != 0) {
559 #if PROXY_DEBUG
560                         printf("sel %d seqoff %d seq1 %x seqmin %x\n", sel,
561                                 aps->aps_seqoff[sel], seq1,
562                                 aps->aps_seqmin[sel]);
563 #endif
564                         if (seq1 > aps->aps_seqmin[sel]) {
565                                 seq2 = aps->aps_seqoff[sel];
566                                 tcp->th_ack = htonl(seq1 - seq2);
567                                 ch = 1;
568                         }
569                 }
570         }
571 #if PROXY_DEBUG
572         printf("appr_fixseqack: seq %x ack %x\n", ntohl(tcp->th_seq),
573                 ntohl(tcp->th_ack));
574 #endif
575         return ch ? 2 : 0;
576 }
577
578
579 /*
580  * Initialise hook for kernel application proxies.
581  * Call the initialise routine for all the compiled in kernel proxies.
582  */
583 int appr_init()
584 {
585         aproxy_t *ap;
586         int err = 0;
587
588         for (ap = ap_proxies; ap->apr_p; ap++) {
589                 err = (*ap->apr_init)();
590                 if (err != 0)
591                         break;
592         }
593         return err;
594 }
595
596
597 /*
598  * Unload hook for kernel application proxies.
599  * Call the finialise routine for all the compiled in kernel proxies.
600  */
601 void appr_unload()
602 {
603         aproxy_t *ap;
604
605         for (ap = ap_proxies; ap->apr_p; ap++)
606                 if (ap->apr_fini)
607                         (*ap->apr_fini)();
608         for (ap = ap_proxylist; ap; ap = ap->apr_next)
609                 if (ap->apr_fini)
610                         (*ap->apr_fini)();
611 }