Commit Graph

112 Commits

Author SHA1 Message Date
Jacob Erlbeck 5f4ef321a6 gbproxy: Implement IMSI acquisition
To modify or route messages based on the IMSI the latter must be known
when the action shall take place.

This patch modifies the gbproxy to optionally retain and enqueue
messages from the MS while initiating an identification procedure.
Further message processing of the LLC PTP link towards the SGSN will
be done, when the identity of the MS has been acquired.

Note that the N(U) of the LLC GMM SAPI are not adjusted, so it is
possible that adjacent messages of a single LLC link arriving either
at the BSS or the SGSN have the same N(U) and might get discarded,
leading to retransmissions and additional delay.

Note also that retransmissions and packet loss are not yet handled
explicitely. If for instance the generated IDENT REQ gets lost, the
gbproxy will not act on its own. In this case, the MS will time out
and eventually resend the Attach Request on which the gbproxy will
act exactly like before (thus having two Attach Req messages in its
queue, which will both be sent after the Ident Resp arrives).

This has been tested successfully with an E71, needing one
retransmission by the SGSN due to an N(U) collision.

Ticket: OW#1261
Sponsored-by: On-Waves ehf
2014-09-02 09:53:47 +02:00
Jacob Erlbeck 4b663ac34a gbproxy: Create STATUS message with original PDU
Currently when patching is enabled and an error happens when
receiving a message from the SGSN, the patched message is sent back
with the PDU_IN_ERROR IE.

This patch modifies gbprox_rx_sig_from_sgsn() to copy the message
before it is patched, so that the original message can be used with
the STATUS message. gbprox_rx_ptp_from_sgsn() does all checks before
the message is patched, so copying is not necessary.

Since gbprox_rx_sig_from_sgsn() is not called for BSSGP UNITDATA
messages and the msgb is already been copied in the gbprox_relay2peer
function, the relative performance impact is expected to be low.

Note that the PDU IE of STATUS messages received from an MS and
forwarded to the SGSN will not be patched. STATUS messages from the
SGSN are only logged and not forwarded to the MS.

Sponsored-by: On-Waves ehf
2014-09-02 09:53:38 +02:00
Jacob Erlbeck 46f1d6fddb gbproxy: Move PTP message handling into separate functions
This patch adds gbprox_rx_data_from_sgsn() and
gbprox_rx_ptp_from_bss() which contain the PTP message processing
of gbprox_rcvmsg(). The calls to gbprox_process_bssgp_ul() are moved
from gbprox_relay2sgsn() to gbprox_rx_ptp_from_bss() and
gbprox_rx_sig_from_bss().

The goal is, to do all patching (and calls to gbprox_process_bssgp_*)
from within the gbprox_rx_* functions. Doing the patching from within
gbprox_relay2sgsn has the drawback, that the patching code cannot
call gbprox_relay2sgsn() which is needed if a single message shall
trigger a sequence of messages.

Sponsored-by: On-Waves ehf
2014-09-02 09:53:30 +02:00
Jacob Erlbeck 5f1faa3cd2 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
2014-08-24 16:16:40 +02:00
Jacob Erlbeck 9114bee242 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
2014-08-24 16:16:40 +02:00
Jacob Erlbeck 0d37671824 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
2014-08-24 16:16:40 +02:00
Jacob Erlbeck 643d5228ed 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
2014-08-24 16:16:40 +02:00
Jacob Erlbeck 03ca10e863 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
2014-08-24 16:16:39 +02:00
Jacob Erlbeck e37487e083 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
2014-08-24 16:16:39 +02:00
Jacob Erlbeck 3b23d639ab 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
2014-08-24 16:16:39 +02:00
Jacob Erlbeck 12496dd7db 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
2014-08-24 16:16:39 +02:00
Jacob Erlbeck 311592fc39 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
2014-08-24 16:16:39 +02:00
Jacob Erlbeck 9057bc3c72 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
2014-08-24 16:16:39 +02:00
Jacob Erlbeck 59748e653b 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
2014-08-24 16:16:39 +02:00
Jacob Erlbeck 3c5b40fb75 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
2014-08-24 16:16:39 +02:00
Jacob Erlbeck 3e23ddf88b 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
2014-08-24 16:16:39 +02:00
Jacob Erlbeck 29805da6d1 gbproxy: Add gbprox_clear_patch_filter() (Coverity)
Add a separate function to clear the IMSI filter to be used instead of
gbprox_set_patch_filter(cfg, NULL, ...). Albeit it fixes a Coverity
issue (Unchecked return value), it is a false positive, since the
return value is always 0 in these cases. Nevertheless it is more
obvious what happens when an explicit clear function is called. Using
NULL as filter argument of gbprox_set_patch_filter still clears the
filter.

