]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
device-storage: introduce feature
authorEric Leblond <eric@regit.org>
Mon, 1 Jan 2018 23:33:23 +0000 (00:33 +0100)
committerEric Leblond <eric@regit.org>
Tue, 6 Feb 2018 15:58:19 +0000 (16:58 +0100)
The capture method may have to store data depending related to the
offloading so having a per interface storage via LiveDevice seems
interesting.

src/Makefile.am
src/device-storage.c [new file with mode: 0644]
src/device-storage.h [new file with mode: 0644]
src/util-device.c
src/util-storage.c
src/util-storage.h

index 518153fc38cd7b1ebf2fee1c7bbca835a5de7776..dfd69ba87991bae012667739fab551cbd0e93569 100644 (file)
@@ -258,6 +258,7 @@ detect-within.c detect-within.h \
 detect-modbus.c detect-modbus.h \
 detect-xbits.c detect-xbits.h \
 detect-cipservice.c detect-cipservice.h \
+device-storage.c device-storage.h \
 flow-bit.c flow-bit.h \
 flow.c flow.h \
 flow-bypass.c flow-bypass.h \
diff --git a/src/device-storage.c b/src/device-storage.c
new file mode 100644 (file)
index 0000000..2035eb8
--- /dev/null
@@ -0,0 +1,111 @@
+/* Copyright (C) 2018 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+/**
+ * \file
+ *
+ * \author Eric Leblond <eric@regit.org>
+ *
+ * Device wrapper around storage api
+ */
+
+#include "suricata-common.h"
+#include "device-storage.h"
+#include "util-unittest.h"
+
+unsigned int LiveDevStorageSize(void)
+{
+    return StorageGetSize(STORAGE_DEVICE);
+}
+
+/** \defgroup devicestorage Device storage API
+ *
+ * The device storage API is a per-device storage. It is a mean to extend
+ * the LiveDevice structure with arbitrary data.
+ *
+ * You have first to register the storage via LiveDevStorageRegister() during
+ * the init of your module. Then you can attach data via LiveDevSetStorageById()
+ * and access them via LiveDevGetStorageById().
+ * @{
+ */
+
+/**
+ * \brief Register a LiveDevice storage
+ *
+ * \param name the name of the storage
+ * \param size integer coding the size of the stored value (sizeof(void *) is best choice here)
+ * \param Alloc allocation function for the storage (can be null)
+ * \param Free free function for the new storage
+ *
+ * \retval The ID of the newly register storage that will be used to access data
+ *
+ * It has to be called once during the init of the sub system
+ */
+
+int LiveDevStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)) {
+    return StorageRegister(STORAGE_DEVICE, name, size, Alloc, Free);
+}
+
+/**
+ * \brief Store a pointer in a given LiveDevice storage
+ *
+ * \param d a pointer to the LiveDevice
+ * \param id the id of the storage (return of HostStorageRegister() call)
+ * \param ptr pointer to the data to store
+ */
+
+int LiveDevSetStorageById(LiveDevice *d, int id, void *ptr)
+{
+    return StorageSetById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id, ptr);
+}
+
+/**
+ * \brief Get a value from a given LiveDevice storage
+ *
+ * \param d a pointer to the LiveDevice
+ * \param id the id of the storage (return of LiveDevStorageRegister() call)
+ *
+ */
+
+void *LiveDevGetStorageById(LiveDevice *d, int id)
+{
+    return StorageGetById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id);
+}
+
+/**
+ * @}
+ */
+
+/* Start of "private" function */
+
+void *LiveDevAllocStorageById(LiveDevice *d, int id)
+{
+    return StorageAllocByIdPrealloc((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id);
+}
+
+void LiveDevFreeStorageById(LiveDevice *d, int id)
+{
+    StorageFreeById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id);
+}
+
+void LiveDevFreeStorage(LiveDevice *d)
+{
+    if (LiveDevStorageSize() > 0)
+        StorageFreeAll((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE);
+}
+
+
diff --git a/src/device-storage.h b/src/device-storage.h
new file mode 100644 (file)
index 0000000..0c671ea
--- /dev/null
@@ -0,0 +1,45 @@
+/* Copyright (C) 2018 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+/**
+ * \file
+ *
+ * \author Eric Leblond <eric@regit.org>
+ *
+ * LiveDevice wrapper around storage api
+ */
+
+#ifndef __DEVICE_STORAGE_H__
+#define __DEVICE_STORAGE_H__
+
+#include "util-storage.h"
+#include "util-device.h"
+
+unsigned int LiveDevStorageSize(void);
+
+void *LiveDevGetStorageById(LiveDevice *d, int id);
+int LiveDevSetStorageById(LiveDevice *d, int id, void *ptr);
+void *LiveDevAllocStorageById(LiveDevice *d, int id);
+
+void LiveDevFreeStorageById(LiveDevice *d, int id);
+void LiveDevFreeStorage(LiveDevice *d);
+
+void RegisterLiveDevStorageTests(void);
+
+int LiveDevStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *));
+
+#endif /* __DEVICE_STORAGE_H__ */
index 9848f2d6356a00037728ae6820aed2921586bceb..d21a14b27b1b8977285b35c76aee23b30d680139 100644 (file)
@@ -20,6 +20,8 @@
 #include "util-device.h"
 #include "util-ioctl.h"
 
+#include "device-storage.h"
+
 #define MAX_DEVNAME 10
 
 /**
@@ -67,7 +69,9 @@ int LiveGetOffload(void)
  */
 int LiveRegisterDevice(const char *dev)
 {
-    LiveDevice *pd = SCCalloc(1, sizeof(LiveDevice));
+    LiveDevice *pd = NULL;
+
+    pd = SCCalloc(1, sizeof(LiveDevice) + LiveDevStorageSize());
     if (unlikely(pd == NULL)) {
         return -1;
     }
@@ -290,6 +294,7 @@ int LiveDeviceListClean()
         SC_ATOMIC_DESTROY(pd->pkts);
         SC_ATOMIC_DESTROY(pd->drop);
         SC_ATOMIC_DESTROY(pd->invalid_checksums);
+        LiveDevFreeStorage(pd);
         SCFree(pd);
     }
 
index 21bbf4e0612b00d23e61d6aea7844fead86de7fe..756ef240d602b071e1513111ad23f4ffdfe9840f 100644 (file)
@@ -56,6 +56,8 @@ static const char *StoragePrintType(StorageEnum type)
             return "flow";
         case STORAGE_IPPAIR:
             return "ippair";
+        case STORAGE_DEVICE:
+            return "livedevice";
         case STORAGE_MAX:
             return "max";
     }
index d88b030fbd96ae8dd0a86d292af4e2e783367584..984bcf8c20bc4c60b6fcfeedfbe0899b360eae14 100644 (file)
@@ -30,6 +30,7 @@ typedef enum StorageEnum_ {
     STORAGE_HOST,
     STORAGE_FLOW,
     STORAGE_IPPAIR,
+    STORAGE_DEVICE,
 
     STORAGE_MAX,
 } StorageEnum;