xref: /dragonfly/sbin/ipfw3/ipfw3log.c (revision 4408d548)
1*4408d548SBill Yuan /*
2*4408d548SBill Yuan  * Copyright (c) 2014 - 2018 The DragonFly Project.  All rights reserved.
3*4408d548SBill Yuan  *
4*4408d548SBill Yuan  * This code is derived from software contributed to The DragonFly Project
5*4408d548SBill Yuan  * by Bill Yuan <bycn82@dragonflybsd.org>
6*4408d548SBill Yuan  *
7*4408d548SBill Yuan  * Redistribution and use in source and binary forms, with or without
8*4408d548SBill Yuan  * modification, are permitted provided that the following conditions
9*4408d548SBill Yuan  * are met:
10*4408d548SBill Yuan  *
11*4408d548SBill Yuan  * 1. Redistributions of source code must retain the above copyright
12*4408d548SBill Yuan  *    notice, this list of conditions and the following disclaimer.
13*4408d548SBill Yuan  * 2. Redistributions in binary form must reproduce the above copyright
14*4408d548SBill Yuan  *    notice, this list of conditions and the following disclaimer in
15*4408d548SBill Yuan  *    the documentation and/or other materials provided with the
16*4408d548SBill Yuan  *    distribution.
17*4408d548SBill Yuan  * 3. Neither the name of The DragonFly Project nor the names of its
18*4408d548SBill Yuan  *    contributors may be used to endorse or promote products derived
19*4408d548SBill Yuan  *    from this software without specific, prior written permission.
20*4408d548SBill Yuan  *
21*4408d548SBill Yuan  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*4408d548SBill Yuan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*4408d548SBill Yuan  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*4408d548SBill Yuan  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25*4408d548SBill Yuan  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*4408d548SBill Yuan  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*4408d548SBill Yuan  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28*4408d548SBill Yuan  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29*4408d548SBill Yuan  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30*4408d548SBill Yuan  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31*4408d548SBill Yuan  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*4408d548SBill Yuan  * SUCH DAMAGE.
33*4408d548SBill Yuan  */
34*4408d548SBill Yuan 
35*4408d548SBill Yuan #include <sys/param.h>
36*4408d548SBill Yuan #include <sys/mbuf.h>
37*4408d548SBill Yuan #include <sys/socket.h>
38*4408d548SBill Yuan #include <sys/sockio.h>
39*4408d548SBill Yuan #include <sys/sysctl.h>
40*4408d548SBill Yuan #include <sys/time.h>
41*4408d548SBill Yuan #include <sys/wait.h>
42*4408d548SBill Yuan 
43*4408d548SBill Yuan #include <arpa/inet.h>
44*4408d548SBill Yuan #include <ctype.h>
45*4408d548SBill Yuan #include <dlfcn.h>
46*4408d548SBill Yuan #include <err.h>
47*4408d548SBill Yuan #include <errno.h>
48*4408d548SBill Yuan #include <grp.h>
49*4408d548SBill Yuan #include <limits.h>
50*4408d548SBill Yuan #include <netdb.h>
51*4408d548SBill Yuan #include <pwd.h>
52*4408d548SBill Yuan #include <sysexits.h>
53*4408d548SBill Yuan #include <signal.h>
54*4408d548SBill Yuan #include <stdio.h>
55*4408d548SBill Yuan #include <stdlib.h>
56*4408d548SBill Yuan #include <stdarg.h>
57*4408d548SBill Yuan #include <string.h>
58*4408d548SBill Yuan #include <timeconv.h>
59*4408d548SBill Yuan #include <unistd.h>
60*4408d548SBill Yuan 
61*4408d548SBill Yuan #include <netinet/in.h>
62*4408d548SBill Yuan #include <netinet/in_systm.h>
63*4408d548SBill Yuan #include <netinet/ip.h>
64*4408d548SBill Yuan #include <netinet/ip_icmp.h>
65*4408d548SBill Yuan #include <netinet/tcp.h>
66*4408d548SBill Yuan #include <net/if.h>
67*4408d548SBill Yuan #include <net/if_dl.h>
68*4408d548SBill Yuan #include <net/route.h>
69*4408d548SBill Yuan #include <net/ethernet.h>
70*4408d548SBill Yuan 
71*4408d548SBill Yuan #include <net/ipfw3/ip_fw3.h>
72*4408d548SBill Yuan #include <net/ipfw3_basic/ip_fw3_table.h>
73*4408d548SBill Yuan #include <net/ipfw3_basic/ip_fw3_sync.h>
74*4408d548SBill Yuan #include <net/ipfw3_basic/ip_fw3_basic.h>
75*4408d548SBill Yuan #include <net/ipfw3_nat/ip_fw3_nat.h>
76*4408d548SBill Yuan #include <net/dummynet3/ip_dummynet3.h>
77*4408d548SBill Yuan 
78*4408d548SBill Yuan #include "ipfw3.h"
79*4408d548SBill Yuan #include "ipfw3log.h"
80*4408d548SBill Yuan 
81*4408d548SBill Yuan extern int	fw3_socket;
82*4408d548SBill Yuan extern int	do_quiet;
83*4408d548SBill Yuan 
84*4408d548SBill Yuan 
85*4408d548SBill Yuan void
resetlog(int ac,char * av[])86*4408d548SBill Yuan resetlog(int ac, char *av[])
87*4408d548SBill Yuan {
88*4408d548SBill Yuan 	int rulenum;
89*4408d548SBill Yuan 	int failed = EX_OK;
90*4408d548SBill Yuan 
91*4408d548SBill Yuan 	NEXT_ARG;
92*4408d548SBill Yuan 
93*4408d548SBill Yuan 	if (!ac) {
94*4408d548SBill Yuan 		/* clear all entries */
95*4408d548SBill Yuan 		if (setsockopt(fw3_socket, IPPROTO_IP,
96*4408d548SBill Yuan 					IP_FW_RESETLOG, NULL, 0) < 0)
97*4408d548SBill Yuan 			err(EX_UNAVAILABLE, "setsockopt(IP_FW_RESETLOG)");
98*4408d548SBill Yuan 		if (!do_quiet)
99*4408d548SBill Yuan 			printf("Logging counts reset.\n");
100*4408d548SBill Yuan 
101*4408d548SBill Yuan 		return;
102*4408d548SBill Yuan 	}
103*4408d548SBill Yuan 
104*4408d548SBill Yuan 	while (ac) {
105*4408d548SBill Yuan 		/* Rule number */
106*4408d548SBill Yuan 		if (isdigit(**av)) {
107*4408d548SBill Yuan 			rulenum = atoi(*av);
108*4408d548SBill Yuan 			NEXT_ARG;
109*4408d548SBill Yuan 			if (setsockopt(fw3_socket, IPPROTO_IP,
110*4408d548SBill Yuan 				IP_FW_RESETLOG, &rulenum, sizeof rulenum)) {
111*4408d548SBill Yuan 				warn("rule %u: setsockopt(IP_FW_RESETLOG)",
112*4408d548SBill Yuan 						rulenum);
113*4408d548SBill Yuan 				failed = EX_UNAVAILABLE;
114*4408d548SBill Yuan 			} else if (!do_quiet)
115*4408d548SBill Yuan 				printf("Entry %d logging count reset\n",
116*4408d548SBill Yuan 						rulenum);
117*4408d548SBill Yuan 		} else {
118*4408d548SBill Yuan 			errx(EX_DATAERR, "invalid rule number ``%s''", *av);
119*4408d548SBill Yuan 		}
120*4408d548SBill Yuan 	}
121*4408d548SBill Yuan 	if (failed != EX_OK)
122*4408d548SBill Yuan 		exit(failed);
123*4408d548SBill Yuan }
124*4408d548SBill Yuan 
125*4408d548SBill Yuan void
log_main(int ac,char ** av)126*4408d548SBill Yuan log_main(int ac, char **av)
127*4408d548SBill Yuan {
128*4408d548SBill Yuan 	if (!strncmp(*av, "reset", strlen(*av))) {
129*4408d548SBill Yuan 		resetlog(ac, av);
130*4408d548SBill Yuan 	} else if (!strncmp(*av, "off", strlen(*av))) {
131*4408d548SBill Yuan 
132*4408d548SBill Yuan 	} else if (!strncmp(*av, "on", strlen(*av))) {
133*4408d548SBill Yuan 
134*4408d548SBill Yuan 	} else {
135*4408d548SBill Yuan 		errx(EX_USAGE, "bad command `%s'", *av);
136*4408d548SBill Yuan 	}
137*4408d548SBill Yuan }
138*4408d548SBill Yuan 
139