nf_tables.h 27 KB

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