Go to the documentation of this file.
51 static uint32_t srep_version = 0;
53 static uint32_t SRepIncrVersion(
void)
55 return ++srep_version;
58 static uint32_t SRepGetVersion(
void)
68 static uint32_t SRepGetEffectiveVersion(
void)
73 static void SRepCIDRFreeUserData(
void *data)
79 static SCRadix4Config iprep_radix4_config = { SRepCIDRFreeUserData, NULL };
80 static SCRadix6Config iprep_radix6_config = { SRepCIDRFreeUserData, NULL };
82 static void SRepCIDRAddNetblock(
SRepCIDRTree *cidr_ctx,
char *ip,
int cat, uint8_t value)
86 FatalError(
"Error allocating memory. Exiting");
89 user_data->
version = SRepGetVersion();
90 user_data->
rep[cat] = value;
92 if (strchr(ip,
':') != NULL) {
95 &cidr_ctx->
srep_ipv6_tree[cat], &iprep_radix6_config, ip, (
void *)user_data)) {
104 &cidr_ctx->
srep_ipv4_tree[cat], &iprep_radix4_config, ip, (
void *)user_data)) {
112 static int8_t SRepCIDRGetIPv4IPRep(
SRepCIDRTree *cidr_ctx, uint8_t *ipv4_addr, uint8_t cat)
114 void *user_data = NULL;
116 if (user_data == NULL)
123 static int8_t SRepCIDRGetIPv6IPRep(
SRepCIDRTree *cidr_ctx, uint8_t *ipv6_addr, uint8_t cat)
125 void *user_data = NULL;
127 if (user_data == NULL)
140 else if (PacketIsIPv6(
p))
152 else if (PacketIsIPv6(
p))
163 SCLogDebug(
"effective Reputation version %u", SRepGetEffectiveVersion());
176 static void SRepInitComplete(
void)
179 SCLogDebug(
"effective Reputation version %u", SRepGetEffectiveVersion());
196 if (h->
iprep == NULL)
199 uint32_t eversion = SRepGetEffectiveVersion();
202 SCLogDebug(
"host %p has reputation version %u, "
203 "effective version is %u", h, r->
version, eversion);
211 static int SRepCatSplitLine(
char *line, uint8_t *cat,
char *shortname,
size_t shortname_len)
213 size_t line_len = strlen(line);
214 char *ptrs[2] = {NULL,NULL};
217 char *origline = line;
219 while (i < (
int)line_len) {
220 if (line[i] ==
',' || line[i] ==
'\n' || line[i] ==
'\0' || i == (
int)(line_len - 1)) {
229 if (line >= origline + line_len)
231 if (strlen(line) == 0)
251 strlcpy(shortname, ptrs[1], shortname_len);
260 static int SRepSplitLine(
SRepCIDRTree *cidr_ctx,
char *line,
Address *ip, uint8_t *cat, uint8_t *value)
262 size_t line_len = strlen(line);
263 char *ptrs[3] = {NULL,NULL,NULL};
266 char *origline = line;
268 while (i < (
int)line_len) {
269 if (line[i] ==
',' || line[i] ==
'\n' || line[i] ==
'\r' || line[i] ==
'\0' ||
270 i == (
int)(line_len - 1)) {
279 if (line >= origline + line_len)
281 if (strlen(line) == 0)
296 if (strcmp(ptrs[0],
"ip") == 0)
306 if (strchr(ptrs[0],
'/') != NULL) {
307 SRepCIDRAddNetblock(cidr_ctx, ptrs[0], c, v);
310 if (inet_pton(AF_INET, ptrs[0], &ip->
address) == 1) {
312 }
else if (inet_pton(AF_INET6, ptrs[0], &ip->
address) == 1) {
325 #define SREP_SHORTNAME_LEN 32
332 if (strcmp(srep_cat_table[cat], shortname) == 0)
339 static int SRepLoadCatFile(
const char *filename)
342 FILE *fp = fopen(filename,
"r");
345 SCLogError(
"opening ip rep file %s: %s", filename, strerror(errno));
356 static inline size_t GetEffectiveLineLen(
const char *line)
358 size_t len = strlen(line);
360 if (
len == 0 || line[0] ==
'\n' || line[0] ==
'\r' || line[0] ==
' ' || line[0] ==
'#' ||
369 char line[8192] =
"";
371 memset(&a, 0x00,
sizeof(a));
373 memset(&srep_cat_table, 0x00,
sizeof(srep_cat_table));
375 BUG_ON(SRepGetVersion() > 0);
377 while(fgets(line, (
int)
sizeof(line), fp) != NULL) {
378 size_t len = GetEffectiveLineLen(line);
383 if (line[
len - 1] ==
'\n' || line[
len - 1] ==
'\r') {
384 line[
len - 1] =
'\0';
389 if (SRepCatSplitLine(line, &cat, shortname,
sizeof(shortname)) == 0) {
399 if (strlen(srep_cat_table[i]) == 0)
401 SCLogDebug(
"CAT %d, name %s", i, srep_cat_table[i]);
406 static int SRepLoadFile(
SRepCIDRTree *cidr_ctx,
char *filename)
409 FILE *fp = fopen(filename,
"r");
412 SCLogError(
"opening ip rep file %s: %s", filename, strerror(errno));
425 char line[8192] =
"";
427 while(fgets(line, (
int)
sizeof(line), fp) != NULL) {
428 size_t len = GetEffectiveLineLen(line);
433 if (line[
len - 1] ==
'\n' || line[
len - 1] ==
'\r') {
434 line[
len - 1] =
'\0';
438 memset(&a, 0x00,
sizeof(a));
441 uint8_t cat = 0, value = 0;
442 int r = SRepSplitLine(cidr_ctx, line, &a, &cat, &value);
446 if (a.
family == AF_INET) {
458 SCLogError(
"failed to get a host, increase host.memcap");
463 if (h->
iprep == NULL) {
465 if (h->
iprep != NULL) {
469 if (h->
iprep != NULL) {
474 if (rep->
version != SRepGetVersion()) {
478 rep->
version = SRepGetVersion();
479 rep->
rep[cat] = value;
481 SCLogDebug(
"host %p iprep %p setting cat %u to value %u",
482 h, h->
iprep, cat, value);
487 if (rep->
rep[i] == 0)
490 SCLogDebug(
"--> host %p iprep %p cat %d to value %u",
510 static char *SRepCompleteFilePath(
char *file)
512 const char *defaultpath = NULL;
519 size_t path_len =
sizeof(char) * (strlen(defaultpath) +
524 strlcpy(path, defaultpath, path_len);
525 #if defined OS_WIN32 || defined __CYGWIN__
526 if (path[strlen(path) - 1] !=
'\\')
527 strlcat(path,
"\\\\", path_len);
529 if (path[strlen(path) - 1] !=
'/')
560 const char *filename = NULL;
574 if (SRepGetVersion() == 0) {
580 (void)
SCConfGet(
"reputation-categories-file", &filename);
582 if (filename == NULL && files == NULL) {
593 if (filename == NULL) {
594 SCLogError(
"\"reputation-categories-file\" not set");
600 if (SRepLoadCatFile(filename) < 0) {
602 "categories file %s",
614 char *sfile = SRepCompleteFilePath(file->
val);
616 SCLogInfo(
"Loading reputation file: %s", sfile);
618 int r = SRepLoadFile(cidr_ctx, sfile);
#define SREP_SHORTNAME_LEN
uint8_t rep[SREP_MAX_CATS]
uint8_t SCSRepCatGetByShortname(const char *shortname)
#define SC_ATOMIC_INIT(name)
wrapper for initializing an atomic variable.
void SRepFreeHostData(Host *h)
SCRadix6Node * SCRadix6TreeFindBestMatch(const SCRadix6Tree *tree, const uint8_t *key, void **user_data)
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
bool SCRadix4AddKeyIPV4String(SCRadix4Tree *tree, const SCRadix4Config *config, const char *str, void *user)
Adds a new IPV4/netblock to the Radix4 tree from a string.
struct HtpBodyChunk_ * next
SCRadix4Tree SCRadix4TreeInitialize(void)
SCRadix6Tree srep_ipv6_tree[SREP_MAX_CATS]
void HostRelease(Host *h)
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
main detection engine ctx
int SCConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Host * HostGetHostFromHash(Address *a)
SC_ATOMIC_DECLARE(uint32_t, srep_eversion)
#define TAILQ_FOREACH(var, head, field)
#define HostDecrUsecnt(h)
#define HostIncrUsecnt(h)
#define GET_IPV6_DST_ADDR(p)
void SCRadix4TreeRelease(SCRadix4Tree *tree, const SCRadix4Config *config)
size_t strlcpy(char *dst, const char *src, size_t siz)
union Address_::@29 address
#define GET_IPV4_DST_ADDR_PTR(p)
size_t strlcat(char *, const char *src, size_t siz)
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)
int SCConfGetNonNull(const char *name, const char **vptr)
Retrieve the non-null value of a configuration node.
const char * PrintInet(int af, const void *src, char *dst, socklen_t size)
#define SCLogWarning(...)
Macro used to log WARNING messages.
SCRadix4Tree srep_ipv4_tree[SREP_MAX_CATS]
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)
SCRadix4Node * SCRadix4TreeFindBestMatch(const SCRadix4Tree *tree, const uint8_t *key, void **user_data)
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
SCConfNode * SCConfGetNode(const char *name)
Get a SCConfNode by name.
#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)
bool SCRadix6AddKeyIPV6String(SCRadix6Tree *tree, const SCRadix6Config *config, const char *str, void *user)
Adds a new IPV6/netblock to the Radix6 tree from a string.
SCRadix6Tree SCRadix6TreeInitialize(void)
void SCRadix6TreeRelease(SCRadix6Tree *tree, const SCRadix6Config *config)
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)