1 /* Shared library add-on to iptables to add ULOG support.
3 * (C) 2000 by Harald Welte <laforge@gnumonks.org>
5 * multipart netlink support based on ideas by Sebastian Zander
6 * <zander@fokus.gmd.de>
8 * This software is released under the terms of GNU GPL
10 * libipt_ULOG.c,v 1.7 2001/01/30 11:55:02 laforge Exp
19 #include <linux/netfilter_ipv4/ip_tables.h>
20 /* For 64bit kernel / 32bit userspace */
21 #include "../include/linux/netfilter_ipv4/ipt_ULOG.h"
24 void print_groups(unsigned int gmask)
29 for (b = 31; b >= 0; b--) {
36 /* Function which prints out usage message. */
37 static void help(void)
39 printf("ULOG v%s options:\n"
40 " --ulog-nlgroup nlgroup NETLINK group used for logging\n"
41 " --ulog-cprange size Bytes of each packet to be passed\n"
42 " --ulog-qthreshold Threshold of in-kernel queue\n"
43 " --ulog-prefix prefix Prefix log messages with this prefix.\n\n",
47 static struct option opts[] = {
48 {"ulog-nlgroup", 1, 0, '!'},
49 {"ulog-prefix", 1, 0, '#'},
50 {"ulog-cprange", 1, 0, 'A'},
51 {"ulog-qthreshold", 1, 0, 'B'},
55 /* Initialize the target. */
56 static void init(struct ipt_entry_target *t, unsigned int *nfcache)
58 struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) t->data;
60 loginfo->nl_group = ULOG_DEFAULT_NLGROUP;
61 loginfo->qthreshold = ULOG_DEFAULT_QTHRESHOLD;
65 #define IPT_LOG_OPT_NLGROUP 0x01
66 #define IPT_LOG_OPT_PREFIX 0x02
67 #define IPT_LOG_OPT_CPRANGE 0x04
68 #define IPT_LOG_OPT_QTHRESHOLD 0x08
70 /* Function which parses command options; returns true if it
72 static int parse(int c, char **argv, int invert, unsigned int *flags,
73 const struct ipt_entry *entry,
74 struct ipt_entry_target **target)
76 struct ipt_ulog_info *loginfo =
77 (struct ipt_ulog_info *) (*target)->data;
82 if (*flags & IPT_LOG_OPT_NLGROUP)
83 exit_error(PARAMETER_PROBLEM,
84 "Can't specify --ulog-nlgroup twice");
86 if (check_inverse(optarg, &invert, NULL, 0))
87 exit_error(PARAMETER_PROBLEM,
88 "Unexpected `!' after --ulog-nlgroup");
89 group_d = atoi(optarg);
90 if (group_d > 32 || group_d < 1)
91 exit_error(PARAMETER_PROBLEM,
92 "--ulog-nlgroup has to be between 1 and 32");
94 loginfo->nl_group = (1 << (group_d - 1));
96 *flags |= IPT_LOG_OPT_NLGROUP;
100 if (*flags & IPT_LOG_OPT_PREFIX)
101 exit_error(PARAMETER_PROBLEM,
102 "Can't specify --ulog-prefix twice");
104 if (check_inverse(optarg, &invert, NULL, 0))
105 exit_error(PARAMETER_PROBLEM,
106 "Unexpected `!' after --ulog-prefix");
108 if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
109 exit_error(PARAMETER_PROBLEM,
110 "Maximum prefix length %u for --ulog-prefix",
111 (unsigned int)sizeof(loginfo->prefix) - 1);
113 strcpy(loginfo->prefix, optarg);
114 *flags |= IPT_LOG_OPT_PREFIX;
117 if (*flags & IPT_LOG_OPT_CPRANGE)
118 exit_error(PARAMETER_PROBLEM,
119 "Can't specify --ulog-cprange twice");
120 if (atoi(optarg) < 0)
121 exit_error(PARAMETER_PROBLEM,
122 "Negative copy range?");
123 #ifdef KERNEL_64_USERSPACE_32
124 loginfo->copy_range = (unsigned long long)atoll(optarg);
126 loginfo->copy_range = atoi(optarg);
128 *flags |= IPT_LOG_OPT_CPRANGE;
131 if (*flags & IPT_LOG_OPT_QTHRESHOLD)
132 exit_error(PARAMETER_PROBLEM,
133 "Can't specify --ulog-qthreshold twice");
134 if (atoi(optarg) < 1)
135 exit_error(PARAMETER_PROBLEM,
136 "Negative or zero queue threshold ?");
137 if (atoi(optarg) > ULOG_MAX_QLEN)
138 exit_error(PARAMETER_PROBLEM,
139 "Maximum queue length exceeded");
140 #ifdef KERNEL_64_USERSPACE_32
141 loginfo->qthreshold = (unsigned long long)atoll(optarg);
143 loginfo->qthreshold = atoi(optarg);
145 *flags |= IPT_LOG_OPT_QTHRESHOLD;
153 /* Final check; nothing. */
154 static void final_check(unsigned int flags)
158 /* Saves the union ipt_targinfo in parsable form to stdout. */
159 static void save(const struct ipt_ip *ip,
160 const struct ipt_entry_target *target)
162 const struct ipt_ulog_info *loginfo
163 = (const struct ipt_ulog_info *) target->data;
165 if (strcmp(loginfo->prefix, "") != 0)
166 printf("--ulog-prefix \"%s\" ", loginfo->prefix);
168 if (loginfo->nl_group != ULOG_DEFAULT_NLGROUP) {
169 printf("--ulog-nlgroup ");
170 print_groups(loginfo->nl_group);
172 #ifdef KERNEL_64_USERSPACE_32
173 if (loginfo->copy_range)
174 printf("--ulog-cprange %llu ", loginfo->copy_range);
176 if (loginfo->qthreshold != ULOG_DEFAULT_QTHRESHOLD)
177 printf("--ulog-qthreshold %llu ", loginfo->qthreshold);
179 if (loginfo->copy_range)
180 printf("--ulog-cprange %u ", (unsigned int)loginfo->copy_range);
182 if (loginfo->qthreshold != ULOG_DEFAULT_QTHRESHOLD)
183 printf("--ulog-qthreshold %u ", (unsigned int)loginfo->qthreshold);
187 /* Prints out the targinfo. */
189 print(const struct ipt_ip *ip,
190 const struct ipt_entry_target *target, int numeric)
192 const struct ipt_ulog_info *loginfo
193 = (const struct ipt_ulog_info *) target->data;
196 #ifdef KERNEL_64_USERSPACE_32
197 printf("copy_range %llu nlgroup ", loginfo->copy_range);
199 printf("copy_range %u nlgroup ", (unsigned int)loginfo->copy_range);
201 print_groups(loginfo->nl_group);
202 if (strcmp(loginfo->prefix, "") != 0)
203 printf("prefix `%s' ", loginfo->prefix);
204 #ifdef KERNEL_64_USERSPACE_32
205 printf("queue_threshold %llu ", loginfo->qthreshold);
207 printf("queue_threshold %u ", (unsigned int)loginfo->qthreshold);
211 static struct iptables_target ulog = {
214 .version = IPTABLES_VERSION,
215 .size = IPT_ALIGN(sizeof(struct ipt_ulog_info)),
216 .userspacesize = IPT_ALIGN(sizeof(struct ipt_ulog_info)),
220 .final_check = &final_check,
228 register_target(&ulog);