IPv6 support (hopefully with fewer future transition pains)
[ircu2.10.12-pk.git] / ircd / IPcheck.c
index 439d60e51fe151cac49c21a1c5929f83a3d620c7..56552b3e58d8d2e13edf006a4d4a7ebd8d2f81e8 100644 (file)
  * This file should be edited in a window with a width of 141 characters
  * ick
  */
+#include "config.h"
+
 #include "IPcheck.h"
 #include "client.h"
 #include "ircd.h"
 #include "msg.h"
 #include "numnicks.h"       /* NumNick, NumServ (GODMODE) */
 #include "ircd_alloc.h"
+#include "ircd_events.h"
+#include "ircd_features.h"
 #include "s_debug.h"        /* Debug */
 #include "s_user.h"         /* TARGET_DELAY */
 #include "send.h"
 
 #include <assert.h>
-
+#include <string.h>
 
 struct IPTargetEntry {
   int           count;
@@ -43,7 +47,7 @@ struct IPTargetEntry {
 struct IPRegistryEntry {
   struct IPRegistryEntry*  next;
   struct IPTargetEntry*    target;
-  unsigned int             addr;
+  struct irc_in_addr       addr;
   int                     last_connect;
   unsigned short           connected;
   unsigned char            attempts;
@@ -61,39 +65,50 @@ struct IPRegistryEntry {
 #define NOW ((unsigned short)(CurrentTime & MASK_16))
 #define CONNECTED_SINCE(x) (NOW - (x))
 
-#define IPCHECK_CLONE_LIMIT 4
-#define IPCHECK_CLONE_PERIOD 40
-#define IPCHECK_CLONE_DELAY 600
+#define IPCHECK_CLONE_LIMIT feature_int(FEAT_IPCHECK_CLONE_LIMIT)
+#define IPCHECK_CLONE_PERIOD feature_int(FEAT_IPCHECK_CLONE_PERIOD)
+#define IPCHECK_CLONE_DELAY feature_int(FEAT_IPCHECK_CLONE_DELAY)
 
 
 static struct IPRegistryEntry* hashTable[IP_REGISTRY_TABLE_SIZE];
 static struct IPRegistryEntry* freeList = 0;
 
-static unsigned int ip_registry_hash(unsigned int ip)
+static struct Timer expireTimer;
+
+static unsigned int ip_registry_hash(const struct irc_in_addr *ip)
 {
-  return ((ip >> 16) ^ ip) & (IP_REGISTRY_TABLE_SIZE - 1);
+  unsigned int res;
+
+  if (ip->in6_16[0] || ip->in6_16[1] || ip->in6_16[2] || ip->in6_16[3] || ip->in6_16[4]) {
+      /* Only use the first 64 bits of address, since the last 64 bits
+       * tend to be under user control. */
+      res = ip->in6_16[0] ^ ip->in6_16[1] ^ ip->in6_16[2] ^ ip->in6_16[3];
+  } else {
+      /* Looks like an IPv4 address. */
+      res = ip->in6_16[6] ^ ip->in6_16[7];
+  }
+  return res & (IP_REGISTRY_TABLE_SIZE - 1);
 }
 
-static struct IPRegistryEntry* ip_registry_find(unsigned int ip)
+static struct IPRegistryEntry* ip_registry_find(const struct irc_in_addr *ip)
 {
   struct IPRegistryEntry* entry = hashTable[ip_registry_hash(ip)];
-  for ( ; entry; entry = entry->next) {
-    if (entry->addr == ip)
+  for ( ; entry; entry = entry->next)
+    if (!irc_in_addr_cmp(ip, &entry->addr))
       break;
-  }
   return entry;
 }
 
 static void ip_registry_add(struct IPRegistryEntry* entry)
 {
-  unsigned int bucket = ip_registry_hash(entry->addr);
+  unsigned int bucket = ip_registry_hash(&entry->addr);
   entry->next = hashTable[bucket];
   hashTable[bucket] = entry;
 }
-  
+
 static void ip_registry_remove(struct IPRegistryEntry* entry)
 {
-  unsigned int bucket = ip_registry_hash(entry->addr);
+  unsigned int bucket = ip_registry_hash(&entry->addr);
   if (hashTable[bucket] == entry)
     hashTable[bucket] = entry->next;
   else {
@@ -106,7 +121,7 @@ static void ip_registry_remove(struct IPRegistryEntry* entry)
     }
   }
 }
+
 static struct IPRegistryEntry* ip_registry_new_entry()
 {
   struct IPRegistryEntry* entry = freeList;
@@ -166,15 +181,16 @@ static void ip_registry_expire_entry(struct IPRegistryEntry* entry)
   }
 }
 
-/*
- * ip_registry_expire
- */
-static void ip_registry_expire()
+/* Callback to run an expiry of the IPcheck registry */
+static void ip_registry_expire(struct Event* ev)
 {
   int i;
   struct IPRegistryEntry* entry;
   struct IPRegistryEntry* entry_next;
 
+  assert(ET_EXPIRE == ev_type(ev));
+  assert(0 != ev_timer(ev));
+
   for (i = 0; i < IP_REGISTRY_TABLE_SIZE; ++i) {
     for (entry = hashTable[i]; entry; entry = entry_next) {
       entry_next = entry->next;
@@ -184,6 +200,16 @@ static void ip_registry_expire()
   }
 }
 
+/*
+ * IPcheck_init()
+ *
+ * Initializes the registry timer
+ */
+void IPcheck_init(void)
+{
+  timer_add(timer_init(&expireTimer), ip_registry_expire, 0, TT_PERIODIC, 60);
+}
+
 /*
  * IPcheck_local_connect
  *
@@ -210,14 +236,14 @@ static void ip_registry_expire()
  * cptr->nexttarget to be `now - (TARGET_DELAY * (FREE_TARGETS - 1))',
  * where FREE_TARGETS may range from 0 till STARTTARGETS.
  */
-int ip_registry_check_local(unsigned int addr, time_t* next_target_out)
+int ip_registry_check_local(const struct irc_in_addr *addr, time_t* next_target_out)
 {
   struct IPRegistryEntry* entry = ip_registry_find(addr);
   unsigned int free_targets = STARTTARGETS;
+
   if (0 == entry) {
     entry       = ip_registry_new_entry();
-    entry->addr = addr;    /* The IP number of registry entry */
+    memcpy(&entry->addr, addr, sizeof(entry->addr));
     ip_registry_add(entry);
     return 1;
   }
@@ -227,7 +253,10 @@ int ip_registry_check_local(unsigned int addr, time_t* next_target_out)
    * Don't allow more then 255 connects from one IP number, ever
    */
   if (0 == ++entry->connected)
+  {
+    entry->connected--;
     return 0;
+  }
 
   if (CONNECTED_SINCE(entry->last_connect) > IPCHECK_CLONE_PERIOD)
     entry->attempts = 0;
@@ -249,6 +278,7 @@ int ip_registry_check_local(unsigned int addr, time_t* next_target_out)
 #ifdef NOTHROTTLE 
     return 1;
 #else
+    assert(entry->connected > 0);
     --entry->connected;
     return 0;
 #endif        
@@ -269,7 +299,7 @@ int ip_registry_check_local(unsigned int addr, time_t* next_target_out)
  */
 int ip_registry_check_remote(struct Client* cptr, int is_burst)
 {
-  struct IPRegistryEntry* entry = ip_registry_find((cli_ip(cptr)).s_addr);
+  struct IPRegistryEntry* entry = ip_registry_find(&cli_ip(cptr));
 
   /*
    * Mark that we did add/update an IPregistry entry
@@ -277,7 +307,7 @@ int ip_registry_check_remote(struct Client* cptr, int is_burst)
   SetIPChecked(cptr);
   if (0 == entry) {
     entry = ip_registry_new_entry();
-    entry->addr = (cli_ip(cptr)).s_addr;
+    memcpy(&entry->addr, &cli_ip(cptr), sizeof(entry->addr));
     if (is_burst)
       entry->attempts = 0;
     ip_registry_add(entry);
@@ -302,7 +332,6 @@ int ip_registry_check_remote(struct Client* cptr, int is_burst)
       entry->last_connect = NOW;
     }
   }
-  SetIPChecked(cptr);
   return 1;
 }
 
@@ -317,11 +346,14 @@ int ip_registry_check_remote(struct Client* cptr, int is_burst)
  *   a way that the client won't be penalized when trying to reconnect
  *   again.
  */
-void ip_registry_connect_fail(unsigned int addr)
+void ip_registry_connect_fail(const struct irc_in_addr *addr)
 {
   struct IPRegistryEntry* entry = ip_registry_find(addr);
   if (entry)
-    --entry->attempts;
+  {
+    if (0 == --entry->attempts)
+      ++entry->attempts;
+  }
 }
 
 /*
@@ -336,7 +368,7 @@ void ip_registry_connect_succeeded(struct Client *cptr)
 {
   const char*             tr    = "";
   unsigned int free_targets     = STARTTARGETS;
-  struct IPRegistryEntry* entry = ip_registry_find((cli_ip(cptr)).s_addr);
+  struct IPRegistryEntry* entry = ip_registry_find(&cli_ip(cptr));
 
   if (!entry) {
     Debug((DEBUG_ERROR, "Missing registry entry for: %s", cli_sock_ip(cptr)));
@@ -347,8 +379,8 @@ void ip_registry_connect_succeeded(struct Client *cptr)
     free_targets = entry->target->count;
     tr = " tr";
   }
-  sendcmdto_one(&me, CMD_NOTICE, cptr, ":on %u ca %u(%u) ft %u(%u)%s",
-               entry->connected, entry->attempts, IPCHECK_CLONE_LIMIT,
+  sendcmdto_one(&me, CMD_NOTICE, cptr, "%C :on %u ca %u(%u) ft %u(%u)%s",
+               cptr, entry->connected, entry->attempts, IPCHECK_CLONE_LIMIT,
                free_targets, STARTTARGETS, tr);
 }
 
@@ -365,17 +397,18 @@ void ip_registry_connect_succeeded(struct Client *cptr)
  */
 void ip_registry_disconnect(struct Client *cptr)
 {
-  struct IPRegistryEntry* entry = ip_registry_find((cli_ip(cptr)).s_addr);
+  struct IPRegistryEntry* entry = ip_registry_find(&cli_ip(cptr));
   if (0 == entry) {
     /*
      * trying to find an entry for a server causes this to happen,
-     * servers should never have FLAGS_IPCHECK set
+     * servers should never have FLAG_IPCHECK set
      */
     return;
   }
   /*
    * If this was the last one, set `last_connect' to disconnect time (used for expiration)
    */
+  /* assert(entry->connected > 0); */
   if (0 == --entry->connected) {
     if (CONNECTED_SINCE(entry->last_connect) > IPCHECK_CLONE_LIMIT * IPCHECK_CLONE_PERIOD) {
       /*
@@ -440,7 +473,7 @@ void ip_registry_disconnect(struct Client *cptr)
  *
  * Returns number of clients with the same IP number
  */
-int ip_registry_count(unsigned int addr)
+int ip_registry_count(const struct irc_in_addr *addr)
 {
   struct IPRegistryEntry* entry = ip_registry_find(addr);
   return (entry) ? entry->connected : 0;
@@ -472,10 +505,10 @@ int ip_registry_count(unsigned int addr)
  * cptr->nexttarget to be `now - (TARGET_DELAY * (FREE_TARGETS - 1))',
  * where FREE_TARGETS may range from 0 till STARTTARGETS.
  */
-int IPcheck_local_connect(struct in_addr a, time_t* next_target_out)
+int IPcheck_local_connect(const struct irc_in_addr *a, time_t* next_target_out)
 {
   assert(0 != next_target_out);
-  return ip_registry_check_local(a.s_addr, next_target_out);
+  return ip_registry_check_local(a, next_target_out);
 }
 
 /*
@@ -506,9 +539,9 @@ int IPcheck_remote_connect(struct Client *cptr, int is_burst)
  *   a way that the client won't be penalized when trying to reconnect
  *   again.
  */
-void IPcheck_connect_fail(struct in_addr a)
+void IPcheck_connect_fail(const struct irc_in_addr *a)
 {
-  ip_registry_connect_fail(a.s_addr);
+  ip_registry_connect_fail(a);
 }
 
 /*
@@ -550,16 +583,5 @@ void IPcheck_disconnect(struct Client *cptr)
 unsigned short IPcheck_nr(struct Client *cptr)
 {
   assert(0 != cptr);
-  return ip_registry_count(cptr->ip.s_addr);
+  return ip_registry_count(&cli_ip(cptr));
 }
-
-void IPcheck_expire()
-{
-  static time_t next_expire = 0;
-  if (next_expire < CurrentTime) {
-    ip_registry_expire();
-    next_expire = CurrentTime + 60;
-  }
-}
-
-