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 "s_debug.h" /* Debug */
35 #include "s_user.h" /* TARGET_DELAY */
41 struct IPTargetEntry {
43 unsigned char targets[MAXTARGETS];
46 struct IPRegistryEntry {
47 struct IPRegistryEntry* next;
48 struct IPTargetEntry* target;
51 unsigned short connected;
52 unsigned char attempts;
56 * Hash table for IPv4 address registry
58 * Hash table size must be a power of 2
59 * Use 64K hash table to conserve memory
61 #define IP_REGISTRY_TABLE_SIZE 0x10000
62 #define MASK_16 0xffff
64 #define NOW ((unsigned short)(CurrentTime & MASK_16))
65 #define CONNECTED_SINCE(x) (NOW - (x))
67 #define IPCHECK_CLONE_LIMIT 4
68 #define IPCHECK_CLONE_PERIOD 40
69 #define IPCHECK_CLONE_DELAY 600
72 static struct IPRegistryEntry* hashTable[IP_REGISTRY_TABLE_SIZE];
73 static struct IPRegistryEntry* freeList = 0;
75 static struct Timer expireTimer;
77 static unsigned int ip_registry_hash(unsigned int ip)
79 return ((ip >> 16) ^ ip) & (IP_REGISTRY_TABLE_SIZE - 1);
82 static struct IPRegistryEntry* ip_registry_find(unsigned int ip)
84 struct IPRegistryEntry* entry = hashTable[ip_registry_hash(ip)];
85 for ( ; entry; entry = entry->next) {
86 if (entry->addr == ip)
92 static void ip_registry_add(struct IPRegistryEntry* entry)
94 unsigned int bucket = ip_registry_hash(entry->addr);
95 entry->next = hashTable[bucket];
96 hashTable[bucket] = entry;
99 static void ip_registry_remove(struct IPRegistryEntry* entry)
101 unsigned int bucket = ip_registry_hash(entry->addr);
102 if (hashTable[bucket] == entry)
103 hashTable[bucket] = entry->next;
105 struct IPRegistryEntry* prev = hashTable[bucket];
106 for ( ; prev; prev = prev->next) {
107 if (prev->next == entry) {
108 prev->next = entry->next;
115 static struct IPRegistryEntry* ip_registry_new_entry()
117 struct IPRegistryEntry* entry = freeList;
119 freeList = entry->next;
121 entry = (struct IPRegistryEntry*) MyMalloc(sizeof(struct IPRegistryEntry));
124 memset(entry, 0, sizeof(struct IPRegistryEntry));
125 entry->last_connect = NOW; /* Seconds since last connect attempt */
126 entry->connected = 1; /* connected clients for this IP */
127 entry->attempts = 1; /* Number attempts for this IP */
131 static void ip_registry_delete_entry(struct IPRegistryEntry* entry)
134 MyFree(entry->target);
135 entry->next = freeList;
139 static unsigned int ip_registry_update_free_targets(struct IPRegistryEntry* entry)
141 unsigned int free_targets = STARTTARGETS;
144 free_targets = entry->target->count + (CONNECTED_SINCE(entry->last_connect) / TARGET_DELAY);
145 if (free_targets > STARTTARGETS)
146 free_targets = STARTTARGETS;
147 entry->target->count = free_targets;
152 static void ip_registry_expire_entry(struct IPRegistryEntry* entry)
155 * Don't touch this number, it has statistical significance
156 * XXX - blah blah blah
158 if (CONNECTED_SINCE(entry->last_connect) > 600) {
162 ip_registry_remove(entry);
163 ip_registry_delete_entry(entry);
165 else if (CONNECTED_SINCE(entry->last_connect) > 120 && 0 != entry->target) {
167 * Expire storage of targets
169 MyFree(entry->target);
174 /* Callback to run an expiry of the IPcheck registry */
175 static void ip_registry_expire(struct Event* ev)
178 struct IPRegistryEntry* entry;
179 struct IPRegistryEntry* entry_next;
181 assert(ET_EXPIRE == ev_type(ev));
182 assert(0 != ev_timer(ev));
184 for (i = 0; i < IP_REGISTRY_TABLE_SIZE; ++i) {
185 for (entry = hashTable[i]; entry; entry = entry_next) {
186 entry_next = entry->next;
187 if (0 == entry->connected)
188 ip_registry_expire_entry(entry);
196 * Initializes the registry timer
198 void IPcheck_init(void)
200 timer_add(&expireTimer, ip_registry_expire, 0, TT_PERIODIC, 60);
204 * IPcheck_local_connect
207 * A new connection was accept()-ed with IP number `cptr->ip.s_addr'.
210 * Update the IPcheck registry.
212 * 1 : You're allowed to connect.
213 * 0 : You're not allowed to connect.
217 * A connection should be rejected when a connection from the same IP number was
218 * received IPCHECK_CLONE_LIMIT times before this connect attempt, with
219 * reconnect intervals of IPCHECK_CLONE_PERIOD seconds or less.
221 * Free target inheritance:
223 * When the client is accepted, then the number of Free Targets
224 * of the cptr is set to the value stored in the found IPregistry
225 * structure, or left at STARTTARGETS. This can be done by changing
226 * cptr->nexttarget to be `now - (TARGET_DELAY * (FREE_TARGETS - 1))',
227 * where FREE_TARGETS may range from 0 till STARTTARGETS.
229 int ip_registry_check_local(unsigned int addr, time_t* next_target_out)
231 struct IPRegistryEntry* entry = ip_registry_find(addr);
232 unsigned int free_targets = STARTTARGETS;
235 entry = ip_registry_new_entry();
236 entry->addr = addr; /* The IP number of registry entry */
237 ip_registry_add(entry);
240 /* Note that this also connects server connects.
241 * It is hard and not interesting, to change that.
243 * Don't allow more then 255 connects from one IP number, ever
245 if (0 == ++entry->connected)
248 if (CONNECTED_SINCE(entry->last_connect) > IPCHECK_CLONE_PERIOD)
251 free_targets = ip_registry_update_free_targets(entry);
252 entry->last_connect = NOW;
254 if (0 == ++entry->attempts) /* Check for overflow */
257 if (entry->attempts < IPCHECK_CLONE_LIMIT) {
259 *next_target_out = CurrentTime - (TARGET_DELAY * free_targets - 1);
261 else if ((CurrentTime - cli_since(&me)) > IPCHECK_CLONE_DELAY) {
263 * Don't refuse connection when we just rebooted the server
276 * IPcheck_remote_connect
279 * A remote client connected to Undernet, with IP number `cptr->ip.s_addr'
280 * and hostname `hostname'.
283 * Update the IPcheck registry.
284 * Return 0 on failure, 1 on success.
286 int ip_registry_check_remote(struct Client* cptr, int is_burst)
288 struct IPRegistryEntry* entry = ip_registry_find((cli_ip(cptr)).s_addr);
291 * Mark that we did add/update an IPregistry entry
295 entry = ip_registry_new_entry();
296 entry->addr = (cli_ip(cptr)).s_addr;
299 ip_registry_add(entry);
302 if (0 == ++entry->connected) {
304 * Don't allow more then 255 connects from one IP number, ever
308 if (CONNECTED_SINCE(entry->last_connect) > IPCHECK_CLONE_PERIOD)
311 if (0 == ++entry->attempts) {
317 ip_registry_update_free_targets(entry);
318 entry->last_connect = NOW;
325 * IPcheck_connect_fail
328 * This local client failed to connect due to legal reasons.
331 * Neutralize the effect of calling IPcheck_local_connect, in such
332 * a way that the client won't be penalized when trying to reconnect
335 void ip_registry_connect_fail(unsigned int addr)
337 struct IPRegistryEntry* entry = ip_registry_find(addr);
343 * IPcheck_connect_succeeded
346 * A client succeeded to finish the registration.
348 * Finish IPcheck registration of a successfully, locally connected client.
350 void ip_registry_connect_succeeded(struct Client *cptr)
353 unsigned int free_targets = STARTTARGETS;
354 struct IPRegistryEntry* entry = ip_registry_find((cli_ip(cptr)).s_addr);
357 Debug((DEBUG_ERROR, "Missing registry entry for: %s", cli_sock_ip(cptr)));
361 memcpy(cli_targets(cptr), entry->target->targets, MAXTARGETS);
362 free_targets = entry->target->count;
365 sendcmdto_one(&me, CMD_NOTICE, cptr, "%C :on %u ca %u(%u) ft %u(%u)%s",
366 cptr, entry->connected, entry->attempts, IPCHECK_CLONE_LIMIT,
367 free_targets, STARTTARGETS, tr);
374 * A local client disconnected or a remote client left Undernet.
377 * Update the IPcheck registry.
378 * Remove all expired IPregistry structures from the hash bucket
379 * that belongs to this clients IP number.
381 void ip_registry_disconnect(struct Client *cptr)
383 struct IPRegistryEntry* entry = ip_registry_find((cli_ip(cptr)).s_addr);
386 * trying to find an entry for a server causes this to happen,
387 * servers should never have FLAGS_IPCHECK set
392 * If this was the last one, set `last_connect' to disconnect time (used for expiration)
394 if (0 == --entry->connected) {
395 if (CONNECTED_SINCE(entry->last_connect) > IPCHECK_CLONE_LIMIT * IPCHECK_CLONE_PERIOD) {
397 * Otherwise we'd penetalize for this old value if the client reconnects within 20 seconds
401 ip_registry_update_free_targets(entry);
402 entry->last_connect = NOW;
404 if (MyConnect(cptr)) {
405 unsigned int free_targets;
407 * Copy the clients targets
409 if (0 == entry->target) {
410 entry->target = (struct IPTargetEntry*) MyMalloc(sizeof(struct IPTargetEntry));
411 entry->target->count = STARTTARGETS;
413 assert(0 != entry->target);
415 memcpy(entry->target->targets, cli_targets(cptr), MAXTARGETS);
417 * This calculation can be pretty unfair towards large multi-user hosts, but
418 * there is "nothing" we can do without also allowing spam bots to send more
419 * messages or by drastically increasing the ammount of memory used in the IPregistry.
421 * The problem is that when a client disconnects, leaving no free targets, then
422 * the next client from that IP number has to pay for it (getting no free targets).
423 * But ALSO the next client, and the next client, and the next client etc - until
424 * another client disconnects that DOES leave free targets. The reason for this
425 * is that if there are 10 SPAM bots, and they all disconnect at once, then they
426 * ALL should get no free targets when reconnecting. We'd need to store an entry
427 * per client (instead of per IP number) to avoid this.
429 if (cli_nexttarget(cptr) < CurrentTime) {
431 * Number of free targets
433 free_targets = (CurrentTime - cli_nexttarget(cptr)) / TARGET_DELAY + 1;
438 * Add bonus, this is pretty fuzzy, but it will help in some cases.
440 if ((CurrentTime - cli_firsttime(cptr)) > 600)
442 * Was longer then 10 minutes online?
444 free_targets += (CurrentTime - cli_firsttime(cptr) - 600) / TARGET_DELAY;
446 * Finally, store smallest value for Judgement Day
448 if (free_targets < entry->target->count)
449 entry->target->count = free_targets;
456 * Returns number of clients with the same IP number
458 int ip_registry_count(unsigned int addr)
460 struct IPRegistryEntry* entry = ip_registry_find(addr);
461 return (entry) ? entry->connected : 0;
465 * IPcheck_local_connect
468 * A new connection was accept()-ed with IP number `cptr->ip.s_addr'.
471 * Update the IPcheck registry.
473 * 1 : You're allowed to connect.
474 * 0 : You're not allowed to connect.
478 * A connection should be rejected when a connection from the same IP number was
479 * received IPCHECK_CLONE_LIMIT times before this connect attempt, with
480 * reconnect intervals of IPCHECK_CLONE_PERIOD seconds or less.
482 * Free target inheritance:
484 * When the client is accepted, then the number of Free Targets
485 * of the cptr is set to the value stored in the found IPregistry
486 * structure, or left at STARTTARGETS. This can be done by changing
487 * cptr->nexttarget to be `now - (TARGET_DELAY * (FREE_TARGETS - 1))',
488 * where FREE_TARGETS may range from 0 till STARTTARGETS.
490 int IPcheck_local_connect(struct in_addr a, time_t* next_target_out)
492 assert(0 != next_target_out);
493 return ip_registry_check_local(a.s_addr, next_target_out);
497 * IPcheck_remote_connect
500 * A remote client connected to Undernet, with IP number `cptr->ip.s_addr'
501 * and hostname `hostname'.
504 * Update the IPcheck registry.
505 * Return 0 on failure, 1 on success.
507 int IPcheck_remote_connect(struct Client *cptr, int is_burst)
510 return ip_registry_check_remote(cptr, is_burst);
514 * IPcheck_connect_fail
517 * This local client failed to connect due to legal reasons.
520 * Neutralize the effect of calling IPcheck_local_connect, in such
521 * a way that the client won't be penalized when trying to reconnect
524 void IPcheck_connect_fail(struct in_addr a)
526 ip_registry_connect_fail(a.s_addr);
530 * IPcheck_connect_succeeded
533 * A client succeeded to finish the registration.
535 * Finish IPcheck registration of a successfully, locally connected client.
537 void IPcheck_connect_succeeded(struct Client *cptr)
540 ip_registry_connect_succeeded(cptr);
547 * A local client disconnected or a remote client left Undernet.
550 * Update the IPcheck registry.
551 * Remove all expired IPregistry structures from the hash bucket
552 * that belongs to this clients IP number.
554 void IPcheck_disconnect(struct Client *cptr)
557 ip_registry_disconnect(cptr);
563 * Returns number of clients with the same IP number
565 unsigned short IPcheck_nr(struct Client *cptr)
568 return ip_registry_count(cli_ip(cptr).s_addr);