move gsm_bts_num() to gsm_data_shared.[ch]

this way we can drop a copy of this function from osmo-bts.
gbproxy: Parse Detach Request messages

GSM 24.008 also allows a P-TMSI field in Detach request messages.

This patch adds gbprox_parse_gmm_detach_req() to parse Detach Request
messages which sets the ptmsi field if the IE is present.

In addition, when power_off is set to 1 (MO only), the
invalidate_tlli field is set, since Detach Request message is
expected in this case.

The second detach test (see 'RA update') is modified to use
power_off instead of relying on a Detach Accept from the network.
To make this work, the PTMSI of the RA Update Accept is fixed to
match the TLLI of the Detach Request.

Sponsored-by: On-Waves ehf
gbproxy: Update enable_patching flag on existing tlli_info

Currently the enable_patching field in tlli_info is not updated,
when an IMSI is assigned to a TLLI that is already known.

This patch fixes this in gbprox_update_state() after the call to
gbprox_update_tlli_info().

The number of APN increases and the test output file is updated
accordingly.

Sponsored-by: On-Waves ehf
gbproxy: Handle old and new P-TMSI/TLLI

Don't replace the current TLLI immediately, store it in an additional
'assigned_tlli' field and discard the old TLLI when both sides have
used the new one (see GSM 04.08, 4.7.1.5).

Add an Attach Complete message to test and check, whether the related
field of the corresponding tlli_info struct are set as expected
during the local TLLI validation cycle.

Sponsored-by: On-Waves ehf
gbproxy/test: Fix BSSGP/LLC test messages

The following parts of the messages have been fixed
  - Attach Accept: checksum
  - Attach Complete: checksum
  - RA Update Accept: Use the same MS Radio Access Capabilities and
    DRX Parameters like the other messages

The N(U) of most messages have not been fixed.

Sponsored-by: On-Waves ehf
gbproxy/test: Generate BSSGP and LLC layer for DTAP messages

This patch adds the functions send_bssgp_ul_unitdata(),
send_bssgp_dl_unitdata(), send_llc_ul_ui(), and send_llc_dl_ui().
They are used instead of send_ns_unitdata() in
test_gbproxy_ra_patching(). This make it easier to modify TLLI, N(U),
and other parameters.

Sponsored-by: On-Waves ehf
gbproxy: Track SGSN and BSS TLLI/PTMSI separately

This patch separates BSS side from SGSN side TLLI/PTMSI tracking. When
TLLI/PTMSI patching is not enabled, the corresponding states shall be
identical. The TLLI/PTMSI state has been moved into the struct
gbproxy_tlli_state and is used twice in gbproxy_tlli_info.

Since the state handling for uplink and downlink messages is
diverging, gbprox_update_state() is replaced by two functions
gbprox_update_state_dl/gbprox_update_state_ul and
gbprox_process_bssgp_message() is replaced by
gbprox_process_bssgp_dl/gbprox_process_bssgp_ul.

Sponsored-by: On-Waves ehf
gbproxy: Add 'patch-ptmsi' command to enable TLLI/P-TMSI patching

This VTY command add the following commands to the gbproxy node:
  - patch-ptmsi: Enables P-TMSI/TLLI patching
  - no patch-ptmsi: Disables P-TMSI/TLLI patching

Note that using these commands interactively can load to undefined
behavior of existing LLC connections.

Sponsored-by: On-Waves ehf
gbproxy: Refactor gbprox_get_detached_tlli_info

This patch splits the functionality of gbprox_get_detached_tlli_info
into 2 new functions:
  - gbprox_tlli_info_alloc to allocate an intialized and detached
    tlli_info
  - gbprox_detach_tlli_info to detach an already attached tlli_info

Sponsored-by: On-Waves ehf
gbproxy: Refactor gbprox_patch_raid(), use different RAI types properly

Currently gbprox_patch_raid() updates the local MCC/MNC with every
BSS originated message, even if the RAI is an 'old' one.

This patch separates state updating and patching into 2 functions
gbprox_update_current_raid and gbprox_patch_raid. In addition, a
field named old_raid_enc is added to gbproxy_parse_context, which is
used for 'old RAI' IEs in Attach Requests and RA Update Requests.
Only the bssg_raid_enc in BSS originated message is used to update
the BSS side 'local' MCC/MNC.

Sponsored-by: On-Waves ehf
gbproxy: Also handle LLC non UI and LL11 messages

Currently, these messages lead to a parsing error which prevents them
from being processed any further.

This patch sets the return value of gbprox_parse_llc to 1 in these
cases and fixes a segfault which is triggered by any non-04.08
message.

