From 98345bbd0ede53c5d17593262acf1f9ec236bf83 Mon Sep 17 00:00:00 2001 From: "/C=DE/ST=Berlin/L=Berlin/O=Netfilter Project/OU=Development/CN=kadlec/emailAddress=kadlec@netfilter.org" Date: Wed, 19 Jan 2005 14:35:44 +0000 Subject: Version 2.1 released: few bugs fixed and nethash type added (see ChangeLog) --- ChangeLog | 14 ++- Makefile | 4 +- ipset.c | 20 ++-- ipset.h | 8 +- ipset_iphash.c | 11 +- ipset_ipmap.c | 27 +++-- ipset_macipmap.c | 16 ++- ipset_nethash.c | 360 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ ipset_portmap.c | 6 +- 9 files changed, 432 insertions(+), 34 deletions(-) create mode 100644 ipset_nethash.c diff --git a/ChangeLog b/ChangeLog index 39a3147..e24b6f4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,15 @@ +2.1 + - Lock debugging used with debugless lock definiton (Piotr Chytla and + others). + - Bindings were not properly filled out at listing (kernel) + - When listing sets from kernel, id was not added to the set structure + (ipset) + - nethash hash type added + - ipset manpage corrections (macipmap) + 2.0.1 - - Cut'n'paste bug at saving macipmap types fixed - Thanks to Vincent Bernat for the bugreport. + - Missing -fPIC in Makefile (Robert Iakobashvili) + - Cut'n'paste bug at saving macipmap types (Vincent Bernat). 2.0 - Chaining of sets are changed: child sets replaced by bindings @@ -9,6 +18,7 @@ - Save and restore functionality implemented - iphash type reworked: clashing resolved by double-hashing and by dynamically growing the set + 1.0 - Renamed to ipset - Rewritten to support child pools diff --git a/Makefile b/Makefile index ce252e4..6f0539b 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ ifndef KERNEL_DIR KERNEL_DIR=/usr/src/linux endif -IPSET_VERSION:=2.0.1 +IPSET_VERSION:=2.1.0 PREFIX:=/usr/local LIBDIR:=$(PREFIX)/lib @@ -23,7 +23,7 @@ RELEASE_DIR:=/tmp COPT_FLAGS:=-O2 CFLAGS:=$(COPT_FLAGS) -Wall -Wunused -I$(KERNEL_DIR)/include -I. #-g -DIPSET_DEBUG #-pg # -DIPTC_DEBUG SH_CFLAGS:=$(CFLAGS) -fPIC -SETTYPES:=ipmap portmap macipmap iphash +SETTYPES:=ipmap portmap macipmap iphash nethash PROGRAMS=ipset SHARED_LIBS=$(foreach T, $(SETTYPES),libipset_$(T).so) diff --git a/ipset.c b/ipset.c index 6f903da..5849892 100644 --- a/ipset.c +++ b/ipset.c @@ -491,6 +491,11 @@ char *ip_tostring(ip_set_ip_t ip, unsigned options) return inet_ntoa(addr); } +char *ip_tostring_numeric(ip_set_ip_t ip) +{ + return ip_tostring(ip, OPT_NUMERIC); +} + /* Fills the 'ip' with the parsed ip or host in host byte order */ void parse_ip(const char *str, ip_set_ip_t * ip) { @@ -770,7 +775,7 @@ static struct set *set_find_byid(ip_set_id_t id) if (set_list[i] && set_list[i]->id == id) { set = set_list[i]; break; - } + } if (set == NULL) exit_error(PARAMETER_PROBLEM, @@ -1007,6 +1012,7 @@ tryagain: set = ipset_malloc(sizeof(struct set)); strcpy(set->name, name_list->name); set->index = name_list->index; + set->id = name_list->id; set->settype = settype_load(name_list->typename); set_list[name_list->index] = set; DP("loaded %s, type %s, index %u", @@ -1028,7 +1034,6 @@ static size_t save_bindings(void *data, size_t offset, size_t len) struct ip_set_hash_save *hash = (struct ip_set_hash_save *) (data + offset); struct set *set; - char * (*printip)(ip_set_ip_t ip, unsigned options); DP("offset %u, len %u", offset, len); if (offset + sizeof(struct ip_set_hash_save) > len) @@ -1039,11 +1044,9 @@ static size_t save_bindings(void *data, size_t offset, size_t len) if (!(set && set_list[hash->binding])) exit_error(OTHER_PROBLEM, "Save binding failed, try again later."); - printip = set->settype->typecode == IPSET_TYPE_IP ? - ip_tostring : port_tostring; printf("-B %s %s -b %s\n", set->name, - printip(hash->ip, OPT_NUMERIC), + set->settype->bindip_tostring(hash->ip, OPT_NUMERIC), set_list[hash->binding]->name); return sizeof(struct ip_set_hash_save); @@ -1579,10 +1582,8 @@ static void set_restore_bind(struct set *set, DP("%s -> %s", adt, binding); if (strcmp(adt, IPSET_TOKEN_DEFAULT) == 0) hash_restore->ip = 0; - else if (set->settype->typecode == IPSET_TYPE_IP) - parse_ip(adt, &hash_restore->ip); else - parse_port(adt, &hash_restore->ip); + set->settype->bindip_parse(adt, &hash_restore->ip); hash_restore->id = set->index; hash_restore->binding = (set_find_byname(binding))->index; DP("id %u, ip %u, binding %u", @@ -1647,8 +1648,7 @@ static size_t print_set(void *data, unsigned options) printf("Bindings:\n"); offset += setlist->members_size; print_bindings(data + offset, setlist->bindings_size, options, - settype->typecode == IPSET_TYPE_IP ? - ip_tostring : port_tostring); + settype->bindip_tostring); printf("\n"); /* One newline between sets */ diff --git a/ipset.h b/ipset.h index e563331..3b41760 100644 --- a/ipset.h +++ b/ipset.h @@ -87,7 +87,6 @@ struct settype { struct settype *next; char typename[IP_SET_MAXNAMELEN]; - char typecode; int protocol_version; @@ -146,6 +145,12 @@ struct settype { /* Print save for all IPs */ void (*saveips) (struct set *set, void *data, size_t len, unsigned options); + /* Conver a single IP (binding) to string */ + char * (*bindip_tostring)(ip_set_ip_t ip, unsigned options); + + /* Parse an IP at restoring bindings. FIXME */ + void (*bindip_parse) (const char *str, ip_set_ip_t * ip); + /* Print usage */ void (*usage) (void); @@ -163,6 +168,7 @@ extern void settype_register(struct settype *settype); extern void exit_error(enum exittype status, char *msg, ...); extern char *ip_tostring(ip_set_ip_t ip, unsigned options); +extern char *ip_tostring_numeric(ip_set_ip_t ip); extern void parse_ip(const char *str, ip_set_ip_t * ip); extern void parse_mask(const char *str, ip_set_ip_t * mask); extern void parse_ipandmask(const char *str, ip_set_ip_t * ip, diff --git a/ipset_iphash.c b/ipset_iphash.c index 8d6cda6..a17efac 100644 --- a/ipset_iphash.c +++ b/ipset_iphash.c @@ -127,11 +127,13 @@ int create_parse(int c, char *argv[], void *data, unsigned *flags) /* Final check; exit if not ok. */ void create_final(void *data, unsigned int flags) { +#ifdef IPSET_DEBUG struct ip_set_req_iphash_create *mydata = (struct ip_set_req_iphash_create *) data; DP("hashsize %u probes %u resize %u", mydata->hashsize, mydata->probes, mydata->resize); +#endif } /* Create commandline options */ @@ -238,7 +240,8 @@ void saveips(struct set *set, void *data, size_t len, unsigned options) while (offset < len) { ip = data + offset; if (*ip) - printf("-A %s %s\n", set->name, ip_tostring(*ip, options)); + printf("-A %s %s\n", set->name, + ip_tostring(*ip, options)); offset += sizeof(ip_set_ip_t); } } @@ -255,7 +258,6 @@ void usage(void) static struct settype settype_iphash = { .typename = SETTYPE_NAME, - .typecode = IPSET_TYPE_IP, .protocol_version = IP_SET_PROTOCOL_VERSION, /* Create */ @@ -277,6 +279,11 @@ static struct settype settype_iphash = { .printips_sorted = &printips, .saveheader = &saveheader, .saveips = &saveips, + + /* Bindings */ + .bindip_tostring = &ip_tostring, + .bindip_parse = &parse_ip, + .usage = &usage, }; diff --git a/ipset_ipmap.c b/ipset_ipmap.c index 7da8f1e..1d8377b 100644 --- a/ipset_ipmap.c +++ b/ipset_ipmap.c @@ -62,7 +62,7 @@ int create_parse(int c, char *argv[], void *data, unsigned *flags) *flags |= OPT_CREATE_FROM; DP("--from %x (%s)", mydata->from, - ip_tostring(mydata->from, 0)); + ip_tostring_numeric(mydata->from)); break; @@ -71,7 +71,8 @@ int create_parse(int c, char *argv[], void *data, unsigned *flags) *flags |= OPT_CREATE_TO; - DP("--to %x (%s)", mydata->to, ip_tostring(mydata->to, 0)); + DP("--to %x (%s)", mydata->to, + ip_tostring_numeric(mydata->to)); break; @@ -84,9 +85,9 @@ int create_parse(int c, char *argv[], void *data, unsigned *flags) *flags |= OPT_CREATE_NETWORK; DP("--network from %x (%s)", mydata->from, - ip_tostring(mydata->from, 0)); + ip_tostring_numeric(mydata->from)); DP("--network to %x (%s)", mydata->to, - ip_tostring(mydata->to, 0)); + ip_tostring_numeric(mydata->to)); break; @@ -152,15 +153,15 @@ void create_final(void *data, unsigned int flags) if ((mydata->from & mydata->netmask) != mydata->from) exit_error(PARAMETER_PROBLEM, "%s is not a network address according to netmask %d\n", - ip_tostring(mydata->from, 0), + ip_tostring_numeric(mydata->from), mask_to_bits(mydata->netmask)); mask = range_to_mask(mydata->from, mydata->to, &mask_bits); if (!mask) exit_error(PARAMETER_PROBLEM, "%s-%s is not a full network\n", - ip_tostring(mydata->from, 0), - ip_tostring(mydata->to, 0)); + ip_tostring_numeric(mydata->from), + ip_tostring_numeric(mydata->to)); netmask_bits = mask_to_bits(mydata->netmask); @@ -168,8 +169,8 @@ void create_final(void *data, unsigned int flags) exit_error(PARAMETER_PROBLEM, "%d netmask specifies larger or equal netblock than %s-%s\n", netmask_bits, - ip_tostring(mydata->from, 0), - ip_tostring(mydata->to, 0)); + ip_tostring_numeric(mydata->from), + ip_tostring_numeric(mydata->to)); } } @@ -191,7 +192,7 @@ ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data) DP("ipmap: %p %p", optarg, data); parse_ip(optarg, &mydata->ip); - DP("%s", ip_tostring(mydata->ip, 0)); + DP("%s", ip_tostring_numeric(mydata->ip)); return mydata->ip; } @@ -302,7 +303,6 @@ void usage(void) static struct settype settype_ipmap = { .typename = SETTYPE_NAME, - .typecode = IPSET_TYPE_IP, .protocol_version = IP_SET_PROTOCOL_VERSION, /* Create */ @@ -324,6 +324,11 @@ static struct settype settype_ipmap = { .printips_sorted = &printips_sorted, .saveheader = &saveheader, .saveips = &saveips, + + /* Bindings */ + .bindip_tostring = &ip_tostring, + .bindip_parse = &parse_ip, + .usage = &usage, }; diff --git a/ipset_macipmap.c b/ipset_macipmap.c index d3b59b0..1a1029e 100644 --- a/ipset_macipmap.c +++ b/ipset_macipmap.c @@ -62,7 +62,7 @@ int create_parse(int c, char *argv[], void *data, unsigned *flags) *flags |= OPT_CREATE_FROM; DP("--from %x (%s)", mydata->from, - ip_tostring(mydata->from, 0)); + ip_tostring_numeric(mydata->from)); break; @@ -71,7 +71,8 @@ int create_parse(int c, char *argv[], void *data, unsigned *flags) *flags |= OPT_CREATE_TO; - DP("--to %x (%s)", mydata->to, ip_tostring(mydata->to, 0)); + DP("--to %x (%s)", mydata->to, + ip_tostring_numeric(mydata->to)); break; @@ -84,9 +85,9 @@ int create_parse(int c, char *argv[], void *data, unsigned *flags) *flags |= OPT_CREATE_NETWORK; DP("--network from %x (%s)", mydata->from, - ip_tostring(mydata->from, 0)); + ip_tostring_numeric(mydata->from)); DP("--network to %x (%s)", mydata->to, - ip_tostring(mydata->to, 0)); + ip_tostring_numeric(mydata->to)); break; @@ -192,6 +193,7 @@ ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data) else memset(mydata->ethernet, 0, ETH_ALEN); + free(saved); return mydata->ip; } @@ -302,7 +304,6 @@ void usage(void) static struct settype settype_macipmap = { .typename = SETTYPE_NAME, - .typecode = IPSET_TYPE_IP, .protocol_version = IP_SET_PROTOCOL_VERSION, /* Create */ @@ -324,6 +325,11 @@ static struct settype settype_macipmap = { .printips_sorted = &printips_sorted, .saveheader = &saveheader, .saveips = &saveips, + + /* Bindings */ + .bindip_tostring = &ip_tostring, + .bindip_parse = &parse_ip, + .usage = &usage, }; diff --git a/ipset_nethash.c b/ipset_nethash.c new file mode 100644 index 0000000..34a7152 --- /dev/null +++ b/ipset_nethash.c @@ -0,0 +1,360 @@ +/* Copyright 2004 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "ipset.h" + +#define BUFLEN 30; + +#define OPT_CREATE_HASHSIZE 0x01U +#define OPT_CREATE_PROBES 0x02U +#define OPT_CREATE_RESIZE 0x04U + +/* Initialize the create. */ +void create_init(void *data) +{ + struct ip_set_req_nethash_create *mydata = + (struct ip_set_req_nethash_create *) data; + + DP("create INIT"); + + /* Default create parameters */ + mydata->hashsize = 1024; + mydata->probes = 2; + mydata->resize = 50; +} + +/* Function which parses command options; returns true if it ate an option */ +int create_parse(int c, char *argv[], void *data, unsigned *flags) +{ + struct ip_set_req_nethash_create *mydata = + (struct ip_set_req_nethash_create *) data; + ip_set_ip_t value; + + DP("create_parse"); + + switch (c) { + case '1': + + if (string_to_number(optarg, 1, UINT_MAX - 1, &mydata->hashsize)) + exit_error(PARAMETER_PROBLEM, "Invalid hashsize `%s' specified", optarg); + + *flags |= OPT_CREATE_HASHSIZE; + + DP("--hashsize %u", mydata->hashsize); + + break; + + case '2': + + if (string_to_number(optarg, 1, 65535, &value)) + exit_error(PARAMETER_PROBLEM, "Invalid probes `%s' specified", optarg); + + mydata->probes = value; + *flags |= OPT_CREATE_PROBES; + + DP("--probes %u", mydata->probes); + + break; + + case '3': + + if (string_to_number(optarg, 0, 65535, &value)) + exit_error(PARAMETER_PROBLEM, "Invalid resize `%s' specified", optarg); + + mydata->resize = value; + *flags |= OPT_CREATE_RESIZE; + + DP("--resize %u", mydata->resize); + + break; + + default: + return 0; + } + + return 1; +} + +/* Final check; exit if not ok. */ +void create_final(void *data, unsigned int flags) +{ +#ifdef IPSET_DEBUG + struct ip_set_req_nethash_create *mydata = + (struct ip_set_req_nethash_create *) data; + + DP("hashsize %u probes %u resize %u", + mydata->hashsize, mydata->probes, mydata->resize); +#endif +} + +/* Create commandline options */ +static struct option create_opts[] = { + {"hashsize", 1, 0, '1'}, + {"probes", 1, 0, '2'}, + {"resize", 1, 0, '3'}, + {0} +}; + +/* Add, del, test parser */ +ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data) +{ + struct ip_set_req_nethash *mydata = + (struct ip_set_req_nethash *) data; + char *saved = strdup(optarg); + char *ptr, *tmp = saved; + ip_set_ip_t cidr; + + ptr = strsep(&tmp, "/"); + + if (tmp == NULL) + exit_error(PARAMETER_PROBLEM, + "Missing cidr from `%s'", optarg); + + if (string_to_number(tmp, 1, 31, &cidr)) + exit_error(PARAMETER_PROBLEM, + "Out of range cidr `%s' specified", optarg); + + mydata->cidr = cidr; + parse_ip(ptr, &mydata->ip); + free(saved); + + return mydata->ip; +}; + +/* + * Print and save + */ + +void initheader(struct set *set, const void *data) +{ + struct ip_set_req_nethash_create *header = + (struct ip_set_req_nethash_create *) data; + struct ip_set_nethash *map = + (struct ip_set_nethash *) set->settype->header; + + memset(map, 0, sizeof(struct ip_set_nethash)); + map->hashsize = header->hashsize; + map->probes = header->probes; + map->resize = header->resize; +} + +unsigned int +mask_to_bits(ip_set_ip_t mask) +{ + unsigned int bits = 32; + ip_set_ip_t maskaddr; + + if (mask == 0xFFFFFFFF) + return bits; + + maskaddr = 0xFFFFFFFE; + while (--bits >= 0 && maskaddr != mask) + maskaddr <<= 1; + + return bits; +} + +void printheader(struct set *set, unsigned options) +{ + struct ip_set_nethash *mysetdata = + (struct ip_set_nethash *) set->settype->header; + + printf(" hashsize: %u", mysetdata->hashsize); + printf(" probes: %u", mysetdata->probes); + printf(" resize: %u\n", mysetdata->resize); +} + +static char buf[20]; + +static char * unpack_ip_tostring(ip_set_ip_t ip, unsigned options) +{ + int i, j = 3; + unsigned char a, b; + + ip = htonl(ip); + for (i = 3; i >= 0; i--) + if (((unsigned char *)&ip)[i] != 0) { + j = i; + break; + } + + a = ((unsigned char *)&ip)[j]; + if (a <= 128) { + a = (a - 1) * 2; + b = 7; + } else if (a <= 192) { + a = (a - 129) * 4; + b = 6; + } else if (a <= 224) { + a = (a - 193) * 8; + b = 5; + } else if (a <= 240) { + a = (a - 225) * 16; + b = 4; + } else if (a <= 248) { + a = (a - 241) * 32; + b = 3; + } else if (a <= 252) { + a = (a - 249) * 64; + b = 2; + } else if (a <= 254) { + a = (a - 253) * 128; + b = 1; + } else { + a = b = 0; + } + ((unsigned char *)&ip)[j] = a; + b += j * 8; + + sprintf(buf, "%u.%u.%u.%u/%u", + ((unsigned char *)&ip)[0], + ((unsigned char *)&ip)[1], + ((unsigned char *)&ip)[2], + ((unsigned char *)&ip)[3], + b); + + DP("%s %s", ip_tostring(htonl(ip), options), buf); + return buf; +} + +void printips(struct set *set, void *data, size_t len, unsigned options) +{ + size_t offset = 0; + ip_set_ip_t *ip; + + while (offset < len) { + ip = data + offset; + if (*ip) + printf("%s\n", unpack_ip_tostring(*ip, options)); + offset += sizeof(ip_set_ip_t); + } +} + +void saveheader(struct set *set, unsigned options) +{ + struct ip_set_nethash *mysetdata = + (struct ip_set_nethash *) set->settype->header; + + printf("-N %s %s --hashsize %u --probes %u --resize %u\n", + set->name, set->settype->typename, + mysetdata->hashsize, mysetdata->probes, mysetdata->resize); +} + +/* Print save for an IP */ +void saveips(struct set *set, void *data, size_t len, unsigned options) +{ + size_t offset = 0; + ip_set_ip_t *ip; + + while (offset < len) { + ip = data + offset; + if (*ip) + printf("-A %s %s\n", set->name, + unpack_ip_tostring(*ip, options)); + offset += sizeof(ip_set_ip_t); + } +} + +static char * net_tostring(ip_set_ip_t ip, unsigned options) +{ + return unpack_ip_tostring(ip, options); +} + +static void parse_net(const char *str, ip_set_ip_t *ip) +{ + char *saved = strdup(str); + char *ptr, *tmp; + ip_set_ip_t cidr; + + ptr = strsep(&tmp, "/"); + + if (tmp == NULL) + exit_error(PARAMETER_PROBLEM, + "Missing cidr from `%s'", str); + + if (string_to_number(tmp, 1, 31, &cidr)) + exit_error(PARAMETER_PROBLEM, + "Out of range cidr `%s' specified", str); + + parse_ip(ptr, ip); + free(saved); + + *ip = pack(*ip, cidr); +} + +void usage(void) +{ + printf + ("-N set nethash [--hashsize hashsize] [--probes probes ]\n" + " [--resize resize]\n" + "-A set IP/cidr\n" + "-D set IP/cidr\n" + "-T set IP/cidr\n"); +} + +static struct settype settype_nethash = { + .typename = SETTYPE_NAME, + .protocol_version = IP_SET_PROTOCOL_VERSION, + + /* Create */ + .create_size = sizeof(struct ip_set_req_nethash_create), + .create_init = &create_init, + .create_parse = &create_parse, + .create_final = &create_final, + .create_opts = create_opts, + + /* Add/del/test */ + .adt_size = sizeof(struct ip_set_req_nethash), + .adt_parser = &adt_parser, + + /* Printing */ + .header_size = sizeof(struct ip_set_nethash), + .initheader = &initheader, + .printheader = &printheader, + .printips = &printips, /* We only have the unsorted version */ + .printips_sorted = &printips, + .saveheader = &saveheader, + .saveips = &saveips, + + /* Bindings */ + .bindip_tostring = &net_tostring, + .bindip_parse = &parse_net, + + .usage = &usage, +}; + +void _init(void) +{ + settype_register(&settype_nethash); + +} diff --git a/ipset_portmap.c b/ipset_portmap.c index ba9e651..df08574 100644 --- a/ipset_portmap.c +++ b/ipset_portmap.c @@ -204,7 +204,6 @@ void usage(void) static struct settype settype_portmap = { .typename = SETTYPE_NAME, - .typecode = IPSET_TYPE_PORT, .protocol_version = IP_SET_PROTOCOL_VERSION, /* Create */ @@ -226,6 +225,11 @@ static struct settype settype_portmap = { .printips_sorted = &printports_sorted, .saveheader = &saveheader, .saveips = &saveports, + + /* Bindings */ + .bindip_tostring = &port_tostring, + .bindip_parse = &parse_port, + .usage = &usage, }; -- cgit v1.2.3