Use libvlr in libmsc (large refactoring)

Original libvlr code is by Harald Welte <laforge@gnumonks.org>,
polished and tweaked by Neels Hofmeyr <nhofmeyr@sysmocom.de>.

This is a long series of trial-and-error development collapsed in one patch.
This may be split in smaller commits if reviewers prefer that. If we can keep
it as one, we have saved ourselves the additional separation work.

SMS:

The SQL based lookup of SMS for attached subscribers no longer works since the
SQL database no longer has the subscriber data. Replace with a round-robin on
the SMS recipient MSISDNs paired with a VLR subscriber RAM lookup whether the
subscriber is currently attached.

If there are many SMS for not-attached subscribers in the SMS database, this
will become inefficient: a DB hit returns a pending SMS, the RAM lookup will
reveal that the subscriber is not attached, after which the DB is hit for the
next SMS. It would become more efficient e.g. by having an MSISDN based hash
list for the VLR subscribers and by marking non-attached SMS recipients in the
SMS database so that they can be excluded with the SQL query already.

There is a sanity limit to do at most 100 db hits per attempt to find a pending
SMS. So if there are more than 100 stored SMS waiting for their recipients to
actually attach to the MSC, it may take more than one SMS queue trigger to
deliver SMS for subscribers that are actually attached.

This is not very beautiful, but is merely intended to carry us over to a time
when we have a proper separate SMSC entity.

Introduce gsm_subscriber_connection ref-counting in libmsc.

Remove/Disable VTY and CTRL commands to create subscribers, which is now a task
of the OsmoHLR. Adjust the python tests accordingly.

Remove VTY cmd subscriber-keep-in-ram.

Use OSMO_GSUP_PORT = 4222 instead of 2222. See
I4222e21686c823985be8ff1f16b1182be8ad6175.

So far use the LAC from conn->bts, will be replaced by conn->lac in
Id3705236350d5f69e447046b0a764bbabc3d493c.

Related: OS#1592 OS#1974
Change-Id: I639544a6cdda77a3aafc4e3446a55393f60e4050
diff --git a/include/openbsc/gsm_data.h b/include/openbsc/gsm_data.h
index a974051..fa2fed7 100644
--- a/include/openbsc/gsm_data.h
+++ b/include/openbsc/gsm_data.h
@@ -24,6 +24,8 @@
 struct mncc_sock_state;
 struct gsm_subscriber_group;
 struct bsc_subscr;
+struct vlr_instance;
+struct vlr_subscr;
 
 #define OBSC_LINKID_CB(__msgb)	(__msgb)->cb[3]
 
@@ -70,20 +72,6 @@
 #define GSM_KEY_SEQ_INVAL	7	/* GSM 04.08 - 10.5.1.2 */
 
 /*
- * LOCATION UPDATING REQUEST state
- *
- * Our current operation is:
- *	- Get imei/tmsi
- *	- Accept/Reject according to global policy
- */
-struct gsm_loc_updating_operation {
-        struct osmo_timer_list updating_timer;
-	unsigned int waiting_for_imsi : 1;
-	unsigned int waiting_for_imei : 1;
-	unsigned int key_seq : 4;
-};
-
-/*
  * AUTHENTICATION/CIPHERING state
  */
 struct gsm_security_operation {
@@ -120,16 +108,34 @@
        RAN_UTRAN_IU,	/* 3G / Iu-interface (IuCS or IuPS) */
 };
 
