cfpkt_skbuff.c 8.0 KB

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