cfpkt_skbuff.c 8.5 KB

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