Skip to content

Commit

Permalink
xt_qtaguid: finish the work and cleanup...
Browse files Browse the repository at this point in the history
the different part is put in xt_qtaguid_sockets.c
2.6.32.9 miss the xt_socket_get_sk functions

Previous version was not tracking correctly the sockets

Change-Id: I68a10fd4238d491e9e854a088ad8b45617e162b4
  • Loading branch information
tpruvot committed May 26, 2012
1 parent 898f243 commit 92228e2
Show file tree
Hide file tree
Showing 8 changed files with 425 additions and 89 deletions.
4 changes: 2 additions & 2 deletions modules/netfilter/Makefile
@@ -1,13 +1,13 @@
# quota2
obj-m += xt_quota2.o

# qtaguid
# qtaguid for ICS usage stats
ccflags-y += -UCONFIG_GENERIC_ATOMIC64
ccflags-y += -UDDEBUG

obj-m += xt_owner2.o

xt_owner2-objs := xt_qtaguid.o xt_qtaguid_print.o
xt_owner2-objs := xt_qtaguid.o xt_qtaguid_print.o xt_qtaguid_sockets.o

# Previous (stub and migration tests)
#xt_owner2-objs += xt_qtafake.o xt_qtaguid_print.o
Expand Down
210 changes: 210 additions & 0 deletions modules/netfilter/nf_tproxy_core.h
@@ -0,0 +1,210 @@
#ifndef _NF_TPROXY_CORE_H
#define _NF_TPROXY_CORE_H

#include <linux/types.h>
#include <linux/in.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <net/inet_hashtables.h>
#include <net/inet6_hashtables.h>
#include <net/tcp.h>

#define NFT_LOOKUP_ANY 0
#define NFT_LOOKUP_LISTENER 1
#define NFT_LOOKUP_ESTABLISHED 2

/* look up and get a reference to a matching socket */


/* This function is used by the 'TPROXY' target and the 'socket'
* match. The following lookups are supported:
*
* Explicit TProxy target rule
* ===========================
*
* This is used when the user wants to intercept a connection matching
* an explicit iptables rule. In this case the sockets are assumed
* matching in preference order:
*
* - match: if there's a fully established connection matching the
* _packet_ tuple, it is returned, assuming the redirection
* already took place and we process a packet belonging to an
* established connection
*
* - match: if there's a listening socket matching the redirection
* (e.g. on-port & on-ip of the connection), it is returned,
* regardless if it was bound to 0.0.0.0 or an explicit
* address. The reasoning is that if there's an explicit rule, it
* does not really matter if the listener is bound to an interface
* or to 0. The user already stated that he wants redirection
* (since he added the rule).
*
* "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.
*
* Please note that there's an overlap between what a TPROXY target
* and a socket match will match. Normally if you have both rules the
* "socket" match will be the first one, effectively all packets
* belonging to established connections going through that one.
*/
static inline struct sock *
nf_tproxy_get_sock_v4(struct net *net, const u8 protocol,
const __be32 saddr, const __be32 daddr,
const __be16 sport, const __be16 dport,
const struct net_device *in, int lookup_type)
{
struct sock *sk;

/* look up socket */
switch (protocol) {
case IPPROTO_TCP:
switch (lookup_type) {
case NFT_LOOKUP_ANY:
sk = __inet_lookup(net, &tcp_hashinfo,
saddr, sport, daddr, dport,
in->ifindex);
break;
case NFT_LOOKUP_LISTENER:
sk = inet_lookup_listener(net, &tcp_hashinfo,
daddr, dport,
in->ifindex);

/* NOTE: we return listeners even if bound to
* 0.0.0.0, those are filtered out in
* xt_socket, since xt_TPROXY needs 0 bound
* listeners too */

break;
case NFT_LOOKUP_ESTABLISHED:
sk = inet_lookup_established(net, &tcp_hashinfo,
saddr, sport, daddr, dport,
in->ifindex);
break;
default:
WARN_ON(1);
sk = NULL;
break;
}
break;
case IPPROTO_UDP:
sk = udp4_lib_lookup(net, saddr, sport, daddr, dport,
in->ifindex);
if (sk && lookup_type != NFT_LOOKUP_ANY) {
int connected = (sk->sk_state == TCP_ESTABLISHED);
int wildcard = (inet_sk(sk)->rcv_saddr == 0);

/* NOTE: we return listeners even if bound to
* 0.0.0.0, those are filtered out in
* xt_socket, since xt_TPROXY needs 0 bound
* listeners too */
if ((lookup_type == NFT_LOOKUP_ESTABLISHED && (!connected || wildcard)) ||
(lookup_type == NFT_LOOKUP_LISTENER && connected)) {
sock_put(sk);
sk = NULL;
}
}
break;
default:
WARN_ON(1);
sk = NULL;
}

pr_debug("tproxy socket lookup: proto %u %08x:%u -> %08x:%u, lookup type: %d, sock %p\n",
protocol, ntohl(saddr), ntohs(sport), ntohl(daddr), ntohs(dport), lookup_type, sk);

return sk;
}

