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> | |
20 | #include <net/ip.h> | |
21 | #include <net/tcp.h> | |
22 | #include <net/route.h> | |
23 | #include <net/dst.h> | |
6709dbbb | 24 | #include <linux/netfilter/x_tables.h> |
1da177e4 LT |
25 | #include <linux/netfilter_ipv4/ip_tables.h> |
26 | #include <linux/netfilter_ipv4/ipt_REJECT.h> | |
27 | #ifdef CONFIG_BRIDGE_NETFILTER | |
28 | #include <linux/netfilter_bridge.h> | |
29 | #endif | |
30 | ||
31 | MODULE_LICENSE("GPL"); | |
32 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); | |
2ae15b64 | 33 | MODULE_DESCRIPTION("Xtables: packet \"rejection\" target for IPv4"); |
1da177e4 | 34 | |
1da177e4 LT |
35 | /* Send RST reply */ |
36 | static void send_reset(struct sk_buff *oldskb, int hook) | |
37 | { | |
38 | struct sk_buff *nskb; | |
3cf93c96 JE |
39 | const struct iphdr *oiph; |
40 | struct iphdr *niph; | |
41 | const struct tcphdr *oth; | |
42 | struct tcphdr _otcph, *tcph; | |
9d02002d | 43 | unsigned int addr_type; |
1da177e4 LT |
44 | |
45 | /* IP header checks: fragment. */ | |
eddc9ec5 | 46 | if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET)) |
1da177e4 LT |
47 | return; |
48 | ||
c9bdd4b5 | 49 | oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb), |
1da177e4 LT |
50 | sizeof(_otcph), &_otcph); |
51 | if (oth == NULL) | |
e905a9ed | 52 | return; |
1da177e4 LT |
53 | |
54 | /* No RST for RST. */ | |
55 | if (oth->rst) | |
56 | return; | |
57 | ||
6150bacf | 58 | /* Check checksum */ |
c9bdd4b5 | 59 | if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP)) |
6150bacf | 60 | return; |
9ba99b0d | 61 | oiph = ip_hdr(oldskb); |
6150bacf | 62 | |
9ba99b0d DV |
63 | nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) + |
64 | LL_MAX_HEADER, GFP_ATOMIC); | |
9d02002d | 65 | if (!nskb) |
1da177e4 | 66 | return; |
1da177e4 | 67 | |
9ba99b0d DV |
68 | skb_reserve(nskb, LL_MAX_HEADER); |
69 | ||
70 | skb_reset_network_header(nskb); | |
71 | niph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr)); | |
72 | niph->version = 4; | |
73 | niph->ihl = sizeof(struct iphdr) / 4; | |
74 | niph->tos = 0; | |
75 | niph->id = 0; | |
76 | niph->frag_off = htons(IP_DF); | |
77 | niph->protocol = IPPROTO_TCP; | |
78 | niph->check = 0; | |
79 | niph->saddr = oiph->daddr; | |
80 | niph->daddr = oiph->saddr; | |
81 | ||
82 | tcph = (struct tcphdr *)skb_put(nskb, sizeof(struct tcphdr)); | |
83 | memset(tcph, 0, sizeof(*tcph)); | |
84 | tcph->source = oth->dest; | |
85 | tcph->dest = oth->source; | |
86 | tcph->doff = sizeof(struct tcphdr) / 4; | |
87 | ||
88 | if (oth->ack) | |
1da177e4 | 89 | tcph->seq = oth->ack_seq; |
9ba99b0d | 90 | else { |
c9bdd4b5 ACM |
91 | tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin + |
92 | oldskb->len - ip_hdrlen(oldskb) - | |
93 | (oth->doff << 2)); | |
9ba99b0d | 94 | tcph->ack = 1; |
1da177e4 LT |
95 | } |
96 | ||
9ba99b0d DV |
97 | tcph->rst = 1; |
98 | tcph->check = tcp_v4_check(sizeof(struct tcphdr), | |
99 | niph->saddr, niph->daddr, | |
100 | csum_partial(tcph, | |
101 | sizeof(struct tcphdr), 0)); | |
9d02002d PM |
102 | |
103 | addr_type = RTN_UNSPEC; | |
6e23ae2a | 104 | if (hook != NF_INET_FORWARD |
9d02002d PM |
105 | #ifdef CONFIG_BRIDGE_NETFILTER |
106 | || (nskb->nf_bridge && nskb->nf_bridge->mask & BRNF_BRIDGED) | |
107 | #endif | |
108 | ) | |
109 | addr_type = RTN_LOCAL; | |
110 | ||
9ba99b0d | 111 | /* ip_route_me_harder expects skb->dst to be set */ |
adf30907 | 112 | skb_dst_set(nskb, dst_clone(skb_dst(oldskb))); |
9ba99b0d | 113 | |
3db05fea | 114 | if (ip_route_me_harder(nskb, addr_type)) |
9d02002d PM |
115 | goto free_nskb; |
116 | ||
adf30907 | 117 | niph->ttl = dst_metric(skb_dst(nskb), RTAX_HOPLIMIT); |
4cf411de | 118 | nskb->ip_summed = CHECKSUM_NONE; |
af443b6d | 119 | |
1da177e4 | 120 | /* "Never happens" */ |
adf30907 | 121 | if (nskb->len > dst_mtu(skb_dst(nskb))) |
1da177e4 LT |
122 | goto free_nskb; |
123 | ||
124 | nf_ct_attach(nskb, oldskb); | |
125 | ||
c439cb2e | 126 | ip_local_out(nskb); |
1da177e4 LT |
127 | return; |
128 | ||
129 | free_nskb: | |
130 | kfree_skb(nskb); | |
131 | } | |
132 | ||
133 | static inline void send_unreach(struct sk_buff *skb_in, int code) | |
134 | { | |
135 | icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0); | |
e905a9ed | 136 | } |
1da177e4 | 137 | |
d3c5ee6d | 138 | static unsigned int |
7eb35586 | 139 | reject_tg(struct sk_buff *skb, const struct xt_target_param *par) |
1da177e4 | 140 | { |
7eb35586 | 141 | const struct ipt_reject_info *reject = par->targinfo; |
1da177e4 | 142 | |
e905a9ed YH |
143 | switch (reject->with) { |
144 | case IPT_ICMP_NET_UNREACHABLE: | |
3db05fea | 145 | send_unreach(skb, ICMP_NET_UNREACH); |
e905a9ed YH |
146 | break; |
147 | case IPT_ICMP_HOST_UNREACHABLE: | |
3db05fea | 148 | send_unreach(skb, ICMP_HOST_UNREACH); |
e905a9ed YH |
149 | break; |
150 | case IPT_ICMP_PROT_UNREACHABLE: | |
3db05fea | 151 | send_unreach(skb, ICMP_PROT_UNREACH); |
e905a9ed YH |
152 | break; |
153 | case IPT_ICMP_PORT_UNREACHABLE: | |
3db05fea | 154 | send_unreach(skb, ICMP_PORT_UNREACH); |
e905a9ed YH |
155 | break; |
156 | case IPT_ICMP_NET_PROHIBITED: | |
3db05fea | 157 | send_unreach(skb, ICMP_NET_ANO); |
e905a9ed | 158 | break; |
1da177e4 | 159 | case IPT_ICMP_HOST_PROHIBITED: |
3db05fea | 160 | send_unreach(skb, ICMP_HOST_ANO); |
e905a9ed YH |
161 | break; |
162 | case IPT_ICMP_ADMIN_PROHIBITED: | |
3db05fea | 163 | send_unreach(skb, ICMP_PKT_FILTERED); |
1da177e4 LT |
164 | break; |
165 | case IPT_TCP_RESET: | |
7eb35586 | 166 | send_reset(skb, par->hooknum); |
1da177e4 LT |
167 | case IPT_ICMP_ECHOREPLY: |
168 | /* Doesn't happen. */ | |
169 | break; | |
170 | } | |
171 | ||
172 | return NF_DROP; | |
173 | } | |
174 | ||
135367b8 | 175 | static int reject_tg_check(const struct xt_tgchk_param *par) |
1da177e4 | 176 | { |
af5d6dc2 JE |
177 | const struct ipt_reject_info *rejinfo = par->targinfo; |
178 | const struct ipt_entry *e = par->entryinfo; | |
1da177e4 | 179 | |
1da177e4 | 180 | if (rejinfo->with == IPT_ICMP_ECHOREPLY) { |
ff67e4e4 | 181 | pr_info("ECHOREPLY no longer supported.\n"); |
d6b00a53 | 182 | return -EINVAL; |
1da177e4 LT |
183 | } else if (rejinfo->with == IPT_TCP_RESET) { |
184 | /* Must specify that it's a TCP packet */ | |
3666ed1c JP |
185 | if (e->ip.proto != IPPROTO_TCP || |
186 | (e->ip.invflags & XT_INV_PROTO)) { | |
ff67e4e4 | 187 | pr_info("TCP_RESET invalid for non-tcp\n"); |
d6b00a53 | 188 | return -EINVAL; |
1da177e4 LT |
189 | } |
190 | } | |
d6b00a53 | 191 | return 0; |
1da177e4 LT |
192 | } |
193 | ||
d3c5ee6d | 194 | static struct xt_target reject_tg_reg __read_mostly = { |
1da177e4 | 195 | .name = "REJECT", |
ee999d8b | 196 | .family = NFPROTO_IPV4, |
d3c5ee6d | 197 | .target = reject_tg, |
1d5cd909 PM |
198 | .targetsize = sizeof(struct ipt_reject_info), |
199 | .table = "filter", | |
6e23ae2a PM |
200 | .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) | |
201 | (1 << NF_INET_LOCAL_OUT), | |
d3c5ee6d | 202 | .checkentry = reject_tg_check, |
1da177e4 LT |
203 | .me = THIS_MODULE, |
204 | }; | |
205 | ||
d3c5ee6d | 206 | static int __init reject_tg_init(void) |
1da177e4 | 207 | { |
d3c5ee6d | 208 | return xt_register_target(&reject_tg_reg); |
1da177e4 LT |
209 | } |
210 | ||
d3c5ee6d | 211 | static void __exit reject_tg_exit(void) |
1da177e4 | 212 | { |
d3c5ee6d | 213 | xt_unregister_target(&reject_tg_reg); |
1da177e4 LT |
214 | } |
215 | ||
d3c5ee6d JE |
216 | module_init(reject_tg_init); |
217 | module_exit(reject_tg_exit); |