blob: 5b3ee282d6152ae05d84143d590e885ed1c95b61 [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
Daniel Willmann537d4802015-10-12 19:36:35 +020053#include <openssl/rand.h>
54
Jacob Erlbeckbc555742013-10-18 14:34:55 +020055static const struct rate_ctr_desc global_ctr_description[] = {
56 { "inv-bvci", "Invalid BVC Identifier " },
Jacob Erlbeck8f503592014-06-02 10:49:00 +020057 { "inv-lai", "Invalid Location Area Identifier" },
58 { "inv-rai", "Invalid Routing Area Identifier " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020059 { "inv-nsei", "No BVC established for NSEI " },
60 { "proto-err.bss", "BSSGP protocol error (BSS )" },
61 { "proto-err.sgsn", "BSSGP protocol error (SGSN)" },
62 { "not-supp.bss", "Feature not supported (BSS )" },
63 { "not-supp.sgsn", "Feature not supported (SGSN)" },
64 { "restart.sgsn", "Restarted RESET procedure (SGSN)" },
65 { "tx-err.sgsn", "NS Transmission error (SGSN)" },
66 { "error", "Other error " },
Jacob Erlbeck006c0382014-05-27 13:49:04 +020067 { "mod-peer-err", "Patch error: no peer " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020068};
69
70static const struct rate_ctr_group_desc global_ctrg_desc = {
71 .group_name_prefix = "gbproxy.global",
72 .group_description = "GBProxy Global Statistics",
73 .num_ctr = ARRAY_SIZE(global_ctr_description),
74 .ctr_desc = global_ctr_description,
75};
76
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020077static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
78 uint16_t ns_bvci);
79static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +020080 uint16_t ns_bvci, uint16_t sgsn_nsei);
Jacob Erlbeckd211d1d2014-09-22 13:30:46 +020081static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info);
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020082
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +020083static int check_peer_nsei(struct gbproxy_peer *peer, uint16_t nsei)
Jacob Erlbeckbc555742013-10-18 14:34:55 +020084{
85 if (peer->nsei != nsei) {
86 LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI "
87 "BVCI=%u via NSEI=%u (expected NSEI=%u)\n",
88 peer->bvci, nsei, peer->nsei);
89 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]);
Jacob Erlbeck64cb9242013-10-24 12:48:56 +020090 return 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +020091 }
92
Jacob Erlbeck64cb9242013-10-24 12:48:56 +020093 return 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +020094}
95
Harald Welte69619e32010-05-03 19:05:10 +020096/* strip off the NS header */
97static void strip_ns_hdr(struct msgb *msg)
98{
99 int strip_len = msgb_bssgph(msg) - msg->data;
100 msgb_pull(msg, strip_len);
101}
102
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200103/* Transmit Chapter 9.2.10 Identity Request */
104static void gprs_put_identity_req(struct msgb *msg, uint8_t id_type)
105{
106 struct gsm48_hdr *gh;
107
108 id_type &= GSM_MI_TYPE_MASK;
109
110 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
111 gh->proto_discr = GSM48_PDISC_MM_GPRS;
112 gh->msg_type = GSM48_MT_GMM_ID_REQ;
113 gh->data[0] = id_type;
114}
115
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200116/* Transmit Chapter 9.4.6.2 Detach Accept (mobile originated detach) */
117static void gprs_put_mo_detach_acc(struct msgb *msg)
118{
119 struct gsm48_hdr *gh;
120
121 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
122 gh->proto_discr = GSM48_PDISC_MM_GPRS;
123 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
124 gh->data[0] = 0; /* no force to standby */
125}
126
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200127static void gprs_push_llc_ui(struct msgb *msg,
128 int is_uplink, unsigned sapi, unsigned nu)
129{
130 const uint8_t e_bit = 0;
131 const uint8_t pm_bit = 1;
132 const uint8_t cr_bit = is_uplink ? 0 : 1;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200133 uint8_t *llc;
134 uint8_t *fcs_field;
135 uint32_t fcs;
136
137 nu &= 0x01ff; /* 9 Bit */
138
139 llc = msgb_push(msg, 3);
140 llc[0] = (cr_bit << 6) | (sapi & 0x0f);
141 llc[1] = 0xc0 | (nu >> 6); /* UI frame */
142 llc[2] = (nu << 2) | ((e_bit & 1) << 1) | (pm_bit & 1);
143
144 fcs = gprs_llc_fcs(llc, msgb_length(msg));
145 fcs_field = msgb_put(msg, 3);
146 fcs_field[0] = (uint8_t)(fcs >> 0);
147 fcs_field[1] = (uint8_t)(fcs >> 8);
148 fcs_field[2] = (uint8_t)(fcs >> 16);
149}
150
151static void gprs_push_bssgp_dl_unitdata(struct msgb *msg,
152 uint32_t tlli)
153{
154 struct bssgp_ud_hdr *budh;
155 uint8_t *llc = msgb_data(msg);
156 size_t llc_size = msgb_length(msg);
157 const size_t llc_ie_hdr_size = 3;
158 const uint8_t qos_profile[] = {0x00, 0x50, 0x20}; /* hard-coded */
159 const uint8_t lifetime[] = {0x02, 0x58}; /* 6s hard-coded */
160
161 const size_t bssgp_overhead = sizeof(*budh) +
162 TVLV_GROSS_LEN(sizeof(lifetime)) + llc_ie_hdr_size;
163 uint8_t *ie;
164 uint32_t tlli_be = htonl(tlli);
165
166 budh = (struct bssgp_ud_hdr *)msgb_push(msg, bssgp_overhead);
167
168 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
169 memcpy(&budh->tlli, &tlli_be, sizeof(budh->tlli));
170 memcpy(&budh->qos_profile, qos_profile, sizeof(budh->qos_profile));
171
172 ie = budh->data;
173 tvlv_put(ie, BSSGP_IE_PDU_LIFETIME, sizeof(lifetime), lifetime);
174 ie += TVLV_GROSS_LEN(sizeof(lifetime));
175
176 /* Note: Add alignment before the LLC IE if inserting other IE */
177
178 *(ie++) = BSSGP_IE_LLC_PDU;
179 *(ie++) = llc_size / 256;
180 *(ie++) = llc_size % 256;
181
182 OSMO_ASSERT(ie == llc);
183
184 msgb_bssgph(msg) = (uint8_t *)budh;
185 msgb_tlli(msg) = tlli;
186}
187
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200188/* update peer according to the BSS message */
189static void gbprox_update_current_raid(uint8_t *raid_enc,
190 struct gbproxy_peer *peer,
191 const char *log_text)
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200192{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200193 struct gbproxy_patch_state *state = &peer->patch_state;
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200194 const int old_local_mcc = state->local_mcc;
195 const int old_local_mnc = state->local_mnc;
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200196 struct gprs_ra_id raid;
197
198 if (!raid_enc)
199 return;
200
201 gsm48_parse_ra(&raid, raid_enc);
202
203 /* save source side MCC/MNC */
204 if (!peer->cfg->core_mcc || raid.mcc == peer->cfg->core_mcc) {
205 state->local_mcc = 0;
206 } else {
207 state->local_mcc = raid.mcc;
208 }
209
210 if (!peer->cfg->core_mnc || raid.mnc == peer->cfg->core_mnc) {
211 state->local_mnc = 0;
212 } else {
213 state->local_mnc = raid.mnc;
214 }
215
216 if (old_local_mcc != state->local_mcc ||
217 old_local_mnc != state->local_mnc)
218 LOGP(DGPRS, LOGL_NOTICE,
219 "Patching RAID %sactivated, msg: %s, "
220 "local: %d-%d, core: %d-%d\n",
221 state->local_mcc || state->local_mnc ?
222 "" : "de",
223 log_text,
224 state->local_mcc, state->local_mnc,
225 peer->cfg->core_mcc, peer->cfg->core_mnc);
226}
227
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200228uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer,
229 uint32_t sgsn_ptmsi)
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200230{
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200231 uint32_t bss_ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200232 int max_retries = 23;
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200233 if (!peer->cfg->patch_ptmsi) {
234 bss_ptmsi = sgsn_ptmsi;
235 } else {
236 do {
Daniel Willmann537d4802015-10-12 19:36:35 +0200237 if (RAND_bytes((uint8_t *) &bss_ptmsi, sizeof(bss_ptmsi)) != 1) {
238 bss_ptmsi = GSM_RESERVED_TMSI;
239 break;
240 }
241
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200242 bss_ptmsi = bss_ptmsi | 0xC0000000;
243
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200244 if (gbproxy_link_info_by_ptmsi(peer, bss_ptmsi))
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200245 bss_ptmsi = GSM_RESERVED_TMSI;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200246 } while (bss_ptmsi == GSM_RESERVED_TMSI && max_retries--);
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200247 }
248
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200249 if (bss_ptmsi == GSM_RESERVED_TMSI)
250 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a BSS P-TMSI\n");
251
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200252 return bss_ptmsi;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200253}
254
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200255uint32_t gbproxy_make_sgsn_tlli(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200256 struct gbproxy_link_info *link_info,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200257 uint32_t bss_tlli)
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200258{
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200259 uint32_t sgsn_tlli;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200260 int max_retries = 23;
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200261 if (!peer->cfg->patch_ptmsi) {
262 sgsn_tlli = bss_tlli;
Jacob Erlbeck41108682014-10-31 10:47:29 +0100263 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI &&
264 gprs_tlli_type(bss_tlli) == TLLI_FOREIGN) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200265 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200266 TLLI_FOREIGN);
Jacob Erlbeck41108682014-10-31 10:47:29 +0100267 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI &&
268 gprs_tlli_type(bss_tlli) == TLLI_LOCAL) {
269 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
270 TLLI_LOCAL);
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200271 } else {
272 do {
273 /* create random TLLI, 0b01111xxx... */
Daniel Willmann537d4802015-10-12 19:36:35 +0200274 if (RAND_bytes((uint8_t *) &sgsn_tlli, sizeof(sgsn_tlli)) != 1) {
275 sgsn_tlli = 0;
276 break;
277 }
278
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200279 sgsn_tlli = (sgsn_tlli & 0x7fffffff) | 0x78000000;
280
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200281 if (gbproxy_link_info_by_any_sgsn_tlli(peer, sgsn_tlli))
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200282 sgsn_tlli = 0;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200283 } while (!sgsn_tlli && max_retries--);
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200284 }
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200285
286 if (!sgsn_tlli)
287 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate an SGSN TLLI\n");
288
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200289 return sgsn_tlli;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200290}
291
Jacob Erlbeckd211d1d2014-09-22 13:30:46 +0200292void gbproxy_reset_link(struct gbproxy_link_info *link_info)
293{
294 gbproxy_reset_imsi_acquisition(link_info);
295}
296
Jacob Erlbeck31591142014-09-03 11:59:48 +0200297/* Returns != 0 iff IMSI acquisition was in progress */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200298static int gbproxy_restart_imsi_acquisition(struct gbproxy_link_info* link_info)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200299{
300 int in_progress = 0;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200301 if (!link_info)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200302 return 0;
303
Jacob Erlbeck65750282014-09-22 15:41:21 +0200304 if (link_info->imsi_acq_pending)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200305 in_progress = 1;
306
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200307 gbproxy_link_info_discard_messages(link_info);
308 link_info->imsi_acq_pending = 0;
Jacob Erlbeck31591142014-09-03 11:59:48 +0200309
310 return in_progress;
311}
312
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200313static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info)
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200314{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200315 gbproxy_restart_imsi_acquisition(link_info);
Jacob Erlbeck65750282014-09-22 15:41:21 +0200316 link_info->vu_gen_tx_bss = GBPROXY_INIT_VU_GEN_TX;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200317}
318
319static void gbproxy_flush_stored_messages(struct gbproxy_peer *peer,
320 struct msgb *msg,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200321 time_t now,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200322 struct gbproxy_link_info* link_info,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200323 struct gprs_gb_parse_context *parse_ctx)
324{
325 int rc;
326 struct msgb *stored_msg;
327 /* Got identity response with IMSI, assuming the request had
328 * been generated by the gbproxy */
329
330 LOGP(DLLC, LOGL_DEBUG,
331 "NSEI=%d(BSS) IMSI acquisition succeeded, "
332 "flushing stored messages\n",
333 msgb_nsei(msg));
334
335 /* Patch and flush stored messages towards the SGSN */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200336 while ((stored_msg = msgb_dequeue(&link_info->stored_msgs))) {
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200337 struct gprs_gb_parse_context tmp_parse_ctx = {0};
338 tmp_parse_ctx.to_bss = 0;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200339 tmp_parse_ctx.peer_nsei = msgb_nsei(stored_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200340 int len_change = 0;
341
342 gprs_gb_parse_bssgp(msgb_bssgph(stored_msg),
343 msgb_bssgp_len(stored_msg),
344 &tmp_parse_ctx);
345 gbproxy_patch_bssgp(msg, msgb_bssgph(stored_msg),
346 msgb_bssgp_len(stored_msg),
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200347 peer, link_info, &len_change,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200348 &tmp_parse_ctx);
349
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200350 gbproxy_update_link_state_after(peer, link_info, now,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200351 &tmp_parse_ctx);
352
353 rc = gbprox_relay2sgsn(peer->cfg, stored_msg,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200354 msgb_bvci(msg), link_info->sgsn_nsei);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200355
356 if (rc < 0)
357 LOGP(DLLC, LOGL_ERROR,
358 "NSEI=%d(BSS) failed to send stored message "
359 "(%s)\n",
360 msgb_nsei(msg),
361 parse_ctx->llc_msg_name ?
362 parse_ctx->llc_msg_name : "BSSGP");
363 msgb_free(stored_msg);
364 }
365}
366
Jacob Erlbeck65750282014-09-22 15:41:21 +0200367static int gbproxy_gsm48_to_peer(struct gbproxy_peer *peer,
368 struct gbproxy_link_info* link_info,
369 uint16_t bvci,
370 struct msgb *msg /* Takes msg ownership */)
371{
372 int rc;
373
374 /* Workaround to avoid N(U) collisions and to enable a restart
375 * of the IMSI acquisition procedure. This will work unless the
376 * SGSN has an initial V(UT) within [256-32, 256+n_retries]
377 * (see GSM 04.64, 8.4.2). */
378 gprs_push_llc_ui(msg, 0, GPRS_SAPI_GMM, link_info->vu_gen_tx_bss);
379 link_info->vu_gen_tx_bss = (link_info->vu_gen_tx_bss + 1) % 512;
380
381 gprs_push_bssgp_dl_unitdata(msg, link_info->tlli.current);
382 rc = gbprox_relay2peer(msg, peer, bvci);
383 msgb_free(msg);
384 return rc;
385}
386
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200387static void gbproxy_acquire_imsi(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200388 struct gbproxy_link_info* link_info,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200389 uint16_t bvci)
390{
391 struct msgb *idreq_msg;
392
393 /* Send IDENT REQ */
394 idreq_msg = gsm48_msgb_alloc();
395 gprs_put_identity_req(idreq_msg, GSM_MI_TYPE_IMSI);
Jacob Erlbeck65750282014-09-22 15:41:21 +0200396 gbproxy_gsm48_to_peer(peer, link_info, bvci, idreq_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200397}
398
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200399static void gbproxy_tx_detach_acc(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200400 struct gbproxy_link_info* link_info,
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200401 uint16_t bvci)
402{
403 struct msgb *detacc_msg;
404
405 /* Send DETACH ACC */
406 detacc_msg = gsm48_msgb_alloc();
407 gprs_put_mo_detach_acc(detacc_msg);
Jacob Erlbeck65750282014-09-22 15:41:21 +0200408 gbproxy_gsm48_to_peer(peer, link_info, bvci, detacc_msg);
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200409}
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200410
411/* Return != 0 iff msg still needs to be processed */
412static int gbproxy_imsi_acquisition(struct gbproxy_peer *peer,
413 struct msgb *msg,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200414 time_t now,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200415 struct gbproxy_link_info* link_info,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200416 struct gprs_gb_parse_context *parse_ctx)
417{
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200418 struct msgb *stored_msg;
419
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200420 if (!link_info)
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200421 return 1;
422
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200423 if (!link_info->imsi_acq_pending && link_info->imsi_len > 0)
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200424 return 1;
425
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200426 if (parse_ctx->g48_hdr)
427 switch (parse_ctx->g48_hdr->msg_type)
428 {
Jacob Erlbecke27ab912014-09-18 10:08:27 +0200429 case GSM48_MT_GMM_RA_UPD_REQ:
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200430 case GSM48_MT_GMM_ATTACH_REQ:
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200431 if (gbproxy_restart_imsi_acquisition(link_info)) {
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200432 LOGP(DLLC, LOGL_INFO,
433 "NSEI=%d(BSS) IMSI acquisition was in progress "
Jacob Erlbecke27ab912014-09-18 10:08:27 +0200434 "when receiving an %s.\n",
435 msgb_nsei(msg), parse_ctx->llc_msg_name);
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200436 }
437 break;
438 case GSM48_MT_GMM_DETACH_REQ:
439 /* Nothing has been sent to the SGSN yet */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200440 if (link_info->imsi_acq_pending) {
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200441 LOGP(DLLC, LOGL_INFO,
442 "NSEI=%d(BSS) IMSI acquisition was in progress "
443 "when receiving a DETACH_REQ.\n",
444 msgb_nsei(msg));
445 }
446 if (!parse_ctx->invalidate_tlli) {
447 LOGP(DLLC, LOGL_INFO,
448 "NSEI=%d(BSS) IMSI not yet acquired, "
449 "faking a DETACH_ACC.\n",
450 msgb_nsei(msg));
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200451 gbproxy_tx_detach_acc(peer, link_info, msgb_bvci(msg));
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200452 parse_ctx->invalidate_tlli = 1;
453 }
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200454 gbproxy_reset_imsi_acquisition(link_info);
455 gbproxy_update_link_state_after(peer, link_info, now,
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200456 parse_ctx);
457 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200458 }
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200459
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200460 if (link_info->imsi_acq_pending && link_info->imsi_len > 0) {
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200461 int is_ident_resp =
462 parse_ctx->g48_hdr &&
463 parse_ctx->g48_hdr->proto_discr == GSM48_PDISC_MM_GPRS &&
464 parse_ctx->g48_hdr->msg_type == GSM48_MT_GMM_ID_RESP;
465
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200466 /* The IMSI is now available */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200467 gbproxy_flush_stored_messages(peer, msg, now, link_info,
Jacob Erlbeck9a6b7632014-09-19 13:23:21 +0200468 parse_ctx);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200469
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200470 gbproxy_reset_imsi_acquisition(link_info);
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200471
472 /* This message is most probably the response to the ident
473 * request sent by gbproxy_acquire_imsi(). Don't forward it to
474 * the SGSN. */
475 return !is_ident_resp;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200476 }
477
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200478 /* The message cannot be processed since the IMSI is still missing */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200479
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200480 /* Enqueue unpatched messages */
481 LOGP(DLLC, LOGL_INFO,
482 "NSEI=%d(BSS) IMSI acquisition in progress, "
483 "storing message (%s)\n",
484 msgb_nsei(msg),
485 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200486
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200487 stored_msg = gprs_msgb_copy(msg, "process_bssgp_ul");
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200488 msgb_enqueue(&link_info->stored_msgs, stored_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200489
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200490 if (!link_info->imsi_acq_pending) {
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200491 LOGP(DLLC, LOGL_INFO,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200492 "NSEI=%d(BSS) IMSI is required but not available, "
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200493 "initiating identification procedure (%s)\n",
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200494 msgb_nsei(msg),
495 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
496
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200497 gbproxy_acquire_imsi(peer, link_info, msgb_bvci(msg));
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200498
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200499 /* There is no explicit retransmission handling, the
500 * implementation relies on the MS doing proper retransmissions
501 * of the triggering message instead */
502
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200503 link_info->imsi_acq_pending = 1;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200504 }
505
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200506 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200507}
508
509struct gbproxy_peer *gbproxy_find_peer(struct gbproxy_config *cfg,
510 struct msgb *msg,
511 struct gprs_gb_parse_context *parse_ctx)
512{
513 struct gbproxy_peer *peer = NULL;
514
515 if (msgb_bvci(msg) >= 2)
516 peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg));
517
518 if (!peer && !parse_ctx->to_bss)
519 peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg));
520
521 if (!peer)
522 peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx->bssgp_tp);
523
524 if (!peer) {
525 LOGP(DLLC, LOGL_INFO,
526 "NSEI=%d(%s) patching: didn't find peer for message, "
527 "PDU %d\n",
528 msgb_nsei(msg), parse_ctx->to_bss ? "BSS" : "SGSN",
529 parse_ctx->pdu_type);
530 /* Increment counter */
531 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]);
532 }
533 return peer;
534}
535
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200536/* patch BSSGP message */
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200537static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg,
538 struct msgb *msg,
539 struct gbproxy_peer *peer)
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200540{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200541 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200542 int rc;
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200543 int len_change = 0;
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200544 time_t now;
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200545 struct timespec ts = {0,};
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200546 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200547 uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200548
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200549 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
Jacob Erlbeck1abfdc22014-09-04 11:42:08 +0200550 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200551 return 1;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200552
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200553 parse_ctx.to_bss = 0;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200554 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200555
556 /* Parse BSSGP/LLC */
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200557 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
558 &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200559
Jacob Erlbeckb4f0e802014-10-09 12:28:15 +0200560 if (!rc && !parse_ctx.need_decryption) {
561 LOGP(DGPRS, LOGL_ERROR,
562 "NSEI=%u(BSS) patching: failed to parse invalid %s message\n",
563 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
564 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
565 LOGP(DGPRS, LOGL_NOTICE,
566 "NSEI=%u(BSS) invalid message was: %s\n",
567 msgb_nsei(msg), msgb_hexdump(msg));
568 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200569 }
570
571 /* Get peer */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200572 if (!peer)
573 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200574
575 if (!peer)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200576 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200577
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200578
579 clock_gettime(CLOCK_MONOTONIC, &ts);
580 now = ts.tv_sec;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200581
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200582 gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer,
583 parse_ctx.llc_msg_name);
584
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200585 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
Jacob Erlbeck31591142014-09-03 11:59:48 +0200586
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200587 link_info = gbproxy_update_link_state_ul(peer, now, &parse_ctx);
Jacob Erlbeck31591142014-09-03 11:59:48 +0200588
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200589 if (parse_ctx.g48_hdr) {
590 switch (parse_ctx.g48_hdr->msg_type) {
591 case GSM48_MT_GMM_ATTACH_REQ:
592 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]);
593 break;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200594
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200595 default:
596 break;
597 }
598 }
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200599
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200600 if (link_info && cfg->route_to_sgsn2) {
601 if (cfg->acquire_imsi && link_info->imsi_len == 0)
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200602 sgsn_nsei = 0xffff;
Jacob Erlbeckb36032c2014-09-25 13:21:48 +0200603 else if (gbproxy_imsi_matches(cfg, GBPROX_MATCH_ROUTING,
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200604 link_info))
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200605 sgsn_nsei = cfg->nsip_sgsn2_nsei;
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200606 }
607
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200608 if (link_info)
609 link_info->sgsn_nsei = sgsn_nsei;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200610
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200611 /* Handle IMSI acquisition */
612 if (cfg->acquire_imsi) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200613 rc = gbproxy_imsi_acquisition(peer, msg, now, link_info,
Jacob Erlbeck9a6b7632014-09-19 13:23:21 +0200614 &parse_ctx);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200615 if (rc <= 0)
616 return rc;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200617 }
618
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200619 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200620 peer, link_info, &len_change, &parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200621
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200622 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200623
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200624 if (sgsn_nsei != cfg->nsip_sgsn_nsei) {
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200625 /* Send message directly to the selected SGSN */
626 rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei);
627 /* Don't let the calling code handle the transmission */
628 return 0;
629 }
630
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200631 return 1;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200632}
633
634/* patch BSSGP message to use core_mcc/mnc on the SGSN side */
635static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg,
636 struct msgb *msg,
637 struct gbproxy_peer *peer)
638{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200639 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200640 int rc;
641 int len_change = 0;
642 time_t now;
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200643 struct timespec ts = {0,};
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200644 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200645
Jacob Erlbeck1abfdc22014-09-04 11:42:08 +0200646 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
647 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
648 return;
649
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200650 parse_ctx.to_bss = 1;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200651 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200652
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200653 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
654 &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200655
Jacob Erlbeckb4f0e802014-10-09 12:28:15 +0200656 if (!rc && !parse_ctx.need_decryption) {
657 LOGP(DGPRS, LOGL_ERROR,
658 "NSEI=%u(SGSN) patching: failed to parse invalid %s message\n",
659 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
660 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
661 LOGP(DGPRS, LOGL_NOTICE,
662 "NSEI=%u(SGSN) invalid message was: %s\n",
663 msgb_nsei(msg), msgb_hexdump(msg));
664 return;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200665 }
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200666
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200667 /* Get peer */
668 if (!peer)
669 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200670
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200671 if (!peer)
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200672 return;
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200673
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200674 clock_gettime(CLOCK_MONOTONIC, &ts);
675 now = ts.tv_sec;
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200676
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200677 if (parse_ctx.g48_hdr) {
678 switch (parse_ctx.g48_hdr->msg_type) {
679 case GSM48_MT_GMM_ATTACH_REJ:
680 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]);
681 break;
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200682
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200683 default:
684 break;
685 }
686 }
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200687
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200688 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200689
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200690 link_info = gbproxy_update_link_state_dl(peer, now, &parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200691
692 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200693 peer, link_info, &len_change, &parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200694
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200695 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200696
Jacob Erlbeck03551e52014-06-27 19:56:05 +0200697 return;
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200698}
699
Harald Welte9f75c352010-04-30 20:26:32 +0200700/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200701static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200702 uint16_t ns_bvci, uint16_t sgsn_nsei)
Harald Welte672f5c42010-05-03 18:54:58 +0200703{
Harald Welte39d0bb52010-05-12 18:10:25 +0000704 /* create a copy of the message so the old one can
705 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200706 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2sgsn");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200707 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000708
Harald Weltee9ea2692010-05-11 20:20:13 +0200709 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200710 msgb_nsei(msg), ns_bvci, sgsn_nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200711
Harald Welte672f5c42010-05-03 18:54:58 +0200712 msgb_bvci(msg) = ns_bvci;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200713 msgb_nsei(msg) = sgsn_nsei;
Harald Welte672f5c42010-05-03 18:54:58 +0200714
Harald Welte69619e32010-05-03 19:05:10 +0200715 strip_ns_hdr(msg);
716
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200717 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
718 if (rc < 0)
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200719 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200720
721 return rc;
Harald Welte672f5c42010-05-03 18:54:58 +0200722}
723
Harald Welte672f5c42010-05-03 18:54:58 +0200724/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200725static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
Harald Welte9f75c352010-04-30 20:26:32 +0200726 uint16_t ns_bvci)
727{
Harald Welte39d0bb52010-05-12 18:10:25 +0000728 /* create a copy of the message so the old one can
729 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200730 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2peer");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200731 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000732
Harald Welte0ab535b2010-05-13 10:34:56 +0200733 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200734 msgb_nsei(msg), ns_bvci, peer->nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200735
Harald Welte9f75c352010-04-30 20:26:32 +0200736 msgb_bvci(msg) = ns_bvci;
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200737 msgb_nsei(msg) = peer->nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200738
Harald Welte0ab535b2010-05-13 10:34:56 +0200739 /* Strip the old NS header, it will be replaced with a new one */
Harald Welte69619e32010-05-03 19:05:10 +0200740 strip_ns_hdr(msg);
741
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200742 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
743 if (rc < 0)
744 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
745
746 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200747}
748
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200749static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type)
Harald Welte36f98d92011-02-06 13:09:29 +0100750{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200751 struct gbproxy_peer *peer;
Harald Welte36f98d92011-02-06 13:09:29 +0100752
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200753 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +0100754 if (!peer) {
755 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
756 ptp_bvci);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200757 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +0100758 return -ENOENT;
759 }
760
761 switch (pdu_type) {
762 case BSSGP_PDUT_BVC_BLOCK_ACK:
763 peer->blocked = 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200764 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100765 break;
766 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
767 peer->blocked = 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200768 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100769 break;
770 default:
771 break;
772 }
773 return 0;
774}
775
Harald Welte9f75c352010-04-30 20:26:32 +0200776/* Send a message to a peer identified by ptp_bvci but using ns_bvci
777 * in the NS hdr */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200778static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci,
Harald Welte9f75c352010-04-30 20:26:32 +0200779 uint16_t ns_bvci)
780{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200781 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +0200782
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200783 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200784 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200785 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
Harald Welte1c77c6e2010-05-03 21:37:11 +0200786 ptp_bvci);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200787 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte9f75c352010-04-30 20:26:32 +0200788 return -ENOENT;
Harald Welte1c77c6e2010-05-03 21:37:11 +0200789 }
Harald Welte9f75c352010-04-30 20:26:32 +0200790
Harald Welte69619e32010-05-03 19:05:10 +0200791 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200792}
793
Harald Welteb1fd9022012-06-17 12:16:31 +0800794int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
795{
796 return 0;
797}
798
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200799/* Receive an incoming PTP message from a BSS-side NS-VC */
800static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg,
801 struct msgb *msg, uint16_t nsei,
802 uint16_t nsvci, uint16_t ns_bvci)
803{
804 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200805 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
806 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200807 int rc;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200808
809 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
Jacob Erlbeck058ae122014-10-10 09:07:04 +0200810 if (!peer) {
811 LOGP(DGPRS, LOGL_NOTICE, "Didn't find peer for "
812 "BVCI=%u for PTP message from NSVC=%u/NSEI=%u (BSS), "
813 "discarding message\n",
814 ns_bvci, nsvci, nsei);
815 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
816 &ns_bvci, msg);
817 }
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200818
Jacob Erlbeck43b8f9f2014-10-02 14:56:44 +0200819 check_peer_nsei(peer, nsei);
Jacob Erlbeck43b8f9f2014-10-02 14:56:44 +0200820
Jacob Erlbeck058ae122014-10-10 09:07:04 +0200821 rc = gbprox_process_bssgp_ul(cfg, msg, peer);
822 if (!rc)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200823 return 0;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200824
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200825 switch (pdu_type) {
826 case BSSGP_PDUT_FLOW_CONTROL_BVC:
827 if (!cfg->route_to_sgsn2)
828 break;
829
830 /* Send a copy to the secondary SGSN */
831 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
832 break;
833 default:
834 break;
835 }
836
837
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200838 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200839}
840
841/* Receive an incoming PTP message from a SGSN-side NS-VC */
842static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg,
843 struct msgb *msg, uint16_t nsei,
844 uint16_t nsvci, uint16_t ns_bvci)
845{
846 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200847 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
848 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200849
850 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
851
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200852 /* Send status messages before patching */
853
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200854 if (!peer) {
855 LOGP(DGPRS, LOGL_INFO, "Didn't find peer for "
856 "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n",
857 ns_bvci, nsvci, nsei);
858 rate_ctr_inc(&cfg->ctrg->
859 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
860 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
861 &ns_bvci, msg);
862 }
863
864 if (peer->blocked) {
865 LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for "
866 "blocked BVCI=%u via NSVC=%u/NSEI=%u\n",
867 ns_bvci, nsvci, nsei);
868 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200869 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200870 }
871
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200872 switch (pdu_type) {
873 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
874 case BSSGP_PDUT_BVC_BLOCK_ACK:
875 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
876 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
877 /* Hide ACKs from the secondary SGSN, the primary SGSN
878 * is responsible to send them. */
879 return 0;
880 break;
881 default:
882 break;
883 }
884
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200885 /* Optionally patch the message */
886 gbprox_process_bssgp_dl(cfg, msg, peer);
887
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200888 return gbprox_relay2peer(msg, peer, ns_bvci);
889}
890
Harald Welte9f75c352010-04-30 20:26:32 +0200891/* Receive an incoming signalling message from a BSS-side NS-VC */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200892static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg,
893 struct msgb *msg, uint16_t nsei,
Harald Welte9f75c352010-04-30 20:26:32 +0200894 uint16_t ns_bvci)
895{
Harald Welteca3620a2010-05-03 16:30:59 +0200896 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200897 struct tlv_parsed tp;
898 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200899 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200900 struct gbproxy_peer *from_peer = NULL;
Harald Welte70f38d22010-05-01 12:10:57 +0200901 struct gprs_ra_id raid;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200902 int copy_to_sgsn2 = 0;
Jacob Erlbeck59300642014-09-08 09:04:01 +0200903 int rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200904
Harald Weltec471d3d2011-02-06 17:13:12 +0100905 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200906 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200907 nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200908 return -EINVAL;
909 }
910
911 /* we actually should never see those two for BVCI == 0, but double-check
912 * just to make sure */
913 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
914 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200915 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200916 "signalling\n", nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200917 return -EINVAL;
918 }
919
920 bssgp_tlv_parse(&tp, bgph->data, data_len);
921
922 switch (pdu_type) {
923 case BSSGP_PDUT_SUSPEND:
924 case BSSGP_PDUT_RESUME:
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200925 /* We implement RAI snooping during SUSPEND/RESUME, since it
926 * establishes a relationsip between BVCI/peer and the routeing
927 * area identification. The snooped information is then used
928 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
929 * BSSGP */
Harald Welte9f75c352010-04-30 20:26:32 +0200930 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
931 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200932 from_peer = gbproxy_peer_by_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200933 if (!from_peer)
934 goto err_no_peer;
Harald Welte1174c082010-05-12 00:07:29 +0200935 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
936 sizeof(from_peer->ra));
Harald Welte7fc98222010-05-11 10:15:42 +0200937 gsm48_parse_ra(&raid, from_peer->ra);
Harald Welte4cf12e92010-05-13 14:14:56 +0200938 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200939 "RAI snooping: RAI %u-%u-%u-%u behind BVCI=%u\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200940 nsei, raid.mcc, raid.mnc, raid.lac,
941 raid.rac , from_peer->bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200942 /* FIXME: This only supports one BSS per RA */
943 break;
Harald Welte44c48302010-05-03 19:22:32 +0200944 case BSSGP_PDUT_BVC_RESET:
945 /* If we receive a BVC reset on the signalling endpoint, we
946 * don't want the SGSN to reset, as the signalling endpoint
947 * is common for all point-to-point BVCs (and thus all BTS) */
948 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200949 uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte72953b82010-05-12 00:20:41 +0200950 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200951 nsei, bvci);
Harald Welte44c48302010-05-03 19:22:32 +0200952 if (bvci == 0) {
953 /* FIXME: only do this if SGSN is alive! */
Harald Weltee9ea2692010-05-11 20:20:13 +0200954 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200955 "BVC RESET ACK of BVCI=0\n", nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200956 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200957 nsei, 0, ns_bvci);
Harald Welte1174c082010-05-12 00:07:29 +0200958 }
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200959 from_peer = gbproxy_peer_by_bvci(cfg, bvci);
Harald Welte1174c082010-05-12 00:07:29 +0200960 if (!from_peer) {
Harald Welte1c77c6e2010-05-03 21:37:11 +0200961 /* if a PTP-BVC is reset, and we don't know that
962 * PTP-BVCI yet, we should allocate a new peer */
963 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200964 "BVCI=%u via NSEI=%u\n", bvci, nsei);
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +0200965 from_peer = gbproxy_peer_alloc(cfg, bvci);
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200966 from_peer->nsei = nsei;
Harald Welte44c48302010-05-03 19:22:32 +0200967 }
Jacob Erlbeck64cb9242013-10-24 12:48:56 +0200968
969 if (!check_peer_nsei(from_peer, nsei))
970 from_peer->nsei = nsei;
971
Harald Welte1174c082010-05-12 00:07:29 +0200972 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
973 struct gprs_ra_id raid;
974 /* We have a Cell Identifier present in this
975 * PDU, this means we can extend our local
976 * state information about this particular cell
977 * */
978 memcpy(from_peer->ra,
979 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
980 sizeof(from_peer->ra));
981 gsm48_parse_ra(&raid, from_peer->ra);
982 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200983 "Cell ID %u-%u-%u-%u\n", nsei,
Harald Welte1174c082010-05-12 00:07:29 +0200984 bvci, raid.mcc, raid.mnc, raid.lac,
985 raid.rac);
986 }
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200987 if (cfg->route_to_sgsn2)
988 copy_to_sgsn2 = 1;
Harald Welte44c48302010-05-03 19:22:32 +0200989 }
990 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200991 }
992
Harald Weltee9ea2692010-05-11 20:20:13 +0200993 /* Normally, we can simply pass on all signalling messages from BSS to
994 * SGSN */
Jacob Erlbeck59300642014-09-08 09:04:01 +0200995 rc = gbprox_process_bssgp_ul(cfg, msg, from_peer);
996 if (!rc)
997 return 0;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200998
999 if (copy_to_sgsn2)
1000 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
1001
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001002 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001003err_no_peer:
Jacob Erlbeckc5085f92013-10-18 13:04:48 +02001004 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001005 nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001006 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001007 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001008err_mand_ie:
Harald Welte0a4050c2010-05-11 10:01:17 +02001009 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001010 nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001011 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001012 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001013}
1014
1015/* Receive paging request from SGSN, we need to relay to proper BSS */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001016static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001017 uint32_t nsei, uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001018{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001019 struct gbproxy_peer *peer = NULL;
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001020 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte9f75c352010-04-30 20:26:32 +02001021
Harald Welte4cf12e92010-05-13 14:14:56 +02001022 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001023 nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001024 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001025 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte4cf12e92010-05-13 14:14:56 +02001026 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n",
1027 bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001028 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte9f75c352010-04-30 20:26:32 +02001029 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001030 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001031 LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +08001032 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001033 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte9f75c352010-04-30 20:26:32 +02001034 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001035 peer = gbproxy_peer_by_lai(cfg, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001036 LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +08001037 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001038 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte9f75c352010-04-30 20:26:32 +02001039 } else
Harald Welte4cf12e92010-05-13 14:14:56 +02001040 LOGPC(DGPRS, LOGL_INFO, "\n");
1041
1042 if (!peer) {
1043 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001044 "unable to route, missing IE\n", nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001045 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
Harald Welte9f75c352010-04-30 20:26:32 +02001046 return -EINVAL;
Harald Welte4cf12e92010-05-13 14:14:56 +02001047 }
1048 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001049}
1050
Harald Welte0a4050c2010-05-11 10:01:17 +02001051/* Receive an incoming BVC-RESET message from the SGSN */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001052static int rx_reset_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001053 struct msgb *orig_msg,
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001054 struct msgb *msg, struct tlv_parsed *tp,
1055 uint32_t nsei, uint16_t ns_bvci)
Harald Welte0a4050c2010-05-11 10:01:17 +02001056{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001057 struct gbproxy_peer *peer;
Harald Welte0a4050c2010-05-11 10:01:17 +02001058 uint16_t ptp_bvci;
1059
1060 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001061 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001062 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001063 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001064 NULL, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +02001065 }
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001066 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte0a4050c2010-05-11 10:01:17 +02001067
1068 if (ptp_bvci >= 2) {
1069 /* A reset for a PTP BVC was received, forward it to its
1070 * respective peer */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001071 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001072 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +02001073 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001074 nsei, ptp_bvci);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001075 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001076 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001077 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001078 &ptp_bvci, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +02001079 }
1080 return gbprox_relay2peer(msg, peer, ns_bvci);
1081 }
1082
1083 /* A reset for the Signalling entity has been received
1084 * from the SGSN. As the signalling BVCI is shared
1085 * among all the BSS's that we multiplex, it needs to
1086 * be relayed */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001087 llist_for_each_entry(peer, &cfg->bts_peers, list)
Harald Welte0a4050c2010-05-11 10:01:17 +02001088 gbprox_relay2peer(msg, peer, ns_bvci);
1089
1090 return 0;
1091}
1092
Harald Welte9f75c352010-04-30 20:26:32 +02001093/* Receive an incoming signalling message from the SGSN-side NS-VC */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001094static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001095 struct msgb *orig_msg, uint32_t nsei,
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001096 uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001097{
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001098 struct bssgp_normal_hdr *bgph =
1099 (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001100 struct tlv_parsed tp;
1101 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001102 int data_len;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001103 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +02001104 uint16_t bvci;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001105 struct msgb *msg;
Harald Welte9f75c352010-04-30 20:26:32 +02001106 int rc = 0;
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001107 int cause;
Harald Welte9f75c352010-04-30 20:26:32 +02001108
Harald Weltec471d3d2011-02-06 17:13:12 +01001109 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +02001110 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001111 "signalling\n", nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001112 /* FIXME: Send proper error message */
Harald Welte9f75c352010-04-30 20:26:32 +02001113 return -EINVAL;
1114 }
1115
1116 /* we actually should never see those two for BVCI == 0, but double-check
1117 * just to make sure */
1118 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1119 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +02001120 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001121 "signalling\n", nsei);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001122 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001123 }
1124
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001125 msg = gprs_msgb_copy(orig_msg, "rx_sig_from_sgsn");
1126 gbprox_process_bssgp_dl(cfg, msg, NULL);
1127 /* Update message info */
1128 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1129 data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +02001130 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1131
1132 switch (pdu_type) {
Harald Welte0a4050c2010-05-11 10:01:17 +02001133 case BSSGP_PDUT_BVC_RESET:
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001134 rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001135 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001136 case BSSGP_PDUT_BVC_RESET_ACK:
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +02001137 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
1138 break;
1139 /* fall through */
1140 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9f75c352010-04-30 20:26:32 +02001141 /* simple case: BVCI IE is mandatory */
1142 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1143 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001144 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001145 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001146 break;
1147 case BSSGP_PDUT_PAGING_PS:
1148 case BSSGP_PDUT_PAGING_CS:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001149 /* process the paging request (LAI/RAI lookup) */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001150 rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001151 break;
1152 case BSSGP_PDUT_STATUS:
Harald Welte0a4050c2010-05-11 10:01:17 +02001153 /* Some exception has occurred */
Harald Welte44c48302010-05-03 19:22:32 +02001154 LOGP(DGPRS, LOGL_NOTICE,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001155 "NSEI=%u(SGSN) BSSGP STATUS ", nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +02001156 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
1157 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1158 goto err_mand_ie;
1159 }
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001160 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
Harald Welte0a4050c2010-05-11 10:01:17 +02001161 LOGPC(DGPRS, LOGL_NOTICE,
1162 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001163 bssgp_cause_str(cause));
Harald Welte0a4050c2010-05-11 10:01:17 +02001164 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001165 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1166 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%u\n", bvci);
1167
1168 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI)
1169 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001170 } else
1171 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welte9f75c352010-04-30 20:26:32 +02001172 break;
1173 /* those only exist in the SGSN -> BSS direction */
1174 case BSSGP_PDUT_SUSPEND_ACK:
1175 case BSSGP_PDUT_SUSPEND_NACK:
1176 case BSSGP_PDUT_RESUME_ACK:
1177 case BSSGP_PDUT_RESUME_NACK:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001178 /* RAI IE is mandatory */
Harald Welte9f75c352010-04-30 20:26:32 +02001179 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1180 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001181 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte9f75c352010-04-30 20:26:32 +02001182 if (!peer)
1183 goto err_no_peer;
Harald Welte69619e32010-05-03 19:05:10 +02001184 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001185 break;
Harald Welte36f98d92011-02-06 13:09:29 +01001186 case BSSGP_PDUT_BVC_BLOCK_ACK:
1187 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1188 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1189 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001190 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte36f98d92011-02-06 13:09:29 +01001191 if (bvci == 0) {
1192 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001193 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
Harald Welte36f98d92011-02-06 13:09:29 +01001194 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
1195 /* should we send STATUS ? */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001196 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001197 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +01001198 } else {
1199 /* Mark BVC as (un)blocked */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001200 block_unblock_peer(cfg, bvci, pdu_type);
Harald Welte36f98d92011-02-06 13:09:29 +01001201 }
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001202 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +01001203 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001204 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte44c48302010-05-03 19:22:32 +02001205 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001206 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001207 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001208 ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001209 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001210 break;
1211 default:
Harald Welte72953b82010-05-12 00:20:41 +02001212 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n",
1213 pdu_type);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001214 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001215 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001216 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001217 break;
1218 }
1219
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001220 msgb_free(msg);
1221
Harald Welte9f75c352010-04-30 20:26:32 +02001222 return rc;
1223err_mand_ie:
Harald Welte1c77c6e2010-05-03 21:37:11 +02001224 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001225 nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001226 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001227 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001228 msgb_free(msg);
1229 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001230err_no_peer:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001231 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001232 nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001233 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001234 msgb_free(msg);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001235 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001236}
1237
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001238static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei)
1239{
1240 return nsei == cfg->nsip_sgsn_nsei ||
1241 (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei);
1242}
1243
Harald Welte9f75c352010-04-30 20:26:32 +02001244/* Main input function for Gb proxy */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001245int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei,
1246 uint16_t ns_bvci, uint16_t nsvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001247{
Harald Welte672f5c42010-05-03 18:54:58 +02001248 int rc;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001249 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001250
1251 /* Only BVCI=0 messages need special treatment */
1252 if (ns_bvci == 0 || ns_bvci == 1) {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001253 if (remote_end_is_sgsn)
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001254 rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001255 else
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001256 rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci);
Harald Welte672f5c42010-05-03 18:54:58 +02001257 } else {
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +02001258 /* All other BVCI are PTP */
1259 if (remote_end_is_sgsn)
1260 rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei, nsvci,
1261 ns_bvci);
1262 else
1263 rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei, nsvci,
1264 ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001265 }
1266
Harald Welte672f5c42010-05-03 18:54:58 +02001267 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +02001268}
Harald Welte85801d02010-05-11 05:49:43 +02001269
Harald Welte1ccbf442010-05-14 11:53:08 +00001270int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi)
1271{
1272 struct gprs_nsvc *nsvc;
1273
1274 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
1275 if (!nsvc->persistent)
1276 continue;
1277 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1278 }
1279 return 0;
1280}
1281
Harald Weltec1c1dd22010-05-11 06:34:24 +02001282/* Signal handler for signals from NS layer */
1283int gbprox_signal(unsigned int subsys, unsigned int signal,
1284 void *handler_data, void *signal_data)
1285{
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001286 struct gbproxy_config *cfg = handler_data;
Harald Weltec1c1dd22010-05-11 06:34:24 +02001287 struct ns_signal_data *nssd = signal_data;
1288 struct gprs_nsvc *nsvc = nssd->nsvc;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001289 struct gbproxy_peer *peer;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001290 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsvc->nsei);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001291
Harald Weltea6a20b42012-06-16 16:40:42 +08001292 if (subsys != SS_L_NS)
Harald Weltec1c1dd22010-05-11 06:34:24 +02001293 return 0;
1294
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001295 if (signal == S_NS_RESET && remote_end_is_sgsn) {
Harald Weltef69c0592010-05-11 18:29:44 +02001296 /* We have received a NS-RESET from the NSEI and NSVC
1297 * of the SGSN. This might happen with SGSN that start
1298 * their own NS-RESET procedure without waiting for our
1299 * NS-RESET */
1300 nsvc->remote_end_is_sgsn = 1;
1301 }
1302
Harald Welteb778d2c2010-05-12 13:28:25 +00001303 if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) {
1304 LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, "
1305 "re-starting RESET procedure\n");
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001306 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001307 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
Harald Weltee6599ee2012-06-17 12:25:53 +08001308 gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr,
1309 nsvc->nsei, nsvc->nsvci);
Harald Welteb778d2c2010-05-12 13:28:25 +00001310 }
1311
Harald Welte5e106d72011-02-06 16:33:29 +01001312 if (!nsvc->remote_end_is_sgsn) {
1313 /* from BSS to SGSN */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001314 peer = gbproxy_peer_by_nsei(cfg, nsvc->nsei);
Harald Welte5e106d72011-02-06 16:33:29 +01001315 if (!peer) {
1316 LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer "
1317 "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei,
1318 nsvc->nsvci);
1319 return 0;
1320 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001321 switch (signal) {
1322 case S_NS_RESET:
Harald Weltec1c1dd22010-05-11 06:34:24 +02001323 case S_NS_BLOCK:
Harald Welte5e106d72011-02-06 16:33:29 +01001324 if (!peer->blocked)
1325 break;
1326 LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from "
1327 "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n",
1328 nsvc->nsei, nsvc->nsvci);
1329 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei,
1330 peer->bvci, 0);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001331 break;
Harald Welte5e106d72011-02-06 16:33:29 +01001332 }
1333 } else {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001334 /* Forward this message to all NS-VC to BSS */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001335 struct gprs_ns_inst *nsi = cfg->nsi;
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001336 struct gprs_nsvc *next_nsvc;
1337
1338 llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) {
1339 if (next_nsvc->remote_end_is_sgsn)
1340 continue;
1341
1342 /* Note that the following does not start the full
1343 * procedures including timer based retransmissions. */
Harald Welte5e106d72011-02-06 16:33:29 +01001344 switch (signal) {
1345 case S_NS_RESET:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001346 gprs_ns_tx_reset(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001347 break;
1348 case S_NS_BLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001349 gprs_ns_tx_block(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001350 break;
1351 case S_NS_UNBLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001352 gprs_ns_tx_unblock(next_nsvc);
Harald Welte5e106d72011-02-06 16:33:29 +01001353 break;
1354 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001355 }
1356 }
1357 return 0;
1358}
1359
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001360void gbprox_reset(struct gbproxy_config *cfg)
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001361{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001362 struct gbproxy_peer *peer, *tmp;
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001363
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001364 llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list)
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +02001365 gbproxy_peer_free(peer);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001366
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001367 rate_ctr_group_free(cfg->ctrg);
1368 gbproxy_init_config(cfg);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001369}
1370
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001371int gbproxy_init_config(struct gbproxy_config *cfg)
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001372{
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001373 struct timespec tp;
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +02001374
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001375 INIT_LLIST_HEAD(&cfg->bts_peers);
Holger Hans Peter Freythera7027a02014-08-04 11:19:56 +02001376 cfg->ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0);
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001377 clock_gettime(CLOCK_REALTIME, &tp);
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +02001378
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001379 return 0;
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001380}