root/net/ipv4/netfilter/nft_dup_ipv4.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. nft_dup_ipv4_eval
  2. nft_dup_ipv4_init
  3. nft_dup_ipv4_dump
  4. nft_dup_ipv4_module_init
  5. nft_dup_ipv4_module_exit

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * Copyright (c) 2015 Pablo Neira Ayuso <pablo@netfilter.org>
   4  */
   5 
   6 #include <linux/kernel.h>
   7 #include <linux/init.h>
   8 #include <linux/module.h>
   9 #include <linux/netlink.h>
  10 #include <linux/netfilter.h>
  11 #include <linux/netfilter/nf_tables.h>
  12 #include <net/netfilter/nf_tables.h>
  13 #include <net/netfilter/ipv4/nf_dup_ipv4.h>
  14 
  15 struct nft_dup_ipv4 {
  16         enum nft_registers      sreg_addr:8;
  17         enum nft_registers      sreg_dev:8;
  18 };
  19 
  20 static void nft_dup_ipv4_eval(const struct nft_expr *expr,
  21                               struct nft_regs *regs,
  22                               const struct nft_pktinfo *pkt)
  23 {
  24         struct nft_dup_ipv4 *priv = nft_expr_priv(expr);
  25         struct in_addr gw = {
  26                 .s_addr = (__force __be32)regs->data[priv->sreg_addr],
  27         };
  28         int oif = priv->sreg_dev ? regs->data[priv->sreg_dev] : -1;
  29 
  30         nf_dup_ipv4(nft_net(pkt), pkt->skb, nft_hook(pkt), &gw, oif);
  31 }
  32 
  33 static int nft_dup_ipv4_init(const struct nft_ctx *ctx,
  34                              const struct nft_expr *expr,
  35                              const struct nlattr * const tb[])
  36 {
  37         struct nft_dup_ipv4 *priv = nft_expr_priv(expr);
  38         int err;
  39 
  40         if (tb[NFTA_DUP_SREG_ADDR] == NULL)
  41                 return -EINVAL;
  42 
  43         priv->sreg_addr = nft_parse_register(tb[NFTA_DUP_SREG_ADDR]);
  44         err = nft_validate_register_load(priv->sreg_addr, sizeof(struct in_addr));
  45         if (err < 0)
  46                 return err;
  47 
  48         if (tb[NFTA_DUP_SREG_DEV] != NULL) {
  49                 priv->sreg_dev = nft_parse_register(tb[NFTA_DUP_SREG_DEV]);
  50                 return nft_validate_register_load(priv->sreg_dev, sizeof(int));
  51         }
  52         return 0;
  53 }
  54 
  55 static int nft_dup_ipv4_dump(struct sk_buff *skb, const struct nft_expr *expr)
  56 {
  57         struct nft_dup_ipv4 *priv = nft_expr_priv(expr);
  58 
  59         if (nft_dump_register(skb, NFTA_DUP_SREG_ADDR, priv->sreg_addr))
  60                 goto nla_put_failure;
  61         if (priv->sreg_dev &&
  62             nft_dump_register(skb, NFTA_DUP_SREG_DEV, priv->sreg_dev))
  63                 goto nla_put_failure;
  64 
  65         return 0;
  66 
  67 nla_put_failure:
  68         return -1;
  69 }
  70 
  71 static struct nft_expr_type nft_dup_ipv4_type;
  72 static const struct nft_expr_ops nft_dup_ipv4_ops = {
  73         .type           = &nft_dup_ipv4_type,
  74         .size           = NFT_EXPR_SIZE(sizeof(struct nft_dup_ipv4)),
  75         .eval           = nft_dup_ipv4_eval,
  76         .init           = nft_dup_ipv4_init,
  77         .dump           = nft_dup_ipv4_dump,
  78 };
  79 
  80 static const struct nla_policy nft_dup_ipv4_policy[NFTA_DUP_MAX + 1] = {
  81         [NFTA_DUP_SREG_ADDR]    = { .type = NLA_U32 },
  82         [NFTA_DUP_SREG_DEV]     = { .type = NLA_U32 },
  83 };
  84 
  85 static struct nft_expr_type nft_dup_ipv4_type __read_mostly = {
  86         .family         = NFPROTO_IPV4,
  87         .name           = "dup",
  88         .ops            = &nft_dup_ipv4_ops,
  89         .policy         = nft_dup_ipv4_policy,
  90         .maxattr        = NFTA_DUP_MAX,
  91         .owner          = THIS_MODULE,
  92 };
  93 
  94 static int __init nft_dup_ipv4_module_init(void)
  95 {
  96         return nft_register_expr(&nft_dup_ipv4_type);
  97 }
  98 
  99 static void __exit nft_dup_ipv4_module_exit(void)
 100 {
 101         nft_unregister_expr(&nft_dup_ipv4_type);
 102 }
 103 
 104 module_init(nft_dup_ipv4_module_init);
 105 module_exit(nft_dup_ipv4_module_exit);
 106 
 107 MODULE_LICENSE("GPL");
 108 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
 109 MODULE_ALIAS_NFT_AF_EXPR(AF_INET, "dup");

/* [<][>][^][v][top][bottom][index][help] */