strparser.c 13.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * Stream Parser
 *
 * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 */

#include <linux/bpf.h>
#include <linux/errno.h>
#include <linux/errqueue.h>
#include <linux/file.h>
#include <linux/in.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/net.h>
#include <linux/netdevice.h>
#include <linux/poll.h>
#include <linux/rculist.h>
#include <linux/skbuff.h>
#include <linux/socket.h>
#include <linux/uaccess.h>
#include <linux/workqueue.h>
#include <net/strparser.h>
#include <net/netns/generic.h>
#include <net/sock.h>

static struct workqueue_struct *strp_wq;

T
Tom Herbert 已提交
32 33
struct _strp_msg {
	/* Internal cb structure. struct strp_msg must be first for passing
34 35
	 * to upper layer.
	 */
T
Tom Herbert 已提交
36
	struct strp_msg strp;
37 38 39 40
	int accum_len;
	int early_eaten;
};

T
Tom Herbert 已提交
41
static inline struct _strp_msg *_strp_msg(struct sk_buff *skb)
42
{
T
Tom Herbert 已提交
43
	return (struct _strp_msg *)((void *)skb->cb +
44 45 46 47
		offsetof(struct qdisc_skb_cb, data));
}

/* Lower lock held */
T
Tom Herbert 已提交
48
static void strp_abort_strp(struct strparser *strp, int err)
49 50 51
{
	/* Unrecoverable error in receive */

52
	cancel_delayed_work(&strp->msg_timer_work);
53

T
Tom Herbert 已提交
54
	if (strp->stopped)
55 56
		return;

T
Tom Herbert 已提交
57 58 59 60
	strp->stopped = 1;

	if (strp->sk) {
		struct sock *sk = strp->sk;
61

T
Tom Herbert 已提交
62 63 64 65
		/* Report an error on the lower socket */
		sk->sk_err = err;
		sk->sk_error_report(sk);
	}
66 67
}

T
Tom Herbert 已提交
68
static void strp_start_timer(struct strparser *strp, long timeo)
69
{
T
Tom Herbert 已提交
70
	if (timeo)
71
		mod_delayed_work(strp_wq, &strp->msg_timer_work, timeo);
72 73 74 75 76 77 78
}

/* Lower lock held */
static void strp_parser_err(struct strparser *strp, int err,
			    read_descriptor_t *desc)
{
	desc->error = err;
T
Tom Herbert 已提交
79 80
	kfree_skb(strp->skb_head);
	strp->skb_head = NULL;
81 82 83
	strp->cb.abort_parser(strp, err);
}

84 85
static inline int strp_peek_len(struct strparser *strp)
{
T
Tom Herbert 已提交
86 87 88 89 90 91 92 93 94
	if (strp->sk) {
		struct socket *sock = strp->sk->sk_socket;

		return sock->ops->peek_len(sock);
	}

	/* If we don't have an associated socket there's nothing to peek.
	 * Return int max to avoid stopping the strparser.
	 */
95

T
Tom Herbert 已提交
96
	return INT_MAX;
97 98
}

99
/* Lower socket lock held */
T
Tom Herbert 已提交
100 101 102
static int __strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
		       unsigned int orig_offset, size_t orig_len,
		       size_t max_msg_size, long timeo)
