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