Annotation of fwsync/driver/fwsync_mod.c, revision 1.5

1.1       misho       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;
1.2       misho      27: struct taskqueue *fws_tq;
1.5     ! misho      28: struct callout fws_co;
1.2       misho      29: struct mtx fws_mtx_c, fws_mtx_e;
                     30: fwsync_sndpkt_t fwsync_sndpkt; 
1.1       misho      31: 
                     32: SYSCTL_NODE(_net_inet_ip, IFT_FWSYNC, fwsync, CTLFLAG_RW, 0, "IPFW Sync - Sync firewall states");
                     33: SYSCTL_INT(_net_inet_ip_fwsync, OID_AUTO, debug, CTLFLAG_RW, &fwsync_debug, 0, "Debug driver");
                     34: 
                     35: static int
                     36: fws_fini(void *arg)
                     37: {
1.2       misho      38:        struct fws_sndpkt *p;
                     39: 
1.1       misho      40:        DTRACE();
                     41: 
                     42:        if (!fwsync_hooked)
                     43:                return EBUSY;
                     44: 
                     45:        if (fws_cfg.cfg.on || fws_ctx.config) {
                     46:                uprintf("Unable to unload ELWIX %s driver, cause you have active configuration.\n"
                     47:                                "Before unload driver flush configuration!\n", DRV_NAME);
                     48:                return EBUSY;
                     49:        }
                     50: 
                     51:        IPFW_DEL_SOPT_HANDLER(1, soc);
                     52: 
1.2       misho      53:        if (fws_tq)
                     54:                taskqueue_free(fws_tq);
                     55: 
                     56:        mtx_lock(&fws_mtx_c);
                     57:        while (!TAILQ_EMPTY(&fwsync_sndpkt)) {
                     58:                p = TAILQ_FIRST(&fwsync_sndpkt);
                     59:                TAILQ_REMOVE(&fwsync_sndpkt, p, sp_next);
                     60:                free(p, M_FWSYNC);
                     61:        }
                     62:        mtx_unlock(&fws_mtx_c);
                     63: 
1.5     ! misho      64:        callout_drain(&fws_co);
        !            65: 
1.2       misho      66:        mtx_destroy(&fws_mtx_c);
                     67:        mtx_destroy(&fws_mtx_e);
                     68: 
1.1       misho      69:        fwsync_hooked = 0;
                     70: 
                     71:        /* sysctl context */
                     72:        sysctl_ctx_free(&fwsync_sysctl_ctx);
                     73: 
                     74:        uprintf("Unloaded ELWIX %s driver version %d ...\n", DRV_NAME, DRV_VERSION);
                     75:        return 0;
                     76: }
                     77: 
                     78: static int
                     79: fws_shut(void *arg)
                     80: {
                     81:        DTRACE();
                     82: 
                     83:        fws_fini(arg);
                     84: 
                     85:        return 0;
                     86: }
                     87: 
                     88: static int
                     89: fws_init(void *arg)
                     90: {
                     91:        DTRACE();
                     92: 
                     93:        if (fwsync_hooked)
                     94:                return 0;
                     95: 
                     96:        memset(&fws_cfg, 0, sizeof fws_cfg);
1.2       misho      97:        memset(&fws_ctx, 0, sizeof fws_ctx);
                     98: 
                     99:        TAILQ_INIT(&fwsync_sndpkt);
                    100: 
                    101:        /* mutexes */
                    102:        mtx_init(&fws_mtx_e, "fwsync mtx edge", NULL, MTX_DEF);
                    103:        mtx_init(&fws_mtx_c, "fwsync mtx collector", NULL, MTX_DEF);
                    104: 
                    105:        /* taskqueue */
1.3       misho     106:        TASK_INIT(&fws_sndpkt_task, 0, fwsync_sndpkt_handler, &fwsync_sndpkt);
1.2       misho     107: 
                    108:        fws_tq = taskqueue_create("fwsync_tq", M_NOWAIT, taskqueue_thread_enqueue, &fws_tq);
                    109:        if (!fws_tq) {
                    110:                printf("Failed to allocate fwsync task queue\n");
                    111:                mtx_destroy(&fws_mtx_c);
                    112:                mtx_destroy(&fws_mtx_e);
                    113:                return ENOMEM;
                    114:        } else
                    115:                taskqueue_start_threads(&fws_tq, 1, PI_NET, "fwsync tq");
1.1       misho     116: 
1.5     ! misho     117:        /* callout */
        !           118: 
        !           119:        callout_init_mtx(&fws_co, &fws_mtx_e, 0);
        !           120: 
1.1       misho     121:        /* sysctl context */
                    122:        sysctl_ctx_init(&fwsync_sysctl_ctx);
                    123: 
                    124:        IPFW_ADD_SOPT_HANDLER(1, soc);
                    125: 
                    126:        fwsync_hooked = 1;
                    127:        uprintf("Loaded ELWIX %s driver version %d ...\n", DRV_NAME, DRV_VERSION);
                    128:        return 0;
                    129: }
                    130: static int
                    131: fwsync_main(module_t m, int what, void *arg)
                    132: {
                    133:        int ret = 0;
                    134: 
                    135:        switch (what) {
                    136:                case MOD_LOAD:
                    137:                        ret = fws_init(arg);
                    138:                        break;
                    139:                case MOD_UNLOAD:
                    140:                        ret = fws_fini(arg);
                    141:                        break;
                    142:                case MOD_SHUTDOWN:
                    143:                        ret = fws_shut(arg);
                    144:                        break;
                    145:                case MOD_QUIESCE:
                    146:                        /* don't unload driver if there have configured driver */
                    147:                        if (fws_cfg.cfg.on || fws_ctx.config)
                    148:                                ret = EBUSY;
                    149:                        break;
                    150:                default:
                    151:                        ret = EINVAL;
                    152:                        break;
                    153:        }
                    154: 
                    155:        return ret;
                    156: }
                    157: 
                    158: static moduledata_t fwsync_mod = {
                    159:        "ipfw_sync",
                    160:        fwsync_main,
                    161:        NULL
                    162: };
                    163: 
                    164: DECLARE_MODULE(ipfw_sync, fwsync_mod, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY);
1.4       misho     165: MODULE_DEPEND(ipfw_sync, libalias, 1, 1, 1);
1.1       misho     166: MODULE_DEPEND(ipfw_sync, ipfw, 3, 3, 3);
                    167: MODULE_VERSION(ipfw_sync, DRV_VERSION);
                    168: 
                    169: SYSINIT(fws_init, SI_SUB_PROTO_FIREWALL, (SI_ORDER_ANY - 128), fws_init, NULL);
                    170: SYSUNINIT(fws_fini, SI_SUB_PROTO_FIREWALL, (SI_ORDER_ANY - 128), fws_fini, NULL);

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>