Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * This is a module which is used for rejecting packets. | |
1da177e4 LT |
3 | */ |
4 | ||
5 | /* (C) 1999-2001 Paul `Rusty' Russell | |
6 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | */ | |
ff67e4e4 | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
1da177e4 LT |
13 | #include <linux/module.h> |
14 | #include <linux/skbuff.h> | |
5a0e3ad6 | 15 | #include <linux/slab.h> |
1da177e4 LT |
16 | #include <linux/ip.h> |
17 | #include <linux/udp.h> | |
18 | #include <linux/icmp.h> | |
19 | #include <net/icmp.h> | |
6709dbbb | 20 | #include <linux/netfilter/x_tables.h> |
1da177e4 LT |
21 | #include <linux/netfilter_ipv4/ip_tables.h> |
22 | #include <linux/netfilter_ipv4/ipt_REJECT.h> | |
1109a90c | 23 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
1da177e4 LT |
24 | #include <linux/netfilter_bridge.h> |
25 | #endif | |
26 | ||
cc70d069 EL |
27 | #include <net/netfilter/ipv4/nf_reject.h> |
28 | ||
1da177e4 LT |
29 | MODULE_LICENSE("GPL"); |
30 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); | |
2ae15b64 | 31 | MODULE_DESCRIPTION("Xtables: packet \"rejection\" target for IPv4"); |
1da177e4 | 32 | |
d3c5ee6d | 33 | static unsigned int |
4b560b44 | 34 | reject_tg(struct sk_buff *skb, const struct xt_action_param *par) |
1da177e4 | 35 | { |
7eb35586 | 36 | const struct ipt_reject_info *reject = par->targinfo; |
1da177e4 | 37 | |
e905a9ed YH |
38 | switch (reject->with) { |
39 | case IPT_ICMP_NET_UNREACHABLE: | |
cc70d069 | 40 | nf_send_unreach(skb, ICMP_NET_UNREACH); |
e905a9ed YH |
41 | break; |
42 | case IPT_ICMP_HOST_UNREACHABLE: | |
cc70d069 | 43 | nf_send_unreach(skb, ICMP_HOST_UNREACH); |
e905a9ed YH |
44 | break; |
45 | case IPT_ICMP_PROT_UNREACHABLE: | |
cc70d069 | 46 | nf_send_unreach(skb, ICMP_PROT_UNREACH); |
e905a9ed YH |
47 | break; |
48 | case IPT_ICMP_PORT_UNREACHABLE: | |
cc70d069 | 49 | nf_send_unreach(skb, ICMP_PORT_UNREACH); |
e905a9ed YH |
50 | break; |
51 | case IPT_ICMP_NET_PROHIBITED: | |
cc70d069 | 52 | nf_send_unreach(skb, ICMP_NET_ANO); |
e905a9ed | 53 | break; |
1da177e4 | 54 | case IPT_ICMP_HOST_PROHIBITED: |
cc70d069 | 55 | nf_send_unreach(skb, ICMP_HOST_ANO); |
e905a9ed YH |
56 | break; |
57 | case IPT_ICMP_ADMIN_PROHIBITED: | |
cc70d069 | 58 | nf_send_unreach(skb, ICMP_PKT_FILTERED); |
1da177e4 LT |
59 | break; |
60 | case IPT_TCP_RESET: | |
cc70d069 | 61 | nf_send_reset(skb, par->hooknum); |
1da177e4 LT |
62 | case IPT_ICMP_ECHOREPLY: |
63 | /* Doesn't happen. */ | |
64 | break; | |
65 | } | |
66 | ||
67 | return NF_DROP; | |
68 | } | |
69 | ||
135367b8 | 70 | static int reject_tg_check(const struct xt_tgchk_param *par) |
1da177e4 | 71 | { |
af5d6dc2 JE |
72 | const struct ipt_reject_info *rejinfo = par->targinfo; |
73 | const struct ipt_entry *e = par->entryinfo; | |
1da177e4 | 74 | |
1da177e4 | 75 | if (rejinfo->with == IPT_ICMP_ECHOREPLY) { |
ff67e4e4 | 76 | pr_info("ECHOREPLY no longer supported.\n"); |
d6b00a53 | 77 | return -EINVAL; |
1da177e4 LT |
78 | } else if (rejinfo->with == IPT_TCP_RESET) { |
79 | /* Must specify that it's a TCP packet */ | |
3666ed1c JP |
80 | if (e->ip.proto != IPPROTO_TCP || |
81 | (e->ip.invflags & XT_INV_PROTO)) { | |
ff67e4e4 | 82 | pr_info("TCP_RESET invalid for non-tcp\n"); |
d6b00a53 | 83 | return -EINVAL; |
1da177e4 LT |
84 | } |
85 | } | |
d6b00a53 | 86 | return 0; |
1da177e4 LT |
87 | } |
88 | ||
d3c5ee6d | 89 | static struct xt_target reject_tg_reg __read_mostly = { |
1da177e4 | 90 | .name = "REJECT", |
ee999d8b | 91 | .family = NFPROTO_IPV4, |
d3c5ee6d | 92 | .target = reject_tg, |
1d5cd909 PM |
93 | .targetsize = sizeof(struct ipt_reject_info), |
94 | .table = "filter", | |
6e23ae2a PM |
95 | .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) | |
96 | (1 << NF_INET_LOCAL_OUT), | |
d3c5ee6d | 97 | .checkentry = reject_tg_check, |
1da177e4 LT |
98 | .me = THIS_MODULE, |
99 | }; | |
100 | ||
d3c5ee6d | 101 | static int __init reject_tg_init(void) |
1da177e4 | 102 | { |
d3c5ee6d | 103 | return xt_register_target(&reject_tg_reg); |
1da177e4 LT |
104 | } |
105 | ||
d3c5ee6d | 106 | static void __exit reject_tg_exit(void) |
1da177e4 | 107 | { |
d3c5ee6d | 108 | xt_unregister_target(&reject_tg_reg); |
1da177e4 LT |
109 | } |
110 | ||
d3c5ee6d JE |
111 | module_init(reject_tg_init); |
112 | module_exit(reject_tg_exit); |