Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 1 | /* ebt_ulog |
| 2 | * |
| 3 | * Authors: |
| 4 | * Bart De Schuymer <bdschuym@pandora.be> |
| 5 | * |
| 6 | * November, 2004 |
| 7 | */ |
| 8 | |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 9 | #define __need_time_t |
| 10 | #define __need_suseconds_t |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <getopt.h> |
| 15 | #include "../include/ebtables_u.h" |
| 16 | #include <sys/time.h> |
| 17 | #include <linux/netfilter_bridge/ebt_ulog.h> |
| 18 | |
| 19 | #define CP_NO_LIMIT_S "default_cprange" |
| 20 | #define CP_NO_LIMIT_N 0 |
| 21 | |
| 22 | #define ULOG_PREFIX '1' |
| 23 | #define ULOG_NLGROUP '2' |
| 24 | #define ULOG_CPRANGE '3' |
| 25 | #define ULOG_QTHRESHOLD '4' |
| 26 | #define ULOG_ULOG '5' |
| 27 | static struct option opts[] = |
| 28 | { |
| 29 | { "ulog-prefix" , required_argument, 0, ULOG_PREFIX }, |
| 30 | { "ulog-nlgroup" , required_argument, 0, ULOG_NLGROUP }, |
| 31 | { "ulog-cprange" , required_argument, 0, ULOG_CPRANGE }, |
| 32 | { "ulog-qthreshold", required_argument, 0, ULOG_QTHRESHOLD }, |
| 33 | { "ulog" , no_argument , 0, ULOG_ULOG }, |
| 34 | { 0 } |
| 35 | }; |
| 36 | |
| 37 | static void print_help() |
| 38 | { |
| 39 | printf( |
| 40 | "ulog options:\n" |
| 41 | "--ulog : use the default ulog parameters\n" |
| 42 | "--ulog-prefix prefix : max %d characters (default is no prefix)\n" |
| 43 | "--ulog-nlgroup group : 0 < group number < %d (default = %d)\n" |
| 44 | "--ulog-cprange range : max copy range (default is " CP_NO_LIMIT_S ")\n" |
| 45 | "--ulog-qthreshold : 0 < queueing threshold < %d (default = %d)\n", |
| 46 | EBT_ULOG_PREFIX_LEN - 1, EBT_ULOG_MAXNLGROUPS + 1, |
| 47 | EBT_ULOG_DEFAULT_NLGROUP + 1, EBT_ULOG_MAX_QLEN + 1, |
| 48 | EBT_ULOG_DEFAULT_QTHRESHOLD); |
| 49 | } |
| 50 | |
| 51 | static void init(struct ebt_entry_watcher *watcher) |
| 52 | { |
| 53 | struct ebt_ulog_info *uloginfo = (struct ebt_ulog_info *)watcher->data; |
| 54 | |
| 55 | uloginfo->prefix[0] = '\0'; |
| 56 | uloginfo->nlgroup = EBT_ULOG_DEFAULT_NLGROUP; |
| 57 | uloginfo->cprange = CP_NO_LIMIT_N; /* Use default netlink buffer size */ |
| 58 | uloginfo->qthreshold = EBT_ULOG_DEFAULT_QTHRESHOLD; |
| 59 | } |
| 60 | |
| 61 | #define OPT_PREFIX 0x01 |
| 62 | #define OPT_NLGROUP 0x02 |
| 63 | #define OPT_CPRANGE 0x04 |
| 64 | #define OPT_QTHRESHOLD 0x08 |
| 65 | #define OPT_ULOG 0x10 |
| 66 | static int parse(int c, char **argv, int argc, const struct ebt_u_entry *entry, |
| 67 | unsigned int *flags, struct ebt_entry_watcher **watcher) |
| 68 | { |
| 69 | struct ebt_ulog_info *uloginfo; |
| 70 | unsigned int i; |
| 71 | char *end; |
| 72 | |
| 73 | uloginfo = (struct ebt_ulog_info *)(*watcher)->data; |
| 74 | switch (c) { |
| 75 | case ULOG_PREFIX: |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 76 | if (ebt_check_inverse2(optarg)) |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 77 | goto inverse_invalid; |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 78 | ebt_check_option2(flags, OPT_PREFIX); |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 79 | if (strlen(optarg) > EBT_ULOG_PREFIX_LEN - 1) |
| 80 | ebt_print_error("Prefix too long for ulog-prefix"); |
| 81 | strcpy(uloginfo->prefix, optarg); |
| 82 | break; |
| 83 | |
| 84 | case ULOG_NLGROUP: |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 85 | if (ebt_check_inverse2(optarg)) |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 86 | goto inverse_invalid; |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 87 | ebt_check_option2(flags, OPT_NLGROUP); |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 88 | i = strtoul(optarg, &end, 10); |
| 89 | if (*end != '\0') |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 90 | ebt_print_error2("Problem with ulog-nlgroup: %s", optarg); |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 91 | if (i < 1 || i > EBT_ULOG_MAXNLGROUPS) |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 92 | ebt_print_error2("the ulog-nlgroup number must be between 1 and 32"); |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 93 | uloginfo->nlgroup = i - 1; |
| 94 | break; |
| 95 | |
| 96 | case ULOG_CPRANGE: |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 97 | if (ebt_check_inverse2(optarg)) |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 98 | goto inverse_invalid; |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 99 | ebt_check_option2(flags, OPT_CPRANGE); |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 100 | i = strtoul(optarg, &end, 10); |
| 101 | if (*end != '\0') { |
| 102 | if (strcasecmp(optarg, CP_NO_LIMIT_S)) |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 103 | ebt_print_error2("Problem with ulog-cprange: %s", optarg); |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 104 | i = CP_NO_LIMIT_N; |
| 105 | } |
| 106 | uloginfo->cprange = i; |
| 107 | break; |
| 108 | |
| 109 | case ULOG_QTHRESHOLD: |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 110 | if (ebt_check_inverse2(optarg)) |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 111 | goto inverse_invalid; |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 112 | ebt_check_option2(flags, OPT_QTHRESHOLD); |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 113 | i = strtoul(optarg, &end, 10); |
| 114 | if (*end != '\0') |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 115 | ebt_print_error2("Problem with ulog-qthreshold: %s", optarg); |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 116 | if (i > EBT_ULOG_MAX_QLEN) |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 117 | ebt_print_error2("ulog-qthreshold argument %d exceeds the maximum of %d", i, EBT_ULOG_MAX_QLEN); |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 118 | uloginfo->qthreshold = i; |
| 119 | break; |
| 120 | case ULOG_ULOG: |
Bart De Schuymer | d3a05a7 | 2005-07-24 09:17:41 +0000 | [diff] [blame] | 121 | if (ebt_check_inverse(optarg)) |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 122 | goto inverse_invalid; |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 123 | ebt_check_option2(flags, OPT_ULOG); |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 124 | break; |
| 125 | |
| 126 | default: |
| 127 | return 0; |
| 128 | } |
| 129 | return 1; |
| 130 | |
| 131 | inverse_invalid: |
| 132 | ebt_print_error("The use of '!' makes no sense for the ulog watcher"); |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame] | 133 | return 1; |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static void final_check(const struct ebt_u_entry *entry, |
| 137 | const struct ebt_entry_watcher *watcher, const char *name, |
| 138 | unsigned int hookmask, unsigned int time) |
| 139 | { |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static void print(const struct ebt_u_entry *entry, |
| 143 | const struct ebt_entry_watcher *watcher) |
| 144 | { |
| 145 | struct ebt_ulog_info *uloginfo = (struct ebt_ulog_info *)watcher->data; |
| 146 | |
| 147 | printf("--ulog-prefix \"%s\" --ulog-nlgroup %d --ulog-cprange ", |
| 148 | uloginfo->prefix, uloginfo->nlgroup + 1); |
| 149 | if (uloginfo->cprange == CP_NO_LIMIT_N) |
| 150 | printf(CP_NO_LIMIT_S); |
| 151 | else |
| 152 | printf("%d", uloginfo->cprange); |
| 153 | printf(" --ulog-qthreshold %d ", uloginfo->qthreshold); |
| 154 | } |
| 155 | |
| 156 | static int compare(const struct ebt_entry_watcher *w1, |
| 157 | const struct ebt_entry_watcher *w2) |
| 158 | { |
| 159 | struct ebt_ulog_info *uloginfo1 = (struct ebt_ulog_info *)w1->data; |
| 160 | struct ebt_ulog_info *uloginfo2 = (struct ebt_ulog_info *)w2->data; |
| 161 | |
| 162 | if (uloginfo1->nlgroup != uloginfo2->nlgroup || |
| 163 | uloginfo1->cprange != uloginfo2->cprange || |
| 164 | uloginfo1->qthreshold != uloginfo2->qthreshold || |
| 165 | strcmp(uloginfo1->prefix, uloginfo2->prefix)) |
| 166 | return 0; |
| 167 | return 1; |
| 168 | } |
| 169 | |
| 170 | static struct ebt_u_watcher ulog_watcher = |
| 171 | { |
Bart De Schuymer | fbdebad | 2008-02-03 19:58:44 +0000 | [diff] [blame] | 172 | .name = "ulog", |
Bart De Schuymer | b78a7e8 | 2004-11-20 13:05:22 +0000 | [diff] [blame] | 173 | .size = sizeof(struct ebt_ulog_info), |
| 174 | .help = print_help, |
| 175 | .init = init, |
| 176 | .parse = parse, |
| 177 | .final_check = final_check, |
| 178 | .print = print, |
| 179 | .compare = compare, |
| 180 | .extra_ops = opts, |
| 181 | }; |
| 182 | |
| 183 | void _init(void) |
| 184 | { |
| 185 | ebt_register_watcher(&ulog_watcher); |
| 186 | } |