Skip to content

Commit d253eee

Browse files
Oliver Hartkoppdavem330
authored andcommitted
can: Fix CAN_(EFF|RTR)_FLAG handling in can_filter
Due to a wrong safety check in af_can.c it was not possible to filter for SFF frames with a specific CAN identifier without getting the same selected CAN identifier from a received EFF frame also. This fix has a minimum (but user visible) impact on the CAN filter API and therefore the CAN version is set to a new date. Indeed the 'old' API is still working as-is. But when now setting CAN_(EFF|RTR)_FLAG in can_filter.can_mask you might get less traffic than before - but still the stuff that you expected to get for your defined filter ... Thanks to Kurt Van Dijck for pointing at this issue and for the review. Signed-off-by: Oliver Hartkopp <oliver@hartkopp.net> Acked-by: Kurt Van Dijck <kurt.van.dijck@eia.be> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent bd7df21 commit d253eee

3 files changed

Lines changed: 53 additions & 19 deletions

File tree

‎include/linux/can/core.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <linux/skbuff.h>
2020
#include <linux/netdevice.h>
2121

22-
#define CAN_VERSION "20071116"
22+
#define CAN_VERSION "20081130"
2323

2424
/* increment this number each time you change some user-space interface */
2525
#define CAN_ABI_VERSION "8"

‎net/can/af_can.c‎

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -319,23 +319,52 @@ static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
319319
return n ? d : NULL;
320320
}
321321

322+
/**
323+
* find_rcv_list - determine optimal filterlist inside device filter struct
324+
* @can_id: pointer to CAN identifier of a given can_filter
325+
* @mask: pointer to CAN mask of a given can_filter
326+
* @d: pointer to the device filter struct
327+
*
328+
* Description:
329+
* Returns the optimal filterlist to reduce the filter handling in the
330+
* receive path. This function is called by service functions that need
331+
* to register or unregister a can_filter in the filter lists.
332+
*
333+
* A filter matches in general, when
334+
*
335+
* <received_can_id> & mask == can_id & mask
336+
*
337+
* so every bit set in the mask (even CAN_EFF_FLAG, CAN_RTR_FLAG) describe
338+
* relevant bits for the filter.
339+
*
340+
* The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
341+
* filter for error frames (CAN_ERR_FLAG bit set in mask). For error frames
342+
* there is a special filterlist and a special rx path filter handling.
343+
*
344+
* Return:
345+
* Pointer to optimal filterlist for the given can_id/mask pair.
346+
* Constistency checked mask.
347+
* Reduced can_id to have a preprocessed filter compare value.
348+
*/
322349
static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
323350
struct dev_rcv_lists *d)
324351
{
325352
canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
326353

327-
/* filter error frames */
354+
/* filter for error frames in extra filterlist */
328355
if (*mask & CAN_ERR_FLAG) {
329-
/* clear CAN_ERR_FLAG in list entry */
356+
/* clear CAN_ERR_FLAG in filter entry */
330357
*mask &= CAN_ERR_MASK;
331358
return &d->rx[RX_ERR];
332359
}
333360

334-
/* ensure valid values in can_mask */
335-
if (*mask & CAN_EFF_FLAG)
336-
*mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
337-
else
338-
*mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
361+
/* with cleared CAN_ERR_FLAG we have a simple mask/value filterpair */
362+
363+
#define CAN_EFF_RTR_FLAGS (CAN_EFF_FLAG | CAN_RTR_FLAG)
364+
365+
/* ensure valid values in can_mask for 'SFF only' frame filtering */
366+
if ((*mask & CAN_EFF_FLAG) && !(*can_id & CAN_EFF_FLAG))
367+
*mask &= (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS);
339368

340369
/* reduce condition testing at receive time */
341370
*can_id &= *mask;
@@ -348,15 +377,19 @@ static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
348377
if (!(*mask))
349378
return &d->rx[RX_ALL];
350379

351-
/* use extra filterset for the subscription of exactly *ONE* can_id */
352-
if (*can_id & CAN_EFF_FLAG) {
353-
if (*mask == (CAN_EFF_MASK | CAN_EFF_FLAG)) {
354-
/* RFC: a use-case for hash-tables in the future? */
355-
return &d->rx[RX_EFF];
380+
/* extra filterlists for the subscription of a single non-RTR can_id */
381+
if (((*mask & CAN_EFF_RTR_FLAGS) == CAN_EFF_RTR_FLAGS)
382+
&& !(*can_id & CAN_RTR_FLAG)) {
383+
384+
if (*can_id & CAN_EFF_FLAG) {
385+
if (*mask == (CAN_EFF_MASK | CAN_EFF_RTR_FLAGS)) {
386+
/* RFC: a future use-case for hash-tables? */
387+
return &d->rx[RX_EFF];
388+
}
389+
} else {
390+
if (*mask == (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS))
391+
return &d->rx_sff[*can_id];
356392
}
357-
} else {
358-
if (*mask == CAN_SFF_MASK)
359-
return &d->rx_sff[*can_id];
360393
}
361394

362395
/* default: filter via can_id/can_mask */

‎net/can/bcm.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@
6464
#define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
6565

6666
/* get best masking value for can_rx_register() for a given single can_id */
67-
#define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
68-
(CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
67+
#define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
68+
(CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
69+
(CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
6970

70-
#define CAN_BCM_VERSION "20080415"
71+
#define CAN_BCM_VERSION CAN_VERSION
7172
static __initdata const char banner[] = KERN_INFO
7273
"can: broadcast manager protocol (rev " CAN_BCM_VERSION ")\n";
7374

0 commit comments

Comments
 (0)