Sponsored-by: On-Waves ehf
gbproxy: Fix TLLI state handling

This patch contains fixes for the TLLI tracking and handling.
It adds and uses gbprox_map_tlli() the map the source TLLI to the
destination TLLI while respecting whether it is current or assigned.
It removes gbprox_register_tlli() from the downlink path. It fixes
TLLI validation and disables the use of the BSSGP TLLI IE.

Sponsored-by: On-Waves ehf
gbproxy: Patch TLLI/P-TMSI

This patch adds code to modify TLLIs and P-TMSIs. Related counters
are also added.

Sponsored-by: On-Waves ehf
gbproxy: Add context info to log messages

This mainly adds the NSEI to the messages, similar to log messages
ogf the existing gbproxy code.

Sponsored-by: On-Waves ehf
gbproxy: Use different TLLI/P-TMSI for BSS and SGSN

This patch modifies gbprox_make_bss_ptmsi() to generate a new P-TMSI
when patch_ptmsi is set in the configuration instead of using the
P-TMSI assigned by the SGSN. It modifies gbprox_make_sgsn_tlli() to
either use a foreign TLLI based on the SGSN side P-TMSI or (if there
is none) generate a random TLLI if patch_ptmsi is set. Otherwise, the
TLLI used by the BSS is used.

The seeds for the pseudo-random sequences sre set based on time
initially. Note that these are neither cryptographically safe nor
protected against collisions.

Ticket: OW#1259
Sponsored-by: On-Waves ehf
gbproxy/test: Add test for TLLI patching

Add LLC test messages containing XID (SAPI LLGMM, U frame) and IP traffic
(SAPI LL11, UI frame).

Add a test case containing a complete SGSN session with TLLI/PTMSI
patching enabled.

Sponsored-by: On-Waves ehf
gbproxy: Refactor gb_proxy.c into several files

This patch moves several functions and declarations out of gb_proxy.c
to make them reusable by other components and to separate them by
context and task.

Counter enums (prefix is changed to gbproxy_):
  enum gbprox_global_ctr -> gprs/gb_proxy.h
  enum gbprox_peer_ctr -> gprs/gb_proxy.h

Generic Gb parsing (prefix is changed to gprs_gb_):
  struct gbproxy_parse_context -> openbsc/gprs_gb_parse.h
  gbprox_parse_dtap() -> gprs/gprs_gb_parse.c
  gbprox_parse_llc() -> gprs/gprs_gb_parse.c
  gbprox_parse_bssgp() -> gprs/gprs_gb_parse.c
  gbprox_log_parse_context() -> gprs/gprs_gb_parse.c
  *_shift(), *_match() -> gprs/gprs_gb_parse.c (no prefix)
  gbprox_parse_gmm_* -> gprs/gprs_gb_parse.c (static)
  gbprox_parse_gsm_* -> gprs/gprs_gb_parse.c (static)

MI testing/parsing (prefix gprs_ added):
  is_mi_tmsi() -> gprs/gprs_utils.c
  is_mi_imsi() -> gprs/gprs_utils.c
  parse_mi_tmsi() -> gprs/gprs_utils.c

TLLI state handling (prefix is changed to gbproxy_):
  gbprox_*tlli* -> gprs/gb_proxy_tlli.c
  (except gbprox_patch_tlli, gbproxy_make_sgsn_tlli)

Message patching (prefix is changed to gbproxy_):
  gbprox_*patch* -> gprs/gb_proxy_patch.c
  gbprox_check_imsi -> gprs/gb_proxy_patch.c

Sponsored-by: On-Waves ehf
gbproxy: Move peer definitions to gb_proxy_peer.c

This patch moves the peer related definitions from gb_proxy.c to
gb_proxy_peer.c and adjusts the prefix of each global symbol to
gbproxy_:

Peer definitions (prefix adjusted to gbproxy_):
  peer_ctr_description -> gprs/gb_proxy_peer.c (static)
  peer_ctrg_desc -> gprs/gb_proxy_peer.c (static)
  *peer_by_* -> gprs/gb_proxy_peer.c
  gbproxy_peer_alloc -> gprs/gb_proxy_peer.c
  gbproxy_peer_free -> gprs/gb_proxy_peer.c
  gbprox_cleanup_peers -> gprs/gb_proxy_peer.c

Sponsored-by: On-Waves ehf
Merge Jacob's GPRS GB Proxy related configs

All review feedback will be addressed _after_ the split of the
files. This is the only reasonable approach to get the split of
files merged. I didn't have the time to review all of the code
before the point of splitting.
gsm_data_shared: Add new trx_power_params for osmo-bts