2014-09-26 12:35:15 +00:00
|
|
|
/* (C) 1999-2001 Paul `Rusty' Russell
|
|
|
|
* (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
2014-10-10 09:25:20 +00:00
|
|
|
#include <linux/module.h>
|
2014-09-26 12:35:15 +00:00
|
|
|
#include <net/ip.h>
|
|
|
|
#include <net/tcp.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/dst.h>
|
2014-11-13 09:04:16 +00:00
|
|
|
#include <net/netfilter/ipv4/nf_reject.h>
|
2014-09-26 12:35:15 +00:00
|
|
|
#include <linux/netfilter_ipv4.h>
|
2015-04-02 12:31:41 +00:00
|
|
|
#include <linux/netfilter_bridge.h>
|
2014-09-26 12:35:15 +00:00
|
|
|
|
2014-10-25 16:24:57 +00:00
|
|
|
const struct tcphdr *nf_reject_ip_tcphdr_get(struct sk_buff *oldskb,
|
|
|
|
struct tcphdr *_oth, int hook)
|
2014-09-26 12:35:15 +00:00
|
|
|
{
|
|
|
|
const struct tcphdr *oth;
|
|
|
|
|
|
|
|
/* IP header checks: fragment. */
|
|
|
|
if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
|
2014-10-25 16:24:57 +00:00
|
|
|
return NULL;
|
2014-09-26 12:35:15 +00:00
|
|
|
|
2016-06-20 13:26:28 +00:00
|
|
|
if (ip_hdr(oldskb)->protocol != IPPROTO_TCP)
|
|
|
|
return NULL;
|
|
|
|
|
2014-09-26 12:35:15 +00:00
|
|
|
oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
|
2014-10-25 16:24:57 +00:00
|
|
|
sizeof(struct tcphdr), _oth);
|
2014-09-26 12:35:15 +00:00
|
|
|
if (oth == NULL)
|
2014-10-25 16:24:57 +00:00
|
|
|
return NULL;
|
2014-09-26 12:35:15 +00:00
|
|
|
|
|
|
|
/* No RST for RST. */
|
|
|
|
if (oth->rst)
|
2014-10-25 16:24:57 +00:00
|
|
|
return NULL;
|
2014-09-26 12:35:15 +00:00
|
|
|
|
|
|
|
/* Check checksum */
|
|
|
|
if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
|
2014-10-25 16:24:57 +00:00
|
|
|
return NULL;
|
2014-09-26 12:35:15 +00:00
|
|
|
|
2014-10-25 16:24:57 +00:00
|
|
|
return oth;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_reject_ip_tcphdr_get);
|
2014-09-26 12:35:15 +00:00
|
|
|
|
2014-10-25 16:24:57 +00:00
|
|
|
struct iphdr *nf_reject_iphdr_put(struct sk_buff *nskb,
|
|
|
|
const struct sk_buff *oldskb,
|
2015-03-09 22:04:15 +00:00
|
|
|
__u8 protocol, int ttl)
|
2014-10-25 16:24:57 +00:00
|
|
|
{
|
|
|
|
struct iphdr *niph, *oiph = ip_hdr(oldskb);
|
2014-09-26 12:35:15 +00:00
|
|
|
|
|
|
|
skb_reset_network_header(nskb);
|
networking: make skb_put & friends return void pointers
It seems like a historic accident that these return unsigned char *,
and in many places that means casts are required, more often than not.
Make these functions (skb_put, __skb_put and pskb_put) return void *
and remove all the casts across the tree, adding a (u8 *) cast only
where the unsigned char pointer was used directly, all done with the
following spatch:
@@
expression SKB, LEN;
typedef u8;
identifier fn = { skb_put, __skb_put };
@@
- *(fn(SKB, LEN))
+ *(u8 *)fn(SKB, LEN)
@@
expression E, SKB, LEN;
identifier fn = { skb_put, __skb_put };
type T;
@@
- E = ((T *)(fn(SKB, LEN)))
+ E = fn(SKB, LEN)
which actually doesn't cover pskb_put since there are only three
users overall.
A handful of stragglers were converted manually, notably a macro in
drivers/isdn/i4l/isdn_bsdcomp.c and, oddly enough, one of the many
instances in net/bluetooth/hci_sock.c. In the former file, I also
had to fix one whitespace problem spatch introduced.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-06-16 12:29:21 +00:00
|
|
|
niph = skb_put(nskb, sizeof(struct iphdr));
|
2014-09-26 12:35:15 +00:00
|
|
|
niph->version = 4;
|
|
|
|
niph->ihl = sizeof(struct iphdr) / 4;
|
|
|
|
niph->tos = 0;
|
|
|
|
niph->id = 0;
|
|
|
|
niph->frag_off = htons(IP_DF);
|
2014-10-25 16:24:57 +00:00
|
|
|
niph->protocol = protocol;
|
2014-09-26 12:35:15 +00:00
|
|
|
niph->check = 0;
|
|
|
|
niph->saddr = oiph->daddr;
|
|
|
|
niph->daddr = oiph->saddr;
|
2014-10-25 16:24:57 +00:00
|
|
|
niph->ttl = ttl;
|
|
|
|
|
|
|
|
nskb->protocol = htons(ETH_P_IP);
|
|
|
|
|
|
|
|
return niph;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_reject_iphdr_put);
|
|
|
|
|
|
|
|
void nf_reject_ip_tcphdr_put(struct sk_buff *nskb, const struct sk_buff *oldskb,
|
|
|
|
const struct tcphdr *oth)
|
|
|
|
{
|
|
|
|
struct iphdr *niph = ip_hdr(nskb);
|
|
|
|
struct tcphdr *tcph;
|
2014-09-26 12:35:15 +00:00
|
|
|
|
|
|
|
skb_reset_transport_header(nskb);
|
networking: convert many more places to skb_put_zero()
There were many places that my previous spatch didn't find,
as pointed out by yuan linyu in various patches.
The following spatch found many more and also removes the
now unnecessary casts:
@@
identifier p, p2;
expression len;
expression skb;
type t, t2;
@@
(
-p = skb_put(skb, len);
+p = skb_put_zero(skb, len);
|
-p = (t)skb_put(skb, len);
+p = skb_put_zero(skb, len);
)
... when != p
(
p2 = (t2)p;
-memset(p2, 0, len);
|
-memset(p, 0, len);
)
@@
type t, t2;
identifier p, p2;
expression skb;
@@
t *p;
...
(
-p = skb_put(skb, sizeof(t));
+p = skb_put_zero(skb, sizeof(t));
|
-p = (t *)skb_put(skb, sizeof(t));
+p = skb_put_zero(skb, sizeof(t));
)
... when != p
(
p2 = (t2)p;
-memset(p2, 0, sizeof(*p));
|
-memset(p, 0, sizeof(*p));
)
@@
expression skb, len;
@@
-memset(skb_put(skb, len), 0, len);
+skb_put_zero(skb, len);
Apply it to the tree (with one manual fixup to keep the
comment in vxlan.c, which spatch removed.)
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-06-16 12:29:19 +00:00
|
|
|
tcph = skb_put_zero(nskb, sizeof(struct tcphdr));
|
2014-09-26 12:35:15 +00:00
|
|
|
tcph->source = oth->dest;
|
|
|
|
tcph->dest = oth->source;
|
|
|
|
tcph->doff = sizeof(struct tcphdr) / 4;
|
|
|
|
|
2014-10-25 16:24:57 +00:00
|
|
|
if (oth->ack) {
|
2014-09-26 12:35:15 +00:00
|
|
|
tcph->seq = oth->ack_seq;
|
2014-10-25 16:24:57 +00:00
|
|
|
} else {
|
2014-09-26 12:35:15 +00:00
|
|
|
tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
|
|
|
|
oldskb->len - ip_hdrlen(oldskb) -
|
|
|
|
(oth->doff << 2));
|
|
|
|
tcph->ack = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcph->rst = 1;
|
|
|
|
tcph->check = ~tcp_v4_check(sizeof(struct tcphdr), niph->saddr,
|
|
|
|
niph->daddr, 0);
|
|
|
|
nskb->ip_summed = CHECKSUM_PARTIAL;
|
|
|
|
nskb->csum_start = (unsigned char *)tcph - nskb->head;
|
|
|
|
nskb->csum_offset = offsetof(struct tcphdr, check);
|
2014-10-25 16:24:57 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_reject_ip_tcphdr_put);
|
|
|
|
|
|
|
|
/* Send RST reply */
|
2015-09-25 20:07:27 +00:00
|
|
|
void nf_send_reset(struct net *net, struct sk_buff *oldskb, int hook)
|
2014-10-25 16:24:57 +00:00
|
|
|
{
|
|
|
|
struct sk_buff *nskb;
|
|
|
|
struct iphdr *niph;
|
|
|
|
const struct tcphdr *oth;
|
|
|
|
struct tcphdr _oth;
|
|
|
|
|
|
|
|
oth = nf_reject_ip_tcphdr_get(oldskb, &_oth, hook);
|
|
|
|
if (!oth)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (skb_rtable(oldskb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
|
|
|
|
return;
|
|
|
|
|
|
|
|
nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
|
|
|
|
LL_MAX_HEADER, GFP_ATOMIC);
|
|
|
|
if (!nskb)
|
|
|
|
return;
|
2014-09-26 12:35:15 +00:00
|
|
|
|
|
|
|
/* ip_route_me_harder expects skb->dst to be set */
|
|
|
|
skb_dst_set_noref(nskb, skb_dst(oldskb));
|
|
|
|
|
2017-01-06 19:33:27 +00:00
|
|
|
nskb->mark = IP4_REPLY_MARK(net, oldskb->mark);
|
|
|
|
|
2014-10-25 16:24:57 +00:00
|
|
|
skb_reserve(nskb, LL_MAX_HEADER);
|
|
|
|
niph = nf_reject_iphdr_put(nskb, oldskb, IPPROTO_TCP,
|
|
|
|
ip4_dst_hoplimit(skb_dst(nskb)));
|
|
|
|
nf_reject_ip_tcphdr_put(nskb, oldskb, oth);
|
|
|
|
|
2015-09-25 20:07:30 +00:00
|
|
|
if (ip_route_me_harder(net, nskb, RTN_UNSPEC))
|
2014-09-26 12:35:15 +00:00
|
|
|
goto free_nskb;
|
|
|
|
|
|
|
|
/* "Never happens" */
|
|
|
|
if (nskb->len > dst_mtu(skb_dst(nskb)))
|
|
|
|
goto free_nskb;
|
|
|
|
|
|
|
|
nf_ct_attach(nskb, oldskb);
|
|
|
|
|
|
|
|
#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
|
|
|
|
/* If we use ip_local_out for bridged traffic, the MAC source on
|
|
|
|
* the RST will be ours, instead of the destination's. This confuses
|
|
|
|
* some routers/firewalls, and they drop the packet. So we need to
|
|
|
|
* build the eth header using the original destination's MAC as the
|
|
|
|
* source, and send the RST packet directly.
|
|
|
|
*/
|
|
|
|
if (oldskb->nf_bridge) {
|
|
|
|
struct ethhdr *oeth = eth_hdr(oldskb);
|
2015-04-02 12:31:41 +00:00
|
|
|
|
|
|
|
nskb->dev = nf_bridge_get_physindev(oldskb);
|
2014-09-26 12:35:15 +00:00
|
|
|
niph->tot_len = htons(nskb->len);
|
|
|
|
ip_send_check(niph);
|
|
|
|
if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
|
|
|
|
oeth->h_source, oeth->h_dest, nskb->len) < 0)
|
|
|
|
goto free_nskb;
|
|
|
|
dev_queue_xmit(nskb);
|
|
|
|
} else
|
|
|
|
#endif
|
2015-10-07 21:48:46 +00:00
|
|
|
ip_local_out(net, nskb->sk, nskb);
|
2014-09-26 12:35:15 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
free_nskb:
|
|
|
|
kfree_skb(nskb);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_send_reset);
|
2014-10-10 09:25:20 +00:00
|
|
|
|
2015-02-16 17:54:04 +00:00
|
|
|
void nf_send_unreach(struct sk_buff *skb_in, int code, int hook)
|
|
|
|
{
|
|
|
|
struct iphdr *iph = ip_hdr(skb_in);
|
|
|
|
u8 proto;
|
|
|
|
|
2017-05-18 13:44:39 +00:00
|
|
|
if (iph->frag_off & htons(IP_OFFSET))
|
2015-02-16 17:54:04 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (skb_csum_unnecessary(skb_in)) {
|
|
|
|
icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP)
|
|
|
|
proto = iph->protocol;
|
|
|
|
else
|
|
|
|
proto = 0;
|
|
|
|
|
|
|
|
if (nf_ip_checksum(skb_in, hook, ip_hdrlen(skb_in), proto) == 0)
|
|
|
|
icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_send_unreach);
|
|
|
|
|
2014-10-10 09:25:20 +00:00
|
|
|
MODULE_LICENSE("GPL");
|