Fixes: Coverity CID 1231255
Sponsored-by: On-Waves ehf
2014-08-22 00:21:56 +02:00
Jacob Erlbeck 5e68ecf3b2 gbproxy: Pass tlli_info around
This patch modifies the code to pass a pointer to the tlli_info
around once it has been acquired. To achieve this,
gbprox_register_tlli() and gbprox_update_state() are modified to
return it (if it has been found or created), and gbprox_patch_llc(),
gbprox_patch_bssgp(), and gbprox_update_state_after() are modified to
take it as parameter.

Add a new function gbprox_touch_tlli() to update timestamp and list
ordering for existing tlli_infos.

The motivation behind this patch is to make the tlli_info available to
the patching code and to avoid repeated searches for the same TLLI.

Sponsored-by: On-Waves ehf
2014-08-13 11:14:08 +02:00
Jacob Erlbeck 2dec9851bd gbproxy: Optimize gbprox_remove_stale_tllis
The current implementation of this function is O(N), where N is the
number of entries.

The new implementation is O(D), where D is the number of entries that
are going to be deleted.

Sponsored-by: On-Waves ehf
2014-08-13 10:42:27 +02:00
Jacob Erlbeck 7b821d031a gbproxy: Don't call time() in TLLI related functions
Currently time() is called at several places to control TLLI aging.
Beside calling time() more often than necessary, the decision which
timesource is to be used is coded into the TLLI handling, and testing
complex aging scenarios is cumbersome.

This patch passes the current time as a parameter instead. The call
to time() is moved to gbprox_process_bssgp_message().

Sponsored-by: On-Waves ehf
2014-08-13 10:42:27 +02:00
Jacob Erlbeck aad32bccc0 gbproxy: Don't remove stale TLLI automatically
This patches removes the call to gbprox_remove_stale_tllis()
from gbprox_register_tlli(), so it must be called explicitly now.
The call is now done from within gbprox_update_state_after().

In addition, the TLLI cache size counter is also kept in sync when
gbprox_remove_stale_tllis is called manually. The call to
gbproxy_peer_free() in gbproxy_peer_free() is moved behind the TLLI
to allow for counter updates in gbprox_delete_tlli().

Sponsored-by: On-Waves ehf
2014-08-13 10:42:26 +02:00
Jacob Erlbeck 12828c1b30 gbproxy: Move parse_ctx logging into separate function
Sponsored-by: On-Waves ehf
2014-08-13 10:42:26 +02:00
Jacob Erlbeck c812882dbf gbproxy: Refactor gbproxy_patch_bssgp_message
This patch refactors that function by separating the actual patch
code into a new function gbproxy_patch_bssgp(), similar to
gbproxy_patch_llc(). The remaining function is renamed to
gbproxy_process_bssgp_message. The existing function
gbproxy_parse_bssgp_message() is renamed to
gbproxy_process_bssgp_message to match gbproxy_parse_llc.

Sponsored-by: On-Waves ehf
2014-08-13 10:42:26 +02:00
Jacob Erlbeck 291f0508c5 gbproxy: Test and fix IMSI/TMSI matching
This adds a test for gbprox_set_patch_filter() and
gbprox_check_imsi().

It also fixes the masking of the type field when IMSIs are checked by
using GSM_MI_TYPE_MASK (0x07) instead of 0x0f.

Sponsored-by: On-Waves ehf
2014-08-13 10:41:23 +02:00
Jacob Erlbeck 89d3d343d1 gbproxy: Track TLLI even when the IMSI is not known
Currently only TLLIs for which it is known that they may be patched
are put into the TLLI list.

This patch changes this to add TLLIs even when the IMSI is not yet
known. A enable_patching flag is added to the gbproxy_tlli_info
structure to control patching.

Note that this puts every active TLLI into the list where accesses
are O(N) currently.

