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