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 AppLayerDecoderEvents *NFSGetEvents(void *state, uint64_t id)
102 {
103  return rs_nfs_state_get_events(state, id);
104 }
105 
106 /**
107  * \brief Probe the input to see if it looks like echo.
108  *
109  * \retval ALPROTO_NFS if it looks like echo, otherwise
110  * ALPROTO_UNKNOWN.
111  */
112 static AppProto NFSProbingParser(Flow *f, uint8_t direction,
113  uint8_t *input, uint32_t input_len, uint8_t *rdir)
114 {
115  SCLogDebug("probing");
116  if (input_len < NFS_MIN_FRAME_LEN) {
117  SCLogDebug("unknown");
118  return ALPROTO_UNKNOWN;
119  }
120 
121  int8_t r = 0;
122  if (direction & STREAM_TOSERVER)
123  r = rs_nfs_probe_udp_ts(input, input_len);
124  else
125  r = rs_nfs_probe_udp_tc(input, input_len);
126 
127  if (r == 1) {
128  SCLogDebug("nfs");
129  return ALPROTO_NFS;
130  } else if (r == -1) {
131  SCLogDebug("failed");
132  return ALPROTO_FAILED;
133  }
134 
135  SCLogDebug("Protocol not detected as ALPROTO_NFS.");
136  return ALPROTO_UNKNOWN;
137 }
138 
139 static int NFSParseRequest(Flow *f, void *state,
140  AppLayerParserState *pstate, uint8_t *input, uint32_t input_len,
141  void *local_data, const uint8_t flags)
142 {
143  uint16_t file_flags = FileFlowToFlags(f, STREAM_TOSERVER);
144  rs_nfs_setfileflags(0, state, file_flags);
145 
146  return rs_nfs_parse_request_udp(f, state, pstate, input, input_len, local_data);
147 }
148 
149 static int NFSParseResponse(Flow *f, void *state, AppLayerParserState *pstate,
150  uint8_t *input, uint32_t input_len, void *local_data,
151  const uint8_t flags)
152 {
153  uint16_t file_flags = FileFlowToFlags(f, STREAM_TOCLIENT);
154  rs_nfs_setfileflags(1, state, file_flags);
155 
156  return rs_nfs_parse_response_udp(f, state, pstate, input, input_len, local_data);
157 }
158 
159 static uint64_t NFSGetTxCnt(void *state)
160 {
161  return rs_nfs_state_get_tx_count(state);
162 }
163 
164 static void *NFSGetTx(void *state, uint64_t tx_id)
165 {
166  return rs_nfs_state_get_tx(state, tx_id);
167 }
168 
169 static AppLayerGetTxIterTuple RustNFSGetTxIterator(
170  const uint8_t ipproto, const AppProto alproto,
171  void *alstate, uint64_t min_tx_id, uint64_t max_tx_id,
172  AppLayerGetTxIterState *istate)
173 {
174  return rs_nfs_state_get_tx_iterator(alstate, min_tx_id, (uint64_t *)istate);
175 }
176 
177 static void NFSSetTxLogged(void *state, void *vtx, LoggerId logged)
178 {
179  rs_nfs_tx_set_logged(state, vtx, logged);
180 }
181 
182 static LoggerId NFSGetTxLogged(void *state, void *vtx)
183 {
184  return rs_nfs_tx_get_logged(state, vtx);
185 }
186 
187 /**
188  * \brief Called by the application layer.
189  *
190  * In most cases 1 can be returned here.
191  */
192 static int NFSGetAlstateProgressCompletionStatus(uint8_t direction) {
193  return rs_nfs_state_progress_completion_status(direction);
194 }
195 
196 /**
197  * \brief Return the state of a transaction in a given direction.
198  *
199  * In the case of the echo protocol, the existence of a transaction
200  * means that the request is done. However, some protocols that may
201  * need multiple chunks of data to complete the request may need more
202  * than just the existence of a transaction for the request to be
203  * considered complete.
204  *
205  * For the response to be considered done, the response for a request
206  * needs to be seen. The response_done flag is set on response for
207  * checking here.
208  */
209 static int NFSGetStateProgress(void *tx, uint8_t direction)
210 {
211  return rs_nfs_tx_get_alstate_progress(tx, direction);
212 }
213 
214 /**
215  * \brief get stored tx detect state
216  */
217 static DetectEngineState *NFSGetTxDetectState(void *vtx)
218 {
219  return rs_nfs_state_get_tx_detect_state(vtx);
220 }
221 
222 /**
223  * \brief set store tx detect state
224  */
225 static int NFSSetTxDetectState(void *vtx, DetectEngineState *s)
226 {
227  rs_nfs_state_set_tx_detect_state(vtx, s);
228  return 0;
229 }
230 
231 static FileContainer *NFSGetFiles(void *state, uint8_t direction)
232 {
233  return rs_nfs_getfiles(direction, state);
234 }
235 
236 static void NFSSetDetectFlags(void *tx, uint8_t dir, uint64_t flags)
237 {
238  rs_nfs_tx_set_detect_flags(tx, dir, flags);
239 }
240 
241 static uint64_t NFSGetDetectFlags(void *tx, uint8_t dir)
242 {
243  return rs_nfs_tx_get_detect_flags(tx, dir);
244 }
245 
247 static SuricataFileContext sfc = { &sbcfg };
248 
249 void RegisterNFSUDPParsers(void)
250 {
251  const char *proto_name = "nfs";
252 
253  /* Check if NFS TCP detection is enabled. If it does not exist in
254  * the configuration file then it will be enabled by default. */
255  if (AppLayerProtoDetectConfProtoDetectionEnabled("udp", proto_name)) {
256 
257  rs_nfs_init(&sfc);
258 
259  SCLogDebug("NFS UDP protocol detection enabled.");
260 
262 
263  if (RunmodeIsUnittests()) {
264 
265  SCLogDebug("Unittest mode, registering default configuration.");
266  AppLayerProtoDetectPPRegister(IPPROTO_UDP, NFS_DEFAULT_PORT,
267  ALPROTO_NFS, 0, NFS_MIN_FRAME_LEN, STREAM_TOSERVER,
268  NFSProbingParser, NFSProbingParser);
269 
270  }
271  else {
272 
273  if (!AppLayerProtoDetectPPParseConfPorts("udp", IPPROTO_UDP,
274  proto_name, ALPROTO_NFS, 0, NFS_MIN_FRAME_LEN,
275  NFSProbingParser, NFSProbingParser)) {
276  SCLogDebug("No NFS app-layer configuration, enabling NFS"
277  " detection TCP detection on port %s.",
278  NFS_DEFAULT_PORT);
279  AppLayerProtoDetectPPRegister(IPPROTO_UDP,
280  NFS_DEFAULT_PORT, ALPROTO_NFS, 0,
281  NFS_MIN_FRAME_LEN, STREAM_TOSERVER,
282  NFSProbingParser, NFSProbingParser);
283  }
284 
285  }
286 
287  }
288 
289  else {
290  SCLogDebug("Protocol detecter and parser disabled for NFS.");
291  return;
292  }
293 
294  if (AppLayerParserConfParserEnabled("udp", proto_name))
295  {
296  SCLogDebug("Registering NFS protocol parser.");
297 
298  /* Register functions for state allocation and freeing. A
299  * state is allocated for every new NFS flow. */
301  NFSStateAlloc, NFSStateFree);
302 
303  /* Register request parser for parsing frame from server to client. */
305  STREAM_TOSERVER, NFSParseRequest);
306 
307  /* Register response parser for parsing frames from server to client. */
309  STREAM_TOCLIENT, NFSParseResponse);
310 
311  /* Register a function to be called by the application layer
312  * when a transaction is to be freed. */
314  NFSStateTxFree);
315 
317  NFSGetTxLogged, NFSSetTxLogged);
318 
319  /* Register a function to return the current transaction count. */
321  NFSGetTxCnt);
322 
323  /* Transaction handling. */
325  NFSGetAlstateProgressCompletionStatus);
327  ALPROTO_NFS, NFSGetStateProgress);
329  NFSGetTx);
331  RustNFSGetTxIterator);
332 
333  AppLayerParserRegisterGetFilesFunc(IPPROTO_UDP, ALPROTO_NFS, NFSGetFiles);
334 
335  /* What is this being registered for? */
337  NFSGetTxDetectState, NFSSetTxDetectState);
338 
340  NFSStateGetEventInfo);
342  NFSGetEvents);
343 
345  NFSGetDetectFlags, NFSSetDetectFlags);
346 
347  }
348  else {
349  SCLogNotice("NFS protocol parsing disabled.");
350  }
351 
352 #ifdef UNITTESTS
355 #endif
356 }
357 
358 #ifdef UNITTESTS
359 #endif
360 
361 void NFSUDPParserRegisterTests(void)
362 {
363 #ifdef UNITTESTS
364 #endif
365 }
366 
367 #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 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:259
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 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
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:324
void AppLayerParserRegisterTxFreeFunc(uint8_t ipproto, AppProto alproto, void(*StateTransactionFree)(void *, uint64_t))