NETFILTER: remove unnecessary goto statement for error recovery
[tomato.git] / release / src-rt / linux / linux-2.6 / net / ipv4 / netfilter / ipt_REJECT.c
blobaff399d737ae61ca6ddadbe2d1fd15e95de07ba3
1 /*
2 * This is a module which is used for rejecting packets.
3 */
5 /* (C) 1999-2001 Paul `Rusty' Russell
6 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
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>
23 #include <linux/netfilter/x_tables.h>
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
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
32 MODULE_DESCRIPTION("iptables REJECT target module");
34 #if 0
35 #define DEBUGP printk
36 #else
37 #define DEBUGP(format, args...)
38 #endif
40 /* Send RST reply */
41 static void send_reset(struct sk_buff *oldskb, int hook)
43 struct sk_buff *nskb;
44 struct iphdr *oiph, *niph;
45 struct tcphdr _otcph, *oth, *tcph;
46 unsigned int addr_type;
48 /* IP header checks: fragment. */
49 if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
50 return;
52 oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
53 sizeof(_otcph), &_otcph);
54 if (oth == NULL)
55 return;
57 /* No RST for RST. */
58 if (oth->rst)
59 return;
61 /* Check checksum */
62 if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
63 return;
64 oiph = ip_hdr(oldskb);
66 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
67 LL_MAX_HEADER, GFP_ATOMIC);
68 if (!nskb)
69 return;
71 skb_reserve(nskb, LL_MAX_HEADER);
73 skb_reset_network_header(nskb);
74 niph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
75 niph->version = 4;
76 niph->ihl = sizeof(struct iphdr) / 4;
77 niph->tos = 0;
78 niph->id = 0;
79 niph->frag_off = htons(IP_DF);
80 niph->protocol = IPPROTO_TCP;
81 niph->check = 0;
82 niph->saddr = oiph->daddr;
83 niph->daddr = oiph->saddr;
85 tcph = (struct tcphdr *)skb_put(nskb, sizeof(struct tcphdr));
86 memset(tcph, 0, sizeof(*tcph));
87 tcph->source = oth->dest;
88 tcph->dest = oth->source;
89 tcph->doff = sizeof(struct tcphdr) / 4;
91 if (oth->ack)
92 tcph->seq = oth->ack_seq;
93 else {
94 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
95 oldskb->len - ip_hdrlen(oldskb) -
96 (oth->doff << 2));
97 tcph->ack = 1;
100 tcph->rst = 1;
101 tcph->check = ~tcp_v4_check(sizeof(struct tcphdr), niph->saddr,
102 niph->daddr, 0);
103 nskb->ip_summed = CHECKSUM_PARTIAL;
104 nskb->csum_start = (unsigned char *)tcph - nskb->head;
105 nskb->csum_offset = offsetof(struct tcphdr, check);
107 addr_type = RTN_UNSPEC;
108 if (hook != NF_IP_FORWARD
109 #ifdef CONFIG_BRIDGE_NETFILTER
110 || (nskb->nf_bridge && nskb->nf_bridge->mask & BRNF_BRIDGED)
111 #endif
113 addr_type = RTN_LOCAL;
115 /* ip_route_me_harder expects skb->dst to be set */
116 nskb->dst = dst_clone(oldskb->dst);
118 nskb->protocol = htons(ETH_P_IP);
119 if (ip_route_me_harder(nskb, addr_type))
120 goto free_nskb;
122 niph->ttl = dst_metric(nskb->dst, RTAX_HOPLIMIT);
124 /* "Never happens" */
125 if (nskb->len > dst_mtu(nskb->dst))
126 goto free_nskb;
128 nf_ct_attach(nskb, oldskb);
130 ip_local_out(nskb);
131 return;
133 free_nskb:
134 kfree_skb(nskb);
137 static inline void send_unreach(struct sk_buff *skb_in, int code)
139 icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
142 static unsigned int reject(struct sk_buff *skb,
143 const struct net_device *in,
144 const struct net_device *out,
145 unsigned int hooknum,
146 const struct xt_target *target,
147 const void *targinfo)
149 const struct ipt_reject_info *reject = targinfo;
151 /* WARNING: This code causes reentry within iptables.
152 This means that the iptables jump stack is now crap. We
153 must return an absolute verdict. --RR */
154 switch (reject->with) {
155 case IPT_ICMP_NET_UNREACHABLE:
156 send_unreach(skb, ICMP_NET_UNREACH);
157 break;
158 case IPT_ICMP_HOST_UNREACHABLE:
159 send_unreach(skb, ICMP_HOST_UNREACH);
160 break;
161 case IPT_ICMP_PROT_UNREACHABLE:
162 send_unreach(skb, ICMP_PROT_UNREACH);
163 break;
164 case IPT_ICMP_PORT_UNREACHABLE:
165 send_unreach(skb, ICMP_PORT_UNREACH);
166 break;
167 case IPT_ICMP_NET_PROHIBITED:
168 send_unreach(skb, ICMP_NET_ANO);
169 break;
170 case IPT_ICMP_HOST_PROHIBITED:
171 send_unreach(skb, ICMP_HOST_ANO);
172 break;
173 case IPT_ICMP_ADMIN_PROHIBITED:
174 send_unreach(skb, ICMP_PKT_FILTERED);
175 break;
176 case IPT_TCP_RESET:
177 send_reset(skb, hooknum);
178 case IPT_ICMP_ECHOREPLY:
179 /* Doesn't happen. */
180 break;
183 return NF_DROP;
186 static int check(const char *tablename,
187 const void *e_void,
188 const struct xt_target *target,
189 void *targinfo,
190 unsigned int hook_mask)
192 const struct ipt_reject_info *rejinfo = targinfo;
193 const struct ipt_entry *e = e_void;
195 if (rejinfo->with == IPT_ICMP_ECHOREPLY) {
196 printk("REJECT: ECHOREPLY no longer supported.\n");
197 return 0;
198 } else if (rejinfo->with == IPT_TCP_RESET) {
199 /* Must specify that it's a TCP packet */
200 if (e->ip.proto != IPPROTO_TCP
201 || (e->ip.invflags & XT_INV_PROTO)) {
202 DEBUGP("REJECT: TCP_RESET invalid for non-tcp\n");
203 return 0;
206 return 1;
209 static struct xt_target ipt_reject_reg = {
210 .name = "REJECT",
211 .family = AF_INET,
212 .target = reject,
213 .targetsize = sizeof(struct ipt_reject_info),
214 .table = "filter",
215 .hooks = (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) |
216 (1 << NF_IP_LOCAL_OUT),
217 .checkentry = check,
218 .me = THIS_MODULE,
221 static int __init ipt_reject_init(void)
223 return xt_register_target(&ipt_reject_reg);
226 static void __exit ipt_reject_fini(void)
228 xt_unregister_target(&ipt_reject_reg);
231 module_init(ipt_reject_init);
232 module_exit(ipt_reject_fini);