blob: fbac298e369515875ed40e32bb2d253d2a1040e5 [file] [log] [blame]
Harald Welte9f75c352010-04-30 20:26:32 +02001/* NS-over-IP proxy */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther90267a92013-10-23 11:24:17 +02004 * (C) 2010-2013 by On-Waves
5 * (C) 2013 by Holger Hans Peter Freyther
Harald Welte9f75c352010-04-30 20:26:32 +02006 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9f75c352010-04-30 20:26:32 +020011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Welte9f75c352010-04-30 20:26:32 +020017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9f75c352010-04-30 20:26:32 +020020 *
21 */
22
23#include <unistd.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <getopt.h>
28#include <errno.h>
29#include <sys/fcntl.h>
30#include <sys/stat.h>
Harald Welte7fc98222010-05-11 10:15:42 +020031#include <arpa/inet.h>
Jacob Erlbeck7c101d92014-06-06 18:49:23 +020032#include <time.h>
Harald Welte9f75c352010-04-30 20:26:32 +020033
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010034#include <osmocom/core/talloc.h>
35#include <osmocom/core/select.h>
Jacob Erlbeckbc555742013-10-18 14:34:55 +020036#include <osmocom/core/rate_ctr.h>
37
Harald Welteea34a4e2012-06-16 14:59:56 +080038#include <osmocom/gprs/gprs_ns.h>
39#include <osmocom/gprs/gprs_bssgp.h>
40
Jacob Erlbeck7c101d92014-06-06 18:49:23 +020041#include <osmocom/gsm/gsm_utils.h>
42
Harald Welte9f75c352010-04-30 20:26:32 +020043#include <openbsc/signal.h>
44#include <openbsc/debug.h>
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020045#include <openbsc/gprs_gb_parse.h>
Harald Welte672f5c42010-05-03 18:54:58 +020046#include <openbsc/gb_proxy.h>
Harald Welte9f75c352010-04-30 20:26:32 +020047
Jacob Erlbeck67a44452014-05-19 10:14:58 +020048#include <openbsc/gprs_llc.h>
49#include <openbsc/gsm_04_08.h>
50#include <openbsc/gsm_04_08_gprs.h>
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +020051#include <openbsc/gprs_utils.h>
Jacob Erlbeck67a44452014-05-19 10:14:58 +020052
Jacob Erlbeckbc555742013-10-18 14:34:55 +020053static const struct rate_ctr_desc global_ctr_description[] = {
54 { "inv-bvci", "Invalid BVC Identifier " },
Jacob Erlbeck8f503592014-06-02 10:49:00 +020055 { "inv-lai", "Invalid Location Area Identifier" },
56 { "inv-rai", "Invalid Routing Area Identifier " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020057 { "inv-nsei", "No BVC established for NSEI " },
58 { "proto-err.bss", "BSSGP protocol error (BSS )" },
59 { "proto-err.sgsn", "BSSGP protocol error (SGSN)" },
60 { "not-supp.bss", "Feature not supported (BSS )" },
61 { "not-supp.sgsn", "Feature not supported (SGSN)" },
62 { "restart.sgsn", "Restarted RESET procedure (SGSN)" },
63 { "tx-err.sgsn", "NS Transmission error (SGSN)" },
64 { "error", "Other error " },
Jacob Erlbeck006c0382014-05-27 13:49:04 +020065 { "mod-peer-err", "Patch error: no peer " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020066};
67
68static const struct rate_ctr_group_desc global_ctrg_desc = {
69 .group_name_prefix = "gbproxy.global",
70 .group_description = "GBProxy Global Statistics",
71 .num_ctr = ARRAY_SIZE(global_ctr_description),
72 .ctr_desc = global_ctr_description,
73};
74
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020075static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
76 uint16_t ns_bvci);
77static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +020078 uint16_t ns_bvci, uint16_t sgsn_nsei);
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020079
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +020080static int check_peer_nsei(struct gbproxy_peer *peer, uint16_t nsei)
Jacob Erlbeckbc555742013-10-18 14:34:55 +020081{
82 if (peer->nsei != nsei) {
83 LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI "
84 "BVCI=%u via NSEI=%u (expected NSEI=%u)\n",
85 peer->bvci, nsei, peer->nsei);
86 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]);
Jacob Erlbeck64cb9242013-10-24 12:48:56 +020087 return 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +020088 }
89
Jacob Erlbeck64cb9242013-10-24 12:48:56 +020090 return 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +020091}
92
Harald Welte69619e32010-05-03 19:05:10 +020093/* strip off the NS header */
94static void strip_ns_hdr(struct msgb *msg)
95{
96 int strip_len = msgb_bssgph(msg) - msg->data;
97 msgb_pull(msg, strip_len);
98}
99
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200100/* Transmit Chapter 9.2.10 Identity Request */
101static void gprs_put_identity_req(struct msgb *msg, uint8_t id_type)
102{
103 struct gsm48_hdr *gh;
104
105 id_type &= GSM_MI_TYPE_MASK;
106
107 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
108 gh->proto_discr = GSM48_PDISC_MM_GPRS;
109 gh->msg_type = GSM48_MT_GMM_ID_REQ;
110 gh->data[0] = id_type;
111}
112
113static void gprs_push_llc_ui(struct msgb *msg,
114 int is_uplink, unsigned sapi, unsigned nu)
115{
116 const uint8_t e_bit = 0;
117 const uint8_t pm_bit = 1;
118 const uint8_t cr_bit = is_uplink ? 0 : 1;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200119 uint8_t *llc;
120 uint8_t *fcs_field;
121 uint32_t fcs;
122
123 nu &= 0x01ff; /* 9 Bit */
124
125 llc = msgb_push(msg, 3);
126 llc[0] = (cr_bit << 6) | (sapi & 0x0f);
127 llc[1] = 0xc0 | (nu >> 6); /* UI frame */
128 llc[2] = (nu << 2) | ((e_bit & 1) << 1) | (pm_bit & 1);
129
130 fcs = gprs_llc_fcs(llc, msgb_length(msg));
131 fcs_field = msgb_put(msg, 3);
132 fcs_field[0] = (uint8_t)(fcs >> 0);
133 fcs_field[1] = (uint8_t)(fcs >> 8);
134 fcs_field[2] = (uint8_t)(fcs >> 16);
135}
136
137static void gprs_push_bssgp_dl_unitdata(struct msgb *msg,
138 uint32_t tlli)
139{
140 struct bssgp_ud_hdr *budh;
141 uint8_t *llc = msgb_data(msg);
142 size_t llc_size = msgb_length(msg);
143 const size_t llc_ie_hdr_size = 3;
144 const uint8_t qos_profile[] = {0x00, 0x50, 0x20}; /* hard-coded */
145 const uint8_t lifetime[] = {0x02, 0x58}; /* 6s hard-coded */
146
147 const size_t bssgp_overhead = sizeof(*budh) +
148 TVLV_GROSS_LEN(sizeof(lifetime)) + llc_ie_hdr_size;
149 uint8_t *ie;
150 uint32_t tlli_be = htonl(tlli);
151
152 budh = (struct bssgp_ud_hdr *)msgb_push(msg, bssgp_overhead);
153
154 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
155 memcpy(&budh->tlli, &tlli_be, sizeof(budh->tlli));
156 memcpy(&budh->qos_profile, qos_profile, sizeof(budh->qos_profile));
157
158 ie = budh->data;
159 tvlv_put(ie, BSSGP_IE_PDU_LIFETIME, sizeof(lifetime), lifetime);
160 ie += TVLV_GROSS_LEN(sizeof(lifetime));
161
162 /* Note: Add alignment before the LLC IE if inserting other IE */
163
164 *(ie++) = BSSGP_IE_LLC_PDU;
165 *(ie++) = llc_size / 256;
166 *(ie++) = llc_size % 256;
167
168 OSMO_ASSERT(ie == llc);
169
170 msgb_bssgph(msg) = (uint8_t *)budh;
171 msgb_tlli(msg) = tlli;
172}
173
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200174/* update peer according to the BSS message */
175static void gbprox_update_current_raid(uint8_t *raid_enc,
176 struct gbproxy_peer *peer,
177 const char *log_text)
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200178{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200179 struct gbproxy_patch_state *state = &peer->patch_state;
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200180 const int old_local_mcc = state->local_mcc;
181 const int old_local_mnc = state->local_mnc;
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200182 struct gprs_ra_id raid;
183
184 if (!raid_enc)
185 return;
186
187 gsm48_parse_ra(&raid, raid_enc);
188
189 /* save source side MCC/MNC */
190 if (!peer->cfg->core_mcc || raid.mcc == peer->cfg->core_mcc) {
191 state->local_mcc = 0;
192 } else {
193 state->local_mcc = raid.mcc;
194 }
195
196 if (!peer->cfg->core_mnc || raid.mnc == peer->cfg->core_mnc) {
197 state->local_mnc = 0;
198 } else {
199 state->local_mnc = raid.mnc;
200 }
201
202 if (old_local_mcc != state->local_mcc ||
203 old_local_mnc != state->local_mnc)
204 LOGP(DGPRS, LOGL_NOTICE,
205 "Patching RAID %sactivated, msg: %s, "
206 "local: %d-%d, core: %d-%d\n",
207 state->local_mcc || state->local_mnc ?
208 "" : "de",
209 log_text,
210 state->local_mcc, state->local_mnc,
211 peer->cfg->core_mcc, peer->cfg->core_mnc);
212}
213
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200214uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer,
215 uint32_t sgsn_ptmsi)
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200216{
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200217 uint32_t bss_ptmsi;
218 if (!peer->cfg->patch_ptmsi) {
219 bss_ptmsi = sgsn_ptmsi;
220 } else {
221 do {
222 bss_ptmsi = rand_r(&peer->cfg->bss_ptmsi_state);
223 bss_ptmsi = bss_ptmsi | 0xC0000000;
224
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200225 if (gbproxy_find_tlli_by_ptmsi(peer, bss_ptmsi))
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200226 bss_ptmsi = GSM_RESERVED_TMSI;
227 } while (bss_ptmsi == GSM_RESERVED_TMSI);
228 }
229
230 return bss_ptmsi;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200231}
232
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200233uint32_t gbproxy_make_sgsn_tlli(struct gbproxy_peer *peer,
234 struct gbproxy_tlli_info *tlli_info,
235 uint32_t bss_tlli)
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200236{
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200237 uint32_t sgsn_tlli;
238 if (!peer->cfg->patch_ptmsi) {
239 sgsn_tlli = bss_tlli;
240 } else if (tlli_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI) {
241 sgsn_tlli = gprs_tmsi2tlli(tlli_info->sgsn_tlli.ptmsi,
242 TLLI_FOREIGN);
243 } else {
244 do {
245 /* create random TLLI, 0b01111xxx... */
246 sgsn_tlli = rand_r(&peer->cfg->sgsn_tlli_state);
247 sgsn_tlli = (sgsn_tlli & 0x7fffffff) | 0x78000000;
248
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200249 if (gbproxy_find_tlli_by_sgsn_tlli(peer, sgsn_tlli))
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200250 sgsn_tlli = 0;
251 } while (!sgsn_tlli);
252 }
253 return sgsn_tlli;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200254}
255
Jacob Erlbeck31591142014-09-03 11:59:48 +0200256/* Returns != 0 iff IMSI acquisition was in progress */
257int gbproxy_reset_imsi_acquisition(struct gbproxy_tlli_info* tlli_info)
258{
259 int in_progress = 0;
260 if (!tlli_info)
261 return 0;
262
263 if (tlli_info->imsi_acq_pending)
264 in_progress = 1;
265
266 gbproxy_tlli_info_discard_messages(tlli_info);
267 tlli_info->imsi_acq_pending = 0;
268
269 return in_progress;
270}
271
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200272/* patch BSSGP message */
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200273static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg,
274 struct msgb *msg,
275 struct gbproxy_peer *peer)
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200276{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200277 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200278 int rc;
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200279 int len_change = 0;
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200280 time_t now;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200281 struct gbproxy_tlli_info *tlli_info = NULL;
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200282 uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei;
283 int send_msg_directly = 0;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200284
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200285 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
286 !cfg->acquire_imsi)
287 return 1;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200288
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200289 parse_ctx.to_bss = 0;
290
291 /* Parse BSSGP/LLC */
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200292 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
293 &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200294
295 if (!rc) {
296 if (!parse_ctx.need_decryption) {
297 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeck643d5222014-08-18 17:26:04 +0200298 "NSEI=%u(BSS) patching: "
299 "failed to parse BSSGP/GMM message\n",
300 msgb_nsei(msg));
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200301 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200302 }
303 }
304
305 /* Get peer */
306 if (!peer && msgb_bvci(msg) >= 2)
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200307 peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg));
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200308
309 if (!peer)
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200310 peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg));
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200311
312 if (!peer)
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200313 peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx.bssgp_tp);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200314
315 if (!peer) {
316 LOGP(DLLC, LOGL_INFO,
317 "NSEI=%d(BSS) patching: didn't find peer for message, "
318 "PDU %d\n",
319 msgb_nsei(msg), parse_ctx.pdu_type);
320 /* Increment counter */
321 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]);
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200322 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200323 }
324
325 now = time(NULL);
326
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200327 if (parse_ctx.bssgp_raid_enc && parse_ctx.old_raid_enc &&
328 memcmp(parse_ctx.bssgp_raid_enc, parse_ctx.old_raid_enc, 6) == 0)
329 parse_ctx.old_raid_matches = 1;
330
331 gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer,
332 parse_ctx.llc_msg_name);
333
Jacob Erlbeck31591142014-09-03 11:59:48 +0200334 gprs_gb_log_parse_context(&parse_ctx, "BSSGP");
335
336 tlli_info = gbproxy_update_tlli_state_ul(peer, now, &parse_ctx);
337
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200338 if (parse_ctx.g48_hdr) {
339 switch (parse_ctx.g48_hdr->msg_type) {
340 case GSM48_MT_GMM_ATTACH_REQ:
341 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]);
Jacob Erlbeck31591142014-09-03 11:59:48 +0200342 if (gbproxy_reset_imsi_acquisition(tlli_info)) {
343 LOGP(DLLC, LOGL_INFO,
344 "NSEI=%d(BSS) IMSI acquisition was in progress "
345 "when receiving an ATTACH_REQ.\n",
346 msgb_nsei(msg));
347 tlli_info->imsi_acq_retries += 1;
348 }
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200349 break;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200350
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200351 default:
352 break;
353 }
354 }
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200355
Jacob Erlbeck18a37872014-09-08 09:59:16 +0200356 if (tlli_info && cfg->route_to_sgsn2 && gbproxy_check_tlli(peer, tlli_info)) {
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200357 sgsn_nsei = cfg->nsip_sgsn2_nsei;
358 send_msg_directly = 1;
359 }
360
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200361 if (tlli_info && tlli_info->imsi_acq_pending && parse_ctx.g48_hdr &&
362 parse_ctx.g48_hdr->proto_discr == GSM48_PDISC_MM_GPRS &&
363 parse_ctx.g48_hdr->msg_type == GSM48_MT_GMM_ID_RESP &&
364 parse_ctx.imsi_len > 0) {
365 struct msgb *stored_msg;
366 /* Got identity response with IMSI, assuming the request had
367 * been generated by the gbproxy */
368
369 LOGP(DLLC, LOGL_DEBUG,
370 "NSEI=%d(BSS) IMSI acquisition succeeded, "
371 "flushing stored messages\n",
372 msgb_nsei(msg));
373
374 /* TODO: Patch and flush stored messages towards the SGSN */
375 while ((stored_msg = msgb_dequeue(&tlli_info->stored_msgs))) {
376 struct gprs_gb_parse_context tmp_parse_ctx = {0};
377 tmp_parse_ctx.to_bss = 0;
378 len_change = 0;
379
380 gprs_gb_parse_bssgp(msgb_bssgph(stored_msg),
381 msgb_bssgp_len(stored_msg),
382 &tmp_parse_ctx);
383 gbproxy_patch_bssgp(msg, msgb_bssgph(stored_msg),
384 msgb_bssgp_len(stored_msg),
385 peer, tlli_info, &len_change,
386 &tmp_parse_ctx);
387
388 gbproxy_update_tlli_state_after(peer, tlli_info, now,
389 &tmp_parse_ctx);
390
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200391 rc = gbprox_relay2sgsn(cfg, stored_msg, msgb_bvci(msg),
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200392 sgsn_nsei);
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200393
394 if (rc < 0)
395 LOGP(DLLC, LOGL_ERROR,
396 "NSEI=%d(BSS) failed to send stored message "
397 "(%s)\n",
398 msgb_nsei(msg),
399 parse_ctx.llc_msg_name ? parse_ctx.llc_msg_name : "BSSGP");
400 msgb_free(stored_msg);
401 }
402
403 tlli_info->imsi_acq_pending = 0;
404
405 return 0;
406 }
407
408 if (tlli_info && tlli_info->imsi_acq_pending) {
409 struct msgb *stored_msg;
410
411 LOGP(DLLC, LOGL_DEBUG,
412 "NSEI=%d(BSS) IMSI acquisition in progess, "
413 "storing message (%s)\n",
414 msgb_nsei(msg),
415 parse_ctx.llc_msg_name ? parse_ctx.llc_msg_name : "BSSGP");
416
417 /* Enqueue unpatched messages */
418 stored_msg = gprs_msgb_copy(msg, "process_bssgp_ul");
419 msgb_enqueue(&tlli_info->stored_msgs, stored_msg);
420
421 /* TODO: Timeout, retransmit IDENT REQ if expired */
422 return 0;
423 }
424
425 if (cfg->acquire_imsi && tlli_info && tlli_info->mi_data_len == 0) {
426 struct msgb *idreq_msg;
427 struct msgb *stored_msg = gprs_msgb_copy(msg, "process_bssgp_ul");
428
429 LOGP(DLLC, LOGL_DEBUG,
430 "NSEI=%d(BSS) IMSI is required but not available, "
431 "storing message (%s)\n",
432 msgb_nsei(msg),
433 parse_ctx.llc_msg_name ? parse_ctx.llc_msg_name : "BSSGP");
434
435 /* Enqueue message */
436 msgb_enqueue(&tlli_info->stored_msgs, stored_msg);
437
438 /* Send IDENT REQ */
439 idreq_msg = gsm48_msgb_alloc();
440 gprs_put_identity_req(idreq_msg, GSM_MI_TYPE_IMSI);
Jacob Erlbeck31591142014-09-03 11:59:48 +0200441 /* Workaround to avoid N(U) collisions and to enable a restart
442 * of the IMSI acquisition procedure. This will work unless the
443 * SGSN has an initial V(UT) within [256-32, 256+n_retries]
444 * (see GSM 04.64, 8.4.2).
445 * TODO: Check whether it is sensible to rely on this, it might
446 * be an issue when P-TMSI patching is _not_ enabled and the SGSN
447 * doesn't reset V(UT) after a detach. */
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200448 gprs_push_llc_ui(idreq_msg, 0, GPRS_SAPI_GMM,
Jacob Erlbeck31591142014-09-03 11:59:48 +0200449 (tlli_info->imsi_acq_retries + 256) % 512);
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200450 gprs_push_bssgp_dl_unitdata(idreq_msg, tlli_info->tlli.current);
451 gbprox_relay2peer(idreq_msg, peer, msgb_bvci(msg));
452 msgb_free(idreq_msg);
453
454 tlli_info->imsi_acq_pending = 1;
455
456 return 0;
457 }
458
459
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200460 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
461 peer, tlli_info, &len_change, &parse_ctx);
462
463 gbproxy_update_tlli_state_after(peer, tlli_info, now, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200464
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200465 if (send_msg_directly) {
466 /* Send message directly to the selected SGSN */
467 rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei);
468 /* Don't let the calling code handle the transmission */
469 return 0;
470 }
471
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200472 return 1;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200473}
474
475/* patch BSSGP message to use core_mcc/mnc on the SGSN side */
476static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg,
477 struct msgb *msg,
478 struct gbproxy_peer *peer)
479{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200480 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200481 int rc;
482 int len_change = 0;
483 time_t now;
484 struct gbproxy_tlli_info *tlli_info = NULL;
485
486 parse_ctx.to_bss = 1;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200487
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200488 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
489 &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200490
491 if (!rc) {
492 if (!parse_ctx.need_decryption) {
493 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeck643d5222014-08-18 17:26:04 +0200494 "NSEI=%u(SGSN) patching: "
495 "failed to parse BSSGP/GMM message\n",
496 msgb_nsei(msg));
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200497 return;
498 }
499 }
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200500
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200501 if (!peer && msgb_bvci(msg) >= 2)
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200502 peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg));
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200503
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200504 if (!peer)
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200505 peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx.bssgp_tp);
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200506
507 if (!peer) {
508 LOGP(DLLC, LOGL_INFO,
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200509 "NSEI=%d(SGSN) patching: didn't find peer for message, "
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200510 "PDU %d\n",
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200511 msgb_nsei(msg), parse_ctx.pdu_type);
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200512 /* Increment counter */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200513 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]);
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200514 return;
515 }
516
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200517 now = time(NULL);
518
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200519 if (parse_ctx.g48_hdr) {
520 switch (parse_ctx.g48_hdr->msg_type) {
521 case GSM48_MT_GMM_ATTACH_REJ:
522 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]);
523 break;
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200524
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200525 default:
526 break;
527 }
528 }
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200529
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200530 gprs_gb_log_parse_context(&parse_ctx, "BSSGP");
531
532 tlli_info = gbproxy_update_tlli_state_dl(peer, now, &parse_ctx);
533
534 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
535 peer, tlli_info, &len_change, &parse_ctx);
536
537 gbproxy_update_tlli_state_after(peer, tlli_info, now, &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200538
Jacob Erlbeck03551e52014-06-27 19:56:05 +0200539 return;
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200540}
541
Harald Welte9f75c352010-04-30 20:26:32 +0200542/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200543static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200544 uint16_t ns_bvci, uint16_t sgsn_nsei)
Harald Welte672f5c42010-05-03 18:54:58 +0200545{
Harald Welte39d0bb52010-05-12 18:10:25 +0000546 /* create a copy of the message so the old one can
547 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200548 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2sgsn");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200549 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000550
Harald Weltee9ea2692010-05-11 20:20:13 +0200551 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200552 msgb_nsei(msg), ns_bvci, sgsn_nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200553
Harald Welte672f5c42010-05-03 18:54:58 +0200554 msgb_bvci(msg) = ns_bvci;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200555 msgb_nsei(msg) = sgsn_nsei;
Harald Welte672f5c42010-05-03 18:54:58 +0200556
Harald Welte69619e32010-05-03 19:05:10 +0200557 strip_ns_hdr(msg);
558
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200559 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
560 if (rc < 0)
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200561 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200562
563 return rc;
Harald Welte672f5c42010-05-03 18:54:58 +0200564}
565
Harald Welte672f5c42010-05-03 18:54:58 +0200566/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200567static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
Harald Welte9f75c352010-04-30 20:26:32 +0200568 uint16_t ns_bvci)
569{
Harald Welte39d0bb52010-05-12 18:10:25 +0000570 /* create a copy of the message so the old one can
571 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200572 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2peer");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200573 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000574
Harald Welte0ab535b2010-05-13 10:34:56 +0200575 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200576 msgb_nsei(msg), ns_bvci, peer->nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200577
Harald Welte9f75c352010-04-30 20:26:32 +0200578 msgb_bvci(msg) = ns_bvci;
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200579 msgb_nsei(msg) = peer->nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200580
Harald Welte0ab535b2010-05-13 10:34:56 +0200581 /* Strip the old NS header, it will be replaced with a new one */
Harald Welte69619e32010-05-03 19:05:10 +0200582 strip_ns_hdr(msg);
583
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200584 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
585 if (rc < 0)
586 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
587
588 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200589}
590
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200591static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type)
Harald Welte36f98d92011-02-06 13:09:29 +0100592{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200593 struct gbproxy_peer *peer;
Harald Welte36f98d92011-02-06 13:09:29 +0100594
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200595 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +0100596 if (!peer) {
597 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
598 ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200599 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +0100600 return -ENOENT;
601 }
602
603 switch (pdu_type) {
604 case BSSGP_PDUT_BVC_BLOCK_ACK:
605 peer->blocked = 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200606 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100607 break;
608 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
609 peer->blocked = 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200610 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100611 break;
612 default:
613 break;
614 }
615 return 0;
616}
617
Harald Welte9f75c352010-04-30 20:26:32 +0200618/* Send a message to a peer identified by ptp_bvci but using ns_bvci
619 * in the NS hdr */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200620static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci,
Harald Welte9f75c352010-04-30 20:26:32 +0200621 uint16_t ns_bvci)
622{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200623 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +0200624
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200625 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200626 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200627 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
Harald Welte1c77c6e2010-05-03 21:37:11 +0200628 ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200629 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte9f75c352010-04-30 20:26:32 +0200630 return -ENOENT;
Harald Welte1c77c6e2010-05-03 21:37:11 +0200631 }
Harald Welte9f75c352010-04-30 20:26:32 +0200632
Harald Welte69619e32010-05-03 19:05:10 +0200633 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200634}
635
Harald Welteb1fd9022012-06-17 12:16:31 +0800636int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
637{
638 return 0;
639}
640
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200641/* Receive an incoming PTP message from a BSS-side NS-VC */
642static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg,
643 struct msgb *msg, uint16_t nsei,
644 uint16_t nsvci, uint16_t ns_bvci)
645{
646 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200647 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
648 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200649 int rc;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200650
651 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
652
653 if (peer)
654 check_peer_nsei(peer, nsei);
655
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200656 rc = gbprox_process_bssgp_ul(cfg, msg, peer);
657 if (!rc)
658 return 0;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200659
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200660 switch (pdu_type) {
661 case BSSGP_PDUT_FLOW_CONTROL_BVC:
662 if (!cfg->route_to_sgsn2)
663 break;
664
665 /* Send a copy to the secondary SGSN */
666 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
667 break;
668 default:
669 break;
670 }
671
672
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200673 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200674}
675
676/* Receive an incoming PTP message from a SGSN-side NS-VC */
677static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg,
678 struct msgb *msg, uint16_t nsei,
679 uint16_t nsvci, uint16_t ns_bvci)
680{
681 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200682 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
683 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200684
685 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
686
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200687 /* Send status messages before patching */
688
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200689 if (!peer) {
690 LOGP(DGPRS, LOGL_INFO, "Didn't find peer for "
691 "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n",
692 ns_bvci, nsvci, nsei);
693 rate_ctr_inc(&cfg->ctrg->
694 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
695 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
696 &ns_bvci, msg);
697 }
698
699 if (peer->blocked) {
700 LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for "
701 "blocked BVCI=%u via NSVC=%u/NSEI=%u\n",
702 ns_bvci, nsvci, nsei);
703 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200704 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200705 }
706
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200707 switch (pdu_type) {
708 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
709 case BSSGP_PDUT_BVC_BLOCK_ACK:
710 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
711 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
712 /* Hide ACKs from the secondary SGSN, the primary SGSN
713 * is responsible to send them. */
714 return 0;
715 break;
716 default:
717 break;
718 }
719
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200720 /* Optionally patch the message */
721 gbprox_process_bssgp_dl(cfg, msg, peer);
722
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200723 return gbprox_relay2peer(msg, peer, ns_bvci);
724}
725
Harald Welte9f75c352010-04-30 20:26:32 +0200726/* Receive an incoming signalling message from a BSS-side NS-VC */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200727static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg,
728 struct msgb *msg, uint16_t nsei,
Harald Welte9f75c352010-04-30 20:26:32 +0200729 uint16_t ns_bvci)
730{
Harald Welteca3620a2010-05-03 16:30:59 +0200731 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200732 struct tlv_parsed tp;
733 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200734 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200735 struct gbproxy_peer *from_peer = NULL;
Harald Welte70f38d22010-05-01 12:10:57 +0200736 struct gprs_ra_id raid;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200737 int copy_to_sgsn2 = 0;
Jacob Erlbeck59300642014-09-08 09:04:01 +0200738 int rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200739
Harald Weltec471d3d2011-02-06 17:13:12 +0100740 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200741 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200742 nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200743 return -EINVAL;
744 }
745
746 /* we actually should never see those two for BVCI == 0, but double-check
747 * just to make sure */
748 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
749 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200750 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200751 "signalling\n", nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200752 return -EINVAL;
753 }
754
755 bssgp_tlv_parse(&tp, bgph->data, data_len);
756
757 switch (pdu_type) {
758 case BSSGP_PDUT_SUSPEND:
759 case BSSGP_PDUT_RESUME:
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200760 /* We implement RAI snooping during SUSPEND/RESUME, since it
761 * establishes a relationsip between BVCI/peer and the routeing
762 * area identification. The snooped information is then used
763 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
764 * BSSGP */
Harald Welte9f75c352010-04-30 20:26:32 +0200765 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
766 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200767 from_peer = gbproxy_peer_by_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200768 if (!from_peer)
769 goto err_no_peer;
Harald Welte1174c082010-05-12 00:07:29 +0200770 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
771 sizeof(from_peer->ra));
Harald Welte7fc98222010-05-11 10:15:42 +0200772 gsm48_parse_ra(&raid, from_peer->ra);
Harald Welte4cf12e92010-05-13 14:14:56 +0200773 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200774 "RAI snooping: RAI %u-%u-%u-%u behind BVCI=%u\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200775 nsei, raid.mcc, raid.mnc, raid.lac,
776 raid.rac , from_peer->bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200777 /* FIXME: This only supports one BSS per RA */
778 break;
Harald Welte44c48302010-05-03 19:22:32 +0200779 case BSSGP_PDUT_BVC_RESET:
780 /* If we receive a BVC reset on the signalling endpoint, we
781 * don't want the SGSN to reset, as the signalling endpoint
782 * is common for all point-to-point BVCs (and thus all BTS) */
783 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200784 uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte72953b82010-05-12 00:20:41 +0200785 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200786 nsei, bvci);
Harald Welte44c48302010-05-03 19:22:32 +0200787 if (bvci == 0) {
788 /* FIXME: only do this if SGSN is alive! */
Harald Weltee9ea2692010-05-11 20:20:13 +0200789 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200790 "BVC RESET ACK of BVCI=0\n", nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200791 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200792 nsei, 0, ns_bvci);
Harald Welte1174c082010-05-12 00:07:29 +0200793 }
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200794 from_peer = gbproxy_peer_by_bvci(cfg, bvci);
Harald Welte1174c082010-05-12 00:07:29 +0200795 if (!from_peer) {
Harald Welte1c77c6e2010-05-03 21:37:11 +0200796 /* if a PTP-BVC is reset, and we don't know that
797 * PTP-BVCI yet, we should allocate a new peer */
798 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200799 "BVCI=%u via NSEI=%u\n", bvci, nsei);
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +0200800 from_peer = gbproxy_peer_alloc(cfg, bvci);
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200801 from_peer->nsei = nsei;
Harald Welte44c48302010-05-03 19:22:32 +0200802 }
Jacob Erlbeck64cb9242013-10-24 12:48:56 +0200803
804 if (!check_peer_nsei(from_peer, nsei))
805 from_peer->nsei = nsei;
806
Harald Welte1174c082010-05-12 00:07:29 +0200807 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
808 struct gprs_ra_id raid;
809 /* We have a Cell Identifier present in this
810 * PDU, this means we can extend our local
811 * state information about this particular cell
812 * */
813 memcpy(from_peer->ra,
814 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
815 sizeof(from_peer->ra));
816 gsm48_parse_ra(&raid, from_peer->ra);
817 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200818 "Cell ID %u-%u-%u-%u\n", nsei,
Harald Welte1174c082010-05-12 00:07:29 +0200819 bvci, raid.mcc, raid.mnc, raid.lac,
820 raid.rac);
821 }
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200822 if (cfg->route_to_sgsn2)
823 copy_to_sgsn2 = 1;
Harald Welte44c48302010-05-03 19:22:32 +0200824 }
825 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200826 }
827
Harald Weltee9ea2692010-05-11 20:20:13 +0200828 /* Normally, we can simply pass on all signalling messages from BSS to
829 * SGSN */
Jacob Erlbeck59300642014-09-08 09:04:01 +0200830 rc = gbprox_process_bssgp_ul(cfg, msg, from_peer);
831 if (!rc)
832 return 0;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200833
834 if (copy_to_sgsn2)
835 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
836
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200837 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200838err_no_peer:
Jacob Erlbeckc5085f92013-10-18 13:04:48 +0200839 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200840 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200841 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200842 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200843err_mand_ie:
Harald Welte0a4050c2010-05-11 10:01:17 +0200844 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200845 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200846 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200847 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200848}
849
850/* Receive paging request from SGSN, we need to relay to proper BSS */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200851static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200852 uint32_t nsei, uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200853{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200854 struct gbproxy_peer *peer = NULL;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200855 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte9f75c352010-04-30 20:26:32 +0200856
Harald Welte4cf12e92010-05-13 14:14:56 +0200857 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200858 nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200859 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200860 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte4cf12e92010-05-13 14:14:56 +0200861 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n",
862 bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200863 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte9f75c352010-04-30 20:26:32 +0200864 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200865 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200866 LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +0800867 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200868 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte9f75c352010-04-30 20:26:32 +0200869 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200870 peer = gbproxy_peer_by_lai(cfg, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200871 LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +0800872 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200873 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte9f75c352010-04-30 20:26:32 +0200874 } else
Harald Welte4cf12e92010-05-13 14:14:56 +0200875 LOGPC(DGPRS, LOGL_INFO, "\n");
876
877 if (!peer) {
878 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200879 "unable to route, missing IE\n", nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200880 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
Harald Welte9f75c352010-04-30 20:26:32 +0200881 return -EINVAL;
Harald Welte4cf12e92010-05-13 14:14:56 +0200882 }
883 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200884}
885
Harald Welte0a4050c2010-05-11 10:01:17 +0200886/* Receive an incoming BVC-RESET message from the SGSN */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200887static int rx_reset_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200888 struct msgb *orig_msg,
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200889 struct msgb *msg, struct tlv_parsed *tp,
890 uint32_t nsei, uint16_t ns_bvci)
Harald Welte0a4050c2010-05-11 10:01:17 +0200891{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200892 struct gbproxy_peer *peer;
Harald Welte0a4050c2010-05-11 10:01:17 +0200893 uint16_t ptp_bvci;
894
895 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200896 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200897 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200898 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200899 NULL, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +0200900 }
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200901 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte0a4050c2010-05-11 10:01:17 +0200902
903 if (ptp_bvci >= 2) {
904 /* A reset for a PTP BVC was received, forward it to its
905 * respective peer */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200906 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +0200907 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200908 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200909 nsei, ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200910 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200911 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200912 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200913 &ptp_bvci, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +0200914 }
915 return gbprox_relay2peer(msg, peer, ns_bvci);
916 }
917
918 /* A reset for the Signalling entity has been received
919 * from the SGSN. As the signalling BVCI is shared
920 * among all the BSS's that we multiplex, it needs to
921 * be relayed */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200922 llist_for_each_entry(peer, &cfg->bts_peers, list)
Harald Welte0a4050c2010-05-11 10:01:17 +0200923 gbprox_relay2peer(msg, peer, ns_bvci);
924
925 return 0;
926}
927
Harald Welte9f75c352010-04-30 20:26:32 +0200928/* Receive an incoming signalling message from the SGSN-side NS-VC */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200929static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200930 struct msgb *orig_msg, uint32_t nsei,
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200931 uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200932{
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200933 struct bssgp_normal_hdr *bgph =
934 (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200935 struct tlv_parsed tp;
936 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200937 int data_len;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200938 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +0200939 uint16_t bvci;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200940 struct msgb *msg;
Harald Welte9f75c352010-04-30 20:26:32 +0200941 int rc = 0;
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +0200942 int cause;
Harald Welte9f75c352010-04-30 20:26:32 +0200943
Harald Weltec471d3d2011-02-06 17:13:12 +0100944 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200945 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200946 "signalling\n", nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +0200947 /* FIXME: Send proper error message */
Harald Welte9f75c352010-04-30 20:26:32 +0200948 return -EINVAL;
949 }
950
951 /* we actually should never see those two for BVCI == 0, but double-check
952 * just to make sure */
953 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
954 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200955 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200956 "signalling\n", nsei);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200957 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200958 }
959
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200960 msg = gprs_msgb_copy(orig_msg, "rx_sig_from_sgsn");
961 gbprox_process_bssgp_dl(cfg, msg, NULL);
962 /* Update message info */
963 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
964 data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +0200965 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
966
967 switch (pdu_type) {
Harald Welte0a4050c2010-05-11 10:01:17 +0200968 case BSSGP_PDUT_BVC_RESET:
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200969 rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +0200970 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200971 case BSSGP_PDUT_BVC_RESET_ACK:
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200972 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
973 break;
974 /* fall through */
975 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9f75c352010-04-30 20:26:32 +0200976 /* simple case: BVCI IE is mandatory */
977 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
978 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200979 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200980 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200981 break;
982 case BSSGP_PDUT_PAGING_PS:
983 case BSSGP_PDUT_PAGING_CS:
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200984 /* process the paging request (LAI/RAI lookup) */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200985 rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200986 break;
987 case BSSGP_PDUT_STATUS:
Harald Welte0a4050c2010-05-11 10:01:17 +0200988 /* Some exception has occurred */
Harald Welte44c48302010-05-03 19:22:32 +0200989 LOGP(DGPRS, LOGL_NOTICE,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200990 "NSEI=%u(SGSN) BSSGP STATUS ", nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +0200991 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
992 LOGPC(DGPRS, LOGL_NOTICE, "\n");
993 goto err_mand_ie;
994 }
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +0200995 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
Harald Welte0a4050c2010-05-11 10:01:17 +0200996 LOGPC(DGPRS, LOGL_NOTICE,
997 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +0200998 bssgp_cause_str(cause));
Harald Welte0a4050c2010-05-11 10:01:17 +0200999 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001000 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1001 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%u\n", bvci);
1002
1003 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI)
1004 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001005 } else
1006 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welte9f75c352010-04-30 20:26:32 +02001007 break;
1008 /* those only exist in the SGSN -> BSS direction */
1009 case BSSGP_PDUT_SUSPEND_ACK:
1010 case BSSGP_PDUT_SUSPEND_NACK:
1011 case BSSGP_PDUT_RESUME_ACK:
1012 case BSSGP_PDUT_RESUME_NACK:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001013 /* RAI IE is mandatory */
Harald Welte9f75c352010-04-30 20:26:32 +02001014 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1015 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001016 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte9f75c352010-04-30 20:26:32 +02001017 if (!peer)
1018 goto err_no_peer;
Harald Welte69619e32010-05-03 19:05:10 +02001019 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001020 break;
Harald Welte36f98d92011-02-06 13:09:29 +01001021 case BSSGP_PDUT_BVC_BLOCK_ACK:
1022 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1023 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1024 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001025 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte36f98d92011-02-06 13:09:29 +01001026 if (bvci == 0) {
1027 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001028 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
Harald Welte36f98d92011-02-06 13:09:29 +01001029 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
1030 /* should we send STATUS ? */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001031 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001032 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +01001033 } else {
1034 /* Mark BVC as (un)blocked */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001035 block_unblock_peer(cfg, bvci, pdu_type);
Harald Welte36f98d92011-02-06 13:09:29 +01001036 }
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001037 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +01001038 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001039 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte44c48302010-05-03 19:22:32 +02001040 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001041 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001042 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001043 ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001044 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001045 break;
1046 default:
Harald Welte72953b82010-05-12 00:20:41 +02001047 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n",
1048 pdu_type);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001049 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001050 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001051 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001052 break;
1053 }
1054
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001055 msgb_free(msg);
1056
Harald Welte9f75c352010-04-30 20:26:32 +02001057 return rc;
1058err_mand_ie:
Harald Welte1c77c6e2010-05-03 21:37:11 +02001059 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001060 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001061 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001062 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001063 msgb_free(msg);
1064 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001065err_no_peer:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001066 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001067 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001068 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001069 msgb_free(msg);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001070 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001071}
1072
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001073static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei)
1074{
1075 return nsei == cfg->nsip_sgsn_nsei ||
1076 (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei);
1077}
1078
Harald Welte9f75c352010-04-30 20:26:32 +02001079/* Main input function for Gb proxy */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001080int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei,
1081 uint16_t ns_bvci, uint16_t nsvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001082{
Harald Welte672f5c42010-05-03 18:54:58 +02001083 int rc;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001084 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001085
1086 /* Only BVCI=0 messages need special treatment */
1087 if (ns_bvci == 0 || ns_bvci == 1) {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001088 if (remote_end_is_sgsn)
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001089 rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001090 else
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001091 rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci);
Harald Welte672f5c42010-05-03 18:54:58 +02001092 } else {
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +02001093 /* All other BVCI are PTP */
1094 if (remote_end_is_sgsn)
1095 rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei, nsvci,
1096 ns_bvci);
1097 else
1098 rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei, nsvci,
1099 ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001100 }
1101
Harald Welte672f5c42010-05-03 18:54:58 +02001102 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +02001103}
Harald Welte85801d02010-05-11 05:49:43 +02001104
Harald Welte1ccbf442010-05-14 11:53:08 +00001105int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi)
1106{
1107 struct gprs_nsvc *nsvc;
1108
1109 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
1110 if (!nsvc->persistent)
1111 continue;
1112 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1113 }
1114 return 0;
1115}
1116
Harald Weltec1c1dd22010-05-11 06:34:24 +02001117/* Signal handler for signals from NS layer */
1118int gbprox_signal(unsigned int subsys, unsigned int signal,
1119 void *handler_data, void *signal_data)
1120{
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001121 struct gbproxy_config *cfg = handler_data;
Harald Weltec1c1dd22010-05-11 06:34:24 +02001122 struct ns_signal_data *nssd = signal_data;
1123 struct gprs_nsvc *nsvc = nssd->nsvc;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001124 struct gbproxy_peer *peer;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001125 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsvc->nsei);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001126
Harald Weltea6a20b42012-06-16 16:40:42 +08001127 if (subsys != SS_L_NS)
Harald Weltec1c1dd22010-05-11 06:34:24 +02001128 return 0;
1129
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001130 if (signal == S_NS_RESET && remote_end_is_sgsn) {
Harald Weltef69c0592010-05-11 18:29:44 +02001131 /* We have received a NS-RESET from the NSEI and NSVC
1132 * of the SGSN. This might happen with SGSN that start
1133 * their own NS-RESET procedure without waiting for our
1134 * NS-RESET */
1135 nsvc->remote_end_is_sgsn = 1;
1136 }
1137
Harald Welteb778d2c2010-05-12 13:28:25 +00001138 if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) {
1139 LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, "
1140 "re-starting RESET procedure\n");
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001141 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001142 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
Harald Weltee6599ee2012-06-17 12:25:53 +08001143 gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr,
1144 nsvc->nsei, nsvc->nsvci);
Harald Welteb778d2c2010-05-12 13:28:25 +00001145 }
1146
Harald Welte5e106d72011-02-06 16:33:29 +01001147 if (!nsvc->remote_end_is_sgsn) {
1148 /* from BSS to SGSN */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001149 peer = gbproxy_peer_by_nsei(cfg, nsvc->nsei);
Harald Welte5e106d72011-02-06 16:33:29 +01001150 if (!peer) {
1151 LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer "
1152 "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei,
1153 nsvc->nsvci);
1154 return 0;
1155 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001156 switch (signal) {
1157 case S_NS_RESET:
Harald Weltec1c1dd22010-05-11 06:34:24 +02001158 case S_NS_BLOCK:
Harald Welte5e106d72011-02-06 16:33:29 +01001159 if (!peer->blocked)
1160 break;
1161 LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from "
1162 "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n",
1163 nsvc->nsei, nsvc->nsvci);
1164 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei,
1165 peer->bvci, 0);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001166 break;
Harald Welte5e106d72011-02-06 16:33:29 +01001167 }
1168 } else {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001169 /* Forward this message to all NS-VC to BSS */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001170 struct gprs_ns_inst *nsi = cfg->nsi;
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001171 struct gprs_nsvc *next_nsvc;
1172
1173 llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) {
1174 if (next_nsvc->remote_end_is_sgsn)
1175 continue;
1176
1177 /* Note that the following does not start the full
1178 * procedures including timer based retransmissions. */
Harald Welte5e106d72011-02-06 16:33:29 +01001179 switch (signal) {
1180 case S_NS_RESET:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001181 gprs_ns_tx_reset(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001182 break;
1183 case S_NS_BLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001184 gprs_ns_tx_block(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001185 break;
1186 case S_NS_UNBLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001187 gprs_ns_tx_unblock(next_nsvc);
Harald Welte5e106d72011-02-06 16:33:29 +01001188 break;
1189 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001190 }
1191 }
1192 return 0;
1193}
1194
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001195void gbprox_reset(struct gbproxy_config *cfg)
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001196{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001197 struct gbproxy_peer *peer, *tmp;
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001198
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001199 llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list)
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +02001200 gbproxy_peer_free(peer);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001201
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001202 rate_ctr_group_free(cfg->ctrg);
1203 gbproxy_init_config(cfg);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001204}
1205
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001206int gbproxy_init_config(struct gbproxy_config *cfg)
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001207{
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001208 struct timespec tp;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001209 INIT_LLIST_HEAD(&cfg->bts_peers);
Holger Hans Peter Freythera7027a02014-08-04 11:19:56 +02001210 cfg->ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0);
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001211 clock_gettime(CLOCK_REALTIME, &tp);
1212 cfg->bss_ptmsi_state = tp.tv_sec + tp.tv_nsec;
1213 cfg->sgsn_tlli_state = tp.tv_sec - tp.tv_nsec;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001214 return 0;
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001215}