4 * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
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.
11 #include <linux/bpf.h>
12 #include <linux/errno.h>
13 #include <linux/errqueue.h>
14 #include <linux/file.h>
16 #include <linux/kernel.h>
17 #include <linux/export.h>
18 #include <linux/init.h>
19 #include <linux/net.h>
20 #include <linux/netdevice.h>
21 #include <linux/poll.h>
22 #include <linux/rculist.h>
23 #include <linux/skbuff.h>
24 #include <linux/socket.h>
25 #include <linux/uaccess.h>
26 #include <linux/workqueue.h>
27 #include <net/strparser.h>
28 #include <net/netns/generic.h>
31 static struct workqueue_struct *strp_wq;
34 /* Internal cb structure. struct strp_msg must be first for passing
41 static inline struct _strp_msg *_strp_msg(struct sk_buff *skb)
43 return (struct _strp_msg *)((void *)skb->cb +
44 offsetof(struct qdisc_skb_cb, data));
48 static void strp_abort_strp(struct strparser *strp, int err)
50 /* Unrecoverable error in receive */
52 cancel_delayed_work(&strp->msg_timer_work);
60 struct sock *sk = strp->sk;
62 /* Report an error on the lower socket */
64 sk->sk_error_report(sk);
68 static void strp_start_timer(struct strparser *strp, long timeo)
70 if (timeo && timeo != LONG_MAX)
71 mod_delayed_work(strp_wq, &strp->msg_timer_work, timeo);
75 static void strp_parser_err(struct strparser *strp, int err,
76 read_descriptor_t *desc)
79 kfree_skb(strp->skb_head);
80 strp->skb_head = NULL;
81 strp->cb.abort_parser(strp, err);
84 static inline int strp_peek_len(struct strparser *strp)
87 struct socket *sock = strp->sk->sk_socket;
89 return sock->ops->peek_len(sock);
92 /* If we don't have an associated socket there's nothing to peek.
93 * Return int max to avoid stopping the strparser.
99 /* Lower socket lock held */
100 static int __strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
101 unsigned int orig_offset, size_t orig_len,
102 size_t max_msg_size, long timeo)
104 struct strparser *strp = (struct strparser *)desc->arg.data;
105 struct _strp_msg *stm;
106 struct sk_buff *head, *skb;
107 size_t eaten = 0, cand_len;
110 bool cloned_orig = false;
115 head = strp->skb_head;
117 /* Message already in progress */
118 if (unlikely(orig_offset)) {
119 /* Getting data with a non-zero offset when a message is
120 * in progress is not expected. If it does happen, we
121 * need to clone and pull since we can't deal with
122 * offsets in the skbs for a message expect in the head.
124 orig_skb = skb_clone(orig_skb, GFP_ATOMIC);
126 STRP_STATS_INCR(strp->stats.mem_fail);
127 desc->error = -ENOMEM;
130 if (!pskb_pull(orig_skb, orig_offset)) {
131 STRP_STATS_INCR(strp->stats.mem_fail);
133 desc->error = -ENOMEM;
140 if (!strp->skb_nextp) {
141 /* We are going to append to the frags_list of head.
142 * Need to unshare the frag_list.
144 err = skb_unclone(head, GFP_ATOMIC);
146 STRP_STATS_INCR(strp->stats.mem_fail);
151 if (unlikely(skb_shinfo(head)->frag_list)) {
152 /* We can't append to an sk_buff that already
153 * has a frag_list. We create a new head, point
154 * the frag_list of that to the old head, and
155 * then are able to use the old head->next for
156 * appending to the message.
158 if (WARN_ON(head->next)) {
159 desc->error = -EINVAL;
163 skb = alloc_skb(0, GFP_ATOMIC);
165 STRP_STATS_INCR(strp->stats.mem_fail);
166 desc->error = -ENOMEM;
169 skb->len = head->len;
170 skb->data_len = head->len;
171 skb->truesize = head->truesize;
172 *_strp_msg(skb) = *_strp_msg(head);
173 strp->skb_nextp = &head->next;
174 skb_shinfo(skb)->frag_list = head;
175 strp->skb_head = skb;
179 &skb_shinfo(head)->frag_list;
184 while (eaten < orig_len) {
185 /* Always clone since we will consume something */
186 skb = skb_clone(orig_skb, GFP_ATOMIC);
188 STRP_STATS_INCR(strp->stats.mem_fail);
189 desc->error = -ENOMEM;
193 cand_len = orig_len - eaten;
195 head = strp->skb_head;
198 strp->skb_head = head;
199 /* Will set skb_nextp on next packet if needed */
200 strp->skb_nextp = NULL;
201 stm = _strp_msg(head);
202 memset(stm, 0, sizeof(*stm));
203 stm->strp.offset = orig_offset + eaten;
205 /* Unclone if we are appending to an skb that we
206 * already share a frag_list with.
208 if (skb_has_frag_list(skb)) {
209 err = skb_unclone(skb, GFP_ATOMIC);
211 STRP_STATS_INCR(strp->stats.mem_fail);
217 stm = _strp_msg(head);
218 *strp->skb_nextp = skb;
219 strp->skb_nextp = &skb->next;
220 head->data_len += skb->len;
221 head->len += skb->len;
222 head->truesize += skb->truesize;
225 if (!stm->strp.full_len) {
228 len = (*strp->cb.parse_msg)(strp, head);
231 /* Need more header to determine length */
232 if (!stm->accum_len) {
233 /* Start RX timer for new message */
234 strp_start_timer(strp, timeo);
236 stm->accum_len += cand_len;
238 STRP_STATS_INCR(strp->stats.need_more_hdr);
239 WARN_ON(eaten != orig_len);
241 } else if (len < 0) {
242 if (len == -ESTRPIPE && stm->accum_len) {
244 strp->unrecov_intr = 1;
246 strp->interrupted = 1;
248 strp_parser_err(strp, len, desc);
250 } else if (len > max_msg_size) {
251 /* Message length exceeds maximum allowed */
252 STRP_STATS_INCR(strp->stats.msg_too_big);
253 strp_parser_err(strp, -EMSGSIZE, desc);
255 } else if (len <= (ssize_t)head->len -
256 skb->len - stm->strp.offset) {
257 /* Length must be into new skb (and also
260 STRP_STATS_INCR(strp->stats.bad_hdr_len);
261 strp_parser_err(strp, -EPROTO, desc);
265 stm->strp.full_len = len;
268 extra = (ssize_t)(stm->accum_len + cand_len) -
272 /* Message not complete yet. */
273 if (stm->strp.full_len - stm->accum_len >
274 strp_peek_len(strp)) {
275 /* Don't have the whole message in the socket
276 * buffer. Set strp->need_bytes to wait for
277 * the rest of the message. Also, set "early
278 * eaten" since we've already buffered the skb
279 * but don't consume yet per strp_read_sock.
282 if (!stm->accum_len) {
283 /* Start RX timer for new message */
284 strp_start_timer(strp, timeo);
287 stm->accum_len += cand_len;
289 strp->need_bytes = stm->strp.full_len -
291 STRP_STATS_ADD(strp->stats.bytes, cand_len);
292 desc->count = 0; /* Stop reading socket */
295 stm->accum_len += cand_len;
297 WARN_ON(eaten != orig_len);
301 /* Positive extra indicates more bytes than needed for the
305 WARN_ON(extra > cand_len);
307 eaten += (cand_len - extra);
309 /* Hurray, we have a new message! */
310 cancel_delayed_work(&strp->msg_timer_work);
311 strp->skb_head = NULL;
312 strp->need_bytes = 0;
313 STRP_STATS_INCR(strp->stats.msgs);
315 /* Give skb to upper layer */
316 strp->cb.rcv_msg(strp, head);
318 if (unlikely(strp->paused)) {
319 /* Upper layer paused strp */
327 STRP_STATS_ADD(strp->stats.bytes, eaten);
332 int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
333 unsigned int orig_offset, size_t orig_len,
334 size_t max_msg_size, long timeo)
336 read_descriptor_t desc; /* Dummy arg to strp_recv */
338 desc.arg.data = strp;
340 return __strp_recv(&desc, orig_skb, orig_offset, orig_len,
341 max_msg_size, timeo);
343 EXPORT_SYMBOL_GPL(strp_process);
345 static int strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
346 unsigned int orig_offset, size_t orig_len)
348 struct strparser *strp = (struct strparser *)desc->arg.data;
350 return __strp_recv(desc, orig_skb, orig_offset, orig_len,
351 strp->sk->sk_rcvbuf, strp->sk->sk_rcvtimeo);
354 static int default_read_sock_done(struct strparser *strp, int err)
359 /* Called with lock held on lower socket */
360 static int strp_read_sock(struct strparser *strp)
362 struct socket *sock = strp->sk->sk_socket;
363 read_descriptor_t desc;
365 if (unlikely(!sock || !sock->ops || !sock->ops->read_sock))
368 desc.arg.data = strp;
370 desc.count = 1; /* give more than one skb per call */
372 /* sk should be locked here, so okay to do read_sock */
373 sock->ops->read_sock(strp->sk, &desc, strp_recv);
375 desc.error = strp->cb.read_sock_done(strp, desc.error);
380 /* Lower sock lock held */
381 void strp_data_ready(struct strparser *strp)
383 if (unlikely(strp->stopped) || strp->paused)
386 /* This check is needed to synchronize with do_strp_work.
387 * do_strp_work acquires a process lock (lock_sock) whereas
388 * the lock held here is bh_lock_sock. The two locks can be
389 * held by different threads at the same time, but bh_lock_sock
390 * allows a thread in BH context to safely check if the process
391 * lock is held. In this case, if the lock is held, queue work.
393 if (sock_owned_by_user_nocheck(strp->sk)) {
394 queue_work(strp_wq, &strp->work);
398 if (strp->need_bytes) {
399 if (strp_peek_len(strp) < strp->need_bytes)
403 if (strp_read_sock(strp) == -ENOMEM)
404 queue_work(strp_wq, &strp->work);
406 EXPORT_SYMBOL_GPL(strp_data_ready);
408 static void do_strp_work(struct strparser *strp)
410 /* We need the read lock to synchronize with strp_data_ready. We
411 * need the socket lock for calling strp_read_sock.
415 if (unlikely(strp->stopped))
421 if (strp_read_sock(strp) == -ENOMEM)
422 queue_work(strp_wq, &strp->work);
425 strp->cb.unlock(strp);
428 static void strp_work(struct work_struct *w)
430 do_strp_work(container_of(w, struct strparser, work));
433 static void strp_msg_timeout(struct work_struct *w)
435 struct strparser *strp = container_of(w, struct strparser,
436 msg_timer_work.work);
438 /* Message assembly timed out */
439 STRP_STATS_INCR(strp->stats.msg_timeouts);
441 strp->cb.abort_parser(strp, -ETIMEDOUT);
442 strp->cb.unlock(strp);
445 static void strp_sock_lock(struct strparser *strp)
450 static void strp_sock_unlock(struct strparser *strp)
452 release_sock(strp->sk);
455 int strp_init(struct strparser *strp, struct sock *sk,
456 const struct strp_callbacks *cb)
459 if (!cb || !cb->rcv_msg || !cb->parse_msg)
462 /* The sk (sock) arg determines the mode of the stream parser.
464 * If the sock is set then the strparser is in receive callback mode.
465 * The upper layer calls strp_data_ready to kick receive processing
466 * and strparser calls the read_sock function on the socket to
469 * If the sock is not set then the strparser is in general mode.
470 * The upper layer calls strp_process for each skb to be parsed.
474 if (!cb->lock || !cb->unlock)
478 memset(strp, 0, sizeof(*strp));
482 strp->cb.lock = cb->lock ? : strp_sock_lock;
483 strp->cb.unlock = cb->unlock ? : strp_sock_unlock;
484 strp->cb.rcv_msg = cb->rcv_msg;
485 strp->cb.parse_msg = cb->parse_msg;
486 strp->cb.read_sock_done = cb->read_sock_done ? : default_read_sock_done;
487 strp->cb.abort_parser = cb->abort_parser ? : strp_abort_strp;
489 INIT_DELAYED_WORK(&strp->msg_timer_work, strp_msg_timeout);
490 INIT_WORK(&strp->work, strp_work);
494 EXPORT_SYMBOL_GPL(strp_init);
496 /* Sock process lock held (lock_sock) */
497 void __strp_unpause(struct strparser *strp)
501 if (strp->need_bytes) {
502 if (strp_peek_len(strp) < strp->need_bytes)
505 strp_read_sock(strp);
507 EXPORT_SYMBOL_GPL(__strp_unpause);
509 void strp_unpause(struct strparser *strp)
513 /* Sync setting paused with RX work */
516 queue_work(strp_wq, &strp->work);
518 EXPORT_SYMBOL_GPL(strp_unpause);
520 /* strp must already be stopped so that strp_recv will no longer be called.
521 * Note that strp_done is not called with the lower socket held.
523 void strp_done(struct strparser *strp)
525 WARN_ON(!strp->stopped);
527 cancel_delayed_work_sync(&strp->msg_timer_work);
528 cancel_work_sync(&strp->work);
530 if (strp->skb_head) {
531 kfree_skb(strp->skb_head);
532 strp->skb_head = NULL;
535 EXPORT_SYMBOL_GPL(strp_done);
537 void strp_stop(struct strparser *strp)
541 EXPORT_SYMBOL_GPL(strp_stop);
543 void strp_check_rcv(struct strparser *strp)
545 queue_work(strp_wq, &strp->work);
547 EXPORT_SYMBOL_GPL(strp_check_rcv);
549 static int __init strp_dev_init(void)
551 strp_wq = create_singlethread_workqueue("kstrp");
552 if (unlikely(!strp_wq))
557 device_initcall(strp_dev_init);