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