strparser.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. if (skb_has_frag_list(head)) {
  124. err = skb_unclone(head, GFP_ATOMIC);
  125. if (err) {
  126. STRP_STATS_INCR(strp->stats.mem_fail);
  127. desc->error = err;
  128. return 0;
  129. }
  130. }
  131. if (unlikely(skb_shinfo(head)->frag_list)) {
  132. /* We can't append to an sk_buff that already
  133. * has a frag_list. We create a new head, point
  134. * the frag_list of that to the old head, and
  135. * then are able to use the old head->next for
  136. * appending to the message.
  137. */
  138. if (WARN_ON(head->next)) {
  139. desc->error = -EINVAL;
  140. return 0;
  141. }
  142. skb = alloc_skb(0, GFP_ATOMIC);
  143. if (!skb) {
  144. STRP_STATS_INCR(strp->stats.mem_fail);
  145. desc->error = -ENOMEM;
  146. return 0;
  147. }
  148. skb->len = head->len;
  149. skb->data_len = head->len;
  150. skb->truesize = head->truesize;
  151. *_strp_msg(skb) = *_strp_msg(head);
  152. strp->skb_nextp = &head->next;
  153. skb_shinfo(skb)->frag_list = head;
  154. strp->skb_head = skb;
  155. head = skb;
  156. } else {
  157. strp->skb_nextp =
  158. &skb_shinfo(head)->frag_list;
  159. }
  160. }
  161. }
  162. while (eaten < orig_len) {
  163. /* Always clone since we will consume something */
  164. skb = skb_clone(orig_skb, GFP_ATOMIC);
  165. if (!skb) {
  166. STRP_STATS_INCR(strp->stats.mem_fail);
  167. desc->error = -ENOMEM;
  168. break;
  169. }
  170. cand_len = orig_len - eaten;
  171. head = strp->skb_head;
  172. if (!head) {
  173. head = skb;
  174. strp->skb_head = head;
  175. /* Will set skb_nextp on next packet if needed */
  176. strp->skb_nextp = NULL;
  177. stm = _strp_msg(head);
  178. memset(stm, 0, sizeof(*stm));
  179. stm->strp.offset = orig_offset + eaten;
  180. } else {
  181. /* Unclone if we are appending to an skb that we
  182. * already share a frag_list with.
  183. */
  184. if (skb_has_frag_list(skb)) {
  185. err = skb_unclone(skb, GFP_ATOMIC);
  186. if (err) {
  187. STRP_STATS_INCR(strp->stats.mem_fail);
  188. desc->error = err;
  189. break;
  190. }
  191. }
  192. stm = _strp_msg(head);
  193. *strp->skb_nextp = skb;
  194. strp->skb_nextp = &skb->next;
  195. head->data_len += skb->len;
  196. head->len += skb->len;
  197. head->truesize += skb->truesize;
  198. }
  199. if (!stm->strp.full_len) {
  200. ssize_t len;
  201. len = (*strp->cb.parse_msg)(strp, head);
  202. if (!len) {
  203. /* Need more header to determine length */
  204. if (!stm->accum_len) {
  205. /* Start RX timer for new message */
  206. strp_start_timer(strp, timeo);
  207. }
  208. stm->accum_len += cand_len;
  209. eaten += cand_len;
  210. STRP_STATS_INCR(strp->stats.need_more_hdr);
  211. WARN_ON(eaten != orig_len);
  212. break;
  213. } else if (len < 0) {
  214. if (len == -ESTRPIPE && stm->accum_len) {
  215. len = -ENODATA;
  216. strp->unrecov_intr = 1;
  217. } else {
  218. strp->interrupted = 1;
  219. }
  220. strp_parser_err(strp, len, desc);
  221. break;
  222. } else if (len > max_msg_size) {
  223. /* Message length exceeds maximum allowed */
  224. STRP_STATS_INCR(strp->stats.msg_too_big);
  225. strp_parser_err(strp, -EMSGSIZE, desc);
  226. break;
  227. } else if (len <= (ssize_t)head->len -
  228. skb->len - stm->strp.offset) {
  229. /* Length must be into new skb (and also
  230. * greater than zero)
  231. */
  232. STRP_STATS_INCR(strp->stats.bad_hdr_len);
  233. strp_parser_err(strp, -EPROTO, desc);
  234. break;
  235. }
  236. stm->strp.full_len = len;
  237. }
  238. extra = (ssize_t)(stm->accum_len + cand_len) -
  239. stm->strp.full_len;
  240. if (extra < 0) {
  241. /* Message not complete yet. */
  242. if (stm->strp.full_len - stm->accum_len >
  243. strp_peek_len(strp)) {
  244. /* Don't have the whole message in the socket
  245. * buffer. Set strp->need_bytes to wait for
  246. * the rest of the message. Also, set "early
  247. * eaten" since we've already buffered the skb
  248. * but don't consume yet per strp_read_sock.
  249. */
  250. if (!stm->accum_len) {
  251. /* Start RX timer for new message */
  252. strp_start_timer(strp, timeo);
  253. }
  254. stm->accum_len += cand_len;
  255. eaten += cand_len;
  256. strp->need_bytes = stm->strp.full_len -
  257. stm->accum_len;
  258. STRP_STATS_ADD(strp->stats.bytes, cand_len);
  259. desc->count = 0; /* Stop reading socket */
  260. break;
  261. }
  262. stm->accum_len += cand_len;
  263. eaten += cand_len;
  264. WARN_ON(eaten != orig_len);
  265. break;
  266. }
  267. /* Positive extra indicates ore bytes than needed for the
  268. * message
  269. */
  270. WARN_ON(extra > cand_len);
  271. eaten += (cand_len - extra);
  272. /* Hurray, we have a new message! */
  273. cancel_delayed_work(&strp->msg_timer_work);
  274. strp->skb_head = NULL;
  275. strp->need_bytes = 0;
  276. STRP_STATS_INCR(strp->stats.msgs);
  277. /* Give skb to upper layer */
  278. strp->cb.rcv_msg(strp, head);
  279. if (unlikely(strp->paused)) {
  280. /* Upper layer paused strp */
  281. break;
  282. }
  283. }
  284. if (cloned_orig)
  285. kfree_skb(orig_skb);
  286. STRP_STATS_ADD(strp->stats.bytes, eaten);
  287. return eaten;
  288. }
  289. int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
  290. unsigned int orig_offset, size_t orig_len,
  291. size_t max_msg_size, long timeo)
  292. {
  293. read_descriptor_t desc; /* Dummy arg to strp_recv */
  294. desc.arg.data = strp;
  295. return __strp_recv(&desc, orig_skb, orig_offset, orig_len,
  296. max_msg_size, timeo);
  297. }
  298. EXPORT_SYMBOL_GPL(strp_process);
  299. static int strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
  300. unsigned int orig_offset, size_t orig_len)
  301. {
  302. struct strparser *strp = (struct strparser *)desc->arg.data;
  303. return __strp_recv(desc, orig_skb, orig_offset, orig_len,
  304. strp->sk->sk_rcvbuf, strp->sk->sk_rcvtimeo);
  305. }
  306. static int default_read_sock_done(struct strparser *strp, int err)
  307. {
  308. return err;
  309. }
  310. /* Called with lock held on lower socket */
  311. static int strp_read_sock(struct strparser *strp)
  312. {
  313. struct socket *sock = strp->sk->sk_socket;
  314. read_descriptor_t desc;
  315. if (unlikely(!sock || !sock->ops || !sock->ops->read_sock))
  316. return -EBUSY;
  317. desc.arg.data = strp;
  318. desc.error = 0;
  319. desc.count = 1; /* give more than one skb per call */
  320. /* sk should be locked here, so okay to do read_sock */
  321. sock->ops->read_sock(strp->sk, &desc, strp_recv);
  322. desc.error = strp->cb.read_sock_done(strp, desc.error);
  323. return desc.error;
  324. }
  325. /* Lower sock lock held */
  326. void strp_data_ready(struct strparser *strp)
  327. {
  328. if (unlikely(strp->stopped) || strp->paused)
  329. return;
  330. /* This check is needed to synchronize with do_strp_work.
  331. * do_strp_work acquires a process lock (lock_sock) whereas
  332. * the lock held here is bh_lock_sock. The two locks can be
  333. * held by different threads at the same time, but bh_lock_sock
  334. * allows a thread in BH context to safely check if the process
  335. * lock is held. In this case, if the lock is held, queue work.
  336. */
  337. if (sock_owned_by_user_nocheck(strp->sk)) {
  338. queue_work(strp_wq, &strp->work);
  339. return;
  340. }
  341. if (strp->need_bytes) {
  342. if (strp_peek_len(strp) < strp->need_bytes)
  343. return;
  344. }
  345. if (strp_read_sock(strp) == -ENOMEM)
  346. queue_work(strp_wq, &strp->work);
  347. }
  348. EXPORT_SYMBOL_GPL(strp_data_ready);
  349. static void do_strp_work(struct strparser *strp)
  350. {
  351. /* We need the read lock to synchronize with strp_data_ready. We
  352. * need the socket lock for calling strp_read_sock.
  353. */
  354. strp->cb.lock(strp);
  355. if (unlikely(strp->stopped))
  356. goto out;
  357. if (strp->paused)
  358. goto out;
  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. /* Sock process lock held (lock_sock) */
  419. void __strp_unpause(struct strparser *strp)
  420. {
  421. strp->paused = 0;
  422. if (strp->need_bytes) {
  423. if (strp_peek_len(strp) < strp->need_bytes)
  424. return;
  425. }
  426. strp_read_sock(strp);
  427. }
  428. EXPORT_SYMBOL_GPL(__strp_unpause);
  429. void strp_unpause(struct strparser *strp)
  430. {
  431. strp->paused = 0;
  432. /* Sync setting paused with RX work */
  433. smp_mb();
  434. queue_work(strp_wq, &strp->work);
  435. }
  436. EXPORT_SYMBOL_GPL(strp_unpause);
  437. /* strp must already be stopped so that strp_recv will no longer be called.
  438. * Note that strp_done is not called with the lower socket held.
  439. */
  440. void strp_done(struct strparser *strp)
  441. {
  442. WARN_ON(!strp->stopped);
  443. cancel_delayed_work_sync(&strp->msg_timer_work);
  444. cancel_work_sync(&strp->work);
  445. if (strp->skb_head) {
  446. kfree_skb(strp->skb_head);
  447. strp->skb_head = NULL;
  448. }
  449. }
  450. EXPORT_SYMBOL_GPL(strp_done);
  451. void strp_stop(struct strparser *strp)
  452. {
  453. strp->stopped = 1;
  454. }
  455. EXPORT_SYMBOL_GPL(strp_stop);
  456. void strp_check_rcv(struct strparser *strp)
  457. {
  458. queue_work(strp_wq, &strp->work);
  459. }
  460. EXPORT_SYMBOL_GPL(strp_check_rcv);
  461. static int __init strp_mod_init(void)
  462. {
  463. strp_wq = create_singlethread_workqueue("kstrp");
  464. return 0;
  465. }
  466. static void __exit strp_mod_exit(void)
  467. {
  468. destroy_workqueue(strp_wq);
  469. }
  470. module_init(strp_mod_init);
  471. module_exit(strp_mod_exit);
  472. MODULE_LICENSE("GPL");