server.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. package quic
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "net"
  9. "sync"
  10. "sync/atomic"
  11. "time"
  12. "github.com/lucas-clemente/quic-go/internal/handshake"
  13. "github.com/lucas-clemente/quic-go/internal/protocol"
  14. "github.com/lucas-clemente/quic-go/internal/qerr"
  15. "github.com/lucas-clemente/quic-go/internal/utils"
  16. "github.com/lucas-clemente/quic-go/internal/wire"
  17. )
  18. // packetHandler handles packets
  19. type packetHandler interface {
  20. handlePacket(*receivedPacket)
  21. io.Closer
  22. destroy(error)
  23. getPerspective() protocol.Perspective
  24. }
  25. type unknownPacketHandler interface {
  26. handlePacket(*receivedPacket)
  27. closeWithError(error) error
  28. }
  29. type packetHandlerManager interface {
  30. io.Closer
  31. Add(protocol.ConnectionID, packetHandler)
  32. Retire(protocol.ConnectionID)
  33. Remove(protocol.ConnectionID)
  34. AddResetToken([16]byte, packetHandler)
  35. RemoveResetToken([16]byte)
  36. GetStatelessResetToken(protocol.ConnectionID) [16]byte
  37. SetServer(unknownPacketHandler)
  38. CloseServer()
  39. }
  40. type quicSession interface {
  41. Session
  42. handlePacket(*receivedPacket)
  43. GetVersion() protocol.VersionNumber
  44. getPerspective() protocol.Perspective
  45. run() error
  46. destroy(error)
  47. closeForRecreating() protocol.PacketNumber
  48. closeRemote(error)
  49. }
  50. type sessionRunner interface {
  51. OnHandshakeComplete(Session)
  52. Retire(protocol.ConnectionID)
  53. Remove(protocol.ConnectionID)
  54. AddResetToken([16]byte, packetHandler)
  55. RemoveResetToken([16]byte)
  56. }
  57. type runner struct {
  58. packetHandlerManager
  59. onHandshakeCompleteImpl func(Session)
  60. }
  61. func (r *runner) OnHandshakeComplete(s Session) { r.onHandshakeCompleteImpl(s) }
  62. var _ sessionRunner = &runner{}
  63. // A Listener of QUIC
  64. type server struct {
  65. mutex sync.Mutex
  66. tlsConf *tls.Config
  67. config *Config
  68. conn net.PacketConn
  69. // If the server is started with ListenAddr, we create a packet conn.
  70. // If it is started with Listen, we take a packet conn as a parameter.
  71. createdPacketConn bool
  72. cookieGenerator *handshake.CookieGenerator
  73. sessionHandler packetHandlerManager
  74. // set as a member, so they can be set in the tests
  75. newSession func(connection, sessionRunner, protocol.ConnectionID /* original connection ID */, protocol.ConnectionID /* destination connection ID */, protocol.ConnectionID /* source connection ID */, *Config, *tls.Config, *handshake.TransportParameters, utils.Logger, protocol.VersionNumber) (quicSession, error)
  76. serverError error
  77. errorChan chan struct{}
  78. closed bool
  79. sessionQueue chan Session
  80. sessionQueueLen int32 // to be used as an atomic
  81. sessionRunner sessionRunner
  82. logger utils.Logger
  83. }
  84. var _ Listener = &server{}
  85. var _ unknownPacketHandler = &server{}
  86. // ListenAddr creates a QUIC server listening on a given address.
  87. // The tls.Config must not be nil and must contain a certificate configuration.
  88. // The quic.Config may be nil, in that case the default values will be used.
  89. func ListenAddr(addr string, tlsConf *tls.Config, config *Config) (Listener, error) {
  90. udpAddr, err := net.ResolveUDPAddr("udp", addr)
  91. if err != nil {
  92. return nil, err
  93. }
  94. conn, err := net.ListenUDP("udp", udpAddr)
  95. if err != nil {
  96. return nil, err
  97. }
  98. serv, err := listen(conn, tlsConf, config)
  99. if err != nil {
  100. return nil, err
  101. }
  102. serv.createdPacketConn = true
  103. return serv, nil
  104. }
  105. // Listen listens for QUIC connections on a given net.PacketConn.
  106. // A single PacketConn only be used for a single call to Listen.
  107. // The PacketConn can be used for simultaneous calls to Dial.
  108. // QUIC connection IDs are used for demultiplexing the different connections.
  109. // The tls.Config must not be nil and must contain a certificate configuration.
  110. // The quic.Config may be nil, in that case the default values will be used.
  111. func Listen(conn net.PacketConn, tlsConf *tls.Config, config *Config) (Listener, error) {
  112. return listen(conn, tlsConf, config)
  113. }
  114. func listen(conn net.PacketConn, tlsConf *tls.Config, config *Config) (*server, error) {
  115. // TODO(#1655): only require that tls.Config.Certificates or tls.Config.GetCertificate is set
  116. if tlsConf == nil || len(tlsConf.Certificates) == 0 {
  117. return nil, errors.New("quic: Certificates not set in tls.Config")
  118. }
  119. config = populateServerConfig(config)
  120. for _, v := range config.Versions {
  121. if !protocol.IsValidVersion(v) {
  122. return nil, fmt.Errorf("%s is not a valid QUIC version", v)
  123. }
  124. }
  125. sessionHandler, err := getMultiplexer().AddConn(conn, config.ConnectionIDLength, config.StatelessResetKey)
  126. if err != nil {
  127. return nil, err
  128. }
  129. s := &server{
  130. conn: conn,
  131. tlsConf: tlsConf,
  132. config: config,
  133. sessionHandler: sessionHandler,
  134. sessionQueue: make(chan Session),
  135. errorChan: make(chan struct{}),
  136. newSession: newSession,
  137. logger: utils.DefaultLogger.WithPrefix("server"),
  138. }
  139. if err := s.setup(); err != nil {
  140. return nil, err
  141. }
  142. sessionHandler.SetServer(s)
  143. s.logger.Debugf("Listening for %s connections on %s", conn.LocalAddr().Network(), conn.LocalAddr().String())
  144. return s, nil
  145. }
  146. func (s *server) setup() error {
  147. s.sessionRunner = &runner{
  148. packetHandlerManager: s.sessionHandler,
  149. onHandshakeCompleteImpl: func(sess Session) {
  150. go func() {
  151. atomic.AddInt32(&s.sessionQueueLen, 1)
  152. defer atomic.AddInt32(&s.sessionQueueLen, -1)
  153. select {
  154. case s.sessionQueue <- sess:
  155. // blocks until the session is accepted
  156. case <-sess.Context().Done():
  157. // don't pass sessions that were already closed to Accept()
  158. }
  159. }()
  160. },
  161. }
  162. cookieGenerator, err := handshake.NewCookieGenerator()
  163. if err != nil {
  164. return err
  165. }
  166. s.cookieGenerator = cookieGenerator
  167. return nil
  168. }
  169. var defaultAcceptCookie = func(clientAddr net.Addr, cookie *Cookie) bool {
  170. if cookie == nil {
  171. return false
  172. }
  173. if time.Now().After(cookie.SentTime.Add(protocol.CookieExpiryTime)) {
  174. return false
  175. }
  176. var sourceAddr string
  177. if udpAddr, ok := clientAddr.(*net.UDPAddr); ok {
  178. sourceAddr = udpAddr.IP.String()
  179. } else {
  180. sourceAddr = clientAddr.String()
  181. }
  182. return sourceAddr == cookie.RemoteAddr
  183. }
  184. // populateServerConfig populates fields in the quic.Config with their default values, if none are set
  185. // it may be called with nil
  186. func populateServerConfig(config *Config) *Config {
  187. if config == nil {
  188. config = &Config{}
  189. }
  190. versions := config.Versions
  191. if len(versions) == 0 {
  192. versions = protocol.SupportedVersions
  193. }
  194. vsa := defaultAcceptCookie
  195. if config.AcceptCookie != nil {
  196. vsa = config.AcceptCookie
  197. }
  198. handshakeTimeout := protocol.DefaultHandshakeTimeout
  199. if config.HandshakeTimeout != 0 {
  200. handshakeTimeout = config.HandshakeTimeout
  201. }
  202. idleTimeout := protocol.DefaultIdleTimeout
  203. if config.IdleTimeout != 0 {
  204. idleTimeout = config.IdleTimeout
  205. }
  206. maxReceiveStreamFlowControlWindow := config.MaxReceiveStreamFlowControlWindow
  207. if maxReceiveStreamFlowControlWindow == 0 {
  208. maxReceiveStreamFlowControlWindow = protocol.DefaultMaxReceiveStreamFlowControlWindow
  209. }
  210. maxReceiveConnectionFlowControlWindow := config.MaxReceiveConnectionFlowControlWindow
  211. if maxReceiveConnectionFlowControlWindow == 0 {
  212. maxReceiveConnectionFlowControlWindow = protocol.DefaultMaxReceiveConnectionFlowControlWindow
  213. }
  214. maxIncomingStreams := config.MaxIncomingStreams
  215. if maxIncomingStreams == 0 {
  216. maxIncomingStreams = protocol.DefaultMaxIncomingStreams
  217. } else if maxIncomingStreams < 0 {
  218. maxIncomingStreams = 0
  219. }
  220. maxIncomingUniStreams := config.MaxIncomingUniStreams
  221. if maxIncomingUniStreams == 0 {
  222. maxIncomingUniStreams = protocol.DefaultMaxIncomingUniStreams
  223. } else if maxIncomingUniStreams < 0 {
  224. maxIncomingUniStreams = 0
  225. }
  226. connIDLen := config.ConnectionIDLength
  227. if connIDLen == 0 {
  228. connIDLen = protocol.DefaultConnectionIDLength
  229. }
  230. return &Config{
  231. Versions: versions,
  232. HandshakeTimeout: handshakeTimeout,
  233. IdleTimeout: idleTimeout,
  234. AcceptCookie: vsa,
  235. KeepAlive: config.KeepAlive,
  236. MaxReceiveStreamFlowControlWindow: maxReceiveStreamFlowControlWindow,
  237. MaxReceiveConnectionFlowControlWindow: maxReceiveConnectionFlowControlWindow,
  238. MaxIncomingStreams: maxIncomingStreams,
  239. MaxIncomingUniStreams: maxIncomingUniStreams,
  240. ConnectionIDLength: connIDLen,
  241. StatelessResetKey: config.StatelessResetKey,
  242. }
  243. }
  244. // Accept returns newly openend sessions
  245. func (s *server) Accept() (Session, error) {
  246. var sess Session
  247. select {
  248. case sess = <-s.sessionQueue:
  249. return sess, nil
  250. case <-s.errorChan:
  251. return nil, s.serverError
  252. }
  253. }
  254. // Close the server
  255. func (s *server) Close() error {
  256. s.mutex.Lock()
  257. defer s.mutex.Unlock()
  258. if s.closed {
  259. return nil
  260. }
  261. return s.closeWithMutex()
  262. }
  263. func (s *server) closeWithMutex() error {
  264. s.sessionHandler.CloseServer()
  265. if s.serverError == nil {
  266. s.serverError = errors.New("server closed")
  267. }
  268. var err error
  269. // If the server was started with ListenAddr, we created the packet conn.
  270. // We need to close it in order to make the go routine reading from that conn return.
  271. if s.createdPacketConn {
  272. err = s.sessionHandler.Close()
  273. }
  274. s.closed = true
  275. close(s.errorChan)
  276. return err
  277. }
  278. func (s *server) closeWithError(e error) error {
  279. s.mutex.Lock()
  280. defer s.mutex.Unlock()
  281. if s.closed {
  282. return nil
  283. }
  284. s.serverError = e
  285. return s.closeWithMutex()
  286. }
  287. // Addr returns the server's network address
  288. func (s *server) Addr() net.Addr {
  289. return s.conn.LocalAddr()
  290. }
  291. func (s *server) handlePacket(p *receivedPacket) {
  292. go func() {
  293. if shouldReleaseBuffer := s.handlePacketImpl(p); !shouldReleaseBuffer {
  294. p.buffer.Release()
  295. }
  296. }()
  297. }
  298. func (s *server) handlePacketImpl(p *receivedPacket) bool /* was the packet passed on to a session */ {
  299. if len(p.data) < protocol.MinInitialPacketSize {
  300. s.logger.Debugf("Dropping a packet that is too small to be a valid Initial (%d bytes)", len(p.data))
  301. return false
  302. }
  303. // If we're creating a new session, the packet will be passed to the session.
  304. // The header will then be parsed again.
  305. hdr, _, _, err := wire.ParsePacket(p.data, s.config.ConnectionIDLength)
  306. if err != nil {
  307. s.logger.Debugf("Error parsing packet: %s", err)
  308. return false
  309. }
  310. // Short header packets should never end up here in the first place
  311. if !hdr.IsLongHeader {
  312. return false
  313. }
  314. // send a Version Negotiation Packet if the client is speaking a different protocol version
  315. if !protocol.IsSupportedVersion(s.config.Versions, hdr.Version) {
  316. s.sendVersionNegotiationPacket(p, hdr)
  317. return false
  318. }
  319. if hdr.IsLongHeader && hdr.Type != protocol.PacketTypeInitial {
  320. // Drop long header packets.
  321. // There's litte point in sending a Stateless Reset, since the client
  322. // might not have received the token yet.
  323. return false
  324. }
  325. s.logger.Debugf("<- Received Initial packet.")
  326. sess, connID, err := s.handleInitialImpl(p, hdr)
  327. if err != nil {
  328. s.logger.Errorf("Error occurred handling initial packet: %s", err)
  329. return false
  330. }
  331. if sess == nil { // a retry was done, or the connection attempt was rejected
  332. return false
  333. }
  334. // Don't put the packet buffer back if a new session was created.
  335. // The session will handle the packet and take of that.
  336. s.sessionHandler.Add(connID, sess)
  337. return true
  338. }
  339. func (s *server) handleInitialImpl(p *receivedPacket, hdr *wire.Header) (quicSession, protocol.ConnectionID, error) {
  340. if len(hdr.Token) == 0 && hdr.DestConnectionID.Len() < protocol.MinConnectionIDLenInitial {
  341. return nil, nil, errors.New("too short connection ID")
  342. }
  343. var cookie *Cookie
  344. var origDestConnectionID protocol.ConnectionID
  345. if len(hdr.Token) > 0 {
  346. c, err := s.cookieGenerator.DecodeToken(hdr.Token)
  347. if err == nil {
  348. cookie = &Cookie{
  349. RemoteAddr: c.RemoteAddr,
  350. SentTime: c.SentTime,
  351. }
  352. origDestConnectionID = c.OriginalDestConnectionID
  353. }
  354. }
  355. if !s.config.AcceptCookie(p.remoteAddr, cookie) {
  356. // Log the Initial packet now.
  357. // If no Retry is sent, the packet will be logged by the session.
  358. (&wire.ExtendedHeader{Header: *hdr}).Log(s.logger)
  359. return nil, nil, s.sendRetry(p.remoteAddr, hdr)
  360. }
  361. if queueLen := atomic.LoadInt32(&s.sessionQueueLen); queueLen >= protocol.MaxAcceptQueueSize {
  362. s.logger.Debugf("Rejecting new connection. Server currently busy. Accept queue length: %d (max %d)", queueLen, protocol.MaxAcceptQueueSize)
  363. return nil, nil, s.sendServerBusy(p.remoteAddr, hdr)
  364. }
  365. connID, err := protocol.GenerateConnectionID(s.config.ConnectionIDLength)
  366. if err != nil {
  367. return nil, nil, err
  368. }
  369. s.logger.Debugf("Changing connection ID to %s.", connID)
  370. sess, err := s.createNewSession(
  371. p.remoteAddr,
  372. origDestConnectionID,
  373. hdr.DestConnectionID,
  374. hdr.SrcConnectionID,
  375. connID,
  376. hdr.Version,
  377. )
  378. if err != nil {
  379. return nil, nil, err
  380. }
  381. sess.handlePacket(p)
  382. return sess, connID, nil
  383. }
  384. func (s *server) createNewSession(
  385. remoteAddr net.Addr,
  386. origDestConnID protocol.ConnectionID,
  387. clientDestConnID protocol.ConnectionID,
  388. destConnID protocol.ConnectionID,
  389. srcConnID protocol.ConnectionID,
  390. version protocol.VersionNumber,
  391. ) (quicSession, error) {
  392. token := s.sessionHandler.GetStatelessResetToken(srcConnID)
  393. params := &handshake.TransportParameters{
  394. InitialMaxStreamDataBidiLocal: protocol.InitialMaxStreamData,
  395. InitialMaxStreamDataBidiRemote: protocol.InitialMaxStreamData,
  396. InitialMaxStreamDataUni: protocol.InitialMaxStreamData,
  397. InitialMaxData: protocol.InitialMaxData,
  398. IdleTimeout: s.config.IdleTimeout,
  399. MaxBidiStreams: uint64(s.config.MaxIncomingStreams),
  400. MaxUniStreams: uint64(s.config.MaxIncomingUniStreams),
  401. AckDelayExponent: protocol.AckDelayExponent,
  402. DisableMigration: true,
  403. StatelessResetToken: &token,
  404. OriginalConnectionID: origDestConnID,
  405. }
  406. sess, err := s.newSession(
  407. &conn{pconn: s.conn, currentAddr: remoteAddr},
  408. s.sessionRunner,
  409. clientDestConnID,
  410. destConnID,
  411. srcConnID,
  412. s.config,
  413. s.tlsConf,
  414. params,
  415. s.logger,
  416. version,
  417. )
  418. if err != nil {
  419. return nil, err
  420. }
  421. go sess.run()
  422. return sess, nil
  423. }
  424. func (s *server) sendRetry(remoteAddr net.Addr, hdr *wire.Header) error {
  425. token, err := s.cookieGenerator.NewToken(remoteAddr, hdr.DestConnectionID)
  426. if err != nil {
  427. return err
  428. }
  429. connID, err := protocol.GenerateConnectionID(s.config.ConnectionIDLength)
  430. if err != nil {
  431. return err
  432. }
  433. replyHdr := &wire.ExtendedHeader{}
  434. replyHdr.IsLongHeader = true
  435. replyHdr.Type = protocol.PacketTypeRetry
  436. replyHdr.Version = hdr.Version
  437. replyHdr.SrcConnectionID = connID
  438. replyHdr.DestConnectionID = hdr.SrcConnectionID
  439. replyHdr.OrigDestConnectionID = hdr.DestConnectionID
  440. replyHdr.Token = token
  441. s.logger.Debugf("Changing connection ID to %s.\n-> Sending Retry", connID)
  442. replyHdr.Log(s.logger)
  443. buf := &bytes.Buffer{}
  444. if err := replyHdr.Write(buf, hdr.Version); err != nil {
  445. return err
  446. }
  447. if _, err := s.conn.WriteTo(buf.Bytes(), remoteAddr); err != nil {
  448. s.logger.Debugf("Error sending Retry: %s", err)
  449. }
  450. return nil
  451. }
  452. func (s *server) sendServerBusy(remoteAddr net.Addr, hdr *wire.Header) error {
  453. sealer, _, err := handshake.NewInitialAEAD(hdr.DestConnectionID, protocol.PerspectiveServer)
  454. if err != nil {
  455. return err
  456. }
  457. packetBuffer := getPacketBuffer()
  458. defer packetBuffer.Release()
  459. buf := bytes.NewBuffer(packetBuffer.Slice[:0])
  460. ccf := &wire.ConnectionCloseFrame{ErrorCode: qerr.ServerBusy}
  461. replyHdr := &wire.ExtendedHeader{}
  462. replyHdr.IsLongHeader = true
  463. replyHdr.Type = protocol.PacketTypeInitial
  464. replyHdr.Version = hdr.Version
  465. replyHdr.SrcConnectionID = hdr.DestConnectionID
  466. replyHdr.DestConnectionID = hdr.SrcConnectionID
  467. replyHdr.PacketNumberLen = protocol.PacketNumberLen4
  468. replyHdr.Length = 4 /* packet number len */ + ccf.Length(hdr.Version) + protocol.ByteCount(sealer.Overhead())
  469. if err := replyHdr.Write(buf, hdr.Version); err != nil {
  470. return err
  471. }
  472. payloadOffset := buf.Len()
  473. if err := ccf.Write(buf, hdr.Version); err != nil {
  474. return err
  475. }
  476. raw := buf.Bytes()
  477. _ = sealer.Seal(raw[payloadOffset:payloadOffset], raw[payloadOffset:], replyHdr.PacketNumber, raw[:payloadOffset])
  478. raw = raw[0 : buf.Len()+sealer.Overhead()]
  479. pnOffset := payloadOffset - int(replyHdr.PacketNumberLen)
  480. sealer.EncryptHeader(
  481. raw[pnOffset+4:pnOffset+4+16],
  482. &raw[0],
  483. raw[pnOffset:payloadOffset],
  484. )
  485. replyHdr.Log(s.logger)
  486. wire.LogFrame(s.logger, ccf, true)
  487. if _, err := s.conn.WriteTo(raw, remoteAddr); err != nil {
  488. s.logger.Debugf("Error rejecting connection: %s", err)
  489. }
  490. return nil
  491. }
  492. func (s *server) sendVersionNegotiationPacket(p *receivedPacket, hdr *wire.Header) {
  493. s.logger.Debugf("Client offered version %s, sending Version Negotiation", hdr.Version)
  494. data, err := wire.ComposeVersionNegotiation(hdr.SrcConnectionID, hdr.DestConnectionID, s.config.Versions)
  495. if err != nil {
  496. s.logger.Debugf("Error composing Version Negotiation: %s", err)
  497. return
  498. }
  499. if _, err := s.conn.WriteTo(data, p.remoteAddr); err != nil {
  500. s.logger.Debugf("Error sending Version Negotiation: %s", err)
  501. }
  502. }