1
2
3
4
5
6
7
8
9
10
11#include <linux/bpf.h>
12#include <linux/errno.h>
13#include <linux/errqueue.h>
14#include <linux/file.h>
15#include <linux/in.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/net.h>
19#include <linux/netdevice.h>
20#include <linux/poll.h>
21#include <linux/rculist.h>
22#include <linux/skbuff.h>
23#include <linux/socket.h>
24#include <linux/uaccess.h>
25#include <linux/workqueue.h>
26#include <net/strparser.h>
27#include <net/netns/generic.h>
28#include <net/sock.h>
29
30static struct workqueue_struct *strp_wq;
31
32struct _strp_msg {
33
34
35
36 struct strp_msg strp;
37 int accum_len;
38};
39
40static inline struct _strp_msg *_strp_msg(struct sk_buff *skb)
41{
42 return (struct _strp_msg *)((void *)skb->cb +
43 offsetof(struct qdisc_skb_cb, data));
44}
45
46
47static void strp_abort_strp(struct strparser *strp, int err)
48{
49
50
51 cancel_delayed_work(&strp->msg_timer_work);
52
53 if (strp->stopped)
54 return;
55
56 strp->stopped = 1;
57
58 if (strp->sk) {
59 struct sock *sk = strp->sk;
60
61
62 sk->sk_err = -err;
63 sk->sk_error_report(sk);
64 }
65}
66
67static void strp_start_timer(struct strparser *strp, long timeo)
68{
69 if (timeo && timeo != LONG_MAX)
70 mod_delayed_work(strp_wq, &strp->msg_timer_work, timeo);
71}
72
73
74static void strp_parser_err(struct strparser *strp, int err,
75 read_descriptor_t *desc)
76{
77 desc->error = err;
78 kfree_skb(strp->skb_head);
79 strp->skb_head = NULL;
80 strp->cb.abort_parser(strp, err);
81}
82
83static inline int strp_peek_len(struct strparser *strp)
84{
85 if (strp->sk) {
86 struct socket *sock = strp->sk->sk_socket;
87
88 return sock->ops->peek_len(sock);
89 }
90
91
92
93
94
95 return INT_MAX;
96}
97
98
99static int __strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
100 unsigned int orig_offset, size_t orig_len,
101 size_t max_msg_size, long timeo)
102{
103 struct strparser *strp = (struct strparser *)desc->arg.data;
104 struct _strp_msg *stm;
105 struct sk_buff *head, *skb;
106 size_t eaten = 0, cand_len;
107 ssize_t extra;
108 int err;
109 bool cloned_orig = false;
110
111 if (strp->paused)
112 return 0;
113
114 head = strp->skb_head;
115 if (head) {
116
117 if (unlikely(orig_offset)) {
118
119
120
121
122
123 orig_skb = skb_clone(orig_skb, GFP_ATOMIC);
124 if (!orig_skb) {
125 STRP_STATS_INCR(strp->stats.mem_fail);
126 desc->error = -ENOMEM;
127 return 0;
128 }
129 if (!pskb_pull(orig_skb, orig_offset)) {
130 STRP_STATS_INCR(strp->stats.mem_fail);
131 kfree_skb(orig_skb);
132 desc->error = -ENOMEM;
133 return 0;
134 }
135 cloned_orig = true;
136 orig_offset = 0;
137 }
138
139 if (!strp->skb_nextp) {
140
141
142
143 if (skb_has_frag_list(head)) {
144 err = skb_unclone(head, GFP_ATOMIC);
145 if (err) {
146 STRP_STATS_INCR(strp->stats.mem_fail);
147 desc->error = err;
148 return 0;
149 }
150 }
151
152 if (unlikely(skb_shinfo(head)->frag_list)) {
153
154
155
156
157
158
159 if (WARN_ON(head->next)) {
160 desc->error = -EINVAL;
161 return 0;
162 }
163
164 skb = alloc_skb(0, GFP_ATOMIC);
165 if (!skb) {
166 STRP_STATS_INCR(strp->stats.mem_fail);
167 desc->error = -ENOMEM;
168 return 0;
169 }
170 skb->len = head->len;
171 skb->data_len = head->len;
172 skb->truesize = head->truesize;
173 *_strp_msg(skb) = *_strp_msg(head);
174 strp->skb_nextp = &head->next;
175 skb_shinfo(skb)->frag_list = head;
176 strp->skb_head = skb;
177 head = skb;
178 } else {
179 strp->skb_nextp =
180 &skb_shinfo(head)->frag_list;
181 }
182 }
183 }
184
185 while (eaten < orig_len) {
186
187 skb = skb_clone(orig_skb, GFP_ATOMIC);
188 if (!skb) {
189 STRP_STATS_INCR(strp->stats.mem_fail);
190 desc->error = -ENOMEM;
191 break;
192 }
193
194 cand_len = orig_len - eaten;
195
196 head = strp->skb_head;
197 if (!head) {
198 head = skb;
199 strp->skb_head = head;
200
201 strp->skb_nextp = NULL;
202 stm = _strp_msg(head);
203 memset(stm, 0, sizeof(*stm));
204 stm->strp.offset = orig_offset + eaten;
205 } else {
206
207
208
209 if (skb_has_frag_list(skb)) {
210 err = skb_unclone(skb, GFP_ATOMIC);
211 if (err) {
212 STRP_STATS_INCR(strp->stats.mem_fail);
213 desc->error = err;
214 break;
215 }
216 }
217
218 stm = _strp_msg(head);
219 *strp->skb_nextp = skb;
220 strp->skb_nextp = &skb->next;
221 head->data_len += skb->len;
222 head->len += skb->len;
223 head->truesize += skb->truesize;
224 }
225
226 if (!stm->strp.full_len) {
227 ssize_t len;
228
229 len = (*strp->cb.parse_msg)(strp, head);
230
231 if (!len) {
232
233 if (!stm->accum_len) {
234
235 strp_start_timer(strp, timeo);
236 }
237 stm->accum_len += cand_len;
238 eaten += cand_len;
239 STRP_STATS_INCR(strp->stats.need_more_hdr);
240 WARN_ON(eaten != orig_len);
241 break;
242 } else if (len < 0) {
243 if (len == -ESTRPIPE && stm->accum_len) {
244 len = -ENODATA;
245 strp->unrecov_intr = 1;
246 } else {
247 strp->interrupted = 1;
248 }
249 strp_parser_err(strp, len, desc);
250 break;
251 } else if (len > max_msg_size) {
252
253 STRP_STATS_INCR(strp->stats.msg_too_big);
254 strp_parser_err(strp, -EMSGSIZE, desc);
255 break;
256 } else if (len <= (ssize_t)head->len -
257 skb->len - stm->strp.offset) {
258
259
260
261 STRP_STATS_INCR(strp->stats.bad_hdr_len);
262 strp_parser_err(strp, -EPROTO, desc);
263 break;
264 }
265
266 stm->strp.full_len = len;
267 }
268
269 extra = (ssize_t)(stm->accum_len + cand_len) -
270 stm->strp.full_len;
271
272 if (extra < 0) {
273
274 if (stm->strp.full_len - stm->accum_len >
275 strp_peek_len(strp)) {
276
277
278
279
280
281
282
283 if (!stm->accum_len) {
284
285 strp_start_timer(strp, timeo);
286 }
287
288 stm->accum_len += cand_len;
289 eaten += cand_len;
290 strp->need_bytes = stm->strp.full_len -
291 stm->accum_len;
292 STRP_STATS_ADD(strp->stats.bytes, cand_len);
293 desc->count = 0;
294 break;
295 }
296 stm->accum_len += cand_len;
297 eaten += cand_len;
298 WARN_ON(eaten != orig_len);
299 break;
300 }
301
302
303
304
305
306 WARN_ON(extra > cand_len);
307
308 eaten += (cand_len - extra);
309
310
311 cancel_delayed_work(&strp->msg_timer_work);
312 strp->skb_head = NULL;
313 strp->need_bytes = 0;
314 STRP_STATS_INCR(strp->stats.msgs);
315
316
317 strp->cb.rcv_msg(strp, head);
318
319 if (unlikely(strp->paused)) {
320
321 break;
322 }
323 }
324
325 if (cloned_orig)
326 kfree_skb(orig_skb);
327
328 STRP_STATS_ADD(strp->stats.bytes, eaten);
329
330 return eaten;
331}
332
333int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
334 unsigned int orig_offset, size_t orig_len,
335 size_t max_msg_size, long timeo)
336{
337 read_descriptor_t desc;
338
339 desc.arg.data = strp;
340
341 return __strp_recv(&desc, orig_skb, orig_offset, orig_len,
342 max_msg_size, timeo);
343}
344EXPORT_SYMBOL_GPL(strp_process);
345
346static int strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
347 unsigned int orig_offset, size_t orig_len)
348{
349 struct strparser *strp = (struct strparser *)desc->arg.data;
350
351 return __strp_recv(desc, orig_skb, orig_offset, orig_len,
352 strp->sk->sk_rcvbuf, strp->sk->sk_rcvtimeo);
353}
354
355static int default_read_sock_done(struct strparser *strp, int err)
356{
357 return err;
358}
359
360
361static int strp_read_sock(struct strparser *strp)
362{
363 struct socket *sock = strp->sk->sk_socket;
364 read_descriptor_t desc;
365
366 if (unlikely(!sock || !sock->ops || !sock->ops->read_sock))
367 return -EBUSY;
368
369 desc.arg.data = strp;
370 desc.error = 0;
371 desc.count = 1;
372
373
374 sock->ops->read_sock(strp->sk, &desc, strp_recv);
375
376 desc.error = strp->cb.read_sock_done(strp, desc.error);
377
378 return desc.error;
379}
380
381
382void strp_data_ready(struct strparser *strp)
383{
384 if (unlikely(strp->stopped) || strp->paused)
385 return;
386
387
388
389
390
391
392
393
394 if (sock_owned_by_user_nocheck(strp->sk)) {
395 queue_work(strp_wq, &strp->work);
396 return;
397 }
398
399 if (strp->need_bytes) {
400 if (strp_peek_len(strp) < strp->need_bytes)
401 return;
402 }
403
404 if (strp_read_sock(strp) == -ENOMEM)
405 queue_work(strp_wq, &strp->work);
406}
407EXPORT_SYMBOL_GPL(strp_data_ready);
408
409static void do_strp_work(struct strparser *strp)
410{
411
412
413
414 strp->cb.lock(strp);
415
416 if (unlikely(strp->stopped))
417 goto out;
418
419 if (strp->paused)
420 goto out;
421
422 if (strp_read_sock(strp) == -ENOMEM)
423 queue_work(strp_wq, &strp->work);
424
425out:
426 strp->cb.unlock(strp);
427}
428
429static void strp_work(struct work_struct *w)
430{
431 do_strp_work(container_of(w, struct strparser, work));
432}
433
434static void strp_msg_timeout(struct work_struct *w)
435{
436 struct strparser *strp = container_of(w, struct strparser,
437 msg_timer_work.work);
438
439
440 STRP_STATS_INCR(strp->stats.msg_timeouts);
441 strp->cb.lock(strp);
442 strp->cb.abort_parser(strp, -ETIMEDOUT);
443 strp->cb.unlock(strp);
444}
445
446static void strp_sock_lock(struct strparser *strp)
447{
448 lock_sock(strp->sk);
449}
450
451static void strp_sock_unlock(struct strparser *strp)
452{
453 release_sock(strp->sk);
454}
455
456int strp_init(struct strparser *strp, struct sock *sk,
457 const struct strp_callbacks *cb)
458{
459
460 if (!cb || !cb->rcv_msg || !cb->parse_msg)
461 return -EINVAL;
462
463
464
465
466
467
468
469
470
471
472
473
474 if (!sk) {
475 if (!cb->lock || !cb->unlock)
476 return -EINVAL;
477 }
478
479 memset(strp, 0, sizeof(*strp));
480
481 strp->sk = sk;
482
483 strp->cb.lock = cb->lock ? : strp_sock_lock;
484 strp->cb.unlock = cb->unlock ? : strp_sock_unlock;
485 strp->cb.rcv_msg = cb->rcv_msg;
486 strp->cb.parse_msg = cb->parse_msg;
487 strp->cb.read_sock_done = cb->read_sock_done ? : default_read_sock_done;
488 strp->cb.abort_parser = cb->abort_parser ? : strp_abort_strp;
489
490 INIT_DELAYED_WORK(&strp->msg_timer_work, strp_msg_timeout);
491 INIT_WORK(&strp->work, strp_work);
492
493 return 0;
494}
495EXPORT_SYMBOL_GPL(strp_init);
496
497
498void __strp_unpause(struct strparser *strp)
499{
500 strp->paused = 0;
501
502 if (strp->need_bytes) {
503 if (strp_peek_len(strp) < strp->need_bytes)
504 return;
505 }
506 strp_read_sock(strp);
507}
508EXPORT_SYMBOL_GPL(__strp_unpause);
509
510void strp_unpause(struct strparser *strp)
511{
512 strp->paused = 0;
513
514
515 smp_mb();
516
517 queue_work(strp_wq, &strp->work);
518}
519EXPORT_SYMBOL_GPL(strp_unpause);
520
521
522
523
524void strp_done(struct strparser *strp)
525{
526 WARN_ON(!strp->stopped);
527
528 cancel_delayed_work_sync(&strp->msg_timer_work);
529 cancel_work_sync(&strp->work);
530
531 if (strp->skb_head) {
532 kfree_skb(strp->skb_head);
533 strp->skb_head = NULL;
534 }
535}
536EXPORT_SYMBOL_GPL(strp_done);
537
538void strp_stop(struct strparser *strp)
539{
540 strp->stopped = 1;
541}
542EXPORT_SYMBOL_GPL(strp_stop);
543
544void strp_check_rcv(struct strparser *strp)
545{
546 queue_work(strp_wq, &strp->work);
547}
548EXPORT_SYMBOL_GPL(strp_check_rcv);
549
550static int __init strp_mod_init(void)
551{
552 strp_wq = create_singlethread_workqueue("kstrp");
553
554 return 0;
555}
556
557static void __exit strp_mod_exit(void)
558{
559 destroy_workqueue(strp_wq);
560}
561module_init(strp_mod_init);
562module_exit(strp_mod_exit);
563MODULE_LICENSE("GPL");
564