blob: 4b46b0e2b9f4601b44d5058191a0ec41aeab8cd6 [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;
229 if (!peer->cfg->patch_ptmsi) {
230 bss_ptmsi = sgsn_ptmsi;
231 } else {
232 do {
233 bss_ptmsi = rand_r(&peer->cfg->bss_ptmsi_state);
234 bss_ptmsi = bss_ptmsi | 0xC0000000;
235
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200236 if (gbproxy_find_tlli_by_ptmsi(peer, bss_ptmsi))
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200237 bss_ptmsi = GSM_RESERVED_TMSI;
238 } while (bss_ptmsi == GSM_RESERVED_TMSI);
239 }
240
241 return bss_ptmsi;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200242}
243
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200244uint32_t gbproxy_make_sgsn_tlli(struct gbproxy_peer *peer,
245 struct gbproxy_tlli_info *tlli_info,
246 uint32_t bss_tlli)
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200247{
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200248 uint32_t sgsn_tlli;
249 if (!peer->cfg->patch_ptmsi) {
250 sgsn_tlli = bss_tlli;
251 } else if (tlli_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI) {
252 sgsn_tlli = gprs_tmsi2tlli(tlli_info->sgsn_tlli.ptmsi,
253 TLLI_FOREIGN);
254 } else {
255 do {
256 /* create random TLLI, 0b01111xxx... */
257 sgsn_tlli = rand_r(&peer->cfg->sgsn_tlli_state);
258 sgsn_tlli = (sgsn_tlli & 0x7fffffff) | 0x78000000;
259
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200260 if (gbproxy_find_tlli_by_any_sgsn_tlli(peer, sgsn_tlli))
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200261 sgsn_tlli = 0;
262 } while (!sgsn_tlli);
263 }
264 return sgsn_tlli;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200265}
266
Jacob Erlbeck31591142014-09-03 11:59:48 +0200267/* Returns != 0 iff IMSI acquisition was in progress */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200268static int gbproxy_restart_imsi_acquisition(struct gbproxy_tlli_info* tlli_info)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200269{
270 int in_progress = 0;
271 if (!tlli_info)
272 return 0;
273
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200274 if (tlli_info->imsi_acq_pending) {
Jacob Erlbeck31591142014-09-03 11:59:48 +0200275 in_progress = 1;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200276 tlli_info->imsi_acq_retries += 1;
277 }
Jacob Erlbeck31591142014-09-03 11:59:48 +0200278
279 gbproxy_tlli_info_discard_messages(tlli_info);
280 tlli_info->imsi_acq_pending = 0;
281
282 return in_progress;
283}
284
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200285static void gbproxy_reset_imsi_acquisition(struct gbproxy_tlli_info* tlli_info)
286{
287 gbproxy_restart_imsi_acquisition(tlli_info);
288 tlli_info->imsi_acq_retries = 0;
289}
290
291static void gbproxy_flush_stored_messages(struct gbproxy_peer *peer,
292 struct msgb *msg,
293 uint16_t sgsn_nsei,
294 time_t now,
295 struct gbproxy_tlli_info* tlli_info,
296 struct gprs_gb_parse_context *parse_ctx)
297{
298 int rc;
299 struct msgb *stored_msg;
300 /* Got identity response with IMSI, assuming the request had
301 * been generated by the gbproxy */
302
303 LOGP(DLLC, LOGL_DEBUG,
304 "NSEI=%d(BSS) IMSI acquisition succeeded, "
305 "flushing stored messages\n",
306 msgb_nsei(msg));
307
308 /* Patch and flush stored messages towards the SGSN */
309 while ((stored_msg = msgb_dequeue(&tlli_info->stored_msgs))) {
310 struct gprs_gb_parse_context tmp_parse_ctx = {0};
311 tmp_parse_ctx.to_bss = 0;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200312 tmp_parse_ctx.peer_nsei = msgb_nsei(stored_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200313 int len_change = 0;
314
315 gprs_gb_parse_bssgp(msgb_bssgph(stored_msg),
316 msgb_bssgp_len(stored_msg),
317 &tmp_parse_ctx);
318 gbproxy_patch_bssgp(msg, msgb_bssgph(stored_msg),
319 msgb_bssgp_len(stored_msg),
320 peer, tlli_info, &len_change,
321 &tmp_parse_ctx);
322
323 gbproxy_update_tlli_state_after(peer, tlli_info, now,
324 &tmp_parse_ctx);
325
326 rc = gbprox_relay2sgsn(peer->cfg, stored_msg,
327 msgb_bvci(msg), sgsn_nsei);
328
329 if (rc < 0)
330 LOGP(DLLC, LOGL_ERROR,
331 "NSEI=%d(BSS) failed to send stored message "
332 "(%s)\n",
333 msgb_nsei(msg),
334 parse_ctx->llc_msg_name ?
335 parse_ctx->llc_msg_name : "BSSGP");
336 msgb_free(stored_msg);
337 }
338}
339
340static void gbproxy_acquire_imsi(struct gbproxy_peer *peer,
341 struct gbproxy_tlli_info* tlli_info,
342 uint16_t bvci)
343{
344 struct msgb *idreq_msg;
345
346 /* Send IDENT REQ */
347 idreq_msg = gsm48_msgb_alloc();
348 gprs_put_identity_req(idreq_msg, GSM_MI_TYPE_IMSI);
349 /* Workaround to avoid N(U) collisions and to enable a restart
350 * of the IMSI acquisition procedure. This will work unless the
351 * SGSN has an initial V(UT) within [256-32, 256+n_retries]
352 * (see GSM 04.64, 8.4.2).
353 * TODO: Check whether it is sensible to rely on this, it might
354 * be an issue when P-TMSI patching is _not_ enabled and the SGSN
355 * doesn't reset V(UT) after a detach. */
356 gprs_push_llc_ui(idreq_msg, 0, GPRS_SAPI_GMM,
357 (tlli_info->imsi_acq_retries + 256) % 512);
358 gprs_push_bssgp_dl_unitdata(idreq_msg, tlli_info->tlli.current);
359 gbprox_relay2peer(idreq_msg, peer, bvci);
360 msgb_free(idreq_msg);
361}
362
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200363static void gbproxy_tx_detach_acc(struct gbproxy_peer *peer,
364 struct gbproxy_tlli_info* tlli_info,
365 uint16_t bvci)
366{
367 struct msgb *detacc_msg;
368
369 /* Send DETACH ACC */
370 detacc_msg = gsm48_msgb_alloc();
371 gprs_put_mo_detach_acc(detacc_msg);
372 gprs_push_llc_ui(detacc_msg, 0, GPRS_SAPI_GMM,
373 (tlli_info->imsi_acq_retries + 256) % 512);
374 gprs_push_bssgp_dl_unitdata(detacc_msg, tlli_info->tlli.current);
375 gbprox_relay2peer(detacc_msg, peer, bvci);
376 msgb_free(detacc_msg);
377}
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200378
379/* Return != 0 iff msg still needs to be processed */
380static int gbproxy_imsi_acquisition(struct gbproxy_peer *peer,
381 struct msgb *msg,
382 uint16_t sgsn_nsei,
383 time_t now,
384 struct gbproxy_tlli_info* tlli_info,
385 struct gprs_gb_parse_context *parse_ctx)
386{
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200387 struct msgb *stored_msg;
388
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200389 if (!tlli_info)
390 return 1;
391
Jacob Erlbeck2fd1ba42014-09-11 14:57:03 +0200392 if (!tlli_info->imsi_acq_pending && tlli_info->imsi_len > 0)
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200393 return 1;
394
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200395 if (parse_ctx->g48_hdr)
396 switch (parse_ctx->g48_hdr->msg_type)
397 {
398 case GSM48_MT_GMM_ATTACH_REQ:
399 if (gbproxy_restart_imsi_acquisition(tlli_info)) {
400 LOGP(DLLC, LOGL_INFO,
401 "NSEI=%d(BSS) IMSI acquisition was in progress "
402 "when receiving an ATTACH_REQ.\n",
403 msgb_nsei(msg));
404 }
405 break;
406 case GSM48_MT_GMM_DETACH_REQ:
407 /* Nothing has been sent to the SGSN yet */
408 if (tlli_info->imsi_acq_pending) {
409 LOGP(DLLC, LOGL_INFO,
410 "NSEI=%d(BSS) IMSI acquisition was in progress "
411 "when receiving a DETACH_REQ.\n",
412 msgb_nsei(msg));
413 }
414 if (!parse_ctx->invalidate_tlli) {
415 LOGP(DLLC, LOGL_INFO,
416 "NSEI=%d(BSS) IMSI not yet acquired, "
417 "faking a DETACH_ACC.\n",
418 msgb_nsei(msg));
419 gbproxy_tx_detach_acc(peer, tlli_info, msgb_bvci(msg));
420 parse_ctx->invalidate_tlli = 1;
421 }
422 gbproxy_reset_imsi_acquisition(tlli_info);
423 gbproxy_update_tlli_state_after(peer, tlli_info, now,
424 parse_ctx);
425 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200426 }
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200427
Jacob Erlbeck2fd1ba42014-09-11 14:57:03 +0200428 if (tlli_info->imsi_acq_pending && tlli_info->imsi_len > 0) {
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200429 int is_ident_resp =
430 parse_ctx->g48_hdr &&
431 parse_ctx->g48_hdr->proto_discr == GSM48_PDISC_MM_GPRS &&
432 parse_ctx->g48_hdr->msg_type == GSM48_MT_GMM_ID_RESP;
433
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200434 /* The IMSI is now available */
435 gbproxy_flush_stored_messages(peer, msg, sgsn_nsei, now,
436 tlli_info, parse_ctx);
437
438 gbproxy_reset_imsi_acquisition(tlli_info);
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200439
440 /* This message is most probably the response to the ident
441 * request sent by gbproxy_acquire_imsi(). Don't forward it to
442 * the SGSN. */
443 return !is_ident_resp;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200444 }
445
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200446 /* The message cannot be processed since the IMSI is still missing */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200447
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200448 /* Enqueue unpatched messages */
449 LOGP(DLLC, LOGL_INFO,
450 "NSEI=%d(BSS) IMSI acquisition in progress, "
451 "storing message (%s)\n",
452 msgb_nsei(msg),
453 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200454
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200455 stored_msg = gprs_msgb_copy(msg, "process_bssgp_ul");
456 msgb_enqueue(&tlli_info->stored_msgs, stored_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200457
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200458 if (!tlli_info->imsi_acq_pending) {
459 LOGP(DLLC, LOGL_INFO,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200460 "NSEI=%d(BSS) IMSI is required but not available, "
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200461 "initiating identification procedure (%s)\n",
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200462 msgb_nsei(msg),
463 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
464
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200465 gbproxy_acquire_imsi(peer, tlli_info, msgb_bvci(msg));
466
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200467 /* There is no explicit retransmission handling, the
468 * implementation relies on the MS doing proper retransmissions
469 * of the triggering message instead */
470
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200471 tlli_info->imsi_acq_pending = 1;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200472 }
473
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200474 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200475}
476
477struct gbproxy_peer *gbproxy_find_peer(struct gbproxy_config *cfg,
478 struct msgb *msg,
479 struct gprs_gb_parse_context *parse_ctx)
480{
481 struct gbproxy_peer *peer = NULL;
482
483 if (msgb_bvci(msg) >= 2)
484 peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg));
485
486 if (!peer && !parse_ctx->to_bss)
487 peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg));
488
489 if (!peer)
490 peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx->bssgp_tp);
491
492 if (!peer) {
493 LOGP(DLLC, LOGL_INFO,
494 "NSEI=%d(%s) patching: didn't find peer for message, "
495 "PDU %d\n",
496 msgb_nsei(msg), parse_ctx->to_bss ? "BSS" : "SGSN",
497 parse_ctx->pdu_type);
498 /* Increment counter */
499 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]);
500 }
501 return peer;
502}
503
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200504/* patch BSSGP message */
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200505static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg,
506 struct msgb *msg,
507 struct gbproxy_peer *peer)
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200508{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200509 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200510 int rc;
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200511 int len_change = 0;
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200512 time_t now;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200513 struct gbproxy_tlli_info *tlli_info = NULL;
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200514 uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200515
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200516 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
Jacob Erlbeck1abfdc22014-09-04 11:42:08 +0200517 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200518 return 1;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200519
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200520 parse_ctx.to_bss = 0;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200521 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200522
523 /* Parse BSSGP/LLC */
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200524 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
525 &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200526
527 if (!rc) {
528 if (!parse_ctx.need_decryption) {
529 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeck643d5222014-08-18 17:26:04 +0200530 "NSEI=%u(BSS) patching: "
531 "failed to parse BSSGP/GMM message\n",
532 msgb_nsei(msg));
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200533 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200534 }
535 }
536
537 /* Get peer */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200538 if (!peer)
539 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200540
541 if (!peer)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200542 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200543
544 now = time(NULL);
545
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200546 gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer,
547 parse_ctx.llc_msg_name);
548
Jacob Erlbeck31591142014-09-03 11:59:48 +0200549 gprs_gb_log_parse_context(&parse_ctx, "BSSGP");
550
551 tlli_info = gbproxy_update_tlli_state_ul(peer, now, &parse_ctx);
552
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200553 if (parse_ctx.g48_hdr) {
554 switch (parse_ctx.g48_hdr->msg_type) {
555 case GSM48_MT_GMM_ATTACH_REQ:
556 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]);
557 break;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200558
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200559 default:
560 break;
561 }
562 }
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200563
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200564 if (tlli_info && cfg->route_to_sgsn2) {
565 if (cfg->acquire_imsi && tlli_info->imsi_len == 0)
566 sgsn_nsei = 0xffff;
567 else if (gbproxy_check_tlli(peer, tlli_info))
568 sgsn_nsei = cfg->nsip_sgsn2_nsei;
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200569 }
570
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200571 if (tlli_info)
572 tlli_info->sgsn_nsei = sgsn_nsei;
573
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200574 /* Handle IMSI acquisition */
575 if (cfg->acquire_imsi) {
576 rc = gbproxy_imsi_acquisition(peer, msg, sgsn_nsei, now,
577 tlli_info, &parse_ctx);
578 if (rc <= 0)
579 return rc;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200580 }
581
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200582 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
583 peer, tlli_info, &len_change, &parse_ctx);
584
585 gbproxy_update_tlli_state_after(peer, tlli_info, now, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200586
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200587 if (sgsn_nsei != cfg->nsip_sgsn_nsei) {
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200588 /* Send message directly to the selected SGSN */
589 rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei);
590 /* Don't let the calling code handle the transmission */
591 return 0;
592 }
593
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200594 return 1;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200595}
596
597/* patch BSSGP message to use core_mcc/mnc on the SGSN side */
598static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg,
599 struct msgb *msg,
600 struct gbproxy_peer *peer)
601{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200602 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200603 int rc;
604 int len_change = 0;
605 time_t now;
606 struct gbproxy_tlli_info *tlli_info = NULL;
607
Jacob Erlbeck1abfdc22014-09-04 11:42:08 +0200608 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
609 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
610 return;
611
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200612 parse_ctx.to_bss = 1;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200613 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200614
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200615 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
616 &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200617
618 if (!rc) {
619 if (!parse_ctx.need_decryption) {
620 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeck643d5222014-08-18 17:26:04 +0200621 "NSEI=%u(SGSN) patching: "
622 "failed to parse BSSGP/GMM message\n",
623 msgb_nsei(msg));
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200624 return;
625 }
626 }
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200627
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200628 /* Get peer */
629 if (!peer)
630 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200631
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200632 if (!peer)
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200633 return;
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200634
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200635 now = time(NULL);
636
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200637 if (parse_ctx.g48_hdr) {
638 switch (parse_ctx.g48_hdr->msg_type) {
639 case GSM48_MT_GMM_ATTACH_REJ:
640 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]);
641 break;
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200642
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200643 default:
644 break;
645 }
646 }
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200647
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200648 gprs_gb_log_parse_context(&parse_ctx, "BSSGP");
649
650 tlli_info = gbproxy_update_tlli_state_dl(peer, now, &parse_ctx);
651
652 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
653 peer, tlli_info, &len_change, &parse_ctx);
654
655 gbproxy_update_tlli_state_after(peer, tlli_info, now, &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200656
Jacob Erlbeck03551e52014-06-27 19:56:05 +0200657 return;
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200658}
659
Harald Welte9f75c352010-04-30 20:26:32 +0200660/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200661static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200662 uint16_t ns_bvci, uint16_t sgsn_nsei)
Harald Welte672f5c42010-05-03 18:54:58 +0200663{
Harald Welte39d0bb52010-05-12 18:10:25 +0000664 /* create a copy of the message so the old one can
665 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200666 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2sgsn");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200667 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000668
Harald Weltee9ea2692010-05-11 20:20:13 +0200669 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200670 msgb_nsei(msg), ns_bvci, sgsn_nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200671
Harald Welte672f5c42010-05-03 18:54:58 +0200672 msgb_bvci(msg) = ns_bvci;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200673 msgb_nsei(msg) = sgsn_nsei;
Harald Welte672f5c42010-05-03 18:54:58 +0200674
Harald Welte69619e32010-05-03 19:05:10 +0200675 strip_ns_hdr(msg);
676
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200677 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
678 if (rc < 0)
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200679 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200680
681 return rc;
Harald Welte672f5c42010-05-03 18:54:58 +0200682}
683
Harald Welte672f5c42010-05-03 18:54:58 +0200684/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200685static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
Harald Welte9f75c352010-04-30 20:26:32 +0200686 uint16_t ns_bvci)
687{
Harald Welte39d0bb52010-05-12 18:10:25 +0000688 /* create a copy of the message so the old one can
689 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200690 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2peer");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200691 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000692
Harald Welte0ab535b2010-05-13 10:34:56 +0200693 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200694 msgb_nsei(msg), ns_bvci, peer->nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200695
Harald Welte9f75c352010-04-30 20:26:32 +0200696 msgb_bvci(msg) = ns_bvci;
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200697 msgb_nsei(msg) = peer->nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200698
Harald Welte0ab535b2010-05-13 10:34:56 +0200699 /* Strip the old NS header, it will be replaced with a new one */
Harald Welte69619e32010-05-03 19:05:10 +0200700 strip_ns_hdr(msg);
701
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200702 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
703 if (rc < 0)
704 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
705
706 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200707}
708
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200709static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type)
Harald Welte36f98d92011-02-06 13:09:29 +0100710{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200711 struct gbproxy_peer *peer;
Harald Welte36f98d92011-02-06 13:09:29 +0100712
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200713 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +0100714 if (!peer) {
715 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
716 ptp_bvci);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200717 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +0100718 return -ENOENT;
719 }
720
721 switch (pdu_type) {
722 case BSSGP_PDUT_BVC_BLOCK_ACK:
723 peer->blocked = 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200724 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100725 break;
726 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
727 peer->blocked = 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200728 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100729 break;
730 default:
731 break;
732 }
733 return 0;
734}
735
Harald Welte9f75c352010-04-30 20:26:32 +0200736/* Send a message to a peer identified by ptp_bvci but using ns_bvci
737 * in the NS hdr */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200738static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci,
Harald Welte9f75c352010-04-30 20:26:32 +0200739 uint16_t ns_bvci)
740{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200741 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +0200742
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200743 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200744 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200745 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
Harald Welte1c77c6e2010-05-03 21:37:11 +0200746 ptp_bvci);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200747 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte9f75c352010-04-30 20:26:32 +0200748 return -ENOENT;
Harald Welte1c77c6e2010-05-03 21:37:11 +0200749 }
Harald Welte9f75c352010-04-30 20:26:32 +0200750
Harald Welte69619e32010-05-03 19:05:10 +0200751 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200752}
753
Harald Welteb1fd9022012-06-17 12:16:31 +0800754int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
755{
756 return 0;
757}
758
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200759/* Receive an incoming PTP message from a BSS-side NS-VC */
760static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg,
761 struct msgb *msg, uint16_t nsei,
762 uint16_t nsvci, uint16_t ns_bvci)
763{
764 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200765 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
766 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200767 int rc;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200768
769 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
770
771 if (peer)
772 check_peer_nsei(peer, nsei);
773
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200774 rc = gbprox_process_bssgp_ul(cfg, msg, peer);
775 if (!rc)
776 return 0;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200777
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200778 switch (pdu_type) {
779 case BSSGP_PDUT_FLOW_CONTROL_BVC:
780 if (!cfg->route_to_sgsn2)
781 break;
782
783 /* Send a copy to the secondary SGSN */
784 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
785 break;
786 default:
787 break;
788 }
789
790
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200791 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200792}
793
794/* Receive an incoming PTP message from a SGSN-side NS-VC */
795static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg,
796 struct msgb *msg, uint16_t nsei,
797 uint16_t nsvci, uint16_t ns_bvci)
798{
799 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200800 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
801 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200802
803 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
804
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200805 /* Send status messages before patching */
806
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200807 if (!peer) {
808 LOGP(DGPRS, LOGL_INFO, "Didn't find peer for "
809 "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n",
810 ns_bvci, nsvci, nsei);
811 rate_ctr_inc(&cfg->ctrg->
812 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
813 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
814 &ns_bvci, msg);
815 }
816
817 if (peer->blocked) {
818 LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for "
819 "blocked BVCI=%u via NSVC=%u/NSEI=%u\n",
820 ns_bvci, nsvci, nsei);
821 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200822 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200823 }
824
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200825 switch (pdu_type) {
826 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
827 case BSSGP_PDUT_BVC_BLOCK_ACK:
828 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
829 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
830 /* Hide ACKs from the secondary SGSN, the primary SGSN
831 * is responsible to send them. */
832 return 0;
833 break;
834 default:
835 break;
836 }
837
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200838 /* Optionally patch the message */
839 gbprox_process_bssgp_dl(cfg, msg, peer);
840
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200841 return gbprox_relay2peer(msg, peer, ns_bvci);
842}
843
Harald Welte9f75c352010-04-30 20:26:32 +0200844/* Receive an incoming signalling message from a BSS-side NS-VC */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200845static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg,
846 struct msgb *msg, uint16_t nsei,
Harald Welte9f75c352010-04-30 20:26:32 +0200847 uint16_t ns_bvci)
848{
Harald Welteca3620a2010-05-03 16:30:59 +0200849 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200850 struct tlv_parsed tp;
851 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200852 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200853 struct gbproxy_peer *from_peer = NULL;
Harald Welte70f38d22010-05-01 12:10:57 +0200854 struct gprs_ra_id raid;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200855 int copy_to_sgsn2 = 0;
Jacob Erlbeck59300642014-09-08 09:04:01 +0200856 int rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200857
Harald Weltec471d3d2011-02-06 17:13:12 +0100858 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200859 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200860 nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200861 return -EINVAL;
862 }
863
864 /* we actually should never see those two for BVCI == 0, but double-check
865 * just to make sure */
866 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
867 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200868 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200869 "signalling\n", nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200870 return -EINVAL;
871 }
872
873 bssgp_tlv_parse(&tp, bgph->data, data_len);
874
875 switch (pdu_type) {
876 case BSSGP_PDUT_SUSPEND:
877 case BSSGP_PDUT_RESUME:
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200878 /* We implement RAI snooping during SUSPEND/RESUME, since it
879 * establishes a relationsip between BVCI/peer and the routeing
880 * area identification. The snooped information is then used
881 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
882 * BSSGP */
Harald Welte9f75c352010-04-30 20:26:32 +0200883 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
884 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200885 from_peer = gbproxy_peer_by_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200886 if (!from_peer)
887 goto err_no_peer;
Harald Welte1174c082010-05-12 00:07:29 +0200888 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
889 sizeof(from_peer->ra));
Harald Welte7fc98222010-05-11 10:15:42 +0200890 gsm48_parse_ra(&raid, from_peer->ra);
Harald Welte4cf12e92010-05-13 14:14:56 +0200891 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200892 "RAI snooping: RAI %u-%u-%u-%u behind BVCI=%u\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200893 nsei, raid.mcc, raid.mnc, raid.lac,
894 raid.rac , from_peer->bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200895 /* FIXME: This only supports one BSS per RA */
896 break;
Harald Welte44c48302010-05-03 19:22:32 +0200897 case BSSGP_PDUT_BVC_RESET:
898 /* If we receive a BVC reset on the signalling endpoint, we
899 * don't want the SGSN to reset, as the signalling endpoint
900 * is common for all point-to-point BVCs (and thus all BTS) */
901 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200902 uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte72953b82010-05-12 00:20:41 +0200903 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200904 nsei, bvci);
Harald Welte44c48302010-05-03 19:22:32 +0200905 if (bvci == 0) {
906 /* FIXME: only do this if SGSN is alive! */
Harald Weltee9ea2692010-05-11 20:20:13 +0200907 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200908 "BVC RESET ACK of BVCI=0\n", nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200909 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200910 nsei, 0, ns_bvci);
Harald Welte1174c082010-05-12 00:07:29 +0200911 }
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200912 from_peer = gbproxy_peer_by_bvci(cfg, bvci);
Harald Welte1174c082010-05-12 00:07:29 +0200913 if (!from_peer) {
Harald Welte1c77c6e2010-05-03 21:37:11 +0200914 /* if a PTP-BVC is reset, and we don't know that
915 * PTP-BVCI yet, we should allocate a new peer */
916 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200917 "BVCI=%u via NSEI=%u\n", bvci, nsei);
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +0200918 from_peer = gbproxy_peer_alloc(cfg, bvci);
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200919 from_peer->nsei = nsei;
Harald Welte44c48302010-05-03 19:22:32 +0200920 }
Jacob Erlbeck64cb9242013-10-24 12:48:56 +0200921
922 if (!check_peer_nsei(from_peer, nsei))
923 from_peer->nsei = nsei;
924
Harald Welte1174c082010-05-12 00:07:29 +0200925 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
926 struct gprs_ra_id raid;
927 /* We have a Cell Identifier present in this
928 * PDU, this means we can extend our local
929 * state information about this particular cell
930 * */
931 memcpy(from_peer->ra,
932 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
933 sizeof(from_peer->ra));
934 gsm48_parse_ra(&raid, from_peer->ra);
935 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200936 "Cell ID %u-%u-%u-%u\n", nsei,
Harald Welte1174c082010-05-12 00:07:29 +0200937 bvci, raid.mcc, raid.mnc, raid.lac,
938 raid.rac);
939 }
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200940 if (cfg->route_to_sgsn2)
941 copy_to_sgsn2 = 1;
Harald Welte44c48302010-05-03 19:22:32 +0200942 }
943 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200944 }
945
Harald Weltee9ea2692010-05-11 20:20:13 +0200946 /* Normally, we can simply pass on all signalling messages from BSS to
947 * SGSN */
Jacob Erlbeck59300642014-09-08 09:04:01 +0200948 rc = gbprox_process_bssgp_ul(cfg, msg, from_peer);
949 if (!rc)
950 return 0;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200951
952 if (copy_to_sgsn2)
953 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
954
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200955 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200956err_no_peer:
Jacob Erlbeckc5085f92013-10-18 13:04:48 +0200957 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200958 nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200959 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200960 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200961err_mand_ie:
Harald Welte0a4050c2010-05-11 10:01:17 +0200962 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200963 nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200964 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200965 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200966}
967
968/* Receive paging request from SGSN, we need to relay to proper BSS */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200969static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200970 uint32_t nsei, uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200971{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200972 struct gbproxy_peer *peer = NULL;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200973 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte9f75c352010-04-30 20:26:32 +0200974
Harald Welte4cf12e92010-05-13 14:14:56 +0200975 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200976 nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200977 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200978 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte4cf12e92010-05-13 14:14:56 +0200979 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n",
980 bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200981 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte9f75c352010-04-30 20:26:32 +0200982 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200983 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200984 LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +0800985 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200986 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte9f75c352010-04-30 20:26:32 +0200987 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200988 peer = gbproxy_peer_by_lai(cfg, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200989 LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +0800990 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200991 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte9f75c352010-04-30 20:26:32 +0200992 } else
Harald Welte4cf12e92010-05-13 14:14:56 +0200993 LOGPC(DGPRS, LOGL_INFO, "\n");
994
995 if (!peer) {
996 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200997 "unable to route, missing IE\n", nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200998 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
Harald Welte9f75c352010-04-30 20:26:32 +0200999 return -EINVAL;
Harald Welte4cf12e92010-05-13 14:14:56 +02001000 }
1001 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001002}
1003
Harald Welte0a4050c2010-05-11 10:01:17 +02001004/* Receive an incoming BVC-RESET message from the SGSN */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001005static int rx_reset_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001006 struct msgb *orig_msg,
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001007 struct msgb *msg, struct tlv_parsed *tp,
1008 uint32_t nsei, uint16_t ns_bvci)
Harald Welte0a4050c2010-05-11 10:01:17 +02001009{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001010 struct gbproxy_peer *peer;
Harald Welte0a4050c2010-05-11 10:01:17 +02001011 uint16_t ptp_bvci;
1012
1013 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001014 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001015 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001016 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001017 NULL, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +02001018 }
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001019 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte0a4050c2010-05-11 10:01:17 +02001020
1021 if (ptp_bvci >= 2) {
1022 /* A reset for a PTP BVC was received, forward it to its
1023 * respective peer */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001024 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001025 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +02001026 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001027 nsei, ptp_bvci);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001028 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001029 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001030 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001031 &ptp_bvci, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +02001032 }
1033 return gbprox_relay2peer(msg, peer, ns_bvci);
1034 }
1035
1036 /* A reset for the Signalling entity has been received
1037 * from the SGSN. As the signalling BVCI is shared
1038 * among all the BSS's that we multiplex, it needs to
1039 * be relayed */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001040 llist_for_each_entry(peer, &cfg->bts_peers, list)
Harald Welte0a4050c2010-05-11 10:01:17 +02001041 gbprox_relay2peer(msg, peer, ns_bvci);
1042
1043 return 0;
1044}
1045
Harald Welte9f75c352010-04-30 20:26:32 +02001046/* Receive an incoming signalling message from the SGSN-side NS-VC */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001047static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001048 struct msgb *orig_msg, uint32_t nsei,
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001049 uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001050{
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001051 struct bssgp_normal_hdr *bgph =
1052 (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001053 struct tlv_parsed tp;
1054 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001055 int data_len;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001056 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +02001057 uint16_t bvci;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001058 struct msgb *msg;
Harald Welte9f75c352010-04-30 20:26:32 +02001059 int rc = 0;
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001060 int cause;
Harald Welte9f75c352010-04-30 20:26:32 +02001061
Harald Weltec471d3d2011-02-06 17:13:12 +01001062 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +02001063 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001064 "signalling\n", nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001065 /* FIXME: Send proper error message */
Harald Welte9f75c352010-04-30 20:26:32 +02001066 return -EINVAL;
1067 }
1068
1069 /* we actually should never see those two for BVCI == 0, but double-check
1070 * just to make sure */
1071 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1072 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +02001073 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001074 "signalling\n", nsei);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001075 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001076 }
1077
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001078 msg = gprs_msgb_copy(orig_msg, "rx_sig_from_sgsn");
1079 gbprox_process_bssgp_dl(cfg, msg, NULL);
1080 /* Update message info */
1081 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1082 data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +02001083 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1084
1085 switch (pdu_type) {
Harald Welte0a4050c2010-05-11 10:01:17 +02001086 case BSSGP_PDUT_BVC_RESET:
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001087 rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001088 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001089 case BSSGP_PDUT_BVC_RESET_ACK:
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +02001090 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
1091 break;
1092 /* fall through */
1093 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9f75c352010-04-30 20:26:32 +02001094 /* simple case: BVCI IE is mandatory */
1095 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1096 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001097 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001098 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001099 break;
1100 case BSSGP_PDUT_PAGING_PS:
1101 case BSSGP_PDUT_PAGING_CS:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001102 /* process the paging request (LAI/RAI lookup) */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001103 rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001104 break;
1105 case BSSGP_PDUT_STATUS:
Harald Welte0a4050c2010-05-11 10:01:17 +02001106 /* Some exception has occurred */
Harald Welte44c48302010-05-03 19:22:32 +02001107 LOGP(DGPRS, LOGL_NOTICE,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001108 "NSEI=%u(SGSN) BSSGP STATUS ", nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +02001109 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
1110 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1111 goto err_mand_ie;
1112 }
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001113 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
Harald Welte0a4050c2010-05-11 10:01:17 +02001114 LOGPC(DGPRS, LOGL_NOTICE,
1115 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001116 bssgp_cause_str(cause));
Harald Welte0a4050c2010-05-11 10:01:17 +02001117 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001118 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1119 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%u\n", bvci);
1120
1121 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI)
1122 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001123 } else
1124 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welte9f75c352010-04-30 20:26:32 +02001125 break;
1126 /* those only exist in the SGSN -> BSS direction */
1127 case BSSGP_PDUT_SUSPEND_ACK:
1128 case BSSGP_PDUT_SUSPEND_NACK:
1129 case BSSGP_PDUT_RESUME_ACK:
1130 case BSSGP_PDUT_RESUME_NACK:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001131 /* RAI IE is mandatory */
Harald Welte9f75c352010-04-30 20:26:32 +02001132 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1133 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001134 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte9f75c352010-04-30 20:26:32 +02001135 if (!peer)
1136 goto err_no_peer;
Harald Welte69619e32010-05-03 19:05:10 +02001137 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001138 break;
Harald Welte36f98d92011-02-06 13:09:29 +01001139 case BSSGP_PDUT_BVC_BLOCK_ACK:
1140 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1141 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1142 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001143 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte36f98d92011-02-06 13:09:29 +01001144 if (bvci == 0) {
1145 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001146 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
Harald Welte36f98d92011-02-06 13:09:29 +01001147 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
1148 /* should we send STATUS ? */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001149 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001150 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +01001151 } else {
1152 /* Mark BVC as (un)blocked */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001153 block_unblock_peer(cfg, bvci, pdu_type);
Harald Welte36f98d92011-02-06 13:09:29 +01001154 }
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001155 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +01001156 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001157 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte44c48302010-05-03 19:22:32 +02001158 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001159 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001160 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001161 ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001162 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001163 break;
1164 default:
Harald Welte72953b82010-05-12 00:20:41 +02001165 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n",
1166 pdu_type);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001167 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001168 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001169 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001170 break;
1171 }
1172
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001173 msgb_free(msg);
1174
Harald Welte9f75c352010-04-30 20:26:32 +02001175 return rc;
1176err_mand_ie:
Harald Welte1c77c6e2010-05-03 21:37:11 +02001177 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001178 nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001179 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001180 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001181 msgb_free(msg);
1182 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001183err_no_peer:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001184 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001185 nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001186 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001187 msgb_free(msg);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001188 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001189}
1190
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001191static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei)
1192{
1193 return nsei == cfg->nsip_sgsn_nsei ||
1194 (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei);
1195}
1196
Harald Welte9f75c352010-04-30 20:26:32 +02001197/* Main input function for Gb proxy */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001198int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei,
1199 uint16_t ns_bvci, uint16_t nsvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001200{
Harald Welte672f5c42010-05-03 18:54:58 +02001201 int rc;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001202 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001203
1204 /* Only BVCI=0 messages need special treatment */
1205 if (ns_bvci == 0 || ns_bvci == 1) {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001206 if (remote_end_is_sgsn)
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001207 rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001208 else
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001209 rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci);
Harald Welte672f5c42010-05-03 18:54:58 +02001210 } else {
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +02001211 /* All other BVCI are PTP */
1212 if (remote_end_is_sgsn)
1213 rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei, nsvci,
1214 ns_bvci);
1215 else
1216 rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei, nsvci,
1217 ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001218 }
1219
Harald Welte672f5c42010-05-03 18:54:58 +02001220 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +02001221}
Harald Welte85801d02010-05-11 05:49:43 +02001222
Harald Welte1ccbf442010-05-14 11:53:08 +00001223int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi)
1224{
1225 struct gprs_nsvc *nsvc;
1226
1227 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
1228 if (!nsvc->persistent)
1229 continue;
1230 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1231 }
1232 return 0;
1233}
1234
Harald Weltec1c1dd22010-05-11 06:34:24 +02001235/* Signal handler for signals from NS layer */
1236int gbprox_signal(unsigned int subsys, unsigned int signal,
1237 void *handler_data, void *signal_data)
1238{
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001239 struct gbproxy_config *cfg = handler_data;
Harald Weltec1c1dd22010-05-11 06:34:24 +02001240 struct ns_signal_data *nssd = signal_data;
1241 struct gprs_nsvc *nsvc = nssd->nsvc;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001242 struct gbproxy_peer *peer;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001243 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsvc->nsei);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001244
Harald Weltea6a20b42012-06-16 16:40:42 +08001245 if (subsys != SS_L_NS)
Harald Weltec1c1dd22010-05-11 06:34:24 +02001246 return 0;
1247
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001248 if (signal == S_NS_RESET && remote_end_is_sgsn) {
Harald Weltef69c0592010-05-11 18:29:44 +02001249 /* We have received a NS-RESET from the NSEI and NSVC
1250 * of the SGSN. This might happen with SGSN that start
1251 * their own NS-RESET procedure without waiting for our
1252 * NS-RESET */
1253 nsvc->remote_end_is_sgsn = 1;
1254 }
1255
Harald Welteb778d2c2010-05-12 13:28:25 +00001256 if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) {
1257 LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, "
1258 "re-starting RESET procedure\n");
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001259 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001260 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
Harald Weltee6599ee2012-06-17 12:25:53 +08001261 gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr,
1262 nsvc->nsei, nsvc->nsvci);
Harald Welteb778d2c2010-05-12 13:28:25 +00001263 }
1264
Harald Welte5e106d72011-02-06 16:33:29 +01001265 if (!nsvc->remote_end_is_sgsn) {
1266 /* from BSS to SGSN */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001267 peer = gbproxy_peer_by_nsei(cfg, nsvc->nsei);
Harald Welte5e106d72011-02-06 16:33:29 +01001268 if (!peer) {
1269 LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer "
1270 "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei,
1271 nsvc->nsvci);
1272 return 0;
1273 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001274 switch (signal) {
1275 case S_NS_RESET:
Harald Weltec1c1dd22010-05-11 06:34:24 +02001276 case S_NS_BLOCK:
Harald Welte5e106d72011-02-06 16:33:29 +01001277 if (!peer->blocked)
1278 break;
1279 LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from "
1280 "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n",
1281 nsvc->nsei, nsvc->nsvci);
1282 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei,
1283 peer->bvci, 0);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001284 break;
Harald Welte5e106d72011-02-06 16:33:29 +01001285 }
1286 } else {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001287 /* Forward this message to all NS-VC to BSS */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001288 struct gprs_ns_inst *nsi = cfg->nsi;
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001289 struct gprs_nsvc *next_nsvc;
1290
1291 llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) {
1292 if (next_nsvc->remote_end_is_sgsn)
1293 continue;
1294
1295 /* Note that the following does not start the full
1296 * procedures including timer based retransmissions. */
Harald Welte5e106d72011-02-06 16:33:29 +01001297 switch (signal) {
1298 case S_NS_RESET:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001299 gprs_ns_tx_reset(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001300 break;
1301 case S_NS_BLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001302 gprs_ns_tx_block(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001303 break;
1304 case S_NS_UNBLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001305 gprs_ns_tx_unblock(next_nsvc);
Harald Welte5e106d72011-02-06 16:33:29 +01001306 break;
1307 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001308 }
1309 }
1310 return 0;
1311}
1312
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001313void gbprox_reset(struct gbproxy_config *cfg)
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001314{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001315 struct gbproxy_peer *peer, *tmp;
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001316
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001317 llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list)
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +02001318 gbproxy_peer_free(peer);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001319
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001320 rate_ctr_group_free(cfg->ctrg);
1321 gbproxy_init_config(cfg);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001322}
1323
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001324int gbproxy_init_config(struct gbproxy_config *cfg)
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001325{
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001326 struct timespec tp;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001327 INIT_LLIST_HEAD(&cfg->bts_peers);
Holger Hans Peter Freythera7027a02014-08-04 11:19:56 +02001328 cfg->ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0);
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001329 clock_gettime(CLOCK_REALTIME, &tp);
1330 cfg->bss_ptmsi_state = tp.tv_sec + tp.tv_nsec;
1331 cfg->sgsn_tlli_state = tp.tv_sec - tp.tv_nsec;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001332 return 0;
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001333}