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 struct sk_buff_head head;
27 /* Lock protects count updates */
32 * net/caif/ is generic and does not
33 * understand SKB, so we do this typecast
39 /* Private data inside SKB */
40 struct cfpkt_priv_data {
41 struct dev_info dev_info;
45 static inline struct cfpkt_priv_data *cfpkt_priv(struct cfpkt *pkt)
47 return (struct cfpkt_priv_data *) pkt->skb.cb;
50 static inline bool is_erronous(struct cfpkt *pkt)
52 return cfpkt_priv(pkt)->erronous;
55 static inline struct sk_buff *pkt_to_skb(struct cfpkt *pkt)
60 static inline struct cfpkt *skb_to_pkt(struct sk_buff *skb)
62 return (struct cfpkt *) skb;
65 struct cfpkt *cfpkt_fromnative(enum caif_direction dir, void *nativepkt)
67 struct cfpkt *pkt = skb_to_pkt(nativepkt);
68 cfpkt_priv(pkt)->erronous = false;
71 EXPORT_SYMBOL(cfpkt_fromnative);
73 void *cfpkt_tonative(struct cfpkt *pkt)
77 EXPORT_SYMBOL(cfpkt_tonative);
79 static struct cfpkt *cfpkt_create_pfx(u16 len, u16 pfx)
83 skb = alloc_skb(len + pfx, GFP_ATOMIC);
84 if (unlikely(skb == NULL))
87 skb_reserve(skb, pfx);
88 return skb_to_pkt(skb);
91 inline struct cfpkt *cfpkt_create(u16 len)
93 return cfpkt_create_pfx(len + PKT_POSTFIX, PKT_PREFIX);
96 void cfpkt_destroy(struct cfpkt *pkt)
98 struct sk_buff *skb = pkt_to_skb(pkt);
102 inline bool cfpkt_more(struct cfpkt *pkt)
104 struct sk_buff *skb = pkt_to_skb(pkt);
108 int cfpkt_peek_head(struct cfpkt *pkt, void *data, u16 len)
110 struct sk_buff *skb = pkt_to_skb(pkt);
111 if (skb_headlen(skb) >= len) {
112 memcpy(data, skb->data, len);
115 return !cfpkt_extr_head(pkt, data, len) &&
116 !cfpkt_add_head(pkt, data, len);
119 int cfpkt_extr_head(struct cfpkt *pkt, void *data, u16 len)
121 struct sk_buff *skb = pkt_to_skb(pkt);
123 if (unlikely(is_erronous(pkt)))
126 if (unlikely(len > skb->len)) {
127 PKT_ERROR(pkt, "read beyond end of packet\n");
131 if (unlikely(len > skb_headlen(skb))) {
132 if (unlikely(skb_linearize(skb) != 0)) {
133 PKT_ERROR(pkt, "linearize failed\n");
137 from = skb_pull(skb, len);
140 memcpy(data, from, len);
143 EXPORT_SYMBOL(cfpkt_extr_head);
145 int cfpkt_extr_trail(struct cfpkt *pkt, void *dta, u16 len)
147 struct sk_buff *skb = pkt_to_skb(pkt);
150 if (unlikely(is_erronous(pkt)))
153 if (unlikely(skb_linearize(skb) != 0)) {
154 PKT_ERROR(pkt, "linearize failed\n");
157 if (unlikely(skb->data + len > skb_tail_pointer(skb))) {
158 PKT_ERROR(pkt, "read beyond end of packet\n");
161 from = skb_tail_pointer(skb) - len;
162 skb_trim(skb, skb->len - len);
163 memcpy(data, from, len);
167 int cfpkt_pad_trail(struct cfpkt *pkt, u16 len)
169 return cfpkt_add_body(pkt, NULL, len);
172 int cfpkt_add_body(struct cfpkt *pkt, const void *data, u16 len)
174 struct sk_buff *skb = pkt_to_skb(pkt);
175 struct sk_buff *lastskb;
180 if (unlikely(is_erronous(pkt)))
185 /* Check whether we need to add space at the tail */
186 if (unlikely(skb_tailroom(skb) < len)) {
187 if (likely(len < PKT_LEN_WHEN_EXTENDING))
188 addlen = PKT_LEN_WHEN_EXTENDING;
193 /* Check whether we need to change the SKB before writing to the tail */
194 if (unlikely((addlen > 0) || skb_cloned(skb) || skb_shared(skb))) {
196 /* Make sure data is writable */
197 if (unlikely(skb_cow_data(skb, addlen, &lastskb) < 0)) {
198 PKT_ERROR(pkt, "cow failed\n");
203 /* All set to put the last SKB and optionally write data there. */
204 to = pskb_put(skb, lastskb, len);
206 memcpy(to, data, len);
210 inline int cfpkt_addbdy(struct cfpkt *pkt, u8 data)
212 return cfpkt_add_body(pkt, &data, 1);
215 int cfpkt_add_head(struct cfpkt *pkt, const void *data2, u16 len)
217 struct sk_buff *skb = pkt_to_skb(pkt);
218 struct sk_buff *lastskb;
220 const u8 *data = data2;
222 if (unlikely(is_erronous(pkt)))
224 if (unlikely(skb_headroom(skb) < len)) {
225 PKT_ERROR(pkt, "no headroom\n");
229 /* Make sure data is writable */
230 ret = skb_cow_data(skb, 0, &lastskb);
231 if (unlikely(ret < 0)) {
232 PKT_ERROR(pkt, "cow failed\n");
236 to = skb_push(skb, len);
237 memcpy(to, data, len);
240 EXPORT_SYMBOL(cfpkt_add_head);
242 inline int cfpkt_add_trail(struct cfpkt *pkt, const void *data, u16 len)
244 return cfpkt_add_body(pkt, data, len);
247 inline u16 cfpkt_getlen(struct cfpkt *pkt)
249 struct sk_buff *skb = pkt_to_skb(pkt);
253 int cfpkt_iterate(struct cfpkt *pkt,
254 u16 (*iter_func)(u16, void *, u16),
258 * Don't care about the performance hit of linearizing,
259 * Checksum should not be used on high-speed interfaces anyway.
261 if (unlikely(is_erronous(pkt)))
263 if (unlikely(skb_linearize(&pkt->skb) != 0)) {
264 PKT_ERROR(pkt, "linearize failed\n");
267 return iter_func(data, pkt->skb.data, cfpkt_getlen(pkt));
270 int cfpkt_setlen(struct cfpkt *pkt, u16 len)
272 struct sk_buff *skb = pkt_to_skb(pkt);
275 if (unlikely(is_erronous(pkt)))
278 if (likely(len <= skb->len)) {
279 if (unlikely(skb->data_len))
280 ___pskb_trim(skb, len);
284 return cfpkt_getlen(pkt);
287 /* Need to expand SKB */
288 if (unlikely(!cfpkt_pad_trail(pkt, len - skb->len)))
289 PKT_ERROR(pkt, "skb_pad_trail failed\n");
291 return cfpkt_getlen(pkt);
294 struct cfpkt *cfpkt_append(struct cfpkt *dstpkt,
295 struct cfpkt *addpkt,
298 struct sk_buff *dst = pkt_to_skb(dstpkt);
299 struct sk_buff *add = pkt_to_skb(addpkt);
300 u16 addlen = skb_headlen(add);
305 if (unlikely(is_erronous(dstpkt) || is_erronous(addpkt))) {
308 if (expectlen > addlen)
309 neededtailspace = expectlen;
311 neededtailspace = addlen;
313 if (dst->tail + neededtailspace > dst->end) {
314 /* Create a dumplicate of 'dst' with more tail space */
315 struct cfpkt *tmppkt;
316 dstlen = skb_headlen(dst);
317 createlen = dstlen + neededtailspace;
318 tmppkt = cfpkt_create(createlen + PKT_PREFIX + PKT_POSTFIX);
321 tmp = pkt_to_skb(tmppkt);
322 skb_put_data(tmp, dst->data, dstlen);
323 cfpkt_destroy(dstpkt);
326 skb_put_data(dst, add->data, skb_headlen(add));
327 cfpkt_destroy(addpkt);
328 return skb_to_pkt(dst);
331 struct cfpkt *cfpkt_split(struct cfpkt *pkt, u16 pos)
333 struct sk_buff *skb2;
334 struct sk_buff *skb = pkt_to_skb(pkt);
335 struct cfpkt *tmppkt;
336 u8 *split = skb->data + pos;
337 u16 len2nd = skb_tail_pointer(skb) - split;
339 if (unlikely(is_erronous(pkt)))
342 if (skb->data + pos > skb_tail_pointer(skb)) {
343 PKT_ERROR(pkt, "trying to split beyond end of packet\n");
347 /* Create a new packet for the second part of the data */
348 tmppkt = cfpkt_create_pfx(len2nd + PKT_PREFIX + PKT_POSTFIX,
352 skb2 = pkt_to_skb(tmppkt);
358 skb_put_data(skb2, split, len2nd);
360 /* Reduce the length of the original packet */
363 skb2->priority = skb->priority;
364 return skb_to_pkt(skb2);
367 bool cfpkt_erroneous(struct cfpkt *pkt)
369 return cfpkt_priv(pkt)->erronous;
372 struct caif_payload_info *cfpkt_info(struct cfpkt *pkt)
374 return (struct caif_payload_info *)&pkt_to_skb(pkt)->cb;
376 EXPORT_SYMBOL(cfpkt_info);
378 void cfpkt_set_prio(struct cfpkt *pkt, int prio)
380 pkt_to_skb(pkt)->priority = prio;
382 EXPORT_SYMBOL(cfpkt_set_prio);