Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1 | /* |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 2 | * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 3 | * |
| 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. |
| 7 | * |
| 8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/netlink.h> |
| 15 | #include <linux/netfilter.h> |
| 16 | #include <linux/netfilter/nf_tables.h> |
| 17 | #include <net/netfilter/nf_tables_core.h> |
| 18 | #include <net/netfilter/nf_tables.h> |
| 19 | |
| 20 | struct nft_bitwise { |
| 21 | enum nft_registers sreg:8; |
| 22 | enum nft_registers dreg:8; |
| 23 | u8 len; |
| 24 | struct nft_data mask; |
| 25 | struct nft_data xor; |
| 26 | }; |
| 27 | |
| 28 | static void nft_bitwise_eval(const struct nft_expr *expr, |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 29 | struct nft_regs *regs, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 30 | const struct nft_pktinfo *pkt) |
| 31 | { |
| 32 | const struct nft_bitwise *priv = nft_expr_priv(expr); |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 33 | const u32 *src = ®s->data[priv->sreg]; |
| 34 | u32 *dst = ®s->data[priv->dreg]; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 35 | unsigned int i; |
| 36 | |
Patrick McHardy | fad136e | 2015-04-11 02:27:33 +0100 | [diff] [blame] | 37 | for (i = 0; i < DIV_ROUND_UP(priv->len, 4); i++) |
| 38 | dst[i] = (src[i] & priv->mask.data[i]) ^ priv->xor.data[i]; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static const struct nla_policy nft_bitwise_policy[NFTA_BITWISE_MAX + 1] = { |
| 42 | [NFTA_BITWISE_SREG] = { .type = NLA_U32 }, |
| 43 | [NFTA_BITWISE_DREG] = { .type = NLA_U32 }, |
| 44 | [NFTA_BITWISE_LEN] = { .type = NLA_U32 }, |
| 45 | [NFTA_BITWISE_MASK] = { .type = NLA_NESTED }, |
| 46 | [NFTA_BITWISE_XOR] = { .type = NLA_NESTED }, |
| 47 | }; |
| 48 | |
| 49 | static int nft_bitwise_init(const struct nft_ctx *ctx, |
| 50 | const struct nft_expr *expr, |
| 51 | const struct nlattr * const tb[]) |
| 52 | { |
| 53 | struct nft_bitwise *priv = nft_expr_priv(expr); |
| 54 | struct nft_data_desc d1, d2; |
Laura Garcia Liebana | 36b701f | 2016-09-14 15:00:02 +0200 | [diff] [blame] | 55 | u32 len; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 56 | int err; |
| 57 | |
| 58 | if (tb[NFTA_BITWISE_SREG] == NULL || |
| 59 | tb[NFTA_BITWISE_DREG] == NULL || |
| 60 | tb[NFTA_BITWISE_LEN] == NULL || |
| 61 | tb[NFTA_BITWISE_MASK] == NULL || |
| 62 | tb[NFTA_BITWISE_XOR] == NULL) |
| 63 | return -EINVAL; |
| 64 | |
Laura Garcia Liebana | 36b701f | 2016-09-14 15:00:02 +0200 | [diff] [blame] | 65 | err = nft_parse_u32_check(tb[NFTA_BITWISE_LEN], U8_MAX, &len); |
| 66 | if (err < 0) |
| 67 | return err; |
| 68 | |
| 69 | priv->len = len; |
| 70 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 71 | priv->sreg = nft_parse_register(tb[NFTA_BITWISE_SREG]); |
Patrick McHardy | d07db98 | 2015-04-11 02:27:30 +0100 | [diff] [blame] | 72 | err = nft_validate_register_load(priv->sreg, priv->len); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 73 | if (err < 0) |
| 74 | return err; |
| 75 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 76 | priv->dreg = nft_parse_register(tb[NFTA_BITWISE_DREG]); |
Patrick McHardy | 1ec1021 | 2015-04-11 02:27:27 +0100 | [diff] [blame] | 77 | err = nft_validate_register_store(ctx, priv->dreg, NULL, |
| 78 | NFT_DATA_VALUE, priv->len); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 79 | if (err < 0) |
| 80 | return err; |
| 81 | |
Patrick McHardy | d0a11fc | 2015-04-11 02:27:38 +0100 | [diff] [blame] | 82 | err = nft_data_init(NULL, &priv->mask, sizeof(priv->mask), &d1, |
| 83 | tb[NFTA_BITWISE_MASK]); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 84 | if (err < 0) |
| 85 | return err; |
| 86 | if (d1.len != priv->len) |
| 87 | return -EINVAL; |
| 88 | |
Patrick McHardy | d0a11fc | 2015-04-11 02:27:38 +0100 | [diff] [blame] | 89 | err = nft_data_init(NULL, &priv->xor, sizeof(priv->xor), &d2, |
| 90 | tb[NFTA_BITWISE_XOR]); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 91 | if (err < 0) |
| 92 | return err; |
| 93 | if (d2.len != priv->len) |
| 94 | return -EINVAL; |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int nft_bitwise_dump(struct sk_buff *skb, const struct nft_expr *expr) |
| 100 | { |
| 101 | const struct nft_bitwise *priv = nft_expr_priv(expr); |
| 102 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 103 | if (nft_dump_register(skb, NFTA_BITWISE_SREG, priv->sreg)) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 104 | goto nla_put_failure; |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 105 | if (nft_dump_register(skb, NFTA_BITWISE_DREG, priv->dreg)) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 106 | goto nla_put_failure; |
| 107 | if (nla_put_be32(skb, NFTA_BITWISE_LEN, htonl(priv->len))) |
| 108 | goto nla_put_failure; |
| 109 | |
| 110 | if (nft_data_dump(skb, NFTA_BITWISE_MASK, &priv->mask, |
| 111 | NFT_DATA_VALUE, priv->len) < 0) |
| 112 | goto nla_put_failure; |
| 113 | |
| 114 | if (nft_data_dump(skb, NFTA_BITWISE_XOR, &priv->xor, |
| 115 | NFT_DATA_VALUE, priv->len) < 0) |
| 116 | goto nla_put_failure; |
| 117 | |
| 118 | return 0; |
| 119 | |
| 120 | nla_put_failure: |
| 121 | return -1; |
| 122 | } |
| 123 | |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 124 | static struct nft_expr_type nft_bitwise_type; |
| 125 | static const struct nft_expr_ops nft_bitwise_ops = { |
| 126 | .type = &nft_bitwise_type, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 127 | .size = NFT_EXPR_SIZE(sizeof(struct nft_bitwise)), |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 128 | .eval = nft_bitwise_eval, |
| 129 | .init = nft_bitwise_init, |
| 130 | .dump = nft_bitwise_dump, |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | static struct nft_expr_type nft_bitwise_type __read_mostly = { |
| 134 | .name = "bitwise", |
| 135 | .ops = &nft_bitwise_ops, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 136 | .policy = nft_bitwise_policy, |
| 137 | .maxattr = NFTA_BITWISE_MAX, |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 138 | .owner = THIS_MODULE, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | int __init nft_bitwise_module_init(void) |
| 142 | { |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 143 | return nft_register_expr(&nft_bitwise_type); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void nft_bitwise_module_exit(void) |
| 147 | { |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 148 | nft_unregister_expr(&nft_bitwise_type); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 149 | } |