Go to the documentation of this file.
50 static uint32_t srep_version = 0;
52 static uint32_t SRepIncrVersion(
void)
54 return ++srep_version;
57 static uint32_t SRepGetVersion(
void)
67 static uint32_t SRepGetEffectiveVersion(
void)
72 static void SRepCIDRFreeUserData(
void *data)
78 static void SRepCIDRAddNetblock(
SRepCIDRTree *cidr_ctx,
char *ip,
int cat, uint8_t value)
82 FatalError(
"Error allocating memory. Exiting");
85 user_data->
version = SRepGetVersion();
86 user_data->
rep[cat] = value;
88 if (strchr(ip,
':') != NULL) {
92 SCLogDebug(
"Error initializing Reputation IPV6 with CIDR module for cat %d", cat);
95 SCLogDebug(
"Reputation IPV6 with CIDR module for cat %d initialized", cat);
109 SCLogDebug(
"Error initializing Reputation IPV4 with CIDR module for cat %d", cat);
112 SCLogDebug(
"Reputation IPV4 with CIDR module for cat %d initialized", cat);
124 static int8_t SRepCIDRGetIPv4IPRep(
SRepCIDRTree *cidr_ctx, uint8_t *ipv4_addr, uint8_t cat)
126 void *user_data = NULL;
128 if (user_data == NULL)
135 static int8_t SRepCIDRGetIPv6IPRep(
SRepCIDRTree *cidr_ctx, uint8_t *ipv6_addr, uint8_t cat)
137 void *user_data = NULL;
139 if (user_data == NULL)
152 else if (PacketIsIPv6(p))
164 else if (PacketIsIPv6(p))
175 SCLogDebug(
"effective Reputation version %u", SRepGetEffectiveVersion());
188 static void SRepInitComplete(
void)
191 SCLogDebug(
"effective Reputation version %u", SRepGetEffectiveVersion());
208 if (h->
iprep == NULL)
211 uint32_t eversion = SRepGetEffectiveVersion();
214 SCLogDebug(
"host %p has reputation version %u, "
215 "effective version is %u", h, r->
version, eversion);
223 static int SRepCatSplitLine(
char *line, uint8_t *cat,
char *shortname,
size_t shortname_len)
225 size_t line_len = strlen(line);
226 char *ptrs[2] = {NULL,NULL};
229 char *origline = line;
231 while (i < (
int)line_len) {
232 if (line[i] ==
',' || line[i] ==
'\n' || line[i] ==
'\0' || i == (
int)(line_len - 1)) {
241 if (line >= origline + line_len)
243 if (strlen(line) == 0)
263 strlcpy(shortname, ptrs[1], shortname_len);
272 static int SRepSplitLine(
SRepCIDRTree *cidr_ctx,
char *line,
Address *ip, uint8_t *cat, uint8_t *value)
274 size_t line_len = strlen(line);
275 char *ptrs[3] = {NULL,NULL,NULL};
278 char *origline = line;
280 while (i < (
int)line_len) {
281 if (line[i] ==
',' || line[i] ==
'\n' || line[i] ==
'\r' || line[i] ==
'\0' ||
282 i == (
int)(line_len - 1)) {
291 if (line >= origline + line_len)
293 if (strlen(line) == 0)
308 if (strcmp(ptrs[0],
"ip") == 0)
318 if (strchr(ptrs[0],
'/') != NULL) {
319 SRepCIDRAddNetblock(cidr_ctx, ptrs[0], c, v);
322 if (inet_pton(AF_INET, ptrs[0], &ip->
address) == 1) {
324 }
else if (inet_pton(AF_INET6, ptrs[0], &ip->
address) == 1) {
337 #define SREP_SHORTNAME_LEN 32
344 if (strcmp(srep_cat_table[cat], shortname) == 0)
351 static int SRepLoadCatFile(
const char *filename)
354 FILE *fp = fopen(filename,
"r");
357 SCLogError(
"opening ip rep file %s: %s", filename, strerror(errno));
370 char line[8192] =
"";
372 memset(&a, 0x00,
sizeof(a));
374 memset(&srep_cat_table, 0x00,
sizeof(srep_cat_table));
376 BUG_ON(SRepGetVersion() > 0);
378 while(fgets(line, (
int)
sizeof(line), fp) != NULL) {
379 size_t len = strlen(line);
384 if (line[0] ==
'\n' || line [0] ==
'\r' || line[0] ==
' ' || line[0] ==
'#' || line[0] ==
'\t')
387 while (isspace((
unsigned char)line[--
len]));
394 if (line[
len - 1] ==
'\n' || line[
len - 1] ==
'\r') {
395 line[
len - 1] =
'\0';
400 if (SRepCatSplitLine(line, &cat, shortname,
sizeof(shortname)) == 0) {
410 if (strlen(srep_cat_table[i]) == 0)
412 SCLogDebug(
"CAT %d, name %s", i, srep_cat_table[i]);
417 static int SRepLoadFile(
SRepCIDRTree *cidr_ctx,
char *filename)
420 FILE *fp = fopen(filename,
"r");
423 SCLogError(
"opening ip rep file %s: %s", filename, strerror(errno));
436 char line[8192] =
"";
438 while(fgets(line, (
int)
sizeof(line), fp) != NULL) {
439 size_t len = strlen(line);
444 if (line[0] ==
'\n' || line [0] ==
'\r' || line[0] ==
' ' || line[0] ==
'#' || line[0] ==
'\t')
447 while (isspace((
unsigned char)line[--
len]));
454 if (line[
len - 1] ==
'\n' || line[
len - 1] ==
'\r') {
455 line[
len - 1] =
'\0';
459 memset(&a, 0x00,
sizeof(a));
462 uint8_t cat = 0, value = 0;
463 int r = SRepSplitLine(cidr_ctx, line, &a, &cat, &value);
467 if (a.
family == AF_INET) {
479 SCLogError(
"failed to get a host, increase host.memcap");
484 if (h->
iprep == NULL) {
486 if (h->
iprep != NULL) {
490 if (h->
iprep != NULL) {
495 if (rep->
version != SRepGetVersion()) {
499 rep->
version = SRepGetVersion();
500 rep->
rep[cat] = value;
502 SCLogDebug(
"host %p iprep %p setting cat %u to value %u",
503 h, h->
iprep, cat, value);
508 if (rep->
rep[i] == 0)
511 SCLogDebug(
"--> host %p iprep %p cat %d to value %u",
531 static char *SRepCompleteFilePath(
char *file)
533 const char *defaultpath = NULL;
538 if (
ConfGet(
"default-reputation-path", &defaultpath) == 1) {
540 size_t path_len =
sizeof(char) * (strlen(defaultpath) +
545 strlcpy(path, defaultpath, path_len);
546 #if defined OS_WIN32 || defined __CYGWIN__
547 if (path[strlen(path) - 1] !=
'\\')
548 strlcat(path,
"\\\\", path_len);
550 if (path[strlen(path) - 1] !=
'/')
581 const char *filename = NULL;
589 if (SRepGetVersion() == 0) {
595 (void)
ConfGet(
"reputation-categories-file", &filename);
597 if (filename == NULL && files == NULL) {
608 if (filename == NULL) {
609 SCLogError(
"\"reputation-categories-file\" not set");
615 if (SRepLoadCatFile(filename) < 0) {
617 "categories file %s",
629 char *sfile = SRepCompleteFilePath(file->
val);
631 SCLogInfo(
"Loading reputation file: %s", sfile);
633 int r = SRepLoadFile(cidr_ctx, sfile);
#define SREP_SHORTNAME_LEN
bool SCRadixAddKeyIPV6String(const char *str, SCRadixTree *tree, void *user)
Adds a new IPV6/netblock to the Radix tree from a string.
uint8_t rep[SREP_MAX_CATS]
#define SC_ATOMIC_INIT(name)
wrapper for initializing an atomic variable.
void SRepFreeHostData(Host *h)
uint8_t SRepCatGetByShortname(char *shortname)
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
struct HtpBodyChunk_ * next
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
union Address_::@26 address
void HostRelease(Host *h)
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
main detection engine ctx
Host * HostGetHostFromHash(Address *a)
SC_ATOMIC_DECLARE(uint32_t, srep_eversion)
#define TAILQ_FOREACH(var, head, field)
#define HostDecrUsecnt(h)
SCRadixNode * SCRadixFindKeyIPV4BestMatch(uint8_t *key_stream, SCRadixTree *tree, void **user_data_result)
Checks if an IPV4 address is present in the tree under a netblock.
SCRadixNode * SCRadixFindKeyIPV6BestMatch(uint8_t *key_stream, SCRadixTree *tree, void **user_data_result)
Checks if an IPV6 address is present in the tree under a netblock.
#define HostIncrUsecnt(h)
#define GET_IPV6_DST_ADDR(p)
bool SCRadixAddKeyIPV4String(const char *str, SCRadixTree *tree, void *user)
Adds a new IPV4/netblock to the Radix tree from a string.
size_t strlcpy(char *dst, const char *src, size_t siz)
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
#define GET_IPV4_DST_ADDR_PTR(p)
size_t strlcat(char *, const char *src, size_t siz)
void SCRadixReleaseRadixTree(SCRadixTree *tree)
Frees a Radix tree and all its nodes.
SRepCIDRTree * srepCIDR_ctx
int StringParseI32RangeCheck(int32_t *res, int base, size_t len, const char *str, int32_t min, int32_t max)
int SRepLoadCatFileFromFD(FILE *fp)
const char * PrintInet(int af, const void *src, char *dst, socklen_t size)
#define SCLogWarning(...)
Macro used to log WARNING messages.
SCRadixTree * SCRadixCreateRadixTree(void(*Free)(void *), void(*PrintData)(void *))
Creates a new Radix tree.
int SRepLoadFileFromFD(SRepCIDRTree *cidr_ctx, FILE *fp)
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
int StringParseU8RangeCheck(uint8_t *res, int base, size_t len, const char *str, uint8_t min, uint8_t max)
void SRepReloadComplete(void)
Increment effective reputation version after a rule/reputation reload is complete.
int SRepHostTimedOut(Host *h)
Check if a Host is timed out wrt ip rep, meaning a new version is in place.
int8_t SRepCIDRGetIPRepSrc(SRepCIDRTree *cidr_ctx, Packet *p, uint8_t cat, uint32_t version)
#define GET_IPV4_SRC_ADDR_PTR(p)
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
#define SCLogError(...)
Macro used to log ERROR messages.
#define GET_IPV6_SRC_ADDR(p)
int SRepInit(DetectEngineCtx *de_ctx)
init reputation
int8_t SRepCIDRGetIPRepDst(SRepCIDRTree *cidr_ctx, Packet *p, uint8_t cat, uint32_t version)
int PathIsRelative(const char *path)
Check if a path is relative.
void HostPrintStats(void)
print some host stats
thread_local SCError sc_errno
void SRepResetVersion(void)
SCRadixTree * srepIPV4_tree[SREP_MAX_CATS]
void SRepDestroy(DetectEngineCtx *de_ctx)
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
int SCLogDebugEnabled(void)
Returns whether debug messages are enabled to be logged or not.
#define DEBUG_VALIDATE_BUG_ON(exp)
SCRadixTree * srepIPV6_tree[SREP_MAX_CATS]