[tls] Send closure alert only when we are initiating the closure

When the TLS connection is closed by the underlying socket, the
closure alert will not be able to be sent.  This currently results in
a harmless but mildly irritating error message when debugging is
enabled.

Fix by sending the closure alert only when we are actively choosing to
close the connection.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
diff --git a/src/net/tls.c b/src/net/tls.c
index b37dce7..a53f8c8 100644
--- a/src/net/tls.c
+++ b/src/net/tls.c
@@ -360,9 +360,6 @@
  */
 static void tls_close ( struct tls_connection *tls, int rc ) {
 
-	/* Send closure alert */
-	tls_send_alert ( tls, TLS_ALERT_WARNING, TLS_ALERT_CLOSE_NOTIFY );
-
 	/* Remove pending operations, if applicable */
 	pending_put ( &tls->client.negotiation );
 	pending_put ( &tls->server.negotiation );
@@ -387,6 +384,21 @@
 	tls_tx_resume_all ( tls->session );
 }
 
+/**
+ * Send closure alert and finish with TLS connection
+ *
+ * @v tls		TLS connection
+ * @v rc		Status code
+ */
+static void tls_close_alert ( struct tls_connection *tls, int rc ) {
+
+	/* Send closure alert */
+	tls_send_alert ( tls, TLS_ALERT_WARNING, TLS_ALERT_CLOSE_NOTIFY );
+
+	/* Close connection */
+	tls_close ( tls, rc );
+}
+
 /******************************************************************************
  *
  * Key schedule
@@ -3722,7 +3734,7 @@
 	INTF_OP ( xfer_window, struct tls_connection *,
 		  tls_plainstream_window ),
 	INTF_OP ( job_progress, struct tls_connection *, tls_progress ),
-	INTF_OP ( intf_close, struct tls_connection *, tls_close ),
+	INTF_OP ( intf_close, struct tls_connection *, tls_close_alert ),
 };
 
 /** TLS plaintext stream interface descriptor */
@@ -3917,7 +3929,7 @@
 		/* Process data if buffer is now full */
 		if ( iob_tailroom ( dest ) == 0 ) {
 			if ( ( rc = process ( tls ) ) != 0 ) {
-				tls_close ( tls, rc );
+				tls_close_alert ( tls, rc );
 				goto done;
 			}
 		}
@@ -4006,7 +4018,7 @@
 	return;
 
  err:
-	tls_close ( tls, rc );
+	tls_close_alert ( tls, rc );
 	return;
 }
 
@@ -4135,7 +4147,7 @@
 	return;
 
  err:
-	tls_close ( tls, rc );
+	tls_close_alert ( tls, rc );
 }
 
 /** TLS TX process descriptor */