log_obj.c

00001 /*
00002  * lib/netfilter/log_obj.c      Netfilter Log Object
00003  *
00004  *      This library is free software; you can redistribute it and/or
00005  *      modify it under the terms of the GNU Lesser General Public
00006  *      License as published by the Free Software Foundation version 2.1
00007  *      of the License.
00008  *
00009  * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
00010  * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
00011  * Copyright (c) 2007 Secure Computing Corporation
00012  * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
00013  */
00014 
00015 #include <netlink-local.h>
00016 #include <netlink/netfilter/nfnl.h>
00017 #include <netlink/netfilter/log.h>
00018 
00019 /** @cond SKIP */
00020 #define LOG_ATTR_GROUP                  (1UL << 0)
00021 #define LOG_ATTR_COPY_MODE              (1UL << 1)
00022 #define LOG_ATTR_COPY_RANGE             (1UL << 3)
00023 #define LOG_ATTR_FLUSH_TIMEOUT          (1UL << 4)
00024 #define LOG_ATTR_ALLOC_SIZE             (1UL << 5)
00025 #define LOG_ATTR_QUEUE_THRESHOLD        (1UL << 6)
00026 
00027 /** @endcond */
00028 
00029 static void nfnl_log_dump(struct nl_object *a, struct nl_dump_params *p)
00030 {
00031         struct nfnl_log *log = (struct nfnl_log *) a;
00032         char buf[64];
00033 
00034         nl_new_line(p);
00035 
00036         if (log->ce_mask & LOG_ATTR_GROUP)
00037                 nl_dump(p, "group=%u ", log->log_group);
00038 
00039         if (log->ce_mask & LOG_ATTR_COPY_MODE)
00040                 nl_dump(p, "copy_mode=%s ",
00041                         nfnl_log_copy_mode2str(log->log_copy_mode,
00042                                                buf, sizeof(buf)));
00043 
00044         if (log->ce_mask & LOG_ATTR_COPY_RANGE)
00045                 nl_dump(p, "copy_range=%u ", log->log_copy_range);
00046 
00047         if (log->ce_mask & LOG_ATTR_FLUSH_TIMEOUT)
00048                 nl_dump(p, "flush_timeout=%u ", log->log_flush_timeout);
00049 
00050         if (log->ce_mask & LOG_ATTR_ALLOC_SIZE)
00051                 nl_dump(p, "alloc_size=%u ", log->log_alloc_size);
00052 
00053         if (log->ce_mask & LOG_ATTR_QUEUE_THRESHOLD)
00054                 nl_dump(p, "queue_threshold=%u ", log->log_queue_threshold);
00055 
00056         nl_dump(p, "\n");
00057 }
00058 
00059 static const struct trans_tbl copy_modes[] = {
00060         __ADD(NFNL_LOG_COPY_NONE,       none)
00061         __ADD(NFNL_LOG_COPY_META,       meta)
00062         __ADD(NFNL_LOG_COPY_PACKET,     packet)
00063 };
00064 
00065 char *nfnl_log_copy_mode2str(enum nfnl_log_copy_mode copy_mode, char *buf,
00066                              size_t len)
00067 {
00068         return __type2str(copy_mode, buf, len, copy_modes,
00069                           ARRAY_SIZE(copy_modes));
00070 }
00071 
00072 enum nfnl_log_copy_mode nfnl_log_str2copy_mode(const char *name)
00073 {
00074         return __str2type(name, copy_modes, ARRAY_SIZE(copy_modes));
00075 }
00076 
00077 /**
00078  * @name Allocation/Freeing
00079  * @{
00080  */
00081 
00082 struct nfnl_log *nfnl_log_alloc(void)
00083 {
00084         return (struct nfnl_log *) nl_object_alloc(&log_obj_ops);
00085 }
00086 
00087 void nfnl_log_get(struct nfnl_log *log)
00088 {
00089         nl_object_get((struct nl_object *) log);
00090 }
00091 
00092 void nfnl_log_put(struct nfnl_log *log)
00093 {
00094         nl_object_put((struct nl_object *) log);
00095 }
00096 
00097 /** @} */
00098 
00099 /**
00100  * @name Attributes
00101  * @{
00102  */
00103 
00104 void nfnl_log_set_group(struct nfnl_log *log, uint16_t group)
00105 {
00106         log->log_group = group;
00107         log->ce_mask |= LOG_ATTR_GROUP;
00108 }
00109 
00110 int nfnl_log_test_group(const struct nfnl_log *log)
00111 {
00112         return !!(log->ce_mask & LOG_ATTR_GROUP);
00113 }
00114 
00115 uint16_t nfnl_log_get_group(const struct nfnl_log *log)
00116 {
00117         return log->log_group;
00118 }
00119 
00120 void nfnl_log_set_copy_mode(struct nfnl_log *log, enum nfnl_log_copy_mode mode)
00121 {
00122         log->log_copy_mode = mode;
00123         log->ce_mask |= LOG_ATTR_COPY_MODE;
00124 }
00125 
00126 int nfnl_log_test_copy_mode(const struct nfnl_log *log)
00127 {
00128         return !!(log->ce_mask & LOG_ATTR_COPY_MODE);
00129 }
00130 
00131 enum nfnl_log_copy_mode nfnl_log_get_copy_mode(const struct nfnl_log *log)
00132 {
00133         return log->log_copy_mode;
00134 }
00135 
00136 void nfnl_log_set_copy_range(struct nfnl_log *log, uint32_t copy_range)
00137 {
00138         log->log_copy_range = copy_range;
00139         log->ce_mask |= LOG_ATTR_COPY_RANGE;
00140 }
00141 
00142 int nfnl_log_test_copy_range(const struct nfnl_log *log)
00143 {
00144         return !!(log->ce_mask & LOG_ATTR_COPY_RANGE);
00145 }
00146 
00147 uint32_t nfnl_log_get_copy_range(const struct nfnl_log *log)
00148 {
00149         return log->log_copy_range;
00150 }
00151 
00152 void nfnl_log_set_flush_timeout(struct nfnl_log *log, uint32_t timeout)
00153 {
00154         log->log_flush_timeout = timeout;
00155         log->ce_mask |= LOG_ATTR_FLUSH_TIMEOUT;
00156 }
00157 
00158 int nfnl_log_test_flush_timeout(const struct nfnl_log *log)
00159 {
00160         return !!(log->ce_mask & LOG_ATTR_FLUSH_TIMEOUT);
00161 }
00162 
00163 uint32_t nfnl_log_get_flush_timeout(const struct nfnl_log *log)
00164 {
00165         return log->log_flush_timeout;
00166 }
00167 
00168 void nfnl_log_set_alloc_size(struct nfnl_log *log, uint32_t alloc_size)
00169 {
00170         log->log_alloc_size = alloc_size;
00171         log->ce_mask |= LOG_ATTR_ALLOC_SIZE;
00172 }
00173 
00174 int nfnl_log_test_alloc_size(const struct nfnl_log *log)
00175 {
00176         return !!(log->ce_mask & LOG_ATTR_ALLOC_SIZE);
00177 }
00178 
00179 uint32_t nfnl_log_get_alloc_size(const struct nfnl_log *log)
00180 {
00181         return log->log_alloc_size;
00182 }
00183 
00184 void nfnl_log_set_queue_threshold(struct nfnl_log *log, uint32_t threshold)
00185 {
00186         log->log_queue_threshold = threshold;
00187         log->ce_mask |= LOG_ATTR_QUEUE_THRESHOLD;
00188 }
00189 
00190 int nfnl_log_test_queue_threshold(const struct nfnl_log *log)
00191 {
00192         return !!(log->ce_mask & LOG_ATTR_QUEUE_THRESHOLD);
00193 }
00194 
00195 uint32_t nfnl_log_get_queue_threshold(const struct nfnl_log *log)
00196 {
00197         return log->log_queue_threshold;
00198 }
00199 
00200 /* We don't actually use the flags for anything yet since the
00201  * nfnetlog_log interface truly sucks - it only contains the
00202  * flag value, but not mask, so we would have to make assumptions
00203  * about the supported flags.
00204  */
00205 void nfnl_log_set_flags(struct nfnl_log *log, unsigned int flags)
00206 {
00207         log->log_flags |= flags;
00208         log->log_flag_mask |= flags;
00209 }
00210 
00211 void nfnl_log_unset_flags(struct nfnl_log *log, unsigned int flags)
00212 {
00213         log->log_flags &= ~flags;
00214         log->log_flag_mask |= flags;
00215 }
00216 
00217 static const struct trans_tbl log_flags[] = {
00218         __ADD(NFNL_LOG_FLAG_SEQ,        seq)
00219         __ADD(NFNL_LOG_FLAG_SEQ_GLOBAL, seq_global)
00220 };
00221 
00222 char *nfnl_log_flags2str(unsigned int flags, char *buf, size_t len)
00223 {
00224         return __flags2str(flags, buf, len, log_flags, ARRAY_SIZE(log_flags));
00225 }
00226 
00227 unsigned int nfnl_log_str2flags(const char *name)
00228 {
00229         return __str2flags(name, log_flags, ARRAY_SIZE(log_flags));
00230 }
00231 
00232 static int nfnl_log_compare(struct nl_object *_a, struct nl_object *_b,
00233                             uint32_t attrs, int flags)
00234 {
00235         struct nfnl_log *a = (struct nfnl_log *) _a;
00236         struct nfnl_log *b = (struct nfnl_log *) _b;
00237         int diff = 0;
00238 
00239 #define NFNL_LOG_DIFF(ATTR, EXPR) \
00240         ATTR_DIFF(attrs, LOG_ATTR_##ATTR, a, b, EXPR)
00241 #define NFNL_LOG_DIFF_VAL(ATTR, FIELD) \
00242         NFNL_LOG_DIFF(ATTR, a->FIELD != b->FIELD)
00243 
00244         diff |= NFNL_LOG_DIFF_VAL(GROUP,                log_group);
00245         diff |= NFNL_LOG_DIFF_VAL(COPY_MODE,            log_copy_mode);
00246         diff |= NFNL_LOG_DIFF_VAL(COPY_RANGE,           log_copy_range);
00247         diff |= NFNL_LOG_DIFF_VAL(FLUSH_TIMEOUT,        log_flush_timeout);
00248         diff |= NFNL_LOG_DIFF_VAL(ALLOC_SIZE,           log_alloc_size);
00249         diff |= NFNL_LOG_DIFF_VAL(QUEUE_THRESHOLD,      log_queue_threshold);
00250 
00251 #undef NFNL_LOG_DIFF
00252 #undef NFNL_LOG_DIFF_VAL
00253 
00254         return diff;
00255 }
00256 
00257 static const struct trans_tbl nfnl_log_attrs[] = {
00258         __ADD(LOG_ATTR_GROUP,           group)
00259         __ADD(LOG_ATTR_COPY_MODE,       copy_mode)
00260         __ADD(LOG_ATTR_COPY_RANGE,      copy_range)
00261         __ADD(LOG_ATTR_FLUSH_TIMEOUT,   flush_timeout)
00262         __ADD(LOG_ATTR_ALLOC_SIZE,      alloc_size)
00263         __ADD(LOG_ATTR_QUEUE_THRESHOLD, queue_threshold)
00264 };
00265 
00266 static char *nfnl_log_attrs2str(int attrs, char *buf, size_t len)
00267 {
00268         return __flags2str(attrs, buf, len, nfnl_log_attrs,
00269                            ARRAY_SIZE(nfnl_log_attrs));
00270 }
00271 
00272 /** @} */
00273 
00274 struct nl_object_ops log_obj_ops = {
00275         .oo_name                = "netfilter/log",
00276         .oo_size                = sizeof(struct nfnl_log),
00277         .oo_dump = {
00278             [NL_DUMP_LINE]      = nfnl_log_dump,
00279             [NL_DUMP_DETAILS]   = nfnl_log_dump,
00280             [NL_DUMP_STATS]     = nfnl_log_dump,
00281         },
00282         .oo_compare             = nfnl_log_compare,
00283         .oo_attrs2str           = nfnl_log_attrs2str,
00284         .oo_id_attrs            = LOG_ATTR_GROUP,
00285 };
00286 
00287 /** @} */