libnl
3.2.3
|
00001 /* 00002 * lib/netfilter/queue_obj.c Netfilter Queue 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) 2007, 2008 Patrick McHardy <kaber@trash.net> 00010 */ 00011 00012 /** 00013 * @ingroup nfnl 00014 * @defgroup queue Queue 00015 * @brief 00016 * @{ 00017 */ 00018 00019 #include <netlink-local.h> 00020 #include <netlink/netfilter/nfnl.h> 00021 #include <netlink/netfilter/queue.h> 00022 00023 /** @cond SKIP */ 00024 #define QUEUE_ATTR_GROUP (1UL << 0) 00025 #define QUEUE_ATTR_MAXLEN (1UL << 1) 00026 #define QUEUE_ATTR_COPY_MODE (1UL << 2) 00027 #define QUEUE_ATTR_COPY_RANGE (1UL << 3) 00028 /** @endcond */ 00029 00030 00031 static void nfnl_queue_dump(struct nl_object *a, struct nl_dump_params *p) 00032 { 00033 struct nfnl_queue *queue = (struct nfnl_queue *) a; 00034 char buf[64]; 00035 00036 nl_new_line(p); 00037 00038 if (queue->ce_mask & QUEUE_ATTR_GROUP) 00039 nl_dump(p, "group=%u ", queue->queue_group); 00040 00041 if (queue->ce_mask & QUEUE_ATTR_MAXLEN) 00042 nl_dump(p, "maxlen=%u ", queue->queue_maxlen); 00043 00044 if (queue->ce_mask & QUEUE_ATTR_COPY_MODE) 00045 nl_dump(p, "copy_mode=%s ", 00046 nfnl_queue_copy_mode2str(queue->queue_copy_mode, 00047 buf, sizeof(buf))); 00048 00049 if (queue->ce_mask & QUEUE_ATTR_COPY_RANGE) 00050 nl_dump(p, "copy_range=%u ", queue->queue_copy_range); 00051 00052 nl_dump(p, "\n"); 00053 } 00054 00055 static const struct trans_tbl copy_modes[] = { 00056 __ADD(NFNL_QUEUE_COPY_NONE, none) 00057 __ADD(NFNL_QUEUE_COPY_META, meta) 00058 __ADD(NFNL_QUEUE_COPY_PACKET, packet) 00059 }; 00060 00061 char *nfnl_queue_copy_mode2str(enum nfnl_queue_copy_mode copy_mode, char *buf, 00062 size_t len) 00063 { 00064 return __type2str(copy_mode, buf, len, copy_modes, 00065 ARRAY_SIZE(copy_modes)); 00066 } 00067 00068 enum nfnl_queue_copy_mode nfnl_queue_str2copy_mode(const char *name) 00069 { 00070 return __str2type(name, copy_modes, ARRAY_SIZE(copy_modes)); 00071 } 00072 00073 /** 00074 * @name Allocation/Freeing 00075 * @{ 00076 */ 00077 00078 struct nfnl_queue *nfnl_queue_alloc(void) 00079 { 00080 return (struct nfnl_queue *) nl_object_alloc(&queue_obj_ops); 00081 } 00082 00083 void nfnl_queue_get(struct nfnl_queue *queue) 00084 { 00085 nl_object_get((struct nl_object *) queue); 00086 } 00087 00088 void nfnl_queue_put(struct nfnl_queue *queue) 00089 { 00090 nl_object_put((struct nl_object *) queue); 00091 } 00092 00093 /** @} */ 00094 00095 /** 00096 * @name Attributes 00097 * @{ 00098 */ 00099 00100 void nfnl_queue_set_group(struct nfnl_queue *queue, uint16_t group) 00101 { 00102 queue->queue_group = group; 00103 queue->ce_mask |= QUEUE_ATTR_GROUP; 00104 } 00105 00106 int nfnl_queue_test_group(const struct nfnl_queue *queue) 00107 { 00108 return !!(queue->ce_mask & QUEUE_ATTR_GROUP); 00109 } 00110 00111 uint16_t nfnl_queue_get_group(const struct nfnl_queue *queue) 00112 { 00113 return queue->queue_group; 00114 } 00115 00116 void nfnl_queue_set_maxlen(struct nfnl_queue *queue, uint32_t maxlen) 00117 { 00118 queue->queue_maxlen = maxlen; 00119 queue->ce_mask |= QUEUE_ATTR_MAXLEN; 00120 } 00121 00122 int nfnl_queue_test_maxlen(const struct nfnl_queue *queue) 00123 { 00124 return !!(queue->ce_mask & QUEUE_ATTR_MAXLEN); 00125 } 00126 00127 uint32_t nfnl_queue_get_maxlen(const struct nfnl_queue *queue) 00128 { 00129 return queue->queue_maxlen; 00130 } 00131 00132 void nfnl_queue_set_copy_mode(struct nfnl_queue *queue, enum nfnl_queue_copy_mode mode) 00133 { 00134 queue->queue_copy_mode = mode; 00135 queue->ce_mask |= QUEUE_ATTR_COPY_MODE; 00136 } 00137 00138 int nfnl_queue_test_copy_mode(const struct nfnl_queue *queue) 00139 { 00140 return !!(queue->ce_mask & QUEUE_ATTR_COPY_MODE); 00141 } 00142 00143 enum nfnl_queue_copy_mode nfnl_queue_get_copy_mode(const struct nfnl_queue *queue) 00144 { 00145 return queue->queue_copy_mode; 00146 } 00147 00148 void nfnl_queue_set_copy_range(struct nfnl_queue *queue, uint32_t copy_range) 00149 { 00150 queue->queue_copy_range = copy_range; 00151 queue->ce_mask |= QUEUE_ATTR_COPY_RANGE; 00152 } 00153 00154 int nfnl_queue_test_copy_range(const struct nfnl_queue *queue) 00155 { 00156 return !!(queue->ce_mask & QUEUE_ATTR_COPY_RANGE); 00157 } 00158 00159 uint32_t nfnl_queue_get_copy_range(const struct nfnl_queue *queue) 00160 { 00161 return queue->queue_copy_range; 00162 } 00163 00164 static int nfnl_queue_compare(struct nl_object *_a, struct nl_object *_b, 00165 uint32_t attrs, int flags) 00166 { 00167 struct nfnl_queue *a = (struct nfnl_queue *) _a; 00168 struct nfnl_queue *b = (struct nfnl_queue *) _b; 00169 int diff = 0; 00170 00171 #define NFNL_QUEUE_DIFF(ATTR, EXPR) \ 00172 ATTR_DIFF(attrs, QUEUE_ATTR_##ATTR, a, b, EXPR) 00173 #define NFNL_QUEUE_DIFF_VAL(ATTR, FIELD) \ 00174 NFNL_QUEUE_DIFF(ATTR, a->FIELD != b->FIELD) 00175 00176 diff |= NFNL_QUEUE_DIFF_VAL(GROUP, queue_group); 00177 diff |= NFNL_QUEUE_DIFF_VAL(MAXLEN, queue_maxlen); 00178 diff |= NFNL_QUEUE_DIFF_VAL(COPY_MODE, queue_copy_mode); 00179 diff |= NFNL_QUEUE_DIFF_VAL(COPY_RANGE, queue_copy_range); 00180 00181 #undef NFNL_QUEUE_DIFF 00182 #undef NFNL_QUEUE_DIFF_VAL 00183 00184 return diff; 00185 } 00186 00187 static const struct trans_tbl nfnl_queue_attrs[] = { 00188 __ADD(QUEUE_ATTR_GROUP, group) 00189 __ADD(QUEUE_ATTR_MAXLEN, maxlen) 00190 __ADD(QUEUE_ATTR_COPY_MODE, copy_mode) 00191 __ADD(QUEUE_ATTR_COPY_RANGE, copy_range) 00192 }; 00193 00194 static char *nfnl_queue_attrs2str(int attrs, char *buf, size_t len) 00195 { 00196 return __flags2str(attrs, buf, len, nfnl_queue_attrs, 00197 ARRAY_SIZE(nfnl_queue_attrs)); 00198 } 00199 00200 /** @} */ 00201 00202 struct nl_object_ops queue_obj_ops = { 00203 .oo_name = "netfilter/queue", 00204 .oo_size = sizeof(struct nfnl_queue), 00205 .oo_dump = { 00206 [NL_DUMP_LINE] = nfnl_queue_dump, 00207 [NL_DUMP_DETAILS] = nfnl_queue_dump, 00208 [NL_DUMP_STATS] = nfnl_queue_dump, 00209 }, 00210 .oo_compare = nfnl_queue_compare, 00211 .oo_attrs2str = nfnl_queue_attrs2str, 00212 .oo_id_attrs = QUEUE_ATTR_GROUP, 00213 }; 00214 00215 /** @} */