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  * TODO: Update \author in this file and in output-json-dhcp.h.
20  * TODO: Remove SCLogNotice statements, or convert to debug.
21  * TODO: Implement your app-layers logging.
22  */
23 
24 /**
25  * \file
26  *
27  * \author Jason Ish <jason.ish@oisf.net>
28  */
29 
30 #include "suricata-common.h"
31 #include "debug.h"
32 #include "detect.h"
33 #include "pkt-var.h"
34 #include "conf.h"
35 
36 #include "threads.h"
37 #include "threadvars.h"
38 #include "tm-threads.h"
39 
40 #include "util-unittest.h"
41 #include "util-buffer.h"
42 #include "util-debug.h"
43 #include "util-byte.h"
44 
45 #include "output.h"
46 #include "output-json.h"
47 
48 #include "app-layer.h"
49 #include "app-layer-parser.h"
50 
51 #include "app-layer-dhcp.h"
52 #include "output-json-dhcp.h"
53 
54 #if defined(HAVE_LIBJANSSON) && defined(HAVE_RUST)
55 
56 #include "rust-dhcp-logger-gen.h"
57 
58 typedef struct LogDHCPFileCtx_ {
59  LogFileCtx *file_ctx;
60  uint32_t flags;
61  void *rs_logger;
62 } LogDHCPFileCtx;
63 
64 typedef struct LogDHCPLogThread_ {
65  LogDHCPFileCtx *dhcplog_ctx;
66  uint32_t count;
67  MemBuffer *buffer;
68 } LogDHCPLogThread;
69 
70 static int JsonDHCPLogger(ThreadVars *tv, void *thread_data,
71  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
72 {
73  LogDHCPLogThread *thread = thread_data;
74  LogDHCPFileCtx *ctx = thread->dhcplog_ctx;
75 
76  json_t *js = CreateJSONHeader((Packet *)p, 0, "dhcp");
77  if (unlikely(js == NULL)) {
78  return TM_ECODE_FAILED;
79  }
80 
81  json_t *dhcp_js = rs_dhcp_logger_log(ctx->rs_logger, tx);
82  if (unlikely(dhcp_js == NULL)) {
83  goto skip;
84  }
85  json_object_set_new(js, "dhcp", dhcp_js);
86 
87  MemBufferReset(thread->buffer);
88  OutputJSONBuffer(js, thread->dhcplog_ctx->file_ctx, &thread->buffer);
89  json_decref(js);
90 
91  return TM_ECODE_OK;
92 
93 skip:
94  json_decref(js);
95  return TM_ECODE_OK;
96 }
97 
98 static void OutputDHCPLogDeInitCtxSub(OutputCtx *output_ctx)
99 {
100  LogDHCPFileCtx *dhcplog_ctx = (LogDHCPFileCtx *)output_ctx->data;
101  rs_dhcp_logger_free(dhcplog_ctx->rs_logger);
102  SCFree(dhcplog_ctx);
103  SCFree(output_ctx);
104 }
105 
106 static OutputInitResult OutputDHCPLogInitSub(ConfNode *conf,
107  OutputCtx *parent_ctx)
108 {
109  OutputInitResult result = { NULL, false };
110  OutputJsonCtx *ajt = parent_ctx->data;
111 
112  LogDHCPFileCtx *dhcplog_ctx = SCCalloc(1, sizeof(*dhcplog_ctx));
113  if (unlikely(dhcplog_ctx == NULL)) {
114  return result;
115  }
116  dhcplog_ctx->file_ctx = ajt->file_ctx;
117 
118  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
119  if (unlikely(output_ctx == NULL)) {
120  SCFree(dhcplog_ctx);
121  return result;
122  }
123  output_ctx->data = dhcplog_ctx;
124  output_ctx->DeInit = OutputDHCPLogDeInitCtxSub;
125 
126  dhcplog_ctx->rs_logger = rs_dhcp_logger_new(conf);
127 
129 
130  result.ctx = output_ctx;
131  result.ok = true;
132  return result;
133 }
134 
135 #define OUTPUT_BUFFER_SIZE 65535
136 
137 static TmEcode JsonDHCPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
138 {
139  LogDHCPLogThread *thread = SCCalloc(1, sizeof(*thread));
140  if (unlikely(thread == NULL)) {
141  return TM_ECODE_FAILED;
142  }
143 
144  if (initdata == NULL) {
145  SCLogDebug("Error getting context for EveLogDHCP. \"initdata\" is NULL.");
146  SCFree(thread);
147  return TM_ECODE_FAILED;
148  }
149 
150  thread->buffer = MemBufferCreateNew(OUTPUT_BUFFER_SIZE);
151  if (unlikely(thread->buffer == NULL)) {
152  SCFree(thread);
153  return TM_ECODE_FAILED;
154  }
155 
156  thread->dhcplog_ctx = ((OutputCtx *)initdata)->data;
157  *data = (void *)thread;
158 
159  return TM_ECODE_OK;
160 }
161 
162 static TmEcode JsonDHCPLogThreadDeinit(ThreadVars *t, void *data)
163 {
164  LogDHCPLogThread *thread = (LogDHCPLogThread *)data;
165  if (thread == NULL) {
166  return TM_ECODE_OK;
167  }
168  if (thread->buffer != NULL) {
169  MemBufferFree(thread->buffer);
170  }
171  SCFree(thread);
172  return TM_ECODE_OK;
173 }
174 
175 void JsonDHCPLogRegister(void)
176 {
177  /* Register as an eve sub-module. */
178  OutputRegisterTxSubModule(LOGGER_JSON_DHCP, "eve-log", "JsonDHCPLog",
179  "eve-log.dhcp", OutputDHCPLogInitSub, ALPROTO_DHCP,
180  JsonDHCPLogger, JsonDHCPLogThreadInit,
181  JsonDHCPLogThreadDeinit, NULL);
182 }
183 
184 #else /* No JSON support. */
185 
187 {
188 }
189 
190 #endif /* HAVE_LIBJANSSON */
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
uint16_t flags
#define SCLogDebug(...)
Definition: util-debug.h:335
#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:205
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
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:399
Definition: conf.h:32
OutputCtx * ctx
Definition: output.h:42
#define SCFree(a)
Definition: util-mem.h:236
#define OUTPUT_BUFFER_SIZE
Definition: log-dnslog.c:58
uint16_t tx_id
void * data
Definition: tm-modules.h:81
Per thread variable structure.
Definition: threadvars.h:57
Flow data structure.
Definition: flow.h:327
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82