blob: 17a01091b157073bf75cca2eb897c0149db696a7 [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>
Jacob Erlbeck46caed82015-11-02 15:15:38 +010037#include <osmocom/core/stats.h>
Jacob Erlbeckbc555742013-10-18 14:34:55 +020038
Harald Welteea34a4e2012-06-16 14:59:56 +080039#include <osmocom/gprs/gprs_ns.h>
40#include <osmocom/gprs/gprs_bssgp.h>
41
Jacob Erlbeck7c101d92014-06-06 18:49:23 +020042#include <osmocom/gsm/gsm_utils.h>
43
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020044#include <osmocom/sgsn/signal.h>
45#include <osmocom/sgsn/debug.h>
46#include <osmocom/sgsn/gprs_gb_parse.h>
47#include <osmocom/sgsn/gb_proxy.h>
Harald Welte9f75c352010-04-30 20:26:32 +020048
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020049#include <osmocom/sgsn/gprs_llc.h>
Harald Welte53373bc2016-04-20 17:11:43 +020050#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020051#include <osmocom/sgsn/gprs_utils.h>
Jacob Erlbeck67a44452014-05-19 10:14:58 +020052
Daniel Willmann537d4802015-10-12 19:36:35 +020053#include <openssl/rand.h>
54
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020055extern void *tall_bsc_ctx;
56
Jacob Erlbeckbc555742013-10-18 14:34:55 +020057static const struct rate_ctr_desc global_ctr_description[] = {
58 { "inv-bvci", "Invalid BVC Identifier " },
Jacob Erlbeck8f503592014-06-02 10:49:00 +020059 { "inv-lai", "Invalid Location Area Identifier" },
60 { "inv-rai", "Invalid Routing Area Identifier " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020061 { "inv-nsei", "No BVC established for NSEI " },
62 { "proto-err.bss", "BSSGP protocol error (BSS )" },
63 { "proto-err.sgsn", "BSSGP protocol error (SGSN)" },
64 { "not-supp.bss", "Feature not supported (BSS )" },
65 { "not-supp.sgsn", "Feature not supported (SGSN)" },
66 { "restart.sgsn", "Restarted RESET procedure (SGSN)" },
67 { "tx-err.sgsn", "NS Transmission error (SGSN)" },
68 { "error", "Other error " },
Jacob Erlbeck006c0382014-05-27 13:49:04 +020069 { "mod-peer-err", "Patch error: no peer " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020070};
71
72static const struct rate_ctr_group_desc global_ctrg_desc = {
73 .group_name_prefix = "gbproxy.global",
74 .group_description = "GBProxy Global Statistics",
75 .num_ctr = ARRAY_SIZE(global_ctr_description),
76 .ctr_desc = global_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010077 .class_id = OSMO_STATS_CLASS_GLOBAL,
Jacob Erlbeckbc555742013-10-18 14:34:55 +020078};
79
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020080static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
81 uint16_t ns_bvci);
82static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +020083 uint16_t ns_bvci, uint16_t sgsn_nsei);
Jacob Erlbeckd211d1d2014-09-22 13:30:46 +020084static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info);
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020085
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +020086static int check_peer_nsei(struct gbproxy_peer *peer, uint16_t nsei)
Jacob Erlbeckbc555742013-10-18 14:34:55 +020087{
88 if (peer->nsei != nsei) {
89 LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI "
90 "BVCI=%u via NSEI=%u (expected NSEI=%u)\n",
91 peer->bvci, nsei, peer->nsei);
92 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]);
Jacob Erlbeck64cb9242013-10-24 12:48:56 +020093 return 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +020094 }
95
Jacob Erlbeck64cb9242013-10-24 12:48:56 +020096 return 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +020097}
98
Harald Welte69619e32010-05-03 19:05:10 +020099/* strip off the NS header */
100static void strip_ns_hdr(struct msgb *msg)
101{
102 int strip_len = msgb_bssgph(msg) - msg->data;
103 msgb_pull(msg, strip_len);
104}
105
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200106/* Transmit Chapter 9.2.10 Identity Request */
107static void gprs_put_identity_req(struct msgb *msg, uint8_t id_type)
108{
109 struct gsm48_hdr *gh;
110
111 id_type &= GSM_MI_TYPE_MASK;
112
113 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
114 gh->proto_discr = GSM48_PDISC_MM_GPRS;
115 gh->msg_type = GSM48_MT_GMM_ID_REQ;
116 gh->data[0] = id_type;
117}
118
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200119/* Transmit Chapter 9.4.6.2 Detach Accept (mobile originated detach) */
120static void gprs_put_mo_detach_acc(struct msgb *msg)
121{
122 struct gsm48_hdr *gh;
123
124 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
125 gh->proto_discr = GSM48_PDISC_MM_GPRS;
126 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
127 gh->data[0] = 0; /* no force to standby */
128}
129
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200130static void gprs_push_llc_ui(struct msgb *msg,
131 int is_uplink, unsigned sapi, unsigned nu)
132{
133 const uint8_t e_bit = 0;
134 const uint8_t pm_bit = 1;
135 const uint8_t cr_bit = is_uplink ? 0 : 1;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200136 uint8_t *llc;
137 uint8_t *fcs_field;
138 uint32_t fcs;
139
140 nu &= 0x01ff; /* 9 Bit */
141
142 llc = msgb_push(msg, 3);
143 llc[0] = (cr_bit << 6) | (sapi & 0x0f);
144 llc[1] = 0xc0 | (nu >> 6); /* UI frame */
145 llc[2] = (nu << 2) | ((e_bit & 1) << 1) | (pm_bit & 1);
146
147 fcs = gprs_llc_fcs(llc, msgb_length(msg));
148 fcs_field = msgb_put(msg, 3);
149 fcs_field[0] = (uint8_t)(fcs >> 0);
150 fcs_field[1] = (uint8_t)(fcs >> 8);
151 fcs_field[2] = (uint8_t)(fcs >> 16);
152}
153
154static void gprs_push_bssgp_dl_unitdata(struct msgb *msg,
155 uint32_t tlli)
156{
157 struct bssgp_ud_hdr *budh;
158 uint8_t *llc = msgb_data(msg);
159 size_t llc_size = msgb_length(msg);
160 const size_t llc_ie_hdr_size = 3;
161 const uint8_t qos_profile[] = {0x00, 0x50, 0x20}; /* hard-coded */
162 const uint8_t lifetime[] = {0x02, 0x58}; /* 6s hard-coded */
163
164 const size_t bssgp_overhead = sizeof(*budh) +
165 TVLV_GROSS_LEN(sizeof(lifetime)) + llc_ie_hdr_size;
166 uint8_t *ie;
167 uint32_t tlli_be = htonl(tlli);
168
169 budh = (struct bssgp_ud_hdr *)msgb_push(msg, bssgp_overhead);
170
171 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
172 memcpy(&budh->tlli, &tlli_be, sizeof(budh->tlli));
173 memcpy(&budh->qos_profile, qos_profile, sizeof(budh->qos_profile));
174
175 ie = budh->data;
176 tvlv_put(ie, BSSGP_IE_PDU_LIFETIME, sizeof(lifetime), lifetime);
177 ie += TVLV_GROSS_LEN(sizeof(lifetime));
178
179 /* Note: Add alignment before the LLC IE if inserting other IE */
180
181 *(ie++) = BSSGP_IE_LLC_PDU;
182 *(ie++) = llc_size / 256;
183 *(ie++) = llc_size % 256;
184
185 OSMO_ASSERT(ie == llc);
186
187 msgb_bssgph(msg) = (uint8_t *)budh;
188 msgb_tlli(msg) = tlli;
189}
190
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200191/* update peer according to the BSS message */
192static void gbprox_update_current_raid(uint8_t *raid_enc,
193 struct gbproxy_peer *peer,
194 const char *log_text)
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200195{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200196 struct gbproxy_patch_state *state = &peer->patch_state;
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200197 const int old_local_mcc = state->local_mcc;
198 const int old_local_mnc = state->local_mnc;
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200199 struct gprs_ra_id raid;
200
201 if (!raid_enc)
202 return;
203
204 gsm48_parse_ra(&raid, raid_enc);
205
206 /* save source side MCC/MNC */
207 if (!peer->cfg->core_mcc || raid.mcc == peer->cfg->core_mcc) {
208 state->local_mcc = 0;
209 } else {
210 state->local_mcc = raid.mcc;
211 }
212
213 if (!peer->cfg->core_mnc || raid.mnc == peer->cfg->core_mnc) {
214 state->local_mnc = 0;
215 } else {
216 state->local_mnc = raid.mnc;
217 }
218
219 if (old_local_mcc != state->local_mcc ||
220 old_local_mnc != state->local_mnc)
221 LOGP(DGPRS, LOGL_NOTICE,
222 "Patching RAID %sactivated, msg: %s, "
223 "local: %d-%d, core: %d-%d\n",
224 state->local_mcc || state->local_mnc ?
225 "" : "de",
226 log_text,
227 state->local_mcc, state->local_mnc,
228 peer->cfg->core_mcc, peer->cfg->core_mnc);
229}
230
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200231uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer,
232 uint32_t sgsn_ptmsi)
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200233{
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200234 uint32_t bss_ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200235 int max_retries = 23;
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200236 if (!peer->cfg->patch_ptmsi) {
237 bss_ptmsi = sgsn_ptmsi;
238 } else {
239 do {
Daniel Willmann537d4802015-10-12 19:36:35 +0200240 if (RAND_bytes((uint8_t *) &bss_ptmsi, sizeof(bss_ptmsi)) != 1) {
241 bss_ptmsi = GSM_RESERVED_TMSI;
242 break;
243 }
244
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200245 bss_ptmsi = bss_ptmsi | 0xC0000000;
246
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200247 if (gbproxy_link_info_by_ptmsi(peer, bss_ptmsi))
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200248 bss_ptmsi = GSM_RESERVED_TMSI;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200249 } while (bss_ptmsi == GSM_RESERVED_TMSI && max_retries--);
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200250 }
251
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200252 if (bss_ptmsi == GSM_RESERVED_TMSI)
253 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a BSS P-TMSI\n");
254
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200255 return bss_ptmsi;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200256}
257
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200258uint32_t gbproxy_make_sgsn_tlli(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200259 struct gbproxy_link_info *link_info,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200260 uint32_t bss_tlli)
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200261{
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200262 uint32_t sgsn_tlli;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200263 int max_retries = 23;
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200264 if (!peer->cfg->patch_ptmsi) {
265 sgsn_tlli = bss_tlli;
Jacob Erlbeck41108682014-10-31 10:47:29 +0100266 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI &&
267 gprs_tlli_type(bss_tlli) == TLLI_FOREIGN) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200268 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200269 TLLI_FOREIGN);
Jacob Erlbeck41108682014-10-31 10:47:29 +0100270 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI &&
271 gprs_tlli_type(bss_tlli) == TLLI_LOCAL) {
272 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
273 TLLI_LOCAL);
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200274 } else {
275 do {
276 /* create random TLLI, 0b01111xxx... */
Daniel Willmann537d4802015-10-12 19:36:35 +0200277 if (RAND_bytes((uint8_t *) &sgsn_tlli, sizeof(sgsn_tlli)) != 1) {
278 sgsn_tlli = 0;
279 break;
280 }
281
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200282 sgsn_tlli = (sgsn_tlli & 0x7fffffff) | 0x78000000;
283
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200284 if (gbproxy_link_info_by_any_sgsn_tlli(peer, sgsn_tlli))
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200285 sgsn_tlli = 0;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200286 } while (!sgsn_tlli && max_retries--);
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200287 }
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200288
289 if (!sgsn_tlli)
290 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate an SGSN TLLI\n");
291
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200292 return sgsn_tlli;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200293}
294
Jacob Erlbeckd211d1d2014-09-22 13:30:46 +0200295void gbproxy_reset_link(struct gbproxy_link_info *link_info)
296{
297 gbproxy_reset_imsi_acquisition(link_info);
298}
299
Jacob Erlbeck31591142014-09-03 11:59:48 +0200300/* Returns != 0 iff IMSI acquisition was in progress */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200301static int gbproxy_restart_imsi_acquisition(struct gbproxy_link_info* link_info)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200302{
303 int in_progress = 0;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200304 if (!link_info)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200305 return 0;
306
Jacob Erlbeck65750282014-09-22 15:41:21 +0200307 if (link_info->imsi_acq_pending)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200308 in_progress = 1;
309
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200310 gbproxy_link_info_discard_messages(link_info);
311 link_info->imsi_acq_pending = 0;
Jacob Erlbeck31591142014-09-03 11:59:48 +0200312
313 return in_progress;
314}
315
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200316static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info)
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200317{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200318 gbproxy_restart_imsi_acquisition(link_info);
Jacob Erlbeck65750282014-09-22 15:41:21 +0200319 link_info->vu_gen_tx_bss = GBPROXY_INIT_VU_GEN_TX;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200320}
321
Daniel Willmannbeade312016-11-07 17:54:29 +0100322static int gbproxy_flush_stored_messages(struct gbproxy_peer *peer,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200323 struct msgb *msg,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200324 time_t now,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200325 struct gbproxy_link_info* link_info,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200326 struct gprs_gb_parse_context *parse_ctx)
327{
328 int rc;
329 struct msgb *stored_msg;
330 /* Got identity response with IMSI, assuming the request had
331 * been generated by the gbproxy */
332
333 LOGP(DLLC, LOGL_DEBUG,
334 "NSEI=%d(BSS) IMSI acquisition succeeded, "
335 "flushing stored messages\n",
336 msgb_nsei(msg));
337
338 /* Patch and flush stored messages towards the SGSN */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200339 while ((stored_msg = msgb_dequeue(&link_info->stored_msgs))) {
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200340 struct gprs_gb_parse_context tmp_parse_ctx = {0};
341 tmp_parse_ctx.to_bss = 0;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200342 tmp_parse_ctx.peer_nsei = msgb_nsei(stored_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200343 int len_change = 0;
344
345 gprs_gb_parse_bssgp(msgb_bssgph(stored_msg),
346 msgb_bssgp_len(stored_msg),
347 &tmp_parse_ctx);
348 gbproxy_patch_bssgp(msg, msgb_bssgph(stored_msg),
349 msgb_bssgp_len(stored_msg),
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200350 peer, link_info, &len_change,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200351 &tmp_parse_ctx);
352
Daniel Willmannbeade312016-11-07 17:54:29 +0100353 rc = gbproxy_update_link_state_after(peer, link_info, now,
354 &tmp_parse_ctx);
355 if (rc == 1) {
356 LOGP(DLLC, LOGL_NOTICE, "link_info deleted while flushing stored messages\n");
357 msgb_free(stored_msg);
358 return -1;
359 }
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200360
361 rc = gbprox_relay2sgsn(peer->cfg, stored_msg,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200362 msgb_bvci(msg), link_info->sgsn_nsei);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200363
364 if (rc < 0)
365 LOGP(DLLC, LOGL_ERROR,
366 "NSEI=%d(BSS) failed to send stored message "
367 "(%s)\n",
368 msgb_nsei(msg),
369 parse_ctx->llc_msg_name ?
370 parse_ctx->llc_msg_name : "BSSGP");
371 msgb_free(stored_msg);
372 }
Daniel Willmannbeade312016-11-07 17:54:29 +0100373
374 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200375}
376
Jacob Erlbeck65750282014-09-22 15:41:21 +0200377static int gbproxy_gsm48_to_peer(struct gbproxy_peer *peer,
378 struct gbproxy_link_info* link_info,
379 uint16_t bvci,
380 struct msgb *msg /* Takes msg ownership */)
381{
382 int rc;
383
384 /* Workaround to avoid N(U) collisions and to enable a restart
385 * of the IMSI acquisition procedure. This will work unless the
386 * SGSN has an initial V(UT) within [256-32, 256+n_retries]
387 * (see GSM 04.64, 8.4.2). */
388 gprs_push_llc_ui(msg, 0, GPRS_SAPI_GMM, link_info->vu_gen_tx_bss);
389 link_info->vu_gen_tx_bss = (link_info->vu_gen_tx_bss + 1) % 512;
390
391 gprs_push_bssgp_dl_unitdata(msg, link_info->tlli.current);
392 rc = gbprox_relay2peer(msg, peer, bvci);
393 msgb_free(msg);
394 return rc;
395}
396
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200397static void gbproxy_acquire_imsi(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200398 struct gbproxy_link_info* link_info,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200399 uint16_t bvci)
400{
401 struct msgb *idreq_msg;
402
403 /* Send IDENT REQ */
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100404 idreq_msg = gsm48_msgb_alloc_name("GSM 04.08 ACQ IMSI");
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200405 gprs_put_identity_req(idreq_msg, GSM_MI_TYPE_IMSI);
Jacob Erlbeck65750282014-09-22 15:41:21 +0200406 gbproxy_gsm48_to_peer(peer, link_info, bvci, idreq_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200407}
408
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200409static void gbproxy_tx_detach_acc(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200410 struct gbproxy_link_info* link_info,
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200411 uint16_t bvci)
412{
413 struct msgb *detacc_msg;
414
415 /* Send DETACH ACC */
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100416 detacc_msg = gsm48_msgb_alloc_name("GSM 04.08 DET ACC");
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200417 gprs_put_mo_detach_acc(detacc_msg);
Jacob Erlbeck65750282014-09-22 15:41:21 +0200418 gbproxy_gsm48_to_peer(peer, link_info, bvci, detacc_msg);
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200419}
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200420
421/* Return != 0 iff msg still needs to be processed */
422static int gbproxy_imsi_acquisition(struct gbproxy_peer *peer,
423 struct msgb *msg,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200424 time_t now,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200425 struct gbproxy_link_info* link_info,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200426 struct gprs_gb_parse_context *parse_ctx)
427{
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200428 struct msgb *stored_msg;
429
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200430 if (!link_info)
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200431 return 1;
432
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200433 if (!link_info->imsi_acq_pending && link_info->imsi_len > 0)
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200434 return 1;
435
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200436 if (parse_ctx->g48_hdr)
437 switch (parse_ctx->g48_hdr->msg_type)
438 {
Jacob Erlbecke27ab912014-09-18 10:08:27 +0200439 case GSM48_MT_GMM_RA_UPD_REQ:
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200440 case GSM48_MT_GMM_ATTACH_REQ:
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200441 if (gbproxy_restart_imsi_acquisition(link_info)) {
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200442 LOGP(DLLC, LOGL_INFO,
443 "NSEI=%d(BSS) IMSI acquisition was in progress "
Jacob Erlbecke27ab912014-09-18 10:08:27 +0200444 "when receiving an %s.\n",
445 msgb_nsei(msg), parse_ctx->llc_msg_name);
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200446 }
447 break;
448 case GSM48_MT_GMM_DETACH_REQ:
449 /* Nothing has been sent to the SGSN yet */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200450 if (link_info->imsi_acq_pending) {
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200451 LOGP(DLLC, LOGL_INFO,
452 "NSEI=%d(BSS) IMSI acquisition was in progress "
453 "when receiving a DETACH_REQ.\n",
454 msgb_nsei(msg));
455 }
456 if (!parse_ctx->invalidate_tlli) {
457 LOGP(DLLC, LOGL_INFO,
458 "NSEI=%d(BSS) IMSI not yet acquired, "
459 "faking a DETACH_ACC.\n",
460 msgb_nsei(msg));
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200461 gbproxy_tx_detach_acc(peer, link_info, msgb_bvci(msg));
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200462 parse_ctx->invalidate_tlli = 1;
463 }
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200464 gbproxy_reset_imsi_acquisition(link_info);
465 gbproxy_update_link_state_after(peer, link_info, now,
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200466 parse_ctx);
467 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200468 }
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200469
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200470 if (link_info->imsi_acq_pending && link_info->imsi_len > 0) {
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200471 int is_ident_resp =
472 parse_ctx->g48_hdr &&
Neels Hofmeyr531734a2016-03-14 16:13:24 +0100473 gsm48_hdr_pdisc(parse_ctx->g48_hdr) == GSM48_PDISC_MM_GPRS &&
474 gsm48_hdr_msg_type(parse_ctx->g48_hdr) == GSM48_MT_GMM_ID_RESP;
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200475
Daniel Willmannbeade312016-11-07 17:54:29 +0100476 /* The IMSI is now available. If flushing the messages fails,
477 * then link_info has been deleted and we should return
478 * immediately. */
479 if (gbproxy_flush_stored_messages(peer, msg, now, link_info,
480 parse_ctx) < 0)
481 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200482
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200483 gbproxy_reset_imsi_acquisition(link_info);
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200484
485 /* This message is most probably the response to the ident
486 * request sent by gbproxy_acquire_imsi(). Don't forward it to
487 * the SGSN. */
488 return !is_ident_resp;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200489 }
490
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200491 /* The message cannot be processed since the IMSI is still missing */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200492
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200493 /* Enqueue unpatched messages */
494 LOGP(DLLC, LOGL_INFO,
495 "NSEI=%d(BSS) IMSI acquisition in progress, "
496 "storing message (%s)\n",
497 msgb_nsei(msg),
498 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200499
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200500 stored_msg = gprs_msgb_copy(msg, "process_bssgp_ul");
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200501 msgb_enqueue(&link_info->stored_msgs, stored_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200502
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200503 if (!link_info->imsi_acq_pending) {
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200504 LOGP(DLLC, LOGL_INFO,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200505 "NSEI=%d(BSS) IMSI is required but not available, "
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200506 "initiating identification procedure (%s)\n",
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200507 msgb_nsei(msg),
508 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
509
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200510 gbproxy_acquire_imsi(peer, link_info, msgb_bvci(msg));
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200511
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200512 /* There is no explicit retransmission handling, the
513 * implementation relies on the MS doing proper retransmissions
514 * of the triggering message instead */
515
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200516 link_info->imsi_acq_pending = 1;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200517 }
518
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200519 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200520}
521
522struct gbproxy_peer *gbproxy_find_peer(struct gbproxy_config *cfg,
523 struct msgb *msg,
524 struct gprs_gb_parse_context *parse_ctx)
525{
526 struct gbproxy_peer *peer = NULL;
527
528 if (msgb_bvci(msg) >= 2)
529 peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg));
530
531 if (!peer && !parse_ctx->to_bss)
532 peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg));
533
534 if (!peer)
535 peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx->bssgp_tp);
536
537 if (!peer) {
538 LOGP(DLLC, LOGL_INFO,
539 "NSEI=%d(%s) patching: didn't find peer for message, "
540 "PDU %d\n",
541 msgb_nsei(msg), parse_ctx->to_bss ? "BSS" : "SGSN",
542 parse_ctx->pdu_type);
543 /* Increment counter */
544 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]);
545 }
546 return peer;
547}
548
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200549/* patch BSSGP message */
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200550static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg,
551 struct msgb *msg,
552 struct gbproxy_peer *peer)
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200553{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200554 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200555 int rc;
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200556 int len_change = 0;
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200557 time_t now;
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200558 struct timespec ts = {0,};
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200559 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200560 uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200561
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200562 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
Jacob Erlbeck1abfdc22014-09-04 11:42:08 +0200563 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200564 return 1;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200565
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200566 parse_ctx.to_bss = 0;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200567 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200568
569 /* Parse BSSGP/LLC */
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200570 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
571 &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200572
Jacob Erlbeckb4f0e802014-10-09 12:28:15 +0200573 if (!rc && !parse_ctx.need_decryption) {
574 LOGP(DGPRS, LOGL_ERROR,
575 "NSEI=%u(BSS) patching: failed to parse invalid %s message\n",
576 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
577 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
578 LOGP(DGPRS, LOGL_NOTICE,
579 "NSEI=%u(BSS) invalid message was: %s\n",
580 msgb_nsei(msg), msgb_hexdump(msg));
581 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200582 }
583
584 /* Get peer */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200585 if (!peer)
586 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200587
588 if (!peer)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200589 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200590
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200591
592 clock_gettime(CLOCK_MONOTONIC, &ts);
593 now = ts.tv_sec;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200594
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200595 gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer,
596 parse_ctx.llc_msg_name);
597
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200598 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
Jacob Erlbeck31591142014-09-03 11:59:48 +0200599
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200600 link_info = gbproxy_update_link_state_ul(peer, now, &parse_ctx);
Jacob Erlbeck31591142014-09-03 11:59:48 +0200601
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200602 if (parse_ctx.g48_hdr) {
603 switch (parse_ctx.g48_hdr->msg_type) {
604 case GSM48_MT_GMM_ATTACH_REQ:
605 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]);
606 break;
Holger Hans Peter Freyther98fa3dc2015-11-09 14:30:22 +0100607 case GSM48_MT_GMM_DETACH_REQ:
608 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DETACH_REQS]);
609 break;
610 case GSM48_MT_GMM_ATTACH_COMPL:
611 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_COMPLS]);
612 break;
613 case GSM48_MT_GMM_RA_UPD_REQ:
614 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_REQS]);
615 break;
616 case GSM48_MT_GMM_RA_UPD_COMPL:
617 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_COMPLS]);
618 break;
619 case GSM48_MT_GMM_STATUS:
620 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_GMM_STATUS_BSS]);
621 break;
622 case GSM48_MT_GSM_ACT_PDP_REQ:
623 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_REQS]);
624 break;
625 case GSM48_MT_GSM_DEACT_PDP_REQ:
626 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_DEACT_REQS]);
627 break;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200628
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200629 default:
630 break;
631 }
632 }
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200633
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200634 if (link_info && cfg->route_to_sgsn2) {
635 if (cfg->acquire_imsi && link_info->imsi_len == 0)
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200636 sgsn_nsei = 0xffff;
Jacob Erlbeckb36032c2014-09-25 13:21:48 +0200637 else if (gbproxy_imsi_matches(cfg, GBPROX_MATCH_ROUTING,
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200638 link_info))
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200639 sgsn_nsei = cfg->nsip_sgsn2_nsei;
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200640 }
641
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200642 if (link_info)
643 link_info->sgsn_nsei = sgsn_nsei;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200644
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200645 /* Handle IMSI acquisition */
646 if (cfg->acquire_imsi) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200647 rc = gbproxy_imsi_acquisition(peer, msg, now, link_info,
Jacob Erlbeck9a6b7632014-09-19 13:23:21 +0200648 &parse_ctx);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200649 if (rc <= 0)
650 return rc;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200651 }
652
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200653 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200654 peer, link_info, &len_change, &parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200655
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200656 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200657
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200658 if (sgsn_nsei != cfg->nsip_sgsn_nsei) {
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200659 /* Send message directly to the selected SGSN */
660 rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei);
661 /* Don't let the calling code handle the transmission */
662 return 0;
663 }
664
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200665 return 1;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200666}
667
668/* patch BSSGP message to use core_mcc/mnc on the SGSN side */
669static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg,
670 struct msgb *msg,
671 struct gbproxy_peer *peer)
672{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200673 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200674 int rc;
675 int len_change = 0;
676 time_t now;
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200677 struct timespec ts = {0,};
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200678 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200679
Jacob Erlbeck1abfdc22014-09-04 11:42:08 +0200680 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
681 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
682 return;
683
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200684 parse_ctx.to_bss = 1;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200685 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200686
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200687 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
688 &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200689
Jacob Erlbeckb4f0e802014-10-09 12:28:15 +0200690 if (!rc && !parse_ctx.need_decryption) {
691 LOGP(DGPRS, LOGL_ERROR,
692 "NSEI=%u(SGSN) patching: failed to parse invalid %s message\n",
693 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
694 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
695 LOGP(DGPRS, LOGL_NOTICE,
696 "NSEI=%u(SGSN) invalid message was: %s\n",
697 msgb_nsei(msg), msgb_hexdump(msg));
698 return;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200699 }
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200700
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200701 /* Get peer */
702 if (!peer)
703 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200704
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200705 if (!peer)
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200706 return;
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200707
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200708 clock_gettime(CLOCK_MONOTONIC, &ts);
709 now = ts.tv_sec;
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200710
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200711 if (parse_ctx.g48_hdr) {
712 switch (parse_ctx.g48_hdr->msg_type) {
Holger Hans Peter Freyther98fa3dc2015-11-09 14:30:22 +0100713 case GSM48_MT_GMM_ATTACH_ACK:
714 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_ACKS]);
715 break;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200716 case GSM48_MT_GMM_ATTACH_REJ:
717 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]);
718 break;
Holger Hans Peter Freyther98fa3dc2015-11-09 14:30:22 +0100719 case GSM48_MT_GMM_DETACH_ACK:
720 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DETACH_ACKS]);
721 break;
722 case GSM48_MT_GMM_RA_UPD_ACK:
723 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_ACKS]);
724 break;
725 case GSM48_MT_GMM_RA_UPD_REJ:
726 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_REJS]);
727 break;
728 case GSM48_MT_GMM_STATUS:
729 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_GMM_STATUS_SGSN]);
730 break;
731 case GSM48_MT_GSM_ACT_PDP_ACK:
732 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_ACKS]);
733 break;
734 case GSM48_MT_GSM_ACT_PDP_REJ:
735 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_REJS]);
736 break;
737 case GSM48_MT_GSM_DEACT_PDP_ACK:
738 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_DEACT_ACKS]);
739 break;
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200740
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200741 default:
742 break;
743 }
744 }
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200745
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200746 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200747
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200748 link_info = gbproxy_update_link_state_dl(peer, now, &parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200749
750 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200751 peer, link_info, &len_change, &parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200752
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200753 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200754
Jacob Erlbeck03551e52014-06-27 19:56:05 +0200755 return;
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200756}
757
Harald Welte9f75c352010-04-30 20:26:32 +0200758/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200759static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200760 uint16_t ns_bvci, uint16_t sgsn_nsei)
Harald Welte672f5c42010-05-03 18:54:58 +0200761{
Harald Welte39d0bb52010-05-12 18:10:25 +0000762 /* create a copy of the message so the old one can
763 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200764 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2sgsn");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200765 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000766
Harald Weltee9ea2692010-05-11 20:20:13 +0200767 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200768 msgb_nsei(msg), ns_bvci, sgsn_nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200769
Harald Welte672f5c42010-05-03 18:54:58 +0200770 msgb_bvci(msg) = ns_bvci;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200771 msgb_nsei(msg) = sgsn_nsei;
Harald Welte672f5c42010-05-03 18:54:58 +0200772
Harald Welte69619e32010-05-03 19:05:10 +0200773 strip_ns_hdr(msg);
774
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200775 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
776 if (rc < 0)
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200777 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200778
779 return rc;
Harald Welte672f5c42010-05-03 18:54:58 +0200780}
781
Harald Welte672f5c42010-05-03 18:54:58 +0200782/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200783static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
Harald Welte9f75c352010-04-30 20:26:32 +0200784 uint16_t ns_bvci)
785{
Harald Welte39d0bb52010-05-12 18:10:25 +0000786 /* create a copy of the message so the old one can
787 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200788 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2peer");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200789 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000790
Harald Welte0ab535b2010-05-13 10:34:56 +0200791 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200792 msgb_nsei(msg), ns_bvci, peer->nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200793
Harald Welte9f75c352010-04-30 20:26:32 +0200794 msgb_bvci(msg) = ns_bvci;
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200795 msgb_nsei(msg) = peer->nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200796
Harald Welte0ab535b2010-05-13 10:34:56 +0200797 /* Strip the old NS header, it will be replaced with a new one */
Harald Welte69619e32010-05-03 19:05:10 +0200798 strip_ns_hdr(msg);
799
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200800 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
801 if (rc < 0)
802 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
803
804 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200805}
806
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200807static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type)
Harald Welte36f98d92011-02-06 13:09:29 +0100808{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200809 struct gbproxy_peer *peer;
Harald Welte36f98d92011-02-06 13:09:29 +0100810
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200811 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +0100812 if (!peer) {
813 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
814 ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200815 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +0100816 return -ENOENT;
817 }
818
819 switch (pdu_type) {
820 case BSSGP_PDUT_BVC_BLOCK_ACK:
821 peer->blocked = 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200822 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100823 break;
824 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
825 peer->blocked = 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200826 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100827 break;
828 default:
829 break;
830 }
831 return 0;
832}
833
Harald Welte9f75c352010-04-30 20:26:32 +0200834/* Send a message to a peer identified by ptp_bvci but using ns_bvci
835 * in the NS hdr */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200836static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci,
Harald Welte9f75c352010-04-30 20:26:32 +0200837 uint16_t ns_bvci)
838{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200839 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +0200840
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200841 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200842 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200843 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
Harald Welte1c77c6e2010-05-03 21:37:11 +0200844 ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200845 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte9f75c352010-04-30 20:26:32 +0200846 return -ENOENT;
Harald Welte1c77c6e2010-05-03 21:37:11 +0200847 }
Harald Welte9f75c352010-04-30 20:26:32 +0200848
Harald Welte69619e32010-05-03 19:05:10 +0200849 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200850}
851
Harald Welteb1fd9022012-06-17 12:16:31 +0800852int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
853{
854 return 0;
855}
856
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200857/* Receive an incoming PTP message from a BSS-side NS-VC */
858static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg,
859 struct msgb *msg, uint16_t nsei,
860 uint16_t nsvci, uint16_t ns_bvci)
861{
862 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200863 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
864 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200865 int rc;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200866
867 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
Jacob Erlbeck058ae122014-10-10 09:07:04 +0200868 if (!peer) {
869 LOGP(DGPRS, LOGL_NOTICE, "Didn't find peer for "
870 "BVCI=%u for PTP message from NSVC=%u/NSEI=%u (BSS), "
871 "discarding message\n",
872 ns_bvci, nsvci, nsei);
873 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
874 &ns_bvci, msg);
875 }
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200876
Jacob Erlbeck43b8f9f2014-10-02 14:56:44 +0200877 check_peer_nsei(peer, nsei);
Jacob Erlbeck43b8f9f2014-10-02 14:56:44 +0200878
Jacob Erlbeck058ae122014-10-10 09:07:04 +0200879 rc = gbprox_process_bssgp_ul(cfg, msg, peer);
880 if (!rc)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200881 return 0;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200882
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200883 switch (pdu_type) {
884 case BSSGP_PDUT_FLOW_CONTROL_BVC:
885 if (!cfg->route_to_sgsn2)
886 break;
887
888 /* Send a copy to the secondary SGSN */
889 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
890 break;
891 default:
892 break;
893 }
894
895
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200896 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200897}
898
899/* Receive an incoming PTP message from a SGSN-side NS-VC */
900static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg,
901 struct msgb *msg, uint16_t nsei,
902 uint16_t nsvci, uint16_t ns_bvci)
903{
904 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200905 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
906 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200907
908 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
909
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200910 /* Send status messages before patching */
911
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200912 if (!peer) {
913 LOGP(DGPRS, LOGL_INFO, "Didn't find peer for "
914 "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n",
915 ns_bvci, nsvci, nsei);
916 rate_ctr_inc(&cfg->ctrg->
917 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
918 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
919 &ns_bvci, msg);
920 }
921
922 if (peer->blocked) {
923 LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for "
924 "blocked BVCI=%u via NSVC=%u/NSEI=%u\n",
925 ns_bvci, nsvci, nsei);
926 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200927 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200928 }
929
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200930 switch (pdu_type) {
931 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
932 case BSSGP_PDUT_BVC_BLOCK_ACK:
933 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
934 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
935 /* Hide ACKs from the secondary SGSN, the primary SGSN
936 * is responsible to send them. */
937 return 0;
938 break;
939 default:
940 break;
941 }
942
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200943 /* Optionally patch the message */
944 gbprox_process_bssgp_dl(cfg, msg, peer);
945
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200946 return gbprox_relay2peer(msg, peer, ns_bvci);
947}
948
Harald Welte9f75c352010-04-30 20:26:32 +0200949/* Receive an incoming signalling message from a BSS-side NS-VC */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200950static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg,
951 struct msgb *msg, uint16_t nsei,
Harald Welte9f75c352010-04-30 20:26:32 +0200952 uint16_t ns_bvci)
953{
Harald Welteca3620a2010-05-03 16:30:59 +0200954 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200955 struct tlv_parsed tp;
956 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200957 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200958 struct gbproxy_peer *from_peer = NULL;
Harald Welte70f38d22010-05-01 12:10:57 +0200959 struct gprs_ra_id raid;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200960 int copy_to_sgsn2 = 0;
Jacob Erlbeck59300642014-09-08 09:04:01 +0200961 int rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200962
Harald Weltec471d3d2011-02-06 17:13:12 +0100963 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200964 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200965 nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200966 return -EINVAL;
967 }
968
969 /* we actually should never see those two for BVCI == 0, but double-check
970 * just to make sure */
971 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
972 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200973 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200974 "signalling\n", nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200975 return -EINVAL;
976 }
977
978 bssgp_tlv_parse(&tp, bgph->data, data_len);
979
980 switch (pdu_type) {
981 case BSSGP_PDUT_SUSPEND:
982 case BSSGP_PDUT_RESUME:
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200983 /* We implement RAI snooping during SUSPEND/RESUME, since it
984 * establishes a relationsip between BVCI/peer and the routeing
985 * area identification. The snooped information is then used
986 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
987 * BSSGP */
Harald Welte9f75c352010-04-30 20:26:32 +0200988 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
989 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200990 from_peer = gbproxy_peer_by_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200991 if (!from_peer)
992 goto err_no_peer;
Harald Welte1174c082010-05-12 00:07:29 +0200993 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
994 sizeof(from_peer->ra));
Harald Welte7fc98222010-05-11 10:15:42 +0200995 gsm48_parse_ra(&raid, from_peer->ra);
Harald Welte4cf12e92010-05-13 14:14:56 +0200996 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200997 "RAI snooping: RAI %u-%u-%u-%u behind BVCI=%u\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200998 nsei, raid.mcc, raid.mnc, raid.lac,
999 raid.rac , from_peer->bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001000 /* FIXME: This only supports one BSS per RA */
1001 break;
Harald Welte44c48302010-05-03 19:22:32 +02001002 case BSSGP_PDUT_BVC_RESET:
1003 /* If we receive a BVC reset on the signalling endpoint, we
1004 * don't want the SGSN to reset, as the signalling endpoint
1005 * is common for all point-to-point BVCs (and thus all BTS) */
1006 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001007 uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte72953b82010-05-12 00:20:41 +02001008 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001009 nsei, bvci);
Harald Welte44c48302010-05-03 19:22:32 +02001010 if (bvci == 0) {
1011 /* FIXME: only do this if SGSN is alive! */
Harald Weltee9ea2692010-05-11 20:20:13 +02001012 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001013 "BVC RESET ACK of BVCI=0\n", nsei);
Harald Welte44c48302010-05-03 19:22:32 +02001014 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001015 nsei, 0, ns_bvci);
Harald Welte1174c082010-05-12 00:07:29 +02001016 }
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001017 from_peer = gbproxy_peer_by_bvci(cfg, bvci);
Harald Welte1174c082010-05-12 00:07:29 +02001018 if (!from_peer) {
Harald Welte1c77c6e2010-05-03 21:37:11 +02001019 /* if a PTP-BVC is reset, and we don't know that
1020 * PTP-BVCI yet, we should allocate a new peer */
Max79605f62017-09-18 16:10:18 +02001021 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for BVCI=%u via NSEI=%u\n", bvci, nsei);
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +02001022 from_peer = gbproxy_peer_alloc(cfg, bvci);
Max79605f62017-09-18 16:10:18 +02001023 OSMO_ASSERT(from_peer);
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001024 from_peer->nsei = nsei;
Harald Welte44c48302010-05-03 19:22:32 +02001025 }
Jacob Erlbeck64cb9242013-10-24 12:48:56 +02001026
1027 if (!check_peer_nsei(from_peer, nsei))
1028 from_peer->nsei = nsei;
1029
Harald Welte1174c082010-05-12 00:07:29 +02001030 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
1031 struct gprs_ra_id raid;
1032 /* We have a Cell Identifier present in this
1033 * PDU, this means we can extend our local
1034 * state information about this particular cell
1035 * */
1036 memcpy(from_peer->ra,
1037 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
1038 sizeof(from_peer->ra));
1039 gsm48_parse_ra(&raid, from_peer->ra);
1040 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001041 "Cell ID %u-%u-%u-%u\n", nsei,
Harald Welte1174c082010-05-12 00:07:29 +02001042 bvci, raid.mcc, raid.mnc, raid.lac,
1043 raid.rac);
1044 }
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +02001045 if (cfg->route_to_sgsn2)
1046 copy_to_sgsn2 = 1;
Harald Welte44c48302010-05-03 19:22:32 +02001047 }
1048 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001049 }
1050
Harald Weltee9ea2692010-05-11 20:20:13 +02001051 /* Normally, we can simply pass on all signalling messages from BSS to
1052 * SGSN */
Jacob Erlbeck59300642014-09-08 09:04:01 +02001053 rc = gbprox_process_bssgp_ul(cfg, msg, from_peer);
1054 if (!rc)
1055 return 0;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +02001056
1057 if (copy_to_sgsn2)
1058 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
1059
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001060 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001061err_no_peer:
Jacob Erlbeckc5085f92013-10-18 13:04:48 +02001062 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001063 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001064 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001065 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001066err_mand_ie:
Harald Welte0a4050c2010-05-11 10:01:17 +02001067 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001068 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001069 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001070 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001071}
1072
1073/* Receive paging request from SGSN, we need to relay to proper BSS */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001074static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001075 uint32_t nsei, uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001076{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001077 struct gbproxy_peer *peer = NULL;
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001078 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte9f75c352010-04-30 20:26:32 +02001079
Harald Welte4cf12e92010-05-13 14:14:56 +02001080 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001081 nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001082 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001083 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte4cf12e92010-05-13 14:14:56 +02001084 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n",
1085 bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001086 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte9f75c352010-04-30 20:26:32 +02001087 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001088 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001089 LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +08001090 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001091 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte9f75c352010-04-30 20:26:32 +02001092 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001093 peer = gbproxy_peer_by_lai(cfg, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001094 LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +08001095 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001096 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte9f75c352010-04-30 20:26:32 +02001097 } else
Harald Welte4cf12e92010-05-13 14:14:56 +02001098 LOGPC(DGPRS, LOGL_INFO, "\n");
1099
1100 if (!peer) {
1101 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001102 "unable to route, missing IE\n", nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001103 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
Harald Welte9f75c352010-04-30 20:26:32 +02001104 return -EINVAL;
Harald Welte4cf12e92010-05-13 14:14:56 +02001105 }
1106 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001107}
1108
Harald Welte0a4050c2010-05-11 10:01:17 +02001109/* Receive an incoming BVC-RESET message from the SGSN */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001110static int rx_reset_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001111 struct msgb *orig_msg,
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001112 struct msgb *msg, struct tlv_parsed *tp,
1113 uint32_t nsei, uint16_t ns_bvci)
Harald Welte0a4050c2010-05-11 10:01:17 +02001114{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001115 struct gbproxy_peer *peer;
Harald Welte0a4050c2010-05-11 10:01:17 +02001116 uint16_t ptp_bvci;
1117
1118 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001119 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001120 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001121 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001122 NULL, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +02001123 }
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001124 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte0a4050c2010-05-11 10:01:17 +02001125
1126 if (ptp_bvci >= 2) {
1127 /* A reset for a PTP BVC was received, forward it to its
1128 * respective peer */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001129 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001130 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +02001131 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001132 nsei, ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001133 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001134 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001135 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001136 &ptp_bvci, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +02001137 }
1138 return gbprox_relay2peer(msg, peer, ns_bvci);
1139 }
1140
1141 /* A reset for the Signalling entity has been received
1142 * from the SGSN. As the signalling BVCI is shared
1143 * among all the BSS's that we multiplex, it needs to
1144 * be relayed */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001145 llist_for_each_entry(peer, &cfg->bts_peers, list)
Harald Welte0a4050c2010-05-11 10:01:17 +02001146 gbprox_relay2peer(msg, peer, ns_bvci);
1147
1148 return 0;
1149}
1150
Harald Welte9f75c352010-04-30 20:26:32 +02001151/* Receive an incoming signalling message from the SGSN-side NS-VC */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001152static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001153 struct msgb *orig_msg, uint32_t nsei,
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001154 uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001155{
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001156 struct bssgp_normal_hdr *bgph =
1157 (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001158 struct tlv_parsed tp;
1159 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001160 int data_len;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001161 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +02001162 uint16_t bvci;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001163 struct msgb *msg;
Harald Welte9f75c352010-04-30 20:26:32 +02001164 int rc = 0;
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001165 int cause;
Harald Welte9f75c352010-04-30 20:26:32 +02001166
Harald Weltec471d3d2011-02-06 17:13:12 +01001167 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +02001168 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001169 "signalling\n", nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001170 /* FIXME: Send proper error message */
Harald Welte9f75c352010-04-30 20:26:32 +02001171 return -EINVAL;
1172 }
1173
1174 /* we actually should never see those two for BVCI == 0, but double-check
1175 * just to make sure */
1176 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1177 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +02001178 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001179 "signalling\n", nsei);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001180 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001181 }
1182
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001183 msg = gprs_msgb_copy(orig_msg, "rx_sig_from_sgsn");
1184 gbprox_process_bssgp_dl(cfg, msg, NULL);
1185 /* Update message info */
1186 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1187 data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +02001188 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1189
1190 switch (pdu_type) {
Harald Welte0a4050c2010-05-11 10:01:17 +02001191 case BSSGP_PDUT_BVC_RESET:
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001192 rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001193 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001194 case BSSGP_PDUT_BVC_RESET_ACK:
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +02001195 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
1196 break;
1197 /* fall through */
1198 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9f75c352010-04-30 20:26:32 +02001199 /* simple case: BVCI IE is mandatory */
1200 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1201 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001202 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001203 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001204 break;
1205 case BSSGP_PDUT_PAGING_PS:
1206 case BSSGP_PDUT_PAGING_CS:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001207 /* process the paging request (LAI/RAI lookup) */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001208 rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001209 break;
1210 case BSSGP_PDUT_STATUS:
Harald Welte0a4050c2010-05-11 10:01:17 +02001211 /* Some exception has occurred */
Harald Welte44c48302010-05-03 19:22:32 +02001212 LOGP(DGPRS, LOGL_NOTICE,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001213 "NSEI=%u(SGSN) BSSGP STATUS ", nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +02001214 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
1215 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1216 goto err_mand_ie;
1217 }
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001218 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
Harald Welte0a4050c2010-05-11 10:01:17 +02001219 LOGPC(DGPRS, LOGL_NOTICE,
1220 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001221 bssgp_cause_str(cause));
Harald Welte0a4050c2010-05-11 10:01:17 +02001222 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001223 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1224 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%u\n", bvci);
1225
1226 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI)
1227 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001228 } else
1229 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welte9f75c352010-04-30 20:26:32 +02001230 break;
1231 /* those only exist in the SGSN -> BSS direction */
1232 case BSSGP_PDUT_SUSPEND_ACK:
1233 case BSSGP_PDUT_SUSPEND_NACK:
1234 case BSSGP_PDUT_RESUME_ACK:
1235 case BSSGP_PDUT_RESUME_NACK:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001236 /* RAI IE is mandatory */
Harald Welte9f75c352010-04-30 20:26:32 +02001237 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1238 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001239 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte9f75c352010-04-30 20:26:32 +02001240 if (!peer)
1241 goto err_no_peer;
Harald Welte69619e32010-05-03 19:05:10 +02001242 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001243 break;
Harald Welte36f98d92011-02-06 13:09:29 +01001244 case BSSGP_PDUT_BVC_BLOCK_ACK:
1245 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1246 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1247 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001248 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte36f98d92011-02-06 13:09:29 +01001249 if (bvci == 0) {
1250 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001251 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
Harald Welte36f98d92011-02-06 13:09:29 +01001252 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
1253 /* should we send STATUS ? */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001254 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001255 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +01001256 } else {
1257 /* Mark BVC as (un)blocked */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001258 block_unblock_peer(cfg, bvci, pdu_type);
Harald Welte36f98d92011-02-06 13:09:29 +01001259 }
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001260 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +01001261 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001262 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte44c48302010-05-03 19:22:32 +02001263 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001264 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001265 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001266 ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001267 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001268 break;
1269 default:
Max537da6c2017-08-17 19:33:11 +02001270 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type %s not supported\n", bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001271 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001272 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001273 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001274 break;
1275 }
1276
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001277 msgb_free(msg);
1278
Harald Welte9f75c352010-04-30 20:26:32 +02001279 return rc;
1280err_mand_ie:
Harald Welte1c77c6e2010-05-03 21:37:11 +02001281 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001282 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001283 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001284 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001285 msgb_free(msg);
1286 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001287err_no_peer:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001288 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001289 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001290 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001291 msgb_free(msg);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001292 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001293}
1294
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001295static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei)
1296{
1297 return nsei == cfg->nsip_sgsn_nsei ||
1298 (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei);
1299}
1300
Harald Welte9f75c352010-04-30 20:26:32 +02001301/* Main input function for Gb proxy */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001302int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei,
1303 uint16_t ns_bvci, uint16_t nsvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001304{
Harald Welte672f5c42010-05-03 18:54:58 +02001305 int rc;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001306 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001307
1308 /* Only BVCI=0 messages need special treatment */
1309 if (ns_bvci == 0 || ns_bvci == 1) {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001310 if (remote_end_is_sgsn)
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001311 rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001312 else
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001313 rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci);
Harald Welte672f5c42010-05-03 18:54:58 +02001314 } else {
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +02001315 /* All other BVCI are PTP */
1316 if (remote_end_is_sgsn)
1317 rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei, nsvci,
1318 ns_bvci);
1319 else
1320 rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei, nsvci,
1321 ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001322 }
1323
Harald Welte672f5c42010-05-03 18:54:58 +02001324 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +02001325}
Harald Welte85801d02010-05-11 05:49:43 +02001326
Harald Welte1ccbf442010-05-14 11:53:08 +00001327int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi)
1328{
1329 struct gprs_nsvc *nsvc;
1330
1331 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
1332 if (!nsvc->persistent)
1333 continue;
1334 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1335 }
1336 return 0;
1337}
1338
Harald Weltec1c1dd22010-05-11 06:34:24 +02001339/* Signal handler for signals from NS layer */
1340int gbprox_signal(unsigned int subsys, unsigned int signal,
1341 void *handler_data, void *signal_data)
1342{
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001343 struct gbproxy_config *cfg = handler_data;
Harald Weltec1c1dd22010-05-11 06:34:24 +02001344 struct ns_signal_data *nssd = signal_data;
1345 struct gprs_nsvc *nsvc = nssd->nsvc;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001346 struct gbproxy_peer *peer;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001347 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsvc->nsei);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001348
Harald Weltea6a20b42012-06-16 16:40:42 +08001349 if (subsys != SS_L_NS)
Harald Weltec1c1dd22010-05-11 06:34:24 +02001350 return 0;
1351
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001352 if (signal == S_NS_RESET && remote_end_is_sgsn) {
Harald Weltef69c0592010-05-11 18:29:44 +02001353 /* We have received a NS-RESET from the NSEI and NSVC
1354 * of the SGSN. This might happen with SGSN that start
1355 * their own NS-RESET procedure without waiting for our
1356 * NS-RESET */
1357 nsvc->remote_end_is_sgsn = 1;
1358 }
1359
Harald Welteb778d2c2010-05-12 13:28:25 +00001360 if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) {
1361 LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, "
1362 "re-starting RESET procedure\n");
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001363 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001364 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
Harald Weltee6599ee2012-06-17 12:25:53 +08001365 gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr,
1366 nsvc->nsei, nsvc->nsvci);
Harald Welteb778d2c2010-05-12 13:28:25 +00001367 }
1368
Harald Welte5e106d72011-02-06 16:33:29 +01001369 if (!nsvc->remote_end_is_sgsn) {
1370 /* from BSS to SGSN */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001371 peer = gbproxy_peer_by_nsei(cfg, nsvc->nsei);
Harald Welte5e106d72011-02-06 16:33:29 +01001372 if (!peer) {
Maxa441e9d2017-08-18 16:23:28 +02001373 LOGP(DGPRS, LOGL_NOTICE, "signal '%s' for unknown peer NSEI=%u/NSVCI=%u\n",
1374 get_value_string(gprs_ns_signal_ns_names, signal), nsvc->nsei, nsvc->nsvci);
Harald Welte5e106d72011-02-06 16:33:29 +01001375 return 0;
1376 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001377 switch (signal) {
1378 case S_NS_RESET:
Harald Weltec1c1dd22010-05-11 06:34:24 +02001379 case S_NS_BLOCK:
Harald Welte5e106d72011-02-06 16:33:29 +01001380 if (!peer->blocked)
1381 break;
Maxa441e9d2017-08-18 16:23:28 +02001382 LOGP(DGPRS, LOGL_NOTICE, "Converting '%s' from NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n",
1383 get_value_string(gprs_ns_signal_ns_names, signal), nsvc->nsei, nsvc->nsvci);
Harald Welte5e106d72011-02-06 16:33:29 +01001384 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei,
1385 peer->bvci, 0);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001386 break;
Harald Welte5e106d72011-02-06 16:33:29 +01001387 }
1388 } else {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001389 /* Forward this message to all NS-VC to BSS */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001390 struct gprs_ns_inst *nsi = cfg->nsi;
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001391 struct gprs_nsvc *next_nsvc;
1392
1393 llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) {
1394 if (next_nsvc->remote_end_is_sgsn)
1395 continue;
1396
1397 /* Note that the following does not start the full
1398 * procedures including timer based retransmissions. */
Harald Welte5e106d72011-02-06 16:33:29 +01001399 switch (signal) {
1400 case S_NS_RESET:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001401 gprs_ns_tx_reset(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001402 break;
1403 case S_NS_BLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001404 gprs_ns_tx_block(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001405 break;
1406 case S_NS_UNBLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001407 gprs_ns_tx_unblock(next_nsvc);
Harald Welte5e106d72011-02-06 16:33:29 +01001408 break;
1409 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001410 }
1411 }
1412 return 0;
1413}
1414
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001415void gbprox_reset(struct gbproxy_config *cfg)
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001416{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001417 struct gbproxy_peer *peer, *tmp;
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001418
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001419 llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list)
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +02001420 gbproxy_peer_free(peer);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001421
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001422 rate_ctr_group_free(cfg->ctrg);
1423 gbproxy_init_config(cfg);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001424}
1425
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001426int gbproxy_init_config(struct gbproxy_config *cfg)
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001427{
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001428 struct timespec tp;
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +02001429
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001430 INIT_LLIST_HEAD(&cfg->bts_peers);
Holger Hans Peter Freythera7027a02014-08-04 11:19:56 +02001431 cfg->ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0);
Harald Welte26c14652017-07-12 00:25:51 +02001432 if (!cfg->ctrg) {
1433 LOGP(DGPRS, LOGL_ERROR, "Cannot allocate global counter group!\n");
1434 return -1;
1435 }
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001436 clock_gettime(CLOCK_REALTIME, &tp);
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +02001437
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001438 return 0;
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001439}