xref: /freebsd-src/sbin/ipf/ipftest/ipftest.c (revision 51e16cb8fc536913f490ac6bc9c17e92ebd0411b)
141edb306SCy Schubert 
241edb306SCy Schubert /*
341edb306SCy Schubert  * Copyright (C) 2012 by Darren Reed.
441edb306SCy Schubert  *
541edb306SCy Schubert  * See the IPFILTER.LICENCE file for details on licencing.
641edb306SCy Schubert  */
741edb306SCy Schubert #include "ipf.h"
841edb306SCy Schubert #include "ipt.h"
941edb306SCy Schubert #include <sys/ioctl.h>
1041edb306SCy Schubert #include <sys/file.h>
1141edb306SCy Schubert 
1241edb306SCy Schubert 
1341edb306SCy Schubert extern	char	*optarg;
1441edb306SCy Schubert extern	struct ipread	pcap, iptext, iphex;
1541edb306SCy Schubert extern	struct ifnet	*get_unit(char *, int);
1641edb306SCy Schubert extern	void	init_ifp(void);
1741edb306SCy Schubert extern	ipnat_t	*natparse(char *, int);
1841edb306SCy Schubert extern	hostmap_t **ipf_hm_maptable;
1941edb306SCy Schubert extern	hostmap_t *ipf_hm_maplist;
2041edb306SCy Schubert 
2141edb306SCy Schubert ipfmutex_t	ipl_mutex, ipf_auth_mx, ipf_rw, ipf_stinsert;
2241edb306SCy Schubert ipfmutex_t	ipf_nat_new, ipf_natio, ipf_timeoutlock;
2341edb306SCy Schubert ipfrwlock_t	ipf_mutex, ipf_global, ipf_ipidfrag, ip_poolrw, ipf_frcache;
2441edb306SCy Schubert ipfrwlock_t	ipf_frag, ipf_state, ipf_nat, ipf_natfrag, ipf_authlk;
2541edb306SCy Schubert ipfrwlock_t	ipf_tokens;
2641edb306SCy Schubert int	opts = OPT_DONTOPEN;
2741edb306SCy Schubert int	use_inet6 = 0;
2841edb306SCy Schubert int	docksum = 0;
2941edb306SCy Schubert int	pfil_delayed_copy = 0;
3041edb306SCy Schubert int	main(int, char *[]);
3141edb306SCy Schubert int	loadrules(char *, int);
3241edb306SCy Schubert int	kmemcpy(char *, long, int);
3341edb306SCy Schubert int     kstrncpy(char *, long, int n);
3441edb306SCy Schubert int	blockreason;
3541edb306SCy Schubert void	dumpnat(void *);
3641edb306SCy Schubert void	dumpgroups(ipf_main_softc_t *);
3741edb306SCy Schubert void	dumprules(frentry_t *);
3841edb306SCy Schubert void	drain_log(char *);
3941edb306SCy Schubert void	fixv4sums(mb_t *, ip_t *);
4041edb306SCy Schubert 
4141edb306SCy Schubert int ipftestioctl(int, ioctlcmd_t, ...);
4241edb306SCy Schubert int ipnattestioctl(int, ioctlcmd_t, ...);
4341edb306SCy Schubert int ipstatetestioctl(int, ioctlcmd_t, ...);
4441edb306SCy Schubert int ipauthtestioctl(int, ioctlcmd_t, ...);
4541edb306SCy Schubert int ipscantestioctl(int, ioctlcmd_t, ...);
4641edb306SCy Schubert int ipsynctestioctl(int, ioctlcmd_t, ...);
4741edb306SCy Schubert int ipooltestioctl(int, ioctlcmd_t, ...);
4841edb306SCy Schubert 
4941edb306SCy Schubert static	ioctlfunc_t	iocfunctions[IPL_LOGSIZE] = { ipftestioctl,
5041edb306SCy Schubert 						      ipnattestioctl,
5141edb306SCy Schubert 						      ipstatetestioctl,
5241edb306SCy Schubert 						      ipauthtestioctl,
5341edb306SCy Schubert 						      ipsynctestioctl,
5441edb306SCy Schubert 						      ipscantestioctl,
5541edb306SCy Schubert 						      ipooltestioctl,
5641edb306SCy Schubert 						      NULL };
5741edb306SCy Schubert static	ipf_main_softc_t	*softc = NULL;
5841edb306SCy Schubert 
5941edb306SCy Schubert 
6041edb306SCy Schubert int
main(int argc,char * argv[])61efeb8bffSCy Schubert main(int argc, char *argv[])
6241edb306SCy Schubert {
6341edb306SCy Schubert 	char	*datain, *iface, *ifname, *logout;
6441edb306SCy Schubert 	int	fd, i, dir, c, loaded, dump, hlen;
6541edb306SCy Schubert 	struct	in_addr	sip;
6641edb306SCy Schubert 	struct	ifnet	*ifp;
6741edb306SCy Schubert 	struct	ipread	*r;
6841edb306SCy Schubert 	mb_t	mb, *m, *n;
6941edb306SCy Schubert 	ip_t	*ip;
7041edb306SCy Schubert 
7141edb306SCy Schubert 	m = &mb;
7241edb306SCy Schubert 	dir = 0;
7341edb306SCy Schubert 	dump = 0;
7441edb306SCy Schubert 	hlen = 0;
7541edb306SCy Schubert 	loaded = 0;
7641edb306SCy Schubert 	r = &iptext;
7741edb306SCy Schubert 	iface = NULL;
7841edb306SCy Schubert 	logout = NULL;
7941edb306SCy Schubert 	datain = NULL;
8041edb306SCy Schubert 	sip.s_addr = 0;
8141edb306SCy Schubert 	ifname = "anon0";
8241edb306SCy Schubert 
8341edb306SCy Schubert 	initparse();
8441edb306SCy Schubert 
8541edb306SCy Schubert 	ipf_load_all();
8641edb306SCy Schubert 
8741edb306SCy Schubert 	softc = ipf_create_all(NULL);
8841edb306SCy Schubert 	if (softc == NULL)
8941edb306SCy Schubert 		exit(1);
9041edb306SCy Schubert 
9141edb306SCy Schubert 	if (ipf_init_all(softc) == -1)
9241edb306SCy Schubert 		exit(1);
9341edb306SCy Schubert 
9441edb306SCy Schubert 	i = 1;
9541edb306SCy Schubert 	if (ipftestioctl(IPL_LOGIPF, SIOCFRENB, &i) != 0)
9641edb306SCy Schubert 		exit(1);
9741edb306SCy Schubert 
9841edb306SCy Schubert 	while ((c = getopt(argc, argv, "6bCdDF:i:I:l:N:P:or:RS:T:vxX")) != -1)
9941edb306SCy Schubert 		switch (c)
10041edb306SCy Schubert 		{
10141edb306SCy Schubert 		case '6' :
10241edb306SCy Schubert #ifdef	USE_INET6
10341edb306SCy Schubert 			use_inet6 = 1;
10441edb306SCy Schubert #else
10541edb306SCy Schubert 			fprintf(stderr, "IPv6 not supported\n");
10641edb306SCy Schubert 			exit(1);
10741edb306SCy Schubert #endif
10841edb306SCy Schubert 			break;
10941edb306SCy Schubert 		case 'b' :
11041edb306SCy Schubert 			opts |= OPT_BRIEF;
11141edb306SCy Schubert 			break;
11241edb306SCy Schubert 		case 'd' :
11341edb306SCy Schubert 			opts |= OPT_DEBUG;
11441edb306SCy Schubert 			break;
11541edb306SCy Schubert 		case 'C' :
11641edb306SCy Schubert 			docksum = 1;
11741edb306SCy Schubert 			break;
11841edb306SCy Schubert 		case 'D' :
11941edb306SCy Schubert 			dump = 1;
12041edb306SCy Schubert 			break;
12141edb306SCy Schubert 		case 'F' :
12241edb306SCy Schubert 			if (strcasecmp(optarg, "pcap") == 0)
12341edb306SCy Schubert 				r = &pcap;
12441edb306SCy Schubert 			else if (strcasecmp(optarg, "hex") == 0)
12541edb306SCy Schubert 				r = &iphex;
12641edb306SCy Schubert 			else if (strcasecmp(optarg, "text") == 0)
12741edb306SCy Schubert 				r = &iptext;
12841edb306SCy Schubert 			break;
12941edb306SCy Schubert 		case 'i' :
13041edb306SCy Schubert 			datain = optarg;
13141edb306SCy Schubert 			break;
13241edb306SCy Schubert 		case 'I' :
13341edb306SCy Schubert 			ifname = optarg;
13441edb306SCy Schubert 			break;
13541edb306SCy Schubert 		case 'l' :
13641edb306SCy Schubert 			logout = optarg;
13741edb306SCy Schubert 			break;
13841edb306SCy Schubert 		case 'N' :
13941edb306SCy Schubert 			if (ipnat_parsefile(-1, ipnat_addrule, ipnattestioctl,
14041edb306SCy Schubert 					    optarg) == -1)
141*2582ae57SCy Schubert 				return (-1);
14241edb306SCy Schubert 			loaded = 1;
14341edb306SCy Schubert 			opts |= OPT_NAT;
14441edb306SCy Schubert 			break;
14541edb306SCy Schubert 		case 'o' :
14641edb306SCy Schubert 			opts |= OPT_SAVEOUT;
14741edb306SCy Schubert 			break;
14841edb306SCy Schubert 		case 'P' :
14941edb306SCy Schubert 			if (ippool_parsefile(-1, optarg, ipooltestioctl) == -1)
150*2582ae57SCy Schubert 				return (-1);
15141edb306SCy Schubert 			loaded = 1;
15241edb306SCy Schubert 			break;
15341edb306SCy Schubert 		case 'r' :
15441edb306SCy Schubert 			if (ipf_parsefile(-1, ipf_addrule, iocfunctions,
15541edb306SCy Schubert 					  optarg) == -1)
156*2582ae57SCy Schubert 				return (-1);
15741edb306SCy Schubert 			loaded = 1;
15841edb306SCy Schubert 			break;
15941edb306SCy Schubert 		case 'S' :
16041edb306SCy Schubert 			sip.s_addr = inet_addr(optarg);
16141edb306SCy Schubert 			break;
16241edb306SCy Schubert 		case 'R' :
16341edb306SCy Schubert 			opts |= OPT_NORESOLVE;
16441edb306SCy Schubert 			break;
16541edb306SCy Schubert 		case 'T' :
16641edb306SCy Schubert 			ipf_dotuning(-1, optarg, ipftestioctl);
16741edb306SCy Schubert 			break;
16841edb306SCy Schubert 		case 'v' :
16941edb306SCy Schubert 			opts |= OPT_VERBOSE;
17041edb306SCy Schubert 			break;
17141edb306SCy Schubert 		case 'x' :
17241edb306SCy Schubert 			opts |= OPT_HEX;
17341edb306SCy Schubert 			break;
17441edb306SCy Schubert 		}
17541edb306SCy Schubert 
17641edb306SCy Schubert 	if (loaded == 0) {
17741edb306SCy Schubert 		(void)fprintf(stderr,"no rules loaded\n");
17841edb306SCy Schubert 		exit(-1);
17941edb306SCy Schubert 	}
18041edb306SCy Schubert 
18141edb306SCy Schubert 	if (opts & OPT_SAVEOUT)
18241edb306SCy Schubert 		init_ifp();
18341edb306SCy Schubert 
18441edb306SCy Schubert 	if (datain)
18541edb306SCy Schubert 		fd = (*r->r_open)(datain);
18641edb306SCy Schubert 	else
18741edb306SCy Schubert 		fd = (*r->r_open)("-");
18841edb306SCy Schubert 
18941edb306SCy Schubert 	if (fd < 0) {
19041edb306SCy Schubert 		perror("error opening input");
19141edb306SCy Schubert 		exit(-1);
19241edb306SCy Schubert 	}
19341edb306SCy Schubert 
19441edb306SCy Schubert 	m->m_data = (char *)m->mb_buf;
19541edb306SCy Schubert 	while ((i = (*r->r_readip)(m, &iface, &dir)) > 0) {
19641edb306SCy Schubert 
19741edb306SCy Schubert 		if ((iface == NULL) || (*iface == '\0'))
19841edb306SCy Schubert 			iface = ifname;
19941edb306SCy Schubert 
20041edb306SCy Schubert 		ip = MTOD(m, ip_t *);
20141edb306SCy Schubert 		ifp = get_unit(iface, IP_V(ip));
20241edb306SCy Schubert 
20341edb306SCy Schubert 		if (IP_V(ip) == 4) {
20441edb306SCy Schubert 			if ((r->r_flags & R_DO_CKSUM) || docksum)
20541edb306SCy Schubert 				fixv4sums(m, ip);
20641edb306SCy Schubert 			hlen = IP_HL(ip) << 2;
20741edb306SCy Schubert 			if (sip.s_addr)
20841edb306SCy Schubert 				dir = !(sip.s_addr == ip->ip_src.s_addr);
20941edb306SCy Schubert 		}
21041edb306SCy Schubert #ifdef	USE_INET6
21141edb306SCy Schubert 		else
21241edb306SCy Schubert 			hlen = sizeof(ip6_t);
21341edb306SCy Schubert #endif
21441edb306SCy Schubert 		/* ipfr_slowtimer(); */
21541edb306SCy Schubert 		blockreason = 0;
21641edb306SCy Schubert 		m = &mb;
21741edb306SCy Schubert 		m->mb_ifp = ifp;
21841edb306SCy Schubert 		m->mb_len = i;
21941edb306SCy Schubert 		i = ipf_check(softc, ip, hlen, ifp, dir, &m);
22041edb306SCy Schubert 		if ((opts & OPT_NAT) == 0)
22141edb306SCy Schubert 			switch (i)
22241edb306SCy Schubert 			{
22341edb306SCy Schubert 			case -4 :
22441edb306SCy Schubert 				(void)printf("preauth");
22541edb306SCy Schubert 				break;
22641edb306SCy Schubert 			case -3 :
22741edb306SCy Schubert 				(void)printf("account");
22841edb306SCy Schubert 				break;
22941edb306SCy Schubert 			case -2 :
23041edb306SCy Schubert 				(void)printf("auth");
23141edb306SCy Schubert 				break;
23241edb306SCy Schubert 			case -1 :
23341edb306SCy Schubert 				(void)printf("block");
23441edb306SCy Schubert 				break;
23541edb306SCy Schubert 			case 0 :
23641edb306SCy Schubert 				(void)printf("pass");
23741edb306SCy Schubert 				break;
23841edb306SCy Schubert 			case 1 :
23941edb306SCy Schubert 				if (m == NULL)
24041edb306SCy Schubert 					(void)printf("bad-packet");
24141edb306SCy Schubert 				else
24241edb306SCy Schubert 					(void)printf("nomatch");
24341edb306SCy Schubert 				break;
24441edb306SCy Schubert 			case 3 :
24541edb306SCy Schubert 				(void)printf("block return-rst");
24641edb306SCy Schubert 				break;
24741edb306SCy Schubert 			case 4 :
24841edb306SCy Schubert 				(void)printf("block return-icmp");
24941edb306SCy Schubert 				break;
25041edb306SCy Schubert 			case 5 :
25141edb306SCy Schubert 				(void)printf("block return-icmp-as-dest");
25241edb306SCy Schubert 				break;
25341edb306SCy Schubert 			default :
254*2582ae57SCy Schubert 				(void)printf("recognised( return %#x\n", i));
25541edb306SCy Schubert 				break;
25641edb306SCy Schubert 			}
25741edb306SCy Schubert 
25841edb306SCy Schubert 		if (!(opts & OPT_BRIEF)) {
25941edb306SCy Schubert 			putchar(' ');
26041edb306SCy Schubert 			if (m != NULL)
26141edb306SCy Schubert 				printpacket(dir, m);
26241edb306SCy Schubert 			else
26341edb306SCy Schubert 				printpacket(dir, &mb);
26441edb306SCy Schubert 			printf("--------------");
26541edb306SCy Schubert 		} else if ((opts & (OPT_BRIEF|OPT_NAT)) ==
26641edb306SCy Schubert 			   (OPT_NAT|OPT_BRIEF)) {
26741edb306SCy Schubert 			if (m != NULL)
26841edb306SCy Schubert 				printpacket(dir, m);
26941edb306SCy Schubert 			else
27041edb306SCy Schubert 				PRINTF("%d\n", blockreason);
27141edb306SCy Schubert 		}
27241edb306SCy Schubert 
27341edb306SCy Schubert 		ipf_state_flush(softc, 1, 0);
27441edb306SCy Schubert 
27541edb306SCy Schubert 		if (dir && (ifp != NULL) && IP_V(ip) && (m != NULL))
27641edb306SCy Schubert 			(*ifp->if_output)(ifp, (void *)m, NULL, 0);
27741edb306SCy Schubert 
27841edb306SCy Schubert 		while ((m != NULL) && (m != &mb)) {
27941edb306SCy Schubert 			n = m->mb_next;
28041edb306SCy Schubert 			freembt(m);
28141edb306SCy Schubert 			m = n;
28241edb306SCy Schubert 		}
28341edb306SCy Schubert 
28441edb306SCy Schubert 		if ((opts & (OPT_BRIEF|OPT_NAT)) != (OPT_NAT|OPT_BRIEF))
28541edb306SCy Schubert 			putchar('\n');
28641edb306SCy Schubert 		dir = 0;
28741edb306SCy Schubert 		if (iface != ifname) {
28841edb306SCy Schubert 			free(iface);
28941edb306SCy Schubert 			iface = ifname;
29041edb306SCy Schubert 		}
29141edb306SCy Schubert 		m = &mb;
29241edb306SCy Schubert 		m->mb_data = (char *)m->mb_buf;
29341edb306SCy Schubert 	}
29441edb306SCy Schubert 
29541edb306SCy Schubert 	if (i != 0)
29641edb306SCy Schubert 		fprintf(stderr, "readip failed: %d\n", i);
29741edb306SCy Schubert 	(*r->r_close)();
29841edb306SCy Schubert 
29941edb306SCy Schubert 	if (logout != NULL) {
30041edb306SCy Schubert 		drain_log(logout);
30141edb306SCy Schubert 	}
30241edb306SCy Schubert 
30341edb306SCy Schubert 	if (dump == 1)  {
30441edb306SCy Schubert 		dumpnat(softc->ipf_nat_soft);
30541edb306SCy Schubert 		ipf_state_dump(softc, softc->ipf_state_soft);
30641edb306SCy Schubert 		ipf_lookup_dump(softc, softc->ipf_state_soft);
30741edb306SCy Schubert 		dumpgroups(softc);
30841edb306SCy Schubert 	}
30941edb306SCy Schubert 
31041edb306SCy Schubert 	ipf_fini_all(softc);
31141edb306SCy Schubert 
31241edb306SCy Schubert 	ipf_destroy_all(softc);
31341edb306SCy Schubert 
31441edb306SCy Schubert 	ipf_unload_all();
31541edb306SCy Schubert 
31641edb306SCy Schubert 	ipf_mutex_clean();
31741edb306SCy Schubert 	ipf_rwlock_clean();
31841edb306SCy Schubert 
31941edb306SCy Schubert 	if (getenv("FINDLEAKS")) {
32041edb306SCy Schubert 		fflush(stdout);
32141edb306SCy Schubert 		abort();
32241edb306SCy Schubert 	}
323*2582ae57SCy Schubert 	return (0);
32441edb306SCy Schubert }
32541edb306SCy Schubert 
32641edb306SCy Schubert 
ipftestioctl(int dev,ioctlcmd_t cmd,...)32741edb306SCy Schubert int ipftestioctl(int dev, ioctlcmd_t cmd, ...)
32841edb306SCy Schubert {
32941edb306SCy Schubert 	caddr_t data;
33041edb306SCy Schubert 	va_list ap;
33141edb306SCy Schubert 	int i;
33241edb306SCy Schubert 
33341edb306SCy Schubert 	dev = dev;	/* gcc -Wextra */
33441edb306SCy Schubert 	va_start(ap, cmd);
33541edb306SCy Schubert 	data = va_arg(ap, caddr_t);
33641edb306SCy Schubert 	va_end(ap);
33741edb306SCy Schubert 
33841edb306SCy Schubert 	i = ipfioctl(softc, IPL_LOGIPF, cmd, data, FWRITE|FREAD);
33941edb306SCy Schubert 	if (opts & OPT_DEBUG)
34041edb306SCy Schubert 		fprintf(stderr, "ipfioctl(IPF,%#x,%p) = %d (%d)\n",
34141edb306SCy Schubert 			(u_int)cmd, data, i, softc->ipf_interror);
34241edb306SCy Schubert 	if (i != 0) {
34341edb306SCy Schubert 		errno = i;
344*2582ae57SCy Schubert 		return (-1);
34541edb306SCy Schubert 	}
346*2582ae57SCy Schubert 	return (0);
34741edb306SCy Schubert }
34841edb306SCy Schubert 
34941edb306SCy Schubert 
350efeb8bffSCy Schubert int
ipnattestioctl(int dev,ioctlcmd_t cmd,...)351efeb8bffSCy Schubert ipnattestioctl(int dev, ioctlcmd_t cmd, ...)
35241edb306SCy Schubert {
35341edb306SCy Schubert 	caddr_t data;
35441edb306SCy Schubert 	va_list ap;
35541edb306SCy Schubert 	int i;
35641edb306SCy Schubert 
35741edb306SCy Schubert 	dev = dev;	/* gcc -Wextra */
35841edb306SCy Schubert 	va_start(ap, cmd);
35941edb306SCy Schubert 	data = va_arg(ap, caddr_t);
36041edb306SCy Schubert 	va_end(ap);
36141edb306SCy Schubert 
36241edb306SCy Schubert 	i = ipfioctl(softc, IPL_LOGNAT, cmd, data, FWRITE|FREAD);
36341edb306SCy Schubert 	if (opts & OPT_DEBUG)
36441edb306SCy Schubert 		fprintf(stderr, "ipfioctl(NAT,%#x,%p) = %d\n",
36541edb306SCy Schubert 			(u_int)cmd, data, i);
36641edb306SCy Schubert 	if (i != 0) {
36741edb306SCy Schubert 		errno = i;
368*2582ae57SCy Schubert 		return (-1);
36941edb306SCy Schubert 	}
370*2582ae57SCy Schubert 	return (0);
37141edb306SCy Schubert }
37241edb306SCy Schubert 
37341edb306SCy Schubert 
374efeb8bffSCy Schubert int
ipstatetestioctl(int dev,ioctlcmd_t cmd,...)375efeb8bffSCy Schubert ipstatetestioctl(int dev, ioctlcmd_t cmd, ...)
37641edb306SCy Schubert {
37741edb306SCy Schubert 	caddr_t data;
37841edb306SCy Schubert 	va_list ap;
37941edb306SCy Schubert 	int i;
38041edb306SCy Schubert 
38141edb306SCy Schubert 	dev = dev;	/* gcc -Wextra */
38241edb306SCy Schubert 	va_start(ap, cmd);
38341edb306SCy Schubert 	data = va_arg(ap, caddr_t);
38441edb306SCy Schubert 	va_end(ap);
38541edb306SCy Schubert 
38641edb306SCy Schubert 	i = ipfioctl(softc, IPL_LOGSTATE, cmd, data, FWRITE|FREAD);
38741edb306SCy Schubert 	if ((opts & OPT_DEBUG) || (i != 0))
38841edb306SCy Schubert 		fprintf(stderr, "ipfioctl(STATE,%#x,%p) = %d\n",
38941edb306SCy Schubert 			(u_int)cmd, data, i);
39041edb306SCy Schubert 	if (i != 0) {
39141edb306SCy Schubert 		errno = i;
392*2582ae57SCy Schubert 		return (-1);
39341edb306SCy Schubert 	}
394*2582ae57SCy Schubert 	return (0);
39541edb306SCy Schubert }
39641edb306SCy Schubert 
39741edb306SCy Schubert 
398efeb8bffSCy Schubert int
ipauthtestioctl(int dev,ioctlcmd_t cmd,...)399efeb8bffSCy Schubert ipauthtestioctl(int dev, ioctlcmd_t cmd, ...)
40041edb306SCy Schubert {
40141edb306SCy Schubert 	caddr_t data;
40241edb306SCy Schubert 	va_list ap;
40341edb306SCy Schubert 	int i;
40441edb306SCy Schubert 
40541edb306SCy Schubert 	dev = dev;	/* gcc -Wextra */
40641edb306SCy Schubert 	va_start(ap, cmd);
40741edb306SCy Schubert 	data = va_arg(ap, caddr_t);
40841edb306SCy Schubert 	va_end(ap);
40941edb306SCy Schubert 
41041edb306SCy Schubert 	i = ipfioctl(softc, IPL_LOGAUTH, cmd, data, FWRITE|FREAD);
41141edb306SCy Schubert 	if ((opts & OPT_DEBUG) || (i != 0))
41241edb306SCy Schubert 		fprintf(stderr, "ipfioctl(AUTH,%#x,%p) = %d\n",
41341edb306SCy Schubert 			(u_int)cmd, data, i);
41441edb306SCy Schubert 	if (i != 0) {
41541edb306SCy Schubert 		errno = i;
416*2582ae57SCy Schubert 		return (-1);
41741edb306SCy Schubert 	}
418*2582ae57SCy Schubert 	return (0);
41941edb306SCy Schubert }
42041edb306SCy Schubert 
42141edb306SCy Schubert 
422efeb8bffSCy Schubert int
ipscantestioctl(int dev,ioctlcmd_t cmd,...)423efeb8bffSCy Schubert ipscantestioctl(int dev, ioctlcmd_t cmd, ...)
42441edb306SCy Schubert {
42541edb306SCy Schubert 	caddr_t data;
42641edb306SCy Schubert 	va_list ap;
42741edb306SCy Schubert 	int i;
42841edb306SCy Schubert 
42941edb306SCy Schubert 	dev = dev;	/* gcc -Wextra */
43041edb306SCy Schubert 	va_start(ap, cmd);
43141edb306SCy Schubert 	data = va_arg(ap, caddr_t);
43241edb306SCy Schubert 	va_end(ap);
43341edb306SCy Schubert 
43441edb306SCy Schubert 	i = ipfioctl(softc, IPL_LOGSCAN, cmd, data, FWRITE|FREAD);
43541edb306SCy Schubert 	if ((opts & OPT_DEBUG) || (i != 0))
43641edb306SCy Schubert 		fprintf(stderr, "ipfioctl(SCAN,%#x,%p) = %d\n",
43741edb306SCy Schubert 			(u_int)cmd, data, i);
43841edb306SCy Schubert 	if (i != 0) {
43941edb306SCy Schubert 		errno = i;
440*2582ae57SCy Schubert 		return (-1);
44141edb306SCy Schubert 	}
442*2582ae57SCy Schubert 	return (0);
44341edb306SCy Schubert }
44441edb306SCy Schubert 
44541edb306SCy Schubert 
446efeb8bffSCy Schubert int
ipsynctestioctl(int dev,ioctlcmd_t cmd,...)447efeb8bffSCy Schubert ipsynctestioctl(int dev, ioctlcmd_t cmd, ...)
44841edb306SCy Schubert {
44941edb306SCy Schubert 	caddr_t data;
45041edb306SCy Schubert 	va_list ap;
45141edb306SCy Schubert 	int i;
45241edb306SCy Schubert 
45341edb306SCy Schubert 	dev = dev;	/* gcc -Wextra */
45441edb306SCy Schubert 	va_start(ap, cmd);
45541edb306SCy Schubert 	data = va_arg(ap, caddr_t);
45641edb306SCy Schubert 	va_end(ap);
45741edb306SCy Schubert 
45841edb306SCy Schubert 	i = ipfioctl(softc, IPL_LOGSYNC, cmd, data, FWRITE|FREAD);
45941edb306SCy Schubert 	if ((opts & OPT_DEBUG) || (i != 0))
46041edb306SCy Schubert 		fprintf(stderr, "ipfioctl(SYNC,%#x,%p) = %d\n",
46141edb306SCy Schubert 			(u_int)cmd, data, i);
46241edb306SCy Schubert 	if (i != 0) {
46341edb306SCy Schubert 		errno = i;
464*2582ae57SCy Schubert 		return (-1);
46541edb306SCy Schubert 	}
466*2582ae57SCy Schubert 	return (0);
46741edb306SCy Schubert }
46841edb306SCy Schubert 
46941edb306SCy Schubert 
470efeb8bffSCy Schubert int
ipooltestioctl(int dev,ioctlcmd_t cmd,...)471efeb8bffSCy Schubert ipooltestioctl(int dev, ioctlcmd_t cmd, ...)
47241edb306SCy Schubert {
47341edb306SCy Schubert 	caddr_t data;
47441edb306SCy Schubert 	va_list ap;
47541edb306SCy Schubert 	int i;
47641edb306SCy Schubert 
47741edb306SCy Schubert 	dev = dev;	/* gcc -Wextra */
47841edb306SCy Schubert 	va_start(ap, cmd);
47941edb306SCy Schubert 	data = va_arg(ap, caddr_t);
48041edb306SCy Schubert 	va_end(ap);
48141edb306SCy Schubert 
48241edb306SCy Schubert 	i = ipfioctl(softc, IPL_LOGLOOKUP, cmd, data, FWRITE|FREAD);
48341edb306SCy Schubert 	if ((opts & OPT_DEBUG) || (i != 0))
48441edb306SCy Schubert 		fprintf(stderr, "ipfioctl(POOL,%#x,%p) = %d (%d)\n",
48541edb306SCy Schubert 			(u_int)cmd, data, i, softc->ipf_interror);
48641edb306SCy Schubert 	if (i != 0) {
48741edb306SCy Schubert 		errno = i;
488*2582ae57SCy Schubert 		return (-1);
48941edb306SCy Schubert 	}
490*2582ae57SCy Schubert 	return (0);
49141edb306SCy Schubert }
49241edb306SCy Schubert 
49341edb306SCy Schubert 
494efeb8bffSCy Schubert int
kmemcpy(char * addr,long offset,int size)495efeb8bffSCy Schubert kmemcpy(char *addr, long offset, int size)
49641edb306SCy Schubert {
49741edb306SCy Schubert 	bcopy((char *)offset, addr, size);
498*2582ae57SCy Schubert 	return (0);
49941edb306SCy Schubert }
50041edb306SCy Schubert 
50141edb306SCy Schubert 
502efeb8bffSCy Schubert int
kstrncpy(char * buf,long pos,int n)503efeb8bffSCy Schubert kstrncpy(char *buf, long pos, int n)
50441edb306SCy Schubert {
50541edb306SCy Schubert 	char *ptr;
50641edb306SCy Schubert 
50741edb306SCy Schubert 	ptr = (char *)pos;
50841edb306SCy Schubert 
50941edb306SCy Schubert 	while ((n > 0) && (*buf++ = *ptr++))
51041edb306SCy Schubert 		;
511*2582ae57SCy Schubert 	return (0);
51241edb306SCy Schubert }
51341edb306SCy Schubert 
51441edb306SCy Schubert 
51541edb306SCy Schubert /*
51641edb306SCy Schubert  * Display the built up NAT table rules and mapping entries.
51741edb306SCy Schubert  */
518efeb8bffSCy Schubert void
dumpnat(void * arg)519efeb8bffSCy Schubert dumpnat(void *arg)
52041edb306SCy Schubert {
52141edb306SCy Schubert 	ipf_nat_softc_t *softn = arg;
52241edb306SCy Schubert 	hostmap_t *hm;
52341edb306SCy Schubert 	ipnat_t	*ipn;
52441edb306SCy Schubert 	nat_t *nat;
52541edb306SCy Schubert 
52641edb306SCy Schubert 	printf("List of active MAP/Redirect filters:\n");
52741edb306SCy Schubert 	for (ipn = softn->ipf_nat_list; ipn != NULL; ipn = ipn->in_next)
52841edb306SCy Schubert 		printnat(ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
52941edb306SCy Schubert 	printf("\nList of active sessions:\n");
53041edb306SCy Schubert 	for (nat = softn->ipf_nat_instances; nat; nat = nat->nat_next) {
53141edb306SCy Schubert 		printactivenat(nat, opts, 0);
53241edb306SCy Schubert 		if (nat->nat_aps)
53341edb306SCy Schubert 			printf("\tproxy active\n");
53441edb306SCy Schubert 	}
53541edb306SCy Schubert 
53641edb306SCy Schubert 	printf("\nHostmap table:\n");
53741edb306SCy Schubert 	for (hm = softn->ipf_hm_maplist; hm != NULL; hm = hm->hm_next)
53841edb306SCy Schubert 		printhostmap(hm, hm->hm_hv);
53941edb306SCy Schubert }
54041edb306SCy Schubert 
54141edb306SCy Schubert 
542efeb8bffSCy Schubert void
dumpgroups(ipf_main_softc_t * softc)543efeb8bffSCy Schubert dumpgroups(ipf_main_softc_t *softc)
54441edb306SCy Schubert {
54541edb306SCy Schubert 	frgroup_t *fg;
54641edb306SCy Schubert 	int i;
54741edb306SCy Schubert 
54841edb306SCy Schubert 	printf("List of groups configured (set 0)\n");
54941edb306SCy Schubert 	for (i = 0; i < IPL_LOGSIZE; i++)
55041edb306SCy Schubert 		for (fg =  softc->ipf_groups[i][0]; fg != NULL;
55141edb306SCy Schubert 		     fg = fg->fg_next) {
55241edb306SCy Schubert 			printf("Dev.%d. Group %s Ref %d Flags %#x\n",
55341edb306SCy Schubert 				i, fg->fg_name, fg->fg_ref, fg->fg_flags);
55441edb306SCy Schubert 			dumprules(fg->fg_start);
55541edb306SCy Schubert 		}
55641edb306SCy Schubert 
55741edb306SCy Schubert 	printf("List of groups configured (set 1)\n");
55841edb306SCy Schubert 	for (i = 0; i < IPL_LOGSIZE; i++)
55941edb306SCy Schubert 		for (fg =  softc->ipf_groups[i][1]; fg != NULL;
56041edb306SCy Schubert 		     fg = fg->fg_next) {
56141edb306SCy Schubert 			printf("Dev.%d. Group %s Ref %d Flags %#x\n",
56241edb306SCy Schubert 				i, fg->fg_name, fg->fg_ref, fg->fg_flags);
56341edb306SCy Schubert 			dumprules(fg->fg_start);
56441edb306SCy Schubert 		}
56541edb306SCy Schubert 
56641edb306SCy Schubert 	printf("Rules configured (set 0, in)\n");
56741edb306SCy Schubert 	dumprules(softc->ipf_rules[0][0]);
56841edb306SCy Schubert 	printf("Rules configured (set 0, out)\n");
56941edb306SCy Schubert 	dumprules(softc->ipf_rules[1][0]);
57041edb306SCy Schubert 	printf("Rules configured (set 1, in)\n");
57141edb306SCy Schubert 	dumprules(softc->ipf_rules[0][1]);
57241edb306SCy Schubert 	printf("Rules configured (set 1, out)\n");
57341edb306SCy Schubert 	dumprules(softc->ipf_rules[1][1]);
57441edb306SCy Schubert 
57541edb306SCy Schubert 	printf("Accounting rules configured (set 0, in)\n");
57641edb306SCy Schubert 	dumprules(softc->ipf_acct[0][0]);
57741edb306SCy Schubert 	printf("Accounting rules configured (set 0, out)\n");
57841edb306SCy Schubert 	dumprules(softc->ipf_acct[0][1]);
57941edb306SCy Schubert 	printf("Accounting rules configured (set 1, in)\n");
58041edb306SCy Schubert 	dumprules(softc->ipf_acct[1][0]);
58141edb306SCy Schubert 	printf("Accounting rules configured (set 1, out)\n");
58241edb306SCy Schubert 	dumprules(softc->ipf_acct[1][1]);
58341edb306SCy Schubert }
58441edb306SCy Schubert 
585efeb8bffSCy Schubert void
dumprules(frentry_t * rulehead)586efeb8bffSCy Schubert dumprules(frentry_t *rulehead)
58741edb306SCy Schubert {
58841edb306SCy Schubert 	frentry_t *fr;
58941edb306SCy Schubert 
59041edb306SCy Schubert 	for (fr = rulehead; fr != NULL; fr = fr->fr_next) {
59141edb306SCy Schubert #ifdef	USE_QUAD_T
59241edb306SCy Schubert 		printf("%"PRIu64" ",(unsigned long long)fr->fr_hits);
59341edb306SCy Schubert #else
59441edb306SCy Schubert 		printf("%ld ", fr->fr_hits);
59541edb306SCy Schubert #endif
59641edb306SCy Schubert 		printfr(fr, ipftestioctl);
59741edb306SCy Schubert 	}
59841edb306SCy Schubert }
59941edb306SCy Schubert 
60041edb306SCy Schubert 
601efeb8bffSCy Schubert void
drain_log(char * filename)602efeb8bffSCy Schubert drain_log(char *filename)
60341edb306SCy Schubert {
60441edb306SCy Schubert 	char buffer[DEFAULT_IPFLOGSIZE];
60541edb306SCy Schubert 	struct iovec iov;
60641edb306SCy Schubert 	struct uio uio;
60741edb306SCy Schubert 	size_t resid;
60841edb306SCy Schubert 	int fd, i;
60941edb306SCy Schubert 
61041edb306SCy Schubert 	fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644);
61141edb306SCy Schubert 	if (fd == -1) {
61241edb306SCy Schubert 		perror("drain_log:open");
61341edb306SCy Schubert 		return;
61441edb306SCy Schubert 	}
61541edb306SCy Schubert 
61641edb306SCy Schubert 	for (i = 0; i <= IPL_LOGMAX; i++)
61741edb306SCy Schubert 		while (1) {
61841edb306SCy Schubert 			bzero((char *)&iov, sizeof(iov));
61941edb306SCy Schubert 			iov.iov_base = buffer;
62041edb306SCy Schubert 			iov.iov_len = sizeof(buffer);
62141edb306SCy Schubert 
62241edb306SCy Schubert 			bzero((char *)&uio, sizeof(uio));
62341edb306SCy Schubert 			uio.uio_iov = &iov;
62441edb306SCy Schubert 			uio.uio_iovcnt = 1;
62541edb306SCy Schubert 			uio.uio_resid = iov.iov_len;
62641edb306SCy Schubert 			resid = uio.uio_resid;
62741edb306SCy Schubert 
62841edb306SCy Schubert 			if (ipf_log_read(softc, i, &uio) == 0) {
62941edb306SCy Schubert 				/*
63041edb306SCy Schubert 				 * If nothing was read then break out.
63141edb306SCy Schubert 				 */
63241edb306SCy Schubert 				if (uio.uio_resid == resid)
63341edb306SCy Schubert 					break;
63441edb306SCy Schubert 				write(fd, buffer, resid - uio.uio_resid);
63541edb306SCy Schubert 			} else
63641edb306SCy Schubert 				break;
63741edb306SCy Schubert 	}
63841edb306SCy Schubert 
63941edb306SCy Schubert 	close(fd);
64041edb306SCy Schubert }
64141edb306SCy Schubert 
64241edb306SCy Schubert 
643efeb8bffSCy Schubert void
fixv4sums(mb_t * m,ip_t * ip)644efeb8bffSCy Schubert fixv4sums(mb_t *m, ip_t *ip)
64541edb306SCy Schubert {
64641edb306SCy Schubert 	u_char *csump, *hdr, p;
64741edb306SCy Schubert 	fr_info_t tmp;
64841edb306SCy Schubert 	int len;
64941edb306SCy Schubert 
65041edb306SCy Schubert 	p = 0;
65141edb306SCy Schubert 	len = 0;
65241edb306SCy Schubert 	bzero((char *)&tmp, sizeof(tmp));
65341edb306SCy Schubert 
65441edb306SCy Schubert 	csump = (u_char *)ip;
65541edb306SCy Schubert 	if (IP_V(ip) == 4) {
65641edb306SCy Schubert 		ip->ip_sum = 0;
65741edb306SCy Schubert 		ip->ip_sum = ipf_cksum((u_short *)ip, IP_HL(ip) << 2);
65841edb306SCy Schubert 		tmp.fin_hlen = IP_HL(ip) << 2;
65941edb306SCy Schubert 		csump += IP_HL(ip) << 2;
66041edb306SCy Schubert 		p = ip->ip_p;
66141edb306SCy Schubert 		len = ntohs(ip->ip_len);
66241edb306SCy Schubert #ifdef USE_INET6
66341edb306SCy Schubert 	} else if (IP_V(ip) == 6) {
66441edb306SCy Schubert 		tmp.fin_hlen = sizeof(ip6_t);
66541edb306SCy Schubert 		csump += sizeof(ip6_t);
66641edb306SCy Schubert 		p = ((ip6_t *)ip)->ip6_nxt;
66741edb306SCy Schubert 		len = ntohs(((ip6_t *)ip)->ip6_plen);
66841edb306SCy Schubert 		len += sizeof(ip6_t);
66941edb306SCy Schubert #endif
67041edb306SCy Schubert 	}
67141edb306SCy Schubert 	tmp.fin_plen = len;
67241edb306SCy Schubert 	tmp.fin_dlen = len - tmp.fin_hlen;
67341edb306SCy Schubert 
67441edb306SCy Schubert 	switch (p)
67541edb306SCy Schubert 	{
67641edb306SCy Schubert 	case IPPROTO_TCP :
67741edb306SCy Schubert 		hdr = csump;
67841edb306SCy Schubert 		csump += offsetof(tcphdr_t, th_sum);
67941edb306SCy Schubert 		break;
68041edb306SCy Schubert 	case IPPROTO_UDP :
68141edb306SCy Schubert 		hdr = csump;
68241edb306SCy Schubert 		csump += offsetof(udphdr_t, uh_sum);
68341edb306SCy Schubert 		break;
68441edb306SCy Schubert 	case IPPROTO_ICMP :
68541edb306SCy Schubert 		hdr = csump;
68641edb306SCy Schubert 		csump += offsetof(icmphdr_t, icmp_cksum);
68741edb306SCy Schubert 		break;
68841edb306SCy Schubert 	default :
68941edb306SCy Schubert 		csump = NULL;
69041edb306SCy Schubert 		hdr = NULL;
69141edb306SCy Schubert 		break;
69241edb306SCy Schubert 	}
69341edb306SCy Schubert 	if (hdr != NULL) {
69441edb306SCy Schubert 		tmp.fin_m = m;
69541edb306SCy Schubert 		tmp.fin_mp = &m;
69641edb306SCy Schubert 		tmp.fin_dp = hdr;
69741edb306SCy Schubert 		tmp.fin_ip = ip;
69841edb306SCy Schubert 		tmp.fin_plen = len;
69941edb306SCy Schubert 		*csump = 0;
70041edb306SCy Schubert 		*(u_short *)csump = fr_cksum(&tmp, ip, p, hdr);
70141edb306SCy Schubert 	}
70241edb306SCy Schubert }
70341edb306SCy Schubert 
70441edb306SCy Schubert void
ip_fillid(struct ip * ip)70541edb306SCy Schubert ip_fillid(struct ip *ip)
70641edb306SCy Schubert {
70741edb306SCy Schubert 	static uint16_t ip_id;
70841edb306SCy Schubert 
70941edb306SCy Schubert 	ip->ip_id = ip_id++;
71041edb306SCy Schubert }
711