suricata
app-layer-register.c
Go to the documentation of this file.
1 /* Copyright (C) 2017-2020 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 "suricata.h"
28 #include "stream.h"
29 #include "conf.h"
30 
31 #include "app-layer-detect-proto.h"
32 #include "app-layer-parser.h"
33 
34 #include "app-layer-register.h"
35 
36 static const char * IpProtoToString(int ip_proto);
37 
38 AppProto AppLayerRegisterProtocolDetection(const struct AppLayerParser *p, int enable_default)
39 {
40  AppProto alproto;
41  const char *ip_proto_str = NULL;
42 
43  if (p == NULL)
44  FatalError("Call to %s with NULL pointer.", __FUNCTION__);
45 
46  alproto = StringToAppProto(p->name);
47  if (alproto == ALPROTO_UNKNOWN || alproto == ALPROTO_FAILED)
48  FatalError("Unknown or invalid AppProto '%s'.", p->name);
49 
50  BUG_ON(strcmp(p->name, AppProtoToString(alproto)) != 0);
51 
52  ip_proto_str = IpProtoToString(p->ip_proto);
53  if (ip_proto_str == NULL)
54  FatalError("Unknown or unsupported ip_proto field in parser '%s'", p->name);
55 
56  SCLogDebug("%s %s protocol detection enabled.", ip_proto_str, p->name);
57 
59 
60  if (p->ProbeTS == NULL && p->ProbeTC == NULL) {
61  BUG_ON(p->default_port != NULL);
62  return alproto;
63  }
64 
65  if (RunmodeIsUnittests()) {
66 
67  SCLogDebug("Unittest mode, registering default configuration.");
69  alproto, p->min_depth, p->max_depth, STREAM_TOSERVER,
70  p->ProbeTS, p->ProbeTC);
71 
72  }
73  else {
74 
75  if (!AppLayerProtoDetectPPParseConfPorts(ip_proto_str, p->ip_proto,
76  p->name, alproto, p->min_depth, p->max_depth,
77  p->ProbeTS, p->ProbeTC)) {
78  if (enable_default != 0) {
79  SCLogDebug("No %s app-layer configuration, enabling %s"
80  " detection %s detection on port %s.",
81  p->name, p->name, ip_proto_str, p->default_port);
83  p->default_port, alproto,
84  p->min_depth, p->max_depth, STREAM_TOSERVER,
85  p->ProbeTS, p->ProbeTC);
86  } else {
87  SCLogDebug("No %s app-layer configuration for detection port (%s).",
88  p->name, ip_proto_str);
89  }
90  }
91 
92  }
93 
94  return alproto;
95 }
96 
97 int AppLayerRegisterParser(const struct AppLayerParser *p, AppProto alproto)
98 {
99  const char *ip_proto_str = NULL;
100 
101  if (p == NULL)
102  FatalError("Call to %s with NULL pointer.", __FUNCTION__);
103 
104  if (alproto == ALPROTO_UNKNOWN || alproto >= ALPROTO_FAILED)
105  FatalError("Unknown or invalid AppProto '%s'.", p->name);
106 
107  BUG_ON(strcmp(p->name, AppProtoToString(alproto)) != 0);
108 
109  ip_proto_str = IpProtoToString(p->ip_proto);
110  if (ip_proto_str == NULL)
111  FatalError("Unknown or unsupported ip_proto field in parser '%s'", p->name);
112 
113  SCLogDebug("Registering %s protocol parser.", p->name);
114 
115  /* Register functions for state allocation and freeing. A
116  * state is allocated for every new flow. */
118  p->StateAlloc, p->StateFree);
119 
120  /* Register request parser for parsing frame from server to server. */
122  STREAM_TOSERVER, p->ParseTS);
123 
124  /* Register response parser for parsing frames from server to client. */
126  STREAM_TOCLIENT, p->ParseTC);
127 
128  /* Register a function to be called by the application layer
129  * when a transaction is to be freed. */
132 
133  /* Register a function to return the current transaction count. */
135  p->StateGetTxCnt);
136 
137  /* Transaction handling. */
139 
141  p->StateGetProgress);
143  p->StateGetTx);
144 
145  if (p->StateGetEventInfo) {
147  p->StateGetEventInfo);
148  }
149  if (p->StateGetEventInfoById) {
152  }
153  if (p->LocalStorageAlloc && p->LocalStorageFree) {
156  }
157  if (p->GetTxFiles) {
159  }
160 
161  if (p->GetTxIterator) {
163  p->GetTxIterator);
164  }
165 
166  if (p->GetTxData) {
168  p->GetTxData);
169  }
170 
171  if (p->GetStateData) {
173  }
174 
175  if (p->ApplyTxConfig) {
177  p->ApplyTxConfig);
178  }
179 
180  if (p->flags) {
182  p->flags);
183 
184  }
185 
186  if (p->Truncate) {
188  }
189 
190  if (p->GetFrameIdByName && p->GetFrameNameById) {
192  p->ip_proto, alproto, p->GetFrameIdByName, p->GetFrameNameById);
193  }
194 
195  return 0;
196 }
197 
198 int AppLayerRegisterParserAlias(const char *proto_name, const char *proto_alias)
199 {
200  AppLayerProtoDetectRegisterAlias(proto_name, proto_alias);
201 
202  return 0;
203 }
204 
205 static const char * IpProtoToString(int ip_proto)
206 {
207  switch (ip_proto) {
208  case IPPROTO_TCP:
209  return "tcp";
210  case IPPROTO_UDP:
211  return "udp";
212  default:
213  return NULL;
214  };
215 
216 }
AppLayerParser::ip_proto
uint8_t ip_proto
Definition: app-layer-register.h:32
AppLayerParser::StateGetEventInfo
int(* StateGetEventInfo)(const char *event_name, int *event_id, AppLayerEventType *event_type)
Definition: app-layer-register.h:54
AppLayerParserRegisterGetStateProgressFunc
void AppLayerParserRegisterGetStateProgressFunc(uint8_t ipproto, AppProto alproto, int(*StateGetProgress)(void *alstate, uint8_t direction))
Definition: app-layer-parser.c:494
AppLayerProtoDetectPPParseConfPorts
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)
Definition: app-layer-detect-proto.c:1605
app-layer-register.h
AppLayerParserRegisterLocalStorageFunc
void AppLayerParserRegisterLocalStorageFunc(uint8_t ipproto, AppProto alproto, void *(*LocalStorageAlloc)(void), void(*LocalStorageFree)(void *))
Definition: app-layer-parser.c:442
AppLayerParserRegisterOptionFlags
void AppLayerParserRegisterOptionFlags(uint8_t ipproto, AppProto alproto, uint32_t flags)
Definition: app-layer-parser.c:413
AppLayerParser::default_port
const char * default_port
Definition: app-layer-register.h:31
AppLayerParser::max_depth
uint16_t max_depth
Definition: app-layer-register.h:38
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:80
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:74
AppLayerParserRegisterTruncateFunc
void AppLayerParserRegisterTruncateFunc(uint8_t ipproto, AppProto alproto, void(*Truncate)(void *, uint8_t))
Definition: app-layer-parser.c:484
AppLayerParser::ProbeTS
ProbingParserFPtr ProbeTS
Definition: app-layer-register.h:34
AppLayerParserRegisterStateProgressCompletionStatus
void AppLayerParserRegisterStateProgressCompletionStatus(AppProto alproto, const int ts, const int tc)
Definition: app-layer-parser.c:546
AppLayerParserRegisterTxFreeFunc
void AppLayerParserRegisterTxFreeFunc(uint8_t ipproto, AppProto alproto, void(*StateTransactionFree)(void *, uint64_t))
Definition: app-layer-parser.c:505
AppLayerParser::LocalStorageAlloc
void *(* LocalStorageAlloc)(void)
Definition: app-layer-register.h:59
AppLayerParserRegisterGetTxFilesFunc
void AppLayerParserRegisterGetTxFilesFunc(uint8_t ipproto, AppProto alproto, AppLayerGetFileState(*GetTxFiles)(void *, void *, uint8_t))
Definition: app-layer-parser.c:456
AppLayerParser::Truncate
void(* Truncate)(void *state, uint8_t direction)
Definition: app-layer-register.h:74
AppLayerParser::StateAlloc
void *(* StateAlloc)(void *, AppProto)
Definition: app-layer-register.h:40
AppLayerParser::ParseTS
AppLayerParserFPtr ParseTS
Definition: app-layer-register.h:43
AppLayerParser::StateFree
void(* StateFree)(void *)
Definition: app-layer-register.h:41
AppLayerParser::GetTxData
AppLayerTxData *(* GetTxData)(void *tx)
Definition: app-layer-register.h:69
AppLayerProtoDetectPPRegister
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
Definition: app-layer-detect-proto.c:1566
app-layer-detect-proto.h
AppLayerParser::StateGetProgress
int(* StateGetProgress)(void *alstate, uint8_t direction)
Definition: app-layer-register.h:52
AppLayerParser
Definition: app-layer-register.h:29
AppLayerParser::GetFrameNameById
AppLayerParserGetFrameNameByIdFn GetFrameNameById
Definition: app-layer-register.h:77
AppLayerParser::StateTransactionFree
void(* StateTransactionFree)(void *, uint64_t)
Definition: app-layer-register.h:48
AppLayerParser::min_depth
uint16_t min_depth
Definition: app-layer-register.h:37
StringToAppProto
AppProto StringToAppProto(const char *proto_name)
Maps a string to its ALPROTO_* equivalent.
Definition: app-layer-protos.c:94
AppLayerParserRegisterGetFrameFuncs
void AppLayerParserRegisterGetFrameFuncs(uint8_t ipproto, AppProto alproto, AppLayerParserGetFrameIdByNameFn GetIdByNameFunc, AppLayerParserGetFrameNameByIdFn GetNameByIdFunc)
Definition: app-layer-parser.c:573
AppLayerParser::ProbeTC
ProbingParserFPtr ProbeTC
Definition: app-layer-register.h:35
AppLayerParserRegisterStateFuncs
void AppLayerParserRegisterStateFuncs(uint8_t ipproto, AppProto alproto, void *(*StateAlloc)(void *, AppProto), void(*StateFree)(void *))
Definition: app-layer-parser.c:429
app-layer-parser.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
AppLayerParser::LocalStorageFree
void(* LocalStorageFree)(void *)
Definition: app-layer-register.h:60
stream.h
AppLayerParser::StateGetEventInfoById
int(* StateGetEventInfoById)(int event_id, const char **event_name, AppLayerEventType *event_type)
Definition: app-layer-register.h:56
conf.h
AppLayerParserRegisterGetEventInfo
void AppLayerParserRegisterGetEventInfo(uint8_t ipproto, AppProto alproto, int(*StateGetEventInfo)(const char *event_name, int *event_id, AppLayerEventType *event_type))
Definition: app-layer-parser.c:583
AppLayerProtoDetectRegisterProtocol
void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_name)
Registers a protocol for protocol detection phase.
Definition: app-layer-detect-proto.c:1783
RunmodeIsUnittests
int RunmodeIsUnittests(void)
Definition: suricata.c:252
AppLayerParserRegisterParser
int AppLayerParserRegisterParser(uint8_t ipproto, AppProto alproto, uint8_t direction, AppLayerParserFPtr Parser)
Register app layer parser for the protocol.
Definition: app-layer-parser.c:390
AppLayerParserRegisterGetTx
void AppLayerParserRegisterGetTx(uint8_t ipproto, AppProto alproto, void *(StateGetTx)(void *alstate, uint64_t tx_id))
Definition: app-layer-parser.c:527
suricata-common.h
AppLayerParser::name
const char * name
Definition: app-layer-register.h:30
AppLayerParserRegisterApplyTxConfigFunc
void AppLayerParserRegisterApplyTxConfigFunc(uint8_t ipproto, AppProto alproto, bool(*ApplyTxConfig)(void *state, void *tx, int mode, AppLayerTxConfig))
Definition: app-layer-parser.c:615
AppLayerParser::ParseTC
AppLayerParserFPtr ParseTC
Definition: app-layer-register.h:44
AppLayerParserRegisterStateDataFunc
void AppLayerParserRegisterStateDataFunc(uint8_t ipproto, AppProto alproto, AppLayerStateData *(*GetStateData)(void *state))
Definition: app-layer-parser.c:605
FatalError
#define FatalError(...)
Definition: util-debug.h:502
AppLayerParserRegisterTxDataFunc
void AppLayerParserRegisterTxDataFunc(uint8_t ipproto, AppProto alproto, AppLayerTxData *(*GetTxData)(void *tx))
Definition: app-layer-parser.c:595
AppLayerRegisterProtocolDetection
AppProto AppLayerRegisterProtocolDetection(const struct AppLayerParser *p, int enable_default)
App layer protocol detection function.
Definition: app-layer-register.c:38
AppLayerParserRegisterGetTxIterator
void AppLayerParserRegisterGetTxIterator(uint8_t ipproto, AppProto alproto, AppLayerGetTxIteratorFunc Func)
Definition: app-layer-parser.c:538
AppLayerParser::GetTxFiles
AppLayerGetFileState(* GetTxFiles)(void *, void *, uint8_t)
Definition: app-layer-register.h:62
AppLayerParserRegisterGetEventInfoById
void AppLayerParserRegisterGetEventInfoById(uint8_t ipproto, AppProto alproto, int(*StateGetEventInfoById)(int event_id, const char **event_name, AppLayerEventType *event_type))
Definition: app-layer-parser.c:561
AppLayerParser::StateGetTxCnt
uint64_t(* StateGetTxCnt)(void *alstate)
Definition: app-layer-register.h:46
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
ALPROTO_FAILED
@ ALPROTO_FAILED
Definition: app-layer-protos.h:70
AppLayerRegisterParser
int AppLayerRegisterParser(const struct AppLayerParser *p, AppProto alproto)
App layer protocol registration function.
Definition: app-layer-register.c:97
AppLayerProtoDetectRegisterAlias
void AppLayerProtoDetectRegisterAlias(const char *proto_name, const char *proto_alias)
Definition: app-layer-detect-proto.c:1793
suricata.h
AppLayerParser::complete_tc
const int complete_tc
Definition: app-layer-register.h:51
AppLayerParserRegisterGetTxCnt
void AppLayerParserRegisterGetTxCnt(uint8_t ipproto, AppProto alproto, uint64_t(*StateGetTxCnt)(void *alstate))
Definition: app-layer-parser.c:516
AppLayerParser::GetTxIterator
AppLayerGetTxIterTuple(* GetTxIterator)(const uint8_t ipproto, const AppProto alproto, void *alstate, uint64_t min_tx_id, uint64_t max_tx_id, AppLayerGetTxIterState *istate)
Definition: app-layer-register.h:64
AppLayerParser::ApplyTxConfig
bool(* ApplyTxConfig)(void *state, void *tx, int mode, AppLayerTxConfig)
Definition: app-layer-register.h:70
AppLayerRegisterParserAlias
int AppLayerRegisterParserAlias(const char *proto_name, const char *proto_alias)
Definition: app-layer-register.c:198
AppLayerParser::GetStateData
AppLayerStateData *(* GetStateData)(void *state)
Definition: app-layer-register.h:68
AppLayerParser::flags
uint32_t flags
Definition: app-layer-register.h:72
AppLayerParser::complete_ts
const int complete_ts
Definition: app-layer-register.h:50
AppLayerParser::GetFrameIdByName
AppLayerParserGetFrameIdByNameFn GetFrameIdByName
Definition: app-layer-register.h:76
AppLayerParser::StateGetTx
void *(* StateGetTx)(void *alstate, uint64_t tx_id)
Definition: app-layer-register.h:47