Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x. |
| 3 | * |
| 4 | * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling |
| 5 | * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/moduleparam.h> |
| 14 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 15 | |
| 16 | MODULE_LICENSE("GPL"); |
| 17 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); |
| 18 | MODULE_DESCRIPTION("ip6tables filter table"); |
| 19 | |
| 20 | #define FILTER_VALID_HOOKS ((1 << NF_IP6_LOCAL_IN) | (1 << NF_IP6_FORWARD) | (1 << NF_IP6_LOCAL_OUT)) |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | static struct |
| 23 | { |
| 24 | struct ip6t_replace repl; |
| 25 | struct ip6t_standard entries[3]; |
| 26 | struct ip6t_error term; |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 27 | } initial_table __initdata = { |
| 28 | .repl = { |
| 29 | .name = "filter", |
| 30 | .valid_hooks = FILTER_VALID_HOOKS, |
| 31 | .num_entries = 4, |
| 32 | .size = sizeof(struct ip6t_standard) * 3 + sizeof(struct ip6t_error), |
| 33 | .hook_entry = { |
| 34 | [NF_IP6_LOCAL_IN] = 0, |
| 35 | [NF_IP6_FORWARD] = sizeof(struct ip6t_standard), |
| 36 | [NF_IP6_LOCAL_OUT] = sizeof(struct ip6t_standard) * 2 |
| 37 | }, |
| 38 | .underflow = { |
| 39 | [NF_IP6_LOCAL_IN] = 0, |
| 40 | [NF_IP6_FORWARD] = sizeof(struct ip6t_standard), |
| 41 | [NF_IP6_LOCAL_OUT] = sizeof(struct ip6t_standard) * 2 |
| 42 | }, |
| 43 | }, |
| 44 | .entries = { |
| 45 | IP6T_STANDARD_INIT(NF_ACCEPT), /* LOCAL_IN */ |
| 46 | IP6T_STANDARD_INIT(NF_ACCEPT), /* FORWARD */ |
| 47 | IP6T_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */ |
| 48 | }, |
| 49 | .term = IP6T_ERROR_INIT, /* ERROR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Jan Engelhardt | e60a13e | 2007-02-07 15:12:33 -0800 | [diff] [blame] | 52 | static struct xt_table packet_filter = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | .name = "filter", |
| 54 | .valid_hooks = FILTER_VALID_HOOKS, |
| 55 | .lock = RW_LOCK_UNLOCKED, |
| 56 | .me = THIS_MODULE, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 57 | .af = AF_INET6, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | /* The work comes in here from netfilter.c. */ |
| 61 | static unsigned int |
| 62 | ip6t_hook(unsigned int hook, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame^] | 63 | struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | const struct net_device *in, |
| 65 | const struct net_device *out, |
| 66 | int (*okfn)(struct sk_buff *)) |
| 67 | { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame^] | 68 | return ip6t_do_table(skb, hook, in, out, &packet_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static unsigned int |
| 72 | ip6t_local_out_hook(unsigned int hook, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame^] | 73 | struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | const struct net_device *in, |
| 75 | const struct net_device *out, |
| 76 | int (*okfn)(struct sk_buff *)) |
| 77 | { |
| 78 | #if 0 |
| 79 | /* root is playing with raw sockets. */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame^] | 80 | if (skb->len < sizeof(struct iphdr) |
| 81 | || ip_hdrlen(skb) < sizeof(struct iphdr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | if (net_ratelimit()) |
| 83 | printk("ip6t_hook: happy cracking.\n"); |
| 84 | return NF_ACCEPT; |
| 85 | } |
| 86 | #endif |
| 87 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame^] | 88 | return ip6t_do_table(skb, hook, in, out, &packet_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static struct nf_hook_ops ip6t_ops[] = { |
| 92 | { |
| 93 | .hook = ip6t_hook, |
| 94 | .owner = THIS_MODULE, |
| 95 | .pf = PF_INET6, |
| 96 | .hooknum = NF_IP6_LOCAL_IN, |
| 97 | .priority = NF_IP6_PRI_FILTER, |
| 98 | }, |
| 99 | { |
| 100 | .hook = ip6t_hook, |
| 101 | .owner = THIS_MODULE, |
| 102 | .pf = PF_INET6, |
| 103 | .hooknum = NF_IP6_FORWARD, |
| 104 | .priority = NF_IP6_PRI_FILTER, |
| 105 | }, |
| 106 | { |
| 107 | .hook = ip6t_local_out_hook, |
| 108 | .owner = THIS_MODULE, |
| 109 | .pf = PF_INET6, |
| 110 | .hooknum = NF_IP6_LOCAL_OUT, |
| 111 | .priority = NF_IP6_PRI_FILTER, |
| 112 | }, |
| 113 | }; |
| 114 | |
| 115 | /* Default to forward because I got too much mail already. */ |
| 116 | static int forward = NF_ACCEPT; |
| 117 | module_param(forward, bool, 0000); |
| 118 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 119 | static int __init ip6table_filter_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
| 121 | int ret; |
| 122 | |
| 123 | if (forward < 0 || forward > NF_MAX_VERDICT) { |
| 124 | printk("iptables forward must be 0 or 1\n"); |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | |
| 128 | /* Entry 1 is the FORWARD hook */ |
| 129 | initial_table.entries[1].target.verdict = -forward - 1; |
| 130 | |
| 131 | /* Register table */ |
| 132 | ret = ip6t_register_table(&packet_filter, &initial_table.repl); |
| 133 | if (ret < 0) |
| 134 | return ret; |
| 135 | |
| 136 | /* Register hooks */ |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 137 | ret = nf_register_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | if (ret < 0) |
| 139 | goto cleanup_table; |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | return ret; |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | cleanup_table: |
| 144 | ip6t_unregister_table(&packet_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return ret; |
| 146 | } |
| 147 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 148 | static void __exit ip6table_filter_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 150 | nf_unregister_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | ip6t_unregister_table(&packet_filter); |
| 152 | } |
| 153 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 154 | module_init(ip6table_filter_init); |
| 155 | module_exit(ip6table_filter_fini); |