Skip to content

Commit 514ac99

Browse files
hartkoppmarckleinebudde
authored andcommitted
can: fix multiple delivery of a single CAN frame for overlapping CAN filters
The CAN_RAW socket can set multiple CAN identifier specific filters that lead to multiple filters in the af_can.c filter processing. These filters are indenpendent from each other which leads to logical OR'ed filters when applied. This patch makes sure that every CAN frame which is filtered for a specific socket is only delivered once to the user space. This is independent from the number of matching CAN filters of this socket. As the raw_rcv() function is executed from NET_RX softirq the introduced variables are implemented as per-CPU variables to avoid extensive locking at CAN frame reception time. Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
1 parent a0bc163 commit 514ac99

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

net/can/raw.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ MODULE_ALIAS("can-proto-1");
7474
* storing the single filter in dfilter, to avoid using dynamic memory.
7575
*/
7676

77+
struct uniqframe {
78+
ktime_t tstamp;
79+
const struct sk_buff *skb;
80+
};
81+
7782
struct raw_sock {
7883
struct sock sk;
7984
int bound;
@@ -86,6 +91,7 @@ struct raw_sock {
8691
struct can_filter dfilter; /* default/single filter */
8792
struct can_filter *filter; /* pointer to filter(s) */
8893
can_err_mask_t err_mask;
94+
struct uniqframe __percpu *uniq;
8995
};
9096

9197
/*
@@ -123,6 +129,15 @@ static void raw_rcv(struct sk_buff *oskb, void *data)
123129
if (!ro->fd_frames && oskb->len != CAN_MTU)
124130
return;
125131

132+
/* eliminate multiple filter matches for the same skb */
133+
if (this_cpu_ptr(ro->uniq)->skb == oskb &&
134+
ktime_equal(this_cpu_ptr(ro->uniq)->tstamp, oskb->tstamp)) {
135+
return;
136+
} else {
137+
this_cpu_ptr(ro->uniq)->skb = oskb;
138+
this_cpu_ptr(ro->uniq)->tstamp = oskb->tstamp;
139+
}
140+
126141
/* clone the given skb to be able to enqueue it into the rcv queue */
127142
skb = skb_clone(oskb, GFP_ATOMIC);
128143
if (!skb)
@@ -297,6 +312,11 @@ static int raw_init(struct sock *sk)
297312
ro->recv_own_msgs = 0;
298313
ro->fd_frames = 0;
299314

315+
/* alloc_percpu provides zero'ed memory */
316+
ro->uniq = alloc_percpu(struct uniqframe);
317+
if (unlikely(!ro->uniq))
318+
return -ENOMEM;
319+
300320
/* set notifier */
301321
ro->notifier.notifier_call = raw_notifier;
302322

@@ -339,6 +359,7 @@ static int raw_release(struct socket *sock)
339359
ro->ifindex = 0;
340360
ro->bound = 0;
341361
ro->count = 0;
362+
free_percpu(ro->uniq);
342363

343364
sock_orphan(sk);
344365
sock->sk = NULL;

0 commit comments

Comments
 (0)