Skip to content

Commit 156c2bb

Browse files
hartkoppmarckleinebudde
authored andcommitted
can: add private data space for CAN sk_buffs
The struct can_skb_priv is used to transport additional information along with the stored struct can(fd)_frame that can not be contained in existing struct sk_buff elements. can_skb_priv is located in the skb headroom, which does not touch the existing CAN sk_buff usage with skb->data and skb->len, so that even out-of-tree CAN drivers can be used without changes. Btw. out-of-tree CAN drivers without can_skb_priv in the sk_buff headroom would not support features based on can_skb_priv. The can_skb_priv->ifindex contains the first interface where the CAN frame appeared on the local host. Unfortunately skb->skb_iif can not be used as this value is overwritten in every netif_receive_skb() call. Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
1 parent e2d5f2c commit 156c2bb

5 files changed

Lines changed: 64 additions & 7 deletions

File tree

‎drivers/net/can/dev.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/if_arp.h>
2525
#include <linux/can.h>
2626
#include <linux/can/dev.h>
27+
#include <linux/can/skb.h>
2728
#include <linux/can/netlink.h>
2829
#include <linux/can/led.h>
2930
#include <net/rtnetlink.h>
@@ -502,13 +503,18 @@ struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
502503
{
503504
struct sk_buff *skb;
504505

505-
skb = netdev_alloc_skb(dev, sizeof(struct can_frame));
506+
skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
507+
sizeof(struct can_frame));
506508
if (unlikely(!skb))
507509
return NULL;
508510

509511
skb->protocol = htons(ETH_P_CAN);
510512
skb->pkt_type = PACKET_BROADCAST;
511513
skb->ip_summed = CHECKSUM_UNNECESSARY;
514+
515+
skb_reserve(skb, sizeof(struct can_skb_priv));
516+
((struct can_skb_priv *)(skb->head))->ifindex = dev->ifindex;
517+
512518
*cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
513519
memset(*cf, 0, sizeof(struct can_frame));
514520

‎drivers/net/can/slcan.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include <linux/init.h>
5656
#include <linux/kernel.h>
5757
#include <linux/can.h>
58+
#include <linux/can/skb.h>
5859

5960
static __initconst const char banner[] =
6061
KERN_INFO "slcan: serial line CAN interface driver\n";
@@ -184,14 +185,19 @@ static void slc_bump(struct slcan *sl)
184185
cf.data[i] |= tmp;
185186
}
186187

187-
skb = dev_alloc_skb(sizeof(struct can_frame));
188+
skb = dev_alloc_skb(sizeof(struct can_frame) +
189+
sizeof(struct can_skb_priv));
188190
if (!skb)
189191
return;
190192

191193
skb->dev = sl->dev;
192194
skb->protocol = htons(ETH_P_CAN);
193195
skb->pkt_type = PACKET_BROADCAST;
194196
skb->ip_summed = CHECKSUM_UNNECESSARY;
197+
198+
skb_reserve(skb, sizeof(struct can_skb_priv));
199+
((struct can_skb_priv *)(skb->head))->ifindex = sl->dev->ifindex;
200+
195201
memcpy(skb_put(skb, sizeof(struct can_frame)),
196202
&cf, sizeof(struct can_frame));
197203
netif_rx_ni(skb);

‎include/linux/can/skb.h‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* linux/can/skb.h
3+
*
4+
* Definitions for the CAN network socket buffer
5+
*
6+
* Copyright (C) 2012 Oliver Hartkopp <socketcan@hartkopp.net>
7+
*
8+
*/
9+
10+
#ifndef CAN_SKB_H
11+
#define CAN_SKB_H
12+
13+
#include <linux/types.h>
14+
#include <linux/can.h>
15+
16+
/*
17+
* The struct can_skb_priv is used to transport additional information along
18+
* with the stored struct can(fd)_frame that can not be contained in existing
19+
* struct sk_buff elements.
20+
* N.B. that this information must not be modified in cloned CAN sk_buffs.
21+
* To modify the CAN frame content or the struct can_skb_priv content
22+
* skb_copy() needs to be used instead of skb_clone().
23+
*/
24+
25+
/**
26+
* struct can_skb_priv - private additional data inside CAN sk_buffs
27+
* @ifindex: ifindex of the first interface the CAN frame appeared on
28+
* @cf: align to the following CAN frame at skb->data
29+
*/
30+
struct can_skb_priv {
31+
int ifindex;
32+
struct can_frame cf[0];
33+
};
34+
35+
#endif /* CAN_SKB_H */

‎net/can/bcm.c‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include <linux/skbuff.h>
5555
#include <linux/can.h>
5656
#include <linux/can/core.h>
57+
#include <linux/can/skb.h>
5758
#include <linux/can/bcm.h>
5859
#include <linux/slab.h>
5960
#include <net/sock.h>
@@ -256,10 +257,13 @@ static void bcm_can_tx(struct bcm_op *op)
256257
return;
257258
}
258259

259-
skb = alloc_skb(CFSIZ, gfp_any());
260+
skb = alloc_skb(CFSIZ + sizeof(struct can_skb_priv), gfp_any());
260261
if (!skb)
261262
goto out;
262263

264+
skb_reserve(skb, sizeof(struct can_skb_priv));
265+
((struct can_skb_priv *)(skb->head))->ifindex = dev->ifindex;
266+
263267
memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
264268

265269
/* send with loopback */
@@ -1199,11 +1203,12 @@ static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
11991203
if (!ifindex)
12001204
return -ENODEV;
12011205

1202-
skb = alloc_skb(CFSIZ, GFP_KERNEL);
1203-
1206+
skb = alloc_skb(CFSIZ + sizeof(struct can_skb_priv), GFP_KERNEL);
12041207
if (!skb)
12051208
return -ENOMEM;
12061209

1210+
skb_reserve(skb, sizeof(struct can_skb_priv));
1211+
12071212
err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
12081213
if (err < 0) {
12091214
kfree_skb(skb);
@@ -1216,6 +1221,7 @@ static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
12161221
return -ENODEV;
12171222
}
12181223

1224+
((struct can_skb_priv *)(skb->head))->ifindex = dev->ifindex;
12191225
skb->dev = dev;
12201226
skb->sk = sk;
12211227
err = can_send(skb, 1); /* send with loopback */

‎net/can/raw.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include <linux/skbuff.h>
5151
#include <linux/can.h>
5252
#include <linux/can/core.h>
53+
#include <linux/can/skb.h>
5354
#include <linux/can/raw.h>
5455
#include <net/sock.h>
5556
#include <net/net_namespace.h>
@@ -699,11 +700,14 @@ static int raw_sendmsg(struct kiocb *iocb, struct socket *sock,
699700
if (!dev)
700701
return -ENXIO;
701702

702-
skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT,
703-
&err);
703+
skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv),
704+
msg->msg_flags & MSG_DONTWAIT, &err);
704705
if (!skb)
705706
goto put_dev;
706707

708+
skb_reserve(skb, sizeof(struct can_skb_priv));
709+
((struct can_skb_priv *)(skb->head))->ifindex = dev->ifindex;
710+
707711
err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
708712
if (err < 0)
709713
goto free_skb;

0 commit comments

Comments
 (0)