strparser.c 13 KB

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