From 08663950b87022ebcf0af69d7cc9075f2046434f Mon Sep 17 00:00:00 2001 From: laforge Date: Thu, 8 Nov 2001 22:35:03 +0000 Subject: add new recent match to patch-o-matic (Stephen Frost) --- extensions/.recent-test | 3 + extensions/libipt_recent.c | 178 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100755 extensions/.recent-test create mode 100644 extensions/libipt_recent.c diff --git a/extensions/.recent-test b/extensions/.recent-test new file mode 100755 index 0000000..2a47fc9 --- /dev/null +++ b/extensions/.recent-test @@ -0,0 +1,3 @@ +#!/bin/sh +# True if recent match patch is applied. +[ -f $KERNEL_DIR/include/linux/netfilter_ipv4/ipt_recent.h ] && echo recent diff --git a/extensions/libipt_recent.c b/extensions/libipt_recent.c new file mode 100644 index 0000000..8c4a1a3 --- /dev/null +++ b/extensions/libipt_recent.c @@ -0,0 +1,178 @@ +/* Shared library add-on to iptables to add recent matching support. */ +#include +#include +#include +#include +#include + +#include +#include + +/* Function which prints out usage message. */ +static void +help(void) +{ + printf( +"recent v%s options:\n" +"[!] --set Add source address to list, always matches.\n" +"[!] --rcheck Match if source address in list.\n" +"[!] --update Match if source address in list, also update last-seen time.\n" +"[!] --remove Match if source address in list, also removes that address from list.\n" +" --seconds seconds For check and update commands above.\n" +" Specifies that the match will only occur if source address last seen within\n" +" the last 'seconds' seconds.\n" +" --hitcount hits For check and update commands above.\n" +" Specifies that the match will only occur if source address seen hits times.\n" +" May be used in conjunction with the seconds option.\n", +NETFILTER_VERSION); + +} + +static struct option opts[] = { + { "set", 0, 0, 201 }, + { "rcheck", 0, 0, 202 }, + { "update", 0, 0, 203 }, + { "seconds", 1, 0, 204 }, + { "hitcount", 1, 0, 205 }, + { "remove",0, 0, 206 }, + {0} +}; + +/* Initialize the match. */ +static void +init(struct ipt_entry_match *m, unsigned int *nfcache) +{ + *nfcache |= NFC_UNKNOWN; +} + +/* Function which parses command options; returns true if it + ate an option */ +static int +parse(int c, char **argv, int invert, unsigned int *flags, + const struct ipt_entry *entry, + unsigned int *nfcache, + struct ipt_entry_match **match) +{ + struct ipt_recent_info *info = (struct ipt_recent_info *)(*match)->data; + + switch (c) { + case 201: + if (*flags) exit_error(PARAMETER_PROBLEM, + "recent: only one of `--set', `--check' " + "`--update' or `--remove' may be set"); + if (check_inverse(optarg, &invert)) optind++; + info->check_set |= IPT_RECENT_SET; + if (invert) info->invert = 1; + *flags = 1; + break; + + case 202: + if (*flags) exit_error(PARAMETER_PROBLEM, + "recent: only one of `--set', `--check' " + "`--update' or `--remove' may be set"); + if (check_inverse(optarg, &invert)) optind++; + info->check_set |= IPT_RECENT_CHECK; + if(invert) info->invert = 1; + *flags = 1; + break; + + case 203: + if (*flags) exit_error(PARAMETER_PROBLEM, + "recent: only one of `--set', `--check' " + "`--update' or `--remove' may be set"); + if (check_inverse(optarg, &invert)) optind++; + info->check_set |= IPT_RECENT_UPDATE; + if (invert) info->invert = 1; + *flags = 1; + break; + + case 206: + if (*flags) exit_error(PARAMETER_PROBLEM, + "recent: only one of `--set', `--check' " + "`--update' or `--remove' may be set"); + if (check_inverse(optarg, &invert)) optind++; + info->check_set |= IPT_RECENT_REMOVE; + if (invert) info->invert = 1; + *flags = 1; + break; + + case 204: + info->seconds = atoi(optarg); + break; + + case 205: + info->hit_count = atoi(optarg); + break; + + default: + return 0; + } + return 1; +} + +/* Final check; must have specified a specific option. */ +static void +final_check(unsigned int flags) +{ + if (!flags) + exit_error(PARAMETER_PROBLEM, + "recent: you must specify one of `--set', `--check' " + "`--update' or `--remove'"); +} + +/* Prints out the matchinfo. */ +static void +print(const struct ipt_ip *ip, + const struct ipt_entry_match *match, + int numeric) +{ + struct ipt_recent_info *info = (struct ipt_recent_info *)match->data; + + if (info->invert) fputc('!', stdout); + + printf("recent: "); + if(info->check_set & IPT_RECENT_SET) printf("SET "); + if(info->check_set & IPT_RECENT_CHECK) printf("CHECK "); + if(info->check_set & IPT_RECENT_UPDATE) printf("UPDATE "); + if(info->check_set & IPT_RECENT_REMOVE) printf("REMOVE "); + if(info->seconds) printf("seconds: %d",info->seconds); + if(info->hit_count) printf("hit_count: %d",info->hit_count); +} + +/* Saves the union ipt_matchinfo in parsable form to stdout. */ +static void +save(const struct ipt_ip *ip, const struct ipt_entry_match *match) +{ + struct ipt_recent_info *info = (struct ipt_recent_info *)match; + + if (info->invert) fputc('!', stdout); + + printf("recent: "); + if(info->check_set & IPT_RECENT_SET) printf("SET "); + if(info->check_set & IPT_RECENT_CHECK) printf("CHECK "); + if(info->check_set & IPT_RECENT_UPDATE) printf("UPDATE "); + if(info->check_set & IPT_RECENT_REMOVE) printf("REMOVE "); + if(info->seconds) printf("seconds: "); + if(info->hit_count) printf("hit_count: "); +} + +static +struct iptables_match recent += { NULL, + "recent", + NETFILTER_VERSION, + IPT_ALIGN(sizeof(struct ipt_recent_info)), + IPT_ALIGN(sizeof(struct ipt_recent_info)), + &help, + &init, + &parse, + &final_check, + &print, + &save, + opts +}; + +void _init(void) +{ + register_match(&recent); +} -- cgit v1.2.3