blob: a0e8bcf041592cac88b9a623cb001e22cd0b55cd [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080024#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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
31MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080033MODULE_DESCRIPTION("Xtables: packet \"rejection\" target for IPv4");
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/* Send RST reply */
36static void send_reset(struct sk_buff *oldskb, int hook)
37{
38 struct sk_buff *nskb;
Jan Engelhardt3cf93c92008-04-14 09:56:05 +020039 const struct iphdr *oiph;
40 struct iphdr *niph;
41 const struct tcphdr *oth;
42 struct tcphdr _otcph, *tcph;
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;
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080061 oiph = ip_hdr(oldskb);
Patrick McHardy6150bac2005-06-21 14:03:46 -070062
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080063 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
64 LL_MAX_HEADER, GFP_ATOMIC);
Patrick McHardy9d020022006-10-02 16:12:20 -070065 if (!nskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080068 skb_reserve(nskb, LL_MAX_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080070 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;
Herbert Xubbf4a6b2007-02-13 12:32:58 -080081
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080082 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080088 if (oth->ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 tcph->seq = oth->ack_seq;
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080090 else {
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030091 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
92 oldskb->len - ip_hdrlen(oldskb) -
93 (oth->doff << 2));
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080094 tcph->ack = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
96
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080097 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));
Patrick McHardy9d020022006-10-02 16:12:20 -0700102
103 addr_type = RTN_UNSPEC;
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800104 if (hook != NF_INET_FORWARD
Patrick McHardy9d020022006-10-02 16:12:20 -0700105#ifdef CONFIG_BRIDGE_NETFILTER
106 || (nskb->nf_bridge && nskb->nf_bridge->mask & BRNF_BRIDGED)
107#endif
108 )
109 addr_type = RTN_LOCAL;
110
Denys Vlasenko9ba99b02008-01-14 23:44:26 -0800111 /* ip_route_me_harder expects skb->dst to be set */
Eric Dumazetadf30902009-06-02 05:19:30 +0000112 skb_dst_set(nskb, dst_clone(skb_dst(oldskb)));
Denys Vlasenko9ba99b02008-01-14 23:44:26 -0800113
Herbert Xu3db05fea2007-10-15 00:53:15 -0700114 if (ip_route_me_harder(nskb, addr_type))
Patrick McHardy9d020022006-10-02 16:12:20 -0700115 goto free_nskb;
116
Eric Dumazetadf30902009-06-02 05:19:30 +0000117 niph->ttl = dst_metric(skb_dst(nskb), RTAX_HOPLIMIT);
Patrick McHardy4cf411d2006-08-05 00:58:33 -0700118 nskb->ip_summed = CHECKSUM_NONE;
Patrick McHardyaf443b62006-11-28 20:10:21 -0800119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 /* "Never happens" */
Eric Dumazetadf30902009-06-02 05:19:30 +0000121 if (nskb->len > dst_mtu(skb_dst(nskb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 goto free_nskb;
123
124 nf_ct_attach(nskb, oldskb);
125
Herbert Xuc439cb22008-01-11 19:14:00 -0800126 ip_local_out(nskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return;
128
129 free_nskb:
130 kfree_skb(nskb);
131}
132
133static inline void send_unreach(struct sk_buff *skb_in, int code)
134{
135 icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900136}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800138static unsigned int
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200139reject_tg(struct sk_buff *skb, const struct xt_target_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200141 const struct ipt_reject_info *reject = par->targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 /* WARNING: This code causes reentry within iptables.
144 This means that the iptables jump stack is now crap. We
145 must return an absolute verdict. --RR */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900146 switch (reject->with) {
147 case IPT_ICMP_NET_UNREACHABLE:
Herbert Xu3db05fea2007-10-15 00:53:15 -0700148 send_unreach(skb, ICMP_NET_UNREACH);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900149 break;
150 case IPT_ICMP_HOST_UNREACHABLE:
Herbert Xu3db05fea2007-10-15 00:53:15 -0700151 send_unreach(skb, ICMP_HOST_UNREACH);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900152 break;
153 case IPT_ICMP_PROT_UNREACHABLE:
Herbert Xu3db05fea2007-10-15 00:53:15 -0700154 send_unreach(skb, ICMP_PROT_UNREACH);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900155 break;
156 case IPT_ICMP_PORT_UNREACHABLE:
Herbert Xu3db05fea2007-10-15 00:53:15 -0700157 send_unreach(skb, ICMP_PORT_UNREACH);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900158 break;
159 case IPT_ICMP_NET_PROHIBITED:
Herbert Xu3db05fea2007-10-15 00:53:15 -0700160 send_unreach(skb, ICMP_NET_ANO);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900161 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 case IPT_ICMP_HOST_PROHIBITED:
Herbert Xu3db05fea2007-10-15 00:53:15 -0700163 send_unreach(skb, ICMP_HOST_ANO);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900164 break;
165 case IPT_ICMP_ADMIN_PROHIBITED:
Herbert Xu3db05fea2007-10-15 00:53:15 -0700166 send_unreach(skb, ICMP_PKT_FILTERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 break;
168 case IPT_TCP_RESET:
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200169 send_reset(skb, par->hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 case IPT_ICMP_ECHOREPLY:
171 /* Doesn't happen. */
172 break;
173 }
174
175 return NF_DROP;
176}
177
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200178static bool reject_tg_check(const struct xt_tgchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200180 const struct ipt_reject_info *rejinfo = par->targinfo;
181 const struct ipt_entry *e = par->entryinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (rejinfo->with == IPT_ICMP_ECHOREPLY) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700184 printk("ipt_REJECT: ECHOREPLY no longer supported.\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700185 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 } else if (rejinfo->with == IPT_TCP_RESET) {
187 /* Must specify that it's a TCP packet */
Joe Perches3666ed12009-11-23 23:17:06 +0100188 if (e->ip.proto != IPPROTO_TCP ||
189 (e->ip.invflags & XT_INV_PROTO)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700190 printk("ipt_REJECT: TCP_RESET invalid for non-tcp\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700191 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193 }
Jan Engelhardte1931b72007-07-07 22:16:26 -0700194 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800197static struct xt_target reject_tg_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .name = "REJECT",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200199 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800200 .target = reject_tg,
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800201 .targetsize = sizeof(struct ipt_reject_info),
202 .table = "filter",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800203 .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) |
204 (1 << NF_INET_LOCAL_OUT),
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800205 .checkentry = reject_tg_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 .me = THIS_MODULE,
207};
208
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800209static int __init reject_tg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800211 return xt_register_target(&reject_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800214static void __exit reject_tg_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800216 xt_unregister_target(&reject_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800219module_init(reject_tg_init);
220module_exit(reject_tg_exit);