nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 8a9fd52 | 2016-03-25 17:09:29 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/transport/chttp2/frame_goaway.h" |
| 35 | #include "src/core/lib/transport/chttp2/internal.h" |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 36 | |
| 37 | #include <string.h> |
| 38 | |
| 39 | #include <grpc/support/alloc.h> |
| 40 | #include <grpc/support/log.h> |
| 41 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 42 | void grpc_chttp2_goaway_parser_init(grpc_chttp2_goaway_parser *p) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 43 | p->debug_data = NULL; |
| 44 | } |
| 45 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 46 | void grpc_chttp2_goaway_parser_destroy(grpc_chttp2_goaway_parser *p) { |
| 47 | gpr_free(p->debug_data); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 50 | grpc_chttp2_parse_error grpc_chttp2_goaway_parser_begin_frame( |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 51 | grpc_chttp2_goaway_parser *p, uint32_t length, uint8_t flags) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 52 | if (length < 8) { |
| 53 | gpr_log(GPR_ERROR, "goaway frame too short (%d bytes)", length); |
| 54 | return GRPC_CHTTP2_CONNECTION_ERROR; |
| 55 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 56 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 57 | gpr_free(p->debug_data); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 58 | p->debug_length = length - 8; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 59 | p->debug_data = gpr_malloc(p->debug_length); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 60 | p->debug_pos = 0; |
| 61 | p->state = GRPC_CHTTP2_GOAWAY_LSI0; |
| 62 | return GRPC_CHTTP2_PARSE_OK; |
| 63 | } |
| 64 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 65 | grpc_chttp2_parse_error grpc_chttp2_goaway_parser_parse( |
| 66 | grpc_exec_ctx *exec_ctx, void *parser, |
| 67 | grpc_chttp2_transport_parsing *transport_parsing, |
| 68 | grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 69 | uint8_t *const beg = GPR_SLICE_START_PTR(slice); |
| 70 | uint8_t *const end = GPR_SLICE_END_PTR(slice); |
| 71 | uint8_t *cur = beg; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 72 | grpc_chttp2_goaway_parser *p = parser; |
| 73 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 74 | switch (p->state) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 75 | case GRPC_CHTTP2_GOAWAY_LSI0: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 76 | if (cur == end) { |
| 77 | p->state = GRPC_CHTTP2_GOAWAY_LSI0; |
| 78 | return GRPC_CHTTP2_PARSE_OK; |
| 79 | } |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 80 | p->last_stream_id = ((uint32_t)*cur) << 24; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 81 | ++cur; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | /* fallthrough */ |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 83 | case GRPC_CHTTP2_GOAWAY_LSI1: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 84 | if (cur == end) { |
| 85 | p->state = GRPC_CHTTP2_GOAWAY_LSI1; |
| 86 | return GRPC_CHTTP2_PARSE_OK; |
| 87 | } |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 88 | p->last_stream_id |= ((uint32_t)*cur) << 16; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 89 | ++cur; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 90 | /* fallthrough */ |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 91 | case GRPC_CHTTP2_GOAWAY_LSI2: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 92 | if (cur == end) { |
| 93 | p->state = GRPC_CHTTP2_GOAWAY_LSI2; |
| 94 | return GRPC_CHTTP2_PARSE_OK; |
| 95 | } |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 96 | p->last_stream_id |= ((uint32_t)*cur) << 8; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 97 | ++cur; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 98 | /* fallthrough */ |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 99 | case GRPC_CHTTP2_GOAWAY_LSI3: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 100 | if (cur == end) { |
| 101 | p->state = GRPC_CHTTP2_GOAWAY_LSI3; |
| 102 | return GRPC_CHTTP2_PARSE_OK; |
| 103 | } |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 104 | p->last_stream_id |= ((uint32_t)*cur); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 105 | ++cur; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 106 | /* fallthrough */ |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 107 | case GRPC_CHTTP2_GOAWAY_ERR0: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 108 | if (cur == end) { |
| 109 | p->state = GRPC_CHTTP2_GOAWAY_ERR0; |
| 110 | return GRPC_CHTTP2_PARSE_OK; |
| 111 | } |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 112 | p->error_code = ((uint32_t)*cur) << 24; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 113 | ++cur; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 114 | /* fallthrough */ |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 115 | case GRPC_CHTTP2_GOAWAY_ERR1: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 116 | if (cur == end) { |
| 117 | p->state = GRPC_CHTTP2_GOAWAY_ERR1; |
| 118 | return GRPC_CHTTP2_PARSE_OK; |
| 119 | } |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 120 | p->error_code |= ((uint32_t)*cur) << 16; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 121 | ++cur; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 122 | /* fallthrough */ |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 123 | case GRPC_CHTTP2_GOAWAY_ERR2: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 124 | if (cur == end) { |
| 125 | p->state = GRPC_CHTTP2_GOAWAY_ERR2; |
| 126 | return GRPC_CHTTP2_PARSE_OK; |
| 127 | } |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 128 | p->error_code |= ((uint32_t)*cur) << 8; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 129 | ++cur; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 130 | /* fallthrough */ |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 131 | case GRPC_CHTTP2_GOAWAY_ERR3: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 132 | if (cur == end) { |
| 133 | p->state = GRPC_CHTTP2_GOAWAY_ERR3; |
| 134 | return GRPC_CHTTP2_PARSE_OK; |
| 135 | } |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 136 | p->error_code |= ((uint32_t)*cur); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 137 | ++cur; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 138 | /* fallthrough */ |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 139 | case GRPC_CHTTP2_GOAWAY_DEBUG: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 140 | memcpy(p->debug_data + p->debug_pos, cur, (size_t)(end - cur)); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 141 | GPR_ASSERT((size_t)(end - cur) < UINT32_MAX - p->debug_pos); |
| 142 | p->debug_pos += (uint32_t)(end - cur); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 143 | p->state = GRPC_CHTTP2_GOAWAY_DEBUG; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 144 | if (is_last) { |
| 145 | transport_parsing->goaway_received = 1; |
| 146 | transport_parsing->goaway_last_stream_index = p->last_stream_id; |
| 147 | gpr_slice_unref(transport_parsing->goaway_text); |
| 148 | transport_parsing->goaway_error = (grpc_status_code)p->error_code; |
| 149 | transport_parsing->goaway_text = |
| 150 | gpr_slice_new(p->debug_data, p->debug_length, gpr_free); |
| 151 | p->debug_data = NULL; |
| 152 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 153 | return GRPC_CHTTP2_PARSE_OK; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 154 | } |
yang-g | b063c87 | 2015-10-07 11:40:13 -0700 | [diff] [blame] | 155 | GPR_UNREACHABLE_CODE(return GRPC_CHTTP2_CONNECTION_ERROR); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 158 | void grpc_chttp2_goaway_append(uint32_t last_stream_id, uint32_t error_code, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | gpr_slice debug_data, |
| 160 | gpr_slice_buffer *slice_buffer) { |
| 161 | gpr_slice header = gpr_slice_malloc(9 + 4 + 4); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 162 | uint8_t *p = GPR_SLICE_START_PTR(header); |
| 163 | uint32_t frame_length; |
| 164 | GPR_ASSERT(GPR_SLICE_LENGTH(debug_data) < UINT32_MAX - 4 - 4); |
| 165 | frame_length = 4 + 4 + (uint32_t)GPR_SLICE_LENGTH(debug_data); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 166 | |
| 167 | /* frame header: length */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 168 | *p++ = (uint8_t)(frame_length >> 16); |
| 169 | *p++ = (uint8_t)(frame_length >> 8); |
| 170 | *p++ = (uint8_t)(frame_length); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 171 | /* frame header: type */ |
| 172 | *p++ = GRPC_CHTTP2_FRAME_GOAWAY; |
| 173 | /* frame header: flags */ |
| 174 | *p++ = 0; |
| 175 | /* frame header: stream id */ |
| 176 | *p++ = 0; |
| 177 | *p++ = 0; |
| 178 | *p++ = 0; |
| 179 | *p++ = 0; |
| 180 | /* payload: last stream id */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 181 | *p++ = (uint8_t)(last_stream_id >> 24); |
| 182 | *p++ = (uint8_t)(last_stream_id >> 16); |
| 183 | *p++ = (uint8_t)(last_stream_id >> 8); |
| 184 | *p++ = (uint8_t)(last_stream_id); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 185 | /* payload: error code */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 186 | *p++ = (uint8_t)(error_code >> 24); |
| 187 | *p++ = (uint8_t)(error_code >> 16); |
| 188 | *p++ = (uint8_t)(error_code >> 8); |
| 189 | *p++ = (uint8_t)(error_code); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 190 | GPR_ASSERT(p == GPR_SLICE_END_PTR(header)); |
| 191 | gpr_slice_buffer_add(slice_buffer, header); |
| 192 | gpr_slice_buffer_add(slice_buffer, debug_data); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 193 | } |