4 * Bart De Schuymer <bdschuym@pandora.be>
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
{
36 static CODE eight_priority
[] = {
37 { "emerg", LOG_EMERG
},
38 { "alert", LOG_ALERT
},
41 { "warning", LOG_WARNING
},
42 { "notice", LOG_NOTICE
},
44 { "debug", LOG_DEBUG
}
47 static int name_to_loglevel(char* arg
)
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 */
58 #define LOG_PREFIX '1'
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
},
75 static void print_help()
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);
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
;
99 loginfo
->prefix
[0] = '\0';
100 loginfo
->loglevel
= LOG_NOTICE
;
103 #define OPT_PREFIX 0x01
104 #define OPT_LEVEL 0x02
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
;
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
);
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
);
134 loginfo
->loglevel
= i
;
135 if (loginfo
->loglevel
== 9)
136 ebt_print_error2("Problem with the log-level");
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
;
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
;
154 ebt_check_option2(flags
, OPT_LOG
);
155 if (ebt_check_inverse(optarg
))
156 ebt_print_error2("Unexpected `!' after --log");
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
;
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
,
185 if (loginfo
->bitmask
& EBT_LOG_IP
)
187 if (loginfo
->bitmask
& EBT_LOG_ARP
)
188 printf(" --log-arp");
189 if (loginfo
->bitmask
& EBT_LOG_IP6
)
190 printf(" --log-ip6");
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
)
202 if (loginfo1
->bitmask
!= loginfo2
->bitmask
)
204 return !strcmp((char *)loginfo1
->prefix
, (char *)loginfo2
->prefix
);
207 static struct ebt_u_watcher log_watcher
=
210 .size
= sizeof(struct ebt_log_info
),
214 .final_check
= final_check
,
222 ebt_register_watcher(&log_watcher
);