blob: ce7393d397e18a71baa80d99bf16c7ff01282e0b [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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
11#ifndef _CAN_SKB_H
12#define _CAN_SKB_H
13
14#include <linux/types.h>
15#include <linux/skbuff.h>
16#include <linux/can.h>
17#include <net/sock.h>
18
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
31 * @skbcnt: atomic counter to have an unique id together with skb pointer
32 * @cf: align to the following CAN frame at skb->data
33 */
34struct can_skb_priv {
35 int ifindex;
36 int skbcnt;
Olivier Deprez157378f2022-04-04 15:47:50 +020037 struct can_frame cf[];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038};
39
40static 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
50static inline void can_skb_set_owner(struct sk_buff *skb, struct sock *sk)
51{
Olivier Deprez0e641232021-09-23 10:07:05 +020052 /* If the socket has already been closed by user space, the
53 * refcount may already be 0 (and the socket will be freed
54 * after the last TX skb has been freed). So only increase
55 * socket refcount if the refcount is > 0.
56 */
57 if (sk && refcount_inc_not_zero(&sk->sk_refcnt)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000058 skb->destructor = sock_efree;
59 skb->sk = sk;
60 }
61}
62
63/*
64 * returns an unshared skb owned by the original sock to be echo'ed back
65 */
66static inline struct sk_buff *can_create_echo_skb(struct sk_buff *skb)
67{
Olivier Deprez0e641232021-09-23 10:07:05 +020068 struct sk_buff *nskb;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000069
Olivier Deprez0e641232021-09-23 10:07:05 +020070 nskb = skb_clone(skb, GFP_ATOMIC);
71 if (unlikely(!nskb)) {
72 kfree_skb(skb);
73 return NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000074 }
75
Olivier Deprez0e641232021-09-23 10:07:05 +020076 can_skb_set_owner(nskb, skb->sk);
77 consume_skb(skb);
78 return nskb;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000079}
80
81#endif /* !_CAN_SKB_H */