• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/skbuff.h>
15 #include <linux/can.h>
16 #include <net/sock.h>
17 
18 /*
19  * The struct can_skb_priv is used to transport additional information along
20  * with the stored struct can(fd)_frame that can not be contained in existing
21  * struct sk_buff elements.
22  * N.B. that this information must not be modified in cloned CAN sk_buffs.
23  * To modify the CAN frame content or the struct can_skb_priv content
24  * skb_copy() needs to be used instead of skb_clone().
25  */
26 
27 /**
28  * struct can_skb_priv - private additional data inside CAN sk_buffs
29  * @ifindex:	ifindex of the first interface the CAN frame appeared on
30  * @skbcnt:	atomic counter to have an unique id together with skb pointer
31  * @cf:		align to the following CAN frame at skb->data
32  */
33 struct can_skb_priv {
34 	int ifindex;
35 	int skbcnt;
36 	struct can_frame cf[0];
37 };
38 
can_skb_prv(struct sk_buff * skb)39 static inline struct can_skb_priv *can_skb_prv(struct sk_buff *skb)
40 {
41 	return (struct can_skb_priv *)(skb->head);
42 }
43 
can_skb_reserve(struct sk_buff * skb)44 static inline void can_skb_reserve(struct sk_buff *skb)
45 {
46 	skb_reserve(skb, sizeof(struct can_skb_priv));
47 }
48 
can_skb_set_owner(struct sk_buff * skb,struct sock * sk)49 static inline void can_skb_set_owner(struct sk_buff *skb, struct sock *sk)
50 {
51 	/* If the socket has already been closed by user space, the
52 	 * refcount may already be 0 (and the socket will be freed
53 	 * after the last TX skb has been freed). So only increase
54 	 * socket refcount if the refcount is > 0.
55 	 */
56 	if (sk && atomic_inc_not_zero(&sk->sk_refcnt)) {
57 		skb->destructor = sock_efree;
58 		skb->sk = sk;
59 	}
60 }
61 
62 /*
63  * returns an unshared skb owned by the original sock to be echo'ed back
64  */
can_create_echo_skb(struct sk_buff * skb)65 static inline struct sk_buff *can_create_echo_skb(struct sk_buff *skb)
66 {
67 	struct sk_buff *nskb;
68 
69 	nskb = skb_clone(skb, GFP_ATOMIC);
70 	if (unlikely(!nskb)) {
71 		kfree_skb(skb);
72 		return NULL;
73 	}
74 
75 	can_skb_set_owner(nskb, skb->sk);
76 	consume_skb(skb);
77 	return nskb;
78 }
79 
80 #endif /* !_CAN_SKB_H */
81