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 "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 (p->ProbeTS == NULL && p->ProbeTC == NULL) {
58  BUG_ON(p->default_port != NULL);
59  return alproto;
60  }
61 
62  if (RunmodeIsUnittests()) {
63 
64  SCLogDebug("Unittest mode, registering default configuration.");
66  alproto, p->min_depth, p->max_depth, STREAM_TOSERVER,
67  p->ProbeTS, p->ProbeTC);
68 
69  }
70  else {
71 
72  if (!AppLayerProtoDetectPPParseConfPorts(ip_proto_str, p->ip_proto,
73  p->name, alproto, p->min_depth, p->max_depth,
74  p->ProbeTS, p->ProbeTC)) {
75  if (enable_default != 0) {
76  SCLogDebug("No %s app-layer configuration, enabling %s"
77  " detection %s detection on port %s.",
78  p->name, p->name, ip_proto_str, p->default_port);
80  p->default_port, alproto,
81  p->min_depth, p->max_depth, STREAM_TOSERVER,
82  p->ProbeTS, p->ProbeTC);
83  } else {
84  SCLogDebug("No %s app-layer configuration for detection port (%s).",
85  p->name, ip_proto_str);
86  }
87  }
88 
89  }
90 
91  return alproto;
92 }
93 
94 int AppLayerRegisterParser(const struct AppLayerParser *p, AppProto alproto)
95 {
96  const char *ip_proto_str = NULL;
97 
98  if (p == NULL)
99  FatalError(SC_ERR_FATAL, "Call to %s with NULL pointer.", __FUNCTION__);
100 
101  if (alproto == ALPROTO_UNKNOWN || alproto >= ALPROTO_FAILED)
102  FatalError(SC_ERR_FATAL, "Unknown or invalid AppProto '%s'.", p->name);
103 
104  ip_proto_str = IpProtoToString(p->ip_proto);
105  if (ip_proto_str == NULL)
106  FatalError(SC_ERR_FATAL, "Unknown or unsupported ip_proto field in parser '%s'", p->name);
107 
108  SCLogDebug("Registering %s protocol parser.", p->name);
109 
110  /* Register functions for state allocation and freeing. A
111  * state is allocated for every new flow. */
113  p->StateAlloc, p->StateFree);
114 
115  /* Register request parser for parsing frame from server to server. */
117  STREAM_TOSERVER, p->ParseTS);
118 
119  /* Register response parser for parsing frames from server to client. */
121  STREAM_TOCLIENT, p->ParseTC);
122 
123  /* Register a function to be called by the application layer
124  * when a transaction is to be freed. */
127 
128  /* Register a function to return the current transaction count. */
130  p->StateGetTxCnt);
131 
132  /* Transaction handling. */
134 
136  p->StateGetProgress);
138  p->StateGetTx);
139 
140  if (p->StateGetEventInfo) {
142  p->StateGetEventInfo);
143  }
144  if (p->StateGetEventInfoById) {
147  }
148  if (p->LocalStorageAlloc && p->LocalStorageFree) {
151  }
152  if (p->StateGetFiles) {
154  p->StateGetFiles);
155  }
156 
157  if (p->GetTxIterator) {
159  p->GetTxIterator);
160  }
161 
162  if (p->GetTxData) {
164  p->GetTxData);
165  }
166 
167  if (p->ApplyTxConfig) {
169  p->ApplyTxConfig);
170  }
171 
172  if (p->flags) {
174  p->flags);
175 
176  }
177 
178  if (p->Truncate) {
180  }
181 
182  if (p->GetFrameIdByName && p->GetFrameNameById) {
184  p->ip_proto, alproto, p->GetFrameIdByName, p->GetFrameNameById);
185  }
186 
187  return 0;
188 }
189 
190 int AppLayerRegisterParserAlias(const char *proto_name, const char *proto_alias)
191 {
192  AppLayerProtoDetectRegisterAlias(proto_name, proto_alias);
193 
194  return 0;
195 }
196 
197 static const char * IpProtoToString(int ip_proto)
198 {
199  switch (ip_proto) {
200  case IPPROTO_TCP:
201  return "tcp";
202  case IPPROTO_UDP:
203  return "udp";
204  default:
205  return NULL;
206  };
207 
208 }
AppLayerParser::ip_proto
uint8_t ip_proto
Definition: app-layer-register.h:30
AppLayerParser::StateGetEventInfo
int(* StateGetEventInfo)(const char *event_name, int *event_id, AppLayerEventType *event_type)
Definition: app-layer-register.h:52
AppLayerParserRegisterGetStateProgressFunc
void AppLayerParserRegisterGetStateProgressFunc(uint8_t ipproto, AppProto alproto, int(*StateGetProgress)(void *alstate, uint8_t direction))
Definition: app-layer-parser.c:503
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:1727
app-layer-register.h
AppLayerParserRegisterLocalStorageFunc
void AppLayerParserRegisterLocalStorageFunc(uint8_t ipproto, AppProto alproto, void *(*LocalStorageAlloc)(void), void(*LocalStorageFree)(void *))
Definition: app-layer-parser.c:450
AppLayerParserRegisterOptionFlags
void AppLayerParserRegisterOptionFlags(uint8_t ipproto, AppProto alproto, uint32_t flags)
Definition: app-layer-parser.c:421
AppLayerParser::default_port
const char * default_port
Definition: app-layer-register.h:29
AppLayerParser::max_depth
uint16_t max_depth
Definition: app-layer-register.h:36
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:80
AppLayerParserRegisterTruncateFunc
void AppLayerParserRegisterTruncateFunc(uint8_t ipproto, AppProto alproto, void(*Truncate)(void *, uint8_t))
Definition: app-layer-parser.c:493
AppLayerParser::ProbeTS
ProbingParserFPtr ProbeTS
Definition: app-layer-register.h:32
AppLayerParserRegisterStateProgressCompletionStatus
void AppLayerParserRegisterStateProgressCompletionStatus(AppProto alproto, const int ts, const int tc)
Definition: app-layer-parser.c:555
AppLayerParserRegisterTxFreeFunc
void AppLayerParserRegisterTxFreeFunc(uint8_t ipproto, AppProto alproto, void(*StateTransactionFree)(void *, uint64_t))
Definition: app-layer-parser.c:514
AppLayerParser::LocalStorageAlloc
void *(* LocalStorageAlloc)(void)
Definition: app-layer-register.h:57
AppLayerParser::Truncate
void(* Truncate)(void *state, uint8_t direction)
Definition: app-layer-register.h:71
AppLayerParser::StateAlloc
void *(* StateAlloc)(void *, AppProto)
Definition: app-layer-register.h:38
AppLayerParser::ParseTS
AppLayerParserFPtr ParseTS
Definition: app-layer-register.h:41
AppLayerParser::StateFree
void(* StateFree)(void *)
Definition: app-layer-register.h:39
AppLayerParser::GetTxData
AppLayerTxData *(* GetTxData)(void *tx)
Definition: app-layer-register.h:66
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:1693
app-layer-detect-proto.h
AppLayerParser::StateGetProgress
int(* StateGetProgress)(void *alstate, uint8_t direction)
Definition: app-layer-register.h:50
AppLayerParser
Definition: app-layer-register.h:27
AppLayerParser::GetFrameNameById
AppLayerParserGetFrameNameByIdFn GetFrameNameById
Definition: app-layer-register.h:74
AppLayerParser::StateTransactionFree
void(* StateTransactionFree)(void *, uint64_t)
Definition: app-layer-register.h:46
AppLayerParser::min_depth
uint16_t min_depth
Definition: app-layer-register.h:35
StringToAppProto
AppProto StringToAppProto(const char *proto_name)
Maps a string to its ALPROTO_* equivalent.
Definition: app-layer-protos.c:149
AppLayerParserRegisterGetFilesFunc
void AppLayerParserRegisterGetFilesFunc(uint8_t ipproto, AppProto alproto, FileContainer *(*StateGetFiles)(void *, uint8_t))
Definition: app-layer-parser.c:464
AppLayerParserRegisterGetFrameFuncs
void AppLayerParserRegisterGetFrameFuncs(uint8_t ipproto, AppProto alproto, AppLayerParserGetFrameIdByNameFn GetIdByNameFunc, AppLayerParserGetFrameNameByIdFn GetNameByIdFunc)
Definition: app-layer-parser.c:582
AppLayerParser::ProbeTC
ProbingParserFPtr ProbeTC
Definition: app-layer-register.h:33
AppLayerParserRegisterStateFuncs
void AppLayerParserRegisterStateFuncs(uint8_t ipproto, AppProto alproto, void *(*StateAlloc)(void *, AppProto), void(*StateFree)(void *))
Definition: app-layer-parser.c:437
app-layer-parser.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:281
AppLayerParser::LocalStorageFree
void(* LocalStorageFree)(void *)
Definition: app-layer-register.h:58
stream.h
AppLayerParser::StateGetEventInfoById
int(* StateGetEventInfoById)(int event_id, const char **event_name, AppLayerEventType *event_type)
Definition: app-layer-register.h:54
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:592
AppLayerProtoDetectRegisterProtocol
void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_name)
Registers a protocol for protocol detection phase.
Definition: app-layer-detect-proto.c:1905
RunmodeIsUnittests
int RunmodeIsUnittests(void)
Definition: suricata.c:271
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:398
AppLayerParserRegisterGetTx
void AppLayerParserRegisterGetTx(uint8_t ipproto, AppProto alproto, void *(StateGetTx)(void *alstate, uint64_t tx_id))
Definition: app-layer-parser.c:536
suricata-common.h
AppLayerParser::name
const char * name
Definition: app-layer-register.h:28
AppLayerParserRegisterApplyTxConfigFunc
void AppLayerParserRegisterApplyTxConfigFunc(uint8_t ipproto, AppProto alproto, bool(*ApplyTxConfig)(void *state, void *tx, int mode, AppLayerTxConfig))
Definition: app-layer-parser.c:614
AppLayerParser::ParseTC
AppLayerParserFPtr ParseTC
Definition: app-layer-register.h:42
FatalError
#define FatalError(x,...)
Definition: util-debug.h:532
AppLayerParserRegisterTxDataFunc
void AppLayerParserRegisterTxDataFunc(uint8_t ipproto, AppProto alproto, AppLayerTxData *(*GetTxData)(void *tx))
Definition: app-layer-parser.c:604
AppLayerParser::StateGetFiles
FileContainer *(* StateGetFiles)(void *, uint8_t)
Definition: app-layer-register.h:60
AppLayerRegisterProtocolDetection
AppProto AppLayerRegisterProtocolDetection(const struct AppLayerParser *p, int enable_default)
App layer protocol detection function.
Definition: app-layer-register.c:37
AppLayerParserRegisterGetTxIterator
void AppLayerParserRegisterGetTxIterator(uint8_t ipproto, AppProto alproto, AppLayerGetTxIteratorFunc Func)
Definition: app-layer-parser.c:547
SC_ERR_FATAL
@ SC_ERR_FATAL
Definition: util-error.h:203
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:570
AppLayerParser::StateGetTxCnt
uint64_t(* StateGetTxCnt)(void *alstate)
Definition: app-layer-register.h:44
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:94
AppLayerProtoDetectRegisterAlias
void AppLayerProtoDetectRegisterAlias(const char *proto_name, const char *proto_alias)
Definition: app-layer-detect-proto.c:1915
AppLayerParser::complete_tc
const int complete_tc
Definition: app-layer-register.h:49
AppLayerParserRegisterGetTxCnt
void AppLayerParserRegisterGetTxCnt(uint8_t ipproto, AppProto alproto, uint64_t(*StateGetTxCnt)(void *alstate))
Definition: app-layer-parser.c:525
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:62
AppLayerParser::ApplyTxConfig
bool(* ApplyTxConfig)(void *state, void *tx, int mode, AppLayerTxConfig)
Definition: app-layer-register.h:67
AppLayerRegisterParserAlias
int AppLayerRegisterParserAlias(const char *proto_name, const char *proto_alias)
Definition: app-layer-register.c:190
AppLayerParser::flags
uint32_t flags
Definition: app-layer-register.h:69
AppLayerParser::complete_ts
const int complete_ts
Definition: app-layer-register.h:48
AppLayerParser::GetFrameIdByName
AppLayerParserGetFrameIdByNameFn GetFrameIdByName
Definition: app-layer-register.h:73
AppLayerParser::StateGetTx
void *(* StateGetTx)(void *alstate, uint64_t tx_id)
Definition: app-layer-register.h:45