strparser.c 13 KB

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