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