]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/strparser/strparser.c
x86/mm: Add TLB purge to free pmd/pte page interfaces
[mirror_ubuntu-bionic-kernel.git] / net / strparser / strparser.c
CommitLineData
43a0c675
TH
1/*
2 * Stream Parser
3 *
4 * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
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>
43a0c675
TH
29
30static struct workqueue_struct *strp_wq;
31
bbb03029
TH
32struct _strp_msg {
33 /* Internal cb structure. struct strp_msg must be first for passing
43a0c675
TH
34 * to upper layer.
35 */
bbb03029 36 struct strp_msg strp;
43a0c675 37 int accum_len;
43a0c675
TH
38};
39
bbb03029 40static inline struct _strp_msg *_strp_msg(struct sk_buff *skb)
43a0c675 41{
bbb03029 42 return (struct _strp_msg *)((void *)skb->cb +
43a0c675
TH
43 offsetof(struct qdisc_skb_cb, data));
44}
45
46/* Lower lock held */
bbb03029 47static void strp_abort_strp(struct strparser *strp, int err)
43a0c675 48{
43a0c675
TH
49 /* Unrecoverable error in receive */
50
829385f0 51 cancel_delayed_work(&strp->msg_timer_work);
43a0c675 52
bbb03029 53 if (strp->stopped)
43a0c675
TH
54 return;
55
bbb03029
TH
56 strp->stopped = 1;
57
58 if (strp->sk) {
59 struct sock *sk = strp->sk;
43a0c675 60
bbb03029 61 /* Report an error on the lower socket */
b6d5f4bf 62 sk->sk_err = -err;
bbb03029
TH
63 sk->sk_error_report(sk);
64 }
43a0c675
TH
65}
66
bbb03029 67static void strp_start_timer(struct strparser *strp, long timeo)
43a0c675 68{
c1e45f5b 69 if (timeo && timeo != LONG_MAX)
829385f0 70 mod_delayed_work(strp_wq, &strp->msg_timer_work, timeo);
43a0c675
TH
71}
72
73/* Lower lock held */
74static void strp_parser_err(struct strparser *strp, int err,
75 read_descriptor_t *desc)
76{
77 desc->error = err;
bbb03029
TH
78 kfree_skb(strp->skb_head);
79 strp->skb_head = NULL;
43a0c675
TH
80 strp->cb.abort_parser(strp, err);
81}
82
96a59083
TH
83static inline int strp_peek_len(struct strparser *strp)
84{
bbb03029
TH
85 if (strp->sk) {
86 struct socket *sock = strp->sk->sk_socket;
87
88 return sock->ops->peek_len(sock);
89 }
90
91 /* If we don't have an associated socket there's nothing to peek.
92 * Return int max to avoid stopping the strparser.
93 */
96a59083 94
bbb03029 95 return INT_MAX;
96a59083
TH
96}
97
43a0c675 98/* Lower socket lock held */
bbb03029
TH
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)
43a0c675
TH
102{
103 struct strparser *strp = (struct strparser *)desc->arg.data;
bbb03029 104 struct _strp_msg *stm;
43a0c675
TH
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
bbb03029 111 if (strp->paused)
43a0c675
TH
112 return 0;
113
bbb03029 114 head = strp->skb_head;
43a0c675
TH
115 if (head) {
116 /* Message already in progress */
43a0c675
TH
117 if (unlikely(orig_offset)) {
118 /* Getting data with a non-zero offset when a message is
119 * in progress is not expected. If it does happen, we
120 * need to clone and pull since we can't deal with
121 * offsets in the skbs for a message expect in the head.
122 */
123 orig_skb = skb_clone(orig_skb, GFP_ATOMIC);
124 if (!orig_skb) {
bbb03029 125 STRP_STATS_INCR(strp->stats.mem_fail);
43a0c675
TH
126 desc->error = -ENOMEM;
127 return 0;
128 }
129 if (!pskb_pull(orig_skb, orig_offset)) {
bbb03029 130 STRP_STATS_INCR(strp->stats.mem_fail);
43a0c675
TH
131 kfree_skb(orig_skb);
132 desc->error = -ENOMEM;
133 return 0;
134 }
135 cloned_orig = true;
136 orig_offset = 0;
137 }
138
bbb03029 139 if (!strp->skb_nextp) {
43a0c675
TH
140 /* We are going to append to the frags_list of head.
141 * Need to unshare the frag_list.
142 */
143 err = skb_unclone(head, GFP_ATOMIC);
144 if (err) {
bbb03029 145 STRP_STATS_INCR(strp->stats.mem_fail);
43a0c675
TH
146 desc->error = err;
147 return 0;
148 }
149
150 if (unlikely(skb_shinfo(head)->frag_list)) {
151 /* We can't append to an sk_buff that already
152 * has a frag_list. We create a new head, point
153 * the frag_list of that to the old head, and
154 * then are able to use the old head->next for
155 * appending to the message.
156 */
157 if (WARN_ON(head->next)) {
158 desc->error = -EINVAL;
159 return 0;
160 }
161
162 skb = alloc_skb(0, GFP_ATOMIC);
163 if (!skb) {
bbb03029 164 STRP_STATS_INCR(strp->stats.mem_fail);
43a0c675
TH
165 desc->error = -ENOMEM;
166 return 0;
167 }
168 skb->len = head->len;
169 skb->data_len = head->len;
170 skb->truesize = head->truesize;
bbb03029
TH
171 *_strp_msg(skb) = *_strp_msg(head);
172 strp->skb_nextp = &head->next;
43a0c675 173 skb_shinfo(skb)->frag_list = head;
bbb03029 174 strp->skb_head = skb;
43a0c675
TH
175 head = skb;
176 } else {
bbb03029 177 strp->skb_nextp =
43a0c675
TH
178 &skb_shinfo(head)->frag_list;
179 }
180 }
181 }
182
183 while (eaten < orig_len) {
184 /* Always clone since we will consume something */
185 skb = skb_clone(orig_skb, GFP_ATOMIC);
186 if (!skb) {
bbb03029 187 STRP_STATS_INCR(strp->stats.mem_fail);
43a0c675
TH
188 desc->error = -ENOMEM;
189 break;
190 }
191
192 cand_len = orig_len - eaten;
193
bbb03029 194 head = strp->skb_head;
43a0c675
TH
195 if (!head) {
196 head = skb;
bbb03029
TH
197 strp->skb_head = head;
198 /* Will set skb_nextp on next packet if needed */
199 strp->skb_nextp = NULL;
200 stm = _strp_msg(head);
201 memset(stm, 0, sizeof(*stm));
202 stm->strp.offset = orig_offset + eaten;
43a0c675
TH
203 } else {
204 /* Unclone since we may be appending to an skb that we
205 * already share a frag_list with.
206 */
207 err = skb_unclone(skb, GFP_ATOMIC);
208 if (err) {
bbb03029 209 STRP_STATS_INCR(strp->stats.mem_fail);
43a0c675
TH
210 desc->error = err;
211 break;
212 }
213
bbb03029
TH
214 stm = _strp_msg(head);
215 *strp->skb_nextp = skb;
216 strp->skb_nextp = &skb->next;
43a0c675
TH
217 head->data_len += skb->len;
218 head->len += skb->len;
219 head->truesize += skb->truesize;
220 }
221
bbb03029 222 if (!stm->strp.full_len) {
43a0c675
TH
223 ssize_t len;
224
225 len = (*strp->cb.parse_msg)(strp, head);
226
227 if (!len) {
228 /* Need more header to determine length */
bbb03029 229 if (!stm->accum_len) {
43a0c675 230 /* Start RX timer for new message */
bbb03029 231 strp_start_timer(strp, timeo);
43a0c675 232 }
bbb03029 233 stm->accum_len += cand_len;
43a0c675 234 eaten += cand_len;
bbb03029 235 STRP_STATS_INCR(strp->stats.need_more_hdr);
43a0c675
TH
236 WARN_ON(eaten != orig_len);
237 break;
238 } else if (len < 0) {
bbb03029 239 if (len == -ESTRPIPE && stm->accum_len) {
43a0c675 240 len = -ENODATA;
bbb03029 241 strp->unrecov_intr = 1;
43a0c675 242 } else {
bbb03029 243 strp->interrupted = 1;
43a0c675 244 }
6d3a4c40 245 strp_parser_err(strp, len, desc);
43a0c675 246 break;
bbb03029 247 } else if (len > max_msg_size) {
43a0c675 248 /* Message length exceeds maximum allowed */
bbb03029 249 STRP_STATS_INCR(strp->stats.msg_too_big);
43a0c675
TH
250 strp_parser_err(strp, -EMSGSIZE, desc);
251 break;
252 } else if (len <= (ssize_t)head->len -
bbb03029 253 skb->len - stm->strp.offset) {
43a0c675
TH
254 /* Length must be into new skb (and also
255 * greater than zero)
256 */
bbb03029 257 STRP_STATS_INCR(strp->stats.bad_hdr_len);
43a0c675
TH
258 strp_parser_err(strp, -EPROTO, desc);
259 break;
260 }
261
bbb03029 262 stm->strp.full_len = len;
43a0c675
TH
263 }
264
bbb03029
TH
265 extra = (ssize_t)(stm->accum_len + cand_len) -
266 stm->strp.full_len;
43a0c675
TH
267
268 if (extra < 0) {
269 /* Message not complete yet. */
bbb03029 270 if (stm->strp.full_len - stm->accum_len >
96a59083 271 strp_peek_len(strp)) {
bbb03029
TH
272 /* Don't have the whole message in the socket
273 * buffer. Set strp->need_bytes to wait for
43a0c675
TH
274 * the rest of the message. Also, set "early
275 * eaten" since we've already buffered the skb
96a59083 276 * but don't consume yet per strp_read_sock.
43a0c675
TH
277 */
278
bbb03029 279 if (!stm->accum_len) {
43a0c675 280 /* Start RX timer for new message */
bbb03029 281 strp_start_timer(strp, timeo);
43a0c675
TH
282 }
283
ddd9d854 284 stm->accum_len += cand_len;
753020a3 285 eaten += cand_len;
bbb03029
TH
286 strp->need_bytes = stm->strp.full_len -
287 stm->accum_len;
bbb03029 288 STRP_STATS_ADD(strp->stats.bytes, cand_len);
43a0c675
TH
289 desc->count = 0; /* Stop reading socket */
290 break;
291 }
bbb03029 292 stm->accum_len += cand_len;
43a0c675
TH
293 eaten += cand_len;
294 WARN_ON(eaten != orig_len);
295 break;
296 }
297
298 /* Positive extra indicates ore bytes than needed for the
299 * message
300 */
301
302 WARN_ON(extra > cand_len);
303
304 eaten += (cand_len - extra);
305
306 /* Hurray, we have a new message! */
829385f0 307 cancel_delayed_work(&strp->msg_timer_work);
bbb03029 308 strp->skb_head = NULL;
ddd9d854 309 strp->need_bytes = 0;
bbb03029 310 STRP_STATS_INCR(strp->stats.msgs);
43a0c675
TH
311
312 /* Give skb to upper layer */
313 strp->cb.rcv_msg(strp, head);
314
bbb03029 315 if (unlikely(strp->paused)) {
43a0c675
TH
316 /* Upper layer paused strp */
317 break;
318 }
319 }
320
321 if (cloned_orig)
322 kfree_skb(orig_skb);
323
bbb03029 324 STRP_STATS_ADD(strp->stats.bytes, eaten);
43a0c675
TH
325
326 return eaten;
327}
328
bbb03029
TH
329int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
330 unsigned int orig_offset, size_t orig_len,
331 size_t max_msg_size, long timeo)
332{
333 read_descriptor_t desc; /* Dummy arg to strp_recv */
334
335 desc.arg.data = strp;
336
337 return __strp_recv(&desc, orig_skb, orig_offset, orig_len,
338 max_msg_size, timeo);
339}
340EXPORT_SYMBOL_GPL(strp_process);
341
342static int strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
343 unsigned int orig_offset, size_t orig_len)
344{
345 struct strparser *strp = (struct strparser *)desc->arg.data;
346
347 return __strp_recv(desc, orig_skb, orig_offset, orig_len,
348 strp->sk->sk_rcvbuf, strp->sk->sk_rcvtimeo);
349}
350
43a0c675
TH
351static int default_read_sock_done(struct strparser *strp, int err)
352{
353 return err;
354}
355
356/* Called with lock held on lower socket */
96a59083 357static int strp_read_sock(struct strparser *strp)
43a0c675 358{
96a59083 359 struct socket *sock = strp->sk->sk_socket;
43a0c675
TH
360 read_descriptor_t desc;
361
f26de110
JF
362 if (unlikely(!sock || !sock->ops || !sock->ops->read_sock))
363 return -EBUSY;
364
43a0c675
TH
365 desc.arg.data = strp;
366 desc.error = 0;
367 desc.count = 1; /* give more than one skb per call */
368
96a59083
TH
369 /* sk should be locked here, so okay to do read_sock */
370 sock->ops->read_sock(strp->sk, &desc, strp_recv);
43a0c675
TH
371
372 desc.error = strp->cb.read_sock_done(strp, desc.error);
373
374 return desc.error;
375}
376
377/* Lower sock lock held */
96a59083 378void strp_data_ready(struct strparser *strp)
43a0c675 379{
bbb03029 380 if (unlikely(strp->stopped))
43a0c675
TH
381 return;
382
bbb03029
TH
383 /* This check is needed to synchronize with do_strp_work.
384 * do_strp_work acquires a process lock (lock_sock) whereas
43a0c675
TH
385 * the lock held here is bh_lock_sock. The two locks can be
386 * held by different threads at the same time, but bh_lock_sock
387 * allows a thread in BH context to safely check if the process
388 * lock is held. In this case, if the lock is held, queue work.
389 */
d66fa9ec 390 if (sock_owned_by_user_nocheck(strp->sk)) {
bbb03029 391 queue_work(strp_wq, &strp->work);
43a0c675
TH
392 return;
393 }
394
bbb03029 395 if (strp->paused)
43a0c675
TH
396 return;
397
bbb03029 398 if (strp->need_bytes) {
ddd9d854 399 if (strp_peek_len(strp) < strp->need_bytes)
43a0c675
TH
400 return;
401 }
402
96a59083 403 if (strp_read_sock(strp) == -ENOMEM)
bbb03029 404 queue_work(strp_wq, &strp->work);
43a0c675 405}
96a59083 406EXPORT_SYMBOL_GPL(strp_data_ready);
43a0c675 407
bbb03029 408static void do_strp_work(struct strparser *strp)
43a0c675
TH
409{
410 read_descriptor_t rd_desc;
43a0c675 411
96a59083
TH
412 /* We need the read lock to synchronize with strp_data_ready. We
413 * need the socket lock for calling strp_read_sock.
43a0c675 414 */
bbb03029 415 strp->cb.lock(strp);
43a0c675 416
bbb03029 417 if (unlikely(strp->stopped))
43a0c675
TH
418 goto out;
419
bbb03029 420 if (strp->paused)
43a0c675
TH
421 goto out;
422
423 rd_desc.arg.data = strp;
424
96a59083 425 if (strp_read_sock(strp) == -ENOMEM)
bbb03029 426 queue_work(strp_wq, &strp->work);
43a0c675
TH
427
428out:
bbb03029 429 strp->cb.unlock(strp);
43a0c675
TH
430}
431
bbb03029 432static void strp_work(struct work_struct *w)
43a0c675 433{
bbb03029 434 do_strp_work(container_of(w, struct strparser, work));
43a0c675
TH
435}
436
829385f0 437static void strp_msg_timeout(struct work_struct *w)
43a0c675 438{
829385f0
TH
439 struct strparser *strp = container_of(w, struct strparser,
440 msg_timer_work.work);
43a0c675
TH
441
442 /* Message assembly timed out */
bbb03029
TH
443 STRP_STATS_INCR(strp->stats.msg_timeouts);
444 strp->cb.lock(strp);
b6d5f4bf 445 strp->cb.abort_parser(strp, -ETIMEDOUT);
bbb03029
TH
446 strp->cb.unlock(strp);
447}
448
449static void strp_sock_lock(struct strparser *strp)
450{
451 lock_sock(strp->sk);
452}
453
454static void strp_sock_unlock(struct strparser *strp)
455{
43a0c675
TH
456 release_sock(strp->sk);
457}
458
bbb03029 459int strp_init(struct strparser *strp, struct sock *sk,
3fd87127 460 const struct strp_callbacks *cb)
43a0c675 461{
96a59083 462
43a0c675
TH
463 if (!cb || !cb->rcv_msg || !cb->parse_msg)
464 return -EINVAL;
465
bbb03029
TH
466 /* The sk (sock) arg determines the mode of the stream parser.
467 *
468 * If the sock is set then the strparser is in receive callback mode.
469 * The upper layer calls strp_data_ready to kick receive processing
470 * and strparser calls the read_sock function on the socket to
471 * get packets.
472 *
473 * If the sock is not set then the strparser is in general mode.
474 * The upper layer calls strp_process for each skb to be parsed.
475 */
96a59083 476
f26de110 477 if (!sk) {
bbb03029
TH
478 if (!cb->lock || !cb->unlock)
479 return -EINVAL;
480 }
43a0c675 481
bbb03029 482 memset(strp, 0, sizeof(*strp));
43a0c675 483
bbb03029 484 strp->sk = sk;
43a0c675 485
bbb03029
TH
486 strp->cb.lock = cb->lock ? : strp_sock_lock;
487 strp->cb.unlock = cb->unlock ? : strp_sock_unlock;
43a0c675
TH
488 strp->cb.rcv_msg = cb->rcv_msg;
489 strp->cb.parse_msg = cb->parse_msg;
490 strp->cb.read_sock_done = cb->read_sock_done ? : default_read_sock_done;
bbb03029
TH
491 strp->cb.abort_parser = cb->abort_parser ? : strp_abort_strp;
492
829385f0 493 INIT_DELAYED_WORK(&strp->msg_timer_work, strp_msg_timeout);
bbb03029 494 INIT_WORK(&strp->work, strp_work);
43a0c675
TH
495
496 return 0;
497}
498EXPORT_SYMBOL_GPL(strp_init);
499
cff6a334
TH
500void strp_unpause(struct strparser *strp)
501{
bbb03029 502 strp->paused = 0;
cff6a334 503
bbb03029 504 /* Sync setting paused with RX work */
cff6a334
TH
505 smp_mb();
506
bbb03029 507 queue_work(strp_wq, &strp->work);
cff6a334
TH
508}
509EXPORT_SYMBOL_GPL(strp_unpause);
510
96a59083 511/* strp must already be stopped so that strp_recv will no longer be called.
43a0c675
TH
512 * Note that strp_done is not called with the lower socket held.
513 */
514void strp_done(struct strparser *strp)
515{
bbb03029 516 WARN_ON(!strp->stopped);
43a0c675 517
829385f0 518 cancel_delayed_work_sync(&strp->msg_timer_work);
bbb03029 519 cancel_work_sync(&strp->work);
43a0c675 520
bbb03029
TH
521 if (strp->skb_head) {
522 kfree_skb(strp->skb_head);
523 strp->skb_head = NULL;
43a0c675
TH
524 }
525}
526EXPORT_SYMBOL_GPL(strp_done);
527
528void strp_stop(struct strparser *strp)
529{
bbb03029 530 strp->stopped = 1;
43a0c675
TH
531}
532EXPORT_SYMBOL_GPL(strp_stop);
533
534void strp_check_rcv(struct strparser *strp)
535{
bbb03029 536 queue_work(strp_wq, &strp->work);
43a0c675
TH
537}
538EXPORT_SYMBOL_GPL(strp_check_rcv);
539
540static int __init strp_mod_init(void)
541{
542 strp_wq = create_singlethread_workqueue("kstrp");
543
544 return 0;
545}
546
547static void __exit strp_mod_exit(void)
548{
f78ef7cd 549 destroy_workqueue(strp_wq);
43a0c675
TH
550}
551module_init(strp_mod_init);
552module_exit(strp_mod_exit);
553MODULE_LICENSE("GPL");