blob: b3379a97245c1f2169bfb08a9be17dccb81a6693 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +01002/*
3 * linux/can/skb.h
4 *
5 * Definitions for the CAN network socket buffer
6 *
7 * Copyright (C) 2012 Oliver Hartkopp <socketcan@hartkopp.net>
8 *
9 */
10
Oliver Hartkopp42193e32014-05-15 20:31:56 +020011#ifndef _CAN_SKB_H
12#define _CAN_SKB_H
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +010013
14#include <linux/types.h>
Oliver Hartkopp0ae89be2014-01-30 10:11:28 +010015#include <linux/skbuff.h>
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +010016#include <linux/can.h>
Oliver Hartkopp0ae89be2014-01-30 10:11:28 +010017#include <net/sock.h>
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +010018
19/*
20 * The struct can_skb_priv is used to transport additional information along
21 * with the stored struct can(fd)_frame that can not be contained in existing
22 * struct sk_buff elements.
23 * N.B. that this information must not be modified in cloned CAN sk_buffs.
24 * To modify the CAN frame content or the struct can_skb_priv content
25 * skb_copy() needs to be used instead of skb_clone().
26 */
27
28/**
29 * struct can_skb_priv - private additional data inside CAN sk_buffs
30 * @ifindex: ifindex of the first interface the CAN frame appeared on
Oliver Hartkoppd3b58c42015-06-26 11:58:19 +020031 * @skbcnt: atomic counter to have an unique id together with skb pointer
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +010032 * @cf: align to the following CAN frame at skb->data
33 */
34struct can_skb_priv {
35 int ifindex;
Oliver Hartkoppd3b58c42015-06-26 11:58:19 +020036 int skbcnt;
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +010037 struct can_frame cf[0];
38};
39
Oliver Hartkopp2bf34402013-01-28 08:33:33 +000040static inline struct can_skb_priv *can_skb_prv(struct sk_buff *skb)
41{
42 return (struct can_skb_priv *)(skb->head);
43}
44
45static inline void can_skb_reserve(struct sk_buff *skb)
46{
47 skb_reserve(skb, sizeof(struct can_skb_priv));
48}
49
Oliver Hartkopp0ae89be2014-01-30 10:11:28 +010050static inline void can_skb_set_owner(struct sk_buff *skb, struct sock *sk)
51{
52 if (sk) {
53 sock_hold(sk);
Florian Westphal2b290bb2015-03-10 04:48:20 +010054 skb->destructor = sock_efree;
Oliver Hartkopp0ae89be2014-01-30 10:11:28 +010055 skb->sk = sk;
56 }
57}
58
59/*
60 * returns an unshared skb owned by the original sock to be echo'ed back
61 */
62static inline struct sk_buff *can_create_echo_skb(struct sk_buff *skb)
63{
64 if (skb_shared(skb)) {
65 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
66
67 if (likely(nskb)) {
68 can_skb_set_owner(nskb, skb->sk);
69 consume_skb(skb);
70 return nskb;
71 } else {
72 kfree_skb(skb);
73 return NULL;
74 }
75 }
76
77 /* we can assume to have an unshared skb with proper owner */
78 return skb;
79}
80
Oliver Hartkopp42193e32014-05-15 20:31:56 +020081#endif /* !_CAN_SKB_H */