sshbpp.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Abstraction of the binary packet protocols used in SSH.
  3. */
  4. #ifndef PUTTY_SSHBPP_H
  5. #define PUTTY_SSHBPP_H
  6. struct BinaryPacketProtocolVtable {
  7. void (*free)(BinaryPacketProtocol *);
  8. void (*handle_input)(BinaryPacketProtocol *);
  9. void (*handle_output)(BinaryPacketProtocol *);
  10. PktOut *(*new_pktout)(int type);
  11. void (*queue_disconnect)(BinaryPacketProtocol *,
  12. const char *msg, int category);
  13. uint32_t packet_size_limit;
  14. };
  15. struct BinaryPacketProtocol {
  16. const struct BinaryPacketProtocolVtable *vt;
  17. bufchain *in_raw, *out_raw;
  18. bool input_eof; /* set this if in_raw will never be added to again */
  19. PktInQueue in_pq;
  20. PktOutQueue out_pq;
  21. PacketLogSettings *pls;
  22. LogContext *logctx;
  23. Ssh *ssh;
  24. /* ic_in_raw is filled in by the BPP (probably by calling
  25. * ssh_bpp_common_setup). The BPP's owner triggers it when data is
  26. * added to in_raw, and also when the BPP is newly created. */
  27. IdempotentCallback ic_in_raw;
  28. /* ic_out_pq is entirely internal to the BPP itself; it's used as
  29. * the callback on out_pq. */
  30. IdempotentCallback ic_out_pq;
  31. int remote_bugs;
  32. /* Set this if remote connection closure should not generate an
  33. * error message (either because it's not to be treated as an
  34. * error at all, or because some other error message has already
  35. * been emitted). */
  36. bool expect_close;
  37. };
  38. static inline void ssh_bpp_handle_input(BinaryPacketProtocol *bpp)
  39. { bpp->vt->handle_input(bpp); }
  40. static inline void ssh_bpp_handle_output(BinaryPacketProtocol *bpp)
  41. { bpp->vt->handle_output(bpp); }
  42. static inline PktOut *ssh_bpp_new_pktout(BinaryPacketProtocol *bpp, int type)
  43. { return bpp->vt->new_pktout(type); }
  44. static inline void ssh_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  45. const char *msg, int category)
  46. { bpp->vt->queue_disconnect(bpp, msg, category); }
  47. /* ssh_bpp_free is more than just a macro wrapper on the vtable; it
  48. * does centralised parts of the freeing too. */
  49. void ssh_bpp_free(BinaryPacketProtocol *bpp);
  50. BinaryPacketProtocol *ssh1_bpp_new(LogContext *logctx);
  51. void ssh1_bpp_new_cipher(BinaryPacketProtocol *bpp,
  52. const ssh_cipheralg *cipher,
  53. const void *session_key);
  54. /* This is only called from outside the BPP in server mode; in client
  55. * mode the BPP detects compression start time automatically by
  56. * snooping message types */
  57. void ssh1_bpp_start_compression(BinaryPacketProtocol *bpp);
  58. /* Helper routine which does common BPP initialisation, e.g. setting
  59. * up in_pq and out_pq, and initialising input_consumer. */
  60. void ssh_bpp_common_setup(BinaryPacketProtocol *);
  61. /* Common helper functions between the SSH-2 full and bare BPPs */
  62. void ssh2_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  63. const char *msg, int category);
  64. bool ssh2_bpp_check_unimplemented(BinaryPacketProtocol *bpp, PktIn *pktin);
  65. /* Convenience macro for BPPs to send formatted strings to the Event
  66. * Log. Assumes a function parameter called 'bpp' is in scope. */
  67. #define bpp_logevent(...) ( \
  68. logevent_and_free((bpp)->logctx, dupprintf(__VA_ARGS__)))
  69. /*
  70. * Structure that tracks how much data is sent and received, for
  71. * purposes of triggering an SSH-2 rekey when either one gets over a
  72. * configured limit. In each direction, the flag 'running' indicates
  73. * that we haven't hit the limit yet, and 'remaining' tracks how much
  74. * longer until we do. The function dts_consume() subtracts a given
  75. * amount from the counter in a particular direction, and sets
  76. * 'expired' if the limit has been hit.
  77. *
  78. * The limit is sticky: once 'running' has flipped to false,
  79. * 'remaining' is no longer decremented, so it shouldn't dangerously
  80. * wrap round.
  81. */
  82. struct DataTransferStatsDirection {
  83. bool running, expired;
  84. unsigned long remaining;
  85. };
  86. struct DataTransferStats {
  87. struct DataTransferStatsDirection in, out;
  88. };
  89. static inline void dts_consume(struct DataTransferStatsDirection *s,
  90. unsigned long size_consumed)
  91. {
  92. if (s->running) {
  93. if (s->remaining <= size_consumed) {
  94. s->running = false;
  95. s->expired = true;
  96. } else {
  97. s->remaining -= size_consumed;
  98. }
  99. }
  100. }
  101. static inline void dts_reset(struct DataTransferStatsDirection *s,
  102. unsigned long starting_size)
  103. {
  104. s->expired = false;
  105. s->remaining = starting_size;
  106. /*
  107. * The semantics of setting CONF_ssh_rekey_data to zero are to
  108. * disable data-volume based rekeying completely. So if the
  109. * starting size is actually zero, we don't set 'running' to true
  110. * in the first place, which means we won't ever set the expired
  111. * flag.
  112. */
  113. s->running = (starting_size != 0);
  114. }
  115. BinaryPacketProtocol *ssh2_bpp_new(
  116. LogContext *logctx, struct DataTransferStats *stats, bool is_server);
  117. void ssh2_bpp_new_outgoing_crypto(
  118. BinaryPacketProtocol *bpp,
  119. const ssh_cipheralg *cipher, const void *ckey, const void *iv,
  120. const ssh2_macalg *mac, bool etm_mode, const void *mac_key,
  121. const ssh_compression_alg *compression, bool delayed_compression);
  122. void ssh2_bpp_new_incoming_crypto(
  123. BinaryPacketProtocol *bpp,
  124. const ssh_cipheralg *cipher, const void *ckey, const void *iv,
  125. const ssh2_macalg *mac, bool etm_mode, const void *mac_key,
  126. const ssh_compression_alg *compression, bool delayed_compression);
  127. /*
  128. * A query method specific to the interface between ssh2transport and
  129. * ssh2bpp. If true, it indicates that we're potentially in the
  130. * race-condition-prone part of delayed compression setup and so
  131. * asynchronous outgoing transport-layer packets are currently not
  132. * being sent, which means in particular that it would be a bad idea
  133. * to start a rekey because then we'd stop responding to anything
  134. * _other_ than transport-layer packets and deadlock the protocol.
  135. */
  136. bool ssh2_bpp_rekey_inadvisable(BinaryPacketProtocol *bpp);
  137. BinaryPacketProtocol *ssh2_bare_bpp_new(LogContext *logctx);
  138. /*
  139. * The initial code to handle the SSH version exchange is also
  140. * structured as an implementation of BinaryPacketProtocol, because
  141. * that makes it easy to switch from that to the next BPP once it
  142. * tells us which one we're using.
  143. */
  144. struct ssh_version_receiver {
  145. void (*got_ssh_version)(struct ssh_version_receiver *rcv,
  146. int major_version);
  147. };
  148. BinaryPacketProtocol *ssh_verstring_new(
  149. Conf *conf, LogContext *logctx, bool bare_connection_mode,
  150. const char *protoversion, struct ssh_version_receiver *rcv,
  151. bool server_mode, const char *impl_name);
  152. const char *ssh_verstring_get_remote(BinaryPacketProtocol *);
  153. const char *ssh_verstring_get_local(BinaryPacketProtocol *);
  154. int ssh_verstring_get_bugs(BinaryPacketProtocol *);
  155. #endif /* PUTTY_SSHBPP_H */