Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 Patrick McHardy <kaber@trash.net> |
| 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.h> |
| 18 | // FIXME: |
| 19 | #include <net/ipv6.h> |
| 20 | |
| 21 | struct nft_exthdr { |
| 22 | u8 type; |
| 23 | u8 offset; |
| 24 | u8 len; |
| 25 | enum nft_registers dreg:8; |
| 26 | }; |
| 27 | |
| 28 | static void nft_exthdr_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 | struct nft_exthdr *priv = nft_expr_priv(expr); |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 33 | u32 *dest = ®s->data[priv->dreg]; |
Daniel Borkmann | 540436c | 2013-12-20 11:23:15 +0100 | [diff] [blame] | 34 | unsigned int offset = 0; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 35 | int err; |
| 36 | |
| 37 | err = ipv6_find_hdr(pkt->skb, &offset, priv->type, NULL, NULL); |
| 38 | if (err < 0) |
| 39 | goto err; |
| 40 | offset += priv->offset; |
| 41 | |
Patrick McHardy | 49499c3 | 2015-04-11 02:27:37 +0100 | [diff] [blame] | 42 | dest[priv->len / NFT_REG32_SIZE] = 0; |
Patrick McHardy | fad136e | 2015-04-11 02:27:33 +0100 | [diff] [blame] | 43 | if (skb_copy_bits(pkt->skb, offset, dest, priv->len) < 0) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 44 | goto err; |
| 45 | return; |
| 46 | err: |
Patrick McHardy | a55e22e | 2015-04-11 02:27:31 +0100 | [diff] [blame] | 47 | regs->verdict.code = NFT_BREAK; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static const struct nla_policy nft_exthdr_policy[NFTA_EXTHDR_MAX + 1] = { |
| 51 | [NFTA_EXTHDR_DREG] = { .type = NLA_U32 }, |
| 52 | [NFTA_EXTHDR_TYPE] = { .type = NLA_U8 }, |
| 53 | [NFTA_EXTHDR_OFFSET] = { .type = NLA_U32 }, |
| 54 | [NFTA_EXTHDR_LEN] = { .type = NLA_U32 }, |
| 55 | }; |
| 56 | |
| 57 | static int nft_exthdr_init(const struct nft_ctx *ctx, |
| 58 | const struct nft_expr *expr, |
| 59 | const struct nlattr * const tb[]) |
| 60 | { |
| 61 | struct nft_exthdr *priv = nft_expr_priv(expr); |
Laura Garcia Liebana | 4da449a | 2016-08-09 20:46:16 +0200 | [diff] [blame] | 62 | u32 offset, len; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 63 | |
| 64 | if (tb[NFTA_EXTHDR_DREG] == NULL || |
| 65 | tb[NFTA_EXTHDR_TYPE] == NULL || |
| 66 | tb[NFTA_EXTHDR_OFFSET] == NULL || |
| 67 | tb[NFTA_EXTHDR_LEN] == NULL) |
| 68 | return -EINVAL; |
| 69 | |
Laura Garcia Liebana | 4da449a | 2016-08-09 20:46:16 +0200 | [diff] [blame] | 70 | offset = ntohl(nla_get_be32(tb[NFTA_EXTHDR_OFFSET])); |
| 71 | len = ntohl(nla_get_be32(tb[NFTA_EXTHDR_LEN])); |
| 72 | |
| 73 | if (offset > U8_MAX || len > U8_MAX) |
| 74 | return -ERANGE; |
| 75 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 76 | priv->type = nla_get_u8(tb[NFTA_EXTHDR_TYPE]); |
Laura Garcia Liebana | 4da449a | 2016-08-09 20:46:16 +0200 | [diff] [blame] | 77 | priv->offset = offset; |
| 78 | priv->len = len; |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 79 | priv->dreg = nft_parse_register(tb[NFTA_EXTHDR_DREG]); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 80 | |
Patrick McHardy | 1ec1021 | 2015-04-11 02:27:27 +0100 | [diff] [blame] | 81 | return nft_validate_register_store(ctx, priv->dreg, NULL, |
| 82 | NFT_DATA_VALUE, priv->len); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr) |
| 86 | { |
| 87 | const struct nft_exthdr *priv = nft_expr_priv(expr); |
| 88 | |
Patrick McHardy | b1c96ed | 2015-04-11 02:27:36 +0100 | [diff] [blame] | 89 | if (nft_dump_register(skb, NFTA_EXTHDR_DREG, priv->dreg)) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 90 | goto nla_put_failure; |
| 91 | if (nla_put_u8(skb, NFTA_EXTHDR_TYPE, priv->type)) |
| 92 | goto nla_put_failure; |
| 93 | if (nla_put_be32(skb, NFTA_EXTHDR_OFFSET, htonl(priv->offset))) |
| 94 | goto nla_put_failure; |
| 95 | if (nla_put_be32(skb, NFTA_EXTHDR_LEN, htonl(priv->len))) |
| 96 | goto nla_put_failure; |
| 97 | return 0; |
| 98 | |
| 99 | nla_put_failure: |
| 100 | return -1; |
| 101 | } |
| 102 | |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 103 | static struct nft_expr_type nft_exthdr_type; |
| 104 | static const struct nft_expr_ops nft_exthdr_ops = { |
| 105 | .type = &nft_exthdr_type, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 106 | .size = NFT_EXPR_SIZE(sizeof(struct nft_exthdr)), |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 107 | .eval = nft_exthdr_eval, |
| 108 | .init = nft_exthdr_init, |
| 109 | .dump = nft_exthdr_dump, |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | static struct nft_expr_type nft_exthdr_type __read_mostly = { |
| 113 | .name = "exthdr", |
| 114 | .ops = &nft_exthdr_ops, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 115 | .policy = nft_exthdr_policy, |
| 116 | .maxattr = NFTA_EXTHDR_MAX, |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 117 | .owner = THIS_MODULE, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | static int __init nft_exthdr_module_init(void) |
| 121 | { |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 122 | return nft_register_expr(&nft_exthdr_type); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static void __exit nft_exthdr_module_exit(void) |
| 126 | { |
Patrick McHardy | ef1f7df | 2013-10-10 11:41:20 +0200 | [diff] [blame] | 127 | nft_unregister_expr(&nft_exthdr_type); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | module_init(nft_exthdr_module_init); |
| 131 | module_exit(nft_exthdr_module_exit); |
| 132 | |
| 133 | MODULE_LICENSE("GPL"); |
| 134 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 135 | MODULE_ALIAS_NFT_EXPR("exthdr"); |