1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson AB 2010
4 * Author: Sjur Brendeland
7 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
9 #include <linux/string.h>
10 #include <linux/skbuff.h>
11 #include <linux/export.h>
12 #include <net/caif/cfpkt.h>
16 #define PKT_LEN_WHEN_EXTENDING 128
17 #define PKT_ERROR(pkt, errmsg) \
19 cfpkt_priv(pkt)->erronous = true; \
20 skb_reset_tail_pointer(&pkt->skb); \
25 * net/caif/ is generic and does not
26 * understand SKB, so we do this typecast
32 /* Private data inside SKB */
33 struct cfpkt_priv_data
{
34 struct dev_info dev_info
;
38 static inline struct cfpkt_priv_data
*cfpkt_priv(struct cfpkt
*pkt
)
40 return (struct cfpkt_priv_data
*) pkt
->skb
.cb
;
43 static inline bool is_erronous(struct cfpkt
*pkt
)
45 return cfpkt_priv(pkt
)->erronous
;
48 static inline struct sk_buff
*pkt_to_skb(struct cfpkt
*pkt
)
53 static inline struct cfpkt
*skb_to_pkt(struct sk_buff
*skb
)
55 return (struct cfpkt
*) skb
;
58 struct cfpkt
*cfpkt_fromnative(enum caif_direction dir
, void *nativepkt
)
60 struct cfpkt
*pkt
= skb_to_pkt(nativepkt
);
61 cfpkt_priv(pkt
)->erronous
= false;
64 EXPORT_SYMBOL(cfpkt_fromnative
);
66 void *cfpkt_tonative(struct cfpkt
*pkt
)
70 EXPORT_SYMBOL(cfpkt_tonative
);
72 static struct cfpkt
*cfpkt_create_pfx(u16 len
, u16 pfx
)
76 skb
= alloc_skb(len
+ pfx
, GFP_ATOMIC
);
77 if (unlikely(skb
== NULL
))
80 skb_reserve(skb
, pfx
);
81 return skb_to_pkt(skb
);
84 inline struct cfpkt
*cfpkt_create(u16 len
)
86 return cfpkt_create_pfx(len
+ PKT_POSTFIX
, PKT_PREFIX
);
89 void cfpkt_destroy(struct cfpkt
*pkt
)
91 struct sk_buff
*skb
= pkt_to_skb(pkt
);
95 inline bool cfpkt_more(struct cfpkt
*pkt
)
97 struct sk_buff
*skb
= pkt_to_skb(pkt
);
101 int cfpkt_peek_head(struct cfpkt
*pkt
, void *data
, u16 len
)
103 struct sk_buff
*skb
= pkt_to_skb(pkt
);
104 if (skb_headlen(skb
) >= len
) {
105 memcpy(data
, skb
->data
, len
);
108 return !cfpkt_extr_head(pkt
, data
, len
) &&
109 !cfpkt_add_head(pkt
, data
, len
);
112 int cfpkt_extr_head(struct cfpkt
*pkt
, void *data
, u16 len
)
114 struct sk_buff
*skb
= pkt_to_skb(pkt
);
116 if (unlikely(is_erronous(pkt
)))
119 if (unlikely(len
> skb
->len
)) {
120 PKT_ERROR(pkt
, "read beyond end of packet\n");
124 if (unlikely(len
> skb_headlen(skb
))) {
125 if (unlikely(skb_linearize(skb
) != 0)) {
126 PKT_ERROR(pkt
, "linearize failed\n");
130 from
= skb_pull(skb
, len
);
133 memcpy(data
, from
, len
);
136 EXPORT_SYMBOL(cfpkt_extr_head
);
138 int cfpkt_extr_trail(struct cfpkt
*pkt
, void *dta
, u16 len
)
140 struct sk_buff
*skb
= pkt_to_skb(pkt
);
143 if (unlikely(is_erronous(pkt
)))
146 if (unlikely(skb_linearize(skb
) != 0)) {
147 PKT_ERROR(pkt
, "linearize failed\n");
150 if (unlikely(skb
->data
+ len
> skb_tail_pointer(skb
))) {
151 PKT_ERROR(pkt
, "read beyond end of packet\n");
154 from
= skb_tail_pointer(skb
) - len
;
155 skb_trim(skb
, skb
->len
- len
);
156 memcpy(data
, from
, len
);
160 int cfpkt_pad_trail(struct cfpkt
*pkt
, u16 len
)
162 return cfpkt_add_body(pkt
, NULL
, len
);
165 int cfpkt_add_body(struct cfpkt
*pkt
, const void *data
, u16 len
)
167 struct sk_buff
*skb
= pkt_to_skb(pkt
);
168 struct sk_buff
*lastskb
;
173 if (unlikely(is_erronous(pkt
)))
178 /* Check whether we need to add space at the tail */
179 if (unlikely(skb_tailroom(skb
) < len
)) {
180 if (likely(len
< PKT_LEN_WHEN_EXTENDING
))
181 addlen
= PKT_LEN_WHEN_EXTENDING
;
186 /* Check whether we need to change the SKB before writing to the tail */
187 if (unlikely((addlen
> 0) || skb_cloned(skb
) || skb_shared(skb
))) {
189 /* Make sure data is writable */
190 if (unlikely(skb_cow_data(skb
, addlen
, &lastskb
) < 0)) {
191 PKT_ERROR(pkt
, "cow failed\n");
196 /* All set to put the last SKB and optionally write data there. */
197 to
= pskb_put(skb
, lastskb
, len
);
199 memcpy(to
, data
, len
);
203 inline int cfpkt_addbdy(struct cfpkt
*pkt
, u8 data
)
205 return cfpkt_add_body(pkt
, &data
, 1);
208 int cfpkt_add_head(struct cfpkt
*pkt
, const void *data2
, u16 len
)
210 struct sk_buff
*skb
= pkt_to_skb(pkt
);
211 struct sk_buff
*lastskb
;
213 const u8
*data
= data2
;
215 if (unlikely(is_erronous(pkt
)))
217 if (unlikely(skb_headroom(skb
) < len
)) {
218 PKT_ERROR(pkt
, "no headroom\n");
222 /* Make sure data is writable */
223 ret
= skb_cow_data(skb
, 0, &lastskb
);
224 if (unlikely(ret
< 0)) {
225 PKT_ERROR(pkt
, "cow failed\n");
229 to
= skb_push(skb
, len
);
230 memcpy(to
, data
, len
);
233 EXPORT_SYMBOL(cfpkt_add_head
);
235 inline int cfpkt_add_trail(struct cfpkt
*pkt
, const void *data
, u16 len
)
237 return cfpkt_add_body(pkt
, data
, len
);
240 inline u16
cfpkt_getlen(struct cfpkt
*pkt
)
242 struct sk_buff
*skb
= pkt_to_skb(pkt
);
246 int cfpkt_iterate(struct cfpkt
*pkt
,
247 u16 (*iter_func
)(u16
, void *, u16
),
251 * Don't care about the performance hit of linearizing,
252 * Checksum should not be used on high-speed interfaces anyway.
254 if (unlikely(is_erronous(pkt
)))
256 if (unlikely(skb_linearize(&pkt
->skb
) != 0)) {
257 PKT_ERROR(pkt
, "linearize failed\n");
260 return iter_func(data
, pkt
->skb
.data
, cfpkt_getlen(pkt
));
263 int cfpkt_setlen(struct cfpkt
*pkt
, u16 len
)
265 struct sk_buff
*skb
= pkt_to_skb(pkt
);
268 if (unlikely(is_erronous(pkt
)))
271 if (likely(len
<= skb
->len
)) {
272 if (unlikely(skb
->data_len
))
273 ___pskb_trim(skb
, len
);
277 return cfpkt_getlen(pkt
);
280 /* Need to expand SKB */
281 if (unlikely(!cfpkt_pad_trail(pkt
, len
- skb
->len
)))
282 PKT_ERROR(pkt
, "skb_pad_trail failed\n");
284 return cfpkt_getlen(pkt
);
287 struct cfpkt
*cfpkt_append(struct cfpkt
*dstpkt
,
288 struct cfpkt
*addpkt
,
291 struct sk_buff
*dst
= pkt_to_skb(dstpkt
);
292 struct sk_buff
*add
= pkt_to_skb(addpkt
);
293 u16 addlen
= skb_headlen(add
);
298 if (unlikely(is_erronous(dstpkt
) || is_erronous(addpkt
))) {
302 neededtailspace
= max(expectlen
, addlen
);
304 if (dst
->tail
+ neededtailspace
> dst
->end
) {
305 /* Create a dumplicate of 'dst' with more tail space */
306 struct cfpkt
*tmppkt
;
307 dstlen
= skb_headlen(dst
);
308 createlen
= dstlen
+ neededtailspace
;
309 tmppkt
= cfpkt_create(createlen
+ PKT_PREFIX
+ PKT_POSTFIX
);
312 tmp
= pkt_to_skb(tmppkt
);
313 skb_put_data(tmp
, dst
->data
, dstlen
);
314 cfpkt_destroy(dstpkt
);
317 skb_put_data(dst
, add
->data
, skb_headlen(add
));
318 cfpkt_destroy(addpkt
);
319 return skb_to_pkt(dst
);
322 struct cfpkt
*cfpkt_split(struct cfpkt
*pkt
, u16 pos
)
324 struct sk_buff
*skb2
;
325 struct sk_buff
*skb
= pkt_to_skb(pkt
);
326 struct cfpkt
*tmppkt
;
327 u8
*split
= skb
->data
+ pos
;
328 u16 len2nd
= skb_tail_pointer(skb
) - split
;
330 if (unlikely(is_erronous(pkt
)))
333 if (skb
->data
+ pos
> skb_tail_pointer(skb
)) {
334 PKT_ERROR(pkt
, "trying to split beyond end of packet\n");
338 /* Create a new packet for the second part of the data */
339 tmppkt
= cfpkt_create_pfx(len2nd
+ PKT_PREFIX
+ PKT_POSTFIX
,
343 skb2
= pkt_to_skb(tmppkt
);
349 skb_put_data(skb2
, split
, len2nd
);
351 /* Reduce the length of the original packet */
354 skb2
->priority
= skb
->priority
;
355 return skb_to_pkt(skb2
);
358 bool cfpkt_erroneous(struct cfpkt
*pkt
)
360 return cfpkt_priv(pkt
)->erronous
;
363 struct caif_payload_info
*cfpkt_info(struct cfpkt
*pkt
)
365 return (struct caif_payload_info
*)&pkt_to_skb(pkt
)->cb
;
367 EXPORT_SYMBOL(cfpkt_info
);
369 void cfpkt_set_prio(struct cfpkt
*pkt
, int prio
)
371 pkt_to_skb(pkt
)->priority
= prio
;
373 EXPORT_SYMBOL(cfpkt_set_prio
);