On 19/12/17(Tue) 12:43, Martin Pieuchot wrote:
> I'd like to see some information about my tunnels in my NMS.  The
> problem is that there's not standard MIB for this and most vendor
> MIBs are huge and are not easy to implement.
> 
> So here's a diff that export the equivalent of "$ ipsecctl -s flow".
> I'm basically gluing ipsecctl(8) internals into snmpd(8).
> 
> It can be considered as a first step towards a more complete solution.
> So I'd like to hear from people interested to export IPsec information
> via SNMP, what would like to see and do you have a preferred format?

Here's an updated diff including a MIB.  I'm still looking for comments
and inputs.  I'm now considering implementing CISCO-IPSEC-FLOW-MONITOR
mib since that would give us out of the box support for many NMS,
including libreNMS.  However this is a lot of work.

Index: usr.sbin/snmpd/Makefile
===================================================================
RCS file: /cvs/src/usr.sbin/snmpd/Makefile,v
retrieving revision 1.15
diff -u -p -r1.15 Makefile
--- usr.sbin/snmpd/Makefile     3 Jul 2017 22:21:47 -0000       1.15
+++ usr.sbin/snmpd/Makefile     17 Oct 2017 12:04:16 -0000
@@ -4,7 +4,8 @@ PROG=           snmpd
 MAN=           snmpd.8 snmpd.conf.5
 SRCS=          parse.y ber.c log.c control.c snmpe.c \
                    mps.c trap.c mib.c smi.c kroute.c snmpd.c timer.c \
-                   pf.c proc.c usm.c agentx.c traphandler.c util.c
+                   pf.c proc.c usm.c agentx.c traphandler.c util.c \
+                   ipsec.c pfkey.c
 
 LDADD=         -levent -lutil -lkvm -lcrypto
 DPADD=         ${LIBEVENT} ${LIBUTIL}
