blob: 8b5a67269bd4ed00b46ea7fb6630542674f44ca6 [file] [log] [blame]
Harald Welte9f75c352010-04-30 20:26:32 +02001/* NS-over-IP proxy */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther90267a92013-10-23 11:24:17 +02004 * (C) 2010-2013 by On-Waves
5 * (C) 2013 by Holger Hans Peter Freyther
Harald Welte9f75c352010-04-30 20:26:32 +02006 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9f75c352010-04-30 20:26:32 +020011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Welte9f75c352010-04-30 20:26:32 +020017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9f75c352010-04-30 20:26:32 +020020 *
21 */
22
23#include <unistd.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <getopt.h>
28#include <errno.h>
29#include <sys/fcntl.h>
30#include <sys/stat.h>
Harald Welte7fc98222010-05-11 10:15:42 +020031#include <arpa/inet.h>
Jacob Erlbeck7c101d92014-06-06 18:49:23 +020032#include <time.h>
Harald Welte9f75c352010-04-30 20:26:32 +020033
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010034#include <osmocom/core/talloc.h>
35#include <osmocom/core/select.h>
Jacob Erlbeckbc555742013-10-18 14:34:55 +020036#include <osmocom/core/rate_ctr.h>
37
Harald Welteea34a4e2012-06-16 14:59:56 +080038#include <osmocom/gprs/gprs_ns.h>
39#include <osmocom/gprs/gprs_bssgp.h>
40
Jacob Erlbeck7c101d92014-06-06 18:49:23 +020041#include <osmocom/gsm/gsm_utils.h>
42
Harald Welte9f75c352010-04-30 20:26:32 +020043#include <openbsc/signal.h>
44#include <openbsc/debug.h>
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020045#include <openbsc/gprs_gb_parse.h>
Harald Welte672f5c42010-05-03 18:54:58 +020046#include <openbsc/gb_proxy.h>
Harald Welte9f75c352010-04-30 20:26:32 +020047
Jacob Erlbeck67a44452014-05-19 10:14:58 +020048#include <openbsc/gprs_llc.h>
49#include <openbsc/gsm_04_08.h>
50#include <openbsc/gsm_04_08_gprs.h>
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +020051#include <openbsc/gprs_utils.h>
Jacob Erlbeck67a44452014-05-19 10:14:58 +020052
Jacob Erlbeckbc555742013-10-18 14:34:55 +020053static const struct rate_ctr_desc global_ctr_description[] = {
54 { "inv-bvci", "Invalid BVC Identifier " },
Jacob Erlbeck8f503592014-06-02 10:49:00 +020055 { "inv-lai", "Invalid Location Area Identifier" },
56 { "inv-rai", "Invalid Routing Area Identifier " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020057 { "inv-nsei", "No BVC established for NSEI " },
58 { "proto-err.bss", "BSSGP protocol error (BSS )" },
59 { "proto-err.sgsn", "BSSGP protocol error (SGSN)" },
60 { "not-supp.bss", "Feature not supported (BSS )" },
61 { "not-supp.sgsn", "Feature not supported (SGSN)" },
62 { "restart.sgsn", "Restarted RESET procedure (SGSN)" },
63 { "tx-err.sgsn", "NS Transmission error (SGSN)" },
64 { "error", "Other error " },
Jacob Erlbeck006c0382014-05-27 13:49:04 +020065 { "mod-peer-err", "Patch error: no peer " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020066};
67
68static const struct rate_ctr_group_desc global_ctrg_desc = {
69 .group_name_prefix = "gbproxy.global",
70 .group_description = "GBProxy Global Statistics",
71 .num_ctr = ARRAY_SIZE(global_ctr_description),
72 .ctr_desc = global_ctr_description,
73};
74
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020075static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
76 uint16_t ns_bvci);
77static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +020078 uint16_t ns_bvci, uint16_t sgsn_nsei);
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020079
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +020080static int check_peer_nsei(struct gbproxy_peer *peer, uint16_t nsei)
Jacob Erlbeckbc555742013-10-18 14:34:55 +020081{
82 if (peer->nsei != nsei) {
83 LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI "
84 "BVCI=%u via NSEI=%u (expected NSEI=%u)\n",
85 peer->bvci, nsei, peer->nsei);
86 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]);
Jacob Erlbeck64cb9242013-10-24 12:48:56 +020087 return 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +020088 }
89
Jacob Erlbeck64cb9242013-10-24 12:48:56 +020090 return 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +020091}
92
Harald Welte69619e32010-05-03 19:05:10 +020093/* strip off the NS header */
94static void strip_ns_hdr(struct msgb *msg)
95{
96 int strip_len = msgb_bssgph(msg) - msg->data;
97 msgb_pull(msg, strip_len);
98}
99
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200100/* Transmit Chapter 9.2.10 Identity Request */
101static void gprs_put_identity_req(struct msgb *msg, uint8_t id_type)
102{
103 struct gsm48_hdr *gh;
104
105 id_type &= GSM_MI_TYPE_MASK;
106
107 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
108 gh->proto_discr = GSM48_PDISC_MM_GPRS;
109 gh->msg_type = GSM48_MT_GMM_ID_REQ;
110 gh->data[0] = id_type;
111}
112
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200113/* Transmit Chapter 9.4.6.2 Detach Accept (mobile originated detach) */
114static void gprs_put_mo_detach_acc(struct msgb *msg)
115{
116 struct gsm48_hdr *gh;
117
118 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
119 gh->proto_discr = GSM48_PDISC_MM_GPRS;
120 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
121 gh->data[0] = 0; /* no force to standby */
122}
123
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200124static void gprs_push_llc_ui(struct msgb *msg,
125 int is_uplink, unsigned sapi, unsigned nu)
126{
127 const uint8_t e_bit = 0;
128 const uint8_t pm_bit = 1;
129 const uint8_t cr_bit = is_uplink ? 0 : 1;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200130 uint8_t *llc;
131 uint8_t *fcs_field;
132 uint32_t fcs;
133
134 nu &= 0x01ff; /* 9 Bit */
135
136 llc = msgb_push(msg, 3);
137 llc[0] = (cr_bit << 6) | (sapi & 0x0f);
138 llc[1] = 0xc0 | (nu >> 6); /* UI frame */
139 llc[2] = (nu << 2) | ((e_bit & 1) << 1) | (pm_bit & 1);
140
141 fcs = gprs_llc_fcs(llc, msgb_length(msg));
142 fcs_field = msgb_put(msg, 3);
143 fcs_field[0] = (uint8_t)(fcs >> 0);
144 fcs_field[1] = (uint8_t)(fcs >> 8);
145 fcs_field[2] = (uint8_t)(fcs >> 16);
146}
147
148static void gprs_push_bssgp_dl_unitdata(struct msgb *msg,
149 uint32_t tlli)
150{
151 struct bssgp_ud_hdr *budh;
152 uint8_t *llc = msgb_data(msg);
153 size_t llc_size = msgb_length(msg);
154 const size_t llc_ie_hdr_size = 3;
155 const uint8_t qos_profile[] = {0x00, 0x50, 0x20}; /* hard-coded */
156 const uint8_t lifetime[] = {0x02, 0x58}; /* 6s hard-coded */
157
158 const size_t bssgp_overhead = sizeof(*budh) +
159 TVLV_GROSS_LEN(sizeof(lifetime)) + llc_ie_hdr_size;
160 uint8_t *ie;
161 uint32_t tlli_be = htonl(tlli);
162
163 budh = (struct bssgp_ud_hdr *)msgb_push(msg, bssgp_overhead);
164
165 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
166 memcpy(&budh->tlli, &tlli_be, sizeof(budh->tlli));
167 memcpy(&budh->qos_profile, qos_profile, sizeof(budh->qos_profile));
168
169 ie = budh->data;
170 tvlv_put(ie, BSSGP_IE_PDU_LIFETIME, sizeof(lifetime), lifetime);
171 ie += TVLV_GROSS_LEN(sizeof(lifetime));
172
173 /* Note: Add alignment before the LLC IE if inserting other IE */
174
175 *(ie++) = BSSGP_IE_LLC_PDU;
176 *(ie++) = llc_size / 256;
177 *(ie++) = llc_size % 256;
178
179 OSMO_ASSERT(ie == llc);
180
181 msgb_bssgph(msg) = (uint8_t *)budh;
182 msgb_tlli(msg) = tlli;
183}
184
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200185/* update peer according to the BSS message */
186static void gbprox_update_current_raid(uint8_t *raid_enc,
187 struct gbproxy_peer *peer,
188 const char *log_text)
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200189{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200190 struct gbproxy_patch_state *state = &peer->patch_state;
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200191 const int old_local_mcc = state->local_mcc;
192 const int old_local_mnc = state->local_mnc;
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200193 struct gprs_ra_id raid;
194
195 if (!raid_enc)
196 return;
197
198 gsm48_parse_ra(&raid, raid_enc);
199
200 /* save source side MCC/MNC */
201 if (!peer->cfg->core_mcc || raid.mcc == peer->cfg->core_mcc) {
202 state->local_mcc = 0;
203 } else {
204 state->local_mcc = raid.mcc;
205 }
206
207 if (!peer->cfg->core_mnc || raid.mnc == peer->cfg->core_mnc) {
208 state->local_mnc = 0;
209 } else {
210 state->local_mnc = raid.mnc;
211 }
212
213 if (old_local_mcc != state->local_mcc ||
214 old_local_mnc != state->local_mnc)
215 LOGP(DGPRS, LOGL_NOTICE,
216 "Patching RAID %sactivated, msg: %s, "
217 "local: %d-%d, core: %d-%d\n",
218 state->local_mcc || state->local_mnc ?
219 "" : "de",
220 log_text,
221 state->local_mcc, state->local_mnc,
222 peer->cfg->core_mcc, peer->cfg->core_mnc);
223}
224
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200225uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer,
226 uint32_t sgsn_ptmsi)
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200227{
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200228 uint32_t bss_ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200229 int max_retries = 23;
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200230 if (!peer->cfg->patch_ptmsi) {
231 bss_ptmsi = sgsn_ptmsi;
232 } else {
233 do {
234 bss_ptmsi = rand_r(&peer->cfg->bss_ptmsi_state);
235 bss_ptmsi = bss_ptmsi | 0xC0000000;
236
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200237 if (gbproxy_find_tlli_by_ptmsi(peer, bss_ptmsi))
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200238 bss_ptmsi = GSM_RESERVED_TMSI;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200239 } while (bss_ptmsi == GSM_RESERVED_TMSI && max_retries--);
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200240 }
241
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200242 if (bss_ptmsi == GSM_RESERVED_TMSI)
243 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a BSS P-TMSI\n");
244
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200245 return bss_ptmsi;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200246}
247
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200248uint32_t gbproxy_make_sgsn_tlli(struct gbproxy_peer *peer,
249 struct gbproxy_tlli_info *tlli_info,
250 uint32_t bss_tlli)
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200251{
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200252 uint32_t sgsn_tlli;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200253 int max_retries = 23;
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200254 if (!peer->cfg->patch_ptmsi) {
255 sgsn_tlli = bss_tlli;
256 } else if (tlli_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI) {
257 sgsn_tlli = gprs_tmsi2tlli(tlli_info->sgsn_tlli.ptmsi,
258 TLLI_FOREIGN);
259 } else {
260 do {
261 /* create random TLLI, 0b01111xxx... */
262 sgsn_tlli = rand_r(&peer->cfg->sgsn_tlli_state);
263 sgsn_tlli = (sgsn_tlli & 0x7fffffff) | 0x78000000;
264
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200265 if (gbproxy_find_tlli_by_any_sgsn_tlli(peer, sgsn_tlli))
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200266 sgsn_tlli = 0;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200267 } while (!sgsn_tlli && max_retries--);
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200268 }
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200269
270 if (!sgsn_tlli)
271 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate an SGSN TLLI\n");
272
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200273 return sgsn_tlli;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200274}
275
Jacob Erlbeck31591142014-09-03 11:59:48 +0200276/* Returns != 0 iff IMSI acquisition was in progress */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200277static int gbproxy_restart_imsi_acquisition(struct gbproxy_tlli_info* tlli_info)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200278{
279 int in_progress = 0;
280 if (!tlli_info)
281 return 0;
282
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200283 if (tlli_info->imsi_acq_pending) {
Jacob Erlbeck31591142014-09-03 11:59:48 +0200284 in_progress = 1;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200285 tlli_info->imsi_acq_retries += 1;
286 }
Jacob Erlbeck31591142014-09-03 11:59:48 +0200287
288 gbproxy_tlli_info_discard_messages(tlli_info);
289 tlli_info->imsi_acq_pending = 0;
290
291 return in_progress;
292}
293
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200294static void gbproxy_reset_imsi_acquisition(struct gbproxy_tlli_info* tlli_info)
295{
296 gbproxy_restart_imsi_acquisition(tlli_info);
297 tlli_info->imsi_acq_retries = 0;
298}
299
300static void gbproxy_flush_stored_messages(struct gbproxy_peer *peer,
301 struct msgb *msg,
302 uint16_t sgsn_nsei,
303 time_t now,
304 struct gbproxy_tlli_info* tlli_info,
305 struct gprs_gb_parse_context *parse_ctx)
306{
307 int rc;
308 struct msgb *stored_msg;
309 /* Got identity response with IMSI, assuming the request had
310 * been generated by the gbproxy */
311
312 LOGP(DLLC, LOGL_DEBUG,
313 "NSEI=%d(BSS) IMSI acquisition succeeded, "
314 "flushing stored messages\n",
315 msgb_nsei(msg));
316
317 /* Patch and flush stored messages towards the SGSN */
318 while ((stored_msg = msgb_dequeue(&tlli_info->stored_msgs))) {
319 struct gprs_gb_parse_context tmp_parse_ctx = {0};
320 tmp_parse_ctx.to_bss = 0;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200321 tmp_parse_ctx.peer_nsei = msgb_nsei(stored_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200322 int len_change = 0;
323
324 gprs_gb_parse_bssgp(msgb_bssgph(stored_msg),
325 msgb_bssgp_len(stored_msg),
326 &tmp_parse_ctx);
327 gbproxy_patch_bssgp(msg, msgb_bssgph(stored_msg),
328 msgb_bssgp_len(stored_msg),
329 peer, tlli_info, &len_change,
330 &tmp_parse_ctx);
331
332 gbproxy_update_tlli_state_after(peer, tlli_info, now,
333 &tmp_parse_ctx);
334
335 rc = gbprox_relay2sgsn(peer->cfg, stored_msg,
336 msgb_bvci(msg), sgsn_nsei);
337
338 if (rc < 0)
339 LOGP(DLLC, LOGL_ERROR,
340 "NSEI=%d(BSS) failed to send stored message "
341 "(%s)\n",
342 msgb_nsei(msg),
343 parse_ctx->llc_msg_name ?
344 parse_ctx->llc_msg_name : "BSSGP");
345 msgb_free(stored_msg);
346 }
347}
348
349static void gbproxy_acquire_imsi(struct gbproxy_peer *peer,
350 struct gbproxy_tlli_info* tlli_info,
351 uint16_t bvci)
352{
353 struct msgb *idreq_msg;
354
355 /* Send IDENT REQ */
356 idreq_msg = gsm48_msgb_alloc();
357 gprs_put_identity_req(idreq_msg, GSM_MI_TYPE_IMSI);
358 /* Workaround to avoid N(U) collisions and to enable a restart
359 * of the IMSI acquisition procedure. This will work unless the
360 * SGSN has an initial V(UT) within [256-32, 256+n_retries]
361 * (see GSM 04.64, 8.4.2).
362 * TODO: Check whether it is sensible to rely on this, it might
363 * be an issue when P-TMSI patching is _not_ enabled and the SGSN
364 * doesn't reset V(UT) after a detach. */
365 gprs_push_llc_ui(idreq_msg, 0, GPRS_SAPI_GMM,
366 (tlli_info->imsi_acq_retries + 256) % 512);
367 gprs_push_bssgp_dl_unitdata(idreq_msg, tlli_info->tlli.current);
368 gbprox_relay2peer(idreq_msg, peer, bvci);
369 msgb_free(idreq_msg);
370}
371
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200372static void gbproxy_tx_detach_acc(struct gbproxy_peer *peer,
373 struct gbproxy_tlli_info* tlli_info,
374 uint16_t bvci)
375{
376 struct msgb *detacc_msg;
377
378 /* Send DETACH ACC */
379 detacc_msg = gsm48_msgb_alloc();
380 gprs_put_mo_detach_acc(detacc_msg);
381 gprs_push_llc_ui(detacc_msg, 0, GPRS_SAPI_GMM,
382 (tlli_info->imsi_acq_retries + 256) % 512);
383 gprs_push_bssgp_dl_unitdata(detacc_msg, tlli_info->tlli.current);
384 gbprox_relay2peer(detacc_msg, peer, bvci);
385 msgb_free(detacc_msg);
386}
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200387
388/* Return != 0 iff msg still needs to be processed */
389static int gbproxy_imsi_acquisition(struct gbproxy_peer *peer,
390 struct msgb *msg,
391 uint16_t sgsn_nsei,
392 time_t now,
393 struct gbproxy_tlli_info* tlli_info,
394 struct gprs_gb_parse_context *parse_ctx)
395{
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200396 struct msgb *stored_msg;
397
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200398 if (!tlli_info)
399 return 1;
400
Jacob Erlbeck2fd1ba42014-09-11 14:57:03 +0200401 if (!tlli_info->imsi_acq_pending && tlli_info->imsi_len > 0)
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200402 return 1;
403
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200404 if (parse_ctx->g48_hdr)
405 switch (parse_ctx->g48_hdr->msg_type)
406 {
Jacob Erlbecke27ab912014-09-18 10:08:27 +0200407 case GSM48_MT_GMM_RA_UPD_REQ:
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200408 case GSM48_MT_GMM_ATTACH_REQ:
409 if (gbproxy_restart_imsi_acquisition(tlli_info)) {
410 LOGP(DLLC, LOGL_INFO,
411 "NSEI=%d(BSS) IMSI acquisition was in progress "
Jacob Erlbecke27ab912014-09-18 10:08:27 +0200412 "when receiving an %s.\n",
413 msgb_nsei(msg), parse_ctx->llc_msg_name);
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200414 }
415 break;
416 case GSM48_MT_GMM_DETACH_REQ:
417 /* Nothing has been sent to the SGSN yet */
418 if (tlli_info->imsi_acq_pending) {
419 LOGP(DLLC, LOGL_INFO,
420 "NSEI=%d(BSS) IMSI acquisition was in progress "
421 "when receiving a DETACH_REQ.\n",
422 msgb_nsei(msg));
423 }
424 if (!parse_ctx->invalidate_tlli) {
425 LOGP(DLLC, LOGL_INFO,
426 "NSEI=%d(BSS) IMSI not yet acquired, "
427 "faking a DETACH_ACC.\n",
428 msgb_nsei(msg));
429 gbproxy_tx_detach_acc(peer, tlli_info, msgb_bvci(msg));
430 parse_ctx->invalidate_tlli = 1;
431 }
432 gbproxy_reset_imsi_acquisition(tlli_info);
433 gbproxy_update_tlli_state_after(peer, tlli_info, now,
434 parse_ctx);
435 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200436 }
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200437
Jacob Erlbeck2fd1ba42014-09-11 14:57:03 +0200438 if (tlli_info->imsi_acq_pending && tlli_info->imsi_len > 0) {
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200439 int is_ident_resp =
440 parse_ctx->g48_hdr &&
441 parse_ctx->g48_hdr->proto_discr == GSM48_PDISC_MM_GPRS &&
442 parse_ctx->g48_hdr->msg_type == GSM48_MT_GMM_ID_RESP;
443
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200444 /* The IMSI is now available */
445 gbproxy_flush_stored_messages(peer, msg, sgsn_nsei, now,
446 tlli_info, parse_ctx);
447
448 gbproxy_reset_imsi_acquisition(tlli_info);
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200449
450 /* This message is most probably the response to the ident
451 * request sent by gbproxy_acquire_imsi(). Don't forward it to
452 * the SGSN. */
453 return !is_ident_resp;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200454 }
455
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200456 /* The message cannot be processed since the IMSI is still missing */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200457
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200458 /* Enqueue unpatched messages */
459 LOGP(DLLC, LOGL_INFO,
460 "NSEI=%d(BSS) IMSI acquisition in progress, "
461 "storing message (%s)\n",
462 msgb_nsei(msg),
463 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200464
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200465 stored_msg = gprs_msgb_copy(msg, "process_bssgp_ul");
466 msgb_enqueue(&tlli_info->stored_msgs, stored_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200467
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200468 if (!tlli_info->imsi_acq_pending) {
469 LOGP(DLLC, LOGL_INFO,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200470 "NSEI=%d(BSS) IMSI is required but not available, "
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200471 "initiating identification procedure (%s)\n",
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200472 msgb_nsei(msg),
473 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
474
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200475 gbproxy_acquire_imsi(peer, tlli_info, msgb_bvci(msg));
476
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200477 /* There is no explicit retransmission handling, the
478 * implementation relies on the MS doing proper retransmissions
479 * of the triggering message instead */
480
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200481 tlli_info->imsi_acq_pending = 1;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200482 }
483
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200484 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200485}
486
487struct gbproxy_peer *gbproxy_find_peer(struct gbproxy_config *cfg,
488 struct msgb *msg,
489 struct gprs_gb_parse_context *parse_ctx)
490{
491 struct gbproxy_peer *peer = NULL;
492
493 if (msgb_bvci(msg) >= 2)
494 peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg));
495
496 if (!peer && !parse_ctx->to_bss)
497 peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg));
498
499 if (!peer)
500 peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx->bssgp_tp);
501
502 if (!peer) {
503 LOGP(DLLC, LOGL_INFO,
504 "NSEI=%d(%s) patching: didn't find peer for message, "
505 "PDU %d\n",
506 msgb_nsei(msg), parse_ctx->to_bss ? "BSS" : "SGSN",
507 parse_ctx->pdu_type);
508 /* Increment counter */
509 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]);
510 }
511 return peer;
512}
513
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200514/* patch BSSGP message */
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200515static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg,
516 struct msgb *msg,
517 struct gbproxy_peer *peer)
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200518{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200519 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200520 int rc;
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200521 int len_change = 0;
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200522 time_t now;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200523 struct gbproxy_tlli_info *tlli_info = NULL;
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200524 uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200525
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200526 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
Jacob Erlbeck1abfdc22014-09-04 11:42:08 +0200527 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200528 return 1;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200529
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200530 parse_ctx.to_bss = 0;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200531 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200532
533 /* Parse BSSGP/LLC */
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200534 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
535 &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200536
537 if (!rc) {
538 if (!parse_ctx.need_decryption) {
539 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeck643d5222014-08-18 17:26:04 +0200540 "NSEI=%u(BSS) patching: "
541 "failed to parse BSSGP/GMM message\n",
542 msgb_nsei(msg));
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200543 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200544 }
545 }
546
547 /* Get peer */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200548 if (!peer)
549 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200550
551 if (!peer)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200552 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200553
554 now = time(NULL);
555
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200556 gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer,
557 parse_ctx.llc_msg_name);
558
Jacob Erlbeck31591142014-09-03 11:59:48 +0200559 gprs_gb_log_parse_context(&parse_ctx, "BSSGP");
560
561 tlli_info = gbproxy_update_tlli_state_ul(peer, now, &parse_ctx);
562
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200563 if (parse_ctx.g48_hdr) {
564 switch (parse_ctx.g48_hdr->msg_type) {
565 case GSM48_MT_GMM_ATTACH_REQ:
566 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]);
567 break;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200568
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200569 default:
570 break;
571 }
572 }
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200573
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200574 if (tlli_info && cfg->route_to_sgsn2) {
575 if (cfg->acquire_imsi && tlli_info->imsi_len == 0)
576 sgsn_nsei = 0xffff;
577 else if (gbproxy_check_tlli(peer, tlli_info))
578 sgsn_nsei = cfg->nsip_sgsn2_nsei;
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200579 }
580
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200581 if (tlli_info)
582 tlli_info->sgsn_nsei = sgsn_nsei;
583
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200584 /* Handle IMSI acquisition */
585 if (cfg->acquire_imsi) {
586 rc = gbproxy_imsi_acquisition(peer, msg, sgsn_nsei, now,
587 tlli_info, &parse_ctx);
588 if (rc <= 0)
589 return rc;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200590 }
591
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200592 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
593 peer, tlli_info, &len_change, &parse_ctx);
594
595 gbproxy_update_tlli_state_after(peer, tlli_info, now, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200596
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200597 if (sgsn_nsei != cfg->nsip_sgsn_nsei) {
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200598 /* Send message directly to the selected SGSN */
599 rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei);
600 /* Don't let the calling code handle the transmission */
601 return 0;
602 }
603
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200604 return 1;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200605}
606
607/* patch BSSGP message to use core_mcc/mnc on the SGSN side */
608static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg,
609 struct msgb *msg,
610 struct gbproxy_peer *peer)
611{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200612 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200613 int rc;
614 int len_change = 0;
615 time_t now;
616 struct gbproxy_tlli_info *tlli_info = NULL;
617
Jacob Erlbeck1abfdc22014-09-04 11:42:08 +0200618 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
619 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
620 return;
621
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200622 parse_ctx.to_bss = 1;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200623 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200624
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200625 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
626 &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200627
628 if (!rc) {
629 if (!parse_ctx.need_decryption) {
630 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeck643d5222014-08-18 17:26:04 +0200631 "NSEI=%u(SGSN) patching: "
632 "failed to parse BSSGP/GMM message\n",
633 msgb_nsei(msg));
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200634 return;
635 }
636 }
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200637
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200638 /* Get peer */
639 if (!peer)
640 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200641
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200642 if (!peer)
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200643 return;
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200644
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200645 now = time(NULL);
646
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200647 if (parse_ctx.g48_hdr) {
648 switch (parse_ctx.g48_hdr->msg_type) {
649 case GSM48_MT_GMM_ATTACH_REJ:
650 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]);
651 break;
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200652
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200653 default:
654 break;
655 }
656 }
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200657
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200658 gprs_gb_log_parse_context(&parse_ctx, "BSSGP");
659
660 tlli_info = gbproxy_update_tlli_state_dl(peer, now, &parse_ctx);
661
662 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
663 peer, tlli_info, &len_change, &parse_ctx);
664
665 gbproxy_update_tlli_state_after(peer, tlli_info, now, &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200666
Jacob Erlbeck03551e52014-06-27 19:56:05 +0200667 return;
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200668}
669
Harald Welte9f75c352010-04-30 20:26:32 +0200670/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200671static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200672 uint16_t ns_bvci, uint16_t sgsn_nsei)
Harald Welte672f5c42010-05-03 18:54:58 +0200673{
Harald Welte39d0bb52010-05-12 18:10:25 +0000674 /* create a copy of the message so the old one can
675 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200676 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2sgsn");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200677 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000678
Harald Weltee9ea2692010-05-11 20:20:13 +0200679 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200680 msgb_nsei(msg), ns_bvci, sgsn_nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200681
Harald Welte672f5c42010-05-03 18:54:58 +0200682 msgb_bvci(msg) = ns_bvci;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200683 msgb_nsei(msg) = sgsn_nsei;
Harald Welte672f5c42010-05-03 18:54:58 +0200684
Harald Welte69619e32010-05-03 19:05:10 +0200685 strip_ns_hdr(msg);
686
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200687 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
688 if (rc < 0)
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200689 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200690
691 return rc;
Harald Welte672f5c42010-05-03 18:54:58 +0200692}
693
Harald Welte672f5c42010-05-03 18:54:58 +0200694/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200695static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
Harald Welte9f75c352010-04-30 20:26:32 +0200696 uint16_t ns_bvci)
697{
Harald Welte39d0bb52010-05-12 18:10:25 +0000698 /* create a copy of the message so the old one can
699 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200700 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2peer");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200701 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000702
Harald Welte0ab535b2010-05-13 10:34:56 +0200703 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200704 msgb_nsei(msg), ns_bvci, peer->nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200705
Harald Welte9f75c352010-04-30 20:26:32 +0200706 msgb_bvci(msg) = ns_bvci;
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200707 msgb_nsei(msg) = peer->nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200708
Harald Welte0ab535b2010-05-13 10:34:56 +0200709 /* Strip the old NS header, it will be replaced with a new one */
Harald Welte69619e32010-05-03 19:05:10 +0200710 strip_ns_hdr(msg);
711
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200712 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
713 if (rc < 0)
714 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
715
716 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200717}
718
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200719static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type)
Harald Welte36f98d92011-02-06 13:09:29 +0100720{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200721 struct gbproxy_peer *peer;
Harald Welte36f98d92011-02-06 13:09:29 +0100722
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200723 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +0100724 if (!peer) {
725 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
726 ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200727 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +0100728 return -ENOENT;
729 }
730
731 switch (pdu_type) {
732 case BSSGP_PDUT_BVC_BLOCK_ACK:
733 peer->blocked = 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200734 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100735 break;
736 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
737 peer->blocked = 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200738 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100739 break;
740 default:
741 break;
742 }
743 return 0;
744}
745
Harald Welte9f75c352010-04-30 20:26:32 +0200746/* Send a message to a peer identified by ptp_bvci but using ns_bvci
747 * in the NS hdr */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200748static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci,
Harald Welte9f75c352010-04-30 20:26:32 +0200749 uint16_t ns_bvci)
750{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200751 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +0200752
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200753 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200754 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200755 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
Harald Welte1c77c6e2010-05-03 21:37:11 +0200756 ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200757 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte9f75c352010-04-30 20:26:32 +0200758 return -ENOENT;
Harald Welte1c77c6e2010-05-03 21:37:11 +0200759 }
Harald Welte9f75c352010-04-30 20:26:32 +0200760
Harald Welte69619e32010-05-03 19:05:10 +0200761 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200762}
763
Harald Welteb1fd9022012-06-17 12:16:31 +0800764int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
765{
766 return 0;
767}
768
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200769/* Receive an incoming PTP message from a BSS-side NS-VC */
770static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg,
771 struct msgb *msg, uint16_t nsei,
772 uint16_t nsvci, uint16_t ns_bvci)
773{
774 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200775 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
776 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200777 int rc;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200778
779 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
780
781 if (peer)
782 check_peer_nsei(peer, nsei);
783
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200784 rc = gbprox_process_bssgp_ul(cfg, msg, peer);
785 if (!rc)
786 return 0;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200787
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200788 switch (pdu_type) {
789 case BSSGP_PDUT_FLOW_CONTROL_BVC:
790 if (!cfg->route_to_sgsn2)
791 break;
792
793 /* Send a copy to the secondary SGSN */
794 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
795 break;
796 default:
797 break;
798 }
799
800
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200801 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200802}
803
804/* Receive an incoming PTP message from a SGSN-side NS-VC */
805static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg,
806 struct msgb *msg, uint16_t nsei,
807 uint16_t nsvci, uint16_t ns_bvci)
808{
809 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200810 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
811 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200812
813 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
814
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200815 /* Send status messages before patching */
816
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200817 if (!peer) {
818 LOGP(DGPRS, LOGL_INFO, "Didn't find peer for "
819 "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n",
820 ns_bvci, nsvci, nsei);
821 rate_ctr_inc(&cfg->ctrg->
822 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
823 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
824 &ns_bvci, msg);
825 }
826
827 if (peer->blocked) {
828 LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for "
829 "blocked BVCI=%u via NSVC=%u/NSEI=%u\n",
830 ns_bvci, nsvci, nsei);
831 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200832 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200833 }
834
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200835 switch (pdu_type) {
836 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
837 case BSSGP_PDUT_BVC_BLOCK_ACK:
838 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
839 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
840 /* Hide ACKs from the secondary SGSN, the primary SGSN
841 * is responsible to send them. */
842 return 0;
843 break;
844 default:
845 break;
846 }
847
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200848 /* Optionally patch the message */
849 gbprox_process_bssgp_dl(cfg, msg, peer);
850
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200851 return gbprox_relay2peer(msg, peer, ns_bvci);
852}
853
Harald Welte9f75c352010-04-30 20:26:32 +0200854/* Receive an incoming signalling message from a BSS-side NS-VC */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200855static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg,
856 struct msgb *msg, uint16_t nsei,
Harald Welte9f75c352010-04-30 20:26:32 +0200857 uint16_t ns_bvci)
858{
Harald Welteca3620a2010-05-03 16:30:59 +0200859 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200860 struct tlv_parsed tp;
861 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200862 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200863 struct gbproxy_peer *from_peer = NULL;
Harald Welte70f38d22010-05-01 12:10:57 +0200864 struct gprs_ra_id raid;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200865 int copy_to_sgsn2 = 0;
Jacob Erlbeck59300642014-09-08 09:04:01 +0200866 int rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200867
Harald Weltec471d3d2011-02-06 17:13:12 +0100868 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200869 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200870 nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200871 return -EINVAL;
872 }
873
874 /* we actually should never see those two for BVCI == 0, but double-check
875 * just to make sure */
876 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
877 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200878 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200879 "signalling\n", nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200880 return -EINVAL;
881 }
882
883 bssgp_tlv_parse(&tp, bgph->data, data_len);
884
885 switch (pdu_type) {
886 case BSSGP_PDUT_SUSPEND:
887 case BSSGP_PDUT_RESUME:
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200888 /* We implement RAI snooping during SUSPEND/RESUME, since it
889 * establishes a relationsip between BVCI/peer and the routeing
890 * area identification. The snooped information is then used
891 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
892 * BSSGP */
Harald Welte9f75c352010-04-30 20:26:32 +0200893 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
894 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200895 from_peer = gbproxy_peer_by_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200896 if (!from_peer)
897 goto err_no_peer;
Harald Welte1174c082010-05-12 00:07:29 +0200898 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
899 sizeof(from_peer->ra));
Harald Welte7fc98222010-05-11 10:15:42 +0200900 gsm48_parse_ra(&raid, from_peer->ra);
Harald Welte4cf12e92010-05-13 14:14:56 +0200901 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200902 "RAI snooping: RAI %u-%u-%u-%u behind BVCI=%u\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200903 nsei, raid.mcc, raid.mnc, raid.lac,
904 raid.rac , from_peer->bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200905 /* FIXME: This only supports one BSS per RA */
906 break;
Harald Welte44c48302010-05-03 19:22:32 +0200907 case BSSGP_PDUT_BVC_RESET:
908 /* If we receive a BVC reset on the signalling endpoint, we
909 * don't want the SGSN to reset, as the signalling endpoint
910 * is common for all point-to-point BVCs (and thus all BTS) */
911 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200912 uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte72953b82010-05-12 00:20:41 +0200913 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200914 nsei, bvci);
Harald Welte44c48302010-05-03 19:22:32 +0200915 if (bvci == 0) {
916 /* FIXME: only do this if SGSN is alive! */
Harald Weltee9ea2692010-05-11 20:20:13 +0200917 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200918 "BVC RESET ACK of BVCI=0\n", nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200919 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200920 nsei, 0, ns_bvci);
Harald Welte1174c082010-05-12 00:07:29 +0200921 }
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200922 from_peer = gbproxy_peer_by_bvci(cfg, bvci);
Harald Welte1174c082010-05-12 00:07:29 +0200923 if (!from_peer) {
Harald Welte1c77c6e2010-05-03 21:37:11 +0200924 /* if a PTP-BVC is reset, and we don't know that
925 * PTP-BVCI yet, we should allocate a new peer */
926 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200927 "BVCI=%u via NSEI=%u\n", bvci, nsei);
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +0200928 from_peer = gbproxy_peer_alloc(cfg, bvci);
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200929 from_peer->nsei = nsei;
Harald Welte44c48302010-05-03 19:22:32 +0200930 }
Jacob Erlbeck64cb9242013-10-24 12:48:56 +0200931
932 if (!check_peer_nsei(from_peer, nsei))
933 from_peer->nsei = nsei;
934
Harald Welte1174c082010-05-12 00:07:29 +0200935 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
936 struct gprs_ra_id raid;
937 /* We have a Cell Identifier present in this
938 * PDU, this means we can extend our local
939 * state information about this particular cell
940 * */
941 memcpy(from_peer->ra,
942 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
943 sizeof(from_peer->ra));
944 gsm48_parse_ra(&raid, from_peer->ra);
945 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200946 "Cell ID %u-%u-%u-%u\n", nsei,
Harald Welte1174c082010-05-12 00:07:29 +0200947 bvci, raid.mcc, raid.mnc, raid.lac,
948 raid.rac);
949 }
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200950 if (cfg->route_to_sgsn2)
951 copy_to_sgsn2 = 1;
Harald Welte44c48302010-05-03 19:22:32 +0200952 }
953 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200954 }
955
Harald Weltee9ea2692010-05-11 20:20:13 +0200956 /* Normally, we can simply pass on all signalling messages from BSS to
957 * SGSN */
Jacob Erlbeck59300642014-09-08 09:04:01 +0200958 rc = gbprox_process_bssgp_ul(cfg, msg, from_peer);
959 if (!rc)
960 return 0;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200961
962 if (copy_to_sgsn2)
963 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
964
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200965 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200966err_no_peer:
Jacob Erlbeckc5085f92013-10-18 13:04:48 +0200967 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200968 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200969 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200970 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200971err_mand_ie:
Harald Welte0a4050c2010-05-11 10:01:17 +0200972 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200973 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200974 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200975 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200976}
977
978/* Receive paging request from SGSN, we need to relay to proper BSS */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200979static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200980 uint32_t nsei, uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200981{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200982 struct gbproxy_peer *peer = NULL;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200983 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte9f75c352010-04-30 20:26:32 +0200984
Harald Welte4cf12e92010-05-13 14:14:56 +0200985 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200986 nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200987 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200988 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte4cf12e92010-05-13 14:14:56 +0200989 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n",
990 bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200991 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte9f75c352010-04-30 20:26:32 +0200992 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200993 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200994 LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +0800995 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200996 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte9f75c352010-04-30 20:26:32 +0200997 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200998 peer = gbproxy_peer_by_lai(cfg, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200999 LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +08001000 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001001 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte9f75c352010-04-30 20:26:32 +02001002 } else
Harald Welte4cf12e92010-05-13 14:14:56 +02001003 LOGPC(DGPRS, LOGL_INFO, "\n");
1004
1005 if (!peer) {
1006 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001007 "unable to route, missing IE\n", nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001008 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
Harald Welte9f75c352010-04-30 20:26:32 +02001009 return -EINVAL;
Harald Welte4cf12e92010-05-13 14:14:56 +02001010 }
1011 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001012}
1013
Harald Welte0a4050c2010-05-11 10:01:17 +02001014/* Receive an incoming BVC-RESET message from the SGSN */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001015static int rx_reset_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001016 struct msgb *orig_msg,
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001017 struct msgb *msg, struct tlv_parsed *tp,
1018 uint32_t nsei, uint16_t ns_bvci)
Harald Welte0a4050c2010-05-11 10:01:17 +02001019{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001020 struct gbproxy_peer *peer;
Harald Welte0a4050c2010-05-11 10:01:17 +02001021 uint16_t ptp_bvci;
1022
1023 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001024 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001025 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001026 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001027 NULL, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +02001028 }
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001029 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte0a4050c2010-05-11 10:01:17 +02001030
1031 if (ptp_bvci >= 2) {
1032 /* A reset for a PTP BVC was received, forward it to its
1033 * respective peer */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001034 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001035 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +02001036 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001037 nsei, ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001038 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001039 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001040 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001041 &ptp_bvci, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +02001042 }
1043 return gbprox_relay2peer(msg, peer, ns_bvci);
1044 }
1045
1046 /* A reset for the Signalling entity has been received
1047 * from the SGSN. As the signalling BVCI is shared
1048 * among all the BSS's that we multiplex, it needs to
1049 * be relayed */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001050 llist_for_each_entry(peer, &cfg->bts_peers, list)
Harald Welte0a4050c2010-05-11 10:01:17 +02001051 gbprox_relay2peer(msg, peer, ns_bvci);
1052
1053 return 0;
1054}
1055
Harald Welte9f75c352010-04-30 20:26:32 +02001056/* Receive an incoming signalling message from the SGSN-side NS-VC */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001057static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001058 struct msgb *orig_msg, uint32_t nsei,
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001059 uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001060{
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001061 struct bssgp_normal_hdr *bgph =
1062 (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001063 struct tlv_parsed tp;
1064 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001065 int data_len;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001066 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +02001067 uint16_t bvci;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001068 struct msgb *msg;
Harald Welte9f75c352010-04-30 20:26:32 +02001069 int rc = 0;
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001070 int cause;
Harald Welte9f75c352010-04-30 20:26:32 +02001071
Harald Weltec471d3d2011-02-06 17:13:12 +01001072 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +02001073 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001074 "signalling\n", nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001075 /* FIXME: Send proper error message */
Harald Welte9f75c352010-04-30 20:26:32 +02001076 return -EINVAL;
1077 }
1078
1079 /* we actually should never see those two for BVCI == 0, but double-check
1080 * just to make sure */
1081 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1082 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +02001083 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001084 "signalling\n", nsei);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001085 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001086 }
1087
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001088 msg = gprs_msgb_copy(orig_msg, "rx_sig_from_sgsn");
1089 gbprox_process_bssgp_dl(cfg, msg, NULL);
1090 /* Update message info */
1091 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1092 data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +02001093 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1094
1095 switch (pdu_type) {
Harald Welte0a4050c2010-05-11 10:01:17 +02001096 case BSSGP_PDUT_BVC_RESET:
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001097 rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001098 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001099 case BSSGP_PDUT_BVC_RESET_ACK:
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +02001100 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
1101 break;
1102 /* fall through */
1103 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9f75c352010-04-30 20:26:32 +02001104 /* simple case: BVCI IE is mandatory */
1105 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1106 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001107 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001108 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001109 break;
1110 case BSSGP_PDUT_PAGING_PS:
1111 case BSSGP_PDUT_PAGING_CS:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001112 /* process the paging request (LAI/RAI lookup) */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001113 rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001114 break;
1115 case BSSGP_PDUT_STATUS:
Harald Welte0a4050c2010-05-11 10:01:17 +02001116 /* Some exception has occurred */
Harald Welte44c48302010-05-03 19:22:32 +02001117 LOGP(DGPRS, LOGL_NOTICE,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001118 "NSEI=%u(SGSN) BSSGP STATUS ", nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +02001119 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
1120 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1121 goto err_mand_ie;
1122 }
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001123 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
Harald Welte0a4050c2010-05-11 10:01:17 +02001124 LOGPC(DGPRS, LOGL_NOTICE,
1125 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001126 bssgp_cause_str(cause));
Harald Welte0a4050c2010-05-11 10:01:17 +02001127 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001128 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1129 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%u\n", bvci);
1130
1131 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI)
1132 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001133 } else
1134 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welte9f75c352010-04-30 20:26:32 +02001135 break;
1136 /* those only exist in the SGSN -> BSS direction */
1137 case BSSGP_PDUT_SUSPEND_ACK:
1138 case BSSGP_PDUT_SUSPEND_NACK:
1139 case BSSGP_PDUT_RESUME_ACK:
1140 case BSSGP_PDUT_RESUME_NACK:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001141 /* RAI IE is mandatory */
Harald Welte9f75c352010-04-30 20:26:32 +02001142 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1143 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001144 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte9f75c352010-04-30 20:26:32 +02001145 if (!peer)
1146 goto err_no_peer;
Harald Welte69619e32010-05-03 19:05:10 +02001147 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001148 break;
Harald Welte36f98d92011-02-06 13:09:29 +01001149 case BSSGP_PDUT_BVC_BLOCK_ACK:
1150 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1151 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1152 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001153 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte36f98d92011-02-06 13:09:29 +01001154 if (bvci == 0) {
1155 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001156 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
Harald Welte36f98d92011-02-06 13:09:29 +01001157 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
1158 /* should we send STATUS ? */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001159 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001160 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +01001161 } else {
1162 /* Mark BVC as (un)blocked */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001163 block_unblock_peer(cfg, bvci, pdu_type);
Harald Welte36f98d92011-02-06 13:09:29 +01001164 }
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001165 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +01001166 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001167 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte44c48302010-05-03 19:22:32 +02001168 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001169 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001170 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001171 ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001172 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001173 break;
1174 default:
Harald Welte72953b82010-05-12 00:20:41 +02001175 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n",
1176 pdu_type);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001177 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001178 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001179 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001180 break;
1181 }
1182
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001183 msgb_free(msg);
1184
Harald Welte9f75c352010-04-30 20:26:32 +02001185 return rc;
1186err_mand_ie:
Harald Welte1c77c6e2010-05-03 21:37:11 +02001187 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001188 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001189 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001190 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001191 msgb_free(msg);
1192 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001193err_no_peer:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001194 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001195 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001196 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001197 msgb_free(msg);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001198 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001199}
1200
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001201static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei)
1202{
1203 return nsei == cfg->nsip_sgsn_nsei ||
1204 (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei);
1205}
1206
Harald Welte9f75c352010-04-30 20:26:32 +02001207/* Main input function for Gb proxy */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001208int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei,
1209 uint16_t ns_bvci, uint16_t nsvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001210{
Harald Welte672f5c42010-05-03 18:54:58 +02001211 int rc;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001212 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001213
1214 /* Only BVCI=0 messages need special treatment */
1215 if (ns_bvci == 0 || ns_bvci == 1) {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001216 if (remote_end_is_sgsn)
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001217 rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001218 else
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001219 rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci);
Harald Welte672f5c42010-05-03 18:54:58 +02001220 } else {
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +02001221 /* All other BVCI are PTP */
1222 if (remote_end_is_sgsn)
1223 rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei, nsvci,
1224 ns_bvci);
1225 else
1226 rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei, nsvci,
1227 ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001228 }
1229
Harald Welte672f5c42010-05-03 18:54:58 +02001230 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +02001231}
Harald Welte85801d02010-05-11 05:49:43 +02001232
Harald Welte1ccbf442010-05-14 11:53:08 +00001233int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi)
1234{
1235 struct gprs_nsvc *nsvc;
1236
1237 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
1238 if (!nsvc->persistent)
1239 continue;
1240 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1241 }
1242 return 0;
1243}
1244
Harald Weltec1c1dd22010-05-11 06:34:24 +02001245/* Signal handler for signals from NS layer */
1246int gbprox_signal(unsigned int subsys, unsigned int signal,
1247 void *handler_data, void *signal_data)
1248{
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001249 struct gbproxy_config *cfg = handler_data;
Harald Weltec1c1dd22010-05-11 06:34:24 +02001250 struct ns_signal_data *nssd = signal_data;
1251 struct gprs_nsvc *nsvc = nssd->nsvc;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001252 struct gbproxy_peer *peer;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001253 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsvc->nsei);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001254
Harald Weltea6a20b42012-06-16 16:40:42 +08001255 if (subsys != SS_L_NS)
Harald Weltec1c1dd22010-05-11 06:34:24 +02001256 return 0;
1257
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001258 if (signal == S_NS_RESET && remote_end_is_sgsn) {
Harald Weltef69c0592010-05-11 18:29:44 +02001259 /* We have received a NS-RESET from the NSEI and NSVC
1260 * of the SGSN. This might happen with SGSN that start
1261 * their own NS-RESET procedure without waiting for our
1262 * NS-RESET */
1263 nsvc->remote_end_is_sgsn = 1;
1264 }
1265
Harald Welteb778d2c2010-05-12 13:28:25 +00001266 if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) {
1267 LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, "
1268 "re-starting RESET procedure\n");
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001269 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001270 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
Harald Weltee6599ee2012-06-17 12:25:53 +08001271 gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr,
1272 nsvc->nsei, nsvc->nsvci);
Harald Welteb778d2c2010-05-12 13:28:25 +00001273 }
1274
Harald Welte5e106d72011-02-06 16:33:29 +01001275 if (!nsvc->remote_end_is_sgsn) {
1276 /* from BSS to SGSN */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001277 peer = gbproxy_peer_by_nsei(cfg, nsvc->nsei);
Harald Welte5e106d72011-02-06 16:33:29 +01001278 if (!peer) {
1279 LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer "
1280 "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei,
1281 nsvc->nsvci);
1282 return 0;
1283 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001284 switch (signal) {
1285 case S_NS_RESET:
Harald Weltec1c1dd22010-05-11 06:34:24 +02001286 case S_NS_BLOCK:
Harald Welte5e106d72011-02-06 16:33:29 +01001287 if (!peer->blocked)
1288 break;
1289 LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from "
1290 "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n",
1291 nsvc->nsei, nsvc->nsvci);
1292 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei,
1293 peer->bvci, 0);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001294 break;
Harald Welte5e106d72011-02-06 16:33:29 +01001295 }
1296 } else {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001297 /* Forward this message to all NS-VC to BSS */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001298 struct gprs_ns_inst *nsi = cfg->nsi;
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001299 struct gprs_nsvc *next_nsvc;
1300
1301 llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) {
1302 if (next_nsvc->remote_end_is_sgsn)
1303 continue;
1304
1305 /* Note that the following does not start the full
1306 * procedures including timer based retransmissions. */
Harald Welte5e106d72011-02-06 16:33:29 +01001307 switch (signal) {
1308 case S_NS_RESET:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001309 gprs_ns_tx_reset(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001310 break;
1311 case S_NS_BLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001312 gprs_ns_tx_block(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001313 break;
1314 case S_NS_UNBLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001315 gprs_ns_tx_unblock(next_nsvc);
Harald Welte5e106d72011-02-06 16:33:29 +01001316 break;
1317 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001318 }
1319 }
1320 return 0;
1321}
1322
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001323void gbprox_reset(struct gbproxy_config *cfg)
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001324{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001325 struct gbproxy_peer *peer, *tmp;
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001326
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001327 llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list)
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +02001328 gbproxy_peer_free(peer);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001329
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001330 rate_ctr_group_free(cfg->ctrg);
1331 gbproxy_init_config(cfg);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001332}
1333
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001334int gbproxy_init_config(struct gbproxy_config *cfg)
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001335{
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001336 struct timespec tp;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001337 INIT_LLIST_HEAD(&cfg->bts_peers);
Holger Hans Peter Freythera7027a02014-08-04 11:19:56 +02001338 cfg->ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0);
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001339 clock_gettime(CLOCK_REALTIME, &tp);
1340 cfg->bss_ptmsi_state = tp.tv_sec + tp.tv_nsec;
1341 cfg->sgsn_tlli_state = tp.tv_sec - tp.tv_nsec;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001342 return 0;
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001343}