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