2 * This is a module which is used for rejecting packets.
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>
16 #include <linux/udp.h>
17 #include <linux/icmp.h>
21 #include <net/route.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>
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
32 MODULE_DESCRIPTION("iptables REJECT target module");
37 #define DEBUGP(format, args...)
41 static void send_reset(struct sk_buff
*oldskb
, int hook
)
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
))
52 oth
= skb_header_pointer(oldskb
, ip_hdrlen(oldskb
),
53 sizeof(_otcph
), &_otcph
);
62 if (nf_ip_checksum(oldskb
, hook
, ip_hdrlen(oldskb
), IPPROTO_TCP
))
64 oiph
= ip_hdr(oldskb
);
66 nskb
= alloc_skb(sizeof(struct iphdr
) + sizeof(struct tcphdr
) +
67 LL_MAX_HEADER
, GFP_ATOMIC
);
71 skb_reserve(nskb
, LL_MAX_HEADER
);
73 skb_reset_network_header(nskb
);
74 niph
= (struct iphdr
*)skb_put(nskb
, sizeof(struct iphdr
));
76 niph
->ihl
= sizeof(struct iphdr
) / 4;
79 niph
->frag_off
= htons(IP_DF
);
80 niph
->protocol
= IPPROTO_TCP
;
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;
92 tcph
->seq
= oth
->ack_seq
;
94 tcph
->ack_seq
= htonl(ntohl(oth
->seq
) + oth
->syn
+ oth
->fin
+
95 oldskb
->len
- ip_hdrlen(oldskb
) -
101 tcph
->check
= ~tcp_v4_check(sizeof(struct tcphdr
), niph
->saddr
,
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
)
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
))
122 niph
->ttl
= dst_metric(nskb
->dst
, RTAX_HOPLIMIT
);
124 /* "Never happens" */
125 if (nskb
->len
> dst_mtu(nskb
->dst
))
128 nf_ct_attach(nskb
, oldskb
);
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
);
158 case IPT_ICMP_HOST_UNREACHABLE
:
159 send_unreach(skb
, ICMP_HOST_UNREACH
);
161 case IPT_ICMP_PROT_UNREACHABLE
:
162 send_unreach(skb
, ICMP_PROT_UNREACH
);
164 case IPT_ICMP_PORT_UNREACHABLE
:
165 send_unreach(skb
, ICMP_PORT_UNREACH
);
167 case IPT_ICMP_NET_PROHIBITED
:
168 send_unreach(skb
, ICMP_NET_ANO
);
170 case IPT_ICMP_HOST_PROHIBITED
:
171 send_unreach(skb
, ICMP_HOST_ANO
);
173 case IPT_ICMP_ADMIN_PROHIBITED
:
174 send_unreach(skb
, ICMP_PKT_FILTERED
);
177 send_reset(skb
, hooknum
);
178 case IPT_ICMP_ECHOREPLY
:
179 /* Doesn't happen. */
186 static int check(const char *tablename
,
188 const struct xt_target
*target
,
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");
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");
209 static struct xt_target ipt_reject_reg
= {
213 .targetsize
= sizeof(struct ipt_reject_info
),
215 .hooks
= (1 << NF_IP_LOCAL_IN
) | (1 << NF_IP_FORWARD
) |
216 (1 << NF_IP_LOCAL_OUT
),
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
);