2012-06-04 16:48:31 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-06-04 16:48:31 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2015-01-08 13:48:39 +01:00
|
|
|
#ifndef SHRPX_CONNECTION_HANDLER_H
|
|
|
|
#define SHRPX_CONNECTION_HANDLER_H
|
2012-06-04 16:48:31 +02:00
|
|
|
|
|
|
|
#include "shrpx.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
2018-06-09 09:21:30 +02:00
|
|
|
# include <sys/socket.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_SYS_SOCKET_H
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2016-07-03 23:00:15 +02:00
|
|
|
#include <mutex>
|
2014-06-26 15:55:22 +02:00
|
|
|
#include <memory>
|
2014-06-26 17:26:21 +02:00
|
|
|
#include <vector>
|
2016-01-15 10:48:14 +01:00
|
|
|
#include <random>
|
2015-09-28 17:40:33 +02:00
|
|
|
#ifndef NOTHREADS
|
2018-06-09 09:21:30 +02:00
|
|
|
# include <future>
|
2015-09-28 17:40:33 +02:00
|
|
|
#endif // NOTHREADS
|
2014-06-26 15:55:22 +02:00
|
|
|
|
2021-08-24 14:13:05 +02:00
|
|
|
#ifdef HAVE_LIBBPF
|
|
|
|
# include <bpf/libbpf.h>
|
|
|
|
#endif // HAVE_LIBBPF
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
#include <openssl/ssl.h>
|
2012-06-05 18:26:04 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
#include <ev.h>
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2015-09-23 12:45:53 +02:00
|
|
|
#ifdef HAVE_NEVERBLEED
|
2018-06-09 09:21:30 +02:00
|
|
|
# include <neverbleed.h>
|
2015-09-23 12:45:53 +02:00
|
|
|
#endif // HAVE_NEVERBLEED
|
|
|
|
|
2014-10-13 14:09:00 +02:00
|
|
|
#include "shrpx_downstream_connection_pool.h"
|
2016-06-03 18:02:57 +02:00
|
|
|
#include "shrpx_config.h"
|
2016-10-08 08:26:13 +02:00
|
|
|
#include "shrpx_exec.h"
|
2014-10-13 14:09:00 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace shrpx {
|
|
|
|
|
2013-11-04 09:53:57 +01:00
|
|
|
class Http2Session;
|
2014-08-19 16:36:04 +02:00
|
|
|
class ConnectBlocker;
|
2014-12-27 18:59:06 +01:00
|
|
|
class AcceptHandler;
|
|
|
|
class Worker;
|
2014-06-26 15:55:22 +02:00
|
|
|
struct WorkerStat;
|
2015-01-08 13:15:45 +01:00
|
|
|
struct TicketKeys;
|
2015-07-27 17:54:44 +02:00
|
|
|
class MemcachedDispatcher;
|
2016-02-07 09:51:53 +01:00
|
|
|
struct UpstreamAddr;
|
2012-11-20 17:29:39 +01:00
|
|
|
|
2017-04-01 08:07:32 +02:00
|
|
|
namespace tls {
|
2016-06-25 16:47:22 +02:00
|
|
|
|
|
|
|
class CertLookupTree;
|
|
|
|
|
2017-04-01 08:07:32 +02:00
|
|
|
} // namespace tls
|
2016-06-25 16:47:22 +02:00
|
|
|
|
2015-04-08 17:25:47 +02:00
|
|
|
struct OCSPUpdateContext {
|
|
|
|
// ocsp response buffer
|
|
|
|
std::vector<uint8_t> resp;
|
2016-10-08 08:26:13 +02:00
|
|
|
// Process running fetch-ocsp-response script
|
|
|
|
Process proc;
|
2015-04-08 17:25:47 +02:00
|
|
|
// index to ConnectionHandler::all_ssl_ctx_, which points to next
|
|
|
|
// SSL_CTX to update ocsp response cache.
|
|
|
|
size_t next;
|
|
|
|
ev_child chldev;
|
|
|
|
ev_io rev;
|
|
|
|
// errno encountered while processing response
|
|
|
|
int error;
|
|
|
|
};
|
|
|
|
|
2016-06-03 18:02:57 +02:00
|
|
|
// SerialEvent is an event sent from Worker thread.
|
2018-10-17 01:56:59 +02:00
|
|
|
enum class SerialEventType {
|
|
|
|
NONE,
|
|
|
|
REPLACE_DOWNSTREAM,
|
2016-06-03 18:02:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SerialEvent {
|
|
|
|
// ctor for event uses DownstreamConfig
|
2018-10-17 01:56:59 +02:00
|
|
|
SerialEvent(SerialEventType type,
|
|
|
|
const std::shared_ptr<DownstreamConfig> &downstreamconf)
|
2016-06-03 18:02:57 +02:00
|
|
|
: type(type), downstreamconf(downstreamconf) {}
|
|
|
|
|
2018-10-17 01:56:59 +02:00
|
|
|
SerialEventType type;
|
2016-06-03 18:02:57 +02:00
|
|
|
std::shared_ptr<DownstreamConfig> downstreamconf;
|
|
|
|
};
|
|
|
|
|
2021-08-29 03:18:59 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
|
|
|
# ifdef HAVE_LIBBPF
|
2021-08-24 14:13:05 +02:00
|
|
|
struct BPFRef {
|
|
|
|
int reuseport_array;
|
|
|
|
int cid_prefix_map;
|
|
|
|
};
|
2021-08-29 03:18:59 +02:00
|
|
|
# endif // HAVE_LIBBPF
|
|
|
|
|
|
|
|
// QUIC IPC message type.
|
|
|
|
enum class QUICIPCType {
|
|
|
|
NONE,
|
|
|
|
// Send forwarded QUIC UDP datagram and its metadata.
|
|
|
|
DGRAM_FORWARD,
|
|
|
|
};
|
|
|
|
|
|
|
|
// WorkerProcesses which are in graceful shutdown period.
|
|
|
|
struct QUICLingeringWorkerProcess {
|
|
|
|
QUICLingeringWorkerProcess(
|
|
|
|
std::vector<std::array<uint8_t, SHRPX_QUIC_CID_PREFIXLEN>> cid_prefixes,
|
|
|
|
int quic_ipc_fd)
|
|
|
|
: cid_prefixes{std::move(cid_prefixes)}, quic_ipc_fd{quic_ipc_fd} {}
|
|
|
|
|
|
|
|
std::vector<std::array<uint8_t, SHRPX_QUIC_CID_PREFIXLEN>> cid_prefixes;
|
|
|
|
// Socket to send QUIC IPC message to this worker process.
|
|
|
|
int quic_ipc_fd;
|
|
|
|
};
|
|
|
|
#endif // ENABLE_HTTP3
|
2021-08-24 14:13:05 +02:00
|
|
|
|
2015-01-08 13:48:39 +01:00
|
|
|
class ConnectionHandler {
|
2012-06-04 16:48:31 +02:00
|
|
|
public:
|
2016-07-08 16:41:53 +02:00
|
|
|
ConnectionHandler(struct ev_loop *loop, std::mt19937 &gen);
|
2015-01-08 13:48:39 +01:00
|
|
|
~ConnectionHandler();
|
2016-01-31 11:41:56 +01:00
|
|
|
int handle_connection(int fd, sockaddr *addr, int addrlen,
|
2016-02-07 09:51:53 +01:00
|
|
|
const UpstreamAddr *faddr);
|
2015-02-11 11:18:41 +01:00
|
|
|
// Creates Worker object for single threaded configuration.
|
2015-09-01 17:19:32 +02:00
|
|
|
int create_single_worker();
|
2015-02-11 11:18:41 +01:00
|
|
|
// Creates |num| Worker objects for multi threaded configuration.
|
|
|
|
// The |num| must be strictly more than 1.
|
2015-09-01 17:19:32 +02:00
|
|
|
int create_worker_thread(size_t num);
|
2015-07-23 16:13:29 +02:00
|
|
|
void
|
|
|
|
set_ticket_keys_to_worker(const std::shared_ptr<TicketKeys> &ticket_keys);
|
2014-07-05 11:22:40 +02:00
|
|
|
void worker_reopen_log_files();
|
2015-02-11 11:18:41 +01:00
|
|
|
void set_ticket_keys(std::shared_ptr<TicketKeys> ticket_keys);
|
|
|
|
const std::shared_ptr<TicketKeys> &get_ticket_keys() const;
|
2014-12-27 18:59:06 +01:00
|
|
|
struct ev_loop *get_loop() const;
|
2015-02-11 11:18:41 +01:00
|
|
|
Worker *get_single_worker() const;
|
2016-01-31 11:41:56 +01:00
|
|
|
void add_acceptor(std::unique_ptr<AcceptHandler> h);
|
2018-02-26 14:40:24 +01:00
|
|
|
void delete_acceptor();
|
2014-12-27 18:59:06 +01:00
|
|
|
void enable_acceptor();
|
|
|
|
void disable_acceptor();
|
2016-01-19 09:03:01 +01:00
|
|
|
void sleep_acceptor(ev_tstamp t);
|
2014-08-12 15:22:02 +02:00
|
|
|
void accept_pending_connection();
|
|
|
|
void graceful_shutdown_worker();
|
2015-02-25 14:53:23 +01:00
|
|
|
void set_graceful_shutdown(bool f);
|
|
|
|
bool get_graceful_shutdown() const;
|
2014-08-12 15:22:02 +02:00
|
|
|
void join_worker();
|
2015-04-08 17:25:47 +02:00
|
|
|
|
|
|
|
// Cancels ocsp update process
|
|
|
|
void cancel_ocsp_update();
|
|
|
|
// Starts ocsp update for certficate |cert_file|.
|
|
|
|
int start_ocsp_update(const char *cert_file);
|
|
|
|
// Reads incoming data from ocsp update process
|
|
|
|
void read_ocsp_chunk();
|
|
|
|
// Handles the completion of one ocsp update
|
|
|
|
void handle_ocsp_complete();
|
|
|
|
// Resets ocsp_;
|
|
|
|
void reset_ocsp();
|
|
|
|
// Proceeds to the next certificate's ocsp update. If all
|
|
|
|
// certificates' ocsp update has been done, schedule next ocsp
|
2015-03-30 16:20:40 +02:00
|
|
|
// update.
|
2015-04-08 17:25:47 +02:00
|
|
|
void proceed_next_cert_ocsp();
|
2014-11-27 15:39:04 +01:00
|
|
|
|
2015-07-27 17:54:44 +02:00
|
|
|
void set_tls_ticket_key_memcached_dispatcher(
|
|
|
|
std::unique_ptr<MemcachedDispatcher> dispatcher);
|
|
|
|
|
|
|
|
MemcachedDispatcher *get_tls_ticket_key_memcached_dispatcher() const;
|
|
|
|
void on_tls_ticket_key_network_error(ev_timer *w);
|
|
|
|
void on_tls_ticket_key_not_found(ev_timer *w);
|
|
|
|
void
|
|
|
|
on_tls_ticket_key_get_success(const std::shared_ptr<TicketKeys> &ticket_keys,
|
|
|
|
ev_timer *w);
|
|
|
|
void schedule_next_tls_ticket_key_memcached_get(ev_timer *w);
|
2016-02-13 10:17:11 +01:00
|
|
|
SSL_CTX *create_tls_ticket_key_memcached_ssl_ctx();
|
2016-06-24 17:28:15 +02:00
|
|
|
// Returns the SSL_CTX at all_ssl_ctx_[idx]. This does not perform
|
|
|
|
// array bound checking.
|
|
|
|
SSL_CTX *get_ssl_ctx(size_t idx) const;
|
2015-07-27 17:54:44 +02:00
|
|
|
|
2017-02-04 10:59:06 +01:00
|
|
|
const std::vector<SSL_CTX *> &get_indexed_ssl_ctx(size_t idx) const;
|
2021-08-21 12:00:43 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
2021-08-15 15:57:26 +02:00
|
|
|
const std::vector<SSL_CTX *> &get_quic_indexed_ssl_ctx(size_t idx) const;
|
2021-08-23 14:40:24 +02:00
|
|
|
|
|
|
|
int forward_quic_packet(const UpstreamAddr *faddr, const Address &remote_addr,
|
|
|
|
const Address &local_addr, const uint8_t *cid_prefix,
|
|
|
|
const uint8_t *data, size_t datalen);
|
2021-08-24 14:13:05 +02:00
|
|
|
|
2021-08-26 07:46:34 +02:00
|
|
|
int create_quic_secret();
|
|
|
|
|
2021-08-29 03:18:59 +02:00
|
|
|
void set_cid_prefixes(
|
|
|
|
const std::vector<std::array<uint8_t, SHRPX_QUIC_CID_PREFIXLEN>>
|
|
|
|
&cid_prefixes);
|
|
|
|
|
|
|
|
void set_quic_lingering_worker_processes(
|
|
|
|
const std::vector<QUICLingeringWorkerProcess> &quic_lwps);
|
|
|
|
|
|
|
|
// Return matching QUICLingeringWorkerProcess which has a CID prefix
|
|
|
|
// such that |dcid| starts with it. If no such
|
|
|
|
// QUICLingeringWorkerProcess, it returns nullptr.
|
|
|
|
QUICLingeringWorkerProcess *
|
|
|
|
match_quic_lingering_worker_process_cid_prefix(const uint8_t *dcid,
|
|
|
|
size_t dcidlen);
|
|
|
|
|
|
|
|
int forward_quic_packet_to_lingering_worker_process(
|
|
|
|
QUICLingeringWorkerProcess *quic_lwp, const Address &remote_addr,
|
|
|
|
const Address &local_addr, const uint8_t *data, size_t datalen);
|
|
|
|
|
|
|
|
void set_quic_ipc_fd(int fd);
|
|
|
|
|
|
|
|
int quic_ipc_read();
|
|
|
|
|
2021-08-24 14:13:05 +02:00
|
|
|
# ifdef HAVE_LIBBPF
|
|
|
|
std::vector<BPFRef> &get_quic_bpf_refs();
|
|
|
|
# endif // HAVE_LIBBPF
|
|
|
|
#endif // ENABLE_HTTP3
|
2017-02-04 10:59:06 +01:00
|
|
|
|
2015-09-23 12:45:53 +02:00
|
|
|
#ifdef HAVE_NEVERBLEED
|
2018-08-24 15:15:43 +02:00
|
|
|
void set_neverbleed(neverbleed_t *nb);
|
2015-09-23 12:45:53 +02:00
|
|
|
#endif // HAVE_NEVERBLEED
|
|
|
|
|
2018-10-17 01:56:59 +02:00
|
|
|
// Send SerialEvent SerialEventType::REPLACE_DOWNSTREAM to this
|
|
|
|
// object.
|
2016-06-03 18:02:57 +02:00
|
|
|
void send_replace_downstream(
|
|
|
|
const std::shared_ptr<DownstreamConfig> &downstreamconf);
|
|
|
|
// Internal function to send |ev| to this object.
|
|
|
|
void send_serial_event(SerialEvent ev);
|
|
|
|
// Handles SerialEvents received.
|
|
|
|
void handle_serial_event();
|
|
|
|
// Sends WorkerEvent to make them replace downstream.
|
|
|
|
void
|
|
|
|
worker_replace_downstream(std::shared_ptr<DownstreamConfig> downstreamconf);
|
|
|
|
|
2017-05-18 14:11:10 +02:00
|
|
|
void set_enable_acceptor_on_ocsp_completion(bool f);
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
private:
|
2015-06-10 14:31:58 +02:00
|
|
|
// Stores all SSL_CTX objects.
|
2015-03-30 16:20:40 +02:00
|
|
|
std::vector<SSL_CTX *> all_ssl_ctx_;
|
2017-02-04 10:59:06 +01:00
|
|
|
// Stores all SSL_CTX objects in a way that its index is stored in
|
|
|
|
// cert_tree. The SSL_CTXs stored in the same index share the same
|
|
|
|
// hostname, but could have different signature algorithm. The
|
|
|
|
// selection among them are performed by hostname presented by SNI,
|
|
|
|
// and signature algorithm presented by client.
|
|
|
|
std::vector<std::vector<SSL_CTX *>> indexed_ssl_ctx_;
|
2021-08-21 12:00:43 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
2021-08-29 03:18:59 +02:00
|
|
|
std::vector<std::array<uint8_t, SHRPX_QUIC_CID_PREFIXLEN>> cid_prefixes_;
|
|
|
|
std::vector<std::array<uint8_t, SHRPX_QUIC_CID_PREFIXLEN>>
|
|
|
|
lingering_cid_prefixes_;
|
|
|
|
int quic_ipc_fd_;
|
|
|
|
std::vector<QUICLingeringWorkerProcess> quic_lingering_worker_processes_;
|
2021-08-24 14:13:05 +02:00
|
|
|
# ifdef HAVE_LIBBPF
|
|
|
|
std::vector<BPFRef> quic_bpf_refs_;
|
|
|
|
# endif // HAVE_LIBBPF
|
2021-08-26 07:46:34 +02:00
|
|
|
std::shared_ptr<QUICSecret> quic_secret_;
|
2021-08-15 15:57:26 +02:00
|
|
|
std::vector<SSL_CTX *> quic_all_ssl_ctx_;
|
|
|
|
std::vector<std::vector<SSL_CTX *>> quic_indexed_ssl_ctx_;
|
2021-08-21 12:00:43 +02:00
|
|
|
#endif // ENABLE_HTTP3
|
2015-04-08 17:25:47 +02:00
|
|
|
OCSPUpdateContext ocsp_;
|
2017-03-17 14:24:32 +01:00
|
|
|
std::mt19937 &gen_;
|
2015-09-04 15:21:10 +02:00
|
|
|
// ev_loop for each worker
|
|
|
|
std::vector<struct ev_loop *> worker_loops_;
|
2015-02-11 11:18:41 +01:00
|
|
|
// Worker instances when multi threaded mode (-nN, N >= 2) is used.
|
2016-06-16 14:22:36 +02:00
|
|
|
// If at least one frontend enables API request, we allocate 1
|
|
|
|
// additional worker dedicated to API request .
|
2014-12-27 18:59:06 +01:00
|
|
|
std::vector<std::unique_ptr<Worker>> workers_;
|
2016-06-03 18:02:57 +02:00
|
|
|
// mutex for serial event resive buffer handling
|
|
|
|
std::mutex serial_event_mu_;
|
|
|
|
// SerialEvent receive buffer
|
|
|
|
std::vector<SerialEvent> serial_events_;
|
2015-02-11 11:18:41 +01:00
|
|
|
// Worker instance used when single threaded mode (-n1) is used.
|
|
|
|
// Otherwise, nullptr and workers_ has instances of Worker instead.
|
|
|
|
std::unique_ptr<Worker> single_worker_;
|
2017-04-01 08:07:32 +02:00
|
|
|
std::unique_ptr<tls::CertLookupTree> cert_tree_;
|
2021-08-21 12:00:43 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
2021-08-15 15:57:26 +02:00
|
|
|
std::unique_ptr<tls::CertLookupTree> quic_cert_tree_;
|
2021-08-21 12:00:43 +02:00
|
|
|
#endif // ENABLE_HTTP3
|
2015-07-27 17:54:44 +02:00
|
|
|
std::unique_ptr<MemcachedDispatcher> tls_ticket_key_memcached_dispatcher_;
|
2015-02-11 11:18:41 +01:00
|
|
|
// Current TLS session ticket keys. Note that TLS connection does
|
|
|
|
// not refer to this field directly. They use TicketKeys object in
|
|
|
|
// Worker object.
|
|
|
|
std::shared_ptr<TicketKeys> ticket_keys_;
|
2014-12-27 18:59:06 +01:00
|
|
|
struct ev_loop *loop_;
|
2016-01-31 11:41:56 +01:00
|
|
|
std::vector<std::unique_ptr<AcceptHandler>> acceptors_;
|
2015-09-23 12:45:53 +02:00
|
|
|
#ifdef HAVE_NEVERBLEED
|
2018-08-24 15:15:43 +02:00
|
|
|
neverbleed_t *nb_;
|
2015-09-23 12:45:53 +02:00
|
|
|
#endif // HAVE_NEVERBLEED
|
2014-12-27 18:59:06 +01:00
|
|
|
ev_timer disable_acceptor_timer_;
|
2015-03-30 16:20:40 +02:00
|
|
|
ev_timer ocsp_timer_;
|
2015-09-28 17:40:33 +02:00
|
|
|
ev_async thread_join_asyncev_;
|
2016-06-03 18:02:57 +02:00
|
|
|
ev_async serial_event_asyncev_;
|
2015-09-28 17:40:33 +02:00
|
|
|
#ifndef NOTHREADS
|
|
|
|
std::future<void> thread_join_fut_;
|
|
|
|
#endif // NOTHREADS
|
2015-07-27 17:54:44 +02:00
|
|
|
size_t tls_ticket_key_memcached_get_retry_count_;
|
|
|
|
size_t tls_ticket_key_memcached_fail_count_;
|
2013-12-06 15:17:38 +01:00
|
|
|
unsigned int worker_round_robin_cnt_;
|
2015-02-25 14:53:23 +01:00
|
|
|
bool graceful_shutdown_;
|
2017-05-18 14:11:10 +02:00
|
|
|
// true if acceptors should be enabled after the initial ocsp update
|
|
|
|
// has finished.
|
|
|
|
bool enable_acceptor_on_ocsp_completion_;
|
2012-06-04 16:48:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace shrpx
|
|
|
|
|
2015-01-08 13:48:39 +01:00
|
|
|
#endif // SHRPX_CONNECTION_HANDLER_H
|