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