103 104
{
	struct strparser *strp = (struct strparser *)desc->arg.data;
T
Tom Herbert 已提交
105
	struct _strp_msg *stm;
106 107 108 109 110 111
	struct sk_buff *head, *skb;
	size_t eaten = 0, cand_len;
	ssize_t extra;
	int err;
	bool cloned_orig = false;

T
Tom Herbert 已提交
112
	if (strp->paused)
113 114
		return 0;

T
Tom Herbert 已提交
115
	head = strp->skb_head;
116 117 118
	if (head) {
		/* Message already in progress */

T
Tom Herbert 已提交
119 120
		stm = _strp_msg(head);
		if (unlikely(stm->early_eaten)) {
121
			/* Already some number of bytes on the receive sock
T
Tom Herbert 已提交
122
			 * data saved in skb_head, just indicate they
123 124
			 * are consumed.
			 */
T
Tom Herbert 已提交
125 126 127
			eaten = orig_len <= stm->early_eaten ?
				orig_len : stm->early_eaten;
			stm->early_eaten -= eaten;
128 129 130 131 132 133 134 135 136 137 138 139

			return eaten;
		}

		if (unlikely(orig_offset)) {
			/* Getting data with a non-zero offset when a message is
			 * in progress is not expected. If it does happen, we
			 * need to clone and pull since we can't deal with
			 * offsets in the skbs for a message expect in the head.
			 */
			orig_skb = skb_clone(orig_skb, GFP_ATOMIC);
			if (!orig_skb) {
T
Tom Herbert 已提交
140
				STRP_STATS_INCR(strp->stats.mem_fail);
141 142 143 144
				desc->error = -ENOMEM;
				return 0;
			}
			if (!pskb_pull(orig_skb, orig_offset)) {
T
Tom Herbert 已提交
145
				STRP_STATS_INCR(strp->stats.mem_fail);
146 147 148 149 150 151 152 153
				kfree_skb(orig_skb);
				desc->error = -ENOMEM;
				return 0;
			}
			cloned_orig = true;
			orig_offset = 0;
		}

T
Tom Herbert 已提交
154
		if (!strp->skb_nextp) {
155 156 157 158 159
			/* We are going to append to the frags_list of head.
			 * Need to unshare the frag_list.
			 */
			err = skb_unclone(head, GFP_ATOMIC);
			if (err) {
T
Tom Herbert 已提交
160
				STRP_STATS_INCR(strp->stats.mem_fail);
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
				desc->error = err;
				return 0;
			}

			if (unlikely(skb_shinfo(head)->frag_list)) {
				/* We can't append to an sk_buff that already
				 * has a frag_list. We create a new head, point
				 * the frag_list of that to the old head, and
				 * then are able to use the old head->next for
				 * appending to the message.
				 */
				if (WARN_ON(head->next)) {
					desc->error = -EINVAL;
					return 0;
				}

				skb = alloc_skb(0, GFP_ATOMIC);
				if (!skb) {
T
Tom Herbert 已提交
179
					STRP_STATS_INCR(strp->stats.mem_fail);
180 181 182 183 184 185
					desc->error = -ENOMEM;
					return 0;
				}
				skb->len = head->len;
				skb->data_len = head->len;
				skb->truesize = head->truesize;
T
Tom Herbert 已提交
186 187
				*_strp_msg(skb) = *_strp_msg(head);
				strp->skb_nextp = &head->next;
188
				skb_shinfo(skb)->frag_list = head;
T
Tom Herbert 已提交
189
				strp->skb_head = skb;
190 191
				head = skb;
			} else {
T
Tom Herbert 已提交
192
				strp->skb_nextp =
193 194 195 196 197 198 199 200 201
				    &skb_shinfo(head)->frag_list;
			}
		}
	}

	while (eaten < orig_len) {
		/* Always clone since we will consume something */
		skb = skb_clone(orig_skb, GFP_ATOMIC);
		if (!skb) {
T
Tom Herbert 已提交
202
			STRP_STATS_INCR(strp->stats.mem_fail);
203 204 205 206 207 208
			desc->error = -ENOMEM;
			break;
		}

		cand_len = orig_len - eaten;

T
Tom Herbert 已提交
209
		head = strp->skb_head;
210 211
		if (!head) {
			head = skb;
T
Tom Herbert 已提交
212 213 214 215 216 217
			strp->skb_head = head;
			/* Will set skb_nextp on next packet if needed */
			strp->skb_nextp = NULL;
			stm = _strp_msg(head);
			memset(stm, 0, sizeof(*stm));
			stm->strp.offset = orig_offset + eaten;
218 219 220 221 222 223
		} else {
			/* Unclone since we may be appending to an skb that we
			 * already share a frag_list with.
			 */
			err = skb_unclone(skb, GFP_ATOMIC);
			if (err) {
T
Tom Herbert 已提交
224
				STRP_STATS_INCR(strp->stats.mem_fail);
225 226 227 228
				desc->error = err;
				break;
			}

T
Tom Herbert 已提交
229 230 231
			stm = _strp_msg(head);
			*strp->skb_nextp = skb;
			strp->skb_nextp = &skb->next;
232 233 234 235 236
			head->data_len += skb->len;
			head->len += skb->len;
			head->truesize += skb->truesize;
		}

T
Tom Herbert 已提交
237
		if (!stm->strp.full_len) {
238 239 240 241 242 243
			ssize_t len;

			len = (*strp->cb.parse_msg)(strp, head);

			if (!len) {
				/* Need more header to determine length */
T
Tom Herbert 已提交
244
				if (!stm->accum_len) {
245
					/* Start RX timer for new message */
T
Tom Herbert 已提交
246
					strp_start_timer(strp, timeo);
247
				}
T
Tom Herbert 已提交
248
				stm->accum_len += cand_len;
249
				eaten += cand_len;
T
Tom Herbert 已提交
250
				STRP_STATS_INCR(strp->stats.need_more_hdr);
251 252 253
				WARN_ON(eaten != orig_len);
				break;
			} else if (len < 0) {
T
Tom Herbert 已提交
254
				if (len == -ESTRPIPE && stm->accum_len) {
255
					len = -ENODATA;
T
Tom Herbert 已提交
256
					strp->unrecov_intr = 1;
257
				} else {
T
Tom Herbert 已提交
258
					strp->interrupted = 1;
259
				}
260
				strp_parser_err(strp, len, desc);
261
				break;
T
Tom Herbert 已提交
262
			} else if (len > max_msg_size) {
263
				/* Message length exceeds maximum allowed */
T
Tom Herbert 已提交
264
				STRP_STATS_INCR(strp->stats.msg_too_big);
265 266 267
				strp_parser_err(strp, -EMSGSIZE, desc);
				break;
			} else if (len <= (ssize_t)head->len -
T
Tom Herbert 已提交
268
					  skb->len - stm->strp.offset) {
269 270 271
				/* Length must be into new skb (and also
				 * greater than zero)
				 */
T
Tom Herbert 已提交
272
				STRP_STATS_INCR(strp->stats.bad_hdr_len);
273 274 275 276
				strp_parser_err(strp, -EPROTO, desc);
				break;
			}

T
Tom Herbert 已提交
277
			stm->strp.full_len = len;
278 279
		}

T
Tom Herbert 已提交
280 281
		extra = (ssize_t)(stm->accum_len + cand_len) -
			stm->strp.full_len;
282 283 284

		if (extra < 0) {
			/* Message not complete yet. */
T
Tom Herbert 已提交
285
			if (stm->strp.full_len - stm->accum_len >
286
			    strp_peek_len(strp)) {
T
Tom Herbert 已提交
287 288
				/* Don't have the whole message in the socket
				 * buffer. Set strp->need_bytes to wait for
289 290
				 * the rest of the message. Also, set "early
				 * eaten" since we've already buffered the skb
291
				 * but don't consume yet per strp_read_sock.
292 293
				 */

T
Tom Herbert 已提交
294
				if (!stm->accum_len) {
295
					/* Start RX timer for new message */
T
Tom Herbert 已提交
296
					strp_start_timer(strp, timeo);
297 298
				}

T
Tom Herbert 已提交
299 300 301 302 303
				strp->need_bytes = stm->strp.full_len -
						       stm->accum_len;
				stm->accum_len += cand_len;
				stm->early_eaten = cand_len;
				STRP_STATS_ADD(strp->stats.bytes, cand_len);
304 305 306
				desc->count = 0; /* Stop reading socket */
				break;
			}
T
Tom Herbert 已提交
307
			stm->accum_len += cand_len;
308 309 310 311 312 313 314 315 316 317 318 319 320 321
			eaten += cand_len;
			WARN_ON(eaten != orig_len);
			break;
		}

		/* Positive extra indicates ore bytes than needed for the
		 * message
		 */

		WARN_ON(extra > cand_len);

		eaten += (cand_len - extra);

		/* Hurray, we have a new message! */
322
		cancel_delayed_work(&strp->msg_timer_work);
T
Tom Herbert 已提交
323 324
		strp->skb_head = NULL;
		STRP_STATS_INCR(strp->stats.msgs);
325 326 327 328

		/* Give skb to upper layer */
		strp->cb.rcv_msg(strp, head);

T
Tom Herbert 已提交
329
		if (unlikely(strp->paused)) {
330 331 332 333 334 335 336 337
			/* Upper layer paused strp */
			break;
		}
	}

	if (cloned_orig)
		kfree_skb(orig_skb);

T
Tom Herbert 已提交
338
	STRP_STATS_ADD(strp->stats.bytes, eaten);
339 340 341 342

	return eaten;
}

