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