Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

netfilter: nft_limit: add per-byte limiting

This patch adds a new NFTA_LIMIT_TYPE netlink attribute to indicate the type of
limiting.

Contrary to per-packet limiting, the cost is calculated from the packet path
since this depends on the packet length.

The burst attribute indicates the number of bytes in which the rate can be
exceeded.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>

+66 -4
+7
include/uapi/linux/netfilter/nf_tables.h
··· 756 756 }; 757 757 #define NFTA_CT_MAX (__NFTA_CT_MAX - 1) 758 758 759 + enum nft_limit_type { 760 + NFT_LIMIT_PKTS, 761 + NFT_LIMIT_PKT_BYTES 762 + }; 763 + 759 764 /** 760 765 * enum nft_limit_attributes - nf_tables limit expression netlink attributes 761 766 * 762 767 * @NFTA_LIMIT_RATE: refill rate (NLA_U64) 763 768 * @NFTA_LIMIT_UNIT: refill unit (NLA_U64) 764 769 * @NFTA_LIMIT_BURST: burst (NLA_U32) 770 + * @NFTA_LIMIT_TYPE: type of limit (NLA_U32: enum nft_limit_type) 765 771 */ 766 772 enum nft_limit_attributes { 767 773 NFTA_LIMIT_UNSPEC, 768 774 NFTA_LIMIT_RATE, 769 775 NFTA_LIMIT_UNIT, 770 776 NFTA_LIMIT_BURST, 777 + NFTA_LIMIT_TYPE, 771 778 __NFTA_LIMIT_MAX 772 779 }; 773 780 #define NFTA_LIMIT_MAX (__NFTA_LIMIT_MAX - 1)
+59 -4
net/netfilter/nft_limit.c
··· 83 83 return 0; 84 84 } 85 85 86 - static int nft_limit_dump(struct sk_buff *skb, const struct nft_limit *limit) 86 + static int nft_limit_dump(struct sk_buff *skb, const struct nft_limit *limit, 87 + enum nft_limit_type type) 87 88 { 88 89 u64 secs = div_u64(limit->nsecs, NSEC_PER_SEC); 89 90 u64 rate = limit->rate - limit->burst; 90 91 91 92 if (nla_put_be64(skb, NFTA_LIMIT_RATE, cpu_to_be64(rate)) || 92 93 nla_put_be64(skb, NFTA_LIMIT_UNIT, cpu_to_be64(secs)) || 93 - nla_put_be32(skb, NFTA_LIMIT_BURST, htonl(limit->burst))) 94 + nla_put_be32(skb, NFTA_LIMIT_BURST, htonl(limit->burst)) || 95 + nla_put_be32(skb, NFTA_LIMIT_TYPE, htonl(type))) 94 96 goto nla_put_failure; 95 97 return 0; 96 98 ··· 119 117 [NFTA_LIMIT_RATE] = { .type = NLA_U64 }, 120 118 [NFTA_LIMIT_UNIT] = { .type = NLA_U64 }, 121 119 [NFTA_LIMIT_BURST] = { .type = NLA_U32 }, 120 + [NFTA_LIMIT_TYPE] = { .type = NLA_U32 }, 122 121 }; 123 122 124 123 static int nft_limit_pkts_init(const struct nft_ctx *ctx, ··· 141 138 { 142 139 const struct nft_limit_pkts *priv = nft_expr_priv(expr); 143 140 144 - return nft_limit_dump(skb, &priv->limit); 141 + return nft_limit_dump(skb, &priv->limit, NFT_LIMIT_PKTS); 145 142 } 146 143 147 144 static struct nft_expr_type nft_limit_type; ··· 153 150 .dump = nft_limit_pkts_dump, 154 151 }; 155 152 153 + static void nft_limit_pkt_bytes_eval(const struct nft_expr *expr, 154 + struct nft_regs *regs, 155 + const struct nft_pktinfo *pkt) 156 + { 157 + struct nft_limit *priv = nft_expr_priv(expr); 158 + u64 cost = div_u64(priv->nsecs * pkt->skb->len, priv->rate); 159 + 160 + if (nft_limit_eval(priv, cost)) 161 + regs->verdict.code = NFT_BREAK; 162 + } 163 + 164 + static int nft_limit_pkt_bytes_init(const struct nft_ctx *ctx, 165 + const struct nft_expr *expr, 166 + const struct nlattr * const tb[]) 167 + { 168 + struct nft_limit *priv = nft_expr_priv(expr); 169 + 170 + return nft_limit_init(priv, tb); 171 + } 172 + 173 + static int nft_limit_pkt_bytes_dump(struct sk_buff *skb, 174 + const struct nft_expr *expr) 175 + { 176 + const struct nft_limit *priv = nft_expr_priv(expr); 177 + 178 + return nft_limit_dump(skb, priv, NFT_LIMIT_PKT_BYTES); 179 + } 180 + 181 + static const struct nft_expr_ops nft_limit_pkt_bytes_ops = { 182 + .type = &nft_limit_type, 183 + .size = NFT_EXPR_SIZE(sizeof(struct nft_limit)), 184 + .eval = nft_limit_pkt_bytes_eval, 185 + .init = nft_limit_pkt_bytes_init, 186 + .dump = nft_limit_pkt_bytes_dump, 187 + }; 188 + 189 + static const struct nft_expr_ops * 190 + nft_limit_select_ops(const struct nft_ctx *ctx, 191 + const struct nlattr * const tb[]) 192 + { 193 + if (tb[NFTA_LIMIT_TYPE] == NULL) 194 + return &nft_limit_pkts_ops; 195 + 196 + switch (ntohl(nla_get_be32(tb[NFTA_LIMIT_TYPE]))) { 197 + case NFT_LIMIT_PKTS: 198 + return &nft_limit_pkts_ops; 199 + case NFT_LIMIT_PKT_BYTES: 200 + return &nft_limit_pkt_bytes_ops; 201 + } 202 + return ERR_PTR(-EOPNOTSUPP); 203 + } 204 + 156 205 static struct nft_expr_type nft_limit_type __read_mostly = { 157 206 .name = "limit", 158 - .ops = &nft_limit_pkts_ops, 207 + .select_ops = nft_limit_select_ops, 159 208 .policy = nft_limit_policy, 160 209 .maxattr = NFTA_LIMIT_MAX, 161 210 .flags = NFT_EXPR_STATEFUL,