cfpkt_skbuff.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 <linux/export.h>
  11. #include <net/caif/cfpkt.h>
  12. #define PKT_PREFIX 48
  13. #define PKT_POSTFIX 2
  14. #define PKT_LEN_WHEN_EXTENDING 128
  15. #define PKT_ERROR(pkt, errmsg) \
  16. do { \
  17. cfpkt_priv(pkt)->erronous = true; \
  18. skb_reset_tail_pointer(&pkt->skb); \
  19. pr_warn(errmsg); \
  20. } while (0)
  21. struct cfpktq {
  22. struct sk_buff_head head;
  23. atomic_t count;
  24. /* Lock protects count updates */
  25. spinlock_t lock;
  26. };
  27. /*
  28. * net/caif/ is generic and does not
  29. * understand SKB, so we do this typecast
  30. */
  31. struct cfpkt {
  32. struct sk_buff skb;
  33. };
  34. /* Private data inside SKB */
  35. struct cfpkt_priv_data {
  36. struct dev_info dev_info;
  37. bool erronous;
  38. };
  39. static inline struct cfpkt_priv_data *cfpkt_priv(struct cfpkt *pkt)
  40. {
  41. return (struct cfpkt_priv_data *) pkt->skb.cb;
  42. }
  43. static inline bool is_erronous(struct cfpkt *pkt)
  44. {
  45. return cfpkt_priv(pkt)->erronous;
  46. }
  47. static inline struct sk_buff *pkt_to_skb(struct cfpkt *pkt)
  48. {
  49. return &pkt->skb;
  50. }
  51. static inline struct cfpkt *skb_to_pkt(struct sk_buff *skb)
  52. {
  53. return (struct cfpkt *) skb;
  54. }
  55. struct cfpkt *cfpkt_fromnative(enum caif_direction dir, void *nativepkt)
  56. {
  57. struct cfpkt *pkt = skb_to_pkt(nativepkt);
  58. cfpkt_priv(pkt)->erronous = false;
  59. return pkt;
  60. }
  61. EXPORT_SYMBOL(cfpkt_fromnative);
  62. void *cfpkt_tonative(struct cfpkt *pkt)
  63. {
  64. return (void *) pkt;
  65. }
  66. EXPORT_SYMBOL(cfpkt_tonative);
  67. static struct cfpkt *cfpkt_create_pfx(u16 len, u16 pfx)
  68. {
  69. struct sk_buff *skb;
  70. if (likely(in_interrupt()))
  71. skb = alloc_skb(len + pfx, GFP_ATOMIC);
  72. else
  73. skb = alloc_skb(len + pfx, GFP_KERNEL);
  74. if (unlikely(skb == NULL))
  75. return NULL;
  76. skb_reserve(skb, pfx);
  77. return skb_to_pkt(skb);
  78. }
  79. inline struct cfpkt *cfpkt_create(u16 len)
  80. {
  81. return cfpkt_create_pfx(len + PKT_POSTFIX, PKT_PREFIX);
  82. }
  83. void cfpkt_destroy(struct cfpkt *pkt)
  84. {
  85. struct sk_buff *skb = pkt_to_skb(pkt);
  86. kfree_skb(skb);
  87. }
  88. inline bool cfpkt_more(struct cfpkt *pkt)
  89. {
  90. struct sk_buff *skb = pkt_to_skb(pkt);
  91. return skb->len > 0;
  92. }
  93. int cfpkt_peek_head(struct cfpkt *pkt, void *data, u16 len)
  94. {
  95. struct sk_buff *skb = pkt_to_skb(pkt);
  96. if (skb_headlen(skb) >= len) {
  97. memcpy(data, skb->data, len);
  98. return 0;
  99. }
  100. return !cfpkt_extr_head(pkt, data, len) &&
  101. !cfpkt_add_head(pkt, data, len);
  102. }
  103. int cfpkt_extr_head(struct cfpkt *pkt, void *data, u16 len)
  104. {
  105. struct sk_buff *skb = pkt_to_skb(pkt);
  106. u8 *from;
  107. if (unlikely(is_erronous(pkt)))
  108. return -EPROTO;
  109. if (unlikely(len > skb->len)) {
  110. PKT_ERROR(pkt, "read beyond end of packet\n");
  111. return -EPROTO;
  112. }
  113. if (unlikely(len > skb_headlen(skb))) {
  114. if (unlikely(skb_linearize(skb) != 0)) {
  115. PKT_ERROR(pkt, "linearize failed\n");
  116. return -EPROTO;
  117. }
  118. }
  119. from = skb_pull(skb, len);
  120. from -= len;
  121. if (data)
  122. memcpy(data, from, len);
  123. return 0;
  124. }
  125. EXPORT_SYMBOL(cfpkt_extr_head);
  126. int cfpkt_extr_trail(struct cfpkt *pkt, void *dta, u16 len)
  127. {
  128. struct sk_buff *skb = pkt_to_skb(pkt);
  129. u8 *data = dta;
  130. u8 *from;
  131. if (unlikely(is_erronous(pkt)))
  132. return -EPROTO;
  133. if (unlikely(skb_linearize(skb) != 0)) {
  134. PKT_ERROR(pkt, "linearize failed\n");
  135. return -EPROTO;
  136. }
  137. if (unlikely(skb->data + len > skb_tail_pointer(skb))) {
  138. PKT_ERROR(pkt, "read beyond end of packet\n");
  139. return -EPROTO;
  140. }
  141. from = skb_tail_pointer(skb) - len;
  142. skb_trim(skb, skb->len - len);
  143. memcpy(data, from, len);
  144. return 0;
  145. }
  146. int cfpkt_pad_trail(struct cfpkt *pkt, u16 len)
  147. {
  148. return cfpkt_add_body(pkt, NULL, len);
  149. }
  150. int cfpkt_add_body(struct cfpkt *pkt, const void *data, u16 len)
  151. {
  152. struct sk_buff *skb = pkt_to_skb(pkt);
  153. struct sk_buff *lastskb;
  154. u8 *to;
  155. u16 addlen = 0;
  156. if (unlikely(is_erronous(pkt)))
  157. return -EPROTO;
  158. lastskb = skb;
  159. /* Check whether we need to add space at the tail */
  160. if (unlikely(skb_tailroom(skb) < len)) {
  161. if (likely(len < PKT_LEN_WHEN_EXTENDING))
  162. addlen = PKT_LEN_WHEN_EXTENDING;
  163. else
  164. addlen = len;
  165. }
  166. /* Check whether we need to change the SKB before writing to the tail */
  167. if (unlikely((addlen > 0) || skb_cloned(skb) || skb_shared(skb))) {
  168. /* Make sure data is writable */
  169. if (unlikely(skb_cow_data(skb, addlen, &lastskb) < 0)) {
  170. PKT_ERROR(pkt, "cow failed\n");
  171. return -EPROTO;
  172. }
  173. /*
  174. * Is the SKB non-linear after skb_cow_data()? If so, we are
  175. * going to add data to the last SKB, so we need to adjust
  176. * lengths of the top SKB.
  177. */
  178. if (lastskb != skb) {
  179. pr_warn("Packet is non-linear\n");
  180. skb->len += len;
  181. skb->data_len += len;
  182. }
  183. }
  184. /* All set to put the last SKB and optionally write data there. */
  185. to = skb_put(lastskb, len);
  186. if (likely(data))
  187. memcpy(to, data, len);
  188. return 0;
  189. }
  190. inline int cfpkt_addbdy(struct cfpkt *pkt, u8 data)
  191. {
  192. return cfpkt_add_body(pkt, &data, 1);
  193. }
  194. int cfpkt_add_head(struct cfpkt *pkt, const void *data2, u16 len)
  195. {
  196. struct sk_buff *skb = pkt_to_skb(pkt);
  197. struct sk_buff *lastskb;
  198. u8 *to;
  199. const u8 *data = data2;
  200. int ret;
  201. if (unlikely(is_erronous(pkt)))
  202. return -EPROTO;
  203. if (unlikely(skb_headroom(skb) < len)) {
  204. PKT_ERROR(pkt, "no headroom\n");
  205. return -EPROTO;
  206. }
  207. /* Make sure data is writable */
  208. ret = skb_cow_data(skb, 0, &lastskb);
  209. if (unlikely(ret < 0)) {
  210. PKT_ERROR(pkt, "cow failed\n");
  211. return ret;
  212. }
  213. to = skb_push(skb, len);
  214. memcpy(to, data, len);
  215. return 0;
  216. }
  217. EXPORT_SYMBOL(cfpkt_add_head);
  218. inline int cfpkt_add_trail(struct cfpkt *pkt, const void *data, u16 len)
  219. {
  220. return cfpkt_add_body(pkt, data, len);
  221. }
  222. inline u16 cfpkt_getlen(struct cfpkt *pkt)
  223. {
  224. struct sk_buff *skb = pkt_to_skb(pkt);
  225. return skb->len;
  226. }
  227. inline u16 cfpkt_iterate(struct cfpkt *pkt,
  228. u16 (*iter_func)(u16, void *, u16),
  229. u16 data)
  230. {
  231. /*
  232. * Don't care about the performance hit of linearizing,
  233. * Checksum should not be used on high-speed interfaces anyway.
  234. */
  235. if (unlikely(is_erronous(pkt)))
  236. return -EPROTO;
  237. if (unlikely(skb_linearize(&pkt->skb) != 0)) {
  238. PKT_ERROR(pkt, "linearize failed\n");
  239. return -EPROTO;
  240. }
  241. return iter_func(data, pkt->skb.data, cfpkt_getlen(pkt));
  242. }
  243. int cfpkt_setlen(struct cfpkt *pkt, u16 len)
  244. {
  245. struct sk_buff *skb = pkt_to_skb(pkt);
  246. if (unlikely(is_erronous(pkt)))
  247. return -EPROTO;
  248. if (likely(len <= skb->len)) {
  249. if (unlikely(skb->data_len))
  250. ___pskb_trim(skb, len);
  251. else
  252. skb_trim(skb, len);
  253. return cfpkt_getlen(pkt);
  254. }
  255. /* Need to expand SKB */
  256. if (unlikely(!cfpkt_pad_trail(pkt, len - skb->len)))
  257. PKT_ERROR(pkt, "skb_pad_trail failed\n");
  258. return cfpkt_getlen(pkt);
  259. }
  260. struct cfpkt *cfpkt_append(struct cfpkt *dstpkt,
  261. struct cfpkt *addpkt,
  262. u16 expectlen)
  263. {
  264. struct sk_buff *dst = pkt_to_skb(dstpkt);
  265. struct sk_buff *add = pkt_to_skb(addpkt);
  266. u16 addlen = skb_headlen(add);
  267. u16 neededtailspace;
  268. struct sk_buff *tmp;
  269. u16 dstlen;
  270. u16 createlen;
  271. if (unlikely(is_erronous(dstpkt) || is_erronous(addpkt))) {
  272. return dstpkt;
  273. }
  274. if (expectlen > addlen)
  275. neededtailspace = expectlen;
  276. else
  277. neededtailspace = addlen;
  278. if (dst->tail + neededtailspace > dst->end) {
  279. /* Create a dumplicate of 'dst' with more tail space */
  280. struct cfpkt *tmppkt;
  281. dstlen = skb_headlen(dst);
  282. createlen = dstlen + neededtailspace;
  283. tmppkt = cfpkt_create(createlen + PKT_PREFIX + PKT_POSTFIX);
  284. if (tmppkt == NULL)
  285. return NULL;
  286. tmp = pkt_to_skb(tmppkt);
  287. skb_set_tail_pointer(tmp, dstlen);
  288. tmp->len = dstlen;
  289. memcpy(tmp->data, dst->data, dstlen);
  290. cfpkt_destroy(dstpkt);
  291. dst = tmp;
  292. }
  293. memcpy(skb_tail_pointer(dst), add->data, skb_headlen(add));
  294. cfpkt_destroy(addpkt);
  295. dst->tail += addlen;
  296. dst->len += addlen;
  297. return skb_to_pkt(dst);
  298. }
  299. struct cfpkt *cfpkt_split(struct cfpkt *pkt, u16 pos)
  300. {
  301. struct sk_buff *skb2;
  302. struct sk_buff *skb = pkt_to_skb(pkt);
  303. struct cfpkt *tmppkt;
  304. u8 *split = skb->data + pos;
  305. u16 len2nd = skb_tail_pointer(skb) - split;
  306. if (unlikely(is_erronous(pkt)))
  307. return NULL;
  308. if (skb->data + pos > skb_tail_pointer(skb)) {
  309. PKT_ERROR(pkt, "trying to split beyond end of packet\n");
  310. return NULL;
  311. }
  312. /* Create a new packet for the second part of the data */
  313. tmppkt = cfpkt_create_pfx(len2nd + PKT_PREFIX + PKT_POSTFIX,
  314. PKT_PREFIX);
  315. if (tmppkt == NULL)
  316. return NULL;
  317. skb2 = pkt_to_skb(tmppkt);
  318. if (skb2 == NULL)
  319. return NULL;
  320. /* Reduce the length of the original packet */
  321. skb_set_tail_pointer(skb, pos);
  322. skb->len = pos;
  323. memcpy(skb2->data, split, len2nd);
  324. skb2->tail += len2nd;
  325. skb2->len += len2nd;
  326. return skb_to_pkt(skb2);
  327. }
  328. bool cfpkt_erroneous(struct cfpkt *pkt)
  329. {
  330. return cfpkt_priv(pkt)->erronous;
  331. }
  332. struct caif_payload_info *cfpkt_info(struct cfpkt *pkt)
  333. {
  334. return (struct caif_payload_info *)&pkt_to_skb(pkt)->cb;
  335. }
  336. EXPORT_SYMBOL(cfpkt_info);