strparser.c 12 KB

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