Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1 | #ifndef _NET_NF_TABLES_H |
| 2 | #define _NET_NF_TABLES_H |
| 3 | |
Patrick McHardy | 0b2d8a7 | 2015-04-11 10:46:38 +0100 | [diff] [blame] | 4 | #include <linux/module.h> |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 5 | #include <linux/list.h> |
| 6 | #include <linux/netfilter.h> |
Patrick McHardy | 67a8fc2 | 2014-02-18 18:06:49 +0000 | [diff] [blame] | 7 | #include <linux/netfilter/nfnetlink.h> |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 8 | #include <linux/netfilter/x_tables.h> |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 9 | #include <linux/netfilter/nf_tables.h> |
Eric Dumazet | ce355e2 | 2014-07-09 15:14:06 +0200 | [diff] [blame] | 10 | #include <linux/u64_stats_sync.h> |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 11 | #include <net/netlink.h> |
| 12 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 13 | #define NFT_JUMP_STACK_SIZE 16 |
| 14 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 15 | struct nft_pktinfo { |
| 16 | struct sk_buff *skb; |
| 17 | const struct net_device *in; |
| 18 | const struct net_device *out; |
Patrick McHardy | c948487 | 2014-01-03 12:16:14 +0000 | [diff] [blame] | 19 | const struct nf_hook_ops *ops; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 20 | u8 nhoff; |
| 21 | u8 thoff; |
Patrick McHardy | 4566bf2 | 2014-01-03 12:16:18 +0000 | [diff] [blame] | 22 | u8 tprot; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 23 | /* for x_tables compatibility */ |
| 24 | struct xt_action_param xt; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 25 | }; |
| 26 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 27 | static inline void nft_set_pktinfo(struct nft_pktinfo *pkt, |
| 28 | const struct nf_hook_ops *ops, |
| 29 | struct sk_buff *skb, |
David S. Miller | 073bfd5 | 2015-04-03 21:16:25 -0400 | [diff] [blame] | 30 | const struct nf_hook_state *state) |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 31 | { |
| 32 | pkt->skb = skb; |
David S. Miller | 073bfd5 | 2015-04-03 21:16:25 -0400 | [diff] [blame] | 33 | pkt->in = pkt->xt.in = state->in; |
| 34 | pkt->out = pkt->xt.out = state->out; |
Patrick McHardy | c948487 | 2014-01-03 12:16:14 +0000 | [diff] [blame] | 35 | pkt->ops = ops; |
| 36 | pkt->xt.hooknum = ops->hooknum; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 37 | pkt->xt.family = ops->pf; |
| 38 | } |
| 39 | |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 40 | /** |
| 41 | * struct nft_verdict - nf_tables verdict |
| 42 | * |
| 43 | * @code: nf_tables/netfilter verdict code |
| 44 | * @chain: destination chain for NFT_JUMP/NFT_GOTO |
| 45 | */ |
| 46 | struct nft_verdict { |
| 47 | u32 code; |
| 48 | struct nft_chain *chain; |
| 49 | }; |
| 50 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 51 | struct nft_data { |
| 52 | union { |
Patrick McHardy | 1ca2e17 | 2015-04-11 02:27:32 +0100 | [diff] [blame] | 53 | u32 data[4]; |
| 54 | struct nft_verdict verdict; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 55 | }; |
| 56 | } __attribute__((aligned(__alignof__(u64)))); |
| 57 | |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 58 | /** |
| 59 | * struct nft_regs - nf_tables register set |
| 60 | * |
| 61 | * @data: data registers |
| 62 | * @verdict: verdict register |
| 63 | * |
| 64 | * The first four data registers alias to the verdict register. |
| 65 | */ |
| 66 | struct nft_regs { |
| 67 | union { |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 68 | u32 data[20]; |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 69 | struct nft_verdict verdict; |
| 70 | }; |
| 71 | }; |
| 72 | |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 73 | static inline void nft_data_copy(u32 *dst, const struct nft_data *src, |
| 74 | unsigned int len) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 75 | { |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 76 | memcpy(dst, src, len); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static inline void nft_data_debug(const struct nft_data *data) |
| 80 | { |
| 81 | pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n", |
| 82 | data->data[0], data->data[1], |
| 83 | data->data[2], data->data[3]); |
| 84 | } |
| 85 | |
| 86 | /** |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 87 | * struct nft_ctx - nf_tables rule/set context |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 88 | * |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 89 | * @net: net namespace |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 90 | * @afi: address family info |
| 91 | * @table: the table the chain is contained in |
| 92 | * @chain: the chain the rule is contained in |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 93 | * @nla: netlink attributes |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 94 | * @portid: netlink portID of the original message |
| 95 | * @seq: netlink sequence number |
| 96 | * @report: notify via unicast netlink message |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 97 | */ |
| 98 | struct nft_ctx { |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 99 | struct net *net; |
Pablo Neira Ayuso | 7c95f6d | 2014-04-04 01:22:45 +0200 | [diff] [blame] | 100 | struct nft_af_info *afi; |
| 101 | struct nft_table *table; |
| 102 | struct nft_chain *chain; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 103 | const struct nlattr * const *nla; |
Pablo Neira Ayuso | 128ad33 | 2014-05-09 17:14:24 +0200 | [diff] [blame] | 104 | u32 portid; |
| 105 | u32 seq; |
| 106 | bool report; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 109 | struct nft_data_desc { |
| 110 | enum nft_data_types type; |
| 111 | unsigned int len; |
| 112 | }; |
| 113 | |
Patrick McHardy | d0a11fc | 2015-04-11 02:27:38 +0100 | [diff] [blame] | 114 | int nft_data_init(const struct nft_ctx *ctx, |
| 115 | struct nft_data *data, unsigned int size, |
Joe Perches | 5eccdfa | 2013-10-19 22:05:31 -0700 | [diff] [blame] | 116 | struct nft_data_desc *desc, const struct nlattr *nla); |
| 117 | void nft_data_uninit(const struct nft_data *data, enum nft_data_types type); |
| 118 | int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, |
| 119 | enum nft_data_types type, unsigned int len); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 120 | |
| 121 | static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg) |
| 122 | { |
| 123 | return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE; |
| 124 | } |
| 125 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 126 | static inline enum nft_registers nft_type_to_reg(enum nft_data_types type) |
| 127 | { |
| 128 | return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1; |
| 129 | } |
| 130 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 131 | unsigned int nft_parse_register(const struct nlattr *attr); |
| 132 | int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg); |
| 133 | |
Patrick McHardy | d07db98 | 2015-04-11 02:27:30 +0100 | [diff] [blame] | 134 | int nft_validate_register_load(enum nft_registers reg, unsigned int len); |
Patrick McHardy | 1ec1021 | 2015-04-11 02:27:27 +0100 | [diff] [blame] | 135 | int nft_validate_register_store(const struct nft_ctx *ctx, |
| 136 | enum nft_registers reg, |
| 137 | const struct nft_data *data, |
| 138 | enum nft_data_types type, unsigned int len); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 139 | |
Patrick McHardy | 86f1ec3 | 2015-03-03 20:04:20 +0000 | [diff] [blame] | 140 | /** |
| 141 | * struct nft_userdata - user defined data associated with an object |
| 142 | * |
| 143 | * @len: length of the data |
| 144 | * @data: content |
| 145 | * |
| 146 | * The presence of user data is indicated in an object specific fashion, |
| 147 | * so a length of zero can't occur and the value "len" indicates data |
| 148 | * of length len + 1. |
| 149 | */ |
| 150 | struct nft_userdata { |
| 151 | u8 len; |
| 152 | unsigned char data[0]; |
| 153 | }; |
| 154 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 155 | /** |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 156 | * struct nft_set_elem - generic representation of set elements |
| 157 | * |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 158 | * @key: element key |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 159 | * @priv: element private data and extensions |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 160 | */ |
| 161 | struct nft_set_elem { |
Patrick McHardy | 7d74026 | 2015-04-11 02:27:39 +0100 | [diff] [blame] | 162 | union { |
| 163 | u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)]; |
| 164 | struct nft_data val; |
| 165 | } key; |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 166 | void *priv; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | struct nft_set; |
| 170 | struct nft_set_iter { |
| 171 | unsigned int count; |
| 172 | unsigned int skip; |
| 173 | int err; |
| 174 | int (*fn)(const struct nft_ctx *ctx, |
| 175 | const struct nft_set *set, |
| 176 | const struct nft_set_iter *iter, |
| 177 | const struct nft_set_elem *elem); |
| 178 | }; |
| 179 | |
| 180 | /** |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 181 | * struct nft_set_desc - description of set elements |
| 182 | * |
| 183 | * @klen: key length |
| 184 | * @dlen: data length |
| 185 | * @size: number of set elements |
| 186 | */ |
| 187 | struct nft_set_desc { |
| 188 | unsigned int klen; |
| 189 | unsigned int dlen; |
| 190 | unsigned int size; |
| 191 | }; |
| 192 | |
| 193 | /** |
| 194 | * enum nft_set_class - performance class |
| 195 | * |
| 196 | * @NFT_LOOKUP_O_1: constant, O(1) |
| 197 | * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N) |
| 198 | * @NFT_LOOKUP_O_N: linear, O(N) |
| 199 | */ |
| 200 | enum nft_set_class { |
| 201 | NFT_SET_CLASS_O_1, |
| 202 | NFT_SET_CLASS_O_LOG_N, |
| 203 | NFT_SET_CLASS_O_N, |
| 204 | }; |
| 205 | |
| 206 | /** |
| 207 | * struct nft_set_estimate - estimation of memory and performance |
| 208 | * characteristics |
| 209 | * |
| 210 | * @size: required memory |
| 211 | * @class: lookup performance class |
| 212 | */ |
| 213 | struct nft_set_estimate { |
| 214 | unsigned int size; |
| 215 | enum nft_set_class class; |
| 216 | }; |
| 217 | |
Patrick McHardy | b2832dd | 2015-03-25 14:08:48 +0000 | [diff] [blame] | 218 | struct nft_set_ext; |
Patrick McHardy | 22fe54d | 2015-04-05 14:41:08 +0200 | [diff] [blame] | 219 | struct nft_expr; |
Patrick McHardy | b2832dd | 2015-03-25 14:08:48 +0000 | [diff] [blame] | 220 | |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 221 | /** |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 222 | * struct nft_set_ops - nf_tables set operations |
| 223 | * |
| 224 | * @lookup: look up an element within the set |
| 225 | * @insert: insert new element into set |
Patrick McHardy | cc02e45 | 2015-03-25 14:08:50 +0000 | [diff] [blame] | 226 | * @activate: activate new element in the next generation |
| 227 | * @deactivate: deactivate element in the next generation |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 228 | * @remove: remove element from set |
| 229 | * @walk: iterate over all set elemeennts |
| 230 | * @privsize: function to return size of set private data |
| 231 | * @init: initialize private data of new set instance |
| 232 | * @destroy: destroy private data of set instance |
| 233 | * @list: nf_tables_set_ops list node |
| 234 | * @owner: module reference |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 235 | * @elemsize: element private size |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 236 | * @features: features supported by the implementation |
| 237 | */ |
| 238 | struct nft_set_ops { |
| 239 | bool (*lookup)(const struct nft_set *set, |
Patrick McHardy | 8cd8937 | 2015-04-11 02:27:35 +0100 | [diff] [blame] | 240 | const u32 *key, |
Patrick McHardy | b2832dd | 2015-03-25 14:08:48 +0000 | [diff] [blame] | 241 | const struct nft_set_ext **ext); |
Patrick McHardy | 22fe54d | 2015-04-05 14:41:08 +0200 | [diff] [blame] | 242 | bool (*update)(struct nft_set *set, |
Patrick McHardy | 8cd8937 | 2015-04-11 02:27:35 +0100 | [diff] [blame] | 243 | const u32 *key, |
Patrick McHardy | 22fe54d | 2015-04-05 14:41:08 +0200 | [diff] [blame] | 244 | void *(*new)(struct nft_set *, |
| 245 | const struct nft_expr *, |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 246 | struct nft_regs *), |
Patrick McHardy | 22fe54d | 2015-04-05 14:41:08 +0200 | [diff] [blame] | 247 | const struct nft_expr *expr, |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 248 | struct nft_regs *regs, |
Patrick McHardy | 22fe54d | 2015-04-05 14:41:08 +0200 | [diff] [blame] | 249 | const struct nft_set_ext **ext); |
| 250 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 251 | int (*insert)(const struct nft_set *set, |
| 252 | const struct nft_set_elem *elem); |
Patrick McHardy | cc02e45 | 2015-03-25 14:08:50 +0000 | [diff] [blame] | 253 | void (*activate)(const struct nft_set *set, |
| 254 | const struct nft_set_elem *elem); |
| 255 | void * (*deactivate)(const struct nft_set *set, |
| 256 | const struct nft_set_elem *elem); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 257 | void (*remove)(const struct nft_set *set, |
| 258 | const struct nft_set_elem *elem); |
| 259 | void (*walk)(const struct nft_ctx *ctx, |
| 260 | const struct nft_set *set, |
| 261 | struct nft_set_iter *iter); |
| 262 | |
| 263 | unsigned int (*privsize)(const struct nlattr * const nla[]); |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 264 | bool (*estimate)(const struct nft_set_desc *desc, |
| 265 | u32 features, |
| 266 | struct nft_set_estimate *est); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 267 | int (*init)(const struct nft_set *set, |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 268 | const struct nft_set_desc *desc, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 269 | const struct nlattr * const nla[]); |
| 270 | void (*destroy)(const struct nft_set *set); |
| 271 | |
| 272 | struct list_head list; |
| 273 | struct module *owner; |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 274 | unsigned int elemsize; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 275 | u32 features; |
| 276 | }; |
| 277 | |
Joe Perches | 5eccdfa | 2013-10-19 22:05:31 -0700 | [diff] [blame] | 278 | int nft_register_set(struct nft_set_ops *ops); |
| 279 | void nft_unregister_set(struct nft_set_ops *ops); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 280 | |
| 281 | /** |
| 282 | * struct nft_set - nf_tables set instance |
| 283 | * |
| 284 | * @list: table set list node |
| 285 | * @bindings: list of set bindings |
| 286 | * @name: name of the set |
| 287 | * @ktype: key type (numeric type defined by userspace, not used in the kernel) |
| 288 | * @dtype: data type (verdict or numeric type defined by userspace) |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 289 | * @size: maximum set size |
| 290 | * @nelems: number of elements |
Patrick McHardy | 3dd0673 | 2015-04-05 14:41:06 +0200 | [diff] [blame] | 291 | * @ndeact: number of deactivated elements queued for removal |
Patrick McHardy | 761da29 | 2015-03-26 12:39:36 +0000 | [diff] [blame] | 292 | * @timeout: default timeout value in msecs |
| 293 | * @gc_int: garbage collection interval in msecs |
Arturo Borrero | 9363dc4 | 2014-09-23 13:30:41 +0200 | [diff] [blame] | 294 | * @policy: set parameterization (see enum nft_set_policies) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 295 | * @ops: set ops |
Patrick McHardy | cc02e45 | 2015-03-25 14:08:50 +0000 | [diff] [blame] | 296 | * @pnet: network namespace |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 297 | * @flags: set flags |
| 298 | * @klen: key length |
| 299 | * @dlen: data length |
| 300 | * @data: private set data |
| 301 | */ |
| 302 | struct nft_set { |
| 303 | struct list_head list; |
| 304 | struct list_head bindings; |
| 305 | char name[IFNAMSIZ]; |
| 306 | u32 ktype; |
| 307 | u32 dtype; |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 308 | u32 size; |
Patrick McHardy | 3dd0673 | 2015-04-05 14:41:06 +0200 | [diff] [blame] | 309 | atomic_t nelems; |
| 310 | u32 ndeact; |
Patrick McHardy | 761da29 | 2015-03-26 12:39:36 +0000 | [diff] [blame] | 311 | u64 timeout; |
| 312 | u32 gc_int; |
Arturo Borrero | 9363dc4 | 2014-09-23 13:30:41 +0200 | [diff] [blame] | 313 | u16 policy; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 314 | /* runtime data below here */ |
| 315 | const struct nft_set_ops *ops ____cacheline_aligned; |
Patrick McHardy | cc02e45 | 2015-03-25 14:08:50 +0000 | [diff] [blame] | 316 | possible_net_t pnet; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 317 | u16 flags; |
| 318 | u8 klen; |
| 319 | u8 dlen; |
| 320 | unsigned char data[] |
| 321 | __attribute__((aligned(__alignof__(u64)))); |
| 322 | }; |
| 323 | |
| 324 | static inline void *nft_set_priv(const struct nft_set *set) |
| 325 | { |
| 326 | return (void *)set->data; |
| 327 | } |
| 328 | |
Patrick McHardy | 9d09829 | 2015-03-26 12:39:40 +0000 | [diff] [blame] | 329 | static inline struct nft_set *nft_set_container_of(const void *priv) |
| 330 | { |
| 331 | return (void *)priv - offsetof(struct nft_set, data); |
| 332 | } |
| 333 | |
Joe Perches | 5eccdfa | 2013-10-19 22:05:31 -0700 | [diff] [blame] | 334 | struct nft_set *nf_tables_set_lookup(const struct nft_table *table, |
| 335 | const struct nlattr *nla); |
Pablo Neira Ayuso | 958bee1 | 2014-04-03 11:48:44 +0200 | [diff] [blame] | 336 | struct nft_set *nf_tables_set_lookup_byid(const struct net *net, |
| 337 | const struct nlattr *nla); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 338 | |
Patrick McHardy | 761da29 | 2015-03-26 12:39:36 +0000 | [diff] [blame] | 339 | static inline unsigned long nft_set_gc_interval(const struct nft_set *set) |
| 340 | { |
| 341 | return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ; |
| 342 | } |
| 343 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 344 | /** |
| 345 | * struct nft_set_binding - nf_tables set binding |
| 346 | * |
| 347 | * @list: set bindings list node |
| 348 | * @chain: chain containing the rule bound to the set |
Patrick McHardy | 11113e1 | 2015-04-05 14:41:07 +0200 | [diff] [blame] | 349 | * @flags: set action flags |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 350 | * |
| 351 | * A set binding contains all information necessary for validation |
| 352 | * of new elements added to a bound set. |
| 353 | */ |
| 354 | struct nft_set_binding { |
| 355 | struct list_head list; |
| 356 | const struct nft_chain *chain; |
Patrick McHardy | 11113e1 | 2015-04-05 14:41:07 +0200 | [diff] [blame] | 357 | u32 flags; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 358 | }; |
| 359 | |
Joe Perches | 5eccdfa | 2013-10-19 22:05:31 -0700 | [diff] [blame] | 360 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, |
| 361 | struct nft_set_binding *binding); |
| 362 | void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, |
| 363 | struct nft_set_binding *binding); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 364 | |
Patrick McHardy | 3ac4c07 | 2015-03-25 13:07:49 +0000 | [diff] [blame] | 365 | /** |
| 366 | * enum nft_set_extensions - set extension type IDs |
| 367 | * |
| 368 | * @NFT_SET_EXT_KEY: element key |
| 369 | * @NFT_SET_EXT_DATA: mapping data |
| 370 | * @NFT_SET_EXT_FLAGS: element flags |
Patrick McHardy | c3e1b00 | 2015-03-26 12:39:37 +0000 | [diff] [blame] | 371 | * @NFT_SET_EXT_TIMEOUT: element timeout |
| 372 | * @NFT_SET_EXT_EXPIRATION: element expiration time |
Patrick McHardy | 68e942e | 2015-04-05 14:43:38 +0200 | [diff] [blame] | 373 | * @NFT_SET_EXT_USERDATA: user data associated with the element |
Patrick McHardy | f25ad2e | 2015-04-11 10:46:39 +0100 | [diff] [blame] | 374 | * @NFT_SET_EXT_EXPR: expression assiociated with the element |
Patrick McHardy | 3ac4c07 | 2015-03-25 13:07:49 +0000 | [diff] [blame] | 375 | * @NFT_SET_EXT_NUM: number of extension types |
| 376 | */ |
| 377 | enum nft_set_extensions { |
| 378 | NFT_SET_EXT_KEY, |
| 379 | NFT_SET_EXT_DATA, |
| 380 | NFT_SET_EXT_FLAGS, |
Patrick McHardy | c3e1b00 | 2015-03-26 12:39:37 +0000 | [diff] [blame] | 381 | NFT_SET_EXT_TIMEOUT, |
| 382 | NFT_SET_EXT_EXPIRATION, |
Patrick McHardy | 68e942e | 2015-04-05 14:43:38 +0200 | [diff] [blame] | 383 | NFT_SET_EXT_USERDATA, |
Patrick McHardy | f25ad2e | 2015-04-11 10:46:39 +0100 | [diff] [blame] | 384 | NFT_SET_EXT_EXPR, |
Patrick McHardy | 3ac4c07 | 2015-03-25 13:07:49 +0000 | [diff] [blame] | 385 | NFT_SET_EXT_NUM |
| 386 | }; |
| 387 | |
| 388 | /** |
| 389 | * struct nft_set_ext_type - set extension type |
| 390 | * |
| 391 | * @len: fixed part length of the extension |
| 392 | * @align: alignment requirements of the extension |
| 393 | */ |
| 394 | struct nft_set_ext_type { |
| 395 | u8 len; |
| 396 | u8 align; |
| 397 | }; |
| 398 | |
| 399 | extern const struct nft_set_ext_type nft_set_ext_types[]; |
| 400 | |
| 401 | /** |
| 402 | * struct nft_set_ext_tmpl - set extension template |
| 403 | * |
| 404 | * @len: length of extension area |
| 405 | * @offset: offsets of individual extension types |
| 406 | */ |
| 407 | struct nft_set_ext_tmpl { |
| 408 | u16 len; |
| 409 | u8 offset[NFT_SET_EXT_NUM]; |
| 410 | }; |
| 411 | |
| 412 | /** |
| 413 | * struct nft_set_ext - set extensions |
| 414 | * |
Patrick McHardy | cc02e45 | 2015-03-25 14:08:50 +0000 | [diff] [blame] | 415 | * @genmask: generation mask |
Patrick McHardy | 3ac4c07 | 2015-03-25 13:07:49 +0000 | [diff] [blame] | 416 | * @offset: offsets of individual extension types |
| 417 | * @data: beginning of extension data |
| 418 | */ |
| 419 | struct nft_set_ext { |
Patrick McHardy | cc02e45 | 2015-03-25 14:08:50 +0000 | [diff] [blame] | 420 | u8 genmask; |
Patrick McHardy | 3ac4c07 | 2015-03-25 13:07:49 +0000 | [diff] [blame] | 421 | u8 offset[NFT_SET_EXT_NUM]; |
| 422 | char data[0]; |
| 423 | }; |
| 424 | |
| 425 | static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl) |
| 426 | { |
| 427 | memset(tmpl, 0, sizeof(*tmpl)); |
| 428 | tmpl->len = sizeof(struct nft_set_ext); |
| 429 | } |
| 430 | |
| 431 | static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id, |
| 432 | unsigned int len) |
| 433 | { |
| 434 | tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align); |
| 435 | BUG_ON(tmpl->len > U8_MAX); |
| 436 | tmpl->offset[id] = tmpl->len; |
| 437 | tmpl->len += nft_set_ext_types[id].len + len; |
| 438 | } |
| 439 | |
| 440 | static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id) |
| 441 | { |
| 442 | nft_set_ext_add_length(tmpl, id, 0); |
| 443 | } |
| 444 | |
| 445 | static inline void nft_set_ext_init(struct nft_set_ext *ext, |
| 446 | const struct nft_set_ext_tmpl *tmpl) |
| 447 | { |
| 448 | memcpy(ext->offset, tmpl->offset, sizeof(ext->offset)); |
| 449 | } |
| 450 | |
| 451 | static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id) |
| 452 | { |
| 453 | return !!ext->offset[id]; |
| 454 | } |
| 455 | |
| 456 | static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id) |
| 457 | { |
| 458 | return ext && __nft_set_ext_exists(ext, id); |
| 459 | } |
| 460 | |
| 461 | static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id) |
| 462 | { |
| 463 | return (void *)ext + ext->offset[id]; |
| 464 | } |
| 465 | |
| 466 | static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext) |
| 467 | { |
| 468 | return nft_set_ext(ext, NFT_SET_EXT_KEY); |
| 469 | } |
| 470 | |
| 471 | static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext) |
| 472 | { |
| 473 | return nft_set_ext(ext, NFT_SET_EXT_DATA); |
| 474 | } |
| 475 | |
| 476 | static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext) |
| 477 | { |
| 478 | return nft_set_ext(ext, NFT_SET_EXT_FLAGS); |
| 479 | } |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 480 | |
Patrick McHardy | c3e1b00 | 2015-03-26 12:39:37 +0000 | [diff] [blame] | 481 | static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext) |
| 482 | { |
| 483 | return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT); |
| 484 | } |
| 485 | |
| 486 | static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext *ext) |
| 487 | { |
| 488 | return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION); |
| 489 | } |
| 490 | |
Patrick McHardy | 68e942e | 2015-04-05 14:43:38 +0200 | [diff] [blame] | 491 | static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext) |
| 492 | { |
| 493 | return nft_set_ext(ext, NFT_SET_EXT_USERDATA); |
| 494 | } |
| 495 | |
Patrick McHardy | f25ad2e | 2015-04-11 10:46:39 +0100 | [diff] [blame] | 496 | static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext) |
| 497 | { |
| 498 | return nft_set_ext(ext, NFT_SET_EXT_EXPR); |
| 499 | } |
| 500 | |
Patrick McHardy | c3e1b00 | 2015-03-26 12:39:37 +0000 | [diff] [blame] | 501 | static inline bool nft_set_elem_expired(const struct nft_set_ext *ext) |
| 502 | { |
| 503 | return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) && |
| 504 | time_is_before_eq_jiffies(*nft_set_ext_expiration(ext)); |
| 505 | } |
| 506 | |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 507 | static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set, |
| 508 | void *elem) |
| 509 | { |
| 510 | return elem + set->ops->elemsize; |
| 511 | } |
| 512 | |
Patrick McHardy | 22fe54d | 2015-04-05 14:41:08 +0200 | [diff] [blame] | 513 | void *nft_set_elem_init(const struct nft_set *set, |
| 514 | const struct nft_set_ext_tmpl *tmpl, |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 515 | const u32 *key, const u32 *data, |
Patrick McHardy | 22fe54d | 2015-04-05 14:41:08 +0200 | [diff] [blame] | 516 | u64 timeout, gfp_t gfp); |
Patrick McHardy | 61edafb | 2015-03-25 14:08:47 +0000 | [diff] [blame] | 517 | void nft_set_elem_destroy(const struct nft_set *set, void *elem); |
| 518 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 519 | /** |
Patrick McHardy | cfed7e1 | 2015-03-26 12:39:38 +0000 | [diff] [blame] | 520 | * struct nft_set_gc_batch_head - nf_tables set garbage collection batch |
| 521 | * |
| 522 | * @rcu: rcu head |
| 523 | * @set: set the elements belong to |
| 524 | * @cnt: count of elements |
| 525 | */ |
| 526 | struct nft_set_gc_batch_head { |
| 527 | struct rcu_head rcu; |
| 528 | const struct nft_set *set; |
| 529 | unsigned int cnt; |
| 530 | }; |
| 531 | |
| 532 | #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \ |
| 533 | sizeof(struct nft_set_gc_batch_head)) / \ |
| 534 | sizeof(void *)) |
| 535 | |
| 536 | /** |
| 537 | * struct nft_set_gc_batch - nf_tables set garbage collection batch |
| 538 | * |
| 539 | * @head: GC batch head |
| 540 | * @elems: garbage collection elements |
| 541 | */ |
| 542 | struct nft_set_gc_batch { |
| 543 | struct nft_set_gc_batch_head head; |
| 544 | void *elems[NFT_SET_GC_BATCH_SIZE]; |
| 545 | }; |
| 546 | |
| 547 | struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set, |
| 548 | gfp_t gfp); |
| 549 | void nft_set_gc_batch_release(struct rcu_head *rcu); |
| 550 | |
| 551 | static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb) |
| 552 | { |
| 553 | if (gcb != NULL) |
| 554 | call_rcu(&gcb->head.rcu, nft_set_gc_batch_release); |
| 555 | } |
| 556 | |
| 557 | static inline struct nft_set_gc_batch * |
| 558 | nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb, |
| 559 | gfp_t gfp) |
| 560 | { |
| 561 | if (gcb != NULL) { |
| 562 | if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems)) |
| 563 | return gcb; |
| 564 | nft_set_gc_batch_complete(gcb); |
| 565 | } |
| 566 | return nft_set_gc_batch_alloc(set, gfp); |
| 567 | } |
| 568 | |
| 569 | static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb, |
| 570 | void *elem) |
| 571 | { |
| 572 | gcb->elems[gcb->head.cnt++] = elem; |
| 573 | } |
| 574 | |
| 575 | /** |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 576 | * struct nft_expr_type - nf_tables expression type |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 577 | * |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 578 | * @select_ops: function to select nft_expr_ops |
| 579 | * @ops: default ops, used when no select_ops functions is present |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 580 | * @list: used internally |
| 581 | * @name: Identifier |
| 582 | * @owner: module reference |
| 583 | * @policy: netlink attribute policy |
| 584 | * @maxattr: highest netlink attribute number |
Patrick McHardy | 64d4680 | 2014-02-05 15:03:37 +0000 | [diff] [blame] | 585 | * @family: address family for AF-specific types |
Patrick McHardy | 151d799 | 2015-04-11 10:46:40 +0100 | [diff] [blame] | 586 | * @flags: expression type flags |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 587 | */ |
| 588 | struct nft_expr_type { |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 589 | const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *, |
| 590 | const struct nlattr * const tb[]); |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 591 | const struct nft_expr_ops *ops; |
| 592 | struct list_head list; |
| 593 | const char *name; |
| 594 | struct module *owner; |
| 595 | const struct nla_policy *policy; |
| 596 | unsigned int maxattr; |
Patrick McHardy | 64d4680 | 2014-02-05 15:03:37 +0000 | [diff] [blame] | 597 | u8 family; |
Patrick McHardy | 151d799 | 2015-04-11 10:46:40 +0100 | [diff] [blame] | 598 | u8 flags; |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 599 | }; |
| 600 | |
Patrick McHardy | 151d799 | 2015-04-11 10:46:40 +0100 | [diff] [blame] | 601 | #define NFT_EXPR_STATEFUL 0x1 |
| 602 | |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 603 | /** |
| 604 | * struct nft_expr_ops - nf_tables expression operations |
| 605 | * |
| 606 | * @eval: Expression evaluation function |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 607 | * @size: full expression size, including private data size |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 608 | * @init: initialization function |
| 609 | * @destroy: destruction function |
| 610 | * @dump: function to dump parameters |
| 611 | * @type: expression type |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 612 | * @validate: validate expression, called during loop detection |
| 613 | * @data: extra data to attach to this expression operation |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 614 | */ |
| 615 | struct nft_expr; |
| 616 | struct nft_expr_ops { |
| 617 | void (*eval)(const struct nft_expr *expr, |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 618 | struct nft_regs *regs, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 619 | const struct nft_pktinfo *pkt); |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 620 | unsigned int size; |
| 621 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 622 | int (*init)(const struct nft_ctx *ctx, |
| 623 | const struct nft_expr *expr, |
| 624 | const struct nlattr * const tb[]); |
Patrick McHardy | 62472bc | 2014-03-07 19:08:30 +0100 | [diff] [blame] | 625 | void (*destroy)(const struct nft_ctx *ctx, |
| 626 | const struct nft_expr *expr); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 627 | int (*dump)(struct sk_buff *skb, |
| 628 | const struct nft_expr *expr); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 629 | int (*validate)(const struct nft_ctx *ctx, |
| 630 | const struct nft_expr *expr, |
| 631 | const struct nft_data **data); |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 632 | const struct nft_expr_type *type; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 633 | void *data; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 634 | }; |
| 635 | |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 636 | #define NFT_EXPR_MAXATTR 16 |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 637 | #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \ |
| 638 | ALIGN(size, __alignof__(struct nft_expr))) |
| 639 | |
| 640 | /** |
| 641 | * struct nft_expr - nf_tables expression |
| 642 | * |
| 643 | * @ops: expression ops |
| 644 | * @data: expression private data |
| 645 | */ |
| 646 | struct nft_expr { |
| 647 | const struct nft_expr_ops *ops; |
| 648 | unsigned char data[]; |
| 649 | }; |
| 650 | |
| 651 | static inline void *nft_expr_priv(const struct nft_expr *expr) |
| 652 | { |
| 653 | return (void *)expr->data; |
| 654 | } |
| 655 | |
Patrick McHardy | 0b2d8a7 | 2015-04-11 10:46:38 +0100 | [diff] [blame] | 656 | struct nft_expr *nft_expr_init(const struct nft_ctx *ctx, |
| 657 | const struct nlattr *nla); |
| 658 | void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr); |
| 659 | int nft_expr_dump(struct sk_buff *skb, unsigned int attr, |
| 660 | const struct nft_expr *expr); |
| 661 | |
| 662 | static inline void nft_expr_clone(struct nft_expr *dst, struct nft_expr *src) |
| 663 | { |
| 664 | __module_get(src->ops->type->owner); |
| 665 | memcpy(dst, src, src->ops->size); |
| 666 | } |
| 667 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 668 | /** |
| 669 | * struct nft_rule - nf_tables rule |
| 670 | * |
| 671 | * @list: used internally |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 672 | * @handle: rule handle |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 673 | * @genmask: generation mask |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 674 | * @dlen: length of expression data |
Patrick McHardy | 86f1ec3 | 2015-03-03 20:04:20 +0000 | [diff] [blame] | 675 | * @udata: user data is appended to the rule |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 676 | * @data: expression data |
| 677 | */ |
| 678 | struct nft_rule { |
| 679 | struct list_head list; |
Pablo Neira Ayuso | 0768b3b | 2014-02-19 17:27:06 +0100 | [diff] [blame] | 680 | u64 handle:42, |
Pablo Neira Ayuso | 0628b12 | 2013-10-14 11:05:33 +0200 | [diff] [blame] | 681 | genmask:2, |
Pablo Neira Ayuso | 0768b3b | 2014-02-19 17:27:06 +0100 | [diff] [blame] | 682 | dlen:12, |
Patrick McHardy | 86f1ec3 | 2015-03-03 20:04:20 +0000 | [diff] [blame] | 683 | udata:1; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 684 | unsigned char data[] |
| 685 | __attribute__((aligned(__alignof__(struct nft_expr)))); |
| 686 | }; |
| 687 | |
| 688 | static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule) |
| 689 | { |
| 690 | return (struct nft_expr *)&rule->data[0]; |
| 691 | } |
| 692 | |
| 693 | static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr) |
| 694 | { |
| 695 | return ((void *)expr) + expr->ops->size; |
| 696 | } |
| 697 | |
| 698 | static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule) |
| 699 | { |
| 700 | return (struct nft_expr *)&rule->data[rule->dlen]; |
| 701 | } |
| 702 | |
Patrick McHardy | 86f1ec3 | 2015-03-03 20:04:20 +0000 | [diff] [blame] | 703 | static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule) |
Pablo Neira Ayuso | 0768b3b | 2014-02-19 17:27:06 +0100 | [diff] [blame] | 704 | { |
| 705 | return (void *)&rule->data[rule->dlen]; |
| 706 | } |
| 707 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 708 | /* |
| 709 | * The last pointer isn't really necessary, but the compiler isn't able to |
| 710 | * determine that the result of nft_expr_last() is always the same since it |
| 711 | * can't assume that the dlen value wasn't changed within calls in the loop. |
| 712 | */ |
| 713 | #define nft_rule_for_each_expr(expr, last, rule) \ |
| 714 | for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \ |
| 715 | (expr) != (last); \ |
| 716 | (expr) = nft_expr_next(expr)) |
| 717 | |
| 718 | enum nft_chain_flags { |
| 719 | NFT_BASE_CHAIN = 0x1, |
Pablo Neira Ayuso | 91c7b38 | 2014-04-09 11:58:08 +0200 | [diff] [blame] | 720 | NFT_CHAIN_INACTIVE = 0x2, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 721 | }; |
| 722 | |
| 723 | /** |
| 724 | * struct nft_chain - nf_tables chain |
| 725 | * |
| 726 | * @rules: list of rules in the chain |
| 727 | * @list: used internally |
Pablo Neira Ayuso | b5bc89b | 2013-10-10 16:49:19 +0200 | [diff] [blame] | 728 | * @table: table that this chain belongs to |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 729 | * @handle: chain handle |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 730 | * @use: number of jump references to this chain |
| 731 | * @level: length of longest path to this chain |
Pablo Neira Ayuso | a0a7379 | 2014-06-10 10:53:01 +0200 | [diff] [blame] | 732 | * @flags: bitmask of enum nft_chain_flags |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 733 | * @name: name of the chain |
| 734 | */ |
| 735 | struct nft_chain { |
| 736 | struct list_head rules; |
| 737 | struct list_head list; |
Pablo Neira Ayuso | b5bc89b | 2013-10-10 16:49:19 +0200 | [diff] [blame] | 738 | struct nft_table *table; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 739 | u64 handle; |
Pablo Neira Ayuso | a0a7379 | 2014-06-10 10:53:01 +0200 | [diff] [blame] | 740 | u32 use; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 741 | u16 level; |
Pablo Neira Ayuso | a0a7379 | 2014-06-10 10:53:01 +0200 | [diff] [blame] | 742 | u8 flags; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 743 | char name[NFT_CHAIN_MAXNAMELEN]; |
| 744 | }; |
| 745 | |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 746 | enum nft_chain_type { |
| 747 | NFT_CHAIN_T_DEFAULT = 0, |
| 748 | NFT_CHAIN_T_ROUTE, |
| 749 | NFT_CHAIN_T_NAT, |
| 750 | NFT_CHAIN_T_MAX |
| 751 | }; |
| 752 | |
Patrick McHardy | 1a1e1a1 | 2015-03-03 20:10:06 +0000 | [diff] [blame] | 753 | /** |
| 754 | * struct nf_chain_type - nf_tables chain type info |
| 755 | * |
| 756 | * @name: name of the type |
| 757 | * @type: numeric identifier |
| 758 | * @family: address family |
| 759 | * @owner: module owner |
| 760 | * @hook_mask: mask of valid hooks |
| 761 | * @hooks: hookfn overrides |
| 762 | */ |
| 763 | struct nf_chain_type { |
| 764 | const char *name; |
| 765 | enum nft_chain_type type; |
| 766 | int family; |
| 767 | struct module *owner; |
| 768 | unsigned int hook_mask; |
| 769 | nf_hookfn *hooks[NF_MAX_HOOKS]; |
| 770 | }; |
| 771 | |
Pablo Neira Ayuso | 7210e4e | 2014-10-13 19:50:22 +0200 | [diff] [blame] | 772 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
| 773 | enum nft_chain_type type); |
Pablo Neira Ayuso | 75e8d06 | 2015-01-14 15:33:57 +0100 | [diff] [blame] | 774 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
| 775 | unsigned int hook_flags); |
Pablo Neira Ayuso | 7210e4e | 2014-10-13 19:50:22 +0200 | [diff] [blame] | 776 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 777 | struct nft_stats { |
Eric Dumazet | ce355e2 | 2014-07-09 15:14:06 +0200 | [diff] [blame] | 778 | u64 bytes; |
| 779 | u64 pkts; |
| 780 | struct u64_stats_sync syncp; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 781 | }; |
| 782 | |
Patrick McHardy | 115a60b1 | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 783 | #define NFT_HOOK_OPS_MAX 2 |
| 784 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 785 | /** |
| 786 | * struct nft_base_chain - nf_tables base chain |
| 787 | * |
| 788 | * @ops: netfilter hook ops |
Patrick McHardy | 5ebb335 | 2015-03-21 15:19:15 +0000 | [diff] [blame] | 789 | * @pnet: net namespace that this chain belongs to |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 790 | * @type: chain type |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 791 | * @policy: default policy |
| 792 | * @stats: per-cpu chain stats |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 793 | * @chain: the chain |
Pablo Neira Ayuso | 2cbce13 | 2015-06-12 13:55:41 +0200 | [diff] [blame^] | 794 | * @dev_name: device name that this base chain is attached to (if any) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 795 | */ |
| 796 | struct nft_base_chain { |
Patrick McHardy | 115a60b1 | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 797 | struct nf_hook_ops ops[NFT_HOOK_OPS_MAX]; |
Patrick McHardy | 5ebb335 | 2015-03-21 15:19:15 +0000 | [diff] [blame] | 798 | possible_net_t pnet; |
Patrick McHardy | 2a37d75 | 2014-01-09 18:42:37 +0000 | [diff] [blame] | 799 | const struct nf_chain_type *type; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 800 | u8 policy; |
| 801 | struct nft_stats __percpu *stats; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 802 | struct nft_chain chain; |
Pablo Neira Ayuso | 2cbce13 | 2015-06-12 13:55:41 +0200 | [diff] [blame^] | 803 | char dev_name[IFNAMSIZ]; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 804 | }; |
| 805 | |
| 806 | static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain) |
| 807 | { |
| 808 | return container_of(chain, struct nft_base_chain, chain); |
| 809 | } |
| 810 | |
Patrick McHardy | 3876d22 | 2014-01-09 18:42:43 +0000 | [diff] [blame] | 811 | unsigned int nft_do_chain(struct nft_pktinfo *pkt, |
| 812 | const struct nf_hook_ops *ops); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 813 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 814 | /** |
| 815 | * struct nft_table - nf_tables table |
| 816 | * |
| 817 | * @list: used internally |
| 818 | * @chains: chains in the table |
| 819 | * @sets: sets in the table |
| 820 | * @hgenerator: handle generator state |
| 821 | * @use: number of chain references to this table |
| 822 | * @flags: table flag (see enum nft_table_flags) |
| 823 | * @name: name of the table |
| 824 | */ |
| 825 | struct nft_table { |
| 826 | struct list_head list; |
| 827 | struct list_head chains; |
| 828 | struct list_head sets; |
| 829 | u64 hgenerator; |
| 830 | u32 use; |
| 831 | u16 flags; |
Pablo Neira Ayuso | 1cae565 | 2015-03-05 15:05:36 +0100 | [diff] [blame] | 832 | char name[NFT_TABLE_MAXNAMELEN]; |
Pablo Neira Ayuso | ebddf1a | 2015-05-26 18:41:20 +0200 | [diff] [blame] | 833 | }; |
| 834 | |
| 835 | enum nft_af_flags { |
| 836 | NFT_AF_NEEDS_DEV = (1 << 0), |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 837 | }; |
| 838 | |
| 839 | /** |
| 840 | * struct nft_af_info - nf_tables address family info |
| 841 | * |
| 842 | * @list: used internally |
| 843 | * @family: address family |
| 844 | * @nhooks: number of hooks in this family |
| 845 | * @owner: module owner |
| 846 | * @tables: used internally |
Pablo Neira Ayuso | ebddf1a | 2015-05-26 18:41:20 +0200 | [diff] [blame] | 847 | * @flags: family flags |
Patrick McHardy | 115a60b1 | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 848 | * @nops: number of hook ops in this family |
| 849 | * @hook_ops_init: initialization function for chain hook ops |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 850 | * @hooks: hookfn overrides for packet validation |
| 851 | */ |
| 852 | struct nft_af_info { |
| 853 | struct list_head list; |
| 854 | int family; |
| 855 | unsigned int nhooks; |
| 856 | struct module *owner; |
| 857 | struct list_head tables; |
Pablo Neira Ayuso | ebddf1a | 2015-05-26 18:41:20 +0200 | [diff] [blame] | 858 | u32 flags; |
Patrick McHardy | 115a60b1 | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 859 | unsigned int nops; |
| 860 | void (*hook_ops_init)(struct nf_hook_ops *, |
| 861 | unsigned int); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 862 | nf_hookfn *hooks[NF_MAX_HOOKS]; |
| 863 | }; |
| 864 | |
Joe Perches | 5eccdfa | 2013-10-19 22:05:31 -0700 | [diff] [blame] | 865 | int nft_register_afinfo(struct net *, struct nft_af_info *); |
| 866 | void nft_unregister_afinfo(struct nft_af_info *); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 867 | |
Patrick McHardy | 2a37d75 | 2014-01-09 18:42:37 +0000 | [diff] [blame] | 868 | int nft_register_chain_type(const struct nf_chain_type *); |
| 869 | void nft_unregister_chain_type(const struct nf_chain_type *); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 870 | |
Joe Perches | 5eccdfa | 2013-10-19 22:05:31 -0700 | [diff] [blame] | 871 | int nft_register_expr(struct nft_expr_type *); |
| 872 | void nft_unregister_expr(struct nft_expr_type *); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 873 | |
Patrick McHardy | 67a8fc2 | 2014-02-18 18:06:49 +0000 | [diff] [blame] | 874 | #define nft_dereference(p) \ |
| 875 | nfnl_dereference(p, NFNL_SUBSYS_NFTABLES) |
| 876 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 877 | #define MODULE_ALIAS_NFT_FAMILY(family) \ |
| 878 | MODULE_ALIAS("nft-afinfo-" __stringify(family)) |
| 879 | |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 880 | #define MODULE_ALIAS_NFT_CHAIN(family, name) \ |
| 881 | MODULE_ALIAS("nft-chain-" __stringify(family) "-" name) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 882 | |
Patrick McHardy | 64d4680 | 2014-02-05 15:03:37 +0000 | [diff] [blame] | 883 | #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \ |
| 884 | MODULE_ALIAS("nft-expr-" __stringify(family) "-" name) |
| 885 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 886 | #define MODULE_ALIAS_NFT_EXPR(name) \ |
| 887 | MODULE_ALIAS("nft-expr-" name) |
| 888 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 889 | #define MODULE_ALIAS_NFT_SET() \ |
| 890 | MODULE_ALIAS("nft-set") |
| 891 | |
Patrick McHardy | ea4bd99 | 2015-03-25 14:08:49 +0000 | [diff] [blame] | 892 | /* |
| 893 | * The gencursor defines two generations, the currently active and the |
| 894 | * next one. Objects contain a bitmask of 2 bits specifying the generations |
| 895 | * they're active in. A set bit means they're inactive in the generation |
| 896 | * represented by that bit. |
| 897 | * |
| 898 | * New objects start out as inactive in the current and active in the |
| 899 | * next generation. When committing the ruleset the bitmask is cleared, |
| 900 | * meaning they're active in all generations. When removing an object, |
| 901 | * it is set inactive in the next generation. After committing the ruleset, |
| 902 | * the objects are removed. |
| 903 | */ |
| 904 | static inline unsigned int nft_gencursor_next(const struct net *net) |
| 905 | { |
| 906 | return net->nft.gencursor + 1 == 1 ? 1 : 0; |
| 907 | } |
| 908 | |
| 909 | static inline u8 nft_genmask_next(const struct net *net) |
| 910 | { |
| 911 | return 1 << nft_gencursor_next(net); |
| 912 | } |
| 913 | |
| 914 | static inline u8 nft_genmask_cur(const struct net *net) |
| 915 | { |
| 916 | /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */ |
| 917 | return 1 << ACCESS_ONCE(net->nft.gencursor); |
| 918 | } |
| 919 | |
Patrick McHardy | 22fe54d | 2015-04-05 14:41:08 +0200 | [diff] [blame] | 920 | #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1)) |
| 921 | |
Patrick McHardy | cc02e45 | 2015-03-25 14:08:50 +0000 | [diff] [blame] | 922 | /* |
| 923 | * Set element transaction helpers |
| 924 | */ |
| 925 | |
| 926 | static inline bool nft_set_elem_active(const struct nft_set_ext *ext, |
| 927 | u8 genmask) |
| 928 | { |
| 929 | return !(ext->genmask & genmask); |
| 930 | } |
| 931 | |
| 932 | static inline void nft_set_elem_change_active(const struct nft_set *set, |
| 933 | struct nft_set_ext *ext) |
| 934 | { |
| 935 | ext->genmask ^= nft_genmask_next(read_pnet(&set->pnet)); |
| 936 | } |
| 937 | |
Patrick McHardy | 6908665 | 2015-03-26 12:39:39 +0000 | [diff] [blame] | 938 | /* |
| 939 | * We use a free bit in the genmask field to indicate the element |
| 940 | * is busy, meaning it is currently being processed either by |
| 941 | * the netlink API or GC. |
| 942 | * |
| 943 | * Even though the genmask is only a single byte wide, this works |
| 944 | * because the extension structure if fully constant once initialized, |
| 945 | * so there are no non-atomic write accesses unless it is already |
| 946 | * marked busy. |
| 947 | */ |
| 948 | #define NFT_SET_ELEM_BUSY_MASK (1 << 2) |
| 949 | |
| 950 | #if defined(__LITTLE_ENDIAN_BITFIELD) |
| 951 | #define NFT_SET_ELEM_BUSY_BIT 2 |
| 952 | #elif defined(__BIG_ENDIAN_BITFIELD) |
| 953 | #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2) |
| 954 | #else |
| 955 | #error |
| 956 | #endif |
| 957 | |
| 958 | static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext) |
| 959 | { |
| 960 | unsigned long *word = (unsigned long *)ext; |
| 961 | |
| 962 | BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0); |
| 963 | return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word); |
| 964 | } |
| 965 | |
| 966 | static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext) |
| 967 | { |
| 968 | unsigned long *word = (unsigned long *)ext; |
| 969 | |
| 970 | clear_bit(NFT_SET_ELEM_BUSY_BIT, word); |
| 971 | } |
| 972 | |
Patrick McHardy | 1a1e1a1 | 2015-03-03 20:10:06 +0000 | [diff] [blame] | 973 | /** |
| 974 | * struct nft_trans - nf_tables object update in transaction |
| 975 | * |
| 976 | * @list: used internally |
| 977 | * @msg_type: message type |
| 978 | * @ctx: transaction context |
| 979 | * @data: internal information related to the transaction |
| 980 | */ |
| 981 | struct nft_trans { |
| 982 | struct list_head list; |
| 983 | int msg_type; |
| 984 | struct nft_ctx ctx; |
| 985 | char data[0]; |
| 986 | }; |
| 987 | |
| 988 | struct nft_trans_rule { |
| 989 | struct nft_rule *rule; |
| 990 | }; |
| 991 | |
| 992 | #define nft_trans_rule(trans) \ |
| 993 | (((struct nft_trans_rule *)trans->data)->rule) |
| 994 | |
| 995 | struct nft_trans_set { |
| 996 | struct nft_set *set; |
| 997 | u32 set_id; |
| 998 | }; |
| 999 | |
| 1000 | #define nft_trans_set(trans) \ |
| 1001 | (((struct nft_trans_set *)trans->data)->set) |
| 1002 | #define nft_trans_set_id(trans) \ |
| 1003 | (((struct nft_trans_set *)trans->data)->set_id) |
| 1004 | |
| 1005 | struct nft_trans_chain { |
| 1006 | bool update; |
| 1007 | char name[NFT_CHAIN_MAXNAMELEN]; |
| 1008 | struct nft_stats __percpu *stats; |
| 1009 | u8 policy; |
| 1010 | }; |
| 1011 | |
| 1012 | #define nft_trans_chain_update(trans) \ |
| 1013 | (((struct nft_trans_chain *)trans->data)->update) |
| 1014 | #define nft_trans_chain_name(trans) \ |
| 1015 | (((struct nft_trans_chain *)trans->data)->name) |
| 1016 | #define nft_trans_chain_stats(trans) \ |
| 1017 | (((struct nft_trans_chain *)trans->data)->stats) |
| 1018 | #define nft_trans_chain_policy(trans) \ |
| 1019 | (((struct nft_trans_chain *)trans->data)->policy) |
| 1020 | |
| 1021 | struct nft_trans_table { |
| 1022 | bool update; |
| 1023 | bool enable; |
| 1024 | }; |
| 1025 | |
| 1026 | #define nft_trans_table_update(trans) \ |
| 1027 | (((struct nft_trans_table *)trans->data)->update) |
| 1028 | #define nft_trans_table_enable(trans) \ |
| 1029 | (((struct nft_trans_table *)trans->data)->enable) |
| 1030 | |
| 1031 | struct nft_trans_elem { |
| 1032 | struct nft_set *set; |
| 1033 | struct nft_set_elem elem; |
| 1034 | }; |
| 1035 | |
| 1036 | #define nft_trans_elem_set(trans) \ |
| 1037 | (((struct nft_trans_elem *)trans->data)->set) |
| 1038 | #define nft_trans_elem(trans) \ |
| 1039 | (((struct nft_trans_elem *)trans->data)->elem) |
| 1040 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1041 | #endif /* _NET_NF_TABLES_H */ |