blob: 867364d8f2f06d54385af71f25bbee1ef8bbbeb9 [file] [log] [blame]
Harald Welted1991e72010-04-30 20:26:32 +02001/* NS-over-IP proxy */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther2ea88122013-10-23 11:24:17 +02004 * (C) 2010-2013 by On-Waves
5 * (C) 2013 by Holger Hans Peter Freyther
Harald Welted1991e72010-04-30 20:26:32 +02006 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte0e3e88e2011-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 Welted1991e72010-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 Welte0e3e88e2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Welted1991e72010-04-30 20:26:32 +020017 *
Harald Welte0e3e88e2011-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 Welted1991e72010-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 Weltef2fdefe2010-05-11 10:15:42 +020031#include <arpa/inet.h>
Jacob Erlbeck43037632014-06-06 18:49:23 +020032#include <time.h>
Harald Welted1991e72010-04-30 20:26:32 +020033
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010034#include <osmocom/core/talloc.h>
35#include <osmocom/core/select.h>
Jacob Erlbeck174bad42013-10-18 14:34:55 +020036#include <osmocom/core/rate_ctr.h>
37
Harald Weltecfb6b282012-06-16 14:59:56 +080038#include <osmocom/gprs/gprs_ns.h>
39#include <osmocom/gprs/gprs_bssgp.h>
40
Jacob Erlbeck43037632014-06-06 18:49:23 +020041#include <osmocom/gsm/gsm_utils.h>
42
Harald Welted1991e72010-04-30 20:26:32 +020043#include <openbsc/signal.h>
44#include <openbsc/debug.h>
Jacob Erlbeckcf11e932014-08-19 12:21:01 +020045#include <openbsc/gprs_gb_parse.h>
Harald Weltef01709d2010-05-03 18:54:58 +020046#include <openbsc/gb_proxy.h>
Harald Welted1991e72010-04-30 20:26:32 +020047
Jacob Erlbeck2504bc42014-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 Freyther8ac3a722014-08-04 11:52:52 +020051#include <openbsc/gprs_utils.h>
Jacob Erlbeck2504bc42014-05-19 10:14:58 +020052
Jacob Erlbeck174bad42013-10-18 14:34:55 +020053static const struct rate_ctr_desc global_ctr_description[] = {
54 { "inv-bvci", "Invalid BVC Identifier " },
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +020055 { "inv-lai", "Invalid Location Area Identifier" },
56 { "inv-rai", "Invalid Routing Area Identifier " },
Jacob Erlbeck174bad42013-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 Erlbeck90a1fd12014-05-27 13:49:04 +020065 { "mod-peer-err", "Patch error: no peer " },
Jacob Erlbeck174bad42013-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 Erlbeck4c0f6982014-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 Erlbeck3803e9b2014-08-26 14:47:15 +020078 uint16_t ns_bvci, uint16_t sgsn_nsei);
Jacob Erlbeck4abbbdd2014-09-22 13:30:46 +020079static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info);
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +020080
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +020081static int check_peer_nsei(struct gbproxy_peer *peer, uint16_t nsei)
Jacob Erlbeck174bad42013-10-18 14:34:55 +020082{
83 if (peer->nsei != nsei) {
84 LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI "
85 "BVCI=%u via NSEI=%u (expected NSEI=%u)\n",
86 peer->bvci, nsei, peer->nsei);
87 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]);
Jacob Erlbeckd0a9b502013-10-24 12:48:56 +020088 return 0;
Jacob Erlbeck174bad42013-10-18 14:34:55 +020089 }
90
Jacob Erlbeckd0a9b502013-10-24 12:48:56 +020091 return 1;
Jacob Erlbeck174bad42013-10-18 14:34:55 +020092}
93
Harald Welte3ad32432010-05-03 19:05:10 +020094/* strip off the NS header */
95static void strip_ns_hdr(struct msgb *msg)
96{
97 int strip_len = msgb_bssgph(msg) - msg->data;
98 msgb_pull(msg, strip_len);
99}
100
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200101/* Transmit Chapter 9.2.10 Identity Request */
102static void gprs_put_identity_req(struct msgb *msg, uint8_t id_type)
103{
104 struct gsm48_hdr *gh;
105
106 id_type &= GSM_MI_TYPE_MASK;
107
108 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
109 gh->proto_discr = GSM48_PDISC_MM_GPRS;
110 gh->msg_type = GSM48_MT_GMM_ID_REQ;
111 gh->data[0] = id_type;
112}
113
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200114/* Transmit Chapter 9.4.6.2 Detach Accept (mobile originated detach) */
115static void gprs_put_mo_detach_acc(struct msgb *msg)
116{
117 struct gsm48_hdr *gh;
118
119 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
120 gh->proto_discr = GSM48_PDISC_MM_GPRS;
121 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
122 gh->data[0] = 0; /* no force to standby */
123}
124
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200125static void gprs_push_llc_ui(struct msgb *msg,
126 int is_uplink, unsigned sapi, unsigned nu)
127{
128 const uint8_t e_bit = 0;
129 const uint8_t pm_bit = 1;
130 const uint8_t cr_bit = is_uplink ? 0 : 1;
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200131 uint8_t *llc;
132 uint8_t *fcs_field;
133 uint32_t fcs;
134
135 nu &= 0x01ff; /* 9 Bit */
136
137 llc = msgb_push(msg, 3);
138 llc[0] = (cr_bit << 6) | (sapi & 0x0f);
139 llc[1] = 0xc0 | (nu >> 6); /* UI frame */
140 llc[2] = (nu << 2) | ((e_bit & 1) << 1) | (pm_bit & 1);
141
142 fcs = gprs_llc_fcs(llc, msgb_length(msg));
143 fcs_field = msgb_put(msg, 3);
144 fcs_field[0] = (uint8_t)(fcs >> 0);
145 fcs_field[1] = (uint8_t)(fcs >> 8);
146 fcs_field[2] = (uint8_t)(fcs >> 16);
147}
148
149static void gprs_push_bssgp_dl_unitdata(struct msgb *msg,
150 uint32_t tlli)
151{
152 struct bssgp_ud_hdr *budh;
153 uint8_t *llc = msgb_data(msg);
154 size_t llc_size = msgb_length(msg);
155 const size_t llc_ie_hdr_size = 3;
156 const uint8_t qos_profile[] = {0x00, 0x50, 0x20}; /* hard-coded */
157 const uint8_t lifetime[] = {0x02, 0x58}; /* 6s hard-coded */
158
159 const size_t bssgp_overhead = sizeof(*budh) +
160 TVLV_GROSS_LEN(sizeof(lifetime)) + llc_ie_hdr_size;
161 uint8_t *ie;
162 uint32_t tlli_be = htonl(tlli);
163
164 budh = (struct bssgp_ud_hdr *)msgb_push(msg, bssgp_overhead);
165
166 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
167 memcpy(&budh->tlli, &tlli_be, sizeof(budh->tlli));
168 memcpy(&budh->qos_profile, qos_profile, sizeof(budh->qos_profile));
169
170 ie = budh->data;
171 tvlv_put(ie, BSSGP_IE_PDU_LIFETIME, sizeof(lifetime), lifetime);
172 ie += TVLV_GROSS_LEN(sizeof(lifetime));
173
174 /* Note: Add alignment before the LLC IE if inserting other IE */
175
176 *(ie++) = BSSGP_IE_LLC_PDU;
177 *(ie++) = llc_size / 256;
178 *(ie++) = llc_size % 256;
179
180 OSMO_ASSERT(ie == llc);
181
182 msgb_bssgph(msg) = (uint8_t *)budh;
183 msgb_tlli(msg) = tlli;
184}
185
Jacob Erlbeck2cb9e3f2014-08-18 15:41:41 +0200186/* update peer according to the BSS message */
187static void gbprox_update_current_raid(uint8_t *raid_enc,
188 struct gbproxy_peer *peer,
189 const char *log_text)
Jacob Erlbeck2504bc42014-05-19 10:14:58 +0200190{
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +0200191 struct gbproxy_patch_state *state = &peer->patch_state;
Jacob Erlbeck2504bc42014-05-19 10:14:58 +0200192 const int old_local_mcc = state->local_mcc;
193 const int old_local_mnc = state->local_mnc;
Jacob Erlbeck2cb9e3f2014-08-18 15:41:41 +0200194 struct gprs_ra_id raid;
195
196 if (!raid_enc)
197 return;
198
199 gsm48_parse_ra(&raid, raid_enc);
200
201 /* save source side MCC/MNC */
202 if (!peer->cfg->core_mcc || raid.mcc == peer->cfg->core_mcc) {
203 state->local_mcc = 0;
204 } else {
205 state->local_mcc = raid.mcc;
206 }
207
208 if (!peer->cfg->core_mnc || raid.mnc == peer->cfg->core_mnc) {
209 state->local_mnc = 0;
210 } else {
211 state->local_mnc = raid.mnc;
212 }
213
214 if (old_local_mcc != state->local_mcc ||
215 old_local_mnc != state->local_mnc)
216 LOGP(DGPRS, LOGL_NOTICE,
217 "Patching RAID %sactivated, msg: %s, "
218 "local: %d-%d, core: %d-%d\n",
219 state->local_mcc || state->local_mnc ?
220 "" : "de",
221 log_text,
222 state->local_mcc, state->local_mnc,
223 peer->cfg->core_mcc, peer->cfg->core_mnc);
224}
225
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200226uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer,
227 uint32_t sgsn_ptmsi)
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200228{
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200229 uint32_t bss_ptmsi;
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200230 int max_retries = 23;
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200231 if (!peer->cfg->patch_ptmsi) {
232 bss_ptmsi = sgsn_ptmsi;
233 } else {
234 do {
235 bss_ptmsi = rand_r(&peer->cfg->bss_ptmsi_state);
236 bss_ptmsi = bss_ptmsi | 0xC0000000;
237
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200238 if (gbproxy_link_info_by_ptmsi(peer, bss_ptmsi))
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200239 bss_ptmsi = GSM_RESERVED_TMSI;
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200240 } while (bss_ptmsi == GSM_RESERVED_TMSI && max_retries--);
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200241 }
242
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200243 if (bss_ptmsi == GSM_RESERVED_TMSI)
244 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a BSS P-TMSI\n");
245
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200246 return bss_ptmsi;
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200247}
248
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200249uint32_t gbproxy_make_sgsn_tlli(struct gbproxy_peer *peer,
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200250 struct gbproxy_link_info *link_info,
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200251 uint32_t bss_tlli)
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200252{
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200253 uint32_t sgsn_tlli;
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200254 int max_retries = 23;
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200255 if (!peer->cfg->patch_ptmsi) {
256 sgsn_tlli = bss_tlli;
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200257 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI) {
258 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200259 TLLI_FOREIGN);
260 } else {
261 do {
262 /* create random TLLI, 0b01111xxx... */
263 sgsn_tlli = rand_r(&peer->cfg->sgsn_tlli_state);
264 sgsn_tlli = (sgsn_tlli & 0x7fffffff) | 0x78000000;
265
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200266 if (gbproxy_link_info_by_any_sgsn_tlli(peer, sgsn_tlli))
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200267 sgsn_tlli = 0;
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200268 } while (!sgsn_tlli && max_retries--);
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200269 }
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200270
271 if (!sgsn_tlli)
272 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate an SGSN TLLI\n");
273
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200274 return sgsn_tlli;
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200275}
276
Jacob Erlbeck4abbbdd2014-09-22 13:30:46 +0200277void gbproxy_reset_link(struct gbproxy_link_info *link_info)
278{
279 gbproxy_reset_imsi_acquisition(link_info);
280}
281
Jacob Erlbeck6e291e02014-09-03 11:59:48 +0200282/* Returns != 0 iff IMSI acquisition was in progress */
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200283static int gbproxy_restart_imsi_acquisition(struct gbproxy_link_info* link_info)
Jacob Erlbeck6e291e02014-09-03 11:59:48 +0200284{
285 int in_progress = 0;
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200286 if (!link_info)
Jacob Erlbeck6e291e02014-09-03 11:59:48 +0200287 return 0;
288
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200289 if (link_info->imsi_acq_pending) {
Jacob Erlbeck6e291e02014-09-03 11:59:48 +0200290 in_progress = 1;
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200291 link_info->imsi_acq_retries += 1;
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200292 }
Jacob Erlbeck6e291e02014-09-03 11:59:48 +0200293
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200294 gbproxy_link_info_discard_messages(link_info);
295 link_info->imsi_acq_pending = 0;
Jacob Erlbeck6e291e02014-09-03 11:59:48 +0200296
297 return in_progress;
298}
299
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200300static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info)
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200301{
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200302 gbproxy_restart_imsi_acquisition(link_info);
303 link_info->imsi_acq_retries = 0;
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200304}
305
306static void gbproxy_flush_stored_messages(struct gbproxy_peer *peer,
307 struct msgb *msg,
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200308 time_t now,
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200309 struct gbproxy_link_info* link_info,
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200310 struct gprs_gb_parse_context *parse_ctx)
311{
312 int rc;
313 struct msgb *stored_msg;
314 /* Got identity response with IMSI, assuming the request had
315 * been generated by the gbproxy */
316
317 LOGP(DLLC, LOGL_DEBUG,
318 "NSEI=%d(BSS) IMSI acquisition succeeded, "
319 "flushing stored messages\n",
320 msgb_nsei(msg));
321
322 /* Patch and flush stored messages towards the SGSN */
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200323 while ((stored_msg = msgb_dequeue(&link_info->stored_msgs))) {
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200324 struct gprs_gb_parse_context tmp_parse_ctx = {0};
325 tmp_parse_ctx.to_bss = 0;
Jacob Erlbeckd918f522014-09-17 10:56:38 +0200326 tmp_parse_ctx.peer_nsei = msgb_nsei(stored_msg);
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200327 int len_change = 0;
328
329 gprs_gb_parse_bssgp(msgb_bssgph(stored_msg),
330 msgb_bssgp_len(stored_msg),
331 &tmp_parse_ctx);
332 gbproxy_patch_bssgp(msg, msgb_bssgph(stored_msg),
333 msgb_bssgp_len(stored_msg),
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200334 peer, link_info, &len_change,
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200335 &tmp_parse_ctx);
336
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200337 gbproxy_update_link_state_after(peer, link_info, now,
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200338 &tmp_parse_ctx);
339
340 rc = gbprox_relay2sgsn(peer->cfg, stored_msg,
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200341 msgb_bvci(msg), link_info->sgsn_nsei);
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200342
343 if (rc < 0)
344 LOGP(DLLC, LOGL_ERROR,
345 "NSEI=%d(BSS) failed to send stored message "
346 "(%s)\n",
347 msgb_nsei(msg),
348 parse_ctx->llc_msg_name ?
349 parse_ctx->llc_msg_name : "BSSGP");
350 msgb_free(stored_msg);
351 }
352}
353
354static void gbproxy_acquire_imsi(struct gbproxy_peer *peer,
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200355 struct gbproxy_link_info* link_info,
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200356 uint16_t bvci)
357{
358 struct msgb *idreq_msg;
359
360 /* Send IDENT REQ */
361 idreq_msg = gsm48_msgb_alloc();
362 gprs_put_identity_req(idreq_msg, GSM_MI_TYPE_IMSI);
363 /* Workaround to avoid N(U) collisions and to enable a restart
364 * of the IMSI acquisition procedure. This will work unless the
365 * SGSN has an initial V(UT) within [256-32, 256+n_retries]
366 * (see GSM 04.64, 8.4.2).
367 * TODO: Check whether it is sensible to rely on this, it might
368 * be an issue when P-TMSI patching is _not_ enabled and the SGSN
369 * doesn't reset V(UT) after a detach. */
370 gprs_push_llc_ui(idreq_msg, 0, GPRS_SAPI_GMM,
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200371 (link_info->imsi_acq_retries + 256) % 512);
372 gprs_push_bssgp_dl_unitdata(idreq_msg, link_info->tlli.current);
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200373 gbprox_relay2peer(idreq_msg, peer, bvci);
374 msgb_free(idreq_msg);
375}
376
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200377static void gbproxy_tx_detach_acc(struct gbproxy_peer *peer,
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200378 struct gbproxy_link_info* link_info,
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200379 uint16_t bvci)
380{
381 struct msgb *detacc_msg;
382
383 /* Send DETACH ACC */
384 detacc_msg = gsm48_msgb_alloc();
385 gprs_put_mo_detach_acc(detacc_msg);
386 gprs_push_llc_ui(detacc_msg, 0, GPRS_SAPI_GMM,
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200387 (link_info->imsi_acq_retries + 256) % 512);
388 gprs_push_bssgp_dl_unitdata(detacc_msg, link_info->tlli.current);
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200389 gbprox_relay2peer(detacc_msg, peer, bvci);
390 msgb_free(detacc_msg);
391}
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200392
393/* Return != 0 iff msg still needs to be processed */
394static int gbproxy_imsi_acquisition(struct gbproxy_peer *peer,
395 struct msgb *msg,
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200396 time_t now,
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200397 struct gbproxy_link_info* link_info,
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200398 struct gprs_gb_parse_context *parse_ctx)
399{
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200400 struct msgb *stored_msg;
401
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200402 if (!link_info)
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200403 return 1;
404
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200405 if (!link_info->imsi_acq_pending && link_info->imsi_len > 0)
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200406 return 1;
407
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200408 if (parse_ctx->g48_hdr)
409 switch (parse_ctx->g48_hdr->msg_type)
410 {
Jacob Erlbecka93b4912014-09-18 10:08:27 +0200411 case GSM48_MT_GMM_RA_UPD_REQ:
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200412 case GSM48_MT_GMM_ATTACH_REQ:
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200413 if (gbproxy_restart_imsi_acquisition(link_info)) {
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200414 LOGP(DLLC, LOGL_INFO,
415 "NSEI=%d(BSS) IMSI acquisition was in progress "
Jacob Erlbecka93b4912014-09-18 10:08:27 +0200416 "when receiving an %s.\n",
417 msgb_nsei(msg), parse_ctx->llc_msg_name);
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200418 }
419 break;
420 case GSM48_MT_GMM_DETACH_REQ:
421 /* Nothing has been sent to the SGSN yet */
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200422 if (link_info->imsi_acq_pending) {
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200423 LOGP(DLLC, LOGL_INFO,
424 "NSEI=%d(BSS) IMSI acquisition was in progress "
425 "when receiving a DETACH_REQ.\n",
426 msgb_nsei(msg));
427 }
428 if (!parse_ctx->invalidate_tlli) {
429 LOGP(DLLC, LOGL_INFO,
430 "NSEI=%d(BSS) IMSI not yet acquired, "
431 "faking a DETACH_ACC.\n",
432 msgb_nsei(msg));
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200433 gbproxy_tx_detach_acc(peer, link_info, msgb_bvci(msg));
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200434 parse_ctx->invalidate_tlli = 1;
435 }
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200436 gbproxy_reset_imsi_acquisition(link_info);
437 gbproxy_update_link_state_after(peer, link_info, now,
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200438 parse_ctx);
439 return 0;
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200440 }
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200441
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200442 if (link_info->imsi_acq_pending && link_info->imsi_len > 0) {
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200443 int is_ident_resp =
444 parse_ctx->g48_hdr &&
445 parse_ctx->g48_hdr->proto_discr == GSM48_PDISC_MM_GPRS &&
446 parse_ctx->g48_hdr->msg_type == GSM48_MT_GMM_ID_RESP;
447
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200448 /* The IMSI is now available */
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200449 gbproxy_flush_stored_messages(peer, msg, now, link_info,
Jacob Erlbeck19a7c7a2014-09-19 13:23:21 +0200450 parse_ctx);
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200451
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200452 gbproxy_reset_imsi_acquisition(link_info);
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200453
454 /* This message is most probably the response to the ident
455 * request sent by gbproxy_acquire_imsi(). Don't forward it to
456 * the SGSN. */
457 return !is_ident_resp;
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200458 }
459
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200460 /* The message cannot be processed since the IMSI is still missing */
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200461
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200462 /* Enqueue unpatched messages */
463 LOGP(DLLC, LOGL_INFO,
464 "NSEI=%d(BSS) IMSI acquisition in progress, "
465 "storing message (%s)\n",
466 msgb_nsei(msg),
467 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200468
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200469 stored_msg = gprs_msgb_copy(msg, "process_bssgp_ul");
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200470 msgb_enqueue(&link_info->stored_msgs, stored_msg);
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200471
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200472 if (!link_info->imsi_acq_pending) {
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200473 LOGP(DLLC, LOGL_INFO,
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200474 "NSEI=%d(BSS) IMSI is required but not available, "
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200475 "initiating identification procedure (%s)\n",
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200476 msgb_nsei(msg),
477 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
478
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200479 gbproxy_acquire_imsi(peer, link_info, msgb_bvci(msg));
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200480
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200481 /* There is no explicit retransmission handling, the
482 * implementation relies on the MS doing proper retransmissions
483 * of the triggering message instead */
484
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200485 link_info->imsi_acq_pending = 1;
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200486 }
487
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200488 return 0;
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200489}
490
491struct gbproxy_peer *gbproxy_find_peer(struct gbproxy_config *cfg,
492 struct msgb *msg,
493 struct gprs_gb_parse_context *parse_ctx)
494{
495 struct gbproxy_peer *peer = NULL;
496
497 if (msgb_bvci(msg) >= 2)
498 peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg));
499
500 if (!peer && !parse_ctx->to_bss)
501 peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg));
502
503 if (!peer)
504 peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx->bssgp_tp);
505
506 if (!peer) {
507 LOGP(DLLC, LOGL_INFO,
508 "NSEI=%d(%s) patching: didn't find peer for message, "
509 "PDU %d\n",
510 msgb_nsei(msg), parse_ctx->to_bss ? "BSS" : "SGSN",
511 parse_ctx->pdu_type);
512 /* Increment counter */
513 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]);
514 }
515 return peer;
516}
517
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200518/* patch BSSGP message */
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200519static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg,
520 struct msgb *msg,
521 struct gbproxy_peer *peer)
Jacob Erlbeck0f9273b2014-08-07 16:07:24 +0200522{
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200523 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck4dc663f2014-08-06 12:41:31 +0200524 int rc;
Jacob Erlbeck0f9273b2014-08-07 16:07:24 +0200525 int len_change = 0;
Jacob Erlbeckc404c082014-08-08 08:37:37 +0200526 time_t now;
Jacob Erlbeck7c276fd2014-09-19 16:34:01 +0200527 struct timespec ts = {0,};
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200528 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeck02817f02014-08-27 12:51:07 +0200529 uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei;
Jacob Erlbeck4dc663f2014-08-06 12:41:31 +0200530
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200531 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
Jacob Erlbeck49f0c3e2014-09-04 11:42:08 +0200532 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200533 return 1;
Jacob Erlbeck4dc663f2014-08-06 12:41:31 +0200534
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200535 parse_ctx.to_bss = 0;
Jacob Erlbeckd918f522014-09-17 10:56:38 +0200536 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200537
538 /* Parse BSSGP/LLC */
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200539 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
540 &parse_ctx);
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200541
542 if (!rc) {
543 if (!parse_ctx.need_decryption) {
544 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeck4b5070e2014-08-18 17:26:04 +0200545 "NSEI=%u(BSS) patching: "
546 "failed to parse BSSGP/GMM message\n",
547 msgb_nsei(msg));
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200548 return 0;
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200549 }
550 }
551
552 /* Get peer */
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200553 if (!peer)
554 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200555
556 if (!peer)
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200557 return 0;
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200558
Jacob Erlbeck7c276fd2014-09-19 16:34:01 +0200559
560 clock_gettime(CLOCK_MONOTONIC, &ts);
561 now = ts.tv_sec;
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200562
Jacob Erlbeck2cb9e3f2014-08-18 15:41:41 +0200563 gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer,
564 parse_ctx.llc_msg_name);
565
Jacob Erlbeck6e291e02014-09-03 11:59:48 +0200566 gprs_gb_log_parse_context(&parse_ctx, "BSSGP");
567
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200568 link_info = gbproxy_update_link_state_ul(peer, now, &parse_ctx);
Jacob Erlbeck6e291e02014-09-03 11:59:48 +0200569
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200570 if (parse_ctx.g48_hdr) {
571 switch (parse_ctx.g48_hdr->msg_type) {
572 case GSM48_MT_GMM_ATTACH_REQ:
573 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]);
574 break;
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200575
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200576 default:
577 break;
578 }
579 }
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200580
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200581 if (link_info && cfg->route_to_sgsn2) {
582 if (cfg->acquire_imsi && link_info->imsi_len == 0)
Jacob Erlbeckd918f522014-09-17 10:56:38 +0200583 sgsn_nsei = 0xffff;
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200584 else if (gbproxy_imsi_matches(peer, link_info))
Jacob Erlbeckd918f522014-09-17 10:56:38 +0200585 sgsn_nsei = cfg->nsip_sgsn2_nsei;
Jacob Erlbeck02817f02014-08-27 12:51:07 +0200586 }
587
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200588 if (link_info)
589 link_info->sgsn_nsei = sgsn_nsei;
Jacob Erlbeckd918f522014-09-17 10:56:38 +0200590
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200591 /* Handle IMSI acquisition */
592 if (cfg->acquire_imsi) {
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200593 rc = gbproxy_imsi_acquisition(peer, msg, now, link_info,
Jacob Erlbeck19a7c7a2014-09-19 13:23:21 +0200594 &parse_ctx);
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200595 if (rc <= 0)
596 return rc;
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200597 }
598
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200599 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200600 peer, link_info, &len_change, &parse_ctx);
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200601
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200602 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200603
Jacob Erlbeckd918f522014-09-17 10:56:38 +0200604 if (sgsn_nsei != cfg->nsip_sgsn_nsei) {
Jacob Erlbeck02817f02014-08-27 12:51:07 +0200605 /* Send message directly to the selected SGSN */
606 rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei);
607 /* Don't let the calling code handle the transmission */
608 return 0;
609 }
610
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200611 return 1;
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200612}
613
614/* patch BSSGP message to use core_mcc/mnc on the SGSN side */
615static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg,
616 struct msgb *msg,
617 struct gbproxy_peer *peer)
618{
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200619 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200620 int rc;
621 int len_change = 0;
622 time_t now;
Jacob Erlbeck7c276fd2014-09-19 16:34:01 +0200623 struct timespec ts = {0,};
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200624 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200625
Jacob Erlbeck49f0c3e2014-09-04 11:42:08 +0200626 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
627 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
628 return;
629
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200630 parse_ctx.to_bss = 1;
Jacob Erlbeckd918f522014-09-17 10:56:38 +0200631 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck4dc663f2014-08-06 12:41:31 +0200632
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200633 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
634 &parse_ctx);
Jacob Erlbeck4dc663f2014-08-06 12:41:31 +0200635
636 if (!rc) {
637 if (!parse_ctx.need_decryption) {
638 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeck4b5070e2014-08-18 17:26:04 +0200639 "NSEI=%u(SGSN) patching: "
640 "failed to parse BSSGP/GMM message\n",
641 msgb_nsei(msg));
Jacob Erlbeck4dc663f2014-08-06 12:41:31 +0200642 return;
643 }
644 }
Jacob Erlbeck2504bc42014-05-19 10:14:58 +0200645
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200646 /* Get peer */
647 if (!peer)
648 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck90a1fd12014-05-27 13:49:04 +0200649
Jacob Erlbeck90a1fd12014-05-27 13:49:04 +0200650 if (!peer)
Jacob Erlbeck90a1fd12014-05-27 13:49:04 +0200651 return;
Jacob Erlbeck90a1fd12014-05-27 13:49:04 +0200652
Jacob Erlbeck7c276fd2014-09-19 16:34:01 +0200653 clock_gettime(CLOCK_MONOTONIC, &ts);
654 now = ts.tv_sec;
Jacob Erlbeckc404c082014-08-08 08:37:37 +0200655
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200656 if (parse_ctx.g48_hdr) {
657 switch (parse_ctx.g48_hdr->msg_type) {
658 case GSM48_MT_GMM_ATTACH_REJ:
659 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]);
660 break;
Jacob Erlbeck0727f9a2014-05-28 10:59:10 +0200661
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200662 default:
663 break;
664 }
665 }
Jacob Erlbeck4dc663f2014-08-06 12:41:31 +0200666
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200667 gprs_gb_log_parse_context(&parse_ctx, "BSSGP");
668
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200669 link_info = gbproxy_update_link_state_dl(peer, now, &parse_ctx);
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200670
671 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200672 peer, link_info, &len_change, &parse_ctx);
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200673
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200674 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
Jacob Erlbeck4dc663f2014-08-06 12:41:31 +0200675
Jacob Erlbeckf14bb912014-06-27 19:56:05 +0200676 return;
Jacob Erlbeck2504bc42014-05-19 10:14:58 +0200677}
678
Harald Welted1991e72010-04-30 20:26:32 +0200679/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200680static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +0200681 uint16_t ns_bvci, uint16_t sgsn_nsei)
Harald Weltef01709d2010-05-03 18:54:58 +0200682{
Harald Welte3fb22d62010-05-12 18:10:25 +0000683 /* create a copy of the message so the old one can
684 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther8ac3a722014-08-04 11:52:52 +0200685 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2sgsn");
Jacob Erlbeck174bad42013-10-18 14:34:55 +0200686 int rc;
Harald Welte3fb22d62010-05-12 18:10:25 +0000687
Harald Welteb8da0612010-05-11 20:20:13 +0200688 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +0200689 msgb_nsei(msg), ns_bvci, sgsn_nsei);
Harald Welte6aa24d12010-05-03 19:22:32 +0200690
Harald Weltef01709d2010-05-03 18:54:58 +0200691 msgb_bvci(msg) = ns_bvci;
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +0200692 msgb_nsei(msg) = sgsn_nsei;
Harald Weltef01709d2010-05-03 18:54:58 +0200693
Harald Welte3ad32432010-05-03 19:05:10 +0200694 strip_ns_hdr(msg);
695
Jacob Erlbeck174bad42013-10-18 14:34:55 +0200696 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
697 if (rc < 0)
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200698 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Jacob Erlbeck174bad42013-10-18 14:34:55 +0200699
700 return rc;
Harald Weltef01709d2010-05-03 18:54:58 +0200701}
702
Harald Weltef01709d2010-05-03 18:54:58 +0200703/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +0200704static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
Harald Welted1991e72010-04-30 20:26:32 +0200705 uint16_t ns_bvci)
706{
Harald Welte3fb22d62010-05-12 18:10:25 +0000707 /* create a copy of the message so the old one can
708 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther8ac3a722014-08-04 11:52:52 +0200709 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2peer");
Jacob Erlbeck174bad42013-10-18 14:34:55 +0200710 int rc;
Harald Welte3fb22d62010-05-12 18:10:25 +0000711
Harald Welte0cc900d2010-05-13 10:34:56 +0200712 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200713 msgb_nsei(msg), ns_bvci, peer->nsei);
Harald Welte6aa24d12010-05-03 19:22:32 +0200714
Harald Welted1991e72010-04-30 20:26:32 +0200715 msgb_bvci(msg) = ns_bvci;
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200716 msgb_nsei(msg) = peer->nsei;
Harald Welted1991e72010-04-30 20:26:32 +0200717
Harald Welte0cc900d2010-05-13 10:34:56 +0200718 /* Strip the old NS header, it will be replaced with a new one */
Harald Welte3ad32432010-05-03 19:05:10 +0200719 strip_ns_hdr(msg);
720
Jacob Erlbeck174bad42013-10-18 14:34:55 +0200721 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
722 if (rc < 0)
723 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
724
725 return rc;
Harald Welted1991e72010-04-30 20:26:32 +0200726}
727
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200728static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type)
Harald Welte51b7d402011-02-06 13:09:29 +0100729{
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +0200730 struct gbproxy_peer *peer;
Harald Welte51b7d402011-02-06 13:09:29 +0100731
Jacob Erlbeck040fabc2014-08-21 10:01:30 +0200732 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte51b7d402011-02-06 13:09:29 +0100733 if (!peer) {
734 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
735 ptp_bvci);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200736 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte51b7d402011-02-06 13:09:29 +0100737 return -ENOENT;
738 }
739
740 switch (pdu_type) {
741 case BSSGP_PDUT_BVC_BLOCK_ACK:
742 peer->blocked = 1;
Jacob Erlbeck174bad42013-10-18 14:34:55 +0200743 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
Harald Welte51b7d402011-02-06 13:09:29 +0100744 break;
745 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
746 peer->blocked = 0;
Jacob Erlbeck174bad42013-10-18 14:34:55 +0200747 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
Harald Welte51b7d402011-02-06 13:09:29 +0100748 break;
749 default:
750 break;
751 }
752 return 0;
753}
754
Harald Welted1991e72010-04-30 20:26:32 +0200755/* Send a message to a peer identified by ptp_bvci but using ns_bvci
756 * in the NS hdr */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200757static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci,
Harald Welted1991e72010-04-30 20:26:32 +0200758 uint16_t ns_bvci)
759{
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +0200760 struct gbproxy_peer *peer;
Harald Welted1991e72010-04-30 20:26:32 +0200761
Jacob Erlbeck040fabc2014-08-21 10:01:30 +0200762 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte0358aae2010-05-03 21:37:11 +0200763 if (!peer) {
Harald Welte93048cf2010-05-13 14:14:56 +0200764 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
Harald Welte0358aae2010-05-03 21:37:11 +0200765 ptp_bvci);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200766 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welted1991e72010-04-30 20:26:32 +0200767 return -ENOENT;
Harald Welte0358aae2010-05-03 21:37:11 +0200768 }
Harald Welted1991e72010-04-30 20:26:32 +0200769
Harald Welte3ad32432010-05-03 19:05:10 +0200770 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200771}
772
Harald Weltea62e9fe2012-06-17 12:16:31 +0800773int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
774{
775 return 0;
776}
777
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200778/* Receive an incoming PTP message from a BSS-side NS-VC */
779static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg,
780 struct msgb *msg, uint16_t nsei,
781 uint16_t nsvci, uint16_t ns_bvci)
782{
783 struct gbproxy_peer *peer;
Jacob Erlbeck89033782014-08-28 13:20:39 +0200784 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
785 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200786 int rc;
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200787
788 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
789
790 if (peer)
791 check_peer_nsei(peer, nsei);
792
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200793 rc = gbprox_process_bssgp_ul(cfg, msg, peer);
794 if (!rc)
795 return 0;
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200796
Jacob Erlbeck89033782014-08-28 13:20:39 +0200797 switch (pdu_type) {
798 case BSSGP_PDUT_FLOW_CONTROL_BVC:
799 if (!cfg->route_to_sgsn2)
800 break;
801
802 /* Send a copy to the secondary SGSN */
803 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
804 break;
805 default:
806 break;
807 }
808
809
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +0200810 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200811}
812
813/* Receive an incoming PTP message from a SGSN-side NS-VC */
814static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg,
815 struct msgb *msg, uint16_t nsei,
816 uint16_t nsvci, uint16_t ns_bvci)
817{
818 struct gbproxy_peer *peer;
Jacob Erlbeck89033782014-08-28 13:20:39 +0200819 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
820 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200821
822 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
823
Jacob Erlbeck85a9f542014-08-21 15:07:11 +0200824 /* Send status messages before patching */
825
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200826 if (!peer) {
827 LOGP(DGPRS, LOGL_INFO, "Didn't find peer for "
828 "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n",
829 ns_bvci, nsvci, nsei);
830 rate_ctr_inc(&cfg->ctrg->
831 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
832 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
833 &ns_bvci, msg);
834 }
835
836 if (peer->blocked) {
837 LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for "
838 "blocked BVCI=%u via NSVC=%u/NSEI=%u\n",
839 ns_bvci, nsvci, nsei);
840 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Jacob Erlbeckd9bc3a52014-08-29 12:20:15 +0200841 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200842 }
843
Jacob Erlbeck89033782014-08-28 13:20:39 +0200844 switch (pdu_type) {
845 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
846 case BSSGP_PDUT_BVC_BLOCK_ACK:
847 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
848 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
849 /* Hide ACKs from the secondary SGSN, the primary SGSN
850 * is responsible to send them. */
851 return 0;
852 break;
853 default:
854 break;
855 }
856
Jacob Erlbeck85a9f542014-08-21 15:07:11 +0200857 /* Optionally patch the message */
858 gbprox_process_bssgp_dl(cfg, msg, peer);
859
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200860 return gbprox_relay2peer(msg, peer, ns_bvci);
861}
862
Harald Welted1991e72010-04-30 20:26:32 +0200863/* Receive an incoming signalling message from a BSS-side NS-VC */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200864static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg,
865 struct msgb *msg, uint16_t nsei,
Harald Welted1991e72010-04-30 20:26:32 +0200866 uint16_t ns_bvci)
867{
Harald Welte2ac2e912010-05-03 16:30:59 +0200868 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welted1991e72010-04-30 20:26:32 +0200869 struct tlv_parsed tp;
870 uint8_t pdu_type = bgph->pdu_type;
Harald Welte2ac2e912010-05-03 16:30:59 +0200871 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +0200872 struct gbproxy_peer *from_peer = NULL;
Harald Weltee49c8292010-05-01 12:10:57 +0200873 struct gprs_ra_id raid;
Jacob Erlbeck89033782014-08-28 13:20:39 +0200874 int copy_to_sgsn2 = 0;
Jacob Erlbeck4d16d6c2014-09-08 09:04:01 +0200875 int rc;
Harald Welted1991e72010-04-30 20:26:32 +0200876
Harald Welte41270722011-02-06 17:13:12 +0100877 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte93048cf2010-05-13 14:14:56 +0200878 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200879 nsei, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200880 return -EINVAL;
881 }
882
883 /* we actually should never see those two for BVCI == 0, but double-check
884 * just to make sure */
885 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
886 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte6aa24d12010-05-03 19:22:32 +0200887 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200888 "signalling\n", nsei);
Harald Welted1991e72010-04-30 20:26:32 +0200889 return -EINVAL;
890 }
891
892 bssgp_tlv_parse(&tp, bgph->data, data_len);
893
894 switch (pdu_type) {
895 case BSSGP_PDUT_SUSPEND:
896 case BSSGP_PDUT_RESUME:
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +0200897 /* We implement RAI snooping during SUSPEND/RESUME, since it
898 * establishes a relationsip between BVCI/peer and the routeing
899 * area identification. The snooped information is then used
900 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
901 * BSSGP */
Harald Welted1991e72010-04-30 20:26:32 +0200902 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
903 goto err_mand_ie;
Jacob Erlbeck040fabc2014-08-21 10:01:30 +0200904 from_peer = gbproxy_peer_by_nsei(cfg, nsei);
Harald Welted1991e72010-04-30 20:26:32 +0200905 if (!from_peer)
906 goto err_no_peer;
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200907 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
908 sizeof(from_peer->ra));
Harald Weltef2fdefe2010-05-11 10:15:42 +0200909 gsm48_parse_ra(&raid, from_peer->ra);
Harald Welte93048cf2010-05-13 14:14:56 +0200910 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +0200911 "RAI snooping: RAI %u-%u-%u-%u behind BVCI=%u\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200912 nsei, raid.mcc, raid.mnc, raid.lac,
913 raid.rac , from_peer->bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200914 /* FIXME: This only supports one BSS per RA */
915 break;
Harald Welte6aa24d12010-05-03 19:22:32 +0200916 case BSSGP_PDUT_BVC_RESET:
917 /* If we receive a BVC reset on the signalling endpoint, we
918 * don't want the SGSN to reset, as the signalling endpoint
919 * is common for all point-to-point BVCs (and thus all BTS) */
920 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freyther25eeaab2012-09-04 20:42:20 +0200921 uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Weltebe6e25a2010-05-12 00:20:41 +0200922 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200923 nsei, bvci);
Harald Welte6aa24d12010-05-03 19:22:32 +0200924 if (bvci == 0) {
925 /* FIXME: only do this if SGSN is alive! */
Harald Welteb8da0612010-05-11 20:20:13 +0200926 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200927 "BVC RESET ACK of BVCI=0\n", nsei);
Harald Welte6aa24d12010-05-03 19:22:32 +0200928 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200929 nsei, 0, ns_bvci);
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200930 }
Jacob Erlbeck040fabc2014-08-21 10:01:30 +0200931 from_peer = gbproxy_peer_by_bvci(cfg, bvci);
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200932 if (!from_peer) {
Harald Welte0358aae2010-05-03 21:37:11 +0200933 /* if a PTP-BVC is reset, and we don't know that
934 * PTP-BVCI yet, we should allocate a new peer */
935 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200936 "BVCI=%u via NSEI=%u\n", bvci, nsei);
Holger Hans Peter Freyther60fa5b92014-08-04 17:10:08 +0200937 from_peer = gbproxy_peer_alloc(cfg, bvci);
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200938 from_peer->nsei = nsei;
Harald Welte6aa24d12010-05-03 19:22:32 +0200939 }
Jacob Erlbeckd0a9b502013-10-24 12:48:56 +0200940
941 if (!check_peer_nsei(from_peer, nsei))
942 from_peer->nsei = nsei;
943
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200944 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
945 struct gprs_ra_id raid;
946 /* We have a Cell Identifier present in this
947 * PDU, this means we can extend our local
948 * state information about this particular cell
949 * */
950 memcpy(from_peer->ra,
951 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
952 sizeof(from_peer->ra));
953 gsm48_parse_ra(&raid, from_peer->ra);
954 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u "
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200955 "Cell ID %u-%u-%u-%u\n", nsei,
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200956 bvci, raid.mcc, raid.mnc, raid.lac,
957 raid.rac);
958 }
Jacob Erlbeck89033782014-08-28 13:20:39 +0200959 if (cfg->route_to_sgsn2)
960 copy_to_sgsn2 = 1;
Harald Welte6aa24d12010-05-03 19:22:32 +0200961 }
962 break;
Harald Welted1991e72010-04-30 20:26:32 +0200963 }
964
Harald Welteb8da0612010-05-11 20:20:13 +0200965 /* Normally, we can simply pass on all signalling messages from BSS to
966 * SGSN */
Jacob Erlbeck4d16d6c2014-09-08 09:04:01 +0200967 rc = gbprox_process_bssgp_ul(cfg, msg, from_peer);
968 if (!rc)
969 return 0;
Jacob Erlbeck89033782014-08-28 13:20:39 +0200970
971 if (copy_to_sgsn2)
972 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
973
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +0200974 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Harald Welted1991e72010-04-30 20:26:32 +0200975err_no_peer:
Jacob Erlbeck700b8732013-10-18 13:04:48 +0200976 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200977 nsei);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200978 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Jacob Erlbeckd9bc3a52014-08-29 12:20:15 +0200979 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200980err_mand_ie:
Harald Welte874acec2010-05-11 10:01:17 +0200981 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200982 nsei);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200983 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welte874acec2010-05-11 10:01:17 +0200984 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200985}
986
987/* Receive paging request from SGSN, we need to relay to proper BSS */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200988static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp,
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200989 uint32_t nsei, uint16_t ns_bvci)
Harald Welted1991e72010-04-30 20:26:32 +0200990{
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +0200991 struct gbproxy_peer *peer = NULL;
Jacob Erlbeck174bad42013-10-18 14:34:55 +0200992 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welted1991e72010-04-30 20:26:32 +0200993
Harald Welte93048cf2010-05-13 14:14:56 +0200994 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200995 nsei);
Harald Welted1991e72010-04-30 20:26:32 +0200996 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freyther25eeaab2012-09-04 20:42:20 +0200997 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte93048cf2010-05-13 14:14:56 +0200998 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n",
999 bvci);
Jacob Erlbeck174bad42013-10-18 14:34:55 +02001000 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welted1991e72010-04-30 20:26:32 +02001001 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
Jacob Erlbeck040fabc2014-08-21 10:01:30 +02001002 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +02001003 LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n",
Holger Hans Peter Freyther98b82c12010-06-08 16:30:24 +08001004 peer ? peer->bvci : -1);
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +02001005 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welted1991e72010-04-30 20:26:32 +02001006 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
Jacob Erlbeck040fabc2014-08-21 10:01:30 +02001007 peer = gbproxy_peer_by_lai(cfg, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +02001008 LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n",
Holger Hans Peter Freyther98b82c12010-06-08 16:30:24 +08001009 peer ? peer->bvci : -1);
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +02001010 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welted1991e72010-04-30 20:26:32 +02001011 } else
Harald Welte93048cf2010-05-13 14:14:56 +02001012 LOGPC(DGPRS, LOGL_INFO, "\n");
1013
1014 if (!peer) {
1015 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001016 "unable to route, missing IE\n", nsei);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001017 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
Harald Welted1991e72010-04-30 20:26:32 +02001018 return -EINVAL;
Harald Welte93048cf2010-05-13 14:14:56 +02001019 }
1020 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +02001021}
1022
Harald Welte874acec2010-05-11 10:01:17 +02001023/* Receive an incoming BVC-RESET message from the SGSN */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001024static int rx_reset_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001025 struct msgb *orig_msg,
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001026 struct msgb *msg, struct tlv_parsed *tp,
1027 uint32_t nsei, uint16_t ns_bvci)
Harald Welte874acec2010-05-11 10:01:17 +02001028{
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +02001029 struct gbproxy_peer *peer;
Harald Welte874acec2010-05-11 10:01:17 +02001030 uint16_t ptp_bvci;
1031
1032 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001033 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeck174bad42013-10-18 14:34:55 +02001034 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte874acec2010-05-11 10:01:17 +02001035 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001036 NULL, orig_msg);
Harald Welte874acec2010-05-11 10:01:17 +02001037 }
Holger Hans Peter Freyther25eeaab2012-09-04 20:42:20 +02001038 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte874acec2010-05-11 10:01:17 +02001039
1040 if (ptp_bvci >= 2) {
1041 /* A reset for a PTP BVC was received, forward it to its
1042 * respective peer */
Jacob Erlbeck040fabc2014-08-21 10:01:30 +02001043 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte874acec2010-05-11 10:01:17 +02001044 if (!peer) {
Harald Welte93048cf2010-05-13 14:14:56 +02001045 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001046 nsei, ptp_bvci);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001047 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeck174bad42013-10-18 14:34:55 +02001048 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte874acec2010-05-11 10:01:17 +02001049 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
Jacob Erlbeckd9bc3a52014-08-29 12:20:15 +02001050 &ptp_bvci, orig_msg);
Harald Welte874acec2010-05-11 10:01:17 +02001051 }
1052 return gbprox_relay2peer(msg, peer, ns_bvci);
1053 }
1054
1055 /* A reset for the Signalling entity has been received
1056 * from the SGSN. As the signalling BVCI is shared
1057 * among all the BSS's that we multiplex, it needs to
1058 * be relayed */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001059 llist_for_each_entry(peer, &cfg->bts_peers, list)
Harald Welte874acec2010-05-11 10:01:17 +02001060 gbprox_relay2peer(msg, peer, ns_bvci);
1061
1062 return 0;
1063}
1064
Harald Welted1991e72010-04-30 20:26:32 +02001065/* Receive an incoming signalling message from the SGSN-side NS-VC */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001066static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001067 struct msgb *orig_msg, uint32_t nsei,
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001068 uint16_t ns_bvci)
Harald Welted1991e72010-04-30 20:26:32 +02001069{
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001070 struct bssgp_normal_hdr *bgph =
1071 (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg);
Harald Welted1991e72010-04-30 20:26:32 +02001072 struct tlv_parsed tp;
1073 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001074 int data_len;
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +02001075 struct gbproxy_peer *peer;
Harald Welted1991e72010-04-30 20:26:32 +02001076 uint16_t bvci;
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001077 struct msgb *msg;
Harald Welted1991e72010-04-30 20:26:32 +02001078 int rc = 0;
Jacob Erlbeck0936c8a2014-08-29 13:32:17 +02001079 int cause;
Harald Welted1991e72010-04-30 20:26:32 +02001080
Harald Welte41270722011-02-06 17:13:12 +01001081 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte93048cf2010-05-13 14:14:56 +02001082 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001083 "signalling\n", nsei, ns_bvci);
Harald Welte874acec2010-05-11 10:01:17 +02001084 /* FIXME: Send proper error message */
Harald Welted1991e72010-04-30 20:26:32 +02001085 return -EINVAL;
1086 }
1087
1088 /* we actually should never see those two for BVCI == 0, but double-check
1089 * just to make sure */
1090 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1091 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte6aa24d12010-05-03 19:22:32 +02001092 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001093 "signalling\n", nsei);
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001094 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welted1991e72010-04-30 20:26:32 +02001095 }
1096
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001097 msg = gprs_msgb_copy(orig_msg, "rx_sig_from_sgsn");
1098 gbprox_process_bssgp_dl(cfg, msg, NULL);
1099 /* Update message info */
1100 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1101 data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph);
Harald Welted1991e72010-04-30 20:26:32 +02001102 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1103
1104 switch (pdu_type) {
Harald Welte874acec2010-05-11 10:01:17 +02001105 case BSSGP_PDUT_BVC_RESET:
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001106 rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci);
Harald Welte874acec2010-05-11 10:01:17 +02001107 break;
Harald Welted1991e72010-04-30 20:26:32 +02001108 case BSSGP_PDUT_BVC_RESET_ACK:
Jacob Erlbeck89033782014-08-28 13:20:39 +02001109 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
1110 break;
1111 /* fall through */
1112 case BSSGP_PDUT_FLUSH_LL:
Harald Welted1991e72010-04-30 20:26:32 +02001113 /* simple case: BVCI IE is mandatory */
1114 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1115 goto err_mand_ie;
Holger Hans Peter Freyther25eeaab2012-09-04 20:42:20 +02001116 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001117 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +02001118 break;
1119 case BSSGP_PDUT_PAGING_PS:
1120 case BSSGP_PDUT_PAGING_CS:
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +02001121 /* process the paging request (LAI/RAI lookup) */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001122 rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +02001123 break;
1124 case BSSGP_PDUT_STATUS:
Harald Welte874acec2010-05-11 10:01:17 +02001125 /* Some exception has occurred */
Harald Welte6aa24d12010-05-03 19:22:32 +02001126 LOGP(DGPRS, LOGL_NOTICE,
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001127 "NSEI=%u(SGSN) BSSGP STATUS ", nsei);
Harald Welte874acec2010-05-11 10:01:17 +02001128 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
1129 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1130 goto err_mand_ie;
1131 }
Jacob Erlbeck0936c8a2014-08-29 13:32:17 +02001132 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
Harald Welte874acec2010-05-11 10:01:17 +02001133 LOGPC(DGPRS, LOGL_NOTICE,
1134 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
Jacob Erlbeck0936c8a2014-08-29 13:32:17 +02001135 bssgp_cause_str(cause));
Harald Welte874acec2010-05-11 10:01:17 +02001136 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Jacob Erlbeck0936c8a2014-08-29 13:32:17 +02001137 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1138 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%u\n", bvci);
1139
1140 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI)
1141 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte874acec2010-05-11 10:01:17 +02001142 } else
1143 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welted1991e72010-04-30 20:26:32 +02001144 break;
1145 /* those only exist in the SGSN -> BSS direction */
1146 case BSSGP_PDUT_SUSPEND_ACK:
1147 case BSSGP_PDUT_SUSPEND_NACK:
1148 case BSSGP_PDUT_RESUME_ACK:
1149 case BSSGP_PDUT_RESUME_NACK:
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +02001150 /* RAI IE is mandatory */
Harald Welted1991e72010-04-30 20:26:32 +02001151 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1152 goto err_mand_ie;
Jacob Erlbeck040fabc2014-08-21 10:01:30 +02001153 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
Harald Welted1991e72010-04-30 20:26:32 +02001154 if (!peer)
1155 goto err_no_peer;
Harald Welte3ad32432010-05-03 19:05:10 +02001156 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +02001157 break;
Harald Welte51b7d402011-02-06 13:09:29 +01001158 case BSSGP_PDUT_BVC_BLOCK_ACK:
1159 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1160 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1161 goto err_mand_ie;
Holger Hans Peter Freyther25eeaab2012-09-04 20:42:20 +02001162 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte51b7d402011-02-06 13:09:29 +01001163 if (bvci == 0) {
1164 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP "
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001165 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
Harald Welte51b7d402011-02-06 13:09:29 +01001166 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
1167 /* should we send STATUS ? */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001168 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeck174bad42013-10-18 14:34:55 +02001169 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte51b7d402011-02-06 13:09:29 +01001170 } else {
1171 /* Mark BVC as (un)blocked */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001172 block_unblock_peer(cfg, bvci, pdu_type);
Harald Welte51b7d402011-02-06 13:09:29 +01001173 }
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001174 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte51b7d402011-02-06 13:09:29 +01001175 break;
Harald Welted1991e72010-04-30 20:26:32 +02001176 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte6aa24d12010-05-03 19:22:32 +02001177 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001178 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001179 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeck174bad42013-10-18 14:34:55 +02001180 ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]);
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001181 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, orig_msg);
Harald Welted1991e72010-04-30 20:26:32 +02001182 break;
1183 default:
Harald Weltebe6e25a2010-05-12 00:20:41 +02001184 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n",
1185 pdu_type);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001186 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeck174bad42013-10-18 14:34:55 +02001187 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001188 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welted1991e72010-04-30 20:26:32 +02001189 break;
1190 }
1191
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001192 msgb_free(msg);
1193
Harald Welted1991e72010-04-30 20:26:32 +02001194 return rc;
1195err_mand_ie:
Harald Welte0358aae2010-05-03 21:37:11 +02001196 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001197 nsei);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001198 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeck174bad42013-10-18 14:34:55 +02001199 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001200 msgb_free(msg);
1201 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg);
Harald Welted1991e72010-04-30 20:26:32 +02001202err_no_peer:
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +02001203 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001204 nsei);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001205 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001206 msgb_free(msg);
Jacob Erlbeckd9bc3a52014-08-29 12:20:15 +02001207 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg);
Harald Welted1991e72010-04-30 20:26:32 +02001208}
1209
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +02001210static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei)
1211{
1212 return nsei == cfg->nsip_sgsn_nsei ||
1213 (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei);
1214}
1215
Harald Welted1991e72010-04-30 20:26:32 +02001216/* Main input function for Gb proxy */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001217int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei,
1218 uint16_t ns_bvci, uint16_t nsvci)
Harald Welted1991e72010-04-30 20:26:32 +02001219{
Harald Weltef01709d2010-05-03 18:54:58 +02001220 int rc;
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +02001221 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei);
Harald Welted1991e72010-04-30 20:26:32 +02001222
1223 /* Only BVCI=0 messages need special treatment */
1224 if (ns_bvci == 0 || ns_bvci == 1) {
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001225 if (remote_end_is_sgsn)
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001226 rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +02001227 else
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001228 rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci);
Harald Weltef01709d2010-05-03 18:54:58 +02001229 } else {
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +02001230 /* All other BVCI are PTP */
1231 if (remote_end_is_sgsn)
1232 rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei, nsvci,
1233 ns_bvci);
1234 else
1235 rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei, nsvci,
1236 ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +02001237 }
1238
Harald Weltef01709d2010-05-03 18:54:58 +02001239 return rc;
Harald Welted1991e72010-04-30 20:26:32 +02001240}
Harald Welteb34e2c12010-05-11 05:49:43 +02001241
Harald Welte9ee404a2010-05-14 11:53:08 +00001242int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi)
1243{
1244 struct gprs_nsvc *nsvc;
1245
1246 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
1247 if (!nsvc->persistent)
1248 continue;
1249 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1250 }
1251 return 0;
1252}
1253
Harald Welte5bb0d362010-05-11 06:34:24 +02001254/* Signal handler for signals from NS layer */
1255int gbprox_signal(unsigned int subsys, unsigned int signal,
1256 void *handler_data, void *signal_data)
1257{
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001258 struct gbproxy_config *cfg = handler_data;
Harald Welte5bb0d362010-05-11 06:34:24 +02001259 struct ns_signal_data *nssd = signal_data;
1260 struct gprs_nsvc *nsvc = nssd->nsvc;
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +02001261 struct gbproxy_peer *peer;
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +02001262 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsvc->nsei);
Harald Welte5bb0d362010-05-11 06:34:24 +02001263
Harald Welte7ed98e32012-06-16 16:40:42 +08001264 if (subsys != SS_L_NS)
Harald Welte5bb0d362010-05-11 06:34:24 +02001265 return 0;
1266
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +02001267 if (signal == S_NS_RESET && remote_end_is_sgsn) {
Harald Welte15cbb0e2010-05-11 18:29:44 +02001268 /* We have received a NS-RESET from the NSEI and NSVC
1269 * of the SGSN. This might happen with SGSN that start
1270 * their own NS-RESET procedure without waiting for our
1271 * NS-RESET */
1272 nsvc->remote_end_is_sgsn = 1;
1273 }
1274
Harald Weltefa41cb72010-05-12 13:28:25 +00001275 if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) {
1276 LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, "
1277 "re-starting RESET procedure\n");
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001278 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeck174bad42013-10-18 14:34:55 +02001279 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
Harald Weltea529c072012-06-17 12:25:53 +08001280 gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr,
1281 nsvc->nsei, nsvc->nsvci);
Harald Weltefa41cb72010-05-12 13:28:25 +00001282 }
1283
Harald Weltedb2a2d52011-02-06 16:33:29 +01001284 if (!nsvc->remote_end_is_sgsn) {
1285 /* from BSS to SGSN */
Jacob Erlbeck040fabc2014-08-21 10:01:30 +02001286 peer = gbproxy_peer_by_nsei(cfg, nsvc->nsei);
Harald Weltedb2a2d52011-02-06 16:33:29 +01001287 if (!peer) {
1288 LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer "
1289 "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei,
1290 nsvc->nsvci);
1291 return 0;
1292 }
Harald Welte5bb0d362010-05-11 06:34:24 +02001293 switch (signal) {
1294 case S_NS_RESET:
Harald Welte5bb0d362010-05-11 06:34:24 +02001295 case S_NS_BLOCK:
Harald Weltedb2a2d52011-02-06 16:33:29 +01001296 if (!peer->blocked)
1297 break;
1298 LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from "
1299 "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n",
1300 nsvc->nsei, nsvc->nsvci);
1301 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei,
1302 peer->bvci, 0);
Harald Welte5bb0d362010-05-11 06:34:24 +02001303 break;
Harald Weltedb2a2d52011-02-06 16:33:29 +01001304 }
1305 } else {
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001306 /* Forward this message to all NS-VC to BSS */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001307 struct gprs_ns_inst *nsi = cfg->nsi;
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001308 struct gprs_nsvc *next_nsvc;
1309
1310 llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) {
1311 if (next_nsvc->remote_end_is_sgsn)
1312 continue;
1313
1314 /* Note that the following does not start the full
1315 * procedures including timer based retransmissions. */
Harald Weltedb2a2d52011-02-06 16:33:29 +01001316 switch (signal) {
1317 case S_NS_RESET:
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001318 gprs_ns_tx_reset(next_nsvc, nssd->cause);
Harald Weltedb2a2d52011-02-06 16:33:29 +01001319 break;
1320 case S_NS_BLOCK:
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001321 gprs_ns_tx_block(next_nsvc, nssd->cause);
Harald Weltedb2a2d52011-02-06 16:33:29 +01001322 break;
1323 case S_NS_UNBLOCK:
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001324 gprs_ns_tx_unblock(next_nsvc);
Harald Weltedb2a2d52011-02-06 16:33:29 +01001325 break;
1326 }
Harald Welte5bb0d362010-05-11 06:34:24 +02001327 }
1328 }
1329 return 0;
1330}
1331
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001332void gbprox_reset(struct gbproxy_config *cfg)
Jacob Erlbeck67f03bd2013-10-24 12:48:55 +02001333{
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +02001334 struct gbproxy_peer *peer, *tmp;
Jacob Erlbeck67f03bd2013-10-24 12:48:55 +02001335
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001336 llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list)
Holger Hans Peter Freyther60fa5b92014-08-04 17:10:08 +02001337 gbproxy_peer_free(peer);
Jacob Erlbeck67f03bd2013-10-24 12:48:55 +02001338
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001339 rate_ctr_group_free(cfg->ctrg);
1340 gbproxy_init_config(cfg);
Jacob Erlbeck67f03bd2013-10-24 12:48:55 +02001341}
1342
Holger Hans Peter Freyther5eaf1a22014-08-04 11:10:09 +02001343int gbproxy_init_config(struct gbproxy_config *cfg)
Jacob Erlbeck7fee9722013-10-24 12:48:23 +02001344{
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +02001345 struct timespec tp;
Holger Hans Peter Freyther5eaf1a22014-08-04 11:10:09 +02001346 INIT_LLIST_HEAD(&cfg->bts_peers);
Holger Hans Peter Freyther98e753f2014-08-04 11:19:56 +02001347 cfg->ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0);
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +02001348 clock_gettime(CLOCK_REALTIME, &tp);
1349 cfg->bss_ptmsi_state = tp.tv_sec + tp.tv_nsec;
1350 cfg->sgsn_tlli_state = tp.tv_sec - tp.tv_nsec;
Holger Hans Peter Freyther5eaf1a22014-08-04 11:10:09 +02001351 return 0;
Jacob Erlbeck7fee9722013-10-24 12:48:23 +02001352}