• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #include "shrpx_client_handler.h"
26 
27 #ifdef HAVE_UNISTD_H
28 #  include <unistd.h>
29 #endif // HAVE_UNISTD_H
30 #ifdef HAVE_SYS_SOCKET_H
31 #  include <sys/socket.h>
32 #endif // HAVE_SYS_SOCKET_H
33 #ifdef HAVE_NETDB_H
34 #  include <netdb.h>
35 #endif // HAVE_NETDB_H
36 
37 #include <cerrno>
38 
39 #include "shrpx_upstream.h"
40 #include "shrpx_http2_upstream.h"
41 #include "shrpx_https_upstream.h"
42 #include "shrpx_config.h"
43 #include "shrpx_http_downstream_connection.h"
44 #include "shrpx_http2_downstream_connection.h"
45 #include "shrpx_tls.h"
46 #include "shrpx_worker.h"
47 #include "shrpx_downstream_connection_pool.h"
48 #include "shrpx_downstream.h"
49 #include "shrpx_http2_session.h"
50 #include "shrpx_connect_blocker.h"
51 #include "shrpx_api_downstream_connection.h"
52 #include "shrpx_health_monitor_downstream_connection.h"
53 #include "shrpx_null_downstream_connection.h"
54 #ifdef ENABLE_HTTP3
55 #  include "shrpx_http3_upstream.h"
56 #endif // ENABLE_HTTP3
57 #include "shrpx_log.h"
58 #include "util.h"
59 #include "template.h"
60 #include "tls.h"
61 
62 using namespace nghttp2;
63 
64 namespace shrpx {
65 
66 namespace {
timeoutcb(struct ev_loop * loop,ev_timer * w,int revents)67 void timeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
68   auto conn = static_cast<Connection *>(w->data);
69   auto handler = static_cast<ClientHandler *>(conn->data);
70 
71   if (LOG_ENABLED(INFO)) {
72     CLOG(INFO, handler) << "Time out";
73   }
74 
75   delete handler;
76 }
77 } // namespace
78 
79 namespace {
shutdowncb(struct ev_loop * loop,ev_timer * w,int revents)80 void shutdowncb(struct ev_loop *loop, ev_timer *w, int revents) {
81   auto handler = static_cast<ClientHandler *>(w->data);
82 
83   if (LOG_ENABLED(INFO)) {
84     CLOG(INFO, handler) << "Close connection due to TLS renegotiation";
85   }
86 
87   delete handler;
88 }
89 } // namespace
90 
91 namespace {
readcb(struct ev_loop * loop,ev_io * w,int revents)92 void readcb(struct ev_loop *loop, ev_io *w, int revents) {
93   auto conn = static_cast<Connection *>(w->data);
94   auto handler = static_cast<ClientHandler *>(conn->data);
95 
96   if (handler->do_read() != 0) {
97     delete handler;
98     return;
99   }
100 }
101 } // namespace
102 
103 namespace {
writecb(struct ev_loop * loop,ev_io * w,int revents)104 void writecb(struct ev_loop *loop, ev_io *w, int revents) {
105   auto conn = static_cast<Connection *>(w->data);
106   auto handler = static_cast<ClientHandler *>(conn->data);
107 
108   if (handler->do_write() != 0) {
109     delete handler;
110     return;
111   }
112 }
113 } // namespace
114 
noop()115 int ClientHandler::noop() { return 0; }
116 
read_clear()117 int ClientHandler::read_clear() {
118   auto should_break = false;
119   rb_.ensure_chunk();
120   for (;;) {
121     if (rb_.rleft() && on_read() != 0) {
122       return -1;
123     }
124     if (rb_.rleft() == 0) {
125       rb_.reset();
126     } else if (rb_.wleft() == 0) {
127       conn_.rlimit.stopw();
128       return 0;
129     }
130 
131     if (!ev_is_active(&conn_.rev) || should_break) {
132       return 0;
133     }
134 
135     auto nread = conn_.read_clear(rb_.last(), rb_.wleft());
136 
137     if (nread == 0) {
138       if (rb_.rleft() == 0) {
139         rb_.release_chunk();
140       }
141       return 0;
142     }
143 
144     if (nread < 0) {
145       return -1;
146     }
147 
148     rb_.write(nread);
149     should_break = true;
150   }
151 }
152 
write_clear()153 int ClientHandler::write_clear() {
154   std::array<iovec, 2> iov;
155 
156   for (;;) {
157     if (on_write() != 0) {
158       return -1;
159     }
160 
161     auto iovcnt = upstream_->response_riovec(iov.data(), iov.size());
162     if (iovcnt == 0) {
163       break;
164     }
165 
166     auto nwrite = conn_.writev_clear(iov.data(), iovcnt);
167     if (nwrite < 0) {
168       return -1;
169     }
170 
171     if (nwrite == 0) {
172       return 0;
173     }
174 
175     upstream_->response_drain(nwrite);
176   }
177 
178   conn_.wlimit.stopw();
179   ev_timer_stop(conn_.loop, &conn_.wt);
180 
181   return 0;
182 }
183 
proxy_protocol_peek_clear()184 int ClientHandler::proxy_protocol_peek_clear() {
185   rb_.ensure_chunk();
186 
187   assert(rb_.rleft() == 0);
188 
189   auto nread = conn_.peek_clear(rb_.last(), rb_.wleft());
190   if (nread < 0) {
191     return -1;
192   }
193   if (nread == 0) {
194     return 0;
195   }
196 
197   if (LOG_ENABLED(INFO)) {
198     CLOG(INFO, this) << "PROXY-protocol: Peek " << nread
199                      << " bytes from socket";
200   }
201 
202   rb_.write(nread);
203 
204   if (on_read() != 0) {
205     return -1;
206   }
207 
208   rb_.reset();
209 
210   return 0;
211 }
212 
tls_handshake()213 int ClientHandler::tls_handshake() {
214   ev_timer_again(conn_.loop, &conn_.rt);
215 
216   ERR_clear_error();
217 
218   auto rv = conn_.tls_handshake();
219 
220   if (rv == SHRPX_ERR_INPROGRESS) {
221     return 0;
222   }
223 
224   if (rv < 0) {
225     return -1;
226   }
227 
228   if (LOG_ENABLED(INFO)) {
229     CLOG(INFO, this) << "SSL/TLS handshake completed";
230   }
231 
232   if (validate_next_proto() != 0) {
233     return -1;
234   }
235 
236   read_ = &ClientHandler::read_tls;
237   write_ = &ClientHandler::write_tls;
238 
239   return 0;
240 }
241 
read_tls()242 int ClientHandler::read_tls() {
243   auto should_break = false;
244 
245   ERR_clear_error();
246 
247   rb_.ensure_chunk();
248 
249   for (;;) {
250     // we should process buffered data first before we read EOF.
251     if (rb_.rleft() && on_read() != 0) {
252       return -1;
253     }
254     if (rb_.rleft() == 0) {
255       rb_.reset();
256     } else if (rb_.wleft() == 0) {
257       conn_.rlimit.stopw();
258       return 0;
259     }
260 
261     if (!ev_is_active(&conn_.rev) || should_break) {
262       return 0;
263     }
264 
265     auto nread = conn_.read_tls(rb_.last(), rb_.wleft());
266 
267     if (nread == 0) {
268       if (rb_.rleft() == 0) {
269         rb_.release_chunk();
270       }
271       return 0;
272     }
273 
274     if (nread < 0) {
275       return -1;
276     }
277 
278     rb_.write(nread);
279     should_break = true;
280   }
281 }
282 
write_tls()283 int ClientHandler::write_tls() {
284   struct iovec iov;
285 
286   ERR_clear_error();
287 
288   if (on_write() != 0) {
289     return -1;
290   }
291 
292   auto iovcnt = upstream_->response_riovec(&iov, 1);
293   if (iovcnt == 0) {
294     conn_.start_tls_write_idle();
295 
296     conn_.wlimit.stopw();
297     ev_timer_stop(conn_.loop, &conn_.wt);
298 
299     return 0;
300   }
301 
302   for (;;) {
303     auto nwrite = conn_.write_tls(iov.iov_base, iov.iov_len);
304     if (nwrite < 0) {
305       return -1;
306     }
307 
308     if (nwrite == 0) {
309       return 0;
310     }
311 
312     upstream_->response_drain(nwrite);
313 
314     iovcnt = upstream_->response_riovec(&iov, 1);
315     if (iovcnt == 0) {
316       return 0;
317     }
318   }
319 }
320 
321 #ifdef ENABLE_HTTP3
read_quic(const UpstreamAddr * faddr,const Address & remote_addr,const Address & local_addr,const ngtcp2_pkt_info & pi,std::span<const uint8_t> data)322 int ClientHandler::read_quic(const UpstreamAddr *faddr,
323                              const Address &remote_addr,
324                              const Address &local_addr,
325                              const ngtcp2_pkt_info &pi,
326                              std::span<const uint8_t> data) {
327   auto upstream = static_cast<Http3Upstream *>(upstream_.get());
328 
329   return upstream->on_read(faddr, remote_addr, local_addr, pi, data);
330 }
331 
write_quic()332 int ClientHandler::write_quic() { return upstream_->on_write(); }
333 #endif // ENABLE_HTTP3
334 
upstream_noop()335 int ClientHandler::upstream_noop() { return 0; }
336 
upstream_read()337 int ClientHandler::upstream_read() {
338   assert(upstream_);
339   if (upstream_->on_read() != 0) {
340     return -1;
341   }
342   return 0;
343 }
344 
upstream_write()345 int ClientHandler::upstream_write() {
346   assert(upstream_);
347   if (upstream_->on_write() != 0) {
348     return -1;
349   }
350 
351   if (get_should_close_after_write() && upstream_->response_empty()) {
352     return -1;
353   }
354 
355   return 0;
356 }
357 
upstream_http2_connhd_read()358 int ClientHandler::upstream_http2_connhd_read() {
359   auto nread = std::min(left_connhd_len_, rb_.rleft());
360   if (memcmp(&NGHTTP2_CLIENT_MAGIC[NGHTTP2_CLIENT_MAGIC_LEN - left_connhd_len_],
361              rb_.pos(), nread) != 0) {
362     // There is no downgrade path here. Just drop the connection.
363     if (LOG_ENABLED(INFO)) {
364       CLOG(INFO, this) << "invalid client connection header";
365     }
366 
367     return -1;
368   }
369 
370   left_connhd_len_ -= nread;
371   rb_.drain(nread);
372   conn_.rlimit.startw();
373 
374   if (left_connhd_len_ == 0) {
375     on_read_ = &ClientHandler::upstream_read;
376     // Run on_read to process data left in buffer since they are not
377     // notified further
378     if (on_read() != 0) {
379       return -1;
380     }
381     return 0;
382   }
383 
384   return 0;
385 }
386 
upstream_http1_connhd_read()387 int ClientHandler::upstream_http1_connhd_read() {
388   auto nread = std::min(left_connhd_len_, rb_.rleft());
389   if (memcmp(&NGHTTP2_CLIENT_MAGIC[NGHTTP2_CLIENT_MAGIC_LEN - left_connhd_len_],
390              rb_.pos(), nread) != 0) {
391     if (LOG_ENABLED(INFO)) {
392       CLOG(INFO, this) << "This is HTTP/1.1 connection, "
393                        << "but may be upgraded to HTTP/2 later.";
394     }
395 
396     // Reset header length for later HTTP/2 upgrade
397     left_connhd_len_ = NGHTTP2_CLIENT_MAGIC_LEN;
398     on_read_ = &ClientHandler::upstream_read;
399     on_write_ = &ClientHandler::upstream_write;
400 
401     if (on_read() != 0) {
402       return -1;
403     }
404 
405     return 0;
406   }
407 
408   left_connhd_len_ -= nread;
409   rb_.drain(nread);
410   conn_.rlimit.startw();
411 
412   if (left_connhd_len_ == 0) {
413     if (LOG_ENABLED(INFO)) {
414       CLOG(INFO, this) << "direct HTTP/2 connection";
415     }
416 
417     direct_http2_upgrade();
418     on_read_ = &ClientHandler::upstream_read;
419     on_write_ = &ClientHandler::upstream_write;
420 
421     // Run on_read to process data left in buffer since they are not
422     // notified further
423     if (on_read() != 0) {
424       return -1;
425     }
426 
427     return 0;
428   }
429 
430   return 0;
431 }
432 
ClientHandler(Worker * worker,int fd,SSL * ssl,const StringRef & ipaddr,const StringRef & port,int family,const UpstreamAddr * faddr)433 ClientHandler::ClientHandler(Worker *worker, int fd, SSL *ssl,
434                              const StringRef &ipaddr, const StringRef &port,
435                              int family, const UpstreamAddr *faddr)
436   : // We use balloc_ for TLS session ID (64), ipaddr (IPv6) (39),
437     // port (5), forwarded-for (IPv6) (41), alpn (5), proxyproto
438     // ipaddr (15), proxyproto port (5), sni (32, estimated).  we
439     // need terminal NULL byte for each.  We also require 8 bytes
440     // header for each allocation.  We align at 16 bytes boundary,
441     // so the required space is 64 + 48 + 16 + 48 + 16 + 16 + 16 +
442     // 32 + 8 + 8 * 8 = 328.
443     balloc_(512, 512),
444     rb_(worker->get_mcpool()),
445     conn_(worker->get_loop(), fd, ssl, worker->get_mcpool(),
446           get_config()->conn.upstream.timeout.write,
447           get_config()->conn.upstream.timeout.idle,
448           get_config()->conn.upstream.ratelimit.write,
449           get_config()->conn.upstream.ratelimit.read, writecb, readcb,
450           timeoutcb, this, get_config()->tls.dyn_rec.warmup_threshold,
451           get_config()->tls.dyn_rec.idle_timeout,
452           faddr->quic ? Proto::HTTP3 : Proto::NONE),
453     ipaddr_(make_string_ref(balloc_, ipaddr)),
454     port_(make_string_ref(balloc_, port)),
455     faddr_(faddr),
456     worker_(worker),
457     left_connhd_len_(NGHTTP2_CLIENT_MAGIC_LEN),
458     affinity_hash_(0),
459     should_close_after_write_(false),
460     affinity_hash_computed_(false) {
461   ++worker_->get_worker_stat()->num_connections;
462 
463   ev_timer_init(&reneg_shutdown_timer_, shutdowncb, 0., 0.);
464 
465   reneg_shutdown_timer_.data = this;
466 
467   if (!faddr->quic) {
468     conn_.rlimit.startw();
469   }
470   ev_timer_again(conn_.loop, &conn_.rt);
471 
472   auto config = get_config();
473 
474   if (!faddr->quic) {
475     if (faddr_->accept_proxy_protocol ||
476         config->conn.upstream.accept_proxy_protocol) {
477       read_ = &ClientHandler::proxy_protocol_peek_clear;
478       write_ = &ClientHandler::noop;
479       on_read_ = &ClientHandler::proxy_protocol_read;
480       on_write_ = &ClientHandler::upstream_noop;
481     } else {
482       setup_upstream_io_callback();
483     }
484   }
485 
486   auto &fwdconf = config->http.forwarded;
487 
488   if (fwdconf.params & FORWARDED_FOR) {
489     if (fwdconf.for_node_type == ForwardedNode::OBFUSCATED) {
490       // 1 for '_'
491       auto len = SHRPX_OBFUSCATED_NODE_LENGTH + 1;
492       // 1 for terminating NUL.
493       auto buf = make_byte_ref(balloc_, len + 1);
494       auto p = std::begin(buf);
495       *p++ = '_';
496       p = util::random_alpha_digit(p, p + SHRPX_OBFUSCATED_NODE_LENGTH,
497                                    worker_->get_randgen());
498       *p = '\0';
499 
500       forwarded_for_ = StringRef{std::span{std::begin(buf), p}};
501     } else {
502       init_forwarded_for(family, ipaddr_);
503     }
504   }
505 }
506 
init_forwarded_for(int family,const StringRef & ipaddr)507 void ClientHandler::init_forwarded_for(int family, const StringRef &ipaddr) {
508   if (family == AF_INET6) {
509     // 2 for '[' and ']'
510     auto len = 2 + ipaddr.size();
511     // 1 for terminating NUL.
512     auto buf = make_byte_ref(balloc_, len + 1);
513     auto p = std::begin(buf);
514     *p++ = '[';
515     p = std::copy(std::begin(ipaddr), std::end(ipaddr), p);
516     *p++ = ']';
517     *p = '\0';
518 
519     forwarded_for_ = StringRef{std::span{std::begin(buf), p}};
520   } else {
521     // family == AF_INET or family == AF_UNIX
522     forwarded_for_ = ipaddr;
523   }
524 }
525 
setup_upstream_io_callback()526 void ClientHandler::setup_upstream_io_callback() {
527   if (conn_.tls.ssl) {
528     conn_.prepare_server_handshake();
529     read_ = write_ = &ClientHandler::tls_handshake;
530     on_read_ = &ClientHandler::upstream_noop;
531     on_write_ = &ClientHandler::upstream_write;
532   } else {
533     // For non-TLS version, first create HttpsUpstream. It may be
534     // upgraded to HTTP/2 through HTTP Upgrade or direct HTTP/2
535     // connection.
536     upstream_ = std::make_unique<HttpsUpstream>(this);
537     alpn_ = "http/1.1"_sr;
538     read_ = &ClientHandler::read_clear;
539     write_ = &ClientHandler::write_clear;
540     on_read_ = &ClientHandler::upstream_http1_connhd_read;
541     on_write_ = &ClientHandler::upstream_noop;
542   }
543 }
544 
545 #ifdef ENABLE_HTTP3
setup_http3_upstream(std::unique_ptr<Http3Upstream> && upstream)546 void ClientHandler::setup_http3_upstream(
547   std::unique_ptr<Http3Upstream> &&upstream) {
548   upstream_ = std::move(upstream);
549   write_ = &ClientHandler::write_quic;
550 
551   auto config = get_config();
552 
553   reset_upstream_read_timeout(config->conn.upstream.timeout.http3_idle);
554 }
555 #endif // ENABLE_HTTP3
556 
~ClientHandler()557 ClientHandler::~ClientHandler() {
558   if (LOG_ENABLED(INFO)) {
559     CLOG(INFO, this) << "Deleting";
560   }
561 
562   if (upstream_) {
563     upstream_->on_handler_delete();
564   }
565 
566   auto worker_stat = worker_->get_worker_stat();
567   --worker_stat->num_connections;
568 
569   if (worker_stat->num_connections == 0) {
570     worker_->schedule_clear_mcpool();
571   }
572 
573   ev_timer_stop(conn_.loop, &reneg_shutdown_timer_);
574 
575   // TODO If backend is http/2, and it is in CONNECTED state, signal
576   // it and make it loopbreak when output is zero.
577   if (worker_->get_graceful_shutdown() && worker_stat->num_connections == 0 &&
578       worker_stat->num_close_waits == 0) {
579     ev_break(conn_.loop);
580   }
581 
582   if (LOG_ENABLED(INFO)) {
583     CLOG(INFO, this) << "Deleted";
584   }
585 }
586 
get_upstream()587 Upstream *ClientHandler::get_upstream() { return upstream_.get(); }
588 
get_loop() const589 struct ev_loop *ClientHandler::get_loop() const { return conn_.loop; }
590 
reset_upstream_read_timeout(ev_tstamp t)591 void ClientHandler::reset_upstream_read_timeout(ev_tstamp t) {
592   conn_.rt.repeat = t;
593 
594   ev_timer_again(conn_.loop, &conn_.rt);
595 }
596 
reset_upstream_write_timeout(ev_tstamp t)597 void ClientHandler::reset_upstream_write_timeout(ev_tstamp t) {
598   conn_.wt.repeat = t;
599 
600   ev_timer_again(conn_.loop, &conn_.wt);
601 }
602 
repeat_read_timer()603 void ClientHandler::repeat_read_timer() {
604   ev_timer_again(conn_.loop, &conn_.rt);
605 }
606 
stop_read_timer()607 void ClientHandler::stop_read_timer() { ev_timer_stop(conn_.loop, &conn_.rt); }
608 
validate_next_proto()609 int ClientHandler::validate_next_proto() {
610   const unsigned char *next_proto = nullptr;
611   unsigned int next_proto_len = 0;
612 
613   // First set callback for catch all cases
614   on_read_ = &ClientHandler::upstream_read;
615 
616   SSL_get0_alpn_selected(conn_.tls.ssl, &next_proto, &next_proto_len);
617 
618   StringRef proto;
619 
620   if (next_proto) {
621     proto = StringRef{next_proto, next_proto_len};
622 
623     if (LOG_ENABLED(INFO)) {
624       CLOG(INFO, this) << "The negotiated next protocol: " << proto;
625     }
626   } else {
627     if (LOG_ENABLED(INFO)) {
628       CLOG(INFO, this) << "No protocol negotiated. Fallback to HTTP/1.1";
629     }
630 
631     proto = "http/1.1"_sr;
632   }
633 
634   if (!tls::in_proto_list(get_config()->tls.alpn_list, proto)) {
635     if (LOG_ENABLED(INFO)) {
636       CLOG(INFO, this) << "The negotiated protocol is not supported: " << proto;
637     }
638     return -1;
639   }
640 
641   if (util::check_h2_is_selected(proto)) {
642     on_read_ = &ClientHandler::upstream_http2_connhd_read;
643 
644     auto http2_upstream = std::make_unique<Http2Upstream>(this);
645 
646     upstream_ = std::move(http2_upstream);
647     alpn_ = make_string_ref(balloc_, proto);
648 
649     // At this point, input buffer is already filled with some bytes.
650     // The read callback is not called until new data come. So consume
651     // input buffer here.
652     if (on_read() != 0) {
653       return -1;
654     }
655 
656     return 0;
657   }
658 
659   if (proto == "http/1.1"_sr) {
660     upstream_ = std::make_unique<HttpsUpstream>(this);
661     alpn_ = "http/1.1"_sr;
662 
663     // At this point, input buffer is already filled with some bytes.
664     // The read callback is not called until new data come. So consume
665     // input buffer here.
666     if (on_read() != 0) {
667       return -1;
668     }
669 
670     return 0;
671   }
672   if (LOG_ENABLED(INFO)) {
673     CLOG(INFO, this) << "The negotiated protocol is not supported";
674   }
675   return -1;
676 }
677 
do_read()678 int ClientHandler::do_read() { return read_(*this); }
do_write()679 int ClientHandler::do_write() { return write_(*this); }
680 
on_read()681 int ClientHandler::on_read() {
682   if (rb_.chunk_avail()) {
683     auto rv = on_read_(*this);
684     if (rv != 0) {
685       return rv;
686     }
687   }
688   conn_.handle_tls_pending_read();
689   return 0;
690 }
on_write()691 int ClientHandler::on_write() { return on_write_(*this); }
692 
get_ipaddr() const693 const StringRef &ClientHandler::get_ipaddr() const { return ipaddr_; }
694 
get_should_close_after_write() const695 bool ClientHandler::get_should_close_after_write() const {
696   return should_close_after_write_;
697 }
698 
set_should_close_after_write(bool f)699 void ClientHandler::set_should_close_after_write(bool f) {
700   should_close_after_write_ = f;
701 }
702 
pool_downstream_connection(std::unique_ptr<DownstreamConnection> dconn)703 void ClientHandler::pool_downstream_connection(
704   std::unique_ptr<DownstreamConnection> dconn) {
705   if (!dconn->poolable()) {
706     return;
707   }
708 
709   dconn->set_client_handler(nullptr);
710 
711   auto &group = dconn->get_downstream_addr_group();
712 
713   if (LOG_ENABLED(INFO)) {
714     CLOG(INFO, this) << "Pooling downstream connection DCONN:" << dconn.get()
715                      << " in group " << group;
716   }
717 
718   auto addr = dconn->get_addr();
719   auto &dconn_pool = addr->dconn_pool;
720   dconn_pool->add_downstream_connection(std::move(dconn));
721 }
722 
723 namespace {
724 // Computes 32bits hash for session affinity for IP address |ip|.
compute_affinity_from_ip(const StringRef & ip)725 uint32_t compute_affinity_from_ip(const StringRef &ip) {
726   int rv;
727   std::array<uint8_t, 32> buf;
728 
729   rv = util::sha256(buf.data(), ip);
730   if (rv != 0) {
731     // Not sure when sha256 failed.  Just fall back to another
732     // function.
733     return util::hash32(ip);
734   }
735 
736   return (static_cast<uint32_t>(buf[0]) << 24) |
737          (static_cast<uint32_t>(buf[1]) << 16) |
738          (static_cast<uint32_t>(buf[2]) << 8) | static_cast<uint32_t>(buf[3]);
739 }
740 } // namespace
741 
get_http2_session(const std::shared_ptr<DownstreamAddrGroup> & group,DownstreamAddr * addr)742 Http2Session *ClientHandler::get_http2_session(
743   const std::shared_ptr<DownstreamAddrGroup> &group, DownstreamAddr *addr) {
744   auto &shared_addr = group->shared_addr;
745 
746   if (LOG_ENABLED(INFO)) {
747     CLOG(INFO, this) << "Selected DownstreamAddr=" << addr
748                      << ", index=" << (addr - shared_addr->addrs.data());
749   }
750 
751   for (auto session = addr->http2_extra_freelist.head; session;) {
752     auto next = session->dlnext;
753 
754     if (session->max_concurrency_reached(0)) {
755       if (LOG_ENABLED(INFO)) {
756         CLOG(INFO, this)
757           << "Maximum streams have been reached for Http2Session(" << session
758           << ").  Skip it";
759       }
760 
761       session->remove_from_freelist();
762       session = next;
763 
764       continue;
765     }
766 
767     if (LOG_ENABLED(INFO)) {
768       CLOG(INFO, this) << "Use Http2Session " << session
769                        << " from http2_extra_freelist";
770     }
771 
772     if (session->max_concurrency_reached(1)) {
773       if (LOG_ENABLED(INFO)) {
774         CLOG(INFO, this) << "Maximum streams are reached for Http2Session("
775                          << session << ").";
776       }
777 
778       session->remove_from_freelist();
779     }
780     return session;
781   }
782 
783   auto session = new Http2Session(conn_.loop, worker_->get_cl_ssl_ctx(),
784                                   worker_, group, addr);
785 
786   if (LOG_ENABLED(INFO)) {
787     CLOG(INFO, this) << "Create new Http2Session " << session;
788   }
789 
790   session->add_to_extra_freelist();
791 
792   return session;
793 }
794 
get_affinity_cookie(Downstream * downstream,const StringRef & cookie_name)795 uint32_t ClientHandler::get_affinity_cookie(Downstream *downstream,
796                                             const StringRef &cookie_name) {
797   auto h = downstream->find_affinity_cookie(cookie_name);
798   if (h) {
799     return h;
800   }
801 
802   auto d = std::uniform_int_distribution<uint32_t>(1);
803   auto rh = d(worker_->get_randgen());
804   h = util::hash32(StringRef{reinterpret_cast<char *>(&rh), sizeof(rh)});
805 
806   downstream->renew_affinity_cookie(h);
807 
808   return h;
809 }
810 
811 namespace {
reschedule_addr(std::priority_queue<DownstreamAddrEntry,std::vector<DownstreamAddrEntry>,DownstreamAddrEntryGreater> & pq,DownstreamAddr * addr)812 void reschedule_addr(
813   std::priority_queue<DownstreamAddrEntry, std::vector<DownstreamAddrEntry>,
814                       DownstreamAddrEntryGreater> &pq,
815   DownstreamAddr *addr) {
816   auto penalty = MAX_DOWNSTREAM_ADDR_WEIGHT + addr->pending_penalty;
817   addr->cycle += penalty / addr->weight;
818   addr->pending_penalty = penalty % addr->weight;
819 
820   pq.push(DownstreamAddrEntry{addr, addr->seq, addr->cycle});
821   addr->queued = true;
822 }
823 } // namespace
824 
825 namespace {
reschedule_wg(std::priority_queue<WeightGroupEntry,std::vector<WeightGroupEntry>,WeightGroupEntryGreater> & pq,WeightGroup * wg)826 void reschedule_wg(
827   std::priority_queue<WeightGroupEntry, std::vector<WeightGroupEntry>,
828                       WeightGroupEntryGreater> &pq,
829   WeightGroup *wg) {
830   auto penalty = MAX_DOWNSTREAM_ADDR_WEIGHT + wg->pending_penalty;
831   wg->cycle += penalty / wg->weight;
832   wg->pending_penalty = penalty % wg->weight;
833 
834   pq.push(WeightGroupEntry{wg, wg->seq, wg->cycle});
835   wg->queued = true;
836 }
837 } // namespace
838 
get_downstream_addr(int & err,DownstreamAddrGroup * group,Downstream * downstream)839 DownstreamAddr *ClientHandler::get_downstream_addr(int &err,
840                                                    DownstreamAddrGroup *group,
841                                                    Downstream *downstream) {
842   err = 0;
843 
844   switch (faddr_->alt_mode) {
845   case UpstreamAltMode::API:
846   case UpstreamAltMode::HEALTHMON:
847     assert(0);
848   default:
849     break;
850   }
851 
852   auto &shared_addr = group->shared_addr;
853 
854   if (shared_addr->affinity.type != SessionAffinity::NONE) {
855     uint32_t hash;
856     switch (shared_addr->affinity.type) {
857     case SessionAffinity::IP:
858       if (!affinity_hash_computed_) {
859         affinity_hash_ = compute_affinity_from_ip(ipaddr_);
860         affinity_hash_computed_ = true;
861       }
862       hash = affinity_hash_;
863       break;
864     case SessionAffinity::COOKIE:
865       if (shared_addr->affinity.cookie.stickiness ==
866           SessionAffinityCookieStickiness::STRICT) {
867         return get_downstream_addr_strict_affinity(err, shared_addr,
868                                                    downstream);
869       }
870 
871       hash = get_affinity_cookie(downstream, shared_addr->affinity.cookie.name);
872       break;
873     default:
874       assert(0);
875     }
876 
877     const auto &affinity_hash = shared_addr->affinity_hash;
878 
879     auto it = std::lower_bound(
880       std::begin(affinity_hash), std::end(affinity_hash), hash,
881       [](const AffinityHash &lhs, uint32_t rhs) { return lhs.hash < rhs; });
882 
883     if (it == std::end(affinity_hash)) {
884       it = std::begin(affinity_hash);
885     }
886 
887     auto aff_idx =
888       static_cast<size_t>(std::distance(std::begin(affinity_hash), it));
889     auto idx = (*it).idx;
890     auto addr = &shared_addr->addrs[idx];
891 
892     if (addr->connect_blocker->blocked()) {
893       size_t i;
894       for (i = aff_idx + 1; i != aff_idx; ++i) {
895         if (i == shared_addr->affinity_hash.size()) {
896           i = 0;
897         }
898         addr = &shared_addr->addrs[shared_addr->affinity_hash[i].idx];
899         if (addr->connect_blocker->blocked()) {
900           continue;
901         }
902         break;
903       }
904       if (i == aff_idx) {
905         err = -1;
906         return nullptr;
907       }
908     }
909 
910     return addr;
911   }
912 
913   auto &wgpq = shared_addr->pq;
914 
915   for (;;) {
916     if (wgpq.empty()) {
917       CLOG(INFO, this) << "No working downstream address found";
918       err = -1;
919       return nullptr;
920     }
921 
922     auto wg = wgpq.top().wg;
923     wgpq.pop();
924     wg->queued = false;
925 
926     for (;;) {
927       if (wg->pq.empty()) {
928         break;
929       }
930 
931       auto addr = wg->pq.top().addr;
932       wg->pq.pop();
933       addr->queued = false;
934 
935       if (addr->connect_blocker->blocked()) {
936         continue;
937       }
938 
939       reschedule_addr(wg->pq, addr);
940       reschedule_wg(wgpq, wg);
941 
942       return addr;
943     }
944   }
945 }
946 
get_downstream_addr_strict_affinity(int & err,const std::shared_ptr<SharedDownstreamAddr> & shared_addr,Downstream * downstream)947 DownstreamAddr *ClientHandler::get_downstream_addr_strict_affinity(
948   int &err, const std::shared_ptr<SharedDownstreamAddr> &shared_addr,
949   Downstream *downstream) {
950   const auto &affinity_hash = shared_addr->affinity_hash;
951 
952   auto h = downstream->find_affinity_cookie(shared_addr->affinity.cookie.name);
953   if (h) {
954     auto it = shared_addr->affinity_hash_map.find(h);
955     if (it != std::end(shared_addr->affinity_hash_map)) {
956       auto addr = &shared_addr->addrs[(*it).second];
957       if (!addr->connect_blocker->blocked()) {
958         return addr;
959       }
960     }
961   } else {
962     auto d = std::uniform_int_distribution<uint32_t>(1);
963     auto rh = d(worker_->get_randgen());
964     h = util::hash32(StringRef{reinterpret_cast<char *>(&rh), sizeof(rh)});
965   }
966 
967   // Client is not bound to a particular backend, or the bound backend
968   // is not found, or is blocked.  Find new backend using h.  Using
969   // existing h allows us to find new server in a deterministic way.
970   // It is preferable because multiple concurrent requests with the
971   // stale cookie might be in-flight.
972   auto it = std::lower_bound(
973     std::begin(affinity_hash), std::end(affinity_hash), h,
974     [](const AffinityHash &lhs, uint32_t rhs) { return lhs.hash < rhs; });
975 
976   if (it == std::end(affinity_hash)) {
977     it = std::begin(affinity_hash);
978   }
979 
980   auto aff_idx =
981     static_cast<size_t>(std::distance(std::begin(affinity_hash), it));
982   auto idx = (*it).idx;
983   auto addr = &shared_addr->addrs[idx];
984 
985   if (addr->connect_blocker->blocked()) {
986     size_t i;
987     for (i = aff_idx + 1; i != aff_idx; ++i) {
988       if (i == shared_addr->affinity_hash.size()) {
989         i = 0;
990       }
991       addr = &shared_addr->addrs[shared_addr->affinity_hash[i].idx];
992       if (addr->connect_blocker->blocked()) {
993         continue;
994       }
995       break;
996     }
997     if (i == aff_idx) {
998       err = -1;
999       return nullptr;
1000     }
1001   }
1002 
1003   downstream->renew_affinity_cookie(addr->affinity_hash);
1004 
1005   return addr;
1006 }
1007 
1008 std::unique_ptr<DownstreamConnection>
get_downstream_connection(int & err,Downstream * downstream)1009 ClientHandler::get_downstream_connection(int &err, Downstream *downstream) {
1010   size_t group_idx;
1011   auto &downstreamconf = *worker_->get_downstream_config();
1012   auto &routerconf = downstreamconf.router;
1013 
1014   auto catch_all = downstreamconf.addr_group_catch_all;
1015   auto &groups = worker_->get_downstream_addr_groups();
1016 
1017   auto &req = downstream->request();
1018 
1019   err = 0;
1020 
1021   switch (faddr_->alt_mode) {
1022   case UpstreamAltMode::API: {
1023     auto dconn = std::make_unique<APIDownstreamConnection>(worker_);
1024     dconn->set_client_handler(this);
1025     return dconn;
1026   }
1027   case UpstreamAltMode::HEALTHMON: {
1028     auto dconn = std::make_unique<HealthMonitorDownstreamConnection>();
1029     dconn->set_client_handler(this);
1030     return dconn;
1031   }
1032   default:
1033     break;
1034   }
1035 
1036   auto &balloc = downstream->get_block_allocator();
1037 
1038   StringRef authority, path;
1039 
1040   if (req.forwarded_once) {
1041     if (groups.size() != 1) {
1042       authority = req.orig_authority;
1043       path = req.orig_path;
1044     }
1045   } else {
1046     if (faddr_->sni_fwd) {
1047       authority = sni_;
1048     } else if (!req.authority.empty()) {
1049       authority = req.authority;
1050     } else {
1051       auto h = req.fs.header(http2::HD_HOST);
1052       if (h) {
1053         authority = h->value;
1054       }
1055     }
1056 
1057     // CONNECT method does not have path.  But we requires path in
1058     // host-path mapping.  As workaround, we assume that path is
1059     // "/".
1060     if (!req.regular_connect_method()) {
1061       path = req.path;
1062     }
1063 
1064     // Cache the authority and path used for the first-time backend
1065     // selection because per-pattern mruby script can change them.
1066     req.orig_authority = authority;
1067     req.orig_path = path;
1068     req.forwarded_once = true;
1069   }
1070 
1071   // Fast path.  If we have one group, it must be catch-all group.
1072   if (groups.size() == 1) {
1073     group_idx = 0;
1074   } else {
1075     group_idx = match_downstream_addr_group(routerconf, authority, path, groups,
1076                                             catch_all, balloc);
1077   }
1078 
1079   if (LOG_ENABLED(INFO)) {
1080     CLOG(INFO, this) << "Downstream address group_idx: " << group_idx;
1081   }
1082 
1083   if (groups[group_idx]->shared_addr->redirect_if_not_tls && !conn_.tls.ssl) {
1084     if (LOG_ENABLED(INFO)) {
1085       CLOG(INFO, this) << "Downstream address group " << group_idx
1086                        << " requires frontend TLS connection.";
1087     }
1088     err = SHRPX_ERR_TLS_REQUIRED;
1089     return nullptr;
1090   }
1091 
1092   auto &group = groups[group_idx];
1093 
1094   if (group->shared_addr->dnf) {
1095     auto dconn = std::make_unique<NullDownstreamConnection>(group);
1096     dconn->set_client_handler(this);
1097     return dconn;
1098   }
1099 
1100   auto addr = get_downstream_addr(err, group.get(), downstream);
1101   if (addr == nullptr) {
1102     return nullptr;
1103   }
1104 
1105   if (addr->proto == Proto::HTTP1) {
1106     auto dconn = addr->dconn_pool->pop_downstream_connection();
1107     if (dconn) {
1108       dconn->set_client_handler(this);
1109       return dconn;
1110     }
1111 
1112     if (worker_->get_connect_blocker()->blocked()) {
1113       if (LOG_ENABLED(INFO)) {
1114         DCLOG(INFO, this)
1115           << "Worker wide backend connection was blocked temporarily";
1116       }
1117       return nullptr;
1118     }
1119 
1120     if (LOG_ENABLED(INFO)) {
1121       CLOG(INFO, this) << "Downstream connection pool is empty."
1122                        << " Create new one";
1123     }
1124 
1125     dconn = std::make_unique<HttpDownstreamConnection>(group, addr, conn_.loop,
1126                                                        worker_);
1127     dconn->set_client_handler(this);
1128     return dconn;
1129   }
1130 
1131   if (LOG_ENABLED(INFO)) {
1132     CLOG(INFO, this) << "Downstream connection pool is empty."
1133                      << " Create new one";
1134   }
1135 
1136   auto http2session = get_http2_session(group, addr);
1137   auto dconn = std::make_unique<Http2DownstreamConnection>(http2session);
1138   dconn->set_client_handler(this);
1139   return dconn;
1140 }
1141 
get_mcpool()1142 MemchunkPool *ClientHandler::get_mcpool() { return worker_->get_mcpool(); }
1143 
get_ssl() const1144 SSL *ClientHandler::get_ssl() const { return conn_.tls.ssl; }
1145 
direct_http2_upgrade()1146 void ClientHandler::direct_http2_upgrade() {
1147   upstream_ = std::make_unique<Http2Upstream>(this);
1148   alpn_ = NGHTTP2_CLEARTEXT_PROTO_VERSION_ID ""_sr;
1149   on_read_ = &ClientHandler::upstream_read;
1150   write_ = &ClientHandler::write_clear;
1151 }
1152 
perform_http2_upgrade(HttpsUpstream * http)1153 int ClientHandler::perform_http2_upgrade(HttpsUpstream *http) {
1154   auto upstream = std::make_unique<Http2Upstream>(this);
1155 
1156   auto output = upstream->get_response_buf();
1157 
1158   // We might have written non-final header in response_buf, in this
1159   // case, response_state is still INITIAL.  If this non-final header
1160   // and upgrade header fit in output buffer, do upgrade.  Otherwise,
1161   // to avoid to send this non-final header as response body in HTTP/2
1162   // upstream, fail upgrade.
1163   auto downstream = http->get_downstream();
1164   auto input = downstream->get_response_buf();
1165 
1166   if (upstream->upgrade_upstream(http) != 0) {
1167     return -1;
1168   }
1169   // http pointer is now owned by upstream.
1170   upstream_.release();
1171   // TODO We might get other version id in HTTP2-settings, if we
1172   // support aliasing for h2, but we just use library default for now.
1173   alpn_ = NGHTTP2_CLEARTEXT_PROTO_VERSION_ID ""_sr;
1174   on_read_ = &ClientHandler::upstream_http2_connhd_read;
1175   write_ = &ClientHandler::write_clear;
1176 
1177   input->remove(*output, input->rleft());
1178 
1179   constexpr auto res = "HTTP/1.1 101 Switching Protocols\r\n"
1180                        "Connection: Upgrade\r\n"
1181                        "Upgrade: " NGHTTP2_CLEARTEXT_PROTO_VERSION_ID "\r\n"
1182                        "\r\n"_sr;
1183 
1184   output->append(res);
1185   upstream_ = std::move(upstream);
1186 
1187   signal_write();
1188   return 0;
1189 }
1190 
get_http2_upgrade_allowed() const1191 bool ClientHandler::get_http2_upgrade_allowed() const { return !conn_.tls.ssl; }
1192 
get_upstream_scheme() const1193 StringRef ClientHandler::get_upstream_scheme() const {
1194   if (conn_.tls.ssl) {
1195     return "https"_sr;
1196   } else {
1197     return "http"_sr;
1198   }
1199 }
1200 
start_immediate_shutdown()1201 void ClientHandler::start_immediate_shutdown() {
1202   ev_timer_start(conn_.loop, &reneg_shutdown_timer_);
1203 }
1204 
write_accesslog(Downstream * downstream)1205 void ClientHandler::write_accesslog(Downstream *downstream) {
1206   auto &req = downstream->request();
1207 
1208   auto config = get_config();
1209 
1210   if (!req.tstamp) {
1211     auto lgconf = log_config();
1212     lgconf->update_tstamp(std::chrono::system_clock::now());
1213     req.tstamp = lgconf->tstamp;
1214   }
1215 
1216   upstream_accesslog(
1217     config->logging.access.format,
1218     LogSpec{
1219       downstream,
1220       ipaddr_,
1221       alpn_,
1222       sni_,
1223       conn_.tls.ssl,
1224       std::chrono::high_resolution_clock::now(), // request_end_time
1225       port_,
1226       faddr_->port,
1227       config->pid,
1228     });
1229 }
1230 
get_rb()1231 ClientHandler::ReadBuf *ClientHandler::get_rb() { return &rb_; }
1232 
signal_write()1233 void ClientHandler::signal_write() { conn_.wlimit.startw(); }
1234 
get_rlimit()1235 RateLimit *ClientHandler::get_rlimit() { return &conn_.rlimit; }
get_wlimit()1236 RateLimit *ClientHandler::get_wlimit() { return &conn_.wlimit; }
1237 
get_wev()1238 ev_io *ClientHandler::get_wev() { return &conn_.wev; }
1239 
get_worker() const1240 Worker *ClientHandler::get_worker() const { return worker_; }
1241 
1242 namespace {
parse_proxy_line_port(const uint8_t * first,const uint8_t * last)1243 ssize_t parse_proxy_line_port(const uint8_t *first, const uint8_t *last) {
1244   auto p = first;
1245   int32_t port = 0;
1246 
1247   if (p == last) {
1248     return -1;
1249   }
1250 
1251   if (*p == '0') {
1252     if (p + 1 != last && util::is_digit(*(p + 1))) {
1253       return -1;
1254     }
1255     return 1;
1256   }
1257 
1258   for (; p != last && util::is_digit(*p); ++p) {
1259     port *= 10;
1260     port += *p - '0';
1261 
1262     if (port > 65535) {
1263       return -1;
1264     }
1265   }
1266 
1267   return p - first;
1268 }
1269 } // namespace
1270 
on_proxy_protocol_finish()1271 int ClientHandler::on_proxy_protocol_finish() {
1272   auto len = rb_.pos() - rb_.begin();
1273 
1274   assert(len);
1275 
1276   if (LOG_ENABLED(INFO)) {
1277     CLOG(INFO, this) << "PROXY-protocol: Draining " << len
1278                      << " bytes from socket";
1279   }
1280 
1281   rb_.reset();
1282 
1283   if (conn_.read_nolim_clear(rb_.pos(), len) < 0) {
1284     return -1;
1285   }
1286 
1287   rb_.reset();
1288 
1289   setup_upstream_io_callback();
1290 
1291   return 0;
1292 }
1293 
1294 namespace {
1295 // PROXY-protocol v2 header signature
1296 constexpr uint8_t PROXY_PROTO_V2_SIG[] =
1297   "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A";
1298 
1299 // PROXY-protocol v2 header length
1300 constexpr size_t PROXY_PROTO_V2_HDLEN =
1301   str_size(PROXY_PROTO_V2_SIG) + /* ver_cmd(1) + fam(1) + len(2) = */ 4;
1302 } // namespace
1303 
1304 // http://www.haproxy.org/download/1.5/doc/proxy-protocol.txt
proxy_protocol_read()1305 int ClientHandler::proxy_protocol_read() {
1306   if (LOG_ENABLED(INFO)) {
1307     CLOG(INFO, this) << "PROXY-protocol: Started";
1308   }
1309 
1310   auto first = rb_.pos();
1311 
1312   if (rb_.rleft() >= PROXY_PROTO_V2_HDLEN &&
1313       (*(first + str_size(PROXY_PROTO_V2_SIG)) & 0xf0) == 0x20) {
1314     if (LOG_ENABLED(INFO)) {
1315       CLOG(INFO, this) << "PROXY-protocol: Detected v2 header signature";
1316     }
1317     return proxy_protocol_v2_read();
1318   }
1319 
1320   // NULL character really destroys functions which expects NULL
1321   // terminated string.  We won't expect it in PROXY protocol line, so
1322   // find it here.
1323   auto chrs = std::to_array({'\n', '\0'});
1324 
1325   constexpr size_t MAX_PROXY_LINELEN = 107;
1326 
1327   auto bufend = rb_.pos() + std::min(MAX_PROXY_LINELEN, rb_.rleft());
1328 
1329   auto end =
1330     std::find_first_of(rb_.pos(), bufend, std::begin(chrs), std::end(chrs));
1331 
1332   if (end == bufend || *end == '\0' || end == rb_.pos() || *(end - 1) != '\r') {
1333     if (LOG_ENABLED(INFO)) {
1334       CLOG(INFO, this) << "PROXY-protocol-v1: No ending CR LF sequence found";
1335     }
1336     return -1;
1337   }
1338 
1339   --end;
1340 
1341   constexpr auto HEADER = "PROXY "_sr;
1342 
1343   if (static_cast<size_t>(end - rb_.pos()) < HEADER.size()) {
1344     if (LOG_ENABLED(INFO)) {
1345       CLOG(INFO, this) << "PROXY-protocol-v1: PROXY version 1 ID not found";
1346     }
1347     return -1;
1348   }
1349 
1350   if (HEADER != StringRef{rb_.pos(), HEADER.size()}) {
1351     if (LOG_ENABLED(INFO)) {
1352       CLOG(INFO, this) << "PROXY-protocol-v1: Bad PROXY protocol version 1 ID";
1353     }
1354     return -1;
1355   }
1356 
1357   rb_.drain(HEADER.size());
1358 
1359   int family;
1360 
1361   if (rb_.pos()[0] == 'T') {
1362     if (end - rb_.pos() < 5) {
1363       if (LOG_ENABLED(INFO)) {
1364         CLOG(INFO, this) << "PROXY-protocol-v1: INET protocol family not found";
1365       }
1366       return -1;
1367     }
1368 
1369     if (rb_.pos()[1] != 'C' || rb_.pos()[2] != 'P') {
1370       if (LOG_ENABLED(INFO)) {
1371         CLOG(INFO, this) << "PROXY-protocol-v1: Unknown INET protocol family";
1372       }
1373       return -1;
1374     }
1375 
1376     switch (rb_.pos()[3]) {
1377     case '4':
1378       family = AF_INET;
1379       break;
1380     case '6':
1381       family = AF_INET6;
1382       break;
1383     default:
1384       if (LOG_ENABLED(INFO)) {
1385         CLOG(INFO, this) << "PROXY-protocol-v1: Unknown INET protocol family";
1386       }
1387       return -1;
1388     }
1389 
1390     rb_.drain(5);
1391   } else {
1392     if (end - rb_.pos() < 7) {
1393       if (LOG_ENABLED(INFO)) {
1394         CLOG(INFO, this) << "PROXY-protocol-v1: INET protocol family not found";
1395       }
1396       return -1;
1397     }
1398     if ("UNKNOWN"_sr != StringRef{rb_.pos(), 7}) {
1399       if (LOG_ENABLED(INFO)) {
1400         CLOG(INFO, this) << "PROXY-protocol-v1: Unknown INET protocol family";
1401       }
1402       return -1;
1403     }
1404 
1405     rb_.drain(end + 2 - rb_.pos());
1406 
1407     return on_proxy_protocol_finish();
1408   }
1409 
1410   // source address
1411   auto token_end = std::find(rb_.pos(), end, ' ');
1412   if (token_end == end) {
1413     if (LOG_ENABLED(INFO)) {
1414       CLOG(INFO, this) << "PROXY-protocol-v1: Source address not found";
1415     }
1416     return -1;
1417   }
1418 
1419   *token_end = '\0';
1420   if (!util::numeric_host(reinterpret_cast<const char *>(rb_.pos()), family)) {
1421     if (LOG_ENABLED(INFO)) {
1422       CLOG(INFO, this) << "PROXY-protocol-v1: Invalid source address";
1423     }
1424     return -1;
1425   }
1426 
1427   auto src_addr = rb_.pos();
1428   auto src_addrlen = token_end - rb_.pos();
1429 
1430   rb_.drain(token_end - rb_.pos() + 1);
1431 
1432   // destination address
1433   token_end = std::find(rb_.pos(), end, ' ');
1434   if (token_end == end) {
1435     if (LOG_ENABLED(INFO)) {
1436       CLOG(INFO, this) << "PROXY-protocol-v1: Destination address not found";
1437     }
1438     return -1;
1439   }
1440 
1441   *token_end = '\0';
1442   if (!util::numeric_host(reinterpret_cast<const char *>(rb_.pos()), family)) {
1443     if (LOG_ENABLED(INFO)) {
1444       CLOG(INFO, this) << "PROXY-protocol-v1: Invalid destination address";
1445     }
1446     return -1;
1447   }
1448 
1449   // Currently we don't use destination address
1450 
1451   rb_.drain(token_end - rb_.pos() + 1);
1452 
1453   // source port
1454   auto n = parse_proxy_line_port(rb_.pos(), end);
1455   if (n <= 0 || *(rb_.pos() + n) != ' ') {
1456     if (LOG_ENABLED(INFO)) {
1457       CLOG(INFO, this) << "PROXY-protocol-v1: Invalid source port";
1458     }
1459     return -1;
1460   }
1461 
1462   rb_.pos()[n] = '\0';
1463   auto src_port = rb_.pos();
1464   auto src_portlen = n;
1465 
1466   rb_.drain(n + 1);
1467 
1468   // destination  port
1469   n = parse_proxy_line_port(rb_.pos(), end);
1470   if (n <= 0 || rb_.pos() + n != end) {
1471     if (LOG_ENABLED(INFO)) {
1472       CLOG(INFO, this) << "PROXY-protocol-v1: Invalid destination port";
1473     }
1474     return -1;
1475   }
1476 
1477   // Currently we don't use destination port
1478 
1479   rb_.drain(end + 2 - rb_.pos());
1480 
1481   ipaddr_ = make_string_ref(
1482     balloc_, StringRef{src_addr, static_cast<size_t>(src_addrlen)});
1483   port_ = make_string_ref(
1484     balloc_, StringRef{src_port, static_cast<size_t>(src_portlen)});
1485 
1486   if (LOG_ENABLED(INFO)) {
1487     CLOG(INFO, this) << "PROXY-protocol-v1: Finished, " << (rb_.pos() - first)
1488                      << " bytes read";
1489   }
1490 
1491   auto config = get_config();
1492   auto &fwdconf = config->http.forwarded;
1493 
1494   if ((fwdconf.params & FORWARDED_FOR) &&
1495       fwdconf.for_node_type == ForwardedNode::IP) {
1496     init_forwarded_for(family, ipaddr_);
1497   }
1498 
1499   return on_proxy_protocol_finish();
1500 }
1501 
proxy_protocol_v2_read()1502 int ClientHandler::proxy_protocol_v2_read() {
1503   // Assume that first str_size(PROXY_PROTO_V2_SIG) octets match v2
1504   // protocol signature and followed by the bytes which indicates v2.
1505   assert(rb_.rleft() >= PROXY_PROTO_V2_HDLEN);
1506 
1507   auto p = rb_.pos() + str_size(PROXY_PROTO_V2_SIG);
1508 
1509   assert(((*p) & 0xf0) == 0x20);
1510 
1511   enum { LOCAL, PROXY } cmd;
1512 
1513   auto cmd_bits = (*p++) & 0xf;
1514   switch (cmd_bits) {
1515   case 0x0:
1516     cmd = LOCAL;
1517     break;
1518   case 0x01:
1519     cmd = PROXY;
1520     break;
1521   default:
1522     if (LOG_ENABLED(INFO)) {
1523       CLOG(INFO, this) << "PROXY-protocol-v2: Unknown command " << log::hex
1524                        << cmd_bits;
1525     }
1526     return -1;
1527   }
1528 
1529   auto fam = *p++;
1530   uint16_t len;
1531   memcpy(&len, p, sizeof(len));
1532   len = ntohs(len);
1533 
1534   p += sizeof(len);
1535 
1536   if (LOG_ENABLED(INFO)) {
1537     CLOG(INFO, this) << "PROXY-protocol-v2: Detected family=" << log::hex << fam
1538                      << ", len=" << log::dec << len;
1539   }
1540 
1541   if (rb_.last() - p < len) {
1542     if (LOG_ENABLED(INFO)) {
1543       CLOG(INFO, this)
1544         << "PROXY-protocol-v2: Prematurely truncated header block; require "
1545         << len << " bytes, " << rb_.last() - p << " bytes left";
1546     }
1547     return -1;
1548   }
1549 
1550   int family;
1551   std::array<char, std::max(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)> src_addr,
1552     dst_addr;
1553   size_t addrlen;
1554 
1555   switch (fam) {
1556   case 0x11:
1557   case 0x12:
1558     if (len < 12) {
1559       if (LOG_ENABLED(INFO)) {
1560         CLOG(INFO, this) << "PROXY-protocol-v2: Too short AF_INET addresses";
1561       }
1562       return -1;
1563     }
1564     family = AF_INET;
1565     addrlen = 4;
1566     break;
1567   case 0x21:
1568   case 0x22:
1569     if (len < 36) {
1570       if (LOG_ENABLED(INFO)) {
1571         CLOG(INFO, this) << "PROXY-protocol-v2: Too short AF_INET6 addresses";
1572       }
1573       return -1;
1574     }
1575     family = AF_INET6;
1576     addrlen = 16;
1577     break;
1578   case 0x31:
1579   case 0x32:
1580     if (len < 216) {
1581       if (LOG_ENABLED(INFO)) {
1582         CLOG(INFO, this) << "PROXY-protocol-v2: Too short AF_UNIX addresses";
1583       }
1584       return -1;
1585     }
1586     // fall through
1587   case 0x00: {
1588     // UNSPEC and UNIX are just ignored.
1589     if (LOG_ENABLED(INFO)) {
1590       CLOG(INFO, this) << "PROXY-protocol-v2: Ignore combination of address "
1591                           "family and protocol "
1592                        << log::hex << fam;
1593     }
1594     rb_.drain(PROXY_PROTO_V2_HDLEN + len);
1595     return on_proxy_protocol_finish();
1596   }
1597   default:
1598     if (LOG_ENABLED(INFO)) {
1599       CLOG(INFO, this) << "PROXY-protocol-v2: Unknown combination of address "
1600                           "family and protocol "
1601                        << log::hex << fam;
1602     }
1603     return -1;
1604   }
1605 
1606   if (cmd != PROXY) {
1607     if (LOG_ENABLED(INFO)) {
1608       CLOG(INFO, this) << "PROXY-protocol-v2: Ignore non-PROXY command";
1609     }
1610     rb_.drain(PROXY_PROTO_V2_HDLEN + len);
1611     return on_proxy_protocol_finish();
1612   }
1613 
1614   if (inet_ntop(family, p, src_addr.data(), src_addr.size()) == nullptr) {
1615     if (LOG_ENABLED(INFO)) {
1616       CLOG(INFO, this) << "PROXY-protocol-v2: Unable to parse source address";
1617     }
1618     return -1;
1619   }
1620 
1621   p += addrlen;
1622 
1623   if (inet_ntop(family, p, dst_addr.data(), dst_addr.size()) == nullptr) {
1624     if (LOG_ENABLED(INFO)) {
1625       CLOG(INFO, this)
1626         << "PROXY-protocol-v2: Unable to parse destination address";
1627     }
1628     return -1;
1629   }
1630 
1631   p += addrlen;
1632 
1633   uint16_t src_port;
1634 
1635   memcpy(&src_port, p, sizeof(src_port));
1636   src_port = ntohs(src_port);
1637 
1638   // We don't use destination port.
1639   p += 4;
1640 
1641   ipaddr_ = make_string_ref(balloc_, StringRef{src_addr.data()});
1642   port_ = util::make_string_ref_uint(balloc_, src_port);
1643 
1644   if (LOG_ENABLED(INFO)) {
1645     CLOG(INFO, this) << "PROXY-protocol-v2: Finished reading proxy addresses, "
1646                      << p - rb_.pos() << " bytes read, "
1647                      << PROXY_PROTO_V2_HDLEN + len - (p - rb_.pos())
1648                      << " bytes left";
1649   }
1650 
1651   auto config = get_config();
1652   auto &fwdconf = config->http.forwarded;
1653 
1654   if ((fwdconf.params & FORWARDED_FOR) &&
1655       fwdconf.for_node_type == ForwardedNode::IP) {
1656     init_forwarded_for(family, ipaddr_);
1657   }
1658 
1659   rb_.drain(PROXY_PROTO_V2_HDLEN + len);
1660   return on_proxy_protocol_finish();
1661 }
1662 
get_forwarded_by() const1663 StringRef ClientHandler::get_forwarded_by() const {
1664   auto &fwdconf = get_config()->http.forwarded;
1665 
1666   if (fwdconf.by_node_type == ForwardedNode::OBFUSCATED) {
1667     return fwdconf.by_obfuscated;
1668   }
1669 
1670   return faddr_->hostport;
1671 }
1672 
get_forwarded_for() const1673 StringRef ClientHandler::get_forwarded_for() const { return forwarded_for_; }
1674 
get_upstream_addr() const1675 const UpstreamAddr *ClientHandler::get_upstream_addr() const { return faddr_; }
1676 
get_connection()1677 Connection *ClientHandler::get_connection() { return &conn_; }
1678 
set_tls_sni(const StringRef & sni)1679 void ClientHandler::set_tls_sni(const StringRef &sni) {
1680   sni_ = make_string_ref(balloc_, sni);
1681 }
1682 
get_tls_sni() const1683 StringRef ClientHandler::get_tls_sni() const { return sni_; }
1684 
get_alpn() const1685 StringRef ClientHandler::get_alpn() const { return alpn_; }
1686 
get_block_allocator()1687 BlockAllocator &ClientHandler::get_block_allocator() { return balloc_; }
1688 
set_alpn_from_conn()1689 void ClientHandler::set_alpn_from_conn() {
1690   const unsigned char *alpn;
1691   unsigned int alpnlen;
1692 
1693   SSL_get0_alpn_selected(conn_.tls.ssl, &alpn, &alpnlen);
1694 
1695   alpn_ = make_string_ref(balloc_, StringRef{alpn, alpnlen});
1696 }
1697 
1698 } // namespace shrpx
1699