Pablo Neira Ayuso | d877f07 | 2015-05-31 18:04:11 +0200 | [diff] [blame] | 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 | |
| 18 | struct nft_dup_ipv6 { |
| 19 | enum nft_registers sreg_addr:8; |
| 20 | enum nft_registers sreg_dev:8; |
| 21 | }; |
| 22 | |
| 23 | static 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 *)®s->data[priv->sreg_addr]; |
| 29 | int oif = regs->data[priv->sreg_dev]; |
| 30 | |
Eric W. Biederman | 206e8c0 | 2015-09-18 14:33:02 -0500 | [diff] [blame] | 31 | nf_dup_ipv6(pkt->net, pkt->skb, pkt->hook, gw, oif); |
Pablo Neira Ayuso | d877f07 | 2015-05-31 18:04:11 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static 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 | |
| 56 | static 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 | nft_dump_register(skb, NFTA_DUP_SREG_DEV, priv->sreg_dev)) |
| 62 | goto nla_put_failure; |
| 63 | |
| 64 | return 0; |
| 65 | |
| 66 | nla_put_failure: |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | static struct nft_expr_type nft_dup_ipv6_type; |
| 71 | static const struct nft_expr_ops nft_dup_ipv6_ops = { |
| 72 | .type = &nft_dup_ipv6_type, |
| 73 | .size = NFT_EXPR_SIZE(sizeof(struct nft_dup_ipv6)), |
| 74 | .eval = nft_dup_ipv6_eval, |
| 75 | .init = nft_dup_ipv6_init, |
| 76 | .dump = nft_dup_ipv6_dump, |
| 77 | }; |
| 78 | |
| 79 | static const struct nla_policy nft_dup_ipv6_policy[NFTA_DUP_MAX + 1] = { |
| 80 | [NFTA_DUP_SREG_ADDR] = { .type = NLA_U32 }, |
| 81 | [NFTA_DUP_SREG_DEV] = { .type = NLA_U32 }, |
| 82 | }; |
| 83 | |
| 84 | static struct nft_expr_type nft_dup_ipv6_type __read_mostly = { |
| 85 | .family = NFPROTO_IPV6, |
| 86 | .name = "dup", |
| 87 | .ops = &nft_dup_ipv6_ops, |
| 88 | .policy = nft_dup_ipv6_policy, |
| 89 | .maxattr = NFTA_DUP_MAX, |
| 90 | .owner = THIS_MODULE, |
| 91 | }; |
| 92 | |
| 93 | static int __init nft_dup_ipv6_module_init(void) |
| 94 | { |
| 95 | return nft_register_expr(&nft_dup_ipv6_type); |
| 96 | } |
| 97 | |
| 98 | static void __exit nft_dup_ipv6_module_exit(void) |
| 99 | { |
| 100 | nft_unregister_expr(&nft_dup_ipv6_type); |
| 101 | } |
| 102 | |
| 103 | module_init(nft_dup_ipv6_module_init); |
| 104 | module_exit(nft_dup_ipv6_module_exit); |
| 105 | |
| 106 | MODULE_LICENSE("GPL"); |
| 107 | MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>"); |
| 108 | MODULE_ALIAS_NFT_AF_EXPR(AF_INET6, "dup"); |