blob: ee80375658e057b1c0ffbf9c97a7096e0ba7129b [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 Erlbeck9114bee2014-08-19 12:21:01 +0200260 if (gbproxy_find_tlli_by_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;
312 int len_change = 0;
313
314 gprs_gb_parse_bssgp(msgb_bssgph(stored_msg),
315 msgb_bssgp_len(stored_msg),
316 &tmp_parse_ctx);
317 gbproxy_patch_bssgp(msg, msgb_bssgph(stored_msg),
318 msgb_bssgp_len(stored_msg),
319 peer, tlli_info, &len_change,
320 &tmp_parse_ctx);
321
322 gbproxy_update_tlli_state_after(peer, tlli_info, now,
323 &tmp_parse_ctx);
324
325 rc = gbprox_relay2sgsn(peer->cfg, stored_msg,
326 msgb_bvci(msg), sgsn_nsei);
327
328 if (rc < 0)
329 LOGP(DLLC, LOGL_ERROR,
330 "NSEI=%d(BSS) failed to send stored message "
331 "(%s)\n",
332 msgb_nsei(msg),
333 parse_ctx->llc_msg_name ?
334 parse_ctx->llc_msg_name : "BSSGP");
335 msgb_free(stored_msg);
336 }
337}
338
339static void gbproxy_acquire_imsi(struct gbproxy_peer *peer,
340 struct gbproxy_tlli_info* tlli_info,
341 uint16_t bvci)
342{
343 struct msgb *idreq_msg;
344
345 /* Send IDENT REQ */
346 idreq_msg = gsm48_msgb_alloc();
347 gprs_put_identity_req(idreq_msg, GSM_MI_TYPE_IMSI);
348 /* Workaround to avoid N(U) collisions and to enable a restart
349 * of the IMSI acquisition procedure. This will work unless the
350 * SGSN has an initial V(UT) within [256-32, 256+n_retries]
351 * (see GSM 04.64, 8.4.2).
352 * TODO: Check whether it is sensible to rely on this, it might
353 * be an issue when P-TMSI patching is _not_ enabled and the SGSN
354 * doesn't reset V(UT) after a detach. */
355 gprs_push_llc_ui(idreq_msg, 0, GPRS_SAPI_GMM,
356 (tlli_info->imsi_acq_retries + 256) % 512);
357 gprs_push_bssgp_dl_unitdata(idreq_msg, tlli_info->tlli.current);
358 gbprox_relay2peer(idreq_msg, peer, bvci);
359 msgb_free(idreq_msg);
360}
361
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200362static void gbproxy_tx_detach_acc(struct gbproxy_peer *peer,
363 struct gbproxy_tlli_info* tlli_info,
364 uint16_t bvci)
365{
366 struct msgb *detacc_msg;
367
368 /* Send DETACH ACC */
369 detacc_msg = gsm48_msgb_alloc();
370 gprs_put_mo_detach_acc(detacc_msg);
371 gprs_push_llc_ui(detacc_msg, 0, GPRS_SAPI_GMM,
372 (tlli_info->imsi_acq_retries + 256) % 512);
373 gprs_push_bssgp_dl_unitdata(detacc_msg, tlli_info->tlli.current);
374 gbprox_relay2peer(detacc_msg, peer, bvci);
375 msgb_free(detacc_msg);
376}
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200377
378/* Return != 0 iff msg still needs to be processed */
379static int gbproxy_imsi_acquisition(struct gbproxy_peer *peer,
380 struct msgb *msg,
381 uint16_t sgsn_nsei,
382 time_t now,
383 struct gbproxy_tlli_info* tlli_info,
384 struct gprs_gb_parse_context *parse_ctx)
385{
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200386 struct msgb *stored_msg;
387
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200388 if (!tlli_info)
389 return 1;
390
Jacob Erlbeck2fd1ba42014-09-11 14:57:03 +0200391 if (!tlli_info->imsi_acq_pending && tlli_info->imsi_len > 0)
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200392 return 1;
393
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200394 if (parse_ctx->g48_hdr)
395 switch (parse_ctx->g48_hdr->msg_type)
396 {
397 case GSM48_MT_GMM_ATTACH_REQ:
398 if (gbproxy_restart_imsi_acquisition(tlli_info)) {
399 LOGP(DLLC, LOGL_INFO,
400 "NSEI=%d(BSS) IMSI acquisition was in progress "
401 "when receiving an ATTACH_REQ.\n",
402 msgb_nsei(msg));
403 }
404 break;
405 case GSM48_MT_GMM_DETACH_REQ:
406 /* Nothing has been sent to the SGSN yet */
407 if (tlli_info->imsi_acq_pending) {
408 LOGP(DLLC, LOGL_INFO,
409 "NSEI=%d(BSS) IMSI acquisition was in progress "
410 "when receiving a DETACH_REQ.\n",
411 msgb_nsei(msg));
412 }
413 if (!parse_ctx->invalidate_tlli) {
414 LOGP(DLLC, LOGL_INFO,
415 "NSEI=%d(BSS) IMSI not yet acquired, "
416 "faking a DETACH_ACC.\n",
417 msgb_nsei(msg));
418 gbproxy_tx_detach_acc(peer, tlli_info, msgb_bvci(msg));
419 parse_ctx->invalidate_tlli = 1;
420 }
421 gbproxy_reset_imsi_acquisition(tlli_info);
422 gbproxy_update_tlli_state_after(peer, tlli_info, now,
423 parse_ctx);
424 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200425 }
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200426
Jacob Erlbeck2fd1ba42014-09-11 14:57:03 +0200427 if (tlli_info->imsi_acq_pending && tlli_info->imsi_len > 0) {
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200428 int is_ident_resp =
429 parse_ctx->g48_hdr &&
430 parse_ctx->g48_hdr->proto_discr == GSM48_PDISC_MM_GPRS &&
431 parse_ctx->g48_hdr->msg_type == GSM48_MT_GMM_ID_RESP;
432
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200433 /* The IMSI is now available */
434 gbproxy_flush_stored_messages(peer, msg, sgsn_nsei, now,
435 tlli_info, parse_ctx);
436
437 gbproxy_reset_imsi_acquisition(tlli_info);
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200438
439 /* This message is most probably the response to the ident
440 * request sent by gbproxy_acquire_imsi(). Don't forward it to
441 * the SGSN. */
442 return !is_ident_resp;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200443 }
444
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200445 /* The message cannot be processed since the IMSI is still missing */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200446
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200447 /* Enqueue unpatched messages */
448 LOGP(DLLC, LOGL_INFO,
449 "NSEI=%d(BSS) IMSI acquisition in progress, "
450 "storing message (%s)\n",
451 msgb_nsei(msg),
452 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200453
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200454 stored_msg = gprs_msgb_copy(msg, "process_bssgp_ul");
455 msgb_enqueue(&tlli_info->stored_msgs, stored_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200456
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200457 if (!tlli_info->imsi_acq_pending) {
458 LOGP(DLLC, LOGL_INFO,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200459 "NSEI=%d(BSS) IMSI is required but not available, "
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200460 "initiating identification procedure (%s)\n",
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200461 msgb_nsei(msg),
462 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
463
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200464 gbproxy_acquire_imsi(peer, tlli_info, msgb_bvci(msg));
465
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200466 /* There is no explicit retransmission handling, the
467 * implementation relies on the MS doing proper retransmissions
468 * of the triggering message instead */
469
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200470 tlli_info->imsi_acq_pending = 1;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200471 }
472
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200473 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200474}
475
476struct gbproxy_peer *gbproxy_find_peer(struct gbproxy_config *cfg,
477 struct msgb *msg,
478 struct gprs_gb_parse_context *parse_ctx)
479{
480 struct gbproxy_peer *peer = NULL;
481
482 if (msgb_bvci(msg) >= 2)
483 peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg));
484
485 if (!peer && !parse_ctx->to_bss)
486 peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg));
487
488 if (!peer)
489 peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx->bssgp_tp);
490
491 if (!peer) {
492 LOGP(DLLC, LOGL_INFO,
493 "NSEI=%d(%s) patching: didn't find peer for message, "
494 "PDU %d\n",
495 msgb_nsei(msg), parse_ctx->to_bss ? "BSS" : "SGSN",
496 parse_ctx->pdu_type);
497 /* Increment counter */
498 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]);
499 }
500 return peer;
501}
502
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200503/* patch BSSGP message */
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200504static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg,
505 struct msgb *msg,
506 struct gbproxy_peer *peer)
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200507{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200508 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200509 int rc;
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200510 int len_change = 0;
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200511 time_t now;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200512 struct gbproxy_tlli_info *tlli_info = NULL;
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200513 uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei;
514 int send_msg_directly = 0;
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;
521
522 /* Parse BSSGP/LLC */
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200523 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
524 &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200525
526 if (!rc) {
527 if (!parse_ctx.need_decryption) {
528 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeck643d5222014-08-18 17:26:04 +0200529 "NSEI=%u(BSS) patching: "
530 "failed to parse BSSGP/GMM message\n",
531 msgb_nsei(msg));
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200532 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200533 }
534 }
535
536 /* Get peer */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200537 if (!peer)
538 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200539
540 if (!peer)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200541 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200542
543 now = time(NULL);
544
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200545 gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer,
546 parse_ctx.llc_msg_name);
547
Jacob Erlbeck31591142014-09-03 11:59:48 +0200548 gprs_gb_log_parse_context(&parse_ctx, "BSSGP");
549
550 tlli_info = gbproxy_update_tlli_state_ul(peer, now, &parse_ctx);
551
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200552 if (parse_ctx.g48_hdr) {
553 switch (parse_ctx.g48_hdr->msg_type) {
554 case GSM48_MT_GMM_ATTACH_REQ:
555 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]);
556 break;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200557
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200558 default:
559 break;
560 }
561 }
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200562
Jacob Erlbeck18a37872014-09-08 09:59:16 +0200563 if (tlli_info && cfg->route_to_sgsn2 && gbproxy_check_tlli(peer, tlli_info)) {
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200564 sgsn_nsei = cfg->nsip_sgsn2_nsei;
565 send_msg_directly = 1;
566 }
567
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200568 /* Handle IMSI acquisition */
569 if (cfg->acquire_imsi) {
570 rc = gbproxy_imsi_acquisition(peer, msg, sgsn_nsei, now,
571 tlli_info, &parse_ctx);
572 if (rc <= 0)
573 return rc;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200574 }
575
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200576 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
577 peer, tlli_info, &len_change, &parse_ctx);
578
579 gbproxy_update_tlli_state_after(peer, tlli_info, now, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200580
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200581 if (send_msg_directly) {
582 /* Send message directly to the selected SGSN */
583 rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei);
584 /* Don't let the calling code handle the transmission */
585 return 0;
586 }
587
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200588 return 1;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200589}
590
591/* patch BSSGP message to use core_mcc/mnc on the SGSN side */
592static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg,
593 struct msgb *msg,
594 struct gbproxy_peer *peer)
595{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200596 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200597 int rc;
598 int len_change = 0;
599 time_t now;
600 struct gbproxy_tlli_info *tlli_info = NULL;
601
Jacob Erlbeck1abfdc22014-09-04 11:42:08 +0200602 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
603 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
604 return;
605
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200606 parse_ctx.to_bss = 1;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200607
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200608 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
609 &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200610
611 if (!rc) {
612 if (!parse_ctx.need_decryption) {
613 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeck643d5222014-08-18 17:26:04 +0200614 "NSEI=%u(SGSN) patching: "
615 "failed to parse BSSGP/GMM message\n",
616 msgb_nsei(msg));
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200617 return;
618 }
619 }
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200620
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200621 /* Get peer */
622 if (!peer)
623 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200624
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200625 if (!peer)
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200626 return;
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200627
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200628 now = time(NULL);
629
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200630 if (parse_ctx.g48_hdr) {
631 switch (parse_ctx.g48_hdr->msg_type) {
632 case GSM48_MT_GMM_ATTACH_REJ:
633 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]);
634 break;
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200635
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200636 default:
637 break;
638 }
639 }
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200640
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200641 gprs_gb_log_parse_context(&parse_ctx, "BSSGP");
642
643 tlli_info = gbproxy_update_tlli_state_dl(peer, now, &parse_ctx);
644
645 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
646 peer, tlli_info, &len_change, &parse_ctx);
647
648 gbproxy_update_tlli_state_after(peer, tlli_info, now, &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200649
Jacob Erlbeck03551e52014-06-27 19:56:05 +0200650 return;
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200651}
652
Harald Welte9f75c352010-04-30 20:26:32 +0200653/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200654static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200655 uint16_t ns_bvci, uint16_t sgsn_nsei)
Harald Welte672f5c42010-05-03 18:54:58 +0200656{
Harald Welte39d0bb52010-05-12 18:10:25 +0000657 /* create a copy of the message so the old one can
658 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200659 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2sgsn");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200660 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000661
Harald Weltee9ea2692010-05-11 20:20:13 +0200662 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200663 msgb_nsei(msg), ns_bvci, sgsn_nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200664
Harald Welte672f5c42010-05-03 18:54:58 +0200665 msgb_bvci(msg) = ns_bvci;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200666 msgb_nsei(msg) = sgsn_nsei;
Harald Welte672f5c42010-05-03 18:54:58 +0200667
Harald Welte69619e32010-05-03 19:05:10 +0200668 strip_ns_hdr(msg);
669
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200670 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
671 if (rc < 0)
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200672 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200673
674 return rc;
Harald Welte672f5c42010-05-03 18:54:58 +0200675}
676
Harald Welte672f5c42010-05-03 18:54:58 +0200677/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200678static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
Harald Welte9f75c352010-04-30 20:26:32 +0200679 uint16_t ns_bvci)
680{
Harald Welte39d0bb52010-05-12 18:10:25 +0000681 /* create a copy of the message so the old one can
682 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200683 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2peer");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200684 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000685
Harald Welte0ab535b2010-05-13 10:34:56 +0200686 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200687 msgb_nsei(msg), ns_bvci, peer->nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200688
Harald Welte9f75c352010-04-30 20:26:32 +0200689 msgb_bvci(msg) = ns_bvci;
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200690 msgb_nsei(msg) = peer->nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200691
Harald Welte0ab535b2010-05-13 10:34:56 +0200692 /* Strip the old NS header, it will be replaced with a new one */
Harald Welte69619e32010-05-03 19:05:10 +0200693 strip_ns_hdr(msg);
694
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200695 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
696 if (rc < 0)
697 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
698
699 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200700}
701
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200702static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type)
Harald Welte36f98d92011-02-06 13:09:29 +0100703{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200704 struct gbproxy_peer *peer;
Harald Welte36f98d92011-02-06 13:09:29 +0100705
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200706 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +0100707 if (!peer) {
708 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
709 ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200710 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +0100711 return -ENOENT;
712 }
713
714 switch (pdu_type) {
715 case BSSGP_PDUT_BVC_BLOCK_ACK:
716 peer->blocked = 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200717 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100718 break;
719 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
720 peer->blocked = 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200721 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100722 break;
723 default:
724 break;
725 }
726 return 0;
727}
728
Harald Welte9f75c352010-04-30 20:26:32 +0200729/* Send a message to a peer identified by ptp_bvci but using ns_bvci
730 * in the NS hdr */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200731static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci,
Harald Welte9f75c352010-04-30 20:26:32 +0200732 uint16_t ns_bvci)
733{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200734 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +0200735
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200736 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200737 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200738 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
Harald Welte1c77c6e2010-05-03 21:37:11 +0200739 ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200740 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte9f75c352010-04-30 20:26:32 +0200741 return -ENOENT;
Harald Welte1c77c6e2010-05-03 21:37:11 +0200742 }
Harald Welte9f75c352010-04-30 20:26:32 +0200743
Harald Welte69619e32010-05-03 19:05:10 +0200744 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200745}
746
Harald Welteb1fd9022012-06-17 12:16:31 +0800747int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
748{
749 return 0;
750}
751
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200752/* Receive an incoming PTP message from a BSS-side NS-VC */
753static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg,
754 struct msgb *msg, uint16_t nsei,
755 uint16_t nsvci, uint16_t ns_bvci)
756{
757 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200758 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
759 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200760 int rc;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200761
762 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
763
764 if (peer)
765 check_peer_nsei(peer, nsei);
766
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200767 rc = gbprox_process_bssgp_ul(cfg, msg, peer);
768 if (!rc)
769 return 0;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200770
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200771 switch (pdu_type) {
772 case BSSGP_PDUT_FLOW_CONTROL_BVC:
773 if (!cfg->route_to_sgsn2)
774 break;
775
776 /* Send a copy to the secondary SGSN */
777 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
778 break;
779 default:
780 break;
781 }
782
783
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200784 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200785}
786
787/* Receive an incoming PTP message from a SGSN-side NS-VC */
788static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg,
789 struct msgb *msg, uint16_t nsei,
790 uint16_t nsvci, uint16_t ns_bvci)
791{
792 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200793 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
794 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200795
796 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
797
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200798 /* Send status messages before patching */
799
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200800 if (!peer) {
801 LOGP(DGPRS, LOGL_INFO, "Didn't find peer for "
802 "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n",
803 ns_bvci, nsvci, nsei);
804 rate_ctr_inc(&cfg->ctrg->
805 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
806 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
807 &ns_bvci, msg);
808 }
809
810 if (peer->blocked) {
811 LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for "
812 "blocked BVCI=%u via NSVC=%u/NSEI=%u\n",
813 ns_bvci, nsvci, nsei);
814 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200815 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200816 }
817
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200818 switch (pdu_type) {
819 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
820 case BSSGP_PDUT_BVC_BLOCK_ACK:
821 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
822 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
823 /* Hide ACKs from the secondary SGSN, the primary SGSN
824 * is responsible to send them. */
825 return 0;
826 break;
827 default:
828 break;
829 }
830
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200831 /* Optionally patch the message */
832 gbprox_process_bssgp_dl(cfg, msg, peer);
833
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200834 return gbprox_relay2peer(msg, peer, ns_bvci);
835}
836
Harald Welte9f75c352010-04-30 20:26:32 +0200837/* Receive an incoming signalling message from a BSS-side NS-VC */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200838static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg,
839 struct msgb *msg, uint16_t nsei,
Harald Welte9f75c352010-04-30 20:26:32 +0200840 uint16_t ns_bvci)
841{
Harald Welteca3620a2010-05-03 16:30:59 +0200842 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200843 struct tlv_parsed tp;
844 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200845 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200846 struct gbproxy_peer *from_peer = NULL;
Harald Welte70f38d22010-05-01 12:10:57 +0200847 struct gprs_ra_id raid;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200848 int copy_to_sgsn2 = 0;
Jacob Erlbeck59300642014-09-08 09:04:01 +0200849 int rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200850
Harald Weltec471d3d2011-02-06 17:13:12 +0100851 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200852 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200853 nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200854 return -EINVAL;
855 }
856
857 /* we actually should never see those two for BVCI == 0, but double-check
858 * just to make sure */
859 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
860 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200861 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200862 "signalling\n", nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200863 return -EINVAL;
864 }
865
866 bssgp_tlv_parse(&tp, bgph->data, data_len);
867
868 switch (pdu_type) {
869 case BSSGP_PDUT_SUSPEND:
870 case BSSGP_PDUT_RESUME:
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200871 /* We implement RAI snooping during SUSPEND/RESUME, since it
872 * establishes a relationsip between BVCI/peer and the routeing
873 * area identification. The snooped information is then used
874 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
875 * BSSGP */
Harald Welte9f75c352010-04-30 20:26:32 +0200876 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
877 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200878 from_peer = gbproxy_peer_by_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200879 if (!from_peer)
880 goto err_no_peer;
Harald Welte1174c082010-05-12 00:07:29 +0200881 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
882 sizeof(from_peer->ra));
Harald Welte7fc98222010-05-11 10:15:42 +0200883 gsm48_parse_ra(&raid, from_peer->ra);
Harald Welte4cf12e92010-05-13 14:14:56 +0200884 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200885 "RAI snooping: RAI %u-%u-%u-%u behind BVCI=%u\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200886 nsei, raid.mcc, raid.mnc, raid.lac,
887 raid.rac , from_peer->bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200888 /* FIXME: This only supports one BSS per RA */
889 break;
Harald Welte44c48302010-05-03 19:22:32 +0200890 case BSSGP_PDUT_BVC_RESET:
891 /* If we receive a BVC reset on the signalling endpoint, we
892 * don't want the SGSN to reset, as the signalling endpoint
893 * is common for all point-to-point BVCs (and thus all BTS) */
894 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200895 uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte72953b82010-05-12 00:20:41 +0200896 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200897 nsei, bvci);
Harald Welte44c48302010-05-03 19:22:32 +0200898 if (bvci == 0) {
899 /* FIXME: only do this if SGSN is alive! */
Harald Weltee9ea2692010-05-11 20:20:13 +0200900 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200901 "BVC RESET ACK of BVCI=0\n", nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200902 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200903 nsei, 0, ns_bvci);
Harald Welte1174c082010-05-12 00:07:29 +0200904 }
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200905 from_peer = gbproxy_peer_by_bvci(cfg, bvci);
Harald Welte1174c082010-05-12 00:07:29 +0200906 if (!from_peer) {
Harald Welte1c77c6e2010-05-03 21:37:11 +0200907 /* if a PTP-BVC is reset, and we don't know that
908 * PTP-BVCI yet, we should allocate a new peer */
909 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200910 "BVCI=%u via NSEI=%u\n", bvci, nsei);
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +0200911 from_peer = gbproxy_peer_alloc(cfg, bvci);
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200912 from_peer->nsei = nsei;
Harald Welte44c48302010-05-03 19:22:32 +0200913 }
Jacob Erlbeck64cb9242013-10-24 12:48:56 +0200914
915 if (!check_peer_nsei(from_peer, nsei))
916 from_peer->nsei = nsei;
917
Harald Welte1174c082010-05-12 00:07:29 +0200918 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
919 struct gprs_ra_id raid;
920 /* We have a Cell Identifier present in this
921 * PDU, this means we can extend our local
922 * state information about this particular cell
923 * */
924 memcpy(from_peer->ra,
925 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
926 sizeof(from_peer->ra));
927 gsm48_parse_ra(&raid, from_peer->ra);
928 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200929 "Cell ID %u-%u-%u-%u\n", nsei,
Harald Welte1174c082010-05-12 00:07:29 +0200930 bvci, raid.mcc, raid.mnc, raid.lac,
931 raid.rac);
932 }
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200933 if (cfg->route_to_sgsn2)
934 copy_to_sgsn2 = 1;
Harald Welte44c48302010-05-03 19:22:32 +0200935 }
936 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200937 }
938
Harald Weltee9ea2692010-05-11 20:20:13 +0200939 /* Normally, we can simply pass on all signalling messages from BSS to
940 * SGSN */
Jacob Erlbeck59300642014-09-08 09:04:01 +0200941 rc = gbprox_process_bssgp_ul(cfg, msg, from_peer);
942 if (!rc)
943 return 0;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200944
945 if (copy_to_sgsn2)
946 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
947
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200948 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200949err_no_peer:
Jacob Erlbeckc5085f92013-10-18 13:04:48 +0200950 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200951 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200952 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200953 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200954err_mand_ie:
Harald Welte0a4050c2010-05-11 10:01:17 +0200955 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200956 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200957 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200958 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200959}
960
961/* Receive paging request from SGSN, we need to relay to proper BSS */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200962static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200963 uint32_t nsei, uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200964{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200965 struct gbproxy_peer *peer = NULL;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200966 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte9f75c352010-04-30 20:26:32 +0200967
Harald Welte4cf12e92010-05-13 14:14:56 +0200968 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200969 nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200970 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200971 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte4cf12e92010-05-13 14:14:56 +0200972 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n",
973 bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200974 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte9f75c352010-04-30 20:26:32 +0200975 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200976 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200977 LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +0800978 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200979 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte9f75c352010-04-30 20:26:32 +0200980 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200981 peer = gbproxy_peer_by_lai(cfg, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200982 LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +0800983 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200984 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte9f75c352010-04-30 20:26:32 +0200985 } else
Harald Welte4cf12e92010-05-13 14:14:56 +0200986 LOGPC(DGPRS, LOGL_INFO, "\n");
987
988 if (!peer) {
989 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200990 "unable to route, missing IE\n", nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200991 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
Harald Welte9f75c352010-04-30 20:26:32 +0200992 return -EINVAL;
Harald Welte4cf12e92010-05-13 14:14:56 +0200993 }
994 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200995}
996
Harald Welte0a4050c2010-05-11 10:01:17 +0200997/* Receive an incoming BVC-RESET message from the SGSN */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200998static int rx_reset_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200999 struct msgb *orig_msg,
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001000 struct msgb *msg, struct tlv_parsed *tp,
1001 uint32_t nsei, uint16_t ns_bvci)
Harald Welte0a4050c2010-05-11 10:01:17 +02001002{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001003 struct gbproxy_peer *peer;
Harald Welte0a4050c2010-05-11 10:01:17 +02001004 uint16_t ptp_bvci;
1005
1006 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001007 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001008 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001009 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001010 NULL, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +02001011 }
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001012 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte0a4050c2010-05-11 10:01:17 +02001013
1014 if (ptp_bvci >= 2) {
1015 /* A reset for a PTP BVC was received, forward it to its
1016 * respective peer */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001017 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001018 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +02001019 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001020 nsei, ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001021 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001022 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001023 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001024 &ptp_bvci, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +02001025 }
1026 return gbprox_relay2peer(msg, peer, ns_bvci);
1027 }
1028
1029 /* A reset for the Signalling entity has been received
1030 * from the SGSN. As the signalling BVCI is shared
1031 * among all the BSS's that we multiplex, it needs to
1032 * be relayed */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001033 llist_for_each_entry(peer, &cfg->bts_peers, list)
Harald Welte0a4050c2010-05-11 10:01:17 +02001034 gbprox_relay2peer(msg, peer, ns_bvci);
1035
1036 return 0;
1037}
1038
Harald Welte9f75c352010-04-30 20:26:32 +02001039/* Receive an incoming signalling message from the SGSN-side NS-VC */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001040static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001041 struct msgb *orig_msg, uint32_t nsei,
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001042 uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001043{
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001044 struct bssgp_normal_hdr *bgph =
1045 (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001046 struct tlv_parsed tp;
1047 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001048 int data_len;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001049 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +02001050 uint16_t bvci;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001051 struct msgb *msg;
Harald Welte9f75c352010-04-30 20:26:32 +02001052 int rc = 0;
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001053 int cause;
Harald Welte9f75c352010-04-30 20:26:32 +02001054
Harald Weltec471d3d2011-02-06 17:13:12 +01001055 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +02001056 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001057 "signalling\n", nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001058 /* FIXME: Send proper error message */
Harald Welte9f75c352010-04-30 20:26:32 +02001059 return -EINVAL;
1060 }
1061
1062 /* we actually should never see those two for BVCI == 0, but double-check
1063 * just to make sure */
1064 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1065 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +02001066 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001067 "signalling\n", nsei);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001068 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001069 }
1070
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001071 msg = gprs_msgb_copy(orig_msg, "rx_sig_from_sgsn");
1072 gbprox_process_bssgp_dl(cfg, msg, NULL);
1073 /* Update message info */
1074 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1075 data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +02001076 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1077
1078 switch (pdu_type) {
Harald Welte0a4050c2010-05-11 10:01:17 +02001079 case BSSGP_PDUT_BVC_RESET:
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001080 rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001081 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001082 case BSSGP_PDUT_BVC_RESET_ACK:
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +02001083 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
1084 break;
1085 /* fall through */
1086 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9f75c352010-04-30 20:26:32 +02001087 /* simple case: BVCI IE is mandatory */
1088 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1089 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001090 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001091 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001092 break;
1093 case BSSGP_PDUT_PAGING_PS:
1094 case BSSGP_PDUT_PAGING_CS:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001095 /* process the paging request (LAI/RAI lookup) */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001096 rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001097 break;
1098 case BSSGP_PDUT_STATUS:
Harald Welte0a4050c2010-05-11 10:01:17 +02001099 /* Some exception has occurred */
Harald Welte44c48302010-05-03 19:22:32 +02001100 LOGP(DGPRS, LOGL_NOTICE,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001101 "NSEI=%u(SGSN) BSSGP STATUS ", nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +02001102 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
1103 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1104 goto err_mand_ie;
1105 }
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001106 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
Harald Welte0a4050c2010-05-11 10:01:17 +02001107 LOGPC(DGPRS, LOGL_NOTICE,
1108 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001109 bssgp_cause_str(cause));
Harald Welte0a4050c2010-05-11 10:01:17 +02001110 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001111 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1112 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%u\n", bvci);
1113
1114 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI)
1115 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001116 } else
1117 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welte9f75c352010-04-30 20:26:32 +02001118 break;
1119 /* those only exist in the SGSN -> BSS direction */
1120 case BSSGP_PDUT_SUSPEND_ACK:
1121 case BSSGP_PDUT_SUSPEND_NACK:
1122 case BSSGP_PDUT_RESUME_ACK:
1123 case BSSGP_PDUT_RESUME_NACK:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001124 /* RAI IE is mandatory */
Harald Welte9f75c352010-04-30 20:26:32 +02001125 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1126 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001127 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte9f75c352010-04-30 20:26:32 +02001128 if (!peer)
1129 goto err_no_peer;
Harald Welte69619e32010-05-03 19:05:10 +02001130 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001131 break;
Harald Welte36f98d92011-02-06 13:09:29 +01001132 case BSSGP_PDUT_BVC_BLOCK_ACK:
1133 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1134 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1135 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001136 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte36f98d92011-02-06 13:09:29 +01001137 if (bvci == 0) {
1138 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001139 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
Harald Welte36f98d92011-02-06 13:09:29 +01001140 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
1141 /* should we send STATUS ? */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001142 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001143 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +01001144 } else {
1145 /* Mark BVC as (un)blocked */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001146 block_unblock_peer(cfg, bvci, pdu_type);
Harald Welte36f98d92011-02-06 13:09:29 +01001147 }
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001148 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +01001149 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001150 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte44c48302010-05-03 19:22:32 +02001151 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001152 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001153 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001154 ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001155 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001156 break;
1157 default:
Harald Welte72953b82010-05-12 00:20:41 +02001158 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n",
1159 pdu_type);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001160 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001161 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001162 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001163 break;
1164 }
1165
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001166 msgb_free(msg);
1167
Harald Welte9f75c352010-04-30 20:26:32 +02001168 return rc;
1169err_mand_ie:
Harald Welte1c77c6e2010-05-03 21:37:11 +02001170 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001171 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001172 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001173 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001174 msgb_free(msg);
1175 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001176err_no_peer:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001177 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001178 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001179 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001180 msgb_free(msg);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001181 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001182}
1183
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001184static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei)
1185{
1186 return nsei == cfg->nsip_sgsn_nsei ||
1187 (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei);
1188}
1189
Harald Welte9f75c352010-04-30 20:26:32 +02001190/* Main input function for Gb proxy */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001191int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei,
1192 uint16_t ns_bvci, uint16_t nsvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001193{
Harald Welte672f5c42010-05-03 18:54:58 +02001194 int rc;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001195 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001196
1197 /* Only BVCI=0 messages need special treatment */
1198 if (ns_bvci == 0 || ns_bvci == 1) {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001199 if (remote_end_is_sgsn)
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001200 rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001201 else
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001202 rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci);
Harald Welte672f5c42010-05-03 18:54:58 +02001203 } else {
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +02001204 /* All other BVCI are PTP */
1205 if (remote_end_is_sgsn)
1206 rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei, nsvci,
1207 ns_bvci);
1208 else
1209 rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei, nsvci,
1210 ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001211 }
1212
Harald Welte672f5c42010-05-03 18:54:58 +02001213 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +02001214}
Harald Welte85801d02010-05-11 05:49:43 +02001215
Harald Welte1ccbf442010-05-14 11:53:08 +00001216int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi)
1217{
1218 struct gprs_nsvc *nsvc;
1219
1220 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
1221 if (!nsvc->persistent)
1222 continue;
1223 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1224 }
1225 return 0;
1226}
1227
Harald Weltec1c1dd22010-05-11 06:34:24 +02001228/* Signal handler for signals from NS layer */
1229int gbprox_signal(unsigned int subsys, unsigned int signal,
1230 void *handler_data, void *signal_data)
1231{
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001232 struct gbproxy_config *cfg = handler_data;
Harald Weltec1c1dd22010-05-11 06:34:24 +02001233 struct ns_signal_data *nssd = signal_data;
1234 struct gprs_nsvc *nsvc = nssd->nsvc;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001235 struct gbproxy_peer *peer;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001236 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsvc->nsei);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001237
Harald Weltea6a20b42012-06-16 16:40:42 +08001238 if (subsys != SS_L_NS)
Harald Weltec1c1dd22010-05-11 06:34:24 +02001239 return 0;
1240
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001241 if (signal == S_NS_RESET && remote_end_is_sgsn) {
Harald Weltef69c0592010-05-11 18:29:44 +02001242 /* We have received a NS-RESET from the NSEI and NSVC
1243 * of the SGSN. This might happen with SGSN that start
1244 * their own NS-RESET procedure without waiting for our
1245 * NS-RESET */
1246 nsvc->remote_end_is_sgsn = 1;
1247 }
1248
Harald Welteb778d2c2010-05-12 13:28:25 +00001249 if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) {
1250 LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, "
1251 "re-starting RESET procedure\n");
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001252 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001253 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
Harald Weltee6599ee2012-06-17 12:25:53 +08001254 gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr,
1255 nsvc->nsei, nsvc->nsvci);
Harald Welteb778d2c2010-05-12 13:28:25 +00001256 }
1257
Harald Welte5e106d72011-02-06 16:33:29 +01001258 if (!nsvc->remote_end_is_sgsn) {
1259 /* from BSS to SGSN */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001260 peer = gbproxy_peer_by_nsei(cfg, nsvc->nsei);
Harald Welte5e106d72011-02-06 16:33:29 +01001261 if (!peer) {
1262 LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer "
1263 "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei,
1264 nsvc->nsvci);
1265 return 0;
1266 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001267 switch (signal) {
1268 case S_NS_RESET:
Harald Weltec1c1dd22010-05-11 06:34:24 +02001269 case S_NS_BLOCK:
Harald Welte5e106d72011-02-06 16:33:29 +01001270 if (!peer->blocked)
1271 break;
1272 LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from "
1273 "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n",
1274 nsvc->nsei, nsvc->nsvci);
1275 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei,
1276 peer->bvci, 0);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001277 break;
Harald Welte5e106d72011-02-06 16:33:29 +01001278 }
1279 } else {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001280 /* Forward this message to all NS-VC to BSS */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001281 struct gprs_ns_inst *nsi = cfg->nsi;
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001282 struct gprs_nsvc *next_nsvc;
1283
1284 llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) {
1285 if (next_nsvc->remote_end_is_sgsn)
1286 continue;
1287
1288 /* Note that the following does not start the full
1289 * procedures including timer based retransmissions. */
Harald Welte5e106d72011-02-06 16:33:29 +01001290 switch (signal) {
1291 case S_NS_RESET:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001292 gprs_ns_tx_reset(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001293 break;
1294 case S_NS_BLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001295 gprs_ns_tx_block(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001296 break;
1297 case S_NS_UNBLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001298 gprs_ns_tx_unblock(next_nsvc);
Harald Welte5e106d72011-02-06 16:33:29 +01001299 break;
1300 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001301 }
1302 }
1303 return 0;
1304}
1305
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001306void gbprox_reset(struct gbproxy_config *cfg)
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001307{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001308 struct gbproxy_peer *peer, *tmp;
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001309
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001310 llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list)
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +02001311 gbproxy_peer_free(peer);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001312
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001313 rate_ctr_group_free(cfg->ctrg);
1314 gbproxy_init_config(cfg);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001315}
1316
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001317int gbproxy_init_config(struct gbproxy_config *cfg)
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001318{
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001319 struct timespec tp;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001320 INIT_LLIST_HEAD(&cfg->bts_peers);
Holger Hans Peter Freythera7027a02014-08-04 11:19:56 +02001321 cfg->ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0);
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001322 clock_gettime(CLOCK_REALTIME, &tp);
1323 cfg->bss_ptmsi_state = tp.tv_sec + tp.tv_nsec;
1324 cfg->sgsn_tlli_state = tp.tv_sec - tp.tv_nsec;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001325 return 0;
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001326}