Deleted Added
sdiff udiff text old ( 139255 ) new ( 145522 )
full compact
1/*
2 * Copyright (C) 1997-2002 by Darren Reed.
3 *
4 * See the IPFILTER.LICENCE file for details on licencing.
5 */
6
7#if defined(__FreeBSD__) && defined(KERNEL) && !defined(_KERNEL)
8# define _KERNEL
9#endif
10
11#if defined(__sgi) && (IRIX > 602)
12# include <sys/ptimers.h>
13#endif
14#include <sys/errno.h>
15#include <sys/types.h>
16#include <sys/param.h>
17#include <sys/time.h>
18#include <sys/file.h>
19#if !defined(__FreeBSD_version)
20# include <sys/ioctl.h>
21#endif
22#include <sys/fcntl.h>
23#if !defined(_KERNEL) && !defined(KERNEL)
24# include <stdio.h>
25# include <string.h>
26# include <stdlib.h>
27#endif
28#ifndef linux
29# include <sys/protosw.h>
30#endif
31#include <sys/socket.h>
32#if defined(_KERNEL)
33# if !defined(linux)
34# include <sys/systm.h>
35# else
36# include <linux/string.h>
37# endif
38#endif
39#if !defined(__SVR4) && !defined(__svr4__)
40# ifndef linux
41# include <sys/mbuf.h>
42# endif
43#else
44# include <sys/byteorder.h>
45# ifdef _KERNEL
46# include <sys/dditypes.h>
47# endif
48# include <sys/stream.h>
49# include <sys/kmem.h>
50#endif
51#if __FreeBSD__ > 2

--- 18 unchanged lines hidden (view full) ---

