strparser.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 since we may be appending to an skb that we
  180. * already share a frag_list with.
  181. */
  182. err = skb_unclone(skb, GFP_ATOMIC);
  183. if (err) {
  184. STRP_STATS_INCR(strp->stats.mem_fail);
  185. desc->error = err;
  186. break;
  187. }
  188. stm = _strp_msg(head);
  189. *strp->skb_nextp = skb;
  190. strp->skb_nextp = &skb->next;
  191. head->data_len += skb->len;
  192. head->len += skb->len;
  193. head->truesize += skb->truesize;
  194. }
  195. if (!stm->strp.full_len) {
  196. ssize_t len;
  197. len = (*strp->cb.parse_msg)(strp, head);
  198. if (!len) {
  199. /* Need more header to determine length */
  200. if (!stm->accum_len) {
  201. /* Start RX timer for new message */
  202. strp_start_timer(strp, timeo);
  203. }
  204. stm->accum_len += cand_len;
  205. eaten += cand_len;
  206. STRP_STATS_INCR(strp->stats.need_more_hdr);
  207. WARN_ON(eaten != orig_len);
  208. break;
  209. } else if (len < 0) {
  210. if (len == -ESTRPIPE && stm->accum_len) {
  211. len = -ENODATA;
  212. strp->unrecov_intr = 1;
  213. } else {
  214. strp->interrupted = 1;
  215. }
  216. strp_parser_err(strp, len, desc);
  217. break;
  218. } else if (len > max_msg_size) {
  219. /* Message length exceeds maximum allowed */
  220. STRP_STATS_INCR(strp->stats.msg_too_big);
  221. strp_parser_err(strp, -EMSGSIZE, desc);
  222. break;
  223. } else if (len <= (ssize_t)head->len -
  224. skb->len - stm->strp.offset) {
  225. /* Length must be into new skb (and also
  226. * greater than zero)
  227. */
  228. STRP_STATS_INCR(strp->stats.bad_hdr_len);
  229. strp_parser_err(strp, -EPROTO, desc);
  230. break;
  231. }
  232. stm->strp.full_len = len;
  233. }
  234. extra = (ssize_t)(stm->accum_len + cand_len) -
  235. stm->strp.full_len;
  236. if (extra < 0) {
  237. /* Message not complete yet. */
  238. if (stm->strp.full_len - stm->accum_len >
  239. strp_peek_len(strp)) {
  240. /* Don't have the whole message in the socket
  241. * buffer. Set strp->need_bytes to wait for
  242. * the rest of the message. Also, set "early
  243. * eaten" since we've already buffered the skb
  244. * but don't consume yet per strp_read_sock.
  245. */
  246. if (!stm->accum_len) {
  247. /* Start RX timer for new message */
  248. strp_start_timer(strp, timeo);
  249. }
  250. stm->accum_len += cand_len;
  251. eaten += cand_len;
  252. strp->need_bytes = stm->strp.full_len -
  253. stm->accum_len;
  254. STRP_STATS_ADD(strp->stats.bytes, cand_len);
  255. desc->count = 0; /* Stop reading socket */
  256. break;
  257. }
  258. stm->accum_len += cand_len;
  259. eaten += cand_len;
  260. WARN_ON(eaten != orig_len);
  261. break;
  262. }
  263. /* Positive extra indicates ore bytes than needed for the
  264. * message
  265. */
  266. WARN_ON(extra > cand_len);
  267. eaten += (cand_len - extra);
  268. /* Hurray, we have a new message! */
  269. cancel_delayed_work(&strp->msg_timer_work);
  270. strp->skb_head = NULL;
  271. strp->need_bytes = 0;
  272. STRP_STATS_INCR(strp->stats.msgs);
  273. /* Give skb to upper layer */
  274. strp->cb.rcv_msg(strp, head);
  275. if (unlikely(strp->paused)) {
  276. /* Upper layer paused strp */
  277. break;
  278. }
  279. }
  280. if (cloned_orig)
  281. kfree_skb(orig_skb);
  282. STRP_STATS_ADD(strp->stats.bytes, eaten);
  283. return eaten;
  284. }
  285. int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
  286. unsigned int orig_offset, size_t orig_len,
  287. size_t max_msg_size, long timeo)
  288. {
  289. read_descriptor_t desc; /* Dummy arg to strp_recv */
  290. desc.arg.data = strp;
  291. return __strp_recv(&desc, orig_skb, orig_offset, orig_len,
  292. max_msg_size, timeo);
  293. }
  294. EXPORT_SYMBOL_GPL(strp_process);
  295. static int strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
  296. unsigned int orig_offset, size_t orig_len)
  297. {
  298. struct strparser *strp = (struct strparser *)desc->arg.data;
  299. return __strp_recv(desc, orig_skb, orig_offset, orig_len,
  300. strp->sk->sk_rcvbuf, strp->sk->sk_rcvtimeo);
  301. }
  302. static int default_read_sock_done(struct strparser *strp, int err)
  303. {
  304. return err;
  305. }
  306. /* Called with lock held on lower socket */
  307. static int strp_read_sock(struct strparser *strp)
  308. {
  309. struct socket *sock = strp->sk->sk_socket;
  310. read_descriptor_t desc;
  311. if (unlikely(!sock || !sock->ops || !sock->ops->read_sock))
  312. return -EBUSY;
  313. desc.arg.data = strp;
  314. desc.error = 0;
  315. desc.count = 1; /* give more than one skb per call */
  316. /* sk should be locked here, so okay to do read_sock */
  317. sock->ops->read_sock(strp->sk, &desc, strp_recv);
  318. desc.error = strp->cb.read_sock_done(strp, desc.error);
  319. return desc.error;
  320. }
  321. /* Lower sock lock held */
  322. void strp_data_ready(struct strparser *strp)
  323. {
  324. if (unlikely(strp->stopped))
  325. return;
  326. /* This check is needed to synchronize with do_strp_work.
  327. * do_strp_work acquires a process lock (lock_sock) whereas
  328. * the lock held here is bh_lock_sock. The two locks can be
  329. * held by different threads at the same time, but bh_lock_sock
  330. * allows a thread in BH context to safely check if the process
  331. * lock is held. In this case, if the lock is held, queue work.
  332. */
  333. if (sock_owned_by_user(strp->sk)) {
  334. queue_work(strp_wq, &strp->work);
  335. return;
  336. }
  337. if (strp->paused)
  338. return;
  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. read_descriptor_t rd_desc;
  350. /* We need the read lock to synchronize with strp_data_ready. We
  351. * need the socket lock for calling strp_read_sock.
  352. */
  353. strp->cb.lock(strp);
  354. if (unlikely(strp->stopped))
  355. goto out;
  356. if (strp->paused)
  357. goto out;
  358. rd_desc.arg.data = strp;
  359. if (strp_read_sock(strp) == -ENOMEM)
  360. queue_work(strp_wq, &strp->work);
  361. out:
  362. strp->cb.unlock(strp);
  363. }
  364. static void strp_work(struct work_struct *w)
  365. {
  366. do_strp_work(container_of(w, struct strparser, work));
  367. }
  368. static void strp_msg_timeout(struct work_struct *w)
  369. {
  370. struct strparser *strp = container_of(w, struct strparser,
  371. msg_timer_work.work);
  372. /* Message assembly timed out */
  373. STRP_STATS_INCR(strp->stats.msg_timeouts);
  374. strp->cb.lock(strp);
  375. strp->cb.abort_parser(strp, -ETIMEDOUT);
  376. strp->cb.unlock(strp);
  377. }
  378. static void strp_sock_lock(struct strparser *strp)
  379. {
  380. lock_sock(strp->sk);
  381. }
  382. static void strp_sock_unlock(struct strparser *strp)
  383. {
  384. release_sock(strp->sk);
  385. }
  386. int strp_init(struct strparser *strp, struct sock *sk,
  387. const struct strp_callbacks *cb)
  388. {
  389. if (!cb || !cb->rcv_msg || !cb->parse_msg)
  390. return -EINVAL;
  391. /* The sk (sock) arg determines the mode of the stream parser.
  392. *
  393. * If the sock is set then the strparser is in receive callback mode.
  394. * The upper layer calls strp_data_ready to kick receive processing
  395. * and strparser calls the read_sock function on the socket to
  396. * get packets.
  397. *
  398. * If the sock is not set then the strparser is in general mode.
  399. * The upper layer calls strp_process for each skb to be parsed.
  400. */
  401. if (!sk) {
  402. if (!cb->lock || !cb->unlock)
  403. return -EINVAL;
  404. }
  405. memset(strp, 0, sizeof(*strp));
  406. strp->sk = sk;
  407. strp->cb.lock = cb->lock ? : strp_sock_lock;
  408. strp->cb.unlock = cb->unlock ? : strp_sock_unlock;
  409. strp->cb.rcv_msg = cb->rcv_msg;
  410. strp->cb.parse_msg = cb->parse_msg;
  411. strp->cb.read_sock_done = cb->read_sock_done ? : default_read_sock_done;
  412. strp->cb.abort_parser = cb->abort_parser ? : strp_abort_strp;
  413. INIT_DELAYED_WORK(&strp->msg_timer_work, strp_msg_timeout);
  414. INIT_WORK(&strp->work, strp_work);
  415. return 0;
  416. }
  417. EXPORT_SYMBOL_GPL(strp_init);
  418. void strp_unpause(struct strparser *strp)
  419. {
  420. strp->paused = 0;
  421. /* Sync setting paused with RX work */
  422. smp_mb();
  423. queue_work(strp_wq, &strp->work);
  424. }
  425. EXPORT_SYMBOL_GPL(strp_unpause);
  426. /* strp must already be stopped so that strp_recv will no longer be called.
  427. * Note that strp_done is not called with the lower socket held.
  428. */
  429. void strp_done(struct strparser *strp)
  430. {
  431. WARN_ON(!strp->stopped);
  432. cancel_delayed_work_sync(&strp->msg_timer_work);
  433. cancel_work_sync(&strp->work);
  434. if (strp->skb_head) {
  435. kfree_skb(strp->skb_head);
  436. strp->skb_head = NULL;
  437. }
  438. }
  439. EXPORT_SYMBOL_GPL(strp_done);
  440. void strp_stop(struct strparser *strp)
  441. {
  442. strp->stopped = 1;
  443. }
  444. EXPORT_SYMBOL_GPL(strp_stop);
  445. void strp_check_rcv(struct strparser *strp)
  446. {
  447. queue_work(strp_wq, &strp->work);
  448. }
  449. EXPORT_SYMBOL_GPL(strp_check_rcv);
  450. static int __init strp_mod_init(void)
  451. {
  452. strp_wq = create_singlethread_workqueue("kstrp");
  453. return 0;
  454. }
  455. static void __exit strp_mod_exit(void)
  456. {
  457. destroy_workqueue(strp_wq);
  458. }
  459. module_init(strp_mod_init);
  460. module_exit(strp_mod_exit);
  461. MODULE_LICENSE("GPL");