xref: /linux/net/strparser/strparser.c (revision 0b3fc7fe6f3f68569b9f9139893491b61a4b45b9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Stream Parser
4  *
5  * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
6  */
7 
8 #include <linux/bpf.h>
9 #include <linux/errno.h>
10 #include <linux/errqueue.h>
11 #include <linux/file.h>
12 #include <linux/in.h>
13 #include <linux/kernel.h>
14 #include <linux/export.h>
15 #include <linux/init.h>
16 #include <linux/net.h>
17 #include <linux/netdevice.h>
18 #include <linux/poll.h>
19 #include <linux/rculist.h>
20 #include <linux/skbuff.h>
21 #include <linux/socket.h>
22 #include <linux/uaccess.h>
23 #include <linux/workqueue.h>
24 #include <net/strparser.h>
25 #include <net/netns/generic.h>
26 #include <net/sock.h>
27 
28 static struct workqueue_struct *strp_wq;
29 
30 static inline struct _strp_msg *_strp_msg(struct sk_buff *skb)
31 {
32 	return (struct _strp_msg *)((void *)skb->cb +
33 		offsetof(struct sk_skb_cb, strp));
34 }
35 
36 /* Lower lock held */
37 static void strp_abort_strp(struct strparser *strp, int err)
38 {
39 	struct sock *sk = strp->sk;
40 
41 	/* Unrecoverable error in receive */
42 
43 	cancel_delayed_work(&strp->msg_timer_work);
44 
45 	if (strp->stopped)
46 		return;
47 
48 	strp->stopped = 1;
49 
50 	if (strp->skb_head) {
51 		kfree_skb(strp->skb_head);
52 		strp->skb_head = NULL;
53 	}
54 
55 	strp->skb_nextp = NULL;
56 	strp->need_bytes = 0;
57 
58 	/* Report an error on the lower socket */
59 	sk->sk_err = -err;
60 	sk_error_report(sk);
61 }
62 
63 static void strp_start_timer(struct strparser *strp, long timeo)
64 {
65 	if (timeo && timeo != LONG_MAX)
66 		mod_delayed_work(strp_wq, &strp->msg_timer_work, timeo);
67 }
68 
69 /* Lower lock held */
70 static void strp_parser_err(struct strparser *strp, int err,
71 			    read_descriptor_t *desc)
72 {
73 	desc->error = err;
74 	kfree_skb(strp->skb_head);
75 	strp->skb_head = NULL;
76 	strp_abort_strp(strp, err);
77 }
78 
79 static inline int strp_peek_len(struct strparser *strp)
80 {
81 	struct socket *sock = strp->sk->sk_socket;
82 
83 	return sock->ops->peek_len(sock);
84 }
85 
86 /* Lower socket lock held */
87 static int strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
88 		     unsigned int orig_offset, size_t orig_len)
89 {
90 	struct strparser *strp = (struct strparser *)desc->arg.data;
91 	long timeo = READ_ONCE(strp->sk->sk_rcvtimeo);
92 	struct _strp_msg *stm;
93 	struct sk_buff *head, *skb;
94 	size_t eaten = 0, cand_len;
95 	ssize_t extra;
96 	int err;
97 	bool cloned_orig = false;
98 
99 	if (strp->paused)
100 		return 0;
101 
102 	head = strp->skb_head;
103 	if (head) {
104 		/* Message already in progress */
105 		if (unlikely(orig_offset)) {
106 			/* Getting data with a non-zero offset when a message is
107 			 * in progress is not expected. If it does happen, we
108 			 * need to clone and pull since we can't deal with
109 			 * offsets in the skbs for a message expect in the head.
110 			 */
111 			orig_skb = skb_clone(orig_skb, GFP_ATOMIC);
112 			if (!orig_skb) {
113 				STRP_STATS_INCR(strp->stats.mem_fail);
114 				desc->error = -ENOMEM;
115 				return 0;
116 			}
117 			if (!pskb_pull(orig_skb, orig_offset)) {
118 				STRP_STATS_INCR(strp->stats.mem_fail);
119 				kfree_skb(orig_skb);
120 				desc->error = -ENOMEM;
121 				return 0;
122 			}
123 			cloned_orig = true;
124 			orig_offset = 0;
125 		}
126 
127 		if (!strp->skb_nextp) {
128 			/* We are going to append to the frag_list of head.
129 			 * Need to unshare the frag_list.
130 			 */
131 			err = skb_unclone(head, GFP_ATOMIC);
132 			if (err) {
133 				STRP_STATS_INCR(strp->stats.mem_fail);
134 				desc->error = err;
135 				return 0;
136 			}
137 
138 			if (unlikely(skb_shinfo(head)->frag_list)) {
139 				/* We can't append to an sk_buff that already
140 				 * has a frag_list. We create a new head, point
141 				 * the frag_list of that to the old head, and
142 				 * then are able to use the old head->next for
143 				 * appending to the message.
144 				 */
145 				if (WARN_ON(head->next)) {
146 					desc->error = -EINVAL;
147 					return 0;
148 				}
149 
150 				skb = alloc_skb_for_msg(head);
151 				if (!skb) {
152 					STRP_STATS_INCR(strp->stats.mem_fail);
153 					desc->error = -ENOMEM;
154 					return 0;
155 				}
156 
157 				strp->skb_nextp = &head->next;
158 				strp->skb_head = skb;
159 				head = skb;
160 			} else {
161 				strp->skb_nextp =
162 				    &skb_shinfo(head)->frag_list;
163 			}
164 		}
165 	}
166 
167 	while (eaten < orig_len) {
168 		/* Always clone since we will consume something */
169 		skb = skb_clone(orig_skb, GFP_ATOMIC);
170 		if (!skb) {
171 			STRP_STATS_INCR(strp->stats.mem_fail);
172 			desc->error = -ENOMEM;
173 			break;
174 		}
175 
176 		cand_len = orig_len - eaten;
177 
178 		head = strp->skb_head;
179 		if (!head) {
180 			head = skb;
181 			strp->skb_head = head;
182 			/* Will set skb_nextp on next packet if needed */
183 			strp->skb_nextp = NULL;
184 			stm = _strp_msg(head);
185 			memset(stm, 0, sizeof(*stm));
186 			stm->strp.offset = orig_offset + eaten;
187 		} else {
188 			/* Unclone if we are appending to an skb that we
189 			 * already share a frag_list with.
190 			 */
191 			if (skb_has_frag_list(skb)) {
192 				err = skb_unclone(skb, GFP_ATOMIC);
193 				if (err) {
194 					STRP_STATS_INCR(strp->stats.mem_fail);
195 					desc->error = err;
196 					break;
197 				}
198 			}
199 
200 			stm = _strp_msg(head);
201 			*strp->skb_nextp = skb;
202 			strp->skb_nextp = &skb->next;
203 			head->data_len += skb->len;
204 			head->len += skb->len;
205 			head->truesize += skb->truesize;
206 		}
207 
208 		if (!stm->strp.full_len) {
209 			ssize_t len;
210 
211 			len = (*strp->cb.parse_msg)(strp, head);
212 
213 			if (!len) {
214 				/* Need more header to determine length */
215 				if (!stm->accum_len) {
216 					/* Start RX timer for new message */
217 					strp_start_timer(strp, timeo);
218 				}
219 				stm->accum_len += cand_len;
220 				eaten += cand_len;
221 				STRP_STATS_INCR(strp->stats.need_more_hdr);
222 				WARN_ON(eaten != orig_len);
223 				break;
224 			} else if (len < 0) {
225 				if (len == -ESTRPIPE && stm->accum_len) {
226 					len = -ENODATA;
227 					strp->unrecov_intr = 1;
228 				} else {
229 					strp->interrupted = 1;
230 				}
231 				strp_parser_err(strp, len, desc);
232 				break;
233 			} else if (len > strp->sk->sk_rcvbuf) {
234 				/* Message length exceeds maximum allowed */
235 				STRP_STATS_INCR(strp->stats.msg_too_big);
236 				strp_parser_err(strp, -EMSGSIZE, desc);
237 				break;
238 			} else if (len <= (ssize_t)head->len -
239 					  (ssize_t)skb->len - stm->strp.offset) {
240 				/* Length must be into new skb (and also
241 				 * greater than zero)
242 				 */
243 				STRP_STATS_INCR(strp->stats.bad_hdr_len);
244 				strp_parser_err(strp, -EPROTO, desc);
245 				break;
246 			}
247 
248 			stm->strp.full_len = len;
249 		}
250 
251 		extra = (ssize_t)(stm->accum_len + cand_len) -
252 			stm->strp.full_len;
253 
254 		if (extra < 0) {
255 			/* Message not complete yet. */
256 			if (stm->strp.full_len - stm->accum_len >
257 			    strp_peek_len(strp)) {
258 				/* Don't have the whole message in the socket
259 				 * buffer. Set strp->need_bytes to wait for
260 				 * the rest of the message. Also, set "early
261 				 * eaten" since we've already buffered the skb
262 				 * but don't consume yet per strp_read_sock.
263 				 */
264 
265 				if (!stm->accum_len) {
266 					/* Start RX timer for new message */
267 					strp_start_timer(strp, timeo);
268 				}
269 
270 				stm->accum_len += cand_len;
271 				eaten += cand_len;
272 				strp->need_bytes = stm->strp.full_len -
273 						       stm->accum_len;
274 				STRP_STATS_ADD(strp->stats.bytes, cand_len);
275 				desc->count = 0; /* Stop reading socket */
276 				break;
277 			}
278 			stm->accum_len += cand_len;
279 			eaten += cand_len;
280 			WARN_ON(eaten != orig_len);
281 			break;
282 		}
283 
284 		/* Positive extra indicates more bytes than needed for the
285 		 * message
286 		 */
287 
288 		WARN_ON(extra > cand_len);
289 
290 		eaten += (cand_len - extra);
291 
292 		/* Hurray, we have a new message! */
293 		cancel_delayed_work(&strp->msg_timer_work);
294 		strp->skb_head = NULL;
295 		strp->need_bytes = 0;
296 		STRP_STATS_INCR(strp->stats.msgs);
297 
298 		/* Give skb to upper layer */
299 		strp->cb.rcv_msg(strp, head);
300 
301 		if (unlikely(strp->paused)) {
302 			/* Upper layer paused strp */
303 			break;
304 		}
305 	}
306 
307 	if (cloned_orig)
308 		kfree_skb(orig_skb);
309 
310 	STRP_STATS_ADD(strp->stats.bytes, eaten);
311 
312 	return eaten;
313 }
314 
315 static int default_read_sock_done(struct strparser *strp, int err)
316 {
317 	return err;
318 }
319 
320 /* Called with lock held on lower socket */
321 static int strp_read_sock(struct strparser *strp)
322 {
323 	struct socket *sock = strp->sk->sk_socket;
324 	read_descriptor_t desc;
325 
326 	if (unlikely(!sock || !sock->ops))
327 		return -EBUSY;
328 
329 	if (unlikely(!strp->cb.read_sock && !sock->ops->read_sock))
330 		return -EBUSY;
331 
332 	desc.arg.data = strp;
333 	desc.error = 0;
334 	desc.count = 1; /* give more than one skb per call */
335 
336 	/* sk should be locked here, so okay to do read_sock */
337 	if (strp->cb.read_sock)
338 		strp->cb.read_sock(strp, &desc, strp_recv);
339 	else
340 		sock->ops->read_sock(strp->sk, &desc, strp_recv);
341 
342 	desc.error = strp->cb.read_sock_done(strp, desc.error);
343 
344 	return desc.error;
345 }
346 
347 /* Lower sock lock held */
348 void strp_data_ready(struct strparser *strp)
349 {
350 	if (unlikely(strp->stopped) || strp->paused)
351 		return;
352 
353 	/* This check is needed to synchronize with do_strp_work.
354 	 * do_strp_work acquires a process lock (lock_sock) whereas
355 	 * the lock held here is bh_lock_sock. The two locks can be
356 	 * held by different threads at the same time, but bh_lock_sock
357 	 * allows a thread in BH context to safely check if the process
358 	 * lock is held. In this case, if the lock is held, queue work.
359 	 */
360 	if (sock_owned_by_user_nocheck(strp->sk)) {
361 		queue_work(strp_wq, &strp->work);
362 		return;
363 	}
364 
365 	if (strp->need_bytes) {
366 		if (strp_peek_len(strp) < strp->need_bytes)
367 			return;
368 	}
369 
370 	if (strp_read_sock(strp) == -ENOMEM)
371 		queue_work(strp_wq, &strp->work);
372 }
373 EXPORT_SYMBOL_GPL(strp_data_ready);
374 
375 static void do_strp_work(struct strparser *strp)
376 {
377 	/* We need the read lock to synchronize with strp_data_ready. We
378 	 * need the socket lock for calling strp_read_sock.
379 	 */
380 	lock_sock(strp->sk);
381 
382 	if (unlikely(strp->stopped))
383 		goto out;
384 
385 	if (strp->paused)
386 		goto out;
387 
388 	if (strp_read_sock(strp) == -ENOMEM)
389 		queue_work(strp_wq, &strp->work);
390 
391 out:
392 	release_sock(strp->sk);
393 }
394 
395 static void strp_work(struct work_struct *w)
396 {
397 	do_strp_work(container_of(w, struct strparser, work));
398 }
399 
400 static void strp_msg_timeout(struct work_struct *w)
401 {
402 	struct strparser *strp = container_of(w, struct strparser,
403 					      msg_timer_work.work);
404 
405 	/* Message assembly timed out */
406 	STRP_STATS_INCR(strp->stats.msg_timeouts);
407 	lock_sock(strp->sk);
408 	strp_abort_strp(strp, -ETIMEDOUT);
409 	release_sock(strp->sk);
410 }
411 
412 int strp_init(struct strparser *strp, struct sock *sk,
413 	      const struct strp_callbacks *cb)
414 {
415 
416 	if (!cb || !cb->rcv_msg || !cb->parse_msg || !sk)
417 		return -EINVAL;
418 
419 	memset(strp, 0, sizeof(*strp));
420 
421 	strp->sk = sk;
422 
423 	strp->cb.rcv_msg = cb->rcv_msg;
424 	strp->cb.parse_msg = cb->parse_msg;
425 	strp->cb.read_sock = cb->read_sock;
426 	strp->cb.read_sock_done = cb->read_sock_done ? : default_read_sock_done;
427 
428 	INIT_DELAYED_WORK(&strp->msg_timer_work, strp_msg_timeout);
429 	INIT_WORK(&strp->work, strp_work);
430 
431 	return 0;
432 }
433 EXPORT_SYMBOL_GPL(strp_init);
434 
435 void strp_unpause(struct strparser *strp)
436 {
437 	strp->paused = 0;
438 
439 	/* Sync setting paused with RX work */
440 	smp_mb();
441 
442 	queue_work(strp_wq, &strp->work);
443 }
444 EXPORT_SYMBOL_GPL(strp_unpause);
445 
446 /* strp must already be stopped so that strp_recv will no longer be called.
447  * Note that strp_done must not be called with the lower socket held.
448  */
449 void strp_done(struct strparser *strp)
450 {
451 	WARN_ON(!strp->stopped);
452 
453 	lock_sock(strp->sk);
454 	/* sync with pending strp_recv */
455 	release_sock(strp->sk);
456 
457 	cancel_delayed_work_sync(&strp->msg_timer_work);
458 	cancel_work_sync(&strp->work);
459 
460 	if (strp->skb_head) {
461 		kfree_skb(strp->skb_head);
462 		strp->skb_head = NULL;
463 	}
464 }
465 EXPORT_SYMBOL_GPL(strp_done);
466 
467 void strp_stop(struct strparser *strp)
468 {
469 	strp->stopped = 1;
470 }
471 EXPORT_SYMBOL_GPL(strp_stop);
472 
473 void strp_check_rcv(struct strparser *strp)
474 {
475 	queue_work(strp_wq, &strp->work);
476 }
477 EXPORT_SYMBOL_GPL(strp_check_rcv);
478 
479 static int __init strp_dev_init(void)
480 {
481 	BUILD_BUG_ON(sizeof(struct sk_skb_cb) >
482 		     sizeof_field(struct sk_buff, cb));
483 
484 	strp_wq = create_singlethread_workqueue("kstrp");
485 	if (unlikely(!strp_wq))
486 		return -ENOMEM;
487 
488 	return 0;
489 }
490 device_initcall(strp_dev_init);
491