1 /* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
5 * Definitions for the CAN network socket buffer
7 * Copyright (C) 2012 Oliver Hartkopp <socketcan@hartkopp.net>
14 #include <linux/types.h>
15 #include <linux/skbuff.h>
16 #include <linux/can.h>
19 void can_flush_echo_skb(struct net_device *dev);
20 int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
21 unsigned int idx, unsigned int frame_len);
22 struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx,
23 u8 *len_ptr, unsigned int *frame_len_ptr);
24 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx,
25 unsigned int *frame_len_ptr);
26 void can_free_echo_skb(struct net_device *dev, unsigned int idx);
27 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
28 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
29 struct canfd_frame **cfd);
30 struct sk_buff *alloc_can_err_skb(struct net_device *dev,
31 struct can_frame **cf);
34 * The struct can_skb_priv is used to transport additional information along
35 * with the stored struct can(fd)_frame that can not be contained in existing
36 * struct sk_buff elements.
37 * N.B. that this information must not be modified in cloned CAN sk_buffs.
38 * To modify the CAN frame content or the struct can_skb_priv content
39 * skb_copy() needs to be used instead of skb_clone().
43 * struct can_skb_priv - private additional data inside CAN sk_buffs
44 * @ifindex: ifindex of the first interface the CAN frame appeared on
45 * @skbcnt: atomic counter to have an unique id together with skb pointer
46 * @frame_len: length of CAN frame in data link layer
47 * @cf: align to the following CAN frame at skb->data
52 unsigned int frame_len;
53 struct can_frame cf[];
56 static inline struct can_skb_priv *can_skb_prv(struct sk_buff *skb)
58 return (struct can_skb_priv *)(skb->head);
61 static inline void can_skb_reserve(struct sk_buff *skb)
63 skb_reserve(skb, sizeof(struct can_skb_priv));
66 static inline void can_skb_set_owner(struct sk_buff *skb, struct sock *sk)
68 /* If the socket has already been closed by user space, the
69 * refcount may already be 0 (and the socket will be freed
70 * after the last TX skb has been freed). So only increase
71 * socket refcount if the refcount is > 0.
73 if (sk && refcount_inc_not_zero(&sk->sk_refcnt)) {
74 skb->destructor = sock_efree;
80 * returns an unshared skb owned by the original sock to be echo'ed back
82 static inline struct sk_buff *can_create_echo_skb(struct sk_buff *skb)
86 nskb = skb_clone(skb, GFP_ATOMIC);
87 if (unlikely(!nskb)) {
92 can_skb_set_owner(nskb, skb->sk);
97 /* Check for outgoing skbs that have not been created by the CAN subsystem */
98 static inline bool can_skb_headroom_valid(struct net_device *dev,
101 /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
102 if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
105 /* af_packet does not apply CAN skb specific settings */
106 if (skb->ip_summed == CHECKSUM_NONE) {
108 can_skb_prv(skb)->ifindex = dev->ifindex;
109 can_skb_prv(skb)->skbcnt = 0;
111 skb->ip_summed = CHECKSUM_UNNECESSARY;
113 /* perform proper loopback on capable devices */
114 if (dev->flags & IFF_ECHO)
115 skb->pkt_type = PACKET_LOOPBACK;
117 skb->pkt_type = PACKET_HOST;
119 skb_reset_mac_header(skb);
120 skb_reset_network_header(skb);
121 skb_reset_transport_header(skb);
127 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
128 static inline bool can_dropped_invalid_skb(struct net_device *dev,
131 const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
133 if (skb->protocol == htons(ETH_P_CAN)) {
134 if (unlikely(skb->len != CAN_MTU ||
135 cfd->len > CAN_MAX_DLEN))
137 } else if (skb->protocol == htons(ETH_P_CANFD)) {
138 if (unlikely(skb->len != CANFD_MTU ||
139 cfd->len > CANFD_MAX_DLEN))
144 if (!can_skb_headroom_valid(dev, skb))
151 dev->stats.tx_dropped++;
155 static inline bool can_is_canfd_skb(const struct sk_buff *skb)
157 /* the CAN specific type of skb is identified by its data length */
158 return skb->len == CANFD_MTU;
161 #endif /* !_CAN_SKB_H */