2008-10-08 09:35:12 +00:00
|
|
|
/*
|
|
|
|
* Transparent proxy support for Linux/iptables
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007-2008 BalaBit IT Ltd.
|
|
|
|
* Author: Krisztian Kovacs
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2010-03-19 20:08:16 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
2008-10-08 09:35:12 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/netfilter/x_tables.h>
|
|
|
|
#include <linux/netfilter_ipv4/ip_tables.h>
|
|
|
|
#include <net/tcp.h>
|
|
|
|
#include <net/udp.h>
|
|
|
|
#include <net/icmp.h>
|
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/inet_sock.h>
|
|
|
|
#include <net/netfilter/ipv4/nf_defrag_ipv4.h>
|
2010-10-24 23:38:32 +00:00
|
|
|
|
2011-12-12 02:58:24 +00:00
|
|
|
#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
|
2010-10-24 23:38:32 +00:00
|
|
|
#define XT_SOCKET_HAVE_IPV6 1
|
|
|
|
#include <linux/netfilter_ipv6/ip6_tables.h>
|
2013-07-29 13:41:53 +00:00
|
|
|
#include <net/inet6_hashtables.h>
|
2010-10-21 14:19:42 +00:00
|
|
|
#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
|
2010-10-24 23:38:32 +00:00
|
|
|
#endif
|
2008-10-08 09:35:12 +00:00
|
|
|
|
2009-06-09 13:16:34 +00:00
|
|
|
#include <linux/netfilter/xt_socket.h>
|
|
|
|
|
2011-12-12 02:58:24 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NF_CONNTRACK)
|
2008-10-08 09:35:12 +00:00
|
|
|
#define XT_SOCKET_HAVE_CONNTRACK 1
|
|
|
|
#include <net/netfilter/nf_conntrack.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int
|
2010-10-21 14:19:42 +00:00
|
|
|
extract_icmp4_fields(const struct sk_buff *skb,
|
2008-10-08 09:35:12 +00:00
|
|
|
u8 *protocol,
|
|
|
|
__be32 *raddr,
|
|
|
|
__be32 *laddr,
|
|
|
|
__be16 *rport,
|
|
|
|
__be16 *lport)
|
|
|
|
{
|
|
|
|
unsigned int outside_hdrlen = ip_hdrlen(skb);
|
|
|
|
struct iphdr *inside_iph, _inside_iph;
|
|
|
|
struct icmphdr *icmph, _icmph;
|
|
|
|
__be16 *ports, _ports[2];
|
|
|
|
|
|
|
|
icmph = skb_header_pointer(skb, outside_hdrlen,
|
|
|
|
sizeof(_icmph), &_icmph);
|
|
|
|
if (icmph == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
switch (icmph->type) {
|
|
|
|
case ICMP_DEST_UNREACH:
|
|
|
|
case ICMP_SOURCE_QUENCH:
|
|
|
|
case ICMP_REDIRECT:
|
|
|
|
case ICMP_TIME_EXCEEDED:
|
|
|
|
case ICMP_PARAMETERPROB:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inside_iph = skb_header_pointer(skb, outside_hdrlen +
|
|
|
|
sizeof(struct icmphdr),
|
|
|
|
sizeof(_inside_iph), &_inside_iph);
|
|
|
|
if (inside_iph == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (inside_iph->protocol != IPPROTO_TCP &&
|
|
|
|
inside_iph->protocol != IPPROTO_UDP)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
ports = skb_header_pointer(skb, outside_hdrlen +
|
|
|
|
sizeof(struct icmphdr) +
|
|
|
|
(inside_iph->ihl << 2),
|
|
|
|
sizeof(_ports), &_ports);
|
|
|
|
if (ports == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* the inside IP packet is the one quoted from our side, thus
|
|
|
|
* its saddr is the local address */
|
|
|
|
*protocol = inside_iph->protocol;
|
|
|
|
*laddr = inside_iph->saddr;
|
|
|
|
*lport = ports[0];
|
|
|
|
*raddr = inside_iph->daddr;
|
|
|
|
*rport = ports[1];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-29 13:41:53 +00:00
|
|
|
/* "socket" match based redirection (no specific rule)
|
|
|
|
* ===================================================
|
|
|
|
*
|
|
|
|
* There are connections with dynamic endpoints (e.g. FTP data
|
|
|
|
* connection) that the user is unable to add explicit rules
|
|
|
|
* for. These are taken care of by a generic "socket" rule. It is
|
|
|
|
* assumed that the proxy application is trusted to open such
|
|
|
|
* connections without explicit iptables rule (except of course the
|
|
|
|
* generic 'socket' rule). In this case the following sockets are
|
|
|
|
* matched in preference order:
|
|
|
|
*
|
|
|
|
* - match: if there's a fully established connection matching the
|
|
|
|
* _packet_ tuple
|
|
|
|
*
|
|
|
|
* - match: if there's a non-zero bound listener (possibly with a
|
|
|
|
* non-local address) We don't accept zero-bound listeners, since
|
|
|
|
* then local services could intercept traffic going through the
|
|
|
|
* box.
|
|
|
|
*/
|
|
|
|
static struct sock *
|
2016-02-10 16:50:38 +00:00
|
|
|
xt_socket_get_sock_v4(struct net *net, struct sk_buff *skb, const int doff,
|
|
|
|
const u8 protocol,
|
2013-07-29 13:41:53 +00:00
|
|
|
const __be32 saddr, const __be32 daddr,
|
|
|
|
const __be16 sport, const __be16 dport,
|
|
|
|
const struct net_device *in)
|
|
|
|
{
|
|
|
|
switch (protocol) {
|
|
|
|
case IPPROTO_TCP:
|
2016-04-01 15:52:17 +00:00
|
|
|
return inet_lookup(net, &tcp_hashinfo, skb, doff,
|
|
|
|
saddr, sport, daddr, dport,
|
|
|
|
in->ifindex);
|
2013-07-29 13:41:53 +00:00
|
|
|
case IPPROTO_UDP:
|
|
|
|
return udp4_lib_lookup(net, saddr, sport, daddr, dport,
|
|
|
|
in->ifindex);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-03-17 04:06:17 +00:00
|
|
|
static bool xt_socket_sk_is_transparent(struct sock *sk)
|
|
|
|
{
|
|
|
|
switch (sk->sk_state) {
|
|
|
|
case TCP_TIME_WAIT:
|
|
|
|
return inet_twsk(sk)->tw_transparent;
|
|
|
|
|
|
|
|
case TCP_NEW_SYN_RECV:
|
|
|
|
return inet_rsk(inet_reqsk(sk))->no_srccheck;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return inet_sk(sk)->transparent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 19:32:59 +00:00
|
|
|
static struct sock *xt_socket_lookup_slow_v4(struct net *net,
|
|
|
|
const struct sk_buff *skb,
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
const struct net_device *indev)
|
2008-10-08 09:35:12 +00:00
|
|
|
{
|
|
|
|
const struct iphdr *iph = ip_hdr(skb);
|
2016-02-10 16:50:38 +00:00
|
|
|
struct sk_buff *data_skb = NULL;
|
|
|
|
int doff = 0;
|
2012-08-29 15:58:29 +00:00
|
|
|
__be32 uninitialized_var(daddr), uninitialized_var(saddr);
|
|
|
|
__be16 uninitialized_var(dport), uninitialized_var(sport);
|
|
|
|
u8 uninitialized_var(protocol);
|
2008-10-08 09:35:12 +00:00
|
|
|
#ifdef XT_SOCKET_HAVE_CONNTRACK
|
|
|
|
struct nf_conn const *ct;
|
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (iph->protocol == IPPROTO_UDP || iph->protocol == IPPROTO_TCP) {
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
struct udphdr _hdr, *hp;
|
|
|
|
|
2008-10-08 09:35:12 +00:00
|
|
|
hp = skb_header_pointer(skb, ip_hdrlen(skb),
|
|
|
|
sizeof(_hdr), &_hdr);
|
|
|
|
if (hp == NULL)
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
return NULL;
|
2008-10-08 09:35:12 +00:00
|
|
|
|
|
|
|
protocol = iph->protocol;
|
|
|
|
saddr = iph->saddr;
|
|
|
|
sport = hp->source;
|
|
|
|
daddr = iph->daddr;
|
|
|
|
dport = hp->dest;
|
2016-02-10 16:50:38 +00:00
|
|
|
data_skb = (struct sk_buff *)skb;
|
|
|
|
doff = iph->protocol == IPPROTO_TCP ?
|
|
|
|
ip_hdrlen(skb) + __tcp_hdrlen((struct tcphdr *)hp) :
|
|
|
|
ip_hdrlen(skb) + sizeof(*hp);
|
2008-10-08 09:35:12 +00:00
|
|
|
|
|
|
|
} else if (iph->protocol == IPPROTO_ICMP) {
|
2010-10-21 14:19:42 +00:00
|
|
|
if (extract_icmp4_fields(skb, &protocol, &saddr, &daddr,
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
&sport, &dport))
|
|
|
|
return NULL;
|
2008-10-08 09:35:12 +00:00
|
|
|
} else {
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
return NULL;
|
2008-10-08 09:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XT_SOCKET_HAVE_CONNTRACK
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
/* Do the lookup with the original socket address in
|
|
|
|
* case this is a reply packet of an established
|
|
|
|
* SNAT-ted connection.
|
|
|
|
*/
|
2008-10-08 09:35:12 +00:00
|
|
|
ct = nf_ct_get(skb, &ctinfo);
|
2010-06-08 14:09:52 +00:00
|
|
|
if (ct && !nf_ct_is_untracked(ct) &&
|
2008-10-08 09:35:12 +00:00
|
|
|
((iph->protocol != IPPROTO_ICMP &&
|
2011-05-19 13:44:27 +00:00
|
|
|
ctinfo == IP_CT_ESTABLISHED_REPLY) ||
|
2008-10-08 09:35:12 +00:00
|
|
|
(iph->protocol == IPPROTO_ICMP &&
|
2011-05-19 13:44:27 +00:00
|
|
|
ctinfo == IP_CT_RELATED_REPLY)) &&
|
2008-10-08 09:35:12 +00:00
|
|
|
(ct->status & IPS_SRC_NAT_DONE)) {
|
|
|
|
|
|
|
|
daddr = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
|
|
|
|
dport = (iph->protocol == IPPROTO_TCP) ?
|
|
|
|
ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.tcp.port :
|
|
|
|
ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.udp.port;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-02-10 16:50:38 +00:00
|
|
|
return xt_socket_get_sock_v4(net, data_skb, doff, protocol, saddr,
|
|
|
|
daddr, sport, dport, indev);
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
socket_match(const struct sk_buff *skb, struct xt_action_param *par,
|
|
|
|
const struct xt_socket_mtinfo1 *info)
|
|
|
|
{
|
2015-06-16 00:40:43 +00:00
|
|
|
struct sk_buff *pskb = (struct sk_buff *)skb;
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
struct sock *sk = skb->sk;
|
|
|
|
|
2013-05-22 11:01:06 +00:00
|
|
|
if (!sk)
|
2015-09-18 19:32:59 +00:00
|
|
|
sk = xt_socket_lookup_slow_v4(par->net, skb, par->in);
|
2013-05-22 11:01:06 +00:00
|
|
|
if (sk) {
|
2009-06-09 13:16:34 +00:00
|
|
|
bool wildcard;
|
|
|
|
bool transparent = true;
|
|
|
|
|
2013-06-20 12:52:22 +00:00
|
|
|
/* Ignore sockets listening on INADDR_ANY,
|
|
|
|
* unless XT_SOCKET_NOWILDCARD is set
|
|
|
|
*/
|
|
|
|
wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
|
2015-03-17 04:06:17 +00:00
|
|
|
sk_fullsock(sk) &&
|
2009-10-15 06:30:45 +00:00
|
|
|
inet_sk(sk)->inet_rcv_saddr == 0);
|
2009-06-09 13:16:34 +00:00
|
|
|
|
|
|
|
/* Ignore non-transparent sockets,
|
2015-03-17 04:06:17 +00:00
|
|
|
* if XT_SOCKET_TRANSPARENT is used
|
|
|
|
*/
|
2013-07-12 02:22:19 +00:00
|
|
|
if (info->flags & XT_SOCKET_TRANSPARENT)
|
2015-03-17 04:06:17 +00:00
|
|
|
transparent = xt_socket_sk_is_transparent(sk);
|
2008-10-08 09:35:12 +00:00
|
|
|
|
2015-06-16 00:40:43 +00:00
|
|
|
if (info->flags & XT_SOCKET_RESTORESKMARK && !wildcard &&
|
|
|
|
transparent)
|
|
|
|
pskb->mark = sk->sk_mark;
|
|
|
|
|
2013-05-22 11:01:06 +00:00
|
|
|
if (sk != skb->sk)
|
2013-10-11 16:03:25 +00:00
|
|
|
sock_gen_put(sk);
|
2009-06-09 13:16:34 +00:00
|
|
|
|
|
|
|
if (wildcard || !transparent)
|
2008-10-08 09:35:12 +00:00
|
|
|
sk = NULL;
|
|
|
|
}
|
|
|
|
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
return sk != NULL;
|
2008-10-08 09:35:12 +00:00
|
|
|
}
|
|
|
|
|
2009-06-09 13:16:34 +00:00
|
|
|
static bool
|
2010-10-21 14:19:42 +00:00
|
|
|
socket_mt4_v0(const struct sk_buff *skb, struct xt_action_param *par)
|
2009-06-09 13:16:34 +00:00
|
|
|
{
|
2013-07-12 02:22:19 +00:00
|
|
|
static struct xt_socket_mtinfo1 xt_info_v0 = {
|
|
|
|
.flags = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
return socket_match(skb, par, &xt_info_v0);
|
2009-06-09 13:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2015-06-16 00:40:43 +00:00
|
|
|
socket_mt4_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
|
2009-06-09 13:16:34 +00:00
|
|
|
{
|
|
|
|
return socket_match(skb, par, par->matchinfo);
|
|
|
|
}
|
|
|
|
|
2010-10-24 23:38:32 +00:00
|
|
|
#ifdef XT_SOCKET_HAVE_IPV6
|
2010-10-21 14:19:42 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
extract_icmp6_fields(const struct sk_buff *skb,
|
|
|
|
unsigned int outside_hdrlen,
|
2010-10-28 19:59:53 +00:00
|
|
|
int *protocol,
|
2015-02-16 03:03:45 +00:00
|
|
|
const struct in6_addr **raddr,
|
|
|
|
const struct in6_addr **laddr,
|
2010-10-21 14:19:42 +00:00
|
|
|
__be16 *rport,
|
2015-02-16 03:03:45 +00:00
|
|
|
__be16 *lport,
|
|
|
|
struct ipv6hdr *ipv6_var)
|
2010-10-21 14:19:42 +00:00
|
|
|
{
|
2015-02-16 03:03:45 +00:00
|
|
|
const struct ipv6hdr *inside_iph;
|
2010-10-21 14:19:42 +00:00
|
|
|
struct icmp6hdr *icmph, _icmph;
|
|
|
|
__be16 *ports, _ports[2];
|
|
|
|
u8 inside_nexthdr;
|
2011-12-01 01:05:51 +00:00
|
|
|
__be16 inside_fragoff;
|
2010-10-21 14:19:42 +00:00
|
|
|
int inside_hdrlen;
|
|
|
|
|
|
|
|
icmph = skb_header_pointer(skb, outside_hdrlen,
|
|
|
|
sizeof(_icmph), &_icmph);
|
|
|
|
if (icmph == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (icmph->icmp6_type & ICMPV6_INFOMSG_MASK)
|
|
|
|
return 1;
|
|
|
|
|
2015-02-16 03:03:45 +00:00
|
|
|
inside_iph = skb_header_pointer(skb, outside_hdrlen + sizeof(_icmph),
|
|
|
|
sizeof(*ipv6_var), ipv6_var);
|
2010-10-21 14:19:42 +00:00
|
|
|
if (inside_iph == NULL)
|
|
|
|
return 1;
|
|
|
|
inside_nexthdr = inside_iph->nexthdr;
|
|
|
|
|
2015-02-16 03:03:45 +00:00
|
|
|
inside_hdrlen = ipv6_skip_exthdr(skb, outside_hdrlen + sizeof(_icmph) +
|
|
|
|
sizeof(*ipv6_var),
|
2011-12-01 01:05:51 +00:00
|
|
|
&inside_nexthdr, &inside_fragoff);
|
2010-10-21 14:19:42 +00:00
|
|
|
if (inside_hdrlen < 0)
|
|
|
|
return 1; /* hjm: Packet has no/incomplete transport layer headers. */
|
|
|
|
|
|
|
|
if (inside_nexthdr != IPPROTO_TCP &&
|
|
|
|
inside_nexthdr != IPPROTO_UDP)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
ports = skb_header_pointer(skb, inside_hdrlen,
|
|
|
|
sizeof(_ports), &_ports);
|
|
|
|
if (ports == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* the inside IP packet is the one quoted from our side, thus
|
|
|
|
* its saddr is the local address */
|
|
|
|
*protocol = inside_nexthdr;
|
|
|
|
*laddr = &inside_iph->saddr;
|
|
|
|
*lport = ports[0];
|
|
|
|
*raddr = &inside_iph->daddr;
|
|
|
|
*rport = ports[1];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-29 13:41:53 +00:00
|
|
|
static struct sock *
|
2016-02-10 16:50:38 +00:00
|
|
|
xt_socket_get_sock_v6(struct net *net, struct sk_buff *skb, int doff,
|
|
|
|
const u8 protocol,
|
2013-07-29 13:41:53 +00:00
|
|
|
const struct in6_addr *saddr, const struct in6_addr *daddr,
|
|
|
|
const __be16 sport, const __be16 dport,
|
|
|
|
const struct net_device *in)
|
|
|
|
{
|
|
|
|
switch (protocol) {
|
|
|
|
case IPPROTO_TCP:
|
2016-02-10 16:50:38 +00:00
|
|
|
return inet6_lookup(net, &tcp_hashinfo, skb, doff,
|
2013-07-29 13:41:53 +00:00
|
|
|
saddr, sport, daddr, dport,
|
|
|
|
in->ifindex);
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
return udp6_lib_lookup(net, saddr, sport, daddr, dport,
|
|
|
|
in->ifindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-09-18 19:32:59 +00:00
|
|
|
static struct sock *xt_socket_lookup_slow_v6(struct net *net,
|
|
|
|
const struct sk_buff *skb,
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
const struct net_device *indev)
|
2010-10-21 14:19:42 +00:00
|
|
|
{
|
2012-08-29 15:58:29 +00:00
|
|
|
__be16 uninitialized_var(dport), uninitialized_var(sport);
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
const struct in6_addr *daddr = NULL, *saddr = NULL;
|
|
|
|
struct ipv6hdr *iph = ipv6_hdr(skb);
|
2016-02-10 16:50:38 +00:00
|
|
|
struct sk_buff *data_skb = NULL;
|
|
|
|
int doff = 0;
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
int thoff = 0, tproto;
|
2010-10-21 14:19:42 +00:00
|
|
|
|
2012-04-23 03:35:26 +00:00
|
|
|
tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
|
2010-10-21 14:19:42 +00:00
|
|
|
if (tproto < 0) {
|
|
|
|
pr_debug("unable to find transport header in IPv6 packet, dropping\n");
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
return NULL;
|
2010-10-21 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tproto == IPPROTO_UDP || tproto == IPPROTO_TCP) {
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
struct udphdr _hdr, *hp;
|
|
|
|
|
|
|
|
hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
|
2010-10-21 14:19:42 +00:00
|
|
|
if (hp == NULL)
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
return NULL;
|
2010-10-21 14:19:42 +00:00
|
|
|
|
|
|
|
saddr = &iph->saddr;
|
|
|
|
sport = hp->source;
|
|
|
|
daddr = &iph->daddr;
|
|
|
|
dport = hp->dest;
|
2016-02-10 16:50:38 +00:00
|
|
|
data_skb = (struct sk_buff *)skb;
|
|
|
|
doff = tproto == IPPROTO_TCP ?
|
|
|
|
thoff + __tcp_hdrlen((struct tcphdr *)hp) :
|
|
|
|
thoff + sizeof(*hp);
|
2010-10-21 14:19:42 +00:00
|
|
|
|
|
|
|
} else if (tproto == IPPROTO_ICMPV6) {
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
struct ipv6hdr ipv6_var;
|
|
|
|
|
2010-10-21 14:19:42 +00:00
|
|
|
if (extract_icmp6_fields(skb, thoff, &tproto, &saddr, &daddr,
|
2015-02-16 03:03:45 +00:00
|
|
|
&sport, &dport, &ipv6_var))
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
return NULL;
|
2010-10-21 14:19:42 +00:00
|
|
|
} else {
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
return NULL;
|
2010-10-21 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-10 16:50:38 +00:00
|
|
|
return xt_socket_get_sock_v6(net, data_skb, doff, tproto, saddr, daddr,
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
sport, dport, indev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2015-06-16 00:40:43 +00:00
|
|
|
socket_mt6_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
{
|
|
|
|
const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
|
2015-06-16 00:40:43 +00:00
|
|
|
struct sk_buff *pskb = (struct sk_buff *)skb;
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
struct sock *sk = skb->sk;
|
|
|
|
|
2013-05-22 11:01:06 +00:00
|
|
|
if (!sk)
|
2015-09-18 19:32:59 +00:00
|
|
|
sk = xt_socket_lookup_slow_v6(par->net, skb, par->in);
|
2013-05-22 11:01:06 +00:00
|
|
|
if (sk) {
|
2010-10-21 14:19:42 +00:00
|
|
|
bool wildcard;
|
|
|
|
bool transparent = true;
|
|
|
|
|
2013-06-20 12:52:22 +00:00
|
|
|
/* Ignore sockets listening on INADDR_ANY
|
|
|
|
* unless XT_SOCKET_NOWILDCARD is set
|
|
|
|
*/
|
|
|
|
wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
|
2015-03-17 04:06:17 +00:00
|
|
|
sk_fullsock(sk) &&
|
ipv6: make lookups simpler and faster
TCP listener refactoring, part 4 :
To speed up inet lookups, we moved IPv4 addresses from inet to struct
sock_common
Now is time to do the same for IPv6, because it permits us to have fast
lookups for all kind of sockets, including upcoming SYN_RECV.
Getting IPv6 addresses in TCP lookups currently requires two extra cache
lines, plus a dereference (and memory stall).
inet6_sk(sk) does the dereference of inet_sk(__sk)->pinet6
This patch is way bigger than its IPv4 counter part, because for IPv4,
we could add aliases (inet_daddr, inet_rcv_saddr), while on IPv6,
it's not doable easily.
inet6_sk(sk)->daddr becomes sk->sk_v6_daddr
inet6_sk(sk)->rcv_saddr becomes sk->sk_v6_rcv_saddr
And timewait socket also have tw->tw_v6_daddr & tw->tw_v6_rcv_saddr
at the same offset.
We get rid of INET6_TW_MATCH() as INET6_MATCH() is now the generic
macro.
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2013-10-03 22:42:29 +00:00
|
|
|
ipv6_addr_any(&sk->sk_v6_rcv_saddr));
|
2010-10-21 14:19:42 +00:00
|
|
|
|
|
|
|
/* Ignore non-transparent sockets,
|
2015-03-17 04:06:17 +00:00
|
|
|
* if XT_SOCKET_TRANSPARENT is used
|
|
|
|
*/
|
2013-07-12 02:22:19 +00:00
|
|
|
if (info->flags & XT_SOCKET_TRANSPARENT)
|
2015-03-17 04:06:17 +00:00
|
|
|
transparent = xt_socket_sk_is_transparent(sk);
|
2010-10-21 14:19:42 +00:00
|
|
|
|
2015-06-16 00:40:43 +00:00
|
|
|
if (info->flags & XT_SOCKET_RESTORESKMARK && !wildcard &&
|
|
|
|
transparent)
|
|
|
|
pskb->mark = sk->sk_mark;
|
|
|
|
|
2013-05-22 11:01:06 +00:00
|
|
|
if (sk != skb->sk)
|
2013-10-11 16:03:25 +00:00
|
|
|
sock_gen_put(sk);
|
2010-10-21 14:19:42 +00:00
|
|
|
|
|
|
|
if (wildcard || !transparent)
|
|
|
|
sk = NULL;
|
|
|
|
}
|
|
|
|
|
netfilter: x_tables: don't extract flow keys on early demuxed sks in socket match
Currently in xt_socket, we take advantage of early demuxed sockets
since commit 00028aa37098 ("netfilter: xt_socket: use IP early demux")
in order to avoid a second socket lookup in the fast path, but we
only make partial use of this:
We still unnecessarily parse headers, extract proto, {s,d}addr and
{s,d}ports from the skb data, accessing possible conntrack information,
etc even though we were not even calling into the socket lookup via
xt_socket_get_sock_{v4,v6}() due to skb->sk hit, meaning those cycles
can be spared.
After this patch, we only proceed the slower, manual lookup path
when we have a skb->sk miss, thus time to match verdict for early
demuxed sockets will improve further, which might be i.e. interesting
for use cases such as mentioned in 681f130f39e1 ("netfilter: xt_socket:
add XT_SOCKET_NOWILDCARD flag").
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-04-02 12:28:30 +00:00
|
|
|
return sk != NULL;
|
2010-10-21 14:19:42 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-20 12:52:22 +00:00
|
|
|
static int socket_mt_v1_check(const struct xt_mtchk_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
|
|
|
|
|
|
|
|
if (info->flags & ~XT_SOCKET_FLAGS_V1) {
|
|
|
|
pr_info("unknown flags 0x%x\n", info->flags & ~XT_SOCKET_FLAGS_V1);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int socket_mt_v2_check(const struct xt_mtchk_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_socket_mtinfo2 *info = (struct xt_socket_mtinfo2 *) par->matchinfo;
|
|
|
|
|
|
|
|
if (info->flags & ~XT_SOCKET_FLAGS_V2) {
|
|
|
|
pr_info("unknown flags 0x%x\n", info->flags & ~XT_SOCKET_FLAGS_V2);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-16 00:40:43 +00:00
|
|
|
static int socket_mt_v3_check(const struct xt_mtchk_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_socket_mtinfo3 *info =
|
|
|
|
(struct xt_socket_mtinfo3 *)par->matchinfo;
|
|
|
|
|
|
|
|
if (info->flags & ~XT_SOCKET_FLAGS_V3) {
|
|
|
|
pr_info("unknown flags 0x%x\n",
|
|
|
|
info->flags & ~XT_SOCKET_FLAGS_V3);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-09 13:16:34 +00:00
|
|
|
static struct xt_match socket_mt_reg[] __read_mostly = {
|
|
|
|
{
|
|
|
|
.name = "socket",
|
|
|
|
.revision = 0,
|
|
|
|
.family = NFPROTO_IPV4,
|
2010-10-21 14:19:42 +00:00
|
|
|
.match = socket_mt4_v0,
|
2009-10-29 14:35:10 +00:00
|
|
|
.hooks = (1 << NF_INET_PRE_ROUTING) |
|
|
|
|
(1 << NF_INET_LOCAL_IN),
|
2009-06-09 13:16:34 +00:00
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "socket",
|
|
|
|
.revision = 1,
|
|
|
|
.family = NFPROTO_IPV4,
|
2015-06-16 00:40:43 +00:00
|
|
|
.match = socket_mt4_v1_v2_v3,
|
2013-06-20 12:52:22 +00:00
|
|
|
.checkentry = socket_mt_v1_check,
|
2009-06-09 13:16:34 +00:00
|
|
|
.matchsize = sizeof(struct xt_socket_mtinfo1),
|
2009-10-29 14:35:10 +00:00
|
|
|
.hooks = (1 << NF_INET_PRE_ROUTING) |
|
|
|
|
(1 << NF_INET_LOCAL_IN),
|
2009-06-09 13:16:34 +00:00
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
2010-10-24 23:38:32 +00:00
|
|
|
#ifdef XT_SOCKET_HAVE_IPV6
|
2010-10-21 14:19:42 +00:00
|
|
|
{
|
|
|
|
.name = "socket",
|
|
|
|
.revision = 1,
|
|
|
|
.family = NFPROTO_IPV6,
|
2015-06-16 00:40:43 +00:00
|
|
|
.match = socket_mt6_v1_v2_v3,
|
2013-06-20 12:52:22 +00:00
|
|
|
.checkentry = socket_mt_v1_check,
|
|
|
|
.matchsize = sizeof(struct xt_socket_mtinfo1),
|
|
|
|
.hooks = (1 << NF_INET_PRE_ROUTING) |
|
|
|
|
(1 << NF_INET_LOCAL_IN),
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
.name = "socket",
|
|
|
|
.revision = 2,
|
|
|
|
.family = NFPROTO_IPV4,
|
2015-06-16 00:40:43 +00:00
|
|
|
.match = socket_mt4_v1_v2_v3,
|
2013-06-20 12:52:22 +00:00
|
|
|
.checkentry = socket_mt_v2_check,
|
|
|
|
.matchsize = sizeof(struct xt_socket_mtinfo1),
|
|
|
|
.hooks = (1 << NF_INET_PRE_ROUTING) |
|
|
|
|
(1 << NF_INET_LOCAL_IN),
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
|
|
|
#ifdef XT_SOCKET_HAVE_IPV6
|
|
|
|
{
|
|
|
|
.name = "socket",
|
|
|
|
.revision = 2,
|
|
|
|
.family = NFPROTO_IPV6,
|
2015-06-16 00:40:43 +00:00
|
|
|
.match = socket_mt6_v1_v2_v3,
|
2013-06-20 12:52:22 +00:00
|
|
|
.checkentry = socket_mt_v2_check,
|
2010-10-21 14:19:42 +00:00
|
|
|
.matchsize = sizeof(struct xt_socket_mtinfo1),
|
|
|
|
.hooks = (1 << NF_INET_PRE_ROUTING) |
|
|
|
|
(1 << NF_INET_LOCAL_IN),
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
2015-06-16 00:40:43 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
.name = "socket",
|
|
|
|
.revision = 3,
|
|
|
|
.family = NFPROTO_IPV4,
|
|
|
|
.match = socket_mt4_v1_v2_v3,
|
|
|
|
.checkentry = socket_mt_v3_check,
|
|
|
|
.matchsize = sizeof(struct xt_socket_mtinfo1),
|
|
|
|
.hooks = (1 << NF_INET_PRE_ROUTING) |
|
|
|
|
(1 << NF_INET_LOCAL_IN),
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
|
|
|
#ifdef XT_SOCKET_HAVE_IPV6
|
|
|
|
{
|
|
|
|
.name = "socket",
|
|
|
|
.revision = 3,
|
|
|
|
.family = NFPROTO_IPV6,
|
|
|
|
.match = socket_mt6_v1_v2_v3,
|
|
|
|
.checkentry = socket_mt_v3_check,
|
|
|
|
.matchsize = sizeof(struct xt_socket_mtinfo1),
|
|
|
|
.hooks = (1 << NF_INET_PRE_ROUTING) |
|
|
|
|
(1 << NF_INET_LOCAL_IN),
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
2010-10-21 14:19:42 +00:00
|
|
|
#endif
|
2008-10-08 09:35:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int __init socket_mt_init(void)
|
|
|
|
{
|
|
|
|
nf_defrag_ipv4_enable();
|
2010-10-24 23:38:32 +00:00
|
|
|
#ifdef XT_SOCKET_HAVE_IPV6
|
2010-10-21 14:19:42 +00:00
|
|
|
nf_defrag_ipv6_enable();
|
|
|
|
#endif
|
|
|
|
|
2009-06-09 13:16:34 +00:00
|
|
|
return xt_register_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
|
2008-10-08 09:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit socket_mt_exit(void)
|
|
|
|
{
|
2009-06-09 13:16:34 +00:00
|
|
|
xt_unregister_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
|
2008-10-08 09:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(socket_mt_init);
|
|
|
|
module_exit(socket_mt_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler");
|
|
|
|
MODULE_DESCRIPTION("x_tables socket match module");
|
|
|
|
MODULE_ALIAS("ipt_socket");
|
2010-10-21 14:19:42 +00:00
|
|
|
MODULE_ALIAS("ip6t_socket");
|