#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
static inline struct sock *
nf_tproxy_get_sock_v6(struct net *net, const u8 protocol,
const struct in6_addr *saddr, const struct in6_addr *daddr,
const __be16 sport, const __be16 dport,
const struct net_device *in, int lookup_type)
{
struct sock *sk;

/* look up socket */
switch (protocol) {
case IPPROTO_TCP:
switch (lookup_type) {
case NFT_LOOKUP_ANY:
sk = inet6_lookup(net, &tcp_hashinfo,
saddr, sport, daddr, dport,
in->ifindex);
break;
case NFT_LOOKUP_LISTENER:
sk = inet6_lookup_listener(net, &tcp_hashinfo,
daddr, ntohs(dport),
in->ifindex);

/* NOTE: we return listeners even if bound to
* 0.0.0.0, those are filtered out in
* xt_socket, since xt_TPROXY needs 0 bound
* listeners too */

break;
case NFT_LOOKUP_ESTABLISHED:
sk = __inet6_lookup_established(net, &tcp_hashinfo,
saddr, sport, daddr, ntohs(dport),
in->ifindex);
break;
default:
WARN_ON(1);
sk = NULL;
break;
}
break;
#if 0
case IPPROTO_UDP:
sk = udp6_lib_lookup(net, saddr, sport, daddr, dport,
in->ifindex);
if (sk && lookup_type != NFT_LOOKUP_ANY) {
int connected = (sk->sk_state == TCP_ESTABLISHED);
int wildcard = ipv6_addr_any(&inet6_sk(sk)->rcv_saddr);

/* NOTE: we return listeners even if bound to
* 0.0.0.0, those are filtered out in
* xt_socket, since xt_TPROXY needs 0 bound
* listeners too */
if ((lookup_type == NFT_LOOKUP_ESTABLISHED && (!connected || wildcard)) ||
(lookup_type == NFT_LOOKUP_LISTENER && connected)) {
sock_put(sk);
sk = NULL;
}
}
break;
#endif
default:
//WARN_ON(1);
sk = NULL;
}

pr_debug("tproxy socket lookup: proto %u %pI6:%u -> %pI6:%u, lookup type: %d, sock %p\n",
protocol, saddr, ntohs(sport), daddr, ntohs(dport), lookup_type, sk);

return sk;
}
#endif

/* assign a socket to the skb -- consumes sk */
void
nf_tproxy_assign_sock(struct sk_buff *skb, struct sock *sk);

#endif
89 changes: 7 additions & 82 deletions modules/netfilter/xt_qtaguid.c
Expand Up @@ -15,7 +15,6 @@
#define DEBUG
#define TAG "qtaguid"

#include <linux/types.h>
#include "atomic.h"

#include <linux/module.h>
Expand All @@ -25,7 +24,6 @@
#include <linux/file.h>
#include <linux/inetdevice.h>
#include <linux/netfilter/x_tables.h>
//#include <linux/netfilter/xt_qtaguid.h>
#include <linux/skbuff.h>
#include <linux/miscdevice.h>
#include <linux/workqueue.h>
Expand All @@ -34,24 +32,17 @@
#include <net/tcp.h>
#include <net/udp.h>

/* TODO: We dont have all the ip6_tables in our kernel (ipv6_find_hdr) */
#include <linux/netfilter_ipv6/ip6_tables.h>

#include <linux/netfilter/xt_socket.h>
#include "xt_qtaguid_internal.h"
#include "xt_qtaguid_print.h"


/* Compat */
#define pr_warn_once pr_warning
#define xt_action_param xt_match_param
#define xt_socket_put_sk(sk) if (sk != NULL) sock_put(sk);
#define xt_socket_put_sk(sk) qtaguid_put_sk(sk)

/*
* We only use the xt_socket funcs within a similar context to avoid unexpected
* return values.
*/
#define XT_SOCKET_SUPPORTED_HOOKS \
((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_IN))
/* in a separate sockets file */
struct sock *qtaguid_find_sk(const struct sk_buff *skb, const struct xt_match_param *par);
void qtaguid_put_sk(struct sock *sk);


static const char *module_procdirname = "xt_qtaguid";
Expand Down Expand Up @@ -1505,74 +1496,9 @@ static int __init iface_stat_init(struct proc_dir_entry *parent_procdir)
return err;
}

