[BACK]Return to pf.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / systat

Annotation of src/usr.bin/systat/pf.c, Revision 1.3

1.3     ! lum         1: /*     $OpenBSD: pf.c,v 1.2 2010/01/19 05:59:20 mcbride Exp $ */
1.1       canacar     2: /*
                      3:  * Copyright (c) 2001, 2007 Can Erkin Acar <canacar@openbsd.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17:
                     18: #include <sys/types.h>
                     19: #include <sys/ioctl.h>
                     20: #include <sys/socket.h>
                     21: #include <sys/param.h>
                     22: #include <net/if.h>
                     23: #include <netinet/in.h>
                     24: #include <netinet/in_systm.h>
                     25: #include <netinet/ip.h>
                     26: #include <net/pfvar.h>
                     27:
                     28: #include <stdio.h>
                     29: #include <stdlib.h>
                     30: #include <string.h>
                     31: #include <ctype.h>
                     32: #include <errno.h>
                     33: #include <err.h>
                     34: #include <unistd.h>
1.2       mcbride    35: #include <syslog.h>
1.1       canacar    36: #include "pfctl_parser.h"
                     37: #include "systat.h"
                     38:
                     39: void print_pf(void);
                     40: int read_pf(void);
                     41: int select_pf(void);
1.3     ! lum        42: void print_fld_double(field_def *, double);
1.1       canacar    43:
                     44: const char     *pf_reasons[PFRES_MAX+1] = PFRES_NAMES;
                     45: const char     *pf_lcounters[LCNT_MAX+1] = LCNT_NAMES;
                     46: const char     *pf_fcounters[FCNT_MAX+1] = FCNT_NAMES;
                     47: const char     *pf_scounters[FCNT_MAX+1] = FCNT_NAMES;
                     48:
                     49: static struct pf_status status;
                     50: extern int pf_dev;
                     51: int num_pf = 0;
                     52:
                     53: field_def fields_pf[] = {
                     54:        {"TYPE", 13, 16, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
                     55:        {"NAME", 12, 24, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0},
                     56:        {"VALUE", 8, 10, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
                     57:        {"RATE", 8, 10, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 60},
                     58:        {"NOTES", 10, 20, 1, FLD_ALIGN_LEFT, -1, 0, 0, 60},
                     59: };
                     60:
                     61: #define FIELD_ADDR(x) (&fields_pf[x])
                     62:
                     63: #define FLD_PF_TYPE    FIELD_ADDR(0)
                     64: #define FLD_PF_NAME    FIELD_ADDR(1)
                     65: #define FLD_PF_VALUE   FIELD_ADDR(2)
                     66: #define FLD_PF_RATE    FIELD_ADDR(3)
                     67: #define FLD_PF_DESC    FIELD_ADDR(4)
                     68:
                     69: /* Define views */
                     70: field_def *view_pf_0[] = {
                     71:        FLD_PF_TYPE, FLD_PF_NAME, FLD_PF_VALUE, FLD_PF_RATE, FLD_PF_DESC, NULL
                     72: };
                     73:
                     74:
                     75: /* Define view managers */
                     76: struct view_manager pf_mgr = {
                     77:        "PF", select_pf, read_pf, NULL, print_header,
                     78:        print_pf, keyboard_callback, NULL, NULL
                     79: };
                     80:
                     81: field_view views_pf[] = {
                     82:        {view_pf_0, "pf", 'P', &pf_mgr},
                     83:        {NULL, NULL, 0, NULL}
                     84: };
                     85:
                     86:
                     87:
                     88: int
                     89: select_pf(void)
                     90: {
                     91:        return (0);
                     92: }
                     93:
                     94: int
                     95: read_pf(void)
                     96: {
                     97:        if (pf_dev < 0) {
                     98:                num_disp = 0;
                     99:                return 0;
                    100:        }
                    101:
                    102:        if (ioctl(pf_dev, DIOCGETSTATUS, &status)) {
                    103:                error("DIOCGETSTATUS: %s", strerror(errno));
                    104:                return (-1);
                    105:        }
                    106:
                    107:        num_disp = 4;
                    108:
                    109:        if (status.ifname[0] != 0)
                    110:                num_disp += 13;
                    111:
                    112:        num_disp += FCNT_MAX + 2;
                    113:        num_disp += SCNT_MAX + 2;
                    114:        num_disp += PFRES_MAX + 1;
                    115:        num_disp += LCNT_MAX + 1;
                    116:
                    117:        return (0);
                    118: }
                    119:
                    120: int
                    121: initpf(void)
                    122: {
                    123:        field_view *v;
                    124:
                    125:        for (v = views_pf; v->name != NULL; v++)
                    126:                add_view(v);
                    127:
                    128:        return(1);
                    129: }
                    130:
                    131: void
                    132: print_fld_double(field_def *fld, double val)
                    133: {
                    134:        int len;
                    135:
                    136:        if (fld == NULL)
                    137:                return;
                    138:
                    139:        len = fld->width;
                    140:        if (len < 1)
                    141:                return;
                    142:
                    143:        tb_start();
                    144:        if (tbprintf("%.2f", val) > len)
                    145:                print_fld_str(fld, "*");
                    146:        else
                    147:                print_fld_tb(fld);
                    148:        tb_end();
                    149: }
                    150:
                    151: #define ADD_LINE_A(t, n, v) \
                    152:        do {                                                    \
                    153:                if (cur >= dispstart && cur < end) {            \
                    154:                        print_fld_str(FLD_PF_TYPE, (t));        \
                    155:                        print_fld_str(FLD_PF_NAME, (n));        \
                    156:                        print_fld_age(FLD_PF_VALUE, (v));       \
                    157:                        end_line();                             \
                    158:                }                                               \
                    159:                if (++cur >= end)                               \
                    160:                        return;                                 \
                    161:        } while (0)
                    162:
                    163: #define ADD_EMPTY_LINE \
                    164:        do {                                                    \
                    165:                if (cur >= dispstart && cur < end)              \
                    166:                        end_line();                             \
                    167:                if (++cur >= end)                               \
                    168:                        return;                                 \
                    169:        } while (0)
                    170:
                    171: #define ADD_LINE_S(t, n, v) \
                    172:        do {                                                    \
                    173:                if (cur >= dispstart && cur < end) {            \
                    174:                        print_fld_str(FLD_PF_TYPE, (t));        \
                    175:                        print_fld_str(FLD_PF_NAME, (n));        \
                    176:                        print_fld_str(FLD_PF_VALUE, (v));       \
                    177:                        end_line();                             \
                    178:                }                                               \
                    179:                if (++cur >= end)                               \
                    180:                        return;                                 \
                    181:        } while (0)
                    182:
                    183: #define ADD_LINE_V(t, n, v) \
                    184:        do {                                                    \
                    185:                if (cur >= dispstart && cur < end) {            \
                    186:                        print_fld_str(FLD_PF_TYPE, (t));        \
                    187:                        print_fld_str(FLD_PF_NAME, (n));        \
                    188:                        print_fld_size(FLD_PF_VALUE, (v));      \
                    189:                        end_line();                             \
                    190:                }                                               \
                    191:                if (++cur >= end)                               \
                    192:                        return;                                 \
                    193:        } while (0)
                    194:
                    195: #define ADD_LINE_VD(t, n, v, d) \
                    196:        do {                                                    \
                    197:                if (cur >= dispstart && cur < end) {            \
                    198:                        print_fld_str(FLD_PF_TYPE, (t));        \
                    199:                        print_fld_str(FLD_PF_NAME, (n));        \
                    200:                        print_fld_size(FLD_PF_VALUE, (v));      \
                    201:                        print_fld_str(FLD_PF_DESC, (d));        \
                    202:                        end_line();                             \
                    203:                }                                               \
                    204:                if (++cur >= end)                               \
                    205:                        return;                                 \
                    206:        } while (0)
                    207:
                    208: #define ADD_LINE_VR(t, n, v, r) \
                    209:        do {                                                    \
                    210:                if (cur >= dispstart && cur < end) {            \
                    211:                        print_fld_str(FLD_PF_TYPE, (t));        \
                    212:                        print_fld_str(FLD_PF_NAME, (n));        \
                    213:                        print_fld_size(FLD_PF_VALUE, (v));      \
                    214:                        print_fld_double(FLD_PF_RATE, (r));     \
                    215:                        end_line();                             \
                    216:                }                                               \
                    217:                if (++cur >= end)                               \
                    218:                        return;                                 \
                    219:        } while (0)
                    220:
                    221:
                    222: void
                    223: print_pf(void)
                    224: {
                    225:        char            *debug;
                    226:        time_t          tm;
                    227:        int             i;
                    228:        struct pf_status *s = &status;
                    229:
                    230:        int cur = 0;
                    231:        int end = dispstart + maxprint;
                    232:        if (end > num_disp)
                    233:                end = num_disp;
                    234:
                    235:        tm = time(NULL) - s->since;
                    236:
                    237:        ADD_LINE_S("pf", "Status", s->running ? "Enabled" : "Disabled");
                    238:        ADD_LINE_A("pf", "Since", tm);
                    239:
                    240:        switch (s->debug) {
1.2       mcbride   241:        case LOG_EMERG:
                    242:                debug = "emerg";
1.1       canacar   243:                break;
1.2       mcbride   244:        case LOG_ALERT:
                    245:                debug = "alert";
1.1       canacar   246:                break;
1.2       mcbride   247:        case LOG_CRIT:
                    248:                debug = "crit";
1.1       canacar   249:                break;
1.2       mcbride   250:        case LOG_ERR:
                    251:                debug = "err";
                    252:                break;
                    253:        case LOG_WARNING:
                    254:                debug = "warning";
                    255:                break;
                    256:        case LOG_NOTICE:
                    257:                debug = "notice";
                    258:                break;
                    259:        case LOG_INFO:
                    260:                debug = "info";
                    261:                break;
                    262:        case LOG_DEBUG:
                    263:                debug = "debug";
1.1       canacar   264:                break;
                    265:        }
                    266:        ADD_LINE_S("pf", "Debug", debug);
                    267:
                    268:        tb_start();
                    269:        tbprintf("0x%08x\n", ntohl(s->hostid));
                    270:        tb_end();
                    271:
                    272:        ADD_LINE_S("pf", "Hostid", tmp_buf);
                    273:
                    274:        if (s->ifname[0] != 0) {
                    275:                ADD_EMPTY_LINE;
                    276:                ADD_LINE_VD(s->ifname, "Bytes In", s->bcounters[0][0], "IPv4");
                    277:                ADD_LINE_VD(s->ifname, "Bytes In", s->bcounters[1][0], "IPv6");
                    278:                ADD_LINE_VD(s->ifname, "Bytes Out", s->bcounters[0][1], "IPv4");
                    279:                ADD_LINE_VD(s->ifname, "Bytes Out", s->bcounters[1][1], "IPv6");
                    280:                ADD_LINE_VD(s->ifname, "Packets In", s->pcounters[0][0][PF_PASS], "IPv4, Passed");
                    281:                ADD_LINE_VD(s->ifname, "Packets In", s->pcounters[1][0][PF_PASS], "IPv6, Passed");
                    282:                ADD_LINE_VD(s->ifname, "Packets In", s->pcounters[0][0][PF_DROP], "IPv4, Blocked");
                    283:                ADD_LINE_VD(s->ifname, "Packets In", s->pcounters[1][0][PF_DROP], "IPv6, Blocked");
                    284:                ADD_LINE_VD(s->ifname, "Packets Out", s->pcounters[0][1][PF_PASS], "IPv4, Passed");
                    285:                ADD_LINE_VD(s->ifname, "Packets Out", s->pcounters[1][1][PF_PASS], "IPv6, Passed");
                    286:                ADD_LINE_VD(s->ifname, "Packets Out", s->pcounters[0][1][PF_DROP], "IPv4, Blocked");
                    287:                ADD_LINE_VD(s->ifname, "Packets Out", s->pcounters[1][1][PF_DROP], "IPv6, Blocked");
                    288:        }
                    289:
                    290:
                    291:        ADD_EMPTY_LINE;
                    292:        ADD_LINE_V("state", "Count", s->states);
                    293:
                    294:        for (i = 0; i < FCNT_MAX; i++) {
                    295:                if (tm > 0)
                    296:                        ADD_LINE_VR("state", pf_fcounters[i], s->fcounters[i],
                    297:                                    (double)s->fcounters[i] / (double)tm);
                    298:                else
                    299:                        ADD_LINE_V("state", pf_fcounters[i], s->fcounters[i]);
                    300:        }
                    301:
                    302:
                    303:        ADD_EMPTY_LINE;
                    304:        ADD_LINE_V("src track", "Count", s->src_nodes);
                    305:
                    306:        for (i = 0; i < SCNT_MAX; i++) {
                    307:                if (tm > 0)
                    308:                        ADD_LINE_VR("src track", pf_scounters[i], s->scounters[i],
                    309:                                    (double)s->scounters[i] / (double)tm);
                    310:                else
                    311:                        ADD_LINE_V("src track", pf_scounters[i], s->scounters[i]);
                    312:        }
                    313:
                    314:        ADD_EMPTY_LINE;
                    315:        for (i = 0; i < PFRES_MAX; i++) {
                    316:                if (tm > 0)
                    317:                        ADD_LINE_VR("counter", pf_reasons[i], s->counters[i],
                    318:                                    (double)s->counters[i] / (double)tm);
                    319:                else
                    320:                        ADD_LINE_V("counter", pf_reasons[i], s->counters[i]);
                    321:        }
                    322:
                    323:        ADD_EMPTY_LINE;
                    324:        for (i = 0; i < LCNT_MAX; i++) {
                    325:                if (tm > 0)
                    326:                        ADD_LINE_VR("limit counter", pf_lcounters[i], s->lcounters[i],
                    327:                                    (double)s->lcounters[i] / (double)tm);
                    328:                else
                    329:                        ADD_LINE_V("limit counter", pf_lcounters[i], s->lcounters[i]);
                    330:        }
                    331: }