Index: usr.sbin/snmpd/ipsec.c
===================================================================
RCS file: usr.sbin/snmpd/ipsec.c
diff -N usr.sbin/snmpd/ipsec.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ usr.sbin/snmpd/ipsec.c      6 Feb 2018 16:12:41 -0000
@@ -0,0 +1,105 @@
+/*     $OpenBSD$       */
+
+/*
+ * Copyright (c) 2004, 2005 Hans-Joerg Hoexer <[email protected]>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <sys/socket.h>
+#include <sys/queue.h>
+
+#include <net/pfkeyv2.h>
+#include <netinet/in.h>
+
+#include <err.h>
+#include <string.h>
+#include <stdlib.h>
+#include <event.h>
+
+#include "snmpd.h"
+#include "ipsec.h"
+
+const char *direction[] = {"?", "in", "out"};
+const char *flowtype[] = {"?", "use", "acquire", "require", "deny",
+    "bypass", "dontacq"};
+const char *satype[] = {"?", "esp", "ah", "ipcomp", "tcpmd5", "ipip"};
+const char *auth[] = {"?", "psk", "rsa"};
+
+struct ipsec_rule *
+ipsec_get_rule(uint32_t idx)
+{
+       struct ipsecctl ipsec;
+       struct ipsec_rule *r, *rule = NULL;
+
+       memset(&ipsec, 0, sizeof(ipsec));
+       TAILQ_INIT(&ipsec.rule_queue);
+       ipsec_get_rules(&ipsec);
+
+       while ((r = TAILQ_FIRST(&ipsec.rule_queue)) != NULL) {
+               TAILQ_REMOVE(&ipsec.rule_queue, r, rule_entry);
+               if ((r->nr + 1) == idx)
+                       rule = r;
+               else
+                       free(r);
+       }
+
+       return rule;
+}
+
+void
+ipsec_get_rules(struct ipsecctl *ipsec)
+{
+       struct sadb_msg *msg;
+       struct ipsec_rule *rule;
+       int              mib[4];
+       size_t           need;
+       char            *buf, *lim, *next;
+
+       mib[0] = CTL_NET;
+       mib[1] = PF_KEY;
+       mib[2] = PF_KEY_V2;
+       mib[3] = NET_KEY_SPD_DUMP;
+
+       if (sysctl(mib, 4, NULL, &need, NULL, 0) == -1)
+               err(1, "%s: sysctl", __func__);
+       if (need == 0)
+               return;
+       if ((buf = malloc(need)) == NULL)
+               err(1, "%s: malloc", __func__);
+       if (sysctl(mib, 4, buf, &need, NULL, 0) == -1)
+               err(1, "%s: sysctl", __func__);
+       lim = buf + need;
+
+       for (next = buf; next < lim; next += msg->sadb_msg_len *
+           PFKEYV2_CHUNK) {
+               msg = (struct sadb_msg *)next;
+               if (msg->sadb_msg_len == 0)
+                       break;
+
+               rule = calloc(1, sizeof(struct ipsec_rule));
+               if (rule == NULL)
+                       err(1, "%s: calloc", __func__);
+               rule->nr = ipsec->rule_nr++;
+               rule->type |= RULE_FLOW;
+
+               if (pfkey_parse(msg, rule))
+                       errx(1, "%s: failed to parse PF_KEY message", __func__);
+
+               TAILQ_INSERT_TAIL(&ipsec->rule_queue, rule, rule_entry);
+       }
+
+       free(buf);
+}
Index: usr.sbin/snmpd/ipsec.h
===================================================================
RCS file: usr.sbin/snmpd/ipsec.h
diff -N usr.sbin/snmpd/ipsec.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ usr.sbin/snmpd/ipsec.h      19 Dec 2017 11:33:09 -0000
@@ -0,0 +1,116 @@
+/*     $OpenBSD: ipsecctl.h,v 1.71 2017/04/19 15:59:38 bluhm Exp $     */
+/*
+ * Copyright (c) 2004, 2005 Hans-Joerg Hoexer <[email protected]>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef _IPSECCTL_H_
+#define _IPSECCTL_H_
+
+#define RULE_FLOW      0x01
+#define RULE_SA                0x02
+#define RULE_IKE       0x04
+#define RULE_BUNDLE    0x08
+
+enum {
+       DIRECTION_UNKNOWN, IPSEC_IN, IPSEC_OUT, IPSEC_INOUT
+};
+enum {
+       PROTO_UNKNOWN, IPSEC_ESP, IPSEC_AH, IPSEC_IPCOMP, IPSEC_TCPMD5,
+       IPSEC_IPIP
+};
+enum {
+       TYPE_UNKNOWN, TYPE_USE, TYPE_ACQUIRE, TYPE_REQUIRE, TYPE_DENY,
+       TYPE_BYPASS, TYPE_DONTACQ
+};
+enum {
+       IKE_ACTIVE, IKE_PASSIVE, IKE_DYNAMIC
+};
+
+struct ipsec_addr_wrap {
+       struct pf_addr           address;
+       struct pf_addr           mask;
+       int                      netaddress;
+       sa_family_t              af;
+       char                    *name;
+       struct ipsec_addr_wrap  *next;
+       struct ipsec_addr_wrap  *tail;
+       struct ipsec_addr_wrap  *srcnat;
+};
+
+struct ipsec_auth {
+       char            *srcid;
+       char            *dstid;
+       uint8_t          srcid_type;
+       uint8_t          dstid_type;
+       uint16_t         type;
+};
+
+/* Complete state of one rule. */
+struct ipsec_rule {
+       uint8_t  type;
+
+       struct ipsec_addr_wrap *src;
+       struct ipsec_addr_wrap *dst;
+       struct ipsec_addr_wrap *local;
+       struct ipsec_addr_wrap *peer;
+       struct ipsec_auth *auth;
+
+       char            *tag;           /* pf tag for SAs */
+       char            *p1name;        /* Phase 1 Name */
+       char            *p2name;        /* Phase 2 Name (IPsec-XX) */
+       char            *p2lid;         /* Phase 2 source ID */
+       char            *p2rid;         /* Phase 2 destination ID */
+       char            *p2nid;         /* Phase 2 source NAT-ID */
+       uint8_t          satype;        /* encapsulating prococol */
+       uint8_t          proto;         /* encapsulated protocol */
+       uint8_t          proto2;
+       uint8_t          tmode;
+       uint8_t          direction;
+       uint8_t          flowtype;
+       uint8_t          ikemode;
+       uint8_t          p1ie;
+       uint8_t          p2ie;
+       uint16_t         sport;
+       uint16_t         dport;
+       uint32_t         spi;
+       uint32_t         spi2;
+       uint32_t         nr;
+
+       TAILQ_ENTRY(ipsec_rule) rule_entry;
+};
+
+TAILQ_HEAD(ipsec_rule_queue, ipsec_rule);
+
+struct ipsecctl {
+       uint32_t        rule_nr;
+       struct ipsec_rule_queue rule_queue;
+};
+
+void   ipsec_get_rules(struct ipsecctl *);
+struct ipsec_rule *ipsec_get_rule(uint32_t);
+
+extern const char *direction[];
+extern const char *flowtype[];
+extern const char *satype[];
+extern const char *auth[];
+
+#define PFKEYV2_CHUNK sizeof(uint64_t)
+
+struct sadb_msg;
+
+int    pfkey_parse(struct sadb_msg *, struct ipsec_rule *);
+void   set_ipmask(struct ipsec_addr_wrap *, uint8_t);
+
+#endif /* _IPSECCTL_H_ */
Index: usr.sbin/snmpd/mib.c
===================================================================
RCS file: /cvs/src/usr.sbin/snmpd/mib.c,v
retrieving revision 1.85
diff -u -p -r1.85 mib.c
--- usr.sbin/snmpd/mib.c        18 Dec 2017 05:51:53 -0000      1.85
+++ usr.sbin/snmpd/mib.c        6 Feb 2018 16:12:01 -0000
@@ -1422,6 +1422,7 @@ int        mib_carpifnum(struct oid *, struct 
 struct carpif
        *mib_carpifget(u_int);
 int     mib_memiftable(struct oid *, struct ber_oid *, struct ber_element **);
+int     mib_ipsecflow(struct oid *, struct ber_oid *, struct ber_element **);
 
 static struct oid openbsd_mib[] = {
        { MIB(pfMIBObjects),            OID_MIB },
@@ -1633,6 +1634,26 @@ static struct oid openbsd_mib[] = {
        { MIB(carpIfAdvbase),           OID_TRD, mib_carpiftable },
        { MIB(carpIfAdvskew),           OID_TRD, mib_carpiftable },
        { MIB(carpIfState),             OID_TRD, mib_carpiftable },
+       { MIB(ipsecMIBObjects),         OID_MIB },
+       { MIB(ipsecFlowSAType),         OID_TRD, mib_ipsecflow },
+       { MIB(ipsecFlowDirection),      OID_TRD, mib_ipsecflow },
+       { MIB(ipsecFlowFromAddr),       OID_TRD, mib_ipsecflow },
+       { MIB(ipsecFlowFromMask),       OID_TRD, mib_ipsecflow },
+       { MIB(ipsecFlowSPort),          OID_TRD, mib_ipsecflow },
+       { MIB(ipsecFlowToAddr),         OID_TRD, mib_ipsecflow },
+       { MIB(ipsecFlowToMask),         OID_TRD, mib_ipsecflow },
+       { MIB(ipsecFlowDPort),          OID_TRD, mib_ipsecflow },
+#if notyet
+       /* Unprivileged user cannot see commented out information. */
+       { MIB(ipsecFlowLocal),          OID_TRD, mib_ipsecflow },
+#endif
+       { MIB(ipsecFlowPeer),           OID_TRD, mib_ipsecflow },
+#if notyet
+       { MIB(ipsecFlowAuthSrcID),      OID_TRD, mib_ipsecflow },
+       { MIB(ipsecFlowAuthDstID),      OID_TRD, mib_ipsecflow },
+       { MIB(ipsecFlowAuthType),       OID_TRD, mib_ipsecflow },
+#endif
+       { MIB(ipsecFlowType),           OID_TRD, mib_ipsecflow },
        { MIB(memMIBObjects),           OID_MIB },
        { MIB(memMIBVersion),           OID_RD, mps_getint, NULL, NULL,
            OIDVER_OPENBSD_MEM },
@@ -2831,7 +2852,6 @@ mib_carpiftable(struct oid *oid, struct 
 
        /* Get and verify the current row index */
        idx = o->bo_id[OIDIDX_carpIfEntry];
-
        if ((cif = mib_carpifget(idx)) == NULL)
                return (1);
 
@@ -2877,10 +2897,12 @@ mib_memiftable(struct oid *oid, struct b
        u_int32_t                idx = 0;
        struct kif              *kif;
 
+       /* Get and verify the current row index */
        idx = o->bo_id[OIDIDX_memIfEntry];
        if ((kif = mib_ifget(idx)) == NULL)
                return (1);
 
+       /* Tables need to prepend the OID on their own */
        o->bo_id[OIDIDX_memIfEntry] = kif->if_index;
        ber = ber_add_oid(ber, o);
 
@@ -2891,6 +2913,113 @@ mib_memiftable(struct oid *oid, struct b
        case 2:
                ber = ber_add_integer(ber, 0);
                ber_set_header(ber, BER_CLASS_APPLICATION, SNMP_T_COUNTER64);
+               break;
+       default:
+               return (-1);
+       }
+
+       return (0);
+}
+
+#include "ipsec.h"
+
+int
+mib_ipsecflow(struct oid *oid, struct ber_oid *o, struct ber_element **elm)
+{
+       struct ber_element      *ber = *elm;
+       struct ipsec_rule       *r;
+       u_int32_t                val, idx = 0;
+
+       /* Get and verify the current row index */
+       idx = o->bo_id[OIDIDX_ipsecFlowEntry];
+       if ((r = ipsec_get_rule(idx)) == NULL)
+               return (1);
+
+       /* Tables need to prepend the OID on their own */
+       o->bo_id[OIDIDX_ipsecFlowEntry] = idx;
+       ber = ber_add_oid(ber, o);
+
+       switch (o->bo_id[OIDIDX_ipsecFlow]) {
+       case 1: /* index */
+               ber = ber_add_integer(ber, idx);
+               break;
+       case 2: /* satype */
+               ber = ber_add_string(ber, satype[r->satype]);
+               break;
+       case 3: /* direction */
+               ber = ber_add_string(ber, direction[r->direction]);
+               break;
+       case 4: /* from address */
+               val = r->src->address.v4.s_addr;
+               ber = ber_add_nstring(ber, (char *)&val, sizeof(val));
+               ber_set_header(ber, BER_CLASS_APPLICATION, SNMP_T_IPADDR);
+               break;
+       case 5: /* from netmask */
+               val = r->src->mask.v4.s_addr;
+               ber = ber_add_nstring(ber, (char *)&val, sizeof(val));
+               ber_set_header(ber, BER_CLASS_APPLICATION, SNMP_T_IPADDR);
+               break;
+       case 6: /* sport */
+               ber = ber_add_integer(ber, ntohs(r->sport));
+               break;
+       case 7: /* to address */
+               val = r->dst->address.v4.s_addr;
+               ber = ber_add_nstring(ber, (char *)&val, sizeof(val));
+               ber_set_header(ber, BER_CLASS_APPLICATION, SNMP_T_IPADDR);
+               break;
+       case 8: /* to netmask */
+               val = r->dst->mask.v4.s_addr;
+               ber = ber_add_nstring(ber, (char *)&val, sizeof(val));
+               ber_set_header(ber, BER_CLASS_APPLICATION, SNMP_T_IPADDR);
+               break;
+       case 9: /* dport */
+               ber = ber_add_integer(ber, ntohs(r->dport));
+               break;
+#if notyet
+       case 10: /* local*/
+               if (r->local == NULL) {
+                       ber = ber_add_string(ber, "");
+                       break;
+               }
+               val = r->local->address.v4.s_addr;
+               ber = ber_add_nstring(ber, (char *)&val, sizeof(val));
+               ber_set_header(ber, BER_CLASS_APPLICATION, SNMP_T_IPADDR);
+               break;
+#endif
+       case 11: /* peer */
+               if (r->peer == NULL) {
+                       ber = ber_add_string(ber, "");
+                       break;
+               }
+               val = r->peer->address.v4.s_addr;
+               ber = ber_add_nstring(ber, (char *)&val, sizeof(val));
+               ber_set_header(ber, BER_CLASS_APPLICATION, SNMP_T_IPADDR);
+               break;
+#if notyet
+       case 12: /* srcid */
+               if (r->auth == NULL || r->auth->srcid == NULL) {
+                       ber = ber_add_string(ber, "");
+                       break;
+               }
+               ber = ber_add_string(ber, r->auth->srcid);
+               break;
+       case 13: /* dstid */
+               if (r->auth == NULL || r->auth->dstid == NULL) {
+                       ber = ber_add_string(ber, "");
+                       break;
+               }
+               ber = ber_add_string(ber, r->auth->dstid);
+               break;
+       case 14: /* auth type */
+               if (r->auth == NULL || r->auth->type == 0) {
+                       ber = ber_add_string(ber, "");
+                       break;
+               }
+               ber = ber_add_string(ber, auth[r->auth->type]);
+               break;
+#endif
+       case 15: /* type */
+               ber = ber_add_string(ber, flowtype[r->flowtype]);
                break;
        default:
                return (-1);
Index: usr.sbin/snmpd/mib.h
===================================================================
RCS file: /cvs/src/usr.sbin/snmpd/mib.h,v
retrieving revision 1.38
diff -u -p -r1.38 mib.h
--- usr.sbin/snmpd/mib.h        7 Mar 2016 19:33:26 -0000       1.38
+++ usr.sbin/snmpd/mib.h        6 Feb 2018 15:47:03 -0000
@@ -677,6 +677,26 @@
 #define MIB_relaydHostTrapRetry                MIB_relaydHostTrap, 8
 #define MIB_relaydHostTrapRetryCount   MIB_relaydHostTrap, 9
 #define MIB_ipsecMIBObjects            MIB_openBSD, 4
+#define MIB_ipsecFlowTable             MIB_ipsecMIBObjects, 1
+#define MIB_ipsecFlowEntry             MIB_ipsecFlowTable, 1
+#define OIDIDX_ipsecFlow               10
+#define OIDIDX_ipsecFlowEntry          11
+#define MIB_ipsecFlowIndex             MIB_ipsecFlowEntry, 1
+#define MIB_ipsecFlowSAType            MIB_ipsecFlowEntry, 2
+#define MIB_ipsecFlowDirection         MIB_ipsecFlowEntry, 3
+#define MIB_ipsecFlowFromAddr          MIB_ipsecFlowEntry, 4
+#define MIB_ipsecFlowFromMask          MIB_ipsecFlowEntry, 5
+#define MIB_ipsecFlowSPort             MIB_ipsecFlowEntry, 6
+#define MIB_ipsecFlowToAddr            MIB_ipsecFlowEntry, 7
+#define MIB_ipsecFlowToMask            MIB_ipsecFlowEntry, 8
+#define MIB_ipsecFlowDPort             MIB_ipsecFlowEntry, 9
+#define MIB_ipsecFlowLocal             MIB_ipsecFlowEntry, 10
+#define MIB_ipsecFlowPeer              MIB_ipsecFlowEntry, 11
+#define MIB_ipsecFlowAuthSrcID         MIB_ipsecFlowEntry, 12
+#define MIB_ipsecFlowAuthDstID         MIB_ipsecFlowEntry, 13
+#define MIB_ipsecFlowAuthType          MIB_ipsecFlowEntry, 14
+#define MIB_ipsecFlowType              MIB_ipsecFlowEntry, 15
+
 #define MIB_memMIBObjects              MIB_openBSD, 5
 #define MIB_memMIBVersion              MIB_memMIBObjects, 1
 #define OIDVER_OPENBSD_MEM             1
@@ -1234,6 +1254,19 @@
        { MIBDECL(sensorValue) },                       \
        { MIBDECL(sensorUnits) },                       \
        { MIBDECL(sensorStatus) },                      \
+       { MIBDECL(ipsecMIBObjects) },                   \
+       { MIBDECL(ipsecFlowTable) },                    \
+       { MIBDECL(ipsecFlowEntry) },                    \
+       { MIBDECL(ipsecFlowSAType) },                   \
+       { MIBDECL(ipsecFlowDirection) },                \
+       { MIBDECL(ipsecFlowFromAddr) },                 \
+       { MIBDECL(ipsecFlowFromMask) },                 \
+       { MIBDECL(ipsecFlowSPort) },                    \
+       { MIBDECL(ipsecFlowToAddr) },                   \
+       { MIBDECL(ipsecFlowToMask) },                   \
+       { MIBDECL(ipsecFlowDPort) },                    \
+       { MIBDECL(ipsecFlowPeer) },                     \
+       { MIBDECL(ipsecFlowType) },                     \
        { MIBDECL(memMIBObjects) },                     \
        { MIBDECL(memMIBVersion) },                     \
        { MIBDECL(memIfTable) },                        \
Index: usr.sbin/snmpd/pfkey.c
===================================================================
RCS file: usr.sbin/snmpd/pfkey.c
diff -N usr.sbin/snmpd/pfkey.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ usr.sbin/snmpd/pfkey.c      19 Dec 2017 11:32:25 -0000
@@ -0,0 +1,364 @@
+/*     $OpenBSD: pfkey.c,v 1.60 2017/04/19 15:59:38 bluhm Exp $        */
+/*
+ * Copyright (c) 2003, 2004 Henning Brauer <[email protected]>
+ * Copyright (c) 2003, 2004 Markus Friedl <[email protected]>
+ * Copyright (c) 2004, 2005 Hans-Joerg Hoexer <[email protected]>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/queue.h>
+#include <sys/uio.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/ip_ipsp.h>
+#include <net/pfkeyv2.h>
+
+#include <err.h>
+#include <errno.h>
+#include <event.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <poll.h>
+#include <unistd.h>
+
+#include "snmpd.h"
+#include "ipsec.h"
+
+#define ROUNDUP(x) (((x) + (PFKEYV2_CHUNK - 1)) & ~(PFKEYV2_CHUNK - 1))
+#define IOV_CNT 20
+
+int
+pfkey_parse(struct sadb_msg *msg, struct ipsec_rule *rule)
+{
+       struct sadb_ext         *ext;
+       struct sadb_address     *saddr;
+       struct sadb_protocol    *sproto;
+       struct sadb_ident       *sident;
+       struct sockaddr         *sa;
+       struct sockaddr_in      *sa_in;
+       struct sockaddr_in6     *sa_in6;
+       int                      len;
+
+       switch (msg->sadb_msg_satype) {
+       case SADB_SATYPE_ESP:
+               rule->satype = IPSEC_ESP;
+               break;
+       case SADB_SATYPE_AH:
+               rule->satype = IPSEC_AH;
+               break;
+       case SADB_X_SATYPE_IPCOMP:
+               rule->satype = IPSEC_IPCOMP;
+               break;
+       case SADB_X_SATYPE_IPIP:
+               rule->satype = IPSEC_IPIP;
+               break;
+       default:
+               return (1);
+       }
+
+       for (ext = (struct sadb_ext *)(msg + 1);
+           (size_t)((u_int8_t *)ext - (u_int8_t *)msg) <
+           msg->sadb_msg_len * PFKEYV2_CHUNK && ext->sadb_ext_len > 0;
+           ext = (struct sadb_ext *)((u_int8_t *)ext +
+           ext->sadb_ext_len * PFKEYV2_CHUNK)) {
+               switch (ext->sadb_ext_type) {
+               case SADB_EXT_ADDRESS_SRC:
+                       saddr = (struct sadb_address *)ext;
+                       sa = (struct sockaddr *)(saddr + 1);
+
+                       rule->local = calloc(1, sizeof(struct ipsec_addr_wrap));
+                       if (rule->local == NULL)
+                               err(1, "pfkey_parse: calloc");
+
+                       rule->local->af = sa->sa_family;
+                       switch (sa->sa_family) {
+                       case AF_INET:
+                               bcopy(&((struct sockaddr_in *)sa)->sin_addr,
+                                   &rule->local->address.v4,
+                                   sizeof(struct in_addr));
+                               set_ipmask(rule->local, 32);
+                               break;
+                       case AF_INET6:
+                               bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
+                                   &rule->local->address.v6,
+                                   sizeof(struct in6_addr));
+                               set_ipmask(rule->local, 128);
+                               break;
+                       default:
+                               return (1);
+                       }
+                       break;
+
+
+               case SADB_EXT_ADDRESS_DST:
+                       saddr = (struct sadb_address *)ext;
+                       sa = (struct sockaddr *)(saddr + 1);
+
+                       rule->peer = calloc(1, sizeof(struct ipsec_addr_wrap));
+                       if (rule->peer == NULL)
+                               err(1, "pfkey_parse: calloc");
+
+                       rule->peer->af = sa->sa_family;
+                       switch (sa->sa_family) {
+                       case AF_INET:
+                               bcopy(&((struct sockaddr_in *)sa)->sin_addr,
+                                   &rule->peer->address.v4,
+                                   sizeof(struct in_addr));
+                               set_ipmask(rule->peer, 32);
+                               break;
+                       case AF_INET6:
+                               bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
+                                   &rule->peer->address.v6,
+                                   sizeof(struct in6_addr));
+                               set_ipmask(rule->peer, 128);
+                               break;
+                       default:
+                               return (1);
+                       }
+                       break;
+
+               case SADB_EXT_IDENTITY_SRC:
+                       sident = (struct sadb_ident *)ext;
+                       len = (sident->sadb_ident_len * sizeof(uint64_t)) -
+                           sizeof(struct sadb_ident);
+
+                       if (rule->auth == NULL) {
+                               rule->auth = calloc(1, sizeof(struct
+                                   ipsec_auth));
+                               if (rule->auth == NULL)
+                                       err(1, "pfkey_parse: calloc");
+                       }
+
+                       rule->auth->srcid = calloc(1, len);
+                       if (rule->auth->srcid == NULL)
+                               err(1, "pfkey_parse: calloc");
+
+                       strlcpy(rule->auth->srcid, (char *)(sident + 1), len);
+                       break;
+
+               case SADB_EXT_IDENTITY_DST:
+                       sident = (struct sadb_ident *)ext;
+                       len = (sident->sadb_ident_len * sizeof(uint64_t)) -
+                           sizeof(struct sadb_ident);
+
+                       if (rule->auth == NULL) {
+                               rule->auth = calloc(1, sizeof(struct
+                                   ipsec_auth));
+                               if (rule->auth == NULL)
+                                       err(1, "pfkey_parse: calloc");
+                       }
+
+                       rule->auth->dstid = calloc(1, len);
+                       if (rule->auth->dstid == NULL)
+                               err(1, "pfkey_parse: calloc");
+
+                       strlcpy(rule->auth->dstid, (char *)(sident + 1), len);
+                       break;
+
+               case SADB_X_EXT_PROTOCOL:
+                       sproto = (struct sadb_protocol *)ext;
+                       if (sproto->sadb_protocol_direction == 0)
+                               rule->proto = sproto->sadb_protocol_proto;
+                       break;
+
+               case SADB_X_EXT_FLOW_TYPE:
+                       sproto = (struct sadb_protocol *)ext;
+
+                       switch (sproto->sadb_protocol_direction) {
+                       case IPSP_DIRECTION_IN:
+                               rule->direction = IPSEC_IN;
+                               break;
+                       case IPSP_DIRECTION_OUT:
+                               rule->direction = IPSEC_OUT;
+                               break;
+                       default:
+                               return (1);
+                       }
+                       switch (sproto->sadb_protocol_proto) {
+                       case SADB_X_FLOW_TYPE_USE:
+                               rule->flowtype = TYPE_USE;
+                               break;
+                       case SADB_X_FLOW_TYPE_ACQUIRE:
+                               rule->flowtype = TYPE_ACQUIRE;
+                               break;
+                       case SADB_X_FLOW_TYPE_REQUIRE:
+                               rule->flowtype = TYPE_REQUIRE;
+                               break;
+                       case SADB_X_FLOW_TYPE_DENY:
+                               rule->flowtype = TYPE_DENY;
+                               break;
+                       case SADB_X_FLOW_TYPE_BYPASS:
+                               rule->flowtype = TYPE_BYPASS;
+                               break;
+                       case SADB_X_FLOW_TYPE_DONTACQ:
+                               rule->flowtype = TYPE_DONTACQ;
+                               break;
+                       default:
+                               rule->flowtype = TYPE_UNKNOWN;
+                               break;
+                       }
+                       break;
+
+               case SADB_X_EXT_SRC_FLOW:
+                       saddr = (struct sadb_address *)ext;
+                       sa = (struct sockaddr *)(saddr + 1);
+
+                       if (rule->src == NULL) {
+                               rule->src = calloc(1,
+                                   sizeof(struct ipsec_addr_wrap));
+                               if (rule->src == NULL)
+                                       err(1, "pfkey_parse: calloc");
+                       }
+
+                       rule->src->af = sa->sa_family;
+                       switch (sa->sa_family) {
+                       case AF_INET:
+                               bcopy(&((struct sockaddr_in *)sa)->sin_addr,
+                                   &rule->src->address.v4,
+                                   sizeof(struct in_addr));
+                               rule->sport =
+                                   ((struct sockaddr_in *)sa)->sin_port;
+                               break;
+                       case AF_INET6:
+                               bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
+                                   &rule->src->address.v6,
+                                   sizeof(struct in6_addr));
+                               rule->sport =
+                                   ((struct sockaddr_in6 *)sa)->sin6_port;
+                               break;
+                       default:
+                               return (1);
+                       }
+                       break;
+
+               case SADB_X_EXT_DST_FLOW:
+                       saddr = (struct sadb_address *)ext;
+                       sa = (struct sockaddr *)(saddr + 1);
+
+                       if (rule->dst == NULL) {
+                               rule->dst = calloc(1,
+                                   sizeof(struct ipsec_addr_wrap));
+                               if (rule->dst == NULL)
+                                       err(1, "pfkey_parse: calloc");
+                       }
+
+                       rule->dst->af = sa->sa_family;
+                       switch (sa->sa_family) {
+                       case AF_INET:
+                               bcopy(&((struct sockaddr_in *)sa)->sin_addr,
+                                   &rule->dst->address.v4,
+                                   sizeof(struct in_addr));
+                               rule->dport =
+                                   ((struct sockaddr_in *)sa)->sin_port;
+                               break;
+                       case AF_INET6:
+                               bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
+                                   &rule->dst->address.v6,
+                                   sizeof(struct in6_addr));
+                               rule->dport =
+                                   ((struct sockaddr_in6 *)sa)->sin6_port;
+                               break;
+                       default:
+                               return (1);
+                       }
+                       break;
+
+
+               case SADB_X_EXT_SRC_MASK:
+                       saddr = (struct sadb_address *)ext;
+                       sa = (struct sockaddr *)(saddr + 1);
+
+                       if (rule->src == NULL) {
+                               rule->src = calloc(1,
+                                   sizeof(struct ipsec_addr_wrap));
+                               if (rule->src == NULL)
+                                       err(1, "pfkey_parse: calloc");
+                       }
+
+                       rule->src->af = sa->sa_family;
+                       switch (sa->sa_family) {
+                       case AF_INET:
+                               sa_in = (struct sockaddr_in *)sa;
+                               bcopy(&sa_in->sin_addr, &rule->src->mask.v4,
+                                   sizeof(struct in_addr));
+                               break;
+                       case AF_INET6:
+                               sa_in6 = (struct sockaddr_in6 *)sa;
+                               bcopy(&sa_in6->sin6_addr, &rule->src->mask.v6,
+                                   sizeof(struct in6_addr));
+                               break;
+
+                       default:
+                               return (1);
+                       }
+                       break;
+
+               case SADB_X_EXT_DST_MASK:
+                       saddr = (struct sadb_address *)ext;
+                       sa = (struct sockaddr *)(saddr + 1);
+
+                       if (rule->dst == NULL) {
+                               rule->dst = calloc(1,
+                                   sizeof(struct ipsec_addr_wrap));
+                               if (rule->dst == NULL)
+                                       err(1, "pfkey_parse: calloc");
+                       }
+
+                       rule->dst->af = sa->sa_family;
+                       switch (sa->sa_family) {
+                       case AF_INET:
+                               sa_in = (struct sockaddr_in *)sa;
+                               bcopy(&sa_in->sin_addr, &rule->dst->mask.v4,
+                                   sizeof(struct in_addr));
+                               break;
+                       case AF_INET6:
+                               sa_in6 = (struct sockaddr_in6 *)sa;
+                               bcopy(&sa_in6->sin6_addr, &rule->dst->mask.v6,
+                                   sizeof(struct in6_addr));
+                               break;
+                       default:
+                               return (1);
+                       }
+                       break;
+
+               default:
+                       return (1);
+               }
+       }
+
+       return (0);
+}
+
+#define ipsec_addr pf_addr
+
+void
+set_ipmask(struct ipsec_addr_wrap *address, u_int8_t b)
+{
+       struct ipsec_addr       *ipa;
+       int                      i, j = 0;
+
+       ipa = &address->mask;
+       bzero(ipa, sizeof(struct ipsec_addr));
+
+       while (b >= 32) {
+               ipa->addr32[j++] = 0xffffffff;
+               b -= 32;
+       }
+       for (i = 31; i > 31 - b; --i)
+               ipa->addr32[j] |= (1 << i);
+       if (b)
+               ipa->addr32[j] = htonl(ipa->addr32[j]);
+}
Index: share/snmp/Makefile
===================================================================
RCS file: /cvs/src/share/snmp/Makefile,v
retrieving revision 1.6
diff -u -p -r1.6 Makefile
--- share/snmp/Makefile 12 Jun 2017 18:49:34 -0000      1.6
+++ share/snmp/Makefile 6 Feb 2018 15:59:22 -0000
@@ -3,7 +3,7 @@
 FILES= OPENBSD-SNMPD-CONF.txt OPENBSD-BASE-MIB.txt
 FILES+=        OPENBSD-MEM-MIB.txt OPENBSD-SENSORS-MIB.txt
 FILES+= OPENBSD-CARP-MIB.txt OPENBSD-PF-MIB.txt
-FILES+= OPENBSD-RELAYD-MIB.txt
+FILES+= OPENBSD-RELAYD-MIB.txt OPENBSD-IPSEC-FLOW-MIB.txt
 
 all clean cleandir depend obj tags: _SUBDIRUSE
 
Index: share/snmp/OPENBSD-IPSEC-FLOW-MIB.txt
===================================================================
RCS file: share/snmp/OPENBSD-IPSEC-FLOW-MIB.txt
diff -N share/snmp/OPENBSD-IPSEC-FLOW-MIB.txt
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ share/snmp/OPENBSD-IPSEC-FLOW-MIB.txt       6 Feb 2018 16:07:16 -0000
@@ -0,0 +1,175 @@
+-- $OpenBSD$
+--
+-- Copyright (c) 2018 Martin Pieuchot <[email protected]>
+-- 
+-- Permission to use, copy, modify, and distribute this document for any
+-- purpose with or without fee is hereby granted, provided that the above
+-- copyright notice and this permission notice appear in all copies.
+--
+-- THE DOCUMENT IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+-- WITH REGARD TO THIS DOCUMENT INCLUDING ALL IMPLIED WARRANTIES OF
+-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS DOCUMENT.
+
+OPENBSD-IPSEC-FLOW-MIB DEFINITIONS ::= BEGIN
+
+IMPORTS
+       MODULE-IDENTITY, NOTIFICATION-TYPE, OBJECT-TYPE,
+       Integer32, IpAddress
+           FROM SNMPv2-SMI
+       DisplayString
+           FROM SNMPv2-TC
+       openBSD
+           FROM OPENBSD-BASE-MIB
+       MODULE-COMPLIANCE, OBJECT-GROUP
+           FROM SNMPv2-CONF;
+
+ipsecMIBObjects MODULE-IDENTITY
+       LAST-UPDATED "201802060000Z"
+       ORGANIZATION "OpenBSD"
+       CONTACT-INFO
+           "Editor:     Martin Pieuchot
+           EMail:      [email protected]
+           WWW:        https://www.openbsd.org/";
+       DESCRIPTION
+           "The MIB module for gathering information about IPsec flows."
+       REVISION     "201802060000Z"
+       DESCRIPTION  "Add the OPENBSD-IPSEC-FLOW-MIB to snmpd."
+       ::= { openBSD 4 }
+
+
+--
+-- Core MIB elements
+--
+
+-- ipsecFlowTable              OBJECT IDENTIFIER ::= { ipsecMIBObjects 1 }
+
+--
+-- MIB details
+--
+
+ipsecFlowTable OBJECT-TYPE
+       SYNTAX          SEQUENCE OF IpsecFlowEntry
+       MAX-ACCESS      not-accessible
+       STATUS          current
+       DESCRIPTION
+           "A list of IPsec flows."
+       ::= { ipsecMIBObjects 1 }    
+
+ipsecFlowEntry OBJECT-TYPE
+       SYNTAX          IpsecFlowEntry
+       MAX-ACCESS      not-accessible
+       STATUS          current
+       DESCRIPTION
+           "An entry containing management information applicable to a
+           particular flow."
+       INDEX { ipsecFlowIndex }
+       ::= { ipsecFlowTable 1 }
+
+IpsecFlowEntry ::=
+       SEQUENCE {
+               ipsecFlowIndex          Integer32,
+               ipsecFlowSAType         DisplayString,
+               ipsecFlowDirection      DisplayString,
+               ipsecFlowFromAddr       IpAddress,
+               ipsecFlowFromMask       IpAddress,
+               ipsecFlowSPort          Integer32,
+               ipsecFlowToAddr         IpAddress,
+               ipsecFlowToMask         IpAddress,
+               ipsecFlowDPort          Integer32,
+               ipsecFlowPeer           IpAddress,
+               ipsecFlowType           DisplayString
+}
+
+ipsecFlowIndex OBJECT-TYPE
+       SYNTAX          Integer32 (0..2147483647)
+       MAX-ACCESS      read-only
+       STATUS          current
+       DESCRIPTION
+           "A unique value for each flow."
+       ::= { ipsecFlowEntry 1 }
+
+ipsecFlowSAType OBJECT-TYPE
+       SYNTAX          DisplayString
+       MAX-ACCESS      read-only
+       STATUS          current
+       DESCRIPTION
+           ""
+       ::= { ipsecFlowEntry 2 }
+
+ipsecFlowDirection OBJECT-TYPE
+       SYNTAX          DisplayString
+       MAX-ACCESS      read-only
+       STATUS          current
+       DESCRIPTION
+           ""
+       ::= { ipsecFlowEntry 3 }
+
+ipsecFlowFromAddr OBJECT-TYPE
+       SYNTAX          IpAddress       
+       MAX-ACCESS      read-only
+       STATUS          current
+       DESCRIPTION
+           ""
+       ::= { ipsecFlowEntry 4 }
+
+ipsecFlowFromMask OBJECT-TYPE
+       SYNTAX          IpAddress       
+       MAX-ACCESS      read-only
+       STATUS          current
+       DESCRIPTION
+           ""
+       ::= { ipsecFlowEntry 5 }
+
+ipsecFlowSPort OBJECT-TYPE
+       SYNTAX          Integer32
+       MAX-ACCESS      read-only
+       STATUS          current
+       DESCRIPTION
+           ""
+       ::= { ipsecFlowEntry 6 }
+
+ipsecFlowToAddr OBJECT-TYPE
+       SYNTAX          IpAddress       
+       MAX-ACCESS      read-only
+       STATUS          current
+       DESCRIPTION
+           ""
+       ::= { ipsecFlowEntry 7 }
+
+ipsecFlowToMask OBJECT-TYPE
+       SYNTAX          IpAddress       
+       MAX-ACCESS      read-only
+       STATUS          current
+       DESCRIPTION
+           ""
+       ::= { ipsecFlowEntry 8 }
+
+ipsecFlowDPort OBJECT-TYPE
+       SYNTAX          Integer32
+       MAX-ACCESS      read-only
+       STATUS          current
+       DESCRIPTION
+           ""
+       ::= { ipsecFlowEntry 9 }
+
+ipsecFlowPeer OBJECT-TYPE
+       SYNTAX          IpAddress       
+       MAX-ACCESS      read-only
+       STATUS          current
+       DESCRIPTION
+           ""
+       ::= { ipsecFlowEntry 11 }
+
+ipsecFlowType OBJECT-TYPE
+       SYNTAX          DisplayString
+       MAX-ACCESS      read-only
+       STATUS          current
+       DESCRIPTION
+           ""
+       ::= { ipsecFlowEntry 15 }
+
+END

Reply via email to