suricata
output-json-nfs.c
Go to the documentation of this file.
1 /* Copyright (C) 2015-2021 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 "detect.h"
28 #include "pkt-var.h"
29 #include "conf.h"
30 
31 #include "threads.h"
32 #include "threadvars.h"
33 #include "tm-threads.h"
34 
35 #include "util-unittest.h"
36 #include "util-buffer.h"
37 #include "util-debug.h"
38 #include "util-byte.h"
39 
40 #include "output.h"
41 #include "output-json.h"
42 
43 #include "app-layer.h"
44 #include "app-layer-parser.h"
45 
46 #include "output-json-nfs.h"
47 
48 #include "rust.h"
49 
50 bool EveNFSAddMetadataRPC(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
51 {
52  NFSState *state = FlowGetAppState(f);
53  if (state) {
54  NFSTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_NFS, state, tx_id);
55  if (tx) {
56  return rs_rpc_log_json_response(tx, jb);
57  }
58  }
59  return false;
60 }
61 
62 bool EveNFSAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
63 {
64  NFSState *state = FlowGetAppState(f);
65  if (state) {
66  NFSTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_NFS, state, tx_id);
67  if (tx) {
68  return rs_nfs_log_json_response(state, tx, jb);
69  }
70  }
71  return false;
72 }
73 
74 static int JsonNFSLogger(ThreadVars *tv, void *thread_data,
75  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
76 {
77  NFSTransaction *nfstx = tx;
78  OutputJsonThreadCtx *thread = thread_data;
79 
80  if (rs_nfs_tx_logging_is_filtered(state, nfstx))
81  return TM_ECODE_OK;
82 
83  JsonBuilder *jb = CreateEveHeader(p, LOG_DIR_PACKET, "nfs", NULL, thread->ctx);
84  if (unlikely(jb == NULL)) {
85  return TM_ECODE_OK;
86  }
87 
88  jb_open_object(jb, "rpc");
89  rs_rpc_log_json_response(tx, jb);
90  jb_close(jb);
91 
92  jb_open_object(jb, "nfs");
93  rs_nfs_log_json_response(state, tx, jb);
94  jb_close(jb);
95 
96  MemBufferReset(thread->buffer);
97  OutputJsonBuilderBuffer(tv, p, p->flow, jb, thread);
98  jb_free(jb);
99  return TM_ECODE_OK;
100 }
101 
102 static OutputInitResult NFSLogInitSub(ConfNode *conf,
103  OutputCtx *parent_ctx)
104 {
107  return OutputJsonLogInitSub(conf, parent_ctx);
108 }
109 
111 {
112  /* Register as an eve sub-module. */
113  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonNFSLog", "eve-log.nfs", NFSLogInitSub,
115 
116  SCLogDebug("NFS JSON logger registered.");
117 }
util-byte.h
tm-threads.h
OutputJsonLogInitSub
OutputInitResult OutputJsonLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
Definition: output-json-common.c:73
OutputJsonThreadCtx_::ctx
OutputJsonCtx * ctx
Definition: output-json.h:90
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
Flow_::proto
uint8_t proto
Definition: flow.h:378
JsonLogThreadInit
TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
Definition: output-json-common.c:90
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
EveNFSAddMetadata
bool EveNFSAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
Definition: output-json-nfs.c:62
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:377
rust.h
util-unittest.h
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
OutputCtx_
Definition: tm-modules.h:84
OutputJsonThreadCtx_
Definition: output-json.h:89
util-debug.h
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:457
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:806
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
app-layer-parser.h
Packet_
Definition: decode.h:473
conf.h
EveNFSAddMetadataRPC
bool EveNFSAddMetadataRPC(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
Definition: output-json-nfs.c:50
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1087
OutputInitResult_
Definition: output.h:46
Packet_::flow
struct Flow_ * flow
Definition: decode.h:512
OutputJsonThreadCtx_::buffer
MemBuffer * buffer
Definition: output-json.h:92
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:37
suricata-common.h
output-json-nfs.h
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:958
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:468
ConfNode_
Definition: conf.h:32
util-buffer.h
JsonLogThreadDeinit
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
Definition: output-json-common.c:123
ALPROTO_NFS
@ ALPROTO_NFS
Definition: app-layer-protos.h:45
output.h
JsonNFSLogRegister
void JsonNFSLogRegister(void)
Definition: output-json-nfs.c:110
app-layer.h