bsc: Fix use-after-free on OML NM messages from the BTS

Currently the sign_link pointer is dereferenced after a call to
osmo_signal_dispatch, which can indirectly call
e1inp_sign_link_destroy. If that happens, accessing *sign_link is
illegal and can lead to a segmentation violation.

Since only the bts pointer is needed from sign_link after the call to
osmo_signal_dispatch, this patch changes abis_nm_rcvmsg_fom to save
that pointer to a local variable earlier.

Addresses:
<0019> input/ipa.c:250 accept()ed new link from 192.168.1.101 to port 3002
SET ATTR NACK  CAUSE=Message cannot be performed
<0005> bsc_init.c:52 Got a NACK going to drop the OML links.
<001b> bsc_init.c:319 Lost some E1 TEI link: 1 0xb351a830
=================================================================
==13198== ERROR: AddressSanitizer: heap-use-after-free on address 0xb5d1bc70 at pc 0x80a6e3d bp 0xbfbb33d8 sp 0xbfbb33cc

Sponsored-by: On-Waves ehf
bts: Allow to set the LAC through the CTRL interface

Allow to set the LAC of the BTS through the CTRL interface.
The change will not be effective immediately.

Fixes: SYS#738
systemd: use Wants for the dependency btw. osmo-bsc-mgcp and osmo-bsc

* osmo-bsc and osmo-bsc-mgcp are needed to run
* with Wants, both are started but can be indepently
  controlled via systemctl

Fixes: SYS#738
gbproxy: Honour the BSS TLLI type when creating an SGSN TLLI

Currently gbproxy_make_sgsn_tlli always returns a foreign TLLI when
it uses the (SGSN) P-TMSI to generate one.

This patch changes the implementation to return a SGSN TLLI of the
same type like the BSS TLLI in that case.

Sponsored-by: On-Waves ehf
sgsn/test: Move message sending to send_0408_message

This replaces serveral occurences of duplicated code for message
creation and sending (passing to gsm0408_gprs_rcvmsg) into a single
function. In addition, the sgsn_tx_counter is always reset within
send_0408_message to simplify the code that checks for the number of
messages sent.

Sponsored-by: On-Waves ehf
sgsn/test: Add test_gmm_attach

This test checks the attach procedure until the Attach Complete is
received.

Note that authorization and GMM state updates are not working
properly yet.

Sponsored-by: On-Waves ehf
bsc: Move gsm_network_init function to libbsc

Currently libcommon depends on libbsc, because gsm_network_init
(libcommon/gsm_data.c) directly calls gsm_net_update_ctype
(libbsc/gsm_04_08_utils.c).

This patch moves gsm_network_init to a new file libbsc/net_init.c.

Sponsored-by: On-Waves ehf
bsc: Move gsm_subscriber_base.c to libcommon

Since it is planned to use struct gsm_subscriber to manage subscriber
data in the SGSN, this file which contains the generic subscriber
related methods is moved to libcommon.

Sponsored-by: On-Waves ehf
gbproxy: Reset TLLIs when the link_info is found by IMSI/P-TMSI

Currently when the MS does a re-attach without doing a proper detach
first, the gbproxy uses the old local TLLI if patching and the keep
mode are enabled. This leads to a failing attachment procedure when
TLLI patching is also enabled.

This patch changes gbproxy_update_link_state_ul to reset all TLLIs
within the link_info if the message contains an unknown TLLI and an
MI. This is generally the case with Attach Request messages.

The gbproxy_get_link_info_ul gets an additional tlli_is_invalid
output parameter that is set, when a TLLI was present but not found.
This value is then used instead of checking tlli.current == 0 to
handle e.g. Attach Requests when a link_info was already present for
the P-TMSI/IMSI used in such a request.

Ticket: OW#1324
Sponsored-by: On-Waves ehf
sgsn: Reorganize and fix gsm48_gmm_authorize

Currently the order of the 'if' clauses in gsm48_gmm_authorize
doesn't match the order in which the conditional parts are entered.
This makes it difficult to maintain. In addition the t3350_mode is
not stored in every path, so that this information is lost when the
identification procedure is started. Since the default value
coincidentally is GMM_T3350_MODE_ATT, this doesn't hurt for Attach
Requests which are the only messages that initially trigger the
authentication yet.

This patch changes the order of the 'if' clause to match the
processing order, it removes the t3350_mode parameter entirely and
introduces a mm->pending_req field. The latter must be set when the
request that causes the authorization before calling
gsm48_gmm_authorize. The gprs_t3350_mode enum is extended by
GMM_T3350_MODE_NONE (value 0, which is the default) to make it
possible to detect related initialisation errors or race conditions.

