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 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/moduleparam.h> |
| 15 | #include <linux/netfilter_ipv4/ip_tables.h> |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 16 | #include <net/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | MODULE_LICENSE("GPL"); |
| 19 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); |
| 20 | MODULE_DESCRIPTION("iptables filter table"); |
| 21 | |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 22 | #define FILTER_VALID_HOOKS ((1 << NF_INET_LOCAL_IN) | \ |
| 23 | (1 << NF_INET_FORWARD) | \ |
| 24 | (1 << NF_INET_LOCAL_OUT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | static struct |
| 27 | { |
| 28 | struct ipt_replace repl; |
| 29 | struct ipt_standard entries[3]; |
| 30 | struct ipt_error term; |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 31 | } initial_table __initdata = { |
| 32 | .repl = { |
| 33 | .name = "filter", |
| 34 | .valid_hooks = FILTER_VALID_HOOKS, |
| 35 | .num_entries = 4, |
| 36 | .size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error), |
| 37 | .hook_entry = { |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 38 | [NF_INET_LOCAL_IN] = 0, |
| 39 | [NF_INET_FORWARD] = sizeof(struct ipt_standard), |
| 40 | [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2, |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 41 | }, |
| 42 | .underflow = { |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 43 | [NF_INET_LOCAL_IN] = 0, |
| 44 | [NF_INET_FORWARD] = sizeof(struct ipt_standard), |
| 45 | [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2, |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 46 | }, |
| 47 | }, |
| 48 | .entries = { |
| 49 | IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_IN */ |
| 50 | IPT_STANDARD_INIT(NF_ACCEPT), /* FORWARD */ |
| 51 | IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */ |
| 52 | }, |
| 53 | .term = IPT_ERROR_INIT, /* ERROR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
Jan Engelhardt | e60a13e | 2007-02-07 15:12:33 -0800 | [diff] [blame] | 56 | static struct xt_table packet_filter = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | .name = "filter", |
| 58 | .valid_hooks = FILTER_VALID_HOOKS, |
| 59 | .lock = RW_LOCK_UNLOCKED, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 60 | .me = THIS_MODULE, |
| 61 | .af = AF_INET, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | /* The work comes in here from netfilter.c. */ |
| 65 | static unsigned int |
| 66 | ipt_hook(unsigned int hook, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 67 | struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | const struct net_device *in, |
| 69 | const struct net_device *out, |
| 70 | int (*okfn)(struct sk_buff *)) |
| 71 | { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 72 | return ipt_do_table(skb, hook, in, out, &packet_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static unsigned int |
| 76 | ipt_local_out_hook(unsigned int hook, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 77 | struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | const struct net_device *in, |
| 79 | const struct net_device *out, |
| 80 | int (*okfn)(struct sk_buff *)) |
| 81 | { |
| 82 | /* root is playing with raw sockets. */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 83 | if (skb->len < sizeof(struct iphdr) || |
| 84 | ip_hdrlen(skb) < sizeof(struct iphdr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | if (net_ratelimit()) |
Patrick McHardy | 4a176c1 | 2007-05-10 14:16:56 -0700 | [diff] [blame] | 86 | printk("iptable_filter: ignoring short SOCK_RAW " |
| 87 | "packet.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | return NF_ACCEPT; |
| 89 | } |
| 90 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 91 | return ipt_do_table(skb, hook, in, out, &packet_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static struct nf_hook_ops ipt_ops[] = { |
| 95 | { |
| 96 | .hook = ipt_hook, |
| 97 | .owner = THIS_MODULE, |
| 98 | .pf = PF_INET, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 99 | .hooknum = NF_INET_LOCAL_IN, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | .priority = NF_IP_PRI_FILTER, |
| 101 | }, |
| 102 | { |
| 103 | .hook = ipt_hook, |
| 104 | .owner = THIS_MODULE, |
| 105 | .pf = PF_INET, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 106 | .hooknum = NF_INET_FORWARD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | .priority = NF_IP_PRI_FILTER, |
| 108 | }, |
| 109 | { |
| 110 | .hook = ipt_local_out_hook, |
| 111 | .owner = THIS_MODULE, |
| 112 | .pf = PF_INET, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 113 | .hooknum = NF_INET_LOCAL_OUT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | .priority = NF_IP_PRI_FILTER, |
| 115 | }, |
| 116 | }; |
| 117 | |
| 118 | /* Default to forward because I got too much mail already. */ |
| 119 | static int forward = NF_ACCEPT; |
| 120 | module_param(forward, bool, 0000); |
| 121 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 122 | static int __init iptable_filter_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
| 124 | int ret; |
| 125 | |
| 126 | if (forward < 0 || forward > NF_MAX_VERDICT) { |
| 127 | printk("iptables forward must be 0 or 1\n"); |
| 128 | return -EINVAL; |
| 129 | } |
| 130 | |
| 131 | /* Entry 1 is the FORWARD hook */ |
| 132 | initial_table.entries[1].target.verdict = -forward - 1; |
| 133 | |
| 134 | /* Register table */ |
| 135 | ret = ipt_register_table(&packet_filter, &initial_table.repl); |
| 136 | if (ret < 0) |
| 137 | return ret; |
| 138 | |
| 139 | /* Register hooks */ |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 140 | ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | if (ret < 0) |
| 142 | goto cleanup_table; |
| 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | return ret; |
| 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | cleanup_table: |
| 147 | ipt_unregister_table(&packet_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | return ret; |
| 149 | } |
| 150 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 151 | static void __exit iptable_filter_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 153 | nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | ipt_unregister_table(&packet_filter); |
| 155 | } |
| 156 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 157 | module_init(iptable_filter_init); |
| 158 | module_exit(iptable_filter_fini); |