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;
50 struct irc_in_addr addr;
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(const struct irc_in_addr *ip)
82 if (ip->in6_16[0] || ip->in6_16[1] || ip->in6_16[2] || ip->in6_16[3] || ip->in6_16[4]) {
83 /* Only use the first 64 bits of address, since the last 64 bits
84 * tend to be under user control. */
85 res = ip->in6_16[0] ^ ip->in6_16[1] ^ ip->in6_16[2] ^ ip->in6_16[3];
87 /* Looks like an IPv4 address. */
88 res = ip->in6_16[6] ^ ip->in6_16[7];
90 return res & (IP_REGISTRY_TABLE_SIZE - 1);
93 static struct IPRegistryEntry* ip_registry_find(const struct irc_in_addr *ip)
95 struct IPRegistryEntry* entry = hashTable[ip_registry_hash(ip)];
96 for ( ; entry; entry = entry->next)
97 if (!irc_in_addr_cmp(ip, &entry->addr))
102 static void ip_registry_add(struct IPRegistryEntry* entry)
104 unsigned int bucket = ip_registry_hash(&entry->addr);
105 entry->next = hashTable[bucket];
106 hashTable[bucket] = entry;
109 static void ip_registry_remove(struct IPRegistryEntry* entry)
111 unsigned int bucket = ip_registry_hash(&entry->addr);
112 if (hashTable[bucket] == entry)
113 hashTable[bucket] = entry->next;
115 struct IPRegistryEntry* prev = hashTable[bucket];
116 for ( ; prev; prev = prev->next) {
117 if (prev->next == entry) {
118 prev->next = entry->next;
125 static struct IPRegistryEntry* ip_registry_new_entry()
127 struct IPRegistryEntry* entry = freeList;
129 freeList = entry->next;
131 entry = (struct IPRegistryEntry*) MyMalloc(sizeof(struct IPRegistryEntry));
134 memset(entry, 0, sizeof(struct IPRegistryEntry));
135 entry->last_connect = NOW; /* Seconds since last connect attempt */
136 entry->connected = 1; /* connected clients for this IP */
137 entry->attempts = 1; /* Number attempts for this IP */
141 static void ip_registry_delete_entry(struct IPRegistryEntry* entry)
144 MyFree(entry->target);
145 entry->next = freeList;
149 static unsigned int ip_registry_update_free_targets(struct IPRegistryEntry* entry)
151 unsigned int free_targets = STARTTARGETS;
154 free_targets = entry->target->count + (CONNECTED_SINCE(entry->last_connect) / TARGET_DELAY);
155 if (free_targets > STARTTARGETS)
156 free_targets = STARTTARGETS;
157 entry->target->count = free_targets;
162 static void ip_registry_expire_entry(struct IPRegistryEntry* entry)
165 * Don't touch this number, it has statistical significance
166 * XXX - blah blah blah
168 if (CONNECTED_SINCE(entry->last_connect) > 600) {
172 ip_registry_remove(entry);
173 ip_registry_delete_entry(entry);
175 else if (CONNECTED_SINCE(entry->last_connect) > 120 && 0 != entry->target) {
177 * Expire storage of targets
179 MyFree(entry->target);
184 /* Callback to run an expiry of the IPcheck registry */
185 static void ip_registry_expire(struct Event* ev)
188 struct IPRegistryEntry* entry;
189 struct IPRegistryEntry* entry_next;
191 assert(ET_EXPIRE == ev_type(ev));
192 assert(0 != ev_timer(ev));
194 for (i = 0; i < IP_REGISTRY_TABLE_SIZE; ++i) {
195 for (entry = hashTable[i]; entry; entry = entry_next) {
196 entry_next = entry->next;
197 if (0 == entry->connected)
198 ip_registry_expire_entry(entry);
206 * Initializes the registry timer
208 void IPcheck_init(void)
210 timer_add(timer_init(&expireTimer), ip_registry_expire, 0, TT_PERIODIC, 60);
214 * IPcheck_local_connect
217 * A new connection was accept()-ed with IP number `cptr->ip.s_addr'.
220 * Update the IPcheck registry.
222 * 1 : You're allowed to connect.
223 * 0 : You're not allowed to connect.
227 * A connection should be rejected when a connection from the same IP number was
228 * received IPCHECK_CLONE_LIMIT times before this connect attempt, with
229 * reconnect intervals of IPCHECK_CLONE_PERIOD seconds or less.
231 * Free target inheritance:
233 * When the client is accepted, then the number of Free Targets
234 * of the cptr is set to the value stored in the found IPregistry
235 * structure, or left at STARTTARGETS. This can be done by changing
236 * cptr->nexttarget to be `now - (TARGET_DELAY * (FREE_TARGETS - 1))',
237 * where FREE_TARGETS may range from 0 till STARTTARGETS.
239 int ip_registry_check_local(const struct irc_in_addr *addr, time_t* next_target_out)
241 struct IPRegistryEntry* entry = ip_registry_find(addr);
242 unsigned int free_targets = STARTTARGETS;
245 entry = ip_registry_new_entry();
246 memcpy(&entry->addr, addr, sizeof(entry->addr));
247 ip_registry_add(entry);
250 /* Note that this also connects server connects.
251 * It is hard and not interesting, to change that.
253 * Don't allow more then 255 connects from one IP number, ever
255 if (0 == ++entry->connected)
261 if (CONNECTED_SINCE(entry->last_connect) > IPCHECK_CLONE_PERIOD)
264 free_targets = ip_registry_update_free_targets(entry);
265 entry->last_connect = NOW;
267 if (0 == ++entry->attempts) /* Check for overflow */
270 if (entry->attempts < IPCHECK_CLONE_LIMIT) {
272 *next_target_out = CurrentTime - (TARGET_DELAY * free_targets - 1);
274 else if ((CurrentTime - cli_since(&me)) > IPCHECK_CLONE_DELAY) {
276 * Don't refuse connection when we just rebooted the server
281 assert(entry->connected > 0);
290 * IPcheck_remote_connect
293 * A remote client connected to Undernet, with IP number `cptr->ip.s_addr'
294 * and hostname `hostname'.
297 * Update the IPcheck registry.
298 * Return 0 on failure, 1 on success.
300 int ip_registry_check_remote(struct Client* cptr, int is_burst)
302 struct IPRegistryEntry* entry = ip_registry_find(&cli_ip(cptr));
305 * Mark that we did add/update an IPregistry entry
309 entry = ip_registry_new_entry();
310 memcpy(&entry->addr, &cli_ip(cptr), sizeof(entry->addr));
313 ip_registry_add(entry);
316 if (0 == ++entry->connected) {
318 * Don't allow more then 255 connects from one IP number, ever
322 if (CONNECTED_SINCE(entry->last_connect) > IPCHECK_CLONE_PERIOD)
325 if (0 == ++entry->attempts) {
331 ip_registry_update_free_targets(entry);
332 entry->last_connect = NOW;
339 * IPcheck_connect_fail
342 * This local client failed to connect due to legal reasons.
345 * Neutralize the effect of calling IPcheck_local_connect, in such
346 * a way that the client won't be penalized when trying to reconnect
349 void ip_registry_connect_fail(const struct irc_in_addr *addr)
351 struct IPRegistryEntry* entry = ip_registry_find(addr);
354 if (0 == --entry->attempts)
360 * IPcheck_connect_succeeded
363 * A client succeeded to finish the registration.
365 * Finish IPcheck registration of a successfully, locally connected client.
367 void ip_registry_connect_succeeded(struct Client *cptr)
370 unsigned int free_targets = STARTTARGETS;
371 struct IPRegistryEntry* entry = ip_registry_find(&cli_ip(cptr));
374 Debug((DEBUG_ERROR, "Missing registry entry for: %s", cli_sock_ip(cptr)));
378 memcpy(cli_targets(cptr), entry->target->targets, MAXTARGETS);
379 free_targets = entry->target->count;
382 sendcmdto_one(&me, CMD_NOTICE, cptr, "%C :on %u ca %u(%u) ft %u(%u)%s",
383 cptr, entry->connected, entry->attempts, IPCHECK_CLONE_LIMIT,
384 free_targets, STARTTARGETS, tr);
391 * A local client disconnected or a remote client left Undernet.
394 * Update the IPcheck registry.
395 * Remove all expired IPregistry structures from the hash bucket
396 * that belongs to this clients IP number.
398 void ip_registry_disconnect(struct Client *cptr)
400 struct IPRegistryEntry* entry = ip_registry_find(&cli_ip(cptr));
403 * trying to find an entry for a server causes this to happen,
404 * servers should never have FLAG_IPCHECK set
409 * If this was the last one, set `last_connect' to disconnect time (used for expiration)
411 /* assert(entry->connected > 0); */
412 if (0 == --entry->connected) {
413 if (CONNECTED_SINCE(entry->last_connect) > IPCHECK_CLONE_LIMIT * IPCHECK_CLONE_PERIOD) {
415 * Otherwise we'd penetalize for this old value if the client reconnects within 20 seconds
419 ip_registry_update_free_targets(entry);
420 entry->last_connect = NOW;
422 if (MyConnect(cptr)) {
423 unsigned int free_targets;
425 * Copy the clients targets
427 if (0 == entry->target) {
428 entry->target = (struct IPTargetEntry*) MyMalloc(sizeof(struct IPTargetEntry));
429 entry->target->count = STARTTARGETS;
431 assert(0 != entry->target);
433 memcpy(entry->target->targets, cli_targets(cptr), MAXTARGETS);
435 * This calculation can be pretty unfair towards large multi-user hosts, but
436 * there is "nothing" we can do without also allowing spam bots to send more
437 * messages or by drastically increasing the ammount of memory used in the IPregistry.
439 * The problem is that when a client disconnects, leaving no free targets, then
440 * the next client from that IP number has to pay for it (getting no free targets).
441 * But ALSO the next client, and the next client, and the next client etc - until
442 * another client disconnects that DOES leave free targets. The reason for this
443 * is that if there are 10 SPAM bots, and they all disconnect at once, then they
444 * ALL should get no free targets when reconnecting. We'd need to store an entry
445 * per client (instead of per IP number) to avoid this.
447 if (cli_nexttarget(cptr) < CurrentTime) {
449 * Number of free targets
451 free_targets = (CurrentTime - cli_nexttarget(cptr)) / TARGET_DELAY + 1;
456 * Add bonus, this is pretty fuzzy, but it will help in some cases.
458 if ((CurrentTime - cli_firsttime(cptr)) > 600)
460 * Was longer then 10 minutes online?
462 free_targets += (CurrentTime - cli_firsttime(cptr) - 600) / TARGET_DELAY;
464 * Finally, store smallest value for Judgement Day
466 if (free_targets < entry->target->count)
467 entry->target->count = free_targets;
474 * Returns number of clients with the same IP number
476 int ip_registry_count(const struct irc_in_addr *addr)
478 struct IPRegistryEntry* entry = ip_registry_find(addr);
479 return (entry) ? entry->connected : 0;
483 * IPcheck_local_connect
486 * A new connection was accept()-ed with IP number `cptr->ip.s_addr'.
489 * Update the IPcheck registry.
491 * 1 : You're allowed to connect.
492 * 0 : You're not allowed to connect.
496 * A connection should be rejected when a connection from the same IP number was
497 * received IPCHECK_CLONE_LIMIT times before this connect attempt, with
498 * reconnect intervals of IPCHECK_CLONE_PERIOD seconds or less.
500 * Free target inheritance:
502 * When the client is accepted, then the number of Free Targets
503 * of the cptr is set to the value stored in the found IPregistry
504 * structure, or left at STARTTARGETS. This can be done by changing
505 * cptr->nexttarget to be `now - (TARGET_DELAY * (FREE_TARGETS - 1))',
506 * where FREE_TARGETS may range from 0 till STARTTARGETS.
508 int IPcheck_local_connect(const struct irc_in_addr *a, time_t* next_target_out)
510 assert(0 != next_target_out);
511 return ip_registry_check_local(a, next_target_out);
515 * IPcheck_remote_connect
518 * A remote client connected to Undernet, with IP number `cptr->ip.s_addr'
519 * and hostname `hostname'.
522 * Update the IPcheck registry.
523 * Return 0 on failure, 1 on success.
525 int IPcheck_remote_connect(struct Client *cptr, int is_burst)
528 return ip_registry_check_remote(cptr, is_burst);
532 * IPcheck_connect_fail
535 * This local client failed to connect due to legal reasons.
538 * Neutralize the effect of calling IPcheck_local_connect, in such
539 * a way that the client won't be penalized when trying to reconnect
542 void IPcheck_connect_fail(const struct irc_in_addr *a)
544 ip_registry_connect_fail(a);
548 * IPcheck_connect_succeeded
551 * A client succeeded to finish the registration.
553 * Finish IPcheck registration of a successfully, locally connected client.
555 void IPcheck_connect_succeeded(struct Client *cptr)
558 ip_registry_connect_succeeded(cptr);
565 * A local client disconnected or a remote client left Undernet.
568 * Update the IPcheck registry.
569 * Remove all expired IPregistry structures from the hash bucket
570 * that belongs to this clients IP number.
572 void IPcheck_disconnect(struct Client *cptr)
575 ip_registry_disconnect(cptr);
581 * Returns number of clients with the same IP number
583 unsigned short IPcheck_nr(struct Client *cptr)
586 return ip_registry_count(&cli_ip(cptr));