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 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 unsigned int *frame_len_ptr);
28 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
29 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
30 struct canfd_frame **cfd);
31 struct sk_buff *alloc_can_err_skb(struct net_device *dev,
32 struct can_frame **cf);
35 * The struct can_skb_priv is used to transport additional information along
36 * with the stored struct can(fd)_frame that can not be contained in existing
37 * struct sk_buff elements.
38 * N.B. that this information must not be modified in cloned CAN sk_buffs.
39 * To modify the CAN frame content or the struct can_skb_priv content
40 * skb_copy() needs to be used instead of skb_clone().
44 * struct can_skb_priv - private additional data inside CAN sk_buffs
45 * @ifindex: ifindex of the first interface the CAN frame appeared on
46 * @skbcnt: atomic counter to have an unique id together with skb pointer
47 * @frame_len: length of CAN frame in data link layer
48 * @cf: align to the following CAN frame at skb->data
53 unsigned int frame_len;
54 struct can_frame cf[];
57 static inline struct can_skb_priv *can_skb_prv(struct sk_buff *skb)
59 return (struct can_skb_priv *)(skb->head);
62 static inline void can_skb_reserve(struct sk_buff *skb)
64 skb_reserve(skb, sizeof(struct can_skb_priv));
67 static inline void can_skb_set_owner(struct sk_buff *skb, struct sock *sk)
69 /* If the socket has already been closed by user space, the
70 * refcount may already be 0 (and the socket will be freed
71 * after the last TX skb has been freed). So only increase
72 * socket refcount if the refcount is > 0.
74 if (sk && refcount_inc_not_zero(&sk->sk_refcnt)) {
75 skb->destructor = sock_efree;
81 * returns an unshared skb owned by the original sock to be echo'ed back
83 static inline struct sk_buff *can_create_echo_skb(struct sk_buff *skb)
87 nskb = skb_clone(skb, GFP_ATOMIC);
88 if (unlikely(!nskb)) {
93 can_skb_set_owner(nskb, skb->sk);
98 /* Check for outgoing skbs that have not been created by the CAN subsystem */
99 static inline bool can_skb_headroom_valid(struct net_device *dev,
102 /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
103 if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
106 /* af_packet does not apply CAN skb specific settings */
107 if (skb->ip_summed == CHECKSUM_NONE) {
109 can_skb_prv(skb)->ifindex = dev->ifindex;
110 can_skb_prv(skb)->skbcnt = 0;
112 skb->ip_summed = CHECKSUM_UNNECESSARY;
114 /* perform proper loopback on capable devices */
115 if (dev->flags & IFF_ECHO)
116 skb->pkt_type = PACKET_LOOPBACK;
118 skb->pkt_type = PACKET_HOST;
120 skb_reset_mac_header(skb);
121 skb_reset_network_header(skb);
122 skb_reset_transport_header(skb);
128 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
129 static inline bool can_dropped_invalid_skb(struct net_device *dev,
132 const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
134 if (skb->protocol == htons(ETH_P_CAN)) {
135 if (unlikely(skb->len != CAN_MTU ||
136 cfd->len > CAN_MAX_DLEN))
138 } else if (skb->protocol == htons(ETH_P_CANFD)) {
139 if (unlikely(skb->len != CANFD_MTU ||
140 cfd->len > CANFD_MAX_DLEN))
145 if (!can_skb_headroom_valid(dev, skb))
152 dev->stats.tx_dropped++;
156 static inline bool can_is_canfd_skb(const struct sk_buff *skb)
158 /* the CAN specific type of skb is identified by its data length */
159 return skb->len == CANFD_MTU;
162 #endif /* !_CAN_SKB_H */