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,
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. */
118 
119  /* Register response parser for parsing frames from server to client. */
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  /* What is this being registered for? */
143 
144  if (p->StateGetEventInfo) {
146  p->StateGetEventInfo);
147  }
148  if (p->StateGetEventInfoById) {
151  }
152  if (p->StateGetEvents) {
154  p->StateGetEvents);
155  }
156  if (p->LocalStorageAlloc && p->LocalStorageFree) {
159  }
160  if (p->StateGetFiles) {
162  p->StateGetFiles);
163  }
164 
165  if (p->GetTxIterator) {
167  p->GetTxIterator);
168  }
169 
170  if (p->GetTxData) {
172  p->GetTxData);
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  return 0;
191 }
192 
193 int AppLayerRegisterParserAlias(const char *proto_name, const char *proto_alias)
194 {
195  AppLayerProtoDetectRegisterAlias(proto_name, proto_alias);
196 
197  return 0;
198 }
199 
200 static const char * IpProtoToString(int ip_proto)
201 {
202  switch (ip_proto) {
203  case IPPROTO_TCP:
204  return "tcp";
205  case IPPROTO_UDP:
206  return "udp";
207  default:
208  return NULL;
209  };
210 
211 }
AppLayerParser::StateGetEventInfo
int(* StateGetEventInfo)(const char *event_name, int *event_id, AppLayerEventType *event_type)
Definition: app-layer-register.h:56
AppLayerParserRegisterGetStateProgressFunc
void AppLayerParserRegisterGetStateProgressFunc(uint8_t ipproto, AppProto alproto, int(*StateGetProgress)(void *alstate, uint8_t direction))
Definition: app-layer-parser.c:474
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:1711
app-layer-register.h
AppLayerParserRegisterLocalStorageFunc
void AppLayerParserRegisterLocalStorageFunc(uint8_t ipproto, AppProto alproto, void *(*LocalStorageAlloc)(void), void(*LocalStorageFree)(void *))
Definition: app-layer-parser.c:410
AppLayerParserRegisterOptionFlags
void AppLayerParserRegisterOptionFlags(uint8_t ipproto, AppProto alproto, uint32_t flags)
Definition: app-layer-parser.c:381
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:77
AppLayerParserRegisterGetEventsFunc
void AppLayerParserRegisterGetEventsFunc(uint8_t ipproto, AppProto alproto, AppLayerDecoderEvents *(*StateGetEvents)(void *))
Definition: app-layer-parser.c:435
AppLayerParserRegisterTruncateFunc
void AppLayerParserRegisterTruncateFunc(uint8_t ipproto, AppProto alproto, void(*Truncate)(void *, uint8_t))
Definition: app-layer-parser.c:464
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:526
AppLayerParserRegisterTxFreeFunc
void AppLayerParserRegisterTxFreeFunc(uint8_t ipproto, AppProto alproto, void(*StateTransactionFree)(void *, uint64_t))
Definition: app-layer-parser.c:485
AppLayerParser::LocalStorageAlloc
void *(* LocalStorageAlloc)(void)
Definition: app-layer-register.h:61
AppLayerParserRegisterDetectStateFuncs
void AppLayerParserRegisterDetectStateFuncs(uint8_t ipproto, AppProto alproto, DetectEngineState *(*GetTxDetectState)(void *tx), int(*SetTxDetectState)(void *tx, DetectEngineState *))
Definition: app-layer-parser.c:565
AppLayerParser::Truncate
void(* Truncate)(void *state, uint8_t direction)
Definition: app-layer-register.h:75
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:70
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:1677
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::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
STREAM_TOSERVER
#define STREAM_TOSERVER
Definition: stream.h:31
StringToAppProto
AppProto StringToAppProto(const char *proto_name)
Maps a string to its ALPROTO_* equivalent.
Definition: app-layer-protos.c:140
AppLayerParserRegisterGetFilesFunc
void AppLayerParserRegisterGetFilesFunc(uint8_t ipproto, AppProto alproto, FileContainer *(*StateGetFiles)(void *, uint8_t))
Definition: app-layer-parser.c:424
AppLayerParser::ip_proto
int ip_proto
Definition: app-layer-register.h:30
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:397
app-layer-parser.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:277
AppLayerParser::LocalStorageFree
void(* LocalStorageFree)(void *)
Definition: app-layer-register.h:62
stream.h
AppLayerParser::StateGetEventInfoById
int(* StateGetEventInfoById)(int event_id, const char **event_name, AppLayerEventType *event_type)
Definition: app-layer-register.h:58
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:553
AppLayerProtoDetectRegisterProtocol
void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_name)
Registers a protocol for protocol detection phase.
Definition: app-layer-detect-proto.c:1889
RunmodeIsUnittests
int RunmodeIsUnittests(void)
Definition: suricata.c:267
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:358
AppLayerParserRegisterGetTx
void AppLayerParserRegisterGetTx(uint8_t ipproto, AppProto alproto, void *(StateGetTx)(void *alstate, uint64_t tx_id))
Definition: app-layer-parser.c:507
AppLayerParser::GetTxDetectState
DetectEngineState *(* GetTxDetectState)(void *tx)
Definition: app-layer-register.h:52
suricata-common.h
AppLayerParser::name
const char * name
Definition: app-layer-register.h:28
STREAM_TOCLIENT
#define STREAM_TOCLIENT
Definition: stream.h:32
AppLayerParser::StateGetEvents
AppLayerDecoderEvents *(* StateGetEvents)(void *)
Definition: app-layer-register.h:55
AppLayerParserRegisterApplyTxConfigFunc
void AppLayerParserRegisterApplyTxConfigFunc(uint8_t ipproto, AppProto alproto, bool(*ApplyTxConfig)(void *state, void *tx, int mode, AppLayerTxConfig))
Definition: app-layer-parser.c:587
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:577
AppLayerParser::StateGetFiles
FileContainer *(* StateGetFiles)(void *, uint8_t)
Definition: app-layer-register.h:64
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:518
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:541
AppLayerParser::StateGetTxCnt
uint64_t(* StateGetTxCnt)(void *alstate)
Definition: app-layer-register.h:44
AppLayerParser::SetTxDetectState
int(* SetTxDetectState)(void *tx, DetectEngineState *)
Definition: app-layer-register.h:53
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
ALPROTO_FAILED
@ ALPROTO_FAILED
Definition: app-layer-protos.h:67
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:1899
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:496
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:66
AppLayerParser::ApplyTxConfig
bool(* ApplyTxConfig)(void *state, void *tx, int mode, AppLayerTxConfig)
Definition: app-layer-register.h:71
AppLayerRegisterParserAlias
int AppLayerRegisterParserAlias(const char *proto_name, const char *proto_alias)
Definition: app-layer-register.c:193
AppLayerParser::flags
uint32_t flags
Definition: app-layer-register.h:73
AppLayerParser::complete_ts
const int complete_ts
Definition: app-layer-register.h:48
AppLayerParser::StateGetTx
void *(* StateGetTx)(void *alstate, uint64_t tx_id)
Definition: app-layer-register.h:45