Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / ebtables / extensions / ebt_log.c
blob1cf831a7ec17aee943b92ff57ca4d7962174c662
1 /* ebt_log
3 * Authors:
4 * Bart De Schuymer <bdschuym@pandora.be>
6 * April, 2002
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <getopt.h>
13 #include "../include/ebtables_u.h"
14 #include <linux/netfilter_bridge/ebt_log.h>
17 * copied from syslog.h
18 * used for the LOG target
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 */
29 #define LOG_DEFAULT_LEVEL LOG_INFO
31 typedef struct _code {
32 char *c_name;
33 int c_val;
34 } CODE;
36 static 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 },
44 { "debug", LOG_DEBUG }
47 static int name_to_loglevel(char* arg)
49 int i;
51 for (i = 0; i < 8; i++)
52 if (!strcmp(arg, eight_priority[i].c_name))
53 return eight_priority[i].c_val;
54 /* return bad loglevel */
55 return 9;
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'
63 #define LOG_IP6 '6'
64 static struct option opts[] =
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 },
71 { "log-ip6" , no_argument , 0, LOG_IP6 },
72 { 0 }
75 static void print_help()
77 int i;
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"
86 "--log-ip6 : put ip6 info. in the log for ip6 packets\n"
87 , 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);
94 static void init(struct ebt_entry_watcher *watcher)
96 struct ebt_log_info *loginfo = (struct ebt_log_info *)watcher->data;
98 loginfo->bitmask = 0;
99 loginfo->prefix[0] = '\0';
100 loginfo->loglevel = LOG_NOTICE;
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
108 #define OPT_IP6 0x20
109 static int parse(int c, char **argv, int argc, const struct ebt_u_entry *entry,
110 unsigned int *flags, struct ebt_entry_watcher **watcher)
112 struct ebt_log_info *loginfo = (struct ebt_log_info *)(*watcher)->data;
113 long int i;
114 char *end;
116 switch (c) {
117 case LOG_PREFIX:
118 ebt_check_option2(flags, OPT_PREFIX);
119 if (ebt_check_inverse(optarg))
120 ebt_print_error2("Unexpected `!' after --log-prefix");
121 if (strlen(optarg) > sizeof(loginfo->prefix) - 1)
122 ebt_print_error2("Prefix too long");
123 if (strchr(optarg, '\"'))
124 ebt_print_error2("Use of \\\" is not allowed in the prefix");
125 strcpy((char *)loginfo->prefix, (char *)optarg);
126 break;
128 case LOG_LEVEL:
129 ebt_check_option2(flags, OPT_LEVEL);
130 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)
136 ebt_print_error2("Problem with the log-level");
137 break;
139 case LOG_IP:
140 ebt_check_option2(flags, OPT_IP);
141 if (ebt_check_inverse(optarg))
142 ebt_print_error2("Unexpected `!' after --log-ip");
143 loginfo->bitmask |= EBT_LOG_IP;
144 break;
146 case LOG_ARP:
147 ebt_check_option2(flags, OPT_ARP);
148 if (ebt_check_inverse(optarg))
149 ebt_print_error2("Unexpected `!' after --log-arp");
150 loginfo->bitmask |= EBT_LOG_ARP;
151 break;
153 case LOG_LOG:
154 ebt_check_option2(flags, OPT_LOG);
155 if (ebt_check_inverse(optarg))
156 ebt_print_error2("Unexpected `!' after --log");
157 break;
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;
165 default:
166 return 0;
168 return 1;
171 static void final_check(const struct ebt_u_entry *entry,
172 const struct ebt_entry_watcher *watcher, const char *name,
173 unsigned int hookmask, unsigned int time)
177 static void print(const struct ebt_u_entry *entry,
178 const struct ebt_entry_watcher *watcher)
180 struct ebt_log_info *loginfo = (struct ebt_log_info *)watcher->data;
182 printf("--log-level %s --log-prefix \"%s\"",
183 eight_priority[loginfo->loglevel].c_name,
184 loginfo->prefix);
185 if (loginfo->bitmask & EBT_LOG_IP)
186 printf(" --log-ip");
187 if (loginfo->bitmask & EBT_LOG_ARP)
188 printf(" --log-arp");
189 if (loginfo->bitmask & EBT_LOG_IP6)
190 printf(" --log-ip6");
191 printf(" ");
194 static int compare(const struct ebt_entry_watcher *w1,
195 const struct ebt_entry_watcher *w2)
197 struct ebt_log_info *loginfo1 = (struct ebt_log_info *)w1->data;
198 struct ebt_log_info *loginfo2 = (struct ebt_log_info *)w2->data;
200 if (loginfo1->loglevel != loginfo2->loglevel)
201 return 0;
202 if (loginfo1->bitmask != loginfo2->bitmask)
203 return 0;
204 return !strcmp((char *)loginfo1->prefix, (char *)loginfo2->prefix);
207 static struct ebt_u_watcher log_watcher =
209 .name = "log",
210 .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,
220 void _init(void)
222 ebt_register_watcher(&log_watcher);