+struct gsm_classmark {
+	bool classmark1_set;
+	struct gsm48_classmark1 classmark1;
+	uint8_t classmark2_len;
+	uint8_t classmark2[3];
+	uint8_t classmark3_len;
+	uint8_t classmark3[14]; /* if cm3 gets extended by spec, it will be truncated */
+};
+
 /* active radio connection of a mobile subscriber */
 struct gsm_subscriber_connection {
+	/* global linked list of subscriber_connections */
 	struct llist_head entry;
 
-	/* To whom we are allocated at the moment */
-	struct gsm_subscriber *subscr;
+	/* usage count. If this drops to zero, we start the release
+	 * towards A/Iu */
+	uint32_t use_count;
 
-	/* libbsc subscriber information */
+	/* The MS has opened the conn with a CM Service Request, and we shall
+	 * keep it open for an actual request (or until timeout). */
+	bool received_cm_service_request;
+
+	/* libbsc subscriber information (if available) */
 	struct bsc_subscr *bsub;
 
+	/* libmsc/libvlr subscriber information (if available) */
+	struct vlr_subscr *vsub;
+
 	/* LU expiration handling */
 	uint8_t expire_timer_stopped;
 	/* SMS helpers for libmsc */
@@ -138,10 +144,11 @@
 	/*
 	 * Operations that have a state and might be pending
 	 */
-	struct gsm_loc_updating_operation *loc_operation;
 	struct gsm_security_operation *sec_operation;
 	struct gsm_anchor_operation *anch_operation;
 
+	struct osmo_fsm_inst *conn_fsm;
+
 	/* Are we part of a special "silent" call */
 	int silent_call;
 
@@ -156,7 +163,7 @@
 	/* back pointers */
 	struct gsm_network *network;
 
-	int in_release;
+	bool in_release;
 	struct gsm_lchan *lchan; /* BSC */
 	struct gsm_lchan *ho_lchan; /* BSC */
 	struct gsm_bts *bts; /* BSC */
@@ -167,6 +174,8 @@
 
 	/* connected via 2G or 3G? */
 	enum ran_type via_ran;
+
+	struct gsm_classmark classmark;
 };
 
 
@@ -312,6 +321,7 @@
 	char *authorized_reg_str;
 	enum gsm48_reject_value reject_cause;
 	int a5_encryption;
+	bool authentication_required;
 	int neci;
 	int send_mm_info;
 	struct {
@@ -375,17 +385,8 @@
 	/* MSC data in case we are a true BSC */
 	struct osmo_bsc_data *bsc_data;
 
-	/* subscriber related features */
-	bool auto_create_subscr;
-	bool auto_assign_exten;
-	uint64_t ext_min;
-	uint64_t ext_max;
-	struct gsm_subscriber_group *subscr_group;
 	struct gsm_sms_queue *sms_queue;
 
-	/* nitb related control */
-	int avoid_tmsi;
-
 	/* control interface */
 	struct ctrl_handle *ctrl;
 
@@ -409,6 +410,12 @@
 	 * not require gsm_data.h). In an MSC-without-BSC environment, this
 	 * pointer is NULL to indicate absence of a bsc_subscribers list. */
 	struct llist_head *bsc_subscribers;
+
+	/* MSC: GSUP server address of the HLR */
+	const char *gsup_server_addr_str;
+	uint16_t gsup_server_port;
+
+	struct vlr_instance *vlr;
 };
 
 struct osmo_esme;
@@ -431,7 +438,7 @@
 
 struct gsm_sms {
 	unsigned long long id;
-	struct gsm_subscriber *receiver;
+	struct vlr_subscr *receiver;
 	struct gsm_sms_addr src, dst;
 	enum gsm_sms_source_id source;
 
@@ -582,7 +589,7 @@
 
 /* control interface handling */
 int bsc_base_ctrl_cmds_install(void);
-int msc_ctrl_cmds_install(void);
+int msc_ctrl_cmds_install(struct gsm_network *net);
 
 /* dependency handling */
 void bts_depend_mark(struct gsm_bts *bts, int dep);
@@ -593,4 +600,6 @@
 int gsm_bts_get_radio_link_timeout(const struct gsm_bts *bts);
 void gsm_bts_set_radio_link_timeout(struct gsm_bts *bts, int value);
 
+bool classmark_is_r99(struct gsm_classmark *cm);
+
 #endif /* _GSM_DATA_H */