2 * IRC - Internet Relay Chat, ircd/IPcheck.c
3 * Copyright (C) 1998 Carlo Wood ( Run @ undernet.org )
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * This file should be edited in a window with a width of 141 characters
31 #include "numnicks.h" /* NumNick, NumServ (GODMODE) */
32 #include "ircd_alloc.h"
33 #include "ircd_events.h"
34 #include "ircd_features.h"
35 #include "s_debug.h" /* Debug */
36 #include "s_user.h" /* TARGET_DELAY */
42 struct IPTargetEntry {
44 unsigned char targets[MAXTARGETS];
47 struct IPRegistryEntry {
48 struct IPRegistryEntry* next;
49 struct IPTargetEntry* target;
52 unsigned short connected;
53 unsigned char attempts;
57 * Hash table for IPv4 address registry
59 * Hash table size must be a power of 2
60 * Use 64K hash table to conserve memory
62 #define IP_REGISTRY_TABLE_SIZE 0x10000
63 #define MASK_16 0xffff
65 #define NOW ((unsigned short)(CurrentTime & MASK_16))
66 #define CONNECTED_SINCE(x) (NOW - (x))
68 #define IPCHECK_CLONE_LIMIT feature_int(FEAT_IPCHECK_CLONE_LIMIT)
69 #define IPCHECK_CLONE_PERIOD feature_int(FEAT_IPCHECK_CLONE_PERIOD)
70 #define IPCHECK_CLONE_DELAY feature_int(FEAT_IPCHECK_CLONE_DELAY)
73 static struct IPRegistryEntry* hashTable[IP_REGISTRY_TABLE_SIZE];
74 static struct IPRegistryEntry* freeList = 0;
76 static struct Timer expireTimer;
78 static unsigned int ip_registry_hash(unsigned int ip)
80 return ((ip >> 16) ^ ip) & (IP_REGISTRY_TABLE_SIZE - 1);
83 static struct IPRegistryEntry* ip_registry_find(unsigned int ip)
85 struct IPRegistryEntry* entry = hashTable[ip_registry_hash(ip)];
86 for ( ; entry; entry = entry->next) {
87 if (entry->addr == ip)
93 static void ip_registry_add(struct IPRegistryEntry* entry)
95 unsigned int bucket = ip_registry_hash(entry->addr);
96 entry->next = hashTable[bucket];
97 hashTable[bucket] = entry;
100 static void ip_registry_remove(struct IPRegistryEntry* entry)
102 unsigned int bucket = ip_registry_hash(entry->addr);
103 if (hashTable[bucket] == entry)
104 hashTable[bucket] = entry->next;
106 struct IPRegistryEntry* prev = hashTable[bucket];
107 for ( ; prev; prev = prev->next) {
108 if (prev->next == entry) {
109 prev->next = entry->next;
116 static struct IPRegistryEntry* ip_registry_new_entry()
118 struct IPRegistryEntry* entry = freeList;
120 freeList = entry->next;
122 entry = (struct IPRegistryEntry*) MyMalloc(sizeof(struct IPRegistryEntry));
125 memset(entry, 0, sizeof(struct IPRegistryEntry));
126 entry->last_connect = NOW; /* Seconds since last connect attempt */
127 entry->connected = 1; /* connected clients for this IP */
128 entry->attempts = 1; /* Number attempts for this IP */
132 static void ip_registry_delete_entry(struct IPRegistryEntry* entry)
135 MyFree(entry->target);
136 entry->next = freeList;
140 static unsigned int ip_registry_update_free_targets(struct IPRegistryEntry* entry)
142 unsigned int free_targets = STARTTARGETS;
145 free_targets = entry->target->count + (CONNECTED_SINCE(entry->last_connect) / TARGET_DELAY);
146 if (free_targets > STARTTARGETS)
147 free_targets = STARTTARGETS;
148 entry->target->count = free_targets;
153 static void ip_registry_expire_entry(struct IPRegistryEntry* entry)
156 * Don't touch this number, it has statistical significance
157 * XXX - blah blah blah
159 if (CONNECTED_SINCE(entry->last_connect) > 600) {
163 ip_registry_remove(entry);
164 ip_registry_delete_entry(entry);
166 else if (CONNECTED_SINCE(entry->last_connect) > 120 && 0 != entry->target) {
168 * Expire storage of targets
170 MyFree(entry->target);
175 /* Callback to run an expiry of the IPcheck registry */
176 static void ip_registry_expire(struct Event* ev)
179 struct IPRegistryEntry* entry;
180 struct IPRegistryEntry* entry_next;
182 assert(ET_EXPIRE == ev_type(ev));
183 assert(0 != ev_timer(ev));
185 for (i = 0; i < IP_REGISTRY_TABLE_SIZE; ++i) {
186 for (entry = hashTable[i]; entry; entry = entry_next) {
187 entry_next = entry->next;
188 if (0 == entry->connected)
189 ip_registry_expire_entry(entry);
197 * Initializes the registry timer
199 void IPcheck_init(void)
201 timer_add(timer_init(&expireTimer), ip_registry_expire, 0, TT_PERIODIC, 60);
205 * IPcheck_local_connect
208 * A new connection was accept()-ed with IP number `cptr->ip.s_addr'.
211 * Update the IPcheck registry.
213 * 1 : You're allowed to connect.
214 * 0 : You're not allowed to connect.
218 * A connection should be rejected when a connection from the same IP number was
219 * received IPCHECK_CLONE_LIMIT times before this connect attempt, with
220 * reconnect intervals of IPCHECK_CLONE_PERIOD seconds or less.
222 * Free target inheritance:
224 * When the client is accepted, then the number of Free Targets
225 * of the cptr is set to the value stored in the found IPregistry
226 * structure, or left at STARTTARGETS. This can be done by changing
227 * cptr->nexttarget to be `now - (TARGET_DELAY * (FREE_TARGETS - 1))',
228 * where FREE_TARGETS may range from 0 till STARTTARGETS.
230 int ip_registry_check_local(unsigned int addr, time_t* next_target_out)
232 struct IPRegistryEntry* entry = ip_registry_find(addr);
233 unsigned int free_targets = STARTTARGETS;
236 entry = ip_registry_new_entry();
237 entry->addr = addr; /* The IP number of registry entry */
238 ip_registry_add(entry);
241 /* Note that this also connects server connects.
242 * It is hard and not interesting, to change that.
244 * Don't allow more then 255 connects from one IP number, ever
246 if (0 == ++entry->connected)
249 if (CONNECTED_SINCE(entry->last_connect) > IPCHECK_CLONE_PERIOD)
252 free_targets = ip_registry_update_free_targets(entry);
253 entry->last_connect = NOW;
255 if (0 == ++entry->attempts) /* Check for overflow */
258 if (entry->attempts < IPCHECK_CLONE_LIMIT) {
260 *next_target_out = CurrentTime - (TARGET_DELAY * free_targets - 1);
262 else if ((CurrentTime - cli_since(&me)) > IPCHECK_CLONE_DELAY) {
264 * Don't refuse connection when we just rebooted the server
277 * IPcheck_remote_connect
280 * A remote client connected to Undernet, with IP number `cptr->ip.s_addr'
281 * and hostname `hostname'.
284 * Update the IPcheck registry.
285 * Return 0 on failure, 1 on success.
287 int ip_registry_check_remote(struct Client* cptr, int is_burst)
289 struct IPRegistryEntry* entry = ip_registry_find((cli_ip(cptr)).s_addr);
292 * Mark that we did add/update an IPregistry entry
296 entry = ip_registry_new_entry();
297 entry->addr = (cli_ip(cptr)).s_addr;
300 ip_registry_add(entry);
303 if (0 == ++entry->connected) {
305 * Don't allow more then 255 connects from one IP number, ever
309 if (CONNECTED_SINCE(entry->last_connect) > IPCHECK_CLONE_PERIOD)
312 if (0 == ++entry->attempts) {
318 ip_registry_update_free_targets(entry);
319 entry->last_connect = NOW;
326 * IPcheck_connect_fail
329 * This local client failed to connect due to legal reasons.
332 * Neutralize the effect of calling IPcheck_local_connect, in such
333 * a way that the client won't be penalized when trying to reconnect
336 void ip_registry_connect_fail(unsigned int addr)
338 struct IPRegistryEntry* entry = ip_registry_find(addr);
344 * IPcheck_connect_succeeded
347 * A client succeeded to finish the registration.
349 * Finish IPcheck registration of a successfully, locally connected client.
351 void ip_registry_connect_succeeded(struct Client *cptr)
354 unsigned int free_targets = STARTTARGETS;
355 struct IPRegistryEntry* entry = ip_registry_find((cli_ip(cptr)).s_addr);
358 Debug((DEBUG_ERROR, "Missing registry entry for: %s", cli_sock_ip(cptr)));
362 memcpy(cli_targets(cptr), entry->target->targets, MAXTARGETS);
363 free_targets = entry->target->count;
366 sendcmdto_one(&me, CMD_NOTICE, cptr, "%C :on %u ca %u(%u) ft %u(%u)%s",
367 cptr, entry->connected, entry->attempts, IPCHECK_CLONE_LIMIT,
368 free_targets, STARTTARGETS, tr);
375 * A local client disconnected or a remote client left Undernet.
378 * Update the IPcheck registry.
379 * Remove all expired IPregistry structures from the hash bucket
380 * that belongs to this clients IP number.
382 void ip_registry_disconnect(struct Client *cptr)
384 struct IPRegistryEntry* entry = ip_registry_find((cli_ip(cptr)).s_addr);
387 * trying to find an entry for a server causes this to happen,
388 * servers should never have FLAG_IPCHECK set
393 * If this was the last one, set `last_connect' to disconnect time (used for expiration)
395 if (0 == --entry->connected) {
396 if (CONNECTED_SINCE(entry->last_connect) > IPCHECK_CLONE_LIMIT * IPCHECK_CLONE_PERIOD) {
398 * Otherwise we'd penetalize for this old value if the client reconnects within 20 seconds
402 ip_registry_update_free_targets(entry);
403 entry->last_connect = NOW;
405 if (MyConnect(cptr)) {
406 unsigned int free_targets;
408 * Copy the clients targets
410 if (0 == entry->target) {
411 entry->target = (struct IPTargetEntry*) MyMalloc(sizeof(struct IPTargetEntry));
412 entry->target->count = STARTTARGETS;
414 assert(0 != entry->target);
416 memcpy(entry->target->targets, cli_targets(cptr), MAXTARGETS);
418 * This calculation can be pretty unfair towards large multi-user hosts, but
419 * there is "nothing" we can do without also allowing spam bots to send more
420 * messages or by drastically increasing the ammount of memory used in the IPregistry.
422 * The problem is that when a client disconnects, leaving no free targets, then
423 * the next client from that IP number has to pay for it (getting no free targets).
424 * But ALSO the next client, and the next client, and the next client etc - until
425 * another client disconnects that DOES leave free targets. The reason for this
426 * is that if there are 10 SPAM bots, and they all disconnect at once, then they
427 * ALL should get no free targets when reconnecting. We'd need to store an entry
428 * per client (instead of per IP number) to avoid this.
430 if (cli_nexttarget(cptr) < CurrentTime) {
432 * Number of free targets
434 free_targets = (CurrentTime - cli_nexttarget(cptr)) / TARGET_DELAY + 1;
439 * Add bonus, this is pretty fuzzy, but it will help in some cases.
441 if ((CurrentTime - cli_firsttime(cptr)) > 600)
443 * Was longer then 10 minutes online?
445 free_targets += (CurrentTime - cli_firsttime(cptr) - 600) / TARGET_DELAY;
447 * Finally, store smallest value for Judgement Day
449 if (free_targets < entry->target->count)
450 entry->target->count = free_targets;
457 * Returns number of clients with the same IP number
459 int ip_registry_count(unsigned int addr)
461 struct IPRegistryEntry* entry = ip_registry_find(addr);
462 return (entry) ? entry->connected : 0;
466 * IPcheck_local_connect
469 * A new connection was accept()-ed with IP number `cptr->ip.s_addr'.
472 * Update the IPcheck registry.
474 * 1 : You're allowed to connect.
475 * 0 : You're not allowed to connect.
479 * A connection should be rejected when a connection from the same IP number was
480 * received IPCHECK_CLONE_LIMIT times before this connect attempt, with
481 * reconnect intervals of IPCHECK_CLONE_PERIOD seconds or less.
483 * Free target inheritance:
485 * When the client is accepted, then the number of Free Targets
486 * of the cptr is set to the value stored in the found IPregistry
487 * structure, or left at STARTTARGETS. This can be done by changing
488 * cptr->nexttarget to be `now - (TARGET_DELAY * (FREE_TARGETS - 1))',
489 * where FREE_TARGETS may range from 0 till STARTTARGETS.
491 int IPcheck_local_connect(struct in_addr a, time_t* next_target_out)
493 assert(0 != next_target_out);
494 return ip_registry_check_local(a.s_addr, next_target_out);
498 * IPcheck_remote_connect
501 * A remote client connected to Undernet, with IP number `cptr->ip.s_addr'
502 * and hostname `hostname'.
505 * Update the IPcheck registry.
506 * Return 0 on failure, 1 on success.
508 int IPcheck_remote_connect(struct Client *cptr, int is_burst)
511 return ip_registry_check_remote(cptr, is_burst);
515 * IPcheck_connect_fail
518 * This local client failed to connect due to legal reasons.
521 * Neutralize the effect of calling IPcheck_local_connect, in such
522 * a way that the client won't be penalized when trying to reconnect
525 void IPcheck_connect_fail(struct in_addr a)
527 ip_registry_connect_fail(a.s_addr);
531 * IPcheck_connect_succeeded
534 * A client succeeded to finish the registration.
536 * Finish IPcheck registration of a successfully, locally connected client.
538 void IPcheck_connect_succeeded(struct Client *cptr)
541 ip_registry_connect_succeeded(cptr);
548 * A local client disconnected or a remote client left Undernet.
551 * Update the IPcheck registry.
552 * Remove all expired IPregistry structures from the hash bucket
553 * that belongs to this clients IP number.
555 void IPcheck_disconnect(struct Client *cptr)
558 ip_registry_disconnect(cptr);
564 * Returns number of clients with the same IP number
566 unsigned short IPcheck_nr(struct Client *cptr)
569 return ip_registry_count(cli_ip(cptr).s_addr);