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 #include <linux/netfilter_ipv4/ipt_ULOG.h>
22 #define ULOG_DEFAULT_NLGROUP 1
23 #define ULOG_DEFAULT_QTHRESHOLD 1
26 void print_groups(unsigned int gmask)
31 for (b = 31; b >= 0; b--) {
38 /* Function which prints out usage message. */
39 static void help(void)
41 printf("ULOG v%s options:\n"
42 " --ulog-nlgroup nlgroup NETLINK group used for logging\n"
43 " --ulog-cprange size Bytes of each packet to be passed\n"
44 " --ulog-qthreshold Threshold of in-kernel queue\n"
45 " --ulog-prefix prefix Prefix log messages with this prefix.\n\n",
49 static struct option opts[] = {
50 {"ulog-nlgroup", 1, 0, '!'},
51 {"ulog-prefix", 1, 0, '#'},
52 {"ulog-cprange", 1, 0, 'A'},
53 {"ulog-qthreshold", 1, 0, 'B'},
57 /* Initialize the target. */
58 static void init(struct ipt_entry_target *t, unsigned int *nfcache)
60 struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) t->data;
62 loginfo->nl_group = ULOG_DEFAULT_NLGROUP;
63 loginfo->qthreshold = ULOG_DEFAULT_QTHRESHOLD;
65 /* Can't cache this */
66 *nfcache |= NFC_UNKNOWN;
69 #define IPT_LOG_OPT_NLGROUP 0x01
70 #define IPT_LOG_OPT_PREFIX 0x02
71 #define IPT_LOG_OPT_CPRANGE 0x04
72 #define IPT_LOG_OPT_QTHRESHOLD 0x08
74 /* Function which parses command options; returns true if it
76 static int parse(int c, char **argv, int invert, unsigned int *flags,
77 const struct ipt_entry *entry,
78 struct ipt_entry_target **target)
80 struct ipt_ulog_info *loginfo =
81 (struct ipt_ulog_info *) (*target)->data;
86 if (*flags & IPT_LOG_OPT_NLGROUP)
87 exit_error(PARAMETER_PROBLEM,
88 "Can't specify --ulog-nlgroup twice");
90 if (check_inverse(optarg, &invert, NULL, 0))
91 exit_error(PARAMETER_PROBLEM,
92 "Unexpected `!' after --ulog-nlgroup");
93 group_d = atoi(optarg);
94 if (group_d > 32 || group_d < 1)
95 exit_error(PARAMETER_PROBLEM,
96 "--ulog-nlgroup has to be between 1 and 32");
98 loginfo->nl_group = (1 << (group_d - 1));
100 *flags |= IPT_LOG_OPT_NLGROUP;
104 if (*flags & IPT_LOG_OPT_PREFIX)
105 exit_error(PARAMETER_PROBLEM,
106 "Can't specify --ulog-prefix twice");
108 if (check_inverse(optarg, &invert, NULL, 0))
109 exit_error(PARAMETER_PROBLEM,
110 "Unexpected `!' after --ulog-prefix");
112 if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
113 exit_error(PARAMETER_PROBLEM,
114 "Maximum prefix length %u for --ulog-prefix",
115 sizeof(loginfo->prefix) - 1);
117 strcpy(loginfo->prefix, optarg);
118 *flags |= IPT_LOG_OPT_PREFIX;
121 if (*flags & IPT_LOG_OPT_CPRANGE)
122 exit_error(PARAMETER_PROBLEM,
123 "Can't specify --ulog-cprange twice");
124 if (atoi(optarg) < 0)
125 exit_error(PARAMETER_PROBLEM,
126 "Negative copy range?");
127 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 loginfo->qthreshold = atoi(optarg);
141 *flags |= IPT_LOG_OPT_QTHRESHOLD;
147 /* Final check; nothing. */
148 static void final_check(unsigned int flags)
152 /* Saves the union ipt_targinfo in parsable form to stdout. */
153 static void save(const struct ipt_ip *ip,
154 const struct ipt_entry_target *target)
156 const struct ipt_ulog_info *loginfo
157 = (const struct ipt_ulog_info *) target->data;
159 if (strcmp(loginfo->prefix, "") != 0)
160 printf("--ulog-prefix \"%s\" ", loginfo->prefix);
162 if (loginfo->nl_group != ULOG_DEFAULT_NLGROUP) {
163 printf("--ulog-nlgroup ");
164 print_groups(loginfo->nl_group);
166 if (loginfo->copy_range)
167 printf("--ulog-cprange %d ", loginfo->copy_range);
169 if (loginfo->qthreshold != ULOG_DEFAULT_QTHRESHOLD)
170 printf("--ulog-qthreshold %d ", loginfo->qthreshold);
173 /* Prints out the targinfo. */
175 print(const struct ipt_ip *ip,
176 const struct ipt_entry_target *target, int numeric)
178 const struct ipt_ulog_info *loginfo
179 = (const struct ipt_ulog_info *) target->data;
182 printf("copy_range %d nlgroup ", loginfo->copy_range);
183 print_groups(loginfo->nl_group);
184 if (strcmp(loginfo->prefix, "") != 0)
185 printf("prefix `%s' ", loginfo->prefix);
186 printf("queue_threshold %d ", loginfo->qthreshold);
190 struct iptables_target ulog = { NULL,
193 IPT_ALIGN(sizeof(struct ipt_ulog_info)),
194 IPT_ALIGN(sizeof(struct ipt_ulog_info)),
206 register_target(&ulog);