70#include "netinet/ip_fil.h"
71#include "netinet/ip_nat.h"
72#include "netinet/ip_state.h"
73#include "netinet/ip_proxy.h"
74#if (__FreeBSD_version >= 300000)
75# include <sys/malloc.h>
76#endif
77
78#if !defined(lint)
79/* static const char rcsid[] = "@(#)$Id: ip_proxy.c,v 2.9.2.6 2001/07/15 22:06:15 darrenr Exp $"; */
80static const char rcsid[] = "@(#)$FreeBSD: head/sys/contrib/ipfilter/netinet/ip_proxy.c 139255 2004-12-24 09:14:26Z darrenr $";
81#endif
82
83#ifdef USE_MUTEX
84extern KRWLOCK_T ipf_nat, ipf_state;
85#endif
86
87static int appr_fixseqack __P((fr_info_t *, ip_t *, ap_session_t *, int ));
88
89
90#define PROXY_DEBUG 0
91
92#define AP_SESS_SIZE 53
93
94#include "netinet/ip_ftp_pxy.c"
95#if defined(_KERNEL)
96#include "netinet/ip_rcmd_pxy.c"
97#include "netinet/ip_raudio_pxy.c"
98#include "netinet/ip_netbios_pxy.c"
99#include "netinet/ip_ipsec_pxy.c"
100#endif
101
102ap_session_t *ap_sess_tab[AP_SESS_SIZE];
103ap_session_t *ap_sess_list = NULL;
104aproxy_t *ap_proxylist = NULL;
105aproxy_t ap_proxies[] = {
106#ifdef IPF_FTP_PROXY
107 { NULL, "ftp", (char)IPPROTO_TCP, 0, 0, ippr_ftp_init, NULL,
108 ippr_ftp_new, NULL, ippr_ftp_in, ippr_ftp_out, NULL },
109#endif
110#ifdef IPF_RCMD_PROXY
111 { NULL, "rcmd", (char)IPPROTO_TCP, 0, 0, ippr_rcmd_init, NULL,
112 ippr_rcmd_new, NULL, NULL, ippr_rcmd_out, NULL },
113#endif
114#ifdef IPF_RAUDIO_PROXY
115 { NULL, "raudio", (char)IPPROTO_TCP, 0, 0, ippr_raudio_init, NULL,
116 ippr_raudio_new, NULL, ippr_raudio_in, ippr_raudio_out, NULL },
117#endif
118#ifdef IPF_IPSEC_PROXY
119 { NULL, "ipsec", (char)IPPROTO_UDP, 0, 0, ippr_ipsec_init, NULL,
120 ippr_ipsec_new, ippr_ipsec_del, NULL, ippr_ipsec_out,
121 ippr_ipsec_match },
122#endif
123#ifdef IPF_NETBIOS_PROXY
124 { NULL, "netbios", (char)IPPROTO_UDP, 0, 0, ippr_netbios_init, NULL,
125 NULL, NULL, NULL, ippr_netbios_out, NULL },
126#endif
127#ifdef IPF_H323_PROXY
128 { NULL, "h323", (char)IPPROTO_TCP, 0, 0, ippr_h323_init, NULL,
129 ippr_h323_new, ippr_h323_del, ippr_h323_in, ippr_h323_out, NULL },
130 { NULL, "h245", (char)IPPROTO_TCP, 0, 0, ippr_h245_init, NULL,
131 ippr_h245_new, NULL, NULL, ippr_h245_out, NULL },
132#endif
133 { NULL, "", '\0', 0, 0, NULL, NULL, NULL }
134};
135
136
137/*
138 * Dynamically add a new kernel proxy. Ensure that it is unique in the
139 * collection compiled in and dynamically added.
140 */
141int appr_add(ap)
142aproxy_t *ap;
143{
144 aproxy_t *a;
145
146 for (a = ap_proxies; a->apr_p; a++)
147 if ((a->apr_p == ap->apr_p) &&
148 !strncmp(a->apr_label, ap->apr_label,
149 sizeof(ap->apr_label)))
150 return -1;
151
152 for (a = ap_proxylist; a && a->apr_p; a = a->apr_next)
153 if ((a->apr_p == ap->apr_p) &&
154 !strncmp(a->apr_label, ap->apr_label,
155 sizeof(ap->apr_label)))
156 return -1;
157 ap->apr_next = ap_proxylist;
158 ap_proxylist = ap;
159 return (*ap->apr_init)();
160}
161
162
163/*
164 * Delete a proxy that has been added dynamically from those available.
165 * If it is in use, return 1 (do not destroy NOW), not in use 0 or -1
166 * if it cannot be matched.
167 */
168int appr_del(ap)
169aproxy_t *ap;
170{
171 aproxy_t *a, **app;
172
173 for (app = &ap_proxylist; (a = *app); app = &a->apr_next)
174 if (a == ap) {
175 a->apr_flags |= APR_DELETE;
176 *app = a->apr_next;
177 if (ap->apr_ref != 0)
178 return 1;
179 return 0;
180 }
181 return -1;
182}
183
184
185/*
186 * Return 1 if the packet is a good match against a proxy, else 0.
187 */
188int appr_ok(ip, tcp, nat)
189ip_t *ip;
190tcphdr_t *tcp;
191ipnat_t *nat;
192{
193 aproxy_t *apr = nat->in_apr;
194 u_short dport = nat->in_dport;
195
196 if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
197 (ip->ip_p != apr->apr_p))
198 return 0;
199 if (((tcp != NULL) && (tcp->th_dport != dport)) || (!tcp && dport))
200 return 0;
201 return 1;
202}
203
204
205/*
206 * If a proxy has a match function, call that to do extended packet
207 * matching.
208 */
209int appr_match(fin, nat)
210fr_info_t *fin;
211nat_t *nat;
212{
213 aproxy_t *apr;
214 ipnat_t *ipn;
215
216 ipn = nat->nat_ptr;
217 if (ipn == NULL)
218 return -1;
219 apr = ipn->in_apr;
220 if ((apr == NULL) || (apr->apr_flags & APR_DELETE) ||
221 (nat->nat_aps == NULL))
222 return -1;
223 if (apr->apr_match != NULL)
224 if ((*apr->apr_match)(fin, nat->nat_aps, nat) != 0)
225 return -1;
226 return 0;
227}
228
229
230/*
231 * Allocate a new application proxy structure and fill it in with the
232 * relevant details. call the init function once complete, prior to
233 * returning.
234 */
235int appr_new(fin, ip, nat)
236fr_info_t *fin;
237ip_t *ip;
238nat_t *nat;
239{
240 register ap_session_t *aps;
241 aproxy_t *apr;
242
243 if ((nat->nat_ptr == NULL) || (nat->nat_aps != NULL))
244 return -1;
245
246 apr = nat->nat_ptr->in_apr;
247
248 if (!apr || (apr->apr_flags & APR_DELETE) || (ip->ip_p != apr->apr_p))
249 return -1;
250
251 KMALLOC(aps, ap_session_t *);
252 if (!aps)
253 return -1;
254 bzero((char *)aps, sizeof(*aps));
255 aps->aps_p = ip->ip_p;
256 aps->aps_data = NULL;
257 aps->aps_apr = apr;
258 aps->aps_psiz = 0;
259 if (apr->apr_new != NULL)
260 if ((*apr->apr_new)(fin, ip, aps, nat) == -1) {
261 if ((aps->aps_data != NULL) && (aps->aps_psiz != 0)) {
262 KFREES(aps->aps_data, aps->aps_psiz);
263 }
264 KFREE(aps);
265 return -1;
266 }
267 aps->aps_nat = nat;
268 aps->aps_next = ap_sess_list;
269 ap_sess_list = aps;
270 nat->nat_aps = aps;
271
272 return 0;
273}
274
275
276/*
277 * check to see if a packet should be passed through an active proxy routine
278 * if one has been setup for it.
279 */
280int appr_check(ip, fin, nat)
281ip_t *ip;
282fr_info_t *fin;
283nat_t *nat;
284{
285#if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
286 mb_t *m = fin->fin_qfm;
287 int dosum = 1;
288#endif
289 tcphdr_t *tcp = NULL;
290 ap_session_t *aps;
291 aproxy_t *apr;
292 u_32_t sum;
293 short rv;
294 int err;
295
296 aps = nat->nat_aps;
297 if ((aps != NULL) && (aps->aps_p == ip->ip_p)) {
298 if (ip->ip_p == IPPROTO_TCP) {
299 tcp = (tcphdr_t *)fin->fin_dp;
300 /*
301 * verify that the checksum is correct. If not, then
302 * don't do anything with this packet.
303 */
304#if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
305 if (dohwcksum && (m->b_ick_flag == ICK_VALID)) {
306 sum = tcp->th_sum;
307 dosum = 0;
308 }
309 if (dosum)
310 sum = fr_tcpsum(fin->fin_qfm, ip, tcp);
311#else
312 sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp);
313#endif
314 if (sum != tcp->th_sum) {
315#if PROXY_DEBUG || (!defined(_KERNEL) && !defined(KERNEL))
316 printf("proxy tcp checksum failure\n");
317#endif
318 frstats[fin->fin_out].fr_tcpbad++;
319 return -1;
320 }
321
322 /*
323 * Don't bother the proxy with these...or in fact,
324 * should we free up proxy stuff when seen?
325 */
326 if ((tcp->th_flags & TH_RST) != 0)
327 return 0;
328 }
329
330 apr = aps->aps_apr;
331 err = 0;
332 if (fin->fin_out != 0) {
333 if (apr->apr_outpkt != NULL)
334 err = (*apr->apr_outpkt)(fin, ip, aps, nat);
335 } else {
336 if (apr->apr_inpkt != NULL)
337 err = (*apr->apr_inpkt)(fin, ip, aps, nat);
338 }
339
340 rv = APR_EXIT(err);
341 if (rv == 1) {
342#if PROXY_DEBUG || (!defined(_KERNEL) && !defined(KERNEL))
343 printf("proxy says bad packet received\n");
344#endif
345 return -1;
346 }
347 if (rv == 2) {
348#if PROXY_DEBUG || (!defined(_KERNEL) && !defined(KERNEL))
349 printf("proxy says free app proxy data\n");
350#endif
351 appr_free(apr);
352 nat->nat_aps = NULL;
353 return -1;
354 }
355
356 if (tcp != NULL) {
357 err = appr_fixseqack(fin, ip, aps, APR_INC(err));
358#if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
359 if (dosum)
360 tcp->th_sum = fr_tcpsum(fin->fin_qfm, ip, tcp);
361#else
362 tcp->th_sum = fr_tcpsum(*(mb_t **)fin->fin_mp, ip, tcp);
363#endif
364 }
365 aps->aps_bytes += ip->ip_len;
366 aps->aps_pkts++;
367 return 1;
368 }
369 return 0;
370}
371
372
373/*
374 * Search for an proxy by the protocol it is being used with and its name.
375 */
376aproxy_t *appr_lookup(pr, name)
377u_int pr;
378char *name;
379{
380 aproxy_t *ap;
381
382 for (ap = ap_proxies; ap->apr_p; ap++)
383 if ((ap->apr_p == pr) &&
384 !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
385 ap->apr_ref++;
386 return ap;
387 }
388
389 for (ap = ap_proxylist; ap; ap = ap->apr_next)
390 if ((ap->apr_p == pr) &&
391 !strncmp(name, ap->apr_label, sizeof(ap->apr_label))) {
392 ap->apr_ref++;
393 return ap;
394 }
395 return NULL;
396}
397
398
399void appr_free(ap)
400aproxy_t *ap;
401{
402 ap->apr_ref--;

--- 4 unchanged lines hidden (view full) ---

407ap_session_t *aps;
408{
409 ap_session_t *a, **ap;
410 aproxy_t *apr;
411
412 if (!aps)
413 return;
414
415 for (ap = &ap_sess_list; (a = *ap); ap = &a->aps_next)
416 if (a == aps) {
417 *ap = a->aps_next;
418 break;
419 }
420
421 apr = aps->aps_apr;
422 if ((apr != NULL) && (apr->apr_del != NULL))
423 (*apr->apr_del)(aps);

--- 19 unchanged lines hidden (view full) ---

443 short inc2;
444
445 tcp = (tcphdr_t *)fin->fin_dp;
446 out = fin->fin_out;
447 /*
448 * ip_len has already been adjusted by 'inc'.
449 */
450 nlen = ip->ip_len;
451 nlen -= (ip->ip_hl << 2) + (tcp->th_off << 2);
452
453 inc2 = inc;
454 inc = (int)inc2;
455
456 if (out != 0) {
457 seq1 = (u_32_t)ntohl(tcp->th_seq);
458 sel = aps->aps_sel[out];
459
460 /* switch to other set ? */
461 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
462 (seq1 > aps->aps_seqmin[!sel])) {
463#if PROXY_DEBUG
464 printf("proxy out switch set seq %d -> %d %x > %x\n",
465 sel, !sel, seq1, aps->aps_seqmin[!sel]);
466#endif
467 sel = aps->aps_sel[out] = !sel;
468 }
469
470 if (aps->aps_seqoff[sel]) {
471 seq2 = aps->aps_seqmin[sel] - aps->aps_seqoff[sel];
472 if (seq1 > seq2) {
473 seq2 = aps->aps_seqoff[sel];
474 seq1 += seq2;
475 tcp->th_seq = htonl(seq1);
476 ch = 1;
477 }
478 }
479
480 if (inc && (seq1 > aps->aps_seqmin[!sel])) {
481 aps->aps_seqmin[sel] = seq1 + nlen - 1;
482 aps->aps_seqoff[sel] = aps->aps_seqoff[sel] + inc;
483#if PROXY_DEBUG
484 printf("proxy seq set %d at %x to %d + %d\n", sel,
485 aps->aps_seqmin[sel], aps->aps_seqoff[sel],
486 inc);
487#endif
488 }
489
490 /***/
491
492 seq1 = ntohl(tcp->th_ack);
493 sel = aps->aps_sel[1 - out];
494
495 /* switch to other set ? */
496 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
497 (seq1 > aps->aps_ackmin[!sel])) {
498#if PROXY_DEBUG
499 printf("proxy out switch set ack %d -> %d %x > %x\n",
500 sel, !sel, seq1, aps->aps_ackmin[!sel]);
501#endif
502 sel = aps->aps_sel[1 - out] = !sel;
503 }
504
505 if (aps->aps_ackoff[sel] && (seq1 > aps->aps_ackmin[sel])) {
506 seq2 = aps->aps_ackoff[sel];
507 tcp->th_ack = htonl(seq1 - seq2);
508 ch = 1;
509 }
510 } else {
511 seq1 = ntohl(tcp->th_seq);
512 sel = aps->aps_sel[out];
513
514 /* switch to other set ? */
515 if ((aps->aps_ackmin[!sel] > aps->aps_ackmin[sel]) &&
516 (seq1 > aps->aps_ackmin[!sel])) {
517#if PROXY_DEBUG
518 printf("proxy in switch set ack %d -> %d %x > %x\n",
519 sel, !sel, seq1, aps->aps_ackmin[!sel]);
520#endif
521 sel = aps->aps_sel[out] = !sel;
522 }
523
524 if (aps->aps_ackoff[sel]) {
525 seq2 = aps->aps_ackmin[sel] - aps->aps_ackoff[sel];
526 if (seq1 > seq2) {
527 seq2 = aps->aps_ackoff[sel];
528 seq1 += seq2;
529 tcp->th_seq = htonl(seq1);
530 ch = 1;
531 }
532 }
533
534 if (inc && (seq1 > aps->aps_ackmin[!sel])) {
535 aps->aps_ackmin[!sel] = seq1 + nlen - 1;
536 aps->aps_ackoff[!sel] = aps->aps_ackoff[sel] + inc;
537#if PROXY_DEBUG
538 printf("proxy ack set %d at %x to %d + %d\n", !sel,
539 aps->aps_seqmin[!sel], aps->aps_seqoff[sel],
540 inc);
541#endif
542 }
543
544 /***/
545
546 seq1 = ntohl(tcp->th_ack);
547 sel = aps->aps_sel[1 - out];
548
549 /* switch to other set ? */
550 if ((aps->aps_seqmin[!sel] > aps->aps_seqmin[sel]) &&
551 (seq1 > aps->aps_seqmin[!sel])) {
552#if PROXY_DEBUG
553 printf("proxy in switch set seq %d -> %d %x > %x\n",
554 sel, !sel, seq1, aps->aps_seqmin[!sel]);
555#endif
556 sel = aps->aps_sel[1 - out] = !sel;
557 }
558
559 if (aps->aps_seqoff[sel] != 0) {
560#if PROXY_DEBUG
561 printf("sel %d seqoff %d seq1 %x seqmin %x\n", sel,
562 aps->aps_seqoff[sel], seq1,
563 aps->aps_seqmin[sel]);
564#endif
565 if (seq1 > aps->aps_seqmin[sel]) {
566 seq2 = aps->aps_seqoff[sel];
567 tcp->th_ack = htonl(seq1 - seq2);
568 ch = 1;
569 }
570 }
571 }
572#if PROXY_DEBUG
573 printf("appr_fixseqack: seq %x ack %x\n", ntohl(tcp->th_seq),
574 ntohl(tcp->th_ack));
575#endif
576 return ch ? 2 : 0;
577}
578
579
580/*
581 * Initialise hook for kernel application proxies.
582 * Call the initialise routine for all the compiled in kernel proxies.
583 */
584int appr_init()
585{
586 aproxy_t *ap;
587 int err = 0;
588
589 for (ap = ap_proxies; ap->apr_p; ap++) {
590 err = (*ap->apr_init)();
591 if (err != 0)
592 break;
593 }
594 return err;
595}
596
597
598/*
599 * Unload hook for kernel application proxies.
600 * Call the finialise routine for all the compiled in kernel proxies.
601 */
602void appr_unload()
603{
604 aproxy_t *ap;
605
606 for (ap = ap_proxies; ap->apr_p; ap++)
607 if (ap->apr_fini)
608 (*ap->apr_fini)();
609 for (ap = ap_proxylist; ap; ap = ap->apr_next)
610 if (ap->apr_fini)
611 (*ap->apr_fini)();
612}