strparser.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. #include <net/tcp.h>
  29. static struct workqueue_struct *strp_wq;
  30. struct _strp_rx_msg {
  31. /* Internal cb structure. struct strp_rx_msg must be first for passing
  32. * to upper layer.
  33. */
  34. struct strp_rx_msg strp;
  35. int accum_len;
  36. int early_eaten;
  37. };
  38. static inline struct _strp_rx_msg *_strp_rx_msg(struct sk_buff *skb)
  39. {
  40. return (struct _strp_rx_msg *)((void *)skb->cb +
  41. offsetof(struct qdisc_skb_cb, data));
  42. }
  43. /* Lower lock held */
  44. static void strp_abort_rx_strp(struct strparser *strp, int err)
  45. {
  46. struct sock *csk = strp->sk;
  47. /* Unrecoverable error in receive */
  48. del_timer(&strp->rx_msg_timer);
  49. if (strp->rx_stopped)
  50. return;
  51. strp->rx_stopped = 1;
  52. /* Report an error on the lower socket */
  53. csk->sk_err = err;
  54. csk->sk_error_report(csk);
  55. }
  56. static void strp_start_rx_timer(struct strparser *strp)
  57. {
  58. if (strp->sk->sk_rcvtimeo)
  59. mod_timer(&strp->rx_msg_timer, strp->sk->sk_rcvtimeo);
  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->rx_skb_head);
  67. strp->rx_skb_head = NULL;
  68. strp->cb.abort_parser(strp, err);
  69. }
  70. /* Lower socket lock held */
  71. static int strp_tcp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
  72. unsigned int orig_offset, size_t orig_len)
  73. {
  74. struct strparser *strp = (struct strparser *)desc->arg.data;
  75. struct _strp_rx_msg *rxm;
  76. struct sk_buff *head, *skb;
  77. size_t eaten = 0, cand_len;
  78. ssize_t extra;
  79. int err;
  80. bool cloned_orig = false;
  81. if (strp->rx_paused)
  82. return 0;
  83. head = strp->rx_skb_head;
  84. if (head) {
  85. /* Message already in progress */
  86. rxm = _strp_rx_msg(head);
  87. if (unlikely(rxm->early_eaten)) {
  88. /* Already some number of bytes on the receive sock
  89. * data saved in rx_skb_head, just indicate they
  90. * are consumed.
  91. */
  92. eaten = orig_len <= rxm->early_eaten ?
  93. orig_len : rxm->early_eaten;
  94. rxm->early_eaten -= eaten;
  95. return eaten;
  96. }
  97. if (unlikely(orig_offset)) {
  98. /* Getting data with a non-zero offset when a message is
  99. * in progress is not expected. If it does happen, we
  100. * need to clone and pull since we can't deal with
  101. * offsets in the skbs for a message expect in the head.
  102. */
  103. orig_skb = skb_clone(orig_skb, GFP_ATOMIC);
  104. if (!orig_skb) {
  105. STRP_STATS_INCR(strp->stats.rx_mem_fail);
  106. desc->error = -ENOMEM;
  107. return 0;
  108. }
  109. if (!pskb_pull(orig_skb, orig_offset)) {
  110. STRP_STATS_INCR(strp->stats.rx_mem_fail);
  111. kfree_skb(orig_skb);
  112. desc->error = -ENOMEM;
  113. return 0;
  114. }
  115. cloned_orig = true;
  116. orig_offset = 0;
  117. }
  118. if (!strp->rx_skb_nextp) {
  119. /* We are going to append to the frags_list of head.
  120. * Need to unshare the frag_list.
  121. */
  122. err = skb_unclone(head, GFP_ATOMIC);
  123. if (err) {
  124. STRP_STATS_INCR(strp->stats.rx_mem_fail);
  125. desc->error = err;
  126. return 0;
  127. }
  128. if (unlikely(skb_shinfo(head)->frag_list)) {
  129. /* We can't append to an sk_buff that already
  130. * has a frag_list. We create a new head, point
  131. * the frag_list of that to the old head, and
  132. * then are able to use the old head->next for
  133. * appending to the message.
  134. */
  135. if (WARN_ON(head->next)) {
  136. desc->error = -EINVAL;
  137. return 0;
  138. }
  139. skb = alloc_skb(0, GFP_ATOMIC);
  140. if (!skb) {
  141. STRP_STATS_INCR(strp->stats.rx_mem_fail);
  142. desc->error = -ENOMEM;
  143. return 0;
  144. }
  145. skb->len = head->len;
  146. skb->data_len = head->len;
  147. skb->truesize = head->truesize;
  148. *_strp_rx_msg(skb) = *_strp_rx_msg(head);
  149. strp->rx_skb_nextp = &head->next;
  150. skb_shinfo(skb)->frag_list = head;
  151. strp->rx_skb_head = skb;
  152. head = skb;
  153. } else {
  154. strp->rx_skb_nextp =
  155. &skb_shinfo(head)->frag_list;
  156. }
  157. }
  158. }
  159. while (eaten < orig_len) {
  160. /* Always clone since we will consume something */
  161. skb = skb_clone(orig_skb, GFP_ATOMIC);
  162. if (!skb) {
  163. STRP_STATS_INCR(strp->stats.rx_mem_fail);
  164. desc->error = -ENOMEM;
  165. break;
  166. }
  167. cand_len = orig_len - eaten;
  168. head = strp->rx_skb_head;
  169. if (!head) {
  170. head = skb;
  171. strp->rx_skb_head = head;
  172. /* Will set rx_skb_nextp on next packet if needed */
  173. strp->rx_skb_nextp = NULL;
  174. rxm = _strp_rx_msg(head);
  175. memset(rxm, 0, sizeof(*rxm));
  176. rxm->strp.offset = orig_offset + eaten;
  177. } else {
  178. /* Unclone since we may be appending to an skb that we
  179. * already share a frag_list with.
  180. */
  181. err = skb_unclone(skb, GFP_ATOMIC);
  182. if (err) {
  183. STRP_STATS_INCR(strp->stats.rx_mem_fail);
  184. desc->error = err;
  185. break;
  186. }
  187. rxm = _strp_rx_msg(head);
  188. *strp->rx_skb_nextp = skb;
  189. strp->rx_skb_nextp = &skb->next;
  190. head->data_len += skb->len;
  191. head->len += skb->len;
  192. head->truesize += skb->truesize;
  193. }
  194. if (!rxm->strp.full_len) {
  195. ssize_t len;
  196. len = (*strp->cb.parse_msg)(strp, head);
  197. if (!len) {
  198. /* Need more header to determine length */
  199. if (!rxm->accum_len) {
  200. /* Start RX timer for new message */
  201. strp_start_rx_timer(strp);
  202. }
  203. rxm->accum_len += cand_len;
  204. eaten += cand_len;
  205. STRP_STATS_INCR(strp->stats.rx_need_more_hdr);
  206. WARN_ON(eaten != orig_len);
  207. break;
  208. } else if (len < 0) {
  209. if (len == -ESTRPIPE && rxm->accum_len) {
  210. len = -ENODATA;
  211. strp->rx_unrecov_intr = 1;
  212. } else {
  213. strp->rx_interrupted = 1;
  214. }
  215. strp_parser_err(strp, err, desc);
  216. break;
  217. } else if (len > strp->sk->sk_rcvbuf) {
  218. /* Message length exceeds maximum allowed */
  219. STRP_STATS_INCR(strp->stats.rx_msg_too_big);
  220. strp_parser_err(strp, -EMSGSIZE, desc);
  221. break;
  222. } else if (len <= (ssize_t)head->len -
  223. skb->len - rxm->strp.offset) {
  224. /* Length must be into new skb (and also
  225. * greater than zero)
  226. */
  227. STRP_STATS_INCR(strp->stats.rx_bad_hdr_len);
  228. strp_parser_err(strp, -EPROTO, desc);
  229. break;
  230. }
  231. rxm->strp.full_len = len;
  232. }
  233. extra = (ssize_t)(rxm->accum_len + cand_len) -
  234. rxm->strp.full_len;
  235. if (extra < 0) {
  236. /* Message not complete yet. */
  237. if (rxm->strp.full_len - rxm->accum_len >
  238. tcp_inq(strp->sk)) {
  239. /* Don't have the whole messages in the socket
  240. * buffer. Set strp->rx_need_bytes to wait for
  241. * the rest of the message. Also, set "early
  242. * eaten" since we've already buffered the skb
  243. * but don't consume yet per tcp_read_sock.
  244. */
  245. if (!rxm->accum_len) {
  246. /* Start RX timer for new message */
  247. strp_start_rx_timer(strp);
  248. }
  249. strp->rx_need_bytes = rxm->strp.full_len -
  250. rxm->accum_len;
  251. rxm->accum_len += cand_len;
  252. rxm->early_eaten = cand_len;
  253. STRP_STATS_ADD(strp->stats.rx_bytes, cand_len);
  254. desc->count = 0; /* Stop reading socket */
  255. break;
  256. }
  257. rxm->accum_len += cand_len;
  258. eaten += cand_len;
  259. WARN_ON(eaten != orig_len);
  260. break;
  261. }
  262. /* Positive extra indicates ore bytes than needed for the
  263. * message
  264. */
  265. WARN_ON(extra > cand_len);
  266. eaten += (cand_len - extra);
  267. /* Hurray, we have a new message! */
  268. del_timer(&strp->rx_msg_timer);
  269. strp->rx_skb_head = NULL;
  270. STRP_STATS_INCR(strp->stats.rx_msgs);
  271. /* Give skb to upper layer */
  272. strp->cb.rcv_msg(strp, head);
  273. if (unlikely(strp->rx_paused)) {
  274. /* Upper layer paused strp */
  275. break;
  276. }
  277. }
  278. if (cloned_orig)
  279. kfree_skb(orig_skb);
  280. STRP_STATS_ADD(strp->stats.rx_bytes, eaten);
  281. return eaten;
  282. }
  283. static int default_read_sock_done(struct strparser *strp, int err)
  284. {
  285. return err;
  286. }
  287. /* Called with lock held on lower socket */
  288. static int strp_tcp_read_sock(struct strparser *strp)
  289. {
  290. read_descriptor_t desc;
  291. desc.arg.data = strp;
  292. desc.error = 0;
  293. desc.count = 1; /* give more than one skb per call */
  294. /* sk should be locked here, so okay to do tcp_read_sock */
  295. tcp_read_sock(strp->sk, &desc, strp_tcp_recv);
  296. desc.error = strp->cb.read_sock_done(strp, desc.error);
  297. return desc.error;
  298. }
  299. /* Lower sock lock held */
  300. void strp_tcp_data_ready(struct strparser *strp)
  301. {
  302. struct sock *csk = strp->sk;
  303. if (unlikely(strp->rx_stopped))
  304. return;
  305. /* This check is needed to synchronize with do_strp_rx_work.
  306. * do_strp_rx_work acquires a process lock (lock_sock) whereas
  307. * the lock held here is bh_lock_sock. The two locks can be
  308. * held by different threads at the same time, but bh_lock_sock
  309. * allows a thread in BH context to safely check if the process
  310. * lock is held. In this case, if the lock is held, queue work.
  311. */
  312. if (sock_owned_by_user(csk)) {
  313. queue_work(strp_wq, &strp->rx_work);
  314. return;
  315. }
  316. if (strp->rx_paused)
  317. return;
  318. if (strp->rx_need_bytes) {
  319. if (tcp_inq(csk) >= strp->rx_need_bytes)
  320. strp->rx_need_bytes = 0;
  321. else
  322. return;
  323. }
  324. if (strp_tcp_read_sock(strp) == -ENOMEM)
  325. queue_work(strp_wq, &strp->rx_work);
  326. }
  327. EXPORT_SYMBOL_GPL(strp_tcp_data_ready);
  328. static void do_strp_rx_work(struct strparser *strp)
  329. {
  330. read_descriptor_t rd_desc;
  331. struct sock *csk = strp->sk;
  332. /* We need the read lock to synchronize with strp_tcp_data_ready. We
  333. * need the socket lock for calling tcp_read_sock.
  334. */
  335. lock_sock(csk);
  336. if (unlikely(csk->sk_user_data != strp))
  337. goto out;
  338. if (unlikely(strp->rx_stopped))
  339. goto out;
  340. if (strp->rx_paused)
  341. goto out;
  342. rd_desc.arg.data = strp;
  343. if (strp_tcp_read_sock(strp) == -ENOMEM)
  344. queue_work(strp_wq, &strp->rx_work);
  345. out:
  346. release_sock(csk);
  347. }
  348. static void strp_rx_work(struct work_struct *w)
  349. {
  350. do_strp_rx_work(container_of(w, struct strparser, rx_work));
  351. }
  352. static void strp_rx_msg_timeout(unsigned long arg)
  353. {
  354. struct strparser *strp = (struct strparser *)arg;
  355. /* Message assembly timed out */
  356. STRP_STATS_INCR(strp->stats.rx_msg_timeouts);
  357. lock_sock(strp->sk);
  358. strp->cb.abort_parser(strp, ETIMEDOUT);
  359. release_sock(strp->sk);
  360. }
  361. int strp_init(struct strparser *strp, struct sock *csk,
  362. struct strp_callbacks *cb)
  363. {
  364. if (!cb || !cb->rcv_msg || !cb->parse_msg)
  365. return -EINVAL;
  366. memset(strp, 0, sizeof(*strp));
  367. strp->sk = csk;
  368. setup_timer(&strp->rx_msg_timer, strp_rx_msg_timeout,
  369. (unsigned long)strp);
  370. INIT_WORK(&strp->rx_work, strp_rx_work);
  371. strp->cb.rcv_msg = cb->rcv_msg;
  372. strp->cb.parse_msg = cb->parse_msg;
  373. strp->cb.read_sock_done = cb->read_sock_done ? : default_read_sock_done;
  374. strp->cb.abort_parser = cb->abort_parser ? : strp_abort_rx_strp;
  375. return 0;
  376. }
  377. EXPORT_SYMBOL_GPL(strp_init);
  378. /* strp must already be stopped so that strp_tcp_recv will no longer be called.
  379. * Note that strp_done is not called with the lower socket held.
  380. */
  381. void strp_done(struct strparser *strp)
  382. {
  383. WARN_ON(!strp->rx_stopped);
  384. del_timer_sync(&strp->rx_msg_timer);
  385. cancel_work_sync(&strp->rx_work);
  386. if (strp->rx_skb_head) {
  387. kfree_skb(strp->rx_skb_head);
  388. strp->rx_skb_head = NULL;
  389. }
  390. }
  391. EXPORT_SYMBOL_GPL(strp_done);
  392. void strp_stop(struct strparser *strp)
  393. {
  394. strp->rx_stopped = 1;
  395. }
  396. EXPORT_SYMBOL_GPL(strp_stop);
  397. void strp_check_rcv(struct strparser *strp)
  398. {
  399. queue_work(strp_wq, &strp->rx_work);
  400. }
  401. EXPORT_SYMBOL_GPL(strp_check_rcv);
  402. static int __init strp_mod_init(void)
  403. {
  404. strp_wq = create_singlethread_workqueue("kstrp");
  405. return 0;
  406. }
  407. static void __exit strp_mod_exit(void)
  408. {
  409. }
  410. module_init(strp_mod_init);
  411. module_exit(strp_mod_exit);
  412. MODULE_LICENSE("GPL");