gsm_04_11: Place subscr_put_channel whenever being done with SMS.
diff --git a/openbsc/src/gsm_04_11.c b/openbsc/src/gsm_04_11.c
index 3d7f7e1..8a85933 100644
--- a/openbsc/src/gsm_04_11.c
+++ b/openbsc/src/gsm_04_11.c
@@ -117,6 +117,19 @@
 	talloc_free(sms);
 }
 
+/*
+ * This should be called whenever all SMS to a given subscriber
+ * on a given connection has been sent. This will inform the higher
+ * layers that a channel can be given up.
+ */
+static void gsm411_release_conn(struct gsm_subscriber_connection *conn)
+{
+	if (!conn->lchan)
+		return;
+
+	subscr_put_channel(conn->lchan);
+}
+
 struct msgb *gsm411_msgb_alloc(void)
 {
 	return msgb_alloc_headroom(GSM411_ALLOC_SIZE, GSM411_ALLOC_HEADROOM,
@@ -750,6 +763,8 @@
 	sms = db_sms_get_unsent_for_subscr(trans->subscr);
 	if (sms)
 		gsm411_send_sms_lchan(trans->conn, sms);
+	else
+		gsm411_release_conn(trans->conn);
 
 	/* free the transaction here */
 	trans_free(trans);
@@ -824,6 +839,8 @@
 	sms = db_sms_get_unsent_for_subscr(trans->subscr);
 	if (sms)
 		gsm411_send_sms_lchan(trans->conn, sms);
+	else
+		gsm411_release_conn(trans->conn);
 
 	return rc;
 }
@@ -1107,6 +1124,7 @@
 
 	switch (event) {
 	case GSM_PAGING_SUCCEEDED:
+		use_subscr_con(&lchan->conn);
 		gsm411_send_sms_lchan(&lchan->conn, sms);
 		break;
 	case GSM_PAGING_EXPIRED:
@@ -1130,8 +1148,10 @@
 	/* check if we already have an open lchan to the subscriber.
 	 * if yes, send the SMS this way */
 	lchan = lchan_for_subscr(subscr);
-	if (lchan)
+	if (lchan) {
+		use_subscr_con(&lchan->conn);
 		gsm411_send_sms_lchan(&lchan->conn, sms);
+	}
 
 	/* if not, we have to start paging */
 	subscr_get_channel(subscr, RSL_CHANNEED_SDCCH, paging_cb_send_sms, sms);
@@ -1156,6 +1176,7 @@
 		sms = db_sms_get_unsent_for_subscr(subscr);
 		if (!sms)
 			break;
+		use_subscr_con(&lchan->conn);
 		gsm411_send_sms_lchan(&lchan->conn, sms);
 		break;
 	default:
@@ -1191,6 +1212,8 @@
 			trans->sms.sms = NULL;
 			trans_free(trans);
 		}
+
+	gsm411_release_conn(conn);
 }
 
 static __attribute__((constructor)) void on_dso_load_sms(void)