linux/net/netfilter/nft_exthdr.c
<<
>>
Prefs
   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
  21struct nft_exthdr {
  22        u8                      type;
  23        u8                      offset;
  24        u8                      len;
  25        enum nft_registers      dreg:8;
  26};
  27
  28static void nft_exthdr_eval(const struct nft_expr *expr,
  29                            struct nft_data data[NFT_REG_MAX + 1],
  30                            const struct nft_pktinfo *pkt)
  31{
  32        struct nft_exthdr *priv = nft_expr_priv(expr);
  33        struct nft_data *dest = &data[priv->dreg];
  34        unsigned int offset = 0;
  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
  42        if (skb_copy_bits(pkt->skb, offset, dest->data, priv->len) < 0)
  43                goto err;
  44        return;
  45err:
  46        data[NFT_REG_VERDICT].verdict = NFT_BREAK;
  47}
  48
  49static const struct nla_policy nft_exthdr_policy[NFTA_EXTHDR_MAX + 1] = {
  50        [NFTA_EXTHDR_DREG]              = { .type = NLA_U32 },
  51        [NFTA_EXTHDR_TYPE]              = { .type = NLA_U8 },
  52        [NFTA_EXTHDR_OFFSET]            = { .type = NLA_U32 },
  53        [NFTA_EXTHDR_LEN]               = { .type = NLA_U32 },
  54};
  55
  56static int nft_exthdr_init(const struct nft_ctx *ctx,
  57                           const struct nft_expr *expr,
  58                           const struct nlattr * const tb[])
  59{
  60        struct nft_exthdr *priv = nft_expr_priv(expr);
  61        int err;
  62
  63        if (tb[NFTA_EXTHDR_DREG] == NULL ||
  64            tb[NFTA_EXTHDR_TYPE] == NULL ||
  65            tb[NFTA_EXTHDR_OFFSET] == NULL ||
  66            tb[NFTA_EXTHDR_LEN] == NULL)
  67                return -EINVAL;
  68
  69        priv->type   = nla_get_u8(tb[NFTA_EXTHDR_TYPE]);
  70        priv->offset = ntohl(nla_get_be32(tb[NFTA_EXTHDR_OFFSET]));
  71        priv->len    = ntohl(nla_get_be32(tb[NFTA_EXTHDR_LEN]));
  72        if (priv->len == 0 ||
  73            priv->len > FIELD_SIZEOF(struct nft_data, data))
  74                return -EINVAL;
  75
  76        priv->dreg = ntohl(nla_get_be32(tb[NFTA_EXTHDR_DREG]));
  77        err = nft_validate_output_register(priv->dreg);
  78        if (err < 0)
  79                return err;
  80        return nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
  81}
  82
  83static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr)
  84{
  85        const struct nft_exthdr *priv = nft_expr_priv(expr);
  86
  87        if (nla_put_be32(skb, NFTA_EXTHDR_DREG, htonl(priv->dreg)))
  88                goto nla_put_failure;
  89        if (nla_put_u8(skb, NFTA_EXTHDR_TYPE, priv->type))
  90                goto nla_put_failure;
  91        if (nla_put_be32(skb, NFTA_EXTHDR_OFFSET, htonl(priv->offset)))
  92                goto nla_put_failure;
  93        if (nla_put_be32(skb, NFTA_EXTHDR_LEN, htonl(priv->len)))
  94                goto nla_put_failure;
  95        return 0;
  96
  97nla_put_failure:
  98        return -1;
  99}
 100
 101static struct nft_expr_type nft_exthdr_type;
 102static const struct nft_expr_ops nft_exthdr_ops = {
 103        .type           = &nft_exthdr_type,
 104        .size           = NFT_EXPR_SIZE(sizeof(struct nft_exthdr)),
 105        .eval           = nft_exthdr_eval,
 106        .init           = nft_exthdr_init,
 107        .dump           = nft_exthdr_dump,
 108};
 109
 110static struct nft_expr_type nft_exthdr_type __read_mostly = {
 111        .name           = "exthdr",
 112        .ops            = &nft_exthdr_ops,
 113        .policy         = nft_exthdr_policy,
 114        .maxattr        = NFTA_EXTHDR_MAX,
 115        .owner          = THIS_MODULE,
 116};
 117
 118static int __init nft_exthdr_module_init(void)
 119{
 120        return nft_register_expr(&nft_exthdr_type);
 121}
 122
 123static void __exit nft_exthdr_module_exit(void)
 124{
 125        nft_unregister_expr(&nft_exthdr_type);
 126}
 127
 128module_init(nft_exthdr_module_init);
 129module_exit(nft_exthdr_module_exit);
 130
 131MODULE_LICENSE("GPL");
 132MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
 133MODULE_ALIAS_NFT_EXPR("exthdr");
 134