1 /* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
5 * Definitions for the CAN network socket buffer
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 __must_check can_get_echo_skb(struct net_device *dev,
26 unsigned int *frame_len_ptr);
27 void can_free_echo_skb(struct net_device *dev, unsigned int idx,
28 unsigned int *frame_len_ptr);
29 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
30 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
31 struct canfd_frame **cfd);
32 struct sk_buff *alloc_can_err_skb(struct net_device *dev,
33 struct can_frame **cf);
36 * The struct can_skb_priv is used to transport additional information along
37 * with the stored struct can(fd)_frame that can not be contained in existing
38 * struct sk_buff elements.
39 * N.B. that this information must not be modified in cloned CAN sk_buffs.
40 * To modify the CAN frame content or the struct can_skb_priv content
41 * skb_copy() needs to be used instead of skb_clone().
45 * struct can_skb_priv - private additional data inside CAN sk_buffs
46 * @ifindex: ifindex of the first interface the CAN frame appeared on
47 * @skbcnt: atomic counter to have an unique id together with skb pointer
48 * @frame_len: length of CAN frame in data link layer
49 * @cf: align to the following CAN frame at skb->data
54 unsigned int frame_len;
55 struct can_frame cf[];
58 static inline struct can_skb_priv *can_skb_prv(struct sk_buff *skb)
60 return (struct can_skb_priv *)(skb->head);
63 static inline void can_skb_reserve(struct sk_buff *skb)
65 skb_reserve(skb, sizeof(struct can_skb_priv));
68 static inline void can_skb_set_owner(struct sk_buff *skb, struct sock *sk)
70 /* If the socket has already been closed by user space, the
71 * refcount may already be 0 (and the socket will be freed
72 * after the last TX skb has been freed). So only increase
73 * socket refcount if the refcount is > 0.
75 if (sk && refcount_inc_not_zero(&sk->sk_refcnt)) {
76 skb->destructor = sock_efree;
82 * returns an unshared skb owned by the original sock to be echo'ed back
84 static inline struct sk_buff *can_create_echo_skb(struct sk_buff *skb)
88 nskb = skb_clone(skb, GFP_ATOMIC);
89 if (unlikely(!nskb)) {
94 can_skb_set_owner(nskb, skb->sk);
99 /* Check for outgoing skbs that have not been created by the CAN subsystem */
100 static inline bool can_skb_headroom_valid(struct net_device *dev,
103 /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
104 if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
107 /* af_packet does not apply CAN skb specific settings */
108 if (skb->ip_summed == CHECKSUM_NONE) {
110 can_skb_prv(skb)->ifindex = dev->ifindex;
111 can_skb_prv(skb)->skbcnt = 0;
113 skb->ip_summed = CHECKSUM_UNNECESSARY;
115 /* perform proper loopback on capable devices */
116 if (dev->flags & IFF_ECHO)
117 skb->pkt_type = PACKET_LOOPBACK;
119 skb->pkt_type = PACKET_HOST;
121 skb_reset_mac_header(skb);
122 skb_reset_network_header(skb);
123 skb_reset_transport_header(skb);
129 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
130 static inline bool can_dropped_invalid_skb(struct net_device *dev,
133 const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
135 if (skb->protocol == htons(ETH_P_CAN)) {
136 if (unlikely(skb->len != CAN_MTU ||
137 cfd->len > CAN_MAX_DLEN))
139 } else if (skb->protocol == htons(ETH_P_CANFD)) {
140 if (unlikely(skb->len != CANFD_MTU ||
141 cfd->len > CANFD_MAX_DLEN))
146 if (!can_skb_headroom_valid(dev, skb))
153 dev->stats.tx_dropped++;
157 static inline bool can_is_canfd_skb(const struct sk_buff *skb)
159 /* the CAN specific type of skb is identified by its data length */
160 return skb->len == CANFD_MTU;
163 #endif /* !_CAN_SKB_H */