blob: 6dff952e6390aabeb571dc498c56bea6ebdc5e04 [file] [log] [blame]
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <sys/socket.h>
5#include <linux/netfilter_bridge/ebtables.h>
6#include <getopt.h>
7#include "../include/ebtables_u.h"
8#include <linux/netfilter_bridge/ebt_log.h>
9
10// copied from syslog.h
11// used for the LOG target
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
20#define LOG_DEFAULT_LEVEL LOG_INFO
21
22typedef struct _code {
23 char *c_name;
24 int c_val;
25} CODE;
26
27static CODE eight_priority[] = {
28 { "emerg", LOG_EMERG },
29 { "alert", LOG_ALERT },
30 { "crit", LOG_CRIT },
31 { "error", LOG_ERR },
32 { "warning", LOG_WARNING },
33 { "notice", LOG_NOTICE },
34 { "info", LOG_INFO },
35 { "debug", LOG_DEBUG },
36 { NULL, -1 }
37};
38
39static int name_to_loglevel(char* arg)
40{
41 int i = 0, c_val = eight_priority[0].c_val;
42
43 while (c_val != -1) {
44 if (!strcmp(arg, eight_priority[i].c_name))
45 return c_val;
46 i++;
47 c_val = eight_priority[i].c_val;
48 }
49 // return bad loglevel
50 return 9;
51}
52
53#define LOG_PREFIX '1'
54#define LOG_LEVEL '2'
55#define LOG_ARP '3'
56#define LOG_IP '4'
57#define LOG_LOG '5'
58static struct option opts[] =
59{
60 { "log-prefix", required_argument, 0, LOG_PREFIX },
61 { "log-level" , required_argument, 0, LOG_LEVEL },
62 { "log-arp" , no_argument , 0, LOG_ARP },
63 { "log-ip" , no_argument , 0, LOG_IP },
64 { "log" , no_argument , 0, LOG_LOG },
65 { 0 }
66};
67
68static void print_help()
69{
70 int i;
71
72 printf(
73"log options:\n"
74"--log : use this if you're not specifying anything\n"
75"--log-level level : level = [1-8] or a string\n"
76"--log-prefix prefix : max. %d chars.\n"
77"--log-ip : put ip info. in the log for ip packets\n"
78"--log-arp : put (r)arp info. in the log for (r)arp packets\n"
79 , EBT_LOG_PREFIX_SIZE - 1);
80 printf("levels:\n");
81 for (i = 0; i < 8; i++)
82 printf("%d = %s\n", eight_priority[i].c_val,
83 eight_priority[i].c_name);
84}
85
86static void init(struct ebt_entry_watcher *watcher)
87{
88 struct ebt_log_info *loginfo = (struct ebt_log_info *)watcher->data;
89
90 loginfo->bitmask = 0;
91 loginfo->prefix[0] = '\0';
92 loginfo->loglevel = LOG_NOTICE;
93}
94
95#define OPT_PREFIX 0x01
96#define OPT_LEVEL 0x02
97#define OPT_ARP 0x04
98#define OPT_IP 0x08
99#define OPT_LOG 0x10
100static int parse(int c, char **argv, int argc, const struct ebt_u_entry *entry,
101 unsigned int *flags, struct ebt_entry_watcher **watcher)
102{
103 struct ebt_log_info *loginfo = (struct ebt_log_info *)(*watcher)->data;
104 int i;
105 char *end;
106
107 switch (c) {
108 case LOG_PREFIX:
109 check_option(flags, OPT_PREFIX);
110 if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
111 print_error("Prefix too long");
112 strcpy(loginfo->prefix, optarg);
113 break;
114
115 case LOG_LEVEL:
116 check_option(flags, OPT_LEVEL);
117 i = strtol(optarg, &end, 16);
118 if (*end != '\0' || i < 0 || i > 7)
119 loginfo->loglevel = name_to_loglevel(optarg);
120 else
121 loginfo->loglevel = i;
122 if (loginfo->loglevel == 9)
123 print_error("Problem with the log-level");
124 break;
125
126 case LOG_IP:
127 check_option(flags, OPT_IP);
128 loginfo->bitmask |= EBT_LOG_IP;
129 break;
130
131 case LOG_ARP:
132 check_option(flags, OPT_ARP);
133 loginfo->bitmask |= EBT_LOG_ARP;
134 break;
135
136 case LOG_LOG:
137 check_option(flags, OPT_LOG);
138 break;
139 default:
140 return 0;
141 }
142 return 1;
143}
144
145static void final_check(const struct ebt_u_entry *entry,
146 const struct ebt_entry_watcher *watcher, const char *name, unsigned int hook)
147{
148 return;
149}
150
151static void print(const struct ebt_u_entry *entry,
152 const struct ebt_entry_watcher *watcher)
153{
154 struct ebt_log_info *loginfo = (struct ebt_log_info *)watcher->data;
155
156 printf("log: log-level = %s - log-prefix = \"%s\"",
157 eight_priority[loginfo->loglevel].c_name,
158 loginfo->prefix);
159 if (loginfo->bitmask & EBT_LOG_IP)
160 printf(" - log-ip");
161 if (loginfo->bitmask & EBT_LOG_ARP)
162 printf(" - log-arp");
163 printf(" ");
164}
165
166static int compare(const struct ebt_entry_watcher *w1,
167 const struct ebt_entry_watcher *w2)
168{
169 struct ebt_log_info *loginfo1 = (struct ebt_log_info *)w1->data;
170 struct ebt_log_info *loginfo2 = (struct ebt_log_info *)w2->data;
171
172 if (loginfo1->loglevel != loginfo2->loglevel)
173 return 0;
174 if (loginfo1->bitmask != loginfo2->bitmask)
175 return 0;
176 return !strcmp(loginfo1->prefix, loginfo2->prefix);
177}
178
179static struct ebt_u_watcher log_watcher =
180{
181 EBT_LOG_WATCHER,
182 sizeof(struct ebt_log_info),
183 print_help,
184 init,
185 parse,
186 final_check,
187 print,
188 compare,
189 opts,
190};
191
192#undef _init
193static void _init(void) __attribute__ ((constructor));
194static void _init(void)
195{
196 register_watcher(&log_watcher);
197}