1: /*************************************************************************
2: * (C) 2022 CloudSigma AG - Sofia/Bulgaria
3: * by Michael Pounov <misho@elwix.org>
4: **************************************************************************/
5: #include "fwsync.h"
6:
7:
8: MALLOC_DEFINE(M_FWSYNC, "fwsync_memory", "FWSync - memory");
9:
10: static struct ipfw_sopt_handler soc[] = {
11: { IP_FW_SYNC_XCONFIG, 0, HDIR_SET, fwsync_cfg },
12: { IP_FW_SYNC_DESTROY, 0, HDIR_SET, fwsync_destroy },
13: { IP_FW_SYNC_XGETCONFIG, 0, HDIR_GET, fwsync_get_cfg },
14: { IP_FW_SYNC_LIST, 0, HDIR_GET, fwsync_list },
15: { IP_FW_SYNC_START, 0, HDIR_SET, fwsync_start },
16: { IP_FW_SYNC_STOP, 0, HDIR_SET, fwsync_stop },
17: };
18:
19: static volatile int fwsync_hooked = 0;
20: struct fwsync_context fws_ctx = { 0 };
21: int fwsync_debug = DRV_DEBUG;
22: static struct sysctl_ctx_list fwsync_sysctl_ctx;
23: struct cfg_sync fws_cfg;
24: //static struct sysctl_oid *fws_sysctl_oid, *fws_sysctl_dir;
25:
26: struct task fws_sndpkt_task;
27:
28: SYSCTL_NODE(_net_inet_ip, IFT_FWSYNC, fwsync, CTLFLAG_RW, 0, "IPFW Sync - Sync firewall states");
29: SYSCTL_INT(_net_inet_ip_fwsync, OID_AUTO, debug, CTLFLAG_RW, &fwsync_debug, 0, "Debug driver");
30:
31: static int
32: fws_fini(void *arg)
33: {
34: DTRACE();
35:
36: if (!fwsync_hooked)
37: return EBUSY;
38:
39: if (fws_cfg.cfg.on || fws_ctx.config) {
40: uprintf("Unable to unload ELWIX %s driver, cause you have active configuration.\n"
41: "Before unload driver flush configuration!\n", DRV_NAME);
42: return EBUSY;
43: }
44:
45: IPFW_DEL_SOPT_HANDLER(1, soc);
46:
47: fwsync_hooked = 0;
48:
49: /* sysctl context */
50: sysctl_ctx_free(&fwsync_sysctl_ctx);
51:
52: uprintf("Unloaded ELWIX %s driver version %d ...\n", DRV_NAME, DRV_VERSION);
53: return 0;
54: }
55:
56: static int
57: fws_shut(void *arg)
58: {
59: DTRACE();
60:
61: fws_fini(arg);
62:
63: return 0;
64: }
65:
66: static int
67: fws_init(void *arg)
68: {
69: DTRACE();
70:
71: if (fwsync_hooked)
72: return 0;
73:
74: memset(&fws_cfg, 0, sizeof fws_cfg);
75:
76: /* sysctl context */
77: sysctl_ctx_init(&fwsync_sysctl_ctx);
78:
79: IPFW_ADD_SOPT_HANDLER(1, soc);
80:
81: memset(&fws_ctx, 0, sizeof fws_ctx);
82:
83: fwsync_hooked = 1;
84: uprintf("Loaded ELWIX %s driver version %d ...\n", DRV_NAME, DRV_VERSION);
85: return 0;
86: }
87: static int
88: fwsync_main(module_t m, int what, void *arg)
89: {
90: int ret = 0;
91:
92: switch (what) {
93: case MOD_LOAD:
94: ret = fws_init(arg);
95: break;
96: case MOD_UNLOAD:
97: ret = fws_fini(arg);
98: break;
99: case MOD_SHUTDOWN:
100: ret = fws_shut(arg);
101: break;
102: case MOD_QUIESCE:
103: /* don't unload driver if there have configured driver */
104: if (fws_cfg.cfg.on || fws_ctx.config)
105: ret = EBUSY;
106: break;
107: default:
108: ret = EINVAL;
109: break;
110: }
111:
112: return ret;
113: }
114:
115: static moduledata_t fwsync_mod = {
116: "ipfw_sync",
117: fwsync_main,
118: NULL
119: };
120:
121: DECLARE_MODULE(ipfw_sync, fwsync_mod, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY);
122: MODULE_DEPEND(ipfw_sync, ipfw, 3, 3, 3);
123: MODULE_VERSION(ipfw_sync, DRV_VERSION);
124:
125: SYSINIT(fws_init, SI_SUB_PROTO_FIREWALL, (SI_ORDER_ANY - 128), fws_init, NULL);
126: SYSUNINIT(fws_fini, SI_SUB_PROTO_FIREWALL, (SI_ORDER_ANY - 128), fws_fini, NULL);
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>