blob: 5c762b22c7ff4f0d952f3b4d218607c92a181b0a [file] [log] [blame]
Bart De Schuymerff587202005-02-08 20:02:28 +00001/* ebt_ulog
2 *
3 * Authors:
4 * Bart De Schuymer <bdschuym@pandora.be>
5 *
6 * November, 2004
7 */
8
Bart De Schuymerb78a7e82004-11-20 13:05:22 +00009#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'
27static 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
37static 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
51static 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
66static 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 Schuymerff587202005-02-08 20:02:28 +000076 if (ebt_check_inverse2(optarg))
Bart De Schuymerb78a7e82004-11-20 13:05:22 +000077 goto inverse_invalid;
Bart De Schuymerff587202005-02-08 20:02:28 +000078 ebt_check_option2(flags, OPT_PREFIX);
Bart De Schuymerb78a7e82004-11-20 13:05:22 +000079 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 Schuymerff587202005-02-08 20:02:28 +000085 if (ebt_check_inverse2(optarg))
Bart De Schuymerb78a7e82004-11-20 13:05:22 +000086 goto inverse_invalid;
Bart De Schuymerff587202005-02-08 20:02:28 +000087 ebt_check_option2(flags, OPT_NLGROUP);
Bart De Schuymerb78a7e82004-11-20 13:05:22 +000088 i = strtoul(optarg, &end, 10);
89 if (*end != '\0')
Bart De Schuymerff587202005-02-08 20:02:28 +000090 ebt_print_error2("Problem with ulog-nlgroup: %s", optarg);
Bart De Schuymerb78a7e82004-11-20 13:05:22 +000091 if (i < 1 || i > EBT_ULOG_MAXNLGROUPS)
Bart De Schuymerff587202005-02-08 20:02:28 +000092 ebt_print_error2("the ulog-nlgroup number must be between 1 and 32");
Bart De Schuymerb78a7e82004-11-20 13:05:22 +000093 uloginfo->nlgroup = i - 1;
94 break;
95
96 case ULOG_CPRANGE:
Bart De Schuymerff587202005-02-08 20:02:28 +000097 if (ebt_check_inverse2(optarg))
Bart De Schuymerb78a7e82004-11-20 13:05:22 +000098 goto inverse_invalid;
Bart De Schuymerff587202005-02-08 20:02:28 +000099 ebt_check_option2(flags, OPT_CPRANGE);
Bart De Schuymerb78a7e82004-11-20 13:05:22 +0000100 i = strtoul(optarg, &end, 10);
101 if (*end != '\0') {
102 if (strcasecmp(optarg, CP_NO_LIMIT_S))
Bart De Schuymerff587202005-02-08 20:02:28 +0000103 ebt_print_error2("Problem with ulog-cprange: %s", optarg);
Bart De Schuymerb78a7e82004-11-20 13:05:22 +0000104 i = CP_NO_LIMIT_N;
105 }
106 uloginfo->cprange = i;
107 break;
108
109 case ULOG_QTHRESHOLD:
Bart De Schuymerff587202005-02-08 20:02:28 +0000110 if (ebt_check_inverse2(optarg))
Bart De Schuymerb78a7e82004-11-20 13:05:22 +0000111 goto inverse_invalid;
Bart De Schuymerff587202005-02-08 20:02:28 +0000112 ebt_check_option2(flags, OPT_QTHRESHOLD);
Bart De Schuymerb78a7e82004-11-20 13:05:22 +0000113 i = strtoul(optarg, &end, 10);
114 if (*end != '\0')
Bart De Schuymerff587202005-02-08 20:02:28 +0000115 ebt_print_error2("Problem with ulog-qthreshold: %s", optarg);
Bart De Schuymerb78a7e82004-11-20 13:05:22 +0000116 if (i > EBT_ULOG_MAX_QLEN)
Bart De Schuymerff587202005-02-08 20:02:28 +0000117 ebt_print_error2("ulog-qthreshold argument %d exceeds the maximum of %d", i, EBT_ULOG_MAX_QLEN);
Bart De Schuymerb78a7e82004-11-20 13:05:22 +0000118 uloginfo->qthreshold = i;
119 break;
120 case ULOG_ULOG:
Bart De Schuymerff587202005-02-08 20:02:28 +0000121 if (ebt_check_inverse2(optarg))
Bart De Schuymerb78a7e82004-11-20 13:05:22 +0000122 goto inverse_invalid;
Bart De Schuymerff587202005-02-08 20:02:28 +0000123 ebt_check_option2(flags, OPT_ULOG);
Bart De Schuymerb78a7e82004-11-20 13:05:22 +0000124 break;
125
126 default:
127 return 0;
128 }
129 return 1;
130
131inverse_invalid:
132 ebt_print_error("The use of '!' makes no sense for the ulog watcher");
Bart De Schuymerff587202005-02-08 20:02:28 +0000133 return 1;
Bart De Schuymerb78a7e82004-11-20 13:05:22 +0000134}
135
136static 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 Schuymerb78a7e82004-11-20 13:05:22 +0000140}
141
142static 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
156static 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
170static struct ebt_u_watcher ulog_watcher =
171{
172 .name = EBT_ULOG_WATCHER,
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
183void _init(void)
184{
185 ebt_register_watcher(&ulog_watcher);
186}