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
sgsn: Don't send XID reset after Detach Accept

Currently when a Detach Accept is received for an unknown TLLI (which
is in general the case afer the SGSN has requested the detachment),
an XID reset is sent to the BSS, causing a BSSGP Status message. This
happens in gsm0408_rcv_gmm.

This patch moves the corresponding call to gprs_llgmm_reset downwards
so that it is not being called in that case.

Addresses:
SGSN->BSS TLLI: 0xd75b91d9 SAPI: LLGMM, UI (DTAP) (GMM) Detach Request
BSS->SGSN TLLI: 0xd75b91d9 SAPI: LLGMM, UI (DTAP) (GMM) Detach Accept
SGSN->BSS TLLI: 0xd75b91d9 SAPI: LLGMM, U, XID (Reset, IOV-UI)
BSS->SGSN TLLI: ---        BSSGP STATUS (Unknown MS)

Sponsored-by: On-Waves ehf
sgsn: Don't assign a new P-TMSI if one is pending

Currently every time an RA Update Req or an Attach Req is processed, a
new P-TMSI is allocated. When an MS issues another of these messages
before it has completed the first procedure, old_ptmsi is replaced by
ptmsi (and thus lost) and ptmsi is replaced by the newly allocated
P-TMSI. This can confuse the gbproxy, which can loose track of the
logical link then. At least a Blackberry emits a double set of RA Upd
Req messages from time to time which may be just 20ms apart.

This patch adds a check whether mm->ptmsi or mm->old_ptmsi are set.
If both are set, the P-TMSI is not re-allocated. This is only the
case, when the Complete message has not been received yet, since that
message will reset old_ptmsi.

Sponsored-by: On-Waves ehf
sgsn: Refactor sgsn_auth to separate request and authorization

Currently the authorization is done in sgsn_auth_request for ACL
based authorization. This doesn't match the way remote authorization
would work, so that there is a second call to sgsn_auth_state already
present in sgsn_auth_update.

This patch removes the autorization check completely from
sgsn_auth_request which in turn calls sgsn_auth_update directly now.

Sponsored-by: On-Waves ehf
sgsn: Remove explicit sgsn_instance parameters

Currently the function in sgsn_auth.c either have an sgsn_config or
an sgsn_instance parameter. Since then global sgsn variable is
already being used in that file and since other parts of the SGSN
related code also rely on a global sgsn singleton, these parameters
pretend to provide a flexibility that is not really supported.

Therefore this patch removes these parameters except for the ACL
related functions, which do not call code that uses the sgsn
variable.

Sponsored-by: On-Waves ehf
sgsn: Extend gsm_subscriber and sgsn_mm_ctx by references

This adds a field mm to struct gsm_subscriber that will be used by
the SGSN to store a reference to the current MM context (or NULL if
there is none). This also adds a field subscr to struct sgsn_mm_ctx
that reversely points to a gsm_subscriber (or NULL if there is none).
Either both fields are NULL or both fields are non-NULL. Note that
subscr is being reference counted.

Sponsored-by: On-Waves ehf
sgsn: Add gprs_subscriber.c (TODO)

This adds GPRS specific functions for gsm_subscriber objects. The
sgsn_update_subscriber_data callback is extended by a parameter that passes a reference to a gsm_subscriber.

TODO:
 - tests

Sponsored-by: On-Waves ehf
sgsn: Integrate subscriber handling into the SGSN (TODO)

This commit adds a new authorization policy 'remote' and uses
the subscriber cache for authorization when this policy is being used.

Note that there is no remote backend implemented yet.

TODO:
 - tests
 - decide, whether 'remote' could be named better, e.g. sth based on
   'subscriber'

Ticket: OW#????
Sponsored-by: On-Waves ehf
sgsn: Add gsm0408_gprs_access_cancelled (TODO)

This function is called to delete an established MM context
without invoking a detach procedure that could disable the GPRS
functionality of the MS until the next power cycle.

It is called when a subscriber is cancelled.

Sponsored-by: On-Waves ehf
sgsn: Add VTY commands to manage subscriber cache

This adds the following commands to the ENABLE node:
  - show subscriber cache
  - update-subscriber imsi IMSI insert authorized (0|1)
  - update-subscriber imsi IMSI cancel

These commands are mainly testing tools and maintenance helpers. The
update commands work asynchronously and can be used to complete a
pending update request or to terminate an existing connection.

Note that the subscriber cache is not stored persistently and will
always be empty after an SGSN restart.

Sponsored-by: On-Waves ehf
sgsn: Do not die in _bssgp_tx_dl_ud on TLLI mismatch (TODO)

Currently an OSMO_ASSERT fails, if the message's and the mmctx's
TLLIs do not match.

This commit turns the assertion into an ERROR log message and uses
the default values for IMSI, DRX, and RA CAP instead of the MM
context values in this case.

TODO:
  - find the real source for the problem
  - add test cases derived from what is being described in the ticket

Ticket: OW#1322
Sponsored-by: On-Waves ehf
sgsn: Notify an affected MM context if an ACL rule is removed (TODO)

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

This patch adds sgsn_auth_update_all() that iterates through allMM
contexts and calls sgsn_auth_update() on each of them. This function
is then called when 'imsi-acl del IMSI' is executed after the ACL has
been removed.

TODO:
 - decide, whether ito replace that by a dedicated update command in
   the VTY ENABLE node

Sponsored-by: On-Waves ehf
sgsn: Added TODOs (TODO)

TODO:
  - fix and remove TODOs
  - add test cases for Detach(reattach)
  - add test cases for PDP context deletion (OW#1311)
  - add test cases for Cancel pending timer in sgsn_mm_ctx_free
  - remove this commit

Sponsored-by: On-Waves ehf