blob: 8cdf9a45a0c91bfc486bdf7d9d028251ff0aa209 [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>
Jacob Erlbeckd31596b2015-11-02 15:15:38 +010037#include <osmocom/core/stats.h>
Jacob Erlbeck174bad42013-10-18 14:34:55 +020038
Harald Weltecfb6b282012-06-16 14:59:56 +080039#include <osmocom/gprs/gprs_ns.h>
40#include <osmocom/gprs/gprs_bssgp.h>
41
Jacob Erlbeck43037632014-06-06 18:49:23 +020042#include <osmocom/gsm/gsm_utils.h>
43
Harald Welted1991e72010-04-30 20:26:32 +020044#include <openbsc/signal.h>
45#include <openbsc/debug.h>
Jacob Erlbeckcf11e932014-08-19 12:21:01 +020046#include <openbsc/gprs_gb_parse.h>
Harald Weltef01709d2010-05-03 18:54:58 +020047#include <openbsc/gb_proxy.h>
Harald Welted1991e72010-04-30 20:26:32 +020048
Jacob Erlbeck2504bc42014-05-19 10:14:58 +020049#include <openbsc/gprs_llc.h>
50#include <openbsc/gsm_04_08.h>
51#include <openbsc/gsm_04_08_gprs.h>
Holger Hans Peter Freyther8ac3a722014-08-04 11:52:52 +020052#include <openbsc/gprs_utils.h>
Jacob Erlbeck2504bc42014-05-19 10:14:58 +020053
Daniel Willmann2b10af32015-10-12 19:36:35 +020054#include <openssl/rand.h>
55
Jacob Erlbeck174bad42013-10-18 14:34:55 +020056static const struct rate_ctr_desc global_ctr_description[] = {
57 { "inv-bvci", "Invalid BVC Identifier " },
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +020058 { "inv-lai", "Invalid Location Area Identifier" },
59 { "inv-rai", "Invalid Routing Area Identifier " },
Jacob Erlbeck174bad42013-10-18 14:34:55 +020060 { "inv-nsei", "No BVC established for NSEI " },
61 { "proto-err.bss", "BSSGP protocol error (BSS )" },
62 { "proto-err.sgsn", "BSSGP protocol error (SGSN)" },
63 { "not-supp.bss", "Feature not supported (BSS )" },
64 { "not-supp.sgsn", "Feature not supported (SGSN)" },
65 { "restart.sgsn", "Restarted RESET procedure (SGSN)" },
66 { "tx-err.sgsn", "NS Transmission error (SGSN)" },
67 { "error", "Other error " },
Jacob Erlbeck90a1fd12014-05-27 13:49:04 +020068 { "mod-peer-err", "Patch error: no peer " },
Jacob Erlbeck174bad42013-10-18 14:34:55 +020069};
70
71static const struct rate_ctr_group_desc global_ctrg_desc = {
72 .group_name_prefix = "gbproxy.global",
73 .group_description = "GBProxy Global Statistics",
74 .num_ctr = ARRAY_SIZE(global_ctr_description),
75 .ctr_desc = global_ctr_description,
Jacob Erlbeckd31596b2015-11-02 15:15:38 +010076 .class_id = OSMO_STATS_CLASS_GLOBAL,
Jacob Erlbeck174bad42013-10-18 14:34:55 +020077};
78
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +020079static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
80 uint16_t ns_bvci);
81static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +020082 uint16_t ns_bvci, uint16_t sgsn_nsei);
Jacob Erlbeck4abbbdd2014-09-22 13:30:46 +020083static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info);
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +020084
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +020085static int check_peer_nsei(struct gbproxy_peer *peer, uint16_t nsei)
Jacob Erlbeck174bad42013-10-18 14:34:55 +020086{
87 if (peer->nsei != nsei) {
88 LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI "
89 "BVCI=%u via NSEI=%u (expected NSEI=%u)\n",
90 peer->bvci, nsei, peer->nsei);
91 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]);
Jacob Erlbeckd0a9b502013-10-24 12:48:56 +020092 return 0;
Jacob Erlbeck174bad42013-10-18 14:34:55 +020093 }
94
Jacob Erlbeckd0a9b502013-10-24 12:48:56 +020095 return 1;
Jacob Erlbeck174bad42013-10-18 14:34:55 +020096}
97
Harald Welte3ad32432010-05-03 19:05:10 +020098/* strip off the NS header */
99static void strip_ns_hdr(struct msgb *msg)
100{
101 int strip_len = msgb_bssgph(msg) - msg->data;
102 msgb_pull(msg, strip_len);
103}
104
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200105/* Transmit Chapter 9.2.10 Identity Request */
106static void gprs_put_identity_req(struct msgb *msg, uint8_t id_type)
107{
108 struct gsm48_hdr *gh;
109
110 id_type &= GSM_MI_TYPE_MASK;
111
112 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
113 gh->proto_discr = GSM48_PDISC_MM_GPRS;
114 gh->msg_type = GSM48_MT_GMM_ID_REQ;
115 gh->data[0] = id_type;
116}
117
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200118/* Transmit Chapter 9.4.6.2 Detach Accept (mobile originated detach) */
119static void gprs_put_mo_detach_acc(struct msgb *msg)
120{
121 struct gsm48_hdr *gh;
122
123 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
124 gh->proto_discr = GSM48_PDISC_MM_GPRS;
125 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
126 gh->data[0] = 0; /* no force to standby */
127}
128
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200129static void gprs_push_llc_ui(struct msgb *msg,
130 int is_uplink, unsigned sapi, unsigned nu)
131{
132 const uint8_t e_bit = 0;
133 const uint8_t pm_bit = 1;
134 const uint8_t cr_bit = is_uplink ? 0 : 1;
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200135 uint8_t *llc;
136 uint8_t *fcs_field;
137 uint32_t fcs;
138
139 nu &= 0x01ff; /* 9 Bit */
140
141 llc = msgb_push(msg, 3);
142 llc[0] = (cr_bit << 6) | (sapi & 0x0f);
143 llc[1] = 0xc0 | (nu >> 6); /* UI frame */
144 llc[2] = (nu << 2) | ((e_bit & 1) << 1) | (pm_bit & 1);
145
146 fcs = gprs_llc_fcs(llc, msgb_length(msg));
147 fcs_field = msgb_put(msg, 3);
148 fcs_field[0] = (uint8_t)(fcs >> 0);
149 fcs_field[1] = (uint8_t)(fcs >> 8);
150 fcs_field[2] = (uint8_t)(fcs >> 16);
151}
152
153static void gprs_push_bssgp_dl_unitdata(struct msgb *msg,
154 uint32_t tlli)
155{
156 struct bssgp_ud_hdr *budh;
157 uint8_t *llc = msgb_data(msg);
158 size_t llc_size = msgb_length(msg);
159 const size_t llc_ie_hdr_size = 3;
160 const uint8_t qos_profile[] = {0x00, 0x50, 0x20}; /* hard-coded */
161 const uint8_t lifetime[] = {0x02, 0x58}; /* 6s hard-coded */
162
163 const size_t bssgp_overhead = sizeof(*budh) +
164 TVLV_GROSS_LEN(sizeof(lifetime)) + llc_ie_hdr_size;
165 uint8_t *ie;
166 uint32_t tlli_be = htonl(tlli);
167
168 budh = (struct bssgp_ud_hdr *)msgb_push(msg, bssgp_overhead);
169
170 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
171 memcpy(&budh->tlli, &tlli_be, sizeof(budh->tlli));
172 memcpy(&budh->qos_profile, qos_profile, sizeof(budh->qos_profile));
173
174 ie = budh->data;
175 tvlv_put(ie, BSSGP_IE_PDU_LIFETIME, sizeof(lifetime), lifetime);
176 ie += TVLV_GROSS_LEN(sizeof(lifetime));
177
178 /* Note: Add alignment before the LLC IE if inserting other IE */
179
180 *(ie++) = BSSGP_IE_LLC_PDU;
181 *(ie++) = llc_size / 256;
182 *(ie++) = llc_size % 256;
183
184 OSMO_ASSERT(ie == llc);
185
186 msgb_bssgph(msg) = (uint8_t *)budh;
187 msgb_tlli(msg) = tlli;
188}
189
Jacob Erlbeck2cb9e3f2014-08-18 15:41:41 +0200190/* update peer according to the BSS message */
191static void gbprox_update_current_raid(uint8_t *raid_enc,
192 struct gbproxy_peer *peer,
193 const char *log_text)
Jacob Erlbeck2504bc42014-05-19 10:14:58 +0200194{
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +0200195 struct gbproxy_patch_state *state = &peer->patch_state;
Jacob Erlbeck2504bc42014-05-19 10:14:58 +0200196 const int old_local_mcc = state->local_mcc;
197 const int old_local_mnc = state->local_mnc;
Jacob Erlbeck2cb9e3f2014-08-18 15:41:41 +0200198 struct gprs_ra_id raid;
199
200 if (!raid_enc)
201 return;
202
203 gsm48_parse_ra(&raid, raid_enc);
204
205 /* save source side MCC/MNC */
206 if (!peer->cfg->core_mcc || raid.mcc == peer->cfg->core_mcc) {
207 state->local_mcc = 0;
208 } else {
209 state->local_mcc = raid.mcc;
210 }
211
212 if (!peer->cfg->core_mnc || raid.mnc == peer->cfg->core_mnc) {
213 state->local_mnc = 0;
214 } else {
215 state->local_mnc = raid.mnc;
216 }
217
218 if (old_local_mcc != state->local_mcc ||
219 old_local_mnc != state->local_mnc)
220 LOGP(DGPRS, LOGL_NOTICE,
221 "Patching RAID %sactivated, msg: %s, "
222 "local: %d-%d, core: %d-%d\n",
223 state->local_mcc || state->local_mnc ?
224 "" : "de",
225 log_text,
226 state->local_mcc, state->local_mnc,
227 peer->cfg->core_mcc, peer->cfg->core_mnc);
228}
229
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200230uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer,
231 uint32_t sgsn_ptmsi)
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200232{
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200233 uint32_t bss_ptmsi;
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200234 int max_retries = 23;
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200235 if (!peer->cfg->patch_ptmsi) {
236 bss_ptmsi = sgsn_ptmsi;
237 } else {
238 do {
Daniel Willmann2b10af32015-10-12 19:36:35 +0200239 if (RAND_bytes((uint8_t *) &bss_ptmsi, sizeof(bss_ptmsi)) != 1) {
240 bss_ptmsi = GSM_RESERVED_TMSI;
241 break;
242 }
243
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200244 bss_ptmsi = bss_ptmsi | 0xC0000000;
245
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200246 if (gbproxy_link_info_by_ptmsi(peer, bss_ptmsi))
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200247 bss_ptmsi = GSM_RESERVED_TMSI;
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200248 } while (bss_ptmsi == GSM_RESERVED_TMSI && max_retries--);
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200249 }
250
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200251 if (bss_ptmsi == GSM_RESERVED_TMSI)
252 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a BSS P-TMSI\n");
253
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200254 return bss_ptmsi;
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200255}
256
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200257uint32_t gbproxy_make_sgsn_tlli(struct gbproxy_peer *peer,
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200258 struct gbproxy_link_info *link_info,
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200259 uint32_t bss_tlli)
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200260{
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200261 uint32_t sgsn_tlli;
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200262 int max_retries = 23;
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200263 if (!peer->cfg->patch_ptmsi) {
264 sgsn_tlli = bss_tlli;
Jacob Erlbeck233a8472014-10-31 10:47:29 +0100265 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI &&
266 gprs_tlli_type(bss_tlli) == TLLI_FOREIGN) {
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200267 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200268 TLLI_FOREIGN);
Jacob Erlbeck233a8472014-10-31 10:47:29 +0100269 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI &&
270 gprs_tlli_type(bss_tlli) == TLLI_LOCAL) {
271 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
272 TLLI_LOCAL);
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200273 } else {
274 do {
275 /* create random TLLI, 0b01111xxx... */
Daniel Willmann2b10af32015-10-12 19:36:35 +0200276 if (RAND_bytes((uint8_t *) &sgsn_tlli, sizeof(sgsn_tlli)) != 1) {
277 sgsn_tlli = 0;
278 break;
279 }
280
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200281 sgsn_tlli = (sgsn_tlli & 0x7fffffff) | 0x78000000;
282
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200283 if (gbproxy_link_info_by_any_sgsn_tlli(peer, sgsn_tlli))
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200284 sgsn_tlli = 0;
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200285 } while (!sgsn_tlli && max_retries--);
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200286 }
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200287
288 if (!sgsn_tlli)
289 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate an SGSN TLLI\n");
290
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +0200291 return sgsn_tlli;
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200292}
293
Jacob Erlbeck4abbbdd2014-09-22 13:30:46 +0200294void gbproxy_reset_link(struct gbproxy_link_info *link_info)
295{
296 gbproxy_reset_imsi_acquisition(link_info);
297}
298
Jacob Erlbeck6e291e02014-09-03 11:59:48 +0200299/* Returns != 0 iff IMSI acquisition was in progress */
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200300static int gbproxy_restart_imsi_acquisition(struct gbproxy_link_info* link_info)
Jacob Erlbeck6e291e02014-09-03 11:59:48 +0200301{
302 int in_progress = 0;
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200303 if (!link_info)
Jacob Erlbeck6e291e02014-09-03 11:59:48 +0200304 return 0;
305
Jacob Erlbeck5e07b812014-09-22 15:41:21 +0200306 if (link_info->imsi_acq_pending)
Jacob Erlbeck6e291e02014-09-03 11:59:48 +0200307 in_progress = 1;
308
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200309 gbproxy_link_info_discard_messages(link_info);
310 link_info->imsi_acq_pending = 0;
Jacob Erlbeck6e291e02014-09-03 11:59:48 +0200311
312 return in_progress;
313}
314
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200315static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info)
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200316{
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200317 gbproxy_restart_imsi_acquisition(link_info);
Jacob Erlbeck5e07b812014-09-22 15:41:21 +0200318 link_info->vu_gen_tx_bss = GBPROXY_INIT_VU_GEN_TX;
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200319}
320
321static void gbproxy_flush_stored_messages(struct gbproxy_peer *peer,
322 struct msgb *msg,
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200323 time_t now,
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200324 struct gbproxy_link_info* link_info,
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200325 struct gprs_gb_parse_context *parse_ctx)
326{
327 int rc;
328 struct msgb *stored_msg;
329 /* Got identity response with IMSI, assuming the request had
330 * been generated by the gbproxy */
331
332 LOGP(DLLC, LOGL_DEBUG,
333 "NSEI=%d(BSS) IMSI acquisition succeeded, "
334 "flushing stored messages\n",
335 msgb_nsei(msg));
336
337 /* Patch and flush stored messages towards the SGSN */
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200338 while ((stored_msg = msgb_dequeue(&link_info->stored_msgs))) {
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200339 struct gprs_gb_parse_context tmp_parse_ctx = {0};
340 tmp_parse_ctx.to_bss = 0;
Jacob Erlbeckd918f522014-09-17 10:56:38 +0200341 tmp_parse_ctx.peer_nsei = msgb_nsei(stored_msg);
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200342 int len_change = 0;
343
344 gprs_gb_parse_bssgp(msgb_bssgph(stored_msg),
345 msgb_bssgp_len(stored_msg),
346 &tmp_parse_ctx);
347 gbproxy_patch_bssgp(msg, msgb_bssgph(stored_msg),
348 msgb_bssgp_len(stored_msg),
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200349 peer, link_info, &len_change,
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200350 &tmp_parse_ctx);
351
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200352 gbproxy_update_link_state_after(peer, link_info, now,
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200353 &tmp_parse_ctx);
354
355 rc = gbprox_relay2sgsn(peer->cfg, stored_msg,
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200356 msgb_bvci(msg), link_info->sgsn_nsei);
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200357
358 if (rc < 0)
359 LOGP(DLLC, LOGL_ERROR,
360 "NSEI=%d(BSS) failed to send stored message "
361 "(%s)\n",
362 msgb_nsei(msg),
363 parse_ctx->llc_msg_name ?
364 parse_ctx->llc_msg_name : "BSSGP");
365 msgb_free(stored_msg);
366 }
367}
368
Jacob Erlbeck5e07b812014-09-22 15:41:21 +0200369static int gbproxy_gsm48_to_peer(struct gbproxy_peer *peer,
370 struct gbproxy_link_info* link_info,
371 uint16_t bvci,
372 struct msgb *msg /* Takes msg ownership */)
373{
374 int rc;
375
376 /* Workaround to avoid N(U) collisions and to enable a restart
377 * of the IMSI acquisition procedure. This will work unless the
378 * SGSN has an initial V(UT) within [256-32, 256+n_retries]
379 * (see GSM 04.64, 8.4.2). */
380 gprs_push_llc_ui(msg, 0, GPRS_SAPI_GMM, link_info->vu_gen_tx_bss);
381 link_info->vu_gen_tx_bss = (link_info->vu_gen_tx_bss + 1) % 512;
382
383 gprs_push_bssgp_dl_unitdata(msg, link_info->tlli.current);
384 rc = gbprox_relay2peer(msg, peer, bvci);
385 msgb_free(msg);
386 return rc;
387}
388
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200389static void gbproxy_acquire_imsi(struct gbproxy_peer *peer,
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200390 struct gbproxy_link_info* link_info,
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200391 uint16_t bvci)
392{
393 struct msgb *idreq_msg;
394
395 /* Send IDENT REQ */
396 idreq_msg = gsm48_msgb_alloc();
397 gprs_put_identity_req(idreq_msg, GSM_MI_TYPE_IMSI);
Jacob Erlbeck5e07b812014-09-22 15:41:21 +0200398 gbproxy_gsm48_to_peer(peer, link_info, bvci, idreq_msg);
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200399}
400
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200401static void gbproxy_tx_detach_acc(struct gbproxy_peer *peer,
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200402 struct gbproxy_link_info* link_info,
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200403 uint16_t bvci)
404{
405 struct msgb *detacc_msg;
406
407 /* Send DETACH ACC */
408 detacc_msg = gsm48_msgb_alloc();
409 gprs_put_mo_detach_acc(detacc_msg);
Jacob Erlbeck5e07b812014-09-22 15:41:21 +0200410 gbproxy_gsm48_to_peer(peer, link_info, bvci, detacc_msg);
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200411}
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200412
413/* Return != 0 iff msg still needs to be processed */
414static int gbproxy_imsi_acquisition(struct gbproxy_peer *peer,
415 struct msgb *msg,
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200416 time_t now,
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200417 struct gbproxy_link_info* link_info,
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200418 struct gprs_gb_parse_context *parse_ctx)
419{
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200420 struct msgb *stored_msg;
421
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200422 if (!link_info)
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200423 return 1;
424
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200425 if (!link_info->imsi_acq_pending && link_info->imsi_len > 0)
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200426 return 1;
427
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200428 if (parse_ctx->g48_hdr)
429 switch (parse_ctx->g48_hdr->msg_type)
430 {
Jacob Erlbecka93b4912014-09-18 10:08:27 +0200431 case GSM48_MT_GMM_RA_UPD_REQ:
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200432 case GSM48_MT_GMM_ATTACH_REQ:
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200433 if (gbproxy_restart_imsi_acquisition(link_info)) {
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200434 LOGP(DLLC, LOGL_INFO,
435 "NSEI=%d(BSS) IMSI acquisition was in progress "
Jacob Erlbecka93b4912014-09-18 10:08:27 +0200436 "when receiving an %s.\n",
437 msgb_nsei(msg), parse_ctx->llc_msg_name);
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200438 }
439 break;
440 case GSM48_MT_GMM_DETACH_REQ:
441 /* Nothing has been sent to the SGSN yet */
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200442 if (link_info->imsi_acq_pending) {
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200443 LOGP(DLLC, LOGL_INFO,
444 "NSEI=%d(BSS) IMSI acquisition was in progress "
445 "when receiving a DETACH_REQ.\n",
446 msgb_nsei(msg));
447 }
448 if (!parse_ctx->invalidate_tlli) {
449 LOGP(DLLC, LOGL_INFO,
450 "NSEI=%d(BSS) IMSI not yet acquired, "
451 "faking a DETACH_ACC.\n",
452 msgb_nsei(msg));
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200453 gbproxy_tx_detach_acc(peer, link_info, msgb_bvci(msg));
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200454 parse_ctx->invalidate_tlli = 1;
455 }
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200456 gbproxy_reset_imsi_acquisition(link_info);
457 gbproxy_update_link_state_after(peer, link_info, now,
Jacob Erlbeck9b7cfb02014-09-12 12:12:31 +0200458 parse_ctx);
459 return 0;
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200460 }
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200461
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200462 if (link_info->imsi_acq_pending && link_info->imsi_len > 0) {
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200463 int is_ident_resp =
464 parse_ctx->g48_hdr &&
465 parse_ctx->g48_hdr->proto_discr == GSM48_PDISC_MM_GPRS &&
466 parse_ctx->g48_hdr->msg_type == GSM48_MT_GMM_ID_RESP;
467
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200468 /* The IMSI is now available */
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200469 gbproxy_flush_stored_messages(peer, msg, now, link_info,
Jacob Erlbeck19a7c7a2014-09-19 13:23:21 +0200470 parse_ctx);
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200471
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200472 gbproxy_reset_imsi_acquisition(link_info);
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200473
474 /* This message is most probably the response to the ident
475 * request sent by gbproxy_acquire_imsi(). Don't forward it to
476 * the SGSN. */
477 return !is_ident_resp;
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200478 }
479
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200480 /* The message cannot be processed since the IMSI is still missing */
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200481
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200482 /* Enqueue unpatched messages */
483 LOGP(DLLC, LOGL_INFO,
484 "NSEI=%d(BSS) IMSI acquisition in progress, "
485 "storing message (%s)\n",
486 msgb_nsei(msg),
487 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200488
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200489 stored_msg = gprs_msgb_copy(msg, "process_bssgp_ul");
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200490 msgb_enqueue(&link_info->stored_msgs, stored_msg);
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200491
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200492 if (!link_info->imsi_acq_pending) {
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200493 LOGP(DLLC, LOGL_INFO,
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200494 "NSEI=%d(BSS) IMSI is required but not available, "
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200495 "initiating identification procedure (%s)\n",
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200496 msgb_nsei(msg),
497 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
498
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200499 gbproxy_acquire_imsi(peer, link_info, msgb_bvci(msg));
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200500
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200501 /* There is no explicit retransmission handling, the
502 * implementation relies on the MS doing proper retransmissions
503 * of the triggering message instead */
504
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200505 link_info->imsi_acq_pending = 1;
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200506 }
507
Jacob Erlbeck8ee2a5e2014-09-11 11:35:17 +0200508 return 0;
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200509}
510
511struct gbproxy_peer *gbproxy_find_peer(struct gbproxy_config *cfg,
512 struct msgb *msg,
513 struct gprs_gb_parse_context *parse_ctx)
514{
515 struct gbproxy_peer *peer = NULL;
516
517 if (msgb_bvci(msg) >= 2)
518 peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg));
519
520 if (!peer && !parse_ctx->to_bss)
521 peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg));
522
523 if (!peer)
524 peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx->bssgp_tp);
525
526 if (!peer) {
527 LOGP(DLLC, LOGL_INFO,
528 "NSEI=%d(%s) patching: didn't find peer for message, "
529 "PDU %d\n",
530 msgb_nsei(msg), parse_ctx->to_bss ? "BSS" : "SGSN",
531 parse_ctx->pdu_type);
532 /* Increment counter */
533 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]);
534 }
535 return peer;
536}
537
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200538/* patch BSSGP message */
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200539static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg,
540 struct msgb *msg,
541 struct gbproxy_peer *peer)
Jacob Erlbeck0f9273b2014-08-07 16:07:24 +0200542{
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200543 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck4dc663f2014-08-06 12:41:31 +0200544 int rc;
Jacob Erlbeck0f9273b2014-08-07 16:07:24 +0200545 int len_change = 0;
Jacob Erlbeckc404c082014-08-08 08:37:37 +0200546 time_t now;
Jacob Erlbeck7c276fd2014-09-19 16:34:01 +0200547 struct timespec ts = {0,};
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200548 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeck02817f02014-08-27 12:51:07 +0200549 uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei;
Jacob Erlbeck4dc663f2014-08-06 12:41:31 +0200550
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200551 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
Jacob Erlbeck49f0c3e2014-09-04 11:42:08 +0200552 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200553 return 1;
Jacob Erlbeck4dc663f2014-08-06 12:41:31 +0200554
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200555 parse_ctx.to_bss = 0;
Jacob Erlbeckd918f522014-09-17 10:56:38 +0200556 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200557
558 /* Parse BSSGP/LLC */
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200559 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
560 &parse_ctx);
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200561
Jacob Erlbeckf9132b22014-10-09 12:28:15 +0200562 if (!rc && !parse_ctx.need_decryption) {
563 LOGP(DGPRS, LOGL_ERROR,
564 "NSEI=%u(BSS) patching: failed to parse invalid %s message\n",
565 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
566 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
567 LOGP(DGPRS, LOGL_NOTICE,
568 "NSEI=%u(BSS) invalid message was: %s\n",
569 msgb_nsei(msg), msgb_hexdump(msg));
570 return 0;
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200571 }
572
573 /* Get peer */
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200574 if (!peer)
575 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200576
577 if (!peer)
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200578 return 0;
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200579
Jacob Erlbeck7c276fd2014-09-19 16:34:01 +0200580
581 clock_gettime(CLOCK_MONOTONIC, &ts);
582 now = ts.tv_sec;
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200583
Jacob Erlbeck2cb9e3f2014-08-18 15:41:41 +0200584 gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer,
585 parse_ctx.llc_msg_name);
586
Jacob Erlbeck42272c12014-10-09 12:16:17 +0200587 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
Jacob Erlbeck6e291e02014-09-03 11:59:48 +0200588
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200589 link_info = gbproxy_update_link_state_ul(peer, now, &parse_ctx);
Jacob Erlbeck6e291e02014-09-03 11:59:48 +0200590
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200591 if (parse_ctx.g48_hdr) {
592 switch (parse_ctx.g48_hdr->msg_type) {
593 case GSM48_MT_GMM_ATTACH_REQ:
594 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]);
595 break;
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200596
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200597 default:
598 break;
599 }
600 }
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200601
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200602 if (link_info && cfg->route_to_sgsn2) {
603 if (cfg->acquire_imsi && link_info->imsi_len == 0)
Jacob Erlbeckd918f522014-09-17 10:56:38 +0200604 sgsn_nsei = 0xffff;
Jacob Erlbeck3d805272014-09-25 13:21:48 +0200605 else if (gbproxy_imsi_matches(cfg, GBPROX_MATCH_ROUTING,
Jacob Erlbeckf4290b02014-09-25 11:17:31 +0200606 link_info))
Jacob Erlbeckd918f522014-09-17 10:56:38 +0200607 sgsn_nsei = cfg->nsip_sgsn2_nsei;
Jacob Erlbeck02817f02014-08-27 12:51:07 +0200608 }
609
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200610 if (link_info)
611 link_info->sgsn_nsei = sgsn_nsei;
Jacob Erlbeckd918f522014-09-17 10:56:38 +0200612
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200613 /* Handle IMSI acquisition */
614 if (cfg->acquire_imsi) {
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200615 rc = gbproxy_imsi_acquisition(peer, msg, now, link_info,
Jacob Erlbeck19a7c7a2014-09-19 13:23:21 +0200616 &parse_ctx);
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200617 if (rc <= 0)
618 return rc;
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200619 }
620
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200621 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200622 peer, link_info, &len_change, &parse_ctx);
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200623
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200624 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200625
Jacob Erlbeckd918f522014-09-17 10:56:38 +0200626 if (sgsn_nsei != cfg->nsip_sgsn_nsei) {
Jacob Erlbeck02817f02014-08-27 12:51:07 +0200627 /* Send message directly to the selected SGSN */
628 rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei);
629 /* Don't let the calling code handle the transmission */
630 return 0;
631 }
632
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200633 return 1;
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200634}
635
636/* patch BSSGP message to use core_mcc/mnc on the SGSN side */
637static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg,
638 struct msgb *msg,
639 struct gbproxy_peer *peer)
640{
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200641 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200642 int rc;
643 int len_change = 0;
644 time_t now;
Jacob Erlbeck7c276fd2014-09-19 16:34:01 +0200645 struct timespec ts = {0,};
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200646 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200647
Jacob Erlbeck49f0c3e2014-09-04 11:42:08 +0200648 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
649 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
650 return;
651
Jacob Erlbeck383c8412014-08-12 16:30:30 +0200652 parse_ctx.to_bss = 1;
Jacob Erlbeckd918f522014-09-17 10:56:38 +0200653 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck4dc663f2014-08-06 12:41:31 +0200654
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200655 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
656 &parse_ctx);
Jacob Erlbeck4dc663f2014-08-06 12:41:31 +0200657
Jacob Erlbeckf9132b22014-10-09 12:28:15 +0200658 if (!rc && !parse_ctx.need_decryption) {
659 LOGP(DGPRS, LOGL_ERROR,
660 "NSEI=%u(SGSN) patching: failed to parse invalid %s message\n",
661 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
662 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
663 LOGP(DGPRS, LOGL_NOTICE,
664 "NSEI=%u(SGSN) invalid message was: %s\n",
665 msgb_nsei(msg), msgb_hexdump(msg));
666 return;
Jacob Erlbeck4dc663f2014-08-06 12:41:31 +0200667 }
Jacob Erlbeck2504bc42014-05-19 10:14:58 +0200668
Jacob Erlbeck97dc4db2014-09-11 10:30:21 +0200669 /* Get peer */
670 if (!peer)
671 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck90a1fd12014-05-27 13:49:04 +0200672
Jacob Erlbeck90a1fd12014-05-27 13:49:04 +0200673 if (!peer)
Jacob Erlbeck90a1fd12014-05-27 13:49:04 +0200674 return;
Jacob Erlbeck90a1fd12014-05-27 13:49:04 +0200675
Jacob Erlbeck7c276fd2014-09-19 16:34:01 +0200676 clock_gettime(CLOCK_MONOTONIC, &ts);
677 now = ts.tv_sec;
Jacob Erlbeckc404c082014-08-08 08:37:37 +0200678
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200679 if (parse_ctx.g48_hdr) {
680 switch (parse_ctx.g48_hdr->msg_type) {
681 case GSM48_MT_GMM_ATTACH_REJ:
682 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]);
683 break;
Jacob Erlbeck0727f9a2014-05-28 10:59:10 +0200684
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200685 default:
686 break;
687 }
688 }
Jacob Erlbeck4dc663f2014-08-06 12:41:31 +0200689
Jacob Erlbeck42272c12014-10-09 12:16:17 +0200690 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200691
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200692 link_info = gbproxy_update_link_state_dl(peer, now, &parse_ctx);
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200693
694 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200695 peer, link_info, &len_change, &parse_ctx);
Jacob Erlbeckcf11e932014-08-19 12:21:01 +0200696
Jacob Erlbeck2b2406a2014-09-19 15:07:27 +0200697 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
Jacob Erlbeck4dc663f2014-08-06 12:41:31 +0200698
Jacob Erlbeckf14bb912014-06-27 19:56:05 +0200699 return;
Jacob Erlbeck2504bc42014-05-19 10:14:58 +0200700}
701
Harald Welted1991e72010-04-30 20:26:32 +0200702/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200703static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +0200704 uint16_t ns_bvci, uint16_t sgsn_nsei)
Harald Weltef01709d2010-05-03 18:54:58 +0200705{
Harald Welte3fb22d62010-05-12 18:10:25 +0000706 /* create a copy of the message so the old one can
707 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther8ac3a722014-08-04 11:52:52 +0200708 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2sgsn");
Jacob Erlbeck174bad42013-10-18 14:34:55 +0200709 int rc;
Harald Welte3fb22d62010-05-12 18:10:25 +0000710
Harald Welteb8da0612010-05-11 20:20:13 +0200711 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +0200712 msgb_nsei(msg), ns_bvci, sgsn_nsei);
Harald Welte6aa24d12010-05-03 19:22:32 +0200713
Harald Weltef01709d2010-05-03 18:54:58 +0200714 msgb_bvci(msg) = ns_bvci;
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +0200715 msgb_nsei(msg) = sgsn_nsei;
Harald Weltef01709d2010-05-03 18:54:58 +0200716
Harald Welte3ad32432010-05-03 19:05:10 +0200717 strip_ns_hdr(msg);
718
Jacob Erlbeck174bad42013-10-18 14:34:55 +0200719 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
720 if (rc < 0)
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200721 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Jacob Erlbeck174bad42013-10-18 14:34:55 +0200722
723 return rc;
Harald Weltef01709d2010-05-03 18:54:58 +0200724}
725
Harald Weltef01709d2010-05-03 18:54:58 +0200726/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +0200727static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
Harald Welted1991e72010-04-30 20:26:32 +0200728 uint16_t ns_bvci)
729{
Harald Welte3fb22d62010-05-12 18:10:25 +0000730 /* create a copy of the message so the old one can
731 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther8ac3a722014-08-04 11:52:52 +0200732 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2peer");
Jacob Erlbeck174bad42013-10-18 14:34:55 +0200733 int rc;
Harald Welte3fb22d62010-05-12 18:10:25 +0000734
Harald Welte0cc900d2010-05-13 10:34:56 +0200735 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200736 msgb_nsei(msg), ns_bvci, peer->nsei);
Harald Welte6aa24d12010-05-03 19:22:32 +0200737
Harald Welted1991e72010-04-30 20:26:32 +0200738 msgb_bvci(msg) = ns_bvci;
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200739 msgb_nsei(msg) = peer->nsei;
Harald Welted1991e72010-04-30 20:26:32 +0200740
Harald Welte0cc900d2010-05-13 10:34:56 +0200741 /* Strip the old NS header, it will be replaced with a new one */
Harald Welte3ad32432010-05-03 19:05:10 +0200742 strip_ns_hdr(msg);
743
Jacob Erlbeck174bad42013-10-18 14:34:55 +0200744 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
745 if (rc < 0)
746 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
747
748 return rc;
Harald Welted1991e72010-04-30 20:26:32 +0200749}
750
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200751static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type)
Harald Welte51b7d402011-02-06 13:09:29 +0100752{
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +0200753 struct gbproxy_peer *peer;
Harald Welte51b7d402011-02-06 13:09:29 +0100754
Jacob Erlbeck040fabc2014-08-21 10:01:30 +0200755 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte51b7d402011-02-06 13:09:29 +0100756 if (!peer) {
757 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
758 ptp_bvci);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200759 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte51b7d402011-02-06 13:09:29 +0100760 return -ENOENT;
761 }
762
763 switch (pdu_type) {
764 case BSSGP_PDUT_BVC_BLOCK_ACK:
765 peer->blocked = 1;
Jacob Erlbeck174bad42013-10-18 14:34:55 +0200766 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
Harald Welte51b7d402011-02-06 13:09:29 +0100767 break;
768 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
769 peer->blocked = 0;
Jacob Erlbeck174bad42013-10-18 14:34:55 +0200770 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
Harald Welte51b7d402011-02-06 13:09:29 +0100771 break;
772 default:
773 break;
774 }
775 return 0;
776}
777
Harald Welted1991e72010-04-30 20:26:32 +0200778/* Send a message to a peer identified by ptp_bvci but using ns_bvci
779 * in the NS hdr */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200780static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci,
Harald Welted1991e72010-04-30 20:26:32 +0200781 uint16_t ns_bvci)
782{
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +0200783 struct gbproxy_peer *peer;
Harald Welted1991e72010-04-30 20:26:32 +0200784
Jacob Erlbeck040fabc2014-08-21 10:01:30 +0200785 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte0358aae2010-05-03 21:37:11 +0200786 if (!peer) {
Harald Welte93048cf2010-05-13 14:14:56 +0200787 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
Harald Welte0358aae2010-05-03 21:37:11 +0200788 ptp_bvci);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200789 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welted1991e72010-04-30 20:26:32 +0200790 return -ENOENT;
Harald Welte0358aae2010-05-03 21:37:11 +0200791 }
Harald Welted1991e72010-04-30 20:26:32 +0200792
Harald Welte3ad32432010-05-03 19:05:10 +0200793 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200794}
795
Harald Weltea62e9fe2012-06-17 12:16:31 +0800796int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
797{
798 return 0;
799}
800
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200801/* Receive an incoming PTP message from a BSS-side NS-VC */
802static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg,
803 struct msgb *msg, uint16_t nsei,
804 uint16_t nsvci, uint16_t ns_bvci)
805{
806 struct gbproxy_peer *peer;
Jacob Erlbeck89033782014-08-28 13:20:39 +0200807 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
808 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200809 int rc;
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200810
811 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
Jacob Erlbeck66140cf2014-10-10 09:07:04 +0200812 if (!peer) {
813 LOGP(DGPRS, LOGL_NOTICE, "Didn't find peer for "
814 "BVCI=%u for PTP message from NSVC=%u/NSEI=%u (BSS), "
815 "discarding message\n",
816 ns_bvci, nsvci, nsei);
817 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
818 &ns_bvci, msg);
819 }
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200820
Jacob Erlbeckd88dd3c2014-10-02 14:56:44 +0200821 check_peer_nsei(peer, nsei);
Jacob Erlbeckd88dd3c2014-10-02 14:56:44 +0200822
Jacob Erlbeck66140cf2014-10-10 09:07:04 +0200823 rc = gbprox_process_bssgp_ul(cfg, msg, peer);
824 if (!rc)
Jacob Erlbeck4c0f6982014-08-22 17:10:01 +0200825 return 0;
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200826
Jacob Erlbeck89033782014-08-28 13:20:39 +0200827 switch (pdu_type) {
828 case BSSGP_PDUT_FLOW_CONTROL_BVC:
829 if (!cfg->route_to_sgsn2)
830 break;
831
832 /* Send a copy to the secondary SGSN */
833 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
834 break;
835 default:
836 break;
837 }
838
839
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +0200840 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200841}
842
843/* Receive an incoming PTP message from a SGSN-side NS-VC */
844static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg,
845 struct msgb *msg, uint16_t nsei,
846 uint16_t nsvci, uint16_t ns_bvci)
847{
848 struct gbproxy_peer *peer;
Jacob Erlbeck89033782014-08-28 13:20:39 +0200849 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
850 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200851
852 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
853
Jacob Erlbeck85a9f542014-08-21 15:07:11 +0200854 /* Send status messages before patching */
855
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200856 if (!peer) {
857 LOGP(DGPRS, LOGL_INFO, "Didn't find peer for "
858 "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n",
859 ns_bvci, nsvci, nsei);
860 rate_ctr_inc(&cfg->ctrg->
861 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
862 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
863 &ns_bvci, msg);
864 }
865
866 if (peer->blocked) {
867 LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for "
868 "blocked BVCI=%u via NSVC=%u/NSEI=%u\n",
869 ns_bvci, nsvci, nsei);
870 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Jacob Erlbeckd9bc3a52014-08-29 12:20:15 +0200871 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200872 }
873
Jacob Erlbeck89033782014-08-28 13:20:39 +0200874 switch (pdu_type) {
875 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
876 case BSSGP_PDUT_BVC_BLOCK_ACK:
877 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
878 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
879 /* Hide ACKs from the secondary SGSN, the primary SGSN
880 * is responsible to send them. */
881 return 0;
882 break;
883 default:
884 break;
885 }
886
Jacob Erlbeck85a9f542014-08-21 15:07:11 +0200887 /* Optionally patch the message */
888 gbprox_process_bssgp_dl(cfg, msg, peer);
889
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +0200890 return gbprox_relay2peer(msg, peer, ns_bvci);
891}
892
Harald Welted1991e72010-04-30 20:26:32 +0200893/* Receive an incoming signalling message from a BSS-side NS-VC */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +0200894static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg,
895 struct msgb *msg, uint16_t nsei,
Harald Welted1991e72010-04-30 20:26:32 +0200896 uint16_t ns_bvci)
897{
Harald Welte2ac2e912010-05-03 16:30:59 +0200898 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welted1991e72010-04-30 20:26:32 +0200899 struct tlv_parsed tp;
900 uint8_t pdu_type = bgph->pdu_type;
Harald Welte2ac2e912010-05-03 16:30:59 +0200901 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +0200902 struct gbproxy_peer *from_peer = NULL;
Harald Weltee49c8292010-05-01 12:10:57 +0200903 struct gprs_ra_id raid;
Jacob Erlbeck89033782014-08-28 13:20:39 +0200904 int copy_to_sgsn2 = 0;
Jacob Erlbeck4d16d6c2014-09-08 09:04:01 +0200905 int rc;
Harald Welted1991e72010-04-30 20:26:32 +0200906
Harald Welte41270722011-02-06 17:13:12 +0100907 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte93048cf2010-05-13 14:14:56 +0200908 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200909 nsei, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200910 return -EINVAL;
911 }
912
913 /* we actually should never see those two for BVCI == 0, but double-check
914 * just to make sure */
915 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
916 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte6aa24d12010-05-03 19:22:32 +0200917 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200918 "signalling\n", nsei);
Harald Welted1991e72010-04-30 20:26:32 +0200919 return -EINVAL;
920 }
921
922 bssgp_tlv_parse(&tp, bgph->data, data_len);
923
924 switch (pdu_type) {
925 case BSSGP_PDUT_SUSPEND:
926 case BSSGP_PDUT_RESUME:
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +0200927 /* We implement RAI snooping during SUSPEND/RESUME, since it
928 * establishes a relationsip between BVCI/peer and the routeing
929 * area identification. The snooped information is then used
930 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
931 * BSSGP */
Harald Welted1991e72010-04-30 20:26:32 +0200932 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
933 goto err_mand_ie;
Jacob Erlbeck040fabc2014-08-21 10:01:30 +0200934 from_peer = gbproxy_peer_by_nsei(cfg, nsei);
Harald Welted1991e72010-04-30 20:26:32 +0200935 if (!from_peer)
936 goto err_no_peer;
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200937 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
938 sizeof(from_peer->ra));
Harald Weltef2fdefe2010-05-11 10:15:42 +0200939 gsm48_parse_ra(&raid, from_peer->ra);
Harald Welte93048cf2010-05-13 14:14:56 +0200940 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +0200941 "RAI snooping: RAI %u-%u-%u-%u behind BVCI=%u\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200942 nsei, raid.mcc, raid.mnc, raid.lac,
943 raid.rac , from_peer->bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200944 /* FIXME: This only supports one BSS per RA */
945 break;
Harald Welte6aa24d12010-05-03 19:22:32 +0200946 case BSSGP_PDUT_BVC_RESET:
947 /* If we receive a BVC reset on the signalling endpoint, we
948 * don't want the SGSN to reset, as the signalling endpoint
949 * is common for all point-to-point BVCs (and thus all BTS) */
950 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freyther25eeaab2012-09-04 20:42:20 +0200951 uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Weltebe6e25a2010-05-12 00:20:41 +0200952 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200953 nsei, bvci);
Harald Welte6aa24d12010-05-03 19:22:32 +0200954 if (bvci == 0) {
955 /* FIXME: only do this if SGSN is alive! */
Harald Welteb8da0612010-05-11 20:20:13 +0200956 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200957 "BVC RESET ACK of BVCI=0\n", nsei);
Harald Welte6aa24d12010-05-03 19:22:32 +0200958 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200959 nsei, 0, ns_bvci);
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200960 }
Jacob Erlbeck040fabc2014-08-21 10:01:30 +0200961 from_peer = gbproxy_peer_by_bvci(cfg, bvci);
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200962 if (!from_peer) {
Harald Welte0358aae2010-05-03 21:37:11 +0200963 /* if a PTP-BVC is reset, and we don't know that
964 * PTP-BVCI yet, we should allocate a new peer */
965 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200966 "BVCI=%u via NSEI=%u\n", bvci, nsei);
Holger Hans Peter Freyther60fa5b92014-08-04 17:10:08 +0200967 from_peer = gbproxy_peer_alloc(cfg, bvci);
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200968 from_peer->nsei = nsei;
Harald Welte6aa24d12010-05-03 19:22:32 +0200969 }
Jacob Erlbeckd0a9b502013-10-24 12:48:56 +0200970
971 if (!check_peer_nsei(from_peer, nsei))
972 from_peer->nsei = nsei;
973
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200974 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
975 struct gprs_ra_id raid;
976 /* We have a Cell Identifier present in this
977 * PDU, this means we can extend our local
978 * state information about this particular cell
979 * */
980 memcpy(from_peer->ra,
981 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
982 sizeof(from_peer->ra));
983 gsm48_parse_ra(&raid, from_peer->ra);
984 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u "
Jacob Erlbecke48c3472013-10-15 12:00:27 +0200985 "Cell ID %u-%u-%u-%u\n", nsei,
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200986 bvci, raid.mcc, raid.mnc, raid.lac,
987 raid.rac);
988 }
Jacob Erlbeck89033782014-08-28 13:20:39 +0200989 if (cfg->route_to_sgsn2)
990 copy_to_sgsn2 = 1;
Harald Welte6aa24d12010-05-03 19:22:32 +0200991 }
992 break;
Harald Welted1991e72010-04-30 20:26:32 +0200993 }
994
Harald Welteb8da0612010-05-11 20:20:13 +0200995 /* Normally, we can simply pass on all signalling messages from BSS to
996 * SGSN */
Jacob Erlbeck4d16d6c2014-09-08 09:04:01 +0200997 rc = gbprox_process_bssgp_ul(cfg, msg, from_peer);
998 if (!rc)
999 return 0;
Jacob Erlbeck89033782014-08-28 13:20:39 +02001000
1001 if (copy_to_sgsn2)
1002 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
1003
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +02001004 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Harald Welted1991e72010-04-30 20:26:32 +02001005err_no_peer:
Jacob Erlbeck700b8732013-10-18 13:04:48 +02001006 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001007 nsei);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001008 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Jacob Erlbeckd9bc3a52014-08-29 12:20:15 +02001009 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +02001010err_mand_ie:
Harald Welte874acec2010-05-11 10:01:17 +02001011 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001012 nsei);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001013 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welte874acec2010-05-11 10:01:17 +02001014 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +02001015}
1016
1017/* Receive paging request from SGSN, we need to relay to proper BSS */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001018static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp,
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001019 uint32_t nsei, uint16_t ns_bvci)
Harald Welted1991e72010-04-30 20:26:32 +02001020{
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +02001021 struct gbproxy_peer *peer = NULL;
Jacob Erlbeck174bad42013-10-18 14:34:55 +02001022 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welted1991e72010-04-30 20:26:32 +02001023
Harald Welte93048cf2010-05-13 14:14:56 +02001024 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001025 nsei);
Harald Welted1991e72010-04-30 20:26:32 +02001026 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freyther25eeaab2012-09-04 20:42:20 +02001027 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte93048cf2010-05-13 14:14:56 +02001028 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n",
1029 bvci);
Jacob Erlbeck174bad42013-10-18 14:34:55 +02001030 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welted1991e72010-04-30 20:26:32 +02001031 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
Jacob Erlbeck040fabc2014-08-21 10:01:30 +02001032 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +02001033 LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n",
Holger Hans Peter Freyther98b82c12010-06-08 16:30:24 +08001034 peer ? peer->bvci : -1);
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +02001035 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welted1991e72010-04-30 20:26:32 +02001036 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
Jacob Erlbeck040fabc2014-08-21 10:01:30 +02001037 peer = gbproxy_peer_by_lai(cfg, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +02001038 LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n",
Holger Hans Peter Freyther98b82c12010-06-08 16:30:24 +08001039 peer ? peer->bvci : -1);
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +02001040 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welted1991e72010-04-30 20:26:32 +02001041 } else
Harald Welte93048cf2010-05-13 14:14:56 +02001042 LOGPC(DGPRS, LOGL_INFO, "\n");
1043
1044 if (!peer) {
1045 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001046 "unable to route, missing IE\n", nsei);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001047 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
Harald Welted1991e72010-04-30 20:26:32 +02001048 return -EINVAL;
Harald Welte93048cf2010-05-13 14:14:56 +02001049 }
1050 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +02001051}
1052
Harald Welte874acec2010-05-11 10:01:17 +02001053/* Receive an incoming BVC-RESET message from the SGSN */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001054static int rx_reset_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001055 struct msgb *orig_msg,
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001056 struct msgb *msg, struct tlv_parsed *tp,
1057 uint32_t nsei, uint16_t ns_bvci)
Harald Welte874acec2010-05-11 10:01:17 +02001058{
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +02001059 struct gbproxy_peer *peer;
Harald Welte874acec2010-05-11 10:01:17 +02001060 uint16_t ptp_bvci;
1061
1062 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001063 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeck174bad42013-10-18 14:34:55 +02001064 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte874acec2010-05-11 10:01:17 +02001065 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001066 NULL, orig_msg);
Harald Welte874acec2010-05-11 10:01:17 +02001067 }
Holger Hans Peter Freyther25eeaab2012-09-04 20:42:20 +02001068 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte874acec2010-05-11 10:01:17 +02001069
1070 if (ptp_bvci >= 2) {
1071 /* A reset for a PTP BVC was received, forward it to its
1072 * respective peer */
Jacob Erlbeck040fabc2014-08-21 10:01:30 +02001073 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte874acec2010-05-11 10:01:17 +02001074 if (!peer) {
Harald Welte93048cf2010-05-13 14:14:56 +02001075 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001076 nsei, ptp_bvci);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001077 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeck174bad42013-10-18 14:34:55 +02001078 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte874acec2010-05-11 10:01:17 +02001079 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
Jacob Erlbeckd9bc3a52014-08-29 12:20:15 +02001080 &ptp_bvci, orig_msg);
Harald Welte874acec2010-05-11 10:01:17 +02001081 }
1082 return gbprox_relay2peer(msg, peer, ns_bvci);
1083 }
1084
1085 /* A reset for the Signalling entity has been received
1086 * from the SGSN. As the signalling BVCI is shared
1087 * among all the BSS's that we multiplex, it needs to
1088 * be relayed */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001089 llist_for_each_entry(peer, &cfg->bts_peers, list)
Harald Welte874acec2010-05-11 10:01:17 +02001090 gbprox_relay2peer(msg, peer, ns_bvci);
1091
1092 return 0;
1093}
1094
Harald Welted1991e72010-04-30 20:26:32 +02001095/* Receive an incoming signalling message from the SGSN-side NS-VC */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001096static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001097 struct msgb *orig_msg, uint32_t nsei,
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001098 uint16_t ns_bvci)
Harald Welted1991e72010-04-30 20:26:32 +02001099{
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001100 struct bssgp_normal_hdr *bgph =
1101 (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg);
Harald Welted1991e72010-04-30 20:26:32 +02001102 struct tlv_parsed tp;
1103 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001104 int data_len;
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +02001105 struct gbproxy_peer *peer;
Harald Welted1991e72010-04-30 20:26:32 +02001106 uint16_t bvci;
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001107 struct msgb *msg;
Harald Welted1991e72010-04-30 20:26:32 +02001108 int rc = 0;
Jacob Erlbeck0936c8a2014-08-29 13:32:17 +02001109 int cause;
Harald Welted1991e72010-04-30 20:26:32 +02001110
Harald Welte41270722011-02-06 17:13:12 +01001111 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte93048cf2010-05-13 14:14:56 +02001112 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001113 "signalling\n", nsei, ns_bvci);
Harald Welte874acec2010-05-11 10:01:17 +02001114 /* FIXME: Send proper error message */
Harald Welted1991e72010-04-30 20:26:32 +02001115 return -EINVAL;
1116 }
1117
1118 /* we actually should never see those two for BVCI == 0, but double-check
1119 * just to make sure */
1120 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1121 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte6aa24d12010-05-03 19:22:32 +02001122 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001123 "signalling\n", nsei);
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001124 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welted1991e72010-04-30 20:26:32 +02001125 }
1126
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001127 msg = gprs_msgb_copy(orig_msg, "rx_sig_from_sgsn");
1128 gbprox_process_bssgp_dl(cfg, msg, NULL);
1129 /* Update message info */
1130 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1131 data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph);
Harald Welted1991e72010-04-30 20:26:32 +02001132 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1133
1134 switch (pdu_type) {
Harald Welte874acec2010-05-11 10:01:17 +02001135 case BSSGP_PDUT_BVC_RESET:
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001136 rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci);
Harald Welte874acec2010-05-11 10:01:17 +02001137 break;
Harald Welted1991e72010-04-30 20:26:32 +02001138 case BSSGP_PDUT_BVC_RESET_ACK:
Jacob Erlbeck89033782014-08-28 13:20:39 +02001139 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
1140 break;
1141 /* fall through */
1142 case BSSGP_PDUT_FLUSH_LL:
Harald Welted1991e72010-04-30 20:26:32 +02001143 /* simple case: BVCI IE is mandatory */
1144 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1145 goto err_mand_ie;
Holger Hans Peter Freyther25eeaab2012-09-04 20:42:20 +02001146 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001147 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +02001148 break;
1149 case BSSGP_PDUT_PAGING_PS:
1150 case BSSGP_PDUT_PAGING_CS:
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +02001151 /* process the paging request (LAI/RAI lookup) */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001152 rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +02001153 break;
1154 case BSSGP_PDUT_STATUS:
Harald Welte874acec2010-05-11 10:01:17 +02001155 /* Some exception has occurred */
Harald Welte6aa24d12010-05-03 19:22:32 +02001156 LOGP(DGPRS, LOGL_NOTICE,
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001157 "NSEI=%u(SGSN) BSSGP STATUS ", nsei);
Harald Welte874acec2010-05-11 10:01:17 +02001158 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
1159 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1160 goto err_mand_ie;
1161 }
Jacob Erlbeck0936c8a2014-08-29 13:32:17 +02001162 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
Harald Welte874acec2010-05-11 10:01:17 +02001163 LOGPC(DGPRS, LOGL_NOTICE,
1164 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
Jacob Erlbeck0936c8a2014-08-29 13:32:17 +02001165 bssgp_cause_str(cause));
Harald Welte874acec2010-05-11 10:01:17 +02001166 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Jacob Erlbeck0936c8a2014-08-29 13:32:17 +02001167 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1168 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%u\n", bvci);
1169
1170 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI)
1171 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte874acec2010-05-11 10:01:17 +02001172 } else
1173 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welted1991e72010-04-30 20:26:32 +02001174 break;
1175 /* those only exist in the SGSN -> BSS direction */
1176 case BSSGP_PDUT_SUSPEND_ACK:
1177 case BSSGP_PDUT_SUSPEND_NACK:
1178 case BSSGP_PDUT_RESUME_ACK:
1179 case BSSGP_PDUT_RESUME_NACK:
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +02001180 /* RAI IE is mandatory */
Harald Welted1991e72010-04-30 20:26:32 +02001181 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1182 goto err_mand_ie;
Jacob Erlbeck040fabc2014-08-21 10:01:30 +02001183 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
Harald Welted1991e72010-04-30 20:26:32 +02001184 if (!peer)
1185 goto err_no_peer;
Harald Welte3ad32432010-05-03 19:05:10 +02001186 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +02001187 break;
Harald Welte51b7d402011-02-06 13:09:29 +01001188 case BSSGP_PDUT_BVC_BLOCK_ACK:
1189 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1190 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1191 goto err_mand_ie;
Holger Hans Peter Freyther25eeaab2012-09-04 20:42:20 +02001192 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte51b7d402011-02-06 13:09:29 +01001193 if (bvci == 0) {
1194 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP "
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001195 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
Harald Welte51b7d402011-02-06 13:09:29 +01001196 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
1197 /* should we send STATUS ? */
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_INV_BVCI]);
Harald Welte51b7d402011-02-06 13:09:29 +01001200 } else {
1201 /* Mark BVC as (un)blocked */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001202 block_unblock_peer(cfg, bvci, pdu_type);
Harald Welte51b7d402011-02-06 13:09:29 +01001203 }
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001204 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte51b7d402011-02-06 13:09:29 +01001205 break;
Harald Welted1991e72010-04-30 20:26:32 +02001206 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte6aa24d12010-05-03 19:22:32 +02001207 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001208 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001209 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeck174bad42013-10-18 14:34:55 +02001210 ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]);
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001211 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, orig_msg);
Harald Welted1991e72010-04-30 20:26:32 +02001212 break;
1213 default:
Harald Weltebe6e25a2010-05-12 00:20:41 +02001214 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n",
1215 pdu_type);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001216 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeck174bad42013-10-18 14:34:55 +02001217 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001218 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welted1991e72010-04-30 20:26:32 +02001219 break;
1220 }
1221
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001222 msgb_free(msg);
1223
Harald Welted1991e72010-04-30 20:26:32 +02001224 return rc;
1225err_mand_ie:
Harald Welte0358aae2010-05-03 21:37:11 +02001226 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001227 nsei);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001228 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeck174bad42013-10-18 14:34:55 +02001229 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001230 msgb_free(msg);
1231 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg);
Harald Welted1991e72010-04-30 20:26:32 +02001232err_no_peer:
Jacob Erlbeck9d7268a2014-06-02 10:49:00 +02001233 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n",
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001234 nsei);
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001235 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
Jacob Erlbeck85a9f542014-08-21 15:07:11 +02001236 msgb_free(msg);
Jacob Erlbeckd9bc3a52014-08-29 12:20:15 +02001237 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg);
Harald Welted1991e72010-04-30 20:26:32 +02001238}
1239
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +02001240static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei)
1241{
1242 return nsei == cfg->nsip_sgsn_nsei ||
1243 (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei);
1244}
1245
Harald Welted1991e72010-04-30 20:26:32 +02001246/* Main input function for Gb proxy */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001247int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei,
1248 uint16_t ns_bvci, uint16_t nsvci)
Harald Welted1991e72010-04-30 20:26:32 +02001249{
Harald Weltef01709d2010-05-03 18:54:58 +02001250 int rc;
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +02001251 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei);
Harald Welted1991e72010-04-30 20:26:32 +02001252
1253 /* Only BVCI=0 messages need special treatment */
1254 if (ns_bvci == 0 || ns_bvci == 1) {
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001255 if (remote_end_is_sgsn)
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001256 rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +02001257 else
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001258 rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci);
Harald Weltef01709d2010-05-03 18:54:58 +02001259 } else {
Jacob Erlbeck5cd205e2014-08-21 13:45:04 +02001260 /* All other BVCI are PTP */
1261 if (remote_end_is_sgsn)
1262 rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei, nsvci,
1263 ns_bvci);
1264 else
1265 rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei, nsvci,
1266 ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +02001267 }
1268
Harald Weltef01709d2010-05-03 18:54:58 +02001269 return rc;
Harald Welted1991e72010-04-30 20:26:32 +02001270}
Harald Welteb34e2c12010-05-11 05:49:43 +02001271
Harald Welte9ee404a2010-05-14 11:53:08 +00001272int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi)
1273{
1274 struct gprs_nsvc *nsvc;
1275
1276 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
1277 if (!nsvc->persistent)
1278 continue;
1279 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1280 }
1281 return 0;
1282}
1283
Harald Welte5bb0d362010-05-11 06:34:24 +02001284/* Signal handler for signals from NS layer */
1285int gbprox_signal(unsigned int subsys, unsigned int signal,
1286 void *handler_data, void *signal_data)
1287{
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001288 struct gbproxy_config *cfg = handler_data;
Harald Welte5bb0d362010-05-11 06:34:24 +02001289 struct ns_signal_data *nssd = signal_data;
1290 struct gprs_nsvc *nsvc = nssd->nsvc;
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +02001291 struct gbproxy_peer *peer;
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +02001292 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsvc->nsei);
Harald Welte5bb0d362010-05-11 06:34:24 +02001293
Harald Welte7ed98e32012-06-16 16:40:42 +08001294 if (subsys != SS_L_NS)
Harald Welte5bb0d362010-05-11 06:34:24 +02001295 return 0;
1296
Jacob Erlbeck3803e9b2014-08-26 14:47:15 +02001297 if (signal == S_NS_RESET && remote_end_is_sgsn) {
Harald Welte15cbb0e2010-05-11 18:29:44 +02001298 /* We have received a NS-RESET from the NSEI and NSVC
1299 * of the SGSN. This might happen with SGSN that start
1300 * their own NS-RESET procedure without waiting for our
1301 * NS-RESET */
1302 nsvc->remote_end_is_sgsn = 1;
1303 }
1304
Harald Weltefa41cb72010-05-12 13:28:25 +00001305 if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) {
1306 LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, "
1307 "re-starting RESET procedure\n");
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001308 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeck174bad42013-10-18 14:34:55 +02001309 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
Harald Weltea529c072012-06-17 12:25:53 +08001310 gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr,
1311 nsvc->nsei, nsvc->nsvci);
Harald Weltefa41cb72010-05-12 13:28:25 +00001312 }
1313
Harald Weltedb2a2d52011-02-06 16:33:29 +01001314 if (!nsvc->remote_end_is_sgsn) {
1315 /* from BSS to SGSN */
Jacob Erlbeck040fabc2014-08-21 10:01:30 +02001316 peer = gbproxy_peer_by_nsei(cfg, nsvc->nsei);
Harald Weltedb2a2d52011-02-06 16:33:29 +01001317 if (!peer) {
1318 LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer "
1319 "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei,
1320 nsvc->nsvci);
1321 return 0;
1322 }
Harald Welte5bb0d362010-05-11 06:34:24 +02001323 switch (signal) {
1324 case S_NS_RESET:
Harald Welte5bb0d362010-05-11 06:34:24 +02001325 case S_NS_BLOCK:
Harald Weltedb2a2d52011-02-06 16:33:29 +01001326 if (!peer->blocked)
1327 break;
1328 LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from "
1329 "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n",
1330 nsvc->nsei, nsvc->nsvci);
1331 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei,
1332 peer->bvci, 0);
Harald Welte5bb0d362010-05-11 06:34:24 +02001333 break;
Harald Weltedb2a2d52011-02-06 16:33:29 +01001334 }
1335 } else {
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001336 /* Forward this message to all NS-VC to BSS */
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001337 struct gprs_ns_inst *nsi = cfg->nsi;
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001338 struct gprs_nsvc *next_nsvc;
1339
1340 llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) {
1341 if (next_nsvc->remote_end_is_sgsn)
1342 continue;
1343
1344 /* Note that the following does not start the full
1345 * procedures including timer based retransmissions. */
Harald Weltedb2a2d52011-02-06 16:33:29 +01001346 switch (signal) {
1347 case S_NS_RESET:
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001348 gprs_ns_tx_reset(next_nsvc, nssd->cause);
Harald Weltedb2a2d52011-02-06 16:33:29 +01001349 break;
1350 case S_NS_BLOCK:
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001351 gprs_ns_tx_block(next_nsvc, nssd->cause);
Harald Weltedb2a2d52011-02-06 16:33:29 +01001352 break;
1353 case S_NS_UNBLOCK:
Jacob Erlbecke48c3472013-10-15 12:00:27 +02001354 gprs_ns_tx_unblock(next_nsvc);
Harald Weltedb2a2d52011-02-06 16:33:29 +01001355 break;
1356 }
Harald Welte5bb0d362010-05-11 06:34:24 +02001357 }
1358 }
1359 return 0;
1360}
1361
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001362void gbprox_reset(struct gbproxy_config *cfg)
Jacob Erlbeck67f03bd2013-10-24 12:48:55 +02001363{
Holger Hans Peter Freytherd64bf222014-08-04 11:35:32 +02001364 struct gbproxy_peer *peer, *tmp;
Jacob Erlbeck67f03bd2013-10-24 12:48:55 +02001365
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001366 llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list)
Holger Hans Peter Freyther60fa5b92014-08-04 17:10:08 +02001367 gbproxy_peer_free(peer);
Jacob Erlbeck67f03bd2013-10-24 12:48:55 +02001368
Holger Hans Peter Freyther7d9c1df2014-08-04 15:42:36 +02001369 rate_ctr_group_free(cfg->ctrg);
1370 gbproxy_init_config(cfg);
Jacob Erlbeck67f03bd2013-10-24 12:48:55 +02001371}
1372
Holger Hans Peter Freyther5eaf1a22014-08-04 11:10:09 +02001373int gbproxy_init_config(struct gbproxy_config *cfg)
Jacob Erlbeck7fee9722013-10-24 12:48:23 +02001374{
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +02001375 struct timespec tp;
Jacob Erlbeckf4290b02014-09-25 11:17:31 +02001376
Holger Hans Peter Freyther5eaf1a22014-08-04 11:10:09 +02001377 INIT_LLIST_HEAD(&cfg->bts_peers);
Holger Hans Peter Freyther98e753f2014-08-04 11:19:56 +02001378 cfg->ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0);
Jacob Erlbeck4c47f2f2014-08-11 19:12:24 +02001379 clock_gettime(CLOCK_REALTIME, &tp);
Jacob Erlbeckf4290b02014-09-25 11:17:31 +02001380
Holger Hans Peter Freyther5eaf1a22014-08-04 11:10:09 +02001381 return 0;
Jacob Erlbeck7fee9722013-10-24 12:48:23 +02001382}