From 5b20d409ef3062b24bbe7667f0daec34523446a6 Mon Sep 17 00:00:00 2001 From: Jozsef Kadlecsik Date: Thu, 22 Apr 2010 17:00:42 +0200 Subject: Fifth stage to ipset-5 Rename files in kernel/ and get rid of old ones (2.4.x kernel tree support). --- kernel/ip_set_hash_ip.c | 164 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 kernel/ip_set_hash_ip.c (limited to 'kernel/ip_set_hash_ip.c') diff --git a/kernel/ip_set_hash_ip.c b/kernel/ip_set_hash_ip.c new file mode 100644 index 0000000..1accbe3 --- /dev/null +++ b/kernel/ip_set_hash_ip.c @@ -0,0 +1,164 @@ +/* Copyright (C) 2003-2008 Jozsef Kadlecsik + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +/* Kernel module implementing an ip hash set */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +static int limit = MAX_RANGE; + +static inline __u32 +iphash_id(struct ip_set *set, ip_set_ip_t ip) +{ + struct ip_set_iphash *map = set->data; + __u32 id; + u_int16_t i; + ip_set_ip_t *elem; + + + ip &= map->netmask; + DP("set: %s, ip:%u.%u.%u.%u", set->name, HIPQUAD(ip)); + for (i = 0; i < map->probes; i++) { + id = jhash_ip(map, i, ip) % map->hashsize; + DP("hash key: %u", id); + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id); + if (*elem == ip) + return id; + /* No shortcut - there can be deleted entries. */ + } + return UINT_MAX; +} + +static inline int +iphash_test(struct ip_set *set, ip_set_ip_t ip) +{ + return (ip && iphash_id(set, ip) != UINT_MAX); +} + +#define KADT_CONDITION + +UADT(iphash, test) +KADT(iphash, test, ipaddr) + +static inline int +__iphash_add(struct ip_set_iphash *map, ip_set_ip_t *ip) +{ + __u32 probe; + u_int16_t i; + ip_set_ip_t *elem, *slot = NULL; + + for (i = 0; i < map->probes; i++) { + probe = jhash_ip(map, i, *ip) % map->hashsize; + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe); + if (*elem == *ip) + return -EEXIST; + if (!(slot || *elem)) + slot = elem; + /* There can be deleted entries, must check all slots */ + } + if (slot) { + *slot = *ip; + map->elements++; + return 0; + } + /* Trigger rehashing */ + return -EAGAIN; +} + +static inline int +iphash_add(struct ip_set *set, ip_set_ip_t ip) +{ + struct ip_set_iphash *map = set->data; + + if (!ip || map->elements >= limit) + return -ERANGE; + + ip &= map->netmask; + return __iphash_add(map, &ip); +} + +UADT(iphash, add) +KADT(iphash, add, ipaddr) + +static inline void +__iphash_retry(struct ip_set_iphash *tmp, struct ip_set_iphash *map) +{ + tmp->netmask = map->netmask; +} + +HASH_RETRY(iphash, ip_set_ip_t) + +static inline int +iphash_del(struct ip_set *set, ip_set_ip_t ip) +{ + struct ip_set_iphash *map = set->data; + ip_set_ip_t id, *elem; + + if (!ip) + return -ERANGE; + + id = iphash_id(set, ip); + if (id == UINT_MAX) + return -EEXIST; + + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id); + *elem = 0; + map->elements--; + + return 0; +} + +UADT(iphash, del) +KADT(iphash, del, ipaddr) + +static inline int +__iphash_create(const struct ip_set_req_iphash_create *req, + struct ip_set_iphash *map) +{ + map->netmask = req->netmask; + + return 0; +} + +HASH_CREATE(iphash, ip_set_ip_t) +HASH_DESTROY(iphash) + +HASH_FLUSH(iphash, ip_set_ip_t) + +static inline void +__iphash_list_header(const struct ip_set_iphash *map, + struct ip_set_req_iphash_create *header) +{ + header->netmask = map->netmask; +} + +HASH_LIST_HEADER(iphash) +HASH_LIST_MEMBERS_SIZE(iphash, ip_set_ip_t) +HASH_LIST_MEMBERS(iphash, ip_set_ip_t) + +IP_SET_RTYPE(iphash, IPSET_TYPE_IP | IPSET_DATA_SINGLE) + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Jozsef Kadlecsik "); +MODULE_DESCRIPTION("iphash type of IP sets"); +module_param(limit, int, 0600); +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets"); + +REGISTER_MODULE(iphash) -- cgit v1.2.3