blob: bbee334ab1b0c1fd5f18587506b5500bc9432590 [file] [log] [blame]
Tom Herbert43a0c672016-08-15 14:51:01 -07001/*
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>
Tom Herbert43a0c672016-08-15 14:51:01 -070029
30static struct workqueue_struct *strp_wq;
31
32struct _strp_rx_msg {
33 /* Internal cb structure. struct strp_rx_msg must be first for passing
34 * to upper layer.
35 */
36 struct strp_rx_msg strp;
37 int accum_len;
38 int early_eaten;
39};
40
41static inline struct _strp_rx_msg *_strp_rx_msg(struct sk_buff *skb)
42{
43 return (struct _strp_rx_msg *)((void *)skb->cb +
44 offsetof(struct qdisc_skb_cb, data));
45}
46
47/* Lower lock held */
48static void strp_abort_rx_strp(struct strparser *strp, int err)
49{
50 struct sock *csk = strp->sk;
51
52 /* Unrecoverable error in receive */
53
54 del_timer(&strp->rx_msg_timer);
55
56 if (strp->rx_stopped)
57 return;
58
59 strp->rx_stopped = 1;
60
61 /* Report an error on the lower socket */
Dave Watson32a2fd92018-03-26 12:31:21 -070062 csk->sk_err = -err;
Tom Herbert43a0c672016-08-15 14:51:01 -070063 csk->sk_error_report(csk);
64}
65
66static void strp_start_rx_timer(struct strparser *strp)
67{
68 if (strp->sk->sk_rcvtimeo)
69 mod_timer(&strp->rx_msg_timer, strp->sk->sk_rcvtimeo);
70}
71
72/* Lower lock held */
73static void strp_parser_err(struct strparser *strp, int err,
74 read_descriptor_t *desc)
75{
76 desc->error = err;
77 kfree_skb(strp->rx_skb_head);
78 strp->rx_skb_head = NULL;
79 strp->cb.abort_parser(strp, err);
80}
81
Tom Herbert96a59082016-08-28 14:43:19 -070082static inline int strp_peek_len(struct strparser *strp)
83{
84 struct socket *sock = strp->sk->sk_socket;
85
86 return sock->ops->peek_len(sock);
87}
88
Tom Herbert43a0c672016-08-15 14:51:01 -070089/* Lower socket lock held */
Tom Herbert96a59082016-08-28 14:43:19 -070090static int strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
91 unsigned int orig_offset, size_t orig_len)
Tom Herbert43a0c672016-08-15 14:51:01 -070092{
93 struct strparser *strp = (struct strparser *)desc->arg.data;
94 struct _strp_rx_msg *rxm;
95 struct sk_buff *head, *skb;
96 size_t eaten = 0, cand_len;
97 ssize_t extra;
98 int err;
99 bool cloned_orig = false;
100
101 if (strp->rx_paused)
102 return 0;
103
104 head = strp->rx_skb_head;
105 if (head) {
106 /* Message already in progress */
107
108 rxm = _strp_rx_msg(head);
109 if (unlikely(rxm->early_eaten)) {
110 /* Already some number of bytes on the receive sock
111 * data saved in rx_skb_head, just indicate they
112 * are consumed.
113 */
114 eaten = orig_len <= rxm->early_eaten ?
115 orig_len : rxm->early_eaten;
116 rxm->early_eaten -= eaten;
117
118 return eaten;
119 }
120
121 if (unlikely(orig_offset)) {
122 /* Getting data with a non-zero offset when a message is
123 * in progress is not expected. If it does happen, we
124 * need to clone and pull since we can't deal with
125 * offsets in the skbs for a message expect in the head.
126 */
127 orig_skb = skb_clone(orig_skb, GFP_ATOMIC);
128 if (!orig_skb) {
129 STRP_STATS_INCR(strp->stats.rx_mem_fail);
130 desc->error = -ENOMEM;
131 return 0;
132 }
133 if (!pskb_pull(orig_skb, orig_offset)) {
134 STRP_STATS_INCR(strp->stats.rx_mem_fail);
135 kfree_skb(orig_skb);
136 desc->error = -ENOMEM;
137 return 0;
138 }
139 cloned_orig = true;
140 orig_offset = 0;
141 }
142
143 if (!strp->rx_skb_nextp) {
144 /* We are going to append to the frags_list of head.
145 * Need to unshare the frag_list.
146 */
147 err = skb_unclone(head, GFP_ATOMIC);
148 if (err) {
149 STRP_STATS_INCR(strp->stats.rx_mem_fail);
150 desc->error = err;
151 return 0;
152 }
153
154 if (unlikely(skb_shinfo(head)->frag_list)) {
155 /* We can't append to an sk_buff that already
156 * has a frag_list. We create a new head, point
157 * the frag_list of that to the old head, and
158 * then are able to use the old head->next for
159 * appending to the message.
160 */
161 if (WARN_ON(head->next)) {
162 desc->error = -EINVAL;
163 return 0;
164 }
165
166 skb = alloc_skb(0, GFP_ATOMIC);
167 if (!skb) {
168 STRP_STATS_INCR(strp->stats.rx_mem_fail);
169 desc->error = -ENOMEM;
170 return 0;
171 }
172 skb->len = head->len;
173 skb->data_len = head->len;
174 skb->truesize = head->truesize;
175 *_strp_rx_msg(skb) = *_strp_rx_msg(head);
176 strp->rx_skb_nextp = &head->next;
177 skb_shinfo(skb)->frag_list = head;
178 strp->rx_skb_head = skb;
179 head = skb;
180 } else {
181 strp->rx_skb_nextp =
182 &skb_shinfo(head)->frag_list;
183 }
184 }
185 }
186
187 while (eaten < orig_len) {
188 /* Always clone since we will consume something */
189 skb = skb_clone(orig_skb, GFP_ATOMIC);
190 if (!skb) {
191 STRP_STATS_INCR(strp->stats.rx_mem_fail);
192 desc->error = -ENOMEM;
193 break;
194 }
195
196 cand_len = orig_len - eaten;
197
198 head = strp->rx_skb_head;
199 if (!head) {
200 head = skb;
201 strp->rx_skb_head = head;
202 /* Will set rx_skb_nextp on next packet if needed */
203 strp->rx_skb_nextp = NULL;
204 rxm = _strp_rx_msg(head);
205 memset(rxm, 0, sizeof(*rxm));
206 rxm->strp.offset = orig_offset + eaten;
207 } else {
208 /* Unclone since we may be appending to an skb that we
209 * already share a frag_list with.
210 */
211 err = skb_unclone(skb, GFP_ATOMIC);
212 if (err) {
213 STRP_STATS_INCR(strp->stats.rx_mem_fail);
214 desc->error = err;
215 break;
216 }
217
218 rxm = _strp_rx_msg(head);
219 *strp->rx_skb_nextp = skb;
220 strp->rx_skb_nextp = &skb->next;
221 head->data_len += skb->len;
222 head->len += skb->len;
223 head->truesize += skb->truesize;
224 }
225
226 if (!rxm->strp.full_len) {
227 ssize_t len;
228
229 len = (*strp->cb.parse_msg)(strp, head);
230
231 if (!len) {
232 /* Need more header to determine length */
233 if (!rxm->accum_len) {
234 /* Start RX timer for new message */
235 strp_start_rx_timer(strp);
236 }
237 rxm->accum_len += cand_len;
238 eaten += cand_len;
239 STRP_STATS_INCR(strp->stats.rx_need_more_hdr);
240 WARN_ON(eaten != orig_len);
241 break;
242 } else if (len < 0) {
243 if (len == -ESTRPIPE && rxm->accum_len) {
244 len = -ENODATA;
245 strp->rx_unrecov_intr = 1;
246 } else {
247 strp->rx_interrupted = 1;
248 }
Geert Uytterhoeven6d3a4c42016-10-06 15:41:49 +0200249 strp_parser_err(strp, len, desc);
Tom Herbert43a0c672016-08-15 14:51:01 -0700250 break;
251 } else if (len > strp->sk->sk_rcvbuf) {
252 /* Message length exceeds maximum allowed */
253 STRP_STATS_INCR(strp->stats.rx_msg_too_big);
254 strp_parser_err(strp, -EMSGSIZE, desc);
255 break;
256 } else if (len <= (ssize_t)head->len -
257 skb->len - rxm->strp.offset) {
258 /* Length must be into new skb (and also
259 * greater than zero)
260 */
261 STRP_STATS_INCR(strp->stats.rx_bad_hdr_len);
262 strp_parser_err(strp, -EPROTO, desc);
263 break;
264 }
265
266 rxm->strp.full_len = len;
267 }
268
269 extra = (ssize_t)(rxm->accum_len + cand_len) -
270 rxm->strp.full_len;
271
272 if (extra < 0) {
273 /* Message not complete yet. */
274 if (rxm->strp.full_len - rxm->accum_len >
Tom Herbert96a59082016-08-28 14:43:19 -0700275 strp_peek_len(strp)) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700276 /* Don't have the whole messages in the socket
277 * buffer. Set strp->rx_need_bytes to wait for
278 * the rest of the message. Also, set "early
279 * eaten" since we've already buffered the skb
Tom Herbert96a59082016-08-28 14:43:19 -0700280 * but don't consume yet per strp_read_sock.
Tom Herbert43a0c672016-08-15 14:51:01 -0700281 */
282
283 if (!rxm->accum_len) {
284 /* Start RX timer for new message */
285 strp_start_rx_timer(strp);
286 }
287
Doron Roberts-Kedes2f7be122018-04-11 15:05:16 -0700288 rxm->accum_len += cand_len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700289 strp->rx_need_bytes = rxm->strp.full_len -
290 rxm->accum_len;
Tom Herbert43a0c672016-08-15 14:51:01 -0700291 rxm->early_eaten = cand_len;
292 STRP_STATS_ADD(strp->stats.rx_bytes, cand_len);
293 desc->count = 0; /* Stop reading socket */
294 break;
295 }
296 rxm->accum_len += cand_len;
297 eaten += cand_len;
298 WARN_ON(eaten != orig_len);
299 break;
300 }
301
302 /* Positive extra indicates ore bytes than needed for the
303 * message
304 */
305
306 WARN_ON(extra > cand_len);
307
308 eaten += (cand_len - extra);
309
310 /* Hurray, we have a new message! */
311 del_timer(&strp->rx_msg_timer);
312 strp->rx_skb_head = NULL;
Doron Roberts-Kedes2f7be122018-04-11 15:05:16 -0700313 strp->rx_need_bytes = 0;
Tom Herbert43a0c672016-08-15 14:51:01 -0700314 STRP_STATS_INCR(strp->stats.rx_msgs);
315
316 /* Give skb to upper layer */
317 strp->cb.rcv_msg(strp, head);
318
319 if (unlikely(strp->rx_paused)) {
320 /* Upper layer paused strp */
321 break;
322 }
323 }
324
325 if (cloned_orig)
326 kfree_skb(orig_skb);
327
328 STRP_STATS_ADD(strp->stats.rx_bytes, eaten);
329
330 return eaten;
331}
332
333static int default_read_sock_done(struct strparser *strp, int err)
334{
335 return err;
336}
337
338/* Called with lock held on lower socket */
Tom Herbert96a59082016-08-28 14:43:19 -0700339static int strp_read_sock(struct strparser *strp)
Tom Herbert43a0c672016-08-15 14:51:01 -0700340{
Tom Herbert96a59082016-08-28 14:43:19 -0700341 struct socket *sock = strp->sk->sk_socket;
Tom Herbert43a0c672016-08-15 14:51:01 -0700342 read_descriptor_t desc;
343
344 desc.arg.data = strp;
345 desc.error = 0;
346 desc.count = 1; /* give more than one skb per call */
347
Tom Herbert96a59082016-08-28 14:43:19 -0700348 /* sk should be locked here, so okay to do read_sock */
349 sock->ops->read_sock(strp->sk, &desc, strp_recv);
Tom Herbert43a0c672016-08-15 14:51:01 -0700350
351 desc.error = strp->cb.read_sock_done(strp, desc.error);
352
353 return desc.error;
354}
355
356/* Lower sock lock held */
Tom Herbert96a59082016-08-28 14:43:19 -0700357void strp_data_ready(struct strparser *strp)
Tom Herbert43a0c672016-08-15 14:51:01 -0700358{
Tom Herbert43a0c672016-08-15 14:51:01 -0700359 if (unlikely(strp->rx_stopped))
360 return;
361
362 /* This check is needed to synchronize with do_strp_rx_work.
363 * do_strp_rx_work acquires a process lock (lock_sock) whereas
364 * the lock held here is bh_lock_sock. The two locks can be
365 * held by different threads at the same time, but bh_lock_sock
366 * allows a thread in BH context to safely check if the process
367 * lock is held. In this case, if the lock is held, queue work.
368 */
Tom Herbert96a59082016-08-28 14:43:19 -0700369 if (sock_owned_by_user(strp->sk)) {
Tom Herbert43a0c672016-08-15 14:51:01 -0700370 queue_work(strp_wq, &strp->rx_work);
371 return;
372 }
373
374 if (strp->rx_paused)
375 return;
376
377 if (strp->rx_need_bytes) {
Doron Roberts-Kedes2f7be122018-04-11 15:05:16 -0700378 if (strp_peek_len(strp) < strp->rx_need_bytes)
Tom Herbert43a0c672016-08-15 14:51:01 -0700379 return;
380 }
381
Tom Herbert96a59082016-08-28 14:43:19 -0700382 if (strp_read_sock(strp) == -ENOMEM)
Tom Herbert43a0c672016-08-15 14:51:01 -0700383 queue_work(strp_wq, &strp->rx_work);
384}
Tom Herbert96a59082016-08-28 14:43:19 -0700385EXPORT_SYMBOL_GPL(strp_data_ready);
Tom Herbert43a0c672016-08-15 14:51:01 -0700386
387static void do_strp_rx_work(struct strparser *strp)
388{
389 read_descriptor_t rd_desc;
390 struct sock *csk = strp->sk;
391
Tom Herbert96a59082016-08-28 14:43:19 -0700392 /* We need the read lock to synchronize with strp_data_ready. We
393 * need the socket lock for calling strp_read_sock.
Tom Herbert43a0c672016-08-15 14:51:01 -0700394 */
395 lock_sock(csk);
396
Tom Herbert43a0c672016-08-15 14:51:01 -0700397 if (unlikely(strp->rx_stopped))
398 goto out;
399
400 if (strp->rx_paused)
401 goto out;
402
403 rd_desc.arg.data = strp;
404
Tom Herbert96a59082016-08-28 14:43:19 -0700405 if (strp_read_sock(strp) == -ENOMEM)
Tom Herbert43a0c672016-08-15 14:51:01 -0700406 queue_work(strp_wq, &strp->rx_work);
407
408out:
409 release_sock(csk);
410}
411
412static void strp_rx_work(struct work_struct *w)
413{
414 do_strp_rx_work(container_of(w, struct strparser, rx_work));
415}
416
417static void strp_rx_msg_timeout(unsigned long arg)
418{
419 struct strparser *strp = (struct strparser *)arg;
420
421 /* Message assembly timed out */
422 STRP_STATS_INCR(strp->stats.rx_msg_timeouts);
423 lock_sock(strp->sk);
Dave Watson32a2fd92018-03-26 12:31:21 -0700424 strp->cb.abort_parser(strp, -ETIMEDOUT);
Tom Herbert43a0c672016-08-15 14:51:01 -0700425 release_sock(strp->sk);
426}
427
428int strp_init(struct strparser *strp, struct sock *csk,
429 struct strp_callbacks *cb)
430{
Tom Herbert96a59082016-08-28 14:43:19 -0700431 struct socket *sock = csk->sk_socket;
432
Tom Herbert43a0c672016-08-15 14:51:01 -0700433 if (!cb || !cb->rcv_msg || !cb->parse_msg)
434 return -EINVAL;
435
Tom Herbert96a59082016-08-28 14:43:19 -0700436 if (!sock->ops->read_sock || !sock->ops->peek_len)
437 return -EAFNOSUPPORT;
438
Tom Herbert43a0c672016-08-15 14:51:01 -0700439 memset(strp, 0, sizeof(*strp));
440
441 strp->sk = csk;
442
443 setup_timer(&strp->rx_msg_timer, strp_rx_msg_timeout,
444 (unsigned long)strp);
445
446 INIT_WORK(&strp->rx_work, strp_rx_work);
447
448 strp->cb.rcv_msg = cb->rcv_msg;
449 strp->cb.parse_msg = cb->parse_msg;
450 strp->cb.read_sock_done = cb->read_sock_done ? : default_read_sock_done;
451 strp->cb.abort_parser = cb->abort_parser ? : strp_abort_rx_strp;
452
453 return 0;
454}
455EXPORT_SYMBOL_GPL(strp_init);
456
Tom Herbertcff6a332016-08-23 11:55:30 -0700457void strp_unpause(struct strparser *strp)
458{
459 strp->rx_paused = 0;
460
461 /* Sync setting rx_paused with RX work */
462 smp_mb();
463
464 queue_work(strp_wq, &strp->rx_work);
465}
466EXPORT_SYMBOL_GPL(strp_unpause);
467
Tom Herbert96a59082016-08-28 14:43:19 -0700468/* strp must already be stopped so that strp_recv will no longer be called.
Tom Herbert43a0c672016-08-15 14:51:01 -0700469 * Note that strp_done is not called with the lower socket held.
470 */
471void strp_done(struct strparser *strp)
472{
473 WARN_ON(!strp->rx_stopped);
474
475 del_timer_sync(&strp->rx_msg_timer);
476 cancel_work_sync(&strp->rx_work);
477
478 if (strp->rx_skb_head) {
479 kfree_skb(strp->rx_skb_head);
480 strp->rx_skb_head = NULL;
481 }
482}
483EXPORT_SYMBOL_GPL(strp_done);
484
485void strp_stop(struct strparser *strp)
486{
487 strp->rx_stopped = 1;
488}
489EXPORT_SYMBOL_GPL(strp_stop);
490
491void strp_check_rcv(struct strparser *strp)
492{
493 queue_work(strp_wq, &strp->rx_work);
494}
495EXPORT_SYMBOL_GPL(strp_check_rcv);
496
497static int __init strp_mod_init(void)
498{
499 strp_wq = create_singlethread_workqueue("kstrp");
500
501 return 0;
502}
503
504static void __exit strp_mod_exit(void)
505{
WANG Cong4547f032017-03-03 12:21:14 -0800506 destroy_workqueue(strp_wq);
Tom Herbert43a0c672016-08-15 14:51:01 -0700507}
508module_init(strp_mod_init);
509module_exit(strp_mod_exit);
510MODULE_LICENSE("GPL");