T
Tom Herbert 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
		 unsigned int orig_offset, size_t orig_len,
		 size_t max_msg_size, long timeo)
{
	read_descriptor_t desc; /* Dummy arg to strp_recv */

	desc.arg.data = strp;

	return __strp_recv(&desc, orig_skb, orig_offset, orig_len,
			   max_msg_size, timeo);
}
EXPORT_SYMBOL_GPL(strp_process);

static int strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
		     unsigned int orig_offset, size_t orig_len)
{
	struct strparser *strp = (struct strparser *)desc->arg.data;

	return __strp_recv(desc, orig_skb, orig_offset, orig_len,
			   strp->sk->sk_rcvbuf, strp->sk->sk_rcvtimeo);
}

365 366 367 368 369 370
static int default_read_sock_done(struct strparser *strp, int err)
{
	return err;
}

/* Called with lock held on lower socket */
371
static int strp_read_sock(struct strparser *strp)
372
{
373
	struct socket *sock = strp->sk->sk_socket;
374 375
	read_descriptor_t desc;

376 377 378
	if (unlikely(!sock || !sock->ops || !sock->ops->read_sock))
		return -EBUSY;

379 380 381 382
	desc.arg.data = strp;
	desc.error = 0;
	desc.count = 1; /* give more than one skb per call */

383 384
	/* sk should be locked here, so okay to do read_sock */
	sock->ops->read_sock(strp->sk, &desc, strp_recv);
385 386 387 388 389 390 391

	desc.error = strp->cb.read_sock_done(strp, desc.error);

	return desc.error;
}

