1 From e62d2df084e2849edffb206559725fa81bb569a8 Mon Sep 17 00:00:00 2001
2 From: Daniel Borkmann <dborkman@redhat.com>
3 Date: Fri, 28 Mar 2014 18:58:21 +0100
4 Subject: [PATCH] net: ptp: use sk_unattached_filter_create() for BPF
6 This patch migrates an open-coded sk_run_filter() implementation with
7 proper use of the BPF API, that is, sk_unattached_filter_create(). This
8 migration is needed, as we will be internally transforming the filter
9 to a different representation, and therefore needs to be decoupled.
11 It is okay to do so as skb_timestamping_init() is called during
12 initialization of the network stack in core initcall via sock_init().
13 This would effectively also allow for PTP filters to be jit compiled if
14 bpf_jit_enable is set.
16 For better readability, there are also some newlines introduced, also
17 ptp_classify.h is only in kernel space.
19 Joint work with Alexei Starovoitov.
21 Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
22 Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
23 Cc: Richard Cochran <richard.cochran@omicron.at>
24 Cc: Jiri Benc <jbenc@redhat.com>
25 Signed-off-by: David S. Miller <davem@davemloft.net>
27 include/linux/ptp_classify.h | 4 ----
28 net/core/timestamping.c | 21 ++++++++++++++-------
29 2 files changed, 14 insertions(+), 11 deletions(-)
31 diff --git a/include/linux/ptp_classify.h b/include/linux/ptp_classify.h
32 index 1dc420b..3decfa4 100644
33 --- a/include/linux/ptp_classify.h
34 +++ b/include/linux/ptp_classify.h
36 #include <linux/if_vlan.h>
38 #include <linux/filter.h>
42 -#include <netinet/in.h>
45 #define PTP_CLASS_NONE 0x00 /* not a PTP event message */
46 #define PTP_CLASS_V1 0x01 /* protocol version 1 */
47 diff --git a/net/core/timestamping.c b/net/core/timestamping.c
48 index 661b5a4..e43d56a 100644
49 --- a/net/core/timestamping.c
50 +++ b/net/core/timestamping.c
52 #include <linux/skbuff.h>
53 #include <linux/export.h>
55 -static struct sock_filter ptp_filter[] = {
58 +static struct sk_filter *ptp_insns __read_mostly;
60 static unsigned int classify(const struct sk_buff *skb)
62 - if (likely(skb->dev &&
64 + if (likely(skb->dev && skb->dev->phydev &&
65 skb->dev->phydev->drv))
66 - return sk_run_filter(skb, ptp_filter);
67 + return SK_RUN_FILTER(ptp_insns, skb);
69 return PTP_CLASS_NONE;
71 @@ -60,11 +57,13 @@ void skb_clone_tx_timestamp(struct sk_buff *skb)
72 if (likely(phydev->drv->txtstamp)) {
73 if (!atomic_inc_not_zero(&sk->sk_refcnt))
76 clone = skb_clone(skb, GFP_ATOMIC);
83 phydev->drv->txtstamp(phydev, clone, type);
85 @@ -89,12 +88,15 @@ void skb_complete_tx_timestamp(struct sk_buff *skb,
88 *skb_hwtstamps(skb) = *hwtstamps;
90 serr = SKB_EXT_ERR(skb);
91 memset(serr, 0, sizeof(*serr));
92 serr->ee.ee_errno = ENOMSG;
93 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
96 err = sock_queue_err_skb(sk, skb);
101 @@ -135,5 +137,10 @@ EXPORT_SYMBOL_GPL(skb_defer_rx_timestamp);
103 void __init skb_timestamping_init(void)
105 - BUG_ON(sk_chk_filter(ptp_filter, ARRAY_SIZE(ptp_filter)));
106 + static struct sock_filter ptp_filter[] = { PTP_FILTER };
107 + struct sock_fprog ptp_prog = {
108 + .len = ARRAY_SIZE(ptp_filter), .filter = ptp_filter,
111 + BUG_ON(sk_unattached_filter_create(&ptp_insns, &ptp_prog));