netfilter: Introduce NFPROTO_* constants
[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;
3cf93c96
JE
38 const struct iphdr *oiph;
39 struct iphdr *niph;
40 const struct tcphdr *oth;
41 struct tcphdr _otcph, *tcph;
9d02002d 42 unsigned int addr_type;
1da177e4
LT
43
44 /* IP header checks: fragment. */
eddc9ec5 45 if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
1da177e4
LT
46 return;
47
c9bdd4b5 48 oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
1da177e4
LT
49 sizeof(_otcph), &_otcph);
50 if (oth == NULL)
e905a9ed 51 return;
1da177e4
LT
52
53 /* No RST for RST. */
54 if (oth->rst)
55 return;
56
6150bacf 57 /* Check checksum */
c9bdd4b5 58 if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
6150bacf 59 return;
9ba99b0d 60 oiph = ip_hdr(oldskb);
6150bacf 61
9ba99b0d
DV
62 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
63 LL_MAX_HEADER, GFP_ATOMIC);
9d02002d 64 if (!nskb)
1da177e4 65 return;
1da177e4 66
9ba99b0d
DV
67 skb_reserve(nskb, LL_MAX_HEADER);
68
69 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;
80
81 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;
86
87 if (oth->ack)
1da177e4 88 tcph->seq = oth->ack_seq;
9ba99b0d 89 else {
c9bdd4b5
ACM
90 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
91 oldskb->len - ip_hdrlen(oldskb) -
92 (oth->doff << 2));
9ba99b0d 93 tcph->ack = 1;
1da177e4
LT
94 }
95
9ba99b0d
DV
96 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));
9d02002d
PM
101
102 addr_type = RTN_UNSPEC;
6e23ae2a 103 if (hook != NF_INET_FORWARD
9d02002d
PM
104#ifdef CONFIG_BRIDGE_NETFILTER
105 || (nskb->nf_bridge && nskb->nf_bridge->mask & BRNF_BRIDGED)
106#endif
107 )
108 addr_type = RTN_LOCAL;
109
9ba99b0d
DV
110 /* ip_route_me_harder expects skb->dst to be set */
111 dst_hold(oldskb->dst);
112 nskb->dst = oldskb->dst;
113
3db05fea 114 if (ip_route_me_harder(nskb, addr_type))
9d02002d
PM
115 goto free_nskb;
116
9ba99b0d 117 niph->ttl = dst_metric(nskb->dst, RTAX_HOPLIMIT);
4cf411de 118 nskb->ip_summed = CHECKSUM_NONE;
af443b6d 119
1da177e4
LT
120 /* "Never happens" */
121 if (nskb->len > dst_mtu(nskb->dst))
122 goto free_nskb;
123
124 nf_ct_attach(nskb, oldskb);
125
c439cb2e 126 ip_local_out(nskb);
1da177e4
LT
127 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);
e905a9ed 136}
1da177e4 137
d3c5ee6d
JE
138static 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)
1da177e4
LT
142{
143 const struct ipt_reject_info *reject = targinfo;
144
1da177e4
LT
145 /* 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 */
e905a9ed
YH
148 switch (reject->with) {
149 case IPT_ICMP_NET_UNREACHABLE:
3db05fea 150 send_unreach(skb, ICMP_NET_UNREACH);
e905a9ed
YH
151 break;
152 case IPT_ICMP_HOST_UNREACHABLE:
3db05fea 153 send_unreach(skb, ICMP_HOST_UNREACH);
e905a9ed
YH
154 break;
155 case IPT_ICMP_PROT_UNREACHABLE:
3db05fea 156 send_unreach(skb, ICMP_PROT_UNREACH);
e905a9ed
YH
157 break;
158 case IPT_ICMP_PORT_UNREACHABLE:
3db05fea 159 send_unreach(skb, ICMP_PORT_UNREACH);
e905a9ed
YH
160 break;
161 case IPT_ICMP_NET_PROHIBITED:
3db05fea 162 send_unreach(skb, ICMP_NET_ANO);
e905a9ed 163 break;
1da177e4 164 case IPT_ICMP_HOST_PROHIBITED:
3db05fea 165 send_unreach(skb, ICMP_HOST_ANO);
e905a9ed
YH
166 break;
167 case IPT_ICMP_ADMIN_PROHIBITED:
3db05fea 168 send_unreach(skb, ICMP_PKT_FILTERED);
1da177e4
LT
169 break;
170 case IPT_TCP_RESET:
3db05fea 171 send_reset(skb, hooknum);
1da177e4
LT
172 case IPT_ICMP_ECHOREPLY:
173 /* Doesn't happen. */
174 break;
175 }
176
177 return NF_DROP;
178}
179
d3c5ee6d
JE
180static bool
181reject_tg_check(const char *tablename, const void *e_void,
182 const struct xt_target *target, void *targinfo,
183 unsigned int hook_mask)
1da177e4 184{
e905a9ed 185 const struct ipt_reject_info *rejinfo = targinfo;
2e4e6a17 186 const struct ipt_entry *e = e_void;
1da177e4 187
1da177e4 188 if (rejinfo->with == IPT_ICMP_ECHOREPLY) {
0d53778e 189 printk("ipt_REJECT: ECHOREPLY no longer supported.\n");
e1931b78 190 return false;
1da177e4
LT
191 } else if (rejinfo->with == IPT_TCP_RESET) {
192 /* Must specify that it's a TCP packet */
193 if (e->ip.proto != IPPROTO_TCP
6709dbbb 194 || (e->ip.invflags & XT_INV_PROTO)) {
0d53778e 195 printk("ipt_REJECT: TCP_RESET invalid for non-tcp\n");
e1931b78 196 return false;
1da177e4
LT
197 }
198 }
e1931b78 199 return true;
1da177e4
LT
200}
201
d3c5ee6d 202static struct xt_target reject_tg_reg __read_mostly = {
1da177e4 203 .name = "REJECT",
6709dbbb 204 .family = AF_INET,
d3c5ee6d 205 .target = reject_tg,
1d5cd909
PM
206 .targetsize = sizeof(struct ipt_reject_info),
207 .table = "filter",
6e23ae2a
PM
208 .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) |
209 (1 << NF_INET_LOCAL_OUT),
d3c5ee6d 210 .checkentry = reject_tg_check,
1da177e4
LT
211 .me = THIS_MODULE,
212};
213
d3c5ee6d 214static int __init reject_tg_init(void)
1da177e4 215{
d3c5ee6d 216 return xt_register_target(&reject_tg_reg);
1da177e4
LT
217}
218
d3c5ee6d 219static void __exit reject_tg_exit(void)
1da177e4 220{
d3c5ee6d 221 xt_unregister_target(&reject_tg_reg);
1da177e4
LT
222}
223
d3c5ee6d
JE
224module_init(reject_tg_init);
225module_exit(reject_tg_exit);
This page took 0.42846 seconds and 5 git commands to generate.