scripts/decode_stacktrace: match basepath using shell prefix operator, not regex
[linux/fpc-iii.git] / net / caif / cfpkt_skbuff.c
blob71b6ab240dea26b228be35ba24ec3f43772f19f7
1 /*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland
4 * License terms: GNU General Public License (GPL) version 2
5 */
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>
15 #define PKT_PREFIX 48
16 #define PKT_POSTFIX 2
17 #define PKT_LEN_WHEN_EXTENDING 128
18 #define PKT_ERROR(pkt, errmsg) \
19 do { \
20 cfpkt_priv(pkt)->erronous = true; \
21 skb_reset_tail_pointer(&pkt->skb); \
22 pr_warn(errmsg); \
23 } while (0)
25 struct cfpktq {
26 struct sk_buff_head head;
27 atomic_t count;
28 /* Lock protects count updates */
29 spinlock_t lock;
33 * net/caif/ is generic and does not
34 * understand SKB, so we do this typecast
36 struct cfpkt {
37 struct sk_buff skb;
40 /* Private data inside SKB */
41 struct cfpkt_priv_data {
42 struct dev_info dev_info;
43 bool erronous;
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)
58 return &pkt->skb;
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;
70 return pkt;
72 EXPORT_SYMBOL(cfpkt_fromnative);
74 void *cfpkt_tonative(struct cfpkt *pkt)
76 return (void *) pkt;
78 EXPORT_SYMBOL(cfpkt_tonative);
80 static struct cfpkt *cfpkt_create_pfx(u16 len, u16 pfx)
82 struct sk_buff *skb;
84 skb = alloc_skb(len + pfx, GFP_ATOMIC);
85 if (unlikely(skb == NULL))
86 return 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);
100 kfree_skb(skb);
103 inline bool cfpkt_more(struct cfpkt *pkt)
105 struct sk_buff *skb = pkt_to_skb(pkt);
106 return skb->len > 0;
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);
114 return 0;
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);
123 u8 *from;
124 if (unlikely(is_erronous(pkt)))
125 return -EPROTO;
127 if (unlikely(len > skb->len)) {
128 PKT_ERROR(pkt, "read beyond end of packet\n");
129 return -EPROTO;
132 if (unlikely(len > skb_headlen(skb))) {
133 if (unlikely(skb_linearize(skb) != 0)) {
134 PKT_ERROR(pkt, "linearize failed\n");
135 return -EPROTO;
138 from = skb_pull(skb, len);
139 from -= len;
140 if (data)
141 memcpy(data, from, len);
142 return 0;
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);
149 u8 *data = dta;
150 u8 *from;
151 if (unlikely(is_erronous(pkt)))
152 return -EPROTO;
154 if (unlikely(skb_linearize(skb) != 0)) {
155 PKT_ERROR(pkt, "linearize failed\n");
156 return -EPROTO;
158 if (unlikely(skb->data + len > skb_tail_pointer(skb))) {
159 PKT_ERROR(pkt, "read beyond end of packet\n");
160 return -EPROTO;
162 from = skb_tail_pointer(skb) - len;
163 skb_trim(skb, skb->len - len);
164 memcpy(data, from, len);
165 return 0;
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;
177 u8 *to;
178 u16 addlen = 0;
181 if (unlikely(is_erronous(pkt)))
182 return -EPROTO;
184 lastskb = skb;
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;
190 else
191 addlen = len;
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");
200 return -EPROTO;
204 /* All set to put the last SKB and optionally write data there. */
205 to = pskb_put(skb, lastskb, len);
206 if (likely(data))
207 memcpy(to, data, len);
208 return 0;
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;
220 u8 *to;
221 const u8 *data = data2;
222 int ret;
223 if (unlikely(is_erronous(pkt)))
224 return -EPROTO;
225 if (unlikely(skb_headroom(skb) < len)) {
226 PKT_ERROR(pkt, "no headroom\n");
227 return -EPROTO;
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");
234 return ret;
237 to = skb_push(skb, len);
238 memcpy(to, data, len);
239 return 0;
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);
251 return skb->len;
254 int cfpkt_iterate(struct cfpkt *pkt,
255 u16 (*iter_func)(u16, void *, u16),
256 u16 data)
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)))
263 return -EPROTO;
264 if (unlikely(skb_linearize(&pkt->skb) != 0)) {
265 PKT_ERROR(pkt, "linearize failed\n");
266 return -EPROTO;
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)))
277 return -EPROTO;
279 if (likely(len <= skb->len)) {
280 if (unlikely(skb->data_len))
281 ___pskb_trim(skb, len);
282 else
283 skb_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,
297 u16 expectlen)
299 struct sk_buff *dst = pkt_to_skb(dstpkt);
300 struct sk_buff *add = pkt_to_skb(addpkt);
301 u16 addlen = skb_headlen(add);
302 u16 neededtailspace;
303 struct sk_buff *tmp;
304 u16 dstlen;
305 u16 createlen;
306 if (unlikely(is_erronous(dstpkt) || is_erronous(addpkt))) {
307 return dstpkt;
309 if (expectlen > addlen)
310 neededtailspace = expectlen;
311 else
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);
320 if (tmppkt == NULL)
321 return NULL;
322 tmp = pkt_to_skb(tmppkt);
323 skb_set_tail_pointer(tmp, dstlen);
324 tmp->len = dstlen;
325 memcpy(tmp->data, dst->data, dstlen);
326 cfpkt_destroy(dstpkt);
327 dst = tmp;
329 memcpy(skb_tail_pointer(dst), add->data, skb_headlen(add));
330 cfpkt_destroy(addpkt);
331 dst->tail += addlen;
332 dst->len += addlen;
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)))
345 return NULL;
347 if (skb->data + pos > skb_tail_pointer(skb)) {
348 PKT_ERROR(pkt, "trying to split beyond end of packet\n");
349 return NULL;
352 /* Create a new packet for the second part of the data */
353 tmppkt = cfpkt_create_pfx(len2nd + PKT_PREFIX + PKT_POSTFIX,
354 PKT_PREFIX);
355 if (tmppkt == NULL)
356 return NULL;
357 skb2 = pkt_to_skb(tmppkt);
360 if (skb2 == NULL)
361 return NULL;
363 /* Reduce the length of the original packet */
364 skb_set_tail_pointer(skb, pos);
365 skb->len = pos;
367 memcpy(skb2->data, split, len2nd);
368 skb2->tail += len2nd;
369 skb2->len += 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);