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