/* Lower sock lock held */
392
void strp_data_ready(struct strparser *strp)
393
{
T
Tom Herbert 已提交
394
	if (unlikely(strp->stopped))
395 396
		return;

T
Tom Herbert 已提交
397 398
	/* This check is needed to synchronize with do_strp_work.
	 * do_strp_work acquires a process lock (lock_sock) whereas
399 400 401 402 403
	 * the lock held here is bh_lock_sock. The two locks can be
	 * held by different threads at the same time, but bh_lock_sock
	 * allows a thread in BH context to safely check if the process
	 * lock is held. In this case, if the lock is held, queue work.
	 */
404
	if (sock_owned_by_user_nocheck(strp->sk)) {
T
Tom Herbert 已提交
405
		queue_work(strp_wq, &strp->work);
406 407 408
		return;
	}

T
Tom Herbert 已提交
409
	if (strp->paused)
410 411
		return;

T
Tom Herbert 已提交
412 413 414
	if (strp->need_bytes) {
		if (strp_peek_len(strp) >= strp->need_bytes)
			strp->need_bytes = 0;
415 416 417 418
		else
			return;
	}

419
	if (strp_read_sock(strp) == -ENOMEM)
T
Tom Herbert 已提交
420
		queue_work(strp_wq, &strp->work);
421
}
422
EXPORT_SYMBOL_GPL(strp_data_ready);
423

T
Tom Herbert 已提交
424
static void do_strp_work(struct strparser *strp)
425 426 427
{
	read_descriptor_t rd_desc;

428 429
	/* We need the read lock to synchronize with strp_data_ready. We
	 * need the socket lock for calling strp_read_sock.
430
	 */
T
Tom Herbert 已提交
431
	strp->cb.lock(strp);
432

T
Tom Herbert 已提交
433
	if (unlikely(strp->stopped))
434 435
		goto out;

T
Tom Herbert 已提交
436
	if (strp->paused)
437 438 439 440
		goto out;

	rd_desc.arg.data = strp;

441
	if (strp_read_sock(strp) == -ENOMEM)
T
Tom Herbert 已提交
442
		queue_work(strp_wq, &strp->work);
443 444

out:
T
Tom Herbert 已提交
445
	strp->cb.unlock(strp);
446 447
}

T
Tom Herbert 已提交
448
static void strp_work(struct work_struct *w)
449
{
T
Tom Herbert 已提交
450
	do_strp_work(container_of(w, struct strparser, work));
451 452
}

453
static void strp_msg_timeout(struct work_struct *w)
454
{
455 456
	struct strparser *strp = container_of(w, struct strparser,
					      msg_timer_work.work);
457 458

	/* Message assembly timed out */
T
Tom Herbert 已提交
459 460
	STRP_STATS_INCR(strp->stats.msg_timeouts);
	strp->cb.lock(strp);
461
	strp->cb.abort_parser(strp, ETIMEDOUT);
T
Tom Herbert 已提交
462 463 464 465 466 467 468 469 470 471
	strp->cb.unlock(strp);
}

