2 * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/netlink.h>
13 #include <linux/netfilter.h>
14 #include <linux/netfilter/nf_tables.h>
15 #include <net/netfilter/nf_tables_core.h>
16 #include <net/netfilter/nf_tables.h>
18 struct nft_range_expr
{
19 struct nft_data data_from
;
20 struct nft_data data_to
;
21 enum nft_registers sreg
:8;
23 enum nft_range_ops op
:8;
26 static void nft_range_eval(const struct nft_expr
*expr
,
27 struct nft_regs
*regs
,
28 const struct nft_pktinfo
*pkt
)
30 const struct nft_range_expr
*priv
= nft_expr_priv(expr
);
33 d1
= memcmp(®s
->data
[priv
->sreg
], &priv
->data_from
, priv
->len
);
34 d2
= memcmp(®s
->data
[priv
->sreg
], &priv
->data_to
, priv
->len
);
38 regs
->verdict
.code
= NFT_BREAK
;
41 if (d1
>= 0 && d2
<= 0)
42 regs
->verdict
.code
= NFT_BREAK
;
47 static const struct nla_policy nft_range_policy
[NFTA_RANGE_MAX
+ 1] = {
48 [NFTA_RANGE_SREG
] = { .type
= NLA_U32
},
49 [NFTA_RANGE_OP
] = { .type
= NLA_U32
},
50 [NFTA_RANGE_FROM_DATA
] = { .type
= NLA_NESTED
},
51 [NFTA_RANGE_TO_DATA
] = { .type
= NLA_NESTED
},
54 static int nft_range_init(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
,
55 const struct nlattr
* const tb
[])
57 struct nft_range_expr
*priv
= nft_expr_priv(expr
);
58 struct nft_data_desc desc_from
, desc_to
;
62 if (!tb
[NFTA_RANGE_SREG
] ||
64 !tb
[NFTA_RANGE_FROM_DATA
] ||
65 !tb
[NFTA_RANGE_TO_DATA
])
68 err
= nft_data_init(NULL
, &priv
->data_from
, sizeof(priv
->data_from
),
69 &desc_from
, tb
[NFTA_RANGE_FROM_DATA
]);
73 err
= nft_data_init(NULL
, &priv
->data_to
, sizeof(priv
->data_to
),
74 &desc_to
, tb
[NFTA_RANGE_TO_DATA
]);
78 if (desc_from
.len
!= desc_to
.len
) {
83 priv
->sreg
= nft_parse_register(tb
[NFTA_RANGE_SREG
]);
84 err
= nft_validate_register_load(priv
->sreg
, desc_from
.len
);
88 err
= nft_parse_u32_check(tb
[NFTA_RANGE_OP
], U8_MAX
, &op
);
102 priv
->len
= desc_from
.len
;
105 nft_data_uninit(&priv
->data_to
, desc_to
.type
);
107 nft_data_uninit(&priv
->data_from
, desc_from
.type
);
111 static int nft_range_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
113 const struct nft_range_expr
*priv
= nft_expr_priv(expr
);
115 if (nft_dump_register(skb
, NFTA_RANGE_SREG
, priv
->sreg
))
116 goto nla_put_failure
;
117 if (nla_put_be32(skb
, NFTA_RANGE_OP
, htonl(priv
->op
)))
118 goto nla_put_failure
;
120 if (nft_data_dump(skb
, NFTA_RANGE_FROM_DATA
, &priv
->data_from
,
121 NFT_DATA_VALUE
, priv
->len
) < 0 ||
122 nft_data_dump(skb
, NFTA_RANGE_TO_DATA
, &priv
->data_to
,
123 NFT_DATA_VALUE
, priv
->len
) < 0)
124 goto nla_put_failure
;
131 static struct nft_expr_type nft_range_type
;
132 static const struct nft_expr_ops nft_range_ops
= {
133 .type
= &nft_range_type
,
134 .size
= NFT_EXPR_SIZE(sizeof(struct nft_range_expr
)),
135 .eval
= nft_range_eval
,
136 .init
= nft_range_init
,
137 .dump
= nft_range_dump
,
140 static struct nft_expr_type nft_range_type __read_mostly
= {
142 .ops
= &nft_range_ops
,
143 .policy
= nft_range_policy
,
144 .maxattr
= NFTA_RANGE_MAX
,
145 .owner
= THIS_MODULE
,
148 int __init
nft_range_module_init(void)
150 return nft_register_expr(&nft_range_type
);
153 void nft_range_module_exit(void)
155 nft_unregister_expr(&nft_range_type
);