• 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 
462   ++worker_->get_worker_stat()->num_connections;
463 
464   ev_timer_init(&reneg_shutdown_timer_, shutdowncb, 0., 0.);
465 
466   reneg_shutdown_timer_.data = this;
467 
468   if (!faddr->quic) {
469     conn_.rlimit.startw();
470   }
471   ev_timer_again(conn_.loop, &conn_.rt);
472 
473   auto config = get_config();
474 
475   if (!faddr->quic) {
476     if (faddr_->accept_proxy_protocol ||
477         config->conn.upstream.accept_proxy_protocol) {
478       read_ = &ClientHandler::proxy_protocol_peek_clear;
479       write_ = &ClientHandler::noop;
480       on_read_ = &ClientHandler::proxy_protocol_read;
481       on_write_ = &ClientHandler::upstream_noop;
482     } else {
483       setup_upstream_io_callback();
484     }
485   }
486 
487   auto &fwdconf = config->http.forwarded;
488 
489   if (fwdconf.params & FORWARDED_FOR) {
490     if (fwdconf.for_node_type == ForwardedNode::OBFUSCATED) {
491       // 1 for '_'
492       auto len = SHRPX_OBFUSCATED_NODE_LENGTH + 1;
493       // 1 for terminating NUL.
494       auto buf = make_byte_ref(balloc_, len + 1);
495       auto p = std::begin(buf);
496       *p++ = '_';
497       p = util::random_alpha_digit(p, p + SHRPX_OBFUSCATED_NODE_LENGTH,
498                                    worker_->get_randgen());
499       *p = '\0';
500 
501       forwarded_for_ = StringRef{std::span{std::begin(buf), p}};
502     } else {
503       init_forwarded_for(family, ipaddr_);
504     }
505   }
506 }
507 
init_forwarded_for(int family,const StringRef & ipaddr)508 void ClientHandler::init_forwarded_for(int family, const StringRef &ipaddr) {
509   if (family == AF_INET6) {
510     // 2 for '[' and ']'
511     auto len = 2 + ipaddr.size();
512     // 1 for terminating NUL.
513     auto buf = make_byte_ref(balloc_, len + 1);
514     auto p = std::begin(buf);
515     *p++ = '[';
516     p = std::copy(std::begin(ipaddr), std::end(ipaddr), p);
517     *p++ = ']';
518     *p = '\0';
519 
520     forwarded_for_ = StringRef{std::span{std::begin(buf), p}};
521   } else {
522     // family == AF_INET or family == AF_UNIX
523     forwarded_for_ = ipaddr;
524   }
525 }
526 
setup_upstream_io_callback()527 void ClientHandler::setup_upstream_io_callback() {
528   if (conn_.tls.ssl) {
529     conn_.prepare_server_handshake();
530     read_ = write_ = &ClientHandler::tls_handshake;
531     on_read_ = &ClientHandler::upstream_noop;
532     on_write_ = &ClientHandler::upstream_write;
533   } else {
534     // For non-TLS version, first create HttpsUpstream. It may be
535     // upgraded to HTTP/2 through HTTP Upgrade or direct HTTP/2
536     // connection.
537     upstream_ = std::make_unique<HttpsUpstream>(this);
538     alpn_ = "http/1.1"_sr;
539     read_ = &ClientHandler::read_clear;
540     write_ = &ClientHandler::write_clear;
541     on_read_ = &ClientHandler::upstream_http1_connhd_read;
542     on_write_ = &ClientHandler::upstream_noop;
543   }
544 }
545 
546 #ifdef ENABLE_HTTP3
setup_http3_upstream(std::unique_ptr<Http3Upstream> && upstream)547 void ClientHandler::setup_http3_upstream(
548     std::unique_ptr<Http3Upstream> &&upstream) {
549   upstream_ = std::move(upstream);
550   write_ = &ClientHandler::write_quic;
551 
552   auto config = get_config();
553 
554   reset_upstream_read_timeout(config->conn.upstream.timeout.http3_idle);
555 }
556 #endif // ENABLE_HTTP3
557 
~ClientHandler()558 ClientHandler::~ClientHandler() {
559   if (LOG_ENABLED(INFO)) {
560     CLOG(INFO, this) << "Deleting";
561   }
562 
563   if (upstream_) {
564     upstream_->on_handler_delete();
565   }
566 
567   auto worker_stat = worker_->get_worker_stat();
568   --worker_stat->num_connections;
569 
570   if (worker_stat->num_connections == 0) {
571     worker_->schedule_clear_mcpool();
572   }
573 
574   ev_timer_stop(conn_.loop, &reneg_shutdown_timer_);
575 
576   // TODO If backend is http/2, and it is in CONNECTED state, signal
577   // it and make it loopbreak when output is zero.
578   if (worker_->get_graceful_shutdown() && worker_stat->num_connections == 0 &&
579       worker_stat->num_close_waits == 0) {
580     ev_break(conn_.loop);
581   }
582 
583   if (LOG_ENABLED(INFO)) {
584     CLOG(INFO, this) << "Deleted";
585   }
586 }
587 
get_upstream()588 Upstream *ClientHandler::get_upstream() { return upstream_.get(); }
589 
get_loop() const590 struct ev_loop *ClientHandler::get_loop() const { return conn_.loop; }
591 
reset_upstream_read_timeout(ev_tstamp t)592 void ClientHandler::reset_upstream_read_timeout(ev_tstamp t) {
593   conn_.rt.repeat = t;
594 
595   ev_timer_again(conn_.loop, &conn_.rt);
596 }
597 
reset_upstream_write_timeout(ev_tstamp t)598 void ClientHandler::reset_upstream_write_timeout(ev_tstamp t) {
599   conn_.wt.repeat = t;
600 
601   ev_timer_again(conn_.loop, &conn_.wt);
602 }
603 
repeat_read_timer()604 void ClientHandler::repeat_read_timer() {
605   ev_timer_again(conn_.loop, &conn_.rt);
606 }
607 
stop_read_timer()608 void ClientHandler::stop_read_timer() { ev_timer_stop(conn_.loop, &conn_.rt); }
609 
validate_next_proto()610 int ClientHandler::validate_next_proto() {
611   const unsigned char *next_proto = nullptr;
612   unsigned int next_proto_len = 0;
613 
614   // First set callback for catch all cases
615   on_read_ = &ClientHandler::upstream_read;
616 
617   SSL_get0_alpn_selected(conn_.tls.ssl, &next_proto, &next_proto_len);
618 
619   StringRef proto;
620 
621   if (next_proto) {
622     proto = StringRef{next_proto, next_proto_len};
623 
624     if (LOG_ENABLED(INFO)) {
625       CLOG(INFO, this) << "The negotiated next protocol: " << proto;
626     }
627   } else {
628     if (LOG_ENABLED(INFO)) {
629       CLOG(INFO, this) << "No protocol negotiated. Fallback to HTTP/1.1";
630     }
631 
632     proto = "http/1.1"_sr;
633   }
634 
635   if (!tls::in_proto_list(get_config()->tls.alpn_list, proto)) {
636     if (LOG_ENABLED(INFO)) {
637       CLOG(INFO, this) << "The negotiated protocol is not supported: " << proto;
638     }
639     return -1;
640   }
641 
642   if (util::check_h2_is_selected(proto)) {
643     on_read_ = &ClientHandler::upstream_http2_connhd_read;
644 
645     auto http2_upstream = std::make_unique<Http2Upstream>(this);
646 
647     upstream_ = std::move(http2_upstream);
648     alpn_ = make_string_ref(balloc_, proto);
649 
650     // At this point, input buffer is already filled with some bytes.
651     // The read callback is not called until new data come. So consume
652     // input buffer here.
653     if (on_read() != 0) {
654       return -1;
655     }
656 
657     return 0;
658   }
659 
660   if (proto == "http/1.1"_sr) {
661     upstream_ = std::make_unique<HttpsUpstream>(this);
662     alpn_ = "http/1.1"_sr;
663 
664     // At this point, input buffer is already filled with some bytes.
665     // The read callback is not called until new data come. So consume
666     // input buffer here.
667     if (on_read() != 0) {
668       return -1;
669     }
670 
671     return 0;
672   }
673   if (LOG_ENABLED(INFO)) {
674     CLOG(INFO, this) << "The negotiated protocol is not supported";
675   }
676   return -1;
677 }
678 
do_read()679 int ClientHandler::do_read() { return read_(*this); }
do_write()680 int ClientHandler::do_write() { return write_(*this); }
681 
on_read()682 int ClientHandler::on_read() {
683   if (rb_.chunk_avail()) {
684     auto rv = on_read_(*this);
685     if (rv != 0) {
686       return rv;
687     }
688   }
689   conn_.handle_tls_pending_read();
690   return 0;
691 }
on_write()692 int ClientHandler::on_write() { return on_write_(*this); }
693 
get_ipaddr() const694 const StringRef &ClientHandler::get_ipaddr() const { return ipaddr_; }
695 
get_should_close_after_write() const696 bool ClientHandler::get_should_close_after_write() const {
697   return should_close_after_write_;
698 }
699 
set_should_close_after_write(bool f)700 void ClientHandler::set_should_close_after_write(bool f) {
701   should_close_after_write_ = f;
702 }
703 
pool_downstream_connection(std::unique_ptr<DownstreamConnection> dconn)704 void ClientHandler::pool_downstream_connection(
705     std::unique_ptr<DownstreamConnection> dconn) {
706   if (!dconn->poolable()) {
707     return;
708   }
709 
710   dconn->set_client_handler(nullptr);
711 
712   auto &group = dconn->get_downstream_addr_group();
713 
714   if (LOG_ENABLED(INFO)) {
715     CLOG(INFO, this) << "Pooling downstream connection DCONN:" << dconn.get()
716                      << " in group " << group;
717   }
718 
719   auto addr = dconn->get_addr();
720   auto &dconn_pool = addr->dconn_pool;
721   dconn_pool->add_downstream_connection(std::move(dconn));
722 }
723 
724 namespace {
725 // Computes 32bits hash for session affinity for IP address |ip|.
compute_affinity_from_ip(const StringRef & ip)726 uint32_t compute_affinity_from_ip(const StringRef &ip) {
727   int rv;
728   std::array<uint8_t, 32> buf;
729 
730   rv = util::sha256(buf.data(), ip);
731   if (rv != 0) {
732     // Not sure when sha256 failed.  Just fall back to another
733     // function.
734     return util::hash32(ip);
735   }
736 
737   return (static_cast<uint32_t>(buf[0]) << 24) |
738          (static_cast<uint32_t>(buf[1]) << 16) |
739          (static_cast<uint32_t>(buf[2]) << 8) | static_cast<uint32_t>(buf[3]);
740 }
741 } // namespace
742 
get_http2_session(const std::shared_ptr<DownstreamAddrGroup> & group,DownstreamAddr * addr)743 Http2Session *ClientHandler::get_http2_session(
744     const std::shared_ptr<DownstreamAddrGroup> &group, DownstreamAddr *addr) {
745   auto &shared_addr = group->shared_addr;
746 
747   if (LOG_ENABLED(INFO)) {
748     CLOG(INFO, this) << "Selected DownstreamAddr=" << addr
749                      << ", index=" << (addr - shared_addr->addrs.data());
750   }
751 
752   for (auto session = addr->http2_extra_freelist.head; session;) {
753     auto next = session->dlnext;
754 
755     if (session->max_concurrency_reached(0)) {
756       if (LOG_ENABLED(INFO)) {
757         CLOG(INFO, this)
758             << "Maximum streams have been reached for Http2Session(" << session
759             << ").  Skip it";
760       }
761 
762       session->remove_from_freelist();
763       session = next;
764 
765       continue;
766     }
767 
768     if (LOG_ENABLED(INFO)) {
769       CLOG(INFO, this) << "Use Http2Session " << session
770                        << " from http2_extra_freelist";
771     }
772 
773     if (session->max_concurrency_reached(1)) {
774       if (LOG_ENABLED(INFO)) {
775         CLOG(INFO, this) << "Maximum streams are reached for Http2Session("
776                          << session << ").";
777       }
778 
779       session->remove_from_freelist();
780     }
781     return session;
782   }
783 
784   auto session = new Http2Session(conn_.loop, worker_->get_cl_ssl_ctx(),
785                                   worker_, group, addr);
786 
787   if (LOG_ENABLED(INFO)) {
788     CLOG(INFO, this) << "Create new Http2Session " << session;
789   }
790 
791   session->add_to_extra_freelist();
792 
793   return session;
794 }
795 
get_affinity_cookie(Downstream * downstream,const StringRef & cookie_name)796 uint32_t ClientHandler::get_affinity_cookie(Downstream *downstream,
797                                             const StringRef &cookie_name) {
798   auto h = downstream->find_affinity_cookie(cookie_name);
799   if (h) {
800     return h;
801   }
802 
803   auto d = std::uniform_int_distribution<uint32_t>(1);
804   auto rh = d(worker_->get_randgen());
805   h = util::hash32(StringRef{reinterpret_cast<char *>(&rh), sizeof(rh)});
806 
807   downstream->renew_affinity_cookie(h);
808 
809   return h;
810 }
811 
812 namespace {
reschedule_addr(std::priority_queue<DownstreamAddrEntry,std::vector<DownstreamAddrEntry>,DownstreamAddrEntryGreater> & pq,DownstreamAddr * addr)813 void reschedule_addr(
814     std::priority_queue<DownstreamAddrEntry, std::vector<DownstreamAddrEntry>,
815                         DownstreamAddrEntryGreater> &pq,
816     DownstreamAddr *addr) {
817   auto penalty = MAX_DOWNSTREAM_ADDR_WEIGHT + addr->pending_penalty;
818   addr->cycle += penalty / addr->weight;
819   addr->pending_penalty = penalty % addr->weight;
820 
821   pq.push(DownstreamAddrEntry{addr, addr->seq, addr->cycle});
822   addr->queued = true;
823 }
824 } // namespace
825 
826 namespace {
reschedule_wg(std::priority_queue<WeightGroupEntry,std::vector<WeightGroupEntry>,WeightGroupEntryGreater> & pq,WeightGroup * wg)827 void reschedule_wg(
828     std::priority_queue<WeightGroupEntry, std::vector<WeightGroupEntry>,
829                         WeightGroupEntryGreater> &pq,
830     WeightGroup *wg) {
831   auto penalty = MAX_DOWNSTREAM_ADDR_WEIGHT + wg->pending_penalty;
832   wg->cycle += penalty / wg->weight;
833   wg->pending_penalty = penalty % wg->weight;
834 
835   pq.push(WeightGroupEntry{wg, wg->seq, wg->cycle});
836   wg->queued = true;
837 }
838 } // namespace
839 
get_downstream_addr(int & err,DownstreamAddrGroup * group,Downstream * downstream)840 DownstreamAddr *ClientHandler::get_downstream_addr(int &err,
841                                                    DownstreamAddrGroup *group,
842                                                    Downstream *downstream) {
843   err = 0;
844 
845   switch (faddr_->alt_mode) {
846   case UpstreamAltMode::API:
847   case UpstreamAltMode::HEALTHMON:
848     assert(0);
849   default:
850     break;
851   }
852 
853   auto &shared_addr = group->shared_addr;
854 
855   if (shared_addr->affinity.type != SessionAffinity::NONE) {
856     uint32_t hash;
857     switch (shared_addr->affinity.type) {
858     case SessionAffinity::IP:
859       if (!affinity_hash_computed_) {
860         affinity_hash_ = compute_affinity_from_ip(ipaddr_);
861         affinity_hash_computed_ = true;
862       }
863       hash = affinity_hash_;
864       break;
865     case SessionAffinity::COOKIE:
866       if (shared_addr->affinity.cookie.stickiness ==
867           SessionAffinityCookieStickiness::STRICT) {
868         return get_downstream_addr_strict_affinity(err, shared_addr,
869                                                    downstream);
870       }
871 
872       hash = get_affinity_cookie(downstream, shared_addr->affinity.cookie.name);
873       break;
874     default:
875       assert(0);
876     }
877 
878     const auto &affinity_hash = shared_addr->affinity_hash;
879 
880     auto it = std::lower_bound(
881         std::begin(affinity_hash), std::end(affinity_hash), hash,
882         [](const AffinityHash &lhs, uint32_t rhs) { return lhs.hash < rhs; });
883 
884     if (it == std::end(affinity_hash)) {
885       it = std::begin(affinity_hash);
886     }
887 
888     auto aff_idx =
889         static_cast<size_t>(std::distance(std::begin(affinity_hash), it));
890     auto idx = (*it).idx;
891     auto addr = &shared_addr->addrs[idx];
892 
893     if (addr->connect_blocker->blocked()) {
894       size_t i;
895       for (i = aff_idx + 1; i != aff_idx; ++i) {
896         if (i == shared_addr->affinity_hash.size()) {
897           i = 0;
898         }
899         addr = &shared_addr->addrs[shared_addr->affinity_hash[i].idx];
900         if (addr->connect_blocker->blocked()) {
901           continue;
902         }
903         break;
904       }
905       if (i == aff_idx) {
906         err = -1;
907         return nullptr;
908       }
909     }
910 
911     return addr;
912   }
913 
914   auto &wgpq = shared_addr->pq;
915 
916   for (;;) {
917     if (wgpq.empty()) {
918       CLOG(INFO, this) << "No working downstream address found";
919       err = -1;
920       return nullptr;
921     }
922 
923     auto wg = wgpq.top().wg;
924     wgpq.pop();
925     wg->queued = false;
926 
927     for (;;) {
928       if (wg->pq.empty()) {
929         break;
930       }
931 
932       auto addr = wg->pq.top().addr;
933       wg->pq.pop();
934       addr->queued = false;
935 
936       if (addr->connect_blocker->blocked()) {
937         continue;
938       }
939 
940       reschedule_addr(wg->pq, addr);
941       reschedule_wg(wgpq, wg);
942 
943       return addr;
944     }
945   }
946 }
947 
get_downstream_addr_strict_affinity(int & err,const std::shared_ptr<SharedDownstreamAddr> & shared_addr,Downstream * downstream)948 DownstreamAddr *ClientHandler::get_downstream_addr_strict_affinity(
949     int &err, const std::shared_ptr<SharedDownstreamAddr> &shared_addr,
950     Downstream *downstream) {
951   const auto &affinity_hash = shared_addr->affinity_hash;
952 
953   auto h = downstream->find_affinity_cookie(shared_addr->affinity.cookie.name);
954   if (h) {
955     auto it = shared_addr->affinity_hash_map.find(h);
956     if (it != std::end(shared_addr->affinity_hash_map)) {
957       auto addr = &shared_addr->addrs[(*it).second];
958       if (!addr->connect_blocker->blocked()) {
959         return addr;
960       }
961     }
962   } else {
963     auto d = std::uniform_int_distribution<uint32_t>(1);
964     auto rh = d(worker_->get_randgen());
965     h = util::hash32(StringRef{reinterpret_cast<char *>(&rh), sizeof(rh)});
966   }
967 
968   // Client is not bound to a particular backend, or the bound backend
969   // is not found, or is blocked.  Find new backend using h.  Using
970   // existing h allows us to find new server in a deterministic way.
971   // It is preferable because multiple concurrent requests with the
972   // stale cookie might be in-flight.
973   auto it = std::lower_bound(
974       std::begin(affinity_hash), std::end(affinity_hash), h,
975       [](const AffinityHash &lhs, uint32_t rhs) { return lhs.hash < rhs; });
976 
977   if (it == std::end(affinity_hash)) {
978     it = std::begin(affinity_hash);
979   }
980 
981   auto aff_idx =
982       static_cast<size_t>(std::distance(std::begin(affinity_hash), it));
983   auto idx = (*it).idx;
984   auto addr = &shared_addr->addrs[idx];
985 
986   if (addr->connect_blocker->blocked()) {
987     size_t i;
988     for (i = aff_idx + 1; i != aff_idx; ++i) {
989       if (i == shared_addr->affinity_hash.size()) {
990         i = 0;
991       }
992       addr = &shared_addr->addrs[shared_addr->affinity_hash[i].idx];
993       if (addr->connect_blocker->blocked()) {
994         continue;
995       }
996       break;
997     }
998     if (i == aff_idx) {
999       err = -1;
1000       return nullptr;
1001     }
1002   }
1003 
1004   downstream->renew_affinity_cookie(addr->affinity_hash);
1005 
1006   return addr;
1007 }
1008 
1009 std::unique_ptr<DownstreamConnection>
get_downstream_connection(int & err,Downstream * downstream)1010 ClientHandler::get_downstream_connection(int &err, Downstream *downstream) {
1011   size_t group_idx;
1012   auto &downstreamconf = *worker_->get_downstream_config();
1013   auto &routerconf = downstreamconf.router;
1014 
1015   auto catch_all = downstreamconf.addr_group_catch_all;
1016   auto &groups = worker_->get_downstream_addr_groups();
1017 
1018   auto &req = downstream->request();
1019 
1020   err = 0;
1021 
1022   switch (faddr_->alt_mode) {
1023   case UpstreamAltMode::API: {
1024     auto dconn = std::make_unique<APIDownstreamConnection>(worker_);
1025     dconn->set_client_handler(this);
1026     return dconn;
1027   }
1028   case UpstreamAltMode::HEALTHMON: {
1029     auto dconn = std::make_unique<HealthMonitorDownstreamConnection>();
1030     dconn->set_client_handler(this);
1031     return dconn;
1032   }
1033   default:
1034     break;
1035   }
1036 
1037   auto &balloc = downstream->get_block_allocator();
1038 
1039   StringRef authority, path;
1040 
1041   if (req.forwarded_once) {
1042     if (groups.size() != 1) {
1043       authority = req.orig_authority;
1044       path = req.orig_path;
1045     }
1046   } else {
1047     if (faddr_->sni_fwd) {
1048       authority = sni_;
1049     } else if (!req.authority.empty()) {
1050       authority = req.authority;
1051     } else {
1052       auto h = req.fs.header(http2::HD_HOST);
1053       if (h) {
1054         authority = h->value;
1055       }
1056     }
1057 
1058     // CONNECT method does not have path.  But we requires path in
1059     // host-path mapping.  As workaround, we assume that path is
1060     // "/".
1061     if (!req.regular_connect_method()) {
1062       path = req.path;
1063     }
1064 
1065     // Cache the authority and path used for the first-time backend
1066     // selection because per-pattern mruby script can change them.
1067     req.orig_authority = authority;
1068     req.orig_path = path;
1069     req.forwarded_once = true;
1070   }
1071 
1072   // Fast path.  If we have one group, it must be catch-all group.
1073   if (groups.size() == 1) {
1074     group_idx = 0;
1075   } else {
1076     group_idx = match_downstream_addr_group(routerconf, authority, path, groups,
1077                                             catch_all, balloc);
1078   }
1079 
1080   if (LOG_ENABLED(INFO)) {
1081     CLOG(INFO, this) << "Downstream address group_idx: " << group_idx;
1082   }
1083 
1084   if (groups[group_idx]->shared_addr->redirect_if_not_tls && !conn_.tls.ssl) {
1085     if (LOG_ENABLED(INFO)) {
1086       CLOG(INFO, this) << "Downstream address group " << group_idx
1087                        << " requires frontend TLS connection.";
1088     }
1089     err = SHRPX_ERR_TLS_REQUIRED;
1090     return nullptr;
1091   }
1092 
1093   auto &group = groups[group_idx];
1094 
1095   if (group->shared_addr->dnf) {
1096     auto dconn = std::make_unique<NullDownstreamConnection>(group);
1097     dconn->set_client_handler(this);
1098     return dconn;
1099   }
1100 
1101   auto addr = get_downstream_addr(err, group.get(), downstream);
1102   if (addr == nullptr) {
1103     return nullptr;
1104   }
1105 
1106   if (addr->proto == Proto::HTTP1) {
1107     auto dconn = addr->dconn_pool->pop_downstream_connection();
1108     if (dconn) {
1109       dconn->set_client_handler(this);
1110       return dconn;
1111     }
1112 
1113     if (worker_->get_connect_blocker()->blocked()) {
1114       if (LOG_ENABLED(INFO)) {
1115         DCLOG(INFO, this)
1116             << "Worker wide backend connection was blocked temporarily";
1117       }
1118       return nullptr;
1119     }
1120 
1121     if (LOG_ENABLED(INFO)) {
1122       CLOG(INFO, this) << "Downstream connection pool is empty."
1123                        << " Create new one";
1124     }
1125 
1126     dconn = std::make_unique<HttpDownstreamConnection>(group, addr, conn_.loop,
1127                                                        worker_);
1128     dconn->set_client_handler(this);
1129     return dconn;
1130   }
1131 
1132   if (LOG_ENABLED(INFO)) {
1133     CLOG(INFO, this) << "Downstream connection pool is empty."
1134                      << " Create new one";
1135   }
1136 
1137   auto http2session = get_http2_session(group, addr);
1138   auto dconn = std::make_unique<Http2DownstreamConnection>(http2session);
1139   dconn->set_client_handler(this);
1140   return dconn;
1141 }
1142 
get_mcpool()1143 MemchunkPool *ClientHandler::get_mcpool() { return worker_->get_mcpool(); }
1144 
get_ssl() const1145 SSL *ClientHandler::get_ssl() const { return conn_.tls.ssl; }
1146 
direct_http2_upgrade()1147 void ClientHandler::direct_http2_upgrade() {
1148   upstream_ = std::make_unique<Http2Upstream>(this);
1149   alpn_ = NGHTTP2_CLEARTEXT_PROTO_VERSION_ID ""_sr;
1150   on_read_ = &ClientHandler::upstream_read;
1151   write_ = &ClientHandler::write_clear;
1152 }
1153 
perform_http2_upgrade(HttpsUpstream * http)1154 int ClientHandler::perform_http2_upgrade(HttpsUpstream *http) {
1155   auto upstream = std::make_unique<Http2Upstream>(this);
1156 
1157   auto output = upstream->get_response_buf();
1158 
1159   // We might have written non-final header in response_buf, in this
1160   // case, response_state is still INITIAL.  If this non-final header
1161   // and upgrade header fit in output buffer, do upgrade.  Otherwise,
1162   // to avoid to send this non-final header as response body in HTTP/2
1163   // upstream, fail upgrade.
1164   auto downstream = http->get_downstream();
1165   auto input = downstream->get_response_buf();
1166 
1167   if (upstream->upgrade_upstream(http) != 0) {
1168     return -1;
1169   }
1170   // http pointer is now owned by upstream.
1171   upstream_.release();
1172   // TODO We might get other version id in HTTP2-settings, if we
1173   // support aliasing for h2, but we just use library default for now.
1174   alpn_ = NGHTTP2_CLEARTEXT_PROTO_VERSION_ID ""_sr;
1175   on_read_ = &ClientHandler::upstream_http2_connhd_read;
1176   write_ = &ClientHandler::write_clear;
1177 
1178   input->remove(*output, input->rleft());
1179 
1180   constexpr auto res = "HTTP/1.1 101 Switching Protocols\r\n"
1181                        "Connection: Upgrade\r\n"
1182                        "Upgrade: " NGHTTP2_CLEARTEXT_PROTO_VERSION_ID "\r\n"
1183                        "\r\n"_sr;
1184 
1185   output->append(res);
1186   upstream_ = std::move(upstream);
1187 
1188   signal_write();
1189   return 0;
1190 }
1191 
get_http2_upgrade_allowed() const1192 bool ClientHandler::get_http2_upgrade_allowed() const { return !conn_.tls.ssl; }
1193 
get_upstream_scheme() const1194 StringRef ClientHandler::get_upstream_scheme() const {
1195   if (conn_.tls.ssl) {
1196     return "https"_sr;
1197   } else {
1198     return "http"_sr;
1199   }
1200 }
1201 
start_immediate_shutdown()1202 void ClientHandler::start_immediate_shutdown() {
1203   ev_timer_start(conn_.loop, &reneg_shutdown_timer_);
1204 }
1205 
write_accesslog(Downstream * downstream)1206 void ClientHandler::write_accesslog(Downstream *downstream) {
1207   auto &req = downstream->request();
1208 
1209   auto config = get_config();
1210 
1211   if (!req.tstamp) {
1212     auto lgconf = log_config();
1213     lgconf->update_tstamp(std::chrono::system_clock::now());
1214     req.tstamp = lgconf->tstamp;
1215   }
1216 
1217   upstream_accesslog(
1218       config->logging.access.format,
1219       LogSpec{
1220           downstream,
1221           ipaddr_,
1222           alpn_,
1223           sni_,
1224           conn_.tls.ssl,
1225           std::chrono::high_resolution_clock::now(), // request_end_time
1226           port_,
1227           faddr_->port,
1228           config->pid,
1229       });
1230 }
1231 
get_rb()1232 ClientHandler::ReadBuf *ClientHandler::get_rb() { return &rb_; }
1233 
signal_write()1234 void ClientHandler::signal_write() { conn_.wlimit.startw(); }
1235 
get_rlimit()1236 RateLimit *ClientHandler::get_rlimit() { return &conn_.rlimit; }
get_wlimit()1237 RateLimit *ClientHandler::get_wlimit() { return &conn_.wlimit; }
1238 
get_wev()1239 ev_io *ClientHandler::get_wev() { return &conn_.wev; }
1240 
get_worker() const1241 Worker *ClientHandler::get_worker() const { return worker_; }
1242 
1243 namespace {
parse_proxy_line_port(const uint8_t * first,const uint8_t * last)1244 ssize_t parse_proxy_line_port(const uint8_t *first, const uint8_t *last) {
1245   auto p = first;
1246   int32_t port = 0;
1247 
1248   if (p == last) {
1249     return -1;
1250   }
1251 
1252   if (*p == '0') {
1253     if (p + 1 != last && util::is_digit(*(p + 1))) {
1254       return -1;
1255     }
1256     return 1;
1257   }
1258 
1259   for (; p != last && util::is_digit(*p); ++p) {
1260     port *= 10;
1261     port += *p - '0';
1262 
1263     if (port > 65535) {
1264       return -1;
1265     }
1266   }
1267 
1268   return p - first;
1269 }
1270 } // namespace
1271 
on_proxy_protocol_finish()1272 int ClientHandler::on_proxy_protocol_finish() {
1273   auto len = rb_.pos() - rb_.begin();
1274 
1275   assert(len);
1276 
1277   if (LOG_ENABLED(INFO)) {
1278     CLOG(INFO, this) << "PROXY-protocol: Draining " << len
1279                      << " bytes from socket";
1280   }
1281 
1282   rb_.reset();
1283 
1284   if (conn_.read_nolim_clear(rb_.pos(), len) < 0) {
1285     return -1;
1286   }
1287 
1288   rb_.reset();
1289 
1290   setup_upstream_io_callback();
1291 
1292   return 0;
1293 }
1294 
1295 namespace {
1296 // PROXY-protocol v2 header signature
1297 constexpr uint8_t PROXY_PROTO_V2_SIG[] =
1298     "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A";
1299 
1300 // PROXY-protocol v2 header length
1301 constexpr size_t PROXY_PROTO_V2_HDLEN =
1302     str_size(PROXY_PROTO_V2_SIG) + /* ver_cmd(1) + fam(1) + len(2) = */ 4;
1303 } // namespace
1304 
1305 // http://www.haproxy.org/download/1.5/doc/proxy-protocol.txt
proxy_protocol_read()1306 int ClientHandler::proxy_protocol_read() {
1307   if (LOG_ENABLED(INFO)) {
1308     CLOG(INFO, this) << "PROXY-protocol: Started";
1309   }
1310 
1311   auto first = rb_.pos();
1312 
1313   if (rb_.rleft() >= PROXY_PROTO_V2_HDLEN &&
1314       (*(first + str_size(PROXY_PROTO_V2_SIG)) & 0xf0) == 0x20) {
1315     if (LOG_ENABLED(INFO)) {
1316       CLOG(INFO, this) << "PROXY-protocol: Detected v2 header signature";
1317     }
1318     return proxy_protocol_v2_read();
1319   }
1320 
1321   // NULL character really destroys functions which expects NULL
1322   // terminated string.  We won't expect it in PROXY protocol line, so
1323   // find it here.
1324   auto chrs = std::to_array({'\n', '\0'});
1325 
1326   constexpr size_t MAX_PROXY_LINELEN = 107;
1327 
1328   auto bufend = rb_.pos() + std::min(MAX_PROXY_LINELEN, rb_.rleft());
1329 
1330   auto end =
1331       std::find_first_of(rb_.pos(), bufend, std::begin(chrs), std::end(chrs));
1332 
1333   if (end == bufend || *end == '\0' || end == rb_.pos() || *(end - 1) != '\r') {
1334     if (LOG_ENABLED(INFO)) {
1335       CLOG(INFO, this) << "PROXY-protocol-v1: No ending CR LF sequence found";
1336     }
1337     return -1;
1338   }
1339 
1340   --end;
1341 
1342   constexpr auto HEADER = "PROXY "_sr;
1343 
1344   if (static_cast<size_t>(end - rb_.pos()) < HEADER.size()) {
1345     if (LOG_ENABLED(INFO)) {
1346       CLOG(INFO, this) << "PROXY-protocol-v1: PROXY version 1 ID not found";
1347     }
1348     return -1;
1349   }
1350 
1351   if (HEADER != StringRef{rb_.pos(), HEADER.size()}) {
1352     if (LOG_ENABLED(INFO)) {
1353       CLOG(INFO, this) << "PROXY-protocol-v1: Bad PROXY protocol version 1 ID";
1354     }
1355     return -1;
1356   }
1357 
1358   rb_.drain(HEADER.size());
1359 
1360   int family;
1361 
1362   if (rb_.pos()[0] == 'T') {
1363     if (end - rb_.pos() < 5) {
1364       if (LOG_ENABLED(INFO)) {
1365         CLOG(INFO, this) << "PROXY-protocol-v1: INET protocol family not found";
1366       }
1367       return -1;
1368     }
1369 
1370     if (rb_.pos()[1] != 'C' || rb_.pos()[2] != 'P') {
1371       if (LOG_ENABLED(INFO)) {
1372         CLOG(INFO, this) << "PROXY-protocol-v1: Unknown INET protocol family";
1373       }
1374       return -1;
1375     }
1376 
1377     switch (rb_.pos()[3]) {
1378     case '4':
1379       family = AF_INET;
1380       break;
1381     case '6':
1382       family = AF_INET6;
1383       break;
1384     default:
1385       if (LOG_ENABLED(INFO)) {
1386         CLOG(INFO, this) << "PROXY-protocol-v1: Unknown INET protocol family";
1387       }
1388       return -1;
1389     }
1390 
1391     rb_.drain(5);
1392   } else {
1393     if (end - rb_.pos() < 7) {
1394       if (LOG_ENABLED(INFO)) {
1395         CLOG(INFO, this) << "PROXY-protocol-v1: INET protocol family not found";
1396       }
1397       return -1;
1398     }
1399     if ("UNKNOWN"_sr != StringRef{rb_.pos(), 7}) {
1400       if (LOG_ENABLED(INFO)) {
1401         CLOG(INFO, this) << "PROXY-protocol-v1: Unknown INET protocol family";
1402       }
1403       return -1;
1404     }
1405 
1406     rb_.drain(end + 2 - rb_.pos());
1407 
1408     return on_proxy_protocol_finish();
1409   }
1410 
1411   // source address
1412   auto token_end = std::find(rb_.pos(), end, ' ');
1413   if (token_end == end) {
1414     if (LOG_ENABLED(INFO)) {
1415       CLOG(INFO, this) << "PROXY-protocol-v1: Source address not found";
1416     }
1417     return -1;
1418   }
1419 
1420   *token_end = '\0';
1421   if (!util::numeric_host(reinterpret_cast<const char *>(rb_.pos()), family)) {
1422     if (LOG_ENABLED(INFO)) {
1423       CLOG(INFO, this) << "PROXY-protocol-v1: Invalid source address";
1424     }
1425     return -1;
1426   }
1427 
1428   auto src_addr = rb_.pos();
1429   auto src_addrlen = token_end - rb_.pos();
1430 
1431   rb_.drain(token_end - rb_.pos() + 1);
1432 
1433   // destination address
1434   token_end = std::find(rb_.pos(), end, ' ');
1435   if (token_end == end) {
1436     if (LOG_ENABLED(INFO)) {
1437       CLOG(INFO, this) << "PROXY-protocol-v1: Destination address not found";
1438     }
1439     return -1;
1440   }
1441 
1442   *token_end = '\0';
1443   if (!util::numeric_host(reinterpret_cast<const char *>(rb_.pos()), family)) {
1444     if (LOG_ENABLED(INFO)) {
1445       CLOG(INFO, this) << "PROXY-protocol-v1: Invalid destination address";
1446     }
1447     return -1;
1448   }
1449 
1450   // Currently we don't use destination address
1451 
1452   rb_.drain(token_end - rb_.pos() + 1);
1453 
1454   // source port
1455   auto n = parse_proxy_line_port(rb_.pos(), end);
1456   if (n <= 0 || *(rb_.pos() + n) != ' ') {
1457     if (LOG_ENABLED(INFO)) {
1458       CLOG(INFO, this) << "PROXY-protocol-v1: Invalid source port";
1459     }
1460     return -1;
1461   }
1462 
1463   rb_.pos()[n] = '\0';
1464   auto src_port = rb_.pos();
1465   auto src_portlen = n;
1466 
1467   rb_.drain(n + 1);
1468 
1469   // destination  port
1470   n = parse_proxy_line_port(rb_.pos(), end);
1471   if (n <= 0 || rb_.pos() + n != end) {
1472     if (LOG_ENABLED(INFO)) {
1473       CLOG(INFO, this) << "PROXY-protocol-v1: Invalid destination port";
1474     }
1475     return -1;
1476   }
1477 
1478   // Currently we don't use destination port
1479 
1480   rb_.drain(end + 2 - rb_.pos());
1481 
1482   ipaddr_ = make_string_ref(
1483       balloc_, StringRef{src_addr, static_cast<size_t>(src_addrlen)});
1484   port_ = make_string_ref(
1485       balloc_, StringRef{src_port, static_cast<size_t>(src_portlen)});
1486 
1487   if (LOG_ENABLED(INFO)) {
1488     CLOG(INFO, this) << "PROXY-protocol-v1: Finished, " << (rb_.pos() - first)
1489                      << " bytes read";
1490   }
1491 
1492   auto config = get_config();
1493   auto &fwdconf = config->http.forwarded;
1494 
1495   if ((fwdconf.params & FORWARDED_FOR) &&
1496       fwdconf.for_node_type == ForwardedNode::IP) {
1497     init_forwarded_for(family, ipaddr_);
1498   }
1499 
1500   return on_proxy_protocol_finish();
1501 }
1502 
proxy_protocol_v2_read()1503 int ClientHandler::proxy_protocol_v2_read() {
1504   // Assume that first str_size(PROXY_PROTO_V2_SIG) octets match v2
1505   // protocol signature and followed by the bytes which indicates v2.
1506   assert(rb_.rleft() >= PROXY_PROTO_V2_HDLEN);
1507 
1508   auto p = rb_.pos() + str_size(PROXY_PROTO_V2_SIG);
1509 
1510   assert(((*p) & 0xf0) == 0x20);
1511 
1512   enum { LOCAL, PROXY } cmd;
1513 
1514   auto cmd_bits = (*p++) & 0xf;
1515   switch (cmd_bits) {
1516   case 0x0:
1517     cmd = LOCAL;
1518     break;
1519   case 0x01:
1520     cmd = PROXY;
1521     break;
1522   default:
1523     if (LOG_ENABLED(INFO)) {
1524       CLOG(INFO, this) << "PROXY-protocol-v2: Unknown command " << log::hex
1525                        << cmd_bits;
1526     }
1527     return -1;
1528   }
1529 
1530   auto fam = *p++;
1531   uint16_t len;
1532   memcpy(&len, p, sizeof(len));
1533   len = ntohs(len);
1534 
1535   p += sizeof(len);
1536 
1537   if (LOG_ENABLED(INFO)) {
1538     CLOG(INFO, this) << "PROXY-protocol-v2: Detected family=" << log::hex << fam
1539                      << ", len=" << log::dec << len;
1540   }
1541 
1542   if (rb_.last() - p < len) {
1543     if (LOG_ENABLED(INFO)) {
1544       CLOG(INFO, this)
1545           << "PROXY-protocol-v2: Prematurely truncated header block; require "
1546           << len << " bytes, " << rb_.last() - p << " bytes left";
1547     }
1548     return -1;
1549   }
1550 
1551   int family;
1552   std::array<char, std::max(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)> src_addr,
1553       dst_addr;
1554   size_t addrlen;
1555 
1556   switch (fam) {
1557   case 0x11:
1558   case 0x12:
1559     if (len < 12) {
1560       if (LOG_ENABLED(INFO)) {
1561         CLOG(INFO, this) << "PROXY-protocol-v2: Too short AF_INET addresses";
1562       }
1563       return -1;
1564     }
1565     family = AF_INET;
1566     addrlen = 4;
1567     break;
1568   case 0x21:
1569   case 0x22:
1570     if (len < 36) {
1571       if (LOG_ENABLED(INFO)) {
1572         CLOG(INFO, this) << "PROXY-protocol-v2: Too short AF_INET6 addresses";
1573       }
1574       return -1;
1575     }
1576     family = AF_INET6;
1577     addrlen = 16;
1578     break;
1579   case 0x31:
1580   case 0x32:
1581     if (len < 216) {
1582       if (LOG_ENABLED(INFO)) {
1583         CLOG(INFO, this) << "PROXY-protocol-v2: Too short AF_UNIX addresses";
1584       }
1585       return -1;
1586     }
1587     // fall through
1588   case 0x00: {
1589     // UNSPEC and UNIX are just ignored.
1590     if (LOG_ENABLED(INFO)) {
1591       CLOG(INFO, this) << "PROXY-protocol-v2: Ignore combination of address "
1592                           "family and protocol "
1593                        << log::hex << fam;
1594     }
1595     rb_.drain(PROXY_PROTO_V2_HDLEN + len);
1596     return on_proxy_protocol_finish();
1597   }
1598   default:
1599     if (LOG_ENABLED(INFO)) {
1600       CLOG(INFO, this) << "PROXY-protocol-v2: Unknown combination of address "
1601                           "family and protocol "
1602                        << log::hex << fam;
1603     }
1604     return -1;
1605   }
1606 
1607   if (cmd != PROXY) {
1608     if (LOG_ENABLED(INFO)) {
1609       CLOG(INFO, this) << "PROXY-protocol-v2: Ignore non-PROXY command";
1610     }
1611     rb_.drain(PROXY_PROTO_V2_HDLEN + len);
1612     return on_proxy_protocol_finish();
1613   }
1614 
1615   if (inet_ntop(family, p, src_addr.data(), src_addr.size()) == nullptr) {
1616     if (LOG_ENABLED(INFO)) {
1617       CLOG(INFO, this) << "PROXY-protocol-v2: Unable to parse source address";
1618     }
1619     return -1;
1620   }
1621 
1622   p += addrlen;
1623 
1624   if (inet_ntop(family, p, dst_addr.data(), dst_addr.size()) == nullptr) {
1625     if (LOG_ENABLED(INFO)) {
1626       CLOG(INFO, this)
1627           << "PROXY-protocol-v2: Unable to parse destination address";
1628     }
1629     return -1;
1630   }
1631 
1632   p += addrlen;
1633 
1634   uint16_t src_port;
1635 
1636   memcpy(&src_port, p, sizeof(src_port));
1637   src_port = ntohs(src_port);
1638 
1639   // We don't use destination port.
1640   p += 4;
1641 
1642   ipaddr_ = make_string_ref(balloc_, StringRef{src_addr.data()});
1643   port_ = util::make_string_ref_uint(balloc_, src_port);
1644 
1645   if (LOG_ENABLED(INFO)) {
1646     CLOG(INFO, this) << "PROXY-protocol-v2: Finished reading proxy addresses, "
1647                      << p - rb_.pos() << " bytes read, "
1648                      << PROXY_PROTO_V2_HDLEN + len - (p - rb_.pos())
1649                      << " bytes left";
1650   }
1651 
1652   auto config = get_config();
1653   auto &fwdconf = config->http.forwarded;
1654 
1655   if ((fwdconf.params & FORWARDED_FOR) &&
1656       fwdconf.for_node_type == ForwardedNode::IP) {
1657     init_forwarded_for(family, ipaddr_);
1658   }
1659 
1660   rb_.drain(PROXY_PROTO_V2_HDLEN + len);
1661   return on_proxy_protocol_finish();
1662 }
1663 
get_forwarded_by() const1664 StringRef ClientHandler::get_forwarded_by() const {
1665   auto &fwdconf = get_config()->http.forwarded;
1666 
1667   if (fwdconf.by_node_type == ForwardedNode::OBFUSCATED) {
1668     return fwdconf.by_obfuscated;
1669   }
1670 
1671   return faddr_->hostport;
1672 }
1673 
get_forwarded_for() const1674 StringRef ClientHandler::get_forwarded_for() const { return forwarded_for_; }
1675 
get_upstream_addr() const1676 const UpstreamAddr *ClientHandler::get_upstream_addr() const { return faddr_; }
1677 
get_connection()1678 Connection *ClientHandler::get_connection() { return &conn_; };
1679 
set_tls_sni(const StringRef & sni)1680 void ClientHandler::set_tls_sni(const StringRef &sni) {
1681   sni_ = make_string_ref(balloc_, sni);
1682 }
1683 
get_tls_sni() const1684 StringRef ClientHandler::get_tls_sni() const { return sni_; }
1685 
get_alpn() const1686 StringRef ClientHandler::get_alpn() const { return alpn_; }
1687 
get_block_allocator()1688 BlockAllocator &ClientHandler::get_block_allocator() { return balloc_; }
1689 
set_alpn_from_conn()1690 void ClientHandler::set_alpn_from_conn() {
1691   const unsigned char *alpn;
1692   unsigned int alpnlen;
1693 
1694   SSL_get0_alpn_selected(conn_.tls.ssl, &alpn, &alpnlen);
1695 
1696   alpn_ = make_string_ref(balloc_, StringRef{alpn, alpnlen});
1697 }
1698 
1699 } // namespace shrpx
1700