drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / net / caif / cfpkt_skbuff.c
blob96236d21b18e5cabc75e493927f11c77dc04a783
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) ST-Ericsson AB 2010
4 * Author: Sjur Brendeland
5 */
7 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
9 #include <linux/string.h>
10 #include <linux/skbuff.h>
11 #include <linux/export.h>
12 #include <net/caif/cfpkt.h>
14 #define PKT_PREFIX 48
15 #define PKT_POSTFIX 2
16 #define PKT_LEN_WHEN_EXTENDING 128
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)
25 * net/caif/ is generic and does not
26 * understand SKB, so we do this typecast
28 struct cfpkt {
29 struct sk_buff skb;
32 /* Private data inside SKB */
33 struct cfpkt_priv_data {
34 struct dev_info dev_info;
35 bool erronous;
38 static inline struct cfpkt_priv_data *cfpkt_priv(struct cfpkt *pkt)
40 return (struct cfpkt_priv_data *) pkt->skb.cb;
43 static inline bool is_erronous(struct cfpkt *pkt)
45 return cfpkt_priv(pkt)->erronous;
48 static inline struct sk_buff *pkt_to_skb(struct cfpkt *pkt)
50 return &pkt->skb;
53 static inline struct cfpkt *skb_to_pkt(struct sk_buff *skb)
55 return (struct cfpkt *) skb;
58 struct cfpkt *cfpkt_fromnative(enum caif_direction dir, void *nativepkt)
60 struct cfpkt *pkt = skb_to_pkt(nativepkt);
61 cfpkt_priv(pkt)->erronous = false;
62 return pkt;
64 EXPORT_SYMBOL(cfpkt_fromnative);
66 void *cfpkt_tonative(struct cfpkt *pkt)
68 return (void *) pkt;
70 EXPORT_SYMBOL(cfpkt_tonative);
72 static struct cfpkt *cfpkt_create_pfx(u16 len, u16 pfx)
74 struct sk_buff *skb;
76 skb = alloc_skb(len + pfx, GFP_ATOMIC);
77 if (unlikely(skb == NULL))
78 return NULL;
80 skb_reserve(skb, pfx);
81 return skb_to_pkt(skb);
84 inline struct cfpkt *cfpkt_create(u16 len)
86 return cfpkt_create_pfx(len + PKT_POSTFIX, PKT_PREFIX);
89 void cfpkt_destroy(struct cfpkt *pkt)
91 struct sk_buff *skb = pkt_to_skb(pkt);
92 kfree_skb(skb);
95 inline bool cfpkt_more(struct cfpkt *pkt)
97 struct sk_buff *skb = pkt_to_skb(pkt);
98 return skb->len > 0;
101 int cfpkt_peek_head(struct cfpkt *pkt, void *data, u16 len)
103 struct sk_buff *skb = pkt_to_skb(pkt);
104 if (skb_headlen(skb) >= len) {
105 memcpy(data, skb->data, len);
106 return 0;
108 return !cfpkt_extr_head(pkt, data, len) &&
109 !cfpkt_add_head(pkt, data, len);
112 int cfpkt_extr_head(struct cfpkt *pkt, void *data, u16 len)
114 struct sk_buff *skb = pkt_to_skb(pkt);
115 u8 *from;
116 if (unlikely(is_erronous(pkt)))
117 return -EPROTO;
119 if (unlikely(len > skb->len)) {
120 PKT_ERROR(pkt, "read beyond end of packet\n");
121 return -EPROTO;
124 if (unlikely(len > skb_headlen(skb))) {
125 if (unlikely(skb_linearize(skb) != 0)) {
126 PKT_ERROR(pkt, "linearize failed\n");
127 return -EPROTO;
130 from = skb_pull(skb, len);
131 from -= len;
132 if (data)
133 memcpy(data, from, len);
134 return 0;
136 EXPORT_SYMBOL(cfpkt_extr_head);
138 int cfpkt_extr_trail(struct cfpkt *pkt, void *dta, u16 len)
140 struct sk_buff *skb = pkt_to_skb(pkt);
141 u8 *data = dta;
142 u8 *from;
143 if (unlikely(is_erronous(pkt)))
144 return -EPROTO;
146 if (unlikely(skb_linearize(skb) != 0)) {
147 PKT_ERROR(pkt, "linearize failed\n");
148 return -EPROTO;
150 if (unlikely(skb->data + len > skb_tail_pointer(skb))) {
151 PKT_ERROR(pkt, "read beyond end of packet\n");
152 return -EPROTO;
154 from = skb_tail_pointer(skb) - len;
155 skb_trim(skb, skb->len - len);
156 memcpy(data, from, len);
157 return 0;
160 int cfpkt_pad_trail(struct cfpkt *pkt, u16 len)
162 return cfpkt_add_body(pkt, NULL, len);
165 int cfpkt_add_body(struct cfpkt *pkt, const void *data, u16 len)
167 struct sk_buff *skb = pkt_to_skb(pkt);
168 struct sk_buff *lastskb;
169 u8 *to;
170 u16 addlen = 0;
173 if (unlikely(is_erronous(pkt)))
174 return -EPROTO;
176 lastskb = skb;
178 /* Check whether we need to add space at the tail */
179 if (unlikely(skb_tailroom(skb) < len)) {
180 if (likely(len < PKT_LEN_WHEN_EXTENDING))
181 addlen = PKT_LEN_WHEN_EXTENDING;
182 else
183 addlen = len;
186 /* Check whether we need to change the SKB before writing to the tail */
187 if (unlikely((addlen > 0) || skb_cloned(skb) || skb_shared(skb))) {
189 /* Make sure data is writable */
190 if (unlikely(skb_cow_data(skb, addlen, &lastskb) < 0)) {
191 PKT_ERROR(pkt, "cow failed\n");
192 return -EPROTO;
196 /* All set to put the last SKB and optionally write data there. */
197 to = pskb_put(skb, lastskb, len);
198 if (likely(data))
199 memcpy(to, data, len);
200 return 0;
203 inline int cfpkt_addbdy(struct cfpkt *pkt, u8 data)
205 return cfpkt_add_body(pkt, &data, 1);
208 int cfpkt_add_head(struct cfpkt *pkt, const void *data2, u16 len)
210 struct sk_buff *skb = pkt_to_skb(pkt);
211 struct sk_buff *lastskb;
212 u8 *to;
213 const u8 *data = data2;
214 int ret;
215 if (unlikely(is_erronous(pkt)))
216 return -EPROTO;
217 if (unlikely(skb_headroom(skb) < len)) {
218 PKT_ERROR(pkt, "no headroom\n");
219 return -EPROTO;
222 /* Make sure data is writable */
223 ret = skb_cow_data(skb, 0, &lastskb);
224 if (unlikely(ret < 0)) {
225 PKT_ERROR(pkt, "cow failed\n");
226 return ret;
229 to = skb_push(skb, len);
230 memcpy(to, data, len);
231 return 0;
233 EXPORT_SYMBOL(cfpkt_add_head);
235 inline int cfpkt_add_trail(struct cfpkt *pkt, const void *data, u16 len)
237 return cfpkt_add_body(pkt, data, len);
240 inline u16 cfpkt_getlen(struct cfpkt *pkt)
242 struct sk_buff *skb = pkt_to_skb(pkt);
243 return skb->len;
246 int cfpkt_iterate(struct cfpkt *pkt,
247 u16 (*iter_func)(u16, void *, u16),
248 u16 data)
251 * Don't care about the performance hit of linearizing,
252 * Checksum should not be used on high-speed interfaces anyway.
254 if (unlikely(is_erronous(pkt)))
255 return -EPROTO;
256 if (unlikely(skb_linearize(&pkt->skb) != 0)) {
257 PKT_ERROR(pkt, "linearize failed\n");
258 return -EPROTO;
260 return iter_func(data, pkt->skb.data, cfpkt_getlen(pkt));
263 int cfpkt_setlen(struct cfpkt *pkt, u16 len)
265 struct sk_buff *skb = pkt_to_skb(pkt);
268 if (unlikely(is_erronous(pkt)))
269 return -EPROTO;
271 if (likely(len <= skb->len)) {
272 if (unlikely(skb->data_len))
273 ___pskb_trim(skb, len);
274 else
275 skb_trim(skb, len);
277 return cfpkt_getlen(pkt);
280 /* Need to expand SKB */
281 if (unlikely(!cfpkt_pad_trail(pkt, len - skb->len)))
282 PKT_ERROR(pkt, "skb_pad_trail failed\n");
284 return cfpkt_getlen(pkt);
287 struct cfpkt *cfpkt_append(struct cfpkt *dstpkt,
288 struct cfpkt *addpkt,
289 u16 expectlen)
291 struct sk_buff *dst = pkt_to_skb(dstpkt);
292 struct sk_buff *add = pkt_to_skb(addpkt);
293 u16 addlen = skb_headlen(add);
294 u16 neededtailspace;
295 struct sk_buff *tmp;
296 u16 dstlen;
297 u16 createlen;
298 if (unlikely(is_erronous(dstpkt) || is_erronous(addpkt))) {
299 return dstpkt;
302 neededtailspace = max(expectlen, addlen);
304 if (dst->tail + neededtailspace > dst->end) {
305 /* Create a dumplicate of 'dst' with more tail space */
306 struct cfpkt *tmppkt;
307 dstlen = skb_headlen(dst);
308 createlen = dstlen + neededtailspace;
309 tmppkt = cfpkt_create(createlen + PKT_PREFIX + PKT_POSTFIX);
310 if (tmppkt == NULL)
311 return NULL;
312 tmp = pkt_to_skb(tmppkt);
313 skb_put_data(tmp, dst->data, dstlen);
314 cfpkt_destroy(dstpkt);
315 dst = tmp;
317 skb_put_data(dst, add->data, skb_headlen(add));
318 cfpkt_destroy(addpkt);
319 return skb_to_pkt(dst);
322 struct cfpkt *cfpkt_split(struct cfpkt *pkt, u16 pos)
324 struct sk_buff *skb2;
325 struct sk_buff *skb = pkt_to_skb(pkt);
326 struct cfpkt *tmppkt;
327 u8 *split = skb->data + pos;
328 u16 len2nd = skb_tail_pointer(skb) - split;
330 if (unlikely(is_erronous(pkt)))
331 return NULL;
333 if (skb->data + pos > skb_tail_pointer(skb)) {
334 PKT_ERROR(pkt, "trying to split beyond end of packet\n");
335 return NULL;
338 /* Create a new packet for the second part of the data */
339 tmppkt = cfpkt_create_pfx(len2nd + PKT_PREFIX + PKT_POSTFIX,
340 PKT_PREFIX);
341 if (tmppkt == NULL)
342 return NULL;
343 skb2 = pkt_to_skb(tmppkt);
346 if (skb2 == NULL)
347 return NULL;
349 skb_put_data(skb2, split, len2nd);
351 /* Reduce the length of the original packet */
352 skb_trim(skb, pos);
354 skb2->priority = skb->priority;
355 return skb_to_pkt(skb2);
358 bool cfpkt_erroneous(struct cfpkt *pkt)
360 return cfpkt_priv(pkt)->erronous;
363 struct caif_payload_info *cfpkt_info(struct cfpkt *pkt)
365 return (struct caif_payload_info *)&pkt_to_skb(pkt)->cb;
367 EXPORT_SYMBOL(cfpkt_info);
369 void cfpkt_set_prio(struct cfpkt *pkt, int prio)
371 pkt_to_skb(pkt)->priority = prio;
373 EXPORT_SYMBOL(cfpkt_set_prio);