linux/net/ipv6/netfilter/nft_dup_ipv6.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2015 Pablo Neira Ayuso <pablo@netfilter.org>
   3 *
   4 * This program is free software; you can redistribute it and/or modify it
   5 * under the terms of the GNU General Public License version 2 as published by
   6 * the Free Software Foundation.
   7 */
   8
   9#include <linux/kernel.h>
  10#include <linux/init.h>
  11#include <linux/module.h>
  12#include <linux/netlink.h>
  13#include <linux/netfilter.h>
  14#include <linux/netfilter/nf_tables.h>
  15#include <net/netfilter/nf_tables.h>
  16#include <net/netfilter/ipv6/nf_dup_ipv6.h>
  17
  18struct nft_dup_ipv6 {
  19        enum nft_registers      sreg_addr:8;
  20        enum nft_registers      sreg_dev:8;
  21};
  22
  23static void nft_dup_ipv6_eval(const struct nft_expr *expr,
  24                              struct nft_regs *regs,
  25                              const struct nft_pktinfo *pkt)
  26{
  27        struct nft_dup_ipv6 *priv = nft_expr_priv(expr);
  28        struct in6_addr *gw = (struct in6_addr *)&regs->data[priv->sreg_addr];
  29        int oif = priv->sreg_dev ? regs->data[priv->sreg_dev] : -1;
  30
  31        nf_dup_ipv6(nft_net(pkt), pkt->skb, nft_hook(pkt), gw, oif);
  32}
  33
  34static int nft_dup_ipv6_init(const struct nft_ctx *ctx,
  35                             const struct nft_expr *expr,
  36                             const struct nlattr * const tb[])
  37{
  38        struct nft_dup_ipv6 *priv = nft_expr_priv(expr);
  39        int err;
  40
  41        if (tb[NFTA_DUP_SREG_ADDR] == NULL)
  42                return -EINVAL;
  43
  44        priv->sreg_addr = nft_parse_register(tb[NFTA_DUP_SREG_ADDR]);
  45        err = nft_validate_register_load(priv->sreg_addr, sizeof(struct in6_addr));
  46        if (err < 0)
  47                return err;
  48
  49        if (tb[NFTA_DUP_SREG_DEV] != NULL) {
  50                priv->sreg_dev = nft_parse_register(tb[NFTA_DUP_SREG_DEV]);
  51                return nft_validate_register_load(priv->sreg_dev, sizeof(int));
  52        }
  53        return 0;
  54}
  55
  56static int nft_dup_ipv6_dump(struct sk_buff *skb, const struct nft_expr *expr)
  57{
  58        struct nft_dup_ipv6 *priv = nft_expr_priv(expr);
  59
  60        if (nft_dump_register(skb, NFTA_DUP_SREG_ADDR, priv->sreg_addr))
  61                goto nla_put_failure;
  62        if (priv->sreg_dev &&
  63            nft_dump_register(skb, NFTA_DUP_SREG_DEV, priv->sreg_dev))
  64                goto nla_put_failure;
  65
  66        return 0;
  67
  68nla_put_failure:
  69        return -1;
  70}
  71
  72static struct nft_expr_type nft_dup_ipv6_type;
  73static const struct nft_expr_ops nft_dup_ipv6_ops = {
  74        .type           = &nft_dup_ipv6_type,
  75        .size           = NFT_EXPR_SIZE(sizeof(struct nft_dup_ipv6)),
  76        .eval           = nft_dup_ipv6_eval,
  77        .init           = nft_dup_ipv6_init,
  78        .dump           = nft_dup_ipv6_dump,
  79};
  80
  81static const struct nla_policy nft_dup_ipv6_policy[NFTA_DUP_MAX + 1] = {
  82        [NFTA_DUP_SREG_ADDR]    = { .type = NLA_U32 },
  83        [NFTA_DUP_SREG_DEV]     = { .type = NLA_U32 },
  84};
  85
  86static struct nft_expr_type nft_dup_ipv6_type __read_mostly = {
  87        .family         = NFPROTO_IPV6,
  88        .name           = "dup",
  89        .ops            = &nft_dup_ipv6_ops,
  90        .policy         = nft_dup_ipv6_policy,
  91        .maxattr        = NFTA_DUP_MAX,
  92        .owner          = THIS_MODULE,
  93};
  94
  95static int __init nft_dup_ipv6_module_init(void)
  96{
  97        return nft_register_expr(&nft_dup_ipv6_type);
  98}
  99
 100static void __exit nft_dup_ipv6_module_exit(void)
 101{
 102        nft_unregister_expr(&nft_dup_ipv6_type);
 103}
 104
 105module_init(nft_dup_ipv6_module_init);
 106module_exit(nft_dup_ipv6_module_exit);
 107
 108MODULE_LICENSE("GPL");
 109MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
 110MODULE_ALIAS_NFT_AF_EXPR(AF_INET6, "dup");
 111