suricata
output-json-llmnr.c
Go to the documentation of this file.
1 /* Copyright (C) 2026 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 // written by Giuseppe Longo <giuseppe@glongo.it>
19 
20 #include "suricata-common.h"
21 #include "conf.h"
22 
23 #include "threadvars.h"
24 
25 #include "util-byte.h"
26 #include "util-debug.h"
27 #include "app-layer-parser.h"
28 #include "output.h"
29 
30 #include "output-json.h"
31 #include "output-json-llmnr.h"
32 #include "rust.h"
33 
34 typedef struct SCLLMNRLogFileCtx_ {
35  uint64_t flags; /** Store mode */
38 
39 typedef struct LogLLMNRLogThread_ {
43 
44 bool AlertJsonLLMNR(void *txptr, SCJsonBuilder *js)
45 {
46  return SCLLMNRLogJson(txptr, js);
47 }
48 
49 static int JsonLLMNRLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f,
50  void *alstate, void *txptr, uint64_t tx_id)
51 {
52  SCLLMNRLogThread *td = (SCLLMNRLogThread *)thread_data;
53  SCLLMNRLogFileCtx *llmnrlog_ctx = td->llmnrlog_ctx;
54 
55  SCJsonBuilder *jb = CreateEveHeader(p, LOG_DIR_FLOW, "llmnr", NULL, llmnrlog_ctx->eve_ctx);
56  if (unlikely(jb == NULL)) {
57  return TM_ECODE_OK;
58  }
59 
60  if (SCLLMNRLogJson(txptr, jb)) {
61  OutputJsonBuilderBuffer(tv, p, p->flow, jb, td->ctx);
62  }
63  SCJbFree(jb);
64 
65  return TM_ECODE_OK;
66 }
67 
68 static TmEcode SCLLMNRLogThreadInit(ThreadVars *t, const void *initdata, void **data)
69 {
70  SCLLMNRLogThread *aft = SCCalloc(1, sizeof(SCLLMNRLogThread));
71  if (unlikely(aft == NULL))
72  return TM_ECODE_FAILED;
73 
74  if (initdata == NULL) {
75  SCLogDebug("Error getting context for EveLogLLMNR. \"initdata\" argument NULL");
76  goto error_exit;
77  }
78 
79  /* Use the Output Context (file pointer and mutex) */
80  aft->llmnrlog_ctx = ((OutputCtx *)initdata)->data;
81  aft->ctx = CreateEveThreadCtx(t, aft->llmnrlog_ctx->eve_ctx);
82  if (!aft->ctx) {
83  goto error_exit;
84  }
85 
86  *data = (void *)aft;
87  return TM_ECODE_OK;
88 
89 error_exit:
90  SCFree(aft);
91  return TM_ECODE_FAILED;
92 }
93 
94 static TmEcode SCLLMNRLogThreadDeinit(ThreadVars *t, void *data)
95 {
96  SCLLMNRLogThread *aft = (SCLLMNRLogThread *)data;
97  if (aft == NULL) {
98  return TM_ECODE_OK;
99  }
100  FreeEveThreadCtx(aft->ctx);
101 
102  /* clear memory */
103  memset(aft, 0, sizeof(SCLLMNRLogThread));
104 
105  SCFree(aft);
106  return TM_ECODE_OK;
107 }
108 
109 static void SCLLMNRLogDeInitCtxSub(OutputCtx *output_ctx)
110 {
111  SCLogDebug("cleaning up sub output_ctx %p", output_ctx);
112  SCLLMNRLogFileCtx *llmnrlog_ctx = (SCLLMNRLogFileCtx *)output_ctx->data;
113  SCFree(llmnrlog_ctx);
114  SCFree(output_ctx);
115 }
116 
117 static OutputInitResult JsonLLMNRLogInitCtxSub(SCConfNode *conf, OutputCtx *parent_ctx)
118 {
119  OutputInitResult result = { NULL, false };
120  const char *enabled = SCConfNodeLookupChildValue(conf, "enabled");
121  if (enabled != NULL && !SCConfValIsTrue(enabled)) {
122  result.ok = true;
123  return result;
124  }
125 
126  OutputJsonCtx *ojc = parent_ctx->data;
127 
128  SCLLMNRLogFileCtx *llmnrlog_ctx = SCCalloc(1, sizeof(SCLLMNRLogFileCtx));
129  if (unlikely(llmnrlog_ctx == NULL)) {
130  return result;
131  }
132 
133  llmnrlog_ctx->eve_ctx = ojc;
134  llmnrlog_ctx->flags = ~0ULL;
135 
136  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
137  if (unlikely(output_ctx == NULL)) {
138  SCFree(llmnrlog_ctx);
139  return result;
140  }
141 
142  output_ctx->data = llmnrlog_ctx;
143  output_ctx->DeInit = SCLLMNRLogDeInitCtxSub;
144 
145  SCLogDebug("LLMNR log sub-module initialized");
146 
149 
150  result.ctx = output_ctx;
151  result.ok = true;
152  return result;
153 }
154 
156 {
157  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonLLMNRLog", "eve-log.llmnr",
158  JsonLLMNRLogInitCtxSub, ALPROTO_LLMNR, JsonLLMNRLogger, SCLLMNRLogThreadInit,
159  SCLLMNRLogThreadDeinit);
160 }
SCJsonBuilder
struct SCJsonBuilder SCJsonBuilder
Definition: detect-engine-helper.h:83
util-byte.h
SCConfValIsTrue
int SCConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:577
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
JsonLLMNRLogRegister
void JsonLLMNRLogRegister(void)
Definition: output-json-llmnr.c:155
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:58
CreateEveHeader
SCJsonBuilder * CreateEveHeader(const Packet *p, enum SCOutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:816
OutputJsonCtx_
Definition: output-json.h:77
Flow_
Flow data structure.
Definition: flow.h:359
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:29
OutputRegisterTxSubModule
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)
Definition: output.c:411
rust.h
p
Packet * p
Definition: fuzz_dataset.c:30
OutputJsonBuilderBuffer
void OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:1018
SCConfNodeLookupChildValue
const char * SCConfNodeLookupChildValue(const SCConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:877
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
OutputCtx_::data
void * data
Definition: tm-modules.h:91
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
SCLLMNRLogFileCtx_
Definition: output-json-llmnr.c:34
OutputCtx_
Definition: tm-modules.h:88
OutputJsonThreadCtx_
Definition: output-json.h:85
LogLLMNRLogThread_::llmnrlog_ctx
SCLLMNRLogFileCtx * llmnrlog_ctx
Definition: output-json-llmnr.c:40
LogLLMNRLogThread_
Definition: output-json-llmnr.c:39
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
SCAppLayerParserRegisterLogger
void SCAppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:535
output-json.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
output-json-llmnr.h
OutputInitResult_::ok
bool ok
Definition: output.h:48
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-eve-bindgen.h:35
app-layer-parser.h
LogLLMNRLogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-llmnr.c:41
SCLLMNRLogFileCtx
struct SCLLMNRLogFileCtx_ SCLLMNRLogFileCtx
Packet_
Definition: decode.h:516
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:80
AlertJsonLLMNR
bool AlertJsonLLMNR(void *txptr, SCJsonBuilder *js)
Definition: output-json-llmnr.c:44
OutputInitResult_
Definition: output.h:46
Packet_::flow
struct Flow_ * flow
Definition: decode.h:564
ALPROTO_LLMNR
@ ALPROTO_LLMNR
Definition: app-layer-protos.h:73
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:94
SCLLMNRLogFileCtx_::flags
uint64_t flags
Definition: output-json-llmnr.c:35
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
SCLLMNRLogThread
struct LogLLMNRLogThread_ SCLLMNRLogThread
threadvars.h
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:489
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SCLLMNRLogFileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-llmnr.c:36
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCConfNode_
Definition: conf.h:37
output.h
f
Flow f
Definition: fuzz_dataset.c:32