Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) ST-Ericsson AB 2010 |
sjur.brandeland@stericsson.com | 26ee65e | 2013-04-22 23:57:01 +0000 | [diff] [blame] | 3 | * Author: Sjur Brendeland |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 4 | * License terms: GNU General Public License (GPL) version 2 |
| 5 | */ |
| 6 | |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 7 | #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__ |
| 8 | |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 9 | #include <linux/string.h> |
| 10 | #include <linux/skbuff.h> |
| 11 | #include <linux/hardirq.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 12 | #include <linux/export.h> |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 13 | #include <net/caif/cfpkt.h> |
| 14 | |
Sjur Braendeland | 24e263a | 2010-08-10 07:36:46 +0000 | [diff] [blame] | 15 | #define PKT_PREFIX 48 |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 16 | #define PKT_POSTFIX 2 |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 17 | #define PKT_LEN_WHEN_EXTENDING 128 |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 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) |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 24 | |
| 25 | struct cfpktq { |
| 26 | struct sk_buff_head head; |
| 27 | atomic_t count; |
| 28 | /* Lock protects count updates */ |
| 29 | spinlock_t lock; |
| 30 | }; |
| 31 | |
| 32 | /* |
| 33 | * net/caif/ is generic and does not |
| 34 | * understand SKB, so we do this typecast |
| 35 | */ |
| 36 | struct cfpkt { |
| 37 | struct sk_buff skb; |
| 38 | }; |
| 39 | |
| 40 | /* Private data inside SKB */ |
| 41 | struct cfpkt_priv_data { |
| 42 | struct dev_info dev_info; |
| 43 | bool erronous; |
| 44 | }; |
| 45 | |
Stephen Hemminger | 73d6ac6 | 2011-04-11 10:43:50 +0000 | [diff] [blame] | 46 | static inline struct cfpkt_priv_data *cfpkt_priv(struct cfpkt *pkt) |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 47 | { |
| 48 | return (struct cfpkt_priv_data *) pkt->skb.cb; |
| 49 | } |
| 50 | |
Stephen Hemminger | 73d6ac6 | 2011-04-11 10:43:50 +0000 | [diff] [blame] | 51 | static inline bool is_erronous(struct cfpkt *pkt) |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 52 | { |
| 53 | return cfpkt_priv(pkt)->erronous; |
| 54 | } |
| 55 | |
Stephen Hemminger | 73d6ac6 | 2011-04-11 10:43:50 +0000 | [diff] [blame] | 56 | static inline struct sk_buff *pkt_to_skb(struct cfpkt *pkt) |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 57 | { |
| 58 | return &pkt->skb; |
| 59 | } |
| 60 | |
Stephen Hemminger | 73d6ac6 | 2011-04-11 10:43:50 +0000 | [diff] [blame] | 61 | static inline struct cfpkt *skb_to_pkt(struct sk_buff *skb) |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 62 | { |
| 63 | return (struct cfpkt *) skb; |
| 64 | } |
| 65 | |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 66 | struct 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 | } |
| 72 | EXPORT_SYMBOL(cfpkt_fromnative); |
| 73 | |
| 74 | void *cfpkt_tonative(struct cfpkt *pkt) |
| 75 | { |
| 76 | return (void *) pkt; |
| 77 | } |
| 78 | EXPORT_SYMBOL(cfpkt_tonative); |
| 79 | |
| 80 | static 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 | |
| 96 | inline struct cfpkt *cfpkt_create(u16 len) |
| 97 | { |
| 98 | return cfpkt_create_pfx(len + PKT_POSTFIX, PKT_PREFIX); |
| 99 | } |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 100 | |
| 101 | void cfpkt_destroy(struct cfpkt *pkt) |
| 102 | { |
| 103 | struct sk_buff *skb = pkt_to_skb(pkt); |
| 104 | kfree_skb(skb); |
| 105 | } |
sjur.brandeland@stericsson.com | 3f874ad | 2011-05-13 02:44:08 +0000 | [diff] [blame] | 106 | |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 107 | inline bool cfpkt_more(struct cfpkt *pkt) |
| 108 | { |
| 109 | struct sk_buff *skb = pkt_to_skb(pkt); |
| 110 | return skb->len > 0; |
| 111 | } |
sjur.brandeland@stericsson.com | 3f874ad | 2011-05-13 02:44:08 +0000 | [diff] [blame] | 112 | |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 113 | int cfpkt_peek_head(struct cfpkt *pkt, void *data, u16 len) |
| 114 | { |
| 115 | struct sk_buff *skb = pkt_to_skb(pkt); |
| 116 | if (skb_headlen(skb) >= len) { |
| 117 | memcpy(data, skb->data, len); |
| 118 | return 0; |
| 119 | } |
| 120 | return !cfpkt_extr_head(pkt, data, len) && |
| 121 | !cfpkt_add_head(pkt, data, len); |
| 122 | } |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 123 | |
| 124 | int cfpkt_extr_head(struct cfpkt *pkt, void *data, u16 len) |
| 125 | { |
| 126 | struct sk_buff *skb = pkt_to_skb(pkt); |
| 127 | u8 *from; |
| 128 | if (unlikely(is_erronous(pkt))) |
| 129 | return -EPROTO; |
| 130 | |
| 131 | if (unlikely(len > skb->len)) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 132 | PKT_ERROR(pkt, "read beyond end of packet\n"); |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 133 | return -EPROTO; |
| 134 | } |
| 135 | |
| 136 | if (unlikely(len > skb_headlen(skb))) { |
| 137 | if (unlikely(skb_linearize(skb) != 0)) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 138 | PKT_ERROR(pkt, "linearize failed\n"); |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 139 | return -EPROTO; |
| 140 | } |
| 141 | } |
| 142 | from = skb_pull(skb, len); |
| 143 | from -= len; |
sjur.brandeland@stericsson.com | 200c5a3 | 2011-11-30 09:22:46 +0000 | [diff] [blame] | 144 | if (data) |
| 145 | memcpy(data, from, len); |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 146 | return 0; |
| 147 | } |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 148 | EXPORT_SYMBOL(cfpkt_extr_head); |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 149 | |
| 150 | int cfpkt_extr_trail(struct cfpkt *pkt, void *dta, u16 len) |
| 151 | { |
| 152 | struct sk_buff *skb = pkt_to_skb(pkt); |
| 153 | u8 *data = dta; |
| 154 | u8 *from; |
| 155 | if (unlikely(is_erronous(pkt))) |
| 156 | return -EPROTO; |
| 157 | |
| 158 | if (unlikely(skb_linearize(skb) != 0)) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 159 | PKT_ERROR(pkt, "linearize failed\n"); |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 160 | return -EPROTO; |
| 161 | } |
| 162 | if (unlikely(skb->data + len > skb_tail_pointer(skb))) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 163 | PKT_ERROR(pkt, "read beyond end of packet\n"); |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 164 | return -EPROTO; |
| 165 | } |
| 166 | from = skb_tail_pointer(skb) - len; |
| 167 | skb_trim(skb, skb->len - len); |
| 168 | memcpy(data, from, len); |
| 169 | return 0; |
| 170 | } |
sjur.brandeland@stericsson.com | 3f874ad | 2011-05-13 02:44:08 +0000 | [diff] [blame] | 171 | |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 172 | int cfpkt_pad_trail(struct cfpkt *pkt, u16 len) |
| 173 | { |
| 174 | return cfpkt_add_body(pkt, NULL, len); |
| 175 | } |
sjur.brandeland@stericsson.com | 3f874ad | 2011-05-13 02:44:08 +0000 | [diff] [blame] | 176 | |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 177 | int cfpkt_add_body(struct cfpkt *pkt, const void *data, u16 len) |
| 178 | { |
| 179 | struct sk_buff *skb = pkt_to_skb(pkt); |
| 180 | struct sk_buff *lastskb; |
| 181 | u8 *to; |
| 182 | u16 addlen = 0; |
| 183 | |
| 184 | |
| 185 | if (unlikely(is_erronous(pkt))) |
| 186 | return -EPROTO; |
| 187 | |
| 188 | lastskb = skb; |
| 189 | |
| 190 | /* Check whether we need to add space at the tail */ |
| 191 | if (unlikely(skb_tailroom(skb) < len)) { |
| 192 | if (likely(len < PKT_LEN_WHEN_EXTENDING)) |
| 193 | addlen = PKT_LEN_WHEN_EXTENDING; |
| 194 | else |
| 195 | addlen = len; |
| 196 | } |
| 197 | |
| 198 | /* Check whether we need to change the SKB before writing to the tail */ |
| 199 | if (unlikely((addlen > 0) || skb_cloned(skb) || skb_shared(skb))) { |
| 200 | |
| 201 | /* Make sure data is writable */ |
| 202 | if (unlikely(skb_cow_data(skb, addlen, &lastskb) < 0)) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 203 | PKT_ERROR(pkt, "cow failed\n"); |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 204 | return -EPROTO; |
| 205 | } |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* All set to put the last SKB and optionally write data there. */ |
Mathias Krause | 253c6da | 2013-11-07 14:18:25 +0100 | [diff] [blame] | 209 | to = pskb_put(skb, lastskb, len); |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 210 | if (likely(data)) |
| 211 | memcpy(to, data, len); |
| 212 | return 0; |
| 213 | } |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 214 | |
| 215 | inline int cfpkt_addbdy(struct cfpkt *pkt, u8 data) |
| 216 | { |
| 217 | return cfpkt_add_body(pkt, &data, 1); |
| 218 | } |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 219 | |
| 220 | int cfpkt_add_head(struct cfpkt *pkt, const void *data2, u16 len) |
| 221 | { |
| 222 | struct sk_buff *skb = pkt_to_skb(pkt); |
| 223 | struct sk_buff *lastskb; |
| 224 | u8 *to; |
| 225 | const u8 *data = data2; |
Sjur Braendeland | 638e628 | 2010-05-21 02:16:09 +0000 | [diff] [blame] | 226 | int ret; |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 227 | if (unlikely(is_erronous(pkt))) |
| 228 | return -EPROTO; |
| 229 | if (unlikely(skb_headroom(skb) < len)) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 230 | PKT_ERROR(pkt, "no headroom\n"); |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 231 | return -EPROTO; |
| 232 | } |
| 233 | |
| 234 | /* Make sure data is writable */ |
Sjur Braendeland | 638e628 | 2010-05-21 02:16:09 +0000 | [diff] [blame] | 235 | ret = skb_cow_data(skb, 0, &lastskb); |
| 236 | if (unlikely(ret < 0)) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 237 | PKT_ERROR(pkt, "cow failed\n"); |
Sjur Braendeland | 638e628 | 2010-05-21 02:16:09 +0000 | [diff] [blame] | 238 | return ret; |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | to = skb_push(skb, len); |
| 242 | memcpy(to, data, len); |
| 243 | return 0; |
| 244 | } |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 245 | EXPORT_SYMBOL(cfpkt_add_head); |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 246 | |
| 247 | inline int cfpkt_add_trail(struct cfpkt *pkt, const void *data, u16 len) |
| 248 | { |
| 249 | return cfpkt_add_body(pkt, data, len); |
| 250 | } |
sjur.brandeland@stericsson.com | 3f874ad | 2011-05-13 02:44:08 +0000 | [diff] [blame] | 251 | |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 252 | inline u16 cfpkt_getlen(struct cfpkt *pkt) |
| 253 | { |
| 254 | struct sk_buff *skb = pkt_to_skb(pkt); |
| 255 | return skb->len; |
| 256 | } |
sjur.brandeland@stericsson.com | 3f874ad | 2011-05-13 02:44:08 +0000 | [diff] [blame] | 257 | |
Dan Carpenter | 278f7b4 | 2015-02-19 12:13:13 +0300 | [diff] [blame] | 258 | int cfpkt_iterate(struct cfpkt *pkt, |
| 259 | u16 (*iter_func)(u16, void *, u16), |
| 260 | u16 data) |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 261 | { |
| 262 | /* |
| 263 | * Don't care about the performance hit of linearizing, |
| 264 | * Checksum should not be used on high-speed interfaces anyway. |
| 265 | */ |
| 266 | if (unlikely(is_erronous(pkt))) |
| 267 | return -EPROTO; |
| 268 | if (unlikely(skb_linearize(&pkt->skb) != 0)) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 269 | PKT_ERROR(pkt, "linearize failed\n"); |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 270 | return -EPROTO; |
| 271 | } |
| 272 | return iter_func(data, pkt->skb.data, cfpkt_getlen(pkt)); |
| 273 | } |
sjur.brandeland@stericsson.com | 3f874ad | 2011-05-13 02:44:08 +0000 | [diff] [blame] | 274 | |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 275 | int cfpkt_setlen(struct cfpkt *pkt, u16 len) |
| 276 | { |
| 277 | struct sk_buff *skb = pkt_to_skb(pkt); |
| 278 | |
| 279 | |
| 280 | if (unlikely(is_erronous(pkt))) |
| 281 | return -EPROTO; |
| 282 | |
| 283 | if (likely(len <= skb->len)) { |
| 284 | if (unlikely(skb->data_len)) |
| 285 | ___pskb_trim(skb, len); |
| 286 | else |
| 287 | skb_trim(skb, len); |
| 288 | |
Arnd Bergmann | 8e0cc8c | 2016-03-14 15:18:38 +0100 | [diff] [blame^] | 289 | return cfpkt_getlen(pkt); |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /* Need to expand SKB */ |
| 293 | if (unlikely(!cfpkt_pad_trail(pkt, len - skb->len))) |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 294 | PKT_ERROR(pkt, "skb_pad_trail failed\n"); |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 295 | |
| 296 | return cfpkt_getlen(pkt); |
| 297 | } |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 298 | |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 299 | struct cfpkt *cfpkt_append(struct cfpkt *dstpkt, |
Silviu-Mihai Popescu | 3bffc47 | 2013-03-06 19:39:57 +0000 | [diff] [blame] | 300 | struct cfpkt *addpkt, |
| 301 | u16 expectlen) |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 302 | { |
| 303 | struct sk_buff *dst = pkt_to_skb(dstpkt); |
| 304 | struct sk_buff *add = pkt_to_skb(addpkt); |
| 305 | u16 addlen = skb_headlen(add); |
| 306 | u16 neededtailspace; |
| 307 | struct sk_buff *tmp; |
| 308 | u16 dstlen; |
| 309 | u16 createlen; |
| 310 | if (unlikely(is_erronous(dstpkt) || is_erronous(addpkt))) { |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 311 | return dstpkt; |
| 312 | } |
| 313 | if (expectlen > addlen) |
| 314 | neededtailspace = expectlen; |
| 315 | else |
| 316 | neededtailspace = addlen; |
| 317 | |
| 318 | if (dst->tail + neededtailspace > dst->end) { |
| 319 | /* Create a dumplicate of 'dst' with more tail space */ |
Sjur Braendeland | 638e628 | 2010-05-21 02:16:09 +0000 | [diff] [blame] | 320 | struct cfpkt *tmppkt; |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 321 | dstlen = skb_headlen(dst); |
| 322 | createlen = dstlen + neededtailspace; |
Sjur Braendeland | 638e628 | 2010-05-21 02:16:09 +0000 | [diff] [blame] | 323 | tmppkt = cfpkt_create(createlen + PKT_PREFIX + PKT_POSTFIX); |
| 324 | if (tmppkt == NULL) |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 325 | return NULL; |
Sjur Braendeland | 638e628 | 2010-05-21 02:16:09 +0000 | [diff] [blame] | 326 | tmp = pkt_to_skb(tmppkt); |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 327 | skb_set_tail_pointer(tmp, dstlen); |
| 328 | tmp->len = dstlen; |
| 329 | memcpy(tmp->data, dst->data, dstlen); |
| 330 | cfpkt_destroy(dstpkt); |
| 331 | dst = tmp; |
| 332 | } |
| 333 | memcpy(skb_tail_pointer(dst), add->data, skb_headlen(add)); |
| 334 | cfpkt_destroy(addpkt); |
| 335 | dst->tail += addlen; |
| 336 | dst->len += addlen; |
| 337 | return skb_to_pkt(dst); |
| 338 | } |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 339 | |
| 340 | struct cfpkt *cfpkt_split(struct cfpkt *pkt, u16 pos) |
| 341 | { |
| 342 | struct sk_buff *skb2; |
| 343 | struct sk_buff *skb = pkt_to_skb(pkt); |
Sjur Braendeland | 638e628 | 2010-05-21 02:16:09 +0000 | [diff] [blame] | 344 | struct cfpkt *tmppkt; |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 345 | u8 *split = skb->data + pos; |
| 346 | u16 len2nd = skb_tail_pointer(skb) - split; |
| 347 | |
| 348 | if (unlikely(is_erronous(pkt))) |
| 349 | return NULL; |
| 350 | |
| 351 | if (skb->data + pos > skb_tail_pointer(skb)) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 352 | PKT_ERROR(pkt, "trying to split beyond end of packet\n"); |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 353 | return NULL; |
| 354 | } |
| 355 | |
| 356 | /* Create a new packet for the second part of the data */ |
Sjur Braendeland | 638e628 | 2010-05-21 02:16:09 +0000 | [diff] [blame] | 357 | tmppkt = cfpkt_create_pfx(len2nd + PKT_PREFIX + PKT_POSTFIX, |
| 358 | PKT_PREFIX); |
| 359 | if (tmppkt == NULL) |
| 360 | return NULL; |
| 361 | skb2 = pkt_to_skb(tmppkt); |
| 362 | |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 363 | |
| 364 | if (skb2 == NULL) |
| 365 | return NULL; |
| 366 | |
| 367 | /* Reduce the length of the original packet */ |
| 368 | skb_set_tail_pointer(skb, pos); |
| 369 | skb->len = pos; |
| 370 | |
| 371 | memcpy(skb2->data, split, len2nd); |
| 372 | skb2->tail += len2nd; |
| 373 | skb2->len += len2nd; |
Dmitry Tarnyagin | 4476481 | 2012-04-12 08:27:24 +0000 | [diff] [blame] | 374 | skb2->priority = skb->priority; |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 375 | return skb_to_pkt(skb2); |
| 376 | } |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 377 | |
Stephen Hemminger | 73d6ac6 | 2011-04-11 10:43:50 +0000 | [diff] [blame] | 378 | bool cfpkt_erroneous(struct cfpkt *pkt) |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 379 | { |
| 380 | return cfpkt_priv(pkt)->erronous; |
| 381 | } |
Sjur Braendeland | 15c9ac0 | 2010-03-30 13:56:24 +0000 | [diff] [blame] | 382 | |
| 383 | struct caif_payload_info *cfpkt_info(struct cfpkt *pkt) |
| 384 | { |
| 385 | return (struct caif_payload_info *)&pkt_to_skb(pkt)->cb; |
| 386 | } |
sjur.brandeland@stericsson.com | 7ad65bf | 2011-12-04 11:22:53 +0000 | [diff] [blame] | 387 | EXPORT_SYMBOL(cfpkt_info); |
Dmitry Tarnyagin | 4476481 | 2012-04-12 08:27:24 +0000 | [diff] [blame] | 388 | |
| 389 | void cfpkt_set_prio(struct cfpkt *pkt, int prio) |
| 390 | { |
| 391 | pkt_to_skb(pkt)->priority = prio; |
| 392 | } |
| 393 | EXPORT_SYMBOL(cfpkt_set_prio); |