0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/kernel.h>
0010 #include <linux/init.h>
0011 #include <linux/module.h>
0012 #include <linux/netlink.h>
0013 #include <linux/jhash.h>
0014 #include <linux/netfilter.h>
0015 #include <linux/netfilter/nf_tables.h>
0016 #include <net/netfilter/nf_tables.h>
0017 #include <net/netfilter/nf_queue.h>
0018
0019 static u32 jhash_initval __read_mostly;
0020
0021 struct nft_queue {
0022 u8 sreg_qnum;
0023 u16 queuenum;
0024 u16 queues_total;
0025 u16 flags;
0026 };
0027
0028 static void nft_queue_eval(const struct nft_expr *expr,
0029 struct nft_regs *regs,
0030 const struct nft_pktinfo *pkt)
0031 {
0032 struct nft_queue *priv = nft_expr_priv(expr);
0033 u32 queue = priv->queuenum;
0034 u32 ret;
0035
0036 if (priv->queues_total > 1) {
0037 if (priv->flags & NFT_QUEUE_FLAG_CPU_FANOUT) {
0038 int cpu = raw_smp_processor_id();
0039
0040 queue = priv->queuenum + cpu % priv->queues_total;
0041 } else {
0042 queue = nfqueue_hash(pkt->skb, queue,
0043 priv->queues_total, nft_pf(pkt),
0044 jhash_initval);
0045 }
0046 }
0047
0048 ret = NF_QUEUE_NR(queue);
0049 if (priv->flags & NFT_QUEUE_FLAG_BYPASS)
0050 ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
0051
0052 regs->verdict.code = ret;
0053 }
0054
0055 static void nft_queue_sreg_eval(const struct nft_expr *expr,
0056 struct nft_regs *regs,
0057 const struct nft_pktinfo *pkt)
0058 {
0059 struct nft_queue *priv = nft_expr_priv(expr);
0060 u32 queue, ret;
0061
0062 queue = regs->data[priv->sreg_qnum];
0063
0064 ret = NF_QUEUE_NR(queue);
0065 if (priv->flags & NFT_QUEUE_FLAG_BYPASS)
0066 ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
0067
0068 regs->verdict.code = ret;
0069 }
0070
0071 static int nft_queue_validate(const struct nft_ctx *ctx,
0072 const struct nft_expr *expr,
0073 const struct nft_data **data)
0074 {
0075 static const unsigned int supported_hooks = ((1 << NF_INET_PRE_ROUTING) |
0076 (1 << NF_INET_LOCAL_IN) |
0077 (1 << NF_INET_FORWARD) |
0078 (1 << NF_INET_LOCAL_OUT) |
0079 (1 << NF_INET_POST_ROUTING));
0080
0081 switch (ctx->family) {
0082 case NFPROTO_IPV4:
0083 case NFPROTO_IPV6:
0084 case NFPROTO_INET:
0085 case NFPROTO_BRIDGE:
0086 break;
0087 case NFPROTO_NETDEV:
0088 fallthrough;
0089 default:
0090 return -EOPNOTSUPP;
0091 }
0092
0093 return nft_chain_validate_hooks(ctx->chain, supported_hooks);
0094 }
0095
0096 static const struct nla_policy nft_queue_policy[NFTA_QUEUE_MAX + 1] = {
0097 [NFTA_QUEUE_NUM] = { .type = NLA_U16 },
0098 [NFTA_QUEUE_TOTAL] = { .type = NLA_U16 },
0099 [NFTA_QUEUE_FLAGS] = { .type = NLA_U16 },
0100 [NFTA_QUEUE_SREG_QNUM] = { .type = NLA_U32 },
0101 };
0102
0103 static int nft_queue_init(const struct nft_ctx *ctx,
0104 const struct nft_expr *expr,
0105 const struct nlattr * const tb[])
0106 {
0107 struct nft_queue *priv = nft_expr_priv(expr);
0108 u32 maxid;
0109
0110 priv->queuenum = ntohs(nla_get_be16(tb[NFTA_QUEUE_NUM]));
0111
0112 if (tb[NFTA_QUEUE_TOTAL])
0113 priv->queues_total = ntohs(nla_get_be16(tb[NFTA_QUEUE_TOTAL]));
0114 else
0115 priv->queues_total = 1;
0116
0117 if (priv->queues_total == 0)
0118 return -EINVAL;
0119
0120 maxid = priv->queues_total - 1 + priv->queuenum;
0121 if (maxid > U16_MAX)
0122 return -ERANGE;
0123
0124 if (tb[NFTA_QUEUE_FLAGS]) {
0125 priv->flags = ntohs(nla_get_be16(tb[NFTA_QUEUE_FLAGS]));
0126 if (priv->flags & ~NFT_QUEUE_FLAG_MASK)
0127 return -EINVAL;
0128 }
0129 return 0;
0130 }
0131
0132 static int nft_queue_sreg_init(const struct nft_ctx *ctx,
0133 const struct nft_expr *expr,
0134 const struct nlattr * const tb[])
0135 {
0136 struct nft_queue *priv = nft_expr_priv(expr);
0137 int err;
0138
0139 err = nft_parse_register_load(tb[NFTA_QUEUE_SREG_QNUM],
0140 &priv->sreg_qnum, sizeof(u32));
0141 if (err < 0)
0142 return err;
0143
0144 if (tb[NFTA_QUEUE_FLAGS]) {
0145 priv->flags = ntohs(nla_get_be16(tb[NFTA_QUEUE_FLAGS]));
0146 if (priv->flags & ~NFT_QUEUE_FLAG_MASK)
0147 return -EINVAL;
0148 if (priv->flags & NFT_QUEUE_FLAG_CPU_FANOUT)
0149 return -EOPNOTSUPP;
0150 }
0151
0152 return 0;
0153 }
0154
0155 static int nft_queue_dump(struct sk_buff *skb, const struct nft_expr *expr)
0156 {
0157 const struct nft_queue *priv = nft_expr_priv(expr);
0158
0159 if (nla_put_be16(skb, NFTA_QUEUE_NUM, htons(priv->queuenum)) ||
0160 nla_put_be16(skb, NFTA_QUEUE_TOTAL, htons(priv->queues_total)) ||
0161 nla_put_be16(skb, NFTA_QUEUE_FLAGS, htons(priv->flags)))
0162 goto nla_put_failure;
0163
0164 return 0;
0165
0166 nla_put_failure:
0167 return -1;
0168 }
0169
0170 static int
0171 nft_queue_sreg_dump(struct sk_buff *skb, const struct nft_expr *expr)
0172 {
0173 const struct nft_queue *priv = nft_expr_priv(expr);
0174
0175 if (nft_dump_register(skb, NFTA_QUEUE_SREG_QNUM, priv->sreg_qnum) ||
0176 nla_put_be16(skb, NFTA_QUEUE_FLAGS, htons(priv->flags)))
0177 goto nla_put_failure;
0178
0179 return 0;
0180
0181 nla_put_failure:
0182 return -1;
0183 }
0184
0185 static struct nft_expr_type nft_queue_type;
0186 static const struct nft_expr_ops nft_queue_ops = {
0187 .type = &nft_queue_type,
0188 .size = NFT_EXPR_SIZE(sizeof(struct nft_queue)),
0189 .eval = nft_queue_eval,
0190 .init = nft_queue_init,
0191 .dump = nft_queue_dump,
0192 .validate = nft_queue_validate,
0193 .reduce = NFT_REDUCE_READONLY,
0194 };
0195
0196 static const struct nft_expr_ops nft_queue_sreg_ops = {
0197 .type = &nft_queue_type,
0198 .size = NFT_EXPR_SIZE(sizeof(struct nft_queue)),
0199 .eval = nft_queue_sreg_eval,
0200 .init = nft_queue_sreg_init,
0201 .dump = nft_queue_sreg_dump,
0202 .validate = nft_queue_validate,
0203 .reduce = NFT_REDUCE_READONLY,
0204 };
0205
0206 static const struct nft_expr_ops *
0207 nft_queue_select_ops(const struct nft_ctx *ctx,
0208 const struct nlattr * const tb[])
0209 {
0210 if (tb[NFTA_QUEUE_NUM] && tb[NFTA_QUEUE_SREG_QNUM])
0211 return ERR_PTR(-EINVAL);
0212
0213 init_hashrandom(&jhash_initval);
0214
0215 if (tb[NFTA_QUEUE_NUM])
0216 return &nft_queue_ops;
0217
0218 if (tb[NFTA_QUEUE_SREG_QNUM])
0219 return &nft_queue_sreg_ops;
0220
0221 return ERR_PTR(-EINVAL);
0222 }
0223
0224 static struct nft_expr_type nft_queue_type __read_mostly = {
0225 .name = "queue",
0226 .select_ops = nft_queue_select_ops,
0227 .policy = nft_queue_policy,
0228 .maxattr = NFTA_QUEUE_MAX,
0229 .owner = THIS_MODULE,
0230 };
0231
0232 static int __init nft_queue_module_init(void)
0233 {
0234 return nft_register_expr(&nft_queue_type);
0235 }
0236
0237 static void __exit nft_queue_module_exit(void)
0238 {
0239 nft_unregister_expr(&nft_queue_type);
0240 }
0241
0242 module_init(nft_queue_module_init);
0243 module_exit(nft_queue_module_exit);
0244
0245 MODULE_LICENSE("GPL");
0246 MODULE_AUTHOR("Eric Leblond <eric@regit.org>");
0247 MODULE_ALIAS_NFT_EXPR("queue");
0248 MODULE_DESCRIPTION("Netfilter nftables queue module");