blob: 1cf831a7ec17aee943b92ff57ca4d7962174c662 [file] [log] [blame]
Bart De Schuymerff587202005-02-08 20:02:28 +00001/* ebt_log
2 *
3 * Authors:
4 * Bart De Schuymer <bdschuym@pandora.be>
5 *
6 * April, 2002
7 */
8
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00009#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000012#include <getopt.h>
13#include "../include/ebtables_u.h"
14#include <linux/netfilter_bridge/ebt_log.h>
15
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000016/*
17 * copied from syslog.h
18 * used for the LOG target
19 */
20#define LOG_EMERG 0 /* system is unusable */
21#define LOG_ALERT 1 /* action must be taken immediately */
22#define LOG_CRIT 2 /* critical conditions */
23#define LOG_ERR 3 /* error conditions */
24#define LOG_WARNING 4 /* warning conditions */
25#define LOG_NOTICE 5 /* normal but significant condition */
26#define LOG_INFO 6 /* informational */
27#define LOG_DEBUG 7 /* debug-level messages */
Bart De Schuymer9cfd6542002-08-13 16:08:08 +000028
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000029#define LOG_DEFAULT_LEVEL LOG_INFO
30
31typedef struct _code {
Bart De Schuymer9cfd6542002-08-13 16:08:08 +000032 char *c_name;
33 int c_val;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000034} CODE;
35
36static CODE eight_priority[] = {
37 { "emerg", LOG_EMERG },
38 { "alert", LOG_ALERT },
39 { "crit", LOG_CRIT },
40 { "error", LOG_ERR },
41 { "warning", LOG_WARNING },
42 { "notice", LOG_NOTICE },
43 { "info", LOG_INFO },
Bart De Schuymer9cfd6542002-08-13 16:08:08 +000044 { "debug", LOG_DEBUG }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000045};
46
47static int name_to_loglevel(char* arg)
48{
Bart De Schuymer9cfd6542002-08-13 16:08:08 +000049 int i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000050
Bart De Schuymer9cfd6542002-08-13 16:08:08 +000051 for (i = 0; i < 8; i++)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000052 if (!strcmp(arg, eight_priority[i].c_name))
Bart De Schuymer9cfd6542002-08-13 16:08:08 +000053 return eight_priority[i].c_val;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000054 /* return bad loglevel */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000055 return 9;
56}
57
58#define LOG_PREFIX '1'
59#define LOG_LEVEL '2'
60#define LOG_ARP '3'
61#define LOG_IP '4'
62#define LOG_LOG '5'
Bart De Schuymer005837e2008-02-21 21:32:25 +000063#define LOG_IP6 '6'
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000064static struct option opts[] =
65{
66 { "log-prefix", required_argument, 0, LOG_PREFIX },
67 { "log-level" , required_argument, 0, LOG_LEVEL },
68 { "log-arp" , no_argument , 0, LOG_ARP },
69 { "log-ip" , no_argument , 0, LOG_IP },
70 { "log" , no_argument , 0, LOG_LOG },
Bart De Schuymer005837e2008-02-21 21:32:25 +000071 { "log-ip6" , no_argument , 0, LOG_IP6 },
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000072 { 0 }
73};
74
75static void print_help()
76{
77 int i;
78
79 printf(
80"log options:\n"
81"--log : use this if you're not specifying anything\n"
82"--log-level level : level = [1-8] or a string\n"
83"--log-prefix prefix : max. %d chars.\n"
84"--log-ip : put ip info. in the log for ip packets\n"
85"--log-arp : put (r)arp info. in the log for (r)arp packets\n"
Bart De Schuymer005837e2008-02-21 21:32:25 +000086"--log-ip6 : put ip6 info. in the log for ip6 packets\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000087 , EBT_LOG_PREFIX_SIZE - 1);
88 printf("levels:\n");
89 for (i = 0; i < 8; i++)
90 printf("%d = %s\n", eight_priority[i].c_val,
91 eight_priority[i].c_name);
92}
93
94static void init(struct ebt_entry_watcher *watcher)
95{
96 struct ebt_log_info *loginfo = (struct ebt_log_info *)watcher->data;
97
98 loginfo->bitmask = 0;
99 loginfo->prefix[0] = '\0';
100 loginfo->loglevel = LOG_NOTICE;
101}
102
103#define OPT_PREFIX 0x01
104#define OPT_LEVEL 0x02
105#define OPT_ARP 0x04
106#define OPT_IP 0x08
107#define OPT_LOG 0x10
Bart De Schuymer005837e2008-02-21 21:32:25 +0000108#define OPT_IP6 0x20
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000109static int parse(int c, char **argv, int argc, const struct ebt_u_entry *entry,
110 unsigned int *flags, struct ebt_entry_watcher **watcher)
111{
112 struct ebt_log_info *loginfo = (struct ebt_log_info *)(*watcher)->data;
Bart De Schuymer9cfd6542002-08-13 16:08:08 +0000113 long int i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000114 char *end;
115
116 switch (c) {
117 case LOG_PREFIX:
Bart De Schuymerff587202005-02-08 20:02:28 +0000118 ebt_check_option2(flags, OPT_PREFIX);
119 if (ebt_check_inverse(optarg))
120 ebt_print_error2("Unexpected `!' after --log-prefix");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000121 if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
Bart De Schuymerff587202005-02-08 20:02:28 +0000122 ebt_print_error2("Prefix too long");
Bart De Schuymerfa747392005-09-26 20:33:47 +0000123 if (strchr(optarg, '\"'))
124 ebt_print_error2("Use of \\\" is not allowed in the prefix");
Bart De Schuymer510c9ce2006-01-23 18:50:54 +0000125 strcpy((char *)loginfo->prefix, (char *)optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000126 break;
127
128 case LOG_LEVEL:
Bart De Schuymerff587202005-02-08 20:02:28 +0000129 ebt_check_option2(flags, OPT_LEVEL);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000130 i = strtol(optarg, &end, 16);
131 if (*end != '\0' || i < 0 || i > 7)
132 loginfo->loglevel = name_to_loglevel(optarg);
133 else
134 loginfo->loglevel = i;
135 if (loginfo->loglevel == 9)
Bart De Schuymerff587202005-02-08 20:02:28 +0000136 ebt_print_error2("Problem with the log-level");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000137 break;
138
139 case LOG_IP:
Bart De Schuymerff587202005-02-08 20:02:28 +0000140 ebt_check_option2(flags, OPT_IP);
141 if (ebt_check_inverse(optarg))
142 ebt_print_error2("Unexpected `!' after --log-ip");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000143 loginfo->bitmask |= EBT_LOG_IP;
144 break;
145
146 case LOG_ARP:
Bart De Schuymerff587202005-02-08 20:02:28 +0000147 ebt_check_option2(flags, OPT_ARP);
148 if (ebt_check_inverse(optarg))
149 ebt_print_error2("Unexpected `!' after --log-arp");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000150 loginfo->bitmask |= EBT_LOG_ARP;
151 break;
152
153 case LOG_LOG:
Bart De Schuymerff587202005-02-08 20:02:28 +0000154 ebt_check_option2(flags, OPT_LOG);
155 if (ebt_check_inverse(optarg))
156 ebt_print_error2("Unexpected `!' after --log");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000157 break;
Bart De Schuymer005837e2008-02-21 21:32:25 +0000158
159 case LOG_IP6:
160 ebt_check_option2(flags, OPT_IP6);
161 if (ebt_check_inverse(optarg))
162 ebt_print_error2("Unexpected `!' after --log-ip6");
163 loginfo->bitmask |= EBT_LOG_IP6;
164 break;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000165 default:
166 return 0;
167 }
168 return 1;
169}
170
171static void final_check(const struct ebt_u_entry *entry,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +0000172 const struct ebt_entry_watcher *watcher, const char *name,
Bart De Schuymerc9b52932002-08-24 13:26:34 +0000173 unsigned int hookmask, unsigned int time)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000174{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000175}
176
177static void print(const struct ebt_u_entry *entry,
178 const struct ebt_entry_watcher *watcher)
179{
180 struct ebt_log_info *loginfo = (struct ebt_log_info *)watcher->data;
181
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000182 printf("--log-level %s --log-prefix \"%s\"",
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000183 eight_priority[loginfo->loglevel].c_name,
184 loginfo->prefix);
185 if (loginfo->bitmask & EBT_LOG_IP)
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000186 printf(" --log-ip");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000187 if (loginfo->bitmask & EBT_LOG_ARP)
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000188 printf(" --log-arp");
Bart De Schuymer005837e2008-02-21 21:32:25 +0000189 if (loginfo->bitmask & EBT_LOG_IP6)
190 printf(" --log-ip6");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000191 printf(" ");
192}
193
194static int compare(const struct ebt_entry_watcher *w1,
195 const struct ebt_entry_watcher *w2)
196{
197 struct ebt_log_info *loginfo1 = (struct ebt_log_info *)w1->data;
198 struct ebt_log_info *loginfo2 = (struct ebt_log_info *)w2->data;
199
200 if (loginfo1->loglevel != loginfo2->loglevel)
201 return 0;
202 if (loginfo1->bitmask != loginfo2->bitmask)
203 return 0;
Bart De Schuymer510c9ce2006-01-23 18:50:54 +0000204 return !strcmp((char *)loginfo1->prefix, (char *)loginfo2->prefix);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000205}
206
207static struct ebt_u_watcher log_watcher =
208{
Bart De Schuymerfbdebad2008-02-03 19:58:44 +0000209 .name = "log",
Bart De Schuymer7cf1cca2003-08-30 16:20:19 +0000210 .size = sizeof(struct ebt_log_info),
211 .help = print_help,
212 .init = init,
213 .parse = parse,
214 .final_check = final_check,
215 .print = print,
216 .compare = compare,
217 .extra_ops = opts,
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000218};
219
Bart De Schuymer64182a32004-01-21 20:39:54 +0000220void _init(void)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000221{
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000222 ebt_register_watcher(&log_watcher);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000223}