cfpkt_skbuff.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  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. #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
  7. #include <linux/string.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/hardirq.h>
  10. #include <net/caif/cfpkt.h>
  11. #define PKT_PREFIX 48
  12. #define PKT_POSTFIX 2
  13. #define PKT_LEN_WHEN_EXTENDING 128
  14. #define PKT_ERROR(pkt, errmsg) \
  15. do { \
  16. cfpkt_priv(pkt)->erronous = true; \
  17. skb_reset_tail_pointer(&pkt->skb); \
  18. pr_warn(errmsg); \
  19. } while (0)
  20. struct cfpktq {
  21. struct sk_buff_head head;
  22. atomic_t count;
  23. /* Lock protects count updates */
  24. spinlock_t lock;
  25. };
  26. /*
  27. * net/caif/ is generic and does not
  28. * understand SKB, so we do this typecast
  29. */
  30. struct cfpkt {
  31. struct sk_buff skb;
  32. };
  33. /* Private data inside SKB */
  34. struct cfpkt_priv_data {
  35. struct dev_info dev_info;
  36. bool erronous;
  37. };
  38. inline struct cfpkt_priv_data *cfpkt_priv(struct cfpkt *pkt)
  39. {
  40. return (struct cfpkt_priv_data *) pkt->skb.cb;
  41. }
  42. inline bool is_erronous(struct cfpkt *pkt)
  43. {
  44. return cfpkt_priv(pkt)->erronous;
  45. }
  46. inline struct sk_buff *pkt_to_skb(struct cfpkt *pkt)
  47. {
  48. return &pkt->skb;
  49. }
  50. inline struct cfpkt *skb_to_pkt(struct sk_buff *skb)
  51. {
  52. return (struct cfpkt *) skb;
  53. }
  54. struct cfpkt *cfpkt_fromnative(enum caif_direction dir, void *nativepkt)
  55. {
  56. struct cfpkt *pkt = skb_to_pkt(nativepkt);
  57. cfpkt_priv(pkt)->erronous = false;
  58. return pkt;
  59. }
  60. EXPORT_SYMBOL(cfpkt_fromnative);
  61. void *cfpkt_tonative(struct cfpkt *pkt)
  62. {
  63. return (void *) pkt;
  64. }
  65. EXPORT_SYMBOL(cfpkt_tonative);
  66. static struct cfpkt *cfpkt_create_pfx(u16 len, u16 pfx)
  67. {
  68. struct sk_buff *skb;
  69. if (likely(in_interrupt()))
  70. skb = alloc_skb(len + pfx, GFP_ATOMIC);
  71. else
  72. skb = alloc_skb(len + pfx, GFP_KERNEL);
  73. if (unlikely(skb == NULL))
  74. return NULL;
  75. skb_reserve(skb, pfx);
  76. return skb_to_pkt(skb);
  77. }
  78. inline struct cfpkt *cfpkt_create(u16 len)
  79. {
  80. return cfpkt_create_pfx(len + PKT_POSTFIX, PKT_PREFIX);
  81. }
  82. EXPORT_SYMBOL(cfpkt_create);
  83. void cfpkt_destroy(struct cfpkt *pkt)
  84. {
  85. struct sk_buff *skb = pkt_to_skb(pkt);
  86. kfree_skb(skb);
  87. }
  88. EXPORT_SYMBOL(cfpkt_destroy);
  89. inline bool cfpkt_more(struct cfpkt *pkt)
  90. {
  91. struct sk_buff *skb = pkt_to_skb(pkt);
  92. return skb->len > 0;
  93. }
  94. EXPORT_SYMBOL(cfpkt_more);
  95. int cfpkt_peek_head(struct cfpkt *pkt, void *data, u16 len)
  96. {
  97. struct sk_buff *skb = pkt_to_skb(pkt);
  98. if (skb_headlen(skb) >= len) {
  99. memcpy(data, skb->data, len);
  100. return 0;
  101. }
  102. return !cfpkt_extr_head(pkt, data, len) &&
  103. !cfpkt_add_head(pkt, data, len);
  104. }
  105. EXPORT_SYMBOL(cfpkt_peek_head);
  106. int cfpkt_extr_head(struct cfpkt *pkt, void *data, u16 len)
  107. {
  108. struct sk_buff *skb = pkt_to_skb(pkt);
  109. u8 *from;
  110. if (unlikely(is_erronous(pkt)))
  111. return -EPROTO;
  112. if (unlikely(len > skb->len)) {
  113. PKT_ERROR(pkt, "read beyond end of packet\n");
  114. return -EPROTO;
  115. }
  116. if (unlikely(len > skb_headlen(skb))) {
  117. if (unlikely(skb_linearize(skb) != 0)) {
  118. PKT_ERROR(pkt, "linearize failed\n");
  119. return -EPROTO;
  120. }
  121. }
  122. from = skb_pull(skb, len);
  123. from -= len;
  124. memcpy(data, from, len);
  125. return 0;
  126. }
  127. EXPORT_SYMBOL(cfpkt_extr_head);
  128. int cfpkt_extr_trail(struct cfpkt *pkt, void *dta, u16 len)
  129. {
  130. struct sk_buff *skb = pkt_to_skb(pkt);
  131. u8 *data = dta;
  132. u8 *from;
  133. if (unlikely(is_erronous(pkt)))
  134. return -EPROTO;
  135. if (unlikely(skb_linearize(skb) != 0)) {
  136. PKT_ERROR(pkt, "linearize failed\n");
  137. return -EPROTO;
  138. }
  139. if (unlikely(skb->data + len > skb_tail_pointer(skb))) {
  140. PKT_ERROR(pkt, "read beyond end of packet\n");
  141. return -EPROTO;
  142. }
  143. from = skb_tail_pointer(skb) - len;
  144. skb_trim(skb, skb->len - len);
  145. memcpy(data, from, len);
  146. return 0;
  147. }
  148. EXPORT_SYMBOL(cfpkt_extr_trail);
  149. int cfpkt_pad_trail(struct cfpkt *pkt, u16 len)
  150. {
  151. return cfpkt_add_body(pkt, NULL, len);
  152. }
  153. EXPORT_SYMBOL(cfpkt_pad_trail);
  154. int cfpkt_add_body(struct cfpkt *pkt, const void *data, u16 len)
  155. {
  156. struct sk_buff *skb = pkt_to_skb(pkt);
  157. struct sk_buff *lastskb;
  158. u8 *to;
  159. u16 addlen = 0;
  160. if (unlikely(is_erronous(pkt)))
  161. return -EPROTO;
  162. lastskb = skb;
  163. /* Check whether we need to add space at the tail */
  164. if (unlikely(skb_tailroom(skb) < len)) {
  165. if (likely(len < PKT_LEN_WHEN_EXTENDING))
  166. addlen = PKT_LEN_WHEN_EXTENDING;
  167. else
  168. addlen = len;
  169. }
  170. /* Check whether we need to change the SKB before writing to the tail */
  171. if (unlikely((addlen > 0) || skb_cloned(skb) || skb_shared(skb))) {
  172. /* Make sure data is writable */
  173. if (unlikely(skb_cow_data(skb, addlen, &lastskb) < 0)) {
  174. PKT_ERROR(pkt, "cow failed\n");
  175. return -EPROTO;
  176. }
  177. /*
  178. * Is the SKB non-linear after skb_cow_data()? If so, we are
  179. * going to add data to the last SKB, so we need to adjust
  180. * lengths of the top SKB.
  181. */
  182. if (lastskb != skb) {
  183. pr_warn("Packet is non-linear\n");
  184. skb->len += len;
  185. skb->data_len += len;
  186. }
  187. }
  188. /* All set to put the last SKB and optionally write data there. */
  189. to = skb_put(lastskb, len);
  190. if (likely(data))
  191. memcpy(to, data, len);
  192. return 0;
  193. }
  194. EXPORT_SYMBOL(cfpkt_add_body);
  195. inline int cfpkt_addbdy(struct cfpkt *pkt, u8 data)
  196. {
  197. return cfpkt_add_body(pkt, &data, 1);
  198. }
  199. EXPORT_SYMBOL(cfpkt_addbdy);
  200. int cfpkt_add_head(struct cfpkt *pkt, const void *data2, u16 len)
  201. {
  202. struct sk_buff *skb = pkt_to_skb(pkt);
  203. struct sk_buff *lastskb;
  204. u8 *to;
  205. const u8 *data = data2;
  206. int ret;
  207. if (unlikely(is_erronous(pkt)))
  208. return -EPROTO;
  209. if (unlikely(skb_headroom(skb) < len)) {
  210. PKT_ERROR(pkt, "no headroom\n");
  211. return -EPROTO;
  212. }
  213. /* Make sure data is writable */
  214. ret = skb_cow_data(skb, 0, &lastskb);
  215. if (unlikely(ret < 0)) {
  216. PKT_ERROR(pkt, "cow failed\n");
  217. return ret;
  218. }
  219. to = skb_push(skb, len);
  220. memcpy(to, data, len);
  221. return 0;
  222. }
  223. EXPORT_SYMBOL(cfpkt_add_head);
  224. inline int cfpkt_add_trail(struct cfpkt *pkt, const void *data, u16 len)
  225. {
  226. return cfpkt_add_body(pkt, data, len);
  227. }
  228. EXPORT_SYMBOL(cfpkt_add_trail);
  229. inline u16 cfpkt_getlen(struct cfpkt *pkt)
  230. {
  231. struct sk_buff *skb = pkt_to_skb(pkt);
  232. return skb->len;
  233. }
  234. EXPORT_SYMBOL(cfpkt_getlen);
  235. inline u16 cfpkt_iterate(struct cfpkt *pkt,
  236. u16 (*iter_func)(u16, void *, u16),
  237. u16 data)
  238. {
  239. /*
  240. * Don't care about the performance hit of linearizing,
  241. * Checksum should not be used on high-speed interfaces anyway.
  242. */
  243. if (unlikely(is_erronous(pkt)))
  244. return -EPROTO;
  245. if (unlikely(skb_linearize(&pkt->skb) != 0)) {
  246. PKT_ERROR(pkt, "linearize failed\n");
  247. return -EPROTO;
  248. }
  249. return iter_func(data, pkt->skb.data, cfpkt_getlen(pkt));
  250. }
  251. EXPORT_SYMBOL(cfpkt_iterate);
  252. int cfpkt_setlen(struct cfpkt *pkt, u16 len)
  253. {
  254. struct sk_buff *skb = pkt_to_skb(pkt);
  255. if (unlikely(is_erronous(pkt)))
  256. return -EPROTO;
  257. if (likely(len <= skb->len)) {
  258. if (unlikely(skb->data_len))
  259. ___pskb_trim(skb, len);
  260. else
  261. skb_trim(skb, len);
  262. return cfpkt_getlen(pkt);
  263. }
  264. /* Need to expand SKB */
  265. if (unlikely(!cfpkt_pad_trail(pkt, len - skb->len)))
  266. PKT_ERROR(pkt, "skb_pad_trail failed\n");
  267. return cfpkt_getlen(pkt);
  268. }
  269. EXPORT_SYMBOL(cfpkt_setlen);
  270. struct cfpkt *cfpkt_create_uplink(const unsigned char *data, unsigned int len)
  271. {
  272. struct cfpkt *pkt = cfpkt_create_pfx(len + PKT_POSTFIX, PKT_PREFIX);
  273. if (!pkt)
  274. return NULL;
  275. if (unlikely(data != NULL))
  276. cfpkt_add_body(pkt, data, len);
  277. return pkt;
  278. }
  279. EXPORT_SYMBOL(cfpkt_create_uplink);
  280. struct cfpkt *cfpkt_append(struct cfpkt *dstpkt,
  281. struct cfpkt *addpkt,
  282. u16 expectlen)
  283. {
  284. struct sk_buff *dst = pkt_to_skb(dstpkt);
  285. struct sk_buff *add = pkt_to_skb(addpkt);
  286. u16 addlen = skb_headlen(add);
  287. u16 neededtailspace;
  288. struct sk_buff *tmp;
  289. u16 dstlen;
  290. u16 createlen;
  291. if (unlikely(is_erronous(dstpkt) || is_erronous(addpkt))) {
  292. return dstpkt;
  293. }
  294. if (expectlen > addlen)
  295. neededtailspace = expectlen;
  296. else
  297. neededtailspace = addlen;
  298. if (dst->tail + neededtailspace > dst->end) {
  299. /* Create a dumplicate of 'dst' with more tail space */
  300. struct cfpkt *tmppkt;
  301. dstlen = skb_headlen(dst);
  302. createlen = dstlen + neededtailspace;
  303. tmppkt = cfpkt_create(createlen + PKT_PREFIX + PKT_POSTFIX);
  304. if (tmppkt == NULL)
  305. return NULL;
  306. tmp = pkt_to_skb(tmppkt);
  307. skb_set_tail_pointer(tmp, dstlen);
  308. tmp->len = dstlen;
  309. memcpy(tmp->data, dst->data, dstlen);
  310. cfpkt_destroy(dstpkt);
  311. dst = tmp;
  312. }
  313. memcpy(skb_tail_pointer(dst), add->data, skb_headlen(add));
  314. cfpkt_destroy(addpkt);
  315. dst->tail += addlen;
  316. dst->len += addlen;
  317. return skb_to_pkt(dst);
  318. }
  319. EXPORT_SYMBOL(cfpkt_append);
  320. struct cfpkt *cfpkt_split(struct cfpkt *pkt, u16 pos)
  321. {
  322. struct sk_buff *skb2;
  323. struct sk_buff *skb = pkt_to_skb(pkt);
  324. struct cfpkt *tmppkt;
  325. u8 *split = skb->data + pos;
  326. u16 len2nd = skb_tail_pointer(skb) - split;
  327. if (unlikely(is_erronous(pkt)))
  328. return NULL;
  329. if (skb->data + pos > skb_tail_pointer(skb)) {
  330. PKT_ERROR(pkt, "trying to split beyond end of packet\n");
  331. return NULL;
  332. }
  333. /* Create a new packet for the second part of the data */
  334. tmppkt = cfpkt_create_pfx(len2nd + PKT_PREFIX + PKT_POSTFIX,
  335. PKT_PREFIX);
  336. if (tmppkt == NULL)
  337. return NULL;
  338. skb2 = pkt_to_skb(tmppkt);
  339. if (skb2 == NULL)
  340. return NULL;
  341. /* Reduce the length of the original packet */
  342. skb_set_tail_pointer(skb, pos);
  343. skb->len = pos;
  344. memcpy(skb2->data, split, len2nd);
  345. skb2->tail += len2nd;
  346. skb2->len += len2nd;
  347. return skb_to_pkt(skb2);
  348. }
  349. EXPORT_SYMBOL(cfpkt_split);
  350. char *cfpkt_log_pkt(struct cfpkt *pkt, char *buf, int buflen)
  351. {
  352. struct sk_buff *skb = pkt_to_skb(pkt);
  353. char *p = buf;
  354. int i;
  355. /*
  356. * Sanity check buffer length, it needs to be at least as large as
  357. * the header info: ~=50+ bytes
  358. */
  359. if (buflen < 50)
  360. return NULL;
  361. snprintf(buf, buflen, "%s: pkt:%p len:%ld(%ld+%ld) {%ld,%ld} data: [",
  362. is_erronous(pkt) ? "ERRONOUS-SKB" :
  363. (skb->data_len != 0 ? "COMPLEX-SKB" : "SKB"),
  364. skb,
  365. (long) skb->len,
  366. (long) (skb_tail_pointer(skb) - skb->data),
  367. (long) skb->data_len,
  368. (long) (skb->data - skb->head),
  369. (long) (skb_tail_pointer(skb) - skb->head));
  370. p = buf + strlen(buf);
  371. for (i = 0; i < skb_tail_pointer(skb) - skb->data && i < 300; i++) {
  372. if (p > buf + buflen - 10) {
  373. sprintf(p, "...");
  374. p = buf + strlen(buf);
  375. break;
  376. }
  377. sprintf(p, "%02x,", skb->data[i]);
  378. p = buf + strlen(buf);
  379. }
  380. sprintf(p, "]\n");
  381. return buf;
  382. }
  383. EXPORT_SYMBOL(cfpkt_log_pkt);
  384. int cfpkt_raw_append(struct cfpkt *pkt, void **buf, unsigned int buflen)
  385. {
  386. struct sk_buff *skb = pkt_to_skb(pkt);
  387. struct sk_buff *lastskb;
  388. caif_assert(buf != NULL);
  389. if (unlikely(is_erronous(pkt)))
  390. return -EPROTO;
  391. /* Make sure SKB is writable */
  392. if (unlikely(skb_cow_data(skb, 0, &lastskb) < 0)) {
  393. PKT_ERROR(pkt, "skb_cow_data failed\n");
  394. return -EPROTO;
  395. }
  396. if (unlikely(skb_linearize(skb) != 0)) {
  397. PKT_ERROR(pkt, "linearize failed\n");
  398. return -EPROTO;
  399. }
  400. if (unlikely(skb_tailroom(skb) < buflen)) {
  401. PKT_ERROR(pkt, "buffer too short - failed\n");
  402. return -EPROTO;
  403. }
  404. *buf = skb_put(skb, buflen);
  405. return 1;
  406. }
  407. EXPORT_SYMBOL(cfpkt_raw_append);
  408. int cfpkt_raw_extract(struct cfpkt *pkt, void **buf, unsigned int buflen)
  409. {
  410. struct sk_buff *skb = pkt_to_skb(pkt);
  411. caif_assert(buf != NULL);
  412. if (unlikely(is_erronous(pkt)))
  413. return -EPROTO;
  414. if (unlikely(buflen > skb->len)) {
  415. PKT_ERROR(pkt, "buflen too large - failed\n");
  416. return -EPROTO;
  417. }
  418. if (unlikely(buflen > skb_headlen(skb))) {
  419. if (unlikely(skb_linearize(skb) != 0)) {
  420. PKT_ERROR(pkt, "linearize failed\n");
  421. return -EPROTO;
  422. }
  423. }
  424. *buf = skb->data;
  425. skb_pull(skb, buflen);
  426. return 1;
  427. }
  428. EXPORT_SYMBOL(cfpkt_raw_extract);
  429. inline bool cfpkt_erroneous(struct cfpkt *pkt)
  430. {
  431. return cfpkt_priv(pkt)->erronous;
  432. }
  433. EXPORT_SYMBOL(cfpkt_erroneous);
  434. struct cfpktq *cfpktq_create(void)
  435. {
  436. struct cfpktq *q = kmalloc(sizeof(struct cfpktq), GFP_ATOMIC);
  437. if (!q)
  438. return NULL;
  439. skb_queue_head_init(&q->head);
  440. atomic_set(&q->count, 0);
  441. spin_lock_init(&q->lock);
  442. return q;
  443. }
  444. EXPORT_SYMBOL(cfpktq_create);
  445. void cfpkt_queue(struct cfpktq *pktq, struct cfpkt *pkt, unsigned short prio)
  446. {
  447. atomic_inc(&pktq->count);
  448. spin_lock(&pktq->lock);
  449. skb_queue_tail(&pktq->head, pkt_to_skb(pkt));
  450. spin_unlock(&pktq->lock);
  451. }
  452. EXPORT_SYMBOL(cfpkt_queue);
  453. struct cfpkt *cfpkt_qpeek(struct cfpktq *pktq)
  454. {
  455. struct cfpkt *tmp;
  456. spin_lock(&pktq->lock);
  457. tmp = skb_to_pkt(skb_peek(&pktq->head));
  458. spin_unlock(&pktq->lock);
  459. return tmp;
  460. }
  461. EXPORT_SYMBOL(cfpkt_qpeek);
  462. struct cfpkt *cfpkt_dequeue(struct cfpktq *pktq)
  463. {
  464. struct cfpkt *pkt;
  465. spin_lock(&pktq->lock);
  466. pkt = skb_to_pkt(skb_dequeue(&pktq->head));
  467. if (pkt) {
  468. atomic_dec(&pktq->count);
  469. caif_assert(atomic_read(&pktq->count) >= 0);
  470. }
  471. spin_unlock(&pktq->lock);
  472. return pkt;
  473. }
  474. EXPORT_SYMBOL(cfpkt_dequeue);
  475. int cfpkt_qcount(struct cfpktq *pktq)
  476. {
  477. return atomic_read(&pktq->count);
  478. }
  479. EXPORT_SYMBOL(cfpkt_qcount);
  480. struct cfpkt *cfpkt_clone_release(struct cfpkt *pkt)
  481. {
  482. struct cfpkt *clone;
  483. clone = skb_to_pkt(skb_clone(pkt_to_skb(pkt), GFP_ATOMIC));
  484. /* Free original packet. */
  485. cfpkt_destroy(pkt);
  486. if (!clone)
  487. return NULL;
  488. return clone;
  489. }
  490. EXPORT_SYMBOL(cfpkt_clone_release);
  491. struct caif_payload_info *cfpkt_info(struct cfpkt *pkt)
  492. {
  493. return (struct caif_payload_info *)&pkt_to_skb(pkt)->cb;
  494. }
  495. EXPORT_SYMBOL(cfpkt_info);