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