2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
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 <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 inline struct cfpkt_priv_data
*cfpkt_priv(struct cfpkt
*pkt
)
47 return (struct cfpkt_priv_data
*) pkt
->skb
.cb
;
50 inline bool is_erronous(struct cfpkt
*pkt
)
52 return cfpkt_priv(pkt
)->erronous
;
55 inline struct sk_buff
*pkt_to_skb(struct cfpkt
*pkt
)
60 inline struct cfpkt
*skb_to_pkt(struct sk_buff
*skb
)
62 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 if (likely(in_interrupt()))
85 skb
= alloc_skb(len
+ pfx
, GFP_ATOMIC
);
87 skb
= alloc_skb(len
+ pfx
, GFP_KERNEL
);
89 if (unlikely(skb
== NULL
))
92 skb_reserve(skb
, pfx
);
93 return skb_to_pkt(skb
);
96 inline struct cfpkt
*cfpkt_create(u16 len
)
98 return cfpkt_create_pfx(len
+ PKT_POSTFIX
, PKT_PREFIX
);
100 EXPORT_SYMBOL(cfpkt_create
);
102 void cfpkt_destroy(struct cfpkt
*pkt
)
104 struct sk_buff
*skb
= pkt_to_skb(pkt
);
107 EXPORT_SYMBOL(cfpkt_destroy
);
109 inline bool cfpkt_more(struct cfpkt
*pkt
)
111 struct sk_buff
*skb
= pkt_to_skb(pkt
);
114 EXPORT_SYMBOL(cfpkt_more
);
116 int cfpkt_peek_head(struct cfpkt
*pkt
, void *data
, u16 len
)
118 struct sk_buff
*skb
= pkt_to_skb(pkt
);
119 if (skb_headlen(skb
) >= len
) {
120 memcpy(data
, skb
->data
, len
);
123 return !cfpkt_extr_head(pkt
, data
, len
) &&
124 !cfpkt_add_head(pkt
, data
, len
);
126 EXPORT_SYMBOL(cfpkt_peek_head
);
128 int cfpkt_extr_head(struct cfpkt
*pkt
, void *data
, u16 len
)
130 struct sk_buff
*skb
= pkt_to_skb(pkt
);
132 if (unlikely(is_erronous(pkt
)))
135 if (unlikely(len
> skb
->len
)) {
136 PKT_ERROR(pkt
, "read beyond end of packet\n");
140 if (unlikely(len
> skb_headlen(skb
))) {
141 if (unlikely(skb_linearize(skb
) != 0)) {
142 PKT_ERROR(pkt
, "linearize failed\n");
146 from
= skb_pull(skb
, len
);
148 memcpy(data
, from
, len
);
151 EXPORT_SYMBOL(cfpkt_extr_head
);
153 int cfpkt_extr_trail(struct cfpkt
*pkt
, void *dta
, u16 len
)
155 struct sk_buff
*skb
= pkt_to_skb(pkt
);
158 if (unlikely(is_erronous(pkt
)))
161 if (unlikely(skb_linearize(skb
) != 0)) {
162 PKT_ERROR(pkt
, "linearize failed\n");
165 if (unlikely(skb
->data
+ len
> skb_tail_pointer(skb
))) {
166 PKT_ERROR(pkt
, "read beyond end of packet\n");
169 from
= skb_tail_pointer(skb
) - len
;
170 skb_trim(skb
, skb
->len
- len
);
171 memcpy(data
, from
, len
);
174 EXPORT_SYMBOL(cfpkt_extr_trail
);
176 int cfpkt_pad_trail(struct cfpkt
*pkt
, u16 len
)
178 return cfpkt_add_body(pkt
, NULL
, len
);
180 EXPORT_SYMBOL(cfpkt_pad_trail
);
182 int cfpkt_add_body(struct cfpkt
*pkt
, const void *data
, u16 len
)
184 struct sk_buff
*skb
= pkt_to_skb(pkt
);
185 struct sk_buff
*lastskb
;
190 if (unlikely(is_erronous(pkt
)))
195 /* Check whether we need to add space at the tail */
196 if (unlikely(skb_tailroom(skb
) < len
)) {
197 if (likely(len
< PKT_LEN_WHEN_EXTENDING
))
198 addlen
= PKT_LEN_WHEN_EXTENDING
;
203 /* Check whether we need to change the SKB before writing to the tail */
204 if (unlikely((addlen
> 0) || skb_cloned(skb
) || skb_shared(skb
))) {
206 /* Make sure data is writable */
207 if (unlikely(skb_cow_data(skb
, addlen
, &lastskb
) < 0)) {
208 PKT_ERROR(pkt
, "cow failed\n");
212 * Is the SKB non-linear after skb_cow_data()? If so, we are
213 * going to add data to the last SKB, so we need to adjust
214 * lengths of the top SKB.
216 if (lastskb
!= skb
) {
217 pr_warn("Packet is non-linear\n");
219 skb
->data_len
+= len
;
223 /* All set to put the last SKB and optionally write data there. */
224 to
= skb_put(lastskb
, len
);
226 memcpy(to
, data
, len
);
229 EXPORT_SYMBOL(cfpkt_add_body
);
231 inline int cfpkt_addbdy(struct cfpkt
*pkt
, u8 data
)
233 return cfpkt_add_body(pkt
, &data
, 1);
235 EXPORT_SYMBOL(cfpkt_addbdy
);
237 int cfpkt_add_head(struct cfpkt
*pkt
, const void *data2
, u16 len
)
239 struct sk_buff
*skb
= pkt_to_skb(pkt
);
240 struct sk_buff
*lastskb
;
242 const u8
*data
= data2
;
244 if (unlikely(is_erronous(pkt
)))
246 if (unlikely(skb_headroom(skb
) < len
)) {
247 PKT_ERROR(pkt
, "no headroom\n");
251 /* Make sure data is writable */
252 ret
= skb_cow_data(skb
, 0, &lastskb
);
253 if (unlikely(ret
< 0)) {
254 PKT_ERROR(pkt
, "cow failed\n");
258 to
= skb_push(skb
, len
);
259 memcpy(to
, data
, len
);
262 EXPORT_SYMBOL(cfpkt_add_head
);
264 inline int cfpkt_add_trail(struct cfpkt
*pkt
, const void *data
, u16 len
)
266 return cfpkt_add_body(pkt
, data
, len
);
268 EXPORT_SYMBOL(cfpkt_add_trail
);
270 inline u16
cfpkt_getlen(struct cfpkt
*pkt
)
272 struct sk_buff
*skb
= pkt_to_skb(pkt
);
275 EXPORT_SYMBOL(cfpkt_getlen
);
277 inline u16
cfpkt_iterate(struct cfpkt
*pkt
,
278 u16 (*iter_func
)(u16
, void *, u16
),
282 * Don't care about the performance hit of linearizing,
283 * Checksum should not be used on high-speed interfaces anyway.
285 if (unlikely(is_erronous(pkt
)))
287 if (unlikely(skb_linearize(&pkt
->skb
) != 0)) {
288 PKT_ERROR(pkt
, "linearize failed\n");
291 return iter_func(data
, pkt
->skb
.data
, cfpkt_getlen(pkt
));
293 EXPORT_SYMBOL(cfpkt_iterate
);
295 int cfpkt_setlen(struct cfpkt
*pkt
, u16 len
)
297 struct sk_buff
*skb
= pkt_to_skb(pkt
);
300 if (unlikely(is_erronous(pkt
)))
303 if (likely(len
<= skb
->len
)) {
304 if (unlikely(skb
->data_len
))
305 ___pskb_trim(skb
, len
);
309 return cfpkt_getlen(pkt
);
312 /* Need to expand SKB */
313 if (unlikely(!cfpkt_pad_trail(pkt
, len
- skb
->len
)))
314 PKT_ERROR(pkt
, "skb_pad_trail failed\n");
316 return cfpkt_getlen(pkt
);
318 EXPORT_SYMBOL(cfpkt_setlen
);
320 struct cfpkt
*cfpkt_create_uplink(const unsigned char *data
, unsigned int len
)
322 struct cfpkt
*pkt
= cfpkt_create_pfx(len
+ PKT_POSTFIX
, PKT_PREFIX
);
325 if (unlikely(data
!= NULL
))
326 cfpkt_add_body(pkt
, data
, len
);
329 EXPORT_SYMBOL(cfpkt_create_uplink
);
331 struct cfpkt
*cfpkt_append(struct cfpkt
*dstpkt
,
332 struct cfpkt
*addpkt
,
335 struct sk_buff
*dst
= pkt_to_skb(dstpkt
);
336 struct sk_buff
*add
= pkt_to_skb(addpkt
);
337 u16 addlen
= skb_headlen(add
);
342 if (unlikely(is_erronous(dstpkt
) || is_erronous(addpkt
))) {
345 if (expectlen
> addlen
)
346 neededtailspace
= expectlen
;
348 neededtailspace
= addlen
;
350 if (dst
->tail
+ neededtailspace
> dst
->end
) {
351 /* Create a dumplicate of 'dst' with more tail space */
352 struct cfpkt
*tmppkt
;
353 dstlen
= skb_headlen(dst
);
354 createlen
= dstlen
+ neededtailspace
;
355 tmppkt
= cfpkt_create(createlen
+ PKT_PREFIX
+ PKT_POSTFIX
);
358 tmp
= pkt_to_skb(tmppkt
);
359 skb_set_tail_pointer(tmp
, dstlen
);
361 memcpy(tmp
->data
, dst
->data
, dstlen
);
362 cfpkt_destroy(dstpkt
);
365 memcpy(skb_tail_pointer(dst
), add
->data
, skb_headlen(add
));
366 cfpkt_destroy(addpkt
);
369 return skb_to_pkt(dst
);
371 EXPORT_SYMBOL(cfpkt_append
);
373 struct cfpkt
*cfpkt_split(struct cfpkt
*pkt
, u16 pos
)
375 struct sk_buff
*skb2
;
376 struct sk_buff
*skb
= pkt_to_skb(pkt
);
377 struct cfpkt
*tmppkt
;
378 u8
*split
= skb
->data
+ pos
;
379 u16 len2nd
= skb_tail_pointer(skb
) - split
;
381 if (unlikely(is_erronous(pkt
)))
384 if (skb
->data
+ pos
> skb_tail_pointer(skb
)) {
385 PKT_ERROR(pkt
, "trying to split beyond end of packet\n");
389 /* Create a new packet for the second part of the data */
390 tmppkt
= cfpkt_create_pfx(len2nd
+ PKT_PREFIX
+ PKT_POSTFIX
,
394 skb2
= pkt_to_skb(tmppkt
);
400 /* Reduce the length of the original packet */
401 skb_set_tail_pointer(skb
, pos
);
404 memcpy(skb2
->data
, split
, len2nd
);
405 skb2
->tail
+= len2nd
;
407 return skb_to_pkt(skb2
);
409 EXPORT_SYMBOL(cfpkt_split
);
411 char *cfpkt_log_pkt(struct cfpkt
*pkt
, char *buf
, int buflen
)
413 struct sk_buff
*skb
= pkt_to_skb(pkt
);
418 * Sanity check buffer length, it needs to be at least as large as
419 * the header info: ~=50+ bytes
424 snprintf(buf
, buflen
, "%s: pkt:%p len:%ld(%ld+%ld) {%ld,%ld} data: [",
425 is_erronous(pkt
) ? "ERRONOUS-SKB" :
426 (skb
->data_len
!= 0 ? "COMPLEX-SKB" : "SKB"),
429 (long) (skb_tail_pointer(skb
) - skb
->data
),
430 (long) skb
->data_len
,
431 (long) (skb
->data
- skb
->head
),
432 (long) (skb_tail_pointer(skb
) - skb
->head
));
433 p
= buf
+ strlen(buf
);
435 for (i
= 0; i
< skb_tail_pointer(skb
) - skb
->data
&& i
< 300; i
++) {
436 if (p
> buf
+ buflen
- 10) {
438 p
= buf
+ strlen(buf
);
441 sprintf(p
, "%02x,", skb
->data
[i
]);
442 p
= buf
+ strlen(buf
);
447 EXPORT_SYMBOL(cfpkt_log_pkt
);
449 int cfpkt_raw_append(struct cfpkt
*pkt
, void **buf
, unsigned int buflen
)
451 struct sk_buff
*skb
= pkt_to_skb(pkt
);
452 struct sk_buff
*lastskb
;
454 caif_assert(buf
!= NULL
);
455 if (unlikely(is_erronous(pkt
)))
457 /* Make sure SKB is writable */
458 if (unlikely(skb_cow_data(skb
, 0, &lastskb
) < 0)) {
459 PKT_ERROR(pkt
, "skb_cow_data failed\n");
463 if (unlikely(skb_linearize(skb
) != 0)) {
464 PKT_ERROR(pkt
, "linearize failed\n");
468 if (unlikely(skb_tailroom(skb
) < buflen
)) {
469 PKT_ERROR(pkt
, "buffer too short - failed\n");
473 *buf
= skb_put(skb
, buflen
);
476 EXPORT_SYMBOL(cfpkt_raw_append
);
478 int cfpkt_raw_extract(struct cfpkt
*pkt
, void **buf
, unsigned int buflen
)
480 struct sk_buff
*skb
= pkt_to_skb(pkt
);
482 caif_assert(buf
!= NULL
);
483 if (unlikely(is_erronous(pkt
)))
486 if (unlikely(buflen
> skb
->len
)) {
487 PKT_ERROR(pkt
, "buflen too large - failed\n");
491 if (unlikely(buflen
> skb_headlen(skb
))) {
492 if (unlikely(skb_linearize(skb
) != 0)) {
493 PKT_ERROR(pkt
, "linearize failed\n");
499 skb_pull(skb
, buflen
);
503 EXPORT_SYMBOL(cfpkt_raw_extract
);
505 inline bool cfpkt_erroneous(struct cfpkt
*pkt
)
507 return cfpkt_priv(pkt
)->erronous
;
509 EXPORT_SYMBOL(cfpkt_erroneous
);
511 struct cfpktq
*cfpktq_create(void)
513 struct cfpktq
*q
= kmalloc(sizeof(struct cfpktq
), GFP_ATOMIC
);
516 skb_queue_head_init(&q
->head
);
517 atomic_set(&q
->count
, 0);
518 spin_lock_init(&q
->lock
);
521 EXPORT_SYMBOL(cfpktq_create
);
523 void cfpkt_queue(struct cfpktq
*pktq
, struct cfpkt
*pkt
, unsigned short prio
)
525 atomic_inc(&pktq
->count
);
526 spin_lock(&pktq
->lock
);
527 skb_queue_tail(&pktq
->head
, pkt_to_skb(pkt
));
528 spin_unlock(&pktq
->lock
);
531 EXPORT_SYMBOL(cfpkt_queue
);
533 struct cfpkt
*cfpkt_qpeek(struct cfpktq
*pktq
)
536 spin_lock(&pktq
->lock
);
537 tmp
= skb_to_pkt(skb_peek(&pktq
->head
));
538 spin_unlock(&pktq
->lock
);
541 EXPORT_SYMBOL(cfpkt_qpeek
);
543 struct cfpkt
*cfpkt_dequeue(struct cfpktq
*pktq
)
546 spin_lock(&pktq
->lock
);
547 pkt
= skb_to_pkt(skb_dequeue(&pktq
->head
));
549 atomic_dec(&pktq
->count
);
550 caif_assert(atomic_read(&pktq
->count
) >= 0);
552 spin_unlock(&pktq
->lock
);
555 EXPORT_SYMBOL(cfpkt_dequeue
);
557 int cfpkt_qcount(struct cfpktq
*pktq
)
559 return atomic_read(&pktq
->count
);
561 EXPORT_SYMBOL(cfpkt_qcount
);
563 struct cfpkt
*cfpkt_clone_release(struct cfpkt
*pkt
)
566 clone
= skb_to_pkt(skb_clone(pkt_to_skb(pkt
), GFP_ATOMIC
));
567 /* Free original packet. */
573 EXPORT_SYMBOL(cfpkt_clone_release
);
575 struct caif_payload_info
*cfpkt_info(struct cfpkt
*pkt
)
577 return (struct caif_payload_info
*)&pkt_to_skb(pkt
)->cb
;
579 EXPORT_SYMBOL(cfpkt_info
);