2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland
4 * License terms: GNU General Public License (GPL) version 2
7 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
9 #include <linux/string.h>
10 #include <linux/skbuff.h>
11 #include <linux/hardirq.h>
12 #include <linux/export.h>
13 #include <net/caif/cfpkt.h>
17 #define PKT_LEN_WHEN_EXTENDING 128
18 #define PKT_ERROR(pkt, errmsg) \
20 cfpkt_priv(pkt)->erronous = true; \
21 skb_reset_tail_pointer(&pkt->skb); \
26 struct sk_buff_head head
;
28 /* Lock protects count updates */
33 * net/caif/ is generic and does not
34 * understand SKB, so we do this typecast
40 /* Private data inside SKB */
41 struct cfpkt_priv_data
{
42 struct dev_info dev_info
;
46 static inline struct cfpkt_priv_data
*cfpkt_priv(struct cfpkt
*pkt
)
48 return (struct cfpkt_priv_data
*) pkt
->skb
.cb
;
51 static inline bool is_erronous(struct cfpkt
*pkt
)
53 return cfpkt_priv(pkt
)->erronous
;
56 static inline struct sk_buff
*pkt_to_skb(struct cfpkt
*pkt
)
61 static inline struct cfpkt
*skb_to_pkt(struct sk_buff
*skb
)
63 return (struct cfpkt
*) skb
;
66 struct cfpkt
*cfpkt_fromnative(enum caif_direction dir
, void *nativepkt
)
68 struct cfpkt
*pkt
= skb_to_pkt(nativepkt
);
69 cfpkt_priv(pkt
)->erronous
= false;
72 EXPORT_SYMBOL(cfpkt_fromnative
);
74 void *cfpkt_tonative(struct cfpkt
*pkt
)
78 EXPORT_SYMBOL(cfpkt_tonative
);
80 static struct cfpkt
*cfpkt_create_pfx(u16 len
, u16 pfx
)
84 skb
= alloc_skb(len
+ pfx
, GFP_ATOMIC
);
85 if (unlikely(skb
== NULL
))
88 skb_reserve(skb
, pfx
);
89 return skb_to_pkt(skb
);
92 inline struct cfpkt
*cfpkt_create(u16 len
)
94 return cfpkt_create_pfx(len
+ PKT_POSTFIX
, PKT_PREFIX
);
97 void cfpkt_destroy(struct cfpkt
*pkt
)
99 struct sk_buff
*skb
= pkt_to_skb(pkt
);
103 inline bool cfpkt_more(struct cfpkt
*pkt
)
105 struct sk_buff
*skb
= pkt_to_skb(pkt
);
109 int cfpkt_peek_head(struct cfpkt
*pkt
, void *data
, u16 len
)
111 struct sk_buff
*skb
= pkt_to_skb(pkt
);
112 if (skb_headlen(skb
) >= len
) {
113 memcpy(data
, skb
->data
, len
);
116 return !cfpkt_extr_head(pkt
, data
, len
) &&
117 !cfpkt_add_head(pkt
, data
, len
);
120 int cfpkt_extr_head(struct cfpkt
*pkt
, void *data
, u16 len
)
122 struct sk_buff
*skb
= pkt_to_skb(pkt
);
124 if (unlikely(is_erronous(pkt
)))
127 if (unlikely(len
> skb
->len
)) {
128 PKT_ERROR(pkt
, "read beyond end of packet\n");
132 if (unlikely(len
> skb_headlen(skb
))) {
133 if (unlikely(skb_linearize(skb
) != 0)) {
134 PKT_ERROR(pkt
, "linearize failed\n");
138 from
= skb_pull(skb
, len
);
141 memcpy(data
, from
, len
);
144 EXPORT_SYMBOL(cfpkt_extr_head
);
146 int cfpkt_extr_trail(struct cfpkt
*pkt
, void *dta
, u16 len
)
148 struct sk_buff
*skb
= pkt_to_skb(pkt
);
151 if (unlikely(is_erronous(pkt
)))
154 if (unlikely(skb_linearize(skb
) != 0)) {
155 PKT_ERROR(pkt
, "linearize failed\n");
158 if (unlikely(skb
->data
+ len
> skb_tail_pointer(skb
))) {
159 PKT_ERROR(pkt
, "read beyond end of packet\n");
162 from
= skb_tail_pointer(skb
) - len
;
163 skb_trim(skb
, skb
->len
- len
);
164 memcpy(data
, from
, len
);
168 int cfpkt_pad_trail(struct cfpkt
*pkt
, u16 len
)
170 return cfpkt_add_body(pkt
, NULL
, len
);
173 int cfpkt_add_body(struct cfpkt
*pkt
, const void *data
, u16 len
)
175 struct sk_buff
*skb
= pkt_to_skb(pkt
);
176 struct sk_buff
*lastskb
;
181 if (unlikely(is_erronous(pkt
)))
186 /* Check whether we need to add space at the tail */
187 if (unlikely(skb_tailroom(skb
) < len
)) {
188 if (likely(len
< PKT_LEN_WHEN_EXTENDING
))
189 addlen
= PKT_LEN_WHEN_EXTENDING
;
194 /* Check whether we need to change the SKB before writing to the tail */
195 if (unlikely((addlen
> 0) || skb_cloned(skb
) || skb_shared(skb
))) {
197 /* Make sure data is writable */
198 if (unlikely(skb_cow_data(skb
, addlen
, &lastskb
) < 0)) {
199 PKT_ERROR(pkt
, "cow failed\n");
204 /* All set to put the last SKB and optionally write data there. */
205 to
= pskb_put(skb
, lastskb
, len
);
207 memcpy(to
, data
, len
);
211 inline int cfpkt_addbdy(struct cfpkt
*pkt
, u8 data
)
213 return cfpkt_add_body(pkt
, &data
, 1);
216 int cfpkt_add_head(struct cfpkt
*pkt
, const void *data2
, u16 len
)
218 struct sk_buff
*skb
= pkt_to_skb(pkt
);
219 struct sk_buff
*lastskb
;
221 const u8
*data
= data2
;
223 if (unlikely(is_erronous(pkt
)))
225 if (unlikely(skb_headroom(skb
) < len
)) {
226 PKT_ERROR(pkt
, "no headroom\n");
230 /* Make sure data is writable */
231 ret
= skb_cow_data(skb
, 0, &lastskb
);
232 if (unlikely(ret
< 0)) {
233 PKT_ERROR(pkt
, "cow failed\n");
237 to
= skb_push(skb
, len
);
238 memcpy(to
, data
, len
);
241 EXPORT_SYMBOL(cfpkt_add_head
);
243 inline int cfpkt_add_trail(struct cfpkt
*pkt
, const void *data
, u16 len
)
245 return cfpkt_add_body(pkt
, data
, len
);
248 inline u16
cfpkt_getlen(struct cfpkt
*pkt
)
250 struct sk_buff
*skb
= pkt_to_skb(pkt
);
254 int cfpkt_iterate(struct cfpkt
*pkt
,
255 u16 (*iter_func
)(u16
, void *, u16
),
259 * Don't care about the performance hit of linearizing,
260 * Checksum should not be used on high-speed interfaces anyway.
262 if (unlikely(is_erronous(pkt
)))
264 if (unlikely(skb_linearize(&pkt
->skb
) != 0)) {
265 PKT_ERROR(pkt
, "linearize failed\n");
268 return iter_func(data
, pkt
->skb
.data
, cfpkt_getlen(pkt
));
271 int cfpkt_setlen(struct cfpkt
*pkt
, u16 len
)
273 struct sk_buff
*skb
= pkt_to_skb(pkt
);
276 if (unlikely(is_erronous(pkt
)))
279 if (likely(len
<= skb
->len
)) {
280 if (unlikely(skb
->data_len
))
281 ___pskb_trim(skb
, len
);
285 return cfpkt_getlen(pkt
);
288 /* Need to expand SKB */
289 if (unlikely(!cfpkt_pad_trail(pkt
, len
- skb
->len
)))
290 PKT_ERROR(pkt
, "skb_pad_trail failed\n");
292 return cfpkt_getlen(pkt
);
295 struct cfpkt
*cfpkt_append(struct cfpkt
*dstpkt
,
296 struct cfpkt
*addpkt
,
299 struct sk_buff
*dst
= pkt_to_skb(dstpkt
);
300 struct sk_buff
*add
= pkt_to_skb(addpkt
);
301 u16 addlen
= skb_headlen(add
);
306 if (unlikely(is_erronous(dstpkt
) || is_erronous(addpkt
))) {
309 if (expectlen
> addlen
)
310 neededtailspace
= expectlen
;
312 neededtailspace
= addlen
;
314 if (dst
->tail
+ neededtailspace
> dst
->end
) {
315 /* Create a dumplicate of 'dst' with more tail space */
316 struct cfpkt
*tmppkt
;
317 dstlen
= skb_headlen(dst
);
318 createlen
= dstlen
+ neededtailspace
;
319 tmppkt
= cfpkt_create(createlen
+ PKT_PREFIX
+ PKT_POSTFIX
);
322 tmp
= pkt_to_skb(tmppkt
);
323 skb_set_tail_pointer(tmp
, dstlen
);
325 memcpy(tmp
->data
, dst
->data
, dstlen
);
326 cfpkt_destroy(dstpkt
);
329 memcpy(skb_tail_pointer(dst
), add
->data
, skb_headlen(add
));
330 cfpkt_destroy(addpkt
);
333 return skb_to_pkt(dst
);
336 struct cfpkt
*cfpkt_split(struct cfpkt
*pkt
, u16 pos
)
338 struct sk_buff
*skb2
;
339 struct sk_buff
*skb
= pkt_to_skb(pkt
);
340 struct cfpkt
*tmppkt
;
341 u8
*split
= skb
->data
+ pos
;
342 u16 len2nd
= skb_tail_pointer(skb
) - split
;
344 if (unlikely(is_erronous(pkt
)))
347 if (skb
->data
+ pos
> skb_tail_pointer(skb
)) {
348 PKT_ERROR(pkt
, "trying to split beyond end of packet\n");
352 /* Create a new packet for the second part of the data */
353 tmppkt
= cfpkt_create_pfx(len2nd
+ PKT_PREFIX
+ PKT_POSTFIX
,
357 skb2
= pkt_to_skb(tmppkt
);
363 /* Reduce the length of the original packet */
364 skb_set_tail_pointer(skb
, pos
);
367 memcpy(skb2
->data
, split
, len2nd
);
368 skb2
->tail
+= len2nd
;
370 skb2
->priority
= skb
->priority
;
371 return skb_to_pkt(skb2
);
374 bool cfpkt_erroneous(struct cfpkt
*pkt
)
376 return cfpkt_priv(pkt
)->erronous
;
379 struct caif_payload_info
*cfpkt_info(struct cfpkt
*pkt
)
381 return (struct caif_payload_info
*)&pkt_to_skb(pkt
)->cb
;
383 EXPORT_SYMBOL(cfpkt_info
);
385 void cfpkt_set_prio(struct cfpkt
*pkt
, int prio
)
387 pkt_to_skb(pkt
)->priority
= prio
;
389 EXPORT_SYMBOL(cfpkt_set_prio
);