strparser.c 14 KB

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