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