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