Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 Patrick McHardy <kaber@trash.net> |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 3 | * Copyright (c) 2012-2013 Pablo Neira Ayuso <pablo@netfilter.org> |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
| 10 | */ |
| 11 | |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/ipv6.h> |
| 15 | #include <linux/netfilter_ipv6.h> |
| 16 | #include <net/netfilter/nf_tables.h> |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 17 | #include <net/netfilter/nf_tables_ipv6.h> |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 18 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 19 | static unsigned int nft_do_chain_ipv6(void *priv, |
Patrick McHardy | 3b088c4 | 2014-01-03 12:16:13 +0000 | [diff] [blame] | 20 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 21 | const struct nf_hook_state *state) |
Patrick McHardy | 3b088c4 | 2014-01-03 12:16:13 +0000 | [diff] [blame] | 22 | { |
| 23 | struct nft_pktinfo pkt; |
| 24 | |
| 25 | /* malformed packet, drop it */ |
Eric W. Biederman | 6aa187f | 2015-09-18 14:32:57 -0500 | [diff] [blame] | 26 | if (nft_set_pktinfo_ipv6(&pkt, skb, state) < 0) |
Patrick McHardy | 3b088c4 | 2014-01-03 12:16:13 +0000 | [diff] [blame] | 27 | return NF_DROP; |
| 28 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 29 | return nft_do_chain(&pkt, priv); |
Patrick McHardy | 3b088c4 | 2014-01-03 12:16:13 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 32 | static unsigned int nft_ipv6_output(void *priv, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 33 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 34 | const struct nf_hook_state *state) |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 35 | { |
| 36 | if (unlikely(skb->len < sizeof(struct ipv6hdr))) { |
| 37 | if (net_ratelimit()) |
| 38 | pr_info("nf_tables_ipv6: ignoring short SOCK_RAW " |
| 39 | "packet\n"); |
| 40 | return NF_ACCEPT; |
| 41 | } |
| 42 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 43 | return nft_do_chain_ipv6(priv, skb, state); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 44 | } |
| 45 | |
Patrick McHardy | 1d49144 | 2014-01-03 12:16:16 +0000 | [diff] [blame] | 46 | struct nft_af_info nft_af_ipv6 __read_mostly = { |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 47 | .family = NFPROTO_IPV6, |
| 48 | .nhooks = NF_INET_NUMHOOKS, |
| 49 | .owner = THIS_MODULE, |
Patrick McHardy | 115a60b | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 50 | .nops = 1, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 51 | .hooks = { |
Patrick McHardy | 3b088c4 | 2014-01-03 12:16:13 +0000 | [diff] [blame] | 52 | [NF_INET_LOCAL_IN] = nft_do_chain_ipv6, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 53 | [NF_INET_LOCAL_OUT] = nft_ipv6_output, |
Patrick McHardy | 3b088c4 | 2014-01-03 12:16:13 +0000 | [diff] [blame] | 54 | [NF_INET_FORWARD] = nft_do_chain_ipv6, |
| 55 | [NF_INET_PRE_ROUTING] = nft_do_chain_ipv6, |
| 56 | [NF_INET_POST_ROUTING] = nft_do_chain_ipv6, |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 57 | }, |
| 58 | }; |
Patrick McHardy | 1d49144 | 2014-01-03 12:16:16 +0000 | [diff] [blame] | 59 | EXPORT_SYMBOL_GPL(nft_af_ipv6); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 60 | |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 61 | static int nf_tables_ipv6_init_net(struct net *net) |
| 62 | { |
| 63 | net->nft.ipv6 = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL); |
| 64 | if (net->nft.ipv6 == NULL) |
| 65 | return -ENOMEM; |
| 66 | |
| 67 | memcpy(net->nft.ipv6, &nft_af_ipv6, sizeof(nft_af_ipv6)); |
| 68 | |
| 69 | if (nft_register_afinfo(net, net->nft.ipv6) < 0) |
| 70 | goto err; |
| 71 | |
| 72 | return 0; |
| 73 | err: |
| 74 | kfree(net->nft.ipv6); |
| 75 | return -ENOMEM; |
| 76 | } |
| 77 | |
| 78 | static void nf_tables_ipv6_exit_net(struct net *net) |
| 79 | { |
Pablo Neira Ayuso | df05ef8 | 2015-12-15 19:39:32 +0100 | [diff] [blame] | 80 | nft_unregister_afinfo(net, net->nft.ipv6); |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 81 | kfree(net->nft.ipv6); |
| 82 | } |
| 83 | |
| 84 | static struct pernet_operations nf_tables_ipv6_net_ops = { |
| 85 | .init = nf_tables_ipv6_init_net, |
| 86 | .exit = nf_tables_ipv6_exit_net, |
| 87 | }; |
| 88 | |
Patrick McHardy | 2a37d75 | 2014-01-09 18:42:37 +0000 | [diff] [blame] | 89 | static const struct nf_chain_type filter_ipv6 = { |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 90 | .name = "filter", |
| 91 | .type = NFT_CHAIN_T_DEFAULT, |
Patrick McHardy | fa2c1de | 2014-01-09 18:42:38 +0000 | [diff] [blame] | 92 | .family = NFPROTO_IPV6, |
| 93 | .owner = THIS_MODULE, |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 94 | .hook_mask = (1 << NF_INET_LOCAL_IN) | |
| 95 | (1 << NF_INET_LOCAL_OUT) | |
| 96 | (1 << NF_INET_FORWARD) | |
| 97 | (1 << NF_INET_PRE_ROUTING) | |
| 98 | (1 << NF_INET_POST_ROUTING), |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 101 | static int __init nf_tables_ipv6_init(void) |
| 102 | { |
Pablo Neira Ayuso | cf4dfa8 | 2014-01-09 20:32:19 +0100 | [diff] [blame] | 103 | int ret; |
| 104 | |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 105 | nft_register_chain_type(&filter_ipv6); |
Pablo Neira Ayuso | cf4dfa8 | 2014-01-09 20:32:19 +0100 | [diff] [blame] | 106 | ret = register_pernet_subsys(&nf_tables_ipv6_net_ops); |
| 107 | if (ret < 0) |
| 108 | nft_unregister_chain_type(&filter_ipv6); |
| 109 | |
| 110 | return ret; |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 111 | } |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 112 | |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 113 | static void __exit nf_tables_ipv6_exit(void) |
| 114 | { |
Pablo Neira Ayuso | 99633ab | 2013-10-10 23:28:33 +0200 | [diff] [blame] | 115 | unregister_pernet_subsys(&nf_tables_ipv6_net_ops); |
Pablo Neira Ayuso | 9370761 | 2013-10-10 23:21:26 +0200 | [diff] [blame] | 116 | nft_unregister_chain_type(&filter_ipv6); |
Patrick McHardy | 9651851 | 2013-10-14 11:00:02 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | module_init(nf_tables_ipv6_init); |
| 120 | module_exit(nf_tables_ipv6_exit); |
| 121 | |
| 122 | MODULE_LICENSE("GPL"); |
| 123 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 124 | MODULE_ALIAS_NFT_FAMILY(AF_INET6); |