Sponsored-by: On-Waves ehf
sgsn: Cleanup GMM state transitions

Currently the GMM state is set to GMM-REGISTERED when an Attach
Accept or a RA Update Accept message is sent, even if a new P-TMSI is
included. In this case 04.08 requires (see 4.7.3.1.3 and 4.7.5.1.3),
that the state is set to GMM-COMMON-PROCEDURE-INITIATED when the
Accept is sent. When the Complete is received, the SGSN shall set
the state to GMM-REGISTERED.

This patch modifies the state updates accordingly.

Sponsored-by: On-Waves ehf
sgsn: Call mm_ctx_cleanup_free to deregister MM context

Currently the MM context isn't always removed when it is
de-registered (mmctx_timer_cb), mm_state is set to GMM_DEREGISTERED
instead. This can lead to left-over MM contexts which are only
cleaned up if the MS reattaches.

This patch replaces all of these assignments by a call to
mm_ctx_cleanup_free.

Ticket: OW#1324
Sponsored-by: On-Waves ehf
sgsn: Split gsm0408_gprs_force_reattach into 2 functions

This patch replaces gsm0408_gprs_force_reattach(msg, mmctx) by two
functions
  - gsm0408_gprs_force_reattach(mmctx)
  - gsm0408_gprs_force_reattach_oldmsg(msg)

The old function basically consists of the code of the two new
functions, where the code patch selected depends on mmctx == NULL,
which is harder to maintain, less obvious to use, and not consistent
with many other SGSN functions.

Sponsored-by: On-Waves ehf
sgsn: Fix LLME leak when forcing a reattach

Currently when forceing a reattach by sending a Detach
Request (reattach), the SGSN waits for the Detach Accept until it
frees the MM context (if present) and the LLME. If that message gets
lost or isn't sent by the MS, the LLME is never freed if it isn't
bound to an MM context.

This patch adds code to free the MM context/LLME when forcing a
reattachment.

Sponsored-by: On-Waves ehf
sgsn: Move IMSI authorization to gsm48_gmm_authorize

Currently the IMSI is only checked immediately when an Attach Request
is received that contains an IMSI IE. If it contains a P-TMSI
instead, access is always granted.

This commit moves the IMSI check to gsm48_gmm_authorize where it is
applied when IMSI and IMEI have been acquired. This fixes the
authorization when the Attach Accept doesn't contain an IMSI.

Sponsored-by: On-Waves ehf
sgsn: Make authorization asynchronous

Currently the authorization of an IMSI is done by checking ACLs
synchronously which is not feasible when the subscriber data has to
be retrieved from an external source.

This patch changes this by using a callback when the information is
available. This is also done when only ACL are checked, in this case
the callback is invoked from within sgsn_auth_request(). The callback
function sgsn_update_subscriber_data calls sgsn_auth_update which
in turn calls either gsm0408_gprs_access_granted or
gsm0408_gprs_access_denied. gsm48_gmm_authorize is extended by a call
to sgsn_auth_request when IMSI and IMEI are available but the
auth_state is unknown.

The change has been successfully tested with single phones (E71 and
IPhone 5c).

Sponsored-by: On-Waves ehf
sgsn/test: Add VTY tests for the SGSN

This patch adds some basic SGSN tests to vty_test_runner.py:
- check for config tree nodes
- check specific show commands

Sponsored-by: On-Waves ehf
sgsn: Add 'acl-only' authentication policy

Currently the VTY 'auth-policy' command results in setting or clearing
the acl_enabled flag. This also enables the matching of the MCC/MNC
prefix of the IMSI.

This patch adds an additional policy 'acl-only' which disables the
MCC/MNC matching and relies on the ACL only.

Sponsored-by: On-Waves ehf
sgsn: Notify an affected MM context if an ACL rule is removed

Currently if an ACL access rule is removed, MS already attached are
not affected until they try to reattach to the SGSN.

This patch extends sgsn_acl_del to re-authenticate an MM context if
it matches the IMSI.

Sponsored-by: On-Waves ehf
sgsn: Cleanup after RA Update Reject / Attach Reject

Currently, the LLME is not cleanup up after sending an RA Update
Reject. This happens after entering a routing area from outside,
since in that case the SGSN sends an RA Update Reject (implicitly
detached) which causes the MS to restart the attach procedure.
The LLME is also not updated if an Attach Request with message errors
(encoding, invalid MI type) is received or if an MM context cannot be
allocated.

This patch changes gsm48_rx_gmm_ra_upd_req and gsm48_rx_gmm_att_req
to unassign the LLME or free the MM context (if available) after a
Reject message has been sent.

Ticket: OW#1324
Sponsored-by: On-Waves ehf