#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/queue.h>
#include <netinet/in.h>
#include <resolv.h>
#include "vmbuf.h"
#include "misc.h"
#include "plog.h"
#include "throttle.h"
#include "sockmisc.h"
#include "isakmp_var.h"
#include "isakmp.h"
#include "isakmp_xauth.h"
#include "isakmp_cfg.h"
#include "gcmalloc.h"
static struct throttle_list throttle_list =
TAILQ_HEAD_INITIALIZER(throttle_list);
struct throttle_entry *
throttle_add(struct sockaddr *addr)
{
struct throttle_entry *te;
struct timeval now, penalty;
size_t len;
len = sizeof(*te)
- sizeof(struct sockaddr_storage)
+ sysdep_sa_len(addr);
if ((te = racoon_malloc(len)) == NULL)
return NULL;
sched_get_monotonic_time(&now);
penalty.tv_sec = isakmp_cfg_config.auth_throttle;
penalty.tv_usec = 0;
timeradd(&now, &penalty, &te->penalty_ends);
memcpy(&te->host, addr, sysdep_sa_len(addr));
TAILQ_INSERT_HEAD(&throttle_list, te, next);
return te;
}
int
throttle_host(struct sockaddr *addr, int authfail)
{
struct throttle_entry *te;
struct timeval now, res;
int found = 0;
if (isakmp_cfg_config.auth_throttle == 0)
return 0;
sched_get_monotonic_time(&now);
restart:
RACOON_TAILQ_FOREACH_REVERSE(te, &throttle_list, throttle_list, next) {
if (timercmp(&te->penalty_ends, &now, <)) {
TAILQ_REMOVE(&throttle_list, te, next);
racoon_free(te);
goto restart;
}
if (cmpsaddr(addr, (struct sockaddr *) &te->host) <= CMPSADDR_WOP_MATCH) {
found = 1;
break;
}
}
if (!found) {
if (authfail) {
if ((te = throttle_add(addr)) == NULL) {
plog(LLV_ERROR, LOCATION, NULL,
"Throttle insertion failed\n");
return isakmp_cfg_config.auth_throttle;
}
}
return 0;
} else {
if (authfail) {
struct timeval remaining, penalty;
timersub(&te->penalty_ends, &now, &remaining);
penalty.tv_sec = isakmp_cfg_config.auth_throttle;
penalty.tv_usec = 0;
timeradd(&penalty, &remaining, &res);
if (res.tv_sec >= THROTTLE_PENALTY_MAX) {
res.tv_sec = THROTTLE_PENALTY_MAX;
res.tv_usec = 0;
}
timeradd(&now, &res, &te->penalty_ends);
}
}
timersub(&te->penalty_ends, &now, &res);
return res.tv_sec;
}