1 /* $NetBSD: throttle.c,v 1.4 2006/09/09 16:22:10 manu Exp $ */
3 /* Id: throttle.c,v 1.5 2006/04/05 20:54:50 manubsd Exp */
6 * Copyright (C) 2004 Emmanuel Dreyfus
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #if TIME_WITH_SYS_TIME
40 # include <sys/time.h>
44 # include <sys/time.h>
49 #include <sys/param.h>
50 #include <sys/queue.h>
51 #include <sys/socket.h>
53 #include <netinet/in.h>
62 #include "isakmp_var.h"
64 #include "isakmp_xauth.h"
65 #include "isakmp_cfg.h"
68 struct throttle_list throttle_list
= TAILQ_HEAD_INITIALIZER(throttle_list
);
71 struct throttle_entry
*
73 struct sockaddr
*addr
;
75 struct throttle_entry
*te
;
79 - sizeof(struct sockaddr_storage
)
80 + sysdep_sa_len(addr
);
82 if ((te
= racoon_malloc(len
)) == NULL
)
85 te
->penalty
= time(NULL
) + isakmp_cfg_config
.auth_throttle
;
86 memcpy(&te
->host
, addr
, sysdep_sa_len(addr
));
87 TAILQ_INSERT_HEAD(&throttle_list
, te
, next
);
93 throttle_host(addr
, authfail
)
94 struct sockaddr
*addr
;
97 struct throttle_entry
*te
;
101 if (isakmp_cfg_config
.auth_throttle
== 0)
107 TAILQ_FOREACH_REVERSE(te
, &throttle_list
, throttle_list
, next
) {
109 * Remove outdated entries
111 if (te
->penalty
< now
) {
112 TAILQ_REMOVE(&throttle_list
, te
, next
);
117 if (cmpsaddrwop(addr
, (struct sockaddr
*)&te
->host
) == 0) {
124 * No match, if auth failed, allocate a new throttle entry
125 * give no penalty even on error: this is the first time
126 * and we are indulgent.
130 if ((te
= throttle_add(addr
)) == NULL
) {
131 plog(LLV_ERROR
, LOCATION
, NULL
,
132 "Throttle insertion failed\n");
134 + isakmp_cfg_config
.auth_throttle
);
140 * We had a match and auth failed, increase penalty.
146 remaining
= te
->penalty
- now
;
147 new = remaining
+ isakmp_cfg_config
.auth_throttle
;
149 if (new > THROTTLE_PENALTY_MAX
)
150 new = THROTTLE_PENALTY_MAX
;
152 te
->penalty
= now
+ new;