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 <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
;
67 struct cfpkt
*cfpkt_fromnative(enum caif_direction dir
, void *nativepkt
)
69 struct cfpkt
*pkt
= skb_to_pkt(nativepkt
);
70 cfpkt_priv(pkt
)->erronous
= false;
73 EXPORT_SYMBOL(cfpkt_fromnative
);
75 void *cfpkt_tonative(struct cfpkt
*pkt
)
79 EXPORT_SYMBOL(cfpkt_tonative
);
81 static struct cfpkt
*cfpkt_create_pfx(u16 len
, u16 pfx
)
85 if (likely(in_interrupt()))
86 skb
= alloc_skb(len
+ pfx
, GFP_ATOMIC
);
88 skb
= alloc_skb(len
+ pfx
, GFP_KERNEL
);
90 if (unlikely(skb
== NULL
))
93 skb_reserve(skb
, pfx
);
94 return skb_to_pkt(skb
);
97 inline struct cfpkt
*cfpkt_create(u16 len
)
99 return cfpkt_create_pfx(len
+ PKT_POSTFIX
, PKT_PREFIX
);
102 void cfpkt_destroy(struct cfpkt
*pkt
)
104 struct sk_buff
*skb
= pkt_to_skb(pkt
);
109 inline bool cfpkt_more(struct cfpkt
*pkt
)
111 struct sk_buff
*skb
= pkt_to_skb(pkt
);
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
);
127 int cfpkt_extr_head(struct cfpkt
*pkt
, void *data
, u16 len
)
129 struct sk_buff
*skb
= pkt_to_skb(pkt
);
131 if (unlikely(is_erronous(pkt
)))
134 if (unlikely(len
> skb
->len
)) {
135 PKT_ERROR(pkt
, "read beyond end of packet\n");
139 if (unlikely(len
> skb_headlen(skb
))) {
140 if (unlikely(skb_linearize(skb
) != 0)) {
141 PKT_ERROR(pkt
, "linearize failed\n");
145 from
= skb_pull(skb
, len
);
147 memcpy(data
, from
, len
);
151 int cfpkt_extr_trail(struct cfpkt
*pkt
, void *dta
, u16 len
)
153 struct sk_buff
*skb
= pkt_to_skb(pkt
);
156 if (unlikely(is_erronous(pkt
)))
159 if (unlikely(skb_linearize(skb
) != 0)) {
160 PKT_ERROR(pkt
, "linearize failed\n");
163 if (unlikely(skb
->data
+ len
> skb_tail_pointer(skb
))) {
164 PKT_ERROR(pkt
, "read beyond end of packet\n");
167 from
= skb_tail_pointer(skb
) - len
;
168 skb_trim(skb
, skb
->len
- len
);
169 memcpy(data
, from
, len
);
174 int cfpkt_pad_trail(struct cfpkt
*pkt
, u16 len
)
176 return cfpkt_add_body(pkt
, NULL
, len
);
180 int cfpkt_add_body(struct cfpkt
*pkt
, const void *data
, u16 len
)
182 struct sk_buff
*skb
= pkt_to_skb(pkt
);
183 struct sk_buff
*lastskb
;
188 if (unlikely(is_erronous(pkt
)))
193 /* Check whether we need to add space at the tail */
194 if (unlikely(skb_tailroom(skb
) < len
)) {
195 if (likely(len
< PKT_LEN_WHEN_EXTENDING
))
196 addlen
= PKT_LEN_WHEN_EXTENDING
;
201 /* Check whether we need to change the SKB before writing to the tail */
202 if (unlikely((addlen
> 0) || skb_cloned(skb
) || skb_shared(skb
))) {
204 /* Make sure data is writable */
205 if (unlikely(skb_cow_data(skb
, addlen
, &lastskb
) < 0)) {
206 PKT_ERROR(pkt
, "cow failed\n");
210 * Is the SKB non-linear after skb_cow_data()? If so, we are
211 * going to add data to the last SKB, so we need to adjust
212 * lengths of the top SKB.
214 if (lastskb
!= skb
) {
215 pr_warn("Packet is non-linear\n");
217 skb
->data_len
+= len
;
221 /* All set to put the last SKB and optionally write data there. */
222 to
= skb_put(lastskb
, len
);
224 memcpy(to
, data
, len
);
228 inline int cfpkt_addbdy(struct cfpkt
*pkt
, u8 data
)
230 return cfpkt_add_body(pkt
, &data
, 1);
233 int cfpkt_add_head(struct cfpkt
*pkt
, const void *data2
, u16 len
)
235 struct sk_buff
*skb
= pkt_to_skb(pkt
);
236 struct sk_buff
*lastskb
;
238 const u8
*data
= data2
;
240 if (unlikely(is_erronous(pkt
)))
242 if (unlikely(skb_headroom(skb
) < len
)) {
243 PKT_ERROR(pkt
, "no headroom\n");
247 /* Make sure data is writable */
248 ret
= skb_cow_data(skb
, 0, &lastskb
);
249 if (unlikely(ret
< 0)) {
250 PKT_ERROR(pkt
, "cow failed\n");
254 to
= skb_push(skb
, len
);
255 memcpy(to
, data
, len
);
260 inline int cfpkt_add_trail(struct cfpkt
*pkt
, const void *data
, u16 len
)
262 return cfpkt_add_body(pkt
, data
, len
);
266 inline u16
cfpkt_getlen(struct cfpkt
*pkt
)
268 struct sk_buff
*skb
= pkt_to_skb(pkt
);
273 inline u16
cfpkt_iterate(struct cfpkt
*pkt
,
274 u16 (*iter_func
)(u16
, void *, u16
),
278 * Don't care about the performance hit of linearizing,
279 * Checksum should not be used on high-speed interfaces anyway.
281 if (unlikely(is_erronous(pkt
)))
283 if (unlikely(skb_linearize(&pkt
->skb
) != 0)) {
284 PKT_ERROR(pkt
, "linearize failed\n");
287 return iter_func(data
, pkt
->skb
.data
, cfpkt_getlen(pkt
));
291 int cfpkt_setlen(struct cfpkt
*pkt
, u16 len
)
293 struct sk_buff
*skb
= pkt_to_skb(pkt
);
296 if (unlikely(is_erronous(pkt
)))
299 if (likely(len
<= skb
->len
)) {
300 if (unlikely(skb
->data_len
))
301 ___pskb_trim(skb
, len
);
305 return cfpkt_getlen(pkt
);
308 /* Need to expand SKB */
309 if (unlikely(!cfpkt_pad_trail(pkt
, len
- skb
->len
)))
310 PKT_ERROR(pkt
, "skb_pad_trail failed\n");
312 return cfpkt_getlen(pkt
);
315 struct cfpkt
*cfpkt_append(struct cfpkt
*dstpkt
,
316 struct cfpkt
*addpkt
,
319 struct sk_buff
*dst
= pkt_to_skb(dstpkt
);
320 struct sk_buff
*add
= pkt_to_skb(addpkt
);
321 u16 addlen
= skb_headlen(add
);
326 if (unlikely(is_erronous(dstpkt
) || is_erronous(addpkt
))) {
329 if (expectlen
> addlen
)
330 neededtailspace
= expectlen
;
332 neededtailspace
= addlen
;
334 if (dst
->tail
+ neededtailspace
> dst
->end
) {
335 /* Create a dumplicate of 'dst' with more tail space */
336 struct cfpkt
*tmppkt
;
337 dstlen
= skb_headlen(dst
);
338 createlen
= dstlen
+ neededtailspace
;
339 tmppkt
= cfpkt_create(createlen
+ PKT_PREFIX
+ PKT_POSTFIX
);
342 tmp
= pkt_to_skb(tmppkt
);
343 skb_set_tail_pointer(tmp
, dstlen
);
345 memcpy(tmp
->data
, dst
->data
, dstlen
);
346 cfpkt_destroy(dstpkt
);
349 memcpy(skb_tail_pointer(dst
), add
->data
, skb_headlen(add
));
350 cfpkt_destroy(addpkt
);
353 return skb_to_pkt(dst
);
356 struct cfpkt
*cfpkt_split(struct cfpkt
*pkt
, u16 pos
)
358 struct sk_buff
*skb2
;
359 struct sk_buff
*skb
= pkt_to_skb(pkt
);
360 struct cfpkt
*tmppkt
;
361 u8
*split
= skb
->data
+ pos
;
362 u16 len2nd
= skb_tail_pointer(skb
) - split
;
364 if (unlikely(is_erronous(pkt
)))
367 if (skb
->data
+ pos
> skb_tail_pointer(skb
)) {
368 PKT_ERROR(pkt
, "trying to split beyond end of packet\n");
372 /* Create a new packet for the second part of the data */
373 tmppkt
= cfpkt_create_pfx(len2nd
+ PKT_PREFIX
+ PKT_POSTFIX
,
377 skb2
= pkt_to_skb(tmppkt
);
383 /* Reduce the length of the original packet */
384 skb_set_tail_pointer(skb
, pos
);
387 memcpy(skb2
->data
, split
, len2nd
);
388 skb2
->tail
+= len2nd
;
390 return skb_to_pkt(skb2
);
393 bool cfpkt_erroneous(struct cfpkt
*pkt
)
395 return cfpkt_priv(pkt
)->erronous
;
398 struct caif_payload_info
*cfpkt_info(struct cfpkt
*pkt
)
400 return (struct caif_payload_info
*)&pkt_to_skb(pkt
)->cb
;