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