Sponsored-by: On-Waves ehf
2014-08-13 10:29:09 +02:00
Jacob Erlbeck 58da91d5d0 gbproxy: Make pointers to MI const in parse/check functions
The encoded mobile identity will never be modified in
is_mi_tmsi/is_mi_imsi/parse_mi_tmsi, thus the pointer is made const.

Sponsored-by: On-Waves ehf
2014-08-13 09:57:26 +02:00
Jacob Erlbeck fb22ac52b0 gbproxy: Explicitly convert PTMSI to TLLI
This patch modifies the new_ptmsi handling by setting its two most
significant bits before using it as TLLI.

Sponsored-by: On-Waves ehf
2014-08-13 09:57:25 +02:00
Jacob Erlbeck 690768a171 gbproxy: Parse additional IMSI/PTMSI/TLLI fields
This adds parsing support for the following messages:
  - Attach Request: IMSI/PTMSI
  - Identity Response: IMSI/PTMSI
  - BSSGP: Optional TLLI IE
  - BSSGP/PAGING_PS: PTMSI

A new new_ptmsi_enc field is added for newly assigned PTMSI in
SGSN->BSS messages (instead of ptmsi_enc). The ptmsi_enc field is now
used for informational PTMSI IE in messages.

Sponsored-by: On-Waves ehf
2014-08-13 09:55:27 +02:00
Jacob Erlbeck 2db2512f4d gbproxy: Separate BSSGP parsing from patching
This adds a gbprox_parse_bssgp_message() function that contains the
parsing part of the former gbprox_patch_bssgp_message(). This
includes a call to gbprox_parse_llc().

The calls to gbprox_patch_llc(), gbprox_update_state() and
gbprox_update_state_after() have therefore been moved to
gbprox_patch_bssgp_message().

Sponsored-by: On-Waves ehf
2014-08-12 19:32:26 +02:00
Jacob Erlbeck 2bdd253d54 gbproxy: Unify TLLI tracking
This patch unifies the TLLI tracking for all LLC messages. The TLLI
state handling is moved into separate functions.

Only Detach Accept messages are taken into account to release a TLLI,
which is safe but not optimal.

Sponsored-by: On-Waves ehf
2014-08-12 19:30:48 +02:00
Jacob Erlbeck 0cee7ad2bf gbproxy: Move patching code out of the 04.08 specific functions
Currently, parsing and optionally patching is done in the same
functions (e.g. gbprox_patch_gmm_attach_req()).

This patch moves the patching code out of these functions into
gbprox_patch_llc() and just stores pointers to the relevant data
areas into parse_ctx. Consequently the len_change parameter is
removed and the _patch_ in the function's names is renamed to
_parse_. In addition, the patching_is_enabled checks and counter
increments are moved out of these functions, too.

Sponsored-by: On-Waves ehf
2014-08-12 19:29:12 +02:00
Jacob Erlbeck 477c69a9c5 gbproxy: Track all TLLIs (not only LOCAL)
This patch removes all checks for the TLLI type.

Sponsored-by: On-Waves ehf
2014-08-08 08:50:14 +02:00
Jacob Erlbeck 58cf664d28 gprs: Refactor gbprox_register_tlli()
Currently gbprox_register_tlli() is a rather complex function.

This patch splits it into several smaller functions to ease reviewing
and maintaining it.

Sponsored-by: On-Waves ehf
2014-08-05 15:11:11 +02:00
Holger Hans Peter Freyther 0196c9936c gbproxy/test: Test TLLI expiry
This adds a unit test for gbprox_register_tlli() and
gbprox_remove_stale_tllis().

The dump_peers() function is extended by a cfg parameter to support
a non-global gbproxy_config.

Done with Jacob
2014-08-05 15:02:06 +02:00
Jacob Erlbeck d8a7e22709 gbproxy: Remove broken TLLI comparison
This comparison bit-ored the TLLI with 0xc000 instead of 0xc000000.

Since this has never worked properly yet and since normalizing to
local TLLIs doesn't seem sensible here, the comparison is removed
entirely.

Sponsored-by: On-Waves ehf
2014-08-04 17:13:17 +02:00
Holger Hans Peter Freyther 3fa26448d1 gbproxy: Kill the global gbprox_global_patch_state struct
Move this patching state into the gbproxy_config as well.

