blob: dcf4d21d51161fa99625168c9990379cd988e01f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This is a module which is used for rejecting packets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 */
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 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/skbuff.h>
15#include <linux/ip.h>
16#include <linux/udp.h>
17#include <linux/icmp.h>
18#include <net/icmp.h>
19#include <net/ip.h>
20#include <net/tcp.h>
21#include <net/route.h>
22#include <net/dst.h>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080023#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/netfilter_ipv4/ip_tables.h>
25#include <linux/netfilter_ipv4/ipt_REJECT.h>
26#ifdef CONFIG_BRIDGE_NETFILTER
27#include <linux/netfilter_bridge.h>
28#endif
29
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
32MODULE_DESCRIPTION("iptables REJECT target module");
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* Send RST reply */
35static void send_reset(struct sk_buff *oldskb, int hook)
36{
37 struct sk_buff *nskb;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070038 struct iphdr *niph;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct tcphdr _otcph, *oth, *tcph;
Al Viro6a19d612006-09-28 14:22:24 -070040 __be16 tmp_port;
41 __be32 tmp_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 int needs_ack;
Patrick McHardy9d020022006-10-02 16:12:20 -070043 unsigned int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 /* IP header checks: fragment. */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070046 if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return;
48
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030049 oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 sizeof(_otcph), &_otcph);
51 if (oth == NULL)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090052 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 /* No RST for RST. */
55 if (oth->rst)
56 return;
57
Patrick McHardy6150bac2005-06-21 14:03:46 -070058 /* Check checksum */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030059 if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
Patrick McHardy6150bac2005-06-21 14:03:46 -070060 return;
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 /* We need a linear, writeable skb. We also need to expand
63 headroom in case hh_len of incoming interface < hh_len of
64 outgoing interface */
Patrick McHardy9d020022006-10-02 16:12:20 -070065 nskb = skb_copy_expand(oldskb, LL_MAX_HEADER, skb_tailroom(oldskb),
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 GFP_ATOMIC);
Patrick McHardy9d020022006-10-02 16:12:20 -070067 if (!nskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 /* This packet will not be the same as the other: clear nf fields */
71 nf_reset(nskb);
Thomas Graf82e91ff2006-11-09 15:19:14 -080072 nskb->mark = 0;
James Morris984bc162006-06-09 00:29:17 -070073 skb_init_secmark(nskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Herbert Xubbf4a6b2007-02-13 12:32:58 -080075 skb_shinfo(nskb)->gso_size = 0;
76 skb_shinfo(nskb)->gso_segs = 0;
77 skb_shinfo(nskb)->gso_type = 0;
78
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030079 tcph = (struct tcphdr *)(skb_network_header(nskb) + ip_hdrlen(nskb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 /* Swap source and dest */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070082 niph = ip_hdr(nskb);
83 tmp_addr = niph->saddr;
84 niph->saddr = niph->daddr;
85 niph->daddr = tmp_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 tmp_port = tcph->source;
87 tcph->source = tcph->dest;
88 tcph->dest = tmp_port;
89
90 /* Truncate to length (no data) */
91 tcph->doff = sizeof(struct tcphdr)/4;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030092 skb_trim(nskb, ip_hdrlen(nskb) + sizeof(struct tcphdr));
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070093 niph->tot_len = htons(nskb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 if (tcph->ack) {
96 needs_ack = 0;
97 tcph->seq = oth->ack_seq;
98 tcph->ack_seq = 0;
99 } else {
100 needs_ack = 1;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300101 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
102 oldskb->len - ip_hdrlen(oldskb) -
103 (oth->doff << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 tcph->seq = 0;
105 }
106
107 /* Reset flags */
108 ((u_int8_t *)tcph)[13] = 0;
109 tcph->rst = 1;
110 tcph->ack = needs_ack;
111
112 tcph->window = 0;
113 tcph->urg_ptr = 0;
114
Patrick McHardyaf443b62006-11-28 20:10:21 -0800115 /* Adjust TCP checksum */
116 tcph->check = 0;
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800117 tcph->check = tcp_v4_check(sizeof(struct tcphdr),
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700118 niph->saddr, niph->daddr,
Jan Engelhardta47362a2007-07-07 22:16:55 -0700119 csum_partial(tcph,
Patrick McHardyaf443b62006-11-28 20:10:21 -0800120 sizeof(struct tcphdr), 0));
121
Patrick McHardy9d020022006-10-02 16:12:20 -0700122 /* Set DF, id = 0 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700123 niph->frag_off = htons(IP_DF);
124 niph->id = 0;
Patrick McHardy9d020022006-10-02 16:12:20 -0700125
126 addr_type = RTN_UNSPEC;
127 if (hook != NF_IP_FORWARD
128#ifdef CONFIG_BRIDGE_NETFILTER
129 || (nskb->nf_bridge && nskb->nf_bridge->mask & BRNF_BRIDGED)
130#endif
131 )
132 addr_type = RTN_LOCAL;
133
Herbert Xu3db05fe2007-10-15 00:53:15 -0700134 if (ip_route_me_harder(nskb, addr_type))
Patrick McHardy9d020022006-10-02 16:12:20 -0700135 goto free_nskb;
136
Patrick McHardy4cf411d2006-08-05 00:58:33 -0700137 nskb->ip_summed = CHECKSUM_NONE;
Patrick McHardyaf443b62006-11-28 20:10:21 -0800138
Patrick McHardy9d020022006-10-02 16:12:20 -0700139 /* Adjust IP TTL */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700140 niph->ttl = dst_metric(nskb->dst, RTAX_HOPLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 /* Adjust IP checksum */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700143 niph->check = 0;
144 niph->check = ip_fast_csum(skb_network_header(nskb), niph->ihl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 /* "Never happens" */
147 if (nskb->len > dst_mtu(nskb->dst))
148 goto free_nskb;
149
150 nf_ct_attach(nskb, oldskb);
151
152 NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, nskb, NULL, nskb->dst->dev,
153 dst_output);
154 return;
155
156 free_nskb:
157 kfree_skb(nskb);
158}
159
160static inline void send_unreach(struct sk_buff *skb_in, int code)
161{
162 icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900163}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Herbert Xu3db05fe2007-10-15 00:53:15 -0700165static unsigned int reject(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 const struct net_device *in,
167 const struct net_device *out,
168 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -0800169 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700170 const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 const struct ipt_reject_info *reject = targinfo;
173
174 /* Our naive response construction doesn't deal with IP
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900175 options, and probably shouldn't try. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700176 if (ip_hdrlen(skb) != sizeof(struct iphdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return NF_DROP;
178
179 /* WARNING: This code causes reentry within iptables.
180 This means that the iptables jump stack is now crap. We
181 must return an absolute verdict. --RR */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900182 switch (reject->with) {
183 case IPT_ICMP_NET_UNREACHABLE:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700184 send_unreach(skb, ICMP_NET_UNREACH);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900185 break;
186 case IPT_ICMP_HOST_UNREACHABLE:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700187 send_unreach(skb, ICMP_HOST_UNREACH);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900188 break;
189 case IPT_ICMP_PROT_UNREACHABLE:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700190 send_unreach(skb, ICMP_PROT_UNREACH);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900191 break;
192 case IPT_ICMP_PORT_UNREACHABLE:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700193 send_unreach(skb, ICMP_PORT_UNREACH);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900194 break;
195 case IPT_ICMP_NET_PROHIBITED:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700196 send_unreach(skb, ICMP_NET_ANO);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900197 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 case IPT_ICMP_HOST_PROHIBITED:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700199 send_unreach(skb, ICMP_HOST_ANO);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900200 break;
201 case IPT_ICMP_ADMIN_PROHIBITED:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700202 send_unreach(skb, ICMP_PKT_FILTERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 break;
204 case IPT_TCP_RESET:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700205 send_reset(skb, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 case IPT_ICMP_ECHOREPLY:
207 /* Doesn't happen. */
208 break;
209 }
210
211 return NF_DROP;
212}
213
Jan Engelhardte1931b72007-07-07 22:16:26 -0700214static bool check(const char *tablename,
215 const void *e_void,
216 const struct xt_target *target,
217 void *targinfo,
218 unsigned int hook_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900220 const struct ipt_reject_info *rejinfo = targinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800221 const struct ipt_entry *e = e_void;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (rejinfo->with == IPT_ICMP_ECHOREPLY) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700224 printk("ipt_REJECT: ECHOREPLY no longer supported.\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700225 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 } else if (rejinfo->with == IPT_TCP_RESET) {
227 /* Must specify that it's a TCP packet */
228 if (e->ip.proto != IPPROTO_TCP
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800229 || (e->ip.invflags & XT_INV_PROTO)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700230 printk("ipt_REJECT: TCP_RESET invalid for non-tcp\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700231 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 }
Jan Engelhardte1931b72007-07-07 22:16:26 -0700234 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Patrick McHardy9f15c532007-07-07 22:22:02 -0700237static struct xt_target ipt_reject_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .name = "REJECT",
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800239 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 .target = reject,
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800241 .targetsize = sizeof(struct ipt_reject_info),
242 .table = "filter",
243 .hooks = (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) |
244 (1 << NF_IP_LOCAL_OUT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .checkentry = check,
246 .me = THIS_MODULE,
247};
248
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800249static int __init ipt_reject_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800251 return xt_register_target(&ipt_reject_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800254static void __exit ipt_reject_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800256 xt_unregister_target(&ipt_reject_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800259module_init(ipt_reject_init);
260module_exit(ipt_reject_fini);