| 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * Copyright (c) 2012-2014 Cisco Systems |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include "qemu/osdep.h" |
| 27 | #include <linux/ip.h> |
| 28 | #include <netdb.h> |
| 29 | #include "net/net.h" |
| 30 | #include "clients.h" |
| 31 | #include "qapi/error.h" |
| 32 | #include "qemu/bswap.h" |
| 33 | #include "qemu/error-report.h" |
| 34 | #include "qemu/option.h" |
| 35 | #include "qemu/sockets.h" |
| 36 | #include "qemu/iov.h" |
| 37 | #include "qemu/main-loop.h" |
| 38 | #include "qemu/memalign.h" |
| 39 | |
| 40 | /* The buffer size needs to be investigated for optimum numbers and |
| 41 | * optimum means of paging in on different systems. This size is |
| 42 | * chosen to be sufficient to accommodate one packet with some headers |
| 43 | */ |
| 44 | |
| 45 | #define BUFFER_ALIGN sysconf(_SC_PAGESIZE) |
| 46 | #define BUFFER_SIZE 16384 |
| 47 | #define IOVSIZE 2 |
| 48 | #define MAX_L2TPV3_MSGCNT 64 |
| 49 | #define MAX_L2TPV3_IOVCNT (MAX_L2TPV3_MSGCNT * IOVSIZE) |
| 50 | |
| 51 | /* Header set to 0x30000 signifies a data packet */ |
| 52 | |
| 53 | #define L2TPV3_DATA_PACKET 0x30000 |
| 54 | |
| 55 | /* IANA-assigned IP protocol ID for L2TPv3 */ |
| 56 | |
| 57 | #ifndef IPPROTO_L2TP |
| 58 | #define IPPROTO_L2TP 0x73 |
| 59 | #endif |
| 60 | |
| 61 | typedef struct NetL2TPV3State { |
| 62 | NetClientState nc; |
| 63 | int fd; |
| 64 | |
| 65 | /* |
| 66 | * these are used for xmit - that happens packet a time |
| 67 | * and for first sign of life packet (easier to parse that once) |
| 68 | */ |
| 69 | |
| 70 | uint8_t *header_buf; |
| 71 | struct iovec *vec; |
| 72 | |
| 73 | /* |
| 74 | * these are used for receive - try to "eat" up to 32 packets at a time |
| 75 | */ |
| 76 | |
| 77 | struct mmsghdr *msgvec; |
| 78 | |
| 79 | /* |
| 80 | * peer address |
| 81 | */ |
| 82 | |
| 83 | struct sockaddr_storage *dgram_dst; |
| 84 | uint32_t dst_size; |
| 85 | |
| 86 | /* |
| 87 | * L2TPv3 parameters |
| 88 | */ |
| 89 | |
| 90 | uint64_t rx_cookie; |
| 91 | uint64_t tx_cookie; |
| 92 | uint32_t rx_session; |
| 93 | uint32_t tx_session; |
| 94 | uint32_t header_size; |
| 95 | uint32_t counter; |
| 96 | |
| 97 | /* |
| 98 | * DOS avoidance in error handling |
| 99 | */ |
| 100 | |
| 101 | bool header_mismatch; |
| 102 | |
| 103 | /* |
| 104 | * Ring buffer handling |
| 105 | */ |
| 106 | |
| 107 | int queue_head; |
| 108 | int queue_tail; |
| 109 | int queue_depth; |
| 110 | |
| 111 | /* |
| 112 | * Precomputed offsets |
| 113 | */ |
| 114 | |
| 115 | uint32_t offset; |
| 116 | uint32_t cookie_offset; |
| 117 | uint32_t counter_offset; |
| 118 | uint32_t session_offset; |
| 119 | |
| 120 | /* Poll Control */ |
| 121 | |
| 122 | bool read_poll; |
| 123 | bool write_poll; |
| 124 | |
| 125 | /* Flags */ |
| 126 | |
| 127 | bool ipv6; |
| 128 | bool udp; |
| 129 | bool has_counter; |
| 130 | bool pin_counter; |
| 131 | bool cookie; |
| 132 | bool cookie_is_64; |
| 133 | |
| 134 | } NetL2TPV3State; |
| 135 | |
| 136 | static void net_l2tpv3_send(void *opaque); |
| 137 | static void l2tpv3_writable(void *opaque); |
| 138 | |
| 139 | static void l2tpv3_update_fd_handler(NetL2TPV3State *s) |
| 140 | { |
| 141 | qemu_set_fd_handler(s->fd, |
| 142 | s->read_poll ? net_l2tpv3_send : NULL, |
| 143 | s->write_poll ? l2tpv3_writable : NULL, |
| 144 | s); |
| 145 | } |
| 146 | |
| 147 | static void l2tpv3_read_poll(NetL2TPV3State *s, bool enable) |
| 148 | { |
| 149 | if (s->read_poll != enable) { |
| 150 | s->read_poll = enable; |
| 151 | l2tpv3_update_fd_handler(s); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | static void l2tpv3_write_poll(NetL2TPV3State *s, bool enable) |
| 156 | { |
| 157 | if (s->write_poll != enable) { |
| 158 | s->write_poll = enable; |
| 159 | l2tpv3_update_fd_handler(s); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | static void l2tpv3_writable(void *opaque) |
| 164 | { |
| 165 | NetL2TPV3State *s = opaque; |
| 166 | l2tpv3_write_poll(s, false); |
| 167 | qemu_flush_queued_packets(&s->nc); |
| 168 | } |
| 169 | |
| 170 | static void l2tpv3_send_completed(NetClientState *nc, ssize_t len) |
| 171 | { |
| 172 | NetL2TPV3State *s = DO_UPCAST(NetL2TPV3State, nc, nc); |
| 173 | l2tpv3_read_poll(s, true); |
| 174 | } |
| 175 | |
| 176 | static void l2tpv3_poll(NetClientState *nc, bool enable) |
| 177 | { |
| 178 | NetL2TPV3State *s = DO_UPCAST(NetL2TPV3State, nc, nc); |
| 179 | l2tpv3_write_poll(s, enable); |
| 180 | l2tpv3_read_poll(s, enable); |
| 181 | } |
| 182 | |
| 183 | static void l2tpv3_form_header(NetL2TPV3State *s) |
| 184 | { |
| 185 | uint32_t *counter; |
| 186 | |
| 187 | if (s->udp) { |
| 188 | stl_be_p((uint32_t *) s->header_buf, L2TPV3_DATA_PACKET); |
| 189 | } |
| 190 | stl_be_p( |
| 191 | (uint32_t *) (s->header_buf + s->session_offset), |
| 192 | s->tx_session |
| 193 | ); |
| 194 | if (s->cookie) { |
| 195 | if (s->cookie_is_64) { |
| 196 | stq_be_p( |
| 197 | (uint64_t *)(s->header_buf + s->cookie_offset), |
| 198 | s->tx_cookie |
| 199 | ); |
| 200 | } else { |
| 201 | stl_be_p( |
| 202 | (uint32_t *) (s->header_buf + s->cookie_offset), |
| 203 | s->tx_cookie |
| 204 | ); |
| 205 | } |
| 206 | } |
| 207 | if (s->has_counter) { |
| 208 | counter = (uint32_t *)(s->header_buf + s->counter_offset); |
| 209 | if (s->pin_counter) { |
| 210 | *counter = 0; |
| 211 | } else { |
| 212 | stl_be_p(counter, ++s->counter); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static ssize_t net_l2tpv3_receive_dgram_iov(NetClientState *nc, |
| 218 | const struct iovec *iov, |
| 219 | int iovcnt) |
| 220 | { |
| 221 | NetL2TPV3State *s = DO_UPCAST(NetL2TPV3State, nc, nc); |
| 222 | |
| 223 | struct msghdr message; |
| 224 | int ret; |
| 225 | |
| 226 | if (iovcnt > MAX_L2TPV3_IOVCNT - 1) { |
| 227 | error_report( |
| 228 | "iovec too long %d > %d, change l2tpv3.h", |
| 229 | iovcnt, MAX_L2TPV3_IOVCNT |
| 230 | ); |
| 231 | return -1; |
| 232 | } |
| 233 | l2tpv3_form_header(s); |
| 234 | memcpy(s->vec + 1, iov, iovcnt * sizeof(struct iovec)); |
| 235 | s->vec->iov_base = s->header_buf; |
| 236 | s->vec->iov_len = s->offset; |
| 237 | message.msg_name = s->dgram_dst; |
| 238 | message.msg_namelen = s->dst_size; |
| 239 | message.msg_iov = s->vec; |
| 240 | message.msg_iovlen = iovcnt + 1; |
| 241 | message.msg_control = NULL; |
| 242 | message.msg_controllen = 0; |
| 243 | message.msg_flags = 0; |
| 244 | ret = RETRY_ON_EINTR(sendmsg(s->fd, &message, 0)); |
| 245 | if (ret > 0) { |
| 246 | ret -= s->offset; |
| 247 | } else if (ret == 0) { |
| 248 | /* belt and braces - should not occur on DGRAM |
| 249 | * we should get an error and never a 0 send |
| 250 | */ |
| 251 | ret = iov_size(iov, iovcnt); |
| 252 | } else { |
| 253 | /* signal upper layer that socket buffer is full */ |
| 254 | ret = -errno; |
| 255 | if (ret == -EAGAIN || ret == -ENOBUFS) { |
| 256 | l2tpv3_write_poll(s, true); |
| 257 | ret = 0; |
| 258 | } |
| 259 | } |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | static ssize_t net_l2tpv3_receive_dgram(NetClientState *nc, |
| 264 | const uint8_t *buf, |
| 265 | size_t size) |
| 266 | { |
| 267 | NetL2TPV3State *s = DO_UPCAST(NetL2TPV3State, nc, nc); |
| 268 | |
| 269 | struct iovec *vec; |
| 270 | struct msghdr message; |
| 271 | ssize_t ret = 0; |
| 272 | |
| 273 | l2tpv3_form_header(s); |
| 274 | vec = s->vec; |
| 275 | vec->iov_base = s->header_buf; |
| 276 | vec->iov_len = s->offset; |
| 277 | vec++; |
| 278 | vec->iov_base = (void *) buf; |
| 279 | vec->iov_len = size; |
| 280 | message.msg_name = s->dgram_dst; |
| 281 | message.msg_namelen = s->dst_size; |
| 282 | message.msg_iov = s->vec; |
| 283 | message.msg_iovlen = 2; |
| 284 | message.msg_control = NULL; |
| 285 | message.msg_controllen = 0; |
| 286 | message.msg_flags = 0; |
| 287 | ret = RETRY_ON_EINTR(sendmsg(s->fd, &message, 0)); |
| 288 | if (ret > 0) { |
| 289 | ret -= s->offset; |
| 290 | } else if (ret == 0) { |
| 291 | /* belt and braces - should not occur on DGRAM |
| 292 | * we should get an error and never a 0 send |
| 293 | */ |
| 294 | ret = size; |
| 295 | } else { |
| 296 | ret = -errno; |
| 297 | if (ret == -EAGAIN || ret == -ENOBUFS) { |
| 298 | /* signal upper layer that socket buffer is full */ |
| 299 | l2tpv3_write_poll(s, true); |
| 300 | ret = 0; |
| 301 | } |
| 302 | } |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | static int l2tpv3_verify_header(NetL2TPV3State *s, uint8_t *buf) |
| 307 | { |
| 308 | |
| 309 | uint32_t *session; |
| 310 | uint64_t cookie; |
| 311 | |
| 312 | if ((!s->udp) && (!s->ipv6)) { |
| 313 | buf += sizeof(struct iphdr) /* fix for ipv4 raw */; |
| 314 | } |
| 315 | |
| 316 | /* we do not do a strict check for "data" packets as per |
| 317 | * the RFC spec because the pure IP spec does not have |
| 318 | * that anyway. |
| 319 | */ |
| 320 | |
| 321 | if (s->cookie) { |
| 322 | if (s->cookie_is_64) { |
| 323 | cookie = ldq_be_p(buf + s->cookie_offset); |
| 324 | } else { |
| 325 | cookie = ldl_be_p(buf + s->cookie_offset) & 0xffffffffULL; |
| 326 | } |
| 327 | if (cookie != s->rx_cookie) { |
| 328 | if (!s->header_mismatch) { |
| 329 | error_report("unknown cookie id"); |
| 330 | } |
| 331 | return -1; |
| 332 | } |
| 333 | } |
| 334 | session = (uint32_t *) (buf + s->session_offset); |
| 335 | if (ldl_be_p(session) != s->rx_session) { |
| 336 | if (!s->header_mismatch) { |
| 337 | error_report("session mismatch"); |
| 338 | } |
| 339 | return -1; |
| 340 | } |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static void net_l2tpv3_process_queue(NetL2TPV3State *s) |
| 345 | { |
| 346 | int size = 0; |
| 347 | struct iovec *vec; |
| 348 | bool bad_read; |
| 349 | int data_size; |
| 350 | struct mmsghdr *msgvec; |
| 351 | |
| 352 | /* go into ring mode only if there is a "pending" tail */ |
| 353 | if (s->queue_depth > 0) { |
| 354 | do { |
| 355 | msgvec = s->msgvec + s->queue_tail; |
| 356 | if (msgvec->msg_len > 0) { |
| 357 | data_size = msgvec->msg_len - s->header_size; |
| 358 | vec = msgvec->msg_hdr.msg_iov; |
| 359 | if ((data_size > 0) && |
| 360 | (l2tpv3_verify_header(s, vec->iov_base) == 0)) { |
| 361 | vec++; |
| 362 | /* Use the legacy delivery for now, we will |
| 363 | * switch to using our own ring as a queueing mechanism |
| 364 | * at a later date |
| 365 | */ |
| 366 | size = qemu_send_packet_async( |
| 367 | &s->nc, |
| 368 | vec->iov_base, |
| 369 | data_size, |
| 370 | l2tpv3_send_completed |
| 371 | ); |
| 372 | if (size == 0) { |
| 373 | l2tpv3_read_poll(s, false); |
| 374 | } |
| 375 | bad_read = false; |
| 376 | } else { |
| 377 | bad_read = true; |
| 378 | if (!s->header_mismatch) { |
| 379 | /* report error only once */ |
| 380 | error_report("l2tpv3 header verification failed"); |
| 381 | s->header_mismatch = true; |
| 382 | } |
| 383 | } |
| 384 | } else { |
| 385 | bad_read = true; |
| 386 | } |
| 387 | s->queue_tail = (s->queue_tail + 1) % MAX_L2TPV3_MSGCNT; |
| 388 | s->queue_depth--; |
| 389 | } while ( |
| 390 | (s->queue_depth > 0) && |
| 391 | qemu_can_send_packet(&s->nc) && |
| 392 | ((size > 0) || bad_read) |
| 393 | ); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | static void net_l2tpv3_send(void *opaque) |
| 398 | { |
| 399 | NetL2TPV3State *s = opaque; |
| 400 | int target_count, count; |
| 401 | struct mmsghdr *msgvec; |
| 402 | |
| 403 | /* go into ring mode only if there is a "pending" tail */ |
| 404 | |
| 405 | if (s->queue_depth) { |
| 406 | |
| 407 | /* The ring buffer we use has variable intake |
| 408 | * count of how much we can read varies - adjust accordingly |
| 409 | */ |
| 410 | |
| 411 | target_count = MAX_L2TPV3_MSGCNT - s->queue_depth; |
| 412 | |
| 413 | /* Ensure we do not overrun the ring when we have |
| 414 | * a lot of enqueued packets |
| 415 | */ |
| 416 | |
| 417 | if (s->queue_head + target_count > MAX_L2TPV3_MSGCNT) { |
| 418 | target_count = MAX_L2TPV3_MSGCNT - s->queue_head; |
| 419 | } |
| 420 | } else { |
| 421 | |
| 422 | /* we do not have any pending packets - we can use |
| 423 | * the whole message vector linearly instead of using |
| 424 | * it as a ring |
| 425 | */ |
| 426 | |
| 427 | s->queue_head = 0; |
| 428 | s->queue_tail = 0; |
| 429 | target_count = MAX_L2TPV3_MSGCNT; |
| 430 | } |
| 431 | |
| 432 | msgvec = s->msgvec + s->queue_head; |
| 433 | if (target_count > 0) { |
| 434 | count = RETRY_ON_EINTR( |
| 435 | recvmmsg(s->fd, msgvec, target_count, MSG_DONTWAIT, NULL) |
| 436 | ); |
| 437 | if (count < 0) { |
| 438 | /* Recv error - we still need to flush packets here, |
| 439 | * (re)set queue head to current position |
| 440 | */ |
| 441 | count = 0; |
| 442 | } |
| 443 | s->queue_head = (s->queue_head + count) % MAX_L2TPV3_MSGCNT; |
| 444 | s->queue_depth += count; |
| 445 | } |
| 446 | net_l2tpv3_process_queue(s); |
| 447 | } |
| 448 | |
| 449 | static void destroy_vector(struct mmsghdr *msgvec, int count, int iovcount) |
| 450 | { |
| 451 | int i, j; |
| 452 | struct iovec *iov; |
| 453 | struct mmsghdr *cleanup = msgvec; |
| 454 | if (cleanup) { |
| 455 | for (i = 0; i < count; i++) { |
| 456 | if (cleanup->msg_hdr.msg_iov) { |
| 457 | iov = cleanup->msg_hdr.msg_iov; |
| 458 | for (j = 0; j < iovcount; j++) { |
| 459 | g_free(iov->iov_base); |
| 460 | iov++; |
| 461 | } |
| 462 | g_free(cleanup->msg_hdr.msg_iov); |
| 463 | } |
| 464 | cleanup++; |
| 465 | } |
| 466 | g_free(msgvec); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | static struct mmsghdr *build_l2tpv3_vector(NetL2TPV3State *s, int count) |
| 471 | { |
| 472 | int i; |
| 473 | struct iovec *iov; |
| 474 | struct mmsghdr *msgvec, *result; |
| 475 | |
| 476 | msgvec = g_new(struct mmsghdr, count); |
| 477 | result = msgvec; |
| 478 | for (i = 0; i < count ; i++) { |
| 479 | msgvec->msg_hdr.msg_name = NULL; |
| 480 | msgvec->msg_hdr.msg_namelen = 0; |
| 481 | iov = g_new(struct iovec, IOVSIZE); |
| 482 | msgvec->msg_hdr.msg_iov = iov; |
| 483 | iov->iov_base = g_malloc(s->header_size); |
| 484 | iov->iov_len = s->header_size; |
| 485 | iov++ ; |
| 486 | iov->iov_base = qemu_memalign(BUFFER_ALIGN, BUFFER_SIZE); |
| 487 | iov->iov_len = BUFFER_SIZE; |
| 488 | msgvec->msg_hdr.msg_iovlen = 2; |
| 489 | msgvec->msg_hdr.msg_control = NULL; |
| 490 | msgvec->msg_hdr.msg_controllen = 0; |
| 491 | msgvec->msg_hdr.msg_flags = 0; |
| 492 | msgvec++; |
| 493 | } |
| 494 | return result; |
| 495 | } |
| 496 | |
| 497 | static void net_l2tpv3_cleanup(NetClientState *nc) |
| 498 | { |
| 499 | NetL2TPV3State *s = DO_UPCAST(NetL2TPV3State, nc, nc); |
| 500 | qemu_purge_queued_packets(nc); |
| 501 | l2tpv3_read_poll(s, false); |
| 502 | l2tpv3_write_poll(s, false); |
| 503 | if (s->fd >= 0) { |
| 504 | close(s->fd); |
| 505 | } |
| 506 | destroy_vector(s->msgvec, MAX_L2TPV3_MSGCNT, IOVSIZE); |
| 507 | g_free(s->vec); |
| 508 | g_free(s->header_buf); |
| 509 | g_free(s->dgram_dst); |
| 510 | } |
| 511 | |
| 512 | static NetClientInfo net_l2tpv3_info = { |
| 513 | .type = NET_CLIENT_DRIVER_L2TPV3, |
| 514 | .size = sizeof(NetL2TPV3State), |
| 515 | .receive = net_l2tpv3_receive_dgram, |
| 516 | .receive_iov = net_l2tpv3_receive_dgram_iov, |
| 517 | .poll = l2tpv3_poll, |
| 518 | .cleanup = net_l2tpv3_cleanup, |
| 519 | }; |
| 520 | |
| 521 | int net_init_l2tpv3(const Netdev *netdev, |
| 522 | const char *name, |
| 523 | NetClientState *peer, Error **errp) |
| 524 | { |
| 525 | const NetdevL2TPv3Options *l2tpv3; |
| 526 | NetL2TPV3State *s; |
| 527 | NetClientState *nc; |
| 528 | int fd = -1, gairet; |
| 529 | struct addrinfo hints; |
| 530 | struct addrinfo *result = NULL; |
| 531 | char *srcport, *dstport; |
| 532 | |
| 533 | nc = qemu_new_net_client(&net_l2tpv3_info, peer, "l2tpv3", name); |
| 534 | |
| 535 | s = DO_UPCAST(NetL2TPV3State, nc, nc); |
| 536 | |
| 537 | s->queue_head = 0; |
| 538 | s->queue_tail = 0; |
| 539 | s->header_mismatch = false; |
| 540 | |
| 541 | assert(netdev->type == NET_CLIENT_DRIVER_L2TPV3); |
| 542 | l2tpv3 = &netdev->u.l2tpv3; |
| 543 | |
| 544 | if (l2tpv3->has_ipv6 && l2tpv3->ipv6) { |
| 545 | s->ipv6 = l2tpv3->ipv6; |
| 546 | } else { |
| 547 | s->ipv6 = false; |
| 548 | } |
| 549 | |
| 550 | if ((l2tpv3->has_offset) && (l2tpv3->offset > 256)) { |
| 551 | error_setg(errp, "offset must be less than 256 bytes"); |
| 552 | goto outerr; |
| 553 | } |
| 554 | |
| 555 | if (l2tpv3->has_rxcookie || l2tpv3->has_txcookie) { |
| 556 | if (l2tpv3->has_rxcookie && l2tpv3->has_txcookie) { |
| 557 | s->cookie = true; |
| 558 | } else { |
| 559 | error_setg(errp, |
| 560 | "require both 'rxcookie' and 'txcookie' or neither"); |
| 561 | goto outerr; |
| 562 | } |
| 563 | } else { |
| 564 | s->cookie = false; |
| 565 | } |
| 566 | |
| 567 | if (l2tpv3->has_cookie64 || l2tpv3->cookie64) { |
| 568 | s->cookie_is_64 = true; |
| 569 | } else { |
| 570 | s->cookie_is_64 = false; |
| 571 | } |
| 572 | |
| 573 | if (l2tpv3->has_udp && l2tpv3->udp) { |
| 574 | s->udp = true; |
| 575 | if (!(l2tpv3->srcport && l2tpv3->dstport)) { |
| 576 | error_setg(errp, "need both src and dst port for udp"); |
| 577 | goto outerr; |
| 578 | } else { |
| 579 | srcport = l2tpv3->srcport; |
| 580 | dstport = l2tpv3->dstport; |
| 581 | } |
| 582 | } else { |
| 583 | s->udp = false; |
| 584 | srcport = NULL; |
| 585 | dstport = NULL; |
| 586 | } |
| 587 | |
| 588 | |
| 589 | s->offset = 4; |
| 590 | s->session_offset = 0; |
| 591 | s->cookie_offset = 4; |
| 592 | s->counter_offset = 4; |
| 593 | |
| 594 | s->tx_session = l2tpv3->txsession; |
| 595 | if (l2tpv3->has_rxsession) { |
| 596 | s->rx_session = l2tpv3->rxsession; |
| 597 | } else { |
| 598 | s->rx_session = s->tx_session; |
| 599 | } |
| 600 | |
| 601 | if (s->cookie) { |
| 602 | s->rx_cookie = l2tpv3->rxcookie; |
| 603 | s->tx_cookie = l2tpv3->txcookie; |
| 604 | if (s->cookie_is_64 == true) { |
| 605 | /* 64 bit cookie */ |
| 606 | s->offset += 8; |
| 607 | s->counter_offset += 8; |
| 608 | } else { |
| 609 | /* 32 bit cookie */ |
| 610 | s->offset += 4; |
| 611 | s->counter_offset += 4; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | memset(&hints, 0, sizeof(hints)); |
| 616 | |
| 617 | if (s->ipv6) { |
| 618 | hints.ai_family = AF_INET6; |
| 619 | } else { |
| 620 | hints.ai_family = AF_INET; |
| 621 | } |
| 622 | if (s->udp) { |
| 623 | hints.ai_socktype = SOCK_DGRAM; |
| 624 | hints.ai_protocol = 0; |
| 625 | s->offset += 4; |
| 626 | s->counter_offset += 4; |
| 627 | s->session_offset += 4; |
| 628 | s->cookie_offset += 4; |
| 629 | } else { |
| 630 | hints.ai_socktype = SOCK_RAW; |
| 631 | hints.ai_protocol = IPPROTO_L2TP; |
| 632 | } |
| 633 | |
| 634 | gairet = getaddrinfo(l2tpv3->src, srcport, &hints, &result); |
| 635 | |
| 636 | if ((gairet != 0) || (result == NULL)) { |
| 637 | error_setg(errp, "could not resolve src, errno = %s", |
| 638 | gai_strerror(gairet)); |
| 639 | goto outerr; |
| 640 | } |
| 641 | fd = socket(result->ai_family, result->ai_socktype, result->ai_protocol); |
| 642 | if (fd == -1) { |
| 643 | error_setg_errno(errp, errno, "socket creation failed"); |
| 644 | goto outerr; |
| 645 | } |
| 646 | if (bind(fd, (struct sockaddr *) result->ai_addr, result->ai_addrlen)) { |
| 647 | error_setg_errno(errp, errno, "could not bind socket"); |
| 648 | goto outerr; |
| 649 | } |
| 650 | if (!qemu_set_blocking(fd, false, errp)) { |
| 651 | goto outerr; |
| 652 | } |
| 653 | |
| 654 | freeaddrinfo(result); |
| 655 | |
| 656 | memset(&hints, 0, sizeof(hints)); |
| 657 | |
| 658 | if (s->ipv6) { |
| 659 | hints.ai_family = AF_INET6; |
| 660 | } else { |
| 661 | hints.ai_family = AF_INET; |
| 662 | } |
| 663 | if (s->udp) { |
| 664 | hints.ai_socktype = SOCK_DGRAM; |
| 665 | hints.ai_protocol = 0; |
| 666 | } else { |
| 667 | hints.ai_socktype = SOCK_RAW; |
| 668 | hints.ai_protocol = IPPROTO_L2TP; |
| 669 | } |
| 670 | |
| 671 | result = NULL; |
| 672 | gairet = getaddrinfo(l2tpv3->dst, dstport, &hints, &result); |
| 673 | if ((gairet != 0) || (result == NULL)) { |
| 674 | error_setg(errp, "could not resolve dst, error = %s", |
| 675 | gai_strerror(gairet)); |
| 676 | goto outerr; |
| 677 | } |
| 678 | |
| 679 | s->dgram_dst = g_new0(struct sockaddr_storage, 1); |
| 680 | memcpy(s->dgram_dst, result->ai_addr, result->ai_addrlen); |
| 681 | s->dst_size = result->ai_addrlen; |
| 682 | |
| 683 | freeaddrinfo(result); |
| 684 | |
| 685 | if (l2tpv3->has_counter && l2tpv3->counter) { |
| 686 | s->has_counter = true; |
| 687 | s->offset += 4; |
| 688 | } else { |
| 689 | s->has_counter = false; |
| 690 | } |
| 691 | |
| 692 | if (l2tpv3->has_pincounter && l2tpv3->pincounter) { |
| 693 | s->has_counter = true; /* pin counter implies that there is counter */ |
| 694 | s->pin_counter = true; |
| 695 | } else { |
| 696 | s->pin_counter = false; |
| 697 | } |
| 698 | |
| 699 | if (l2tpv3->has_offset) { |
| 700 | /* extra offset */ |
| 701 | s->offset += l2tpv3->offset; |
| 702 | } |
| 703 | |
| 704 | if ((s->ipv6) || (s->udp)) { |
| 705 | s->header_size = s->offset; |
| 706 | } else { |
| 707 | s->header_size = s->offset + sizeof(struct iphdr); |
| 708 | } |
| 709 | |
| 710 | s->msgvec = build_l2tpv3_vector(s, MAX_L2TPV3_MSGCNT); |
| 711 | s->vec = g_new(struct iovec, MAX_L2TPV3_IOVCNT); |
| 712 | s->header_buf = g_malloc(s->header_size); |
| 713 | |
| 714 | s->fd = fd; |
| 715 | s->counter = 0; |
| 716 | |
| 717 | l2tpv3_read_poll(s, true); |
| 718 | |
| 719 | qemu_set_info_str(&s->nc, "l2tpv3: connected"); |
| 720 | return 0; |
| 721 | outerr: |
| 722 | qemu_del_net_client(nc); |
| 723 | if (fd >= 0) { |
| 724 | close(fd); |
| 725 | } |
| 726 | if (result) { |
| 727 | freeaddrinfo(result); |
| 728 | } |
| 729 | return -1; |
| 730 | } |
| 731 |