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 AppLayerDecoderEvents *NFSTCPGetEvents(void *state, uint64_t id)
105 {
106  return rs_nfs_state_get_events(state, id);
107 }
108 
109 /**
110  * \brief Probe the input to see if it looks like echo.
111  *
112  * \retval ALPROTO_NFS if it looks like echo, otherwise
113  * ALPROTO_UNKNOWN.
114  */
115 static AppProto NFSTCPProbingParserMidstream(Flow *f,
116  uint8_t direction,
117  uint8_t *input, uint32_t input_len,
118  uint8_t *rdir)
119 {
120  if (input_len < NFSTCP_MIN_FRAME_LEN) {
121  return ALPROTO_UNKNOWN;
122  }
123 
124  int8_t r = rs_nfs_probe_ms(direction, input, input_len, rdir);
125  if (r == 1) {
126  return ALPROTO_NFS;
127  } else if (r == -1) {
128  return ALPROTO_FAILED;
129  }
130 
131  SCLogDebug("Protocol not detected as ALPROTO_NFS.");
132  return ALPROTO_UNKNOWN;
133 }
134 
135 /**
136  * \brief Probe the input to see if it looks like echo.
137  *
138  * \retval ALPROTO_NFS if it looks like echo, otherwise
139  * ALPROTO_UNKNOWN.
140  */
141 static AppProto NFSTCPProbingParser(Flow *f,
142  uint8_t direction,
143  uint8_t *input, uint32_t input_len,
144  uint8_t *rdir)
145 {
146  if (input_len < NFSTCP_MIN_FRAME_LEN) {
147  return ALPROTO_UNKNOWN;
148  }
149 
150  int8_t r = rs_nfs_probe(direction, input, input_len);
151  if (r == 1) {
152  return ALPROTO_NFS;
153  } else if (r == -1) {
154  return ALPROTO_FAILED;
155  }
156 
157  SCLogDebug("Protocol not detected as ALPROTO_NFS.");
158  return ALPROTO_UNKNOWN;
159 }
160 
161 static int NFSTCPParseRequest(Flow *f, void *state,
162  AppLayerParserState *pstate, uint8_t *input, uint32_t input_len,
163  void *local_data, const uint8_t flags)
164 {
165  uint16_t file_flags = FileFlowToFlags(f, STREAM_TOSERVER);
166  rs_nfs_setfileflags(0, state, file_flags);
167 
168  int res;
169  if (input == NULL && input_len > 0) {
170  res = rs_nfs_parse_request_tcp_gap(state, input_len);
171  } else {
172  res = rs_nfs_parse_request(f, state, pstate, input, input_len, local_data);
173  }
174  return res;
175 }
176 
177 static int NFSTCPParseResponse(Flow *f, void *state, AppLayerParserState *pstate,
178  uint8_t *input, uint32_t input_len, void *local_data,
179  const uint8_t flags)
180 {
181  uint16_t file_flags = FileFlowToFlags(f, STREAM_TOCLIENT);
182  rs_nfs_setfileflags(1, state, file_flags);
183 
184  int res;
185  if (input == NULL && input_len > 0) {
186  res = rs_nfs_parse_response_tcp_gap(state, input_len);
187  } else {
188  res = rs_nfs_parse_response(f, state, pstate, input, input_len, local_data);
189  }
190  return res;
191 }
192 
193 static uint64_t NFSTCPGetTxCnt(void *state)
194 {
195  return rs_nfs_state_get_tx_count(state);
196 }
197 
198 static void *NFSTCPGetTx(void *state, uint64_t tx_id)
199 {
200  return rs_nfs_state_get_tx(state, tx_id);
201 }
202 
203 static AppLayerGetTxIterTuple RustNFSTCPGetTxIterator(
204  const uint8_t ipproto, const AppProto alproto,
205  void *alstate, uint64_t min_tx_id, uint64_t max_tx_id,
206  AppLayerGetTxIterState *istate)
207 {
208  return rs_nfs_state_get_tx_iterator(alstate, min_tx_id, (uint64_t *)istate);
209 }
210 
211 static void NFSTCPSetTxLogged(void *state, void *vtx, LoggerId logged)
212 {
213  rs_nfs_tx_set_logged(state, vtx, logged);
214 }
215 
216 static LoggerId NFSTCPGetTxLogged(void *state, void *vtx)
217 {
218  return rs_nfs_tx_get_logged(state, vtx);
219 }
220 
221 /**
222  * \brief Called by the application layer.
223  *
224  * In most cases 1 can be returned here.
225  */
226 static int NFSTCPGetAlstateProgressCompletionStatus(uint8_t direction) {
227  return rs_nfs_state_progress_completion_status(direction);
228 }
229 
230 /**
231  * \brief Return the state of a transaction in a given direction.
232  *
233  * In the case of the echo protocol, the existence of a transaction
234  * means that the request is done. However, some protocols that may
235  * need multiple chunks of data to complete the request may need more
236  * than just the existence of a transaction for the request to be
237  * considered complete.
238  *
239  * For the response to be considered done, the response for a request
240  * needs to be seen. The response_done flag is set on response for
241  * checking here.
242  */
243 static int NFSTCPGetStateProgress(void *tx, uint8_t direction)
244 {
245  return rs_nfs_tx_get_alstate_progress(tx, direction);
246 }
247 
248 /**
249  * \brief get stored tx detect state
250  */
251 static DetectEngineState *NFSTCPGetTxDetectState(void *vtx)
252 {
253  return rs_nfs_state_get_tx_detect_state(vtx);
254 }
255 
256 /**
257  * \brief set store tx detect state
258  */
259 static int NFSTCPSetTxDetectState(void *vtx, DetectEngineState *s)
260 {
261  rs_nfs_state_set_tx_detect_state(vtx, s);
262  return 0;
263 }
264 
265 static FileContainer *NFSTCPGetFiles(void *state, uint8_t direction)
266 {
267  return rs_nfs_getfiles(direction, state);
268 }
269 
270 static void NFSTCPSetDetectFlags(void *tx, uint8_t dir, uint64_t flags)
271 {
272  rs_nfs_tx_set_detect_flags(tx, dir, flags);
273 }
274 
275 static uint64_t NFSTCPGetDetectFlags(void *tx, uint8_t dir)
276 {
277  return rs_nfs_tx_get_detect_flags(tx, dir);
278 }
279 
281 static SuricataFileContext sfc = { &sbcfg };
282 
283 void RegisterNFSTCPParsers(void)
284 {
285  const char *proto_name = "nfs";
286 
287  /* Check if NFSTCP TCP detection is enabled. If it does not exist in
288  * the configuration file then it will be enabled by default. */
289  if (AppLayerProtoDetectConfProtoDetectionEnabled("tcp", proto_name)) {
290 
291  rs_nfs_init(&sfc);
292 
293  SCLogDebug("NFSTCP TCP protocol detection enabled.");
294 
296 
297  if (RunmodeIsUnittests()) {
298 
299  SCLogDebug("Unittest mode, registering default configuration.");
300  AppLayerProtoDetectPPRegister(IPPROTO_TCP, NFSTCP_DEFAULT_PORT,
301  ALPROTO_NFS, 0, NFSTCP_MIN_FRAME_LEN, STREAM_TOSERVER,
302  NFSTCPProbingParser, NFSTCPProbingParser);
303 
304  }
305  else {
306  int midstream = 0;
307  ConfGetBool("stream.midstream", &midstream);
308  ProbingParserFPtr FuncPtr = NFSTCPProbingParser;
309  if (midstream)
310  FuncPtr = NFSTCPProbingParserMidstream;
311 
312  if (!AppLayerProtoDetectPPParseConfPorts("tcp", IPPROTO_TCP,
313  proto_name, ALPROTO_NFS, 0, NFSTCP_MIN_FRAME_LEN,
314  FuncPtr, FuncPtr)) {
315  SCLogDebug("No NFSTCP app-layer configuration, enabling NFSTCP"
316  " detection TCP detection on port %s.",
317  NFSTCP_DEFAULT_PORT);
318  /* register 'midstream' probing parsers if midstream is enabled. */
319  AppLayerProtoDetectPPRegister(IPPROTO_TCP,
320  NFSTCP_DEFAULT_PORT, ALPROTO_NFS, 0,
321  NFSTCP_MIN_FRAME_LEN, STREAM_TOSERVER,
322  FuncPtr, FuncPtr);
323  }
324 
325  }
326 
327  }
328 
329  else {
330  SCLogDebug("Protocol detecter and parser disabled for NFSTCP.");
331  return;
332  }
333 
334  if (AppLayerParserConfParserEnabled("tcp", proto_name))
335  {
336  SCLogDebug("Registering NFSTCP protocol parser.");
337 
338  /* Register functions for state allocation and freeing. A
339  * state is allocated for every new NFSTCP flow. */
341  NFSTCPStateAlloc, NFSTCPStateFree);
342 
343  /* Register request parser for parsing frame from server to client. */
345  STREAM_TOSERVER, NFSTCPParseRequest);
346 
347  /* Register response parser for parsing frames from server to client. */
349  STREAM_TOCLIENT, NFSTCPParseResponse);
350 
351  /* Register a function to be called by the application layer
352  * when a transaction is to be freed. */
354  NFSTCPStateTxFree);
355 
357  NFSTCPGetTxLogged, NFSTCPSetTxLogged);
358 
359  /* Register a function to return the current transaction count. */
361  NFSTCPGetTxCnt);
362 
363  /* Transaction handling. */
365  NFSTCPGetAlstateProgressCompletionStatus);
367  ALPROTO_NFS, NFSTCPGetStateProgress);
369  NFSTCPGetTx);
371  RustNFSTCPGetTxIterator);
372 
373  AppLayerParserRegisterGetFilesFunc(IPPROTO_TCP, ALPROTO_NFS, NFSTCPGetFiles);
374 
375  /* What is this being registered for? */
377  NFSTCPGetTxDetectState, NFSTCPSetTxDetectState);
378 
380  NFSTCPStateGetEventInfo);
382  NFSTCPGetEvents);
383 
385  NFSTCPGetDetectFlags, NFSTCPSetDetectFlags);
386 
387  /* This parser accepts gaps. */
390  }
391  else {
392  SCLogDebug("NFSTCP protocol parsing disabled.");
393  }
394 
395 #ifdef UNITTESTS
398 #endif
399 }
400 
401 #ifdef UNITTESTS
402 #endif
403 
404 void NFSTCPParserRegisterTests(void)
405 {
406 #ifdef UNITTESTS
407 #endif
408 }
409 
410 #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 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:261
void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_name)
Registers a protocol for protocol detection phase.
PoolThreadReserved res
uint16_t tx_id
void AppLayerParserRegisterGetEventsFunc(uint8_t ipproto, AppProto alproto, AppLayerDecoderEvents *(*StateGetEvents)(void *, uint64_t))
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))