suricata
app-layer-nfs-tcp.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  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  * NFS application layer detector and parser.
24  *
25  * This implements a application layer for the NFS protocol
26  * running on port 2049.
27  */
28 
29 #include "suricata-common.h"
30 #include "stream.h"
31 #include "conf.h"
32 
33 #include "util-unittest.h"
34 
35 #include "app-layer-detect-proto.h"
36 #include "app-layer-parser.h"
37 
38 #include "app-layer-nfs-tcp.h"
39 
40 #ifndef HAVE_RUST
42 {
43 }
44 
45 #else
46 
47 #include "rust.h"
48 #include "rust-nfs-nfs-gen.h"
49 
50 /* The default port to probe for echo traffic if not provided in the
51  * configuration file. */
52 #define NFSTCP_DEFAULT_PORT "2049"
53 
54 /* The minimum size for a RFC message. For some protocols this might
55  * be the size of a header. TODO actual min size is likely larger */
56 #define NFSTCP_MIN_FRAME_LEN 32
57 
58 /* Enum of app-layer events for an echo protocol. Normally you might
59  * have events for errors in parsing data, like unexpected data being
60  * received. For echo we'll make something up, and log an app-layer
61  * level alert if an empty message is received.
62  *
63  * Example rule:
64  *
65  * alert nfs any any -> any any (msg:"SURICATA NFS empty message"; \
66  * app-layer-event:nfs.empty_message; sid:X; rev:Y;)
67  */
68 enum {
69  NFSTCP_DECODER_EVENT_EMPTY_MESSAGE,
70 };
71 
72 SCEnumCharMap nfs_decoder_event_table[] = {
73  {"EMPTY_MESSAGE", NFSTCP_DECODER_EVENT_EMPTY_MESSAGE},
74  { NULL, 0 }
75 };
76 
77 static void *NFSTCPStateAlloc(void)
78 {
79  return rs_nfs_state_new();
80 }
81 
82 static void NFSTCPStateFree(void *state)
83 {
84  rs_nfs_state_free(state);
85 }
86 
87 /**
88  * \brief Callback from the application layer to have a transaction freed.
89  *
90  * \param state a void pointer to the NFSTCPState object.
91  * \param tx_id the transaction ID to free.
92  */
93 static void NFSTCPStateTxFree(void *state, uint64_t tx_id)
94 {
95  rs_nfs_state_tx_free(state, tx_id);
96 }
97 
98 static int NFSTCPStateGetEventInfo(const char *event_name, int *event_id,
100 {
101  return rs_nfs_state_get_event_info(event_name, event_id, event_type);
102 }
103 
104 static int NFSTCPStateGetEventInfoById(int event_id, const char **event_name,
106 {
107  return rs_nfs_state_get_event_info_by_id(event_id, event_name, event_type);
108 }
109 
110 static AppLayerDecoderEvents *NFSTCPGetEvents(void *tx)
111 {
112  return rs_nfs_state_get_events(tx);
113 }
114 
115 /**
116  * \brief Probe the input to see if it looks like echo.
117  *
118  * \retval ALPROTO_NFS if it looks like echo, otherwise
119  * ALPROTO_UNKNOWN.
120  */
121 static AppProto NFSTCPProbingParserMidstream(Flow *f,
122  uint8_t direction,
123  uint8_t *input, uint32_t input_len,
124  uint8_t *rdir)
125 {
126  if (input_len < NFSTCP_MIN_FRAME_LEN) {
127  return ALPROTO_UNKNOWN;
128  }
129 
130  int8_t r = rs_nfs_probe_ms(direction, input, input_len, rdir);
131  if (r == 1) {
132  return ALPROTO_NFS;
133  } else if (r == -1) {
134  return ALPROTO_FAILED;
135  }
136 
137  SCLogDebug("Protocol not detected as ALPROTO_NFS.");
138  return ALPROTO_UNKNOWN;
139 }
140 
141 /**
142  * \brief Probe the input to see if it looks like echo.
143  *
144  * \retval ALPROTO_NFS if it looks like echo, otherwise
145  * ALPROTO_UNKNOWN.
146  */
147 static AppProto NFSTCPProbingParser(Flow *f,
148  uint8_t direction,
149  uint8_t *input, uint32_t input_len,
150  uint8_t *rdir)
151 {
152  if (input_len < NFSTCP_MIN_FRAME_LEN) {
153  return ALPROTO_UNKNOWN;
154  }
155 
156  int8_t r = rs_nfs_probe(direction, input, input_len);
157  if (r == 1) {
158  return ALPROTO_NFS;
159  } else if (r == -1) {
160  return ALPROTO_FAILED;
161  }
162 
163  SCLogDebug("Protocol not detected as ALPROTO_NFS.");
164  return ALPROTO_UNKNOWN;
165 }
166 
167 static int NFSTCPParseRequest(Flow *f, void *state,
168  AppLayerParserState *pstate, uint8_t *input, uint32_t input_len,
169  void *local_data, const uint8_t flags)
170 {
171  uint16_t file_flags = FileFlowToFlags(f, STREAM_TOSERVER);
172  rs_nfs_setfileflags(0, state, file_flags);
173 
174  int res;
175  if (input == NULL && input_len > 0) {
176  res = rs_nfs_parse_request_tcp_gap(state, input_len);
177  } else {
178  res = rs_nfs_parse_request(f, state, pstate, input, input_len, local_data);
179  }
180  return res;
181 }
182 
183 static int NFSTCPParseResponse(Flow *f, void *state, AppLayerParserState *pstate,
184  uint8_t *input, uint32_t input_len, void *local_data,
185  const uint8_t flags)
186 {
187  uint16_t file_flags = FileFlowToFlags(f, STREAM_TOCLIENT);
188  rs_nfs_setfileflags(1, state, file_flags);
189 
190  int res;
191  if (input == NULL && input_len > 0) {
192  res = rs_nfs_parse_response_tcp_gap(state, input_len);
193  } else {
194  res = rs_nfs_parse_response(f, state, pstate, input, input_len, local_data);
195  }
196  return res;
197 }
198 
199 static uint64_t NFSTCPGetTxCnt(void *state)
200 {
201  return rs_nfs_state_get_tx_count(state);
202 }
203 
204 static void *NFSTCPGetTx(void *state, uint64_t tx_id)
205 {
206  return rs_nfs_state_get_tx(state, tx_id);
207 }
208 
209 static AppLayerGetTxIterTuple RustNFSTCPGetTxIterator(
210  const uint8_t ipproto, const AppProto alproto,
211  void *alstate, uint64_t min_tx_id, uint64_t max_tx_id,
212  AppLayerGetTxIterState *istate)
213 {
214  return rs_nfs_state_get_tx_iterator(alstate, min_tx_id, (uint64_t *)istate);
215 }
216 
217 static void NFSTCPSetTxLogged(void *state, void *vtx, LoggerId logged)
218 {
219  rs_nfs_tx_set_logged(state, vtx, logged);
220 }
221 
222 static LoggerId NFSTCPGetTxLogged(void *state, void *vtx)
223 {
224  return rs_nfs_tx_get_logged(state, vtx);
225 }
226 
227 /**
228  * \brief Called by the application layer.
229  *
230  * In most cases 1 can be returned here.
231  */
232 static int NFSTCPGetAlstateProgressCompletionStatus(uint8_t direction) {
233  return rs_nfs_state_progress_completion_status(direction);
234 }
235 
236 /**
237  * \brief Return the state of a transaction in a given direction.
238  *
239  * In the case of the echo protocol, the existence of a transaction
240  * means that the request is done. However, some protocols that may
241  * need multiple chunks of data to complete the request may need more
242  * than just the existence of a transaction for the request to be
243  * considered complete.
244  *
245  * For the response to be considered done, the response for a request
246  * needs to be seen. The response_done flag is set on response for
247  * checking here.
248  */
249 static int NFSTCPGetStateProgress(void *tx, uint8_t direction)
250 {
251  return rs_nfs_tx_get_alstate_progress(tx, direction);
252 }
253 
254 /**
255  * \brief get stored tx detect state
256  */
257 static DetectEngineState *NFSTCPGetTxDetectState(void *vtx)
258 {
259  return rs_nfs_state_get_tx_detect_state(vtx);
260 }
261 
262 /**
263  * \brief set store tx detect state
264  */
265 static int NFSTCPSetTxDetectState(void *vtx, DetectEngineState *s)
266 {
267  rs_nfs_state_set_tx_detect_state(vtx, s);
268  return 0;
269 }
270 
271 static FileContainer *NFSTCPGetFiles(void *state, uint8_t direction)
272 {
273  return rs_nfs_getfiles(direction, state);
274 }
275 
276 static void NFSTCPSetDetectFlags(void *tx, uint8_t dir, uint64_t flags)
277 {
278  rs_nfs_tx_set_detect_flags(tx, dir, flags);
279 }
280 
281 static uint64_t NFSTCPGetDetectFlags(void *tx, uint8_t dir)
282 {
283  return rs_nfs_tx_get_detect_flags(tx, dir);
284 }
285 
287 static SuricataFileContext sfc = { &sbcfg };
288 
289 void RegisterNFSTCPParsers(void)
290 {
291  const char *proto_name = "nfs";
292 
293  /* Check if NFSTCP TCP detection is enabled. If it does not exist in
294  * the configuration file then it will be enabled by default. */
295  if (AppLayerProtoDetectConfProtoDetectionEnabled("tcp", proto_name)) {
296 
297  rs_nfs_init(&sfc);
298 
299  SCLogDebug("NFSTCP TCP protocol detection enabled.");
300 
302 
303  if (RunmodeIsUnittests()) {
304 
305  SCLogDebug("Unittest mode, registering default configuration.");
306  AppLayerProtoDetectPPRegister(IPPROTO_TCP, NFSTCP_DEFAULT_PORT,
307  ALPROTO_NFS, 0, NFSTCP_MIN_FRAME_LEN, STREAM_TOSERVER,
308  NFSTCPProbingParser, NFSTCPProbingParser);
309 
310  }
311  else {
312  int midstream = 0;
313  ConfGetBool("stream.midstream", &midstream);
314  ProbingParserFPtr FuncPtr = NFSTCPProbingParser;
315  if (midstream)
316  FuncPtr = NFSTCPProbingParserMidstream;
317 
318  if (!AppLayerProtoDetectPPParseConfPorts("tcp", IPPROTO_TCP,
319  proto_name, ALPROTO_NFS, 0, NFSTCP_MIN_FRAME_LEN,
320  FuncPtr, FuncPtr)) {
321  SCLogDebug("No NFSTCP app-layer configuration, enabling NFSTCP"
322  " detection TCP detection on port %s.",
323  NFSTCP_DEFAULT_PORT);
324  /* register 'midstream' probing parsers if midstream is enabled. */
325  AppLayerProtoDetectPPRegister(IPPROTO_TCP,
326  NFSTCP_DEFAULT_PORT, ALPROTO_NFS, 0,
327  NFSTCP_MIN_FRAME_LEN, STREAM_TOSERVER,
328  FuncPtr, FuncPtr);
329  }
330 
331  }
332 
333  }
334 
335  else {
336  SCLogDebug("Protocol detecter and parser disabled for NFSTCP.");
337  return;
338  }
339 
340  if (AppLayerParserConfParserEnabled("tcp", proto_name))
341  {
342  SCLogDebug("Registering NFSTCP protocol parser.");
343 
344  /* Register functions for state allocation and freeing. A
345  * state is allocated for every new NFSTCP flow. */
347  NFSTCPStateAlloc, NFSTCPStateFree);
348 
349  /* Register request parser for parsing frame from server to client. */
351  STREAM_TOSERVER, NFSTCPParseRequest);
352 
353  /* Register response parser for parsing frames from server to client. */
355  STREAM_TOCLIENT, NFSTCPParseResponse);
356 
357  /* Register a function to be called by the application layer
358  * when a transaction is to be freed. */
360  NFSTCPStateTxFree);
361 
363  NFSTCPGetTxLogged, NFSTCPSetTxLogged);
364 
365  /* Register a function to return the current transaction count. */
367  NFSTCPGetTxCnt);
368 
369  /* Transaction handling. */
371  NFSTCPGetAlstateProgressCompletionStatus);
373  ALPROTO_NFS, NFSTCPGetStateProgress);
375  NFSTCPGetTx);
377  RustNFSTCPGetTxIterator);
378 
379  AppLayerParserRegisterGetFilesFunc(IPPROTO_TCP, ALPROTO_NFS, NFSTCPGetFiles);
380 
381  /* What is this being registered for? */
383  NFSTCPGetTxDetectState, NFSTCPSetTxDetectState);
384 
386  NFSTCPStateGetEventInfo);
387 
389  NFSTCPStateGetEventInfoById);
390 
392  NFSTCPGetEvents);
393 
395  NFSTCPGetDetectFlags, NFSTCPSetDetectFlags);
396 
397  /* This parser accepts gaps. */
400  }
401  else {
402  SCLogDebug("NFSTCP protocol parsing disabled.");
403  }
404 
405 #ifdef UNITTESTS
408 #endif
409 }
410 
411 #ifdef UNITTESTS
412 #endif
413 
414 void NFSTCPParserRegisterTests(void)
415 {
416 #ifdef UNITTESTS
417 #endif
418 }
419 
420 #endif /* HAVE_RUST */
#define STREAMING_BUFFER_CONFIG_INITIALIZER
enum AppLayerEventType_ AppLayerEventType
uint16_t flags
#define SCLogDebug(...)
Definition: util-debug.h:335
void AppLayerParserRegisterOptionFlags(uint8_t ipproto, AppProto alproto, uint32_t flags)
void AppLayerProtoDetectPPRegister(uint8_t ipproto, const char *portstr, AppProto alproto, uint16_t min_depth, uint16_t max_depth, uint8_t direction, ProbingParserFPtr ProbingParser1, ProbingParserFPtr ProbingParser2)
register parser at a port
void RegisterNFSTCPParsers(void)
void AppLayerParserRegisterGetStateProgressFunc(uint8_t ipproto, AppProto alproto, int(*StateGetProgress)(void *alstate, uint8_t direction))
uint32_t event_type
LoggerId
int logged
int ConfGetBool(const char *name, int *val)
Retrieve a configuration value as an boolen.
Definition: conf.c:517
int AppLayerProtoDetectConfProtoDetectionEnabled(const char *ipproto, const char *alproto)
Given a protocol name, checks if proto detection is enabled in the conf file.
void AppLayerParserRegisterDetectFlagsFuncs(uint8_t ipproto, AppProto alproto, uint64_t(*GetTxDetectFlags)(void *tx, uint8_t dir), void(*SetTxDetectFlags)(void *tx, uint8_t dir, uint64_t))
void AppLayerParserRegisterDetectStateFuncs(uint8_t ipproto, AppProto alproto, DetectEngineState *(*GetTxDetectState)(void *tx), int(*SetTxDetectState)(void *tx, DetectEngineState *))
AppProto(* ProbingParserFPtr)(Flow *f, uint8_t dir, uint8_t *input, uint32_t input_len, uint8_t *rdir)
void AppLayerParserRegisterGetEventsFunc(uint8_t ipproto, AppProto alproto, AppLayerDecoderEvents *(*StateGetEvents)(void *))
void AppLayerParserRegisterLoggerFuncs(uint8_t ipproto, AppProto alproto, LoggerId(*StateGetTxLogged)(void *, void *), void(*StateSetTxLogged)(void *, void *, LoggerId))
uint16_t AppProto
void NFSTCPParserRegisterTests(void)
int AppLayerParserConfParserEnabled(const char *ipproto, const char *alproto_name)
check if a parser is enabled in the config Returns enabled always if: were running unittests and when...
Data structure to store app layer decoder events.
int AppLayerParserRegisterParser(uint8_t ipproto, AppProto alproto, uint8_t direction, AppLayerParserFPtr Parser)
Register app layer parser for the protocol.
void AppLayerParserRegisterGetTx(uint8_t ipproto, AppProto alproto, void *(StateGetTx)(void *alstate, uint64_t tx_id))
void AppLayerParserRegisterGetFilesFunc(uint8_t ipproto, AppProto alproto, FileContainer *(*StateGetFiles)(void *, uint8_t))
int AppLayerProtoDetectPPParseConfPorts(const char *ipproto_name, uint8_t ipproto, const char *alproto_name, AppProto alproto, uint16_t min_depth, uint16_t max_depth, ProbingParserFPtr ProbingParserTs, ProbingParserFPtr ProbingParserTc)
void AppLayerParserRegisterGetEventInfo(uint8_t ipproto, AppProto alproto, int(*StateGetEventInfo)(const char *event_name, int *event_id, AppLayerEventType *event_type))
#define STREAM_TOCLIENT
Definition: stream.h:32
void AppLayerParserRegisterGetStateProgressCompletionStatus(AppProto alproto, int(*StateGetProgressCompletionStatus)(uint8_t direction))
int RunmodeIsUnittests(void)
Definition: suricata.c:265
void AppLayerParserRegisterGetEventInfoById(uint8_t ipproto, AppProto alproto, int(*StateGetEventInfoById)(int event_id, const char **event_name, AppLayerEventType *event_type))
void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_name)
Registers a protocol for protocol detection phase.
PoolThreadReserved res
uint16_t tx_id
void AppLayerParserRegisterProtocolUnittests(uint8_t ipproto, AppProto alproto, void(*RegisterUnittests)(void))
#define STREAM_TOSERVER
Definition: stream.h:31
#define APP_LAYER_PARSER_OPT_ACCEPT_GAPS
void AppLayerParserRegisterGetTxIterator(uint8_t ipproto, AppProto alproto, AppLayerGetTxIteratorFunc Func)
uint16_t FileFlowToFlags(const Flow *flow, uint8_t direction)
Definition: util-file.c:217
void AppLayerParserRegisterGetTxCnt(uint8_t ipproto, AppProto alproto, uint64_t(*StateGetTxCnt)(void *alstate))
void AppLayerParserRegisterStateFuncs(uint8_t ipproto, AppProto alproto, void *(*StateAlloc)(void), void(*StateFree)(void *))
Flow data structure.
Definition: flow.h:325
void AppLayerParserRegisterTxFreeFunc(uint8_t ipproto, AppProto alproto, void(*StateTransactionFree)(void *, uint64_t))