suricata
app-layer-nfs-udp.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 
26 #include "suricata-common.h"
27 #include "stream.h"
28 #include "conf.h"
29 
30 #include "util-unittest.h"
31 
32 #include "app-layer-detect-proto.h"
33 #include "app-layer-parser.h"
34 
35 #include "app-layer-nfs-udp.h"
36 
37 #ifndef HAVE_RUST
39 {
40 }
41 
42 #else
43 
44 #include "rust.h"
45 #include "rust-nfs-nfs-gen.h"
46 
47 /* The default port to probe for echo traffic if not provided in the
48  * configuration file. */
49 #define NFS_DEFAULT_PORT "2049"
50 
51 /* The minimum size for a RFC message. For some protocols this might
52  * be the size of a header. TODO actual min size is likely larger */
53 #define NFS_MIN_FRAME_LEN 32
54 
55 /* Enum of app-layer events for an echo protocol. Normally you might
56  * have events for errors in parsing data, like unexpected data being
57  * received. For echo we'll make something up, and log an app-layer
58  * level alert if an empty message is received.
59  *
60  * Example rule:
61  *
62  * alert nfs any any -> any any (msg:"SURICATA NFS empty message"; \
63  * app-layer-event:nfs.empty_message; sid:X; rev:Y;)
64  */
65 enum {
66  NFS_DECODER_EVENT_EMPTY_MESSAGE,
67 };
68 
69 SCEnumCharMap nfs_udp_decoder_event_table[] = {
70  {"EMPTY_MESSAGE", NFS_DECODER_EVENT_EMPTY_MESSAGE},
71  { NULL, 0 }
72 };
73 
74 static void *NFSStateAlloc(void)
75 {
76  return rs_nfs_state_new();
77 }
78 
79 static void NFSStateFree(void *state)
80 {
81  rs_nfs_state_free(state);
82 }
83 
84 /**
85  * \brief Callback from the application layer to have a transaction freed.
86  *
87  * \param state a void pointer to the NFSState object.
88  * \param tx_id the transaction ID to free.
89  */
90 static void NFSStateTxFree(void *state, uint64_t tx_id)
91 {
92  rs_nfs_state_tx_free(state, tx_id);
93 }
94 
95 static int NFSStateGetEventInfo(const char *event_name, int *event_id,
97 {
98  return rs_nfs_state_get_event_info(event_name, event_id, event_type);
99 }
100 
101 static int NFSStateGetEventInfoById(int event_id, const char **event_name,
103 {
104  *event_name = "NFS UDP event name (generic)";
105  *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
106  return 0;
107 }
108 
109 static AppLayerDecoderEvents *NFSGetEvents(void *tx)
110 {
111  return rs_nfs_state_get_events(tx);
112 }
113 
114 /**
115  * \brief Probe the input to see if it looks like echo.
116  *
117  * \retval ALPROTO_NFS if it looks like echo, otherwise
118  * ALPROTO_UNKNOWN.
119  */
120 static AppProto NFSProbingParser(Flow *f, uint8_t direction,
121  uint8_t *input, uint32_t input_len, uint8_t *rdir)
122 {
123  SCLogDebug("probing");
124  if (input_len < NFS_MIN_FRAME_LEN) {
125  SCLogDebug("unknown");
126  return ALPROTO_UNKNOWN;
127  }
128 
129  int8_t r = 0;
130  if (direction & STREAM_TOSERVER)
131  r = rs_nfs_probe_udp_ts(input, input_len);
132  else
133  r = rs_nfs_probe_udp_tc(input, input_len);
134 
135  if (r == 1) {
136  SCLogDebug("nfs");
137  return ALPROTO_NFS;
138  } else if (r == -1) {
139  SCLogDebug("failed");
140  return ALPROTO_FAILED;
141  }
142 
143  SCLogDebug("Protocol not detected as ALPROTO_NFS.");
144  return ALPROTO_UNKNOWN;
145 }
146 
147 static int NFSParseRequest(Flow *f, void *state,
148  AppLayerParserState *pstate, uint8_t *input, uint32_t input_len,
149  void *local_data, const uint8_t flags)
150 {
151  uint16_t file_flags = FileFlowToFlags(f, STREAM_TOSERVER);
152  rs_nfs_setfileflags(0, state, file_flags);
153 
154  return rs_nfs_parse_request_udp(f, state, pstate, input, input_len, local_data);
155 }
156 
157 static int NFSParseResponse(Flow *f, void *state, AppLayerParserState *pstate,
158  uint8_t *input, uint32_t input_len, void *local_data,
159  const uint8_t flags)
160 {
161  uint16_t file_flags = FileFlowToFlags(f, STREAM_TOCLIENT);
162  rs_nfs_setfileflags(1, state, file_flags);
163 
164  return rs_nfs_parse_response_udp(f, state, pstate, input, input_len, local_data);
165 }
166 
167 static uint64_t NFSGetTxCnt(void *state)
168 {
169  return rs_nfs_state_get_tx_count(state);
170 }
171 
172 static void *NFSGetTx(void *state, uint64_t tx_id)
173 {
174  return rs_nfs_state_get_tx(state, tx_id);
175 }
176 
177 static AppLayerGetTxIterTuple RustNFSGetTxIterator(
178  const uint8_t ipproto, const AppProto alproto,
179  void *alstate, uint64_t min_tx_id, uint64_t max_tx_id,
180  AppLayerGetTxIterState *istate)
181 {
182  return rs_nfs_state_get_tx_iterator(alstate, min_tx_id, (uint64_t *)istate);
183 }
184 
185 static void NFSSetTxLogged(void *state, void *vtx, LoggerId logged)
186 {
187  rs_nfs_tx_set_logged(state, vtx, logged);
188 }
189 
190 static LoggerId NFSGetTxLogged(void *state, void *vtx)
191 {
192  return rs_nfs_tx_get_logged(state, vtx);
193 }
194 
195 /**
196  * \brief Called by the application layer.
197  *
198  * In most cases 1 can be returned here.
199  */
200 static int NFSGetAlstateProgressCompletionStatus(uint8_t direction) {
201  return rs_nfs_state_progress_completion_status(direction);
202 }
203 
204 /**
205  * \brief Return the state of a transaction in a given direction.
206  *
207  * In the case of the echo protocol, the existence of a transaction
208  * means that the request is done. However, some protocols that may
209  * need multiple chunks of data to complete the request may need more
210  * than just the existence of a transaction for the request to be
211  * considered complete.
212  *
213  * For the response to be considered done, the response for a request
214  * needs to be seen. The response_done flag is set on response for
215  * checking here.
216  */
217 static int NFSGetStateProgress(void *tx, uint8_t direction)
218 {
219  return rs_nfs_tx_get_alstate_progress(tx, direction);
220 }
221 
222 /**
223  * \brief get stored tx detect state
224  */
225 static DetectEngineState *NFSGetTxDetectState(void *vtx)
226 {
227  return rs_nfs_state_get_tx_detect_state(vtx);
228 }
229 
230 /**
231  * \brief set store tx detect state
232  */
233 static int NFSSetTxDetectState(void *vtx, DetectEngineState *s)
234 {
235  rs_nfs_state_set_tx_detect_state(vtx, s);
236  return 0;
237 }
238 
239 static FileContainer *NFSGetFiles(void *state, uint8_t direction)
240 {
241  return rs_nfs_getfiles(direction, state);
242 }
243 
244 static void NFSSetDetectFlags(void *tx, uint8_t dir, uint64_t flags)
245 {
246  rs_nfs_tx_set_detect_flags(tx, dir, flags);
247 }
248 
249 static uint64_t NFSGetDetectFlags(void *tx, uint8_t dir)
250 {
251  return rs_nfs_tx_get_detect_flags(tx, dir);
252 }
253 
255 static SuricataFileContext sfc = { &sbcfg };
256 
257 void RegisterNFSUDPParsers(void)
258 {
259  const char *proto_name = "nfs";
260 
261  /* Check if NFS TCP detection is enabled. If it does not exist in
262  * the configuration file then it will be enabled by default. */
263  if (AppLayerProtoDetectConfProtoDetectionEnabled("udp", proto_name)) {
264 
265  rs_nfs_init(&sfc);
266 
267  SCLogDebug("NFS UDP protocol detection enabled.");
268 
270 
271  if (RunmodeIsUnittests()) {
272 
273  SCLogDebug("Unittest mode, registering default configuration.");
274  AppLayerProtoDetectPPRegister(IPPROTO_UDP, NFS_DEFAULT_PORT,
275  ALPROTO_NFS, 0, NFS_MIN_FRAME_LEN, STREAM_TOSERVER,
276  NFSProbingParser, NFSProbingParser);
277 
278  }
279  else {
280 
281  if (!AppLayerProtoDetectPPParseConfPorts("udp", IPPROTO_UDP,
282  proto_name, ALPROTO_NFS, 0, NFS_MIN_FRAME_LEN,
283  NFSProbingParser, NFSProbingParser)) {
284  SCLogDebug("No NFS app-layer configuration, enabling NFS"
285  " detection TCP detection on port %s.",
286  NFS_DEFAULT_PORT);
287  AppLayerProtoDetectPPRegister(IPPROTO_UDP,
288  NFS_DEFAULT_PORT, ALPROTO_NFS, 0,
289  NFS_MIN_FRAME_LEN, STREAM_TOSERVER,
290  NFSProbingParser, NFSProbingParser);
291  }
292 
293  }
294 
295  }
296 
297  else {
298  SCLogDebug("Protocol detecter and parser disabled for NFS.");
299  return;
300  }
301 
302  if (AppLayerParserConfParserEnabled("udp", proto_name))
303  {
304  SCLogDebug("Registering NFS protocol parser.");
305 
306  /* Register functions for state allocation and freeing. A
307  * state is allocated for every new NFS flow. */
309  NFSStateAlloc, NFSStateFree);
310 
311  /* Register request parser for parsing frame from server to client. */
313  STREAM_TOSERVER, NFSParseRequest);
314 
315  /* Register response parser for parsing frames from server to client. */
317  STREAM_TOCLIENT, NFSParseResponse);
318 
319  /* Register a function to be called by the application layer
320  * when a transaction is to be freed. */
322  NFSStateTxFree);
323 
325  NFSGetTxLogged, NFSSetTxLogged);
326 
327  /* Register a function to return the current transaction count. */
329  NFSGetTxCnt);
330 
331  /* Transaction handling. */
333  NFSGetAlstateProgressCompletionStatus);
335  ALPROTO_NFS, NFSGetStateProgress);
337  NFSGetTx);
339  RustNFSGetTxIterator);
340 
341  AppLayerParserRegisterGetFilesFunc(IPPROTO_UDP, ALPROTO_NFS, NFSGetFiles);
342 
343  /* What is this being registered for? */
345  NFSGetTxDetectState, NFSSetTxDetectState);
346 
348  NFSStateGetEventInfo);
349 
351  NFSStateGetEventInfoById);
352 
354  NFSGetEvents);
355 
357  NFSGetDetectFlags, NFSSetDetectFlags);
358 
359  }
360  else {
361  SCLogNotice("NFS protocol parsing disabled.");
362  }
363 
364 #ifdef UNITTESTS
367 #endif
368 }
369 
370 #ifdef UNITTESTS
371 #endif
372 
373 void NFSUDPParserRegisterTests(void)
374 {
375 #ifdef UNITTESTS
376 #endif
377 }
378 
379 #endif /* HAVE_RUST */
#define STREAMING_BUFFER_CONFIG_INITIALIZER
enum AppLayerEventType_ AppLayerEventType
uint16_t flags
#define SCLogDebug(...)
Definition: util-debug.h:335
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 AppLayerParserRegisterGetStateProgressFunc(uint8_t ipproto, AppProto alproto, int(*StateGetProgress)(void *alstate, uint8_t direction))
uint32_t event_type
LoggerId
int logged
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 *))
void RegisterNFSUDPParsers(void)
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
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))
void NFSUDPParserRegisterTests(void)
#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.
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:269
uint16_t tx_id
void AppLayerParserRegisterProtocolUnittests(uint8_t ipproto, AppProto alproto, void(*RegisterUnittests)(void))
#define STREAM_TOSERVER
Definition: stream.h:31
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))