blob: 20c6cb3522e0f94ec600ee119d91f7c1b1e6f75f [file] [log] [blame]
Sjur Braendeland15c9ac02010-03-30 13:56:24 +00001/*
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
5 */
6
Joe Perchesb31fa5b2010-09-05 21:31:11 +00007#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
8
Sjur Braendeland15c9ac02010-03-30 13:56:24 +00009#include <linux/string.h>
10#include <linux/skbuff.h>
11#include <linux/hardirq.h>
12#include <net/caif/cfpkt.h>
13
Sjur Braendeland24e263a2010-08-10 07:36:46 +000014#define PKT_PREFIX 48
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +000015#define PKT_POSTFIX 2
Sjur Braendeland15c9ac02010-03-30 13:56:24 +000016#define PKT_LEN_WHEN_EXTENDING 128
Joe Perchesb31fa5b2010-09-05 21:31:11 +000017#define PKT_ERROR(pkt, errmsg) \
18do { \
19 cfpkt_priv(pkt)->erronous = true; \
20 skb_reset_tail_pointer(&pkt->skb); \
21 pr_warn(errmsg); \
22} while (0)
Sjur Braendeland15c9ac02010-03-30 13:56:24 +000023
24struct cfpktq {
25 struct sk_buff_head head;
26 atomic_t count;
27 /* Lock protects count updates */
28 spinlock_t lock;
29};
30
31/*
32 * net/caif/ is generic and does not
33 * understand SKB, so we do this typecast
34 */
35struct cfpkt {
36 struct sk_buff skb;
37};
38
39/* Private data inside SKB */
40struct cfpkt_priv_data {
41 struct dev_info dev_info;
42 bool erronous;
43};
44
Stephen Hemminger73d6ac62011-04-11 10:43:50 +000045static inline struct cfpkt_priv_data *cfpkt_priv(struct cfpkt *pkt)
Sjur Braendeland15c9ac02010-03-30 13:56:24 +000046{
47 return (struct cfpkt_priv_data *) pkt->skb.cb;
48}
49
Stephen Hemminger73d6ac62011-04-11 10:43:50 +000050static inline bool is_erronous(struct cfpkt *pkt)
Sjur Braendeland15c9ac02010-03-30 13:56:24 +000051{
52 return cfpkt_priv(pkt)->erronous;
53}
54
Stephen Hemminger73d6ac62011-04-11 10:43:50 +000055static inline struct sk_buff *pkt_to_skb(struct cfpkt *pkt)
Sjur Braendeland15c9ac02010-03-30 13:56:24 +000056{
57 return &pkt->skb;
58}
59
Stephen Hemminger73d6ac62011-04-11 10:43:50 +000060static inline struct cfpkt *skb_to_pkt(struct sk_buff *skb)
Sjur Braendeland15c9ac02010-03-30 13:56:24 +000061{
62 return (struct cfpkt *) skb;
63}
64
65
66struct cfpkt *cfpkt_fromnative(enum caif_direction dir, void *nativepkt)
67{
68 struct cfpkt *pkt = skb_to_pkt(nativepkt);
69 cfpkt_priv(pkt)->erronous = false;
70 return pkt;
71}
72EXPORT_SYMBOL(cfpkt_fromnative);
73
74void *cfpkt_tonative(struct cfpkt *pkt)
75{
76 return (void *) pkt;
77}
78EXPORT_SYMBOL(cfpkt_tonative);
79
80static struct cfpkt *cfpkt_create_pfx(u16 len, u16 pfx)
81{
82 struct sk_buff *skb;
83
84 if (likely(in_interrupt()))
85 skb = alloc_skb(len + pfx, GFP_ATOMIC);
86 else
87 skb = alloc_skb(len + pfx, GFP_KERNEL);
88
89 if (unlikely(skb == NULL))
90 return NULL;
91
92 skb_reserve(skb, pfx);
93 return skb_to_pkt(skb);
94}
95
96inline struct cfpkt *cfpkt_create(u16 len)
97{
98 return cfpkt_create_pfx(len + PKT_POSTFIX, PKT_PREFIX);
99}
100EXPORT_SYMBOL(cfpkt_create);
101
102void cfpkt_destroy(struct cfpkt *pkt)
103{
104 struct sk_buff *skb = pkt_to_skb(pkt);
105 kfree_skb(skb);
106}
107EXPORT_SYMBOL(cfpkt_destroy);
108
109inline bool cfpkt_more(struct cfpkt *pkt)
110{
111 struct sk_buff *skb = pkt_to_skb(pkt);
112 return skb->len > 0;
113}
114EXPORT_SYMBOL(cfpkt_more);
115
116int cfpkt_peek_head(struct cfpkt *pkt, void *data, u16 len)
117{
118 struct sk_buff *skb = pkt_to_skb(pkt);
119 if (skb_headlen(skb) >= len) {
120 memcpy(data, skb->data, len);
121 return 0;
122 }
123 return !cfpkt_extr_head(pkt, data, len) &&
124 !cfpkt_add_head(pkt, data, len);
125}
126EXPORT_SYMBOL(cfpkt_peek_head);
127
128int cfpkt_extr_head(struct cfpkt *pkt, void *data, u16 len)
129{
130 struct sk_buff *skb = pkt_to_skb(pkt);
131 u8 *from;
132 if (unlikely(is_erronous(pkt)))
133 return -EPROTO;
134
135 if (unlikely(len > skb->len)) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000136 PKT_ERROR(pkt, "read beyond end of packet\n");
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000137 return -EPROTO;
138 }
139
140 if (unlikely(len > skb_headlen(skb))) {
141 if (unlikely(skb_linearize(skb) != 0)) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000142 PKT_ERROR(pkt, "linearize failed\n");
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000143 return -EPROTO;
144 }
145 }
146 from = skb_pull(skb, len);
147 from -= len;
148 memcpy(data, from, len);
149 return 0;
150}
151EXPORT_SYMBOL(cfpkt_extr_head);
152
153int cfpkt_extr_trail(struct cfpkt *pkt, void *dta, u16 len)
154{
155 struct sk_buff *skb = pkt_to_skb(pkt);
156 u8 *data = dta;
157 u8 *from;
158 if (unlikely(is_erronous(pkt)))
159 return -EPROTO;
160
161 if (unlikely(skb_linearize(skb) != 0)) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000162 PKT_ERROR(pkt, "linearize failed\n");
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000163 return -EPROTO;
164 }
165 if (unlikely(skb->data + len > skb_tail_pointer(skb))) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000166 PKT_ERROR(pkt, "read beyond end of packet\n");
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000167 return -EPROTO;
168 }
169 from = skb_tail_pointer(skb) - len;
170 skb_trim(skb, skb->len - len);
171 memcpy(data, from, len);
172 return 0;
173}
174EXPORT_SYMBOL(cfpkt_extr_trail);
175
176int cfpkt_pad_trail(struct cfpkt *pkt, u16 len)
177{
178 return cfpkt_add_body(pkt, NULL, len);
179}
180EXPORT_SYMBOL(cfpkt_pad_trail);
181
182int cfpkt_add_body(struct cfpkt *pkt, const void *data, u16 len)
183{
184 struct sk_buff *skb = pkt_to_skb(pkt);
185 struct sk_buff *lastskb;
186 u8 *to;
187 u16 addlen = 0;
188
189
190 if (unlikely(is_erronous(pkt)))
191 return -EPROTO;
192
193 lastskb = skb;
194
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;
199 else
200 addlen = len;
201 }
202
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))) {
205
206 /* Make sure data is writable */
207 if (unlikely(skb_cow_data(skb, addlen, &lastskb) < 0)) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000208 PKT_ERROR(pkt, "cow failed\n");
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000209 return -EPROTO;
210 }
211 /*
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.
215 */
216 if (lastskb != skb) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000217 pr_warn("Packet is non-linear\n");
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000218 skb->len += len;
219 skb->data_len += len;
220 }
221 }
222
223 /* All set to put the last SKB and optionally write data there. */
224 to = skb_put(lastskb, len);
225 if (likely(data))
226 memcpy(to, data, len);
227 return 0;
228}
229EXPORT_SYMBOL(cfpkt_add_body);
230
231inline int cfpkt_addbdy(struct cfpkt *pkt, u8 data)
232{
233 return cfpkt_add_body(pkt, &data, 1);
234}
235EXPORT_SYMBOL(cfpkt_addbdy);
236
237int cfpkt_add_head(struct cfpkt *pkt, const void *data2, u16 len)
238{
239 struct sk_buff *skb = pkt_to_skb(pkt);
240 struct sk_buff *lastskb;
241 u8 *to;
242 const u8 *data = data2;
Sjur Braendeland638e6282010-05-21 02:16:09 +0000243 int ret;
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000244 if (unlikely(is_erronous(pkt)))
245 return -EPROTO;
246 if (unlikely(skb_headroom(skb) < len)) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000247 PKT_ERROR(pkt, "no headroom\n");
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000248 return -EPROTO;
249 }
250
251 /* Make sure data is writable */
Sjur Braendeland638e6282010-05-21 02:16:09 +0000252 ret = skb_cow_data(skb, 0, &lastskb);
253 if (unlikely(ret < 0)) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000254 PKT_ERROR(pkt, "cow failed\n");
Sjur Braendeland638e6282010-05-21 02:16:09 +0000255 return ret;
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000256 }
257
258 to = skb_push(skb, len);
259 memcpy(to, data, len);
260 return 0;
261}
262EXPORT_SYMBOL(cfpkt_add_head);
263
264inline int cfpkt_add_trail(struct cfpkt *pkt, const void *data, u16 len)
265{
266 return cfpkt_add_body(pkt, data, len);
267}
268EXPORT_SYMBOL(cfpkt_add_trail);
269
270inline u16 cfpkt_getlen(struct cfpkt *pkt)
271{
272 struct sk_buff *skb = pkt_to_skb(pkt);
273 return skb->len;
274}
275EXPORT_SYMBOL(cfpkt_getlen);
276
277inline u16 cfpkt_iterate(struct cfpkt *pkt,
278 u16 (*iter_func)(u16, void *, u16),
279 u16 data)
280{
281 /*
282 * Don't care about the performance hit of linearizing,
283 * Checksum should not be used on high-speed interfaces anyway.
284 */
285 if (unlikely(is_erronous(pkt)))
286 return -EPROTO;
287 if (unlikely(skb_linearize(&pkt->skb) != 0)) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000288 PKT_ERROR(pkt, "linearize failed\n");
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000289 return -EPROTO;
290 }
291 return iter_func(data, pkt->skb.data, cfpkt_getlen(pkt));
292}
293EXPORT_SYMBOL(cfpkt_iterate);
294
295int cfpkt_setlen(struct cfpkt *pkt, u16 len)
296{
297 struct sk_buff *skb = pkt_to_skb(pkt);
298
299
300 if (unlikely(is_erronous(pkt)))
301 return -EPROTO;
302
303 if (likely(len <= skb->len)) {
304 if (unlikely(skb->data_len))
305 ___pskb_trim(skb, len);
306 else
307 skb_trim(skb, len);
308
309 return cfpkt_getlen(pkt);
310 }
311
312 /* Need to expand SKB */
313 if (unlikely(!cfpkt_pad_trail(pkt, len - skb->len)))
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000314 PKT_ERROR(pkt, "skb_pad_trail failed\n");
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000315
316 return cfpkt_getlen(pkt);
317}
318EXPORT_SYMBOL(cfpkt_setlen);
319
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000320struct cfpkt *cfpkt_append(struct cfpkt *dstpkt,
321 struct cfpkt *addpkt,
322 u16 expectlen)
323{
324 struct sk_buff *dst = pkt_to_skb(dstpkt);
325 struct sk_buff *add = pkt_to_skb(addpkt);
326 u16 addlen = skb_headlen(add);
327 u16 neededtailspace;
328 struct sk_buff *tmp;
329 u16 dstlen;
330 u16 createlen;
331 if (unlikely(is_erronous(dstpkt) || is_erronous(addpkt))) {
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000332 return dstpkt;
333 }
334 if (expectlen > addlen)
335 neededtailspace = expectlen;
336 else
337 neededtailspace = addlen;
338
339 if (dst->tail + neededtailspace > dst->end) {
340 /* Create a dumplicate of 'dst' with more tail space */
Sjur Braendeland638e6282010-05-21 02:16:09 +0000341 struct cfpkt *tmppkt;
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000342 dstlen = skb_headlen(dst);
343 createlen = dstlen + neededtailspace;
Sjur Braendeland638e6282010-05-21 02:16:09 +0000344 tmppkt = cfpkt_create(createlen + PKT_PREFIX + PKT_POSTFIX);
345 if (tmppkt == NULL)
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000346 return NULL;
Sjur Braendeland638e6282010-05-21 02:16:09 +0000347 tmp = pkt_to_skb(tmppkt);
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000348 skb_set_tail_pointer(tmp, dstlen);
349 tmp->len = dstlen;
350 memcpy(tmp->data, dst->data, dstlen);
351 cfpkt_destroy(dstpkt);
352 dst = tmp;
353 }
354 memcpy(skb_tail_pointer(dst), add->data, skb_headlen(add));
355 cfpkt_destroy(addpkt);
356 dst->tail += addlen;
357 dst->len += addlen;
358 return skb_to_pkt(dst);
359}
360EXPORT_SYMBOL(cfpkt_append);
361
362struct cfpkt *cfpkt_split(struct cfpkt *pkt, u16 pos)
363{
364 struct sk_buff *skb2;
365 struct sk_buff *skb = pkt_to_skb(pkt);
Sjur Braendeland638e6282010-05-21 02:16:09 +0000366 struct cfpkt *tmppkt;
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000367 u8 *split = skb->data + pos;
368 u16 len2nd = skb_tail_pointer(skb) - split;
369
370 if (unlikely(is_erronous(pkt)))
371 return NULL;
372
373 if (skb->data + pos > skb_tail_pointer(skb)) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000374 PKT_ERROR(pkt, "trying to split beyond end of packet\n");
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000375 return NULL;
376 }
377
378 /* Create a new packet for the second part of the data */
Sjur Braendeland638e6282010-05-21 02:16:09 +0000379 tmppkt = cfpkt_create_pfx(len2nd + PKT_PREFIX + PKT_POSTFIX,
380 PKT_PREFIX);
381 if (tmppkt == NULL)
382 return NULL;
383 skb2 = pkt_to_skb(tmppkt);
384
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000385
386 if (skb2 == NULL)
387 return NULL;
388
389 /* Reduce the length of the original packet */
390 skb_set_tail_pointer(skb, pos);
391 skb->len = pos;
392
393 memcpy(skb2->data, split, len2nd);
394 skb2->tail += len2nd;
395 skb2->len += len2nd;
396 return skb_to_pkt(skb2);
397}
398EXPORT_SYMBOL(cfpkt_split);
399
Stephen Hemminger73d6ac62011-04-11 10:43:50 +0000400bool cfpkt_erroneous(struct cfpkt *pkt)
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000401{
402 return cfpkt_priv(pkt)->erronous;
403}
404EXPORT_SYMBOL(cfpkt_erroneous);
405
Sjur Braendeland15c9ac02010-03-30 13:56:24 +0000406
407struct caif_payload_info *cfpkt_info(struct cfpkt *pkt)
408{
409 return (struct caif_payload_info *)&pkt_to_skb(pkt)->cb;
410}
411EXPORT_SYMBOL(cfpkt_info);