suricata
output-json-dhcp.c
Go to the documentation of this file.
1 /* Copyright (C) 2015 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Jason Ish <jason.ish@oisf.net>
22  */
23 
24 #include "suricata-common.h"
25 #include "debug.h"
26 #include "detect.h"
27 #include "pkt-var.h"
28 #include "conf.h"
29 
30 #include "threads.h"
31 #include "threadvars.h"
32 #include "tm-threads.h"
33 
34 #include "util-unittest.h"
35 #include "util-buffer.h"
36 #include "util-debug.h"
37 #include "util-byte.h"
38 
39 #include "output.h"
40 #include "output-json.h"
41 
42 #include "app-layer.h"
43 #include "app-layer-parser.h"
44 
45 #include "app-layer-dhcp.h"
46 #include "output-json-dhcp.h"
47 
48 #include "rust-dhcp-logger-gen.h"
49 
50 typedef struct LogDHCPFileCtx_ {
52  uint32_t flags;
53  void *rs_logger;
55 
56 typedef struct LogDHCPLogThread_ {
58  uint32_t count;
61 
62 static int JsonDHCPLogger(ThreadVars *tv, void *thread_data,
63  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
64 {
65  LogDHCPLogThread *thread = thread_data;
66  LogDHCPFileCtx *ctx = thread->dhcplog_ctx;
67 
68  json_t *js = CreateJSONHeader((Packet *)p, 0, "dhcp");
69  if (unlikely(js == NULL)) {
70  return TM_ECODE_FAILED;
71  }
72 
73  json_t *dhcp_js = rs_dhcp_logger_log(ctx->rs_logger, tx);
74  if (unlikely(dhcp_js == NULL)) {
75  goto skip;
76  }
77  json_object_set_new(js, "dhcp", dhcp_js);
78 
79  MemBufferReset(thread->buffer);
80  OutputJSONBuffer(js, thread->dhcplog_ctx->file_ctx, &thread->buffer);
81  json_decref(js);
82 
83  return TM_ECODE_OK;
84 
85 skip:
86  json_decref(js);
87  return TM_ECODE_OK;
88 }
89 
90 static void OutputDHCPLogDeInitCtxSub(OutputCtx *output_ctx)
91 {
92  LogDHCPFileCtx *dhcplog_ctx = (LogDHCPFileCtx *)output_ctx->data;
93  rs_dhcp_logger_free(dhcplog_ctx->rs_logger);
94  SCFree(dhcplog_ctx);
95  SCFree(output_ctx);
96 }
97 
98 static OutputInitResult OutputDHCPLogInitSub(ConfNode *conf,
99  OutputCtx *parent_ctx)
100 {
101  OutputInitResult result = { NULL, false };
102  OutputJsonCtx *ajt = parent_ctx->data;
103 
104  LogDHCPFileCtx *dhcplog_ctx = SCCalloc(1, sizeof(*dhcplog_ctx));
105  if (unlikely(dhcplog_ctx == NULL)) {
106  return result;
107  }
108  dhcplog_ctx->file_ctx = ajt->file_ctx;
109 
110  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
111  if (unlikely(output_ctx == NULL)) {
112  SCFree(dhcplog_ctx);
113  return result;
114  }
115  output_ctx->data = dhcplog_ctx;
116  output_ctx->DeInit = OutputDHCPLogDeInitCtxSub;
117 
118  dhcplog_ctx->rs_logger = rs_dhcp_logger_new(conf);
119 
121 
122  result.ctx = output_ctx;
123  result.ok = true;
124  return result;
125 }
126 
127 
128 static TmEcode JsonDHCPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
129 {
130  LogDHCPLogThread *thread = SCCalloc(1, sizeof(*thread));
131  if (unlikely(thread == NULL)) {
132  return TM_ECODE_FAILED;
133  }
134 
135  if (initdata == NULL) {
136  SCLogDebug("Error getting context for EveLogDHCP. \"initdata\" is NULL.");
137  SCFree(thread);
138  return TM_ECODE_FAILED;
139  }
140 
142  if (unlikely(thread->buffer == NULL)) {
143  SCFree(thread);
144  return TM_ECODE_FAILED;
145  }
146 
147  thread->dhcplog_ctx = ((OutputCtx *)initdata)->data;
148  *data = (void *)thread;
149 
150  return TM_ECODE_OK;
151 }
152 
153 static TmEcode JsonDHCPLogThreadDeinit(ThreadVars *t, void *data)
154 {
155  LogDHCPLogThread *thread = (LogDHCPLogThread *)data;
156  if (thread == NULL) {
157  return TM_ECODE_OK;
158  }
159  if (thread->buffer != NULL) {
160  MemBufferFree(thread->buffer);
161  }
162  SCFree(thread);
163  return TM_ECODE_OK;
164 }
165 
167 {
168  /* Register as an eve sub-module. */
169  OutputRegisterTxSubModule(LOGGER_JSON_DHCP, "eve-log", "JsonDHCPLog",
170  "eve-log.dhcp", OutputDHCPLogInitSub, ALPROTO_DHCP,
171  JsonDHCPLogger, JsonDHCPLogThreadInit,
172  JsonDHCPLogThreadDeinit, NULL);
173 }
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:44
json_t * CreateJSONHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type)
Definition: output-json.c:710
#define SCLogDebug(...)
Definition: util-debug.h:335
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:809
#define unlikely(expr)
Definition: util-optimize.h:35
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
#define SCCalloc(nm, a)
Definition: util-mem.h:253
struct LogDHCPFileCtx_ LogDHCPFileCtx
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
LogFileCtx * file_ctx
Definition: output-json.h:79
void JsonDHCPLogRegister(void)
void OutputRegisterTxSubModule(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output.c:402
Definition: conf.h:32
OutputCtx * ctx
Definition: output.h:42
LogFileCtx * file_ctx
#define SCFree(a)
Definition: util-mem.h:322
uint16_t tx_id
void * data
Definition: tm-modules.h:81
LogDHCPFileCtx * dhcplog_ctx
Per thread variable structure.
Definition: threadvars.h:57
struct LogDHCPLogThread_ LogDHCPLogThread
Flow data structure.
Definition: flow.h:325
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82