blob: 11b4f46c8b3de2090e2ba8abb2bf141571e0e02d [file] [log] [blame]
Harald Welte015dffb2000-07-31 23:38:14 +00001/* Shared library add-on to iptables to add ULOG support.
2 *
3 * (C) 2000 by Harald Welte <laforge@sunbeam.franken.de>
4 *
5 * This software is released under the terms of GNU GPL
6 *
7 * libipt_ULOG.c,v 1.3 2000/07/31 11:51:50 laforge Exp
8 */
Harald Weltec5bdb402000-07-31 14:24:57 +00009#include <stdio.h>
10#include <netdb.h>
11#include <string.h>
12#include <stdlib.h>
13#include <syslog.h>
14#include <getopt.h>
15#include <iptables.h>
16#include <linux/netfilter_ipv4/ip_tables.h>
17#include <linux/netfilter_ipv4/ipt_ULOG.h>
18
19#define ULOG_DEFAULT_NLGROUP 1
20
21
22void print_groups(unsigned int gmask)
23{
24 int b;
25 unsigned int test;
26
Harald Welte015dffb2000-07-31 23:38:14 +000027 for (b = 31; b >= 0; b--) {
Harald Weltec5bdb402000-07-31 14:24:57 +000028 test = (1 << b);
29 if (gmask & test)
30 printf("%d ", b + 1);
31 }
32}
33
34/* Function which prints out usage message. */
35static void help(void)
36{
Harald Welte015dffb2000-07-31 23:38:14 +000037 printf("ULOG v%s options:\n"
38 " --ulog-nlgroup nlgroup NETLINK grouo used for logging\n"
39 " --ulog-cprange size Bytes of each packet to be passed\n"
40 " --ulog-prefix prefix Prefix log messages with this prefix.\n\n",
41 NETFILTER_VERSION);
Harald Weltec5bdb402000-07-31 14:24:57 +000042}
43
44static struct option opts[] = {
Harald Welte015dffb2000-07-31 23:38:14 +000045 {"ulog-nlgroup", 1, 0, '!'},
46 {"ulog-prefix", 1, 0, '#'},
47 {"ulog-cprange", 1, 0, 'A'},
48 {0}
Harald Weltec5bdb402000-07-31 14:24:57 +000049};
50
51/* Initialize the target. */
52static void init(struct ipt_entry_target *t, unsigned int *nfcache)
53{
Harald Welte015dffb2000-07-31 23:38:14 +000054 struct ipt_ulog_info *loginfo = (struct ipt_ulog_info *) t->data;
Harald Weltec5bdb402000-07-31 14:24:57 +000055
56 loginfo->nl_group = ULOG_DEFAULT_NLGROUP;
57
58 /* Can't cache this */
59 *nfcache |= NFC_UNKNOWN;
60}
61
62#define IPT_LOG_OPT_NLGROUP 0x01
63#define IPT_LOG_OPT_PREFIX 0x02
Harald Welte015dffb2000-07-31 23:38:14 +000064#define IPT_LOG_OPT_CPRANGE 0x04
Harald Weltec5bdb402000-07-31 14:24:57 +000065
66/* Function which parses command options; returns true if it
67 ate an option */
68static int parse(int c, char **argv, int invert, unsigned int *flags,
Harald Welte015dffb2000-07-31 23:38:14 +000069 const struct ipt_entry *entry,
70 struct ipt_entry_target **target)
Harald Weltec5bdb402000-07-31 14:24:57 +000071{
Harald Welte015dffb2000-07-31 23:38:14 +000072 struct ipt_ulog_info *loginfo =
73 (struct ipt_ulog_info *) (*target)->data;
Harald Weltec5bdb402000-07-31 14:24:57 +000074 int group_d;
75
76 switch (c) {
77 case '!':
78 if (*flags & IPT_LOG_OPT_NLGROUP)
79 exit_error(PARAMETER_PROBLEM,
80 "Can't specify --ulog-nlgroup twice");
81
82 if (check_inverse(optarg, &invert))
83 exit_error(PARAMETER_PROBLEM,
84 "Unexpected `!' after --ulog-nlgroup");
85 group_d = atoi(optarg);
86 if (group_d > 32 || group_d < 1)
87 exit_error(PARAMETER_PROBLEM,
Harald Welte015dffb2000-07-31 23:38:14 +000088 "--ulog-nlgroup has to be between 1 and 32");
Harald Weltec5bdb402000-07-31 14:24:57 +000089
Harald Welte015dffb2000-07-31 23:38:14 +000090 loginfo->nl_group = (1 << (group_d - 1));
Harald Weltec5bdb402000-07-31 14:24:57 +000091
92 *flags |= IPT_LOG_OPT_NLGROUP;
93 break;
94
95 case '#':
96 if (*flags & IPT_LOG_OPT_PREFIX)
97 exit_error(PARAMETER_PROBLEM,
98 "Can't specify --ulog-prefix twice");
99
100 if (check_inverse(optarg, &invert))
101 exit_error(PARAMETER_PROBLEM,
102 "Unexpected `!' after --ulog-prefix");
103
104 if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
105 exit_error(PARAMETER_PROBLEM,
106 "Maximum prefix length %u for --ulog-prefix",
107 sizeof(loginfo->prefix) - 1);
108
109 strcpy(loginfo->prefix, optarg);
110 *flags |= IPT_LOG_OPT_PREFIX;
111 break;
Harald Welte015dffb2000-07-31 23:38:14 +0000112 case 'A':
113 if (*flags & IPT_LOG_OPT_CPRANGE)
114 exit_error(PARAMETER_PROBLEM,
115 "Can't specify --ulog-cprange twice");
116 if (atoi(optarg) < 0)
117 exit_error(PARAMETER_PROBLEM,
118 "Negative copy range?");
119 loginfo->copy_range = atoi(optarg);
120 *flags |= IPT_LOG_OPT_CPRANGE;
121 break;
Harald Weltec5bdb402000-07-31 14:24:57 +0000122 }
123 return 1;
124}
125
126/* Final check; nothing. */
127static void final_check(unsigned int flags)
128{
129}
130
131/* Saves the union ipt_targinfo in parsable form to stdout. */
Harald Welte015dffb2000-07-31 23:38:14 +0000132static void save(const struct ipt_ip *ip,
133 const struct ipt_entry_target *target)
Harald Weltec5bdb402000-07-31 14:24:57 +0000134{
Harald Welte015dffb2000-07-31 23:38:14 +0000135 const struct ipt_ulog_info *loginfo
136 = (const struct ipt_ulog_info *) target->data;
Harald Weltec5bdb402000-07-31 14:24:57 +0000137
Harald Welte015dffb2000-07-31 23:38:14 +0000138 if (strcmp(loginfo->prefix, "") != 0)
139 printf("--ulog-prefix %s ", loginfo->prefix);
Harald Weltec5bdb402000-07-31 14:24:57 +0000140
Harald Welte015dffb2000-07-31 23:38:14 +0000141 if (loginfo->nl_group != ULOG_DEFAULT_NLGROUP) {
142 printf("--ulog-nlgroup ");
Harald Weltec5bdb402000-07-31 14:24:57 +0000143 print_groups(loginfo->nl_group);
144 printf("\n");
145 }
Harald Welte015dffb2000-07-31 23:38:14 +0000146 if (loginfo->copy_range)
147 printf("--ulog-cprange %d", loginfo->copy_range);
Harald Weltec5bdb402000-07-31 14:24:57 +0000148}
149
150/* Prints out the targinfo. */
151static void
152print(const struct ipt_ip *ip,
Harald Welte015dffb2000-07-31 23:38:14 +0000153 const struct ipt_entry_target *target, int numeric)
Harald Weltec5bdb402000-07-31 14:24:57 +0000154{
155 const struct ipt_ulog_info *loginfo
Harald Welte015dffb2000-07-31 23:38:14 +0000156 = (const struct ipt_ulog_info *) target->data;
Harald Weltec5bdb402000-07-31 14:24:57 +0000157
158 printf("ULOG ");
Harald Welte015dffb2000-07-31 23:38:14 +0000159 printf("copy_range %d nlgroup ", loginfo->copy_range);
Harald Weltec5bdb402000-07-31 14:24:57 +0000160 print_groups(loginfo->nl_group);
161 if (strcmp(loginfo->prefix, "") != 0)
162 printf("prefix `%s' ", loginfo->prefix);
163}
164
Harald Welte015dffb2000-07-31 23:38:14 +0000165struct iptables_target ulog = { NULL,
166 "ULOG",
167 NETFILTER_VERSION,
168 IPT_ALIGN(sizeof(struct ipt_ulog_info)),
169 IPT_ALIGN(sizeof(struct ipt_ulog_info)),
170 &help,
171 &init,
172 &parse,
173 &final_check,
174 &print,
175 &save,
176 opts
Harald Weltec5bdb402000-07-31 14:24:57 +0000177};
178
179void _init(void)
180{
181 register_target(&ulog);
182}