blob: 3c2409fa3fc004189210dfc795581c2002aefab1 [file] [log] [blame]
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00004#include <getopt.h>
5#include "../include/ebtables_u.h"
6#include <linux/netfilter_bridge/ebt_log.h>
7
Bart De Schuymer9895a8e2003-01-11 10:14:24 +00008/*
9 * copied from syslog.h
10 * used for the LOG target
11 */
12#define LOG_EMERG 0 /* system is unusable */
13#define LOG_ALERT 1 /* action must be taken immediately */
14#define LOG_CRIT 2 /* critical conditions */
15#define LOG_ERR 3 /* error conditions */
16#define LOG_WARNING 4 /* warning conditions */
17#define LOG_NOTICE 5 /* normal but significant condition */
18#define LOG_INFO 6 /* informational */
19#define LOG_DEBUG 7 /* debug-level messages */
Bart De Schuymer9cfd6542002-08-13 16:08:08 +000020
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000021#define LOG_DEFAULT_LEVEL LOG_INFO
22
23typedef struct _code {
Bart De Schuymer9cfd6542002-08-13 16:08:08 +000024 char *c_name;
25 int c_val;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000026} CODE;
27
28static CODE eight_priority[] = {
29 { "emerg", LOG_EMERG },
30 { "alert", LOG_ALERT },
31 { "crit", LOG_CRIT },
32 { "error", LOG_ERR },
33 { "warning", LOG_WARNING },
34 { "notice", LOG_NOTICE },
35 { "info", LOG_INFO },
Bart De Schuymer9cfd6542002-08-13 16:08:08 +000036 { "debug", LOG_DEBUG }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000037};
38
39static int name_to_loglevel(char* arg)
40{
Bart De Schuymer9cfd6542002-08-13 16:08:08 +000041 int i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000042
Bart De Schuymer9cfd6542002-08-13 16:08:08 +000043 for (i = 0; i < 8; i++)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000044 if (!strcmp(arg, eight_priority[i].c_name))
Bart De Schuymer9cfd6542002-08-13 16:08:08 +000045 return eight_priority[i].c_val;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000046 /* return bad loglevel */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000047 return 9;
48}
49
50#define LOG_PREFIX '1'
51#define LOG_LEVEL '2'
52#define LOG_ARP '3'
53#define LOG_IP '4'
54#define LOG_LOG '5'
55static struct option opts[] =
56{
57 { "log-prefix", required_argument, 0, LOG_PREFIX },
58 { "log-level" , required_argument, 0, LOG_LEVEL },
59 { "log-arp" , no_argument , 0, LOG_ARP },
60 { "log-ip" , no_argument , 0, LOG_IP },
61 { "log" , no_argument , 0, LOG_LOG },
62 { 0 }
63};
64
65static void print_help()
66{
67 int i;
68
69 printf(
70"log options:\n"
71"--log : use this if you're not specifying anything\n"
72"--log-level level : level = [1-8] or a string\n"
73"--log-prefix prefix : max. %d chars.\n"
74"--log-ip : put ip info. in the log for ip packets\n"
75"--log-arp : put (r)arp info. in the log for (r)arp packets\n"
76 , EBT_LOG_PREFIX_SIZE - 1);
77 printf("levels:\n");
78 for (i = 0; i < 8; i++)
79 printf("%d = %s\n", eight_priority[i].c_val,
80 eight_priority[i].c_name);
81}
82
83static void init(struct ebt_entry_watcher *watcher)
84{
85 struct ebt_log_info *loginfo = (struct ebt_log_info *)watcher->data;
86
87 loginfo->bitmask = 0;
88 loginfo->prefix[0] = '\0';
89 loginfo->loglevel = LOG_NOTICE;
90}
91
92#define OPT_PREFIX 0x01
93#define OPT_LEVEL 0x02
94#define OPT_ARP 0x04
95#define OPT_IP 0x08
96#define OPT_LOG 0x10
97static int parse(int c, char **argv, int argc, const struct ebt_u_entry *entry,
98 unsigned int *flags, struct ebt_entry_watcher **watcher)
99{
100 struct ebt_log_info *loginfo = (struct ebt_log_info *)(*watcher)->data;
Bart De Schuymer9cfd6542002-08-13 16:08:08 +0000101 long int i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000102 char *end;
103
104 switch (c) {
105 case LOG_PREFIX:
106 check_option(flags, OPT_PREFIX);
107 if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
108 print_error("Prefix too long");
109 strcpy(loginfo->prefix, optarg);
110 break;
111
112 case LOG_LEVEL:
113 check_option(flags, OPT_LEVEL);
114 i = strtol(optarg, &end, 16);
115 if (*end != '\0' || i < 0 || i > 7)
116 loginfo->loglevel = name_to_loglevel(optarg);
117 else
118 loginfo->loglevel = i;
119 if (loginfo->loglevel == 9)
120 print_error("Problem with the log-level");
121 break;
122
123 case LOG_IP:
124 check_option(flags, OPT_IP);
125 loginfo->bitmask |= EBT_LOG_IP;
126 break;
127
128 case LOG_ARP:
129 check_option(flags, OPT_ARP);
130 loginfo->bitmask |= EBT_LOG_ARP;
131 break;
132
133 case LOG_LOG:
134 check_option(flags, OPT_LOG);
135 break;
136 default:
137 return 0;
138 }
139 return 1;
140}
141
142static void final_check(const struct ebt_u_entry *entry,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +0000143 const struct ebt_entry_watcher *watcher, const char *name,
Bart De Schuymerc9b52932002-08-24 13:26:34 +0000144 unsigned int hookmask, unsigned int time)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000145{
146 return;
147}
148
149static void print(const struct ebt_u_entry *entry,
150 const struct ebt_entry_watcher *watcher)
151{
152 struct ebt_log_info *loginfo = (struct ebt_log_info *)watcher->data;
153
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000154 printf("--log-level %s --log-prefix \"%s\"",
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000155 eight_priority[loginfo->loglevel].c_name,
156 loginfo->prefix);
157 if (loginfo->bitmask & EBT_LOG_IP)
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000158 printf(" --log-ip");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000159 if (loginfo->bitmask & EBT_LOG_ARP)
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000160 printf(" --log-arp");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000161 printf(" ");
162}
163
164static int compare(const struct ebt_entry_watcher *w1,
165 const struct ebt_entry_watcher *w2)
166{
167 struct ebt_log_info *loginfo1 = (struct ebt_log_info *)w1->data;
168 struct ebt_log_info *loginfo2 = (struct ebt_log_info *)w2->data;
169
170 if (loginfo1->loglevel != loginfo2->loglevel)
171 return 0;
172 if (loginfo1->bitmask != loginfo2->bitmask)
173 return 0;
174 return !strcmp(loginfo1->prefix, loginfo2->prefix);
175}
176
177static struct ebt_u_watcher log_watcher =
178{
179 EBT_LOG_WATCHER,
180 sizeof(struct ebt_log_info),
181 print_help,
182 init,
183 parse,
184 final_check,
185 print,
186 compare,
Bart De Schuymer9cfd6542002-08-13 16:08:08 +0000187 opts
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000188};
189
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000190static void _init(void) __attribute__ ((constructor));
191static void _init(void)
192{
193 register_watcher(&log_watcher);
194}