/* static struct sock * qtaguid_find_sk(const struct sk_buff *skb, struct xt_action_param *par) */
static struct sock *qtaguid_find_sk(const struct sk_buff *skb, const struct xt_match_param *par)
{
const struct iphdr *iph = NULL;
struct ipv6hdr *iph6 = NULL;
struct udphdr _hdr, *hp;
int tproto = 0, thoff;
struct sock *sk = NULL;
unsigned int hook_type = (1 << par->hooknum);

MT_DEBUG(TAG": find_sk(skb=%p) hooknum=%d family=%d\n", skb,
par->hooknum, par->family);

/*
* Let's not abuse the the xt_socket_get*_sk(), or else it will
* return garbage SKs.
*/
if (!(hook_type & XT_SOCKET_SUPPORTED_HOOKS))
return NULL;

switch (par->family) {
case NFPROTO_IPV4:
/* sk = xt_socket_get4_sk(skb, par); */
iph = ip_hdr(skb);
if (iph->protocol == IPPROTO_UDP || iph->protocol == IPPROTO_TCP) {
hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
pr_info(TAG": IPV4(%u) packet hdr=%p\n", par->family, hp);
}
break;
case NFPROTO_IPV6:
/* sk = xt_socket_get6_sk(skb, par); */
iph6 = ipv6_hdr(skb);
//TODO: ipv6_find_tlv is available, but not ipv6_find_hdr
//tproto = ipv6_find_hdr(skb, &thoff, -1, NULL);
if (tproto == IPPROTO_UDP || tproto == IPPROTO_TCP) {
hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);

/* saddr = &iph6->saddr;
sport = hp->source;
daddr = &iph6->daddr;
dport = hp->dest;
*/
pr_info(TAG": IPV6(%u) packet hdr=%p\n", par->family, iph6);
}
break;
default:
pr_warning(TAG": unknown protocol %u\n", par->family);
}

/*
* Seems to be issues on the file ptr for TCP_TIME_WAIT SKs.
* http://kerneltrap.org/mailarchive/linux-netdev/2010/10/21/6287959
* Not fixed in 3.0-r3 :(
*/
if (sk) {
MT_DEBUG(TAG": %p->sk_proto=%u "
"->sk_state=%d\n", sk, sk->sk_protocol, sk->sk_state);
if (sk->sk_state == TCP_TIME_WAIT) {
xt_socket_put_sk(sk);
sk = NULL;
}
}
return sk;
}

static void account_for_uid(const struct sk_buff *skb,
struct sock *alternate_sk, uid_t uid,
const struct xt_action_param *par)
const struct xt_match_param *par)
{
const struct net_device *el_dev;

Expand Down Expand Up @@ -1608,7 +1534,6 @@ static void account_for_uid(const struct sk_buff *skb,
}
}

/* static bool qtaguid_mt(const struct sk_buff *skb, struct xt_action_param *par) */
static bool qtaguid_mt(const struct sk_buff *skb, const struct xt_match_param *par)
{
const struct xt_qtaguid_match_info *info = par->matchinfo;
Expand Down Expand Up @@ -2816,7 +2741,7 @@ MODULE_AUTHOR("jpa <jpa@google.com>");
MODULE_AUTHOR("Backported-by: Tanguy Pruvot <tpruvot@github>");
MODULE_DESCRIPTION("Xtables: socket owner+tag matching and associated stats");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0");
MODULE_VERSION("1.1");
MODULE_ALIAS("ipt_owner");
MODULE_ALIAS("ip6t_owner");
MODULE_ALIAS("ipt_qtaguid");
Expand Down
11 changes: 9 additions & 2 deletions modules/netfilter/xt_qtaguid_internal.h
Expand Up @@ -15,7 +15,6 @@
#include <linux/spinlock_types.h>
#include <linux/workqueue.h>

#undef XT_SOCKET_HAVE_IPV6
#define rtnl_link_stats64 net_device_stats

#ifndef _LINUX_ANDROID_AID_H
Expand Down Expand Up @@ -356,15 +355,23 @@ static inline long __must_check IS_ERR_OR_NULL(const void *ptr)
return !ptr || IS_ERR_VALUE((unsigned long)ptr);
}

/*----------------------------------------------*/

#ifdef TAG

/* TAG is defined before the includes only in main module file */
unsigned int qtaguid_debug_mask = DEFAULT_DEBUG_MASK;
unsigned int qtaguid_get_debug_mask(void) {
return qtaguid_debug_mask;
}
EXPORT_SYMBOL(qtaguid_get_debug_mask);

#else

extern unsigned int qtaguid_get_debug_mask(void);
#endif
#define qtaguid_debug_mask qtaguid_get_debug_mask()

#endif /* TAG */

/*----------------------------------------------*/
#endif /* ifndef __XT_QTAGUID_INTERNAL_H__ */

0 comments on commit 92228e2

Please sign in to comment.