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