drivers/net: macvtap and tun depend on INET
[sfrench/cifs-2.6.git] / net / ipv4 / netfilter / nf_reject_ipv4.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #include <linux/module.h>
10 #include <net/ip.h>
11 #include <net/tcp.h>
12 #include <net/route.h>
13 #include <net/dst.h>
14 #include <linux/netfilter_ipv4.h>
15
16 /* Send RST reply */
17 void nf_send_reset(struct sk_buff *oldskb, int hook)
18 {
19         struct sk_buff *nskb;
20         const struct iphdr *oiph;
21         struct iphdr *niph;
22         const struct tcphdr *oth;
23         struct tcphdr _otcph, *tcph;
24
25         /* IP header checks: fragment. */
26         if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
27                 return;
28
29         oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
30                                  sizeof(_otcph), &_otcph);
31         if (oth == NULL)
32                 return;
33
34         /* No RST for RST. */
35         if (oth->rst)
36                 return;
37
38         if (skb_rtable(oldskb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
39                 return;
40
41         /* Check checksum */
42         if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
43                 return;
44         oiph = ip_hdr(oldskb);
45
46         nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
47                          LL_MAX_HEADER, GFP_ATOMIC);
48         if (!nskb)
49                 return;
50
51         skb_reserve(nskb, LL_MAX_HEADER);
52
53         skb_reset_network_header(nskb);
54         niph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
55         niph->version   = 4;
56         niph->ihl       = sizeof(struct iphdr) / 4;
57         niph->tos       = 0;
58         niph->id        = 0;
59         niph->frag_off  = htons(IP_DF);
60         niph->protocol  = IPPROTO_TCP;
61         niph->check     = 0;
62         niph->saddr     = oiph->daddr;
63         niph->daddr     = oiph->saddr;
64
65         skb_reset_transport_header(nskb);
66         tcph = (struct tcphdr *)skb_put(nskb, sizeof(struct tcphdr));
67         memset(tcph, 0, sizeof(*tcph));
68         tcph->source    = oth->dest;
69         tcph->dest      = oth->source;
70         tcph->doff      = sizeof(struct tcphdr) / 4;
71
72         if (oth->ack)
73                 tcph->seq = oth->ack_seq;
74         else {
75                 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
76                                       oldskb->len - ip_hdrlen(oldskb) -
77                                       (oth->doff << 2));
78                 tcph->ack = 1;
79         }
80
81         tcph->rst       = 1;
82         tcph->check = ~tcp_v4_check(sizeof(struct tcphdr), niph->saddr,
83                                     niph->daddr, 0);
84         nskb->ip_summed = CHECKSUM_PARTIAL;
85         nskb->csum_start = (unsigned char *)tcph - nskb->head;
86         nskb->csum_offset = offsetof(struct tcphdr, check);
87
88         /* ip_route_me_harder expects skb->dst to be set */
89         skb_dst_set_noref(nskb, skb_dst(oldskb));
90
91         nskb->protocol = htons(ETH_P_IP);
92         if (ip_route_me_harder(nskb, RTN_UNSPEC))
93                 goto free_nskb;
94
95         niph->ttl       = ip4_dst_hoplimit(skb_dst(nskb));
96
97         /* "Never happens" */
98         if (nskb->len > dst_mtu(skb_dst(nskb)))
99                 goto free_nskb;
100
101         nf_ct_attach(nskb, oldskb);
102
103 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
104         /* If we use ip_local_out for bridged traffic, the MAC source on
105          * the RST will be ours, instead of the destination's.  This confuses
106          * some routers/firewalls, and they drop the packet.  So we need to
107          * build the eth header using the original destination's MAC as the
108          * source, and send the RST packet directly.
109          */
110         if (oldskb->nf_bridge) {
111                 struct ethhdr *oeth = eth_hdr(oldskb);
112                 nskb->dev = oldskb->nf_bridge->physindev;
113                 niph->tot_len = htons(nskb->len);
114                 ip_send_check(niph);
115                 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
116                                     oeth->h_source, oeth->h_dest, nskb->len) < 0)
117                         goto free_nskb;
118                 dev_queue_xmit(nskb);
119         } else
120 #endif
121                 ip_local_out(nskb);
122
123         return;
124
125  free_nskb:
126         kfree_skb(nskb);
127 }
128 EXPORT_SYMBOL_GPL(nf_send_reset);
129
130 MODULE_LICENSE("GPL");