1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/netlink.h>
10 #include <linux/netfilter.h>
11 #include <linux/netfilter/nf_tables.h>
12 #include <net/netfilter/nf_tables_core.h>
13 #include <net/netfilter/nf_tables.h>
15 struct nft_range_expr
{
16 struct nft_data data_from
;
17 struct nft_data data_to
;
18 enum nft_registers sreg
:8;
20 enum nft_range_ops op
:8;
23 void nft_range_eval(const struct nft_expr
*expr
,
24 struct nft_regs
*regs
, const struct nft_pktinfo
*pkt
)
26 const struct nft_range_expr
*priv
= nft_expr_priv(expr
);
29 d1
= memcmp(®s
->data
[priv
->sreg
], &priv
->data_from
, priv
->len
);
30 d2
= memcmp(®s
->data
[priv
->sreg
], &priv
->data_to
, priv
->len
);
34 regs
->verdict
.code
= NFT_BREAK
;
37 if (d1
>= 0 && d2
<= 0)
38 regs
->verdict
.code
= NFT_BREAK
;
43 static const struct nla_policy nft_range_policy
[NFTA_RANGE_MAX
+ 1] = {
44 [NFTA_RANGE_SREG
] = { .type
= NLA_U32
},
45 [NFTA_RANGE_OP
] = { .type
= NLA_U32
},
46 [NFTA_RANGE_FROM_DATA
] = { .type
= NLA_NESTED
},
47 [NFTA_RANGE_TO_DATA
] = { .type
= NLA_NESTED
},
50 static int nft_range_init(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
,
51 const struct nlattr
* const tb
[])
53 struct nft_range_expr
*priv
= nft_expr_priv(expr
);
54 struct nft_data_desc desc_from
, desc_to
;
58 if (!tb
[NFTA_RANGE_SREG
] ||
60 !tb
[NFTA_RANGE_FROM_DATA
] ||
61 !tb
[NFTA_RANGE_TO_DATA
])
64 err
= nft_data_init(NULL
, &priv
->data_from
, sizeof(priv
->data_from
),
65 &desc_from
, tb
[NFTA_RANGE_FROM_DATA
]);
69 err
= nft_data_init(NULL
, &priv
->data_to
, sizeof(priv
->data_to
),
70 &desc_to
, tb
[NFTA_RANGE_TO_DATA
]);
74 if (desc_from
.len
!= desc_to
.len
) {
79 priv
->sreg
= nft_parse_register(tb
[NFTA_RANGE_SREG
]);
80 err
= nft_validate_register_load(priv
->sreg
, desc_from
.len
);
84 err
= nft_parse_u32_check(tb
[NFTA_RANGE_OP
], U8_MAX
, &op
);
98 priv
->len
= desc_from
.len
;
101 nft_data_release(&priv
->data_to
, desc_to
.type
);
103 nft_data_release(&priv
->data_from
, desc_from
.type
);
107 static int nft_range_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
109 const struct nft_range_expr
*priv
= nft_expr_priv(expr
);
111 if (nft_dump_register(skb
, NFTA_RANGE_SREG
, priv
->sreg
))
112 goto nla_put_failure
;
113 if (nla_put_be32(skb
, NFTA_RANGE_OP
, htonl(priv
->op
)))
114 goto nla_put_failure
;
116 if (nft_data_dump(skb
, NFTA_RANGE_FROM_DATA
, &priv
->data_from
,
117 NFT_DATA_VALUE
, priv
->len
) < 0 ||
118 nft_data_dump(skb
, NFTA_RANGE_TO_DATA
, &priv
->data_to
,
119 NFT_DATA_VALUE
, priv
->len
) < 0)
120 goto nla_put_failure
;
127 static const struct nft_expr_ops nft_range_ops
= {
128 .type
= &nft_range_type
,
129 .size
= NFT_EXPR_SIZE(sizeof(struct nft_range_expr
)),
130 .eval
= nft_range_eval
,
131 .init
= nft_range_init
,
132 .dump
= nft_range_dump
,
135 struct nft_expr_type nft_range_type __read_mostly
= {
137 .ops
= &nft_range_ops
,
138 .policy
= nft_range_policy
,
139 .maxattr
= NFTA_RANGE_MAX
,
140 .owner
= THIS_MODULE
,