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("Xtables: packet \"rejection\" target for IPv4");
35 static void send_reset(struct sk_buff
*oldskb
, int hook
)
38 struct iphdr
*oiph
, *niph
;
39 struct tcphdr _otcph
, *oth
, *tcph
;
40 unsigned int addr_type
;
42 /* IP header checks: fragment. */
43 if (ip_hdr(oldskb
)->frag_off
& htons(IP_OFFSET
))
46 oth
= skb_header_pointer(oldskb
, ip_hdrlen(oldskb
),
47 sizeof(_otcph
), &_otcph
);
56 if (nf_ip_checksum(oldskb
, hook
, ip_hdrlen(oldskb
), IPPROTO_TCP
))
58 oiph
= ip_hdr(oldskb
);
60 nskb
= alloc_skb(sizeof(struct iphdr
) + sizeof(struct tcphdr
) +
61 LL_MAX_HEADER
, GFP_ATOMIC
);
65 skb_reserve(nskb
, LL_MAX_HEADER
);
67 skb_reset_network_header(nskb
);
68 niph
= (struct iphdr
*)skb_put(nskb
, sizeof(struct iphdr
));
70 niph
->ihl
= sizeof(struct iphdr
) / 4;
73 niph
->frag_off
= htons(IP_DF
);
74 niph
->protocol
= IPPROTO_TCP
;
76 niph
->saddr
= oiph
->daddr
;
77 niph
->daddr
= oiph
->saddr
;
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;
86 tcph
->seq
= oth
->ack_seq
;
88 tcph
->ack_seq
= htonl(ntohl(oth
->seq
) + oth
->syn
+ oth
->fin
+
89 oldskb
->len
- ip_hdrlen(oldskb
) -
95 tcph
->check
= tcp_v4_check(sizeof(struct tcphdr
),
96 niph
->saddr
, niph
->daddr
,
98 sizeof(struct tcphdr
), 0));
100 addr_type
= RTN_UNSPEC
;
101 if (hook
!= NF_INET_FORWARD
102 #ifdef CONFIG_BRIDGE_NETFILTER
103 || (nskb
->nf_bridge
&& nskb
->nf_bridge
->mask
& BRNF_BRIDGED
)
106 addr_type
= RTN_LOCAL
;
108 /* ip_route_me_harder expects skb->dst to be set */
109 dst_hold(oldskb
->dst
);
110 nskb
->dst
= oldskb
->dst
;
112 if (ip_route_me_harder(nskb
, addr_type
))
115 niph
->ttl
= dst_metric(nskb
->dst
, RTAX_HOPLIMIT
);
116 nskb
->ip_summed
= CHECKSUM_NONE
;
118 /* "Never happens" */
119 if (nskb
->len
> dst_mtu(nskb
->dst
))
122 nf_ct_attach(nskb
, oldskb
);
131 static inline void send_unreach(struct sk_buff
*skb_in
, int code
)
133 icmp_send(skb_in
, ICMP_DEST_UNREACH
, code
, 0);
137 reject_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
)
141 const struct ipt_reject_info
*reject
= targinfo
;
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 */
146 switch (reject
->with
) {
147 case IPT_ICMP_NET_UNREACHABLE
:
148 send_unreach(skb
, ICMP_NET_UNREACH
);
150 case IPT_ICMP_HOST_UNREACHABLE
:
151 send_unreach(skb
, ICMP_HOST_UNREACH
);
153 case IPT_ICMP_PROT_UNREACHABLE
:
154 send_unreach(skb
, ICMP_PROT_UNREACH
);
156 case IPT_ICMP_PORT_UNREACHABLE
:
157 send_unreach(skb
, ICMP_PORT_UNREACH
);
159 case IPT_ICMP_NET_PROHIBITED
:
160 send_unreach(skb
, ICMP_NET_ANO
);
162 case IPT_ICMP_HOST_PROHIBITED
:
163 send_unreach(skb
, ICMP_HOST_ANO
);
165 case IPT_ICMP_ADMIN_PROHIBITED
:
166 send_unreach(skb
, ICMP_PKT_FILTERED
);
169 send_reset(skb
, hooknum
);
170 case IPT_ICMP_ECHOREPLY
:
171 /* Doesn't happen. */
179 reject_tg_check(const char *tablename
, const void *e_void
,
180 const struct xt_target
*target
, void *targinfo
,
181 unsigned int hook_mask
)
183 const struct ipt_reject_info
*rejinfo
= targinfo
;
184 const struct ipt_entry
*e
= e_void
;
186 if (rejinfo
->with
== IPT_ICMP_ECHOREPLY
) {
187 printk("ipt_REJECT: ECHOREPLY no longer supported.\n");
189 } else if (rejinfo
->with
== IPT_TCP_RESET
) {
190 /* Must specify that it's a TCP packet */
191 if (e
->ip
.proto
!= IPPROTO_TCP
192 || (e
->ip
.invflags
& XT_INV_PROTO
)) {
193 printk("ipt_REJECT: TCP_RESET invalid for non-tcp\n");
200 static struct xt_target reject_tg_reg __read_mostly
= {
204 .targetsize
= sizeof(struct ipt_reject_info
),
206 .hooks
= (1 << NF_INET_LOCAL_IN
) | (1 << NF_INET_FORWARD
) |
207 (1 << NF_INET_LOCAL_OUT
),
208 .checkentry
= reject_tg_check
,
212 static int __init
reject_tg_init(void)
214 return xt_register_target(&reject_tg_reg
);
217 static void __exit
reject_tg_exit(void)
219 xt_unregister_target(&reject_tg_reg
);
222 module_init(reject_tg_init
);
223 module_exit(reject_tg_exit
);