libxt_NFLOG.c revision 7070b1f3c88a0c3d4e315c00cca61f05b0fbc882
1#include <stdbool.h> 2#include <stdlib.h> 3#include <stdio.h> 4#include <string.h> 5#include <getopt.h> 6#include <xtables.h> 7 8#include <linux/netfilter/x_tables.h> 9#include <linux/netfilter/xt_NFLOG.h> 10 11enum { 12 O_GROUP = 0, 13 O_PREFIX, 14 O_RANGE, 15 O_SIZE, 16 O_THRESHOLD, 17 F_RANGE = 1 << O_RANGE, 18 F_SIZE = 1 << O_SIZE, 19}; 20 21#define s struct xt_nflog_info 22static const struct xt_option_entry NFLOG_opts[] = { 23 {.name = "nflog-group", .id = O_GROUP, .type = XTTYPE_UINT16, 24 .flags = XTOPT_PUT, XTOPT_POINTER(s, group)}, 25 {.name = "nflog-prefix", .id = O_PREFIX, .type = XTTYPE_STRING, 26 .min = 1, .flags = XTOPT_PUT, XTOPT_POINTER(s, prefix)}, 27 {.name = "nflog-range", .id = O_RANGE, .type = XTTYPE_UINT32, 28 .excl = F_SIZE, .flags = XTOPT_PUT, XTOPT_POINTER(s, len)}, 29 {.name = "nflog-size", .id = O_SIZE, .type = XTTYPE_UINT32, 30 .excl = F_RANGE, .flags = XTOPT_PUT, XTOPT_POINTER(s, len)}, 31 {.name = "nflog-threshold", .id = O_THRESHOLD, .type = XTTYPE_UINT16, 32 .flags = XTOPT_PUT, XTOPT_POINTER(s, threshold)}, 33 XTOPT_TABLEEND, 34}; 35#undef s 36 37static void NFLOG_help(void) 38{ 39 printf("NFLOG target options:\n" 40 " --nflog-group NUM NETLINK group used for logging\n" 41 " --nflog-range NUM This option has no effect, use --nflog-size\n" 42 " --nflog-size NUM Number of bytes to copy\n" 43 " --nflog-threshold NUM Message threshold of in-kernel queue\n" 44 " --nflog-prefix STRING Prefix string for log messages\n"); 45} 46 47static void NFLOG_init(struct xt_entry_target *t) 48{ 49 struct xt_nflog_info *info = (struct xt_nflog_info *)t->data; 50 51 info->threshold = XT_NFLOG_DEFAULT_THRESHOLD; 52} 53 54static void NFLOG_parse(struct xt_option_call *cb) 55{ 56 xtables_option_parse(cb); 57 switch (cb->entry->id) { 58 case O_PREFIX: 59 if (strchr(cb->arg, '\n') != NULL) 60 xtables_error(PARAMETER_PROBLEM, 61 "Newlines not allowed in --log-prefix"); 62 break; 63 } 64} 65 66static void NFLOG_check(struct xt_fcheck_call *cb) 67{ 68 struct xt_nflog_info *info = cb->data; 69 70 if (cb->xflags & F_RANGE) 71 fprintf(stderr, "warn: --nflog-range has never worked and is no" 72 " longer supported, please use --nflog-size insted\n"); 73 74 if (cb->xflags & F_SIZE) 75 info->flags |= XT_NFLOG_F_COPY_LEN; 76} 77 78static void nflog_print(const struct xt_nflog_info *info, char *prefix) 79{ 80 if (info->prefix[0] != '\0') { 81 printf(" %snflog-prefix ", prefix); 82 xtables_save_string(info->prefix); 83 } 84 if (info->group) 85 printf(" %snflog-group %u", prefix, info->group); 86 if (info->len && info->flags & XT_NFLOG_F_COPY_LEN) 87 printf(" %snflog-size %u", prefix, info->len); 88 else if (info->len) 89 printf(" %snflog-range %u", prefix, info->len); 90 if (info->threshold != XT_NFLOG_DEFAULT_THRESHOLD) 91 printf(" %snflog-threshold %u", prefix, info->threshold); 92} 93 94static void NFLOG_print(const void *ip, const struct xt_entry_target *target, 95 int numeric) 96{ 97 const struct xt_nflog_info *info = (struct xt_nflog_info *)target->data; 98 99 nflog_print(info, ""); 100} 101 102static void NFLOG_save(const void *ip, const struct xt_entry_target *target) 103{ 104 const struct xt_nflog_info *info = (struct xt_nflog_info *)target->data; 105 106 nflog_print(info, "--"); 107} 108 109static void nflog_print_xlate(const struct xt_nflog_info *info, 110 struct xt_xlate *xl) 111{ 112 xt_xlate_add(xl, "log "); 113 if (info->prefix[0] != '\0') 114 xt_xlate_add(xl, "prefix \\\"%s\\\" ", info->prefix); 115 if (info->len) 116 xt_xlate_add(xl, "snaplen %u ", info->len); 117 if (info->threshold != XT_NFLOG_DEFAULT_THRESHOLD) 118 xt_xlate_add(xl, "queue-threshold %u ", info->threshold); 119 xt_xlate_add(xl, "group %u ", info->group); 120} 121 122static int NFLOG_xlate(const void *ip, const struct xt_entry_target *target, 123 struct xt_xlate *xl, int numeric) 124{ 125 const struct xt_nflog_info *info = (struct xt_nflog_info *)target->data; 126 127 nflog_print_xlate(info, xl); 128 129 return 1; 130} 131 132static struct xtables_target nflog_target = { 133 .family = NFPROTO_UNSPEC, 134 .name = "NFLOG", 135 .version = XTABLES_VERSION, 136 .size = XT_ALIGN(sizeof(struct xt_nflog_info)), 137 .userspacesize = XT_ALIGN(sizeof(struct xt_nflog_info)), 138 .help = NFLOG_help, 139 .init = NFLOG_init, 140 .x6_fcheck = NFLOG_check, 141 .x6_parse = NFLOG_parse, 142 .print = NFLOG_print, 143 .save = NFLOG_save, 144 .x6_options = NFLOG_opts, 145 .xlate = NFLOG_xlate, 146}; 147 148void _init(void) 149{ 150 xtables_register_target(&nflog_target); 151} 152