Done by Jacob
2014-08-04 16:27:11 +02:00
Holger Hans Peter Freyther 3748ada073 gbproxy: Add a hint to the compiler that all parameters are not NULL
In these functions we assume that peer is not NULL. Add a compiler
attribute in the hope that either coverity or GCC/Clang will help
us to find a misusage.

Done with Jacob
2014-08-04 16:06:20 +02:00
Holger Hans Peter Freyther eece627799 gbproxy: Remove global state from the gbproxy
Global state prevents us from writing simple units tests for
single routines. Go through the code and add pointers to the
gbproxy configuration. Only the vty and the test code remain
using the global gbproxy instance.
2014-08-04 16:01:12 +02:00
Holger Hans Peter Freyther 4d9fc422d2 gbproxy: Use gbprox_delete_tlli if possible
Make use of the delete routine in more places and get test coverage
for it.

Done with Jacob
2014-08-04 12:18:07 +02:00
Holger Hans Peter Freyther 7127b0295e gprs: Create a gprs_utils file and move to be shared code in there
We intend to move some of these routines to libosmocore but to avoid
a feature symbol clash we are prefixing these routines with gprs_.

Done with Jacob
2014-08-04 11:52:52 +02:00
Holger Hans Peter Freyther 1ddd9e518e gbproxy: Use gbproxy_ for all structures
The application is called gbproxy but the structures and functions
were inconsistently named as either gbprox or gbproxy. Rename all
structures to use gbproxy.

Done with Jacob
2014-08-04 11:37:50 +02:00
Holger Hans Peter Freyther b900459e96 gbproxy: The dump routines are only used by the test, move it there
Done with Jacob
2014-08-04 11:37:47 +02:00
Holger Hans Peter Freyther 16f30b5373 gbproxy: Have a clear namespace for the public functions
Done with Jacob
2014-08-04 11:37:45 +02:00
Holger Hans Peter Freyther a7027a04dd gbproxy: Remove the global rate counter and place it in the config
Move the global data into the struct and use it. gbprox_reset will
first free data and then re-initialize the structure. This code is
used by the unit test.

Done with Jacob
2014-08-04 11:37:40 +02:00
Holger Hans Peter Freyther d4d36f22ee gbproxy: Correct the method name. We work on TLLIs
Done with Jacob
2014-08-04 11:37:33 +02:00
Holger Hans Peter Freyther 18739ea32d gbproxy: Move the VTY code into the vty file and create public API
Create public accessors to the core of the peer to allow to
simplify the test and separate concerns.

Done with Jacob.
2014-08-04 11:37:28 +02:00
Jacob Erlbeck 03551e5f3c gprs: Move LLC IE length fix to BSSGP level
This commit moves the fixing code of the length field of the LLC
information element to the BSSGP patching level since that is not a
part of LLC itself.

Sponsored-by: On-Waves ehf
2014-07-22 17:13:32 +02:00
Jacob Erlbeck 2e2650f8f3 gprs: Parse PTMSI and update TLLI accordingly
This commit adds code to parse the PTMSI in network originated
messages

  - Attach Accept,
  - Routing Area Update Accept, and
  - P-TMSI Reallocation Command (see below)

to keep track of the TLLI identifying the LLC connection.

The P_TMSI Realloc Command specific code is not being tested yet, so
a corresponding notice is logged when such a message will be
received.

NOTE:
  The gbproxy will lose the TLLI when the MS doesn't receive/use
  the message (normally the SGSN remembers the old TLLI for some time
  to avoid this kind of problem). If this happens the MS will
  probably restart the procedure and the network will have to answer
  again eventually using one of the above messages which will
  re-associate the IMSI with the TLLI before the MS can send a
  PDP Context Request message.

Ticket: OW#1192
Sponsored-by: On-Waves ehf
2014-07-22 17:11:57 +02:00
Jacob Erlbeck 35cc03f97d gprs: Use struct to pass context information
Add a struct containing context information from the parts of the
message that have been parsed already. A pointer to this (temporary)
struct is passed to parse/patch functions.

Sponsored-by: On-Waves ehf
2014-07-22 17:10:51 +02:00
Jacob Erlbeck aefaf92d1a gprs: Use shift functions instead of manual parsing
Currently the patching code directly accesses the single bytes to
parse the LLC/DTAP messages.

This patch uses the shift functions instead to parse tlv and similar
structures.

Sponsored-by: On-Waves ehf
2014-07-22 17:08:28 +02:00