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 #include "rust.h"
31 
32 #include "app-layer-detect-proto.h"
33 #include "app-layer-parser.h"
34 
35 #include "app-layer-register.h"
36 
37 static const char * IpProtoToString(int ip_proto);
38 
40  const struct AppLayerProtocolDetect *p, int enable_default)
41 {
42  AppProto alproto;
43  const char *ip_proto_str = NULL;
44 
45  if (p == NULL)
46  FatalError("Call to %s with NULL pointer.", __FUNCTION__);
47 
48  alproto = StringToAppProto(p->name);
49  if (alproto == ALPROTO_UNKNOWN || alproto == ALPROTO_FAILED)
50  FatalError("Unknown or invalid AppProto '%s'.", p->name);
51 
52  BUG_ON(strcmp(p->name, AppProtoToString(alproto)) != 0);
53 
54  ip_proto_str = IpProtoToString(p->ip_proto);
55  if (ip_proto_str == NULL)
56  FatalError("Unknown or unsupported ip_proto field in parser '%s'", p->name);
57 
58  SCLogDebug("%s %s protocol detection enabled.", ip_proto_str, p->name);
59 
60  AppLayerProtoDetectRegisterProtocol(alproto, p->name);
61 
62  if (p->ProbeTS == NULL && p->ProbeTC == NULL) {
63  BUG_ON(p->default_port != NULL);
64  return alproto;
65  }
66 
67  if (RunmodeIsUnittests()) {
68 
69  SCLogDebug("Unittest mode, registering default configuration.");
70  SCAppLayerProtoDetectPPRegister(p->ip_proto, p->default_port, alproto, p->min_depth,
71  p->max_depth, STREAM_TOSERVER, p->ProbeTS, p->ProbeTC);
72 
73  }
74  else {
75 
76  if (!SCAppLayerProtoDetectPPParseConfPorts(ip_proto_str, p->ip_proto, p->name, alproto,
77  p->min_depth, p->max_depth, 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);
82  SCAppLayerProtoDetectPPRegister(p->ip_proto, p->default_port, alproto, p->min_depth,
83  p->max_depth, STREAM_TOSERVER, p->ProbeTS, p->ProbeTC);
84  } else {
85  SCLogDebug("No %s app-layer configuration for detection port (%s).",
86  p->name, ip_proto_str);
87  }
88  }
89  }
90 
91  return alproto;
92 }
93 
95 {
96  const char *ip_proto_str = NULL;
97 
98  if (p == NULL)
99  FatalError("Call to %s with NULL pointer.", __FUNCTION__);
100 
101  if (!AppProtoIsValid(alproto))
102  FatalError("Unknown or invalid AppProto '%s'.", p->name);
103 
104  BUG_ON(strcmp(p->name, AppProtoToString(alproto)) != 0);
105 
106  ip_proto_str = IpProtoToString(p->ip_proto);
107  if (ip_proto_str == NULL)
108  FatalError("Unknown or unsupported ip_proto field in parser '%s'", p->name);
109 
110  SCLogDebug("Registering %s protocol parser.", p->name);
111 
112  /* Register functions for state allocation and freeing. A
113  * state is allocated for every new flow. */
114  AppLayerParserRegisterStateFuncs(p->ip_proto, alproto,
115  p->StateAlloc, p->StateFree);
116 
117  /* Register request parser for parsing frame from server to server. */
118  AppLayerParserRegisterParser(p->ip_proto, alproto,
119  STREAM_TOSERVER, p->ParseTS);
120 
121  /* Register response parser for parsing frames from server to client. */
122  AppLayerParserRegisterParser(p->ip_proto, alproto,
123  STREAM_TOCLIENT, p->ParseTC);
124 
125  /* Register a function to be called by the application layer
126  * when a transaction is to be freed. */
127  AppLayerParserRegisterTxFreeFunc(p->ip_proto, alproto,
128  p->StateTransactionFree);
129 
130  /* Register a function to return the current transaction count. */
131  AppLayerParserRegisterGetTxCnt(p->ip_proto, alproto,
132  p->StateGetTxCnt);
133 
134  /* Transaction handling. */
135  AppLayerParserRegisterStateProgressCompletionStatus(alproto, p->complete_ts, p->complete_tc);
136 
138  p->StateGetProgress);
139  AppLayerParserRegisterGetTx(p->ip_proto, alproto,
140  p->StateGetTx);
141 
142  if (p->StateGetEventInfo) {
143  AppLayerParserRegisterGetEventInfo(p->ip_proto, alproto,
144  p->StateGetEventInfo);
145  }
146  if (p->StateGetEventInfoById) {
147  AppLayerParserRegisterGetEventInfoById(p->ip_proto, alproto,
148  p->StateGetEventInfoById);
149  }
150  if (p->LocalStorageAlloc && p->LocalStorageFree) {
151  AppLayerParserRegisterLocalStorageFunc(p->ip_proto, alproto,
152  p->LocalStorageAlloc, p->LocalStorageFree);
153  }
154  if (p->GetTxFiles) {
155  AppLayerParserRegisterGetTxFilesFunc(p->ip_proto, alproto, p->GetTxFiles);
156  }
157 
158  if (p->GetTxIterator) {
159  AppLayerParserRegisterGetTxIterator(p->ip_proto, alproto,
160  p->GetTxIterator);
161  }
162 
163  if (p->GetTxData) {
164  AppLayerParserRegisterTxDataFunc(p->ip_proto, alproto,
165  p->GetTxData);
166  }
167 
168  if (p->GetStateData) {
169  AppLayerParserRegisterStateDataFunc(p->ip_proto, alproto, p->GetStateData);
170  }
171 
172  if (p->ApplyTxConfig) {
173  AppLayerParserRegisterApplyTxConfigFunc(p->ip_proto, alproto,
174  p->ApplyTxConfig);
175  }
176 
177  if (p->flags) {
178  AppLayerParserRegisterOptionFlags(p->ip_proto, alproto,
179  p->flags);
180 
181  }
182 
183  if (p->GetFrameIdByName && p->GetFrameNameById) {
185  p->ip_proto, alproto, p->GetFrameIdByName, p->GetFrameNameById);
186  }
187 
188  if (p->GetStateIdByName && p->GetStateNameById) {
190  p->ip_proto, alproto, p->GetStateIdByName, p->GetStateNameById);
191  }
192 
193  return 0;
194 }
195 
196 int SCAppLayerRegisterParserAlias(const char *proto_name, const char *proto_alias)
197 {
198  AppLayerProtoDetectRegisterAlias(proto_name, proto_alias);
199 
200  return 0;
201 }
202 
203 static const char * IpProtoToString(int ip_proto)
204 {
205  switch (ip_proto) {
206  case IPPROTO_TCP:
207  return "tcp";
208  case IPPROTO_UDP:
209  return "udp";
210  default:
211  return NULL;
212  };
213 
214 }
AppLayerParserRegisterGetStateProgressFunc
void AppLayerParserRegisterGetStateProgressFunc(uint8_t ipproto, AppProto alproto, int(*StateGetProgress)(void *alstate, uint8_t direction))
Definition: app-layer-parser.c:504
app-layer-register.h
AppLayerParserRegisterLocalStorageFunc
void AppLayerParserRegisterLocalStorageFunc(uint8_t ipproto, AppProto alproto, void *(*LocalStorageAlloc)(void), void(*LocalStorageFree)(void *))
Definition: app-layer-parser.c:464
AppLayerParserRegisterOptionFlags
void AppLayerParserRegisterOptionFlags(uint8_t ipproto, AppProto alproto, uint32_t flags)
Definition: app-layer-parser.c:443
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:87
Packet_::flags
uint32_t flags
Definition: decode.h:560
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:41
AppLayerParserRegisterStateProgressCompletionStatus
void AppLayerParserRegisterStateProgressCompletionStatus(AppProto alproto, const int ts, const int tc)
Definition: app-layer-parser.c:552
AppLayerParserRegisterTxFreeFunc
void AppLayerParserRegisterTxFreeFunc(uint8_t ipproto, AppProto alproto, void(*StateTransactionFree)(void *, uint64_t))
Definition: app-layer-parser.c:514
rust.h
AppLayerParserRegisterGetStateFuncs
void AppLayerParserRegisterGetStateFuncs(uint8_t ipproto, AppProto alproto, AppLayerParserGetStateIdByNameFn GetIdByNameFunc, AppLayerParserGetStateNameByIdFn GetNameByIdFunc)
Definition: app-layer-parser.c:579
p
Packet * p
Definition: fuzz_iprep.c:21
AppLayerParserRegisterApplyTxConfigFunc
void AppLayerParserRegisterApplyTxConfigFunc(uint8_t ipproto, AppProto alproto, void(*ApplyTxConfig)(void *state, void *tx, int mode, AppLayerTxConfig))
Definition: app-layer-parser.c:630
SCAppLayerRegisterProtocolDetection
AppProto SCAppLayerRegisterProtocolDetection(const struct AppLayerProtocolDetect *p, int enable_default)
App layer protocol detection function.
Definition: app-layer-register.c:39
SCAppLayerRegisterParser
int SCAppLayerRegisterParser(const struct AppLayerParser *p, AppProto alproto)
App layer protocol registration function.
Definition: app-layer-register.c:94
app-layer-detect-proto.h
AppLayerParser
Definition: app-layer-register.h:29
StringToAppProto
AppProto StringToAppProto(const char *proto_name)
Maps a string to its ALPROTO_* equivalent.
Definition: app-layer-protos.c:61
AppLayerParserRegisterGetFrameFuncs
void AppLayerParserRegisterGetFrameFuncs(uint8_t ipproto, AppProto alproto, AppLayerParserGetFrameIdByNameFn GetIdByNameFunc, AppLayerParserGetFrameNameByIdFn GetNameByIdFunc)
Definition: app-layer-parser.c:589
AppLayerParserRegisterStateFuncs
void AppLayerParserRegisterStateFuncs(uint8_t ipproto, AppProto alproto, void *(*StateAlloc)(void *, AppProto), void(*StateFree)(void *))
Definition: app-layer-parser.c:453
app-layer-parser.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:325
AppLayerParserRegisterGetEventInfo
void AppLayerParserRegisterGetEventInfo(uint8_t ipproto, AppProto alproto, int(*StateGetEventInfo)(const char *event_name, uint8_t *event_id, AppLayerEventType *event_type))
Definition: app-layer-parser.c:599
stream.h
conf.h
AppLayerParserRegisterGetTxFilesFunc
void AppLayerParserRegisterGetTxFilesFunc(uint8_t ipproto, AppProto alproto, AppLayerGetFileState(*GetTxFiles)(void *, uint8_t))
Definition: app-layer-parser.c:476
AppLayerProtoDetectRegisterProtocol
void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_name)
Registers a protocol for protocol detection phase.
Definition: app-layer-detect-proto.c:1782
RunmodeIsUnittests
int RunmodeIsUnittests(void)
Definition: suricata.c:292
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:420
SCAppLayerProtoDetectPPRegister
void SCAppLayerProtoDetectPPRegister(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:1541
AppLayerParserRegisterGetTx
void AppLayerParserRegisterGetTx(uint8_t ipproto, AppProto alproto, void *(StateGetTx)(void *alstate, uint64_t tx_id))
Definition: app-layer-parser.c:534
suricata-common.h
AppLayerParserRegisterStateDataFunc
void AppLayerParserRegisterStateDataFunc(uint8_t ipproto, AppProto alproto, AppLayerStateData *(*GetStateData)(void *state))
Definition: app-layer-parser.c:620
SCAppLayerRegisterParserAlias
int SCAppLayerRegisterParserAlias(const char *proto_name, const char *proto_alias)
Definition: app-layer-register.c:196
FatalError
#define FatalError(...)
Definition: util-debug.h:517
AppLayerParserRegisterTxDataFunc
void AppLayerParserRegisterTxDataFunc(uint8_t ipproto, AppProto alproto, AppLayerTxData *(*GetTxData)(void *tx))
Definition: app-layer-parser.c:610
AppLayerProtocolDetect
First part of AppLayerParser, needed only for protocol detection.
Definition: app-layer-register.h:82
AppLayerParserRegisterGetTxIterator
void AppLayerParserRegisterGetTxIterator(uint8_t ipproto, AppProto alproto, AppLayerGetTxIteratorFunc Func)
Definition: app-layer-parser.c:544
SCAppLayerProtoDetectPPParseConfPorts
int SCAppLayerProtoDetectPPParseConfPorts(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:1577
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
ALPROTO_FAILED
@ ALPROTO_FAILED
Definition: app-layer-protos.h:33
AppLayerProtoDetectRegisterAlias
void AppLayerProtoDetectRegisterAlias(const char *proto_name, const char *proto_alias)
Definition: app-layer-detect-proto.c:1802
suricata.h
AppLayerParserRegisterGetTxCnt
void AppLayerParserRegisterGetTxCnt(uint8_t ipproto, AppProto alproto, uint64_t(*StateGetTxCnt)(void *alstate))
Definition: app-layer-parser.c:524
AppLayerParserRegisterGetEventInfoById
void AppLayerParserRegisterGetEventInfoById(uint8_t ipproto, AppProto alproto, int(*StateGetEventInfoById)(uint8_t event_id, const char **event_name, AppLayerEventType *event_type))
Definition: app-layer-parser.c:567