Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add LOG support. */ |
| 2 | #include <stdio.h> |
| 3 | #include <netdb.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <syslog.h> |
| 7 | #include <getopt.h> |
| 8 | #include <iptables.h> |
| 9 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 10 | #include <linux/netfilter_ipv4/ipt_LOG.h> |
| 11 | |
| 12 | #define LOG_DEFAULT_LEVEL LOG_WARNING |
| 13 | |
Martin Josefsson | 2b9a577 | 2005-01-05 15:21:15 +0000 | [diff] [blame] | 14 | #ifndef IPT_LOG_UID /* Old kernel */ |
| 15 | #define IPT_LOG_UID 0x08 /* Log UID owning local socket */ |
| 16 | #undef IPT_LOG_MASK |
| 17 | #define IPT_LOG_MASK 0x0f |
| 18 | #endif |
| 19 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 20 | /* Function which prints out usage message. */ |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 21 | static void LOG_help(void) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 22 | { |
| 23 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame^] | 24 | "LOG target options:\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 25 | " --log-level level Level of logging (numeric or see syslog.conf)\n" |
| 26 | " --log-prefix prefix Prefix log messages with this prefix.\n\n" |
| 27 | " --log-tcp-sequence Log TCP sequence numbers.\n\n" |
| 28 | " --log-tcp-options Log TCP options.\n\n" |
John Lange | f46e1af | 2005-01-02 23:33:12 +0000 | [diff] [blame] | 29 | " --log-ip-options Log IP options.\n\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame^] | 30 | " --log-uid Log UID owning the local socket.\n\n"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 33 | static const struct option LOG_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 34 | { .name = "log-level", .has_arg = 1, .val = '!' }, |
| 35 | { .name = "log-prefix", .has_arg = 1, .val = '#' }, |
| 36 | { .name = "log-tcp-sequence", .has_arg = 0, .val = '1' }, |
| 37 | { .name = "log-tcp-options", .has_arg = 0, .val = '2' }, |
| 38 | { .name = "log-ip-options", .has_arg = 0, .val = '3' }, |
| 39 | { .name = "log-uid", .has_arg = 0, .val = '4' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 40 | { .name = NULL } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | /* Initialize the target. */ |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 44 | static void LOG_init(struct xt_entry_target *t) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 45 | { |
| 46 | struct ipt_log_info *loginfo = (struct ipt_log_info *)t->data; |
| 47 | |
| 48 | loginfo->level = LOG_DEFAULT_LEVEL; |
| 49 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | struct ipt_log_names { |
| 53 | const char *name; |
| 54 | unsigned int level; |
| 55 | }; |
| 56 | |
Jan Engelhardt | 0e2abed | 2007-10-04 16:25:58 +0000 | [diff] [blame] | 57 | static const struct ipt_log_names ipt_log_names[] |
Stephane Ouellette | 2be28ab | 2003-08-11 19:58:56 +0000 | [diff] [blame] | 58 | = { { .name = "alert", .level = LOG_ALERT }, |
| 59 | { .name = "crit", .level = LOG_CRIT }, |
| 60 | { .name = "debug", .level = LOG_DEBUG }, |
| 61 | { .name = "emerg", .level = LOG_EMERG }, |
| 62 | { .name = "error", .level = LOG_ERR }, /* DEPRECATED */ |
| 63 | { .name = "info", .level = LOG_INFO }, |
| 64 | { .name = "notice", .level = LOG_NOTICE }, |
| 65 | { .name = "panic", .level = LOG_EMERG }, /* DEPRECATED */ |
| 66 | { .name = "warning", .level = LOG_WARNING } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | static u_int8_t |
| 70 | parse_level(const char *level) |
| 71 | { |
Marc Boucher | 459357f | 2001-09-08 02:16:51 +0000 | [diff] [blame] | 72 | unsigned int lev = -1; |
Harald Welte | 3e44c50 | 2001-10-22 08:16:24 +0000 | [diff] [blame] | 73 | unsigned int set = 0; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 74 | |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame] | 75 | if (string_to_number(level, 0, 7, &lev) == -1) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 76 | unsigned int i = 0; |
| 77 | |
| 78 | for (i = 0; |
| 79 | i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names); |
| 80 | i++) { |
| 81 | if (strncasecmp(level, ipt_log_names[i].name, |
| 82 | strlen(level)) == 0) { |
Harald Welte | 3e44c50 | 2001-10-22 08:16:24 +0000 | [diff] [blame] | 83 | if (set++) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 84 | exit_error(PARAMETER_PROBLEM, |
| 85 | "log-level `%s' ambiguous", |
| 86 | level); |
| 87 | lev = ipt_log_names[i].level; |
| 88 | } |
| 89 | } |
| 90 | |
Harald Welte | 3e44c50 | 2001-10-22 08:16:24 +0000 | [diff] [blame] | 91 | if (!set) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 92 | exit_error(PARAMETER_PROBLEM, |
| 93 | "log-level `%s' unknown", level); |
| 94 | } |
| 95 | |
| 96 | return (u_int8_t)lev; |
| 97 | } |
| 98 | |
| 99 | #define IPT_LOG_OPT_LEVEL 0x01 |
| 100 | #define IPT_LOG_OPT_PREFIX 0x02 |
| 101 | #define IPT_LOG_OPT_TCPSEQ 0x04 |
| 102 | #define IPT_LOG_OPT_TCPOPT 0x08 |
| 103 | #define IPT_LOG_OPT_IPOPT 0x10 |
John Lange | f46e1af | 2005-01-02 23:33:12 +0000 | [diff] [blame] | 104 | #define IPT_LOG_OPT_UID 0x20 |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 105 | |
| 106 | /* Function which parses command options; returns true if it |
| 107 | ate an option */ |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 108 | static int LOG_parse(int c, char **argv, int invert, unsigned int *flags, |
| 109 | const void *entry, struct xt_entry_target **target) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 110 | { |
| 111 | struct ipt_log_info *loginfo = (struct ipt_log_info *)(*target)->data; |
| 112 | |
| 113 | switch (c) { |
| 114 | case '!': |
| 115 | if (*flags & IPT_LOG_OPT_LEVEL) |
| 116 | exit_error(PARAMETER_PROBLEM, |
| 117 | "Can't specify --log-level twice"); |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 118 | |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 119 | if (check_inverse(optarg, &invert, NULL, 0)) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 120 | exit_error(PARAMETER_PROBLEM, |
| 121 | "Unexpected `!' after --log-level"); |
| 122 | |
| 123 | loginfo->level = parse_level(optarg); |
| 124 | *flags |= IPT_LOG_OPT_LEVEL; |
| 125 | break; |
| 126 | |
| 127 | case '#': |
| 128 | if (*flags & IPT_LOG_OPT_PREFIX) |
| 129 | exit_error(PARAMETER_PROBLEM, |
| 130 | "Can't specify --log-prefix twice"); |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 131 | |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 132 | if (check_inverse(optarg, &invert, NULL, 0)) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 133 | exit_error(PARAMETER_PROBLEM, |
| 134 | "Unexpected `!' after --log-prefix"); |
| 135 | |
| 136 | if (strlen(optarg) > sizeof(loginfo->prefix) - 1) |
| 137 | exit_error(PARAMETER_PROBLEM, |
| 138 | "Maximum prefix length %u for --log-prefix", |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 139 | (unsigned int)sizeof(loginfo->prefix) - 1); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 140 | |
Mike Frysinger | ed26b7b | 2006-10-10 06:18:40 +0000 | [diff] [blame] | 141 | if (strlen(optarg) == 0) |
| 142 | exit_error(PARAMETER_PROBLEM, |
| 143 | "No prefix specified for --log-prefix"); |
| 144 | |
Phil Oester | 182f3f6 | 2005-04-01 07:07:00 +0000 | [diff] [blame] | 145 | if (strlen(optarg) != strlen(strtok(optarg, "\n"))) |
| 146 | exit_error(PARAMETER_PROBLEM, |
| 147 | "Newlines not allowed in --log-prefix"); |
| 148 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 149 | strcpy(loginfo->prefix, optarg); |
| 150 | *flags |= IPT_LOG_OPT_PREFIX; |
| 151 | break; |
| 152 | |
| 153 | case '1': |
| 154 | if (*flags & IPT_LOG_OPT_TCPSEQ) |
| 155 | exit_error(PARAMETER_PROBLEM, |
| 156 | "Can't specify --log-tcp-sequence " |
| 157 | "twice"); |
| 158 | |
| 159 | loginfo->logflags |= IPT_LOG_TCPSEQ; |
| 160 | *flags |= IPT_LOG_OPT_TCPSEQ; |
| 161 | break; |
| 162 | |
| 163 | case '2': |
| 164 | if (*flags & IPT_LOG_OPT_TCPOPT) |
| 165 | exit_error(PARAMETER_PROBLEM, |
| 166 | "Can't specify --log-tcp-options twice"); |
| 167 | |
| 168 | loginfo->logflags |= IPT_LOG_TCPOPT; |
| 169 | *flags |= IPT_LOG_OPT_TCPOPT; |
| 170 | break; |
| 171 | |
| 172 | case '3': |
| 173 | if (*flags & IPT_LOG_OPT_IPOPT) |
| 174 | exit_error(PARAMETER_PROBLEM, |
| 175 | "Can't specify --log-ip-options twice"); |
| 176 | |
| 177 | loginfo->logflags |= IPT_LOG_IPOPT; |
| 178 | *flags |= IPT_LOG_OPT_IPOPT; |
| 179 | break; |
| 180 | |
John Lange | f46e1af | 2005-01-02 23:33:12 +0000 | [diff] [blame] | 181 | case '4': |
| 182 | if (*flags & IPT_LOG_OPT_UID) |
| 183 | exit_error(PARAMETER_PROBLEM, |
| 184 | "Can't specify --log-uid twice"); |
| 185 | |
| 186 | loginfo->logflags |= IPT_LOG_UID; |
| 187 | *flags |= IPT_LOG_OPT_UID; |
| 188 | break; |
| 189 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 190 | default: |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | return 1; |
| 195 | } |
| 196 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 197 | /* Prints out the targinfo. */ |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 198 | static void LOG_print(const void *ip, const struct xt_entry_target *target, |
| 199 | int numeric) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 200 | { |
| 201 | const struct ipt_log_info *loginfo |
| 202 | = (const struct ipt_log_info *)target->data; |
| 203 | unsigned int i = 0; |
| 204 | |
| 205 | printf("LOG "); |
| 206 | if (numeric) |
| 207 | printf("flags %u level %u ", |
| 208 | loginfo->logflags, loginfo->level); |
| 209 | else { |
| 210 | for (i = 0; |
| 211 | i < sizeof(ipt_log_names) / sizeof(struct ipt_log_names); |
| 212 | i++) { |
| 213 | if (loginfo->level == ipt_log_names[i].level) { |
| 214 | printf("level %s ", ipt_log_names[i].name); |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | if (i == sizeof(ipt_log_names) / sizeof(struct ipt_log_names)) |
| 219 | printf("UNKNOWN level %u ", loginfo->level); |
| 220 | if (loginfo->logflags & IPT_LOG_TCPSEQ) |
| 221 | printf("tcp-sequence "); |
| 222 | if (loginfo->logflags & IPT_LOG_TCPOPT) |
| 223 | printf("tcp-options "); |
| 224 | if (loginfo->logflags & IPT_LOG_IPOPT) |
| 225 | printf("ip-options "); |
John Lange | f46e1af | 2005-01-02 23:33:12 +0000 | [diff] [blame] | 226 | if (loginfo->logflags & IPT_LOG_UID) |
| 227 | printf("uid "); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 228 | if (loginfo->logflags & ~(IPT_LOG_MASK)) |
| 229 | printf("unknown-flags "); |
| 230 | } |
| 231 | |
| 232 | if (strcmp(loginfo->prefix, "") != 0) |
| 233 | printf("prefix `%s' ", loginfo->prefix); |
| 234 | } |
| 235 | |
| 236 | /* Saves the union ipt_targinfo in parsable form to stdout. */ |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 237 | static void LOG_save(const void *ip, const struct xt_entry_target *target) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 238 | { |
| 239 | const struct ipt_log_info *loginfo |
| 240 | = (const struct ipt_log_info *)target->data; |
| 241 | |
Max Kellermann | a5d0994 | 2008-01-29 13:44:34 +0000 | [diff] [blame] | 242 | if (strcmp(loginfo->prefix, "") != 0) { |
| 243 | printf("--log-prefix "); |
| 244 | save_string(loginfo->prefix); |
| 245 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 246 | |
Thomas Woerner | 01cbaa6 | 2003-07-14 20:01:29 +0000 | [diff] [blame] | 247 | if (loginfo->level != LOG_DEFAULT_LEVEL) |
| 248 | printf("--log-level %d ", loginfo->level); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 249 | |
| 250 | if (loginfo->logflags & IPT_LOG_TCPSEQ) |
| 251 | printf("--log-tcp-sequence "); |
| 252 | if (loginfo->logflags & IPT_LOG_TCPOPT) |
| 253 | printf("--log-tcp-options "); |
| 254 | if (loginfo->logflags & IPT_LOG_IPOPT) |
| 255 | printf("--log-ip-options "); |
John Lange | f46e1af | 2005-01-02 23:33:12 +0000 | [diff] [blame] | 256 | if (loginfo->logflags & IPT_LOG_UID) |
| 257 | printf("--log-uid "); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame^] | 260 | static struct xtables_target log_tg_reg = { |
Stephane Ouellette | 2be28ab | 2003-08-11 19:58:56 +0000 | [diff] [blame] | 261 | .name = "LOG", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame^] | 262 | .version = XTABLES_VERSION, |
| 263 | .family = PF_INET, |
| 264 | .size = XT_ALIGN(sizeof(struct ipt_log_info)), |
| 265 | .userspacesize = XT_ALIGN(sizeof(struct ipt_log_info)), |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 266 | .help = LOG_help, |
| 267 | .init = LOG_init, |
| 268 | .parse = LOG_parse, |
| 269 | .print = LOG_print, |
| 270 | .save = LOG_save, |
| 271 | .extra_opts = LOG_opts, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | void _init(void) |
| 275 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame^] | 276 | xtables_register_target(&log_tg_reg); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 277 | } |