Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 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 | |
Oliver Hartkopp | 42193e3 | 2014-05-15 20:31:56 +0200 | [diff] [blame] | 10 | #ifndef _CAN_SKB_H |
| 11 | #define _CAN_SKB_H |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 12 | |
| 13 | #include <linux/types.h> |
Oliver Hartkopp | 0ae89be | 2014-01-30 10:11:28 +0100 | [diff] [blame] | 14 | #include <linux/skbuff.h> |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 15 | #include <linux/can.h> |
Oliver Hartkopp | 0ae89be | 2014-01-30 10:11:28 +0100 | [diff] [blame] | 16 | #include <net/sock.h> |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 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 | * @cf: align to the following CAN frame at skb->data |
| 31 | */ |
| 32 | struct can_skb_priv { |
| 33 | int ifindex; |
| 34 | struct can_frame cf[0]; |
| 35 | }; |
| 36 | |
Oliver Hartkopp | 2bf3440 | 2013-01-28 08:33:33 +0000 | [diff] [blame] | 37 | static inline struct can_skb_priv *can_skb_prv(struct sk_buff *skb) |
| 38 | { |
| 39 | return (struct can_skb_priv *)(skb->head); |
| 40 | } |
| 41 | |
| 42 | static inline void can_skb_reserve(struct sk_buff *skb) |
| 43 | { |
| 44 | skb_reserve(skb, sizeof(struct can_skb_priv)); |
| 45 | } |
| 46 | |
Oliver Hartkopp | 0ae89be | 2014-01-30 10:11:28 +0100 | [diff] [blame] | 47 | static inline void can_skb_set_owner(struct sk_buff *skb, struct sock *sk) |
| 48 | { |
| 49 | if (sk) { |
| 50 | sock_hold(sk); |
Florian Westphal | 2b290bb | 2015-03-10 04:48:20 +0100 | [diff] [blame^] | 51 | skb->destructor = sock_efree; |
Oliver Hartkopp | 0ae89be | 2014-01-30 10:11:28 +0100 | [diff] [blame] | 52 | skb->sk = sk; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * returns an unshared skb owned by the original sock to be echo'ed back |
| 58 | */ |
| 59 | static inline struct sk_buff *can_create_echo_skb(struct sk_buff *skb) |
| 60 | { |
| 61 | if (skb_shared(skb)) { |
| 62 | struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); |
| 63 | |
| 64 | if (likely(nskb)) { |
| 65 | can_skb_set_owner(nskb, skb->sk); |
| 66 | consume_skb(skb); |
| 67 | return nskb; |
| 68 | } else { |
| 69 | kfree_skb(skb); |
| 70 | return NULL; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /* we can assume to have an unshared skb with proper owner */ |
| 75 | return skb; |
| 76 | } |
| 77 | |
Oliver Hartkopp | 42193e3 | 2014-05-15 20:31:56 +0200 | [diff] [blame] | 78 | #endif /* !_CAN_SKB_H */ |