Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 1 | /* $OpenBSD: roaming_common.c,v 1.12 2014/01/09 23:20:00 djm Exp $ */ |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2004-2009 AppGate Network Security AB |
| 4 | * |
| 5 | * Permission to use, copy, modify, and distribute this software for any |
| 6 | * purpose with or without fee is hereby granted, provided that the above |
| 7 | * copyright notice and this permission notice appear in all copies. |
| 8 | * |
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 16 | */ |
| 17 | |
Darren Tucker | 828c96d | 2009-06-21 22:22:08 +1000 | [diff] [blame] | 18 | #include "includes.h" |
| 19 | |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <sys/socket.h> |
| 22 | #include <sys/uio.h> |
| 23 | |
| 24 | #include <errno.h> |
Darren Tucker | 828c96d | 2009-06-21 22:22:08 +1000 | [diff] [blame] | 25 | #ifdef HAVE_INTTYPES_H |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 26 | #include <inttypes.h> |
Darren Tucker | 828c96d | 2009-06-21 22:22:08 +1000 | [diff] [blame] | 27 | #endif |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 28 | #include <stdarg.h> |
Darren Tucker | 466df21 | 2009-07-06 07:11:52 +1000 | [diff] [blame] | 29 | #include <string.h> |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 30 | #include <unistd.h> |
| 31 | |
| 32 | #include "atomicio.h" |
| 33 | #include "log.h" |
| 34 | #include "packet.h" |
| 35 | #include "xmalloc.h" |
| 36 | #include "cipher.h" |
| 37 | #include "buffer.h" |
| 38 | #include "roaming.h" |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 39 | #include "digest.h" |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 40 | |
Darren Tucker | 466df21 | 2009-07-06 07:11:52 +1000 | [diff] [blame] | 41 | static size_t out_buf_size = 0; |
| 42 | static char *out_buf = NULL; |
| 43 | static size_t out_start; |
| 44 | static size_t out_last; |
| 45 | |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 46 | static u_int64_t write_bytes = 0; |
| 47 | static u_int64_t read_bytes = 0; |
| 48 | |
Darren Tucker | 466df21 | 2009-07-06 07:11:52 +1000 | [diff] [blame] | 49 | int roaming_enabled = 0; |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 50 | int resume_in_progress = 0; |
| 51 | |
Darren Tucker | 466df21 | 2009-07-06 07:11:52 +1000 | [diff] [blame] | 52 | int |
Damien Miller | ca67a7e | 2013-11-04 09:05:17 +1100 | [diff] [blame] | 53 | get_snd_buf_size(void) |
Darren Tucker | 466df21 | 2009-07-06 07:11:52 +1000 | [diff] [blame] | 54 | { |
| 55 | int fd = packet_get_connection_out(); |
Darren Tucker | 75694db | 2010-01-08 18:51:14 +1100 | [diff] [blame] | 56 | int optval; |
| 57 | socklen_t optvallen = sizeof(optval); |
Darren Tucker | 466df21 | 2009-07-06 07:11:52 +1000 | [diff] [blame] | 58 | |
Darren Tucker | 466df21 | 2009-07-06 07:11:52 +1000 | [diff] [blame] | 59 | if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &optval, &optvallen) != 0) |
| 60 | optval = DEFAULT_ROAMBUF; |
| 61 | return optval; |
| 62 | } |
| 63 | |
| 64 | int |
Damien Miller | ca67a7e | 2013-11-04 09:05:17 +1100 | [diff] [blame] | 65 | get_recv_buf_size(void) |
Darren Tucker | 466df21 | 2009-07-06 07:11:52 +1000 | [diff] [blame] | 66 | { |
| 67 | int fd = packet_get_connection_in(); |
Darren Tucker | 75694db | 2010-01-08 18:51:14 +1100 | [diff] [blame] | 68 | int optval; |
| 69 | socklen_t optvallen = sizeof(optval); |
Darren Tucker | 466df21 | 2009-07-06 07:11:52 +1000 | [diff] [blame] | 70 | |
Darren Tucker | 466df21 | 2009-07-06 07:11:52 +1000 | [diff] [blame] | 71 | if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &optval, &optvallen) != 0) |
| 72 | optval = DEFAULT_ROAMBUF; |
| 73 | return optval; |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | set_out_buffer_size(size_t size) |
| 78 | { |
Damien Miller | 8ed4de8 | 2011-12-19 10:52:50 +1100 | [diff] [blame] | 79 | if (size == 0 || size > MAX_ROAMBUF) |
| 80 | fatal("%s: bad buffer size %lu", __func__, (u_long)size); |
Darren Tucker | 466df21 | 2009-07-06 07:11:52 +1000 | [diff] [blame] | 81 | /* |
| 82 | * The buffer size can only be set once and the buffer will live |
| 83 | * as long as the session lives. |
| 84 | */ |
| 85 | if (out_buf == NULL) { |
| 86 | out_buf_size = size; |
| 87 | out_buf = xmalloc(size); |
| 88 | out_start = 0; |
| 89 | out_last = 0; |
| 90 | } |
| 91 | } |
| 92 | |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 93 | u_int64_t |
| 94 | get_recv_bytes(void) |
| 95 | { |
| 96 | return read_bytes; |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | add_recv_bytes(u_int64_t num) |
| 101 | { |
| 102 | read_bytes += num; |
| 103 | } |
| 104 | |
| 105 | u_int64_t |
| 106 | get_sent_bytes(void) |
| 107 | { |
| 108 | return write_bytes; |
| 109 | } |
| 110 | |
| 111 | void |
Darren Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame] | 112 | roam_set_bytes(u_int64_t sent, u_int64_t recvd) |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 113 | { |
Darren Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame] | 114 | read_bytes = recvd; |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 115 | write_bytes = sent; |
| 116 | } |
| 117 | |
Darren Tucker | 466df21 | 2009-07-06 07:11:52 +1000 | [diff] [blame] | 118 | static void |
| 119 | buf_append(const char *buf, size_t count) |
| 120 | { |
| 121 | if (count > out_buf_size) { |
| 122 | buf += count - out_buf_size; |
| 123 | count = out_buf_size; |
| 124 | } |
| 125 | if (count < out_buf_size - out_last) { |
| 126 | memcpy(out_buf + out_last, buf, count); |
| 127 | if (out_start > out_last) |
| 128 | out_start += count; |
| 129 | out_last += count; |
| 130 | } else { |
| 131 | /* data will wrap */ |
| 132 | size_t chunk = out_buf_size - out_last; |
| 133 | memcpy(out_buf + out_last, buf, chunk); |
| 134 | memcpy(out_buf, buf + chunk, count - chunk); |
| 135 | out_last = count - chunk; |
| 136 | out_start = out_last + 1; |
| 137 | } |
| 138 | } |
| 139 | |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 140 | ssize_t |
| 141 | roaming_write(int fd, const void *buf, size_t count, int *cont) |
| 142 | { |
| 143 | ssize_t ret; |
| 144 | |
| 145 | ret = write(fd, buf, count); |
| 146 | if (ret > 0 && !resume_in_progress) { |
| 147 | write_bytes += ret; |
Darren Tucker | 466df21 | 2009-07-06 07:11:52 +1000 | [diff] [blame] | 148 | if (out_buf_size > 0) |
| 149 | buf_append(buf, ret); |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 150 | } |
Darren Tucker | e730118 | 2010-01-08 16:53:31 +1100 | [diff] [blame] | 151 | if (out_buf_size > 0 && |
| 152 | (ret == 0 || (ret == -1 && errno == EPIPE))) { |
| 153 | if (wait_for_roaming_reconnect() != 0) { |
| 154 | ret = 0; |
| 155 | *cont = 1; |
| 156 | } else { |
| 157 | ret = -1; |
| 158 | errno = EAGAIN; |
| 159 | } |
| 160 | } |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | ssize_t |
| 165 | roaming_read(int fd, void *buf, size_t count, int *cont) |
| 166 | { |
| 167 | ssize_t ret = read(fd, buf, count); |
| 168 | if (ret > 0) { |
| 169 | if (!resume_in_progress) { |
| 170 | read_bytes += ret; |
| 171 | } |
Darren Tucker | e730118 | 2010-01-08 16:53:31 +1100 | [diff] [blame] | 172 | } else if (out_buf_size > 0 && |
| 173 | (ret == 0 || (ret == -1 && (errno == ECONNRESET |
| 174 | || errno == ECONNABORTED || errno == ETIMEDOUT |
| 175 | || errno == EHOSTUNREACH)))) { |
| 176 | debug("roaming_read failed for %d ret=%ld errno=%d", |
| 177 | fd, (long)ret, errno); |
| 178 | ret = 0; |
| 179 | if (wait_for_roaming_reconnect() == 0) |
| 180 | *cont = 1; |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 181 | } |
| 182 | return ret; |
| 183 | } |
| 184 | |
Darren Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame] | 185 | size_t |
| 186 | roaming_atomicio(ssize_t(*f)(int, void*, size_t), int fd, void *buf, |
| 187 | size_t count) |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 188 | { |
Darren Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame] | 189 | size_t ret = atomicio(f, fd, buf, count); |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 190 | |
Darren Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame] | 191 | if (f == vwrite && ret > 0 && !resume_in_progress) { |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 192 | write_bytes += ret; |
| 193 | } else if (f == read && ret > 0 && !resume_in_progress) { |
| 194 | read_bytes += ret; |
| 195 | } |
| 196 | return ret; |
| 197 | } |
Darren Tucker | 466df21 | 2009-07-06 07:11:52 +1000 | [diff] [blame] | 198 | |
| 199 | void |
| 200 | resend_bytes(int fd, u_int64_t *offset) |
| 201 | { |
| 202 | size_t available, needed; |
| 203 | |
| 204 | if (out_start < out_last) |
| 205 | available = out_last - out_start; |
| 206 | else |
| 207 | available = out_buf_size; |
| 208 | needed = write_bytes - *offset; |
| 209 | debug3("resend_bytes: resend %lu bytes from %llu", |
| 210 | (unsigned long)needed, (unsigned long long)*offset); |
| 211 | if (needed > available) |
| 212 | fatal("Needed to resend more data than in the cache"); |
| 213 | if (out_last < needed) { |
| 214 | int chunkend = needed - out_last; |
| 215 | atomicio(vwrite, fd, out_buf + out_buf_size - chunkend, |
| 216 | chunkend); |
| 217 | atomicio(vwrite, fd, out_buf, out_last); |
| 218 | } else { |
| 219 | atomicio(vwrite, fd, out_buf + (out_last - needed), needed); |
| 220 | } |
| 221 | } |
Darren Tucker | e730118 | 2010-01-08 16:53:31 +1100 | [diff] [blame] | 222 | |
| 223 | /* |
| 224 | * Caclulate a new key after a reconnect |
| 225 | */ |
| 226 | void |
| 227 | calculate_new_key(u_int64_t *key, u_int64_t cookie, u_int64_t challenge) |
| 228 | { |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 229 | u_char hash[SSH_DIGEST_MAX_LENGTH]; |
Darren Tucker | e730118 | 2010-01-08 16:53:31 +1100 | [diff] [blame] | 230 | Buffer b; |
| 231 | |
| 232 | buffer_init(&b); |
| 233 | buffer_put_int64(&b, *key); |
| 234 | buffer_put_int64(&b, cookie); |
| 235 | buffer_put_int64(&b, challenge); |
| 236 | |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 237 | if (ssh_digest_buffer(SSH_DIGEST_SHA1, &b, hash, sizeof(hash)) != 0) |
| 238 | fatal("%s: digest_buffer failed", __func__); |
Darren Tucker | e730118 | 2010-01-08 16:53:31 +1100 | [diff] [blame] | 239 | |
| 240 | buffer_clear(&b); |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 241 | buffer_append(&b, hash, ssh_digest_bytes(SSH_DIGEST_SHA1)); |
Darren Tucker | e730118 | 2010-01-08 16:53:31 +1100 | [diff] [blame] | 242 | *key = buffer_get_int64(&b); |
| 243 | buffer_free(&b); |
| 244 | } |