static void strp_sock_lock(struct strparser *strp)
{
	lock_sock(strp->sk);
}

static void strp_sock_unlock(struct strparser *strp)
{
472 473 474
	release_sock(strp->sk);
}

T
Tom Herbert 已提交
475
int strp_init(struct strparser *strp, struct sock *sk,
476
	      const struct strp_callbacks *cb)
477
{
478

479 480 481
	if (!cb || !cb->rcv_msg || !cb->parse_msg)
		return -EINVAL;

T
Tom Herbert 已提交
482 483 484 485 486 487 488 489 490 491
	/* The sk (sock) arg determines the mode of the stream parser.
	 *
	 * If the sock is set then the strparser is in receive callback mode.
	 * The upper layer calls strp_data_ready to kick receive processing
	 * and strparser calls the read_sock function on the socket to
	 * get packets.
	 *
	 * If the sock is not set then the strparser is in general mode.
	 * The upper layer calls strp_process for each skb to be parsed.
	 */
492

493
	if (!sk) {
T
Tom Herbert 已提交
494 495 496
		if (!cb->lock || !cb->unlock)
			return -EINVAL;
	}
497

T
Tom Herbert 已提交
498
	memset(strp, 0, sizeof(*strp));
499

T
Tom Herbert 已提交
500
	strp->sk = sk;
501

T
Tom Herbert 已提交
502 503
	strp->cb.lock = cb->lock ? : strp_sock_lock;
	strp->cb.unlock = cb->unlock ? : strp_sock_unlock;
504 505 506
	strp->cb.rcv_msg = cb->rcv_msg;
	strp->cb.parse_msg = cb->parse_msg;
	strp->cb.read_sock_done = cb->read_sock_done ? : default_read_sock_done;
T
Tom Herbert 已提交
507 508
	strp->cb.abort_parser = cb->abort_parser ? : strp_abort_strp;

509
	INIT_DELAYED_WORK(&strp->msg_timer_work, strp_msg_timeout);
T
Tom Herbert 已提交
510
	INIT_WORK(&strp->work, strp_work);
511 512 513 514 515

	return 0;
}
EXPORT_SYMBOL_GPL(strp_init);

516 517
void strp_unpause(struct strparser *strp)
{
T
Tom Herbert 已提交
518
	strp->paused = 0;
519

T
Tom Herbert 已提交
520
	/* Sync setting paused with RX work */
521 522
	smp_mb();

T
Tom Herbert 已提交
523
	queue_work(strp_wq, &strp->work);
524 525 526
}
EXPORT_SYMBOL_GPL(strp_unpause);

527
/* strp must already be stopped so that strp_recv will no longer be called.
528 529 530 531
 * Note that strp_done is not called with the lower socket held.
 */
void strp_done(struct strparser *strp)
{
T
Tom Herbert 已提交
532
	WARN_ON(!strp->stopped);
533

534
	cancel_delayed_work_sync(&strp->msg_timer_work);
T
Tom Herbert 已提交
535
	cancel_work_sync(&strp->work);
536

T
Tom Herbert 已提交
537 538 539
	if (strp->skb_head) {
		kfree_skb(strp->skb_head);
		strp->skb_head = NULL;
540 541 542 543 544 545
	}
}
EXPORT_SYMBOL_GPL(strp_done);

void strp_stop(struct strparser *strp)
{
T
Tom Herbert 已提交
546
	strp->stopped = 1;
547 548 549 550 551
}
EXPORT_SYMBOL_GPL(strp_stop);

void strp_check_rcv(struct strparser *strp)
{
T
Tom Herbert 已提交
552
	queue_work(strp_wq, &strp->work);
553 554 555 556 557 558 559 560 561 562 563 564
}
EXPORT_SYMBOL_GPL(strp_check_rcv);

static int __init strp_mod_init(void)
{
	strp_wq = create_singlethread_workqueue("kstrp");

	return 0;
}

static void __exit strp_mod_exit(void)
{
565
	destroy_workqueue(strp_wq);
566 567 568 569
}
module_init(strp_mod_init);
module_exit(strp_mod_exit);
MODULE_LICENSE("GPL");