| 1 |
/* SPDX-License-Identifier: GPL-2.0 */ |
| 2 |
#ifndef _NET_NF_TABLES_H |
| 3 |
#define _NET_NF_TABLES_H |
| 4 |
|
| 5 |
#include <linux/unaligned.h> |
| 6 |
#include <linux/list.h> |
| 7 |
#include <linux/netfilter.h> |
| 8 |
#include <linux/netfilter/nfnetlink.h> |
| 9 |
#include <linux/netfilter/nf_tables.h> |
| 10 |
#include <linux/u64_stats_sync.h> |
| 11 |
#include <linux/rhashtable.h> |
| 12 |
#include <net/netfilter/nf_flow_table.h> |
| 13 |
#include <net/netlink.h> |
| 14 |
#include <net/flow_offload.h> |
| 15 |
#include <net/netns/generic.h> |
| 16 |
|
| 17 |
#define NFT_MAX_HOOKS (NF_INET_INGRESS + 1) |
| 18 |
|
| 19 |
struct module; |
| 20 |
|
| 21 |
#define NFT_JUMP_STACK_SIZE 16 |
| 22 |
|
| 23 |
enum { |
| 24 |
NFT_PKTINFO_L4PROTO = (1 << 0), |
| 25 |
NFT_PKTINFO_INNER = (1 << 1), |
| 26 |
NFT_PKTINFO_INNER_FULL = (1 << 2), |
| 27 |
}; |
| 28 |
|
| 29 |
struct nft_pktinfo { |
| 30 |
struct sk_buff *skb; |
| 31 |
const struct nf_hook_state *state; |
| 32 |
u8 flags; |
| 33 |
u8 tprot; |
| 34 |
u16 fragoff; |
| 35 |
u16 thoff; |
| 36 |
u16 inneroff; |
| 37 |
}; |
| 38 |
|
| 39 |
static inline struct sock *nft_sk(const struct nft_pktinfo *pkt) |
| 40 |
{ |
| 41 |
return pkt->state->sk; |
| 42 |
} |
| 43 |
|
| 44 |
static inline unsigned int nft_thoff(const struct nft_pktinfo *pkt) |
| 45 |
{ |
| 46 |
return pkt->thoff; |
| 47 |
} |
| 48 |
|
| 49 |
static inline struct net *nft_net(const struct nft_pktinfo *pkt) |
| 50 |
{ |
| 51 |
return pkt->state->net; |
| 52 |
} |
| 53 |
|
| 54 |
static inline unsigned int nft_hook(const struct nft_pktinfo *pkt) |
| 55 |
{ |
| 56 |
return pkt->state->hook; |
| 57 |
} |
| 58 |
|
| 59 |
static inline u8 nft_pf(const struct nft_pktinfo *pkt) |
| 60 |
{ |
| 61 |
return pkt->state->pf; |
| 62 |
} |
| 63 |
|
| 64 |
static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt) |
| 65 |
{ |
| 66 |
return pkt->state->in; |
| 67 |
} |
| 68 |
|
| 69 |
static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt) |
| 70 |
{ |
| 71 |
return pkt->state->out; |
| 72 |
} |
| 73 |
|
| 74 |
static inline void nft_set_pktinfo(struct nft_pktinfo *pkt, |
| 75 |
struct sk_buff *skb, |
| 76 |
const struct nf_hook_state *state) |
| 77 |
{ |
| 78 |
pkt->skb = skb; |
| 79 |
pkt->state = state; |
| 80 |
} |
| 81 |
|
| 82 |
static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt) |
| 83 |
{ |
| 84 |
pkt->flags = 0; |
| 85 |
pkt->tprot = 0; |
| 86 |
pkt->thoff = 0; |
| 87 |
pkt->fragoff = 0; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* struct nft_verdict - nf_tables verdict |
| 92 |
* |
| 93 |
* @code: nf_tables/netfilter verdict code |
| 94 |
* @chain: destination chain for NFT_JUMP/NFT_GOTO |
| 95 |
*/ |
| 96 |
struct nft_verdict { |
| 97 |
u32 code; |
| 98 |
struct nft_chain *chain; |
| 99 |
}; |
| 100 |
|
| 101 |
struct nft_data { |
| 102 |
union { |
| 103 |
u32 data[4]; |
| 104 |
struct nft_verdict verdict; |
| 105 |
}; |
| 106 |
} __attribute__((aligned(__alignof__(u64)))); |
| 107 |
|
| 108 |
#define NFT_REG32_NUM 20 |
| 109 |
|
| 110 |
/** |
| 111 |
* struct nft_regs - nf_tables register set |
| 112 |
* |
| 113 |
* @data: data registers |
| 114 |
* @verdict: verdict register |
| 115 |
* |
| 116 |
* The first four data registers alias to the verdict register. |
| 117 |
*/ |
| 118 |
struct nft_regs { |
| 119 |
union { |
| 120 |
u32 data[NFT_REG32_NUM]; |
| 121 |
struct nft_verdict verdict; |
| 122 |
}; |
| 123 |
}; |
| 124 |
|
| 125 |
struct nft_regs_track { |
| 126 |
struct { |
| 127 |
const struct nft_expr *selector; |
| 128 |
const struct nft_expr *bitwise; |
| 129 |
u8 num_reg; |
| 130 |
} regs[NFT_REG32_NUM]; |
| 131 |
|
| 132 |
const struct nft_expr *cur; |
| 133 |
const struct nft_expr *last; |
| 134 |
}; |
| 135 |
|
| 136 |
/* Store/load an u8, u16 or u64 integer to/from the u32 data register. |
| 137 |
* |
| 138 |
* Note, when using concatenations, register allocation happens at 32-bit |
| 139 |
* level. So for store instruction, pad the rest part with zero to avoid |
| 140 |
* garbage values. |
| 141 |
*/ |
| 142 |
|
| 143 |
static inline void nft_reg_store8(u32 *dreg, u8 val) |
| 144 |
{ |
| 145 |
*dreg = 0; |
| 146 |
*(u8 *)dreg = val; |
| 147 |
} |
| 148 |
|
| 149 |
static inline u8 nft_reg_load8(const u32 *sreg) |
| 150 |
{ |
| 151 |
return *(u8 *)sreg; |
| 152 |
} |
| 153 |
|
| 154 |
static inline void nft_reg_store16(u32 *dreg, u16 val) |
| 155 |
{ |
| 156 |
*dreg = 0; |
| 157 |
*(u16 *)dreg = val; |
| 158 |
} |
| 159 |
|
| 160 |
static inline void nft_reg_store_be16(u32 *dreg, __be16 val) |
| 161 |
{ |
| 162 |
nft_reg_store16(dreg, (__force __u16)val); |
| 163 |
} |
| 164 |
|
| 165 |
static inline u16 nft_reg_load16(const u32 *sreg) |
| 166 |
{ |
| 167 |
return *(u16 *)sreg; |
| 168 |
} |
| 169 |
|
| 170 |
static inline __be16 nft_reg_load_be16(const u32 *sreg) |
| 171 |
{ |
| 172 |
return (__force __be16)nft_reg_load16(sreg); |
| 173 |
} |
| 174 |
|
| 175 |
static inline __be32 nft_reg_load_be32(const u32 *sreg) |
| 176 |
{ |
| 177 |
return *(__force __be32 *)sreg; |
| 178 |
} |
| 179 |
|
| 180 |
static inline void nft_reg_store64(u64 *dreg, u64 val) |
| 181 |
{ |
| 182 |
put_unaligned(val, dreg); |
| 183 |
} |
| 184 |
|
| 185 |
static inline u64 nft_reg_load64(const u32 *sreg) |
| 186 |
{ |
| 187 |
return get_unaligned((u64 *)sreg); |
| 188 |
} |
| 189 |
|
| 190 |
static inline void nft_data_copy(u32 *dst, const struct nft_data *src, |
| 191 |
unsigned int len) |
| 192 |
{ |
| 193 |
if (len % NFT_REG32_SIZE) |
| 194 |
dst[len / NFT_REG32_SIZE] = 0; |
| 195 |
memcpy(dst, src, len); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* struct nft_ctx - nf_tables rule/set context |
| 200 |
* |
| 201 |
* @net: net namespace |
| 202 |
* @table: the table the chain is contained in |
| 203 |
* @chain: the chain the rule is contained in |
| 204 |
* @nla: netlink attributes |
| 205 |
* @portid: netlink portID of the original message |
| 206 |
* @seq: netlink sequence number |
| 207 |
* @flags: modifiers to new request |
| 208 |
* @family: protocol family |
| 209 |
* @level: depth of the chains |
| 210 |
* @report: notify via unicast netlink message |
| 211 |
* @reg_inited: bitmap of initialised registers |
| 212 |
*/ |
| 213 |
struct nft_ctx { |
| 214 |
struct net *net; |
| 215 |
struct nft_table *table; |
| 216 |
struct nft_chain *chain; |
| 217 |
const struct nlattr * const *nla; |
| 218 |
u32 portid; |
| 219 |
u32 seq; |
| 220 |
u16 flags; |
| 221 |
u8 family; |
| 222 |
u8 level; |
| 223 |
bool report; |
| 224 |
DECLARE_BITMAP(reg_inited, NFT_REG32_NUM); |
| 225 |
}; |
| 226 |
|
| 227 |
enum nft_data_desc_flags { |
| 228 |
NFT_DATA_DESC_SETELEM = (1 << 0), |
| 229 |
}; |
| 230 |
|
| 231 |
struct nft_data_desc { |
| 232 |
enum nft_data_types type; |
| 233 |
unsigned int size; |
| 234 |
unsigned int len; |
| 235 |
unsigned int flags; |
| 236 |
}; |
| 237 |
|
| 238 |
int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data, |
| 239 |
struct nft_data_desc *desc, const struct nlattr *nla); |
| 240 |
void nft_data_hold(const struct nft_data *data, enum nft_data_types type); |
| 241 |
void nft_data_release(const struct nft_data *data, enum nft_data_types type); |
| 242 |
int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, |
| 243 |
enum nft_data_types type, unsigned int len); |
| 244 |
|
| 245 |
static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg) |
| 246 |
{ |
| 247 |
return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE; |
| 248 |
} |
| 249 |
|
| 250 |
static inline enum nft_registers nft_type_to_reg(enum nft_data_types type) |
| 251 |
{ |
| 252 |
return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE; |
| 253 |
} |
| 254 |
|
| 255 |
int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest); |
| 256 |
int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg); |
| 257 |
|
| 258 |
int nft_parse_register_load(const struct nft_ctx *ctx, |
| 259 |
const struct nlattr *attr, u8 *sreg, u32 len); |
| 260 |
int nft_parse_register_store(const struct nft_ctx *ctx, |
| 261 |
const struct nlattr *attr, u8 *dreg, |
| 262 |
const struct nft_data *data, |
| 263 |
enum nft_data_types type, unsigned int len); |
| 264 |
|
| 265 |
/** |
| 266 |
* struct nft_userdata - user defined data associated with an object |
| 267 |
* |
| 268 |
* @len: length of the data |
| 269 |
* @data: content |
| 270 |
* |
| 271 |
* The presence of user data is indicated in an object specific fashion, |
| 272 |
* so a length of zero can't occur and the value "len" indicates data |
| 273 |
* of length len + 1. |
| 274 |
*/ |
| 275 |
struct nft_userdata { |
| 276 |
u8 len; |
| 277 |
unsigned char data[]; |
| 278 |
}; |
| 279 |
|
| 280 |
/* placeholder structure for opaque set element backend representation. */ |
| 281 |
struct nft_elem_priv { }; |
| 282 |
|
| 283 |
/** |
| 284 |
* struct nft_set_elem - generic representation of set elements |
| 285 |
* |
| 286 |
* @key: element key |
| 287 |
* @key_end: closing element key |
| 288 |
* @data: element data |
| 289 |
* @priv: element private data and extensions |
| 290 |
*/ |
| 291 |
struct nft_set_elem { |
| 292 |
union { |
| 293 |
u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)]; |
| 294 |
struct nft_data val; |
| 295 |
} key; |
| 296 |
union { |
| 297 |
u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)]; |
| 298 |
struct nft_data val; |
| 299 |
} key_end; |
| 300 |
union { |
| 301 |
u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)]; |
| 302 |
struct nft_data val; |
| 303 |
} data; |
| 304 |
struct nft_elem_priv *priv; |
| 305 |
}; |
| 306 |
|
| 307 |
static inline void *nft_elem_priv_cast(const struct nft_elem_priv *priv) |
| 308 |
{ |
| 309 |
return (void *)priv; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
/** |
| 314 |
* enum nft_iter_type - nftables set iterator type |
| 315 |
* |
| 316 |
* @NFT_ITER_UNSPEC: unspecified, to catch errors |
| 317 |
* @NFT_ITER_READ: read-only iteration over set elements |
| 318 |
* @NFT_ITER_UPDATE: iteration under mutex to update set element state |
| 319 |
* @NFT_ITER_UPDATE_CLONE: clone set before iteration under mutex to update element |
| 320 |
*/ |
| 321 |
enum nft_iter_type { |
| 322 |
NFT_ITER_UNSPEC, |
| 323 |
NFT_ITER_READ, |
| 324 |
NFT_ITER_UPDATE, |
| 325 |
NFT_ITER_UPDATE_CLONE, |
| 326 |
}; |
| 327 |
|
| 328 |
struct nft_set; |
| 329 |
struct nft_set_iter { |
| 330 |
u8 genmask; |
| 331 |
enum nft_iter_type type:8; |
| 332 |
unsigned int count; |
| 333 |
unsigned int skip; |
| 334 |
int err; |
| 335 |
int (*fn)(const struct nft_ctx *ctx, |
| 336 |
struct nft_set *set, |
| 337 |
const struct nft_set_iter *iter, |
| 338 |
struct nft_elem_priv *elem_priv); |
| 339 |
}; |
| 340 |
|
| 341 |
/** |
| 342 |
* struct nft_set_desc - description of set elements |
| 343 |
* |
| 344 |
* @ktype: key type |
| 345 |
* @klen: key length |
| 346 |
* @dtype: data type |
| 347 |
* @dlen: data length |
| 348 |
* @objtype: object type |
| 349 |
* @size: number of set elements |
| 350 |
* @policy: set policy |
| 351 |
* @gc_int: garbage collector interval |
| 352 |
* @timeout: element timeout |
| 353 |
* @field_len: length of each field in concatenation, bytes |
| 354 |
* @field_count: number of concatenated fields in element |
| 355 |
* @expr: set must support for expressions |
| 356 |
*/ |
| 357 |
struct nft_set_desc { |
| 358 |
u32 ktype; |
| 359 |
unsigned int klen; |
| 360 |
u32 dtype; |
| 361 |
unsigned int dlen; |
| 362 |
u32 objtype; |
| 363 |
unsigned int size; |
| 364 |
u32 policy; |
| 365 |
u32 gc_int; |
| 366 |
u64 timeout; |
| 367 |
u8 field_len[NFT_REG32_COUNT]; |
| 368 |
u8 field_count; |
| 369 |
bool expr; |
| 370 |
}; |
| 371 |
|
| 372 |
/** |
| 373 |
* enum nft_set_class - performance class |
| 374 |
* |
| 375 |
* @NFT_SET_CLASS_O_1: constant, O(1) |
| 376 |
* @NFT_SET_CLASS_O_LOG_N: logarithmic, O(log N) |
| 377 |
* @NFT_SET_CLASS_O_N: linear, O(N) |
| 378 |
*/ |
| 379 |
enum nft_set_class { |
| 380 |
NFT_SET_CLASS_O_1, |
| 381 |
NFT_SET_CLASS_O_LOG_N, |
| 382 |
NFT_SET_CLASS_O_N, |
| 383 |
}; |
| 384 |
|
| 385 |
/** |
| 386 |
* struct nft_set_estimate - estimation of memory and performance |
| 387 |
* characteristics |
| 388 |
* |
| 389 |
* @size: required memory |
| 390 |
* @lookup: lookup performance class |
| 391 |
* @space: memory class |
| 392 |
*/ |
| 393 |
struct nft_set_estimate { |
| 394 |
u64 size; |
| 395 |
enum nft_set_class lookup; |
| 396 |
enum nft_set_class space; |
| 397 |
}; |
| 398 |
|
| 399 |
#define NFT_EXPR_MAXATTR 16 |
| 400 |
#define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \ |
| 401 |
ALIGN(size, __alignof__(struct nft_expr))) |
| 402 |
|
| 403 |
/** |
| 404 |
* struct nft_expr - nf_tables expression |
| 405 |
* |
| 406 |
* @ops: expression ops |
| 407 |
* @data: expression private data |
| 408 |
*/ |
| 409 |
struct nft_expr { |
| 410 |
const struct nft_expr_ops *ops; |
| 411 |
unsigned char data[] |
| 412 |
__attribute__((aligned(__alignof__(u64)))); |
| 413 |
}; |
| 414 |
|
| 415 |
static inline void *nft_expr_priv(const struct nft_expr *expr) |
| 416 |
{ |
| 417 |
return (void *)expr->data; |
| 418 |
} |
| 419 |
|
| 420 |
struct nft_expr_info; |
| 421 |
|
| 422 |
int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla, |
| 423 |
struct nft_expr_info *info); |
| 424 |
int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src, gfp_t gfp); |
| 425 |
void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr); |
| 426 |
int nft_expr_dump(struct sk_buff *skb, unsigned int attr, |
| 427 |
const struct nft_expr *expr, bool reset); |
| 428 |
bool nft_expr_reduce_bitwise(struct nft_regs_track *track, |
| 429 |
const struct nft_expr *expr); |
| 430 |
|
| 431 |
struct nft_set_ext; |
| 432 |
|
| 433 |
/** |
| 434 |
* struct nft_set_ops - nf_tables set operations |
| 435 |
* |
| 436 |
* @lookup: look up an element within the set |
| 437 |
* @update: update an element if exists, add it if doesn't exist |
| 438 |
* @delete: delete an element |
| 439 |
* @insert: insert new element into set |
| 440 |
* @activate: activate new element in the next generation |
| 441 |
* @deactivate: lookup for element and deactivate it in the next generation |
| 442 |
* @flush: deactivate element in the next generation |
| 443 |
* @remove: remove element from set |
| 444 |
* @walk: iterate over all set elements |
| 445 |
* @get: get set elements |
| 446 |
* @ksize: kernel set size |
| 447 |
* @usize: userspace set size |
| 448 |
* @adjust_maxsize: delta to adjust maximum set size |
| 449 |
* @commit: commit set elements |
| 450 |
* @abort: abort set elements |
| 451 |
* @privsize: function to return size of set private data |
| 452 |
* @estimate: estimate the required memory size and the lookup complexity class |
| 453 |
* @init: initialize private data of new set instance |
| 454 |
* @destroy: destroy private data of set instance |
| 455 |
* @gc_init: initialize garbage collection |
| 456 |
* @abort_skip_removal: skip removal of elements from abort path |
| 457 |
* @elemsize: element private size |
| 458 |
* |
| 459 |
* Operations lookup, update and delete have simpler interfaces, are faster |
| 460 |
* and currently only used in the packet path. All the rest are slower, |
| 461 |
* control plane functions. |
| 462 |
*/ |
| 463 |
struct nft_set_ops { |
| 464 |
const struct nft_set_ext * (*lookup)(const struct net *net, |
| 465 |
const struct nft_set *set, |
| 466 |
const u32 *key); |
| 467 |
const struct nft_set_ext * (*update)(struct nft_set *set, |
| 468 |
const u32 *key, |
| 469 |
const struct nft_expr *expr, |
| 470 |
struct nft_regs *regs); |
| 471 |
bool (*delete)(const struct nft_set *set, |
| 472 |
const u32 *key); |
| 473 |
|
| 474 |
int (*insert)(const struct net *net, |
| 475 |
const struct nft_set *set, |
| 476 |
const struct nft_set_elem *elem, |
| 477 |
struct nft_elem_priv **priv); |
| 478 |
void (*activate)(const struct net *net, |
| 479 |
const struct nft_set *set, |
| 480 |
struct nft_elem_priv *elem_priv); |
| 481 |
struct nft_elem_priv * (*deactivate)(const struct net *net, |
| 482 |
const struct nft_set *set, |
| 483 |
const struct nft_set_elem *elem); |
| 484 |
void (*flush)(const struct net *net, |
| 485 |
const struct nft_set *set, |
| 486 |
struct nft_elem_priv *priv); |
| 487 |
void (*remove)(const struct net *net, |
| 488 |
const struct nft_set *set, |
| 489 |
struct nft_elem_priv *elem_priv); |
| 490 |
void (*walk)(const struct nft_ctx *ctx, |
| 491 |
struct nft_set *set, |
| 492 |
struct nft_set_iter *iter); |
| 493 |
struct nft_elem_priv * (*get)(const struct net *net, |
| 494 |
const struct nft_set *set, |
| 495 |
const struct nft_set_elem *elem, |
| 496 |
unsigned int flags); |
| 497 |
u32 (*ksize)(u32 size); |
| 498 |
u32 (*usize)(u32 size); |
| 499 |
u32 (*adjust_maxsize)(const struct nft_set *set); |
| 500 |
void (*commit)(struct nft_set *set); |
| 501 |
void (*abort)(const struct nft_set *set); |
| 502 |
u64 (*privsize)(const struct nlattr * const nla[], |
| 503 |
const struct nft_set_desc *desc); |
| 504 |
bool (*estimate)(const struct nft_set_desc *desc, |
| 505 |
u32 features, |
| 506 |
struct nft_set_estimate *est); |
| 507 |
int (*init)(const struct nft_set *set, |
| 508 |
const struct nft_set_desc *desc, |
| 509 |
const struct nlattr * const nla[]); |
| 510 |
void (*destroy)(const struct nft_ctx *ctx, |
| 511 |
const struct nft_set *set); |
| 512 |
void (*gc_init)(const struct nft_set *set); |
| 513 |
|
| 514 |
bool abort_skip_removal; |
| 515 |
unsigned int elemsize; |
| 516 |
}; |
| 517 |
|
| 518 |
/** |
| 519 |
* struct nft_set_type - nf_tables set type |
| 520 |
* |
| 521 |
* @ops: set ops for this type |
| 522 |
* @features: features supported by the implementation |
| 523 |
*/ |
| 524 |
struct nft_set_type { |
| 525 |
const struct nft_set_ops ops; |
| 526 |
u32 features; |
| 527 |
}; |
| 528 |
#define to_set_type(o) container_of(o, struct nft_set_type, ops) |
| 529 |
|
| 530 |
struct nft_set_elem_expr { |
| 531 |
u8 size; |
| 532 |
unsigned char data[] |
| 533 |
__attribute__((aligned(__alignof__(struct nft_expr)))); |
| 534 |
}; |
| 535 |
|
| 536 |
#define nft_setelem_expr_at(__elem_expr, __offset) \ |
| 537 |
((struct nft_expr *)&__elem_expr->data[__offset]) |
| 538 |
|
| 539 |
#define nft_setelem_expr_foreach(__expr, __elem_expr, __size) \ |
| 540 |
for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \ |
| 541 |
__size < (__elem_expr)->size; \ |
| 542 |
__size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size) |
| 543 |
|
| 544 |
#define NFT_SET_EXPR_MAX 2 |
| 545 |
|
| 546 |
/** |
| 547 |
* struct nft_set - nf_tables set instance |
| 548 |
* |
| 549 |
* @list: table set list node |
| 550 |
* @bindings: list of set bindings |
| 551 |
* @refs: internal refcounting for async set destruction |
| 552 |
* @table: table this set belongs to |
| 553 |
* @net: netnamespace this set belongs to |
| 554 |
* @name: name of the set |
| 555 |
* @handle: unique handle of the set |
| 556 |
* @ktype: key type (numeric type defined by userspace, not used in the kernel) |
| 557 |
* @dtype: data type (verdict or numeric type defined by userspace) |
| 558 |
* @objtype: object type (see NFT_OBJECT_* definitions) |
| 559 |
* @size: maximum set size |
| 560 |
* @field_len: length of each field in concatenation, bytes |
| 561 |
* @field_count: number of concatenated fields in element |
| 562 |
* @in_update_walk: true during ->walk() in transaction phase |
| 563 |
* @use: number of rules references to this set |
| 564 |
* @nelems: number of elements |
| 565 |
* @ndeact: number of deactivated elements queued for removal |
| 566 |
* @timeout: default timeout value in jiffies |
| 567 |
* @gc_int: garbage collection interval in msecs |
| 568 |
* @policy: set parameterization (see enum nft_set_policies) |
| 569 |
* @udlen: user data length |
| 570 |
* @udata: user data |
| 571 |
* @pending_update: list of pending update set element |
| 572 |
* @ops: set ops |
| 573 |
* @flags: set flags |
| 574 |
* @dead: set will be freed, never cleared |
| 575 |
* @genmask: generation mask |
| 576 |
* @klen: key length |
| 577 |
* @dlen: data length |
| 578 |
* @num_exprs: numbers of exprs |
| 579 |
* @exprs: stateful expression |
| 580 |
* @catchall_list: list of catch-all set element |
| 581 |
* @data: private set data |
| 582 |
*/ |
| 583 |
struct nft_set { |
| 584 |
struct list_head list; |
| 585 |
struct list_head bindings; |
| 586 |
refcount_t refs; |
| 587 |
struct nft_table *table; |
| 588 |
possible_net_t net; |
| 589 |
char *name; |
| 590 |
u64 handle; |
| 591 |
u32 ktype; |
| 592 |
u32 dtype; |
| 593 |
u32 objtype; |
| 594 |
u32 size; |
| 595 |
u8 field_len[NFT_REG32_COUNT]; |
| 596 |
u8 field_count; |
| 597 |
bool in_update_walk; |
| 598 |
u32 use; |
| 599 |
atomic_t nelems; |
| 600 |
u32 ndeact; |
| 601 |
u64 timeout; |
| 602 |
u32 gc_int; |
| 603 |
u16 policy; |
| 604 |
u16 udlen; |
| 605 |
unsigned char *udata; |
| 606 |
struct list_head pending_update; |
| 607 |
/* runtime data below here */ |
| 608 |
const struct nft_set_ops *ops ____cacheline_aligned; |
| 609 |
u16 flags:13, |
| 610 |
dead:1, |
| 611 |
genmask:2; |
| 612 |
u8 klen; |
| 613 |
u8 dlen; |
| 614 |
u8 num_exprs; |
| 615 |
struct nft_expr *exprs[NFT_SET_EXPR_MAX]; |
| 616 |
struct list_head catchall_list; |
| 617 |
unsigned char data[] |
| 618 |
__attribute__((aligned(__alignof__(u64)))); |
| 619 |
}; |
| 620 |
|
| 621 |
static inline bool nft_set_is_anonymous(const struct nft_set *set) |
| 622 |
{ |
| 623 |
return set->flags & NFT_SET_ANONYMOUS; |
| 624 |
} |
| 625 |
|
| 626 |
static inline void *nft_set_priv(const struct nft_set *set) |
| 627 |
{ |
| 628 |
return (void *)set->data; |
| 629 |
} |
| 630 |
|
| 631 |
static inline enum nft_data_types nft_set_datatype(const struct nft_set *set) |
| 632 |
{ |
| 633 |
return set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE; |
| 634 |
} |
| 635 |
|
| 636 |
static inline bool nft_set_gc_is_pending(const struct nft_set *s) |
| 637 |
{ |
| 638 |
return refcount_read(&s->refs) != 1; |
| 639 |
} |
| 640 |
|
| 641 |
static inline struct nft_set *nft_set_container_of(const void *priv) |
| 642 |
{ |
| 643 |
return (void *)priv - offsetof(struct nft_set, data); |
| 644 |
} |
| 645 |
|
| 646 |
struct nft_set *nft_set_lookup_global(const struct net *net, |
| 647 |
const struct nft_table *table, |
| 648 |
const struct nlattr *nla_set_name, |
| 649 |
const struct nlattr *nla_set_id, |
| 650 |
u8 genmask); |
| 651 |
|
| 652 |
struct nft_set_ext *nft_set_catchall_lookup(const struct net *net, |
| 653 |
const struct nft_set *set); |
| 654 |
|
| 655 |
static inline unsigned long nft_set_gc_interval(const struct nft_set *set) |
| 656 |
{ |
| 657 |
u32 gc_int = READ_ONCE(set->gc_int); |
| 658 |
|
| 659 |
return gc_int ? msecs_to_jiffies(gc_int) : HZ; |
| 660 |
} |
| 661 |
|
| 662 |
/** |
| 663 |
* struct nft_set_binding - nf_tables set binding |
| 664 |
* |
| 665 |
* @list: set bindings list node |
| 666 |
* @chain: chain containing the rule bound to the set |
| 667 |
* @flags: set action flags |
| 668 |
* |
| 669 |
* A set binding contains all information necessary for validation |
| 670 |
* of new elements added to a bound set. |
| 671 |
*/ |
| 672 |
struct nft_set_binding { |
| 673 |
struct list_head list; |
| 674 |
const struct nft_chain *chain; |
| 675 |
u32 flags; |
| 676 |
}; |
| 677 |
|
| 678 |
enum nft_trans_phase; |
| 679 |
void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set); |
| 680 |
void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set, |
| 681 |
struct nft_set_binding *binding, |
| 682 |
enum nft_trans_phase phase); |
| 683 |
int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, |
| 684 |
struct nft_set_binding *binding); |
| 685 |
void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set); |
| 686 |
|
| 687 |
/** |
| 688 |
* enum nft_set_extensions - set extension type IDs |
| 689 |
* |
| 690 |
* @NFT_SET_EXT_KEY: element key |
| 691 |
* @NFT_SET_EXT_KEY_END: upper bound element key, for ranges |
| 692 |
* @NFT_SET_EXT_DATA: mapping data |
| 693 |
* @NFT_SET_EXT_FLAGS: element flags |
| 694 |
* @NFT_SET_EXT_TIMEOUT: element timeout |
| 695 |
* @NFT_SET_EXT_USERDATA: user data associated with the element |
| 696 |
* @NFT_SET_EXT_EXPRESSIONS: expressions associated with the element |
| 697 |
* @NFT_SET_EXT_OBJREF: stateful object reference associated with element |
| 698 |
* @NFT_SET_EXT_NUM: number of extension types |
| 699 |
*/ |
| 700 |
enum nft_set_extensions { |
| 701 |
NFT_SET_EXT_KEY, |
| 702 |
NFT_SET_EXT_KEY_END, |
| 703 |
NFT_SET_EXT_DATA, |
| 704 |
NFT_SET_EXT_FLAGS, |
| 705 |
NFT_SET_EXT_TIMEOUT, |
| 706 |
NFT_SET_EXT_USERDATA, |
| 707 |
NFT_SET_EXT_EXPRESSIONS, |
| 708 |
NFT_SET_EXT_OBJREF, |
| 709 |
NFT_SET_EXT_NUM |
| 710 |
}; |
| 711 |
|
| 712 |
/** |
| 713 |
* struct nft_set_ext_type - set extension type |
| 714 |
* |
| 715 |
* @len: fixed part length of the extension |
| 716 |
* @align: alignment requirements of the extension |
| 717 |
*/ |
| 718 |
struct nft_set_ext_type { |
| 719 |
u8 len; |
| 720 |
u8 align; |
| 721 |
}; |
| 722 |
|
| 723 |
extern const struct nft_set_ext_type nft_set_ext_types[]; |
| 724 |
|
| 725 |
/** |
| 726 |
* struct nft_set_ext_tmpl - set extension template |
| 727 |
* |
| 728 |
* @len: length of extension area |
| 729 |
* @offset: offsets of individual extension types |
| 730 |
* @ext_len: length of the expected extension(used to sanity check) |
| 731 |
*/ |
| 732 |
struct nft_set_ext_tmpl { |
| 733 |
u16 len; |
| 734 |
u8 offset[NFT_SET_EXT_NUM]; |
| 735 |
u8 ext_len[NFT_SET_EXT_NUM]; |
| 736 |
}; |
| 737 |
|
| 738 |
/** |
| 739 |
* struct nft_set_ext - set extensions |
| 740 |
* |
| 741 |
* @genmask: generation mask, but also flags (see NFT_SET_ELEM_DEAD_BIT) |
| 742 |
* @offset: offsets of individual extension types |
| 743 |
* @data: beginning of extension data |
| 744 |
* |
| 745 |
* This structure must be aligned to word size, otherwise atomic bitops |
| 746 |
* on genmask field can cause alignment failure on some archs. |
| 747 |
*/ |
| 748 |
struct nft_set_ext { |
| 749 |
u8 genmask; |
| 750 |
u8 offset[NFT_SET_EXT_NUM]; |
| 751 |
char data[]; |
| 752 |
} __aligned(BITS_PER_LONG / 8); |
| 753 |
|
| 754 |
static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl) |
| 755 |
{ |
| 756 |
memset(tmpl, 0, sizeof(*tmpl)); |
| 757 |
tmpl->len = sizeof(struct nft_set_ext); |
| 758 |
} |
| 759 |
|
| 760 |
static inline int nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id, |
| 761 |
unsigned int len) |
| 762 |
{ |
| 763 |
tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align); |
| 764 |
if (tmpl->len > U8_MAX) |
| 765 |
return -EINVAL; |
| 766 |
|
| 767 |
tmpl->offset[id] = tmpl->len; |
| 768 |
tmpl->ext_len[id] = nft_set_ext_types[id].len + len; |
| 769 |
tmpl->len += tmpl->ext_len[id]; |
| 770 |
|
| 771 |
return 0; |
| 772 |
} |
| 773 |
|
| 774 |
static inline int nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id) |
| 775 |
{ |
| 776 |
return nft_set_ext_add_length(tmpl, id, 0); |
| 777 |
} |
| 778 |
|
| 779 |
static inline void nft_set_ext_init(struct nft_set_ext *ext, |
| 780 |
const struct nft_set_ext_tmpl *tmpl) |
| 781 |
{ |
| 782 |
memcpy(ext->offset, tmpl->offset, sizeof(ext->offset)); |
| 783 |
} |
| 784 |
|
| 785 |
static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id) |
| 786 |
{ |
| 787 |
return !!ext->offset[id]; |
| 788 |
} |
| 789 |
|
| 790 |
static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id) |
| 791 |
{ |
| 792 |
return ext && __nft_set_ext_exists(ext, id); |
| 793 |
} |
| 794 |
|
| 795 |
static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id) |
| 796 |
{ |
| 797 |
return (void *)ext + ext->offset[id]; |
| 798 |
} |
| 799 |
|
| 800 |
static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext) |
| 801 |
{ |
| 802 |
return nft_set_ext(ext, NFT_SET_EXT_KEY); |
| 803 |
} |
| 804 |
|
| 805 |
static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext) |
| 806 |
{ |
| 807 |
return nft_set_ext(ext, NFT_SET_EXT_KEY_END); |
| 808 |
} |
| 809 |
|
| 810 |
static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext) |
| 811 |
{ |
| 812 |
return nft_set_ext(ext, NFT_SET_EXT_DATA); |
| 813 |
} |
| 814 |
|
| 815 |
static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext) |
| 816 |
{ |
| 817 |
return nft_set_ext(ext, NFT_SET_EXT_FLAGS); |
| 818 |
} |
| 819 |
|
| 820 |
struct nft_timeout { |
| 821 |
u64 timeout; |
| 822 |
u64 expiration; |
| 823 |
}; |
| 824 |
|
| 825 |
static inline struct nft_timeout *nft_set_ext_timeout(const struct nft_set_ext *ext) |
| 826 |
{ |
| 827 |
return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT); |
| 828 |
} |
| 829 |
|
| 830 |
static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext) |
| 831 |
{ |
| 832 |
return nft_set_ext(ext, NFT_SET_EXT_USERDATA); |
| 833 |
} |
| 834 |
|
| 835 |
static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_ext *ext) |
| 836 |
{ |
| 837 |
return nft_set_ext(ext, NFT_SET_EXT_EXPRESSIONS); |
| 838 |
} |
| 839 |
|
| 840 |
static inline bool __nft_set_elem_expired(const struct nft_set_ext *ext, |
| 841 |
u64 tstamp) |
| 842 |
{ |
| 843 |
if (!nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) || |
| 844 |
READ_ONCE(nft_set_ext_timeout(ext)->timeout) == 0) |
| 845 |
return false; |
| 846 |
|
| 847 |
return time_after_eq64(tstamp, READ_ONCE(nft_set_ext_timeout(ext)->expiration)); |
| 848 |
} |
| 849 |
|
| 850 |
static inline bool nft_set_elem_expired(const struct nft_set_ext *ext) |
| 851 |
{ |
| 852 |
return __nft_set_elem_expired(ext, get_jiffies_64()); |
| 853 |
} |
| 854 |
|
| 855 |
static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set, |
| 856 |
const struct nft_elem_priv *elem_priv) |
| 857 |
{ |
| 858 |
return (void *)elem_priv + set->ops->elemsize; |
| 859 |
} |
| 860 |
|
| 861 |
static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext) |
| 862 |
{ |
| 863 |
return nft_set_ext(ext, NFT_SET_EXT_OBJREF); |
| 864 |
} |
| 865 |
|
| 866 |
struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx, |
| 867 |
const struct nft_set *set, |
| 868 |
const struct nlattr *attr); |
| 869 |
|
| 870 |
struct nft_elem_priv *nft_set_elem_init(const struct nft_set *set, |
| 871 |
const struct nft_set_ext_tmpl *tmpl, |
| 872 |
const u32 *key, const u32 *key_end, |
| 873 |
const u32 *data, |
| 874 |
u64 timeout, u64 expiration, gfp_t gfp); |
| 875 |
int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set, |
| 876 |
struct nft_expr *expr_array[]); |
| 877 |
void nft_set_elem_expr_destroy(const struct nft_ctx *ctx, |
| 878 |
struct nft_set_elem_expr *elem_expr); |
| 879 |
void nft_set_elem_destroy(const struct nft_set *set, |
| 880 |
const struct nft_elem_priv *elem_priv, |
| 881 |
bool destroy_expr); |
| 882 |
void nf_tables_set_elem_destroy(const struct nft_ctx *ctx, |
| 883 |
const struct nft_set *set, |
| 884 |
const struct nft_elem_priv *elem_priv); |
| 885 |
|
| 886 |
struct nft_expr_ops; |
| 887 |
/** |
| 888 |
* struct nft_expr_type - nf_tables expression type |
| 889 |
* |
| 890 |
* @select_ops: function to select nft_expr_ops |
| 891 |
* @release_ops: release nft_expr_ops |
| 892 |
* @ops: default ops, used when no select_ops functions is present |
| 893 |
* @inner_ops: inner ops, used for inner packet operation |
| 894 |
* @list: used internally |
| 895 |
* @name: Identifier |
| 896 |
* @owner: module reference |
| 897 |
* @policy: netlink attribute policy |
| 898 |
* @maxattr: highest netlink attribute number |
| 899 |
* @family: address family for AF-specific types |
| 900 |
* @flags: expression type flags |
| 901 |
*/ |
| 902 |
struct nft_expr_type { |
| 903 |
const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *, |
| 904 |
const struct nlattr * const tb[]); |
| 905 |
void (*release_ops)(const struct nft_expr_ops *ops); |
| 906 |
const struct nft_expr_ops *ops; |
| 907 |
const struct nft_expr_ops *inner_ops; |
| 908 |
struct list_head list; |
| 909 |
const char *name; |
| 910 |
struct module *owner; |
| 911 |
const struct nla_policy *policy; |
| 912 |
unsigned int maxattr; |
| 913 |
u8 family; |
| 914 |
u8 flags; |
| 915 |
}; |
| 916 |
|
| 917 |
#define NFT_EXPR_STATEFUL 0x1 |
| 918 |
#define NFT_EXPR_GC 0x2 |
| 919 |
|
| 920 |
enum nft_trans_phase { |
| 921 |
NFT_TRANS_PREPARE, |
| 922 |
NFT_TRANS_PREPARE_ERROR, |
| 923 |
NFT_TRANS_ABORT, |
| 924 |
NFT_TRANS_COMMIT, |
| 925 |
NFT_TRANS_RELEASE |
| 926 |
}; |
| 927 |
|
| 928 |
struct nft_flow_rule; |
| 929 |
struct nft_offload_ctx; |
| 930 |
|
| 931 |
/** |
| 932 |
* struct nft_expr_ops - nf_tables expression operations |
| 933 |
* |
| 934 |
* @eval: Expression evaluation function |
| 935 |
* @clone: Expression clone function |
| 936 |
* @size: full expression size, including private data size |
| 937 |
* @init: initialization function |
| 938 |
* @activate: activate expression in the next generation |
| 939 |
* @deactivate: deactivate expression in next generation |
| 940 |
* @destroy: destruction function, called after synchronize_rcu |
| 941 |
* @destroy_clone: destruction clone function |
| 942 |
* @dump: function to dump parameters |
| 943 |
* @validate: validate expression, called during loop detection |
| 944 |
* @reduce: reduce expression |
| 945 |
* @gc: garbage collection expression |
| 946 |
* @offload: hardware offload expression |
| 947 |
* @offload_action: function to report true/false to allocate one slot or not in the flow |
| 948 |
* offload array |
| 949 |
* @offload_stats: function to synchronize hardware stats via updating the counter expression |
| 950 |
* @type: expression type |
| 951 |
* @data: extra data to attach to this expression operation |
| 952 |
*/ |
| 953 |
struct nft_expr_ops { |
| 954 |
void (*eval)(const struct nft_expr *expr, |
| 955 |
struct nft_regs *regs, |
| 956 |
const struct nft_pktinfo *pkt); |
| 957 |
int (*clone)(struct nft_expr *dst, |
| 958 |
const struct nft_expr *src, gfp_t gfp); |
| 959 |
unsigned int size; |
| 960 |
|
| 961 |
int (*init)(const struct nft_ctx *ctx, |
| 962 |
const struct nft_expr *expr, |
| 963 |
const struct nlattr * const tb[]); |
| 964 |
void (*activate)(const struct nft_ctx *ctx, |
| 965 |
const struct nft_expr *expr); |
| 966 |
void (*deactivate)(const struct nft_ctx *ctx, |
| 967 |
const struct nft_expr *expr, |
| 968 |
enum nft_trans_phase phase); |
| 969 |
void (*destroy)(const struct nft_ctx *ctx, |
| 970 |
const struct nft_expr *expr); |
| 971 |
void (*destroy_clone)(const struct nft_ctx *ctx, |
| 972 |
const struct nft_expr *expr); |
| 973 |
int (*dump)(struct sk_buff *skb, |
| 974 |
const struct nft_expr *expr, |
| 975 |
bool reset); |
| 976 |
int (*validate)(const struct nft_ctx *ctx, |
| 977 |
const struct nft_expr *expr); |
| 978 |
bool (*reduce)(struct nft_regs_track *track, |
| 979 |
const struct nft_expr *expr); |
| 980 |
bool (*gc)(struct net *net, |
| 981 |
const struct nft_expr *expr); |
| 982 |
int (*offload)(struct nft_offload_ctx *ctx, |
| 983 |
struct nft_flow_rule *flow, |
| 984 |
const struct nft_expr *expr); |
| 985 |
bool (*offload_action)(const struct nft_expr *expr); |
| 986 |
void (*offload_stats)(struct nft_expr *expr, |
| 987 |
const struct flow_stats *stats); |
| 988 |
const struct nft_expr_type *type; |
| 989 |
void *data; |
| 990 |
}; |
| 991 |
|
| 992 |
/** |
| 993 |
* struct nft_rule - nf_tables rule |
| 994 |
* |
| 995 |
* @list: used internally |
| 996 |
* @handle: rule handle |
| 997 |
* @genmask: generation mask |
| 998 |
* @dlen: length of expression data |
| 999 |
* @udata: user data is appended to the rule |
| 1000 |
* @data: expression data |
| 1001 |
*/ |
| 1002 |
struct nft_rule { |
| 1003 |
struct list_head list; |
| 1004 |
u64 handle:42, |
| 1005 |
genmask:2, |
| 1006 |
dlen:12, |
| 1007 |
udata:1; |
| 1008 |
unsigned char data[] |
| 1009 |
__attribute__((aligned(__alignof__(struct nft_expr)))); |
| 1010 |
}; |
| 1011 |
|
| 1012 |
static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule) |
| 1013 |
{ |
| 1014 |
return (struct nft_expr *)&rule->data[0]; |
| 1015 |
} |
| 1016 |
|
| 1017 |
static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr) |
| 1018 |
{ |
| 1019 |
return ((void *)expr) + expr->ops->size; |
| 1020 |
} |
| 1021 |
|
| 1022 |
static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule) |
| 1023 |
{ |
| 1024 |
return (struct nft_expr *)&rule->data[rule->dlen]; |
| 1025 |
} |
| 1026 |
|
| 1027 |
static inline bool nft_expr_more(const struct nft_rule *rule, |
| 1028 |
const struct nft_expr *expr) |
| 1029 |
{ |
| 1030 |
return expr != nft_expr_last(rule) && expr->ops; |
| 1031 |
} |
| 1032 |
|
| 1033 |
static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule) |
| 1034 |
{ |
| 1035 |
return (void *)&rule->data[rule->dlen]; |
| 1036 |
} |
| 1037 |
|
| 1038 |
void nft_rule_expr_activate(const struct nft_ctx *ctx, struct nft_rule *rule); |
| 1039 |
void nft_rule_expr_deactivate(const struct nft_ctx *ctx, struct nft_rule *rule, |
| 1040 |
enum nft_trans_phase phase); |
| 1041 |
void nf_tables_rule_destroy(const struct nft_ctx *ctx, struct nft_rule *rule); |
| 1042 |
|
| 1043 |
static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext, |
| 1044 |
struct nft_regs *regs, |
| 1045 |
const struct nft_pktinfo *pkt) |
| 1046 |
{ |
| 1047 |
struct nft_set_elem_expr *elem_expr; |
| 1048 |
struct nft_expr *expr; |
| 1049 |
u32 size; |
| 1050 |
|
| 1051 |
if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) { |
| 1052 |
elem_expr = nft_set_ext_expr(ext); |
| 1053 |
nft_setelem_expr_foreach(expr, elem_expr, size) { |
| 1054 |
expr->ops->eval(expr, regs, pkt); |
| 1055 |
if (regs->verdict.code == NFT_BREAK) |
| 1056 |
return; |
| 1057 |
} |
| 1058 |
} |
| 1059 |
} |
| 1060 |
|
| 1061 |
/* |
| 1062 |
* The last pointer isn't really necessary, but the compiler isn't able to |
| 1063 |
* determine that the result of nft_expr_last() is always the same since it |
| 1064 |
* can't assume that the dlen value wasn't changed within calls in the loop. |
| 1065 |
*/ |
| 1066 |
#define nft_rule_for_each_expr(expr, last, rule) \ |
| 1067 |
for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \ |
| 1068 |
(expr) != (last); \ |
| 1069 |
(expr) = nft_expr_next(expr)) |
| 1070 |
|
| 1071 |
#define NFT_CHAIN_POLICY_UNSET U8_MAX |
| 1072 |
|
| 1073 |
struct nft_rule_dp { |
| 1074 |
u64 is_last:1, |
| 1075 |
dlen:12, |
| 1076 |
handle:42; /* for tracing */ |
| 1077 |
unsigned char data[] |
| 1078 |
__attribute__((aligned(__alignof__(struct nft_expr)))); |
| 1079 |
}; |
| 1080 |
|
| 1081 |
struct nft_rule_dp_last { |
| 1082 |
struct nft_rule_dp end; /* end of nft_rule_blob marker */ |
| 1083 |
struct rcu_head h; /* call_rcu head */ |
| 1084 |
struct nft_rule_blob *blob; /* ptr to free via call_rcu */ |
| 1085 |
const struct nft_chain *chain; /* for nftables tracing */ |
| 1086 |
}; |
| 1087 |
|
| 1088 |
static inline const struct nft_rule_dp *nft_rule_next(const struct nft_rule_dp *rule) |
| 1089 |
{ |
| 1090 |
return (void *)rule + sizeof(*rule) + rule->dlen; |
| 1091 |
} |
| 1092 |
|
| 1093 |
struct nft_rule_blob { |
| 1094 |
unsigned long size; |
| 1095 |
unsigned char data[] |
| 1096 |
__attribute__((aligned(__alignof__(struct nft_rule_dp)))); |
| 1097 |
}; |
| 1098 |
|
| 1099 |
enum nft_chain_types { |
| 1100 |
NFT_CHAIN_T_DEFAULT = 0, |
| 1101 |
NFT_CHAIN_T_ROUTE, |
| 1102 |
NFT_CHAIN_T_NAT, |
| 1103 |
NFT_CHAIN_T_MAX |
| 1104 |
}; |
| 1105 |
|
| 1106 |
/** |
| 1107 |
* struct nft_chain_validate_state - validation state |
| 1108 |
* |
| 1109 |
* If a chain is encountered again during table validation it is |
| 1110 |
* possible to avoid revalidation provided the calling context is |
| 1111 |
* compatible. This structure stores relevant calling context of |
| 1112 |
* previous validations. |
| 1113 |
* |
| 1114 |
* @hook_mask: the hook numbers and locations the chain is linked to |
| 1115 |
* @depth: the deepest call chain level the chain is linked to |
| 1116 |
*/ |
| 1117 |
struct nft_chain_validate_state { |
| 1118 |
u8 hook_mask[NFT_CHAIN_T_MAX]; |
| 1119 |
u8 depth; |
| 1120 |
}; |
| 1121 |
|
| 1122 |
/** |
| 1123 |
* struct nft_chain - nf_tables chain |
| 1124 |
* |
| 1125 |
* @blob_gen_0: rule blob pointer to the current generation |
| 1126 |
* @blob_gen_1: rule blob pointer to the future generation |
| 1127 |
* @rules: list of rules in the chain |
| 1128 |
* @list: used internally |
| 1129 |
* @rhlhead: used internally |
| 1130 |
* @table: table that this chain belongs to |
| 1131 |
* @handle: chain handle |
| 1132 |
* @use: number of jump references to this chain |
| 1133 |
* @flags: bitmask of enum NFTA_CHAIN_FLAGS |
| 1134 |
* @bound: bind or not |
| 1135 |
* @genmask: generation mask |
| 1136 |
* @name: name of the chain |
| 1137 |
* @udlen: user data length |
| 1138 |
* @udata: user data in the chain |
| 1139 |
* @blob_next: rule blob pointer to the next in the chain |
| 1140 |
* @vstate: validation state |
| 1141 |
*/ |
| 1142 |
struct nft_chain { |
| 1143 |
struct nft_rule_blob __rcu *blob_gen_0; |
| 1144 |
struct nft_rule_blob __rcu *blob_gen_1; |
| 1145 |
struct list_head rules; |
| 1146 |
struct list_head list; |
| 1147 |
struct rhlist_head rhlhead; |
| 1148 |
struct nft_table *table; |
| 1149 |
u64 handle; |
| 1150 |
u32 use; |
| 1151 |
u8 flags:5, |
| 1152 |
bound:1, |
| 1153 |
genmask:2; |
| 1154 |
char *name; |
| 1155 |
u16 udlen; |
| 1156 |
u8 *udata; |
| 1157 |
|
| 1158 |
/* Only used during control plane commit phase: */ |
| 1159 |
struct nft_rule_blob *blob_next; |
| 1160 |
struct nft_chain_validate_state vstate; |
| 1161 |
}; |
| 1162 |
|
| 1163 |
int nft_chain_validate(const struct nft_ctx *ctx, struct nft_chain *chain); |
| 1164 |
int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set, |
| 1165 |
const struct nft_set_iter *iter, |
| 1166 |
struct nft_elem_priv *elem_priv); |
| 1167 |
int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set); |
| 1168 |
int nf_tables_bind_chain(const struct nft_ctx *ctx, struct nft_chain *chain); |
| 1169 |
void nf_tables_unbind_chain(const struct nft_ctx *ctx, struct nft_chain *chain); |
| 1170 |
|
| 1171 |
/** |
| 1172 |
* struct nft_chain_type - nf_tables chain type info |
| 1173 |
* |
| 1174 |
* @name: name of the type |
| 1175 |
* @type: numeric identifier |
| 1176 |
* @family: address family |
| 1177 |
* @owner: module owner |
| 1178 |
* @hook_mask: mask of valid hooks |
| 1179 |
* @hooks: array of hook functions |
| 1180 |
* @ops_register: base chain register function |
| 1181 |
* @ops_unregister: base chain unregister function |
| 1182 |
*/ |
| 1183 |
struct nft_chain_type { |
| 1184 |
const char *name; |
| 1185 |
enum nft_chain_types type; |
| 1186 |
int family; |
| 1187 |
struct module *owner; |
| 1188 |
unsigned int hook_mask; |
| 1189 |
nf_hookfn *hooks[NFT_MAX_HOOKS]; |
| 1190 |
int (*ops_register)(struct net *net, const struct nf_hook_ops *ops); |
| 1191 |
void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops); |
| 1192 |
}; |
| 1193 |
|
| 1194 |
int nft_chain_validate_dependency(const struct nft_chain *chain, |
| 1195 |
enum nft_chain_types type); |
| 1196 |
int nft_chain_validate_hooks(const struct nft_chain *chain, |
| 1197 |
unsigned int hook_flags); |
| 1198 |
|
| 1199 |
static inline bool nft_chain_binding(const struct nft_chain *chain) |
| 1200 |
{ |
| 1201 |
return chain->flags & NFT_CHAIN_BINDING; |
| 1202 |
} |
| 1203 |
|
| 1204 |
static inline bool nft_chain_is_bound(struct nft_chain *chain) |
| 1205 |
{ |
| 1206 |
return (chain->flags & NFT_CHAIN_BINDING) && chain->bound; |
| 1207 |
} |
| 1208 |
|
| 1209 |
int nft_chain_add(struct nft_table *table, struct nft_chain *chain); |
| 1210 |
void nft_chain_del(struct nft_chain *chain); |
| 1211 |
void nf_tables_chain_destroy(struct nft_chain *chain); |
| 1212 |
|
| 1213 |
struct nft_stats { |
| 1214 |
u64 bytes; |
| 1215 |
u64 pkts; |
| 1216 |
struct u64_stats_sync syncp; |
| 1217 |
}; |
| 1218 |
|
| 1219 |
struct nft_hook { |
| 1220 |
struct list_head list; |
| 1221 |
struct list_head ops_list; |
| 1222 |
struct rcu_head rcu; |
| 1223 |
char ifname[IFNAMSIZ]; |
| 1224 |
u8 ifnamelen; |
| 1225 |
}; |
| 1226 |
|
| 1227 |
struct nf_hook_ops *nft_hook_find_ops(const struct nft_hook *hook, |
| 1228 |
const struct net_device *dev); |
| 1229 |
struct nf_hook_ops *nft_hook_find_ops_rcu(const struct nft_hook *hook, |
| 1230 |
const struct net_device *dev); |
| 1231 |
|
| 1232 |
/** |
| 1233 |
* struct nft_base_chain - nf_tables base chain |
| 1234 |
* |
| 1235 |
* @ops: netfilter hook ops |
| 1236 |
* @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family) |
| 1237 |
* @type: chain type |
| 1238 |
* @policy: default policy |
| 1239 |
* @flags: indicate the base chain disabled or not |
| 1240 |
* @stats: per-cpu chain stats |
| 1241 |
* @chain: the chain |
| 1242 |
* @flow_block: flow block (for hardware offload) |
| 1243 |
*/ |
| 1244 |
struct nft_base_chain { |
| 1245 |
struct nf_hook_ops ops; |
| 1246 |
struct list_head hook_list; |
| 1247 |
const struct nft_chain_type *type; |
| 1248 |
u8 policy; |
| 1249 |
u8 flags; |
| 1250 |
struct nft_stats __percpu *stats; |
| 1251 |
struct nft_chain chain; |
| 1252 |
struct flow_block flow_block; |
| 1253 |
}; |
| 1254 |
|
| 1255 |
static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain) |
| 1256 |
{ |
| 1257 |
return container_of(chain, struct nft_base_chain, chain); |
| 1258 |
} |
| 1259 |
|
| 1260 |
static inline bool nft_is_base_chain(const struct nft_chain *chain) |
| 1261 |
{ |
| 1262 |
return chain->flags & NFT_CHAIN_BASE; |
| 1263 |
} |
| 1264 |
|
| 1265 |
unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv); |
| 1266 |
|
| 1267 |
static inline bool nft_use_inc(u32 *use) |
| 1268 |
{ |
| 1269 |
if (*use == UINT_MAX) |
| 1270 |
return false; |
| 1271 |
|
| 1272 |
(*use)++; |
| 1273 |
|
| 1274 |
return true; |
| 1275 |
} |
| 1276 |
|
| 1277 |
static inline void nft_use_dec(u32 *use) |
| 1278 |
{ |
| 1279 |
WARN_ON_ONCE((*use)-- == 0); |
| 1280 |
} |
| 1281 |
|
| 1282 |
/* For error and abort path: restore use counter to previous state. */ |
| 1283 |
static inline void nft_use_inc_restore(u32 *use) |
| 1284 |
{ |
| 1285 |
WARN_ON_ONCE(!nft_use_inc(use)); |
| 1286 |
} |
| 1287 |
|
| 1288 |
#define nft_use_dec_restore nft_use_dec |
| 1289 |
|
| 1290 |
/** |
| 1291 |
* struct nft_table - nf_tables table |
| 1292 |
* |
| 1293 |
* @list: used internally |
| 1294 |
* @chains_ht: chains in the table |
| 1295 |
* @chains: same, for stable walks |
| 1296 |
* @sets: sets in the table |
| 1297 |
* @objects: stateful objects in the table |
| 1298 |
* @flowtables: flow tables in the table |
| 1299 |
* @hgenerator: handle generator state |
| 1300 |
* @handle: table handle |
| 1301 |
* @use: number of chain references to this table |
| 1302 |
* @family:address family |
| 1303 |
* @flags: table flag (see enum nft_table_flags) |
| 1304 |
* @genmask: generation mask |
| 1305 |
* @nlpid: netlink port ID |
| 1306 |
* @name: name of the table |
| 1307 |
* @udlen: length of the user data |
| 1308 |
* @udata: user data |
| 1309 |
* @validate_state: internal, set when transaction adds jumps |
| 1310 |
*/ |
| 1311 |
struct nft_table { |
| 1312 |
struct list_head list; |
| 1313 |
struct rhltable chains_ht; |
| 1314 |
struct list_head chains; |
| 1315 |
struct list_head sets; |
| 1316 |
struct list_head objects; |
| 1317 |
struct list_head flowtables; |
| 1318 |
u64 hgenerator; |
| 1319 |
u64 handle; |
| 1320 |
u32 use; |
| 1321 |
u16 family:6, |
| 1322 |
flags:8, |
| 1323 |
genmask:2; |
| 1324 |
u32 nlpid; |
| 1325 |
char *name; |
| 1326 |
u16 udlen; |
| 1327 |
u8 *udata; |
| 1328 |
u8 validate_state; |
| 1329 |
}; |
| 1330 |
|
| 1331 |
static inline bool nft_table_has_owner(const struct nft_table *table) |
| 1332 |
{ |
| 1333 |
return table->flags & NFT_TABLE_F_OWNER; |
| 1334 |
} |
| 1335 |
|
| 1336 |
static inline bool nft_table_is_orphan(const struct nft_table *table) |
| 1337 |
{ |
| 1338 |
return (table->flags & (NFT_TABLE_F_OWNER | NFT_TABLE_F_PERSIST)) == |
| 1339 |
NFT_TABLE_F_PERSIST; |
| 1340 |
} |
| 1341 |
|
| 1342 |
static inline bool nft_base_chain_netdev(int family, u32 hooknum) |
| 1343 |
{ |
| 1344 |
return family == NFPROTO_NETDEV || |
| 1345 |
(family == NFPROTO_INET && hooknum == NF_INET_INGRESS); |
| 1346 |
} |
| 1347 |
|
| 1348 |
void nft_register_chain_type(const struct nft_chain_type *); |
| 1349 |
void nft_unregister_chain_type(const struct nft_chain_type *); |
| 1350 |
|
| 1351 |
int nft_register_expr(struct nft_expr_type *); |
| 1352 |
void nft_unregister_expr(struct nft_expr_type *); |
| 1353 |
|
| 1354 |
int nft_verdict_dump(struct sk_buff *skb, int type, |
| 1355 |
const struct nft_verdict *v); |
| 1356 |
|
| 1357 |
/** |
| 1358 |
* struct nft_object_hash_key - key to lookup nft_object |
| 1359 |
* |
| 1360 |
* @name: name of the stateful object to look up |
| 1361 |
* @table: table the object belongs to |
| 1362 |
*/ |
| 1363 |
struct nft_object_hash_key { |
| 1364 |
const char *name; |
| 1365 |
const struct nft_table *table; |
| 1366 |
}; |
| 1367 |
|
| 1368 |
/** |
| 1369 |
* struct nft_object - nf_tables stateful object |
| 1370 |
* |
| 1371 |
* @list: table stateful object list node |
| 1372 |
* @rhlhead: nft_objname_ht node |
| 1373 |
* @key: keys that identify this object |
| 1374 |
* @genmask: generation mask |
| 1375 |
* @use: number of references to this stateful object |
| 1376 |
* @handle: unique object handle |
| 1377 |
* @udlen: length of user data |
| 1378 |
* @udata: user data |
| 1379 |
* @ops: object operations |
| 1380 |
* @data: object data, layout depends on type |
| 1381 |
*/ |
| 1382 |
struct nft_object { |
| 1383 |
struct list_head list; |
| 1384 |
struct rhlist_head rhlhead; |
| 1385 |
struct nft_object_hash_key key; |
| 1386 |
u32 genmask:2; |
| 1387 |
u32 use; |
| 1388 |
u64 handle; |
| 1389 |
u16 udlen; |
| 1390 |
u8 *udata; |
| 1391 |
/* runtime data below here */ |
| 1392 |
const struct nft_object_ops *ops ____cacheline_aligned; |
| 1393 |
unsigned char data[] |
| 1394 |
__attribute__((aligned(__alignof__(u64)))); |
| 1395 |
}; |
| 1396 |
|
| 1397 |
static inline void *nft_obj_data(const struct nft_object *obj) |
| 1398 |
{ |
| 1399 |
return (void *)obj->data; |
| 1400 |
} |
| 1401 |
|
| 1402 |
#define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr)) |
| 1403 |
|
| 1404 |
struct nft_object *nft_obj_lookup(const struct net *net, |
| 1405 |
const struct nft_table *table, |
| 1406 |
const struct nlattr *nla, u32 objtype, |
| 1407 |
u8 genmask); |
| 1408 |
|
| 1409 |
void nft_obj_notify(struct net *net, const struct nft_table *table, |
| 1410 |
struct nft_object *obj, u32 portid, u32 seq, |
| 1411 |
int event, u16 flags, int family, int report, gfp_t gfp); |
| 1412 |
|
| 1413 |
/** |
| 1414 |
* struct nft_object_type - stateful object type |
| 1415 |
* |
| 1416 |
* @select_ops: function to select nft_object_ops |
| 1417 |
* @ops: default ops, used when no select_ops functions is present |
| 1418 |
* @list: list node in list of object types |
| 1419 |
* @type: stateful object numeric type |
| 1420 |
* @owner: module owner |
| 1421 |
* @maxattr: maximum netlink attribute |
| 1422 |
* @family: address family for AF-specific object types |
| 1423 |
* @policy: netlink attribute policy |
| 1424 |
*/ |
| 1425 |
struct nft_object_type { |
| 1426 |
const struct nft_object_ops *(*select_ops)(const struct nft_ctx *, |
| 1427 |
const struct nlattr * const tb[]); |
| 1428 |
const struct nft_object_ops *ops; |
| 1429 |
struct list_head list; |
| 1430 |
u32 type; |
| 1431 |
unsigned int maxattr; |
| 1432 |
u8 family; |
| 1433 |
struct module *owner; |
| 1434 |
const struct nla_policy *policy; |
| 1435 |
}; |
| 1436 |
|
| 1437 |
/** |
| 1438 |
* struct nft_object_ops - stateful object operations |
| 1439 |
* |
| 1440 |
* @eval: stateful object evaluation function |
| 1441 |
* @size: stateful object size |
| 1442 |
* @init: initialize object from netlink attributes |
| 1443 |
* @destroy: release existing stateful object |
| 1444 |
* @dump: netlink dump stateful object |
| 1445 |
* @update: update stateful object |
| 1446 |
* @type: pointer to object type |
| 1447 |
*/ |
| 1448 |
struct nft_object_ops { |
| 1449 |
void (*eval)(struct nft_object *obj, |
| 1450 |
struct nft_regs *regs, |
| 1451 |
const struct nft_pktinfo *pkt); |
| 1452 |
unsigned int size; |
| 1453 |
int (*init)(const struct nft_ctx *ctx, |
| 1454 |
const struct nlattr *const tb[], |
| 1455 |
struct nft_object *obj); |
| 1456 |
void (*destroy)(const struct nft_ctx *ctx, |
| 1457 |
struct nft_object *obj); |
| 1458 |
int (*dump)(struct sk_buff *skb, |
| 1459 |
struct nft_object *obj, |
| 1460 |
bool reset); |
| 1461 |
void (*update)(struct nft_object *obj, |
| 1462 |
struct nft_object *newobj); |
| 1463 |
const struct nft_object_type *type; |
| 1464 |
}; |
| 1465 |
|
| 1466 |
int nft_register_obj(struct nft_object_type *obj_type); |
| 1467 |
void nft_unregister_obj(struct nft_object_type *obj_type); |
| 1468 |
|
| 1469 |
#define NFT_NETDEVICE_MAX 256 |
| 1470 |
|
| 1471 |
/** |
| 1472 |
* struct nft_flowtable - nf_tables flow table |
| 1473 |
* |
| 1474 |
* @list: flow table list node in table list |
| 1475 |
* @table: the table the flow table is contained in |
| 1476 |
* @name: name of this flow table |
| 1477 |
* @hooknum: hook number |
| 1478 |
* @ops_len: number of hooks in array |
| 1479 |
* @genmask: generation mask |
| 1480 |
* @use: number of references to this flow table |
| 1481 |
* @handle: unique object handle |
| 1482 |
* @hook_list: hook list for hooks per net_device in flowtables |
| 1483 |
* @data: rhashtable and garbage collector |
| 1484 |
*/ |
| 1485 |
struct nft_flowtable { |
| 1486 |
struct list_head list; |
| 1487 |
struct nft_table *table; |
| 1488 |
char *name; |
| 1489 |
int hooknum; |
| 1490 |
int ops_len; |
| 1491 |
u32 genmask:2; |
| 1492 |
u32 use; |
| 1493 |
u64 handle; |
| 1494 |
/* runtime data below here */ |
| 1495 |
struct list_head hook_list ____cacheline_aligned; |
| 1496 |
struct nf_flowtable data; |
| 1497 |
}; |
| 1498 |
|
| 1499 |
struct nft_flowtable *nft_flowtable_lookup(const struct net *net, |
| 1500 |
const struct nft_table *table, |
| 1501 |
const struct nlattr *nla, |
| 1502 |
u8 genmask); |
| 1503 |
|
| 1504 |
void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx, |
| 1505 |
struct nft_flowtable *flowtable, |
| 1506 |
enum nft_trans_phase phase); |
| 1507 |
|
| 1508 |
void nft_register_flowtable_type(struct nf_flowtable_type *type); |
| 1509 |
void nft_unregister_flowtable_type(struct nf_flowtable_type *type); |
| 1510 |
|
| 1511 |
/** |
| 1512 |
* struct nft_traceinfo - nft tracing information and state |
| 1513 |
* |
| 1514 |
* @trace: other struct members are initialised |
| 1515 |
* @nf_trace: copy of skb->nf_trace before rule evaluation |
| 1516 |
* @type: event type (enum nft_trace_types) |
| 1517 |
* @skbid: hash of skb to be used as trace id |
| 1518 |
* @packet_dumped: packet headers sent in a previous traceinfo message |
| 1519 |
* @basechain: base chain currently processed |
| 1520 |
*/ |
| 1521 |
struct nft_traceinfo { |
| 1522 |
bool trace; |
| 1523 |
bool nf_trace; |
| 1524 |
bool packet_dumped; |
| 1525 |
enum nft_trace_types type:8; |
| 1526 |
u32 skbid; |
| 1527 |
const struct nft_base_chain *basechain; |
| 1528 |
}; |
| 1529 |
|
| 1530 |
void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt, |
| 1531 |
const struct nft_chain *basechain); |
| 1532 |
|
| 1533 |
void nft_trace_notify(const struct nft_pktinfo *pkt, |
| 1534 |
const struct nft_verdict *verdict, |
| 1535 |
const struct nft_rule_dp *rule, |
| 1536 |
struct nft_traceinfo *info); |
| 1537 |
|
| 1538 |
#define MODULE_ALIAS_NFT_CHAIN(family, name) \ |
| 1539 |
MODULE_ALIAS("nft-chain-" __stringify(family) "-" name) |
| 1540 |
|
| 1541 |
#define MODULE_ALIAS_NFT_AF_EXPR(family, name) \ |
| 1542 |
MODULE_ALIAS("nft-expr-" __stringify(family) "-" name) |
| 1543 |
|
| 1544 |
#define MODULE_ALIAS_NFT_EXPR(name) \ |
| 1545 |
MODULE_ALIAS("nft-expr-" name) |
| 1546 |
|
| 1547 |
#define MODULE_ALIAS_NFT_OBJ(type) \ |
| 1548 |
MODULE_ALIAS("nft-obj-" __stringify(type)) |
| 1549 |
|
| 1550 |
#if IS_ENABLED(CONFIG_NF_TABLES) |
| 1551 |
|
| 1552 |
/* |
| 1553 |
* The gencursor defines two generations, the currently active and the |
| 1554 |
* next one. Objects contain a bitmask of 2 bits specifying the generations |
| 1555 |
* they're active in. A set bit means they're inactive in the generation |
| 1556 |
* represented by that bit. |
| 1557 |
* |
| 1558 |
* New objects start out as inactive in the current and active in the |
| 1559 |
* next generation. When committing the ruleset the bitmask is cleared, |
| 1560 |
* meaning they're active in all generations. When removing an object, |
| 1561 |
* it is set inactive in the next generation. After committing the ruleset, |
| 1562 |
* the objects are removed. |
| 1563 |
*/ |
| 1564 |
static inline unsigned int nft_gencursor_next(const struct net *net) |
| 1565 |
{ |
| 1566 |
return net->nft.gencursor + 1 == 1 ? 1 : 0; |
| 1567 |
} |
| 1568 |
|
| 1569 |
static inline u8 nft_genmask_next(const struct net *net) |
| 1570 |
{ |
| 1571 |
return 1 << nft_gencursor_next(net); |
| 1572 |
} |
| 1573 |
|
| 1574 |
static inline u8 nft_genmask_cur(const struct net *net) |
| 1575 |
{ |
| 1576 |
/* Use READ_ONCE() to prevent refetching the value for atomicity */ |
| 1577 |
return 1 << READ_ONCE(net->nft.gencursor); |
| 1578 |
} |
| 1579 |
|
| 1580 |
#define NFT_GENMASK_ANY ((1 << 0) | (1 << 1)) |
| 1581 |
|
| 1582 |
/* |
| 1583 |
* Generic transaction helpers |
| 1584 |
*/ |
| 1585 |
|
| 1586 |
/* Check if this object is currently active. */ |
| 1587 |
#define nft_is_active(__net, __obj) \ |
| 1588 |
(((__obj)->genmask & nft_genmask_cur(__net)) == 0) |
| 1589 |
|
| 1590 |
/* Check if this object is active in the next generation. */ |
| 1591 |
#define nft_is_active_next(__net, __obj) \ |
| 1592 |
(((__obj)->genmask & nft_genmask_next(__net)) == 0) |
| 1593 |
|
| 1594 |
/* This object becomes active in the next generation. */ |
| 1595 |
#define nft_activate_next(__net, __obj) \ |
| 1596 |
(__obj)->genmask = nft_genmask_cur(__net) |
| 1597 |
|
| 1598 |
/* This object becomes inactive in the next generation. */ |
| 1599 |
#define nft_deactivate_next(__net, __obj) \ |
| 1600 |
(__obj)->genmask = nft_genmask_next(__net) |
| 1601 |
|
| 1602 |
/* After committing the ruleset, clear the stale generation bit. */ |
| 1603 |
#define nft_clear(__net, __obj) \ |
| 1604 |
(__obj)->genmask &= ~nft_genmask_next(__net) |
| 1605 |
#define nft_active_genmask(__obj, __genmask) \ |
| 1606 |
!((__obj)->genmask & __genmask) |
| 1607 |
|
| 1608 |
/* |
| 1609 |
* Set element transaction helpers |
| 1610 |
*/ |
| 1611 |
|
| 1612 |
static inline bool nft_set_elem_active(const struct nft_set_ext *ext, |
| 1613 |
u8 genmask) |
| 1614 |
{ |
| 1615 |
return !(ext->genmask & genmask); |
| 1616 |
} |
| 1617 |
|
| 1618 |
static inline void nft_set_elem_change_active(const struct net *net, |
| 1619 |
const struct nft_set *set, |
| 1620 |
struct nft_set_ext *ext) |
| 1621 |
{ |
| 1622 |
ext->genmask ^= nft_genmask_next(net); |
| 1623 |
} |
| 1624 |
|
| 1625 |
#endif /* IS_ENABLED(CONFIG_NF_TABLES) */ |
| 1626 |
|
| 1627 |
#define NFT_SET_ELEM_DEAD_MASK (1 << 2) |
| 1628 |
|
| 1629 |
#if defined(__LITTLE_ENDIAN_BITFIELD) |
| 1630 |
#define NFT_SET_ELEM_DEAD_BIT 2 |
| 1631 |
#elif defined(__BIG_ENDIAN_BITFIELD) |
| 1632 |
#define NFT_SET_ELEM_DEAD_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2) |
| 1633 |
#else |
| 1634 |
#error |
| 1635 |
#endif |
| 1636 |
|
| 1637 |
static inline void nft_set_elem_dead(struct nft_set_ext *ext) |
| 1638 |
{ |
| 1639 |
unsigned long *word = (unsigned long *)ext; |
| 1640 |
|
| 1641 |
BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0); |
| 1642 |
set_bit(NFT_SET_ELEM_DEAD_BIT, word); |
| 1643 |
} |
| 1644 |
|
| 1645 |
static inline int nft_set_elem_is_dead(const struct nft_set_ext *ext) |
| 1646 |
{ |
| 1647 |
unsigned long *word = (unsigned long *)ext; |
| 1648 |
|
| 1649 |
BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0); |
| 1650 |
return test_bit(NFT_SET_ELEM_DEAD_BIT, word); |
| 1651 |
} |
| 1652 |
|
| 1653 |
/** |
| 1654 |
* struct nft_trans - nf_tables object update in transaction |
| 1655 |
* |
| 1656 |
* @list: used internally |
| 1657 |
* @net: struct net |
| 1658 |
* @table: struct nft_table the object resides in |
| 1659 |
* @msg_type: message type |
| 1660 |
* @seq: netlink sequence number |
| 1661 |
* @flags: modifiers to new request |
| 1662 |
* @report: notify via unicast netlink message |
| 1663 |
* @put_net: net needs to be put |
| 1664 |
* |
| 1665 |
* This is the information common to all objects in the transaction, |
| 1666 |
* this must always be the first member of derived sub-types. |
| 1667 |
*/ |
| 1668 |
struct nft_trans { |
| 1669 |
struct list_head list; |
| 1670 |
struct net *net; |
| 1671 |
struct nft_table *table; |
| 1672 |
int msg_type; |
| 1673 |
u32 seq; |
| 1674 |
u16 flags; |
| 1675 |
u8 report:1; |
| 1676 |
u8 put_net:1; |
| 1677 |
}; |
| 1678 |
|
| 1679 |
/** |
| 1680 |
* struct nft_trans_binding - nf_tables object with binding support in transaction |
| 1681 |
* @nft_trans: base structure, MUST be first member |
| 1682 |
* @binding_list: list of objects with possible bindings |
| 1683 |
* |
| 1684 |
* This is the base type used by objects that can be bound to a chain. |
| 1685 |
*/ |
| 1686 |
struct nft_trans_binding { |
| 1687 |
struct nft_trans nft_trans; |
| 1688 |
struct list_head binding_list; |
| 1689 |
}; |
| 1690 |
|
| 1691 |
struct nft_trans_rule { |
| 1692 |
struct nft_trans nft_trans; |
| 1693 |
struct nft_rule *rule; |
| 1694 |
struct nft_chain *chain; |
| 1695 |
struct nft_flow_rule *flow; |
| 1696 |
u32 rule_id; |
| 1697 |
bool bound; |
| 1698 |
}; |
| 1699 |
|
| 1700 |
#define nft_trans_container_rule(trans) \ |
| 1701 |
container_of(trans, struct nft_trans_rule, nft_trans) |
| 1702 |
#define nft_trans_rule(trans) \ |
| 1703 |
nft_trans_container_rule(trans)->rule |
| 1704 |
#define nft_trans_flow_rule(trans) \ |
| 1705 |
nft_trans_container_rule(trans)->flow |
| 1706 |
#define nft_trans_rule_id(trans) \ |
| 1707 |
nft_trans_container_rule(trans)->rule_id |
| 1708 |
#define nft_trans_rule_bound(trans) \ |
| 1709 |
nft_trans_container_rule(trans)->bound |
| 1710 |
#define nft_trans_rule_chain(trans) \ |
| 1711 |
nft_trans_container_rule(trans)->chain |
| 1712 |
|
| 1713 |
struct nft_trans_set { |
| 1714 |
struct nft_trans_binding nft_trans_binding; |
| 1715 |
struct list_head list_trans_newset; |
| 1716 |
struct nft_set *set; |
| 1717 |
u32 set_id; |
| 1718 |
u32 gc_int; |
| 1719 |
u64 timeout; |
| 1720 |
bool update; |
| 1721 |
bool bound; |
| 1722 |
u32 size; |
| 1723 |
}; |
| 1724 |
|
| 1725 |
#define nft_trans_container_set(t) \ |
| 1726 |
container_of(t, struct nft_trans_set, nft_trans_binding.nft_trans) |
| 1727 |
#define nft_trans_set(trans) \ |
| 1728 |
nft_trans_container_set(trans)->set |
| 1729 |
#define nft_trans_set_id(trans) \ |
| 1730 |
nft_trans_container_set(trans)->set_id |
| 1731 |
#define nft_trans_set_bound(trans) \ |
| 1732 |
nft_trans_container_set(trans)->bound |
| 1733 |
#define nft_trans_set_update(trans) \ |
| 1734 |
nft_trans_container_set(trans)->update |
| 1735 |
#define nft_trans_set_timeout(trans) \ |
| 1736 |
nft_trans_container_set(trans)->timeout |
| 1737 |
#define nft_trans_set_gc_int(trans) \ |
| 1738 |
nft_trans_container_set(trans)->gc_int |
| 1739 |
#define nft_trans_set_size(trans) \ |
| 1740 |
nft_trans_container_set(trans)->size |
| 1741 |
|
| 1742 |
struct nft_trans_chain { |
| 1743 |
struct nft_trans_binding nft_trans_binding; |
| 1744 |
struct nft_chain *chain; |
| 1745 |
char *name; |
| 1746 |
struct nft_stats __percpu *stats; |
| 1747 |
u8 policy; |
| 1748 |
bool update; |
| 1749 |
bool bound; |
| 1750 |
u32 chain_id; |
| 1751 |
struct nft_base_chain *basechain; |
| 1752 |
struct list_head hook_list; |
| 1753 |
}; |
| 1754 |
|
| 1755 |
#define nft_trans_container_chain(t) \ |
| 1756 |
container_of(t, struct nft_trans_chain, nft_trans_binding.nft_trans) |
| 1757 |
#define nft_trans_chain(trans) \ |
| 1758 |
nft_trans_container_chain(trans)->chain |
| 1759 |
#define nft_trans_chain_update(trans) \ |
| 1760 |
nft_trans_container_chain(trans)->update |
| 1761 |
#define nft_trans_chain_name(trans) \ |
| 1762 |
nft_trans_container_chain(trans)->name |
| 1763 |
#define nft_trans_chain_stats(trans) \ |
| 1764 |
nft_trans_container_chain(trans)->stats |
| 1765 |
#define nft_trans_chain_policy(trans) \ |
| 1766 |
nft_trans_container_chain(trans)->policy |
| 1767 |
#define nft_trans_chain_bound(trans) \ |
| 1768 |
nft_trans_container_chain(trans)->bound |
| 1769 |
#define nft_trans_chain_id(trans) \ |
| 1770 |
nft_trans_container_chain(trans)->chain_id |
| 1771 |
#define nft_trans_basechain(trans) \ |
| 1772 |
nft_trans_container_chain(trans)->basechain |
| 1773 |
#define nft_trans_chain_hooks(trans) \ |
| 1774 |
nft_trans_container_chain(trans)->hook_list |
| 1775 |
|
| 1776 |
struct nft_trans_table { |
| 1777 |
struct nft_trans nft_trans; |
| 1778 |
bool update; |
| 1779 |
}; |
| 1780 |
|
| 1781 |
#define nft_trans_container_table(trans) \ |
| 1782 |
container_of(trans, struct nft_trans_table, nft_trans) |
| 1783 |
#define nft_trans_table_update(trans) \ |
| 1784 |
nft_trans_container_table(trans)->update |
| 1785 |
|
| 1786 |
enum nft_trans_elem_flags { |
| 1787 |
NFT_TRANS_UPD_TIMEOUT = (1 << 0), |
| 1788 |
NFT_TRANS_UPD_EXPIRATION = (1 << 1), |
| 1789 |
}; |
| 1790 |
|
| 1791 |
struct nft_elem_update { |
| 1792 |
u64 timeout; |
| 1793 |
u64 expiration; |
| 1794 |
u8 flags; |
| 1795 |
}; |
| 1796 |
|
| 1797 |
struct nft_trans_one_elem { |
| 1798 |
struct nft_elem_priv *priv; |
| 1799 |
struct nft_elem_update *update; |
| 1800 |
}; |
| 1801 |
|
| 1802 |
struct nft_trans_elem { |
| 1803 |
struct nft_trans nft_trans; |
| 1804 |
struct nft_set *set; |
| 1805 |
bool bound; |
| 1806 |
unsigned int nelems; |
| 1807 |
struct nft_trans_one_elem elems[] __counted_by(nelems); |
| 1808 |
}; |
| 1809 |
|
| 1810 |
#define nft_trans_container_elem(t) \ |
| 1811 |
container_of(t, struct nft_trans_elem, nft_trans) |
| 1812 |
#define nft_trans_elem_set(trans) \ |
| 1813 |
nft_trans_container_elem(trans)->set |
| 1814 |
#define nft_trans_elem_set_bound(trans) \ |
| 1815 |
nft_trans_container_elem(trans)->bound |
| 1816 |
|
| 1817 |
struct nft_trans_obj { |
| 1818 |
struct nft_trans nft_trans; |
| 1819 |
struct nft_object *obj; |
| 1820 |
struct nft_object *newobj; |
| 1821 |
bool update; |
| 1822 |
}; |
| 1823 |
|
| 1824 |
#define nft_trans_container_obj(t) \ |
| 1825 |
container_of(t, struct nft_trans_obj, nft_trans) |
| 1826 |
#define nft_trans_obj(trans) \ |
| 1827 |
nft_trans_container_obj(trans)->obj |
| 1828 |
#define nft_trans_obj_newobj(trans) \ |
| 1829 |
nft_trans_container_obj(trans)->newobj |
| 1830 |
#define nft_trans_obj_update(trans) \ |
| 1831 |
nft_trans_container_obj(trans)->update |
| 1832 |
|
| 1833 |
struct nft_trans_flowtable { |
| 1834 |
struct nft_trans nft_trans; |
| 1835 |
struct nft_flowtable *flowtable; |
| 1836 |
struct list_head hook_list; |
| 1837 |
u32 flags; |
| 1838 |
bool update; |
| 1839 |
}; |
| 1840 |
|
| 1841 |
#define nft_trans_container_flowtable(t) \ |
| 1842 |
container_of(t, struct nft_trans_flowtable, nft_trans) |
| 1843 |
#define nft_trans_flowtable(trans) \ |
| 1844 |
nft_trans_container_flowtable(trans)->flowtable |
| 1845 |
#define nft_trans_flowtable_update(trans) \ |
| 1846 |
nft_trans_container_flowtable(trans)->update |
| 1847 |
#define nft_trans_flowtable_hooks(trans) \ |
| 1848 |
nft_trans_container_flowtable(trans)->hook_list |
| 1849 |
#define nft_trans_flowtable_flags(trans) \ |
| 1850 |
nft_trans_container_flowtable(trans)->flags |
| 1851 |
|
| 1852 |
#define NFT_TRANS_GC_BATCHCOUNT 256 |
| 1853 |
|
| 1854 |
struct nft_trans_gc { |
| 1855 |
struct list_head list; |
| 1856 |
struct net *net; |
| 1857 |
struct nft_set *set; |
| 1858 |
u32 seq; |
| 1859 |
u16 count; |
| 1860 |
struct nft_elem_priv *priv[NFT_TRANS_GC_BATCHCOUNT]; |
| 1861 |
struct rcu_head rcu; |
| 1862 |
}; |
| 1863 |
|
| 1864 |
static inline int nft_trans_gc_space(const struct nft_trans_gc *trans) |
| 1865 |
{ |
| 1866 |
return NFT_TRANS_GC_BATCHCOUNT - trans->count; |
| 1867 |
} |
| 1868 |
|
| 1869 |
static inline void nft_ctx_update(struct nft_ctx *ctx, |
| 1870 |
const struct nft_trans *trans) |
| 1871 |
{ |
| 1872 |
switch (trans->msg_type) { |
| 1873 |
case NFT_MSG_NEWRULE: |
| 1874 |
case NFT_MSG_DELRULE: |
| 1875 |
case NFT_MSG_DESTROYRULE: |
| 1876 |
ctx->chain = nft_trans_rule_chain(trans); |
| 1877 |
break; |
| 1878 |
case NFT_MSG_NEWCHAIN: |
| 1879 |
case NFT_MSG_DELCHAIN: |
| 1880 |
case NFT_MSG_DESTROYCHAIN: |
| 1881 |
ctx->chain = nft_trans_chain(trans); |
| 1882 |
break; |
| 1883 |
default: |
| 1884 |
ctx->chain = NULL; |
| 1885 |
break; |
| 1886 |
} |
| 1887 |
|
| 1888 |
ctx->net = trans->net; |
| 1889 |
ctx->table = trans->table; |
| 1890 |
ctx->family = trans->table->family; |
| 1891 |
ctx->report = trans->report; |
| 1892 |
ctx->flags = trans->flags; |
| 1893 |
ctx->seq = trans->seq; |
| 1894 |
} |
| 1895 |
|
| 1896 |
struct nft_trans_gc *nft_trans_gc_alloc(struct nft_set *set, |
| 1897 |
unsigned int gc_seq, gfp_t gfp); |
| 1898 |
void nft_trans_gc_destroy(struct nft_trans_gc *trans); |
| 1899 |
|
| 1900 |
struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc, |
| 1901 |
unsigned int gc_seq, gfp_t gfp); |
| 1902 |
void nft_trans_gc_queue_async_done(struct nft_trans_gc *gc); |
| 1903 |
|
| 1904 |
struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp); |
| 1905 |
void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans); |
| 1906 |
|
| 1907 |
void nft_trans_gc_elem_add(struct nft_trans_gc *gc, void *priv); |
| 1908 |
|
| 1909 |
struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc, |
| 1910 |
unsigned int gc_seq); |
| 1911 |
struct nft_trans_gc *nft_trans_gc_catchall_sync(struct nft_trans_gc *gc); |
| 1912 |
|
| 1913 |
void nft_setelem_data_deactivate(const struct net *net, |
| 1914 |
const struct nft_set *set, |
| 1915 |
struct nft_elem_priv *elem_priv); |
| 1916 |
|
| 1917 |
int __init nft_chain_filter_init(void); |
| 1918 |
void nft_chain_filter_fini(void); |
| 1919 |
|
| 1920 |
void __init nft_chain_route_init(void); |
| 1921 |
void nft_chain_route_fini(void); |
| 1922 |
|
| 1923 |
void nf_tables_trans_destroy_flush_work(struct net *net); |
| 1924 |
|
| 1925 |
int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result); |
| 1926 |
__be64 nf_jiffies64_to_msecs(u64 input); |
| 1927 |
|
| 1928 |
#ifdef CONFIG_MODULES |
| 1929 |
__printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...); |
| 1930 |
#else |
| 1931 |
static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; } |
| 1932 |
#endif |
| 1933 |
|
| 1934 |
struct nftables_pernet { |
| 1935 |
struct list_head tables; |
| 1936 |
struct list_head commit_list; |
| 1937 |
struct list_head destroy_list; |
| 1938 |
struct list_head commit_set_list; |
| 1939 |
struct list_head binding_list; |
| 1940 |
struct list_head module_list; |
| 1941 |
struct list_head notify_list; |
| 1942 |
struct mutex commit_mutex; |
| 1943 |
u64 table_handle; |
| 1944 |
u64 tstamp; |
| 1945 |
unsigned int gc_seq; |
| 1946 |
u8 validate_state; |
| 1947 |
struct work_struct destroy_work; |
| 1948 |
}; |
| 1949 |
|
| 1950 |
extern unsigned int nf_tables_net_id; |
| 1951 |
|
| 1952 |
static inline struct nftables_pernet *nft_pernet(const struct net *net) |
| 1953 |
{ |
| 1954 |
return net_generic(net, nf_tables_net_id); |
| 1955 |
} |
| 1956 |
|
| 1957 |
static inline u64 nft_net_tstamp(const struct net *net) |
| 1958 |
{ |
| 1959 |
return nft_pernet(net)->tstamp; |
| 1960 |
} |
| 1961 |
|
| 1962 |
#define __NFT_REDUCE_READONLY 1UL |
| 1963 |
#define NFT_REDUCE_READONLY (void *)__NFT_REDUCE_READONLY |
| 1964 |
|
| 1965 |
void nft_reg_track_update(struct nft_regs_track *track, |
| 1966 |
const struct nft_expr *expr, u8 dreg, u8 len); |
| 1967 |
void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len); |
| 1968 |
void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg); |
| 1969 |
|
| 1970 |
static inline bool nft_reg_track_cmp(struct nft_regs_track *track, |
| 1971 |
const struct nft_expr *expr, u8 dreg) |
| 1972 |
{ |
| 1973 |
return track->regs[dreg].selector && |
| 1974 |
track->regs[dreg].selector->ops == expr->ops && |
| 1975 |
track->regs[dreg].num_reg == 0; |
| 1976 |
} |
| 1977 |
|
| 1978 |
#endif /* _NET_NF_TABLES_H */ |