Home | History | Annotate | Line # | Download | only in netinet
ip_scan.c revision 1.3
      1  1.3   darrenr /*	$NetBSD: ip_scan.c,v 1.3 2012/07/22 14:27:51 darrenr Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.3   darrenr  * Copyright (C) 2012 by Darren Reed.
      5  1.1  christos  *
      6  1.1  christos  * See the IPFILTER.LICENCE file for details on licencing.
      7  1.1  christos  */
      8  1.1  christos #if defined(KERNEL) || defined(_KERNEL)
      9  1.1  christos # undef KERNEL
     10  1.1  christos # undef _KERNEL
     11  1.1  christos # define        KERNEL	1
     12  1.1  christos # define        _KERNEL	1
     13  1.1  christos #endif
     14  1.1  christos #include <sys/param.h>
     15  1.1  christos #if defined(__hpux) && (HPUXREV >= 1111) && !defined(_KERNEL)
     16  1.1  christos # include <sys/kern_svcs.h>
     17  1.1  christos #endif
     18  1.1  christos #include <sys/types.h>
     19  1.1  christos #include <sys/time.h>
     20  1.1  christos #include <sys/errno.h>
     21  1.1  christos #if !defined(_KERNEL)
     22  1.1  christos # include <stdlib.h>
     23  1.1  christos # include <string.h>
     24  1.1  christos # define _KERNEL
     25  1.1  christos # ifdef __OpenBSD__
     26  1.1  christos struct file;
     27  1.1  christos # endif
     28  1.1  christos # include <sys/uio.h>
     29  1.1  christos # undef _KERNEL
     30  1.1  christos #else
     31  1.1  christos # include <sys/systm.h>
     32  1.1  christos # if !defined(__svr4__) && !defined(__SVR4)
     33  1.1  christos #  include <sys/mbuf.h>
     34  1.1  christos # endif
     35  1.1  christos #endif
     36  1.1  christos #include <sys/socket.h>
     37  1.1  christos #if !defined(__hpux) && !defined(__osf__) && !defined(linux) && !defined(AIX)
     38  1.1  christos # include <sys/ioccom.h>
     39  1.1  christos #endif
     40  1.1  christos #ifdef __FreeBSD__
     41  1.1  christos # include <sys/filio.h>
     42  1.1  christos # include <sys/malloc.h>
     43  1.1  christos #else
     44  1.1  christos # include <sys/ioctl.h>
     45  1.1  christos #endif
     46  1.1  christos 
     47  1.1  christos #include <netinet/in.h>
     48  1.1  christos #include <netinet/in_systm.h>
     49  1.1  christos #include <netinet/ip.h>
     50  1.1  christos #include <netinet/tcp.h>
     51  1.1  christos 
     52  1.1  christos #include <net/if.h>
     53  1.1  christos 
     54  1.1  christos 
     55  1.1  christos #include "netinet/ip_compat.h"
     56  1.1  christos #include "netinet/ip_fil.h"
     57  1.1  christos #include "netinet/ip_state.h"
     58  1.1  christos #include "netinet/ip_scan.h"
     59  1.1  christos /* END OF INCLUDES */
     60  1.1  christos 
     61  1.1  christos #if !defined(lint)
     62  1.2  christos #if defined(__NetBSD__)
     63  1.2  christos #include <sys/cdefs.h>
     64  1.3   darrenr __KERNEL_RCSID(0, "$NetBSD: ip_scan.c,v 1.3 2012/07/22 14:27:51 darrenr Exp $");
     65  1.2  christos #else
     66  1.1  christos static const char sccsid[] = "@(#)ip_state.c	1.8 6/5/96 (C) 1993-2000 Darren Reed";
     67  1.3   darrenr static const char rcsid[] = "@(#)Id: ip_scan.c,v 1.1.1.2 2012/07/22 13:45:34 darrenr Exp";
     68  1.2  christos #endif
     69  1.1  christos #endif
     70  1.1  christos 
     71  1.1  christos #ifdef	IPFILTER_SCAN	/* endif at bottom of file */
     72  1.1  christos 
     73  1.1  christos 
     74  1.1  christos ipscan_t	*ipf_scan_list = NULL,
     75  1.1  christos 		*ipf_scan_tail = NULL;
     76  1.1  christos ipscanstat_t	ipf_scan_stat;
     77  1.1  christos # ifdef USE_MUTEXES
     78  1.1  christos ipfrwlock_t	ipf_scan_rwlock;
     79  1.1  christos # endif
     80  1.1  christos 
     81  1.1  christos # ifndef isalpha
     82  1.1  christos #  define	isalpha(x)	(((x) >= 'A' && 'Z' >= (x)) || \
     83  1.1  christos 				 ((x) >= 'a' && 'z' >= (x)))
     84  1.1  christos # endif
     85  1.1  christos 
     86  1.1  christos 
     87  1.2  christos int ipf_scan_add(void *);
     88  1.2  christos int ipf_scan_remove(void *);
     89  1.2  christos struct ipscan *ipf_scan_lookup(char *);
     90  1.2  christos int ipf_scan_matchstr(sinfo_t *, char *, int);
     91  1.2  christos int ipf_scan_matchisc(ipscan_t *, ipstate_t *, int, int, int *);
     92  1.2  christos int ipf_scan_match(ipstate_t *);
     93  1.1  christos 
     94  1.1  christos static int	ipf_scan_inited = 0;
     95  1.1  christos 
     96  1.1  christos 
     97  1.1  christos int
     98  1.1  christos ipf_scan_init()
     99  1.1  christos {
    100  1.1  christos 	RWLOCK_INIT(&ipf_scan_rwlock, "ip scan rwlock");
    101  1.1  christos 	ipf_scan_inited = 1;
    102  1.1  christos 	return 0;
    103  1.1  christos }
    104  1.1  christos 
    105  1.1  christos 
    106  1.1  christos void
    107  1.1  christos ipf_scan_unload(void *arg)
    108  1.1  christos {
    109  1.1  christos 	if (ipf_scan_inited == 1) {
    110  1.1  christos 		RW_DESTROY(&ipf_scan_rwlock);
    111  1.1  christos 		ipf_scan_inited = 0;
    112  1.1  christos 	}
    113  1.1  christos }
    114  1.1  christos 
    115  1.1  christos 
    116  1.1  christos int
    117  1.1  christos ipf_scan_add(data)
    118  1.2  christos 	void *data;
    119  1.1  christos {
    120  1.1  christos 	ipscan_t *i, *isc;
    121  1.1  christos 	int err;
    122  1.1  christos 
    123  1.1  christos 	KMALLOC(isc, ipscan_t *);
    124  1.1  christos 	if (!isc) {
    125  1.1  christos 		ipf_interror = 90001;
    126  1.1  christos 		return ENOMEM;
    127  1.1  christos 	}
    128  1.1  christos 
    129  1.1  christos 	err = copyinptr(data, isc, sizeof(*isc));
    130  1.1  christos 	if (err) {
    131  1.1  christos 		KFREE(isc);
    132  1.1  christos 		return err;
    133  1.1  christos 	}
    134  1.1  christos 
    135  1.1  christos 	WRITE_ENTER(&ipf_scan_rwlock);
    136  1.1  christos 
    137  1.1  christos 	i = ipf_scan_lookup(isc->ipsc_tag);
    138  1.1  christos 	if (i != NULL) {
    139  1.1  christos 		RWLOCK_EXIT(&ipf_scan_rwlock);
    140  1.1  christos 		KFREE(isc);
    141  1.1  christos 		ipf_interror = 90002;
    142  1.1  christos 		return EEXIST;
    143  1.1  christos 	}
    144  1.1  christos 
    145  1.1  christos 	if (ipf_scan_tail) {
    146  1.1  christos 		ipf_scan_tail->ipsc_next = isc;
    147  1.1  christos 		isc->ipsc_pnext = &ipf_scan_tail->ipsc_next;
    148  1.1  christos 		ipf_scan_tail = isc;
    149  1.1  christos 	} else {
    150  1.1  christos 		ipf_scan_list = isc;
    151  1.1  christos 		ipf_scan_tail = isc;
    152  1.1  christos 		isc->ipsc_pnext = &ipf_scan_list;
    153  1.1  christos 	}
    154  1.1  christos 	isc->ipsc_next = NULL;
    155  1.1  christos 
    156  1.1  christos 	isc->ipsc_hits = 0;
    157  1.1  christos 	isc->ipsc_fref = 0;
    158  1.1  christos 	isc->ipsc_sref = 0;
    159  1.1  christos 	isc->ipsc_active = 0;
    160  1.1  christos 
    161  1.1  christos 	ipf_scan_stat.iscs_entries++;
    162  1.1  christos 	RWLOCK_EXIT(&ipf_scan_rwlock);
    163  1.1  christos 	return 0;
    164  1.1  christos }
    165  1.1  christos 
    166  1.1  christos 
    167  1.1  christos int
    168  1.1  christos ipf_scan_remove(data)
    169  1.2  christos 	void *data;
    170  1.1  christos {
    171  1.1  christos 	ipscan_t isc, *i;
    172  1.1  christos 	int err;
    173  1.1  christos 
    174  1.1  christos 	err = copyinptr(data, &isc, sizeof(isc));
    175  1.1  christos 	if (err)
    176  1.1  christos 		return err;
    177  1.1  christos 
    178  1.1  christos 	WRITE_ENTER(&ipf_scan_rwlock);
    179  1.1  christos 
    180  1.1  christos 	i = ipf_scan_lookup(isc.ipsc_tag);
    181  1.1  christos 	if (i == NULL)
    182  1.1  christos 		err = ENOENT;
    183  1.1  christos 	else {
    184  1.1  christos 		if (i->ipsc_fref) {
    185  1.1  christos 			RWLOCK_EXIT(&ipf_scan_rwlock);
    186  1.1  christos 			ipf_interror = 90003;
    187  1.1  christos 			return EBUSY;
    188  1.1  christos 		}
    189  1.1  christos 
    190  1.1  christos 		*i->ipsc_pnext = i->ipsc_next;
    191  1.1  christos 		if (i->ipsc_next)
    192  1.1  christos 			i->ipsc_next->ipsc_pnext = i->ipsc_pnext;
    193  1.1  christos 		else {
    194  1.1  christos 			if (i->ipsc_pnext == &ipf_scan_list)
    195  1.1  christos 				ipf_scan_tail = NULL;
    196  1.1  christos 			else
    197  1.1  christos 				ipf_scan_tail = *(*i->ipsc_pnext)->ipsc_pnext;
    198  1.1  christos 		}
    199  1.1  christos 
    200  1.1  christos 		ipf_scan_stat.iscs_entries--;
    201  1.1  christos 		KFREE(i);
    202  1.1  christos 	}
    203  1.1  christos 	RWLOCK_EXIT(&ipf_scan_rwlock);
    204  1.1  christos 	return err;
    205  1.1  christos }
    206  1.1  christos 
    207  1.1  christos 
    208  1.1  christos struct ipscan *
    209  1.1  christos ipf_scan_lookup(tag)
    210  1.1  christos 	char *tag;
    211  1.1  christos {
    212  1.1  christos 	ipscan_t *i;
    213  1.1  christos 
    214  1.1  christos 	for (i = ipf_scan_list; i; i = i->ipsc_next)
    215  1.1  christos 		if (!strcmp(i->ipsc_tag, tag))
    216  1.1  christos 			return i;
    217  1.1  christos 	return NULL;
    218  1.1  christos }
    219  1.1  christos 
    220  1.1  christos 
    221  1.1  christos int
    222  1.1  christos ipf_scan_attachfr(fr)
    223  1.1  christos 	struct frentry *fr;
    224  1.1  christos {
    225  1.1  christos 	ipscan_t *i;
    226  1.1  christos 
    227  1.1  christos 	if (fr->fr_isctag != -1) {
    228  1.1  christos 		READ_ENTER(&ipf_scan_rwlock);
    229  1.1  christos 		i = ipf_scan_lookup(fr->fr_isctag + fr->fr_names);
    230  1.1  christos 		if (i != NULL) {
    231  1.1  christos 			ATOMIC_INC32(i->ipsc_fref);
    232  1.1  christos 		}
    233  1.1  christos 		RWLOCK_EXIT(&ipf_scan_rwlock);
    234  1.1  christos 		if (i == NULL) {
    235  1.1  christos 			ipf_interror = 90004;
    236  1.1  christos 			return ENOENT;
    237  1.1  christos 		}
    238  1.1  christos 		fr->fr_isc = i;
    239  1.1  christos 	}
    240  1.1  christos 	return 0;
    241  1.1  christos }
    242  1.1  christos 
    243  1.1  christos 
    244  1.1  christos int
    245  1.1  christos ipf_scan_attachis(is)
    246  1.1  christos 	struct ipstate *is;
    247  1.1  christos {
    248  1.1  christos 	frentry_t *fr;
    249  1.1  christos 	ipscan_t *i;
    250  1.1  christos 
    251  1.1  christos 	READ_ENTER(&ipf_scan_rwlock);
    252  1.1  christos 	fr = is->is_rule;
    253  1.1  christos 	if (fr != NULL) {
    254  1.1  christos 		i = fr->fr_isc;
    255  1.1  christos 		if ((i != NULL) && (i != (ipscan_t *)-1)) {
    256  1.1  christos 			is->is_isc = i;
    257  1.1  christos 			ATOMIC_INC32(i->ipsc_sref);
    258  1.1  christos 			if (i->ipsc_clen)
    259  1.1  christos 				is->is_flags |= IS_SC_CLIENT;
    260  1.1  christos 			else
    261  1.1  christos 				is->is_flags |= IS_SC_MATCHC;
    262  1.1  christos 			if (i->ipsc_slen)
    263  1.1  christos 				is->is_flags |= IS_SC_SERVER;
    264  1.1  christos 			else
    265  1.1  christos 				is->is_flags |= IS_SC_MATCHS;
    266  1.1  christos 		}
    267  1.1  christos 	}
    268  1.1  christos 	RWLOCK_EXIT(&ipf_scan_rwlock);
    269  1.1  christos 	return 0;
    270  1.1  christos }
    271  1.1  christos 
    272  1.1  christos 
    273  1.1  christos int
    274  1.1  christos ipf_scan_detachfr(fr)
    275  1.1  christos 	struct frentry *fr;
    276  1.1  christos {
    277  1.1  christos 	ipscan_t *i;
    278  1.1  christos 
    279  1.1  christos 	i = fr->fr_isc;
    280  1.1  christos 	if (i != NULL) {
    281  1.1  christos 		ATOMIC_DEC32(i->ipsc_fref);
    282  1.1  christos 	}
    283  1.1  christos 	return 0;
    284  1.1  christos }
    285  1.1  christos 
    286  1.1  christos 
    287  1.1  christos int
    288  1.1  christos ipf_scan_detachis(is)
    289  1.1  christos 	struct ipstate *is;
    290  1.1  christos {
    291  1.1  christos 	ipscan_t *i;
    292  1.1  christos 
    293  1.1  christos 	READ_ENTER(&ipf_scan_rwlock);
    294  1.1  christos 	if ((i = is->is_isc) && (i != (ipscan_t *)-1)) {
    295  1.1  christos 		ATOMIC_DEC32(i->ipsc_sref);
    296  1.1  christos 		is->is_isc = NULL;
    297  1.1  christos 		is->is_flags &= ~(IS_SC_CLIENT|IS_SC_SERVER);
    298  1.1  christos 	}
    299  1.1  christos 	RWLOCK_EXIT(&ipf_scan_rwlock);
    300  1.1  christos 	return 0;
    301  1.1  christos }
    302  1.1  christos 
    303  1.1  christos 
    304  1.1  christos /*
    305  1.1  christos  * 'string' compare for scanning
    306  1.1  christos  */
    307  1.1  christos int
    308  1.1  christos ipf_scan_matchstr(sp, str, n)
    309  1.1  christos 	sinfo_t *sp;
    310  1.1  christos 	char *str;
    311  1.1  christos 	int n;
    312  1.1  christos {
    313  1.1  christos 	char *s, *t, *up;
    314  1.1  christos 	int i = n;
    315  1.1  christos 
    316  1.1  christos 	if (i > sp->s_len)
    317  1.1  christos 		i = sp->s_len;
    318  1.1  christos 	up = str;
    319  1.1  christos 
    320  1.1  christos 	for (s = sp->s_txt, t = sp->s_msk; i; i--, s++, t++, up++)
    321  1.1  christos 		switch ((int)*t)
    322  1.1  christos 		{
    323  1.1  christos 		case '.' :
    324  1.1  christos 			if (*s != *up)
    325  1.1  christos 				return 1;
    326  1.1  christos 			break;
    327  1.1  christos 		case '?' :
    328  1.1  christos 			if (!ISALPHA(*up) || ((*s & 0x5f) != (*up & 0x5f)))
    329  1.1  christos 				return 1;
    330  1.1  christos 			break;
    331  1.1  christos 		case '*' :
    332  1.1  christos 			break;
    333  1.1  christos 		}
    334  1.1  christos 	return 0;
    335  1.1  christos }
    336  1.1  christos 
    337  1.1  christos 
    338  1.1  christos /*
    339  1.1  christos  * Returns 3 if both server and client match, 2 if just server,
    340  1.1  christos  * 1 if just client
    341  1.1  christos  */
    342  1.1  christos int
    343  1.1  christos ipf_scan_matchisc(isc, is, cl, sl, maxm)
    344  1.1  christos 	ipscan_t *isc;
    345  1.1  christos 	ipstate_t *is;
    346  1.1  christos 	int cl, sl, maxm[2];
    347  1.1  christos {
    348  1.1  christos 	int i, j, k, n, ret = 0, flags;
    349  1.1  christos 
    350  1.1  christos 	flags = is->is_flags;
    351  1.1  christos 
    352  1.1  christos 	/*
    353  1.1  christos 	 * If we've already matched more than what is on offer, then
    354  1.1  christos 	 * assume we have a better match already and forget this one.
    355  1.1  christos 	 */
    356  1.1  christos 	if (maxm != NULL) {
    357  1.1  christos 		if (isc->ipsc_clen < maxm[0])
    358  1.1  christos 			return 0;
    359  1.1  christos 		if (isc->ipsc_slen < maxm[1])
    360  1.1  christos 			return 0;
    361  1.1  christos 		j = maxm[0];
    362  1.1  christos 		k = maxm[1];
    363  1.1  christos 	} else {
    364  1.1  christos 		j = 0;
    365  1.1  christos 		k = 0;
    366  1.1  christos 	}
    367  1.1  christos 
    368  1.1  christos 	if (!isc->ipsc_clen)
    369  1.1  christos 		ret = 1;
    370  1.1  christos 	else if (((flags & (IS_SC_MATCHC|IS_SC_CLIENT)) == IS_SC_CLIENT) &&
    371  1.1  christos 		 cl && isc->ipsc_clen) {
    372  1.1  christos 		i = 0;
    373  1.1  christos 		n = MIN(cl, isc->ipsc_clen);
    374  1.1  christos 		if ((n > 0) && (!maxm || (n >= maxm[1]))) {
    375  1.1  christos 			if (!ipf_scan_matchstr(&isc->ipsc_cl,
    376  1.1  christos 					       is->is_sbuf[0], n)) {
    377  1.1  christos 				i++;
    378  1.1  christos 				ret |= 1;
    379  1.1  christos 				if (n > j)
    380  1.1  christos 					j = n;
    381  1.1  christos 			}
    382  1.1  christos 		}
    383  1.1  christos 	}
    384  1.1  christos 
    385  1.1  christos 	if (!isc->ipsc_slen)
    386  1.1  christos 		ret |= 2;
    387  1.1  christos 	else if (((flags & (IS_SC_MATCHS|IS_SC_SERVER)) == IS_SC_SERVER) &&
    388  1.1  christos 		 sl && isc->ipsc_slen) {
    389  1.1  christos 		i = 0;
    390  1.1  christos 		n = MIN(cl, isc->ipsc_slen);
    391  1.1  christos 		if ((n > 0) && (!maxm || (n >= maxm[1]))) {
    392  1.1  christos 			if (!ipf_scan_matchstr(&isc->ipsc_sl,
    393  1.1  christos 					       is->is_sbuf[1], n)) {
    394  1.1  christos 				i++;
    395  1.1  christos 				ret |= 2;
    396  1.1  christos 				if (n > k)
    397  1.1  christos 					k = n;
    398  1.1  christos 			}
    399  1.1  christos 		}
    400  1.1  christos 	}
    401  1.1  christos 
    402  1.1  christos 	if (maxm && (ret == 3)) {
    403  1.1  christos 		maxm[0] = j;
    404  1.1  christos 		maxm[1] = k;
    405  1.1  christos 	}
    406  1.1  christos 	return ret;
    407  1.1  christos }
    408  1.1  christos 
    409  1.1  christos 
    410  1.1  christos int
    411  1.1  christos ipf_scan_match(is)
    412  1.1  christos 	ipstate_t *is;
    413  1.1  christos {
    414  1.1  christos 	int i, j, k, n, cl, sl, maxm[2];
    415  1.1  christos 	ipscan_t *isc, *lm;
    416  1.1  christos 	tcpdata_t *t;
    417  1.1  christos 
    418  1.1  christos 	for (cl = 0, n = is->is_smsk[0]; n & 1; n >>= 1)
    419  1.1  christos 		cl++;
    420  1.1  christos 	for (sl = 0, n = is->is_smsk[1]; n & 1; n >>= 1)
    421  1.1  christos 		sl++;
    422  1.1  christos 
    423  1.1  christos 	j = 0;
    424  1.1  christos 	isc = is->is_isc;
    425  1.1  christos 	if (isc != NULL) {
    426  1.1  christos 		/*
    427  1.1  christos 		 * Known object to scan for.
    428  1.1  christos 		 */
    429  1.1  christos 		i = ipf_scan_matchisc(isc, is, cl, sl, NULL);
    430  1.1  christos 		if (i & 1) {
    431  1.1  christos 			is->is_flags |= IS_SC_MATCHC;
    432  1.1  christos 			is->is_flags &= ~IS_SC_CLIENT;
    433  1.1  christos 		} else if (cl >= isc->ipsc_clen)
    434  1.1  christos 			is->is_flags &= ~IS_SC_CLIENT;
    435  1.1  christos 		if (i & 2) {
    436  1.1  christos 			is->is_flags |= IS_SC_MATCHS;
    437  1.1  christos 			is->is_flags &= ~IS_SC_SERVER;
    438  1.1  christos 		} else if (sl >= isc->ipsc_slen)
    439  1.1  christos 			is->is_flags &= ~IS_SC_SERVER;
    440  1.1  christos 	} else {
    441  1.1  christos 		i = 0;
    442  1.1  christos 		lm = NULL;
    443  1.1  christos 		maxm[0] = 0;
    444  1.1  christos 		maxm[1] = 0;
    445  1.1  christos 		for (k = 0, isc = ipf_scan_list; isc; isc = isc->ipsc_next) {
    446  1.1  christos 			i = ipf_scan_matchisc(isc, is, cl, sl, maxm);
    447  1.1  christos 			if (i) {
    448  1.1  christos 				/*
    449  1.1  christos 				 * We only want to remember the best match
    450  1.1  christos 				 * and the number of times we get a best
    451  1.1  christos 				 * match.
    452  1.1  christos 				 */
    453  1.1  christos 				if ((j == 3) && (i < 3))
    454  1.1  christos 					continue;
    455  1.1  christos 				if ((i == 3) && (j != 3))
    456  1.1  christos 					k = 1;
    457  1.1  christos 				else
    458  1.1  christos 					k++;
    459  1.1  christos 				j = i;
    460  1.1  christos 				lm = isc;
    461  1.1  christos 			}
    462  1.1  christos 		}
    463  1.1  christos 		if (k == 1)
    464  1.1  christos 			isc = lm;
    465  1.1  christos 		if (isc == NULL)
    466  1.1  christos 			return 0;
    467  1.1  christos 
    468  1.1  christos 		/*
    469  1.1  christos 		 * No matches or partial matches, so reset the respective
    470  1.1  christos 		 * search flag.
    471  1.1  christos 		 */
    472  1.1  christos 		if (!(j & 1))
    473  1.1  christos 			is->is_flags &= ~IS_SC_CLIENT;
    474  1.1  christos 
    475  1.1  christos 		if (!(j & 2))
    476  1.1  christos 			is->is_flags &= ~IS_SC_SERVER;
    477  1.1  christos 
    478  1.1  christos 		/*
    479  1.1  christos 		 * If we found the best match, then set flags appropriately.
    480  1.1  christos 		 */
    481  1.1  christos 		if ((j == 3) && (k == 1)) {
    482  1.1  christos 			is->is_flags &= ~(IS_SC_SERVER|IS_SC_CLIENT);
    483  1.1  christos 			is->is_flags |= (IS_SC_MATCHS|IS_SC_MATCHC);
    484  1.1  christos 		}
    485  1.1  christos 	}
    486  1.1  christos 
    487  1.1  christos 	/*
    488  1.1  christos 	 * If the acknowledged side of a connection has moved past the data in
    489  1.1  christos 	 * which we are interested, then reset respective flag.
    490  1.1  christos 	 */
    491  1.1  christos 	t = &is->is_tcp.ts_data[0];
    492  1.1  christos 	if (t->td_end > is->is_s0[0] + 15)
    493  1.1  christos 		is->is_flags &= ~IS_SC_CLIENT;
    494  1.1  christos 
    495  1.1  christos 	t = &is->is_tcp.ts_data[1];
    496  1.1  christos 	if (t->td_end > is->is_s0[1] + 15)
    497  1.1  christos 		is->is_flags &= ~IS_SC_SERVER;
    498  1.1  christos 
    499  1.1  christos 	/*
    500  1.1  christos 	 * Matching complete ?
    501  1.1  christos 	 */
    502  1.1  christos 	j = ISC_A_NONE;
    503  1.1  christos 	if ((is->is_flags & IS_SC_MATCHALL) == IS_SC_MATCHALL) {
    504  1.1  christos 		j = isc->ipsc_action;
    505  1.1  christos 		ipf_scan_stat.iscs_acted++;
    506  1.1  christos 	} else if ((is->is_isc != NULL) &&
    507  1.1  christos 		   ((is->is_flags & IS_SC_MATCHALL) != IS_SC_MATCHALL) &&
    508  1.1  christos 		   !(is->is_flags & (IS_SC_CLIENT|IS_SC_SERVER))) {
    509  1.1  christos 		/*
    510  1.1  christos 		 * Matching failed...
    511  1.1  christos 		 */
    512  1.1  christos 		j = isc->ipsc_else;
    513  1.1  christos 		ipf_scan_stat.iscs_else++;
    514  1.1  christos 	}
    515  1.1  christos 
    516  1.1  christos 	switch (j)
    517  1.1  christos 	{
    518  1.1  christos 	case  ISC_A_CLOSE :
    519  1.1  christos 		/*
    520  1.1  christos 		 * If as a result of a successful match we are to
    521  1.1  christos 		 * close a connection, change the "keep state" info.
    522  1.1  christos 		 * to block packets and generate TCP RST's.
    523  1.1  christos 		 */
    524  1.1  christos 		is->is_pass &= ~FR_RETICMP;
    525  1.1  christos 		is->is_pass |= FR_RETRST;
    526  1.1  christos 		break;
    527  1.1  christos 	default :
    528  1.1  christos 		break;
    529  1.1  christos 	}
    530  1.1  christos 
    531  1.1  christos 	return i;
    532  1.1  christos }
    533  1.1  christos 
    534  1.1  christos 
    535  1.1  christos /*
    536  1.1  christos  * check if a packet matches what we're scanning for
    537  1.1  christos  */
    538  1.1  christos int
    539  1.1  christos ipf_scan_packet(fin, is)
    540  1.1  christos 	fr_info_t *fin;
    541  1.1  christos 	ipstate_t *is;
    542  1.1  christos {
    543  1.1  christos 	int i, j, rv, dlen, off, thoff;
    544  1.1  christos 	u_32_t seq, s0;
    545  1.1  christos 	tcphdr_t *tcp;
    546  1.1  christos 
    547  1.1  christos 	rv = !IP6_EQ(&fin->fin_fi.fi_src, &is->is_src);
    548  1.1  christos 	tcp = fin->fin_dp;
    549  1.1  christos 	seq = ntohl(tcp->th_seq);
    550  1.1  christos 
    551  1.1  christos 	if (!is->is_s0[rv])
    552  1.1  christos 		return 1;
    553  1.1  christos 
    554  1.1  christos 	/*
    555  1.1  christos 	 * check if this packet has more data that falls within the first
    556  1.1  christos 	 * 16 bytes sent in either direction.
    557  1.1  christos 	 */
    558  1.1  christos 	s0 = is->is_s0[rv];
    559  1.1  christos 	off = seq - s0;
    560  1.1  christos 	if ((off > 15) || (off < 0))
    561  1.1  christos 		return 1;
    562  1.1  christos 	thoff = TCP_OFF(tcp) << 2;
    563  1.1  christos 	dlen = fin->fin_dlen - thoff;
    564  1.1  christos 	if (dlen <= 0)
    565  1.1  christos 		return 1;
    566  1.1  christos 	if (dlen > 16)
    567  1.1  christos 		dlen = 16;
    568  1.1  christos 	if (off + dlen > 16)
    569  1.1  christos 		dlen = 16 - off;
    570  1.1  christos 
    571  1.1  christos 	j = 0xffff >> (16 - dlen);
    572  1.1  christos 	i = (0xffff & j) << off;
    573  1.1  christos #ifdef _KERNEL
    574  1.1  christos 	COPYDATA(*(mb_t **)fin->fin_mp, fin->fin_plen - fin->fin_dlen + thoff,
    575  1.2  christos 		 dlen, (void *)is->is_sbuf[rv] + off);
    576  1.1  christos #endif
    577  1.1  christos 	is->is_smsk[rv] |= i;
    578  1.1  christos 	for (j = 0, i = is->is_smsk[rv]; i & 1; i >>= 1)
    579  1.1  christos 		j++;
    580  1.1  christos 	if (j == 0)
    581  1.1  christos 		return 1;
    582  1.1  christos 
    583  1.1  christos 	(void) ipf_scan_match(is);
    584  1.1  christos #if 0
    585  1.1  christos 	/*
    586  1.1  christos 	 * There is the potential here for plain text passwords to get
    587  1.1  christos 	 * buffered and stored for some time...
    588  1.1  christos 	 */
    589  1.1  christos 	if (!(is->is_flags & IS_SC_CLIENT))
    590  1.1  christos 		bzero(is->is_sbuf[0], sizeof(is->is_sbuf[0]));
    591  1.1  christos 	if (!(is->is_flags & IS_SC_SERVER))
    592  1.1  christos 		bzero(is->is_sbuf[1], sizeof(is->is_sbuf[1]));
    593  1.1  christos #endif
    594  1.1  christos 	return 0;
    595  1.1  christos }
    596  1.1  christos 
    597  1.1  christos 
    598  1.1  christos int
    599  1.1  christos ipf_scan_ioctl(data, cmd, mode, uid, ctx)
    600  1.2  christos 	void *data;
    601  1.1  christos 	ioctlcmd_t cmd;
    602  1.1  christos 	int mode, uid;
    603  1.1  christos 	void *ctx;
    604  1.1  christos {
    605  1.1  christos 	ipscanstat_t ipscs;
    606  1.1  christos 	int err = 0;
    607  1.1  christos 
    608  1.1  christos 	switch (cmd)
    609  1.1  christos 	{
    610  1.1  christos 	case SIOCADSCA :
    611  1.1  christos 		err = ipf_scan_add(data);
    612  1.1  christos 		break;
    613  1.1  christos 	case SIOCRMSCA :
    614  1.1  christos 		err = ipf_scan_remove(data);
    615  1.1  christos 		break;
    616  1.1  christos 	case SIOCGSCST :
    617  1.1  christos 		bcopy((char *)&ipf_scan_stat, (char *)&ipscs, sizeof(ipscs));
    618  1.1  christos 		ipscs.iscs_list = ipf_scan_list;
    619  1.1  christos 		err = BCOPYOUT(&ipscs, data, sizeof(ipscs));
    620  1.1  christos 		if (err != 0) {
    621  1.1  christos 			ipf_interror = 90005;
    622  1.1  christos 			err = EFAULT;
    623  1.1  christos 		}
    624  1.1  christos 		break;
    625  1.1  christos 	default :
    626  1.1  christos 		err = EINVAL;
    627  1.1  christos 		break;
    628  1.1  christos 	}
    629  1.1  christos 
    630  1.1  christos 	return err;
    631  1.1  christos }
    632  1.1  christos #endif	/* IPFILTER_SCAN */
    633