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 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 20 | static void nft_payload_eval(const struct nft_expr *expr, |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 21 | struct nft_regs *regs, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 22 | const struct nft_pktinfo *pkt) |
| 23 | { |
| 24 | const struct nft_payload *priv = nft_expr_priv(expr); |
| 25 | const struct sk_buff *skb = pkt->skb; |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 26 | u32 *dest = ®s->data[priv->dreg]; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 27 | int offset; |
| 28 | |
| 29 | switch (priv->base) { |
| 30 | case NFT_PAYLOAD_LL_HEADER: |
| 31 | if (!skb_mac_header_was_set(skb)) |
| 32 | goto err; |
| 33 | offset = skb_mac_header(skb) - skb->data; |
| 34 | break; |
| 35 | case NFT_PAYLOAD_NETWORK_HEADER: |
| 36 | offset = skb_network_offset(skb); |
| 37 | break; |
| 38 | case NFT_PAYLOAD_TRANSPORT_HEADER: |
Pablo Neira Ayuso | c54032e | 2013-10-11 10:00:22 +0200 | [diff] [blame] | 39 | offset = pkt->xt.thoff; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 40 | break; |
| 41 | default: |
| 42 | BUG(); |
| 43 | } |
| 44 | offset += priv->offset; |
| 45 | |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 46 | dest[priv->len / NFT_REG32_SIZE] = 0; |
Patrick McHardy | fad136e | 2015-04-11 02:27:33 +0100 | [diff] [blame] | 47 | if (skb_copy_bits(skb, offset, dest, priv->len) < 0) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 48 | goto err; |
| 49 | return; |
| 50 | err: |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 51 | regs->verdict.code = NFT_BREAK; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static const struct nla_policy nft_payload_policy[NFTA_PAYLOAD_MAX + 1] = { |
| 55 | [NFTA_PAYLOAD_DREG] = { .type = NLA_U32 }, |
| 56 | [NFTA_PAYLOAD_BASE] = { .type = NLA_U32 }, |
| 57 | [NFTA_PAYLOAD_OFFSET] = { .type = NLA_U32 }, |
| 58 | [NFTA_PAYLOAD_LEN] = { .type = NLA_U32 }, |
| 59 | }; |
| 60 | |
| 61 | static int nft_payload_init(const struct nft_ctx *ctx, |
| 62 | const struct nft_expr *expr, |
| 63 | const struct nlattr * const tb[]) |
| 64 | { |
| 65 | struct nft_payload *priv = nft_expr_priv(expr); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 66 | |
Patrick McHardy | c29b72e | 2013-10-10 11:06:41 +0200 | [diff] [blame] | 67 | priv->base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE])); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 68 | priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET])); |
| 69 | priv->len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN])); |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 70 | priv->dreg = nft_parse_register(tb[NFTA_PAYLOAD_DREG]); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 71 | |
Patrick McHardy | 1ec1021 | 2015-04-11 02:27:27 +0100 | [diff] [blame] | 72 | return nft_validate_register_store(ctx, priv->dreg, NULL, |
| 73 | NFT_DATA_VALUE, priv->len); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static int nft_payload_dump(struct sk_buff *skb, const struct nft_expr *expr) |
| 77 | { |
| 78 | const struct nft_payload *priv = nft_expr_priv(expr); |
| 79 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 80 | if (nft_dump_register(skb, NFTA_PAYLOAD_DREG, priv->dreg) || |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 81 | nla_put_be32(skb, NFTA_PAYLOAD_BASE, htonl(priv->base)) || |
| 82 | nla_put_be32(skb, NFTA_PAYLOAD_OFFSET, htonl(priv->offset)) || |
| 83 | nla_put_be32(skb, NFTA_PAYLOAD_LEN, htonl(priv->len))) |
| 84 | goto nla_put_failure; |
| 85 | return 0; |
| 86 | |
| 87 | nla_put_failure: |
| 88 | return -1; |
| 89 | } |
| 90 | |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 91 | static struct nft_expr_type nft_payload_type; |
| 92 | static const struct nft_expr_ops nft_payload_ops = { |
| 93 | .type = &nft_payload_type, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 94 | .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)), |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 95 | .eval = nft_payload_eval, |
| 96 | .init = nft_payload_init, |
| 97 | .dump = nft_payload_dump, |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 98 | }; |
| 99 | |
Patrick McHardy | c29b72e | 2013-10-10 11:06:41 +0200 | [diff] [blame] | 100 | const struct nft_expr_ops nft_payload_fast_ops = { |
| 101 | .type = &nft_payload_type, |
| 102 | .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)), |
| 103 | .eval = nft_payload_eval, |
| 104 | .init = nft_payload_init, |
| 105 | .dump = nft_payload_dump, |
| 106 | }; |
| 107 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 108 | static const struct nft_expr_ops * |
| 109 | nft_payload_select_ops(const struct nft_ctx *ctx, |
| 110 | const struct nlattr * const tb[]) |
Patrick McHardy | c29b72e | 2013-10-10 11:06:41 +0200 | [diff] [blame] | 111 | { |
| 112 | enum nft_payload_bases base; |
| 113 | unsigned int offset, len; |
| 114 | |
| 115 | if (tb[NFTA_PAYLOAD_DREG] == NULL || |
| 116 | tb[NFTA_PAYLOAD_BASE] == NULL || |
| 117 | tb[NFTA_PAYLOAD_OFFSET] == NULL || |
| 118 | tb[NFTA_PAYLOAD_LEN] == NULL) |
| 119 | return ERR_PTR(-EINVAL); |
| 120 | |
| 121 | base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE])); |
| 122 | switch (base) { |
| 123 | case NFT_PAYLOAD_LL_HEADER: |
| 124 | case NFT_PAYLOAD_NETWORK_HEADER: |
| 125 | case NFT_PAYLOAD_TRANSPORT_HEADER: |
| 126 | break; |
| 127 | default: |
| 128 | return ERR_PTR(-EOPNOTSUPP); |
| 129 | } |
| 130 | |
| 131 | offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET])); |
Patrick McHardy | 45d9bcd | 2015-04-11 02:27:26 +0100 | [diff] [blame] | 132 | len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN])); |
Patrick McHardy | c29b72e | 2013-10-10 11:06:41 +0200 | [diff] [blame] | 133 | |
Nikolay Aleksandrov | f627ed9 | 2014-02-16 14:01:58 +0100 | [diff] [blame] | 134 | if (len <= 4 && is_power_of_2(len) && IS_ALIGNED(offset, len) && |
| 135 | base != NFT_PAYLOAD_LL_HEADER) |
Patrick McHardy | c29b72e | 2013-10-10 11:06:41 +0200 | [diff] [blame] | 136 | return &nft_payload_fast_ops; |
| 137 | else |
| 138 | return &nft_payload_ops; |
| 139 | } |
| 140 | |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 141 | static struct nft_expr_type nft_payload_type __read_mostly = { |
| 142 | .name = "payload", |
Patrick McHardy | c29b72e | 2013-10-10 11:06:41 +0200 | [diff] [blame] | 143 | .select_ops = nft_payload_select_ops, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 144 | .policy = nft_payload_policy, |
| 145 | .maxattr = NFTA_PAYLOAD_MAX, |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 146 | .owner = THIS_MODULE, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | int __init nft_payload_module_init(void) |
| 150 | { |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 151 | return nft_register_expr(&nft_payload_type); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void nft_payload_module_exit(void) |
| 155 | { |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 156 | nft_unregister_expr(&nft_payload_type); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 157 | } |