blob: d55b262bf6084e095f60fc1d34421a2886922050 [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));
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 if (tcph->ack) {
95 needs_ack = 0;
96 tcph->seq = oth->ack_seq;
97 tcph->ack_seq = 0;
98 } else {
99 needs_ack = 1;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300100 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
101 oldskb->len - ip_hdrlen(oldskb) -
102 (oth->doff << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 tcph->seq = 0;
104 }
105
106 /* Reset flags */
107 ((u_int8_t *)tcph)[13] = 0;
108 tcph->rst = 1;
109 tcph->ack = needs_ack;
110
111 tcph->window = 0;
112 tcph->urg_ptr = 0;
113
Patrick McHardyaf443b62006-11-28 20:10:21 -0800114 /* Adjust TCP checksum */
115 tcph->check = 0;
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800116 tcph->check = tcp_v4_check(sizeof(struct tcphdr),
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700117 niph->saddr, niph->daddr,
Jan Engelhardta47362a2007-07-07 22:16:55 -0700118 csum_partial(tcph,
Patrick McHardyaf443b62006-11-28 20:10:21 -0800119 sizeof(struct tcphdr), 0));
120
Patrick McHardy9d020022006-10-02 16:12:20 -0700121 /* Set DF, id = 0 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700122 niph->frag_off = htons(IP_DF);
123 niph->id = 0;
Patrick McHardy9d020022006-10-02 16:12:20 -0700124
125 addr_type = RTN_UNSPEC;
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800126 if (hook != NF_INET_FORWARD
Patrick McHardy9d020022006-10-02 16:12:20 -0700127#ifdef CONFIG_BRIDGE_NETFILTER
128 || (nskb->nf_bridge && nskb->nf_bridge->mask & BRNF_BRIDGED)
129#endif
130 )
131 addr_type = RTN_LOCAL;
132
Herbert Xu3db05fe2007-10-15 00:53:15 -0700133 if (ip_route_me_harder(nskb, addr_type))
Patrick McHardy9d020022006-10-02 16:12:20 -0700134 goto free_nskb;
135
Patrick McHardy4cf411d2006-08-05 00:58:33 -0700136 nskb->ip_summed = CHECKSUM_NONE;
Patrick McHardyaf443b62006-11-28 20:10:21 -0800137
Patrick McHardy9d020022006-10-02 16:12:20 -0700138 /* Adjust IP TTL */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700139 niph->ttl = dst_metric(nskb->dst, RTAX_HOPLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 /* "Never happens" */
142 if (nskb->len > dst_mtu(nskb->dst))
143 goto free_nskb;
144
145 nf_ct_attach(nskb, oldskb);
146
Herbert Xuc439cb22008-01-11 19:14:00 -0800147 ip_local_out(nskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return;
149
150 free_nskb:
151 kfree_skb(nskb);
152}
153
154static inline void send_unreach(struct sk_buff *skb_in, int code)
155{
156 icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900157}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Herbert Xu3db05fe2007-10-15 00:53:15 -0700159static unsigned int reject(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 const struct net_device *in,
161 const struct net_device *out,
162 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -0800163 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700164 const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 const struct ipt_reject_info *reject = targinfo;
167
168 /* Our naive response construction doesn't deal with IP
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900169 options, and probably shouldn't try. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700170 if (ip_hdrlen(skb) != sizeof(struct iphdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return NF_DROP;
172
173 /* WARNING: This code causes reentry within iptables.
174 This means that the iptables jump stack is now crap. We
175 must return an absolute verdict. --RR */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900176 switch (reject->with) {
177 case IPT_ICMP_NET_UNREACHABLE:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700178 send_unreach(skb, ICMP_NET_UNREACH);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900179 break;
180 case IPT_ICMP_HOST_UNREACHABLE:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700181 send_unreach(skb, ICMP_HOST_UNREACH);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900182 break;
183 case IPT_ICMP_PROT_UNREACHABLE:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700184 send_unreach(skb, ICMP_PROT_UNREACH);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900185 break;
186 case IPT_ICMP_PORT_UNREACHABLE:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700187 send_unreach(skb, ICMP_PORT_UNREACH);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900188 break;
189 case IPT_ICMP_NET_PROHIBITED:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700190 send_unreach(skb, ICMP_NET_ANO);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900191 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 case IPT_ICMP_HOST_PROHIBITED:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700193 send_unreach(skb, ICMP_HOST_ANO);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900194 break;
195 case IPT_ICMP_ADMIN_PROHIBITED:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700196 send_unreach(skb, ICMP_PKT_FILTERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 break;
198 case IPT_TCP_RESET:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700199 send_reset(skb, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 case IPT_ICMP_ECHOREPLY:
201 /* Doesn't happen. */
202 break;
203 }
204
205 return NF_DROP;
206}
207
Jan Engelhardte1931b72007-07-07 22:16:26 -0700208static bool check(const char *tablename,
209 const void *e_void,
210 const struct xt_target *target,
211 void *targinfo,
212 unsigned int hook_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900214 const struct ipt_reject_info *rejinfo = targinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800215 const struct ipt_entry *e = e_void;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (rejinfo->with == IPT_ICMP_ECHOREPLY) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700218 printk("ipt_REJECT: ECHOREPLY no longer supported.\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700219 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 } else if (rejinfo->with == IPT_TCP_RESET) {
221 /* Must specify that it's a TCP packet */
222 if (e->ip.proto != IPPROTO_TCP
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800223 || (e->ip.invflags & XT_INV_PROTO)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700224 printk("ipt_REJECT: TCP_RESET invalid for non-tcp\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700225 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227 }
Jan Engelhardte1931b72007-07-07 22:16:26 -0700228 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Patrick McHardy9f15c532007-07-07 22:22:02 -0700231static struct xt_target ipt_reject_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 .name = "REJECT",
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800233 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 .target = reject,
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800235 .targetsize = sizeof(struct ipt_reject_info),
236 .table = "filter",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800237 .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) |
238 (1 << NF_INET_LOCAL_OUT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 .checkentry = check,
240 .me = THIS_MODULE,
241};
242
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800243static int __init ipt_reject_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800245 return xt_register_target(&ipt_reject_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800248static void __exit ipt_reject_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800250 xt_unregister_target(&ipt_reject_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800253module_init(ipt_reject_init);
254module_exit(ipt_reject_fini);