1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/netlink.h>
12 #include <linux/netfilter.h>
13 #include <linux/netfilter/nf_tables.h>
14 #include <net/netfilter/nf_tables_core.h>
15 #include <net/netfilter/nf_tables.h>
18 enum nft_registers sreg
:8;
19 enum nft_registers dreg
:8;
25 void nft_bitwise_eval(const struct nft_expr
*expr
,
26 struct nft_regs
*regs
, const struct nft_pktinfo
*pkt
)
28 const struct nft_bitwise
*priv
= nft_expr_priv(expr
);
29 const u32
*src
= ®s
->data
[priv
->sreg
];
30 u32
*dst
= ®s
->data
[priv
->dreg
];
33 for (i
= 0; i
< DIV_ROUND_UP(priv
->len
, 4); i
++)
34 dst
[i
] = (src
[i
] & priv
->mask
.data
[i
]) ^ priv
->xor.data
[i
];
37 static const struct nla_policy nft_bitwise_policy
[NFTA_BITWISE_MAX
+ 1] = {
38 [NFTA_BITWISE_SREG
] = { .type
= NLA_U32
},
39 [NFTA_BITWISE_DREG
] = { .type
= NLA_U32
},
40 [NFTA_BITWISE_LEN
] = { .type
= NLA_U32
},
41 [NFTA_BITWISE_MASK
] = { .type
= NLA_NESTED
},
42 [NFTA_BITWISE_XOR
] = { .type
= NLA_NESTED
},
45 static int nft_bitwise_init(const struct nft_ctx
*ctx
,
46 const struct nft_expr
*expr
,
47 const struct nlattr
* const tb
[])
49 struct nft_bitwise
*priv
= nft_expr_priv(expr
);
50 struct nft_data_desc d1
, d2
;
54 if (tb
[NFTA_BITWISE_SREG
] == NULL
||
55 tb
[NFTA_BITWISE_DREG
] == NULL
||
56 tb
[NFTA_BITWISE_LEN
] == NULL
||
57 tb
[NFTA_BITWISE_MASK
] == NULL
||
58 tb
[NFTA_BITWISE_XOR
] == NULL
)
61 err
= nft_parse_u32_check(tb
[NFTA_BITWISE_LEN
], U8_MAX
, &len
);
67 priv
->sreg
= nft_parse_register(tb
[NFTA_BITWISE_SREG
]);
68 err
= nft_validate_register_load(priv
->sreg
, priv
->len
);
72 priv
->dreg
= nft_parse_register(tb
[NFTA_BITWISE_DREG
]);
73 err
= nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
74 NFT_DATA_VALUE
, priv
->len
);
78 err
= nft_data_init(NULL
, &priv
->mask
, sizeof(priv
->mask
), &d1
,
79 tb
[NFTA_BITWISE_MASK
]);
82 if (d1
.len
!= priv
->len
) {
87 err
= nft_data_init(NULL
, &priv
->xor, sizeof(priv
->xor), &d2
,
88 tb
[NFTA_BITWISE_XOR
]);
91 if (d2
.len
!= priv
->len
) {
98 nft_data_release(&priv
->xor, d2
.type
);
100 nft_data_release(&priv
->mask
, d1
.type
);
104 static int nft_bitwise_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
106 const struct nft_bitwise
*priv
= nft_expr_priv(expr
);
108 if (nft_dump_register(skb
, NFTA_BITWISE_SREG
, priv
->sreg
))
109 goto nla_put_failure
;
110 if (nft_dump_register(skb
, NFTA_BITWISE_DREG
, priv
->dreg
))
111 goto nla_put_failure
;
112 if (nla_put_be32(skb
, NFTA_BITWISE_LEN
, htonl(priv
->len
)))
113 goto nla_put_failure
;
115 if (nft_data_dump(skb
, NFTA_BITWISE_MASK
, &priv
->mask
,
116 NFT_DATA_VALUE
, priv
->len
) < 0)
117 goto nla_put_failure
;
119 if (nft_data_dump(skb
, NFTA_BITWISE_XOR
, &priv
->xor,
120 NFT_DATA_VALUE
, priv
->len
) < 0)
121 goto nla_put_failure
;
129 static const struct nft_expr_ops nft_bitwise_ops
= {
130 .type
= &nft_bitwise_type
,
131 .size
= NFT_EXPR_SIZE(sizeof(struct nft_bitwise
)),
132 .eval
= nft_bitwise_eval
,
133 .init
= nft_bitwise_init
,
134 .dump
= nft_bitwise_dump
,
137 struct nft_expr_type nft_bitwise_type __read_mostly
= {
139 .ops
= &nft_bitwise_ops
,
140 .policy
= nft_bitwise_policy
,
141 .maxattr
= NFTA_BITWISE_MAX
,
142 .owner
= THIS_MODULE
,