Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IPv6 raw table, a port of the IPv4 raw table to IPv6 |
| 3 | * |
| 4 | * Copyright (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> |
| 5 | */ |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/netfilter_ipv6/ip6_tables.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 10 | #define RAW_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 12 | static int __net_init ip6table_raw_table_init(struct net *net); |
| 13 | |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 14 | static const struct xt_table packet_raw = { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 15 | .name = "raw", |
| 16 | .valid_hooks = RAW_VALID_HOOKS, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 17 | .me = THIS_MODULE, |
Jan Engelhardt | f88e6a8 | 2009-06-13 06:25:44 +0200 | [diff] [blame] | 18 | .af = NFPROTO_IPV6, |
Jozsef Kadlecsik | 9c13886 | 2010-03-25 11:17:26 +0100 | [diff] [blame] | 19 | .priority = NF_IP6_PRI_RAW, |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 20 | .table_init = ip6table_raw_table_init, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | /* The work comes in here from netfilter.c. */ |
| 24 | static unsigned int |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 25 | ip6table_raw_hook(void *priv, struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 26 | const struct nf_hook_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
Eric W. Biederman | 6cb8ff3f1 | 2015-09-18 14:32:55 -0500 | [diff] [blame] | 28 | return ip6t_do_table(skb, state, state->net->ipv6.ip6table_raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 31 | static struct nf_hook_ops *rawtable_ops __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 33 | static int __net_init ip6table_raw_table_init(struct net *net) |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 34 | { |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 35 | struct ip6t_replace *repl; |
Florian Westphal | a67dd26 | 2016-02-25 10:08:35 +0100 | [diff] [blame] | 36 | int ret; |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 37 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 38 | if (net->ipv6.ip6table_raw) |
| 39 | return 0; |
| 40 | |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 41 | repl = ip6t_alloc_initial_table(&packet_raw); |
| 42 | if (repl == NULL) |
| 43 | return -ENOMEM; |
Florian Westphal | a67dd26 | 2016-02-25 10:08:35 +0100 | [diff] [blame] | 44 | ret = ip6t_register_table(net, &packet_raw, repl, rawtable_ops, |
| 45 | &net->ipv6.ip6table_raw); |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 46 | kfree(repl); |
Florian Westphal | a67dd26 | 2016-02-25 10:08:35 +0100 | [diff] [blame] | 47 | return ret; |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static void __net_exit ip6table_raw_net_exit(struct net *net) |
| 51 | { |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 52 | if (!net->ipv6.ip6table_raw) |
| 53 | return; |
Florian Westphal | a67dd26 | 2016-02-25 10:08:35 +0100 | [diff] [blame] | 54 | ip6t_unregister_table(net, net->ipv6.ip6table_raw, rawtable_ops); |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 55 | net->ipv6.ip6table_raw = NULL; |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static struct pernet_operations ip6table_raw_net_ops = { |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 59 | .exit = ip6table_raw_net_exit, |
| 60 | }; |
| 61 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 62 | static int __init ip6table_raw_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
| 64 | int ret; |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | /* Register hooks */ |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 67 | rawtable_ops = xt_hook_ops_alloc(&packet_raw, ip6table_raw_hook); |
| 68 | if (IS_ERR(rawtable_ops)) |
| 69 | return PTR_ERR(rawtable_ops); |
| 70 | |
| 71 | ret = register_pernet_subsys(&ip6table_raw_net_ops); |
| 72 | if (ret < 0) { |
| 73 | kfree(rawtable_ops); |
| 74 | return ret; |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 75 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 77 | ret = ip6table_raw_table_init(&init_net); |
| 78 | if (ret) { |
| 79 | unregister_pernet_subsys(&ip6table_raw_net_ops); |
| 80 | kfree(rawtable_ops); |
| 81 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | return ret; |
| 83 | } |
| 84 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 85 | static void __exit ip6table_raw_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 87 | unregister_pernet_subsys(&ip6table_raw_net_ops); |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 88 | kfree(rawtable_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 91 | module_init(ip6table_raw_init); |
| 92 | module_exit(ip6table_raw_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | MODULE_LICENSE("GPL"); |