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