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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/slab.h>
17 #include <linux/udp.h>
18 #include <linux/icmp.h>
22 #include <net/route.h>
24 #include <linux/netfilter/x_tables.h>
25 #include <linux/netfilter_ipv4/ip_tables.h>
26 #include <linux/netfilter_ipv4/ipt_REJECT.h>
27 #ifdef CONFIG_BRIDGE_NETFILTER
28 #include <linux/netfilter_bridge.h>
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
33 MODULE_DESCRIPTION("Xtables: packet \"rejection\" target for IPv4");
36 static void send_reset(struct sk_buff *oldskb, int hook)
39 const struct iphdr *oiph;
41 const struct tcphdr *oth;
42 struct tcphdr _otcph, *tcph;
44 /* IP header checks: fragment. */
45 if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
48 oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
49 sizeof(_otcph), &_otcph);
57 if (skb_rtable(oldskb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
61 if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
63 oiph = ip_hdr(oldskb);
65 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
66 LL_MAX_HEADER, GFP_ATOMIC);
70 skb_reserve(nskb, LL_MAX_HEADER);
72 skb_reset_network_header(nskb);
73 niph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
75 niph->ihl = sizeof(struct iphdr) / 4;
78 niph->frag_off = htons(IP_DF);
79 niph->protocol = IPPROTO_TCP;
81 niph->saddr = oiph->daddr;
82 niph->daddr = oiph->saddr;
84 skb_reset_transport_header(nskb);
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 /* ip_route_me_harder expects skb->dst to be set */
108 skb_dst_set_noref(nskb, skb_dst(oldskb));
110 nskb->protocol = htons(ETH_P_IP);
111 if (ip_route_me_harder(nskb, RTN_UNSPEC))
114 niph->ttl = ip4_dst_hoplimit(skb_dst(nskb));
116 /* "Never happens" */
117 if (nskb->len > dst_mtu(skb_dst(nskb)))
120 nf_ct_attach(nskb, oldskb);
122 #ifdef CONFIG_BRIDGE_NETFILTER
123 /* If we use ip_local_out for bridged traffic, the MAC source on
124 * the RST will be ours, instead of the destination's. This confuses
125 * some routers/firewalls, and they drop the packet. So we need to
126 * build the eth header using the original destination's MAC as the
127 * source, and send the RST packet directly.
129 if (oldskb->nf_bridge) {
130 struct ethhdr *oeth = eth_hdr(oldskb);
131 nskb->dev = oldskb->nf_bridge->physindev;
132 niph->tot_len = htons(nskb->len);
134 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
135 oeth->h_source, oeth->h_dest, nskb->len) < 0)
137 dev_queue_xmit(nskb);
148 static inline void send_unreach(struct sk_buff *skb_in, int code)
150 icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
154 reject_tg(struct sk_buff *skb, const struct xt_action_param *par)
156 const struct ipt_reject_info *reject = par->targinfo;
158 switch (reject->with) {
159 case IPT_ICMP_NET_UNREACHABLE:
160 send_unreach(skb, ICMP_NET_UNREACH);
162 case IPT_ICMP_HOST_UNREACHABLE:
163 send_unreach(skb, ICMP_HOST_UNREACH);
165 case IPT_ICMP_PROT_UNREACHABLE:
166 send_unreach(skb, ICMP_PROT_UNREACH);
168 case IPT_ICMP_PORT_UNREACHABLE:
169 send_unreach(skb, ICMP_PORT_UNREACH);
171 case IPT_ICMP_NET_PROHIBITED:
172 send_unreach(skb, ICMP_NET_ANO);
174 case IPT_ICMP_HOST_PROHIBITED:
175 send_unreach(skb, ICMP_HOST_ANO);
177 case IPT_ICMP_ADMIN_PROHIBITED:
178 send_unreach(skb, ICMP_PKT_FILTERED);
181 send_reset(skb, par->hooknum);
182 case IPT_ICMP_ECHOREPLY:
183 /* Doesn't happen. */
190 static int reject_tg_check(const struct xt_tgchk_param *par)
192 const struct ipt_reject_info *rejinfo = par->targinfo;
193 const struct ipt_entry *e = par->entryinfo;
195 if (rejinfo->with == IPT_ICMP_ECHOREPLY) {
196 pr_info("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 pr_info("TCP_RESET invalid for non-tcp\n");
209 static struct xt_target reject_tg_reg __read_mostly = {
211 .family = NFPROTO_IPV4,
213 .targetsize = sizeof(struct ipt_reject_info),
215 .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) |
216 (1 << NF_INET_LOCAL_OUT),
217 .checkentry = reject_tg_check,
221 static int __init reject_tg_init(void)
223 return xt_register_target(&reject_tg_reg);
226 static void __exit reject_tg_exit(void)
228 xt_unregister_target(&reject_tg_reg);
231 module_init(reject_tg_init);
232 module_exit(reject_tg_exit);