1 // SPDX-License-Identifier: GPL-2.0-only
5 * Development of this code partly funded by OISF
6 * (http://www.openinfosecfoundation.org/)
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/netlink.h>
13 #include <linux/jhash.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter/nf_tables.h>
16 #include <net/netfilter/nf_tables.h>
17 #include <net/netfilter/nf_queue.h>
19 static u32 jhash_initval __read_mostly;
28 static void nft_queue_eval(const struct nft_expr *expr,
29 struct nft_regs *regs,
30 const struct nft_pktinfo *pkt)
32 struct nft_queue *priv = nft_expr_priv(expr);
33 u32 queue = priv->queuenum;
36 if (priv->queues_total > 1) {
37 if (priv->flags & NFT_QUEUE_FLAG_CPU_FANOUT) {
38 int cpu = raw_smp_processor_id();
40 queue = priv->queuenum + cpu % priv->queues_total;
42 queue = nfqueue_hash(pkt->skb, queue,
43 priv->queues_total, nft_pf(pkt),
48 ret = NF_QUEUE_NR(queue);
49 if (priv->flags & NFT_QUEUE_FLAG_BYPASS)
50 ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
52 regs->verdict.code = ret;
55 static void nft_queue_sreg_eval(const struct nft_expr *expr,
56 struct nft_regs *regs,
57 const struct nft_pktinfo *pkt)
59 struct nft_queue *priv = nft_expr_priv(expr);
62 queue = regs->data[priv->sreg_qnum];
64 ret = NF_QUEUE_NR(queue);
65 if (priv->flags & NFT_QUEUE_FLAG_BYPASS)
66 ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
68 regs->verdict.code = ret;
71 static int nft_queue_validate(const struct nft_ctx *ctx,
72 const struct nft_expr *expr,
73 const struct nft_data **data)
75 static const unsigned int supported_hooks = ((1 << NF_INET_PRE_ROUTING) |
76 (1 << NF_INET_LOCAL_IN) |
77 (1 << NF_INET_FORWARD) |
78 (1 << NF_INET_LOCAL_OUT) |
79 (1 << NF_INET_POST_ROUTING));
81 switch (ctx->family) {
87 case NFPROTO_NETDEV: /* lacks okfn */
93 return nft_chain_validate_hooks(ctx->chain, supported_hooks);
96 static const struct nla_policy nft_queue_policy[NFTA_QUEUE_MAX + 1] = {
97 [NFTA_QUEUE_NUM] = { .type = NLA_U16 },
98 [NFTA_QUEUE_TOTAL] = { .type = NLA_U16 },
99 [NFTA_QUEUE_FLAGS] = { .type = NLA_U16 },
100 [NFTA_QUEUE_SREG_QNUM] = { .type = NLA_U32 },
103 static int nft_queue_init(const struct nft_ctx *ctx,
104 const struct nft_expr *expr,
105 const struct nlattr * const tb[])
107 struct nft_queue *priv = nft_expr_priv(expr);
110 priv->queuenum = ntohs(nla_get_be16(tb[NFTA_QUEUE_NUM]));
112 if (tb[NFTA_QUEUE_TOTAL])
113 priv->queues_total = ntohs(nla_get_be16(tb[NFTA_QUEUE_TOTAL]));
115 priv->queues_total = 1;
117 if (priv->queues_total == 0)
120 maxid = priv->queues_total - 1 + priv->queuenum;
124 if (tb[NFTA_QUEUE_FLAGS]) {
125 priv->flags = ntohs(nla_get_be16(tb[NFTA_QUEUE_FLAGS]));
126 if (priv->flags & ~NFT_QUEUE_FLAG_MASK)
132 static int nft_queue_sreg_init(const struct nft_ctx *ctx,
133 const struct nft_expr *expr,
134 const struct nlattr * const tb[])
136 struct nft_queue *priv = nft_expr_priv(expr);
139 err = nft_parse_register_load(tb[NFTA_QUEUE_SREG_QNUM],
140 &priv->sreg_qnum, sizeof(u32));
144 if (tb[NFTA_QUEUE_FLAGS]) {
145 priv->flags = ntohs(nla_get_be16(tb[NFTA_QUEUE_FLAGS]));
146 if (priv->flags & ~NFT_QUEUE_FLAG_MASK)
148 if (priv->flags & NFT_QUEUE_FLAG_CPU_FANOUT)
155 static int nft_queue_dump(struct sk_buff *skb,
156 const struct nft_expr *expr, bool reset)
158 const struct nft_queue *priv = nft_expr_priv(expr);
160 if (nla_put_be16(skb, NFTA_QUEUE_NUM, htons(priv->queuenum)) ||
161 nla_put_be16(skb, NFTA_QUEUE_TOTAL, htons(priv->queues_total)) ||
162 nla_put_be16(skb, NFTA_QUEUE_FLAGS, htons(priv->flags)))
163 goto nla_put_failure;
172 nft_queue_sreg_dump(struct sk_buff *skb,
173 const struct nft_expr *expr, bool reset)
175 const struct nft_queue *priv = nft_expr_priv(expr);
177 if (nft_dump_register(skb, NFTA_QUEUE_SREG_QNUM, priv->sreg_qnum) ||
178 nla_put_be16(skb, NFTA_QUEUE_FLAGS, htons(priv->flags)))
179 goto nla_put_failure;
187 static struct nft_expr_type nft_queue_type;
188 static const struct nft_expr_ops nft_queue_ops = {
189 .type = &nft_queue_type,
190 .size = NFT_EXPR_SIZE(sizeof(struct nft_queue)),
191 .eval = nft_queue_eval,
192 .init = nft_queue_init,
193 .dump = nft_queue_dump,
194 .validate = nft_queue_validate,
195 .reduce = NFT_REDUCE_READONLY,
198 static const struct nft_expr_ops nft_queue_sreg_ops = {
199 .type = &nft_queue_type,
200 .size = NFT_EXPR_SIZE(sizeof(struct nft_queue)),
201 .eval = nft_queue_sreg_eval,
202 .init = nft_queue_sreg_init,
203 .dump = nft_queue_sreg_dump,
204 .validate = nft_queue_validate,
205 .reduce = NFT_REDUCE_READONLY,
208 static const struct nft_expr_ops *
209 nft_queue_select_ops(const struct nft_ctx *ctx,
210 const struct nlattr * const tb[])
212 if (tb[NFTA_QUEUE_NUM] && tb[NFTA_QUEUE_SREG_QNUM])
213 return ERR_PTR(-EINVAL);
215 init_hashrandom(&jhash_initval);
217 if (tb[NFTA_QUEUE_NUM])
218 return &nft_queue_ops;
220 if (tb[NFTA_QUEUE_SREG_QNUM])
221 return &nft_queue_sreg_ops;
223 return ERR_PTR(-EINVAL);
226 static struct nft_expr_type nft_queue_type __read_mostly = {
228 .select_ops = nft_queue_select_ops,
229 .policy = nft_queue_policy,
230 .maxattr = NFTA_QUEUE_MAX,
231 .owner = THIS_MODULE,
234 static int __init nft_queue_module_init(void)
236 return nft_register_expr(&nft_queue_type);
239 static void __exit nft_queue_module_exit(void)
241 nft_unregister_expr(&nft_queue_type);
244 module_init(nft_queue_module_init);
245 module_exit(nft_queue_module_exit);
247 MODULE_LICENSE("GPL");
249 MODULE_ALIAS_NFT_EXPR("queue");
250 MODULE_DESCRIPTION("Netfilter nftables queue module");