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