suricata
output-json-nfs.c
Go to the documentation of this file.
1 /* Copyright (C) 2015-2020 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 Victor Julien <victor@inliniac.net>
22  *
23  * Implement JSON/eve logging app-layer NFS.
24  */
25 
26 #include "suricata-common.h"
27 #include "debug.h"
28 #include "detect.h"
29 #include "pkt-var.h"
30 #include "conf.h"
31 
32 #include "threads.h"
33 #include "threadvars.h"
34 #include "tm-threads.h"
35 
36 #include "util-unittest.h"
37 #include "util-buffer.h"
38 #include "util-debug.h"
39 #include "util-byte.h"
40 
41 #include "output.h"
42 #include "output-json.h"
43 
44 #include "app-layer.h"
45 #include "app-layer-parser.h"
46 
47 #include "output-json-nfs.h"
48 
49 #include "rust.h"
50 
51 bool EveNFSAddMetadataRPC(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
52 {
53  NFSState *state = FlowGetAppState(f);
54  if (state) {
55  NFSTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_NFS, state, tx_id);
56  if (tx) {
57  return rs_rpc_log_json_response(tx, jb);
58  }
59  }
60  return false;
61 }
62 
63 bool EveNFSAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
64 {
65  NFSState *state = FlowGetAppState(f);
66  if (state) {
67  NFSTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_NFS, state, tx_id);
68  if (tx) {
69  return rs_nfs_log_json_response(state, tx, jb);
70  }
71  }
72  return false;
73 }
74 
75 static int JsonNFSLogger(ThreadVars *tv, void *thread_data,
76  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
77 {
78  NFSTransaction *nfstx = tx;
79  OutputJsonThreadCtx *thread = thread_data;
80 
81  if (rs_nfs_tx_logging_is_filtered(state, nfstx))
82  return TM_ECODE_OK;
83 
84  JsonBuilder *jb = CreateEveHeader(p, LOG_DIR_PACKET, "nfs", NULL);
85  if (unlikely(jb == NULL)) {
86  return TM_ECODE_OK;
87  }
88  EveAddCommonOptions(&thread->ctx->cfg, p, f, jb);
89 
90  jb_open_object(jb, "rpc");
91  rs_rpc_log_json_response(tx, jb);
92  jb_close(jb);
93 
94  jb_open_object(jb, "nfs");
95  rs_nfs_log_json_response(state, tx, jb);
96  jb_close(jb);
97 
98  MemBufferReset(thread->buffer);
99  OutputJsonBuilderBuffer(jb, thread->ctx->file_ctx, &thread->buffer);
100  jb_free(jb);
101  return TM_ECODE_OK;
102 }
103 
104 static OutputInitResult NFSLogInitSub(ConfNode *conf,
105  OutputCtx *parent_ctx)
106 {
109  return OutputJsonLogInitSub(conf, parent_ctx);
110 }
111 
113 {
114  /* Register as an eve sub-module. */
115  OutputRegisterTxSubModule(LOGGER_JSON_NFS, "eve-log", "JsonNFSLog",
116  "eve-log.nfs", NFSLogInitSub, ALPROTO_NFS,
117  JsonNFSLogger, JsonLogThreadInit,
118  JsonLogThreadDeinit, NULL);
119 
120  SCLogDebug("NFS JSON logger registered.");
121 }
util-byte.h
tm-threads.h
OutputJsonLogInitSub
OutputInitResult OutputJsonLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
Definition: output-json-common.c:50
OutputJsonThreadCtx_::ctx
OutputJsonCtx * ctx
Definition: output-json.h:116
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:111
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
Flow_::proto
uint8_t proto
Definition: flow.h:361
JsonLogThreadInit
TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
Definition: output-json-common.c:75
threads.h
Flow_
Flow data structure.
Definition: flow.h:343
EveNFSAddMetadata
bool EveNFSAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
Definition: output-json-nfs.c:63
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:1431
rust.h
EveAddCommonOptions
void EveAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, JsonBuilder *js)
Definition: output-json.c:673
util-unittest.h
FlowGetAppState
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1081
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:78
OutputCtx_
Definition: tm-modules.h:78
OutputJsonThreadCtx_
Definition: output-json.h:115
util-debug.h
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr)
Definition: output-json.c:1286
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:471
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
app-layer-parser.h
Packet_
Definition: decode.h:411
conf.h
EveNFSAddMetadataRPC
bool EveNFSAddMetadataRPC(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
Definition: output-json-nfs.c:51
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1037
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:41
LOGGER_JSON_NFS
@ LOGGER_JSON_NFS
Definition: suricata-common.h:453
OutputJsonThreadCtx_::buffer
MemBuffer * buffer
Definition: output-json.h:117
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:38
suricata-common.h
output-json-nfs.h
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, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output.c:410
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
threadvars.h
ConfNode_
Definition: conf.h:32
util-buffer.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:109
JsonLogThreadDeinit
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
Definition: output-json-common.c:97
ALPROTO_NFS
@ ALPROTO_NFS
Definition: app-layer-protos.h:45
debug.h
output.h
JsonNFSLogRegister
void JsonNFSLogRegister(void)
Definition: output-json-nfs.c:112
app-layer.h