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