suricata
app-layer-register.c
Go to the documentation of this file.
1 /* Copyright (C) 2017 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 Pierre Chifflier <chifflier@wzdftpd.net>
22  *
23  * Parser registration functions.
24  */
25 
26 #include "suricata-common.h"
27 #include "stream.h"
28 #include "conf.h"
29 
30 #include "app-layer-detect-proto.h"
31 #include "app-layer-parser.h"
32 
33 #include "app-layer-register.h"
34 
35 static const char * IpProtoToString(int ip_proto);
36 
37 AppProto AppLayerRegisterProtocolDetection(const struct AppLayerParser *p, int enable_default)
38 {
39  AppProto alproto;
40  const char *ip_proto_str = NULL;
41 
42  if (p == NULL)
43  FatalError(SC_ERR_FATAL, "Call to %s with NULL pointer.", __FUNCTION__);
44 
45  alproto = StringToAppProto(p->name);
46  if (alproto == ALPROTO_UNKNOWN || alproto == ALPROTO_FAILED)
47  FatalError(SC_ERR_FATAL, "Unknown or invalid AppProto '%s'.", p->name);
48 
49  ip_proto_str = IpProtoToString(p->ip_proto);
50  if (ip_proto_str == NULL)
51  FatalError(SC_ERR_FATAL, "Unknown or unsupported ip_proto field in parser '%s'", p->name);
52 
53  SCLogDebug("%s %s protocol detection enabled.", ip_proto_str, p->name);
54 
56 
57  if (RunmodeIsUnittests()) {
58 
59  SCLogDebug("Unittest mode, registering default configuration.");
61  alproto, p->min_depth, p->max_depth, STREAM_TOSERVER,
62  p->ProbeTS, p->ProbeTC);
63 
64  }
65  else {
66 
67  if (!AppLayerProtoDetectPPParseConfPorts(ip_proto_str, p->ip_proto,
68  p->name, alproto, p->min_depth, p->max_depth,
69  p->ProbeTS, p->ProbeTC)) {
70  if (enable_default != 0) {
71  SCLogDebug("No %s app-layer configuration, enabling %s"
72  " detection %s detection on port %s.",
73  p->name, p->name, ip_proto_str, p->default_port);
75  p->default_port, alproto,
77  p->ProbeTS, p->ProbeTC);
78  } else {
79  SCLogDebug("No %s app-layer configuration for detection port (%s).",
80  p->name, ip_proto_str);
81  }
82  }
83 
84  }
85 
86  return alproto;
87 }
88 
89 int AppLayerRegisterParser(const struct AppLayerParser *p, AppProto alproto)
90 {
91  const char *ip_proto_str = NULL;
92 
93  if (p == NULL)
94  FatalError(SC_ERR_FATAL, "Call to %s with NULL pointer.", __FUNCTION__);
95 
96  if (alproto == ALPROTO_UNKNOWN || alproto >= ALPROTO_FAILED)
97  FatalError(SC_ERR_FATAL, "Unknown or invalid AppProto '%s'.", p->name);
98 
99  ip_proto_str = IpProtoToString(p->ip_proto);
100  if (ip_proto_str == NULL)
101  FatalError(SC_ERR_FATAL, "Unknown or unsupported ip_proto field in parser '%s'", p->name);
102 
103  SCLogDebug("Registering %s protocol parser.", p->name);
104 
105  /* Register functions for state allocation and freeing. A
106  * state is allocated for every new flow. */
108  p->StateAlloc, p->StateFree);
109 
110  /* Register request parser for parsing frame from server to server. */
113 
114  /* Register response parser for parsing frames from server to client. */
117 
118  /* Register a function to be called by the application layer
119  * when a transaction is to be freed. */
122 
123  /* Register a function to return the current transaction count. */
125  p->StateGetTxCnt);
126 
127  /* Transaction handling. */
131  p->StateGetProgress);
133  p->StateGetTx);
134 
135  if (p->StateGetTxLogged && p->StateSetTxLogged) {
138  }
139 
140  /* What is this being registered for? */
143 
144  if (p->StateGetEventInfo) {
146  p->StateGetEventInfo);
147  }
148  if (p->StateGetEvents) {
150  p->StateGetEvents);
151  }
152  if (p->LocalStorageAlloc && p->LocalStorageFree) {
155  }
156  if (p->GetTxMpmIDs && p->SetTxMpmIDs) {
158  p->GetTxMpmIDs, p->SetTxMpmIDs);
159  }
160  if (p->StateGetFiles) {
162  p->StateGetFiles);
163  }
164 
165  if (p->GetTxIterator) {
167  p->GetTxIterator);
168  }
169 
170  return 0;
171 }
172 
173 static const char * IpProtoToString(int ip_proto)
174 {
175  switch (ip_proto) {
176  case IPPROTO_TCP:
177  return "tcp";
178  case IPPROTO_UDP:
179  return "udp";
180  default:
181  return NULL;
182  };
183 
184 }
const char * default_port
int(* StateGetProgress)(void *alstate, uint8_t direction)
#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 AppLayerParserRegisterLocalStorageFunc(uint8_t ipproto, AppProto alproto, void *(*LocalStorageAlloc)(void), void(*LocalStorageFree)(void *))
FileContainer *(* StateGetFiles)(void *, uint8_t)
void AppLayerParserRegisterGetStateProgressFunc(uint8_t ipproto, AppProto alproto, int(*StateGetProgress)(void *alstate, uint8_t direction))
void *(* LocalStorageAlloc)(void)
void AppLayerParserRegisterDetectStateFuncs(uint8_t ipproto, AppProto alproto, DetectEngineState *(*GetTxDetectState)(void *tx), int(*SetTxDetectState)(void *tx, DetectEngineState *))
int AppLayerRegisterParser(const struct AppLayerParser *p, AppProto alproto)
App layer protocol registration function.
uint32_t(* StateGetTxLogged)(void *alstate, void *tx)
AppLayerGetTxIterTuple(* GetTxIterator)(const uint8_t ipproto, const AppProto alproto, void *alstate, uint64_t min_tx_id, uint64_t max_tx_id, AppLayerGetTxIterState *istate)
void AppLayerParserRegisterLoggerFuncs(uint8_t ipproto, AppProto alproto, LoggerId(*StateGetTxLogged)(void *, void *), void(*StateSetTxLogged)(void *, void *, LoggerId))
uint16_t AppProto
int(* StateGetProgressCompletionStatus)(uint8_t direction)
uint64_t(* StateGetTxCnt)(void *alstate)
void *(* StateAlloc)(void)
int AppLayerParserRegisterParser(uint8_t ipproto, AppProto alproto, uint8_t direction, AppLayerParserFPtr Parser)
Register app layer parser for the protocol.
const char * name
void AppLayerParserRegisterGetTx(uint8_t ipproto, AppProto alproto, void *(StateGetTx)(void *alstate, uint64_t tx_id))
AppProto AppLayerRegisterProtocolDetection(const struct AppLayerParser *p, int enable_default)
App layer protocol detection function.
void(* StateFree)(void *)
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))
AppProto StringToAppProto(const char *proto_name)
Maps a string to its ALPROTO_* equivalent.
#define STREAM_TOCLIENT
Definition: stream.h:32
void AppLayerParserRegisterGetStateProgressCompletionStatus(AppProto alproto, int(*StateGetProgressCompletionStatus)(uint8_t direction))
DetectEngineState *(* GetTxDetectState)(void *tx)
int RunmodeIsUnittests(void)
Definition: suricata.c:261
void(* LocalStorageFree)(void *)
uint64_t(* GetTxMpmIDs)(void *tx)
void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_name)
Registers a protocol for protocol detection phase.
void AppLayerParserRegisterGetEventsFunc(uint8_t ipproto, AppProto alproto, AppLayerDecoderEvents *(*StateGetEvents)(void *, uint64_t))
AppLayerDecoderEvents *(* StateGetEvents)(void *, uint64_t)
#define FatalError(x,...)
Definition: util-debug.h:539
void(* StateTransactionFree)(void *, uint64_t)
#define STREAM_TOSERVER
Definition: stream.h:31
void AppLayerParserRegisterGetTxIterator(uint8_t ipproto, AppProto alproto, AppLayerGetTxIteratorFunc Func)
int(* StateGetEventInfo)(const char *event_name, int *event_id, AppLayerEventType *event_type)
ProbingParserFPtr ProbeTS
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 *))
void(* StateSetTxLogged)(void *alstate, void *tx, uint32_t logger)
void *(* StateGetTx)(void *alstate, uint64_t tx_id)
ProbingParserFPtr ProbeTC
AppLayerParserFPtr ParseTC
int(* SetTxDetectState)(void *tx, DetectEngineState *)
AppLayerParserFPtr ParseTS
int(* SetTxMpmIDs)(void *tx, uint64_t)
void AppLayerParserRegisterTxFreeFunc(uint8_t ipproto, AppProto alproto, void(*StateTransactionFree)(void *, uint64_t))
void AppLayerParserRegisterMpmIDsFuncs(uint8_t ipproto, AppProto alproto, uint64_t(*GetTxMpmIDs)(void *tx), int(*SetTxMpmIDs)(void *tx, uint64_t))