blob: 2639872849da434bbdc5de4c50584ef2ab9497f7 [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>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080032MODULE_DESCRIPTION("Xtables: packet \"rejection\" target for IPv4");
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
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;
Jan Engelhardt3cf93c92008-04-14 09:56:05 +020038 const struct iphdr *oiph;
39 struct iphdr *niph;
40 const struct tcphdr *oth;
41 struct tcphdr _otcph, *tcph;
Patrick McHardy9d020022006-10-02 16:12:20 -070042 unsigned int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 /* IP header checks: fragment. */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070045 if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return;
47
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030048 oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 sizeof(_otcph), &_otcph);
50 if (oth == NULL)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090051 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 /* No RST for RST. */
54 if (oth->rst)
55 return;
56
Patrick McHardy6150bac2005-06-21 14:03:46 -070057 /* Check checksum */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030058 if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
Patrick McHardy6150bac2005-06-21 14:03:46 -070059 return;
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080060 oiph = ip_hdr(oldskb);
Patrick McHardy6150bac2005-06-21 14:03:46 -070061
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080062 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
63 LL_MAX_HEADER, GFP_ATOMIC);
Patrick McHardy9d020022006-10-02 16:12:20 -070064 if (!nskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080067 skb_reserve(nskb, LL_MAX_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080069 skb_reset_network_header(nskb);
70 niph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
71 niph->version = 4;
72 niph->ihl = sizeof(struct iphdr) / 4;
73 niph->tos = 0;
74 niph->id = 0;
75 niph->frag_off = htons(IP_DF);
76 niph->protocol = IPPROTO_TCP;
77 niph->check = 0;
78 niph->saddr = oiph->daddr;
79 niph->daddr = oiph->saddr;
Herbert Xubbf4a6b2007-02-13 12:32:58 -080080
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080081 tcph = (struct tcphdr *)skb_put(nskb, sizeof(struct tcphdr));
82 memset(tcph, 0, sizeof(*tcph));
83 tcph->source = oth->dest;
84 tcph->dest = oth->source;
85 tcph->doff = sizeof(struct tcphdr) / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080087 if (oth->ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 tcph->seq = oth->ack_seq;
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080089 else {
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030090 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
91 oldskb->len - ip_hdrlen(oldskb) -
92 (oth->doff << 2));
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080093 tcph->ack = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95
Denys Vlasenko9ba99b02008-01-14 23:44:26 -080096 tcph->rst = 1;
97 tcph->check = tcp_v4_check(sizeof(struct tcphdr),
98 niph->saddr, niph->daddr,
99 csum_partial(tcph,
100 sizeof(struct tcphdr), 0));
Patrick McHardy9d020022006-10-02 16:12:20 -0700101
102 addr_type = RTN_UNSPEC;
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800103 if (hook != NF_INET_FORWARD
Patrick McHardy9d020022006-10-02 16:12:20 -0700104#ifdef CONFIG_BRIDGE_NETFILTER
105 || (nskb->nf_bridge && nskb->nf_bridge->mask & BRNF_BRIDGED)
106#endif
107 )
108 addr_type = RTN_LOCAL;
109
Denys Vlasenko9ba99b02008-01-14 23:44:26 -0800110 /* ip_route_me_harder expects skb->dst to be set */
111 dst_hold(oldskb->dst);
112 nskb->dst = oldskb->dst;
113
Herbert Xu3db05fe2007-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
Denys Vlasenko9ba99b02008-01-14 23:44:26 -0800117 niph->ttl = dst_metric(nskb->dst, 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" */
121 if (nskb->len > dst_mtu(nskb->dst))
122 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
139reject_tg(struct sk_buff *skb, const struct net_device *in,
140 const struct net_device *out, unsigned int hooknum,
141 const struct xt_target *target, const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 const struct ipt_reject_info *reject = targinfo;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* WARNING: This code causes reentry within iptables.
146 This means that the iptables jump stack is now crap. We
147 must return an absolute verdict. --RR */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900148 switch (reject->with) {
149 case IPT_ICMP_NET_UNREACHABLE:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700150 send_unreach(skb, ICMP_NET_UNREACH);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900151 break;
152 case IPT_ICMP_HOST_UNREACHABLE:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700153 send_unreach(skb, ICMP_HOST_UNREACH);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900154 break;
155 case IPT_ICMP_PROT_UNREACHABLE:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700156 send_unreach(skb, ICMP_PROT_UNREACH);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900157 break;
158 case IPT_ICMP_PORT_UNREACHABLE:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700159 send_unreach(skb, ICMP_PORT_UNREACH);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900160 break;
161 case IPT_ICMP_NET_PROHIBITED:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700162 send_unreach(skb, ICMP_NET_ANO);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900163 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 case IPT_ICMP_HOST_PROHIBITED:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700165 send_unreach(skb, ICMP_HOST_ANO);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900166 break;
167 case IPT_ICMP_ADMIN_PROHIBITED:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700168 send_unreach(skb, ICMP_PKT_FILTERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 break;
170 case IPT_TCP_RESET:
Herbert Xu3db05fe2007-10-15 00:53:15 -0700171 send_reset(skb, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 case IPT_ICMP_ECHOREPLY:
173 /* Doesn't happen. */
174 break;
175 }
176
177 return NF_DROP;
178}
179
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800180static bool
181reject_tg_check(const char *tablename, const void *e_void,
182 const struct xt_target *target, void *targinfo,
183 unsigned int hook_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900185 const struct ipt_reject_info *rejinfo = targinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800186 const struct ipt_entry *e = e_void;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (rejinfo->with == IPT_ICMP_ECHOREPLY) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700189 printk("ipt_REJECT: ECHOREPLY no longer supported.\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700190 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 } else if (rejinfo->with == IPT_TCP_RESET) {
192 /* Must specify that it's a TCP packet */
193 if (e->ip.proto != IPPROTO_TCP
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800194 || (e->ip.invflags & XT_INV_PROTO)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700195 printk("ipt_REJECT: TCP_RESET invalid for non-tcp\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700196 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198 }
Jan Engelhardte1931b72007-07-07 22:16:26 -0700199 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800202static struct xt_target reject_tg_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 .name = "REJECT",
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800204 .family = AF_INET,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800205 .target = reject_tg,
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800206 .targetsize = sizeof(struct ipt_reject_info),
207 .table = "filter",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800208 .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) |
209 (1 << NF_INET_LOCAL_OUT),
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800210 .checkentry = reject_tg_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 .me = THIS_MODULE,
212};
213
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800214static int __init reject_tg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800216 return xt_register_target(&reject_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800219static void __exit reject_tg_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800221 xt_unregister_target(&reject_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800224module_init(reject_tg_init);
225module_exit(reject_tg_exit);