can: dev: can_put_echo_skb(): extend to handle frame_len
[linux-2.6-microblaze.git] / include / linux / can / skb.h
1 /* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
2 /*
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 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 void can_free_echo_skb(struct net_device *dev, unsigned int idx);
26 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
27 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
28                                 struct canfd_frame **cfd);
29 struct sk_buff *alloc_can_err_skb(struct net_device *dev,
30                                   struct can_frame **cf);
31
32 /*
33  * The struct can_skb_priv is used to transport additional information along
34  * with the stored struct can(fd)_frame that can not be contained in existing
35  * struct sk_buff elements.
36  * N.B. that this information must not be modified in cloned CAN sk_buffs.
37  * To modify the CAN frame content or the struct can_skb_priv content
38  * skb_copy() needs to be used instead of skb_clone().
39  */
40
41 /**
42  * struct can_skb_priv - private additional data inside CAN sk_buffs
43  * @ifindex:    ifindex of the first interface the CAN frame appeared on
44  * @skbcnt:     atomic counter to have an unique id together with skb pointer
45  * @frame_len:  length of CAN frame in data link layer
46  * @cf:         align to the following CAN frame at skb->data
47  */
48 struct can_skb_priv {
49         int ifindex;
50         int skbcnt;
51         unsigned int frame_len;
52         struct can_frame cf[];
53 };
54
55 static inline struct can_skb_priv *can_skb_prv(struct sk_buff *skb)
56 {
57         return (struct can_skb_priv *)(skb->head);
58 }
59
60 static inline void can_skb_reserve(struct sk_buff *skb)
61 {
62         skb_reserve(skb, sizeof(struct can_skb_priv));
63 }
64
65 static inline void can_skb_set_owner(struct sk_buff *skb, struct sock *sk)
66 {
67         if (sk) {
68                 sock_hold(sk);
69                 skb->destructor = sock_efree;
70                 skb->sk = sk;
71         }
72 }
73
74 /*
75  * returns an unshared skb owned by the original sock to be echo'ed back
76  */
77 static inline struct sk_buff *can_create_echo_skb(struct sk_buff *skb)
78 {
79         struct sk_buff *nskb;
80
81         nskb = skb_clone(skb, GFP_ATOMIC);
82         if (unlikely(!nskb)) {
83                 kfree_skb(skb);
84                 return NULL;
85         }
86
87         can_skb_set_owner(nskb, skb->sk);
88         consume_skb(skb);
89         return nskb;
90 }
91
92 /* Check for outgoing skbs that have not been created by the CAN subsystem */
93 static inline bool can_skb_headroom_valid(struct net_device *dev,
94                                           struct sk_buff *skb)
95 {
96         /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */
97         if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv)))
98                 return false;
99
100         /* af_packet does not apply CAN skb specific settings */
101         if (skb->ip_summed == CHECKSUM_NONE) {
102                 /* init headroom */
103                 can_skb_prv(skb)->ifindex = dev->ifindex;
104                 can_skb_prv(skb)->skbcnt = 0;
105
106                 skb->ip_summed = CHECKSUM_UNNECESSARY;
107
108                 /* perform proper loopback on capable devices */
109                 if (dev->flags & IFF_ECHO)
110                         skb->pkt_type = PACKET_LOOPBACK;
111                 else
112                         skb->pkt_type = PACKET_HOST;
113
114                 skb_reset_mac_header(skb);
115                 skb_reset_network_header(skb);
116                 skb_reset_transport_header(skb);
117         }
118
119         return true;
120 }
121
122 /* Drop a given socketbuffer if it does not contain a valid CAN frame. */
123 static inline bool can_dropped_invalid_skb(struct net_device *dev,
124                                           struct sk_buff *skb)
125 {
126         const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
127
128         if (skb->protocol == htons(ETH_P_CAN)) {
129                 if (unlikely(skb->len != CAN_MTU ||
130                              cfd->len > CAN_MAX_DLEN))
131                         goto inval_skb;
132         } else if (skb->protocol == htons(ETH_P_CANFD)) {
133                 if (unlikely(skb->len != CANFD_MTU ||
134                              cfd->len > CANFD_MAX_DLEN))
135                         goto inval_skb;
136         } else
137                 goto inval_skb;
138
139         if (!can_skb_headroom_valid(dev, skb))
140                 goto inval_skb;
141
142         return false;
143
144 inval_skb:
145         kfree_skb(skb);
146         dev->stats.tx_dropped++;
147         return true;
148 }
149
150 static inline bool can_is_canfd_skb(const struct sk_buff *skb)
151 {
152         /* the CAN specific type of skb is identified by its data length */
153         return skb->len == CANFD_MTU;
154 }
155
156 #endif /* !_CAN_SKB_H */