LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] af_unix: implement socket filter
@ 2011-01-18 16:39 Ian Molton
2011-01-18 17:22 ` Eric Dumazet
2011-01-19 5:33 ` David Miller
0 siblings, 2 replies; 7+ messages in thread
From: Ian Molton @ 2011-01-18 16:39 UTC (permalink / raw)
To: netdev
Cc: linux-kernel, davem, eric.dumazet, ebiederm, xemul, davidel,
Alban Crequy
From: Alban Crequy <alban.crequy@collabora.co.uk>
Linux Socket Filters can already be successfully attached and detached on unix
sockets with setsockopt(sockfd, SOL_SOCKET, SO_{ATTACH,DETACH}_FILTER, ...).
See: Documentation/networking/filter.txt
But the filter was never used in the unix socket code so it did not work. This
patch uses sk_filter() to filter buffers before delivery.
This short program demonstrates the problem on SOCK_DGRAM.
int main(void) {
int i, j, ret;
int sv[2];
struct pollfd fds[2];
char *message = "Hello world!";
char buffer[64];
struct sock_filter ins[32] = {{0,},};
struct sock_fprog filter;
socketpair(AF_UNIX, SOCK_DGRAM, 0, sv);
for (i = 0 ; i < 2 ; i++) {
fds[i].fd = sv[i];
fds[i].events = POLLIN;
fds[i].revents = 0;
}
for(j = 1 ; j < 13 ; j++) {
/* Set a socket filter to truncate the message */
memset(ins, 0, sizeof(ins));
ins[0].code = BPF_RET|BPF_K;
ins[0].k = j;
filter.len = 1;
filter.filter = ins;
setsockopt(sv[1], SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
/* send a message */
send(sv[0], message, strlen(message) + 1, 0);
/* The filter should let the message pass but truncated. */
poll(fds, 2, 0);
/* Receive the truncated message*/
ret = recv(sv[1], buffer, 64, 0);
printf("received %d bytes, expected %d\n", ret, j);
}
for (i = 0 ; i < 2 ; i++)
close(sv[i]);
return 0;
}
Signed-off-by: Alban Crequy <alban.crequy@collabora.co.uk>
Reviewed-by: Ian Molton <ian.molton@collabora.co.uk>
---
net/unix/af_unix.c | 6 ++++++
1 files changed, 6 insertions(+), 0 deletions(-)
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index dd419d2..8d9bbba 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -1475,6 +1475,12 @@ restart:
goto out_free;
}
+ if (sk_filter(other, skb) < 0) {
+ /* Toss the packet but do not return any error to the sender */
+ err = len;
+ goto out_free;
+ }
+
unix_state_lock(other);
err = -EPERM;
if (!unix_may_send(sk, other))
--
1.7.2.3
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] af_unix: implement socket filter
2011-01-18 16:39 [PATCH] af_unix: implement socket filter Ian Molton
@ 2011-01-18 17:22 ` Eric Dumazet
2011-01-18 17:46 ` [PATCH] net: filter: dont block softirqs in sk_run_filter() Eric Dumazet
2011-01-18 17:51 ` [PATCH] af_unix: implement socket filter Alban Crequy
2011-01-19 5:33 ` David Miller
1 sibling, 2 replies; 7+ messages in thread
From: Eric Dumazet @ 2011-01-18 17:22 UTC (permalink / raw)
To: Ian Molton
Cc: netdev, linux-kernel, davem, ebiederm, xemul, davidel, Alban Crequy
Le mardi 18 janvier 2011 à 16:39 +0000, Ian Molton a écrit :
> From: Alban Crequy <alban.crequy@collabora.co.uk>
>
> Linux Socket Filters can already be successfully attached and detached on unix
> sockets with setsockopt(sockfd, SOL_SOCKET, SO_{ATTACH,DETACH}_FILTER, ...).
> See: Documentation/networking/filter.txt
>
> But the filter was never used in the unix socket code so it did not work. This
> patch uses sk_filter() to filter buffers before delivery.
>
> This short program demonstrates the problem on SOCK_DGRAM.
Any idea on performance cost adding sk_filter() call ?
Hmm, looking at it, I have no idea why sk_filter() needs to block BH.
I'll send a patch to relax this requirement.
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH] net: filter: dont block softirqs in sk_run_filter()
2011-01-18 17:22 ` Eric Dumazet
@ 2011-01-18 17:46 ` Eric Dumazet
2011-01-19 5:33 ` David Miller
2011-01-18 17:51 ` [PATCH] af_unix: implement socket filter Alban Crequy
1 sibling, 1 reply; 7+ messages in thread
From: Eric Dumazet @ 2011-01-18 17:46 UTC (permalink / raw)
To: David Miller
Cc: netdev, linux-kernel, ebiederm, xemul, davidel, Alban Crequy, Ian Molton
Packet filter (BPF) doesnt need to disable softirqs, being fully
re-entrant and lock-less.
Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com>
---
include/net/sock.h | 2 +-
net/core/filter.c | 6 +++---
net/packet/af_packet.c | 6 +++---
3 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/include/net/sock.h b/include/net/sock.h
index d884d26..ba6465b 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -1189,7 +1189,7 @@ extern void sk_filter_release_rcu(struct rcu_head *rcu);
static inline void sk_filter_release(struct sk_filter *fp)
{
if (atomic_dec_and_test(&fp->refcnt))
- call_rcu_bh(&fp->rcu, sk_filter_release_rcu);
+ call_rcu(&fp->rcu, sk_filter_release_rcu);
}
static inline void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
diff --git a/net/core/filter.c b/net/core/filter.c
index afc5837..232b187 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -142,14 +142,14 @@ int sk_filter(struct sock *sk, struct sk_buff *skb)
if (err)
return err;
- rcu_read_lock_bh();
- filter = rcu_dereference_bh(sk->sk_filter);
+ rcu_read_lock();
+ filter = rcu_dereference(sk->sk_filter);
if (filter) {
unsigned int pkt_len = sk_run_filter(skb, filter->insns);
err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
}
- rcu_read_unlock_bh();
+ rcu_read_unlock();
return err;
}
diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 91cb1d7..c3fc7b7 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -523,11 +523,11 @@ static inline unsigned int run_filter(const struct sk_buff *skb,
{
struct sk_filter *filter;
- rcu_read_lock_bh();
- filter = rcu_dereference_bh(sk->sk_filter);
+ rcu_read_lock();
+ filter = rcu_dereference(sk->sk_filter);
if (filter != NULL)
res = sk_run_filter(skb, filter->insns);
- rcu_read_unlock_bh();
+ rcu_read_unlock();
return res;
}
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] af_unix: implement socket filter
2011-01-18 17:22 ` Eric Dumazet
2011-01-18 17:46 ` [PATCH] net: filter: dont block softirqs in sk_run_filter() Eric Dumazet
@ 2011-01-18 17:51 ` Alban Crequy
2011-01-18 20:19 ` Eric Dumazet
1 sibling, 1 reply; 7+ messages in thread
From: Alban Crequy @ 2011-01-18 17:51 UTC (permalink / raw)
To: Eric Dumazet, Ian Molton
Cc: netdev, linux-kernel, davem, ebiederm, xemul, davidel
Le Tue, 18 Jan 2011 18:22:41 +0100,
Eric Dumazet <eric.dumazet@gmail.com> a écrit :
> Le mardi 18 janvier 2011 à 16:39 +0000, Ian Molton a écrit :
> > From: Alban Crequy <alban.crequy@collabora.co.uk>
> >
> > Linux Socket Filters can already be successfully attached and
> > detached on unix sockets with setsockopt(sockfd, SOL_SOCKET,
> > SO_{ATTACH,DETACH}_FILTER, ....). See:
> > Documentation/networking/filter.txt
> >
> > But the filter was never used in the unix socket code so it did not
> > work. This patch uses sk_filter() to filter buffers before delivery.
> >
> > This short program demonstrates the problem on SOCK_DGRAM.
By the way, the patch implements socket filters on SOCK_DGRAM and
SOCK_SEQPACKET but not SOCK_STREAM. Socket filters does not make sense
to me when there is no packet boundaries. But if there is a need for
it, the code for SOCK_STREAM could be added easily.
> Any idea on performance cost adding sk_filter() call ?
Ian will write a performance test and repost the patch with some stats.
I don't know about the performance cost.
> Hmm, looking at it, I have no idea why sk_filter() needs to block BH.
I don't know neither.
> I'll send a patch to relax this requirement.
Thanks for your review!
Alban
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] af_unix: implement socket filter
2011-01-18 17:51 ` [PATCH] af_unix: implement socket filter Alban Crequy
@ 2011-01-18 20:19 ` Eric Dumazet
0 siblings, 0 replies; 7+ messages in thread
From: Eric Dumazet @ 2011-01-18 20:19 UTC (permalink / raw)
To: Alban Crequy
Cc: Ian Molton, netdev, linux-kernel, davem, ebiederm, xemul, davidel
Le mardi 18 janvier 2011 à 17:51 +0000, Alban Crequy a écrit :
> Le Tue, 18 Jan 2011 18:22:41 +0100,
> Eric Dumazet <eric.dumazet@gmail.com> a écrit :
> > Any idea on performance cost adding sk_filter() call ?
>
> Ian will write a performance test and repost the patch with some stats.
> I don't know about the performance cost.
Dont spend time on this, it was more a question for myself ;)
Cost should be very small, unless complex filter is used, and I have a
JIT compiler for BPF on x86_64, will post it when net-next-2.6 reopens.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] af_unix: implement socket filter
2011-01-18 16:39 [PATCH] af_unix: implement socket filter Ian Molton
2011-01-18 17:22 ` Eric Dumazet
@ 2011-01-19 5:33 ` David Miller
1 sibling, 0 replies; 7+ messages in thread
From: David Miller @ 2011-01-19 5:33 UTC (permalink / raw)
To: ian.molton
Cc: netdev, linux-kernel, eric.dumazet, ebiederm, xemul, davidel,
alban.crequy
From: Ian Molton <ian.molton@collabora.co.uk>
Date: Tue, 18 Jan 2011 16:39:15 +0000
> From: Alban Crequy <alban.crequy@collabora.co.uk>
>
> Linux Socket Filters can already be successfully attached and detached on unix
> sockets with setsockopt(sockfd, SOL_SOCKET, SO_{ATTACH,DETACH}_FILTER, ...).
> See: Documentation/networking/filter.txt
>
> But the filter was never used in the unix socket code so it did not work. This
> patch uses sk_filter() to filter buffers before delivery.
>
> This short program demonstrates the problem on SOCK_DGRAM.
...
> Signed-off-by: Alban Crequy <alban.crequy@collabora.co.uk>
> Reviewed-by: Ian Molton <ian.molton@collabora.co.uk>
Applied.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] net: filter: dont block softirqs in sk_run_filter()
2011-01-18 17:46 ` [PATCH] net: filter: dont block softirqs in sk_run_filter() Eric Dumazet
@ 2011-01-19 5:33 ` David Miller
0 siblings, 0 replies; 7+ messages in thread
From: David Miller @ 2011-01-19 5:33 UTC (permalink / raw)
To: eric.dumazet
Cc: netdev, linux-kernel, ebiederm, xemul, davidel, alban.crequy, ian.molton
From: Eric Dumazet <eric.dumazet@gmail.com>
Date: Tue, 18 Jan 2011 18:46:52 +0100
> Packet filter (BPF) doesnt need to disable softirqs, being fully
> re-entrant and lock-less.
>
> Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com>
Applied.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2011-01-19 5:33 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-18 16:39 [PATCH] af_unix: implement socket filter Ian Molton
2011-01-18 17:22 ` Eric Dumazet
2011-01-18 17:46 ` [PATCH] net: filter: dont block softirqs in sk_run_filter() Eric Dumazet
2011-01-19 5:33 ` David Miller
2011-01-18 17:51 ` [PATCH] af_unix: implement socket filter Alban Crequy
2011-01-18 20:19 ` Eric Dumazet
2011-01-19 5:33 ` David Miller
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).