blob: 130ab4ee11e0de42d2b96861e9cd152eba022883 [file] [log] [blame]
Harald Welte9f75c352010-04-30 20:26:32 +02001/* NS-over-IP proxy */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther90267a92013-10-23 11:24:17 +02004 * (C) 2010-2013 by On-Waves
5 * (C) 2013 by Holger Hans Peter Freyther
Harald Welte9f75c352010-04-30 20:26:32 +02006 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9f75c352010-04-30 20:26:32 +020011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Welte9f75c352010-04-30 20:26:32 +020017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9f75c352010-04-30 20:26:32 +020020 *
21 */
22
23#include <unistd.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <getopt.h>
28#include <errno.h>
29#include <sys/fcntl.h>
30#include <sys/stat.h>
Harald Welte7fc98222010-05-11 10:15:42 +020031#include <arpa/inet.h>
Jacob Erlbeck7c101d92014-06-06 18:49:23 +020032#include <time.h>
Harald Welte9f75c352010-04-30 20:26:32 +020033
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010034#include <osmocom/core/talloc.h>
35#include <osmocom/core/select.h>
Jacob Erlbeckbc555742013-10-18 14:34:55 +020036#include <osmocom/core/rate_ctr.h>
37
Harald Welteea34a4e2012-06-16 14:59:56 +080038#include <osmocom/gprs/gprs_ns.h>
39#include <osmocom/gprs/gprs_bssgp.h>
40
Jacob Erlbeck7c101d92014-06-06 18:49:23 +020041#include <osmocom/gsm/gsm_utils.h>
42
Harald Welte9f75c352010-04-30 20:26:32 +020043#include <openbsc/signal.h>
44#include <openbsc/debug.h>
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020045#include <openbsc/gprs_gb_parse.h>
Harald Welte672f5c42010-05-03 18:54:58 +020046#include <openbsc/gb_proxy.h>
Harald Welte9f75c352010-04-30 20:26:32 +020047
Jacob Erlbeck67a44452014-05-19 10:14:58 +020048#include <openbsc/gprs_llc.h>
49#include <openbsc/gsm_04_08.h>
50#include <openbsc/gsm_04_08_gprs.h>
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +020051#include <openbsc/gprs_utils.h>
Jacob Erlbeck67a44452014-05-19 10:14:58 +020052
Jacob Erlbeckbc555742013-10-18 14:34:55 +020053static const struct rate_ctr_desc global_ctr_description[] = {
54 { "inv-bvci", "Invalid BVC Identifier " },
Jacob Erlbeck8f503592014-06-02 10:49:00 +020055 { "inv-lai", "Invalid Location Area Identifier" },
56 { "inv-rai", "Invalid Routing Area Identifier " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020057 { "inv-nsei", "No BVC established for NSEI " },
58 { "proto-err.bss", "BSSGP protocol error (BSS )" },
59 { "proto-err.sgsn", "BSSGP protocol error (SGSN)" },
60 { "not-supp.bss", "Feature not supported (BSS )" },
61 { "not-supp.sgsn", "Feature not supported (SGSN)" },
62 { "restart.sgsn", "Restarted RESET procedure (SGSN)" },
63 { "tx-err.sgsn", "NS Transmission error (SGSN)" },
64 { "error", "Other error " },
Jacob Erlbeck006c0382014-05-27 13:49:04 +020065 { "mod-peer-err", "Patch error: no peer " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020066};
67
68static const struct rate_ctr_group_desc global_ctrg_desc = {
69 .group_name_prefix = "gbproxy.global",
70 .group_description = "GBProxy Global Statistics",
71 .num_ctr = ARRAY_SIZE(global_ctr_description),
72 .ctr_desc = global_ctr_description,
73};
74
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020075static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
76 uint16_t ns_bvci);
77static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +020078 uint16_t ns_bvci, uint16_t sgsn_nsei);
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020079
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +020080static int check_peer_nsei(struct gbproxy_peer *peer, uint16_t nsei)
Jacob Erlbeckbc555742013-10-18 14:34:55 +020081{
82 if (peer->nsei != nsei) {
83 LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI "
84 "BVCI=%u via NSEI=%u (expected NSEI=%u)\n",
85 peer->bvci, nsei, peer->nsei);
86 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]);
Jacob Erlbeck64cb9242013-10-24 12:48:56 +020087 return 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +020088 }
89
Jacob Erlbeck64cb9242013-10-24 12:48:56 +020090 return 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +020091}
92
Harald Welte69619e32010-05-03 19:05:10 +020093/* strip off the NS header */
94static void strip_ns_hdr(struct msgb *msg)
95{
96 int strip_len = msgb_bssgph(msg) - msg->data;
97 msgb_pull(msg, strip_len);
98}
99
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200100/* Transmit Chapter 9.2.10 Identity Request */
101static void gprs_put_identity_req(struct msgb *msg, uint8_t id_type)
102{
103 struct gsm48_hdr *gh;
104
105 id_type &= GSM_MI_TYPE_MASK;
106
107 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
108 gh->proto_discr = GSM48_PDISC_MM_GPRS;
109 gh->msg_type = GSM48_MT_GMM_ID_REQ;
110 gh->data[0] = id_type;
111}
112
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200113/* Transmit Chapter 9.4.6.2 Detach Accept (mobile originated detach) */
114static void gprs_put_mo_detach_acc(struct msgb *msg)
115{
116 struct gsm48_hdr *gh;
117
118 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
119 gh->proto_discr = GSM48_PDISC_MM_GPRS;
120 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
121 gh->data[0] = 0; /* no force to standby */
122}
123
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200124static void gprs_push_llc_ui(struct msgb *msg,
125 int is_uplink, unsigned sapi, unsigned nu)
126{
127 const uint8_t e_bit = 0;
128 const uint8_t pm_bit = 1;
129 const uint8_t cr_bit = is_uplink ? 0 : 1;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200130 uint8_t *llc;
131 uint8_t *fcs_field;
132 uint32_t fcs;
133
134 nu &= 0x01ff; /* 9 Bit */
135
136 llc = msgb_push(msg, 3);
137 llc[0] = (cr_bit << 6) | (sapi & 0x0f);
138 llc[1] = 0xc0 | (nu >> 6); /* UI frame */
139 llc[2] = (nu << 2) | ((e_bit & 1) << 1) | (pm_bit & 1);
140
141 fcs = gprs_llc_fcs(llc, msgb_length(msg));
142 fcs_field = msgb_put(msg, 3);
143 fcs_field[0] = (uint8_t)(fcs >> 0);
144 fcs_field[1] = (uint8_t)(fcs >> 8);
145 fcs_field[2] = (uint8_t)(fcs >> 16);
146}
147
148static void gprs_push_bssgp_dl_unitdata(struct msgb *msg,
149 uint32_t tlli)
150{
151 struct bssgp_ud_hdr *budh;
152 uint8_t *llc = msgb_data(msg);
153 size_t llc_size = msgb_length(msg);
154 const size_t llc_ie_hdr_size = 3;
155 const uint8_t qos_profile[] = {0x00, 0x50, 0x20}; /* hard-coded */
156 const uint8_t lifetime[] = {0x02, 0x58}; /* 6s hard-coded */
157
158 const size_t bssgp_overhead = sizeof(*budh) +
159 TVLV_GROSS_LEN(sizeof(lifetime)) + llc_ie_hdr_size;
160 uint8_t *ie;
161 uint32_t tlli_be = htonl(tlli);
162
163 budh = (struct bssgp_ud_hdr *)msgb_push(msg, bssgp_overhead);
164
165 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
166 memcpy(&budh->tlli, &tlli_be, sizeof(budh->tlli));
167 memcpy(&budh->qos_profile, qos_profile, sizeof(budh->qos_profile));
168
169 ie = budh->data;
170 tvlv_put(ie, BSSGP_IE_PDU_LIFETIME, sizeof(lifetime), lifetime);
171 ie += TVLV_GROSS_LEN(sizeof(lifetime));
172
173 /* Note: Add alignment before the LLC IE if inserting other IE */
174
175 *(ie++) = BSSGP_IE_LLC_PDU;
176 *(ie++) = llc_size / 256;
177 *(ie++) = llc_size % 256;
178
179 OSMO_ASSERT(ie == llc);
180
181 msgb_bssgph(msg) = (uint8_t *)budh;
182 msgb_tlli(msg) = tlli;
183}
184
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200185/* update peer according to the BSS message */
186static void gbprox_update_current_raid(uint8_t *raid_enc,
187 struct gbproxy_peer *peer,
188 const char *log_text)
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200189{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200190 struct gbproxy_patch_state *state = &peer->patch_state;
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200191 const int old_local_mcc = state->local_mcc;
192 const int old_local_mnc = state->local_mnc;
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200193 struct gprs_ra_id raid;
194
195 if (!raid_enc)
196 return;
197
198 gsm48_parse_ra(&raid, raid_enc);
199
200 /* save source side MCC/MNC */
201 if (!peer->cfg->core_mcc || raid.mcc == peer->cfg->core_mcc) {
202 state->local_mcc = 0;
203 } else {
204 state->local_mcc = raid.mcc;
205 }
206
207 if (!peer->cfg->core_mnc || raid.mnc == peer->cfg->core_mnc) {
208 state->local_mnc = 0;
209 } else {
210 state->local_mnc = raid.mnc;
211 }
212
213 if (old_local_mcc != state->local_mcc ||
214 old_local_mnc != state->local_mnc)
215 LOGP(DGPRS, LOGL_NOTICE,
216 "Patching RAID %sactivated, msg: %s, "
217 "local: %d-%d, core: %d-%d\n",
218 state->local_mcc || state->local_mnc ?
219 "" : "de",
220 log_text,
221 state->local_mcc, state->local_mnc,
222 peer->cfg->core_mcc, peer->cfg->core_mnc);
223}
224
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200225uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer,
226 uint32_t sgsn_ptmsi)
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200227{
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200228 uint32_t bss_ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200229 int max_retries = 23;
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200230 if (!peer->cfg->patch_ptmsi) {
231 bss_ptmsi = sgsn_ptmsi;
232 } else {
233 do {
234 bss_ptmsi = rand_r(&peer->cfg->bss_ptmsi_state);
235 bss_ptmsi = bss_ptmsi | 0xC0000000;
236
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200237 if (gbproxy_link_info_by_ptmsi(peer, bss_ptmsi))
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200238 bss_ptmsi = GSM_RESERVED_TMSI;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200239 } while (bss_ptmsi == GSM_RESERVED_TMSI && max_retries--);
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200240 }
241
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200242 if (bss_ptmsi == GSM_RESERVED_TMSI)
243 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a BSS P-TMSI\n");
244
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200245 return bss_ptmsi;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200246}
247
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200248uint32_t gbproxy_make_sgsn_tlli(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200249 struct gbproxy_link_info *link_info,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200250 uint32_t bss_tlli)
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200251{
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200252 uint32_t sgsn_tlli;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200253 int max_retries = 23;
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200254 if (!peer->cfg->patch_ptmsi) {
255 sgsn_tlli = bss_tlli;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200256 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI) {
257 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200258 TLLI_FOREIGN);
259 } else {
260 do {
261 /* create random TLLI, 0b01111xxx... */
262 sgsn_tlli = rand_r(&peer->cfg->sgsn_tlli_state);
263 sgsn_tlli = (sgsn_tlli & 0x7fffffff) | 0x78000000;
264
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200265 if (gbproxy_link_info_by_any_sgsn_tlli(peer, sgsn_tlli))
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200266 sgsn_tlli = 0;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200267 } while (!sgsn_tlli && max_retries--);
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200268 }
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200269
270 if (!sgsn_tlli)
271 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate an SGSN TLLI\n");
272
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200273 return sgsn_tlli;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200274}
275
Jacob Erlbeck31591142014-09-03 11:59:48 +0200276/* Returns != 0 iff IMSI acquisition was in progress */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200277static int gbproxy_restart_imsi_acquisition(struct gbproxy_link_info* link_info)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200278{
279 int in_progress = 0;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200280 if (!link_info)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200281 return 0;
282
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200283 if (link_info->imsi_acq_pending) {
Jacob Erlbeck31591142014-09-03 11:59:48 +0200284 in_progress = 1;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200285 link_info->imsi_acq_retries += 1;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200286 }
Jacob Erlbeck31591142014-09-03 11:59:48 +0200287
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200288 gbproxy_link_info_discard_messages(link_info);
289 link_info->imsi_acq_pending = 0;
Jacob Erlbeck31591142014-09-03 11:59:48 +0200290
291 return in_progress;
292}
293
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200294static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info)
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200295{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200296 gbproxy_restart_imsi_acquisition(link_info);
297 link_info->imsi_acq_retries = 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200298}
299
300static void gbproxy_flush_stored_messages(struct gbproxy_peer *peer,
301 struct msgb *msg,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200302 time_t now,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200303 struct gbproxy_link_info* link_info,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200304 struct gprs_gb_parse_context *parse_ctx)
305{
306 int rc;
307 struct msgb *stored_msg;
308 /* Got identity response with IMSI, assuming the request had
309 * been generated by the gbproxy */
310
311 LOGP(DLLC, LOGL_DEBUG,
312 "NSEI=%d(BSS) IMSI acquisition succeeded, "
313 "flushing stored messages\n",
314 msgb_nsei(msg));
315
316 /* Patch and flush stored messages towards the SGSN */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200317 while ((stored_msg = msgb_dequeue(&link_info->stored_msgs))) {
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200318 struct gprs_gb_parse_context tmp_parse_ctx = {0};
319 tmp_parse_ctx.to_bss = 0;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200320 tmp_parse_ctx.peer_nsei = msgb_nsei(stored_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200321 int len_change = 0;
322
323 gprs_gb_parse_bssgp(msgb_bssgph(stored_msg),
324 msgb_bssgp_len(stored_msg),
325 &tmp_parse_ctx);
326 gbproxy_patch_bssgp(msg, msgb_bssgph(stored_msg),
327 msgb_bssgp_len(stored_msg),
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200328 peer, link_info, &len_change,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200329 &tmp_parse_ctx);
330
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200331 gbproxy_update_link_state_after(peer, link_info, now,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200332 &tmp_parse_ctx);
333
334 rc = gbprox_relay2sgsn(peer->cfg, stored_msg,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200335 msgb_bvci(msg), link_info->sgsn_nsei);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200336
337 if (rc < 0)
338 LOGP(DLLC, LOGL_ERROR,
339 "NSEI=%d(BSS) failed to send stored message "
340 "(%s)\n",
341 msgb_nsei(msg),
342 parse_ctx->llc_msg_name ?
343 parse_ctx->llc_msg_name : "BSSGP");
344 msgb_free(stored_msg);
345 }
346}
347
348static void gbproxy_acquire_imsi(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200349 struct gbproxy_link_info* link_info,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200350 uint16_t bvci)
351{
352 struct msgb *idreq_msg;
353
354 /* Send IDENT REQ */
355 idreq_msg = gsm48_msgb_alloc();
356 gprs_put_identity_req(idreq_msg, GSM_MI_TYPE_IMSI);
357 /* Workaround to avoid N(U) collisions and to enable a restart
358 * of the IMSI acquisition procedure. This will work unless the
359 * SGSN has an initial V(UT) within [256-32, 256+n_retries]
360 * (see GSM 04.64, 8.4.2).
361 * TODO: Check whether it is sensible to rely on this, it might
362 * be an issue when P-TMSI patching is _not_ enabled and the SGSN
363 * doesn't reset V(UT) after a detach. */
364 gprs_push_llc_ui(idreq_msg, 0, GPRS_SAPI_GMM,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200365 (link_info->imsi_acq_retries + 256) % 512);
366 gprs_push_bssgp_dl_unitdata(idreq_msg, link_info->tlli.current);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200367 gbprox_relay2peer(idreq_msg, peer, bvci);
368 msgb_free(idreq_msg);
369}
370
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200371static void gbproxy_tx_detach_acc(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200372 struct gbproxy_link_info* link_info,
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200373 uint16_t bvci)
374{
375 struct msgb *detacc_msg;
376
377 /* Send DETACH ACC */
378 detacc_msg = gsm48_msgb_alloc();
379 gprs_put_mo_detach_acc(detacc_msg);
380 gprs_push_llc_ui(detacc_msg, 0, GPRS_SAPI_GMM,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200381 (link_info->imsi_acq_retries + 256) % 512);
382 gprs_push_bssgp_dl_unitdata(detacc_msg, link_info->tlli.current);
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200383 gbprox_relay2peer(detacc_msg, peer, bvci);
384 msgb_free(detacc_msg);
385}
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200386
387/* Return != 0 iff msg still needs to be processed */
388static int gbproxy_imsi_acquisition(struct gbproxy_peer *peer,
389 struct msgb *msg,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200390 time_t now,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200391 struct gbproxy_link_info* link_info,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200392 struct gprs_gb_parse_context *parse_ctx)
393{
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200394 struct msgb *stored_msg;
395
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200396 if (!link_info)
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200397 return 1;
398
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200399 if (!link_info->imsi_acq_pending && link_info->imsi_len > 0)
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200400 return 1;
401
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200402 if (parse_ctx->g48_hdr)
403 switch (parse_ctx->g48_hdr->msg_type)
404 {
Jacob Erlbecke27ab912014-09-18 10:08:27 +0200405 case GSM48_MT_GMM_RA_UPD_REQ:
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200406 case GSM48_MT_GMM_ATTACH_REQ:
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200407 if (gbproxy_restart_imsi_acquisition(link_info)) {
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200408 LOGP(DLLC, LOGL_INFO,
409 "NSEI=%d(BSS) IMSI acquisition was in progress "
Jacob Erlbecke27ab912014-09-18 10:08:27 +0200410 "when receiving an %s.\n",
411 msgb_nsei(msg), parse_ctx->llc_msg_name);
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200412 }
413 break;
414 case GSM48_MT_GMM_DETACH_REQ:
415 /* Nothing has been sent to the SGSN yet */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200416 if (link_info->imsi_acq_pending) {
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200417 LOGP(DLLC, LOGL_INFO,
418 "NSEI=%d(BSS) IMSI acquisition was in progress "
419 "when receiving a DETACH_REQ.\n",
420 msgb_nsei(msg));
421 }
422 if (!parse_ctx->invalidate_tlli) {
423 LOGP(DLLC, LOGL_INFO,
424 "NSEI=%d(BSS) IMSI not yet acquired, "
425 "faking a DETACH_ACC.\n",
426 msgb_nsei(msg));
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200427 gbproxy_tx_detach_acc(peer, link_info, msgb_bvci(msg));
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200428 parse_ctx->invalidate_tlli = 1;
429 }
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200430 gbproxy_reset_imsi_acquisition(link_info);
431 gbproxy_update_link_state_after(peer, link_info, now,
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200432 parse_ctx);
433 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200434 }
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200435
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200436 if (link_info->imsi_acq_pending && link_info->imsi_len > 0) {
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200437 int is_ident_resp =
438 parse_ctx->g48_hdr &&
439 parse_ctx->g48_hdr->proto_discr == GSM48_PDISC_MM_GPRS &&
440 parse_ctx->g48_hdr->msg_type == GSM48_MT_GMM_ID_RESP;
441
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200442 /* The IMSI is now available */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200443 gbproxy_flush_stored_messages(peer, msg, now, link_info,
Jacob Erlbeck9a6b7632014-09-19 13:23:21 +0200444 parse_ctx);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200445
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200446 gbproxy_reset_imsi_acquisition(link_info);
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200447
448 /* This message is most probably the response to the ident
449 * request sent by gbproxy_acquire_imsi(). Don't forward it to
450 * the SGSN. */
451 return !is_ident_resp;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200452 }
453
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200454 /* The message cannot be processed since the IMSI is still missing */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200455
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200456 /* Enqueue unpatched messages */
457 LOGP(DLLC, LOGL_INFO,
458 "NSEI=%d(BSS) IMSI acquisition in progress, "
459 "storing message (%s)\n",
460 msgb_nsei(msg),
461 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200462
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200463 stored_msg = gprs_msgb_copy(msg, "process_bssgp_ul");
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200464 msgb_enqueue(&link_info->stored_msgs, stored_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200465
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200466 if (!link_info->imsi_acq_pending) {
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200467 LOGP(DLLC, LOGL_INFO,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200468 "NSEI=%d(BSS) IMSI is required but not available, "
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200469 "initiating identification procedure (%s)\n",
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200470 msgb_nsei(msg),
471 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
472
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200473 gbproxy_acquire_imsi(peer, link_info, msgb_bvci(msg));
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200474
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200475 /* There is no explicit retransmission handling, the
476 * implementation relies on the MS doing proper retransmissions
477 * of the triggering message instead */
478
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200479 link_info->imsi_acq_pending = 1;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200480 }
481
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200482 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200483}
484
485struct gbproxy_peer *gbproxy_find_peer(struct gbproxy_config *cfg,
486 struct msgb *msg,
487 struct gprs_gb_parse_context *parse_ctx)
488{
489 struct gbproxy_peer *peer = NULL;
490
491 if (msgb_bvci(msg) >= 2)
492 peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg));
493
494 if (!peer && !parse_ctx->to_bss)
495 peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg));
496
497 if (!peer)
498 peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx->bssgp_tp);
499
500 if (!peer) {
501 LOGP(DLLC, LOGL_INFO,
502 "NSEI=%d(%s) patching: didn't find peer for message, "
503 "PDU %d\n",
504 msgb_nsei(msg), parse_ctx->to_bss ? "BSS" : "SGSN",
505 parse_ctx->pdu_type);
506 /* Increment counter */
507 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]);
508 }
509 return peer;
510}
511
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200512/* patch BSSGP message */
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200513static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg,
514 struct msgb *msg,
515 struct gbproxy_peer *peer)
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200516{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200517 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200518 int rc;
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200519 int len_change = 0;
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200520 time_t now;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200521 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200522 uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200523
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200524 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
Jacob Erlbeck1abfdc22014-09-04 11:42:08 +0200525 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200526 return 1;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200527
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200528 parse_ctx.to_bss = 0;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200529 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200530
531 /* Parse BSSGP/LLC */
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200532 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
533 &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200534
535 if (!rc) {
536 if (!parse_ctx.need_decryption) {
537 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeck643d5222014-08-18 17:26:04 +0200538 "NSEI=%u(BSS) patching: "
539 "failed to parse BSSGP/GMM message\n",
540 msgb_nsei(msg));
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200541 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200542 }
543 }
544
545 /* Get peer */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200546 if (!peer)
547 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200548
549 if (!peer)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200550 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200551
552 now = time(NULL);
553
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200554 gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer,
555 parse_ctx.llc_msg_name);
556
Jacob Erlbeck31591142014-09-03 11:59:48 +0200557 gprs_gb_log_parse_context(&parse_ctx, "BSSGP");
558
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200559 link_info = gbproxy_update_link_state_ul(peer, now, &parse_ctx);
Jacob Erlbeck31591142014-09-03 11:59:48 +0200560
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200561 if (parse_ctx.g48_hdr) {
562 switch (parse_ctx.g48_hdr->msg_type) {
563 case GSM48_MT_GMM_ATTACH_REQ:
564 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]);
565 break;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200566
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200567 default:
568 break;
569 }
570 }
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200571
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200572 if (link_info && cfg->route_to_sgsn2) {
573 if (cfg->acquire_imsi && link_info->imsi_len == 0)
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200574 sgsn_nsei = 0xffff;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200575 else if (gbproxy_imsi_matches(peer, link_info))
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200576 sgsn_nsei = cfg->nsip_sgsn2_nsei;
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200577 }
578
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200579 if (link_info)
580 link_info->sgsn_nsei = sgsn_nsei;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200581
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200582 /* Handle IMSI acquisition */
583 if (cfg->acquire_imsi) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200584 rc = gbproxy_imsi_acquisition(peer, msg, now, link_info,
Jacob Erlbeck9a6b7632014-09-19 13:23:21 +0200585 &parse_ctx);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200586 if (rc <= 0)
587 return rc;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200588 }
589
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200590 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200591 peer, link_info, &len_change, &parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200592
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200593 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200594
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200595 if (sgsn_nsei != cfg->nsip_sgsn_nsei) {
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200596 /* Send message directly to the selected SGSN */
597 rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei);
598 /* Don't let the calling code handle the transmission */
599 return 0;
600 }
601
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200602 return 1;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200603}
604
605/* patch BSSGP message to use core_mcc/mnc on the SGSN side */
606static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg,
607 struct msgb *msg,
608 struct gbproxy_peer *peer)
609{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200610 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200611 int rc;
612 int len_change = 0;
613 time_t now;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200614 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200615
Jacob Erlbeck1abfdc22014-09-04 11:42:08 +0200616 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
617 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
618 return;
619
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200620 parse_ctx.to_bss = 1;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200621 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200622
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200623 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
624 &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200625
626 if (!rc) {
627 if (!parse_ctx.need_decryption) {
628 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeck643d5222014-08-18 17:26:04 +0200629 "NSEI=%u(SGSN) patching: "
630 "failed to parse BSSGP/GMM message\n",
631 msgb_nsei(msg));
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200632 return;
633 }
634 }
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200635
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200636 /* Get peer */
637 if (!peer)
638 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200639
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200640 if (!peer)
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200641 return;
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200642
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200643 now = time(NULL);
644
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200645 if (parse_ctx.g48_hdr) {
646 switch (parse_ctx.g48_hdr->msg_type) {
647 case GSM48_MT_GMM_ATTACH_REJ:
648 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]);
649 break;
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200650
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200651 default:
652 break;
653 }
654 }
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200655
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200656 gprs_gb_log_parse_context(&parse_ctx, "BSSGP");
657
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200658 link_info = gbproxy_update_link_state_dl(peer, now, &parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200659
660 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200661 peer, link_info, &len_change, &parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200662
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200663 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200664
Jacob Erlbeck03551e52014-06-27 19:56:05 +0200665 return;
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200666}
667
Harald Welte9f75c352010-04-30 20:26:32 +0200668/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200669static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200670 uint16_t ns_bvci, uint16_t sgsn_nsei)
Harald Welte672f5c42010-05-03 18:54:58 +0200671{
Harald Welte39d0bb52010-05-12 18:10:25 +0000672 /* create a copy of the message so the old one can
673 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200674 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2sgsn");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200675 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000676
Harald Weltee9ea2692010-05-11 20:20:13 +0200677 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200678 msgb_nsei(msg), ns_bvci, sgsn_nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200679
Harald Welte672f5c42010-05-03 18:54:58 +0200680 msgb_bvci(msg) = ns_bvci;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200681 msgb_nsei(msg) = sgsn_nsei;
Harald Welte672f5c42010-05-03 18:54:58 +0200682
Harald Welte69619e32010-05-03 19:05:10 +0200683 strip_ns_hdr(msg);
684
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200685 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
686 if (rc < 0)
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200687 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200688
689 return rc;
Harald Welte672f5c42010-05-03 18:54:58 +0200690}
691
Harald Welte672f5c42010-05-03 18:54:58 +0200692/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200693static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
Harald Welte9f75c352010-04-30 20:26:32 +0200694 uint16_t ns_bvci)
695{
Harald Welte39d0bb52010-05-12 18:10:25 +0000696 /* create a copy of the message so the old one can
697 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200698 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2peer");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200699 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000700
Harald Welte0ab535b2010-05-13 10:34:56 +0200701 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200702 msgb_nsei(msg), ns_bvci, peer->nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200703
Harald Welte9f75c352010-04-30 20:26:32 +0200704 msgb_bvci(msg) = ns_bvci;
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200705 msgb_nsei(msg) = peer->nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200706
Harald Welte0ab535b2010-05-13 10:34:56 +0200707 /* Strip the old NS header, it will be replaced with a new one */
Harald Welte69619e32010-05-03 19:05:10 +0200708 strip_ns_hdr(msg);
709
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200710 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
711 if (rc < 0)
712 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
713
714 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200715}
716
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200717static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type)
Harald Welte36f98d92011-02-06 13:09:29 +0100718{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200719 struct gbproxy_peer *peer;
Harald Welte36f98d92011-02-06 13:09:29 +0100720
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200721 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +0100722 if (!peer) {
723 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
724 ptp_bvci);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200725 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +0100726 return -ENOENT;
727 }
728
729 switch (pdu_type) {
730 case BSSGP_PDUT_BVC_BLOCK_ACK:
731 peer->blocked = 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200732 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100733 break;
734 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
735 peer->blocked = 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200736 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100737 break;
738 default:
739 break;
740 }
741 return 0;
742}
743
Harald Welte9f75c352010-04-30 20:26:32 +0200744/* Send a message to a peer identified by ptp_bvci but using ns_bvci
745 * in the NS hdr */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200746static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci,
Harald Welte9f75c352010-04-30 20:26:32 +0200747 uint16_t ns_bvci)
748{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200749 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +0200750
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200751 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200752 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200753 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
Harald Welte1c77c6e2010-05-03 21:37:11 +0200754 ptp_bvci);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200755 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte9f75c352010-04-30 20:26:32 +0200756 return -ENOENT;
Harald Welte1c77c6e2010-05-03 21:37:11 +0200757 }
Harald Welte9f75c352010-04-30 20:26:32 +0200758
Harald Welte69619e32010-05-03 19:05:10 +0200759 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200760}
761
Harald Welteb1fd9022012-06-17 12:16:31 +0800762int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
763{
764 return 0;
765}
766
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200767/* Receive an incoming PTP message from a BSS-side NS-VC */
768static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg,
769 struct msgb *msg, uint16_t nsei,
770 uint16_t nsvci, uint16_t ns_bvci)
771{
772 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200773 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
774 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200775 int rc;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200776
777 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
778
779 if (peer)
780 check_peer_nsei(peer, nsei);
781
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200782 rc = gbprox_process_bssgp_ul(cfg, msg, peer);
783 if (!rc)
784 return 0;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200785
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200786 switch (pdu_type) {
787 case BSSGP_PDUT_FLOW_CONTROL_BVC:
788 if (!cfg->route_to_sgsn2)
789 break;
790
791 /* Send a copy to the secondary SGSN */
792 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
793 break;
794 default:
795 break;
796 }
797
798
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200799 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200800}
801
802/* Receive an incoming PTP message from a SGSN-side NS-VC */
803static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg,
804 struct msgb *msg, uint16_t nsei,
805 uint16_t nsvci, uint16_t ns_bvci)
806{
807 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200808 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
809 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200810
811 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
812
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200813 /* Send status messages before patching */
814
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200815 if (!peer) {
816 LOGP(DGPRS, LOGL_INFO, "Didn't find peer for "
817 "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n",
818 ns_bvci, nsvci, nsei);
819 rate_ctr_inc(&cfg->ctrg->
820 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
821 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
822 &ns_bvci, msg);
823 }
824
825 if (peer->blocked) {
826 LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for "
827 "blocked BVCI=%u via NSVC=%u/NSEI=%u\n",
828 ns_bvci, nsvci, nsei);
829 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200830 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200831 }
832
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200833 switch (pdu_type) {
834 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
835 case BSSGP_PDUT_BVC_BLOCK_ACK:
836 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
837 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
838 /* Hide ACKs from the secondary SGSN, the primary SGSN
839 * is responsible to send them. */
840 return 0;
841 break;
842 default:
843 break;
844 }
845
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200846 /* Optionally patch the message */
847 gbprox_process_bssgp_dl(cfg, msg, peer);
848
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200849 return gbprox_relay2peer(msg, peer, ns_bvci);
850}
851
Harald Welte9f75c352010-04-30 20:26:32 +0200852/* Receive an incoming signalling message from a BSS-side NS-VC */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200853static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg,
854 struct msgb *msg, uint16_t nsei,
Harald Welte9f75c352010-04-30 20:26:32 +0200855 uint16_t ns_bvci)
856{
Harald Welteca3620a2010-05-03 16:30:59 +0200857 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200858 struct tlv_parsed tp;
859 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200860 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200861 struct gbproxy_peer *from_peer = NULL;
Harald Welte70f38d22010-05-01 12:10:57 +0200862 struct gprs_ra_id raid;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200863 int copy_to_sgsn2 = 0;
Jacob Erlbeck59300642014-09-08 09:04:01 +0200864 int rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200865
Harald Weltec471d3d2011-02-06 17:13:12 +0100866 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200867 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200868 nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200869 return -EINVAL;
870 }
871
872 /* we actually should never see those two for BVCI == 0, but double-check
873 * just to make sure */
874 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
875 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200876 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200877 "signalling\n", nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200878 return -EINVAL;
879 }
880
881 bssgp_tlv_parse(&tp, bgph->data, data_len);
882
883 switch (pdu_type) {
884 case BSSGP_PDUT_SUSPEND:
885 case BSSGP_PDUT_RESUME:
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200886 /* We implement RAI snooping during SUSPEND/RESUME, since it
887 * establishes a relationsip between BVCI/peer and the routeing
888 * area identification. The snooped information is then used
889 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
890 * BSSGP */
Harald Welte9f75c352010-04-30 20:26:32 +0200891 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
892 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200893 from_peer = gbproxy_peer_by_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200894 if (!from_peer)
895 goto err_no_peer;
Harald Welte1174c082010-05-12 00:07:29 +0200896 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
897 sizeof(from_peer->ra));
Harald Welte7fc98222010-05-11 10:15:42 +0200898 gsm48_parse_ra(&raid, from_peer->ra);
Harald Welte4cf12e92010-05-13 14:14:56 +0200899 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200900 "RAI snooping: RAI %u-%u-%u-%u behind BVCI=%u\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200901 nsei, raid.mcc, raid.mnc, raid.lac,
902 raid.rac , from_peer->bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200903 /* FIXME: This only supports one BSS per RA */
904 break;
Harald Welte44c48302010-05-03 19:22:32 +0200905 case BSSGP_PDUT_BVC_RESET:
906 /* If we receive a BVC reset on the signalling endpoint, we
907 * don't want the SGSN to reset, as the signalling endpoint
908 * is common for all point-to-point BVCs (and thus all BTS) */
909 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200910 uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte72953b82010-05-12 00:20:41 +0200911 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200912 nsei, bvci);
Harald Welte44c48302010-05-03 19:22:32 +0200913 if (bvci == 0) {
914 /* FIXME: only do this if SGSN is alive! */
Harald Weltee9ea2692010-05-11 20:20:13 +0200915 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200916 "BVC RESET ACK of BVCI=0\n", nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200917 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200918 nsei, 0, ns_bvci);
Harald Welte1174c082010-05-12 00:07:29 +0200919 }
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200920 from_peer = gbproxy_peer_by_bvci(cfg, bvci);
Harald Welte1174c082010-05-12 00:07:29 +0200921 if (!from_peer) {
Harald Welte1c77c6e2010-05-03 21:37:11 +0200922 /* if a PTP-BVC is reset, and we don't know that
923 * PTP-BVCI yet, we should allocate a new peer */
924 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200925 "BVCI=%u via NSEI=%u\n", bvci, nsei);
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +0200926 from_peer = gbproxy_peer_alloc(cfg, bvci);
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200927 from_peer->nsei = nsei;
Harald Welte44c48302010-05-03 19:22:32 +0200928 }
Jacob Erlbeck64cb9242013-10-24 12:48:56 +0200929
930 if (!check_peer_nsei(from_peer, nsei))
931 from_peer->nsei = nsei;
932
Harald Welte1174c082010-05-12 00:07:29 +0200933 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
934 struct gprs_ra_id raid;
935 /* We have a Cell Identifier present in this
936 * PDU, this means we can extend our local
937 * state information about this particular cell
938 * */
939 memcpy(from_peer->ra,
940 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
941 sizeof(from_peer->ra));
942 gsm48_parse_ra(&raid, from_peer->ra);
943 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200944 "Cell ID %u-%u-%u-%u\n", nsei,
Harald Welte1174c082010-05-12 00:07:29 +0200945 bvci, raid.mcc, raid.mnc, raid.lac,
946 raid.rac);
947 }
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200948 if (cfg->route_to_sgsn2)
949 copy_to_sgsn2 = 1;
Harald Welte44c48302010-05-03 19:22:32 +0200950 }
951 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200952 }
953
Harald Weltee9ea2692010-05-11 20:20:13 +0200954 /* Normally, we can simply pass on all signalling messages from BSS to
955 * SGSN */
Jacob Erlbeck59300642014-09-08 09:04:01 +0200956 rc = gbprox_process_bssgp_ul(cfg, msg, from_peer);
957 if (!rc)
958 return 0;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200959
960 if (copy_to_sgsn2)
961 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
962
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200963 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200964err_no_peer:
Jacob Erlbeckc5085f92013-10-18 13:04:48 +0200965 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200966 nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200967 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200968 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200969err_mand_ie:
Harald Welte0a4050c2010-05-11 10:01:17 +0200970 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200971 nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200972 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200973 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200974}
975
976/* Receive paging request from SGSN, we need to relay to proper BSS */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200977static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200978 uint32_t nsei, uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200979{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200980 struct gbproxy_peer *peer = NULL;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200981 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte9f75c352010-04-30 20:26:32 +0200982
Harald Welte4cf12e92010-05-13 14:14:56 +0200983 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200984 nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200985 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200986 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte4cf12e92010-05-13 14:14:56 +0200987 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n",
988 bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200989 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte9f75c352010-04-30 20:26:32 +0200990 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200991 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200992 LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +0800993 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200994 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte9f75c352010-04-30 20:26:32 +0200995 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200996 peer = gbproxy_peer_by_lai(cfg, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200997 LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +0800998 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200999 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte9f75c352010-04-30 20:26:32 +02001000 } else
Harald Welte4cf12e92010-05-13 14:14:56 +02001001 LOGPC(DGPRS, LOGL_INFO, "\n");
1002
1003 if (!peer) {
1004 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001005 "unable to route, missing IE\n", nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001006 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
Harald Welte9f75c352010-04-30 20:26:32 +02001007 return -EINVAL;
Harald Welte4cf12e92010-05-13 14:14:56 +02001008 }
1009 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001010}
1011
Harald Welte0a4050c2010-05-11 10:01:17 +02001012/* Receive an incoming BVC-RESET message from the SGSN */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001013static int rx_reset_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001014 struct msgb *orig_msg,
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001015 struct msgb *msg, struct tlv_parsed *tp,
1016 uint32_t nsei, uint16_t ns_bvci)
Harald Welte0a4050c2010-05-11 10:01:17 +02001017{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001018 struct gbproxy_peer *peer;
Harald Welte0a4050c2010-05-11 10:01:17 +02001019 uint16_t ptp_bvci;
1020
1021 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001022 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001023 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001024 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001025 NULL, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +02001026 }
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001027 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte0a4050c2010-05-11 10:01:17 +02001028
1029 if (ptp_bvci >= 2) {
1030 /* A reset for a PTP BVC was received, forward it to its
1031 * respective peer */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001032 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001033 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +02001034 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001035 nsei, ptp_bvci);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001036 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001037 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001038 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001039 &ptp_bvci, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +02001040 }
1041 return gbprox_relay2peer(msg, peer, ns_bvci);
1042 }
1043
1044 /* A reset for the Signalling entity has been received
1045 * from the SGSN. As the signalling BVCI is shared
1046 * among all the BSS's that we multiplex, it needs to
1047 * be relayed */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001048 llist_for_each_entry(peer, &cfg->bts_peers, list)
Harald Welte0a4050c2010-05-11 10:01:17 +02001049 gbprox_relay2peer(msg, peer, ns_bvci);
1050
1051 return 0;
1052}
1053
Harald Welte9f75c352010-04-30 20:26:32 +02001054/* Receive an incoming signalling message from the SGSN-side NS-VC */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001055static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001056 struct msgb *orig_msg, uint32_t nsei,
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001057 uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001058{
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001059 struct bssgp_normal_hdr *bgph =
1060 (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001061 struct tlv_parsed tp;
1062 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001063 int data_len;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001064 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +02001065 uint16_t bvci;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001066 struct msgb *msg;
Harald Welte9f75c352010-04-30 20:26:32 +02001067 int rc = 0;
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001068 int cause;
Harald Welte9f75c352010-04-30 20:26:32 +02001069
Harald Weltec471d3d2011-02-06 17:13:12 +01001070 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +02001071 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001072 "signalling\n", nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001073 /* FIXME: Send proper error message */
Harald Welte9f75c352010-04-30 20:26:32 +02001074 return -EINVAL;
1075 }
1076
1077 /* we actually should never see those two for BVCI == 0, but double-check
1078 * just to make sure */
1079 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1080 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +02001081 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001082 "signalling\n", nsei);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001083 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001084 }
1085
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001086 msg = gprs_msgb_copy(orig_msg, "rx_sig_from_sgsn");
1087 gbprox_process_bssgp_dl(cfg, msg, NULL);
1088 /* Update message info */
1089 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1090 data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +02001091 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1092
1093 switch (pdu_type) {
Harald Welte0a4050c2010-05-11 10:01:17 +02001094 case BSSGP_PDUT_BVC_RESET:
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001095 rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001096 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001097 case BSSGP_PDUT_BVC_RESET_ACK:
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +02001098 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
1099 break;
1100 /* fall through */
1101 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9f75c352010-04-30 20:26:32 +02001102 /* simple case: BVCI IE is mandatory */
1103 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1104 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001105 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001106 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001107 break;
1108 case BSSGP_PDUT_PAGING_PS:
1109 case BSSGP_PDUT_PAGING_CS:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001110 /* process the paging request (LAI/RAI lookup) */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001111 rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001112 break;
1113 case BSSGP_PDUT_STATUS:
Harald Welte0a4050c2010-05-11 10:01:17 +02001114 /* Some exception has occurred */
Harald Welte44c48302010-05-03 19:22:32 +02001115 LOGP(DGPRS, LOGL_NOTICE,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001116 "NSEI=%u(SGSN) BSSGP STATUS ", nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +02001117 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
1118 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1119 goto err_mand_ie;
1120 }
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001121 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
Harald Welte0a4050c2010-05-11 10:01:17 +02001122 LOGPC(DGPRS, LOGL_NOTICE,
1123 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001124 bssgp_cause_str(cause));
Harald Welte0a4050c2010-05-11 10:01:17 +02001125 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001126 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1127 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%u\n", bvci);
1128
1129 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI)
1130 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001131 } else
1132 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welte9f75c352010-04-30 20:26:32 +02001133 break;
1134 /* those only exist in the SGSN -> BSS direction */
1135 case BSSGP_PDUT_SUSPEND_ACK:
1136 case BSSGP_PDUT_SUSPEND_NACK:
1137 case BSSGP_PDUT_RESUME_ACK:
1138 case BSSGP_PDUT_RESUME_NACK:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001139 /* RAI IE is mandatory */
Harald Welte9f75c352010-04-30 20:26:32 +02001140 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1141 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001142 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte9f75c352010-04-30 20:26:32 +02001143 if (!peer)
1144 goto err_no_peer;
Harald Welte69619e32010-05-03 19:05:10 +02001145 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001146 break;
Harald Welte36f98d92011-02-06 13:09:29 +01001147 case BSSGP_PDUT_BVC_BLOCK_ACK:
1148 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1149 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1150 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001151 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte36f98d92011-02-06 13:09:29 +01001152 if (bvci == 0) {
1153 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001154 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
Harald Welte36f98d92011-02-06 13:09:29 +01001155 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
1156 /* should we send STATUS ? */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001157 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001158 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +01001159 } else {
1160 /* Mark BVC as (un)blocked */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001161 block_unblock_peer(cfg, bvci, pdu_type);
Harald Welte36f98d92011-02-06 13:09:29 +01001162 }
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001163 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +01001164 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001165 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte44c48302010-05-03 19:22:32 +02001166 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001167 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001168 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001169 ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001170 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001171 break;
1172 default:
Harald Welte72953b82010-05-12 00:20:41 +02001173 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n",
1174 pdu_type);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001175 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001176 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001177 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001178 break;
1179 }
1180
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001181 msgb_free(msg);
1182
Harald Welte9f75c352010-04-30 20:26:32 +02001183 return rc;
1184err_mand_ie:
Harald Welte1c77c6e2010-05-03 21:37:11 +02001185 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001186 nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001187 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001188 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001189 msgb_free(msg);
1190 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001191err_no_peer:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001192 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001193 nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001194 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001195 msgb_free(msg);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001196 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001197}
1198
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001199static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei)
1200{
1201 return nsei == cfg->nsip_sgsn_nsei ||
1202 (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei);
1203}
1204
Harald Welte9f75c352010-04-30 20:26:32 +02001205/* Main input function for Gb proxy */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001206int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei,
1207 uint16_t ns_bvci, uint16_t nsvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001208{
Harald Welte672f5c42010-05-03 18:54:58 +02001209 int rc;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001210 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001211
1212 /* Only BVCI=0 messages need special treatment */
1213 if (ns_bvci == 0 || ns_bvci == 1) {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001214 if (remote_end_is_sgsn)
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001215 rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001216 else
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001217 rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci);
Harald Welte672f5c42010-05-03 18:54:58 +02001218 } else {
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +02001219 /* All other BVCI are PTP */
1220 if (remote_end_is_sgsn)
1221 rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei, nsvci,
1222 ns_bvci);
1223 else
1224 rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei, nsvci,
1225 ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001226 }
1227
Harald Welte672f5c42010-05-03 18:54:58 +02001228 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +02001229}
Harald Welte85801d02010-05-11 05:49:43 +02001230
Harald Welte1ccbf442010-05-14 11:53:08 +00001231int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi)
1232{
1233 struct gprs_nsvc *nsvc;
1234
1235 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
1236 if (!nsvc->persistent)
1237 continue;
1238 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1239 }
1240 return 0;
1241}
1242
Harald Weltec1c1dd22010-05-11 06:34:24 +02001243/* Signal handler for signals from NS layer */
1244int gbprox_signal(unsigned int subsys, unsigned int signal,
1245 void *handler_data, void *signal_data)
1246{
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001247 struct gbproxy_config *cfg = handler_data;
Harald Weltec1c1dd22010-05-11 06:34:24 +02001248 struct ns_signal_data *nssd = signal_data;
1249 struct gprs_nsvc *nsvc = nssd->nsvc;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001250 struct gbproxy_peer *peer;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001251 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsvc->nsei);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001252
Harald Weltea6a20b42012-06-16 16:40:42 +08001253 if (subsys != SS_L_NS)
Harald Weltec1c1dd22010-05-11 06:34:24 +02001254 return 0;
1255
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001256 if (signal == S_NS_RESET && remote_end_is_sgsn) {
Harald Weltef69c0592010-05-11 18:29:44 +02001257 /* We have received a NS-RESET from the NSEI and NSVC
1258 * of the SGSN. This might happen with SGSN that start
1259 * their own NS-RESET procedure without waiting for our
1260 * NS-RESET */
1261 nsvc->remote_end_is_sgsn = 1;
1262 }
1263
Harald Welteb778d2c2010-05-12 13:28:25 +00001264 if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) {
1265 LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, "
1266 "re-starting RESET procedure\n");
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001267 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001268 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
Harald Weltee6599ee2012-06-17 12:25:53 +08001269 gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr,
1270 nsvc->nsei, nsvc->nsvci);
Harald Welteb778d2c2010-05-12 13:28:25 +00001271 }
1272
Harald Welte5e106d72011-02-06 16:33:29 +01001273 if (!nsvc->remote_end_is_sgsn) {
1274 /* from BSS to SGSN */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001275 peer = gbproxy_peer_by_nsei(cfg, nsvc->nsei);
Harald Welte5e106d72011-02-06 16:33:29 +01001276 if (!peer) {
1277 LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer "
1278 "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei,
1279 nsvc->nsvci);
1280 return 0;
1281 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001282 switch (signal) {
1283 case S_NS_RESET:
Harald Weltec1c1dd22010-05-11 06:34:24 +02001284 case S_NS_BLOCK:
Harald Welte5e106d72011-02-06 16:33:29 +01001285 if (!peer->blocked)
1286 break;
1287 LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from "
1288 "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n",
1289 nsvc->nsei, nsvc->nsvci);
1290 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei,
1291 peer->bvci, 0);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001292 break;
Harald Welte5e106d72011-02-06 16:33:29 +01001293 }
1294 } else {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001295 /* Forward this message to all NS-VC to BSS */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001296 struct gprs_ns_inst *nsi = cfg->nsi;
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001297 struct gprs_nsvc *next_nsvc;
1298
1299 llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) {
1300 if (next_nsvc->remote_end_is_sgsn)
1301 continue;
1302
1303 /* Note that the following does not start the full
1304 * procedures including timer based retransmissions. */
Harald Welte5e106d72011-02-06 16:33:29 +01001305 switch (signal) {
1306 case S_NS_RESET:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001307 gprs_ns_tx_reset(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001308 break;
1309 case S_NS_BLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001310 gprs_ns_tx_block(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001311 break;
1312 case S_NS_UNBLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001313 gprs_ns_tx_unblock(next_nsvc);
Harald Welte5e106d72011-02-06 16:33:29 +01001314 break;
1315 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001316 }
1317 }
1318 return 0;
1319}
1320
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001321void gbprox_reset(struct gbproxy_config *cfg)
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001322{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001323 struct gbproxy_peer *peer, *tmp;
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001324
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001325 llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list)
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +02001326 gbproxy_peer_free(peer);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001327
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001328 rate_ctr_group_free(cfg->ctrg);
1329 gbproxy_init_config(cfg);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001330}
1331
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001332int gbproxy_init_config(struct gbproxy_config *cfg)
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001333{
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001334 struct timespec tp;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001335 INIT_LLIST_HEAD(&cfg->bts_peers);
Holger Hans Peter Freythera7027a02014-08-04 11:19:56 +02001336 cfg->ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0);
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001337 clock_gettime(CLOCK_REALTIME, &tp);
1338 cfg->bss_ptmsi_state = tp.tv_sec + tp.tv_nsec;
1339 cfg->sgsn_tlli_state = tp.tv_sec - tp.tv_nsec;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001340 return 0;
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001341}